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) {
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);
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 /* FIXME this is totally unrobust against bogus image data. */
262 if (State->BufferSize < lsb_32 (&BIH[0]) + 14) {
263 State->BufferSize = lsb_32 (&BIH[0]) + 14;
264 if (!grow_buffer (State, error))
273 State->Header.size = lsb_32 (&BIH[0]);
274 if (State->Header.size == 124) {
276 State->Header.width = lsb_32 (&BIH[4]);
277 State->Header.height = lsb_32 (&BIH[8]);
278 State->Header.depth = lsb_16 (&BIH[14]);
279 State->Compressed = lsb_32 (&BIH[16]);
280 } else if (State->Header.size == 108) {
282 State->Header.width = lsb_32 (&BIH[4]);
283 State->Header.height = lsb_32 (&BIH[8]);
284 State->Header.depth = lsb_16 (&BIH[14]);
285 State->Compressed = lsb_32 (&BIH[16]);
286 } else if (State->Header.size == 64) {
288 State->Header.width = lsb_32 (&BIH[4]);
289 State->Header.height = lsb_32 (&BIH[8]);
290 State->Header.depth = lsb_16 (&BIH[14]);
291 State->Compressed = lsb_32 (&BIH[16]);
292 } else if (State->Header.size == 40) {
294 State->Header.width = lsb_32 (&BIH[4]);
295 State->Header.height = lsb_32 (&BIH[8]);
296 State->Header.depth = lsb_16 (&BIH[14]);
297 State->Compressed = lsb_32 (&BIH[16]);
298 } else if (State->Header.size == 12) {
300 State->Header.width = lsb_16 (&BIH[4]);
301 State->Header.height = lsb_16 (&BIH[6]);
302 State->Header.depth = lsb_16 (&BIH[10]);
303 State->Compressed = BI_RGB;
307 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
308 _("BMP image has unsupported header size"));
309 State->read_state = READ_STATE_ERROR;
313 if (State->Header.size == 12)
314 clrUsed = 1 << State->Header.depth;
316 clrUsed = (int) (BIH[35] << 24) + (BIH[34] << 16) + (BIH[33] << 8) + (BIH[32]);
319 State->Header.n_colors = clrUsed;
321 State->Header.n_colors = (1 << State->Header.depth);
323 if (State->Header.n_colors > (1 << State->Header.depth)) {
326 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
327 _("BMP image has bogus header data"));
328 State->read_state = READ_STATE_ERROR;
332 State->Type = State->Header.depth; /* This may be less trivial someday */
334 /* Negative heights indicates bottom-down pixelorder */
335 if (State->Header.height < 0) {
336 State->Header.height = -State->Header.height;
337 State->Header.Negative = 1;
340 if (State->Header.Negative &&
341 (State->Compressed != BI_RGB && State->Compressed != BI_BITFIELDS))
345 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
346 _("Topdown BMP images cannot be compressed"));
347 State->read_state = READ_STATE_ERROR;
351 if (State->Header.width <= 0 || State->Header.height == 0 ||
352 (State->Compressed == BI_RLE4 && State->Type != 4) ||
353 (State->Compressed == BI_RLE8 && State->Type != 8) ||
354 (State->Compressed == BI_BITFIELDS && !(State->Type == 16 || State->Type == 32)) ||
355 (State->Compressed > BI_BITFIELDS)) {
358 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
359 _("BMP image has bogus header data"));
360 State->read_state = READ_STATE_ERROR;
364 if (State->Type == 32)
365 State->LineWidth = State->Header.width * 4;
366 else if (State->Type == 24)
367 State->LineWidth = State->Header.width * 3;
368 else if (State->Type == 16)
369 State->LineWidth = State->Header.width * 2;
370 else if (State->Type == 8)
371 State->LineWidth = State->Header.width * 1;
372 else if (State->Type == 4)
373 State->LineWidth = (State->Header.width + 1) / 2;
374 else if (State->Type == 1) {
375 State->LineWidth = State->Header.width / 8;
376 if ((State->Header.width & 7) != 0)
381 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
382 _("BMP image has bogus header data"));
383 State->read_state = READ_STATE_ERROR;
387 /* Pad to a 32 bit boundary */
388 if (((State->LineWidth % 4) > 0)
389 && (State->Compressed == BI_RGB || State->Compressed == BI_BITFIELDS))
390 State->LineWidth = (State->LineWidth / 4) * 4 + 4;
392 if (State->pixbuf == NULL) {
393 if (State->size_func) {
394 gint width = State->Header.width;
395 gint height = State->Header.height;
397 (*State->size_func) (&width, &height, State->user_data);
398 if (width == 0 || height == 0) {
399 State->read_state = READ_STATE_DONE;
400 State->BufferSize = 0;
405 if (State->Type == 32 ||
406 State->Compressed == BI_RLE4 ||
407 State->Compressed == BI_RLE8)
409 gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
410 (gint) State->Header.width,
411 (gint) State->Header.height);
414 gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
415 (gint) State->Header.width,
416 (gint) State->Header.height);
418 if (State->pixbuf == NULL) {
421 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
422 _("Not enough memory to load bitmap image"));
423 State->read_state = READ_STATE_ERROR;
427 if (State->prepared_func != NULL)
428 /* Notify the client that we are ready to go */
429 (*State->prepared_func) (State->pixbuf, NULL, State->user_data);
431 /* make all pixels initially transparent */
432 if (State->Compressed == BI_RLE4 || State->Compressed == BI_RLE8) {
433 memset (State->pixbuf->pixels, 0, State->pixbuf->rowstride * State->Header.height);
434 State->compr.p = State->pixbuf->pixels
435 + State->pixbuf->rowstride * (State->Header.height- 1);
439 State->BufferDone = 0;
440 if (State->Type <= 8) {
443 State->read_state = READ_STATE_PALETTE;
445 /* Allocate enough to hold the palette */
446 samples = (State->Header.size == 12 ? 3 : 4);
447 State->BufferSize = State->Header.n_colors * samples;
449 /* Skip over everything between the palette and the data.
450 This protects us against a malicious BFH[10] value.
452 State->BufferPadding = (lsb_32 (&BFH[10]) - 14 - State->Header.size) - State->BufferSize;
454 } else if (State->Compressed == BI_RGB) {
455 if (State->BufferSize < lsb_32 (&BFH[10]))
457 /* skip over padding between headers and image data */
458 State->read_state = READ_STATE_HEADERS;
459 State->BufferDone = State->BufferSize;
460 State->BufferSize = lsb_32 (&BFH[10]);
464 State->read_state = READ_STATE_DATA;
465 State->BufferSize = State->LineWidth;
467 } else if (State->Compressed == BI_BITFIELDS) {
468 if (State->Header.size == 108 || State->Header.size == 124)
470 /* v4 and v5 have the bitmasks in the header */
471 if (!decode_bitmasks (&BIH[40], State, error)) {
472 State->read_state = READ_STATE_ERROR;
478 State->read_state = READ_STATE_BITMASKS;
479 State->BufferSize = 12;
484 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
485 _("BMP image has bogus header data"));
486 State->read_state = READ_STATE_ERROR;
490 if (!grow_buffer (State, error))
496 static gboolean DecodeColormap (guchar *buff,
497 struct bmp_progressive_state *State,
503 g_assert (State->read_state == READ_STATE_PALETTE);
505 samples = (State->Header.size == 12 ? 3 : 4);
506 if (State->BufferSize < State->Header.n_colors * samples) {
507 State->BufferSize = State->Header.n_colors * samples;
508 if (!grow_buffer (State, error))
513 State->Colormap = g_malloc0 ((1 << State->Header.depth) * sizeof (*State->Colormap));
514 for (i = 0; i < State->Header.n_colors; i++)
517 State->Colormap[i][0] = buff[i * samples];
518 State->Colormap[i][1] = buff[i * samples + 1];
519 State->Colormap[i][2] = buff[i * samples + 2];
521 g_print ("color %d %x %x %x\n", i,
522 State->Colormap[i][0],
523 State->Colormap[i][1],
524 State->Colormap[i][2]);
528 State->read_state = READ_STATE_DATA;
530 State->BufferDone = 0;
531 if (!(State->Compressed == BI_RGB || State->Compressed == BI_BITFIELDS))
532 State->BufferSize = 2;
534 State->BufferSize = State->LineWidth;
536 if (!grow_buffer (State, error))
542 /* Finds the lowest set bit and the number of set bits */
544 find_bits (int n, int *lowest, int *n_set)
550 for (i = 31; i >= 0; i--)
557 /* Decodes the bitmasks for BI_BITFIELDS coding */
559 decode_bitmasks (guchar *buf,
560 struct bmp_progressive_state *State,
563 State->a_mask = State->a_shift = State->a_bits = 0;
564 State->r_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
567 State->g_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
570 State->b_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
572 find_bits (State->r_mask, &State->r_shift, &State->r_bits);
573 find_bits (State->g_mask, &State->g_shift, &State->g_bits);
574 find_bits (State->b_mask, &State->b_shift, &State->b_bits);
576 /* v4 and v5 have an alpha mask */
577 if (State->Header.size == 108 || State->Header.size == 124) {
579 State->a_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
580 find_bits (State->a_mask, &State->a_shift, &State->a_bits);
583 if (State->r_bits == 0 || State->g_bits == 0 || State->b_bits == 0) {
584 if (State->Type == 16) {
585 State->r_mask = 0x7c00;
587 State->g_mask = 0x03e0;
589 State->b_mask = 0x001f;
592 State->r_bits = State->g_bits = State->b_bits = 5;
595 State->r_mask = 0x00ff0000;
597 State->g_mask = 0x0000ff00;
599 State->b_mask = 0x000000ff;
601 State->a_mask = 0xff000000;
604 State->r_bits = State->g_bits = State->b_bits = State->a_bits = 8;
608 if (State->r_bits > 8) {
609 State->r_shift += State->r_bits - 8;
612 if (State->g_bits > 8) {
613 State->g_shift += State->g_bits - 8;
616 if (State->b_bits > 8) {
617 State->b_shift += State->b_bits - 8;
620 if (State->a_bits > 8) {
621 State->a_shift += State->a_bits - 8;
625 State->read_state = READ_STATE_DATA;
626 State->BufferDone = 0;
627 State->BufferSize = State->LineWidth;
628 if (!grow_buffer (State, error))
635 * func - called when we have pixmap created (but no image data)
636 * user_data - passed as arg 1 to func
637 * return context (opaque to user)
641 gdk_pixbuf__bmp_image_begin_load(GdkPixbufModuleSizeFunc size_func,
642 GdkPixbufModulePreparedFunc prepared_func,
643 GdkPixbufModuleUpdatedFunc updated_func,
647 struct bmp_progressive_state *context;
649 context = g_new0(struct bmp_progressive_state, 1);
650 context->size_func = size_func;
651 context->prepared_func = prepared_func;
652 context->updated_func = updated_func;
653 context->user_data = user_data;
655 context->read_state = READ_STATE_HEADERS;
657 context->BufferSize = 26;
658 context->BufferPadding = 0;
659 context->buff = g_malloc(26);
660 context->BufferDone = 0;
661 /* 14 for the BitmapFileHeader, 12 for the BitmapImageHeader */
663 context->Colormap = NULL;
669 memset(&context->Header, 0, sizeof(struct headerpair));
670 memset(&context->compr, 0, sizeof(struct bmp_compression_state));
673 context->pixbuf = NULL;
675 return (gpointer) context;
679 * context - returned from image_begin_load
681 * free context, unref gdk_pixbuf
683 static gboolean gdk_pixbuf__bmp_image_stop_load(gpointer data, GError **error)
685 struct bmp_progressive_state *context =
686 (struct bmp_progressive_state *) data;
688 /* FIXME this thing needs to report errors if
689 * we have unused image data
692 g_return_val_if_fail(context != NULL, TRUE);
694 if (context->Colormap != NULL)
695 g_free(context->Colormap);
698 g_object_unref(context->pixbuf);
700 g_free(context->buff);
708 The OneLineXX functions are called when 1 line worth of data is present.
709 OneLine24 is the 24 bpp-version.
711 static void OneLine32(struct bmp_progressive_state *context)
717 if (!context->Header.Negative)
718 pixels = (context->pixbuf->pixels +
719 context->pixbuf->rowstride * (context->Header.height - context->Lines - 1));
721 pixels = (context->pixbuf->pixels +
722 context->pixbuf->rowstride * context->Lines);
726 if (context->Compressed == BI_BITFIELDS) {
727 int r_lshift, r_rshift;
728 int g_lshift, g_rshift;
729 int b_lshift, b_rshift;
730 int a_lshift, a_rshift;
732 r_lshift = 8 - context->r_bits;
733 g_lshift = 8 - context->g_bits;
734 b_lshift = 8 - context->b_bits;
735 a_lshift = 8 - context->a_bits;
737 r_rshift = context->r_bits - r_lshift;
738 g_rshift = context->g_bits - g_lshift;
739 b_rshift = context->b_bits - b_lshift;
740 a_rshift = context->a_bits - a_lshift;
742 for (i = 0; i < context->Header.width; i++) {
745 v = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
747 r = (v & context->r_mask) >> context->r_shift;
748 g = (v & context->g_mask) >> context->g_shift;
749 b = (v & context->b_mask) >> context->b_shift;
750 a = (v & context->a_mask) >> context->a_shift;
752 *pixels++ = (r << r_lshift) | (r >> r_rshift);
753 *pixels++ = (g << g_lshift) | (g >> g_rshift);
754 *pixels++ = (b << b_lshift) | (b >> b_rshift);
756 *pixels++ = 0xff - ((a << a_lshift) | (a >> a_rshift));
763 for (i = 0; i < context->Header.width; i++) {
773 static void OneLine24(struct bmp_progressive_state *context)
779 if (context->Header.Negative == 0)
780 Pixels = (context->pixbuf->pixels +
781 context->pixbuf->rowstride *
782 (context->Header.height - context->Lines - 1));
784 Pixels = (context->pixbuf->pixels +
785 context->pixbuf->rowstride *
787 while (X < context->Header.width) {
788 Pixels[X * 3 + 0] = context->buff[X * 3 + 2];
789 Pixels[X * 3 + 1] = context->buff[X * 3 + 1];
790 Pixels[X * 3 + 2] = context->buff[X * 3 + 0];
796 static void OneLine16(struct bmp_progressive_state *context)
802 if (!context->Header.Negative)
803 pixels = (context->pixbuf->pixels +
804 context->pixbuf->rowstride * (context->Header.height - context->Lines - 1));
806 pixels = (context->pixbuf->pixels +
807 context->pixbuf->rowstride * context->Lines);
811 if (context->Compressed == BI_BITFIELDS) {
812 int r_lshift, r_rshift;
813 int g_lshift, g_rshift;
814 int b_lshift, b_rshift;
816 r_lshift = 8 - context->r_bits;
817 g_lshift = 8 - context->g_bits;
818 b_lshift = 8 - context->b_bits;
820 r_rshift = context->r_bits - r_lshift;
821 g_rshift = context->g_bits - g_lshift;
822 b_rshift = context->b_bits - b_lshift;
824 for (i = 0; i < context->Header.width; i++) {
827 v = (int) src[0] | ((int) src[1] << 8);
829 r = (v & context->r_mask) >> context->r_shift;
830 g = (v & context->g_mask) >> context->g_shift;
831 b = (v & context->b_mask) >> context->b_shift;
833 *pixels++ = (r << r_lshift) | (r >> r_rshift);
834 *pixels++ = (g << g_lshift) | (g >> g_rshift);
835 *pixels++ = (b << b_lshift) | (b >> b_rshift);
840 for (i = 0; i < context->Header.width; i++) {
843 v = src[0] | (src[1] << 8);
845 r = (v >> 10) & 0x1f;
849 *pixels++ = (r << 3) | (r >> 2);
850 *pixels++ = (g << 3) | (g >> 2);
851 *pixels++ = (b << 3) | (b >> 2);
857 static void OneLine8(struct bmp_progressive_state *context)
863 if (context->Header.Negative == 0)
864 Pixels = (context->pixbuf->pixels +
865 context->pixbuf->rowstride *
866 (context->Header.height - context->Lines - 1));
868 Pixels = (context->pixbuf->pixels +
869 context->pixbuf->rowstride *
871 while (X < context->Header.width) {
873 context->Colormap[context->buff[X]][2];
875 context->Colormap[context->buff[X]][1];
877 context->Colormap[context->buff[X]][0];
882 static void OneLine4(struct bmp_progressive_state *context)
888 if (context->Header.Negative == 0)
889 Pixels = (context->pixbuf->pixels +
890 context->pixbuf->rowstride *
891 (context->Header.height - context->Lines - 1));
893 Pixels = (context->pixbuf->pixels +
894 context->pixbuf->rowstride *
897 while (X < context->Header.width) {
900 Pix = context->buff[X / 2];
903 context->Colormap[Pix >> 4][2];
905 context->Colormap[Pix >> 4][1];
907 context->Colormap[Pix >> 4][0];
909 if (X < context->Header.width) {
910 /* Handle the other 4 bit pixel only when there is one */
912 context->Colormap[Pix & 15][2];
914 context->Colormap[Pix & 15][1];
916 context->Colormap[Pix & 15][0];
923 static void OneLine1(struct bmp_progressive_state *context)
929 if (context->Header.Negative == 0)
930 Pixels = (context->pixbuf->pixels +
931 context->pixbuf->rowstride *
932 (context->Header.height - context->Lines - 1));
934 Pixels = (context->pixbuf->pixels +
935 context->pixbuf->rowstride *
937 while (X < context->Header.width) {
940 Bit = (context->buff[X / 8]) >> (7 - (X & 7));
942 Pixels[X * 3 + 0] = context->Colormap[Bit][2];
943 Pixels[X * 3 + 1] = context->Colormap[Bit][1];
944 Pixels[X * 3 + 2] = context->Colormap[Bit][0];
950 static void OneLine(struct bmp_progressive_state *context)
952 context->BufferDone = 0;
953 if (context->Lines >= context->Header.height)
956 if (context->Type == 32)
958 else if (context->Type == 24)
960 else if (context->Type == 16)
962 else if (context->Type == 8)
964 else if (context->Type == 4)
966 else if (context->Type == 1)
969 g_assert_not_reached ();
973 if (context->updated_func != NULL) {
974 (*context->updated_func) (context->pixbuf,
976 (context->Header.Negative ?
977 (context->Lines - 1) :
978 (context->Header.height - context->Lines)),
979 context->Header.width,
994 #define END_OF_LINE 0
995 #define END_OF_BITMAP 1
999 DoCompressed(struct bmp_progressive_state *context, GError **error)
1006 /* context->compr.y might be past the last line because we are
1007 * on padding past the end of a valid data, or we might have hit
1008 * out-of-bounds data. Either way we just eat-and-ignore the
1009 * rest of the file. Doing the check only here and not when
1010 * we change y below is fine since BufferSize is always 2 here
1011 * and the BMP file format always starts new data on 16-bit
1014 if (context->compr.y >= context->Header.height) {
1015 context->BufferDone = 0;
1019 y = context->compr.y;
1021 for (i = 0; i < context->BufferSize; i++) {
1022 c = context->buff[i];
1023 switch (context->compr.phase) {
1026 context->compr.run = c;
1027 context->compr.phase = ENCODED;
1030 context->compr.phase = ESCAPE;
1033 for (j = 0; j < context->compr.run; j++) {
1034 if (context->Compressed == BI_RLE8)
1039 idx = (c >> 4) & 0x0f;
1040 if (context->compr.x < context->Header.width) {
1041 *context->compr.p++ = context->Colormap[idx][2];
1042 *context->compr.p++ = context->Colormap[idx][1];
1043 *context->compr.p++ = context->Colormap[idx][0];
1044 *context->compr.p++ = 0xff;
1048 context->compr.phase = NEUTRAL;
1053 context->compr.x = 0;
1055 context->compr.p = context->pixbuf->pixels
1056 + (context->pixbuf->rowstride * (context->Header.height - context->compr.y - 1))
1057 + (4 * context->compr.x);
1058 context->compr.phase = NEUTRAL;
1061 context->compr.x = 0;
1062 context->compr.y = context->Header.height;
1063 context->compr.phase = NEUTRAL;
1066 context->compr.phase = DELTA_X;
1069 context->compr.run = c;
1070 context->compr.count = 0;
1071 context->compr.phase = ABSOLUTE;
1076 context->compr.x += c;
1077 context->compr.phase = DELTA_Y;
1080 context->compr.y += c;
1081 context->compr.p = context->pixbuf->pixels
1082 + (context->pixbuf->rowstride * (context->Header.height - context->compr.y - 1))
1083 + (4 * context->compr.x);
1084 context->compr.phase = NEUTRAL;
1087 if (context->Compressed == BI_RLE8) {
1089 if (context->compr.x < context->Header.width) {
1090 *context->compr.p++ = context->Colormap[idx][2];
1091 *context->compr.p++ = context->Colormap[idx][1];
1092 *context->compr.p++ = context->Colormap[idx][0];
1093 *context->compr.p++ = 0xff;
1096 context->compr.count++;
1098 if (context->compr.count == context->compr.run) {
1099 if (context->compr.run & 1)
1100 context->compr.phase = SKIP;
1102 context->compr.phase = NEUTRAL;
1106 for (j = 0; j < 2; j++) {
1107 if (context->compr.count & 1)
1110 idx = (c >> 4) & 0x0f;
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 & 3) == 1
1122 || (context->compr.run & 3) == 2)
1123 context->compr.phase = SKIP;
1125 context->compr.phase = NEUTRAL;
1132 context->compr.phase = NEUTRAL;
1136 if (context->updated_func != NULL) {
1137 if (context->compr.y > y)
1139 gint new_y = MIN (context->compr.y, context->Header.height);
1140 (*context->updated_func) (context->pixbuf,
1142 context->Header.height - new_y,
1143 context->Header.width,
1145 context->user_data);
1150 context->BufferDone = 0;
1155 * context - from image_begin_load
1156 * buf - new image data
1157 * size - length of new image data
1159 * append image data onto incrementally built output image
1162 gdk_pixbuf__bmp_image_load_increment(gpointer data,
1167 struct bmp_progressive_state *context =
1168 (struct bmp_progressive_state *) data;
1173 if (context->read_state == READ_STATE_DONE)
1175 else if (context->read_state == READ_STATE_ERROR)
1179 if (context->BufferDone < context->BufferSize) { /* We still
1180 have headerbytes to do */
1182 context->BufferSize - context->BufferDone;
1183 if (BytesToCopy > size)
1186 memmove(context->buff + context->BufferDone,
1189 size -= BytesToCopy;
1191 context->BufferDone += BytesToCopy;
1193 if (context->BufferDone != context->BufferSize)
1197 /* context->buff is full. Now we discard all "padding" */
1198 if (context->BufferPadding != 0) {
1199 BytesToRemove = context->BufferPadding - size;
1200 if (BytesToRemove > size) {
1201 BytesToRemove = size;
1203 size -= BytesToRemove;
1204 context->BufferPadding -= BytesToRemove;
1206 if (context->BufferPadding != 0)
1210 switch (context->read_state) {
1211 case READ_STATE_HEADERS:
1212 if (!DecodeHeader (context->buff,
1213 context->buff + 14, context,
1219 case READ_STATE_PALETTE:
1220 if (!DecodeColormap (context->buff, context, error))
1224 case READ_STATE_BITMASKS:
1225 if (!decode_bitmasks (context->buff, context, error))
1229 case READ_STATE_DATA:
1230 if (context->Compressed == BI_RGB || context->Compressed == BI_BITFIELDS)
1232 else if (!DoCompressed (context, error))
1236 case READ_STATE_DONE:
1241 g_assert_not_reached ();
1248 /* for our convenience when filling file header */
1249 #define put16(buf,data) { guint16 x; \
1250 x = GUINT16_TO_LE (data); \
1251 memcpy(buf, &x, 2); \
1253 #define put32(buf,data) { guint32 x; \
1254 x = GUINT32_TO_LE (data); \
1255 memcpy(buf, &x, 4); \
1259 gdk_pixbuf__bmp_image_save_to_callback (GdkPixbufSaveFunc save_func,
1266 guint width, height, channel, size, stride, src_stride, x, y;
1267 guchar BFH_BIH[54], *pixels, *buf, *src, *dst, *dst_line;
1270 width = gdk_pixbuf_get_width (pixbuf);
1271 height = gdk_pixbuf_get_height (pixbuf);
1272 channel = gdk_pixbuf_get_n_channels (pixbuf);
1273 pixels = gdk_pixbuf_get_pixels (pixbuf);
1274 src_stride = gdk_pixbuf_get_rowstride (pixbuf);
1275 stride = (width * 3 + 3) & ~3;
1276 size = stride * height;
1280 *dst++ = 'B'; /* bfType */
1282 put32 (dst, size + 14 + 40); /* bfSize */
1283 put32 (dst, 0); /* bfReserved1 + bfReserved2 */
1284 put32 (dst, 14 + 40); /* bfOffBits */
1287 put32 (dst, 40); /* biSize */
1288 put32 (dst, width); /* biWidth */
1289 put32 (dst, height); /* biHeight */
1290 put16 (dst, 1); /* biPlanes */
1291 put16 (dst, 24); /* biBitCount */
1292 put32 (dst, BI_RGB); /* biCompression */
1293 put32 (dst, size); /* biSizeImage */
1294 put32 (dst, 0); /* biXPelsPerMeter */
1295 put32 (dst, 0); /* biYPelsPerMeter */
1296 put32 (dst, 0); /* biClrUsed */
1297 put32 (dst, 0); /* biClrImportant */
1299 if (!save_func (BFH_BIH, 14 + 40, error, user_data))
1302 dst_line = buf = g_try_malloc (size);
1306 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1307 _("Couldn't allocate memory for saving BMP file"));
1311 /* saving as a bottom-up bmp */
1312 pixels += (height - 1) * src_stride;
1313 for (y = 0; y < height; ++y, pixels -= src_stride, dst_line += stride) {
1316 for (x = 0; x < width; ++x, dst += 3, src += channel) {
1322 ret = save_func (buf, size, error, user_data);
1329 save_to_file_cb (const gchar *buf,
1337 bytes = fwrite (buf, sizeof (gchar), count, (FILE *) data);
1347 GDK_PIXBUF_ERROR_FAILED,
1348 _("Couldn't write to BMP file"));
1356 gdk_pixbuf__bmp_image_save (FILE *f,
1362 return gdk_pixbuf__bmp_image_save_to_callback (save_to_file_cb,
1368 #define MODULE_ENTRY(type,function) function
1370 #define MODULE_ENTRY(type,function) _gdk_pixbuf__ ## type ## _ ## function
1374 MODULE_ENTRY (bmp, fill_vtable) (GdkPixbufModule *module)
1376 module->begin_load = gdk_pixbuf__bmp_image_begin_load;
1377 module->stop_load = gdk_pixbuf__bmp_image_stop_load;
1378 module->load_increment = gdk_pixbuf__bmp_image_load_increment;
1379 module->save = gdk_pixbuf__bmp_image_save;
1380 module->save_to_callback = gdk_pixbuf__bmp_image_save_to_callback;
1384 MODULE_ENTRY (bmp, fill_info) (GdkPixbufFormat *info)
1386 static GdkPixbufModulePattern signature[] = {
1387 { "BM", NULL, 100 },
1390 static gchar * mime_types[] = {
1396 static gchar * extensions[] = {
1402 info->signature = signature;
1403 info->description = N_("The BMP image format");
1404 info->mime_types = mime_types;
1405 info->extensions = extensions;
1406 info->flags = GDK_PIXBUF_FORMAT_WRITABLE | GDK_PIXBUF_FORMAT_THREADSAFE;
1407 info->license = "LGPL";