Img * Img::loadXPM(void *tex, ImageReader read_func) { #if HAVE_LIBXPM Texture *_tex = (Texture *) tex; FILE *f; if ((f = Cache::openCache(_tex->url, _tex->http)) == NULL) return NULL; File::closeFile(f); XpmImage xpmimage; int r = XpmReadFileToXpmImage(Cache::getFileName(_tex->url), &xpmimage, NULL); if (r != XpmSuccess) { if (r == XpmColorFailed) error("XpmReadFileToXpmImage: bad color"); return NULL; } int size = xpmimage.width * xpmimage.height; Img *img = new Img(xpmimage.width, xpmimage.height, Img::RGB); uint8_t *data = new uint8_t[3 * size]; uint8_t *pxpm = (uint8_t *) xpmimage.data; for (int i=0; i < size ; i++, pxpm++) { data[i*3 + 0] = *pxpm++; data[i*3 + 1] = *pxpm++; data[i*3 + 2] = *pxpm++; } img->pixmap = (uint8_t *) xpmimage.data; return img; #else return NULL; #endif }
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); }
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; }
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; }
gn_error file_xpm_load(char *filename, gn_bmp *bitmap) { int error, x, y; XpmImage image; XpmInfo info; error = XpmReadFileToXpmImage(filename, &image, &info); switch (error) { case XpmColorError: return GN_ERR_WRONGDATAFORMAT; case XpmColorFailed: return GN_ERR_WRONGDATAFORMAT; case XpmOpenFailed: return GN_ERR_FAILED; case XpmFileInvalid: return GN_ERR_WRONGDATAFORMAT; case XpmSuccess: default: break; } if (image.ncolors != 2) return GN_ERR_WRONGDATAFORMAT; bitmap->height = image.height; bitmap->width = image.width; bitmap->size = ((bitmap->width + 7) / 8) * bitmap->height; if (bitmap->size > GN_BMP_MAX_SIZE) { fprintf(stderr, _("Bitmap too large\n")); return GN_ERR_INVALIDSIZE; } gn_bmp_clear(bitmap); for (y = 0; y < image.height; y++) { for (x = 0; x < image.width; x++) { if (image.data[y * image.width + x] == 0) gn_bmp_point_set(bitmap, x, y); } } return GN_ERR_NONE; }
/**************************************************************************** * * Looks for a color XPM icon file * ****************************************************************************/ void GetXPMFile(struct icon_info *item) { #ifdef XPM XpmAttributes xpm_attributes; char *path = NULL; int rc; XpmImage my_image; path = findImageFile(item->icon_file, imagePath,R_OK); if(path == NULL)return; rc = XpmReadFileToXpmImage(path, &my_image, NULL); if (rc != XpmSuccess) { fprintf(stderr, "Problem reading pixmap %s, rc %d\n", path, rc); free(path); return; } color_reduce_pixmap(&my_image,save_color_limit); xpm_attributes.visual = Pvisual; xpm_attributes.colormap = Pcmap; xpm_attributes.depth = Pdepth; xpm_attributes.closeness = 40000; /* same closeness used elsewhere */ xpm_attributes.valuemask = XpmVisual | XpmColormap | XpmDepth | XpmCloseness | XpmReturnPixels; rc = XpmCreatePixmapFromXpmImage(dpy,main_win, &my_image, &item->iconPixmap, &item->icon_maskPixmap, &xpm_attributes); if (rc != XpmSuccess) { fprintf(stderr, "Problem creating pixmap from image, rc %d\n", rc); free(path); return; } item->icon_w = min(max_icon_width, my_image.width); item->icon_h = min(max_icon_height, my_image.height); item->icon_depth = Pdepth; free(path); #endif /* XPM */ }
static PyObject *pyxpm_read(PyObject *self, PyObject *args){ char* fn; int i; XpmImage XPM; XpmInfo XPM_info; npy_intp dims[2]; PyIntObject *py_int; if (!PyArg_ParseTuple(args,"s", &fn)) return NULL; //printf("Start Reading file : %s\n", fn); XpmReadFileToXpmImage(fn, &XPM, &XPM_info); printf("Done!\n"); printf("%d x %d \n",XPM.height,XPM.width); // Create a Python List to return back PyListObject *ret = PyList_New((Py_ssize_t) 6); // Start Populating the return list with info from the XPM PyList_SetItem(ret, (Py_ssize_t) 0, PyInt_FromLong(XPM.height)); PyList_SetItem(ret, (Py_ssize_t) 1, PyInt_FromLong(XPM.width)); PyList_SetItem(ret, (Py_ssize_t) 2, PyInt_FromLong(XPM.cpp)); PyList_SetItem(ret, (Py_ssize_t) 3, PyInt_FromLong(XPM.ncolors)); dims[0] = XPM.height; dims[1] = XPM.width; //printf(dims[0],dims[1]); /* * List that will contain the Color Table of XPM: * LIST = [ [ string0, color0] ,[string1, color1] ... [stringN, colorN] ] */ PyListObject *colorTab = PyList_New((Py_ssize_t) XPM.ncolors); for (i=0;i<XPM.ncolors;i++){ PyListObject *color = PyList_New((Py_ssize_t) 2); PyList_SetItem(color, (Py_ssize_t) 0, PyString_FromString(XPM.colorTable[i].string)); PyList_SetItem(color, (Py_ssize_t) 1, PyString_FromString(XPM.colorTable[i].c_color)); PyList_SetItem(colorTab, (Py_ssize_t) i, color); } // Insert also the Color Table on the return list PyList_SetItem(ret,(Py_ssize_t) 4, colorTab); // Python Array that will contain all the data PyArrayObject *data = PyArray_SimpleNewFromData(2, dims, NPY_INT, XPM.data); // Insert the array in the return list PyList_SetItem(ret,(Py_ssize_t) 5, data); return ret; }
Bool GetBackPixmap(void) { #ifdef XPM XpmAttributes xpm_attributes; XpmImage my_image; Pixmap maskPixmap; int rc; #endif char *path = NULL; Pixmap tmp_bitmap; int x, y, w=0, h=0; if (IconwinPixmapFile == NULL) return False; if ((path = findImageFile(IconwinPixmapFile, imagePath,R_OK)) != NULL){ if (XReadBitmapFile(dpy, main_win ,path,(unsigned int *)&w, (unsigned int *)&h, &tmp_bitmap, (int *)&x, (int *)&y)!= BitmapSuccess) w = h = 0; else{ IconwinPixmap = XCreatePixmap(dpy, main_win, w, h, Pdepth); XCopyPlane(dpy, tmp_bitmap, IconwinPixmap, NormalGC, 0, 0, w, h, 0, 0, 1); XFreePixmap(dpy, tmp_bitmap); } free(path); } #ifdef XPM if ( w == 0 && h == 0 && (path = findImageFile(IconwinPixmapFile, imagePath,R_OK)) != NULL) { rc = XpmReadFileToXpmImage(path, &my_image, NULL); if (rc != XpmSuccess) { fprintf(stderr, "Problem reading pixmap %s, rc %d\n", path, rc); free(path); return False; } color_reduce_pixmap(&my_image,save_color_limit); xpm_attributes.visual = Pvisual; xpm_attributes.colormap = Pcmap; xpm_attributes.depth = Pdepth; xpm_attributes.closeness = 40000; /* same closeness used elsewhere */ xpm_attributes.valuemask = XpmVisual | XpmColormap | XpmDepth | XpmCloseness | XpmReturnPixels; rc = XpmCreatePixmapFromXpmImage(dpy, main_win, &my_image, &IconwinPixmap, &maskPixmap, &xpm_attributes); if (rc != XpmSuccess) { fprintf(stderr, "Problem creating pixmap from image, rc %d\n", rc); free(path); return False; } w = my_image.width; h = my_image.height; free(path); } #endif if (w != 0 && h != 0) return True; return False; }
BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm (char *filename) { XpmInfo info; XpmImage image; int i, j, k, number; 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; if (!(im = gdImageCreate (image.width, image.height))) return 0; number = image.ncolors; colors = (int *) gdMalloc (sizeof (int) * number); if (colors == NULL) return (0); for (i = 0; i < number; i++) { switch (strlen (image.colorTable[i].c_color)) { case 4: buf[1] = '\0'; buf[0] = image.colorTable[i].c_color[1]; red = strtol (buf, NULL, 16); buf[0] = image.colorTable[i].c_color[3]; green = strtol (buf, NULL, 16); buf[0] = image.colorTable[i].c_color[5]; blue = strtol (buf, NULL, 16); break; case 7: buf[2] = '\0'; buf[0] = image.colorTable[i].c_color[1]; buf[1] = image.colorTable[i].c_color[2]; red = strtol (buf, NULL, 16); buf[0] = image.colorTable[i].c_color[3]; buf[1] = image.colorTable[i].c_color[4]; green = strtol (buf, NULL, 16); buf[0] = image.colorTable[i].c_color[5]; buf[1] = image.colorTable[i].c_color[6]; blue = strtol (buf, NULL, 16); break; case 10: buf[3] = '\0'; buf[0] = image.colorTable[i].c_color[1]; buf[1] = image.colorTable[i].c_color[2]; buf[2] = image.colorTable[i].c_color[3]; red = strtol (buf, NULL, 16); red /= 64; buf[0] = image.colorTable[i].c_color[4]; buf[1] = image.colorTable[i].c_color[5]; buf[2] = image.colorTable[i].c_color[6]; green = strtol (buf, NULL, 16); green /= 64; buf[0] = image.colorTable[i].c_color[7]; buf[1] = image.colorTable[i].c_color[8]; buf[2] = image.colorTable[i].c_color[9]; blue = strtol (buf, NULL, 16); blue /= 64; break; case 13: buf[4] = '\0'; buf[0] = image.colorTable[i].c_color[1]; buf[1] = image.colorTable[i].c_color[2]; buf[2] = image.colorTable[i].c_color[3]; buf[3] = image.colorTable[i].c_color[4]; red = strtol (buf, NULL, 16); red /= 256; buf[0] = image.colorTable[i].c_color[5]; buf[1] = image.colorTable[i].c_color[6]; buf[2] = image.colorTable[i].c_color[7]; buf[3] = image.colorTable[i].c_color[8]; green = strtol (buf, NULL, 16); green /= 256; buf[0] = image.colorTable[i].c_color[9]; buf[1] = image.colorTable[i].c_color[10]; buf[2] = image.colorTable[i].c_color[11]; buf[3] = image.colorTable[i].c_color[12]; blue = strtol (buf, NULL, 16); blue /= 256; break; } colors[i] = gdImageColorResolve (im, red, green, blue); if (colors[i] == -1) fprintf (stderr, "ARRRGH\n"); } 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); return (im); }
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; }