io-bmp.c 37.2 KB
Newer Older
1
/* -*- mode: C; c-file-style: "linux" -*- */
2 3 4
/* GdkPixbuf library - Windows Bitmap image loader
 *
 * Copyright (C) 1999 The Free Software Foundation
Mark Crichton's avatar
Mark Crichton committed
5
 *
6 7 8 9
 * Authors: Arjan van de Ven <arjan@fenrus.demon.nl>
 *          Federico Mena-Quintero <federico@gimp.org>
 *
 * Based on io-ras.c
Mark Crichton's avatar
Mark Crichton committed
10 11
 *
 * This library is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU Lesser General Public
Mark Crichton's avatar
Mark Crichton committed
13 14 15 16 17 18
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
 * Lesser General Public License for more details.
Mark Crichton's avatar
Mark Crichton committed
20
 *
21
 * You should have received a copy of the GNU Lesser General Public
22 23 24
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Mark Crichton's avatar
Mark Crichton committed
25 26 27 28
 */

#include <config.h>
#include <stdio.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
29
#ifdef HAVE_UNISTD_H
30
#include <unistd.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
31
#endif
32
#include <string.h>
33 34
#include "gdk-pixbuf-private.h"
#include "gdk-pixbuf-io.h"
35

Matthias Clasen's avatar
Matthias Clasen committed
36
#define DUMPBIH 0
37

38

39

40 41 42 43 44 45
#if 0
/* If these structures were unpacked, they would define the two headers of the
 * BMP file.  After them comes the palette, and then the image data.
 *
 * We do not use these structures; we just keep them here for reference.
 */
46
struct BitmapFileHeader {
47 48 49 50
	guint16 magic;
	guint32 file_size;
	guint32 reserved;
	guint32 data_offset;
51 52 53
};

struct BitmapInfoHeader {
54 55 56 57 58 59 60 61 62 63 64
	guint32 header_size;
	guint32 width;
	guint32 height;
	guint16 planes;
	guint16 bpp;
	guint32 compression;
	guint32 data_size;
	guint32 x_ppm;
	guint32 y_ppm;
	guint32 n_colors;
	guint32 n_important_colors;
65
};
66
#endif
67

68
/* Compression values */
69 70 71 72 73 74

#define BI_RGB 0
#define BI_RLE8 1
#define BI_RLE4 2
#define BI_BITFIELDS 3

75 76 77 78 79 80 81 82 83
/* State machine */
typedef enum {
	READ_STATE_HEADERS,	/* Reading the bitmap file header and bitmap info header */
	READ_STATE_PALETTE,	/* Reading the palette */
	READ_STATE_BITMASKS,	/* Reading the bitmasks for BI_BITFIELDS */
	READ_STATE_DATA,	/* Reading the actual image data */
	READ_STATE_ERROR,	/* An error occurred; further data will be ignored */
	READ_STATE_DONE		/* Done reading the image; further data will be ignored */
} ReadState;
84

85 86 87
/*

DumpBIH printf's the values in a BitmapInfoHeader to the screen, for
88 89 90 91
debugging purposes.

*/
#if DUMPBIH
92
static void DumpBIH(unsigned char *BIH)
93
{
94
	printf("biSize      = %i \n",
95 96
	       (int) (BIH[3] << 24) + (BIH[2] << 16) + (BIH[1] << 8) +
	       (BIH[0]));
97
	printf("biWidth     = %i \n",
98 99
	       (int) (BIH[7] << 24) + (BIH[6] << 16) + (BIH[5] << 8) +
	       (BIH[4]));
100
	printf("biHeight    = %i \n",
101
	       (int) (BIH[11] << 24) + (BIH[10] << 16) + (BIH[9] << 8) +
102
	       (BIH[8]));
103 104
	printf("biPlanes    = %i \n", (int) (BIH[13] << 8) + (BIH[12]));
	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) +
