io-bmp.c 26.8 KB
Newer Older
1 2 3
/* GdkPixbuf library - Windows Bitmap image loader
 *
 * Copyright (C) 1999 The Free Software Foundation
Mark Crichton's avatar
Mark Crichton committed
4
 *
5 6 7 8
 * 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
9 10
 *
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
Mark Crichton's avatar
Mark Crichton committed
12 13 14 15 16 17
 * 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
18
 * Lesser General Public License for more details.
Mark Crichton's avatar
Mark Crichton committed
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21 22 23
 * 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
24 25 26 27
 */

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

35

36

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

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

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

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

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

82 83 84
/*

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

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

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

134 135
/* Data needed for the "state" during decompression */
struct bmp_compression_state {
136
	gint phase;
137
	gint RunCount;
138 139 140 141

	guchar *linebuff;
	gint linebuffsize;	/* these two counts in nibbles */
	gint linebuffdone;
142
};
143 144 145 146 147 148 149 150

/* Progressive loading */

struct bmp_progressive_state {
	ModulePreparedNotifyFunc prepared_func;
	ModuleUpdatedNotifyFunc updated_func;
	gpointer user_data;

151 152
	ReadState read_state;

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

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

	guchar (*Colormap)[3];
161

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


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

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

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

184
static gpointer
Federico Mena Quintero's avatar
Federico Mena Quintero committed
185 186
gdk_pixbuf__bmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
				 ModuleUpdatedNotifyFunc updated_func,
Havoc Pennington's avatar
Havoc Pennington committed
187
                                 gpointer user_data,
Havoc Pennington's avatar
Havoc Pennington committed
188
                                 GError **error);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
189

190 191 192 193 194
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);
195

Mark Crichton's avatar
Mark Crichton committed
196 197


198 199
/* Shared library entry point --> This should be removed when
   generic_image_load enters gdk-pixbuf-io. */
200
static GdkPixbuf *gdk_pixbuf__bmp_image_load(FILE * f, GError **error)
Mark Crichton's avatar
Mark Crichton committed
201
{
202
	guchar membuf[4096];
203 204
	size_t length;
	struct bmp_progressive_state *State;
205

206
	GdkPixbuf *pb;
207

208
	State =
Havoc Pennington's avatar
Havoc Pennington committed
209
	    gdk_pixbuf__bmp_image_begin_load(NULL, NULL, NULL,
Havoc Pennington's avatar
Havoc Pennington committed
210
                                             error);
211

Havoc Pennington's avatar
Havoc Pennington committed
212 213
        if (State == NULL)
          return NULL;
214

215
	while (feof(f) == 0) {
216
		length = fread(membuf, 1, sizeof (membuf), f);
217
		if (length > 0)
Havoc Pennington's avatar
Havoc Pennington committed
218 219 220 221
                  if (!gdk_pixbuf__bmp_image_load_increment(State,
                                                            membuf,
                                                            length,
                                                            error)) {
222
                          gdk_pixbuf__bmp_image_stop_load (State, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
223 224
                          return NULL;
                  }
225 226

	}
227
	if (State->pixbuf != NULL)
228
		g_object_ref(State->pixbuf);
229 230 231

	pb = State->pixbuf;

232
	gdk_pixbuf__bmp_image_stop_load(State, NULL);
233
	return pb;
234 235
}

236 237 238
static gboolean DecodeHeader(unsigned char *BFH, unsigned char *BIH,
                             struct bmp_progressive_state *State,
                             GError **error)
239
{
240
        /* FIXME this is totally unrobust against bogus image data. */
241

242 243 244 245 246 247
	if (State->BufferSize < GUINT32_FROM_LE (* (guint32 *) &BIH[0]) + 14) {
		State->BufferSize = GUINT32_FROM_LE (* (guint32 *) &BIH[0]) + 14;
		State->buff = g_realloc (State->buff, State->BufferSize);
		return TRUE;
	}

248 249 250 251
#if DUMPBIH
	DumpBIH(BIH);
#endif

252 253 254 255 256 257 258 259 260 261
	State->Header.size = GUINT32_FROM_LE (* (guint32 *) &BIH[0]);
	if (State->Header.size == 40) {
		State->Header.width = GINT32_FROM_LE (* (gint32 *) &BIH[4]);
		State->Header.height = GINT32_FROM_LE (* (gint32 *) &BIH[8]);
		State->Header.depth = GUINT16_FROM_LE (* (guint16 *) &BIH[14]);
		State->Compressed = GUINT32_FROM_LE (* (guint32 *) &BIH[16]);
	} else if (State->Header.size == 12) {
		State->Header.width = GUINT16_FROM_LE (* (guint16 *) &BIH[4]);
		State->Header.height = GUINT16_FROM_LE (* (guint16 *) &BIH[6]);
		State->Header.depth = GUINT16_FROM_LE (* (guint16 *) &BIH[10]);
262
		State->Compressed = BI_RGB;
263 264 265 266 267
	} else {
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has unsupported header size"));
268
		State->read_state = READ_STATE_ERROR;
269 270
		return FALSE;
	}
271 272

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

274
	/* Negative heights indicates bottom-down pixelorder */
275 276 277 278 279 280 281 282
	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;
	}
283

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

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

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

325
	if (State->pixbuf == NULL) {
326 327
		if (State->Type == 32)
			State->pixbuf =
328
			    gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
329 330 331 332
					   (gint) State->Header.width,
					   (gint) State->Header.height);
		else
			State->pixbuf =
333
			    gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
334 335
					   (gint) State->Header.width,
					   (gint) State->Header.height);
