Back to index

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