io-bmp.c 28.1 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

36

37

38 39 40 41 42 43
#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.
 */
44
struct BitmapFileHeader {
45 46 47 48
	guint16 magic;
	guint32 file_size;
	guint32 reserved;
	guint32 data_offset;
49 50 51
};

struct BitmapInfoHeader {
52 53 54 55 56 57 58 59 60 61 62
	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;
63
};
64
#endif
65

66
/* Compression values */
67 68 69 70 71 72

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

73 74 75 76 77 78 79 80 81
/* 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;
82

83 84 85
/*

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

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

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

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

/* Progressive loading */

struct bmp_progressive_state {
147
	GdkPixbufModuleSizeFunc size_func;
148 149
	GdkPixbufModulePreparedFunc prepared_func;
	GdkPixbufModuleUpdatedFunc updated_func;
150 151
	gpointer user_data;

152 153
	ReadState read_state;

154 155
	guint LineWidth;
	guint Lines;		/* # of finished lines */
156

157 158 159 160 161
	guchar *buff;
	gint BufferSize;
	gint BufferDone;

	guchar (*Colormap)[3];
162

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


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

177 178 179 180
	/* 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;
181 182 183 184

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

185
static gpointer
186 187 188
gdk_pixbuf__bmp_image_begin_load(GdkPixbufModuleSizeFunc size_func,
                                 GdkPixbufModulePreparedFunc prepared_func,
				 GdkPixbufModuleUpdatedFunc updated_func,
Havoc Pennington's avatar
Havoc Pennington committed
189
                                 gpointer user_data,
Havoc Pennington's avatar
Havoc Pennington committed
190
                                 GError **error);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
191

192 193 194 195 196
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);
197

Mark Crichton's avatar
Mark Crichton committed
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
/* 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);
}

216 217 218
static gboolean grow_buffer (struct bmp_progressive_state *State,
                             GError **error)
{
219 220
  guchar *tmp = g_try_realloc (State->buff, State->BufferSize);
  if (!tmp) {
221 222 223 224 225 226 227
    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;
  }
228
  State->buff = tmp;
229 230 231
  return TRUE;
}

232 233 234
static gboolean DecodeHeader(unsigned char *BFH, unsigned char *BIH,
                             struct bmp_progressive_state *State,
                             GError **error)
235
{
236
        /* FIXME this is totally unrobust against bogus image data. */
237

238 239
	if (State->BufferSize < lsb_32 (&BIH[0]) + 14) {
		State->BufferSize = lsb_32 (&BIH[0]) + 14;
240
		if (!grow_buffer (State, error))
Matthias Clasen's avatar
Matthias Clasen committed
241
			return FALSE;
242 243 244
		return TRUE;
	}

245 246 247 248
#if DUMPBIH
	DumpBIH(BIH);
#endif

249
	State->Header.size = lsb_32 (&BIH[0]);
250
	if (State->Header.size == 40) {
251 252 253 254
		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]);
255
	} else if (State->Header.size == 12) {
256 257 258
		State->Header.width = lsb_16 (&BIH[4]);
		State->Header.height = lsb_16 (&BIH[6]);
		State->Header.depth = lsb_16 (&BIH[10]);
259
		State->Compressed = BI_RGB;
260 261 262 263 264
	} else {
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has unsupported header size"));
265
		State->read_state = READ_STATE_ERROR;
266 267
		return FALSE;
	}
268 269

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

271
	/* Negative heights indicates bottom-down pixelorder */
272 273 274 275 276 277 278 279
	if (State->Header.height < 0) {
		State->Header.height = -State->Header.height;
		State->Header.Negative = 1;
	}
	if (State->Header.width < 0) {
		State->Header.width = -State->Header.width;
		State->Header.Negative = 0;
	}
280

281 282 283
	if (State->Header.width == 0 || State->Header.height == 0 ||
	    (State->Compressed == BI_RLE4 && State->Type != 4)    ||
	    (State->Compressed == BI_RLE8 && State->Type != 8)	  ||
284 285
	    (State->Compressed == BI_BITFIELDS && !(State->Type == 16 || State->Type == 32)) ||
	    State->Compressed > BI_BITFIELDS) {
286 287 288 289
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has bogus header data"));
290
		State->read_state = READ_STATE_ERROR;
291 292 293
		return FALSE;
	}