107 108
	       (BIH[16]));
	printf("biSizeImage = %i \n",
109
	       (int) (BIH[23] << 24) + (BIH[22] << 16) + (BIH[21] << 8) +
110 111
	       (BIH[20]));
	printf("biXPels     = %i \n",
112
	       (int) (BIH[27] << 24) + (BIH[26] << 16) + (BIH[25] << 8) +
113 114
	       (BIH[24]));
	printf("biYPels     = %i \n",
115
	       (int) (BIH[31] << 24) + (BIH[30] << 16) + (BIH[29] << 8) +
116 117
	       (BIH[28]));
	printf("biClrUsed   = %i \n",
118
	       (int) (BIH[35] << 24) + (BIH[34] << 16) + (BIH[33] << 8) +
119 120
	       (BIH[32]));
	printf("biClrImprtnt= %i \n",
121
	       (int) (BIH[39] << 24) + (BIH[38] << 16) + (BIH[37] << 8) +
122 123
	       (BIH[36]));
}
124 125 126
#endif
/* struct headerpair contains the decoded width/height/depth info for
   the current bitmap */
127

128
struct headerpair {
129 130 131
	guint32 size;
	gint32 width;
	gint32 height;
132
	guint depth;
133
	guint Negative;		/* Negative = 1 -> top down BMP,
134
				   Negative = 0 -> bottom up BMP */
135
	guint  n_colors;
136
};
137

138 139
/* Data needed for the "state" during decompression */
struct bmp_compression_state {
140
	gint phase;
141 142 143 144
	gint run;
	gint count;
	gint x, y;
	guchar *p;
145
};
146 147 148 149

/* Progressive loading */

struct bmp_progressive_state {
150
	GdkPixbufModuleSizeFunc size_func;
151 152
	GdkPixbufModulePreparedFunc prepared_func;
	GdkPixbufModuleUpdatedFunc updated_func;
153 154
	gpointer user_data;

155 156
	ReadState read_state;

157 158
	guint LineWidth;
	guint Lines;		/* # of finished lines */
159

160
	guchar *buff;
161
	guint BufferSize;
162
	guint BufferPadding;
163
	guint BufferDone;
164 165

	guchar (*Colormap)[3];
166

167
	gint Type;		/*
168
				   32 = RGB + alpha
169
				   24 = RGB
170
				   16 = RGB
171 172
				   4  = 4 bpp colormapped
				   8  = 8 bpp colormapped
173
				   1  = 1 bit bitonal
174
				 */
175
	guint Compressed;
176
	struct bmp_compression_state compr;
177 178 179 180


	struct headerpair Header;	/* Decoded (BE->CPU) header */

181 182 183 184
	/* Bit masks, shift amounts, and significant bits for BI_BITFIELDS coding */
	int r_mask, r_shift, r_bits;
	int g_mask, g_shift, g_bits;
	int b_mask, b_shift, b_bits;
185
	int a_mask, a_shift, a_bits;
186 187 188 189

	GdkPixbuf *pixbuf;	/* Our "target" */
};

190
static gpointer
191 192 193
gdk_pixbuf__bmp_image_begin_load(GdkPixbufModuleSizeFunc size_func,
                                 GdkPixbufModulePreparedFunc prepared_func,
				 GdkPixbufModuleUpdatedFunc updated_func,
Havoc Pennington's avatar
Havoc Pennington committed
194
                                 gpointer user_data,
Havoc Pennington's avatar
Havoc Pennington committed
195
                                 GError **error);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
196

197 198 199 200 201
static gboolean gdk_pixbuf__bmp_image_stop_load(gpointer data, GError **error);
static gboolean gdk_pixbuf__bmp_image_load_increment(gpointer data,
                                                     const guchar * buf,
                                                     guint size,
                                                     GError **error);
202

Mark Crichton's avatar
Mark Crichton committed
203

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
/* Picks up a 32-bit little-endian integer starting at the specified location.
 * Does it by hand instead of dereferencing a simple (gint *) cast due to
 * alignment constraints many platforms.
 */
