home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gd201.zip / gd-2.0.1 / gd_gd.c < prev    next >
C/C++ Source or Header  |  2001-04-03  |  4KB  |  273 lines

  1.  
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "gd.h"
  7.  
  8. #define TRUE 1
  9. #define FALSE 0
  10.  
  11. /* Exported functions: */
  12. extern void gdImageGd (gdImagePtr im, FILE * out);
  13.  
  14.  
  15. /* Use this for commenting out debug-print statements. */
  16. /* Just use the first '#define' to allow all the prints... */
  17. /*#define GD2_DBG(s) (s) */
  18. #define GD2_DBG(s)
  19.  
  20. /* */
  21. /* Shared code to read color tables from gd file. */
  22. /* */
  23. int
  24. _gdGetColors (gdIOCtx * in, gdImagePtr im, int gd2xFlag)
  25. {
  26.   int i;
  27.   if (gd2xFlag)
  28.     {
  29.       if (!gdGetByte (&im->trueColor, in))
  30.     {
  31.       goto fail1;
  32.     }
  33.       /* This should have been a word all along */
  34.       if (!im->trueColor)
  35.     {
  36.       if (!gdGetWord (&im->colorsTotal, in))
  37.         {
  38.           goto fail1;
  39.         }
  40.     }
  41.       /* Int to accommodate truecolor single-color transparency */
  42.       if (!gdGetInt (&im->transparent, in))
  43.     {
  44.       goto fail1;
  45.     }
  46.     }
  47.   else
  48.     {
  49.       if (!gdGetByte (&im->colorsTotal, in))
  50.     {
  51.       goto fail1;
  52.     }
  53.       if (!gdGetWord (&im->transparent, in))
  54.     {
  55.       goto fail1;
  56.     }
  57.       if (im->transparent == 257)
  58.     {
  59.       im->transparent = (-1);
  60.     }
  61.     }
  62.   GD2_DBG (printf ("Pallette had %d colours (T=%d)\n", im->colorsTotal, im->transparent));
  63.  
  64.   for (i = 0; (i < gdMaxColors); i++)
  65.     {
  66.       if (!gdGetByte (&im->red[i], in))
  67.     {
  68.       goto fail1;
  69.     }
  70.       if (!gdGetByte (&im->green[i], in))
  71.     {
  72.       goto fail1;
  73.     }
  74.       if (!gdGetByte (&im->blue[i], in))
  75.     {
  76.       goto fail1;
  77.     }
  78.       if (gd2xFlag)
  79.     {
  80.       if (!gdGetByte (&im->alpha[i], in))
  81.         {
  82.           goto fail1;
  83.         }
  84.     }
  85.     }
  86.  
  87.   for (i = 0; (i < im->colorsTotal); i++)
  88.     {
  89.       im->open[i] = 0;
  90.     };
  91.  
  92.   return TRUE;
  93. fail1:
  94.   return FALSE;
  95. }
  96.  
  97. /* */
  98. /* Use the common basic header info to make the image object. */
  99. /* This is also called from _gd2CreateFromFile */
  100. /* */
  101. static
  102.   gdImagePtr
  103. _gdCreateFromFile (gdIOCtx * in, int *sx, int *sy)
  104. {
  105.   gdImagePtr im;
  106.   int gd2xFlag = 0;
  107.   if (!gdGetWord (sx, in))
  108.     {
  109.       goto fail1;
  110.     }
  111.   if (*sx == 65535)
  112.     {
  113.       /* This is a gd 2.0 .gd file */
  114.       gd2xFlag = 1;
  115.       if (!gdGetWord (sx, in))
  116.     {
  117.       goto fail1;
  118.     }
  119.     }
  120.   if (!gdGetWord (sy, in))
  121.     {
  122.       goto fail1;
  123.     }
  124.  
  125.   GD2_DBG (printf ("Image is %dx%d\n", *sx, *sy));
  126.  
  127.   im = gdImageCreate (*sx, *sy);
  128.  
  129.   if (!_gdGetColors (in, im, gd2xFlag))
  130.     {
  131.       goto fail2;
  132.     }
  133.  
  134.   return im;
  135. fail2:
  136.   gdImageDestroy (im);
  137. fail1:
  138.   return 0;
  139. }
  140.  
  141. gdImagePtr
  142. gdImageCreateFromGd (FILE * inFile)
  143. {
  144.   gdImagePtr im;
  145.   gdIOCtx *in;
  146.  
  147.   in = gdNewFileCtx (inFile);
  148.   im = gdImageCreateFromGdCtx (in);
  149.  
  150.   in->free (in);
  151.  
  152.   return im;
  153. }
  154.  
  155. gdImagePtr
  156. gdImageCreateFromGdCtx (gdIOCtxPtr in)
  157. {
  158.   int sx, sy;
  159.   int x, y;
  160.   gdImagePtr im;
  161.  
  162.   /* Read the header */
  163.   im = _gdCreateFromFile (in, &sx, &sy);
  164.  
  165.   if (im == NULL)
  166.     {
  167.       goto fail1;
  168.     };
  169.  
  170.   /* Then the data... */
  171.   for (y = 0; (y < sy); y++)
  172.     {
  173.       for (x = 0; (x < sx); x++)
  174.     {
  175.       int ch;
  176.       ch = gdGetC (in);
  177.       if (ch == EOF)
  178.         {
  179.           goto fail2;
  180.         }
  181.       /* ROW-MAJOR IN GD 1.3 */
  182.       im->pixels[y][x] = ch;
  183.     }
  184.     }
  185.  
  186.   return im;
  187.  
  188. fail2:
  189.   gdImageDestroy (im);
  190. fail1:
  191.   return 0;
  192. }
  193.  
  194. void
  195. _gdPutColors (gdImagePtr im, gdIOCtx * out)
  196. {
  197.   int i;
  198.   int trans;
  199.  
  200.   gdPutC (im->trueColor, out);
  201.   if (!im->trueColor)
  202.     {
  203.       gdPutWord (im->colorsTotal, out);
  204.     }
  205.   gdPutInt (im->transparent, out);
  206.   if (!im->trueColor)
  207.     {
  208.       for (i = 0; (i < gdMaxColors); i++)
  209.     {
  210.       gdPutC ((unsigned char) im->red[i], out);
  211.       gdPutC ((unsigned char) im->green[i], out);
  212.       gdPutC ((unsigned char) im->blue[i], out);
  213.       gdPutC ((unsigned char) im->alpha[i], out);
  214.     }
  215.     }
  216. }
  217.  
  218. static
  219. void
  220. _gdPutHeader (gdImagePtr im, gdIOCtx * out)
  221. {
  222.   /* 65535 indicates this is a gd 2.x .gd file. */
  223.   gdPutWord (65535, out);
  224.   gdPutWord (im->sx, out);
  225.   gdPutWord (im->sy, out);
  226.  
  227.   _gdPutColors (im, out);
  228.  
  229. }
  230.  
  231. static void
  232. _gdImageGd (gdImagePtr im, gdIOCtx * out)
  233. {
  234.   int x, y;
  235.  
  236.   _gdPutHeader (im, out);
  237.  
  238.   for (y = 0; (y < im->sy); y++)
  239.     {
  240.       for (x = 0; (x < im->sx); x++)
  241.     {
  242.       /* ROW-MAJOR IN GD 1.3 */
  243.       if (im->trueColor)
  244.         {
  245.           gdPutInt (im->tpixels[y][x], out);
  246.         }
  247.       else
  248.         {
  249.           gdPutC ((unsigned char) im->pixels[y][x], out);
  250.         }
  251.     }
  252.     }
  253. }
  254.  
  255. void
  256. gdImageGd (gdImagePtr im, FILE * outFile)
  257. {
  258.   gdIOCtx *out = gdNewFileCtx (outFile);
  259.   _gdImageGd (im, out);
  260.   out->free (out);
  261. }
  262.  
  263. void *
  264. gdImageGdPtr (gdImagePtr im, int *size)
  265. {
  266.   void *rv;
  267.   gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
  268.   _gdImageGd (im, out);
  269.   rv = gdDPExtractData (out, size);
  270.   out->free (out);
  271.   return rv;
  272. }
  273.