]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-bmp.c
2 * "$Id: image-bmp.c 177 2006-06-21 00:20:03Z jlovell $"
4 * BMP image routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2006 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
--)
209 case 1 : /* Bitmap */
210 for (x
= img
->xsize
, bit
= 128, byte
= 0; x
> 0; x
--)
217 *ptr
++ = colormap
[1][2];
218 *ptr
++ = colormap
[1][1];
219 *ptr
++ = colormap
[1][0];
223 *ptr
++ = colormap
[0][2];
224 *ptr
++ = colormap
[0][1];
225 *ptr
++ = colormap
[0][0];
235 * Read remaining bytes to align to 32 bits...
238 for (temp
= (img
->xsize
+ 7) / 8; temp
& 3; temp
++)
242 case 4 : /* 16-color */
243 for (x
= img
->xsize
, bit
= 0xf0, temp
= 0; x
> 0; x
--)
246 * Get a new count as needed...
249 if (compression
!= BI_RLE4
&& count
== 0)
263 if ((count
= getc(fp
)) == 0)
265 if ((count
= getc(fp
)) == 0)
288 count
= getc(fp
) * getc(fp
) * img
->xsize
;
298 align
= ((4 - (count
& 3)) / 2) & 1;
306 * Get a new color as needed...
319 * Copy the color value...
322 *ptr
++ = colormap
[temp
>> 4][2];
323 *ptr
++ = colormap
[temp
>> 4][1];
324 *ptr
++ = colormap
[temp
>> 4][0];
330 * Copy the color value...
333 *ptr
++ = colormap
[temp
& 15][2];
334 *ptr
++ = colormap
[temp
& 15][1];
335 *ptr
++ = colormap
[temp
& 15][0];
341 case 8 : /* 256-color */
342 for (x
= img
->xsize
; x
> 0; x
--)
345 * Get a new count as needed...
348 if (compression
!= BI_RLE8
)
362 if ((count
= getc(fp
)) == 0)
364 if ((count
= getc(fp
)) == 0)
387 count
= getc(fp
) * getc(fp
) * img
->xsize
;
397 align
= (2 - (count
& 1)) & 1;
405 * Get a new color as needed...
416 * Copy the color value...
419 *ptr
++ = colormap
[temp
][2];
420 *ptr
++ = colormap
[temp
][1];
421 *ptr
++ = colormap
[temp
][0];
425 case 24 : /* 24-bit RGB */
426 for (x
= img
->xsize
; x
> 0; x
--, ptr
+= 3)
434 * Read remaining bytes to align to 32 bits...
437 for (temp
= img
->xsize
* 3; temp
& 3; temp
++)
442 if (saturation
!= 100 || hue
!= 0)
443 cupsImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
445 switch (img
->colorspace
)
450 case CUPS_IMAGE_WHITE
:
451 cupsImageRGBToWhite(in
, out
, img
->xsize
);
454 case CUPS_IMAGE_RGB
:
455 cupsImageRGBToRGB(in
, out
, img
->xsize
);
458 case CUPS_IMAGE_BLACK
:
459 cupsImageRGBToBlack(in
, out
, img
->xsize
);
462 case CUPS_IMAGE_CMY
:
463 cupsImageRGBToCMY(in
, out
, img
->xsize
);
466 case CUPS_IMAGE_CMYK
:
467 cupsImageRGBToCMYK(in
, out
, img
->xsize
);
472 cupsImageLut(out
, img
->xsize
* bpp
, lut
);
474 _cupsImagePutRow(img
, 0, y
, img
->xsize
, out
);
486 * 'read_word()' - Read a 16-bit unsigned integer.
489 static unsigned short /* O - 16-bit unsigned integer */
490 read_word(FILE *fp
) /* I - File to read from */
492 unsigned char b0
, b1
; /* Bytes from file */
497 return ((b1
<< 8) | b0
);
502 * 'read_dword()' - Read a 32-bit unsigned integer.
505 static unsigned int /* O - 32-bit unsigned integer */
506 read_dword(FILE *fp
) /* I - File to read from */
508 unsigned char b0
, b1
, b2
, b3
; /* Bytes from file */
515 return ((((((b3
<< 8) | b2
) << 8) | b1
) << 8) | b0
);
520 * 'read_long()' - Read a 32-bit signed integer.
523 static int /* O - 32-bit signed integer */
524 read_long(FILE *fp
) /* I - File to read from */
526 unsigned char b0
, b1
, b2
, b3
; /* Bytes from file */
533 return ((int)(((((b3
<< 8) | b2
) << 8) | b1
) << 8) | b0
);
538 * End of "$Id: image-bmp.c 177 2006-06-21 00:20:03Z jlovell $".