static int
lsb_32 (guchar *src)
{
	return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
}

/* Same as above, but for 16-bit little-endian integers. */
static short
lsb_16 (guchar *src)
{
	return src[0] | (src[1] << 8);
}

221 222 223
static gboolean grow_buffer (struct bmp_progressive_state *State,
                             GError **error)
{
224 225 226 227 228 229 230 231 232 233 234 235 236
  guchar *tmp;

  if (State->BufferSize == 0) {
    g_set_error (error,
		 GDK_PIXBUF_ERROR,
		 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
		 _("BMP image has bogus header data"));
    State->read_state = READ_STATE_ERROR;
    return FALSE;
  }

  tmp = g_try_realloc (State->buff, State->BufferSize);

237
  if (!tmp) {
238 239 240 241 242 243 244
    g_set_error (error,
		 GDK_PIXBUF_ERROR,
		 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
		 _("Not enough memory to load bitmap image"));
    State->read_state = READ_STATE_ERROR;
    return FALSE;
  }
245

246
  State->buff = tmp;
247 248 249
  return TRUE;
}

250 251 252 253 254
static gboolean
decode_bitmasks (guchar *buf,
		 struct bmp_progressive_state *State, 
		 GError **error);

255 256 257
static gboolean DecodeHeader(unsigned char *BFH, unsigned char *BIH,
                             struct bmp_progressive_state *State,
                             GError **error)
258
{
259 260
	gint clrUsed;

261 262 263 264 265 266 267 268 269 270 271
	/* First check for the two first bytes content. A sane
	   BMP file must start with bytes 0x42 0x4D.  */
	if (*BFH != 0x42 || *(BFH + 1) != 0x4D) {
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has bogus header data"));
		State->read_state = READ_STATE_ERROR;
		return FALSE;
	}

272
        /* FIXME this is totally unrobust against bogus image data. */
273 274
	if (State->BufferSize < lsb_32 (&BIH[0]) + 14) {
		State->BufferSize = lsb_32 (&BIH[0]) + 14;
275
		if (!grow_buffer (State, error))
Matthias Clasen's avatar
Matthias Clasen committed
276
			return FALSE;
277 278 279
		return TRUE;
	}

280 281
#if DUMPBIH
	DumpBIH(BIH);
282
#endif    
283

284
	State->Header.size = lsb_32 (&BIH[0]);
285 286 287 288 289 290 291 292 293 294 295 296 297 298
	if (State->Header.size == 124) {
                /* BMP v5 */
		State->Header.width = lsb_32 (&BIH[4]);
		State->Header.height = lsb_32 (&BIH[8]);
		State->Header.depth = lsb_16 (&BIH[14]);
		State->Compressed = lsb_32 (&BIH[16]);
	} else if (State->Header.size == 108) {
                /* BMP v4 */
		State->Header.width = lsb_32 (&BIH[4]);
		State->Header.height = lsb_32 (&BIH[8]);
		State->Header.depth = lsb_16 (&BIH[14]);
		State->Compressed = lsb_32 (&BIH[16]);
	} else if (State->Header.size == 64) {
                /* BMP OS/2 v2 */
299 300 301 302 303
		State->Header.width = lsb_32 (&BIH[4]);
		State->Header.height = lsb_32 (&BIH[8]);
		State->Header.depth = lsb_16 (&BIH[14]);
		State->Compressed = lsb_32 (&BIH[16]);
	} else if (State->Header.size == 40) {
304
                /* BMP v3 */ 
305 306 307 308
		State->Header.width = lsb_32 (&BIH[4]);
		State->Header.height = lsb_32 (&BIH[8]);
		State->Header.depth = lsb_16 (&BIH[14]);
		State->Compressed = lsb_32 (&BIH[16]);
309
	} else if (State->Header.size == 12) {
310
                /* BMP OS/2 */
311 312 313
		State->Header.width = lsb_16 (&BIH[4]);
		State->Header.height = lsb_16 (&BIH[6]);
		State->Header.depth = lsb_16 (&BIH[10]);
314
		State->Compressed = BI_RGB;
315 316 317 318 319
	} else {
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has unsupported header size"));
320
		State->read_state = READ_STATE_ERROR;
321 322
		return FALSE;
	}
323

324 325 326 327 328
	if (State->Header.size == 12)
		clrUsed = 1 << State->Header.depth;
	else
		clrUsed = (int) (BIH[35] << 24) + (BIH[34] << 16) + (BIH[33] << 8) + (BIH[32]);

329 330 331
	if (clrUsed != 0)
		State->Header.n_colors = clrUsed;
	else
332
            State->Header.n_colors = (1 << State->Header.depth);
333
	
334
	if (State->Header.n_colors > (1 << State->Header.depth)) {
335 336 337 338 339 340 341 342
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has bogus header data"));
		State->read_state = READ_STATE_ERROR;
		return FALSE;
	}

343
	State->Type = State->Header.depth;	/* This may be less trivial someday */
344

345
	/* Negative heights indicates bottom-down pixelorder */
346 347 348 349
	if (State->Header.height < 0) {
		State->Header.height = -State->Header.height;
		State->Header.Negative = 1;
	}
350 351 352 353 354 355 356 357 358 359

	if (State->Header.Negative && 
	    (State->Compressed != BI_RGB && State->Compressed != BI_BITFIELDS))
	{
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("Topdown BMP images cannot be compressed"));
		State->read_state = READ_STATE_ERROR;
		return FALSE;
360
	}
