io-bmp.c 22.5 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
/* 

These structures are actually dummies. These are according to
40 41 42
the "Windows API reference guide volume II" as written by 
Borland International, but GCC fiddles with the alignment of 
the internal members, so these aren't actually usable.
43 44 45

*/

46
struct BitmapFileHeader {
47 48 49 50
	gushort bfType;
	guint bfSize;
	guint reserverd;
	guint bfOffbits;
51 52 53
};

struct BitmapInfoHeader {
54 55 56 57 58 59 60 61 62 63 64
	guint biSize;
	guint biWidth;
	guint biHeight;
	gushort biPlanes;
	gushort biBitCount;
	guint biCompression;
	guint biSizeImage;
	guint biXPelsPerMeter;
	guint biYPelsPerMeter;
	guint biClrUsed;
	guint biClrImportant;
65 66
};

67 68 69 70 71 72 73 74 75
/* biCompression values */

#define BI_RGB 0
#define BI_RLE8 1
#define BI_RLE4 2
#define BI_BITFIELDS 3
#define BI_JPEG 4
#define BI_PNG 5

76 77 78 79 80 81 82
/* 

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

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

119
struct headerpair {
120 121 122
	guint32 size;
	gint32 width;
	gint32 height;
123 124 125 126
	guint depth;
	guint Negative;		/* Negative = 1 -> top down BMP,  
				   Negative = 0 -> bottom up BMP */
};
127

128 129
/* Data needed for the "state" during decompression */
struct bmp_compression_state {
130
	gint phase; 
131
	gint RunCount;
132 133 134 135

	guchar *linebuff;
	gint linebuffsize;	/* these two counts in nibbles */
	gint linebuffdone;
136
};
137 138 139 140 141 142 143 144

/* Progressive loading */

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

145 146
	guint LineWidth;
	guint Lines;		/* # of finished lines */
147

148 149 150 151 152
	guchar *buff;
	gint BufferSize;
	gint BufferDone;

	guchar (*Colormap)[3];
153 154

	gint Type;		/*  
155
				   32 = RGB + alpha
156
				   24 = RGB
157
				   16 = RGB
158 159
				   4  = 4 bpp colormapped
				   8  = 8 bpp colormapped
160 161
				   1  = 1 bit bitonal 
				 */
162 163
	gint Compressed;
	struct bmp_compression_state compr;
164 165 166 167 168 169 170 171


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


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

172
static gpointer
Federico Mena Quintero's avatar
Federico Mena Quintero committed
173 174
gdk_pixbuf__bmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
				 ModuleUpdatedNotifyFunc updated_func,
Havoc Pennington's avatar
Havoc Pennington committed
175
                                 gpointer user_data,
Havoc Pennington's avatar
Havoc Pennington committed
176
                                 GError **error);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
177

178 179 180 181 182
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);
183

Mark Crichton's avatar
Mark Crichton committed
184 185


186 187
/* Shared library entry point --> This should be removed when
   generic_image_load enters gdk-pixbuf-io. */
188
static GdkPixbuf *gdk_pixbuf__bmp_image_load(FILE * f, GError **error)
Mark Crichton's avatar
Mark Crichton committed
189
{
190
	guchar membuf[4096];
191 192
	size_t length;
	struct bmp_progressive_state *State;
193

194
	GdkPixbuf *pb;
195

196
	State =
Havoc Pennington's avatar
Havoc Pennington committed
197
	    gdk_pixbuf__bmp_image_begin_load(NULL, NULL, NULL,
Havoc Pennington's avatar
Havoc Pennington committed
198
                                             error);
199

Havoc Pennington's avatar
Havoc Pennington committed
200 201 202
        if (State == NULL)
          return NULL;
        
203 204
	while (feof(f) == 0) {
		length = fread(membuf, 1, 4096, f);
205
		if (length > 0)
Havoc Pennington's avatar
Havoc Pennington committed
206 207 208 209
                  if (!gdk_pixbuf__bmp_image_load_increment(State,
                                                            membuf,
                                                            length,
                                                            error)) {
210
                          gdk_pixbuf__bmp_image_stop_load (State, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
211 212
                          return NULL;
                  }
213 214

	}
215 216 217 218 219
	if (State->pixbuf != NULL)
		gdk_pixbuf_ref(State->pixbuf);

	pb = State->pixbuf;

220
	gdk_pixbuf__bmp_image_stop_load(State, NULL);
221
	return pb;
222 223
}

224 225 226
static gboolean DecodeHeader(unsigned char *BFH, unsigned char *BIH,
                             struct bmp_progressive_state *State,
                             GError **error)
227
{
228 229
        /* FIXME this is totally unrobust against bogus image data. */
        
230 231 232 233 234 235
	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;
	}

236 237 238 239
#if DUMPBIH
	DumpBIH(BIH);
#endif

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	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]);
		State->Compressed = 0;
	} else {
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has unsupported header size"));
		return FALSE;
	}