336

337 338 339 340 341
                if (State->pixbuf == NULL) {
                        g_set_error (error,
                                     GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
                                     _("Not enough memory to load bitmap image"));
342
			State->read_state = READ_STATE_ERROR;
343
                        return FALSE;
344
                }
345

346 347
		if (State->prepared_func != NULL)
			/* Notify the client that we are ready to go */
Havoc Pennington's avatar
Havoc Pennington committed
348
			(*State->prepared_func) (State->pixbuf, NULL, State->user_data);
349 350

	}
351

352
	if (!(State->Compressed == BI_RGB || State->Compressed == BI_BITFIELDS)) {
353 354 355 356 357 358 359 360
		State->compr.linebuffdone = 0;
		State->compr.linebuffsize = State->Header.width;
		if (State->Type == 8)
			State->compr.linebuffsize *= 2;
		State->compr.linebuff = g_malloc ((State->compr.linebuffsize + 1) / 2);
	}

	State->BufferDone = 0;
361 362
	if (State->Type <= 8) {
		State->read_state = READ_STATE_PALETTE;
363
		State->BufferSize = GUINT32_FROM_LE (* (guint32 *) &BFH[10]) - 14 - State->Header.size;
364 365
	} else if (State->Compressed == BI_RGB) {
		State->read_state = READ_STATE_DATA;
366
		State->BufferSize = State->LineWidth;
367 368 369 370 371 372
	} else if (State->Compressed == BI_BITFIELDS) {
		State->read_state = READ_STATE_BITMASKS;
		State->BufferSize = 12;
	} else
		g_assert_not_reached ();

373 374 375 376 377 378 379 380 381 382 383
	State->buff = g_realloc (State->buff, State->BufferSize);

        return TRUE;
}

static void DecodeColormap (guchar *buff,
			    struct bmp_progressive_state *State,
			    GError **error)
{
	gint i;

384 385
	g_assert (State->read_state == READ_STATE_PALETTE);

386 387 388 389 390 391 392 393 394
	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];
	}

395 396
	State->read_state = READ_STATE_DATA;

397
	State->BufferDone = 0;
398
	if (!(State->Compressed == BI_RGB || State->Compressed == BI_BITFIELDS))
399 400 401
		State->BufferSize = 2;
	else
		State->BufferSize = State->LineWidth;
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

	State->buff = g_realloc (State->buff, State->BufferSize);
}