361

362
	if (State->Header.width <= 0 || State->Header.height == 0 ||
363 364
	    (State->Compressed == BI_RLE4 && State->Type != 4)    ||
	    (State->Compressed == BI_RLE8 && State->Type != 8)	  ||
365
	    (State->Compressed == BI_BITFIELDS && !(State->Type == 16 || State->Type == 32)) ||
366
	    (State->Compressed > BI_BITFIELDS)) {
367 368 369 370
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has bogus header data"));
371
		State->read_state = READ_STATE_ERROR;
372 373 374
		return FALSE;
	}

375 376
	if (State->Type == 32)
		State->LineWidth = State->Header.width * 4;
377
	else if (State->Type == 24)
378
		State->LineWidth = State->Header.width * 3;
379 380 381
	else if (State->Type == 16)
		State->LineWidth = State->Header.width * 2;
	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 387 388
		State->LineWidth = State->Header.width / 8;
		if ((State->Header.width & 7) != 0)
			State->LineWidth++;
389 390 391 392 393
	} else {
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has bogus header data"));
394
		State->read_state = READ_STATE_ERROR;
395
		return FALSE;
396
	}
397

398
	/* Pad to a 32 bit boundary */
399 400
	if (((State->LineWidth % 4) > 0)
	    && (State->Compressed == BI_RGB || State->Compressed == BI_BITFIELDS))
401 402
		State->LineWidth = (State->LineWidth / 4) * 4 + 4;

403
	if (State->pixbuf == NULL) {
404 405 406 407 408 409 410 411 412 413 414 415
		if (State->size_func) {
			gint width = State->Header.width;
			gint height = State->Header.height;

			(*State->size_func) (&width, &height, State->user_data);
			if (width == 0 || height == 0) {
				State->read_state = READ_STATE_DONE;
				State->BufferSize = 0;
				return TRUE;
			}
		}

416 417 418
		if (State->Type == 32 || 
		    State->Compressed == BI_RLE4 || 
		    State->Compressed == BI_RLE8)
419
			State->pixbuf =
420 421 422
				gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
					       (gint) State->Header.width,
					       (gint) State->Header.height);
423 424
		else
			State->pixbuf =
