Back to index

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