/* 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 */
static void
decode_bitmasks (struct bmp_progressive_state *State, guchar *buf)
{
	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;
451
	State->buff = g_realloc (State->buff, State->BufferSize);
452 453
}

454
/*
455 456 457 458 459
 * func - called when we have pixmap created (but no image data)
 * user_data - passed as arg 1 to func
 * return context (opaque to user)
 */

460
static gpointer
Federico Mena Quintero's avatar
Federico Mena Quintero committed
461 462
gdk_pixbuf__bmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
				 ModuleUpdatedNotifyFunc updated_func,
Havoc Pennington's avatar
Havoc Pennington committed
463
                                 gpointer user_data,
Havoc Pennington's avatar
Havoc Pennington committed
464
                                 GError **error)
465 466 467 468 469 470 471 472
{
	struct bmp_progressive_state *context;

	context = g_new0(struct bmp_progressive_state, 1);
	context->prepared_func = prepared_func;
	context->updated_func = updated_func;
	context->user_data = user_data;

473 474
	context->read_state = READ_STATE_HEADERS;

475 476 477
	context->BufferSize = 26;
	context->buff = g_malloc(26);
	context->BufferDone = 0;
478
	/* 14 for the BitmapFileHeader, 12 for the BitmapImageHeader */
479

480
	context->Colormap = NULL;
481 482 483 484 485 486

	context->Lines = 0;

	context->Type = 0;

	memset(&context->Header, 0, sizeof(struct headerpair));
487
	memset(&context->compr, 0, sizeof(struct bmp_compression_state));
488 489 490 491 492 493 494 495 496 497 498 499 500


	context->pixbuf = NULL;


	return (gpointer) context;
}

/*
 * context - returned from image_begin_load
 *
 * free context, unref gdk_pixbuf
 */
501
static gboolean gdk_pixbuf__bmp_image_stop_load(gpointer data, GError **error)
502 503 504 505
{
	struct bmp_progressive_state *context =
	    (struct bmp_progressive_state *) data;

506 507
        /* FIXME this thing needs to report errors if
         * we have unused image data
508
         */
509

510
	g_return_val_if_fail(context != NULL, TRUE);
511

512 513
	if (context->compr.linebuff != NULL)
		g_free(context->compr.linebuff);
514

515 516
	if (context->Colormap != NULL)
		g_free(context->Colormap);
517 518

	if (context->pixbuf)
519
		g_object_unref(context->pixbuf);
520

521
	g_free(context->buff);
522
	g_free(context);
523 524

        return TRUE;
525 526 527
}


528 529 530 531 532 533
/*
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)
{
534
	int i;
535 536
	guchar *pixels;
	guchar *src;
537

538 539 540
	if (!context->Header.Negative)
		pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride * (context->Header.height - context->Lines - 1));
541
	else
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
		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;
559

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
		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;
		}
585 586
}

587 588 589 590 591 592
static void OneLine24(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
593
	if (context->Header.Negative == 0)
594 595 596
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
597
	else
598 599 600
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
601
	while (X < context->Header.width) {
602 603 604
		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];
605 606 607 608 609
		X++;
	}

}

610 611
static void OneLine16(struct bmp_progressive_state *context)
{
612 613 614
	int i;
	guchar *pixels;
	guchar *src;
615

616 617 618
	if (!context->Header.Negative)
		pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride * (context->Header.height - context->Lines - 1));
619
	else
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
		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);
		}
667 668
}

669 670 671 672 673 674
static void OneLine8(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
675
	if (context->Header.Negative == 0)
676 677 678
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
679
	else
680 681 682
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
683 684
	while (X < context->Header.width) {
		Pixels[X * 3 + 0] =
685
		    context->Colormap[context->buff[X]][2];
686
		Pixels[X * 3 + 1] =
687
		    context->Colormap[context->buff[X]][1];
688
		Pixels[X * 3 + 2] =
689
		    context->Colormap[context->buff[X]][0];
690 691 692 693
		X++;
	}
}

694 695 696 697 698 699 700
static void OneLine4(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
	if (context->Header.Negative == 0)
701 702 703
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
704
	else
705 706 707
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
708 709 710 711

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

712
		Pix = context->buff[X / 2];
713 714

		Pixels[X * 3 + 0] =
715
		    context->Colormap[Pix >> 4][2];
716
		Pixels[X * 3 + 1] =
717
		    context->Colormap[Pix >> 4][1];
718
		Pixels[X * 3 + 2] =
719
		    context->Colormap[Pix >> 4][0];
720 721 722 723
		X++;
		if (X < context->Header.width) {
			/* Handle the other 4 bit pixel only when there is one */
			Pixels[X * 3 + 0] =
