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

  1.  
  2.  
  3. /*
  4.    WBMP: Wireless Bitmap Type 0: B/W, Uncompressed Bitmap
  5.    Specification of the WBMP format can be found in the file: 
  6.    SPEC-WAESpec-19990524.pdf
  7.    You can download the WAP specification on: http://www.wapforum.com/ 
  8.  
  9.    gd_wbmp.c
  10.  
  11.    Copyright (C) Johan Van den Brande (johan@vandenbrande.com)
  12.  
  13.    Fixed: gdImageWBMPPtr, gdImageWBMP
  14.  
  15.    Recoded: gdImageWBMPCtx for use with my wbmp library
  16.    (wbmp library included, but you can find the latest distribution
  17.    at http://www.vandenbrande.com/wbmp)
  18.  
  19.    Implemented: gdImageCreateFromWBMPCtx, gdImageCreateFromWBMP 
  20.  
  21.    ---------------------------------------------------------------------------
  22.  
  23.    Parts of this code are from Maurice Smurlo.
  24.  
  25.  
  26.    ** Copyright (C) Maurice Szmurlo --- T-SIT --- January 2000
  27.    ** (Maurice.Szmurlo@info.unicaen.fr)
  28.  
  29.    ** Permission to use, copy, modify, and distribute this software and its
  30.    ** documentation for any purpose and without fee is hereby granted, provided
  31.    ** that the above copyright notice appear in all copies and that both that
  32.    ** copyright notice and this permission notice appear in supporting
  33.    ** documentation.  This software is provided "as is" without express or
  34.    ** implied warranty.
  35.  
  36.    ---------------------------------------------------------------------------
  37.    Parts od this code are inspired by  'pbmtowbmp.c' and 'wbmptopbm.c' by 
  38.    Terje Sannum <terje@looplab.com>.
  39.    **
  40.    ** Permission to use, copy, modify, and distribute this software and its
  41.    ** documentation for any purpose and without fee is hereby granted, provided
  42.    ** that the above copyright notice appear in all copies and that both that
  43.    ** copyright notice and this permission notice appear in supporting
  44.    ** documentation.  This software is provided "as is" without express or
  45.    ** implied warranty.
  46.    **
  47.    ---------------------------------------------------------------------------
  48.  
  49.    Todo:
  50.  
  51.    gdCreateFromWBMP function for reading WBMP files
  52.  
  53.    ----------------------------------------------------------------------------
  54.  */
  55.  
  56. #include <gd.h>
  57. #include <gdfonts.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <limits.h>
  61.  
  62. #include "wbmp.h"
  63.  
  64.  
  65. /* gd_putout
  66.    ** ---------
  67.    ** Wrapper around gdPutC for use with writewbmp
  68.    **
  69.  */
  70. void
  71. gd_putout (int i, void *out)
  72. {
  73.   gdPutC (i, (gdIOCtx *) out);
  74. }
  75.  
  76.  
  77. /* gd_getin
  78.    ** --------
  79.    ** Wrapper around gdGetC for use with readwbmp
  80.    **
  81.  */
  82. int
  83. gd_getin (void *in)
  84. {
  85.   return (gdGetC ((gdIOCtx *) in));
  86. }
  87.  
  88.  
  89. /*      gdImageWBMPCtx
  90.    **  --------------
  91.    **  Write the image as a wbmp file
  92.    **  Parameters are:
  93.    **  image:  gd image structure;
  94.    **  fg:     the index of the foreground color. any other value will be 
  95.    **          considered as background and will not be written
  96.    **  out:    the stream where to write
  97.  */
  98. void
  99. gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
  100. {
  101.  
  102.   int x, y, pos;
  103.   Wbmp *wbmp;
  104.  
  105.  
  106.   /* create the WBMP */
  107.   if ((wbmp = createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL)
  108.     fprintf (stderr, "Could not create WBMP\n");
  109.  
  110.   /* fill up the WBMP structure */
  111.   pos = 0;
  112.   for (y = 0; y < gdImageSY (image); y++)
  113.     {
  114.       for (x = 0; x < gdImageSX (image); x++)
  115.     {
  116.       if (gdImageGetPixel (image, x, y) == fg)
  117.         {
  118.           wbmp->bitmap[pos] = WBMP_BLACK;
  119.         }
  120.       pos++;
  121.     }
  122.     }
  123.  
  124.   /* write the WBMP to a gd file descriptor */
  125.   if (writewbmp (wbmp, &gd_putout, out))
  126.     fprintf (stderr, "Could not save WBMP\n");
  127.   /* des submitted this bugfix: gdFree the memory. */
  128.   freewbmp (wbmp);
  129. }
  130.  
  131.  
  132. /* gdImageCreateFromWBMPCtx
  133.    ** ------------------------
  134.    ** Create a gdImage from a WBMP file input from an gdIOCtx
  135.  */
  136. gdImagePtr
  137. gdImageCreateFromWBMPCtx (gdIOCtx * infile)
  138. {
  139.   /* FILE *wbmp_file; */
  140.   Wbmp *wbmp;
  141.   gdImagePtr im = NULL;
  142.   int black, white;
  143.   int col, row, pos;
  144.  
  145.   if (readwbmp (&gd_getin, infile, &wbmp))
  146.     return (NULL);
  147.  
  148.   if (!(im = gdImageCreate (wbmp->width, wbmp->height)))
  149.     {
  150.       freewbmp (wbmp);
  151.       return (NULL);
  152.     }
  153.  
  154.   /* create the background color */
  155.   white = gdImageColorAllocate (im, 255, 255, 255);
  156.   /* create foreground color */
  157.   black = gdImageColorAllocate (im, 0, 0, 0);
  158.  
  159.   /* fill in image (in a wbmp 1 = white/ 0 = black) */
  160.   pos = 0;
  161.   for (row = 0; row < wbmp->height; row++)
  162.     {
  163.       for (col = 0; col < wbmp->width; col++)
  164.     {
  165.       if (wbmp->bitmap[pos++] == WBMP_WHITE)
  166.         {
  167.           gdImageSetPixel (im, col, row, white);
  168.         }
  169.       else
  170.         {
  171.           gdImageSetPixel (im, col, row, black);
  172.         }
  173.     }
  174.     }
  175.  
  176.   freewbmp (wbmp);
  177.  
  178.   return (im);
  179. }
  180.  
  181.  
  182. /* gdImageCreateFromWBMP
  183.    ** ---------------------
  184.  */
  185. gdImagePtr
  186. gdImageCreateFromWBMP (FILE * inFile)
  187. {
  188.   gdImagePtr im;
  189.   gdIOCtx *in = gdNewFileCtx (inFile);
  190.   im = gdImageCreateFromWBMPCtx (in);
  191.   in->free (in);
  192.   return (im);
  193. }
  194.  
  195. /* gdImageWBMP
  196.    ** -----------
  197.  */
  198. void
  199. gdImageWBMP (gdImagePtr im, int fg, FILE * outFile)
  200. {
  201.   gdIOCtx *out = gdNewFileCtx (outFile);
  202.   gdImageWBMPCtx (im, fg, out);
  203.   out->free (out);
  204. }
  205.  
  206. /* gdImageWBMPPtr
  207.    ** --------------
  208.  */
  209. void *
  210. gdImageWBMPPtr (gdImagePtr im, int *size, int fg)
  211. {
  212.   void *rv;
  213.   gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
  214.   gdImageWBMPCtx (im, fg, out);
  215.   rv = gdDPExtractData (out, size);
  216.   out->free (out);
  217.   return rv;
  218. }
  219.