425 426 427 428 429 430 431 432 433
				gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
					       (gint) State->Header.width,
					       (gint) State->Header.height);
		
		if (State->pixbuf == NULL) {
			g_set_error (error,
				     GDK_PIXBUF_ERROR,
				     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
				     _("Not enough memory to load bitmap image"));
434
			State->read_state = READ_STATE_ERROR;
435 436 437
			return FALSE;
			}
		
438 439
		if (State->prepared_func != NULL)
			/* Notify the client that we are ready to go */
Havoc Pennington's avatar
Havoc Pennington committed
440
			(*State->prepared_func) (State->pixbuf, NULL, State->user_data);
441 442 443 444 445 446 447
		
		/* make all pixels initially transparent */
		if (State->Compressed == BI_RLE4 || State->Compressed == BI_RLE8) {
			memset (State->pixbuf->pixels, 0, State->pixbuf->rowstride * State->Header.height);
			State->compr.p = State->pixbuf->pixels 
				+ State->pixbuf->rowstride * (State->Header.height- 1);
		}
448
	}
449
	
450
	State->BufferDone = 0;
451
	if (State->Type <= 8) {
452 453
                gint samples;

454
		State->read_state = READ_STATE_PALETTE;
455 456 457 458 459 460 461 462 463 464

		/* Allocate enough to hold the palette */
	        samples = (State->Header.size == 12 ? 3 : 4);
		State->BufferSize = State->Header.n_colors * samples;

		/* Skip over everything between the palette and the data.
		   This protects us against a malicious BFH[10] value.
                */
		State->BufferPadding = (lsb_32 (&BFH[10]) - 14 - State->Header.size) - State->BufferSize;

465
	} else if (State->Compressed == BI_RGB) {
466 467 468 469 470 471 472 473 474 475 476 477
		if (State->BufferSize < lsb_32 (&BFH[10]))
		{
			/* skip over padding between headers and image data */
			State->read_state = READ_STATE_HEADERS;
			State->BufferDone = State->BufferSize;
			State->BufferSize = lsb_32 (&BFH[10]);
		}
		else
		{
			State->read_state = READ_STATE_DATA;
			State->BufferSize = State->LineWidth;
		}
478
	} else if (State->Compressed == BI_BITFIELDS) {
479 480 481 482 483 484 485 486 487 488 489 490 491
               if (State->Header.size == 108 || State->Header.size == 124) 
               {
			/* v4 and v5 have the bitmasks in the header */
			if (!decode_bitmasks (&BIH[40], State, error)) {
			       State->read_state = READ_STATE_ERROR;
			       return FALSE;
                        }
               }
               else 
               {
		       State->read_state = READ_STATE_BITMASKS;
		       State->BufferSize = 12;
               }
492 493 494 495 496 497 498 499
	} else {
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has bogus header data"));
		State->read_state = READ_STATE_ERROR;
		return FALSE;
	}
500

501 502
	if (!grow_buffer (State, error)) 
		return FALSE;
503 504 505 506

        return TRUE;
}

507 508 509
static gboolean DecodeColormap (guchar *buff,
				struct bmp_progressive_state *State,
				GError **error)
510 511
{
	gint i;
512
	gint samples;
513

514 515
	g_assert (State->read_state == READ_STATE_PALETTE);

516 517 518 519 520 521 522 523
	samples = (State->Header.size == 12 ? 3 : 4);
	if (State->BufferSize < State->Header.n_colors * samples) {
		State->BufferSize = State->Header.n_colors * samples;
		if (!grow_buffer (State, error))
			return FALSE;
		return TRUE;
	}

524
	State->Colormap = g_malloc0 ((1 << State->Header.depth) * sizeof (*State->Colormap));
525
	for (i = 0; i < State->Header.n_colors; i++)
526 527

	{
528 529 530
		State->Colormap[i][0] = buff[i * samples];
		State->Colormap[i][1] = buff[i * samples + 1];
		State->Colormap[i][2] = buff[i * samples + 2];
531 532 533 534 535 536
#ifdef DUMPCMAP
		g_print ("color %d %x %x %x\n", i,
			 State->Colormap[i][0],
			 State->Colormap[i][1],
			 State->Colormap[i][2]);
#endif
537 538
	}

539 540
	State->read_state = READ_STATE_DATA;

541
	State->BufferDone = 0;
542
	if (!(State->Compressed == BI_RGB || State->Compressed == BI_BITFIELDS))
543 544 545
		State->BufferSize = 2;
	else
		State->BufferSize = State->LineWidth;
546 547 548
	
	if (!grow_buffer (State, error))
		return FALSE;
549

550
	return TRUE;
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
}

