Пример #1
0
void DrawImage(char *data, int x, int y, int width, int height)
{
  XImage *xi;

  if (lsx_curwin->toplevel == NULL || data == NULL)
    return;

  xi = XCreateImage(display, DefaultVisual(display, DefaultScreen(display)),
                    8, ZPixmap, 0, data, width, height,
                    XBitmapPad(display),  width);
  if (xi == NULL)
    return;

  XPutImage(display, window, drawgc, xi, 0,0, x,y,  xi->width,xi->height);

  XFree((char *)xi);
}
Пример #2
0
static void draw_gradient(Widget w, Pixmap scribble)
{
	MwAnimatorWidget aw = (MwAnimatorWidget)w;
	char *bgrad = aw->animator.bgrad;
	Display *dpy = XtDisplay(w);
	int screen = DefaultScreen(dpy);
	Visual *visual = DefaultVisual(dpy, screen);
	int bitmap_pad = XBitmapPad(dpy);
	GC gc = aw->animator.gc;
	int bytes_per_line = 0;
	XColor color;
	unsigned long pixel;
	int format = ZPixmap;
	int offset = 0;
	char *data = NULL;
	int width, height, depth;
	int x, y;
	int bw, bh, bc, ba, bz, bn;
	char bc1[100], bc2[100];
	XColor top, bot;

	unsigned int neww = aw->core.width;
	unsigned int newh = aw->core.height;
	unsigned int ox, oy;
	unsigned int oldw = width, oldh = height;

	/* we're going from black at the top to blue
	   at the bottom */
	if (bgrad == NULL) bgrad = "";
	if (aw->animator.ximage == NULL) {
		XImage *img;
		bn = sscanf(bgrad, "%d %d %d %d %d %s %s",
			&bw, &bh, &bc, &ba, &bz, bc1, bc2);
		if (bn < 0) bn = 0;
		switch (bn) {
		case 0:	bw = 100;
		case 1:	bh = 100;
		case 2: bc = 128;
		case 3: ba = 0;
		case 4:	bz = 1;
		case 5: strcpy(bc1, "black");
		case 6:	strcpy(bc2, "blue");
		default: ;	/* catches e.g. -1 */
		}

		width = aw->core.width*bw/100;
		height = aw->core.height*bh/100;
		depth = aw->core.depth;
		img = aw->animator.ximage = XCreateImage(dpy,
			visual, depth, format, offset, data,
			width, height, bitmap_pad, bytes_per_line);
		img->data = MwMalloc(img->bytes_per_line*img->height);
		MwAllocNamedColor(dpy, bc1, &top);
		MwAllocNamedColor(dpy, bc2, &bot);
		for (y = 0; y < height; y++) {
			color.red = (int)top.red
				+((int)bot.red-top.red)*y/height;
			color.green = (int)top.green
				+((int)bot.green-top.green)*y/height;
			color.blue = (int)top.blue
				+((int)bot.blue-top.blue)*y/height;
			MwAllocColor(dpy, None, &color);
			pixel = color.pixel;
			for (x = 0; x < width; x++ ) {
				XPutPixel(img, x, y, pixel);
			}
		}
		aw->animator.ximage = XCreateImage(dpy,
			visual, depth, format, offset, data,
			aw->core.width, aw->core.height,
			bitmap_pad, bytes_per_line);
		aw->animator.ximage->data = MwMalloc(
					aw->animator.ximage->bytes_per_line
					*aw->animator.ximage->height);

		neww = aw->core.width;
		newh = aw->core.height;
		oldw = width, oldh = height;

		if (bz) {	/* zoom */
			/* snarfed from Image widget */
			for (y = 0; y < newh; y++) {
				oy = y*oldh/newh;
				for (x = 0; x < neww; x++) {
					ox = x*oldw/neww;
					pixel = XGetPixel(img, ox, oy);
					XPutPixel(aw->animator.ximage,
						x, y, pixel);
				}
			}
		} else {	/* tile */
			for (y = 0; y < newh; y++) {
				oy = y % oldh;
				for (x = 0; x < neww; x++) {
					ox = x % oldw;
					pixel = XGetPixel(img, ox, oy);
					XPutPixel(aw->animator.ximage,
						x, y, pixel);
				}
			}
		}
		XDestroyImage(img);
	}
	XPutImage(dpy, scribble, gc, aw->animator.ximage,
		0, 0, 0, 0, aw->core.width, aw->core.height);
}
Пример #3
0
XImage *
read_ppm_file(Display *disp, Colormap cmap, int depth, IOSTREAM *fd)
{ XImage *img;
  long here = Stell(fd);
  int c;
  int fmt, encoding;
  int width, height, bytes_per_line, scale=0;
  char *data;
  int allocdepth;
  int pad = XBitmapPad(disp);
  Visual *v = DefaultVisual(disp, DefaultScreen(disp));

  ncolours = nmapped = nfailed = 0;	/* statistics */
  assert(pad%8 == 0);

  if ( (c=Sgetc(fd)) != 'P' )
  { Sungetc(c, fd);
    return NULL;
  }

  if ( !cmap )
    cmap = DefaultColormap(disp, DefaultScreen(disp));

  c = Sgetc(fd);
  if ( c < '1' || c > '9' )
    goto errout;
  c -= '0';
  fmt      = ((c - 1) % 3) + 1;
  encoding = c - fmt;

  width = getNum(fd);
  height = getNum(fd);

  if ( fmt == PNM_PBM )
  { depth = 1;
  } else
  { scale = getNum(fd);
    if ( !depth )
      depth = DefaultDepth(disp, DefaultScreen(disp));
  }

  if ( width < 0 || height < 0 || scale < 0 )
    goto errout;

  allocdepth = (depth >= 24 ? 32 : depth);
  bytes_per_line = roundup((width*allocdepth+7)/8, pad/8);
  data = (char *)pceMalloc(height * bytes_per_line);

  img = XCreateImage(disp,
		     v,
		     depth,
		     fmt == PNM_PBM ? XYBitmap : ZPixmap,
		     0,
		     data,
		     width, height,
		     pad, bytes_per_line);
  if ( !img )
  { perror("XCreateImage");
    pceFree(data);
    goto errout;
  }
  img->bits_per_pixel = depth;

  switch(encoding)
  { int x, y;

    case PNM_ASCII:
    { switch(fmt)
      { case PNM_PBM:
	  for(y=0; y<height; y++)
	  { for(x=0; x<width; x++)
	    { int value = getNum(fd);

	      if ( value < 0 || value > 1 )
		goto errout;

	      XPutPixel(img, x, y, value);
	    }
	  }
	  break;
	case PNM_PGM:
	{ Table t = newTable(64);

	  for(y=0; y<height; y++)
	  { for(x=0; x<width; x++)
	    { int g = getNum(fd);
	      unsigned long pixel;

	      if ( g < 0 || g > scale )
		goto errout;
	      if ( scale != 255 )
		g = rescale(g, scale, 255);

	      pixel = colourPixel(disp, depth, cmap, t, g, g, g);
	      XPutPixel(img, x, y, pixel);
	    }
	  }
	  freeTable(t);

	  break;
	}
	case PNM_PPM:
	{ Table t = newTable(64);

	  for(y=0; y<height; y++)
	  { for(x=0; x<width; x++)
	    { int r = getNum(fd);
	      int g = getNum(fd);
	      int b = getNum(fd);
	      unsigned long pixel;

	      if ( r < 0 || r > scale ||
		   g < 0 || g > scale ||
		   b < 0 || b > scale )
		goto errout;

	      if ( scale != 255 )
	      { r = rescale(r, scale, 255);
		g = rescale(g, scale, 255);
		b = rescale(b, scale, 255);
	      }

	      pixel = colourPixel(disp, depth, cmap, t, r, g, b);

	      XPutPixel(img, x, y, pixel);
	    }
	  }
	  freeTable(t);

	  break;
	}
	break;
      }
      break;
    }
    case PNM_RAWBITS:
    { switch(fmt)
      { case PNM_PBM:
	{ int byte = 0;
	  int bit = 0;

	  for(y=0; y<height; y++)
	  { for(x=0; x<width; x++)
	    { if ( !bit )
	      { byte = Sgetc(fd);
		bit = 8;
	      }

	      bit--;
	      XPutPixel(img, x, y, (byte & (1<<bit)) ? 1 : 0);
	    }
	    bit = 0;			/* scanlines are byte-aligned */
	  }
	  break;
	}
	case PNM_PGM:
	{ Table t = newTable(64);

	  for(y=0; y<height; y++)
	  { for(x=0; x<width; x++)
	    { int g;
	      unsigned long pixel;

	      if ( Sfeof(fd) || (g=Sgetc(fd)) > scale )
		goto errout;
	      if ( scale != 255 )
		g = rescale(g, scale, 255);

	      pixel = colourPixel(disp, depth, cmap, t, g, g, g);
	      XPutPixel(img, x, y, pixel);
	    }
	  }
	  freeTable(t);

	  break;
	}
	case PNM_PPM:
	{ Table t = newTable(64);

	  for(y=0; y<height; y++)
	  { for(x=0; x<width; x++)
	    { int r, g, b;
	      unsigned long pixel;

	      if ( Sfeof(fd) ||
		   (r=Sgetc(fd)) > scale ||
		   (g=Sgetc(fd)) > scale ||
		   (b=Sgetc(fd)) > scale )
		goto errout;

	      if ( scale != 255 )
	      { r = rescale(r, scale, 255);
		g = rescale(g, scale, 255);
		b = rescale(b, scale, 255);
	      }

	      pixel = colourPixel(disp, depth, cmap, t, r, g, b);

	      XPutPixel(img, x, y, pixel);
	    }
	  }
	  freeTable(t);

	  break;
	}
	break;
      }
      break;
    }
    case PNM_RUNLEN:
    { int rlen = 0;
      unsigned long cpixel = NOPIXEL;

      switch(fmt)
      { case PNM_PGM:
	{ Table t = newTable(64);

	  DEBUG(NAME_pnm, Cprintf("Reading runlength encoded graymap\n"));

	  for(y=0; y<height; y++)
	  { for(x=0; x<width; x++)
	    { if ( rlen-- > 0 )
	      { XPutPixel(img, x, y, cpixel);
	      } else
	      { int g;

		if ( (g=Sgetc(fd)) > scale ||
		     (rlen = Sgetc(fd)) == EOF )
		  goto errout;
		rlen &= 0xff;
		if ( scale != 255 )
		  g = rescale(g, scale, 255);

		cpixel = colourPixel(disp, depth, cmap, t, g, g, g);
		XPutPixel(img, x, y, cpixel);
		rlen--;
	      }
	    }
	  }
	  freeTable(t);

	  break;
	}
	case PNM_PPM:
	{ Table t = newTable(64);

	  for(y=0; y<height; y++)
	  { for(x=0; x<width; x++)
	    { if ( rlen-- > 0 )
	      { XPutPixel(img, x, y, cpixel);
	      } else
	      { int r, g, b;

		if ( (r=Sgetc(fd)) > scale ||
		     (g=Sgetc(fd)) > scale ||
		     (b=Sgetc(fd)) > scale ||
		     (rlen = Sgetc(fd)) == EOF )
		  goto errout;

		rlen &= 0xff;
		if ( scale != 255 )
		{ r = rescale(r, scale, 255);
		  g = rescale(g, scale, 255);
		  b = rescale(b, scale, 255);
		}

		cpixel = colourPixel(disp, depth, cmap, t, r, g, b);

		XPutPixel(img, x, y, cpixel);
		rlen--;
	      }
	    }
	  }
	  freeTable(t);

	  break;
	}
      }
    }
  }

  DEBUG(NAME_ppm,
	Cprintf("PNM: Converted %dx%dx%d image, %d colours (%d mapped, %d failed)\n",
		width, height, depth, ncolours, nmapped, nfailed));

  return img;

errout:
  DEBUG(NAME_ppm,
	Cprintf("PNM: Format error, index = %d\n", Stell(fd)));
  Sseek(fd, here, SEEK_SET);
  return NULL;
}
Пример #4
0
static void
CreateDefaultPixmaps(XmLTreeWidget t)
	{
	Display *dpy;
	Window win;
	XWindowAttributes attr;
	XColor color;
	Pixmap pixmap;
	Pixel pixel;
	XImage *image;
	int i, x, y;
	enum { white = 0, black = 1, yellow = 2, gray = 3 };
	static unsigned short colors[4][3] =
		{
			{ 65535, 65535, 65535 },
			{ 0,         0,     0 },
			{ 57344, 57344,     0 },
			{ 32768, 32768, 32768 },
		};
	static unsigned char fileMask_bits[] =
		{
		0xf8, 0x0f, 0xfc, 0x1f, 0xfc, 0x3f, 0xfc, 0x7f, 0xfc, 0x7f, 0xfc, 0x7f,
		0xfc, 0x7f, 0xfc, 0x7f, 0xfc, 0x7f, 0xfc, 0x7f, 0xfc, 0x7f, 0xfc, 0x7f,
		0xfc, 0x7f, 0xfc, 0x7f, 0xfc, 0x7f, 0xf8, 0x7f
		};
	static unsigned char folderMask_bits[] =
		{
		0x00, 0x00, 0x00, 0x00, 0xe0, 0x07, 0xf0, 0x0f, 0xfc, 0x3f, 0xfe, 0x7f,
		0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xff,
		0xfe, 0xff, 0xfe, 0xff, 0xfc, 0xff, 0xf8, 0x7f
		};
	static unsigned char folderOpenMask_bits[] =
		{
		0x00, 0x00, 0x00, 0x00, 0xe0, 0x07, 0xf0, 0x0f, 0xfc, 0x3f, 0xfe, 0x7f,
		0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xfe, 0xff,
		0xfc, 0xff, 0xfc, 0xff, 0xf8, 0xff, 0xf0, 0x7f
		};
	static char icons[3][16][16] =
		{
			{
			"   GGGGGGGGG    ",
			"  GWWWWWWWWKK   ",
			"  GWWWWWWWWKWK  ",
			"  GWWWWWWWWKKKK ",
			"  GWWWWWWWWWWGK ",
			"  GWGGGGGGGWWGK ",
			"  GWWKKKKKKKWGK ",
			"  GWWWWWWWWWWGK ",
			"  GWGGGGGGGWWGK ",
			"  GWWKKKKKKKWGK ",
			"  GWWWWWWWWWWGK ",
			"  GWGGGGGGGWWGK ",
			"  GWWKKKKKKKWGK ",
			"  GWWWWWWWWWWGK ",
			"  GGGGGGGGGGGGK ",
			"   KKKKKKKKKKKK ",
			},
			{
			"                ",
			"                ",
			"     GGGGGG     ",
			"    GYYYYYYG    ",
			"  GGYYYYYYYYGG  ",
			" GWWWWWWWWWWWYG ",
			" GWYYYYYYYYYYYGK",
			" GWYYYYYYYYYYYGK",
			" GWYYYYYYYYYYYGK",
			" GWYYYYYYYYYYYGK",
			" GWYYYYYYYYYYYGK",
			" GWYYYYYYYYYYYGK",
			" GWYYYYYYYYYYYGK",
			" GYYYYYYYYYYYYGK",
			"  GGGGGGGGGGGGKK",
			"   KKKKKKKKKKKK ",
			},
			{
			"                ",
			"                ",
			"     GGGGGG     ",
			"    GYYYYYYG    ",
			"  GGYYYYYYYYGG  ",
			" GYYYYYYYYYYYYG ",
			" GYYYYYYYYYYYYGK",
			"GGGGGGGGGGGYYYGK",
			"GWWWWWWWWWYKYYGK",
			"GWYYYYYYYYYKYYGK",
			" GYYYYYYYYYYKYGK",
			" GYYYYYYYYYYKYGK",
			"  GYYYYYYYYYYKGK",
			"  GYYYYYYYYYYKGK",
			"   GGGGGGGGGGGKK",
			"    KKKKKKKKKKK ",
			},
		};

	dpy = XtDisplay(t);
	win = XtWindow(t);
	XGetWindowAttributes(dpy, win, &attr);
	t->tree.filePixmask = XCreatePixmapFromBitmapData(dpy, win,
		(char *)fileMask_bits, 16, 16, 1L, 0L, 1);
	t->tree.folderPixmask = XCreatePixmapFromBitmapData(dpy, win,
		(char *)folderMask_bits, 16, 16, 1L, 0L, 1);
	t->tree.folderOpenPixmask = XCreatePixmapFromBitmapData(dpy, win,
		(char *)folderOpenMask_bits, 16, 16, 1L, 0L, 1);
	for (i = 0; i < 4; i++)
		{
		color.red = colors[i][0];
		color.green = colors[i][1];
		color.blue = colors[i][2];
		color.flags = DoRed | DoGreen | DoBlue;
		if (XAllocColor(dpy, attr.colormap, &color))
			t->tree.pixColors[i] = color.pixel;
		else
			{
			color.flags = 0;
			XAllocColor(dpy, attr.colormap, &color);
			t->tree.pixColors[i] = color.pixel;
			}
		}
	image = XCreateImage(dpy, attr.visual, attr.depth, ZPixmap, 0,
		NULL, 16, 16, XBitmapPad(dpy), 0);
	if (!image)
		XmLWarning((Widget)t,
			"CreateDefaultPixmaps() - can't allocate image");
	else
		image->data = (char *)malloc(image->bytes_per_line * 16);
	for (i = 0; i < 3; i++)
		{
		pixmap = XCreatePixmap(dpy, win, 16, 16, attr.depth);
		for (x = 0; x < 16; x++)
			for (y = 0; y < 16; y++)
				{
				switch (icons[i][y][x])
					{
					case ' ':
						pixel = t->core.background_pixel;
						break;
					case 'W':
						pixel = t->tree.pixColors[white];
						break;
					case 'K':
						pixel = t->tree.pixColors[black];
						break;
					case 'Y':
						pixel = t->tree.pixColors[yellow];
						break;
					case 'G':
						pixel = t->tree.pixColors[gray];
						break;
					}
				XPutPixel(image, x, y, pixel);
				}
		if (image)
			XPutImage(dpy, pixmap, t->grid.gc, image, 0, 0, 0, 0, 16, 16);
		if (i == 0)
			t->tree.filePixmap = pixmap;
		else if (i == 1)
			t->tree.folderPixmap = pixmap;
		else
			t->tree.folderOpenPixmap = pixmap;
		}
	if (image)
		XDestroyImage(image);
	t->tree.defaultPixmapsCreated = 1;
	}