258 259

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

261
	/* Negative heights indicates bottom-down pixelorder */
262 263 264 265 266 267 268 269
	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;
	}
270

271 272 273 274 275 276 277 278 279 280 281
	if (State->Header.width == 0 || State->Header.height == 0 ||
	    (State->Compressed == BI_RLE4 && State->Type != 4)    ||
	    (State->Compressed == BI_RLE8 && State->Type != 8)	  ||
	    State->Compressed > BI_RLE4) {
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has bogus header data"));
		return FALSE;
	}

282 283
	if (State->Type == 32)
		State->LineWidth = State->Header.width * 4;
284
	else if (State->Type == 24)
285
		State->LineWidth = State->Header.width * 3;
286 287 288
	else if (State->Type == 16)
		State->LineWidth = State->Header.width * 2;
	else if (State->Type == 8)
289
		State->LineWidth = State->Header.width * 1;
290
	else if (State->Type == 4)
291
		State->LineWidth = (State->Header.width + 1) / 2;
292
	else if (State->Type == 1) {
293 294 295
		State->LineWidth = State->Header.width / 8;
		if ((State->Header.width & 7) != 0)
			State->LineWidth++;
296 297 298 299 300 301
	} else {
		g_set_error (error,
			     GDK_PIXBUF_ERROR,
			     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
			     _("BMP image has bogus header data"));
		return FALSE;
302
	}
303

304
	/* Pad to a 32 bit boundary */
305
	if (((State->LineWidth % 4) > 0) && (State->Compressed == BI_RGB))
306 307
		State->LineWidth = (State->LineWidth / 4) * 4 + 4;

308 309

	if (State->pixbuf == NULL) {
310 311
		if (State->Type == 32)
			State->pixbuf =
312
			    gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
313 314 315 316
					   (gint) State->Header.width,
					   (gint) State->Header.height);
		else
			State->pixbuf =
317
			    gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
318 319
					   (gint) State->Header.width,
					   (gint) State->Header.height);
320

321 322 323 324 325
                if (State->pixbuf == NULL) {
                        g_set_error (error,
                                     GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
                                     _("Not enough memory to load bitmap image"));
326
                        return FALSE;
327 328
                }
                
329 330
		if (State->prepared_func != NULL)
			/* Notify the client that we are ready to go */
Havoc Pennington's avatar
Havoc Pennington committed
331
			(*State->prepared_func) (State->pixbuf, NULL, State->user_data);
332 333

	}
334

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	if (State->Compressed != BI_RGB) {
		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;
	if (State->Type <= 8)
		State->BufferSize = GUINT32_FROM_LE (* (guint32 *) &BFH[10]) - 14 - State->Header.size;
	else if (State->Compressed != BI_RGB)
		State->BufferSize = 2;
	else
		State->BufferSize = State->LineWidth;
	State->buff = g_realloc (State->buff, State->BufferSize);

        return TRUE;
}

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

	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];
	}

	State->BufferDone = 0;
	if (State->Compressed != BI_RGB)
		State->BufferSize = 2;
	else
		State->BufferSize = State->LineWidth;
	State->buff = g_realloc (State->buff, State->BufferSize);
376 377 378 379 380 381 382 383
}

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

384
static gpointer
Federico Mena Quintero's avatar
Federico Mena Quintero committed
385 386
gdk_pixbuf__bmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
				 ModuleUpdatedNotifyFunc updated_func,
Havoc Pennington's avatar
Havoc Pennington committed
387
                                 gpointer user_data,
Havoc Pennington's avatar
Havoc Pennington committed
388
                                 GError **error)
389 390 391 392 393 394 395 396
{
	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;

397 398 399 400
	context->BufferSize = 26;
	context->buff = g_malloc(26);
	context->BufferDone = 0;
	/* 14 for the BitmapFileHeader, 12 for the BitmapImageHeader */ 
401

402
	context->Colormap = NULL;
403 404 405 406 407 408

	context->Lines = 0;

	context->Type = 0;

	memset(&context->Header, 0, sizeof(struct headerpair));
409
	memset(&context->compr, 0, sizeof(struct bmp_compression_state));
410 411 412 413 414 415 416 417 418 419 420 421 422


	context->pixbuf = NULL;


	return (gpointer) context;
}