724
			    context->Colormap[Pix & 15][2];
725
			Pixels[X * 3 + 1] =
726
			    context->Colormap[Pix & 15][1];
727
			Pixels[X * 3 + 2] =
728
			    context->Colormap[Pix & 15][0];
729 730 731 732 733 734
			X++;
		}
	}

}

735 736 737 738 739 740
static void OneLine1(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
741
	if (context->Header.Negative == 0)
742 743 744
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
745
	else
746 747 748
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
749
	while (X < context->Header.width) {
750
		gint Bit;
751

752
		Bit = (context->buff[X / 8]) >> (7 - (X & 7));
753
		Bit = Bit & 1;
754 755 756
		Pixels[X * 3 + 0] = context->Colormap[Bit][2];
		Pixels[X * 3 + 1] = context->Colormap[Bit][1];
		Pixels[X * 3 + 2] = context->Colormap[Bit][0];
757 758 759 760 761 762 763
		X++;
	}
}


static void OneLine(struct bmp_progressive_state *context)
{
764
	context->BufferDone = 0;
765 766 767
	if (context->Lines >= context->Header.height)
		return;

768 769
	if (context->Type == 32)
		OneLine32(context);
770
	else if (context->Type == 24)
771
		OneLine24(context);
772
	else if (context->Type == 16)
773
		OneLine16(context);
774
	else if (context->Type == 8)
775
		OneLine8(context);
776
	else if (context->Type == 4)
777
		OneLine4(context);
778
	else if (context->Type == 1)
779
		OneLine1(context);
780 781
	else
		g_assert_not_reached ();
782 783 784 785 786 787 788 789

	context->Lines++;

	if (context->updated_func != NULL) {
		(*context->updated_func) (context->pixbuf,
					  0,
					  context->Lines,
					  context->Header.width,
790
					  1,
791
					  context->user_data);
792 793 794 795

	}
}

