1 /* -*- mode: C; c-file-style: "linux" -*- */
2 /* GdkPixbuf library - Windows Bitmap image loader
4 * Copyright (C) 1999 The Free Software Foundation
6 * Authors: Arjan van de Ven <arjan@fenrus.demon.nl>
7 * Federico Mena-Quintero <federico@gimp.org>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
33 #include "gdk-pixbuf-private.h"
34 #include "gdk-pixbuf-io.h"
41 /* If these structures were unpacked, they would define the two headers of the
42 * BMP file. After them comes the palette, and then the image data.
44 * We do not use these structures; we just keep them here for reference.
46 struct BitmapFileHeader {
53 struct BitmapInfoHeader {
64 guint32 n_important_colors;
68 /* Compression values */
73 #define BI_BITFIELDS 3
77 READ_STATE_HEADERS, /* Reading the bitmap file header and bitmap info header */
78 READ_STATE_PALETTE, /* Reading the palette */
79 READ_STATE_BITMASKS, /* Reading the bitmasks for BI_BITFIELDS */
80 READ_STATE_DATA, /* Reading the actual image data */
81 READ_STATE_ERROR, /* An error occurred; further data will be ignored */
82 READ_STATE_DONE /* Done reading the image; further data will be ignored */
87 DumpBIH printf's the values in a BitmapInfoHeader to the screen, for
92 static void DumpBIH(unsigned char *BIH)
94 printf("biSize = %i \n",
95 (int) (BIH[3] << 24) + (BIH[2] << 16) + (BIH[1] << 8) +
97 printf("biWidth = %i \n",
98 (int) (BIH[7] << 24) + (BIH[6] << 16) + (BIH[5] << 8) +
100 printf("biHeight = %i \n",
101 (int) (BIH[11] << 24) + (BIH[10] << 16) + (BIH[9] << 8) +
103 printf("biPlanes = %i \n", (int) (BIH[13] << 8) + (BIH[12]));
104 printf("biBitCount = %i \n", (int) (BIH[15] << 8) + (BIH[14]));
105 printf("biCompress = %i \n",
106 (int) (BIH[19] << 24) + (BIH[18] << 16) + (BIH[17] << 8) +
108 printf("biSizeImage = %i \n",
109 (int) (BIH[23] << 24) + (BIH[22] << 16) + (BIH[21] << 8) +
111 printf("biXPels = %i \n",
112 (int) (BIH[27] << 24) + (BIH[26] << 16) + (BIH[25] << 8) +
114 printf("biYPels = %i \n",
115 (int) (BIH[31] << 24) + (BIH[30] << 16) + (BIH[29] << 8) +
117 printf("biClrUsed = %i \n",
118 (int) (BIH[35] << 24) + (BIH[34] << 16) + (BIH[33] << 8) +
120 printf("biClrImprtnt= %i \n",
121 (int) (BIH[39] << 24) + (BIH[38] << 16) + (BIH[37] << 8) +
125 /* struct headerpair contains the decoded width/height/depth info for
126 the current bitmap */
133 guint Negative; /* Negative = 1 -> top down BMP,
134 Negative = 0 -> bottom up BMP */
138 /* Data needed for the "state" during decompression */
139 struct bmp_compression_state {
147 /* Progressive loading */
149 struct bmp_progressive_state {
150 GdkPixbufModuleSizeFunc size_func;
151 GdkPixbufModulePreparedFunc prepared_func;
152 GdkPixbufModuleUpdatedFunc updated_func;
155 ReadState read_state;
158 guint Lines; /* # of finished lines */
165 guchar (*Colormap)[3];
171 4 = 4 bpp colormapped
172 8 = 8 bpp colormapped
176 struct bmp_compression_state compr;
179 struct headerpair Header; /* Decoded (BE->CPU) header */
181 /* Bit masks, shift amounts, and significant bits for BI_BITFIELDS coding */
182 int r_mask, r_shift, r_bits;
183 int g_mask, g_shift, g_bits;
184 int b_mask, b_shift, b_bits;
185 int a_mask, a_shift, a_bits;
187 GdkPixbuf *pixbuf; /* Our "target" */
191 gdk_pixbuf__bmp_image_begin_load(GdkPixbufModuleSizeFunc size_func,
192 GdkPixbufModulePreparedFunc prepared_func,
193 GdkPixbufModuleUpdatedFunc updated_func,
197 static gboolean gdk_pixbuf__bmp_image_stop_load(gpointer data, GError **error);
198 static gboolean gdk_pixbuf__bmp_image_load_increment(gpointer data,
204 /* Picks up a 32-bit little-endian integer starting at the specified location.
205 * Does it by hand instead of dereferencing a simple (gint *) cast due to
206 * alignment constraints many platforms.
211 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
214 /* Same as above, but for 16-bit little-endian integers. */
218 return src[0] | (src[1] << 8);
221 static gboolean grow_buffer (struct bmp_progressive_state *State,
226 if (State->BufferSize == 0) {
227 g_set_error_literal (error,
229 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
230 _("BMP image has bogus header data"));
231 State->read_state = READ_STATE_ERROR;
235 tmp = g_try_realloc (State->buff, State->BufferSize);
238 g_set_error_literal (error,
240 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
241 _("Not enough memory to load bitmap image"));
242 State->read_state = READ_STATE_ERROR;
251 decode_bitmasks (guchar *buf,
252 struct bmp_progressive_state *State,
255 static gboolean DecodeHeader(unsigned char *BFH, unsigned char *BIH,
256 struct bmp_progressive_state *State,
261 /* First check for the two first bytes content. A sane
262 BMP file must start with bytes 0x42 0x4D. */
263 if (*BFH != 0x42 || *(BFH + 1) != 0x4D) {
264 g_set_error_literal (error,
266 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
267 _("BMP image has bogus header data"));
268 State->read_state = READ_STATE_ERROR;
272 /* FIXME this is totally unrobust against bogus image data. */
273 if (State->BufferSize < lsb_32 (&BIH[0]) + 14) {
274 State->BufferSize = lsb_32 (&BIH[0]) + 14;
275 if (!grow_buffer (State, error))
284 State->Header.size = lsb_32 (&BIH[0]);
285 if (State->Header.size == 124) {
287 State->Header.width = lsb_32 (&BIH[4]);
288 State->Header.height = lsb_32 (&BIH[8]);
289 State->Header.depth = lsb_16 (&BIH[14]);
290 State->Compressed = lsb_32 (&BIH[16]);
291 } else if (State->Header.size == 108) {
293 State->Header.width = lsb_32 (&BIH[4]);
294 State->Header.height = lsb_32 (&BIH[8]);
295 State->Header.depth = lsb_16 (&BIH[14]);
296 State->Compressed = lsb_32 (&BIH[16]);
297 } else if (State->Header.size == 64) {
299 State->Header.width = lsb_32 (&BIH[4]);
300 State->Header.height = lsb_32 (&BIH[8]);
301 State->Header.depth = lsb_16 (&BIH[14]);
302 State->Compressed = lsb_32 (&BIH[16]);
303 } else if (State->Header.size == 40) {
305 State->Header.width = lsb_32 (&BIH[4]);
306 State->Header.height = lsb_32 (&BIH[8]);
307 State->Header.depth = lsb_16 (&BIH[14]);
308 State->Compressed = lsb_32 (&BIH[16]);
309 } else if (State->Header.size == 12) {
311 State->Header.width = lsb_16 (&BIH[4]);
312 State->Header.height = lsb_16 (&BIH[6]);
313 State->Header.depth = lsb_16 (&BIH[10]);
314 State->Compressed = BI_RGB;
316 g_set_error_literal (error,
318 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
319 _("BMP image has unsupported header size"));
320 State->read_state = READ_STATE_ERROR;
324 if (State->Header.size == 12)
325 clrUsed = 1 << State->Header.depth;
327 clrUsed = (int) (BIH[35] << 24) + (BIH[34] << 16) + (BIH[33] << 8) + (BIH[32]);
330 State->Header.n_colors = clrUsed;
332 State->Header.n_colors = (1 << State->Header.depth);
334 if (State->Header.n_colors > (1 << State->Header.depth)) {
335 g_set_error_literal (error,
337 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
338 _("BMP image has bogus header data"));
339 State->read_state = READ_STATE_ERROR;
343 State->Type = State->Header.depth; /* This may be less trivial someday */
345 /* Negative heights indicates bottom-down pixelorder */
346 if (State->Header.height < 0) {
347 State->Header.height = -State->Header.height;
348 State->Header.Negative = 1;
351 if (State->Header.Negative &&
352 (State->Compressed != BI_RGB && State->Compressed != BI_BITFIELDS))
354 g_set_error_literal (error,
356 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
357 _("Topdown BMP images cannot be compressed"));
358 State->read_state = READ_STATE_ERROR;
362 if (State->Header.width <= 0 || State->Header.height == 0 ||
363 (State->Compressed == BI_RLE4 && State->Type != 4) ||
364 (State->Compressed == BI_RLE8 && State->Type != 8) ||
365 (State->Compressed == BI_BITFIELDS && !(State->Type == 16 || State->Type == 32)) ||
366 (State->Compressed > BI_BITFIELDS)) {
367 g_set_error_literal (error,
369 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
370 _("BMP image has bogus header data"));
371 State->read_state = READ_STATE_ERROR;
375 if (State->Type == 32)
376 State->LineWidth = State->Header.width * 4;
377 else if (State->Type == 24)
378 State->LineWidth = State->Header.width * 3;
379 else if (State->Type == 16)
380 State->LineWidth = State->Header.width * 2;
381 else if (State->Type == 8)
382 State->LineWidth = State->Header.width * 1;
383 else if (State->Type == 4)
384 State->LineWidth = (State->Header.width + 1) / 2;
385 else if (State->Type == 1) {
386 State->LineWidth = State->Header.width / 8;
387 if ((State->Header.width & 7) != 0)
390 g_set_error_literal (error,
392 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
393 _("BMP image has bogus header data"));
394 State->read_state = READ_STATE_ERROR;
398 /* Pad to a 32 bit boundary */
399 if (((State->LineWidth % 4) > 0)
400 && (State->Compressed == BI_RGB || State->Compressed == BI_BITFIELDS))
401 State->LineWidth = (State->LineWidth / 4) * 4 + 4;
403 if (State->pixbuf == NULL) {
404 if (State->size_func) {
405 gint width = State->Header.width;
406 gint height = State->Header.height;
408 (*State->size_func) (&width, &height, State->user_data);
409 if (width == 0 || height == 0) {
410 State->read_state = READ_STATE_DONE;
411 State->BufferSize = 0;
416 if (State->Type == 32 ||
417 State->Compressed == BI_RLE4 ||
418 State->Compressed == BI_RLE8)
420 gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
421 (gint) State->Header.width,
422 (gint) State->Header.height);
425 gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
426 (gint) State->Header.width,
427 (gint) State->Header.height);
429 if (State->pixbuf == NULL) {
430 g_set_error_literal (error,
432 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
433 _("Not enough memory to load bitmap image"));
434 State->read_state = READ_STATE_ERROR;
438 if (State->prepared_func != NULL)
439 /* Notify the client that we are ready to go */
440 (*State->prepared_func) (State->pixbuf, NULL, State->user_data);
442 /* make all pixels initially transparent */
443 if (State->Compressed == BI_RLE4 || State->Compressed == BI_RLE8) {
444 memset (State->pixbuf->pixels, 0, State->pixbuf->rowstride * State->Header.height);
445 State->compr.p = State->pixbuf->pixels
446 + State->pixbuf->rowstride * (State->Header.height- 1);
450 State->BufferDone = 0;
451 if (State->Type <= 8) {
454 State->read_state = READ_STATE_PALETTE;
456 /* Allocate enough to hold the palette */
457 samples = (State->Header.size == 12 ? 3 : 4);
458 State->BufferSize = State->Header.n_colors * samples;
460 /* Skip over everything between the palette and the data.
461 This protects us against a malicious BFH[10] value.
463 State->BufferPadding = (lsb_32 (&BFH[10]) - 14 - State->Header.size) - State->BufferSize;
465 } else if (State->Compressed == BI_RGB) {
466 if (State->BufferSize < lsb_32 (&BFH[10]))
468 /* skip over padding between headers and image data */
469 State->read_state = READ_STATE_HEADERS;
470 State->BufferDone = State->BufferSize;
471 State->BufferSize = lsb_32 (&BFH[10]);
475 State->read_state = READ_STATE_DATA;
476 State->BufferSize = State->LineWidth;
478 } else if (State->Compressed == BI_BITFIELDS) {
479 if (State->Header.size == 108 || State->Header.size == 124)
481 /* v4 and v5 have the bitmasks in the header */
482 if (!decode_bitmasks (&BIH[40], State, error)) {
483 State->read_state = READ_STATE_ERROR;
489 State->read_state = READ_STATE_BITMASKS;
490 State->BufferSize = 12;
493 g_set_error_literal (error,
495 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
496 _("BMP image has bogus header data"));
497 State->read_state = READ_STATE_ERROR;
501 if (!grow_buffer (State, error))
507 static gboolean DecodeColormap (guchar *buff,
508 struct bmp_progressive_state *State,
514 g_assert (State->read_state == READ_STATE_PALETTE);
516 samples = (State->Header.size == 12 ? 3 : 4);
517 if (State->BufferSize < State->Header.n_colors * samples) {
518 State->BufferSize = State->Header.n_colors * samples;
519 if (!grow_buffer (State, error))
524 State->Colormap = g_malloc0 ((1 << State->Header.depth) * sizeof (*State->Colormap));
525 for (i = 0; i < State->Header.n_colors; i++)
528 State->Colormap[i][0] = buff[i * samples];
529 State->Colormap[i][1] = buff[i * samples + 1];
530 State->Colormap[i][2] = buff[i * samples + 2];
532 g_print ("color %d %x %x %x\n", i,
533 State->Colormap[i][0],
534 State->Colormap[i][1],
535 State->Colormap[i][2]);
539 State->read_state = READ_STATE_DATA;
541 State->BufferDone = 0;
542 if (!(State->Compressed == BI_RGB || State->Compressed == BI_BITFIELDS))
543 State->BufferSize = 2;
545 State->BufferSize = State->LineWidth;
547 if (!grow_buffer (State, error))
553 /* Finds the lowest set bit and the number of set bits */
555 find_bits (int n, int *lowest, int *n_set)
561 for (i = 31; i >= 0; i--)
568 /* Decodes the bitmasks for BI_BITFIELDS coding */
570 decode_bitmasks (guchar *buf,
571 struct bmp_progressive_state *State,
574 State->a_mask = State->a_shift = State->a_bits = 0;
575 State->r_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
578 State->g_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
581 State->b_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
583 find_bits (State->r_mask, &State->r_shift, &State->r_bits);
584 find_bits (State->g_mask, &State->g_shift, &State->g_bits);
585 find_bits (State->b_mask, &State->b_shift, &State->b_bits);
587 /* v4 and v5 have an alpha mask */
588 if (State->Header.size == 108 || State->Header.size == 124) {
590 State->a_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
591 find_bits (State->a_mask, &State->a_shift, &State->a_bits);
594 if (State->r_bits == 0 || State->g_bits == 0 || State->b_bits == 0) {
595 if (State->Type == 16) {
596 State->r_mask = 0x7c00;
598 State->g_mask = 0x03e0;
600 State->b_mask = 0x001f;
603 State->r_bits = State->g_bits = State->b_bits = 5;
606 State->r_mask = 0x00ff0000;
608 State->g_mask = 0x0000ff00;
610 State->b_mask = 0x000000ff;
612 State->a_mask = 0xff000000;
615 State->r_bits = State->g_bits = State->b_bits = State->a_bits = 8;
619 if (State->r_bits > 8) {
620 State->r_shift += State->r_bits - 8;
623 if (State->g_bits > 8) {
624 State->g_shift += State->g_bits - 8;
627 if (State->b_bits > 8) {
628 State->b_shift += State->b_bits - 8;
631 if (State->a_bits > 8) {
632 State->a_shift += State->a_bits - 8;
636 State->read_state = READ_STATE_DATA;
637 State->BufferDone = 0;
638 State->BufferSize = State->LineWidth;
639 if (!grow_buffer (State, error))
646 * func - called when we have pixmap created (but no image data)
647 * user_data - passed as arg 1 to func
648 * return context (opaque to user)
652 gdk_pixbuf__bmp_image_begin_load(GdkPixbufModuleSizeFunc size_func,
653 GdkPixbufModulePreparedFunc prepared_func,
654 GdkPixbufModuleUpdatedFunc updated_func,
658 struct bmp_progressive_state *context;
660 context = g_new0(struct bmp_progressive_state, 1);
661 context->size_func = size_func;
662 context->prepared_func = prepared_func;
663 context->updated_func = updated_func;
664 context->user_data = user_data;
666 context->read_state = READ_STATE_HEADERS;
668 context->BufferSize = 26;
669 context->BufferPadding = 0;
670 context->buff = g_malloc(26);
671 context->BufferDone = 0;
672 /* 14 for the BitmapFileHeader, 12 for the BitmapImageHeader */
674 context->Colormap = NULL;
680 memset(&context->Header, 0, sizeof(struct headerpair));
681 memset(&context->compr, 0, sizeof(struct bmp_compression_state));
684 context->pixbuf = NULL;
686 return (gpointer) context;
690 * context - returned from image_begin_load
692 * free context, unref gdk_pixbuf
694 static gboolean gdk_pixbuf__bmp_image_stop_load(gpointer data, GError **error)
696 gboolean retval = TRUE;
698 struct bmp_progressive_state *context =
699 (struct bmp_progressive_state *) data;
701 /* FIXME this thing needs to report errors if
702 * we have unused image data
705 g_return_val_if_fail(context != NULL, TRUE);
707 g_free(context->Colormap);
710 g_object_unref(context->pixbuf);
712 if (context->read_state == READ_STATE_HEADERS) {
713 if (error && *error == NULL) {
714 g_set_error_literal (error,
716 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
717 _("Premature end-of-file encountered"));
722 g_free(context->buff);
730 The OneLineXX functions are called when 1 line worth of data is present.
731 OneLine24 is the 24 bpp-version.
733 static void OneLine32(struct bmp_progressive_state *context)
739 if (!context->Header.Negative)
740 pixels = (context->pixbuf->pixels +
741 context->pixbuf->rowstride * (context->Header.height - context->Lines - 1));
743 pixels = (context->pixbuf->pixels +
744 context->pixbuf->rowstride * context->Lines);
748 if (context->Compressed == BI_BITFIELDS) {
749 int r_lshift, r_rshift;
750 int g_lshift, g_rshift;
751 int b_lshift, b_rshift;
752 int a_lshift, a_rshift;
754 r_lshift = 8 - context->r_bits;
755 g_lshift = 8 - context->g_bits;
756 b_lshift = 8 - context->b_bits;
757 a_lshift = 8 - context->a_bits;
759 r_rshift = context->r_bits - r_lshift;
760 g_rshift = context->g_bits - g_lshift;
761 b_rshift = context->b_bits - b_lshift;
762 a_rshift = context->a_bits - a_lshift;
764 for (i = 0; i < context->Header.width; i++) {
765 unsigned int v, r, g, b, a;
767 v = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
769 r = (v & context->r_mask) >> context->r_shift;
770 g = (v & context->g_mask) >> context->g_shift;
771 b = (v & context->b_mask) >> context->b_shift;
772 a = (v & context->a_mask) >> context->a_shift;
774 *pixels++ = (r << r_lshift) | (r >> r_rshift);
775 *pixels++ = (g << g_lshift) | (g >> g_rshift);
776 *pixels++ = (b << b_lshift) | (b >> b_rshift);
778 *pixels++ = (a << a_lshift) | (a >> a_rshift);
785 for (i = 0; i < context->Header.width; i++) {
795 static void OneLine24(struct bmp_progressive_state *context)
801 if (context->Header.Negative == 0)
802 Pixels = (context->pixbuf->pixels +
803 context->pixbuf->rowstride *
804 (context->Header.height - context->Lines - 1));
806 Pixels = (context->pixbuf->pixels +
807 context->pixbuf->rowstride *
809 while (X < context->Header.width) {
810 Pixels[X * 3 + 0] = context->buff[X * 3 + 2];
811 Pixels[X * 3 + 1] = context->buff[X * 3 + 1];
812 Pixels[X * 3 + 2] = context->buff[X * 3 + 0];
818 static void OneLine16(struct bmp_progressive_state *context)
824 if (!context->Header.Negative)
825 pixels = (context->pixbuf->pixels +
826 context->pixbuf->rowstride * (context->Header.height - context->Lines - 1));
828 pixels = (context->pixbuf->pixels +
829 context->pixbuf->rowstride * context->Lines);
833 if (context->Compressed == BI_BITFIELDS) {
834 int r_lshift, r_rshift;
835 int g_lshift, g_rshift;
836 int b_lshift, b_rshift;
838 r_lshift = 8 - context->r_bits;
839 g_lshift = 8 - context->g_bits;
840 b_lshift = 8 - context->b_bits;
842 r_rshift = context->r_bits - r_lshift;
843 g_rshift = context->g_bits - g_lshift;
844 b_rshift = context->b_bits - b_lshift;
846 for (i = 0; i < context->Header.width; i++) {
849 v = (int) src[0] | ((int) src[1] << 8);
851 r = (v & context->r_mask) >> context->r_shift;
852 g = (v & context->g_mask) >> context->g_shift;
853 b = (v & context->b_mask) >> context->b_shift;
855 *pixels++ = (r << r_lshift) | (r >> r_rshift);
856 *pixels++ = (g << g_lshift) | (g >> g_rshift);
857 *pixels++ = (b << b_lshift) | (b >> b_rshift);
862 for (i = 0; i < context->Header.width; i++) {
865 v = src[0] | (src[1] << 8);
867 r = (v >> 10) & 0x1f;
871 *pixels++ = (r << 3) | (r >> 2);
872 *pixels++ = (g << 3) | (g >> 2);
873 *pixels++ = (b << 3) | (b >> 2);
879 static void OneLine8(struct bmp_progressive_state *context)
885 if (context->Header.Negative == 0)
886 Pixels = (context->pixbuf->pixels +
887 context->pixbuf->rowstride *
888 (context->Header.height - context->Lines - 1));
890 Pixels = (context->pixbuf->pixels +
891 context->pixbuf->rowstride *
893 while (X < context->Header.width) {
895 context->Colormap[context->buff[X]][2];
897 context->Colormap[context->buff[X]][1];
899 context->Colormap[context->buff[X]][0];
904 static void OneLine4(struct bmp_progressive_state *context)
910 if (context->Header.Negative == 0)
911 Pixels = (context->pixbuf->pixels +
912 context->pixbuf->rowstride *
913 (context->Header.height - context->Lines - 1));
915 Pixels = (context->pixbuf->pixels +
916 context->pixbuf->rowstride *
919 while (X < context->Header.width) {
922 Pix = context->buff[X / 2];
925 context->Colormap[Pix >> 4][2];
927 context->Colormap[Pix >> 4][1];
929 context->Colormap[Pix >> 4][0];
931 if (X < context->Header.width) {
932 /* Handle the other 4 bit pixel only when there is one */
934 context->Colormap[Pix & 15][2];
936 context->Colormap[Pix & 15][1];
938 context->Colormap[Pix & 15][0];
945 static void OneLine1(struct bmp_progressive_state *context)
951 if (context->Header.Negative == 0)
952 Pixels = (context->pixbuf->pixels +
953 context->pixbuf->rowstride *
954 (context->Header.height - context->Lines - 1));
956 Pixels = (context->pixbuf->pixels +
957 context->pixbuf->rowstride *
959 while (X < context->Header.width) {
962 Bit = (context->buff[X / 8]) >> (7 - (X & 7));
964 Pixels[X * 3 + 0] = context->Colormap[Bit][2];
965 Pixels[X * 3 + 1] = context->Colormap[Bit][1];
966 Pixels[X * 3 + 2] = context->Colormap[Bit][0];
972 static void OneLine(struct bmp_progressive_state *context)
974 context->BufferDone = 0;
975 if (context->Lines >= context->Header.height)
978 if (context->Type == 32)
980 else if (context->Type == 24)
982 else if (context->Type == 16)
984 else if (context->Type == 8)
986 else if (context->Type == 4)
988 else if (context->Type == 1)
991 g_assert_not_reached ();
995 if (context->updated_func != NULL) {
996 (*context->updated_func) (context->pixbuf,
998 (context->Header.Negative ?
999 (context->Lines - 1) :
1000 (context->Header.height - context->Lines)),
1001 context->Header.width,
1003 context->user_data);
1016 #define END_OF_LINE 0
1017 #define END_OF_BITMAP 1
1021 DoCompressed(struct bmp_progressive_state *context, GError **error)
1028 /* context->compr.y might be past the last line because we are
1029 * on padding past the end of a valid data, or we might have hit
1030 * out-of-bounds data. Either way we just eat-and-ignore the
1031 * rest of the file. Doing the check only here and not when
1032 * we change y below is fine since BufferSize is always 2 here
1033 * and the BMP file format always starts new data on 16-bit
1036 if (context->compr.y >= context->Header.height) {
1037 context->BufferDone = 0;
1041 y = context->compr.y;
1043 for (i = 0; i < context->BufferSize; i++) {
1044 c = context->buff[i];
1045 switch (context->compr.phase) {
1048 context->compr.run = c;
1049 context->compr.phase = ENCODED;
1052 context->compr.phase = ESCAPE;
1055 for (j = 0; j < context->compr.run; j++) {
1056 if (context->Compressed == BI_RLE8)
1061 idx = (c >> 4) & 0x0f;
1062 if (context->compr.x < context->Header.width) {
1063 *context->compr.p++ = context->Colormap[idx][2];
1064 *context->compr.p++ = context->Colormap[idx][1];
1065 *context->compr.p++ = context->Colormap[idx][0];
1066 *context->compr.p++ = 0xff;
1070 context->compr.phase = NEUTRAL;
1075 context->compr.x = 0;
1077 context->compr.p = context->pixbuf->pixels
1078 + (context->pixbuf->rowstride * (context->Header.height - context->compr.y - 1))
1079 + (4 * context->compr.x);
1080 context->compr.phase = NEUTRAL;
1083 context->compr.x = 0;
1084 context->compr.y = context->Header.height;
1085 context->compr.phase = NEUTRAL;
1088 context->compr.phase = DELTA_X;
1091 context->compr.run = c;
1092 context->compr.count = 0;
1093 context->compr.phase = ABSOLUTE;
1098 context->compr.x += c;
1099 context->compr.phase = DELTA_Y;
1102 context->compr.y += c;
1103 context->compr.p = context->pixbuf->pixels
1104 + (context->pixbuf->rowstride * (context->Header.height - context->compr.y - 1))
1105 + (4 * context->compr.x);
1106 context->compr.phase = NEUTRAL;
1109 if (context->Compressed == BI_RLE8) {
1111 if (context->compr.x < context->Header.width) {
1112 *context->compr.p++ = context->Colormap[idx][2];
1113 *context->compr.p++ = context->Colormap[idx][1];
1114 *context->compr.p++ = context->Colormap[idx][0];
1115 *context->compr.p++ = 0xff;
1118 context->compr.count++;
1120 if (context->compr.count == context->compr.run) {
1121 if (context->compr.run & 1)
1122 context->compr.phase = SKIP;
1124 context->compr.phase = NEUTRAL;
1128 for (j = 0; j < 2; j++) {
1129 if (context->compr.count & 1)
1132 idx = (c >> 4) & 0x0f;
1133 if (context->compr.x < context->Header.width) {
1134 *context->compr.p++ = context->Colormap[idx][2];
1135 *context->compr.p++ = context->Colormap[idx][1];
1136 *context->compr.p++ = context->Colormap[idx][0];
1137 *context->compr.p++ = 0xff;
1140 context->compr.count++;
1142 if (context->compr.count == context->compr.run) {
1143 if ((context->compr.run & 3) == 1
1144 || (context->compr.run & 3) == 2)
1145 context->compr.phase = SKIP;
1147 context->compr.phase = NEUTRAL;
1154 context->compr.phase = NEUTRAL;
1158 if (context->updated_func != NULL) {
1159 if (context->compr.y > y)
1161 gint new_y = MIN (context->compr.y, context->Header.height);
1162 (*context->updated_func) (context->pixbuf,
1164 context->Header.height - new_y,
1165 context->Header.width,
1167 context->user_data);
1172 context->BufferDone = 0;
1177 * context - from image_begin_load
1178 * buf - new image data
1179 * size - length of new image data
1181 * append image data onto incrementally built output image
1184 gdk_pixbuf__bmp_image_load_increment(gpointer data,
1189 struct bmp_progressive_state *context =
1190 (struct bmp_progressive_state *) data;
1195 if (context->read_state == READ_STATE_DONE)
1197 else if (context->read_state == READ_STATE_ERROR)
1201 if (context->BufferDone < context->BufferSize) { /* We still
1202 have headerbytes to do */
1204 context->BufferSize - context->BufferDone;
1205 if (BytesToCopy > size)
1208 memmove(context->buff + context->BufferDone,
1211 size -= BytesToCopy;
1213 context->BufferDone += BytesToCopy;
1215 if (context->BufferDone != context->BufferSize)
1219 /* context->buff is full. Now we discard all "padding" */
1220 if (context->BufferPadding != 0) {
1221 BytesToRemove = context->BufferPadding - size;
1222 if (BytesToRemove > size) {
1223 BytesToRemove = size;
1225 size -= BytesToRemove;
1226 context->BufferPadding -= BytesToRemove;
1228 if (context->BufferPadding != 0)
1232 switch (context->read_state) {
1233 case READ_STATE_HEADERS:
1234 if (!DecodeHeader (context->buff,
1235 context->buff + 14, context,
1241 case READ_STATE_PALETTE:
1242 if (!DecodeColormap (context->buff, context, error))
1246 case READ_STATE_BITMASKS:
1247 if (!decode_bitmasks (context->buff, context, error))
1251 case READ_STATE_DATA:
1252 if (context->Compressed == BI_RGB || context->Compressed == BI_BITFIELDS)
1254 else if (!DoCompressed (context, error))
1258 case READ_STATE_DONE:
1263 g_assert_not_reached ();
1270 /* for our convenience when filling file header */
1271 #define put16(buf,data) { guint16 x; \
1272 x = GUINT16_TO_LE (data); \
1273 memcpy(buf, &x, 2); \
1275 #define put32(buf,data) { guint32 x; \
1276 x = GUINT32_TO_LE (data); \
1277 memcpy(buf, &x, 4); \
1281 gdk_pixbuf__bmp_image_save_to_callback (GdkPixbufSaveFunc save_func,
1288 guint width, height, channel, size, stride, src_stride, x, y;
1289 guchar BFH_BIH[54], *pixels, *buf, *src, *dst, *dst_line;
1292 width = gdk_pixbuf_get_width (pixbuf);
1293 height = gdk_pixbuf_get_height (pixbuf);
1294 channel = gdk_pixbuf_get_n_channels (pixbuf);
1295 pixels = gdk_pixbuf_get_pixels (pixbuf);
1296 src_stride = gdk_pixbuf_get_rowstride (pixbuf);
1297 stride = (width * 3 + 3) & ~3;
1298 size = stride * height;
1302 *dst++ = 'B'; /* bfType */
1304 put32 (dst, size + 14 + 40); /* bfSize */
1305 put32 (dst, 0); /* bfReserved1 + bfReserved2 */
1306 put32 (dst, 14 + 40); /* bfOffBits */
1309 put32 (dst, 40); /* biSize */
1310 put32 (dst, width); /* biWidth */
1311 put32 (dst, height); /* biHeight */
1312 put16 (dst, 1); /* biPlanes */
1313 put16 (dst, 24); /* biBitCount */
1314 put32 (dst, BI_RGB); /* biCompression */
1315 put32 (dst, size); /* biSizeImage */
1316 put32 (dst, 0); /* biXPelsPerMeter */
1317 put32 (dst, 0); /* biYPelsPerMeter */
1318 put32 (dst, 0); /* biClrUsed */
1319 put32 (dst, 0); /* biClrImportant */
1321 if (!save_func ((gchar *)BFH_BIH, 14 + 40, error, user_data))
1324 dst_line = buf = g_try_malloc (size);
1326 g_set_error_literal (error,
1328 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1329 _("Couldn't allocate memory for saving BMP file"));
1333 /* saving as a bottom-up bmp */
1334 pixels += (height - 1) * src_stride;
1335 for (y = 0; y < height; ++y, pixels -= src_stride, dst_line += stride) {
1338 for (x = 0; x < width; ++x, dst += 3, src += channel) {
1344 ret = save_func ((gchar *)buf, size, error, user_data);
1351 save_to_file_cb (const gchar *buf,
1359 bytes = fwrite (buf, sizeof (gchar), count, (FILE *) data);
1367 g_set_error_literal (error,
1369 GDK_PIXBUF_ERROR_FAILED,
1370 _("Couldn't write to BMP file"));
1378 gdk_pixbuf__bmp_image_save (FILE *f,
1384 return gdk_pixbuf__bmp_image_save_to_callback (save_to_file_cb,
1390 #define MODULE_ENTRY(function) G_MODULE_EXPORT void function
1392 #define MODULE_ENTRY(function) void _gdk_pixbuf__bmp_ ## function
1395 MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
1397 module->begin_load = gdk_pixbuf__bmp_image_begin_load;
1398 module->stop_load = gdk_pixbuf__bmp_image_stop_load;
1399 module->load_increment = gdk_pixbuf__bmp_image_load_increment;
1400 module->save = gdk_pixbuf__bmp_image_save;
1401 module->save_to_callback = gdk_pixbuf__bmp_image_save_to_callback;
1404 MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
1406 static GdkPixbufModulePattern signature[] = {
1407 { "BM", NULL, 100 },
1410 static gchar * mime_types[] = {
1416 static gchar * extensions[] = {
1422 info->signature = signature;
1423 info->description = N_("The BMP image format");
1424 info->mime_types = mime_types;
1425 info->extensions = extensions;
1426 info->flags = GDK_PIXBUF_FORMAT_WRITABLE | GDK_PIXBUF_FORMAT_THREADSAFE;
1427 info->license = "LGPL";