294 295
	if (State->Type == 32)
		State->LineWidth = State->Header.width * 4;
296
	else if (State->Type == 24)
297
		State->LineWidth = State->Header.width * 3;
298 299 300
	else if (State->Type == 16)
		State->LineWidth = State->Header.width * 2;
	else if (State->Type == 8)
301
		State->LineWidth = State->Header.width * 1;
302
	else if (State->Type == 4)
303
		State->LineWidth = (State->Header.width + 1) / 2;
304
	else if (State->Type == 1) {
305 306 307
		State->LineWidth = State->Header.width / 8;
		if ((State->Header.width & 7) != 0)
			State->LineWidth++;
308 309 310 311 312
	} else {
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has bogus header data"));
313
		State->read_state = READ_STATE_ERROR;
314
		return FALSE;
315
	}
316

317
	/* Pad to a 32 bit boundary */
318 319
	if (((State->LineWidth % 4) > 0)
	    && (State->Compressed == BI_RGB || State->Compressed == BI_BITFIELDS))
320 321
		State->LineWidth = (State->LineWidth / 4) * 4 + 4;

322
	if (State->pixbuf == NULL) {
323 324 325 326 327 328 329 330 331 332 333 334
		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;
			}
		}

335 336 337
		if (State->Type == 32 || 
		    State->Compressed == BI_RLE4 || 
		    State->Compressed == BI_RLE8)
338
			State->pixbuf =
339 340 341
				gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
					       (gint) State->Header.width,
					       (gint) State->Header.height);
342 343
		else
			State->pixbuf =
344 345 346 347 348 349 350 351 352
				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"));
353
			State->read_state = READ_STATE_ERROR;
354 355 356
			return FALSE;
			}
		
357 358
		if (State->prepared_func != NULL)
			/* Notify the client that we are ready to go */
Havoc Pennington's avatar
Havoc Pennington committed
359
			(*State->prepared_func) (State->pixbuf, NULL, State->user_data);
360 361 362 363 364 365 366
		
		/* 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);
		}
367
	}
368
	
369
	State->BufferDone = 0;
370 371
	if (State->Type <= 8) {
		State->read_state = READ_STATE_PALETTE;
372
		State->BufferSize = lsb_32 (&BFH[10]) - 14 - State->Header.size; 
373 374
	} else if (State->Compressed == BI_RGB) {
		State->read_state = READ_STATE_DATA;
375
		State->BufferSize = State->LineWidth;
376 377 378
	} else if (State->Compressed == BI_BITFIELDS) {
		State->read_state = READ_STATE_BITMASKS;
		State->BufferSize = 12;
379 380 381 382 383 384 385 386
	} 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;
	}
387

388 389
	if (!grow_buffer (State, error)) 
		return FALSE;
390 391 392 393

        return TRUE;
}

394 395 396
static gboolean DecodeColormap (guchar *buff,
				struct bmp_progressive_state *State,
				GError **error)
397 398 399
{
	gint i;

400 401
	g_assert (State->read_state == READ_STATE_PALETTE);

402 403 404 405 406 407 408
	State->Colormap = g_malloc ((1 << State->Header.depth) * sizeof (*State->Colormap));

	for (i = 0; i < (1 << State->Header.depth); i++)
	{
		State->Colormap[i][0] = buff[i * (State->Header.size == 12 ? 3 : 4)];
		State->Colormap[i][1] = buff[i * (State->Header.size == 12 ? 3 : 4) + 1];
		State->Colormap[i][2] = buff[i * (State->Header.size == 12 ? 3 : 4) + 2];
409 410 411 412 413 414
#ifdef DUMPCMAP
		g_print ("color %d %x %x %x\n", i,
			 State->Colormap[i][0],
			 State->Colormap[i][1],
			 State->Colormap[i][2]);
#endif
415 416
	}

417 418
	State->read_state = READ_STATE_DATA;

419
	State->BufferDone = 0;
420
	if (!(State->Compressed == BI_RGB || State->Compressed == BI_BITFIELDS))
421 422 423
		State->BufferSize = 2;
	else
		State->BufferSize = State->LineWidth;
424 425 426
	
	if (!grow_buffer (State, error))
		return FALSE;
427

428
	return TRUE;
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
}

/* 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)++;
		}
}

/* Decodes the 3 shorts that follow for the bitmasks for BI_BITFIELDS coding */
447 448 449 450
static gboolean
decode_bitmasks (guchar *buf,
		 struct bmp_progressive_state *State, 
		 GError **error)
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
{
	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);

	if (State->r_bits == 0 || State->g_bits == 0 || State->b_bits == 0) {
		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;
	}

	State->read_state = READ_STATE_DATA;
	State->BufferDone = 0;
	State->BufferSize = State->LineWidth;
