]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-bmp.c
2 * "$Id: image-bmp.c 4741 2005-10-02 04:25:52Z mike $"
4 * BMP image routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2005 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
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.
35 * Include necessary headers...
38 #include "image-private.h"
42 * Constants for the bitmap compression...
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 */
55 static unsigned short read_word(FILE *fp
);
56 static unsigned int read_dword(FILE *fp
);
57 static int read_long(FILE *fp
);
61 * '_cupsImageReadBMP()' - Read a BMP image file.
64 int /* O - Read status */
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 */
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 */
102 getc(fp
); /* Skip "BM" sync chars */
104 read_dword(fp
); /* Skip size */
105 read_word(fp
); /* Skip reserved stuff */
107 offset
= read_dword(fp
);
109 fprintf(stderr
, "DEBUG: offset = %d\n", offset
);
113 fprintf(stderr
, "ERROR: Bad BMP offset %d\n", offset
);
119 * Then the bitmap information...
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
);
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))
138 fprintf(stderr
, "ERROR: Bad BMP dimensions %ux%ux%d\n",
139 img
->xsize
, img
->ysize
, depth
);
144 if (colors_used
< 0 || colors_used
> 256)
146 fprintf(stderr
, "ERROR: Bad BMP colormap size %d\n", colors_used
);
151 if (img
->xppi
== 0 || img
->yppi
== 0)
153 fprintf(stderr
, "ERROR: Bad BMP resolution %dx%d PPI.\n",
154 img
->xppi
, img
->yppi
);
155 img
->xppi
= img
->yppi
= 128;
159 * Make sure the resolution info is valid...
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
,
170 for (info_size
-= 40; info_size
> 0; info_size
--)
177 if (colors_used
== 0 && depth
<= 8)
178 colors_used
= 1 << depth
;
181 fread(colormap
, colors_used
, 4, fp
);
184 * Setup image and buffers...
187 img
->colorspace
= (primary
== CUPS_IMAGE_RGB_CMYK
) ? CUPS_IMAGE_RGB
: primary
;
189 cupsImageSetMaxTiles(img
, 0);
191 in
= malloc(img
->xsize
* 3);
192 bpp
= cupsImageGetDepth(img
);
193 out
= malloc(img
->xsize
* bpp
);
196 * Read the image data...
203 for (y
= img
->ysize
- 1; y
>= 0; y
--)
205 if (img
->colorspace
== CUPS_IMAGE_RGB
)
212 case 1 : /* Bitmap */
213 for (x
= img
->xsize
, bit
= 128, byte
= 0; x
> 0; x
--)
220 *ptr
++ = colormap
[1][2];
221 *ptr
++ = colormap
[1][1];
222 *ptr
++ = colormap
[1][0];
226 *ptr
++ = colormap
[0][2];
227 *ptr
++ = colormap
[0][1];
228 *ptr
++ = colormap
[0][0];
238 * Read remaining bytes to align to 32 bits...
241 for (temp
= (img
->xsize
+ 7) / 8; temp
& 3; temp
++)
245 case 4 : /* 16-color */
246 for (x
= img
->xsize
, bit
= 0xf0, temp
= 0; x
> 0; x
--)
249 * Get a new count as needed...
252 if (compression
!= BI_RLE4
&& count
== 0)
266 if ((count
= getc(fp
)) == 0)
268 if ((count
= getc(fp
)) == 0)
291 count
= getc(fp
) * getc(fp
) * img
->xsize
;
301 align
= ((4 - (count
& 3)) / 2) & 1;
309 * Get a new color as needed...
322 * Copy the color value...
325 *ptr
++ = colormap
[temp
>> 4][2];
326 *ptr
++ = colormap
[temp
>> 4][1];
327 *ptr
++ = colormap
[temp
>> 4][0];
333 * Copy the color value...
336 *ptr
++ = colormap
[temp
& 15][2];
337 *ptr
++ = colormap
[temp
& 15][1];
338 *ptr
++ = colormap
[temp
& 15][0];
344 case 8 : /* 256-color */
345 for (x
= img
->xsize
; x
> 0; x
--)
348 * Get a new count as needed...
351 if (compression
!= BI_RLE8
)
365 if ((count
= getc(fp
)) == 0)
367 if ((count
= getc(fp
)) == 0)
390 count
= getc(fp
) * getc(fp
) * img
->xsize
;
400 align
= (2 - (count
& 1)) & 1;
408 * Get a new color as needed...
419 * Copy the color value...
422 *ptr
++ = colormap
[temp
][2];
423 *ptr
++ = colormap
[temp
][1];
424 *ptr
++ = colormap
[temp
][0];
428 case 24 : /* 24-bit RGB */
429 for (x
= img
->xsize
; x
> 0; x
--, ptr
+= 3)
437 * Read remaining bytes to align to 32 bits...
440 for (temp
= img
->xsize
* 3; temp
& 3; temp
++)
445 if (img
->colorspace
== CUPS_IMAGE_RGB
)
447 if (saturation
!= 100 || hue
!= 0)
448 cupsImageRGBAdjust(out
, img
->xsize
, saturation
, hue
);
452 if (saturation
!= 100 || hue
!= 0)
453 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
455 switch (img
->colorspace
)
460 case CUPS_IMAGE_WHITE
:
461 cupsImageRGBToWhite(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
);
479 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
481 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
493 * 'read_word()' - Read a 16-bit unsigned integer.
496 static unsigned short /* O - 16-bit unsigned integer */
497 read_word(FILE *fp
) /* I - File to read from */
499 unsigned char b0
, b1
; /* Bytes from file */
504 return ((b1
<< 8) | b0
);
509 * 'read_dword()' - Read a 32-bit unsigned integer.
512 static unsigned int /* O - 32-bit unsigned integer */
513 read_dword(FILE *fp
) /* I - File to read from */
515 unsigned char b0
, b1
, b2
, b3
; /* Bytes from file */
522 return ((((((b3
<< 8) | b2
) << 8) | b1
) << 8) | b0
);
527 * 'read_long()' - Read a 32-bit signed integer.
530 static int /* O - 32-bit signed integer */
531 read_long(FILE *fp
) /* I - File to read from */
533 unsigned char b0
, b1
, b2
, b3
; /* Bytes from file */
540 return ((int)(((((b3
<< 8) | b2
) << 8) | b1
) << 8) | b0
);
545 * End of "$Id: image-bmp.c 4741 2005-10-02 04:25:52Z mike $".