/*
 * context - returned from image_begin_load
 *
 * free context, unref gdk_pixbuf
 */
423
static gboolean gdk_pixbuf__bmp_image_stop_load(gpointer data, GError **error)
424 425 426 427
{
	struct bmp_progressive_state *context =
	    (struct bmp_progressive_state *) data;

428 429 430
        /* FIXME this thing needs to report errors if
         * we have unused image data
         */        
431

432
	g_return_val_if_fail(context != NULL, TRUE);
433

434 435
	if (context->compr.linebuff != NULL)
		g_free(context->compr.linebuff);
436

437 438
	if (context->Colormap != NULL)
		g_free(context->Colormap);
439 440 441 442

	if (context->pixbuf)
		gdk_pixbuf_unref(context->pixbuf);

443
	g_free(context->buff);
444
	g_free(context);
445 446

        return TRUE;
447 448 449
}


450 451 452 453 454 455 456 457 458 459 460
/*
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)
{
	gint X;
	guchar *Pixels;

	X = 0;
	if (context->Header.Negative == 0)
461 462 463
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
464
	else
465 466 467
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
468
	while (X < context->Header.width) {
469 470 471 472
		Pixels[X * 4 + 0] = context->buff[X * 4 + 2];
		Pixels[X * 4 + 1] = context->buff[X * 4 + 1];
		Pixels[X * 4 + 2] = context->buff[X * 4 + 0];
		Pixels[X * 4 + 3] = context->buff[X * 4 + 3];
473 474 475 476 477
		X++;
	}

}

478 479 480 481 482 483
static void OneLine24(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
484
	if (context->Header.Negative == 0)
485 486 487
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
488
	else
489 490 491
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
492
	while (X < context->Header.width) {
493 494 495
		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];
496 497 498 499 500
		X++;
	}

}

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
static void OneLine16(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
	if (context->Header.Negative == 0)
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
	else
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
	while (X < context->Header.width) {
		Pixels[X * 3 + 0] = (GUINT16_FROM_LE (* (guint16 *) &context->buff[X * 2]) & 0x7C00) >> 7;
		Pixels[X * 3 + 1] = (GUINT16_FROM_LE (* (guint16 *) &context->buff[X * 2]) & 0x03E0) >> 2;
		Pixels[X * 3 + 2] = (GUINT16_FROM_LE (* (guint16 *) &context->buff[X * 2]) & 0x001F) << 3;
		X++;
	}
}

523 524 525 526 527 528
static void OneLine8(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
529
	if (context->Header.Negative == 0)
530 531 532
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
533
	else
534 535 536
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
537 538
	while (X < context->Header.width) {
		Pixels[X * 3 + 0] =
539
		    context->Colormap[context->buff[X]][2];
540
		Pixels[X * 3 + 1] =
541
		    context->Colormap[context->buff[X]][1];
542
		Pixels[X * 3 + 2] =
543
		    context->Colormap[context->buff[X]][0];
544 545 546 547
		X++;
	}
}

548 549 550 551 552 553 554
static void OneLine4(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
	if (context->Header.Negative == 0)
555 556 557
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
558
	else
559 560 561
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
562 563 564 565

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

566
		Pix = context->buff[X / 2];
567 568

		Pixels[X * 3 + 0] =
569
		    context->Colormap[Pix >> 4][2];
570
		Pixels[X * 3 + 1] =
571
		    context->Colormap[Pix >> 4][1];
572
		Pixels[X * 3 + 2] =
573
		    context->Colormap[Pix >> 4][0];
574 575 576 577
		X++;
		if (X < context->Header.width) {
			/* Handle the other 4 bit pixel only when there is one */
			Pixels[X * 3 + 0] =
578
			    context->Colormap[Pix & 15][2];
579
			Pixels[X * 3 + 1] =
580
			    context->Colormap[Pix & 15][1];
581
			Pixels[X * 3 + 2] =
582
			    context->Colormap[Pix & 15][0];
583 584 585 586 587 588
			X++;
		}
	}

}

589 590 591 592 593 594
static void OneLine1(struct bmp_progressive_state *context)
{
	gint X;
	guchar *Pixels;

	X = 0;
595
	if (context->Header.Negative == 0)
596 597 598
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  (context->Header.height - context->Lines - 1));
599
	else
