/* gdImageWBMPCtx * -------------- * Write the image as a wbmp file * Parameters are: * image: gd image structure; * fg: the index of the foreground color. any other value will be * considered as background and will not be written * out: the stream where to write */ BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out) { int x, y, pos; Wbmp *wbmp; /* create the WBMP */ if((wbmp = createwbmp(gdImageSX(image), gdImageSY(image), WBMP_WHITE)) == NULL) { fprintf(stderr, "Could not create WBMP\n"); return; } /* fill up the WBMP structure */ pos = 0; for(y = 0; y < gdImageSY(image); y++) { for(x = 0; x < gdImageSX(image); x++) { if(gdImageGetPixel(image, x, y) == fg) { wbmp->bitmap[pos] = WBMP_BLACK; } pos++; } } /* write the WBMP to a gd file descriptor */ if(writewbmp(wbmp, &gd_putout, out)) { fprintf(stderr, "Could not save WBMP\n"); } /* des submitted this bugfix: gdFree the memory. */ freewbmp(wbmp); }
/* gdImageCreateFromWBMPCtx ** ------------------------ ** Create a gdImage from a WBMP file input from an gdIOCtx */ BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPCtx (gdIOCtx * infile) { /* FILE *wbmp_file; */ Wbmp *wbmp; gdImagePtr im = NULL; int black, white; int col, row, pos; if (readwbmp (&gd_getin, infile, &wbmp)) return (NULL); if (!(im = gdImageCreate (wbmp->width, wbmp->height))) { freewbmp (wbmp); return (NULL); } /* create the background color */ white = gdImageColorAllocate (im, 255, 255, 255); /* create foreground color */ black = gdImageColorAllocate (im, 0, 0, 0); /* fill in image (in a wbmp 1 = white/ 0 = black) */ pos = 0; for (row = 0; row < wbmp->height; row++) { for (col = 0; col < wbmp->width; col++) { if (wbmp->bitmap[pos++] == WBMP_WHITE) { gdImageSetPixel (im, col, row, white); } else { gdImageSetPixel (im, col, row, black); } } } freewbmp (wbmp); return (im); }
/* Main function * ------------- */ int main(int argc, char *argv[]) { FILE *wbmp_file; Wbmp *wbmp; wbmp_file = fopen(argv[1], "rb"); if(wbmp_file) { readwbmp(&getin, wbmp_file, &wbmp); #ifdef __VIEW #ifdef __DEBUG printf("\nVIEWING IMAGE\n"); #endif printwbmp(wbmp); #endif #ifdef __WRITE #ifdef __DEBUG printf("\nDUMPING WBMP to STDOUT\n"); #endif writewbmp(wbmp, &putout, stdout); #endif freewbmp(wbmp); fclose(wbmp_file); } }