Ejemplo n.º 1
0
FvwmPicture *LoadPicture(Display *dpy,Window Root,char *path, int color_limit)
{
  int l;
  FvwmPicture *p;
#ifdef XPM
  XpmAttributes xpm_attributes;
  int rc;
  XpmImage	my_image = {0};
#endif

  p=(FvwmPicture*)safemalloc(sizeof(FvwmPicture));
  p->count=1;
  p->name=path;
  p->next=NULL;

#ifdef XPM
  /* Try to load it as an X Pixmap first */
  xpm_attributes.colormap=PictureCMap;
  xpm_attributes.closeness=40000; /* Allow for "similar" colors */
  xpm_attributes.valuemask=
    XpmSize | XpmReturnPixels | XpmColormap | XpmCloseness;

  rc =XpmReadFileToXpmImage(path, &my_image, NULL);
  if (rc == XpmSuccess) {
    color_reduce_pixmap(&my_image, color_limit);
    rc = XpmCreatePixmapFromXpmImage(dpy, Root, &my_image,
                                     &p->picture,&p->mask,
                                     &xpm_attributes);
    if (rc == XpmSuccess) {
      p->width = my_image.width;
      p->height = my_image.height;
      XpmFreeXpmImage(&my_image);
      p->depth = DefaultDepthOfScreen(DefaultScreenOfDisplay(dpy));
      return p;
    }
    XpmFreeXpmImage(&my_image);
  }
#endif

  /* If no XPM support, or XPM loading failed, try bitmap */
  if(XReadBitmapFile(dpy,Root,path,&p->width,&p->height,&p->picture,&l,&l)
     == BitmapSuccess)
    {
      p->depth = 0;
      p->mask = None;
      return p;
    }

  free(p);
  return NULL;
}
Ejemplo n.º 2
0
int
XpmWriteFileFromImage(
    Display		*display,
    char		*filename,
    XImage		*image,
    XImage		*shapeimage,
    XpmAttributes	*attributes)
{
    XpmImage xpmimage;
    XpmInfo info;
    int ErrorStatus;

    /* create an XpmImage from the image */
    ErrorStatus = XpmCreateXpmImageFromImage(display, image, shapeimage,
                  &xpmimage, attributes);
    if (ErrorStatus != XpmSuccess)
        return (ErrorStatus);

    /* write the file from the XpmImage */
    if (attributes) {
        xpmSetInfo(&info, attributes);
        ErrorStatus = XpmWriteFileFromXpmImage(filename, &xpmimage, &info);
    } else
        ErrorStatus = XpmWriteFileFromXpmImage(filename, &xpmimage, NULL);

    /* free the XpmImage */
    XpmFreeXpmImage(&xpmimage);

    return (ErrorStatus);
}
Ejemplo n.º 3
0
int
XpmReadFileToData(
    const char	  *filename,
    char	***data_return)
{
    XpmImage image;
    XpmInfo info;
    int ErrorStatus;

    info.valuemask = XpmReturnComments | XpmReturnExtensions;

    /*
     * initialize return value
     */
    if (data_return)
	*data_return = NULL;

    ErrorStatus = XpmReadFileToXpmImage(filename, &image, &info);
    if (ErrorStatus != XpmSuccess)
	return (ErrorStatus);

    ErrorStatus =
	XpmCreateDataFromXpmImage(data_return, &image, &info);

    XpmFreeXpmImage(&image);
    XpmFreeXpmInfo(&info);

    return (ErrorStatus);
}
Ejemplo n.º 4
0
int
XpmCreateImageFromBuffer(Display *display, char *buffer, XImage **image_return,
			 XImage **shapeimage_return, XpmAttributes *attributes)
{
    XpmImage image;
    XpmInfo info;
    int ErrorStatus;

    /* create an XpmImage from the buffer */
    if (attributes) {
	xpmInitAttributes(attributes);
	xpmSetInfoMask(&info, attributes);
	ErrorStatus = XpmCreateXpmImageFromBuffer(buffer, &image, &info);
    } else
	ErrorStatus = XpmCreateXpmImageFromBuffer(buffer, &image, NULL);

    if (ErrorStatus != XpmSuccess)
	return (ErrorStatus);

    /* create the related ximages */
    ErrorStatus = XpmCreateImageFromXpmImage(display, &image,
					     image_return, shapeimage_return,
					     attributes);
    if (attributes) {
	if (ErrorStatus >= 0)		/* no fatal error */
	    xpmSetAttributes(attributes, &image, &info);
	XpmFreeXpmInfo(&info);
    }
    /* free the XpmImage */
    XpmFreeXpmImage(&image);

    return (ErrorStatus);
}
Ejemplo n.º 5
0
bool LoadXPM( IndexedImg& img, RGBx* palette, const char* filename )
{
    XpmImage image;
    XpmInfo info;
    int r = XpmReadFileToXpmImage( (char*)filename, &image, &info );

    if( r != 0 )
    {
        printf("XpmReadFileToXpmImage() failed (code %d)\n",r);
        return false;
    }


    printf("xpm is %dx%d, %d colours, %d cpp\n",
        image.width,
        image.height,
        image.ncolors,
        image.cpp );


    // install palette
    int i;
    for( i=0; i<image.ncolors; ++i )
    {
        XpmColor const& c = image.colorTable[i];
        printf("%d: '%s'\n", i, c.c_color );
//        palette[i] = RGBx( c.Red, c.Green, c.Blue );
    }

    IndexedImg tmp( image.width, image.height );

    const unsigned int* src = image.data;
    int y;
    for( y=0; y<image.height; ++y )
    {
        int x;
        uint8_t* dest = tmp.Ptr(0,y);
        for( x=0; x<image.width; ++x )
        {
            *dest++ = (uint8_t)*src++;
        }
    }

    img.Copy( tmp );

/*
    GifImageDesc* desc = si[0]

    printf("ltwh: %d,%d,%d,%d\n",
        desc->Left,
        desc->Top,
        desc->Width,
        desc->Height ); 
*/

    XpmFreeXpmImage( &image );

    return true;
}
Ejemplo n.º 6
0
void
scalePixmap (Display * dpy, MyPixmap * src, MyPixmap * dst, int width,
	     int height)
{
  XpmImage xi_src, xi_dst;
  int x, y, sx, sy;
  unsigned int *src_data, *dst_data;

#ifdef DEBUG
  printf ("entering scalePixmap\n");
#endif

  /* don't ask me why but sometimes this function gets _REALLY_ big imagesizes
   * so there must be something totally wrong somewhere... */
  if (width > 20000)
    return;
  if (height > 20000)
    return;
  if (width < 1)
    return;
  if (height < 1)
    return;
  /* I currently don't know exactly but it _IS_ a bug in here */

  XpmCreateXpmImageFromPixmap (dpy, src->pixmap, src->mask, &xi_src, NULL);
  dst->width = width;
  dst->height = height;
  xi_dst.width = width;
  xi_dst.height = height;
  xi_dst.cpp = xi_src.cpp;
  xi_dst.ncolors = xi_src.ncolors;
  xi_dst.colorTable = xi_src.colorTable;
  xi_dst.data = malloc (sizeof (int) * (xi_dst.width * xi_dst.height));
  dst_data = xi_dst.data;
  src_data = xi_src.data;
#ifdef DEBUG
  printf ("xi_dst.width %i\n", xi_dst.width);
  printf ("xi_dst.height %i\n", xi_dst.height);
  printf ("xi_src.width %i\n", xi_src.width);
  printf ("xi_src.height %i\n", xi_src.height);
#endif
  for (y = 0; y < xi_dst.height; y++)
    {
      dst_data = xi_dst.data + (y * xi_dst.width);
      for (x = 0; x < xi_dst.width; x++)
	{
	  sx = (x * xi_src.width) / xi_dst.width;
	  sy = (y * xi_src.height) / xi_dst.height;
	  *dst_data = *(src_data + sx + (sy * xi_src.width));
	  dst_data++;
	}
    }
  XpmCreatePixmapFromXpmImage (dpy, DefaultRootWindow (dpy), &xi_dst,
			       &dst->pixmap, &dst->mask, NULL);
  free (xi_dst.data);
  XpmFreeXpmImage (&xi_src);
}
Ejemplo n.º 7
0
int
SetPixmapsBackground(Image * image, Drawable drawable, Pixel color)
{
  XpmImage xpmimage;
  XpmAttributes xpmattr;
  XpmColorSymbol xpmcolor[1];
  unsigned int i;

  xpmattr.valuemask = XpmCloseness;
  xpmattr.closeness = 32768;

  /*
   * By default, the XPM library assumes screen 0, so we have
   * to pass in the real values. Submitted by Caveh Frank Jalali
   */
  xpmattr.valuemask |= XpmVisual | XpmColormap | XpmDepth;
  xpmattr.visual = Scr->d_visual;
  xpmattr.colormap = XDefaultColormap(dpy, Scr->screen);
  xpmattr.depth = Scr->d_depth;

  if (XpmCreateXpmImageFromPixmap(dpy, image->pixmap, image->mask, &xpmimage, &xpmattr) != XpmSuccess)
  {
    fprintf(stderr, "Failed to XpmCreateImage\n");
    return (0);
  }

  for (i = 0; i < xpmimage.ncolors; i++)
    if (!strcmp(xpmimage.colorTable[i].c_color, "None"))
      break;

  if (i < xpmimage.ncolors)
  {
    XFreePixmap(dpy, image->pixmap);
    XFreePixmap(dpy, image->mask);

    xpmcolor[0].name = NULL;
    xpmcolor[0].value = "none";
    xpmcolor[0].pixel = color;

    xpmattr.colorsymbols = xpmcolor;
    xpmattr.numsymbols = 1;
    xpmattr.valuemask |= XpmColorSymbols;

    XpmCreatePixmapFromXpmImage(dpy, drawable, &xpmimage, &image->pixmap, &image->mask, &xpmattr);
  }

  i = xpmimage.ncolors;
  XpmFreeXpmImage(&xpmimage);

  return (i);
}
Ejemplo n.º 8
0
int
XpmReadFileToImage(
    Display		 *display,
    char		 *filename,
    XImage		**image_return,
    XImage		**shapeimage_return,
    XpmAttributes	 *attributes)
{
    XpmImage image;
    XpmInfo info;
    int ErrorStatus;
    xpmData mdata;

    xpmInitXpmImage(&image);
    xpmInitXpmInfo(&info);

    /* open file to read */
    if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
	return (ErrorStatus);

    /* create the XImage from the XpmData */
    if (attributes) {
	xpmInitAttributes(attributes);
	xpmSetInfoMask(&info, attributes);
	ErrorStatus = xpmParseDataAndCreate(display, &mdata,
					    image_return, shapeimage_return,
					    &image, &info, attributes);
    } else
	ErrorStatus = xpmParseDataAndCreate(display, &mdata,
					    image_return, shapeimage_return,
					    &image, NULL, attributes);
    if (attributes) {
	if (ErrorStatus >= 0)		/* no fatal error */
	    xpmSetAttributes(attributes, &image, &info);
	XpmFreeXpmInfo(&info);
    }

    xpmDataClose(&mdata);
    /* free the XpmImage */
    XpmFreeXpmImage(&image);

    return (ErrorStatus);
}
int
main(int argc, char **argv)
{
    XpmImage image;
    char *filename;
    int ErrorStatus;
    xpmData data;

#ifdef USE_GETTEXT
    setlocale(LC_ALL,"");
    bindtextdomain("cxpm",LOCALEDIR);
    textdomain("cxpm");
#endif

    if (argc > 1) {
        if (!strcmp(argv[1], "-?") || !strncmp(argv[1], "-h", 2)) {
	    /* L10N_Comments : Usage message produced by running cxpm -h
		%s will be replaced by argv[0] (program name) */
	    fprintf(stderr, gettext("Usage: %s [filename]\n"), argv[0]);
	    exit(1);
	}
        filename = argv[1];
    } else {
        filename = NULL;
    }

    xpmInitXpmImage(&image);

    if ((ErrorStatus = OpenReadFile(filename, &data)) != XpmSuccess)
	ErrorMessage(ErrorStatus, NULL);

    ErrorStatus = xpmParseData(&data, &image, NULL);
    ErrorMessage(ErrorStatus, &data);

    xpmDataClose(&data);
    XpmFreeXpmImage(&image);

    exit(0);
}
Ejemplo n.º 10
0
int
XpmWriteFileFromData(
    char	 *filename,
    char	**data)
{
    XpmImage image;
    XpmInfo info;
    int ErrorStatus;

    info.valuemask = XpmReturnComments | XpmReturnExtensions;

    ErrorStatus = XpmCreateXpmImageFromData(data, &image, &info);

    if (ErrorStatus != XpmSuccess)
	return (ErrorStatus);

    ErrorStatus = XpmWriteFileFromXpmImage(filename, &image, &info);

    XpmFreeXpmImage(&image);
    XpmFreeXpmInfo(&info);

    return (ErrorStatus);
}
int
XpmCreateBufferFromImage(
    Display		 *display,
    char		**buffer_return,
    XImage		 *image,
    XImage		 *shapeimage,
    XpmAttributes	 *attributes)
{
    XpmImage xpmimage;
    XpmInfo info;
    int ErrorStatus;

    /* initialize return value */
    if (buffer_return)
	*buffer_return = NULL;

    /* create an XpmImage from the image */
    ErrorStatus = XpmCreateXpmImageFromImage(display, image, shapeimage,
					     &xpmimage, attributes);
    if (ErrorStatus != XpmSuccess)
	return (ErrorStatus);

    /* create the buffer from the XpmImage */
    if (attributes) {
	xpmSetInfo(&info, attributes);
	ErrorStatus =
	    XpmCreateBufferFromXpmImage(buffer_return, &xpmimage, &info);
    } else
	ErrorStatus =
	    XpmCreateBufferFromXpmImage(buffer_return, &xpmimage, NULL);

    /* free the XpmImage */
    XpmFreeXpmImage(&xpmimage);

    return (ErrorStatus);
}
Ejemplo n.º 12
0
RImage*
RGetImageFromXPMData(RContext *context, char **xpmData)
{
    Display *dpy = context->dpy;
    Colormap cmap = context->cmap;
    RImage *image;
    XpmImage xpm;
    unsigned char *color_table[4];
    unsigned char *data;
    int *p;
    int i;

    i = XpmCreateXpmImageFromData(xpmData, &xpm, (XpmInfo *)NULL);
    if (i!=XpmSuccess) {
        switch (i) {
        case XpmOpenFailed:
            RErrorCode = RERR_OPEN;
            break;
        case XpmFileInvalid:
            RErrorCode = RERR_BADIMAGEFILE;
            break;
        case XpmNoMemory:
            RErrorCode = RERR_NOMEMORY;
            break;
        default:
            RErrorCode = RERR_BADIMAGEFILE;
            break;
        }
        return NULL;
    }
    if (xpm.height<1 || xpm.width < 1) {
        RErrorCode = RERR_BADIMAGEFILE;
        XpmFreeXpmImage(&xpm);
        return NULL;
    }

    if (xpm.colorTable==NULL) {
        RErrorCode = RERR_BADIMAGEFILE;
        XpmFreeXpmImage(&xpm);
        return NULL;
    }
    image = RCreateImage(xpm.width, xpm.height, True);
    if (!image) {
        XpmFreeXpmImage(&xpm);
        return NULL;
    }

    /* make color table */
    for (i=0; i<4; i++) {
        color_table[i] = malloc(xpm.ncolors*sizeof(char));
        if (!color_table[i]) {
            for (i=i-1;i>=0; i--) {
                if (color_table[i])
                    free(color_table[i]);
            }
            RReleaseImage(image);
            RErrorCode = RERR_NOMEMORY;
            XpmFreeXpmImage(&xpm);
            return NULL;
        }
    }

    for (i=0; i<xpm.ncolors; i++) {
        XColor xcolor;
        char * color = NULL;

        if (xpm.colorTable[i].c_color)
            color = xpm.colorTable[i].c_color;
        else if (xpm.colorTable[i].g_color)
            color = xpm.colorTable[i].g_color;
        else if (xpm.colorTable[i].g4_color)
            color = xpm.colorTable[i].g4_color;
        else if (xpm.colorTable[i].m_color)
            color = xpm.colorTable[i].m_color;
        else if (xpm.colorTable[i].symbolic)
            color = xpm.colorTable[i].symbolic;

        if (!color) {
            color_table[0][i] = 0xbe;
            color_table[1][i] = 0xbe;
            color_table[2][i] = 0xbe;
            color_table[3][i] = 0xff;
            continue;
        }

        if (strncmp(color,"None",4)==0) {
            color_table[0][i]=0;
            color_table[1][i]=0;
            color_table[2][i]=0;
            color_table[3][i]=0;
            continue;
        }
        if (XParseColor(dpy, cmap, color, &xcolor)) {
            color_table[0][i] = xcolor.red>>8;
            color_table[1][i] = xcolor.green>>8;
            color_table[2][i] = xcolor.blue>>8;
            color_table[3][i] = 0xff;
        } else {
Ejemplo n.º 13
0
void
qxe_XpmFreeXpmImage (XpmImage *image)
{
  XpmFreeXpmImage (image);
}
Ejemplo n.º 14
0
Archivo: gdxpm.c Proyecto: johlim/study
BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm(char *filename)
{
	XpmInfo info;
	XpmImage image;
	unsigned int i, j, k, number, len;
	char buf[5];
	gdImagePtr im = 0;
	int *pointer;
	int red = 0, green = 0, blue = 0;
	int *colors;
	int ret;

	ret = XpmReadFileToXpmImage(filename, &image, &info);
	if(ret != XpmSuccess) {
		return 0;
	}

	number = image.ncolors;
	if(overflow2(sizeof(int), number)) {
		goto done;
	}

	colors = (int *)gdMalloc(sizeof(int) * number);
	if(colors == NULL) {
		goto done;
	}

	if(!(im = gdImageCreate(image.width, image.height))) {
		gdFree(colors);
		goto done;
	}

	for(i = 0; i < number; i++) {
		char *c_color = image.colorTable[i].c_color;
		if(strcmp(c_color, "None") == 0) {
		  colors[i] = gdImageGetTransparent(im);
		  if(colors[i] == -1) colors[i] = gdImageColorAllocate(im, 0, 0, 0);
		  if(colors[i] != -1) gdImageColorTransparent(im, colors[i]);
		  continue;
		}
		len = strlen(c_color);
		if(len < 1) continue;
		if(c_color[0] == '#') {
			switch(len) {
			case 4:
				buf[2] = '\0';
				buf[0] = buf[1] = c_color[1];
				red = strtol(buf, NULL, 16);

				buf[0] = buf[1] = c_color[2];
				green = strtol(buf, NULL, 16);

				buf[0] = buf[1] = c_color[3];
				blue = strtol(buf, NULL, 16);
				break;

			case 7:
				buf[2] = '\0';
				buf[0] = c_color[1];
				buf[1] = c_color[2];
				red = strtol(buf, NULL, 16);

				buf[0] = c_color[3];
				buf[1] = c_color[4];
				green = strtol(buf, NULL, 16);

				buf[0] = c_color[5];
				buf[1] = c_color[6];
				blue = strtol(buf, NULL, 16);
				break;

			case 10:
				buf[3] = '\0';
				buf[0] = c_color[1];
				buf[1] = c_color[2];
				buf[2] = c_color[3];
				red = strtol(buf, NULL, 16);
				red /= 64;

				buf[0] = c_color[4];
				buf[1] = c_color[5];
				buf[2] = c_color[6];
				green = strtol(buf, NULL, 16);
				green /= 64;

				buf[0] = c_color[7];
				buf[1] = c_color[8];
				buf[2] = c_color[9];
				blue = strtol(buf, NULL, 16);
				blue /= 64;
				break;

			case 13:
				buf[4] = '\0';
				buf[0] = c_color[1];
				buf[1] = c_color[2];
				buf[2] = c_color[3];
				buf[3] = c_color[4];
				red = strtol(buf, NULL, 16);
				red /= 256;

				buf[0] = c_color[5];
				buf[1] = c_color[6];
				buf[2] = c_color[7];
				buf[3] = c_color[8];
				green = strtol(buf, NULL, 16);
				green /= 256;

				buf[0] = c_color[9];
				buf[1] = c_color[10];
				buf[2] = c_color[11];
				buf[3] = c_color[12];
				blue = strtol(buf, NULL, 16);
				blue /= 256;
				break;
			}
		} else if(!gdColorMapLookup(GD_COLOR_MAP_X11, c_color, &red, &green, &blue)) {
			continue;
		}

		colors[i] = gdImageColorResolve(im, red, green, blue);
	}

	pointer = (int *)image.data;

	for(i = 0; i < image.height; i++) {
		for(j = 0; j < image.width; j++) {
			k = *pointer++;
			gdImageSetPixel(im, j, i, colors[k]);
		}
	}

	gdFree(colors);

 done:
	XpmFreeXpmImage(&image);
	XpmFreeXpmInfo(&info);
	return im;
}