- sync to 1.8.3
parent
7232c8a82a
commit
dd92c2b0df
54
src/gd.c
54
src/gd.c
|
@ -1,3 +1,6 @@
|
|||
#ifndef _OSD_POSIX /* _OSD_POSIX defines *alloc() in stdlib.h */
|
||||
#include <malloc.h>
|
||||
#endif /*_OSD_POSIX*/
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
@ -5,6 +8,51 @@
|
|||
#include <zlib.h>
|
||||
#include "gd.h"
|
||||
|
||||
#ifdef _OSD_POSIX /* BS2000 uses the EBCDIC char set instead of ASCII */
|
||||
#define CHARSET_EBCDIC
|
||||
#define __attribute__(any) /*nothing*/
|
||||
#endif /*_OSD_POSIX*/
|
||||
|
||||
#ifndef CHARSET_EBCDIC
|
||||
#define ASC(ch) ch
|
||||
#else /*CHARSET_EBCDIC*/
|
||||
#define ASC(ch) gd_toascii[(unsigned char)ch]
|
||||
static const unsigned char gd_toascii[256] = {
|
||||
/*00*/ 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f,
|
||||
0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /*................*/
|
||||
/*10*/ 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97,
|
||||
0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /*................*/
|
||||
/*20*/ 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b,
|
||||
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /*................*/
|
||||
/*30*/ 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04,
|
||||
0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /*................*/
|
||||
/*40*/ 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5,
|
||||
0xe7, 0xf1, 0x60, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* .........`.<(+|*/
|
||||
/*50*/ 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef,
|
||||
0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x9f, /*&.........!$*);.*/
|
||||
/*60*/ 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5,
|
||||
0xc7, 0xd1, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /*-/........^,%_>?*/
|
||||
/*70*/ 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf,
|
||||
0xcc, 0xa8, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /*..........:#@'="*/
|
||||
/*80*/ 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
|
||||
0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /*.abcdefghi......*/
|
||||
/*90*/ 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
|
||||
0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /*.jklmnopqr......*/
|
||||
/*a0*/ 0xb5, 0xaf, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
|
||||
0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae, /*..stuvwxyz......*/
|
||||
/*b0*/ 0xa2, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc,
|
||||
0xbd, 0xbe, 0xac, 0x5b, 0x5c, 0x5d, 0xb4, 0xd7, /*...........[\]..*/
|
||||
/*c0*/ 0xf9, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
|
||||
0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /*.ABCDEFGHI......*/
|
||||
/*d0*/ 0xa6, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
|
||||
0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xdb, 0xfa, 0xff, /*.JKLMNOPQR......*/
|
||||
/*e0*/ 0xd9, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
|
||||
0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /*..STUVWXYZ......*/
|
||||
/*f0*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
|
||||
0x38, 0x39, 0xb3, 0x7b, 0xdc, 0x7d, 0xda, 0x7e /*0123456789.{.}.~*/
|
||||
};
|
||||
#endif /*CHARSET_EBCDIC*/
|
||||
|
||||
extern int gdCosT[1024];
|
||||
extern int gdSinT[1024];
|
||||
|
||||
|
@ -669,6 +717,9 @@ void gdImageChar(gdImagePtr im, gdFontPtr f, int x, int y,
|
|||
int fline;
|
||||
cx = 0;
|
||||
cy = 0;
|
||||
#ifdef CHARSET_EBCDIC
|
||||
c = ASC(c);
|
||||
#endif /*CHARSET_EBCDIC*/
|
||||
if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
|
||||
return;
|
||||
}
|
||||
|
@ -693,6 +744,9 @@ void gdImageCharUp(gdImagePtr im, gdFontPtr f,
|
|||
int fline;
|
||||
cx = 0;
|
||||
cy = 0;
|
||||
#ifdef CHARSET_EBCDIC
|
||||
c = ASC(c);
|
||||
#endif /*CHARSET_EBCDIC*/
|
||||
if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
|
||||
return;
|
||||
}
|
||||
|
|
5
src/gd.h
5
src/gd.h
|
@ -156,8 +156,13 @@ void gdImageColorTransparent(gdImagePtr im, int color);
|
|||
void gdImagePaletteCopy(gdImagePtr dst, gdImagePtr src);
|
||||
void gdImagePng(gdImagePtr im, FILE *out);
|
||||
void gdImagePngCtx(gdImagePtr im, gdIOCtx *out);
|
||||
|
||||
void gdImageWBMP(gdImagePtr image, int fg, FILE *out);
|
||||
void gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
|
||||
void *gdImageWBMPPtr(gdImagePtr im, int *size, int fg);
|
||||
gdImagePtr gdImageCreateFromWBMP(FILE *inFile);
|
||||
gdImagePtr gdImageCreateFromWBMPCtx(gdIOCtx *infile);
|
||||
|
||||
|
||||
void gdImageJpeg(gdImagePtr im, FILE *out, int quality);
|
||||
void gdImageJpegCtx(gdImagePtr im, gdIOCtx *out, int quality);
|
||||
|
|
|
@ -11,6 +11,10 @@
|
|||
* Caveat emptor.
|
||||
*
|
||||
* Copyright 2000 Doug Becker, mailto:thebeckers@home.com
|
||||
*
|
||||
* Modification 4/18/00 TBB: JPEG_DEBUG rather than just DEBUG,
|
||||
* so VC++ builds don't spew to standard output, causing
|
||||
* major CGI brain damage
|
||||
*/
|
||||
|
||||
#ifdef HAVE_JPEG
|
||||
|
@ -97,7 +101,7 @@ gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
|
|||
JDIMENSION nlines;
|
||||
char comment[255];
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef JPEG_DEBUG
|
||||
printf("gd-jpeg: gd JPEG version %s\n", GD_JPEG_VERSION);
|
||||
printf("gd-jpeg: JPEG library version %d, %d-bit sample values\n",
|
||||
JPEG_LIB_VERSION, BITS_IN_JSAMPLE);
|
||||
|
@ -107,7 +111,7 @@ gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
|
|||
printf("gd-jpeg: gd colormap index %d: (%d, %d, %d)\n", i,
|
||||
im->red[i], im->green[i], im->blue[i]);
|
||||
}
|
||||
#endif /* DEBUG */
|
||||
#endif /* JPEG_DEBUG */
|
||||
|
||||
memset(&cinfo, 0, sizeof(cinfo));
|
||||
memset(&jerr, 0, sizeof(jerr));
|
||||
|
@ -135,7 +139,7 @@ gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
|
|||
|
||||
/* If user requests interlace, translate that to progressive JPEG */
|
||||
if (gdImageGetInterlaced(im)) {
|
||||
#ifdef DEBUG
|
||||
#ifdef JPEG_DEBUG
|
||||
printf("gd-jpeg: interlace set, outputting progressive"
|
||||
" JPEG image\n");
|
||||
#endif
|
||||
|
@ -230,7 +234,7 @@ gdImageCreateFromJpegCtx(gdIOCtx *infile)
|
|||
int i, j, retval;
|
||||
JDIMENSION nrows;
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef JPEG_DEBUG
|
||||
printf("gd-jpeg: gd JPEG version %s\n", GD_JPEG_VERSION);
|
||||
printf("gd-jpeg: JPEG library version %d, %d-bit sample values\n",
|
||||
JPEG_LIB_VERSION, BITS_IN_JSAMPLE);
|
||||
|
@ -292,7 +296,7 @@ gdImageCreateFromJpegCtx(gdIOCtx *infile)
|
|||
fprintf(stderr, "gd-jpeg: warning: jpeg_start_decompress"
|
||||
" reports suspended data source\n");
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef JPEG_DEBUG
|
||||
printf("gd-jpeg: JPEG image information:");
|
||||
if (cinfo.saw_JFIF_marker)
|
||||
printf(" JFIF version %d.%.2d",
|
||||
|
@ -338,7 +342,7 @@ gdImageCreateFromJpegCtx(gdIOCtx *infile)
|
|||
}
|
||||
printf(" colorspace\n");
|
||||
fflush(stdout);
|
||||
#endif /* DEBUG */
|
||||
#endif /* JPEG_DEBUG */
|
||||
|
||||
gdImageInterlace(im, cinfo.progressive_mode != 0);
|
||||
|
||||
|
@ -363,7 +367,7 @@ gdImageCreateFromJpegCtx(gdIOCtx *infile)
|
|||
#error IJG JPEG library BITS_IN_JSAMPLE value must be 8 or 12
|
||||
#endif
|
||||
im->open[i] = 0;
|
||||
#ifdef DEBUG
|
||||
#ifdef JPEG_DEBUG
|
||||
printf("gd-jpeg: gd colormap index %d set to (%d, %d, %d)\n", i,
|
||||
im->red[i], im->green[i], im->blue[i]);
|
||||
#endif
|
||||
|
|
19
src/gd_png.c
19
src/gd_png.c
|
@ -168,6 +168,10 @@ gdImagePtr gdImageCreateFromPngCtx(gdIOCtx *infile)
|
|||
switch (color_type) {
|
||||
case PNG_COLOR_TYPE_PALETTE:
|
||||
png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "gd-png color_type is palette, colors: %d\n",
|
||||
num_palette);
|
||||
#endif /* DEBUG */
|
||||
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
||||
int real_num_trans = 0, idx_first_trans = -1;
|
||||
int min_trans = 256, idx_min_trans = -1;
|
||||
|
@ -205,9 +209,22 @@ gdImagePtr gdImageCreateFromPngCtx(gdIOCtx *infile)
|
|||
return NULL;
|
||||
}
|
||||
palette_allocated = TRUE;
|
||||
if (bit_depth < 8)
|
||||
{
|
||||
num_palette = 1<<bit_depth;
|
||||
for (i = 0; i < 256; ++i)
|
||||
{
|
||||
j = (255*i)/(num_palette-1);
|
||||
palette[i].red = palette[i].green = palette[i].blue = j;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
num_palette = 256;
|
||||
for (i = 0; i < 256; ++i) {
|
||||
for (i = 0; i < 256; ++i)
|
||||
{
|
||||
palette[i].red = palette[i].green = palette[i].blue = i;
|
||||
}
|
||||
}
|
||||
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
||||
png_get_tRNS(png_ptr, info_ptr, NULL, NULL, &trans_gray_rgb);
|
||||
|
|
418
src/gd_wbmp.c
418
src/gd_wbmp.c
|
@ -1,292 +1,208 @@
|
|||
/*
|
||||
Creation of wbmp image files with gd library
|
||||
gd_wbmp.c
|
||||
/*
|
||||
WBMP: Wireless Bitmap Type 0: B/W, Uncompressed Bitmap
|
||||
Specification of the WBMP format can be found in the file:
|
||||
SPEC-WAESpec-19990524.pdf
|
||||
You can download the WAP specification on: http://www.wapforum.com/
|
||||
|
||||
Copyright (C) Maurice Szmurlo --- T-SIT --- January 2000
|
||||
(Maurice.Szmurlo@info.unicaen.fr)
|
||||
gd_wbmp.c
|
||||
|
||||
Permission to use, copy, modify, and distribute this software and its
|
||||
documentation for any purpose and without fee is hereby granted, provided
|
||||
that the above copyright notice appear in all copies and that both that
|
||||
copyright notice and this permission notice appear in supporting
|
||||
documentation. This software is provided "as is" without express or
|
||||
implied warranty.
|
||||
|
||||
-----------------------------------------------------------------------------------------
|
||||
Parts od this code are inspired by 'pbmtowbmp.c' and 'wbmptopbm.c' by
|
||||
Terje Sannum <terje@looplab.com>.
|
||||
**
|
||||
** Permission to use, copy, modify, and distribute this software and its
|
||||
** documentation for any purpose and without fee is hereby granted, provided
|
||||
** that the above copyright notice appear in all copies and that both that
|
||||
** copyright notice and this permission notice appear in supporting
|
||||
** documentation. This software is provided "as is" without express or
|
||||
** implied warranty.
|
||||
**
|
||||
-----------------------------------------------------------------------------------------
|
||||
|
||||
Todo:
|
||||
|
||||
gdCreateFromWBMP function for reading WBMP files
|
||||
|
||||
-----------------------------------------------------------------------------------------
|
||||
|
||||
Compilation:
|
||||
Copyright (C) Johan Van den Brande (johan@vandenbrande.com)
|
||||
|
||||
* testing the mbi data structure:
|
||||
gcc -Wall -W -D__MBI_DEBUG__ gd_wbmp.c -o mbi -L/usr/local/lib -lgd
|
||||
|
||||
* testing generation of wbmp images
|
||||
gcc -Wall -W -D__GD_WBMP_DEBUG__ gd_wbmp.c -o gd_wbmp -L/usr/local/lib -lgd -lpng
|
||||
|
||||
* simply making the object file
|
||||
gcc -c -Wall -W gd_wbmp.c -o gd_wbmp.o
|
||||
Fixed: gdImageWBMPPtr, gdImageWBMP
|
||||
|
||||
Recoded: gdImageWBMPCtx for use with my wbmp library
|
||||
(wbmp library included, but you can find the latest distribution
|
||||
at http://www.vandenbrande.com/wbmp)
|
||||
|
||||
Implemented: gdImageCreateFromWBMPCtx, gdImageCreateFromWBMP
|
||||
|
||||
---------------------------------------------------------------------------
|
||||
|
||||
Parts of this code are from Maurice Smurlo.
|
||||
|
||||
|
||||
** Copyright (C) Maurice Szmurlo --- T-SIT --- January 2000
|
||||
** (Maurice.Szmurlo@info.unicaen.fr)
|
||||
|
||||
** Permission to use, copy, modify, and distribute this software and its
|
||||
** documentation for any purpose and without fee is hereby granted, provided
|
||||
** that the above copyright notice appear in all copies and that both that
|
||||
** copyright notice and this permission notice appear in supporting
|
||||
** documentation. This software is provided "as is" without express or
|
||||
** implied warranty.
|
||||
|
||||
---------------------------------------------------------------------------
|
||||
Parts od this code are inspired by 'pbmtowbmp.c' and 'wbmptopbm.c' by
|
||||
Terje Sannum <terje@looplab.com>.
|
||||
**
|
||||
** Permission to use, copy, modify, and distribute this software and its
|
||||
** documentation for any purpose and without fee is hereby granted, provided
|
||||
** that the above copyright notice appear in all copies and that both that
|
||||
** copyright notice and this permission notice appear in supporting
|
||||
** documentation. This software is provided "as is" without express or
|
||||
** implied warranty.
|
||||
**
|
||||
---------------------------------------------------------------------------
|
||||
|
||||
Todo:
|
||||
|
||||
gdCreateFromWBMP function for reading WBMP files
|
||||
|
||||
----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <gd.h>
|
||||
#include <gdfonts.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "gd.h"
|
||||
#include "wbmp.h"
|
||||
|
||||
/* *************************** Multi Byte Integer Funcionnalities *************************** */
|
||||
|
||||
/* **************************************************
|
||||
An mbi is an array of bytes.
|
||||
For 32 bit integers, 5 bytes are sufficient.
|
||||
/* gd_putout
|
||||
** ---------
|
||||
** Wrapper around gdPutC for use with writewbmp
|
||||
**
|
||||
*/
|
||||
typedef unsigned char mbi_t[6];
|
||||
int gd_putout( int i, void * out )
|
||||
{
|
||||
gdPutC( i , (gdIOCtx *) out );
|
||||
}
|
||||
|
||||
|
||||
/* **************************************************
|
||||
initialization of an mbi in order to be used by the int2mbi function
|
||||
/* gd_getin
|
||||
** --------
|
||||
** Wrapper around gdGetC for use with readwbmp
|
||||
**
|
||||
*/
|
||||
static void mbiInit(mbi_t mbi) {
|
||||
int i;
|
||||
mbi[0] = mbi[5] = (unsigned char)0;
|
||||
for(i=1; i<5; mbi[i++] = 0x80);
|
||||
int gd_getin( void *in )
|
||||
{
|
||||
return( gdGetC( ( gdIOCtx *) in ) );
|
||||
}
|
||||
|
||||
|
||||
/* **************************************************
|
||||
Displays the content of the mbi as a serie of hexa bytes.
|
||||
For debuging purpose only
|
||||
*/
|
||||
#ifdef __MBI_DEBUG__
|
||||
static void mbiDisplay(mbi_t mbi, FILE *out) {
|
||||
unsigned int i;
|
||||
fprintf(out, "MBI:%d [", (unsigned int)mbi[0]);
|
||||
if(mbi[0] > 0)
|
||||
fprintf(out, "%X", (unsigned int)mbi[1]);
|
||||
for(i=2; i<=mbi[0]; i++)
|
||||
fprintf(out, ":%02X", (unsigned int)mbi[i]);
|
||||
fprintf(out, "]\n");
|
||||
}
|
||||
#endif /* __MBI_DEBUG__ */
|
||||
|
||||
|
||||
/* **************************************************
|
||||
writes the bytes constituing the mbi on the stream 'out'
|
||||
*/
|
||||
static void mbiPrint(mbi_t mbi, gdIOCtx *out) {
|
||||
unsigned int i;
|
||||
for(i=1; i<=(unsigned int)mbi[0]; gdPutC(mbi[i++], out));
|
||||
}
|
||||
|
||||
|
||||
/* **************************************************
|
||||
Conversion routines:
|
||||
unsigned int -> multi byte integer
|
||||
multi byte integer -> unsigned int
|
||||
*/
|
||||
static void int2mbi(mbi_t mbi, unsigned int val) {
|
||||
unsigned int i, j;
|
||||
mbiInit(mbi);
|
||||
for(i=5; i>0; i--) {
|
||||
mbi[i] = mbi[i] | (val & 0x7F);
|
||||
val = val >> 7;
|
||||
}
|
||||
for(j=1; (mbi[j] & 0x7F) == 0 && j<=5; j++);
|
||||
mbi[0] = (unsigned char)(5-j+1);
|
||||
for(i=1; i<=mbi[0]; mbi[i++] = mbi[j++]);
|
||||
}
|
||||
|
||||
static unsigned int mbi2int(mbi_t mbi) {
|
||||
unsigned int res=0, i;
|
||||
for(i=0; i<(unsigned int)mbi[0]; i++)
|
||||
res = (res << 7) | (unsigned int) ( mbi[i+1] & 0x7F );
|
||||
return res;
|
||||
}
|
||||
|
||||
#ifdef __MBI_DEBUG__
|
||||
int main(void) {
|
||||
mbi_t mbi;
|
||||
fprintf(stdout, "testing Multi Byte Integers:\n");
|
||||
|
||||
int2mbi(mbi, 0);
|
||||
mbiDisplay(mbi, stdout);
|
||||
fprintf(stdout, ">> = %u\n", mbi2int(mbi));
|
||||
|
||||
/* 0xA0 and 0x60 are the two examples from the wbmp specs */
|
||||
int2mbi(mbi, 0xA0);
|
||||
mbiDisplay(mbi, stdout);
|
||||
fprintf(stdout, ">> = %u\n", mbi2int(mbi));
|
||||
|
||||
int2mbi(mbi, 0x60);
|
||||
mbiDisplay(mbi, stdout);
|
||||
fprintf(stdout, ">> = %u\n", mbi2int(mbi));
|
||||
|
||||
int2mbi(mbi, 1024+512);
|
||||
mbiDisplay(mbi, stdout);
|
||||
fprintf(stdout, ">> = %u\n", mbi2int(mbi));
|
||||
|
||||
int2mbi(mbi, 1024+512+1);
|
||||
mbiDisplay(mbi, stdout);
|
||||
fprintf(stdout, ">> = %u\n", mbi2int(mbi));
|
||||
|
||||
int2mbi(mbi, 775432);
|
||||
mbiDisplay(mbi, stdout);
|
||||
fprintf(stdout, ">> = %u\n", mbi2int(mbi));
|
||||
|
||||
int2mbi(mbi, 405589432);
|
||||
mbiDisplay(mbi, stdout);
|
||||
fprintf(stdout, ">> = %u\n", mbi2int(mbi));
|
||||
|
||||
int2mbi(mbi, UINT_MAX);
|
||||
mbiDisplay(mbi, stdout);
|
||||
fprintf(stdout, ">> = %u\n", mbi2int(mbi));
|
||||
|
||||
fprintf(stdout, "\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* *************************** GD interface *************************** */
|
||||
|
||||
/*
|
||||
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
|
||||
/* 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
|
||||
*/
|
||||
void gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out) {
|
||||
|
||||
int x, y;
|
||||
mbi_t mbi;
|
||||
int x, y, pos;
|
||||
Wbmp *wbmp;
|
||||
|
||||
/* wbmp images are type 0: B&W; no compression; empty FixHeaderFlield */
|
||||
gdPutC(0, out); /* header */
|
||||
gdPutC(0, out); /* FixHeaderFlield */
|
||||
|
||||
/* width and height as mbi */
|
||||
int2mbi(mbi, gdImageSX(image)); /* width */
|
||||
mbiPrint(mbi, out);
|
||||
int2mbi(mbi, gdImageSY(image)); /* height */
|
||||
mbiPrint(mbi, out);
|
||||
/* create the WBMP */
|
||||
if ( (wbmp = createwbmp( gdImageSX(image), gdImageSY(image), WBMP_WHITE)) == NULL )
|
||||
fprintf(stderr, "Could not create WBMP\n");
|
||||
|
||||
/*
|
||||
now come the pixels as strings of bits padded with 0's at the end of the
|
||||
lines, if necessary
|
||||
*/
|
||||
for(y=0; y<gdImageSY(image); y++) {
|
||||
unsigned char nbPixs = 0; /* how many pixels already written */
|
||||
unsigned char pixels = 0; /* the 8 pixels string */
|
||||
for(x=0; x<gdImageSX(image); x++) {
|
||||
if(gdImageGetPixel(image, x, y) == fg) {
|
||||
pixels = pixels | (1 << (7-nbPixs));
|
||||
}
|
||||
nbPixs ++;
|
||||
if(nbPixs == 8) { /* 8 pixels written -> write to file */
|
||||
gdPutC(pixels, out);
|
||||
pixels = 0;
|
||||
nbPixs = 0;
|
||||
}
|
||||
/* 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++;
|
||||
}
|
||||
}
|
||||
/* if there are pixels left to write, write them */
|
||||
if(nbPixs != 0)
|
||||
gdPutC(pixels, out);
|
||||
}
|
||||
|
||||
/* 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: free the memory. */
|
||||
freewbmp(wbmp);
|
||||
}
|
||||
|
||||
/*
|
||||
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
|
||||
|
||||
/* gdImageCreateFromWBMPCtx
|
||||
** ------------------------
|
||||
** Create a gdImage from a WBMP file input from an gdIOCtx
|
||||
*/
|
||||
void gdImageWBMP(gdImagePtr image, int fg, FILE *out)
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/* gdImageCreateFromWBMP
|
||||
** ---------------------
|
||||
*/
|
||||
gdImagePtr gdImageCreateFromWBMP( FILE *inFile )
|
||||
{
|
||||
gdImagePtr im;
|
||||
gdIOCtx *in = gdNewFileCtx( inFile );
|
||||
im = gdImageCreateFromWBMPCtx( in );
|
||||
in->free(in);
|
||||
return (im);
|
||||
}
|
||||
|
||||
/* gdImageWBMP
|
||||
** -----------
|
||||
*/
|
||||
void gdImageWBMP(gdImagePtr im, int fg, FILE *outFile)
|
||||
{
|
||||
gdIOCtx *out = gdNewFileCtx(outFile);
|
||||
gdImageWBMPCtx(im, out);
|
||||
gdImageWBMPCtx(im, fg, out);
|
||||
out->free(out);
|
||||
}
|
||||
|
||||
void* gdImageWBMPPtr(gdImagePtr im, int *size)
|
||||
/* gdImageWBMPPtr
|
||||
** --------------
|
||||
*/
|
||||
void *gdImageWBMPPtr(gdImagePtr im, int *size, int fg)
|
||||
{
|
||||
void *rv;
|
||||
gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
|
||||
gdImageWBMPCtx(im, out);
|
||||
gdImageWBMPCtx(im, fg, out);
|
||||
rv = gdDPExtractData(out, size);
|
||||
out->free(out);
|
||||
return rv;
|
||||
}
|
||||
|
||||
#ifdef __GD_WBMP_DEBUG__
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
gdImagePtr image;
|
||||
int bg, fg;
|
||||
FILE *out;
|
||||
|
||||
if(argc != 2) {
|
||||
fprintf(stderr, "usage %s <wbmp file>\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fprintf(stdout, "testing gd generation of WBMP files\n");
|
||||
|
||||
fprintf(stdout, "Opening WBMP file '%s'\n", argv[1]);
|
||||
if(!(out = fopen(argv[1], "w"))) {
|
||||
fprintf(stderr, "Cannot open out file '%s'\n", argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fprintf(stdout, "Creating GD image and drawing...y\n");
|
||||
if(!(image = gdImageCreate(60, 30))) {
|
||||
fprintf(stderr, "Cannot create image 30x60\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* don't really care about rgb values */
|
||||
bg = gdImageColorAllocate(image, 0, 0, 0);
|
||||
fg = gdImageColorAllocate(image, 255, 255, 255);
|
||||
|
||||
/* draw something : */
|
||||
gdImageRectangle(image, 1, 1, gdImageSX(image)-2, gdImageSY(image)-2, fg);
|
||||
|
||||
/* write something interesting */
|
||||
gdImageString(image, gdFontSmall, 3, 0, "Hello", fg);
|
||||
|
||||
/* write the wbmp file */
|
||||
fprintf(stdout, "Writing WBMP file '%s':\n", argv[1]);
|
||||
gdImageWBMP(image, fg, out);
|
||||
fclose(out);
|
||||
fprintf(stdout, "Written.\nTry now to load the file '%s' with a WAP browser.\n", argv[1]);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fprintf(stdout, "\n");
|
||||
return 0;
|
||||
}
|
||||
#endif /* __GD_WBMP_DEBUG__ */
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
55
src/gdtest.c
55
src/gdtest.c
|
@ -9,11 +9,6 @@ int unlink(const char * filename) {
|
|||
#endif
|
||||
#include "gd.h"
|
||||
|
||||
/* A short program which converts a .png file into a .gd file, for
|
||||
your convenience in creating images on the fly from a
|
||||
basis image that must be loaded quickly. The .gd format
|
||||
is not intended to be a general-purpose format. */
|
||||
|
||||
void CompareImages(char *msg, gdImagePtr im1, gdImagePtr im2);
|
||||
|
||||
static int freadWrapper(void *context, char *buf, int len);
|
||||
|
@ -30,7 +25,8 @@ int main(int argc, char **argv)
|
|||
int colRed, colBlu;
|
||||
gdSource imgsrc;
|
||||
gdSink imgsnk;
|
||||
|
||||
int foreground;
|
||||
int i;
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage: gdtest filename.png\n");
|
||||
exit(1);
|
||||
|
@ -244,7 +240,7 @@ int main(int argc, char **argv)
|
|||
|
||||
in = fopen("test/gdtest_merge.png", "rb");
|
||||
if (!in) {
|
||||
fprintf(stderr, "gdtest.gd2 does not exist!\n");
|
||||
fprintf(stderr, "gdtest_merge.png does not exist!\n");
|
||||
exit(1);
|
||||
}
|
||||
im3 = gdImageCreateFromPng(in);
|
||||
|
@ -280,6 +276,51 @@ int main(int argc, char **argv)
|
|||
"to the input image manually. Some difference must be\n"
|
||||
"expected as JPEG is a lossy file format.\n");
|
||||
#endif /* HAVE_JPEG */
|
||||
/* Assume the color closest to black is the foreground
|
||||
color for the B&W wbmp image. */
|
||||
fprintf(stderr, "NOTE: the WBMP output image will NOT match the original unless the original\n"
|
||||
"is also black and white. This is OK!\n");
|
||||
foreground = gdImageColorClosest(im, 0, 0, 0);
|
||||
fprintf(stderr, "Foreground index is %d\n", foreground);
|
||||
if (foreground == -1) {
|
||||
fprintf(stderr, "Source image has no colors, skipping wbmp test.\n");
|
||||
} else {
|
||||
out = fopen("test/gdtest.wbmp", "wb");
|
||||
if (!out) {
|
||||
fprintf(stderr, "Can't create file test/gdtest.wbmp.\n");
|
||||
exit(1);
|
||||
}
|
||||
gdImageWBMP(im, foreground, out);
|
||||
fclose(out);
|
||||
in = fopen("test/gdtest.wbmp", "rb");
|
||||
if (!in) {
|
||||
fprintf(stderr, "Can't open file test/gdtest.wbmp.\n");
|
||||
exit(1);
|
||||
}
|
||||
im2 = gdImageCreateFromWBMP(in);
|
||||
fprintf(stderr, "WBMP has %d colors\n", gdImageColorsTotal(im2));
|
||||
fprintf(stderr, "WBMP colors are:\n");
|
||||
for (i = 0; (i < gdImageColorsTotal(im2)); i++) {
|
||||
fprintf(stderr, "%02X%02X%02X\n",
|
||||
gdImageRed(im2, i),
|
||||
gdImageGreen(im2, i),
|
||||
gdImageBlue(im2, i));
|
||||
}
|
||||
fclose(in);
|
||||
if (!im2) {
|
||||
fprintf(stderr, "gdImageCreateFromWBMP failed.\n");
|
||||
exit(1);
|
||||
}
|
||||
CompareImages("WBMP test (gdtest.png, gdtest.wbmp)", ref, im2);
|
||||
out = fopen("test/gdtest_wbmp_to_png.png", "wb");
|
||||
if (!out) {
|
||||
fprintf(stderr, "Can't create file test/gdtest_wbmp_to_png.png.\n");
|
||||
exit(1);
|
||||
}
|
||||
gdImagePng(im2, out);
|
||||
fclose(out);
|
||||
gdImageDestroy(im2);
|
||||
}
|
||||
gdImageDestroy(im);
|
||||
gdImageDestroy(ref);
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
|
||||
gd 1.8.1
|
||||
gd 1.8.3
|
||||
|
||||
A graphics library for fast image creation
|
||||
|
||||
Follow this link to the latest version of this document.
|
||||
|
||||
_HEY! READ THIS!_ gd 1.8.1 creates PNG, JPEG and WBMP images, not
|
||||
_HEY! READ THIS!_ gd 1.8.3 creates PNG, JPEG and WBMP images, not
|
||||
GIF images. This is a good thing. PNG is a more compact format, and
|
||||
full compression is available. JPEG works well with photographic
|
||||
images, and is still more compatible with the major Web browsers
|
||||
|
@ -18,7 +18,7 @@ Follow this link to the latest version of this document.
|
|||
well-compressed, modern image formats such as PNG and JPEG as soon
|
||||
as possible.
|
||||
|
||||
gd 1.8.1 _requires_ that the following libraries also be installed:
|
||||
gd 1.8.3 _requires_ that the following libraries also be installed:
|
||||
|
||||
libpng
|
||||
|
||||
|
@ -42,6 +42,8 @@ Follow this link to the latest version of this document.
|
|||
Table of Contents
|
||||
|
||||
* Credits and license terms
|
||||
* What's new in version 1.8.3?
|
||||
* What's new in version 1.8.2?
|
||||
* What's new in version 1.8.1?
|
||||
* What's new in version 1.8?
|
||||
* What's new in version 1.7.3?
|
||||
|
@ -95,7 +97,8 @@ COPYRIGHT STATEMENT FOLLOWS THIS LINE
|
|||
(C) 1994-1998, Thomas G. Lane. This software is based in part on
|
||||
the work of the Independent JPEG Group.
|
||||
|
||||
Portions relating to WBMP copyright 2000 Maurice Szmurlo.
|
||||
Portions relating to WBMP copyright 2000 Maurice Szmurlo and Johan
|
||||
Van den Brande.
|
||||
|
||||
_Permission has been granted to copy, distribute and modify gd in
|
||||
any context without fee, including a commercial application,
|
||||
|
@ -114,7 +117,7 @@ COPYRIGHT STATEMENT FOLLOWS THIS LINE
|
|||
particular purpose, with respect to this code and accompanying
|
||||
documentation.
|
||||
|
||||
Although their code does not appear in gd 1.8.1, the authors wish
|
||||
Although their code does not appear in gd 1.8.3, the authors wish
|
||||
to thank David Koblas, David Rowley, and Hutchison Avenue Software
|
||||
Corporation for their prior contributions.
|
||||
|
||||
|
@ -145,7 +148,7 @@ END OF COPYRIGHT STATEMENT
|
|||
|
||||
gd can also be used from Perl, courtesy of Lincoln Stein's GD.pm
|
||||
library, which uses gd as the basis for a set of Perl 5.x classes.
|
||||
Updated to gd 1.6 and up.
|
||||
Highly recommended.
|
||||
|
||||
Tcl
|
||||
|
||||
|
@ -157,6 +160,10 @@ END OF COPYRIGHT STATEMENT
|
|||
|
||||
Pascal enthusiasts should look into Michael Bradbury's gdfp package.
|
||||
|
||||
Haskell
|
||||
|
||||
A new gd interface is now available for Haskell programmers.
|
||||
|
||||
Any Language
|
||||
|
||||
There are, at the moment, at least three simple interpreters that
|
||||
|
@ -169,6 +176,26 @@ END OF COPYRIGHT STATEMENT
|
|||
* tgd, by Bradley K. Sherman
|
||||
* fly, by Martin Gleeson
|
||||
|
||||
What's new in version 1.8.3?
|
||||
|
||||
* WBMP output memory leak fixed
|
||||
* #include <gd.h> corrected to #include "gd.h" in gd_wbmp.c
|
||||
* Documented the fact that the source and output images shouldn't
|
||||
match in the WBMP test except for black and white source images
|
||||
|
||||
What's new in version 1.8.2?
|
||||
|
||||
* WBMP support debugged and improved by Johann Van den Brande
|
||||
* WBMP tests added to gdtest.c by Thomas Boutell
|
||||
* Use of platform-dependent 'install' command removed by Thomas
|
||||
Boutell
|
||||
* Comments added to Makefile warning users to juggle the order of
|
||||
the libraries if the linker complains; is there any portable way
|
||||
to do this automatically, short of using autoconf?
|
||||
* Documentation of gdImageCreateFromXpm corrected
|
||||
* Updated links to fast-moving, always dodging libpng and zlib web
|
||||
sites
|
||||
|
||||
What's new in version 1.8.1?
|
||||
|
||||
* Optional components no longer built by default (following the
|
||||
|
@ -466,11 +493,11 @@ END OF COPYRIGHT STATEMENT
|
|||
(Windows), please consult with an experienced user of your system.
|
||||
Sorry, we cannot answer questions about basic Internet skills.
|
||||
|
||||
Unpacking the archive will produce a directory called "gd-1.8.1".
|
||||
Unpacking the archive will produce a directory called "gd-1.8.3".
|
||||
|
||||
For Unix
|
||||
|
||||
cd to the 1.8.1 directory. Edit the Makefile with your preferred text
|
||||
cd to the 1.8.3 directory. Edit the Makefile with your preferred text
|
||||
editor and make any necessary changes to the settings at the top,
|
||||
especially if you want Xpm or TrueType support. Next, type "make". If
|
||||
you are the system administrator, and you wish to make the gd library
|
||||
|
@ -903,19 +930,18 @@ fclose(in);
|
|||
/* ... Use the image ... */
|
||||
gdImageDestroy(im);
|
||||
|
||||
gdImageCreateFromXpm(FILE *in) _(FUNCTION)_
|
||||
gdImageCreateFromXpm(char *filename) _(FUNCTION)_
|
||||
gdImageCreateFromXbm is called to load images from XPM X Window
|
||||
System color bitmap format files. This function is available
|
||||
only if HAVE_XPM is selected in the Makefile and the Xpm
|
||||
library is linked with the application. Invoke
|
||||
gdImageCreateFromXpm with an already opened pointer to a file
|
||||
containing the desired image. gdImageCreateFromXpm returns a
|
||||
gdImagePtr to the new image, or NULL if unable to load the
|
||||
image (most often because the file is corrupt or does not
|
||||
contain an XPM bitmap format image). gdImageCreateFromXpm does
|
||||
_not_ close the file. You can inspect the sx and sy members of
|
||||
the image to determine its size. The image must eventually be
|
||||
destroyed using gdImageDestroy().
|
||||
library is linked with the application. Unlike most gd file
|
||||
functions, the Xpm functions require filenames, not file
|
||||
pointers. gdImageCreateFromXpm returns a gdImagePtr to the new
|
||||
image, or NULL if unable to load the image (most often because
|
||||
the file is corrupt or does not contain an XPM bitmap format
|
||||
image). You can inspect the sx and sy members of the image to
|
||||
determine its size. The image must eventually be destroyed
|
||||
using gdImageDestroy().
|
||||
|
||||
|
||||
... inside a function ...
|
||||
|
|
|
@ -0,0 +1,350 @@
|
|||
/* WBMP
|
||||
** ----
|
||||
** WBMP Level 0: B/W, Uncompressed
|
||||
** This implements the WBMP format as specified in WAPSpec 1.1 and 1.2.
|
||||
** It does not support ExtHeaders as defined in the spec. The spec states
|
||||
** that a WAP client does not need to implement ExtHeaders.
|
||||
**
|
||||
** (c) 2000 Johan Van den Brande <johan@vandenbrande.com>
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wbmp.h"
|
||||
|
||||
//#define __TEST /* Compile with main function */
|
||||
//#define __DEBUG /* Extra verbose when with __TEST */
|
||||
//#define __WRITE /* readwbmp and writewbmp(stdout) */
|
||||
//#define __VIEW /* view the wbmp on stdout */
|
||||
|
||||
/* getmbi
|
||||
** ------
|
||||
** Get a multibyte integer from a generic getin function
|
||||
** 'getin' can be getc, with in = NULL
|
||||
** you can find getin as a function just above the main function
|
||||
** This way you gain a lot of flexibilty about how this package
|
||||
** reads a wbmp file.
|
||||
*/
|
||||
int getmbi ( int (*getin)(void *in), void *in )
|
||||
{
|
||||
int i, mbi = 0;
|
||||
|
||||
do
|
||||
{
|
||||
i = getin (in);
|
||||
if ( i<0 ) return (-1);
|
||||
mbi = mbi << 7 | i & 0x7f;
|
||||
} while ( i & 0x80 );
|
||||
|
||||
return ( mbi );
|
||||
}
|
||||
|
||||
|
||||
/* putmbi
|
||||
** ------
|
||||
** Put a multibyte intgerer in some kind of output stream
|
||||
** I work here with a function pointer, to make it as generic
|
||||
** as possible. Look at this function as an iterator on the
|
||||
** mbi integers it spits out.
|
||||
**
|
||||
*/
|
||||
int putmbi ( int i, int (*putout)(int c, void *out), void *out )
|
||||
{
|
||||
int cnt, l, accu;
|
||||
|
||||
/* Get number of septets */
|
||||
cnt=0;
|
||||
accu=0;
|
||||
while ( accu != i )
|
||||
accu+= i & 0x7f << 7*cnt++;
|
||||
|
||||
/* Produce the multibyte output */
|
||||
for ( l=cnt-1; l>0; l--)
|
||||
putout( 0x80 | (i & 0x7f << 7*l ) >> 7*l, out );
|
||||
|
||||
putout( i & 0x7f, out );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* skipheader
|
||||
** ----------
|
||||
** Skips the ExtHeader. Not needed for the moment
|
||||
**
|
||||
*/
|
||||
int skipheader( int (*getin)(void *in), void *in )
|
||||
{
|
||||
int i;
|
||||
|
||||
do
|
||||
{
|
||||
i = getin ( in );
|
||||
if (i<0)
|
||||
return (-1);
|
||||
} while ( i & 0x80 );
|
||||
|
||||
return ( 0 );
|
||||
}
|
||||
|
||||
/* create wbmp
|
||||
** -----------
|
||||
** create an empty wbmp
|
||||
**
|
||||
*/
|
||||
Wbmp *createwbmp(int width, int height, int color)
|
||||
{
|
||||
int i;
|
||||
|
||||
Wbmp *wbmp;
|
||||
if ( (wbmp = (Wbmp *) malloc( sizeof(Wbmp) )) == NULL)
|
||||
return (NULL);
|
||||
|
||||
if ( (wbmp->bitmap = (int *) malloc( sizeof(int)*width*height )) == NULL)
|
||||
{
|
||||
free( wbmp );
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
wbmp->width = width;
|
||||
wbmp->height= height;
|
||||
|
||||
for (i=0; i<width*height; wbmp->bitmap[i++] = color);
|
||||
|
||||
return(wbmp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* readwbmp
|
||||
** -------
|
||||
** Actually reads the WBMP format from an open file descriptor
|
||||
** It goes along by returning a pointer to a WBMP struct.
|
||||
**
|
||||
*/
|
||||
int readwbmp ( int (*getin)(void *in), void *in, Wbmp **return_wbmp )
|
||||
{
|
||||
int row, col, byte, pel, pos;
|
||||
Wbmp *wbmp;
|
||||
|
||||
if ( (wbmp = (Wbmp *) malloc( sizeof(Wbmp) )) == NULL)
|
||||
return (-1);
|
||||
|
||||
wbmp->type = getin( in );
|
||||
if ( wbmp->type != 0 )
|
||||
{
|
||||
free( wbmp );
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if ( skipheader( getin, in ) )
|
||||
return (-1);
|
||||
|
||||
|
||||
wbmp->width = getmbi ( getin, in );
|
||||
if ( wbmp->width == -1 )
|
||||
{
|
||||
free( wbmp );
|
||||
return (-1);
|
||||
}
|
||||
|
||||
wbmp->height = getmbi ( getin, in );
|
||||
if ( wbmp->height == -1 )
|
||||
{
|
||||
free( wbmp );
|
||||
return (-1);
|
||||
}
|
||||
|
||||
#ifdef __DEBUG
|
||||
printf("W: %d, H: %d\n", wbmp->width, wbmp->height);
|
||||
#endif
|
||||
|
||||
if ( (wbmp->bitmap = (int *) malloc( sizeof(int)*wbmp->width*wbmp->height )) == NULL)
|
||||
{
|
||||
free( wbmp );
|
||||
return (-1);
|
||||
}
|
||||
|
||||
#ifdef __DEBUG
|
||||
printf("DATA CONSTRUCTED\n");
|
||||
#endif
|
||||
|
||||
pos = 0;
|
||||
for ( row=0; row<wbmp->height; row++)
|
||||
{
|
||||
for ( col=0; col<wbmp->width; )
|
||||
{
|
||||
byte = getin( in );
|
||||
|
||||
for ( pel=7; pel>=0; pel--)
|
||||
{
|
||||
if ( col++ < wbmp->width )
|
||||
{
|
||||
if ( byte & 1<<pel )
|
||||
{
|
||||
wbmp->bitmap[pos] = WBMP_WHITE;
|
||||
}
|
||||
else
|
||||
{
|
||||
wbmp->bitmap[pos] = WBMP_BLACK;
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*return_wbmp = wbmp;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
/* writewbmp
|
||||
** ---------
|
||||
** Write a wbmp to a file descriptor
|
||||
**
|
||||
** Why not just giving a filedescriptor to this function?
|
||||
** Well, the incentive to write this function was the complete
|
||||
** integration in gd library from www.boutell.com. They use
|
||||
** their own io functions, so the passing of a function seemed to be
|
||||
** a logic(?) decision ...
|
||||
**
|
||||
*/
|
||||
int writewbmp( Wbmp *wbmp, int (*putout)(int c, void *out), void *out )
|
||||
{
|
||||
int row, col;
|
||||
int bitpos, octet;
|
||||
|
||||
/* Generate the header */
|
||||
putout(0, out); /* WBMP Type 0: B/W, Uncompressed bitmap */
|
||||
putout(0, out); /* FixHeaderField */
|
||||
|
||||
|
||||
|
||||
/* Size of the image */
|
||||
putmbi(wbmp->width, putout, out); /* width */
|
||||
putmbi(wbmp->height, putout, out); /* height*/
|
||||
|
||||
|
||||
/* Image data */
|
||||
for (row=0; row<wbmp->height; row++)
|
||||
{
|
||||
bitpos=8;
|
||||
octet=0;
|
||||
for (col=0; col<wbmp->width; col++)
|
||||
{
|
||||
octet|= ((wbmp->bitmap[ row*wbmp->width + col] == 1)?WBMP_WHITE:WBMP_BLACK) << --bitpos;
|
||||
if (bitpos == 0)
|
||||
{
|
||||
bitpos=8;
|
||||
putout(octet, out);
|
||||
octet=0;
|
||||
}
|
||||
}
|
||||
if (bitpos != 8)
|
||||
putout(octet, out);
|
||||
|
||||
}
|
||||
return (0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* freewbmp
|
||||
** --------
|
||||
** frees up memory occupied by a WBMP structure
|
||||
**
|
||||
*/
|
||||
void freewbmp( Wbmp *wbmp )
|
||||
{
|
||||
free( wbmp->bitmap );
|
||||
free( wbmp );
|
||||
}
|
||||
|
||||
|
||||
/* printwbmp
|
||||
** ---------
|
||||
** print a WBMP to stdout for visualisation
|
||||
**
|
||||
*/
|
||||
void printwbmp( Wbmp *wbmp )
|
||||
{
|
||||
int row, col;
|
||||
for( row=0; row<wbmp->height; row++)
|
||||
{
|
||||
for ( col=0; col<wbmp->width; col++)
|
||||
{
|
||||
if ( wbmp->bitmap[wbmp->width * row + col] == WBMP_BLACK )
|
||||
{
|
||||
putchar('#');
|
||||
}
|
||||
else
|
||||
{
|
||||
putchar(' ');
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __TEST
|
||||
|
||||
/* putout to file descriptor
|
||||
** -------------------------
|
||||
*/
|
||||
int putout(int c, void *out)
|
||||
{
|
||||
return(putc(c, (FILE *) out));
|
||||
}
|
||||
|
||||
/* getin from file descriptor
|
||||
** --------------------------
|
||||
*/
|
||||
int getin( void *in )
|
||||
{
|
||||
return( getc( (FILE *) in ) );
|
||||
}
|
||||
|
||||
|
||||
/* Main function
|
||||
** -------------
|
||||
**
|
||||
*/
|
||||
int main ( int argc, char *argv[] )
|
||||
{
|
||||
FILE *wbmp_file;
|
||||
Wbmp *wbmp;
|
||||
|
||||
wbmp_file = fopen(argv[1], "r");
|
||||
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 );
|
||||
}
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,47 @@
|
|||
/* WBMP
|
||||
** ----
|
||||
** WBMP Level 0: B/W, Uncompressed
|
||||
** This implements the WBMP format as specified in WAPSpec 1.1 and 1.2.
|
||||
** It does not support ExtHeaders as defined in the spec. The spec states
|
||||
** that a WAP client does not need to implement ExtHeaders.
|
||||
**
|
||||
** (c) 2000 Johan Van den Brande <johan@vandenbrande.com>
|
||||
**
|
||||
** Header file
|
||||
*/
|
||||
#ifndef __WBMP_H
|
||||
#define __WBMP_H 1
|
||||
|
||||
|
||||
/* WBMP struct
|
||||
** -----------
|
||||
** A Wireless bitmap structure
|
||||
**
|
||||
*/
|
||||
|
||||
typedef struct Wbmp_
|
||||
{
|
||||
int type; /* type of the wbmp */
|
||||
int width; /* width of the image */
|
||||
int height; /* height of the image */
|
||||
int *bitmap; /* pointer to data: 0 = WHITE , 1 = BLACK */
|
||||
} Wbmp;
|
||||
|
||||
#define WBMP_WHITE 1
|
||||
#define WBMP_BLACK 0
|
||||
|
||||
|
||||
/* Proto's
|
||||
** -------
|
||||
**
|
||||
*/
|
||||
int putmbi( int i, int (*putout)(int c, void *out), void *out);
|
||||
int getmbi ( int (*getin)(void *in), void *in );
|
||||
int skipheader( int (*getin)(void *in), void *in );
|
||||
Wbmp *createwbmp( int width, int height, int color );
|
||||
int readwbmp( int (*getin)(void *in), void *in, Wbmp **wbmp );
|
||||
int writewbmp( Wbmp *wbmp, int (*putout)( int c, void *out), void *out);
|
||||
void freewbmp( Wbmp *wbmp );
|
||||
void printwbmp( Wbmp *wbmp );
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue