Back to index

php5  5.3.10
gdxpm.c
Go to the documentation of this file.
00001 
00002 /*
00003    add ability to load xpm files to gd, requires the xpm
00004    library.
00005    Caolan.McNamara@ul.ie
00006    http://www.csn.ul.ie/~caolan
00007  */
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include "gd.h"
00012 #include "gdhelpers.h"
00013 
00014 #ifdef HAVE_XPM
00015 
00016 #include <X11/xpm.h>
00017 
00018 gdImagePtr gdImageCreateFromXpm (char *filename)
00019 {
00020        XpmInfo info;
00021        XpmImage image;
00022        int i, j, k, number;
00023        char buf[5];
00024        gdImagePtr im = 0;
00025        int *pointer;
00026        int red = 0, green = 0, blue = 0;
00027        int *colors;
00028        int ret;
00029 
00030        ret = XpmReadFileToXpmImage(filename, &image, &info);
00031        if (ret != XpmSuccess) {
00032               return 0;
00033        }
00034 
00035        if (!(im = gdImageCreate(image.width, image.height))) {
00036               goto done;
00037        }
00038 
00039        number = image.ncolors;
00040        colors = (int *) safe_emalloc(number, sizeof(int), 0);
00041        for (i = 0; i < number; i++) {
00042               switch (strlen (image.colorTable[i].c_color)) {
00043                      case 4:
00044                             buf[1] = '\0';
00045                             buf[0] = image.colorTable[i].c_color[1];
00046                             red = strtol(buf, NULL, 16);
00047 
00048                             buf[0] = image.colorTable[i].c_color[2];
00049                             green = strtol(buf, NULL, 16);
00050 
00051                             buf[0] = image.colorTable[i].c_color[3];
00052                             blue = strtol(buf, NULL, 16);
00053                             break;
00054 
00055                      case 7:
00056                             buf[2] = '\0';
00057                             buf[0] = image.colorTable[i].c_color[1];
00058                             buf[1] = image.colorTable[i].c_color[2];
00059                             red = strtol(buf, NULL, 16);
00060 
00061                             buf[0] = image.colorTable[i].c_color[3];
00062                             buf[1] = image.colorTable[i].c_color[4];
00063                             green = strtol(buf, NULL, 16);
00064 
00065                             buf[0] = image.colorTable[i].c_color[5];
00066                             buf[1] = image.colorTable[i].c_color[6];
00067                             blue = strtol(buf, NULL, 16);
00068                             break;
00069 
00070                      case 10:
00071                             buf[3] = '\0';
00072                             buf[0] = image.colorTable[i].c_color[1];
00073                             buf[1] = image.colorTable[i].c_color[2];
00074                             buf[2] = image.colorTable[i].c_color[3];
00075                             red = strtol(buf, NULL, 16);
00076                             red /= 64;
00077 
00078                             buf[0] = image.colorTable[i].c_color[4];
00079                             buf[1] = image.colorTable[i].c_color[5];
00080                             buf[2] = image.colorTable[i].c_color[6];
00081                             green = strtol(buf, NULL, 16);
00082                             green /= 64;
00083 
00084                             buf[0] = image.colorTable[i].c_color[7];
00085                             buf[1] = image.colorTable[i].c_color[8];
00086                             buf[2] = image.colorTable[i].c_color[9];
00087                             blue = strtol(buf, NULL, 16);
00088                             blue /= 64;
00089                             break;
00090 
00091                      case 13:
00092                             buf[4] = '\0';
00093                             buf[0] = image.colorTable[i].c_color[1];
00094                             buf[1] = image.colorTable[i].c_color[2];
00095                             buf[2] = image.colorTable[i].c_color[3];
00096                             buf[3] = image.colorTable[i].c_color[4];
00097                             red = strtol(buf, NULL, 16);
00098                             red /= 256;
00099 
00100                             buf[0] = image.colorTable[i].c_color[5];
00101                             buf[1] = image.colorTable[i].c_color[6];
00102                             buf[2] = image.colorTable[i].c_color[7];
00103                             buf[3] = image.colorTable[i].c_color[8];
00104                             green = strtol(buf, NULL, 16);
00105                             green /= 256;
00106 
00107                             buf[0] = image.colorTable[i].c_color[9];
00108                             buf[1] = image.colorTable[i].c_color[10];
00109                             buf[2] = image.colorTable[i].c_color[11];
00110                             buf[3] = image.colorTable[i].c_color[12];
00111                             blue = strtol(buf, NULL, 16);
00112                             blue /= 256;
00113                             break;
00114               }
00115 
00116 
00117               colors[i] = gdImageColorResolve(im, red, green, blue);
00118        }
00119 
00120        pointer = (int *) image.data;
00121        for (i = 0; i < image.height; i++) {
00122               for (j = 0; j < image.width; j++) {
00123                      k = *pointer++;
00124                      gdImageSetPixel(im, j, i, colors[k]);
00125               }
00126        }
00127 
00128        gdFree(colors);
00129  done:
00130        XpmFreeXpmImage(&image);
00131        XpmFreeXpmInfo(&info);
00132        return im;
00133 }
00134 #endif