796
static void
797
DoCompressed(struct bmp_progressive_state *context)
798
{
799
	gint count, pos;
800 801
	switch (context->compr.phase) {
	case 0:		/* Neutral state */
802 803 804 805 806 807 808 809 810 811 812 813 814 815 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
		if (context->buff[0] != 0) {	/* run count */
			context->compr.RunCount = context->buff[0];
			if (context->Type == 8)
				context->compr.RunCount *= 2;
			while (context->compr.RunCount > 0) {
				if (context->compr.linebuffdone & 1) {
					guchar *ptr = context->compr.linebuff +
					    context->compr.linebuffdone / 2;

					*ptr = (*ptr & 0xF0) | (context->buff[1] >> 4);
					context->buff[1] = (context->buff[1] << 4) |
							   (context->buff[1] >> 4);
					context->compr.linebuffdone++;
					context->compr.RunCount--;
				}

				if (context->compr.RunCount) {
					count = context->compr.linebuffsize -
					    context->compr.linebuffdone;
					if (count > context->compr.RunCount)
						count = context->compr.RunCount;

					memset (context->compr.linebuff +
						context->compr.linebuffdone / 2,
						context->buff[1],
						(count + 1) / 2);
					context->compr.RunCount -= count;
					context->compr.linebuffdone += count;
				}
				if (context->compr.linebuffdone == context->compr.linebuffsize) {
					guchar *tmp = context->buff;
					context->buff = context->compr.linebuff;
					OneLine (context);
					context->buff = tmp;

					if (context->compr.linebuffdone & 1)
						context->buff[1] = (context->buff[1] << 4) |
								   (context->buff[1] >> 4);
					context->compr.linebuffdone = 0;
				}
842
			}
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
		} else {	/* Escape */
			if (context->buff[1] == 0) {	/* End of line */
				if (context->compr.linebuffdone) {
					guchar *tmp = context->buff;
					context->buff = context->compr.linebuff;
					OneLine (context);
					context->buff = tmp;

					context->compr.linebuffdone = 0;
				}
			} else if (context->buff[1] == 1) {	/* End of image */
				if (context->compr.linebuffdone) {
					guchar *tmp = context->buff;
					context->buff = context->compr.linebuff;
					OneLine (context);
					context->buff = tmp;
				}

				context->compr.phase = 2;
			} else if (context->buff[1] == 2)	/* Cursor displacement */
				;	/* not implemented */
			else {
				context->compr.phase = 1;
				context->compr.RunCount = context->buff[1];
				if (context->Type == 8)
					context->compr.RunCount *= 2;
				context->BufferSize = (context->compr.RunCount + 3) / 4 * 2;
				context->buff = g_realloc (context->buff, context->BufferSize);
871 872
			}
		}
873
		context->BufferDone = 0;
874
		break;
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
	case 1:
		pos = 0;
		while (pos < context->compr.RunCount) {
			count = context->compr.linebuffsize - context->compr.linebuffdone;
			if (count > context->compr.RunCount)
				count = context->compr.RunCount;

			if ((context->compr.linebuffdone & 1) || (pos & 1)) {
				gint i, newval;
				guchar *ptr;
				for (i = 0; i < count; i++) {
					ptr = context->compr.linebuff + (i +
					      context->compr.linebuffdone) / 2;
					newval = *(context->buff + (pos + i) / 2) & (0xf0 >> (((pos + i) % 2) * 4));
					if (((pos + i) % 2) ^ ((context->compr.linebuffdone + i) % 2)) {
						if ((pos + i) % 2)
							newval <<= 4;
						else
							newval >>= 4;
					}
					*ptr = (*ptr & (0xf << (((i + context->compr.linebuffdone) % 2) * 4))) | newval;
				}
			} else {
				memmove (context->compr.linebuff +
					 context->compr.linebuffdone / 2,
					 context->buff + pos / 2,
					 (count + 1) / 2);
902
			}
903 904 905 906 907 908 909 910 911
			pos += count;
			context->compr.linebuffdone += count;
			if (context->compr.linebuffdone == context->compr.linebuffsize) {
				guchar *tmp = context->buff;
				context->buff = context->compr.linebuff;
				OneLine (context);
				context->buff = tmp;

				context->compr.linebuffdone = 0;
912 913 914
			}
		}
		context->compr.phase = 0;
915 916 917 918 919 920
		context->BufferSize = 2;
		context->buff = g_realloc (context->buff, context->BufferSize);
		context->BufferDone = 0;
		break;
	case 2:
		context->BufferDone = 0;
921
		break;
922 923 924
	}
}

925 926 927 928 929 930 931
/*
 * context - from image_begin_load
 * buf - new image data
 * size - length of new image data
 *
 * append image data onto inrecrementally built output image
 */
932 933 934 935 936
static gboolean
gdk_pixbuf__bmp_image_load_increment(gpointer data,
                                     const guchar * buf,
                                     guint size,
                                     GError **error)
937 938 939 940 941 942
{
	struct bmp_progressive_state *context =
	    (struct bmp_progressive_state *) data;

	gint BytesToCopy;

943 944 945 946 947
	if (context->read_state == READ_STATE_DONE)
		return TRUE;
	else if (context->read_state == READ_STATE_ERROR)
		return FALSE;

948
	while (size > 0) {