]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/image-bmp.c
4 * BMP image routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2001 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-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * ImageReadBMP() - Read a BMP image file.
27 * read_word() - Read a 16-bit unsigned integer.
28 * read_dword() - Read a 32-bit unsigned integer.
29 * read_long() - Read a 32-bit signed integer.
33 * Include necessary headers...
40 * Constants for the bitmap compression...
43 # define BI_RGB 0 /* No compression - straight BGR data */
44 # define BI_RLE8 1 /* 8-bit run-length compression */
45 # define BI_RLE4 2 /* 4-bit run-length compression */
46 # define BI_BITFIELDS 3 /* RGB bitmap with RGB masks */
53 static unsigned short read_word(FILE *fp
);
54 static unsigned int read_dword(FILE *fp
);
55 static int read_long(FILE *fp
);
59 * 'ImageReadBMP()' - Read a BMP image file.
62 int /* O - Read status */
63 ImageReadBMP(image_t
*img
, /* IO - Image */
64 FILE *fp
, /* I - Image file */
65 int primary
, /* I - Primary choice for colorspace */
66 int secondary
, /* I - Secondary choice for colorspace */
67 int saturation
, /* I - Color saturation (%) */
68 int hue
, /* I - Color hue (degrees) */
69 const ib_t
*lut
) /* I - Lookup table for gamma/brightness */
71 int offset
, /* Offset to bitmap data */
72 info_size
, /* Size of info header */
73 planes
, /* Number of planes (always 1) */
74 depth
, /* Depth of image (bits) */
75 compression
, /* Type of compression */
76 image_size
, /* Size of image in bytes */
77 colors_used
, /* Number of colors used */
78 colors_important
, /* Number of important colors */
79 bpp
, /* Bytes per pixel */
80 x
, y
, /* Looping vars */
81 color
, /* Color of RLE pixel */
82 count
, /* Number of times to repeat */
83 temp
, /* Temporary color */
84 align
; /* Alignment bytes */
85 ib_t bit
, /* Bit in image */
86 byte
; /* Byte in image */
87 ib_t
*in
, /* Input pixels */
88 *out
, /* Output pixels */
89 *ptr
; /* Pointer into pixels */
90 ib_t colormap
[256][4]; /* Colormap */
99 getc(fp
); /* Skip "BM" sync chars */
101 read_dword(fp
); /* Skip size */
102 read_word(fp
); /* Skip reserved stuff */
104 offset
= read_dword(fp
);
106 fprintf(stderr
, "offset = %d\n", offset
);
109 * Then the bitmap information...
112 info_size
= read_dword(fp
);
113 img
->xsize
= read_long(fp
);
114 img
->ysize
= read_long(fp
);
115 planes
= read_word(fp
);
116 depth
= read_word(fp
);
117 compression
= read_dword(fp
);
118 image_size
= read_dword(fp
);
119 img
->xppi
= read_long(fp
) * 0.0254 + 0.5;
120 img
->yppi
= read_long(fp
) * 0.0254 + 0.5;
121 colors_used
= read_dword(fp
);
122 colors_important
= read_dword(fp
);
125 * Make sure the resolution info is valid...
133 fprintf(stderr
, "info_size = %d, xsize = %d, ysize = %d, planes = %d, depth = %d\n",
134 info_size
, img
->xsize
, img
->ysize
, planes
, depth
);
135 fprintf(stderr
, "compression = %d, image_size = %d, xppi = %d, yppi = %d\n",
136 compression
, image_size
, img
->xppi
, img
->yppi
);
137 fprintf(stderr
, "colors_used = %d, colors_important = %d\n", colors_used
,
141 for (info_size
-= 40; info_size
> 0; info_size
--)
148 if (colors_used
== 0 && depth
<= 8)
149 colors_used
= 1 << depth
;
151 fread(colormap
, colors_used
, 4, fp
);
154 * Setup image and buffers...
157 img
->colorspace
= primary
;
159 ImageSetMaxTiles(img
, 0);
161 in
= malloc(img
->xsize
* 3);
162 bpp
= ImageGetDepth(img
);
163 out
= malloc(img
->xsize
* bpp
);
166 * Read the image data...
173 for (y
= img
->ysize
- 1; y
>= 0; y
--)
175 if (img
->colorspace
== IMAGE_RGB
)
182 case 1 : /* Bitmap */
183 for (x
= img
->xsize
, bit
= 128, byte
= 0; x
> 0; x
--)
190 *ptr
++ = colormap
[1][2];
191 *ptr
++ = colormap
[1][1];
192 *ptr
++ = colormap
[1][0];
196 *ptr
++ = colormap
[0][2];
197 *ptr
++ = colormap
[0][1];
198 *ptr
++ = colormap
[0][0];
208 * Read remaining bytes to align to 32 bits...
211 for (temp
= (img
->xsize
+ 7) / 8; temp
& 3; temp
++)
215 case 4 : /* 16-color */
216 for (x
= img
->xsize
, bit
= 0xf0, temp
= 0; x
> 0; x
--)
219 * Get a new count as needed...
222 if (compression
!= BI_RLE4
&& count
== 0)
236 if ((count
= getc(fp
)) == 0)
238 if ((count
= getc(fp
)) == 0)
261 count
= getc(fp
) * getc(fp
) * img
->xsize
;
271 align
= ((4 - (count
& 3)) / 2) & 1;
279 * Get a new color as needed...
292 * Copy the color value...
295 *ptr
++ = colormap
[temp
>> 4][2];
296 *ptr
++ = colormap
[temp
>> 4][1];
297 *ptr
++ = colormap
[temp
>> 4][0];
303 * Copy the color value...
306 *ptr
++ = colormap
[temp
& 15][2];
307 *ptr
++ = colormap
[temp
& 15][1];
308 *ptr
++ = colormap
[temp
& 15][0];
314 case 8 : /* 256-color */
315 for (x
= img
->xsize
; x
> 0; x
--)
318 * Get a new count as needed...
321 if (compression
!= BI_RLE8
)
335 if ((count
= getc(fp
)) == 0)
337 if ((count
= getc(fp
)) == 0)
360 count
= getc(fp
) * getc(fp
) * img
->xsize
;
370 align
= (2 - (count
& 1)) & 1;
378 * Get a new color as needed...
389 * Copy the color value...
392 *ptr
++ = colormap
[temp
][2];
393 *ptr
++ = colormap
[temp
][1];
394 *ptr
++ = colormap
[temp
][0];
398 case 24 : /* 24-bit RGB */
399 for (x
= img
->xsize
; x
> 0; x
--, ptr
+= 3)
407 * Read remaining bytes to align to 32 bits...
410 for (temp
= img
->xsize
* 3; temp
& 3; temp
++)
415 if (img
->colorspace
== IMAGE_RGB
)
417 if (saturation
!= 100 || hue
!= 0)
418 ImageRGBAdjust(out
, img
->xsize
, saturation
, hue
);
422 if (saturation
!= 100 || hue
!= 0)
423 ImageRGBAdjust(in
, img
->xsize
, saturation
, hue
);
425 switch (img
->colorspace
)
428 ImageRGBToWhite(in
, out
, img
->xsize
);
431 ImageRGBToBlack(in
, out
, img
->xsize
);
434 ImageRGBToCMY(in
, out
, img
->xsize
);
437 ImageRGBToCMYK(in
, out
, img
->xsize
);
443 ImageLut(out
, img
->xsize
* bpp
, lut
);
445 ImagePutRow(img
, 0, y
, img
->xsize
, out
);
457 * 'read_word()' - Read a 16-bit unsigned integer.
460 static unsigned short /* O - 16-bit unsigned integer */
461 read_word(FILE *fp
) /* I - File to read from */
463 unsigned char b0
, b1
; /* Bytes from file */
468 return ((b1
<< 8) | b0
);
473 * 'read_dword()' - Read a 32-bit unsigned integer.
476 static unsigned int /* O - 32-bit unsigned integer */
477 read_dword(FILE *fp
) /* I - File to read from */
479 unsigned char b0
, b1
, b2
, b3
; /* Bytes from file */
486 return ((((((b3
<< 8) | b2
) << 8) | b1
) << 8) | b0
);
491 * 'read_long()' - Read a 32-bit signed integer.
494 static int /* O - 32-bit signed integer */
495 read_long(FILE *fp
) /* I - File to read from */
497 unsigned char b0
, b1
, b2
, b3
; /* Bytes from file */
504 return ((int)(((((b3
<< 8) | b2
) << 8) | b1
) << 8) | b0
);