/* Finds the lowest set bit and the number of set bits */
static void
find_bits (int n, int *lowest, int *n_set)
{
	int i;

	*n_set = 0;

	for (i = 31; i >= 0; i--)
		if (n & (1 << i)) {
			*lowest = i;
			(*n_set)++;
		}
}

568
/* Decodes the bitmasks for BI_BITFIELDS coding */
569 570 571 572
static gboolean
decode_bitmasks (guchar *buf,
		 struct bmp_progressive_state *State, 
		 GError **error)
573
{
574
        State->a_mask = State->a_shift = State->a_bits = 0;
575 576 577 578 579 580 581 582 583 584 585 586
	State->r_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
	buf += 4;

	State->g_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
	buf += 4;

	State->b_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);

	find_bits (State->r_mask, &State->r_shift, &State->r_bits);
	find_bits (State->g_mask, &State->g_shift, &State->g_bits);
	find_bits (State->b_mask, &State->b_shift, &State->b_bits);

587 588 589 590 591 592 593
        /* v4 and v5 have an alpha mask */
        if (State->Header.size == 108 || State->Header.size == 124) {
	      buf += 4;
	      State->a_mask = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
	      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 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
                if (State->Type == 16) {
		       State->r_mask = 0x7c00;
		       State->r_shift = 10;
		       State->g_mask = 0x03e0;
		       State->g_shift = 5;
		       State->b_mask = 0x001f;
		       State->b_shift = 0;

		       State->r_bits = State->g_bits = State->b_bits = 5;
                }
                else {
		       State->r_mask = 0x00ff0000;
		       State->r_shift = 16;
		       State->g_mask = 0x0000ff00;
		       State->g_shift = 8;
		       State->b_mask = 0x000000ff;
		       State->b_shift = 0;
		       State->a_mask = 0xff000000;
		       State->a_shift = 24;

		       State->r_bits = State->g_bits = State->b_bits = State->a_bits = 8;
                }
617 618
	}

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
        if (State->r_bits > 8) {
          State->r_shift += State->r_bits - 8;
          State->r_bits = 8;
        }
        if (State->g_bits > 8) {
          State->g_shift += State->g_bits - 8;
          State->g_bits = 8;
        }
        if (State->b_bits > 8) {
          State->b_shift += State->b_bits - 8;
          State->b_bits = 8;
        }
        if (State->a_bits > 8) {
          State->a_shift += State->a_bits - 8;
          State->a_bits = 8;
        }

636 637 638
	State->read_state = READ_STATE_DATA;
	State->BufferDone = 0;
	State->BufferSize = State->LineWidth;
639 640 641 642
	if (!grow_buffer (State, error)) 
		return FALSE;

	return TRUE;
643 644
}

645
/*
646 647 648 649 650
 * func - called when we have pixmap created (but no image data)
 * user_data - passed as arg 1 to func
 * return context (opaque to user)
 */

651
static gpointer
652 653 654
gdk_pixbuf__bmp_image_begin_load(GdkPixbufModuleSizeFunc size_func,
                                 GdkPixbufModulePreparedFunc prepared_func,
				 GdkPixbufModuleUpdatedFunc updated_func,
Havoc Pennington's avatar
Havoc Pennington committed
655
                                 gpointer user_data,
Havoc Pennington's avatar
Havoc Pennington committed
656
                                 GError **error)
