]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-bmp.c
Remove svn:keywords since they cause svn_load_dirs.pl to complain about every file.
[thirdparty/cups.git] / filter / image-bmp.c
1 /*
2 * "$Id: image-bmp.c 177 2006-06-21 00:20:03Z jlovell $"
3 *
4 * BMP image routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1993-2006 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * _cupsImageReadBMP() - Read a BMP image file.
29 * read_word() - Read a 16-bit unsigned integer.
30 * read_dword() - Read a 32-bit unsigned integer.
31 * read_long() - Read a 32-bit signed integer.
32 */
33
34 /*
35 * Include necessary headers...
36 */
37
38 #include "image-private.h"
39
40
41 /*
42 * Constants for the bitmap compression...
43 */
44
45 # define BI_RGB 0 /* No compression - straight BGR data */
46 # define BI_RLE8 1 /* 8-bit run-length compression */
47 # define BI_RLE4 2 /* 4-bit run-length compression */
48 # define BI_BITFIELDS 3 /* RGB bitmap with RGB masks */
49
50
51 /*
52 * Local functions...
53 */
54
55 static unsigned short read_word(FILE *fp);
56 static unsigned int read_dword(FILE *fp);
57 static int read_long(FILE *fp);
58
59
60 /*
61 * '_cupsImageReadBMP()' - Read a BMP image file.
62 */
63
64 int /* O - Read status */
65 _cupsImageReadBMP(
66 cups_image_t *img, /* IO - cupsImage */
67 FILE *fp, /* I - cupsImage file */
68 cups_icspace_t primary, /* I - Primary choice for colorspace */
69 cups_icspace_t secondary, /* I - Secondary choice for colorspace */
70 int saturation, /* I - Color saturation (%) */
71 int hue, /* I - Color hue (degrees) */
72 const cups_ib_t *lut) /* I - Lookup table for gamma/brightness */
73 {
74 int offset, /* Offset to bitmap data */
75 info_size, /* Size of info header */
76 planes, /* Number of planes (always 1) */
77 depth, /* Depth of image (bits) */
78 compression, /* Type of compression */
79 image_size, /* Size of image in bytes */
80 colors_used, /* Number of colors used */
81 colors_important, /* Number of important colors */
82 bpp, /* Bytes per pixel */
83 x, y, /* Looping vars */
84 color, /* Color of RLE pixel */
85 count, /* Number of times to repeat */
86 temp, /* Temporary color */
87 align; /* Alignment bytes */
88 cups_ib_t bit, /* Bit in image */
89 byte; /* Byte in image */
90 cups_ib_t *in, /* Input pixels */
91 *out, /* Output pixels */
92 *ptr; /* Pointer into pixels */
93 cups_ib_t colormap[256][4]; /* Colormap */
94
95
96 (void)secondary;
97
98 /*
99 * Get the header...
100 */
101
102 getc(fp); /* Skip "BM" sync chars */
103 getc(fp);
104 read_dword(fp); /* Skip size */
105 read_word(fp); /* Skip reserved stuff */
106 read_word(fp);
107 offset = read_dword(fp);
108
109 fprintf(stderr, "DEBUG: offset = %d\n", offset);
110
111 if (offset < 0)
112 {
113 fprintf(stderr, "ERROR: Bad BMP offset %d\n", offset);
114 fclose(fp);
115 return (1);
116 }
117
118 /*
119 * Then the bitmap information...
120 */
121
122 info_size = read_dword(fp);
123 img->xsize = read_long(fp);
124 img->ysize = read_long(fp);
125 planes = read_word(fp);
126 depth = read_word(fp);
127 compression = read_dword(fp);
128 image_size = read_dword(fp);
129 img->xppi = read_long(fp) * 0.0254 + 0.5;
130 img->yppi = read_long(fp) * 0.0254 + 0.5;
131 colors_used = read_dword(fp);
132 colors_important = read_dword(fp);
133
134 if (img->xsize == 0 || img->xsize > CUPS_IMAGE_MAX_WIDTH ||
135 img->ysize == 0 || img->ysize > CUPS_IMAGE_MAX_HEIGHT ||
136 (depth != 1 && depth != 4 && depth != 8 && depth != 24))
137 {
138 fprintf(stderr, "ERROR: Bad BMP dimensions %ux%ux%d\n",
139 img->xsize, img->ysize, depth);
140 fclose(fp);
141 return (1);
142 }
143
144 if (colors_used < 0 || colors_used > 256)
145 {
146 fprintf(stderr, "ERROR: Bad BMP colormap size %d\n", colors_used);
147 fclose(fp);
148 return (1);
149 }
150
151 if (img->xppi == 0 || img->yppi == 0)
152 {
153 fprintf(stderr, "ERROR: Bad BMP resolution %dx%d PPI.\n",
154 img->xppi, img->yppi);
155 img->xppi = img->yppi = 128;
156 }
157
158 /*
159 * Make sure the resolution info is valid...
160 */
161
162 fprintf(stderr, "info_size = %d, xsize = %d, ysize = %d, planes = %d, depth = %d\n",
163 info_size, img->xsize, img->ysize, planes, depth);
164 fprintf(stderr, "compression = %d, image_size = %d, xppi = %d, yppi = %d\n",
165 compression, image_size, img->xppi, img->yppi);
166 fprintf(stderr, "colors_used = %d, colors_important = %d\n", colors_used,
167 colors_important);
168
169 if (info_size > 40)
170 for (info_size -= 40; info_size > 0; info_size --)
171 getc(fp);
172
173 /*
174 * Get colormap...
175 */
176
177 if (colors_used == 0 && depth <= 8)
178 colors_used = 1 << depth;
179
180 if (colors_used > 0)
181 fread(colormap, colors_used, 4, fp);
182
183 /*
184 * Setup image and buffers...
185 */
186
187 img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_RGB : primary;
188
189 cupsImageSetMaxTiles(img, 0);
190
191 in = malloc(img->xsize * 3);
192 bpp = cupsImageGetDepth(img);
193 out = malloc(img->xsize * bpp);
194
195 /*
196 * Read the image data...
197 */
198
199 color = 0;
200 count = 0;
201 align = 0;
202
203 for (y = img->ysize - 1; y >= 0; y --)
204 {
205 ptr = in;
206
207 switch (depth)
208 {
209 case 1 : /* Bitmap */
210 for (x = img->xsize, bit = 128, byte = 0; x > 0; x --)
211 {
212 if (bit == 128)
213 byte = getc(fp);
214
215 if (byte & bit)
216 {
217 *ptr++ = colormap[1][2];
218 *ptr++ = colormap[1][1];
219 *ptr++ = colormap[1][0];
220 }
221 else
222 {
223 *ptr++ = colormap[0][2];
224 *ptr++ = colormap[0][1];
225 *ptr++ = colormap[0][0];
226 }
227
228 if (bit > 1)
229 bit >>= 1;
230 else
231 bit = 128;
232 }
233
234 /*
235 * Read remaining bytes to align to 32 bits...
236 */
237
238 for (temp = (img->xsize + 7) / 8; temp & 3; temp ++)
239 getc(fp);
240 break;
241
242 case 4 : /* 16-color */
243 for (x = img->xsize, bit = 0xf0, temp = 0; x > 0; x --)
244 {
245 /*
246 * Get a new count as needed...
247 */
248
249 if (compression != BI_RLE4 && count == 0)
250 {
251 count = 2;
252 color = -1;
253 }
254
255 if (count == 0)
256 {
257 while (align > 0)
258 {
259 align --;
260 getc(fp);
261 }
262
263 if ((count = getc(fp)) == 0)
264 {
265 if ((count = getc(fp)) == 0)
266 {
267 /*
268 * End of line...
269 */
270
271 x ++;
272 continue;
273 }
274 else if (count == 1)
275 {
276 /*
277 * End of image...
278 */
279
280 break;
281 }
282 else if (count == 2)
283 {
284 /*
285 * Delta...
286 */
287
288 count = getc(fp) * getc(fp) * img->xsize;
289 color = 0;
290 }
291 else
292 {
293 /*
294 * Absolute...
295 */
296
297 color = -1;
298 align = ((4 - (count & 3)) / 2) & 1;
299 }
300 }
301 else
302 color = getc(fp);
303 }
304
305 /*
306 * Get a new color as needed...
307 */
308
309 count --;
310
311 if (bit == 0xf0)
312 {
313 if (color < 0)
314 temp = getc(fp);
315 else
316 temp = color;
317
318 /*
319 * Copy the color value...
320 */
321
322 *ptr++ = colormap[temp >> 4][2];
323 *ptr++ = colormap[temp >> 4][1];
324 *ptr++ = colormap[temp >> 4][0];
325 bit = 0x0f;
326 }
327 else
328 {
329 /*
330 * Copy the color value...
331 */
332
333 *ptr++ = colormap[temp & 15][2];
334 *ptr++ = colormap[temp & 15][1];
335 *ptr++ = colormap[temp & 15][0];
336 bit = 0xf0;
337 }
338 }
339 break;
340
341 case 8 : /* 256-color */
342 for (x = img->xsize; x > 0; x --)
343 {
344 /*
345 * Get a new count as needed...
346 */
347
348 if (compression != BI_RLE8)
349 {
350 count = 1;
351 color = -1;
352 }
353
354 if (count == 0)
355 {
356 while (align > 0)
357 {
358 align --;
359 getc(fp);
360 }
361
362 if ((count = getc(fp)) == 0)
363 {
364 if ((count = getc(fp)) == 0)
365 {
366 /*
367 * End of line...
368 */
369
370 x ++;
371 continue;
372 }
373 else if (count == 1)
374 {
375 /*
376 * End of image...
377 */
378
379 break;
380 }
381 else if (count == 2)
382 {
383 /*
384 * Delta...
385 */
386
387 count = getc(fp) * getc(fp) * img->xsize;
388 color = 0;
389 }
390 else
391 {
392 /*
393 * Absolute...
394 */
395
396 color = -1;
397 align = (2 - (count & 1)) & 1;
398 }
399 }
400 else
401 color = getc(fp);
402 }
403
404 /*
405 * Get a new color as needed...
406 */
407
408 if (color < 0)
409 temp = getc(fp);
410 else
411 temp = color;
412
413 count --;
414
415 /*
416 * Copy the color value...
417 */
418
419 *ptr++ = colormap[temp][2];
420 *ptr++ = colormap[temp][1];
421 *ptr++ = colormap[temp][0];
422 }
423 break;
424
425 case 24 : /* 24-bit RGB */
426 for (x = img->xsize; x > 0; x --, ptr += 3)
427 {
428 ptr[2] = getc(fp);
429 ptr[1] = getc(fp);
430 ptr[0] = getc(fp);
431 }
432
433 /*
434 * Read remaining bytes to align to 32 bits...
435 */
436
437 for (temp = img->xsize * 3; temp & 3; temp ++)
438 getc(fp);
439 break;
440 }
441
442 if (saturation != 100 || hue != 0)
443 cupsImageRGBAdjust(in, img->xsize, saturation, hue);
444
445 switch (img->colorspace)
446 {
447 default :
448 break;
449
450 case CUPS_IMAGE_WHITE :
451 cupsImageRGBToWhite(in, out, img->xsize);
452 break;
453
454 case CUPS_IMAGE_RGB :
455 cupsImageRGBToRGB(in, out, img->xsize);
456 break;
457
458 case CUPS_IMAGE_BLACK :
459 cupsImageRGBToBlack(in, out, img->xsize);
460 break;
461
462 case CUPS_IMAGE_CMY :
463 cupsImageRGBToCMY(in, out, img->xsize);
464 break;
465
466 case CUPS_IMAGE_CMYK :
467 cupsImageRGBToCMYK(in, out, img->xsize);
468 break;
469 }
470
471 if (lut)
472 cupsImageLut(out, img->xsize * bpp, lut);
473
474 _cupsImagePutRow(img, 0, y, img->xsize, out);
475 }
476
477 fclose(fp);
478 free(in);
479 free(out);
480
481 return (0);
482 }
483
484
485 /*
486 * 'read_word()' - Read a 16-bit unsigned integer.
487 */
488
489 static unsigned short /* O - 16-bit unsigned integer */
490 read_word(FILE *fp) /* I - File to read from */
491 {
492 unsigned char b0, b1; /* Bytes from file */
493
494 b0 = getc(fp);
495 b1 = getc(fp);
496
497 return ((b1 << 8) | b0);
498 }
499
500
501 /*
502 * 'read_dword()' - Read a 32-bit unsigned integer.
503 */
504
505 static unsigned int /* O - 32-bit unsigned integer */
506 read_dword(FILE *fp) /* I - File to read from */
507 {
508 unsigned char b0, b1, b2, b3; /* Bytes from file */
509
510 b0 = getc(fp);
511 b1 = getc(fp);
512 b2 = getc(fp);
513 b3 = getc(fp);
514
515 return ((((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
516 }
517
518
519 /*
520 * 'read_long()' - Read a 32-bit signed integer.
521 */
522
523 static int /* O - 32-bit signed integer */
524 read_long(FILE *fp) /* I - File to read from */
525 {
526 unsigned char b0, b1, b2, b3; /* Bytes from file */
527
528 b0 = getc(fp);
529 b1 = getc(fp);
530 b2 = getc(fp);
531 b3 = getc(fp);
532
533 return ((int)(((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
534 }
535
536
537 /*
538 * End of "$Id: image-bmp.c 177 2006-06-21 00:20:03Z jlovell $".
539 */