Back to index

tetex-bin  3.0
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 
00009 #ifdef HAVE_CONFIG_H
00010 #include "config.h"
00011 #endif
00012 
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016 #include "gd.h"
00017 #include "gdhelpers.h"
00018 
00019 #ifndef HAVE_LIBXPM
00020 BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm (char *filename)
00021 {
00022   fprintf (stderr, "libgd was not built with xpm support\n");
00023   return (NULL);
00024 }
00025 
00026 #else
00027 
00028 #include <X11/xpm.h>
00029 
00030 BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm (char *filename)
00031 {
00032   XpmInfo info;
00033   XpmImage image;
00034   int i, j, k, number;
00035   char buf[5];
00036   gdImagePtr im = 0;
00037   int *pointer;
00038   int red = 0, green = 0, blue = 0;
00039   int *colors;
00040   int ret;
00041 
00042   ret = XpmReadFileToXpmImage (filename, &image, &info);
00043   if (ret != XpmSuccess)
00044     return 0;
00045 
00046   if (!(im = gdImageCreate (image.width, image.height)))
00047     return 0;
00048 
00049   number = image.ncolors;
00050   colors = (int *) gdMalloc (sizeof (int) * number);
00051   if (colors == NULL)
00052     return (0);
00053   for (i = 0; i < number; i++)
00054     {
00055       switch (strlen (image.colorTable[i].c_color))
00056        {
00057        case 4:
00058          buf[1] = '\0';
00059          buf[0] = image.colorTable[i].c_color[1];
00060          red = strtol (buf, NULL, 16);
00061 
00062          buf[0] = image.colorTable[i].c_color[3];
00063          green = strtol (buf, NULL, 16);
00064 
00065          buf[0] = image.colorTable[i].c_color[5];
00066          blue = strtol (buf, NULL, 16);
00067          break;
00068        case 7:
00069          buf[2] = '\0';
00070          buf[0] = image.colorTable[i].c_color[1];
00071          buf[1] = image.colorTable[i].c_color[2];
00072          red = strtol (buf, NULL, 16);
00073 
00074          buf[0] = image.colorTable[i].c_color[3];
00075          buf[1] = image.colorTable[i].c_color[4];
00076          green = strtol (buf, NULL, 16);
00077 
00078          buf[0] = image.colorTable[i].c_color[5];
00079          buf[1] = image.colorTable[i].c_color[6];
00080          blue = strtol (buf, NULL, 16);
00081          break;
00082        case 10:
00083          buf[3] = '\0';
00084          buf[0] = image.colorTable[i].c_color[1];
00085          buf[1] = image.colorTable[i].c_color[2];
00086          buf[2] = image.colorTable[i].c_color[3];
00087          red = strtol (buf, NULL, 16);
00088          red /= 64;
00089 
00090          buf[0] = image.colorTable[i].c_color[4];
00091          buf[1] = image.colorTable[i].c_color[5];
00092          buf[2] = image.colorTable[i].c_color[6];
00093          green = strtol (buf, NULL, 16);
00094          green /= 64;
00095 
00096          buf[0] = image.colorTable[i].c_color[7];
00097          buf[1] = image.colorTable[i].c_color[8];
00098          buf[2] = image.colorTable[i].c_color[9];
00099          blue = strtol (buf, NULL, 16);
00100          blue /= 64;
00101          break;
00102        case 13:
00103          buf[4] = '\0';
00104          buf[0] = image.colorTable[i].c_color[1];
00105          buf[1] = image.colorTable[i].c_color[2];
00106          buf[2] = image.colorTable[i].c_color[3];
00107          buf[3] = image.colorTable[i].c_color[4];
00108          red = strtol (buf, NULL, 16);
00109          red /= 256;
00110 
00111          buf[0] = image.colorTable[i].c_color[5];
00112          buf[1] = image.colorTable[i].c_color[6];
00113          buf[2] = image.colorTable[i].c_color[7];
00114          buf[3] = image.colorTable[i].c_color[8];
00115          green = strtol (buf, NULL, 16);
00116          green /= 256;
00117 
00118          buf[0] = image.colorTable[i].c_color[9];
00119          buf[1] = image.colorTable[i].c_color[10];
00120          buf[2] = image.colorTable[i].c_color[11];
00121          buf[3] = image.colorTable[i].c_color[12];
00122          blue = strtol (buf, NULL, 16);
00123          blue /= 256;
00124          break;
00125        }
00126 
00127 
00128       colors[i] = gdImageColorResolve (im, red, green, blue);
00129       if (colors[i] == -1)
00130        fprintf (stderr, "ARRRGH\n");
00131     }
00132 
00133   pointer = (int *) image.data;
00134   for (i = 0; i < image.height; i++)
00135     {
00136       for (j = 0; j < image.width; j++)
00137        {
00138          k = *pointer++;
00139          gdImageSetPixel (im, j, i, colors[k]);
00140        }
00141     }
00142   gdFree (colors);
00143   return (im);
00144 }
00145 #endif /* HAVE_LIBXPM */