657 658
{
	struct bmp_progressive_state *context;
659
	
660
	context = g_new0(struct bmp_progressive_state, 1);
661
	context->size_func = size_func;
662 663 664 665
	context->prepared_func = prepared_func;
	context->updated_func = updated_func;
	context->user_data = user_data;

666 667
	context->read_state = READ_STATE_HEADERS;

668
	context->BufferSize = 26;
669
	context->BufferPadding = 0;
670 671
	context->buff = g_malloc(26);
	context->BufferDone = 0;
672
	/* 14 for the BitmapFileHeader, 12 for the BitmapImageHeader */
673

674
	context->Colormap = NULL;
675 676 677 678 679 680

	context->Lines = 0;

	context->Type = 0;

	memset(&context->Header, 0, sizeof(struct headerpair));
681
	memset(&context->compr, 0, sizeof(struct bmp_compression_state));
682 683 684


	context->pixbuf = NULL;
685
	
686 687 688 689 690 691 692 693
	return (gpointer) context;
}

/*
 * context - returned from image_begin_load
 *
 * free context, unref gdk_pixbuf
 */
694
static gboolean gdk_pixbuf__bmp_image_stop_load(gpointer data, GError **error)
695 696 697 698
{
	struct bmp_progressive_state *context =
	    (struct bmp_progressive_state *) data;

699 700
        /* FIXME this thing needs to report errors if
         * we have unused image data
701
         */
702
	
703
	g_return_val_if_fail(context != NULL, TRUE);
704

705
	g_free(context->Colormap);
706 707

	if (context->pixbuf)
708
		g_object_unref(context->pixbuf);
709

710
	g_free(context->buff);
711
	g_free(context);
712 713

        return TRUE;
714 715 716
}


717 718 719 720 721 722
/*
The OneLineXX functions are called when 1 line worth of data is present.
OneLine24 is the 24 bpp-version.
*/
static void OneLine32(struct bmp_progressive_state *context)
{
723
	int i;
724 725
	guchar *pixels;
	guchar *src;
726

727 728 729
	if (!context->Header.Negative)
		pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride * (context->Header.height - context->Lines - 1));
730
	else
731 732 733 734 735 736 737 738 739
		pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride * context->Lines);

	src = context->buff;

	if (context->Compressed == BI_BITFIELDS) {
		int r_lshift, r_rshift;
		int g_lshift, g_rshift;
		int b_lshift, b_rshift;
740
		int a_lshift, a_rshift;
741 742 743 744

		r_lshift = 8 - context->r_bits;
		g_lshift = 8 - context->g_bits;
		b_lshift = 8 - context->b_bits;
745
		a_lshift = 8 - context->a_bits;
746 747 748 749

		r_rshift = context->r_bits - r_lshift;
		g_rshift = context->g_bits - g_lshift;
		b_rshift = context->b_bits - b_lshift;
750
		a_rshift = context->a_bits - a_lshift;
751

752
		for (i = 0; i < context->Header.width; i++) {
753
			int v, r, g, b, a;
754

755
			v = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
756 757 758 759

			r = (v & context->r_mask) >> context->r_shift;
			g = (v & context->g_mask) >> context->g_shift;
			b = (v & context->b_mask) >> context->b_shift;
760
			a = (v & context->a_mask) >> context->a_shift;
761 762 763 764

			*pixels++ = (r << r_lshift) | (r >> r_rshift);
			*pixels++ = (g << g_lshift) | (g >> g_rshift);
			*pixels++ = (b << b_lshift) | (b >> b_rshift);
765
                        if (context->a_bits)
Matthias Clasen's avatar
Matthias Clasen committed
766
			  *pixels++ = 0xff - ((a << a_lshift) | (a >> a_rshift));
767 768
                        else
                          *pixels++ = 0xff;
769 770 771 772 773 774 775 776

			src += 4;
		}
	} else
		for (i = 0; i < context->Header.width; i++) {
			*pixels++ = src[2];
			*pixels++ = src[1];
			*pixels++ = src[0];
777
			*pixels++ = 0xff;
778 779 780

			src += 4;
		}