600 601 602
		Pixels = (context->pixbuf->pixels +
			  context->pixbuf->rowstride *
			  context->Lines);
603
	while (X < context->Header.width) {
604
		gint Bit;
605

606
		Bit = (context->buff[X / 8]) >> (7 - (X & 7));
607
		Bit = Bit & 1;
608 609 610
		Pixels[X * 3 + 0] = context->Colormap[Bit][2];
		Pixels[X * 3 + 1] = context->Colormap[Bit][1];
		Pixels[X * 3 + 2] = context->Colormap[Bit][0];
611 612 613 614 615 616 617
		X++;
	}
}


static void OneLine(struct bmp_progressive_state *context)
{
618
	context->BufferDone = 0;
619 620 621
	if (context->Lines >= context->Header.height)
		return;

622 623
	if (context->Type == 32)
		OneLine32(context);
624 625
	if (context->Type == 24)
		OneLine24(context);
626 627
	if (context->Type == 16)
		OneLine16(context);
628 629
	if (context->Type == 8)
		OneLine8(context);
630 631
	if (context->Type == 4)
		OneLine4(context);
632 633 634 635 636 637 638 639 640 641
	if (context->Type == 1)
		OneLine1(context);

	context->Lines++;

	if (context->updated_func != NULL) {
		(*context->updated_func) (context->pixbuf,
					  0,
					  context->Lines,
					  context->Header.width,
642
					  1,
643
					  context->user_data);
644 645 646 647

	}
}

648
static void
649
DoCompressed(struct bmp_progressive_state *context)
650
{
651
	gint count, pos;
652 653
	switch (context->compr.phase) {
	case 0:		/* Neutral state */
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
		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;
				}
694
			}
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
		} 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);
723 724
			}
		}
725
		context->BufferDone = 0;
726
		break;
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
	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);
754
			}
755 756 757 758 759 760 761 762 763
			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;
764 765 766
			}
		}
		context->compr.phase = 0;
767 768 769 770 771 772
		context->BufferSize = 2;
		context->buff = g_realloc (context->buff, context->BufferSize);
		context->BufferDone = 0;
		break;
	case 2:
		context->BufferDone = 0;
773
		break;
774 775 776
	}
}

777 778 779 780 781 782 783
/*
 * context - from image_begin_load
 * buf - new image data
 * size - length of new image data
 *
 * append image data onto inrecrementally built output image
 */
784 785 786 787 788
static gboolean
gdk_pixbuf__bmp_image_load_increment(gpointer data,
                                     const guchar * buf,
                                     guint size,
                                     GError **error)
789 790 791 792 793 794 795
{
	struct bmp_progressive_state *context =
	    (struct bmp_progressive_state *) data;

	gint BytesToCopy;

	while (size > 0) {
796
		if (context->BufferDone < context->BufferSize) {	/* We still 
797 798
									   have headerbytes to do */
			BytesToCopy =
799
			    context->BufferSize - context->BufferDone;
800 801 802
			if (BytesToCopy > size)
				BytesToCopy = size;

803
			memmove(context->buff + context->BufferDone,
804
				buf, BytesToCopy);
805 806 807

			size -= BytesToCopy;
			buf += BytesToCopy;
808
			context->BufferDone += BytesToCopy;
809

810 811
			if (context->BufferDone != context->BufferSize)
				break;
812 813
		}

814 815 816 817 818 819 820 821 822 823 824 825 826
		if (!context->Header.size) {
			if (!DecodeHeader (context->buff,
					   context->buff + 14, context,
					   error))
				return FALSE;
		}
		else if (context->Type <= 8 && context->Colormap == NULL)
			DecodeColormap (context->buff, context, error);
		else if (context->Compressed != BI_RGB)
			DoCompressed(context);
		else
			/* Uncompressed pixeldata */
			OneLine(context);
827
	}
Mark Crichton's avatar
Mark Crichton committed
828

829
	return TRUE;
Mark Crichton's avatar
Mark Crichton committed
830
}
831 832 833 834 835 836 837 838 839

void
gdk_pixbuf__bmp_fill_vtable (GdkPixbufModule *module)
{
  module->load = gdk_pixbuf__bmp_image_load;
  module->begin_load = gdk_pixbuf__bmp_image_begin_load;
  module->stop_load = gdk_pixbuf__bmp_image_stop_load;
  module->load_increment = gdk_pixbuf__bmp_image_load_increment;
}