478 479 480 481
	if (!grow_buffer (State, error)) 
		return FALSE;

	return TRUE;
482 483
}

484
/*
485 486 487 488 489
 * func - called when we have pixmap created (but no image data)
 * user_data - passed as arg 1 to func
 * return context (opaque to user)
 */

490
static gpointer
491 492 493
gdk_pixbuf__bmp_image_begin_load(GdkPixbufModuleSizeFunc size_func,
                                 GdkPixbufModulePreparedFunc prepared_func,
				 GdkPixbufModuleUpdatedFunc updated_func,
Havoc Pennington's avatar
Havoc Pennington committed
494
                                 gpointer user_data,
Havoc Pennington's avatar
Havoc Pennington committed
495
                                 GError **error)
496 497 498 499
{
	struct bmp_progressive_state *context;

	context = g_new0(struct bmp_progressive_state, 1);
500
	context->size_func = size_func;
501 502 503 504
	context->prepared_func = prepared_func;
	context->updated_func = updated_func;
	context->user_data = user_data;

505 506
	context->read_state = READ_STATE_HEADERS;

507 508 509
	context->BufferSize = 26;
	context->buff = g_malloc(26);
	context->BufferDone = 0;
510
	/* 14 for the BitmapFileHeader, 12 for the BitmapImageHeader */
511

512
	context->Colormap = NULL;
513 514 515 516 517 518

	context->Lines = 0;

	context->Type = 0;

	memset(&context->Header, 0, sizeof(struct headerpair));
519
	memset(&context->compr, 0, sizeof(struct bmp_compression_state));
520 521 522 523 524 525 526 527 528 529 530 531 532


	context->pixbuf = NULL;


	return (gpointer) context;
}

/*
 * context - returned from image_begin_load
 *
 * free context, unref gdk_pixbuf
 */
533
static gboolean gdk_pixbuf__bmp_image_stop_load(gpointer data, GError **error)
534 535 536 537
{
	struct bmp_progressive_state *context =
	    (struct bmp_progressive_state *) data;

538 539
        /* FIXME this thing needs to report errors if
         * we have unused image data
540
         */
541

542
	g_return_val_if_fail(context != NULL, TRUE);
543

544 545
	if (context->Colormap != NULL)
		g_free(context->Colormap);
546 547

	if (context->pixbuf)
548
		g_object_unref(context->pixbuf);
549

550
	g_free(context->buff);
551
	g_free(context);
552 553

        return TRUE;
554 555 556
}


557 558 559 560 561 562
/*
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)
{
563
	int i;
564 565
	guchar *pixels;
	guchar *src;
566

567 568 569
	if (!context->Header.Negative)
		pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride * (context->Header.height - context->Lines - 1));
570
	else
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
		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;
588

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
		for (i = 0; i < context->Header.width; i++) {
			int v, r, g, b;

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

			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);
			*pixels++ = src[3]; /* alpha */

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

			src += 4;
		}
614 615
}

616 617 618 619 620 621
static void OneLine24(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
622
	if (context->Header.Negative == 0)
623 624 625
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
626
	else
627 628 629
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
630
	while (X < context->Header.width) {
631 632 633
		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];
634 635 636 637 638
		X++;
	}

}

639 640
static void OneLine16(struct bmp_progressive_state *context)
{
641 642 643
	int i;
	guchar *pixels;
	guchar *src;
644

645 646 647
	if (!context->Header.Negative)
		pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride * (context->Header.height - context->Lines - 1));