781 782
}

783 784 785 786 787 788
static void OneLine24(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
789
	if (context->Header.Negative == 0)
790 791 792
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
793
	else
794 795 796
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
797
	while (X < context->Header.width) {
798 799 800
		Pixels[X * 3 + 0] = context->buff[X * 3 + 2];
		Pixels[X * 3 + 1] = context->buff[X * 3 + 1];
		Pixels[X * 3 + 2] = context->buff[X * 3 + 0];
801 802 803 804 805
		X++;
	}

}

806 807
static void OneLine16(struct bmp_progressive_state *context)
{
808 809 810
	int i;
	guchar *pixels;
	guchar *src;
811

812 813 814
	if (!context->Header.Negative)
		pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride * (context->Header.height - context->Lines - 1));
815
	else
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
		pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride * context->Lines);

	src = context->buff;

	if (context->Compressed == BI_BITFIELDS) {
		int r_lshift, r_rshift;
		int g_lshift, g_rshift;
		int b_lshift, b_rshift;

		r_lshift = 8 - context->r_bits;
		g_lshift = 8 - context->g_bits;
		b_lshift = 8 - context->b_bits;

		r_rshift = context->r_bits - r_lshift;
		g_rshift = context->g_bits - g_lshift;
		b_rshift = context->b_bits - b_lshift;

		for (i = 0; i < context->Header.width; i++) {
			int v, r, g, b;

			v = (int) src[0] | ((int) src[1] << 8);

			r = (v & context->r_mask) >> context->r_shift;
			g = (v & context->g_mask) >> context->g_shift;
			b = (v & context->b_mask) >> context->b_shift;

			*pixels++ = (r << r_lshift) | (r >> r_rshift);
			*pixels++ = (g << g_lshift) | (g >> g_rshift);
			*pixels++ = (b << b_lshift) | (b >> b_rshift);

			src += 2;
		}
	} else
		for (i = 0; i < context->Header.width; i++) {
			int v, r, g, b;

			v = src[0] | (src[1] << 8);

			r = (v >> 10) & 0x1f;
			g = (v >> 5) & 0x1f;
			b = v & 0x1f;

			*pixels++ = (r << 3) | (r >> 2);
			*pixels++ = (g << 3) | (g >> 2);
			*pixels++ = (b << 3) | (b >> 2);
862 863

			src += 2;
864
		}
865 866
}

867 868 869 870 871 872
static void OneLine8(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
873
	if (context->Header.Negative == 0)
874 875 876
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
877
	else
878 879 880
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
881 882
	while (X < context->Header.width) {
		Pixels[X * 3 + 0] =
883
		    context->Colormap[context->buff[X]][2];
884
		Pixels[X * 3 + 1] =
885
		    context->Colormap[context->buff[X]][1];
886
		Pixels[X * 3 + 2] =
887
		    context->Colormap[context->buff[X]][0];
888 889 890 891
		X++;
	}
}

892 893 894 895 896 897 898
static void OneLine4(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
	if (context->Header.Negative == 0)
899 900 901
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
902
	else
903 904 905
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
906 907 908 909

	while (X < context->Header.width) {
		guchar Pix;

910
		Pix = context->buff[X / 2];
911 912

		Pixels[X * 3 + 0] =
913
		    context->Colormap[Pix >> 4][2];
914
		Pixels[X * 3 + 1] =
915
		    context->Colormap[Pix >> 4][1];
916
		Pixels[X * 3 + 2] =
917
		    context->Colormap[Pix >> 4][0];
918 919 920 921
		X++;
		if (X < context->Header.width) {
			/* Handle the other 4 bit pixel only when there is one */
			Pixels[X * 3 + 0] =