Back to index

tetex-bin  3.0
gd_wbmp.c
Go to the documentation of this file.
00001 
00002 
00003 /*
00004    WBMP: Wireless Bitmap Type 0: B/W, Uncompressed Bitmap
00005    Specification of the WBMP format can be found in the file: 
00006    SPEC-WAESpec-19990524.pdf
00007    You can download the WAP specification on: http://www.wapforum.com/ 
00008 
00009    gd_wbmp.c
00010 
00011    Copyright (C) Johan Van den Brande (johan@vandenbrande.com)
00012 
00013    Fixed: gdImageWBMPPtr, gdImageWBMP
00014 
00015    Recoded: gdImageWBMPCtx for use with my wbmp library
00016    (wbmp library included, but you can find the latest distribution
00017    at http://www.vandenbrande.com/wbmp)
00018 
00019    Implemented: gdImageCreateFromWBMPCtx, gdImageCreateFromWBMP 
00020 
00021    ---------------------------------------------------------------------------
00022 
00023    Parts of this code are from Maurice Smurlo.
00024 
00025 
00026    ** Copyright (C) Maurice Szmurlo --- T-SIT --- January 2000
00027    ** (Maurice.Szmurlo@info.unicaen.fr)
00028 
00029    ** Permission to use, copy, modify, and distribute this software and its
00030    ** documentation for any purpose and without fee is hereby granted, provided
00031    ** that the above copyright notice appear in all copies and that both that
00032    ** copyright notice and this permission notice appear in supporting
00033    ** documentation.  This software is provided "as is" without express or
00034    ** implied warranty.
00035 
00036    ---------------------------------------------------------------------------
00037    Parts od this code are inspired by  'pbmtowbmp.c' and 'wbmptopbm.c' by 
00038    Terje Sannum <terje@looplab.com>.
00039    **
00040    ** Permission to use, copy, modify, and distribute this software and its
00041    ** documentation for any purpose and without fee is hereby granted, provided
00042    ** that the above copyright notice appear in all copies and that both that
00043    ** copyright notice and this permission notice appear in supporting
00044    ** documentation.  This software is provided "as is" without express or
00045    ** implied warranty.
00046    **
00047    ---------------------------------------------------------------------------
00048 
00049    Todo:
00050 
00051    gdCreateFromWBMP function for reading WBMP files
00052 
00053    ----------------------------------------------------------------------------
00054  */
00055 
00056 #ifdef HAVE_CONFIG_H
00057 #include "config.h"
00058 #endif
00059 
00060 #include <gd.h>
00061 #include <gdfonts.h>
00062 #include <stdio.h>
00063 #include <stdlib.h>
00064 #include <limits.h>
00065 
00066 #include "wbmp.h"
00067 
00068 
00069 /* gd_putout
00070    ** ---------
00071    ** Wrapper around gdPutC for use with writewbmp
00072    **
00073  */
00074 void
00075 gd_putout (int i, void *out)
00076 {
00077   gdPutC (i, (gdIOCtx *) out);
00078 }
00079 
00080 
00081 /* gd_getin
00082    ** --------
00083    ** Wrapper around gdGetC for use with readwbmp
00084    **
00085  */
00086 int
00087 gd_getin (void *in)
00088 {
00089   return (gdGetC ((gdIOCtx *) in));
00090 }
00091 
00092 
00093 /*      gdImageWBMPCtx
00094    **  --------------
00095    **  Write the image as a wbmp file
00096    **  Parameters are:
00097    **  image:  gd image structure;
00098    **  fg:     the index of the foreground color. any other value will be 
00099    **          considered as background and will not be written
00100    **  out:    the stream where to write
00101  */
00102 BGD_DECLARE(void) gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
00103 {
00104 
00105   int x, y, pos;
00106   Wbmp *wbmp;
00107 
00108 
00109   /* create the WBMP */
00110   if ((wbmp =
00111        createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL)
00112     fprintf (stderr, "Could not create WBMP\n");
00113 
00114   /* fill up the WBMP structure */
00115   pos = 0;
00116   for (y = 0; y < gdImageSY (image); y++)
00117     {
00118       for (x = 0; x < gdImageSX (image); x++)
00119        {
00120          if (gdImageGetPixel (image, x, y) == fg)
00121            {
00122              wbmp->bitmap[pos] = WBMP_BLACK;
00123            }
00124          pos++;
00125        }
00126     }
00127 
00128   /* write the WBMP to a gd file descriptor */
00129   if (writewbmp (wbmp, &gd_putout, out))
00130     fprintf (stderr, "Could not save WBMP\n");
00131   /* des submitted this bugfix: gdFree the memory. */
00132   freewbmp (wbmp);
00133 }
00134 
00135 
00136 /* gdImageCreateFromWBMPCtx
00137    ** ------------------------
00138    ** Create a gdImage from a WBMP file input from an gdIOCtx
00139  */
00140 BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPCtx (gdIOCtx * infile)
00141 {
00142   /* FILE *wbmp_file; */
00143   Wbmp *wbmp;
00144   gdImagePtr im = NULL;
00145   int black, white;
00146   int col, row, pos;
00147 
00148   if (readwbmp (&gd_getin, infile, &wbmp))
00149     return (NULL);
00150 
00151   if (!(im = gdImageCreate (wbmp->width, wbmp->height)))
00152     {
00153       freewbmp (wbmp);
00154       return (NULL);
00155     }
00156 
00157   /* create the background color */
00158   white = gdImageColorAllocate (im, 255, 255, 255);
00159   /* create foreground color */
00160   black = gdImageColorAllocate (im, 0, 0, 0);
00161 
00162   /* fill in image (in a wbmp 1 = white/ 0 = black) */
00163   pos = 0;
00164   for (row = 0; row < wbmp->height; row++)
00165     {
00166       for (col = 0; col < wbmp->width; col++)
00167        {
00168          if (wbmp->bitmap[pos++] == WBMP_WHITE)
00169            {
00170              gdImageSetPixel (im, col, row, white);
00171            }
00172          else
00173            {
00174              gdImageSetPixel (im, col, row, black);
00175            }
00176        }
00177     }
00178 
00179   freewbmp (wbmp);
00180 
00181   return (im);
00182 }
00183 
00184 
00185 /* gdImageCreateFromWBMP
00186    ** ---------------------
00187  */
00188 BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMP (FILE * inFile)
00189 {
00190   gdImagePtr im;
00191   gdIOCtx *in = gdNewFileCtx (inFile);
00192   im = gdImageCreateFromWBMPCtx (in);
00193   in->gd_free (in);
00194   return (im);
00195 }
00196 
00197 BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPPtr (int size, void *data)
00198 {
00199   gdImagePtr im;
00200   gdIOCtx *in = gdNewDynamicCtxEx (size, data, 0);
00201   im = gdImageCreateFromWBMPCtx (in);
00202   in->gd_free (in);
00203   return im;
00204 }
00205 
00206 /* gdImageWBMP
00207    ** -----------
00208  */
00209 BGD_DECLARE(void) gdImageWBMP (gdImagePtr im, int fg, FILE * outFile)
00210 {
00211   gdIOCtx *out = gdNewFileCtx (outFile);
00212   gdImageWBMPCtx (im, fg, out);
00213   out->gd_free (out);
00214 }
00215 
00216 /* gdImageWBMPPtr
00217    ** --------------
00218  */
00219 BGD_DECLARE(void *)
00220 gdImageWBMPPtr (gdImagePtr im, int *size, int fg)
00221 {
00222   void *rv;
00223   gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
00224   gdImageWBMPCtx (im, fg, out);
00225   rv = gdDPExtractData (out, size);
00226   out->gd_free (out);
00227   return rv;
00228 }
00229 
00230