648
	else
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		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);
695 696

			src += 2;
697
		}
698 699
}

700 701 702 703 704 705
static void OneLine8(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
706
	if (context->Header.Negative == 0)
707 708 709
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
710
	else
711 712 713
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
714 715
	while (X < context->Header.width) {
		Pixels[X * 3 + 0] =
716
		    context->Colormap[context->buff[X]][2];
717
		Pixels[X * 3 + 1] =
718
		    context->Colormap[context->buff[X]][1];
719
		Pixels[X * 3 + 2] =
720
		    context->Colormap[context->buff[X]][0];
721 722 723 724
		X++;
	}
}

725 726 727 728 729 730 731
static void OneLine4(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
	if (context->Header.Negative == 0)
732 733 734
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
735
	else
736 737 738
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
739 740 741 742

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

743
		Pix = context->buff[X / 2];
744 745

		Pixels[X * 3 + 0] =
746
		    context->Colormap[Pix >> 4][2];
747
		Pixels[X * 3 + 1] =
748
		    context->Colormap[Pix >> 4][1];
749
		Pixels[X * 3 + 2] =
750
		    context->Colormap[Pix >> 4][0];
751 752 753 754
		X++;
		if (X < context->Header.width) {
			/* Handle the other 4 bit pixel only when there is one */
			Pixels[X * 3 + 0] =
755
			    context->Colormap[Pix & 15][2];
756
			Pixels[X * 3 + 1] =
757
			    context->Colormap[Pix & 15][1];
758
			Pixels[X * 3 + 2] =
759
			    context->Colormap[Pix & 15][0];
760 761 762 763 764 765
			X++;
		}
	}

}

766 767 768 769 770 771
static void OneLine1(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
772
	if (context->Header.Negative == 0)
773 774 775
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
776
	else
777 778 779
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
780
	while (X < context->Header.width) {
781
		gint Bit;
782

783
		Bit = (context->buff[X / 8]) >> (7 - (X & 7));
784
		Bit = Bit & 1;
785 786 787
		Pixels[X * 3 + 0] = context->Colormap[Bit][2];
		Pixels[X * 3 + 1] = context->Colormap[Bit][1];
		Pixels[X * 3 + 2] = context->Colormap[Bit][0];
788 789 790 791 792 793 794
		X++;
	}
}


static void OneLine(struct bmp_progressive_state *context)
{
795
	context->BufferDone = 0;
796 797 798
	if (context->Lines >= context->Header.height)
		return;

799 800
	if (context->Type == 32)
		OneLine32(context);
801
	else if (context->Type == 24)
802
		OneLine24(context);
803
	else if (context->Type == 16)
804
		OneLine16(context);
805
	else if (context->Type == 8)
806
		OneLine8(context);
807
	else if (context->Type == 4)
808
		OneLine4(context);
809
	else if (context->Type == 1)
810
		OneLine1(context);
811 812
	else
		g_assert_not_reached ();
813 814 815 816 817 818

	context->Lines++;

	if (context->updated_func != NULL) {
		(*context->updated_func) (context->pixbuf,
					  0,
819 820 821
					  (context->Header.Negative ?
					   (context->Lines - 1) :
					   (context->Header.height - context->Lines)),
822
					  context->Header.width,
823
					  1,
824
					  context->user_data);
825 826 827 828

	}
}

829 830 831 832 833 834 835 836 837 838 839 840 841 842
#define NEUTRAL       0
#define ENCODED       1
#define ESCAPE        2   
#define DELTA_X       3
#define DELTA_Y       4
#define ABSOLUTE      5
#define SKIP          6

#define END_OF_LINE   0
#define END_OF_BITMAP 1
#define DELTA         2

