]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-bmp.c
2 * "$Id: image-bmp.c 7221 2008-01-16 22:20:08Z mike $"
4 * BMP image routines for CUPS.
6 * Copyright 2007-2011 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
);
174 memset(colormap
, 0, sizeof(colormap
));
177 * Setup image and buffers...
180 img
->colorspace
= (primary
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_RGB
: primary
;
182 cupsImageSetMaxTiles(img
, 0);
184 bpp
= cupsImageGetDepth(img
);
186 if ((in
= malloc(img
->xsize
* 3)) == NULL
)
188 fputs("DEBUG: Unable to allocate memory!\n", stderr
);
193 if ((out
= malloc(img
->xsize
* bpp
)) == NULL
)
195 fputs("DEBUG: Unable to allocate memory!\n", stderr
);
202 * Read the image data...
209 for (y
= img
->ysize
- 1; y
>= 0; y
--)
215 case 1 : /* Bitmap */
216 for (x
= img
->xsize
, bit
= 128, byte
= 0; x
> 0; x
--)
223 *ptr
++ = colormap
[1][2];
224 *ptr
++ = colormap
[1][1];
225 *ptr
++ = colormap
[1][0];
229 *ptr
++ = colormap
[0][2];
230 *ptr
++ = colormap
[0][1];
231 *ptr
++ = colormap
[0][0];
241 * Read remaining bytes to align to 32 bits...
244 for (temp
= (img
->xsize
+ 7) / 8; temp
& 3; temp
++)
248 case 4 : /* 16-color */
249 for (x
= img
->xsize
, bit
= 0xf0, temp
= 0; x
> 0; x
--)
252 * Get a new count as needed...
255 if (compression
!= BI_RLE4
&& count
== 0)
269 if ((count
= getc(fp
)) == 0)
271 if ((count
= getc(fp
)) == 0)
294 count
= getc(fp
) * getc(fp
) * img
->xsize
;
304 align
= ((4 - (count
& 3)) / 2) & 1;
312 * Get a new color as needed...
325 * Copy the color value...
328 *ptr
++ = colormap
[temp
>> 4][2];
329 *ptr
++ = colormap
[temp
>> 4][1];
330 *ptr
++ = colormap
[temp
>> 4][0];
336 * Copy the color value...
339 *ptr
++ = colormap
[temp
& 15][2];
340 *ptr
++ = colormap
[temp
& 15][1];
341 *ptr
++ = colormap
[temp
& 15][0];
347 case 8 : /* 256-color */
348 for (x
= img
->xsize
; x
> 0; x
--)
351 * Get a new count as needed...
354 if (compression
!= BI_RLE8
)
368 if ((count
= getc(fp
)) == 0)
370 if ((count
= getc(fp
)) == 0)
393 count
= getc(fp
) * getc(fp
) * img
->xsize
;
403 align
= (2 - (count
& 1)) & 1;
411 * Get a new color as needed...
422 * Copy the color value...
425 *ptr
++ = colormap
[temp
][2];
426 *ptr
++ = colormap
[temp
][1];
427 *ptr
++ = colormap
[temp
][0];
431 case 24 : /* 24-bit RGB */
432 for (x
= img
->xsize
; x
> 0; x
--, ptr
+= 3)
440 * Read remaining bytes to align to 32 bits...
443 for (temp
= img
->xsize
* 3; temp
& 3; temp
++)
448 if (saturation
!= 100 || hue
!= 0)
449 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
451 switch (img
->colorspace
)
456 case CUPS_IMAGE_WHITE
:
457 cupsImageRGBToWhite(in
, out
, img
->xsize
);
460 case CUPS_IMAGE_RGB
:
461 cupsImageRGBToRGB(in
, out
, img
->xsize
);
464 case CUPS_IMAGE_BLACK
:
465 cupsImageRGBToBlack(in
, out
, img
->xsize
);
468 case CUPS_IMAGE_CMY
:
469 cupsImageRGBToCMY(in
, out
, img
->xsize
);
472 case CUPS_IMAGE_CMYK
:
473 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
478 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
480 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
492 * 'read_word()' - Read a 16-bit unsigned integer.
495 static unsigned short /* O - 16-bit unsigned integer */
496 read_word(FILE *fp
) /* I - File to read from */
498 unsigned char b0
, b1
; /* Bytes from file */
503 return ((b1
<< 8) | b0
);
508 * 'read_dword()' - Read a 32-bit unsigned integer.
511 static unsigned int /* O - 32-bit unsigned integer */
512 read_dword(FILE *fp
) /* I - File to read from */
514 unsigned char b0
, b1
, b2
, b3
; /* Bytes from file */
521 return ((((((b3
<< 8) | b2
) << 8) | b1
) << 8) | b0
);
526 * 'read_long()' - Read a 32-bit signed integer.
529 static int /* O - 32-bit signed integer */
530 read_long(FILE *fp
) /* I - File to read from */
532 unsigned char b0
, b1
, b2
, b3
; /* Bytes from file */
539 return ((int)(((((b3
<< 8) | b2
) << 8) | b1
) << 8) | b0
);
544 * End of "$Id: image-bmp.c 7221 2008-01-16 22:20:08Z mike $".