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