2 // source code for the ImageLib BMP functions
4 // Copyright (C) 2001 M. Scott Heiman
7 // You may use the software for any purpose you see fit. You may modify
8 // it, incorporate it in a commercial application, use it for school,
9 // even turn it in as homework. You must keep the Copyright in the
10 // header and source files. This software is not in the "Public Domain".
11 // You may use this software at your own risk. I have made a reasonable
12 // effort to verify that this software works in the manner I expect it to;
15 // THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
16 // WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING
17 // WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A
18 // PARTICULAR PURPOSE. IN NO EVENT SHALL MICHAEL S. HEIMAN BE LIABLE TO
19 // YOU OR ANYONE ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR
20 // CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING
21 // WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE,
22 // OR THE CLAIMS OF THIRD PARTIES, WHETHER OR NOT MICHAEL S. HEIMAN HAS
23 // BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
24 // ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
25 // POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
38 static const unsigned short BMP_ID = 0x4D42;
41 ReadBMP - reads the image data from a BMP files and stores it in a
45 filename - the name of the file to be opened
48 img - the BMGImageStruct containing the image data
51 BMGError - if the file could not be read or a resource error occurred
52 BMG_OK - if the file was read and the data was stored in img
55 will not read BMP files using BI_RLE8, BI_RLE4, or BI_BITFIELDS
57 BMGError ReadBMP( const char *filename,
58 struct BMGImageStruct *img )
63 unsigned char *p, *q; /*, *q_end; */
64 /* unsigned int cnt; */
68 BITMAPFILEHEADER bmfh;
69 BITMAPINFOHEADER bmih;
74 unsigned int DIBScanWidth;
75 unsigned int bit_size, rawbit_size;
76 unsigned char *rawbits = NULL;
78 SetLastBMGError( BMG_OK );
81 { error = (int) errInvalidBMGImage; goto err_jmp; }
84 file = fopen( filename, "rb" );
86 { error = (int) errFileOpen; goto err_jmp; }
88 /* read the file header */
89 if ( fread( (void *)&bmfh, sizeof(BITMAPFILEHEADER), 1, file ) != 1 )
90 { error = (int) errFileRead; goto err_jmp; }
92 /* confirm that this is a BMP file */
93 if ( bmfh.bfType != BMP_ID )
94 { error = (int) errUnsupportedFileFormat; goto err_jmp; }
96 /* read the bitmap info header */
97 if ( fread( (void *)&bmih, sizeof(BITMAPINFOHEADER), 1, file ) != 1 )
98 { error = (int) errFileRead; goto err_jmp; }
100 /* abort if this is an unsupported format */
101 if ( bmih.biCompression != BI_RGB )
102 { printf("planes: %i bits: %i type: %i ", bmih.biPlanes, bmih.biBitCount, bmih.biCompression); error = (int) errUnsupportedFileFormat; goto err_jmp; }
104 img->bits_per_pixel = (unsigned char)bmih.biBitCount;
105 img->width = bmih.biWidth;
106 img->height = bmih.biHeight;
107 if ( img->bits_per_pixel <= 8 )
109 img->palette_size = (unsigned short)bmih.biClrUsed;
110 img->bytes_per_palette_entry = 4U;
113 tmp = AllocateBMGImage( img );
115 { error = (int) tmp; goto err_jmp; }
117 /* read palette if necessary */
118 if ( img->bits_per_pixel <= 8 )
120 if ( fread( (void *)img->palette, sizeof(RGBQUAD), img->palette_size,
121 file ) != (unsigned int)img->palette_size )
123 error = (int) errFileRead;
129 DIBScanWidth = ( img->bits_per_pixel * img->width + 7 ) / 8;
130 if ( DIBScanWidth %4 )
131 DIBScanWidth += 4 - DIBScanWidth % 4;
133 bit_size = img->scan_width * img->height;
135 /* allocate memory for the raw bits */
136 if ( bmih.biCompression != BI_RGB )
137 rawbit_size = bmfh.bfSize - bmfh.bfOffBits;
139 rawbit_size = DIBScanWidth * img->height;
141 rawbits = (unsigned char *)calloc( rawbit_size, 1 );
142 if ( rawbits == NULL )
143 { error = (int) errMemoryAllocation; goto err_jmp; }
145 if ( fread( (void *)rawbits, sizeof(unsigned char), rawbit_size, file )
148 error = (int) errFileRead;
152 if ( bmih.biCompression == BI_RGB )
155 for ( q = img->bits; q < img->bits + bit_size;
156 q += img->scan_width, p += DIBScanWidth )
158 memcpy( (void *)q, (void *)p, img->scan_width );
162 /* swap rows if necessary */
163 if ( bmih.biHeight < 0 )
165 for ( i = 0; i < (int)(img->height) / 2; i++ )
167 p = img->bits + i * img->scan_width;
168 q = img->bits + ((img->height) - i - 1 ) * img->scan_width;
169 memcpy( (void *)rawbits, (void *)p, img->scan_width );
170 memcpy( (void *)p, (void *)q, img->scan_width );
171 memcpy( (void *)q, (void *)rawbits, img->scan_width );
183 if ( rawbits != NULL )
186 SetLastBMGError( (BMGError)error );
187 return (BMGError)error;
192 WriteBMP - writes the contents of an BMGImageStruct to a bmp file.
195 filename - the name of the file to be opened
196 img - the BMGImageStruct containing the image data
199 BMGError - if a write error or a resource error occurred
200 BMG_OK - if the data was successfilly stored in filename
203 will not write BMP files using BI_RLE8, BI_RLE4, or BI_BITFIELDS
205 BMGError WriteBMP( const char *filename,
206 struct BMGImageStruct img )
208 FILE * volatile file = NULL;
212 unsigned char * volatile bits = NULL;
213 unsigned int DIBScanWidth;
214 unsigned int BitsPerPixel;
215 unsigned int bit_size; /*, new_bit_size; */
216 /* unsigned int rawbit_size; */
217 unsigned char *p, *q, *r, *t;
218 /* unsigned int cnt; */
219 unsigned char * volatile pColor = NULL;
221 BITMAPFILEHEADER bmfh;
222 BITMAPINFOHEADER bmih;
224 SetLastBMGError( BMG_OK );
227 error = setjmp(err_jmp);
236 SetLastBMGError((BMGError)error);
237 return (BMGError) error;
240 if ( img.bits == NULL )
241 longjmp( err_jmp, (int)errInvalidBMGImage );
243 file = fopen( filename, "wb" );
245 longjmp( err_jmp, (int)errFileOpen );
247 /* abort if we do not support the data */
248 if ( img.palette != NULL && img.bytes_per_palette_entry < 3 )
249 longjmp( err_jmp, (int)errInvalidBMGImage );
251 /* calculate dimensions */
252 BitsPerPixel = img.bits_per_pixel < 32 ? img.bits_per_pixel : 24U;
253 DIBScanWidth = ( BitsPerPixel * img.width + 7 ) / 8;
254 if ( DIBScanWidth % 4 )
255 DIBScanWidth += 4 - DIBScanWidth % 4;
256 bit_size = DIBScanWidth * img.height;
257 /* rawbit_size = BITScanWidth * img.height; */
259 /* allocate memory for bit array - assume that compression will
260 // actually compress the bitmap */
261 bits = (unsigned char *)calloc( bit_size, 1 );
263 longjmp( err_jmp, (int)errMemoryAllocation );
265 /* some initialization */
266 memset( (void *)&bmih, 0, sizeof(BITMAPINFOHEADER) );
267 bmih.biSize = sizeof(BITMAPINFOHEADER);
268 bmih.biWidth = img.width;
269 bmih.biHeight = img.height;
271 /* 32-bit images will be stored as 24-bit images to save space. The BMP
272 format does not use the high word and I do not want to store alpha
273 components in an image format that does not recognize it */
274 bmih.biBitCount = BitsPerPixel;
275 bmih.biCompression = BI_RGB; // assumed
276 bmih.biSizeImage = bit_size; // assumed
277 bmih.biClrUsed = img.palette == NULL ? 0 : img.palette_size;
278 bmih.biClrImportant = img.palette == NULL ? 0 : img.palette_size;
280 /* if we are not compressed then copy the raw bits to bits */
281 if ( bmih.biCompression == BI_RGB )
284 /* simple memcpy's for images containing < 32-bits per pixel */
285 if ( img.bits_per_pixel < 32 )
287 for ( q = bits; q < bits + bit_size; q += DIBScanWidth,
288 p += img.scan_width )
290 memcpy( (void *)q, (void *)p, img.scan_width );
293 /* store 32-bit images as 24-bit images to save space. alpha terms
297 DIBScanWidth = 3 * img.width;
298 if ( DIBScanWidth % 4 )
299 DIBScanWidth += 4 - DIBScanWidth % 4;
301 for ( q = bits; q < bits + bit_size; q += DIBScanWidth,
302 p += img.scan_width )
305 for ( r = q; r < q + DIBScanWidth; r += 3, t += 4 )
306 memcpy( (void *)r, (void *)t, 3 );
311 /* create the palette if necessary */
312 if ( img.palette != NULL )
314 pColor = (unsigned char *)calloc( img.palette_size, sizeof(RGBQUAD) );
315 if ( pColor == NULL )
316 longjmp( err_jmp, (int)errMemoryAllocation );
318 if ( img.bytes_per_palette_entry == 3 )
321 for ( q = pColor + 1; q < pColor +img.palette_size*sizeof(RGBQUAD);
322 q += sizeof(RGBQUAD), p += 3 )
324 memcpy( (void *)pColor, (void *)p, 3 );
327 else /* img.bytes_per_palette_entry == 4 */
329 memcpy( (void *)pColor, (void *)img.palette,
330 img.palette_size * sizeof(RGBQUAD) );
334 /* now that we know how big everything is let's write the file */
335 memset( (void *)&bmfh, 0, sizeof(BITMAPFILEHEADER) );
336 bmfh.bfType = BMP_ID;
337 bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
338 img.palette_size * sizeof(RGBQUAD);
339 bmfh.bfSize = bmfh.bfOffBits + bit_size;
341 if ( fwrite( (void *)&bmfh, sizeof(BITMAPFILEHEADER), 1, file ) != 1 )
342 longjmp( err_jmp, (int)errFileWrite );
344 if ( fwrite( (void *)&bmih, sizeof(BITMAPINFOHEADER), 1, file ) != 1 )
345 longjmp( err_jmp, (int)errFileWrite );
347 if ( pColor != NULL )
349 if ( fwrite( (void *)pColor, sizeof(RGBQUAD), img.palette_size, file )
350 != (unsigned int)img.palette_size )
352 longjmp( err_jmp, (int)errFileWrite );
356 if ( fwrite( (void *)bits, sizeof(unsigned char), bit_size, file )
359 longjmp( err_jmp, (int)errFileWrite );
364 if ( pColor != NULL )
369 #ifdef _NEVER_DEFINE_THIS_DEF_
370 /* following code is not tested. I keep it here in case I ever find a BMP
371 file that is compressed and I want to test it */
372 else if ( bmih.biCompression == BI_RLE8 )
374 bmih.biCompression = BI_RGB;
375 bmih.biSizeImage = DIBScanWidth * img.height;
379 while ( q < img.bits + bit_size && p < rawbits + rawbit_size && EOBMP )
381 cnt = (unsigned int)*p;
387 cnt = (unsigned int)*p;
401 q += *p; /* columns */
403 q += (*p)*BITScanWidth; /* rows */
407 /* copy *p duplicates of *(p++) into the bit array */
410 cnt = (unsigned int)*p;
428 /* if compression is desired && possible then attempt compression. The
429 // following logic will try to compress the data. If the compressed data
430 // requires more space than the uncompressed data then the bits will be
431 // stored in an uncompressed format */
432 if ( try_compression != 0 && img.bits_per_pixel == 8 )
438 while ( p < rawbits + rawbit_size && new_bit_size < bit_size )
441 while ( q < p + BITScanWidth )
444 while ( t < q + 255 && t < p + BITScanWidth )
446 /* look for non-repeats - absolute mode */
449 while ( *t != *(t+1) &&
451 t < p + BITScanWidth )
457 *r++ = (unsigned char)cnt;
458 memcpy( (void *)r, (void *)q, cnt );
461 new_bit_size += 1 + cnt;
464 /* else look for repeats */
467 while ( *t == *(t+1) &&
469 t < p + BITScanWidth )
478 *r++ = (unsigned char)cnt;
484 /* use absolute mode if we have reached the EOL &&
486 else if ( t >= p + BITScanWidth )
488 *r++ = (unsigned char)cnt;
489 memcpy( (void *)r, (void *)q, cnt );
492 new_bit_size += 1 + cnt;
498 /* put an EOL marker here */
508 /* put the EOB marker here */
509 if ( new_bit_size < bit_size - 2 )
516 new_bit_size = bit_size + 1;
518 /* if the compressed image will take less space then use it */
519 if ( new_bit_size < bit_size )
521 bmih.biCompression = BI_RLE8;
522 bmih.biSizeImage = bit_size = new_bit_size;