]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-bmp.c
2 * "$Id: image-bmp.c 6649 2007-07-11 21:46:42Z mike $"
4 * BMP image routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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.
26 * Include necessary headers...
29 #include "image-private.h"
33 * Constants for the bitmap compression...
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 */
46 static unsigned short read_word(FILE *fp
);
47 static unsigned int read_dword(FILE *fp
);
48 static int read_long(FILE *fp
);
52 * '_cupsImageReadBMP()' - Read a BMP image file.
55 int /* O - Read status */
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 */
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 */
93 getc(fp
); /* Skip "BM" sync chars */
95 read_dword(fp
); /* Skip size */
96 read_word(fp
); /* Skip reserved stuff */
98 offset
= read_dword(fp
);
100 fprintf(stderr
, "DEBUG: offset = %d\n", offset
);
104 fprintf(stderr
, "DEBUG: Bad BMP offset %d\n", offset
);
110 * Then the bitmap information...
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
);
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))
129 fprintf(stderr
, "DEBUG: Bad BMP dimensions %ux%ux%d\n",
130 img
->xsize
, img
->ysize
, depth
);
135 if (colors_used
< 0 || colors_used
> 256)
137 fprintf(stderr
, "DEBUG: Bad BMP colormap size %d\n", colors_used
);
142 if (img
->xppi
== 0 || img
->yppi
== 0)
144 fprintf(stderr
, "DEBUG: Bad BMP resolution %dx%d PPI.\n",
145 img
->xppi
, img
->yppi
);
146 img
->xppi
= img
->yppi
= 128;
150 * Make sure the resolution info is valid...
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
,
161 for (info_size
-= 40; info_size
> 0; info_size
--)
168 if (colors_used
== 0 && depth
<= 8)
169 colors_used
= 1 << depth
;
172 fread(colormap
, colors_used
, 4, fp
);
175 * Setup image and buffers...
178 img
->colorspace
= (primary
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_RGB
: primary
;
180 cupsImageSetMaxTiles(img
, 0);
182 in
= malloc(img
->xsize
* 3);
183 bpp
= cupsImageGetDepth(img
);
184 out
= malloc(img
->xsize
* bpp
);
187 * Read the image data...
194 for (y
= img
->ysize
- 1; y
>= 0; y
--)
200 case 1 : /* Bitmap */
201 for (x
= img
->xsize
, bit
= 128, byte
= 0; x
> 0; x
--)
208 *ptr
++ = colormap
[1][2];
209 *ptr
++ = colormap
[1][1];
210 *ptr
++ = colormap
[1][0];
214 *ptr
++ = colormap
[0][2];
215 *ptr
++ = colormap
[0][1];
216 *ptr
++ = colormap
[0][0];
226 * Read remaining bytes to align to 32 bits...
229 for (temp
= (img
->xsize
+ 7) / 8; temp
& 3; temp
++)
233 case 4 : /* 16-color */
234 for (x
= img
->xsize
, bit
= 0xf0, temp
= 0; x
> 0; x
--)
237 * Get a new count as needed...
240 if (compression
!= BI_RLE4
&& count
== 0)
254 if ((count
= getc(fp
)) == 0)
256 if ((count
= getc(fp
)) == 0)
279 count
= getc(fp
) * getc(fp
) * img
->xsize
;
289 align
= ((4 - (count
& 3)) / 2) & 1;
297 * Get a new color as needed...
310 * Copy the color value...
313 *ptr
++ = colormap
[temp
>> 4][2];
314 *ptr
++ = colormap
[temp
>> 4][1];
315 *ptr
++ = colormap
[temp
>> 4][0];
321 * Copy the color value...
324 *ptr
++ = colormap
[temp
& 15][2];
325 *ptr
++ = colormap
[temp
& 15][1];
326 *ptr
++ = colormap
[temp
& 15][0];
332 case 8 : /* 256-color */
333 for (x
= img
->xsize
; x
> 0; x
--)
336 * Get a new count as needed...
339 if (compression
!= BI_RLE8
)
353 if ((count
= getc(fp
)) == 0)
355 if ((count
= getc(fp
)) == 0)
378 count
= getc(fp
) * getc(fp
) * img
->xsize
;
388 align
= (2 - (count
& 1)) & 1;
396 * Get a new color as needed...
407 * Copy the color value...
410 *ptr
++ = colormap
[temp
][2];
411 *ptr
++ = colormap
[temp
][1];
412 *ptr
++ = colormap
[temp
][0];
416 case 24 : /* 24-bit RGB */
417 for (x
= img
->xsize
; x
> 0; x
--, ptr
+= 3)
425 * Read remaining bytes to align to 32 bits...
428 for (temp
= img
->xsize
* 3; temp
& 3; temp
++)
433 if (saturation
!= 100 || hue
!= 0)
434 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
436 switch (img
->colorspace
)
441 case CUPS_IMAGE_WHITE
:
442 cupsImageRGBToWhite(in
, out
, img
->xsize
);
445 case CUPS_IMAGE_RGB
:
446 cupsImageRGBToRGB(in
, out
, img
->xsize
);
449 case CUPS_IMAGE_BLACK
:
450 cupsImageRGBToBlack(in
, out
, img
->xsize
);
453 case CUPS_IMAGE_CMY
:
454 cupsImageRGBToCMY(in
, out
, img
->xsize
);
457 case CUPS_IMAGE_CMYK
:
458 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
463 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
465 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
477 * 'read_word()' - Read a 16-bit unsigned integer.
480 static unsigned short /* O - 16-bit unsigned integer */
481 read_word(FILE *fp
) /* I - File to read from */
483 unsigned char b0
, b1
; /* Bytes from file */
488 return ((b1
<< 8) | b0
);
493 * 'read_dword()' - Read a 32-bit unsigned integer.
496 static unsigned int /* O - 32-bit unsigned integer */
497 read_dword(FILE *fp
) /* I - File to read from */
499 unsigned char b0
, b1
, b2
, b3
; /* Bytes from file */
506 return ((((((b3
<< 8) | b2
) << 8) | b1
) << 8) | b0
);
511 * 'read_long()' - Read a 32-bit signed integer.
514 static int /* O - 32-bit signed integer */
515 read_long(FILE *fp
) /* I - File to read from */
517 unsigned char b0
, b1
, b2
, b3
; /* Bytes from file */
524 return ((int)(((((b3
<< 8) | b2
) << 8) | b1
) << 8) | b0
);
529 * End of "$Id: image-bmp.c 6649 2007-07-11 21:46:42Z mike $".