static gboolean 
DoCompressed(struct bmp_progressive_state *context, GError **error)
843
{
844 845 846 847 848 849 850
	gint i, j;
	gint y;
	guchar c;
	gint idx;

	if (context->compr.y >= context->Header.height)
		return TRUE;
851

852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
	y = context->compr.y;

 	for (i = 0; i < context->BufferSize; i++) {
		c = context->buff[i];
		switch (context->compr.phase) {
		    case NEUTRAL:
			    if (c) {
				    context->compr.run = c;
				    context->compr.phase = ENCODED;
			    }
			    else
				    context->compr.phase = ESCAPE;
			    break;
		    case ENCODED:
			    for (j = 0; j < context->compr.run; j++) {
				    if (context->Compressed == BI_RLE8)
					    idx = c;
				    else if (j & 1) 
					    idx = c & 0x0f;
				    else 
					    idx = (c >> 4) & 0x0f;
				    if (context->compr.x < context->Header.width) {
					    *context->compr.p++ = context->Colormap[idx][2];
					    *context->compr.p++ = context->Colormap[idx][1];
					    *context->compr.p++ = context->Colormap[idx][0];
					    *context->compr.p++ = 0xff;
					    context->compr.x++;    
				    }
			    }
			    context->compr.phase = NEUTRAL;
			    break;
		    case ESCAPE:
			    switch (c) {
				case END_OF_LINE:
					context->compr.x = 0;
					context->compr.y++;
					context->compr.p = context->pixbuf->pixels 
						+ (context->pixbuf->rowstride * (context->Header.height - context->compr.y - 1))
						+ (4 * context->compr.x);
					context->compr.phase = NEUTRAL;
					break;
				case END_OF_BITMAP:
					context->compr.x = 0;
					context->compr.y = context->Header.height;
					context->compr.phase = NEUTRAL;
					break;
				case DELTA:
					context->compr.phase = DELTA_X;
					break;
				default:
					context->compr.run = c;
					context->compr.count = 0;
					context->compr.phase = ABSOLUTE;
					break;
			    }
			    break;
		    case DELTA_X:
			    context->compr.x += c;
			    context->compr.phase = DELTA_Y;
			    break;
		    case DELTA_Y:
			    context->compr.y += c;
			    context->compr.p = context->pixbuf->pixels 
				    + (context->pixbuf->rowstride * (context->Header.height - context->compr.y - 1))
				    + (4 * context->compr.x);
			    context->compr.phase = NEUTRAL;
			    break;
		    case ABSOLUTE:
			    if (context->Compressed == BI_RLE8) {
				    idx = c;
				    if (context->compr.x < context->Header.width) {
					    *context->compr.p++ = context->Colormap[idx][2];
					    *context->compr.p++ = context->Colormap[idx][1];
					    *context->compr.p++ = context->Colormap[idx][0];
					    *context->compr.p++ = 0xff;
					    context->compr.x++;    
				    }
				    context->compr.count++;

				    if (context->compr.count == context->compr.run) {
					    if (context->compr.run & 1)
						    context->compr.phase = SKIP;
					    else
						    context->compr.phase = NEUTRAL;
				    }
			    }
			    else {
				    for (j = 0; j < 2; j++) {
					    if (context->compr.count & 1)
						    idx = c & 0x0f;
					    else 
						    idx = (c >> 4) & 0x0f;
					    if (context->compr.x < context->Header.width) {
						    *context->compr.p++ = context->Colormap[idx][2];
						    *context->compr.p++ = context->Colormap[idx][1];
						    *context->compr.p++ = context->Colormap[idx][0];
						    *context->compr.p++ = 0xff;
						    context->compr.x++;    
					    }
					    context->compr.count++;

					    if (context->compr.count == context->compr.run) {
						    if ((context->compr.run & 3) == 1
							|| (context->compr.run & 3) == 2) 
							    context->compr.phase = SKIP;
						    else
							    context->compr.phase = NEUTRAL;
						    break;
					    }
				    }
			    }
			    break;
		    case SKIP:
			    context->compr.phase = NEUTRAL;
			    break;
967
		}
968
	}
969 970 971 972 973 974 975 976 977 978 979 980 981
	if (context->updated_func != NULL) {
		if (context->compr.y > y)
			(*context->updated_func) (context->pixbuf,
						  0,
						  y,
						  context->Header.width,
						  context->compr.y - y,
						  context->user_data);

	}

	context->BufferDone = 0;
	return TRUE;
982 983
}

984 985 986 987 988 989 990
/*
 * context - from image_begin_load
 * buf - new image data
 * size - length of new image data
 *
 * append image data onto inrecrementally built output image
 */