aboutsummaryrefslogtreecommitdiffstats
path: root/src/gdisp/image_bmp.c
blob: e54ad5e4475d49b216a1c59617f30b51b928ac5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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
585
586
587
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
614
615
616
617
618
619
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
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
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
723
724
725
726
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
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
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
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
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://chibios-gfx.com/license.html
 */

/**
 * @file    src/gdisp/image_bmp.c
 * @brief   GDISP native image code.
 *
 * @defgroup Image Image
 * @ingroup GDISP
 */
#include "gfx.h"

#if GFX_USE_GDISP && GDISP_NEED_IMAGE && GDISP_NEED_IMAGE_BMP

#ifndef GDISP_NEED_IMAGE_BMP_1
	#define GDISP_NEED_IMAGE_BMP_1		TRUE
#endif
#ifndef GDISP_NEED_IMAGE_BMP_4
	#define GDISP_NEED_IMAGE_BMP_4		TRUE
#endif
#ifndef GDISP_NEED_IMAGE_BMP_4_RLE
	#define GDISP_NEED_IMAGE_BMP_4_RLE	TRUE
#endif
#ifndef GDISP_NEED_IMAGE_BMP_8
	#define GDISP_NEED_IMAGE_BMP_8		TRUE
#endif
#ifndef GDISP_NEED_IMAGE_BMP_8_RLE
	#define GDISP_NEED_IMAGE_BMP_8_RLE	TRUE
#endif
#ifndef GDISP_NEED_IMAGE_BMP_16
	#define GDISP_NEED_IMAGE_BMP_16		TRUE
#endif
#ifndef GDISP_NEED_IMAGE_BMP_24
	#define GDISP_NEED_IMAGE_BMP_24		TRUE
#endif
#ifndef GDISP_NEED_IMAGE_BMP_32
	#define GDISP_NEED_IMAGE_BMP_32		TRUE
#endif

/**
 * Helper Routines Needed
 */
void *gdispImageAlloc(gdispImage *img, size_t sz);
void gdispImageFree(gdispImage *img, void *ptr, size_t sz);

/**
 * How big a pixel array to allocate for blitting (in pixels)
 * Bigger is faster but uses more RAM.
 * This must be greater than 40 bytes and 32 pixels as we read our headers into this space as well
 */
#define BLIT_BUFFER_SIZE	32

/*
 * Determining endianness as at compile time is not guaranteed or compiler portable.
 * We use the best test we can. If we can't guarantee little endianness we do things the
 * hard way.
 */
#define GUARANTEED_LITTLE_ENDIAN	(!defined(SAFE_ENDIAN) && !defined(SAFE_ALIGNMENT) && (\
		(defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) \
		|| defined(__LITTLE_ENDIAN__) \
		|| defined(__LITTLE_ENDIAN) \
		|| defined(_LITTLE_ENDIAN) \
/*		|| (1 == *(unsigned char *)&(const int){1})*/ \
		))


/* This is a runtime test */
static const uint8_t	dwordOrder[4]	= { 1, 2, 3, 4 };

#define isWordLittleEndian()	(*(uint16_t *)&dwordOrder == 0x0201)
#define isDWordLittleEndian()	(*(uint32_t *)&dwordOrder == 0x04030201)

#if GUARANTEED_LITTLE_ENDIAN
	/* These are fast routines for guaranteed little endian machines */
	#define CONVERT_FROM_WORD_LE(w)
	#define CONVERT_FROM_DWORD_LE(dw)
#else
	/* These are slower routines for when little endianness cannot be guaranteed at compile time */
	#define CONVERT_FROM_WORD_LE(w)		{ if (!isWordLittleEndian()) w = ((((uint16_t)(w))>>8)|(((uint16_t)(w))<<8)); }
	#define CONVERT_FROM_DWORD_LE(dw)	{ if (!isDWordLittleEndian()) dw = (((uint32_t)(((const uint8_t *)(&dw))[0]))|(((uint32_t)(((const uint8_t *)(&dw))[1]))<<8)|(((uint32_t)(((const uint8_t *)(&dw))[2]))<<16)|(((uint32_t)(((const uint8_t *)(&dw))[3]))<<24)); }
#endif

typedef struct gdispImagePrivate {
	uint8_t		bmpflags;
		#define BMP_V2				0x01		// Version 2 (old) header format
		#define BMP_V4				0x02		// Version 4 (alpha support) header format
		#define BMP_PALETTE			0x04		// Uses a palette
		#define	BMP_COMP_RLE		0x08		// Uses RLE compression
		#define	BMP_COMP_MASK		0x10		// Uses mask & shift decoding
		#define BMP_RLE_ENC			0x20		// Currently in RLE encoded run
		#define BMP_RLE_ABS			0x40		// Currently in RLE absolute run
		#define BMP_TOP_TO_BOTTOM	0x80		// Decodes bottom to top line
	uint8_t		bitsperpixel;
#if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_4_RLE || GDISP_NEED_IMAGE_BMP_8 || GDISP_NEED_IMAGE_BMP_8_RLE
	uint16_t	palsize;
	pixel_t		*palette;
#endif
#if GDISP_NEED_IMAGE_BMP_4_RLE || GDISP_NEED_IMAGE_BMP_8_RLE
	uint16_t	rlerun;
	uint8_t		rlecode;
#endif
#if GDISP_NEED_IMAGE_BMP_16 || GDISP_NEED_IMAGE_BMP_32
	int8_t		shiftred;
	int8_t		shiftgreen;
	int8_t		shiftblue;
	int8_t		shiftalpha;
	uint32_t	maskred;
	uint32_t	maskgreen;
	uint32_t	maskblue;
	uint32_t	maskalpha;
#endif
	size_t		frame0pos;
	pixel_t		*frame0cache;
	pixel_t		buf[BLIT_BUFFER_SIZE];
	} gdispImagePrivate;

gdispImageError gdispImageOpen_BMP(gdispImage *img) {
	gdispImagePrivate *priv;
	uint8_t		hdr[2];
	uint16_t	aword;
	uint32_t	adword;
	uint32_t	offsetColorTable;

	/* Read the file identifier */
	if (img->io.fns->read(&img->io, hdr, 2) != 2)
		return GDISP_IMAGE_ERR_BADFORMAT;		// It can't be us

	/* Process the BITMAPFILEHEADER structure */

	/**
	 * We only accept Windows V2+ bitmaps.
	 *  - we don't support OS/2 bitmaps, icons, pointers, or Windows V1 bitmaps.
	 */
	if (hdr[0] != 'B' || hdr[1] != 'M')
		return GDISP_IMAGE_ERR_BADFORMAT;		// It can't be us

	/* We know we are a BMP format image */
	img->flags = 0;

	/* Allocate our private area */
	if (!(img->priv = (gdispImagePrivate *)gdispImageAlloc(img, sizeof(gdispImagePrivate))))
		return GDISP_IMAGE_ERR_NOMEMORY;

	/* Initialise the essential bits in the private area */
	priv = img->priv;
	priv->frame0cache = 0;
	priv->bmpflags = 0;
#if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_4_RLE || GDISP_NEED_IMAGE_BMP_8 || GDISP_NEED_IMAGE_BMP_8_RLE
	priv->palette = 0;
#endif

	/* Skip the size field and the 2 reserved fields */
	if (img->io.fns->read(&img->io, priv->buf, 8) != 8)
		goto baddatacleanup;

	/* Get the offset to the bitmap data */
	if (img->io.fns->read(&img->io, &priv->frame0pos, 4) != 4)
		goto baddatacleanup;
	CONVERT_FROM_DWORD_LE(priv->frame0pos);

	/* Process the BITMAPCOREHEADER structure */

	/* Get the offset to the colour data */
	if (img->io.fns->read(&img->io, &offsetColorTable, 4) != 4)
		goto baddatacleanup;
	CONVERT_FROM_DWORD_LE(offsetColorTable);
	offsetColorTable += 14;						// Add the size of the BITMAPFILEHEADER

	// Detect our bitmap version
	if (offsetColorTable == 12+14) {
		img->priv->bmpflags |= BMP_V2;

		// Read the header
		if (img->io.fns->read(&img->io, priv->buf, 12-4) != 12-4)
			goto baddatacleanup;
		// Get the width
		img->width = *(uint16_t *)(((uint8_t *)priv->buf)+0);
		CONVERT_FROM_WORD_LE(img->width);
		// Get the height
		img->height = *(uint16_t *)(((uint8_t *)priv->buf)+2);
		CONVERT_FROM_WORD_LE(img->height);
		if (img->height < 0) {
			img->priv->bmpflags |= BMP_TOP_TO_BOTTOM;
			img->height = -img->height;
		}
		// Get the planes
		aword = *(uint16_t *)(((uint8_t *)priv->buf)+4);
		CONVERT_FROM_WORD_LE(aword);
		if (aword != 1)
			goto unsupportedcleanup;
		// Get the bits per pixel
		aword = *(uint16_t *)(((uint8_t *)priv->buf)+6);
		CONVERT_FROM_WORD_LE(aword);
		switch(aword) {
#if GDISP_NEED_IMAGE_BMP_1
		case 1:
#endif
#if GDISP_NEED_IMAGE_BMP_4
		case 4:
#endif
#if GDISP_NEED_IMAGE_BMP_8
		case 8:
#endif
#if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_8
			priv->bmpflags |= BMP_PALETTE;
			priv->palsize = 1<<aword;
			break;
#endif
#if GDISP_NEED_IMAGE_BMP_24
		case 24:
			break;
#endif
		default:
			goto unsupportedcleanup;
		}
		priv->bitsperpixel = aword;

	} else if (offsetColorTable >= 40+14) {
		if (offsetColorTable > 40+14)
			priv->bmpflags |= BMP_V4;

		// Read the header
		if (img->io.fns->read(&img->io, priv->buf, 40-4) != 40-4)
			goto baddatacleanup;
		// Get the width
		adword = *(uint32_t *)(((uint8_t *)priv->buf)+0);
		CONVERT_FROM_DWORD_LE(adword);
		if (adword > 32768)				// This also picks up negative values
			goto unsupportedcleanup;
		img->width = adword;
		// Get the height
		adword = *(uint32_t *)(((uint8_t *)priv->buf)+4);
		CONVERT_FROM_DWORD_LE(adword);
		if ((int32_t)adword < 0) {		// Negative test
			priv->bmpflags |= BMP_TOP_TO_BOTTOM;
			adword = -adword;
		}
		if (adword > 32768)
			goto unsupportedcleanup;
		img->height = adword;
		// Get the planes
		aword = *(uint16_t *)(((uint8_t *)priv->buf)+8);
		CONVERT_FROM_WORD_LE(aword);
		if (aword != 1)
			goto unsupportedcleanup;
		// Get the bits per pixel
		aword = *(uint16_t *)(((uint8_t *)priv->buf)+10);
		CONVERT_FROM_WORD_LE(aword);
		switch(aword) {
#if GDISP_NEED_IMAGE_BMP_1
		case 1:
#endif
#if GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_4_RLE
		case 4:
#endif
#if GDISP_NEED_IMAGE_BMP_8 || GDISP_NEED_IMAGE_BMP_8_RLE
		case 8:
#endif
#if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_4_RLE || GDISP_NEED_IMAGE_BMP_8 || GDISP_NEED_IMAGE_BMP_8_RLE
			priv->bmpflags |= BMP_PALETTE;
			priv->palsize = 1<<aword;
			break;
#endif
#if GDISP_NEED_IMAGE_BMP_16
		case 16:
#endif
#if GDISP_NEED_IMAGE_BMP_24
		case 24:
#endif
#if GDISP_NEED_IMAGE_BMP_32
		case 32:
#endif
#if GDISP_NEED_IMAGE_BMP_16 || GDISP_NEED_IMAGE_BMP_24 || GDISP_NEED_IMAGE_BMP_32
			break;
#endif
		default:
			goto unsupportedcleanup;
		}
		priv->bitsperpixel = aword;
		// Get the compression
		adword = *(uint32_t *)(((uint8_t *)priv->buf)+12);
		CONVERT_FROM_DWORD_LE(adword);
		switch(adword) {
		case 0:					// BI_RGB - uncompressed
			break;
#if GDISP_NEED_IMAGE_BMP_8_RLE
		case 1:					// BI_RLE8 compression
			if (priv->bitsperpixel != 8)
				goto unsupportedcleanup;
			priv->bmpflags |= BMP_COMP_RLE;
			break;
#endif
#if GDISP_NEED_IMAGE_BMP_4_RLE
		case 2:					// BI_RLE4 compression
			if (priv->bitsperpixel != 4)
				goto unsupportedcleanup;
			priv->bmpflags |= BMP_COMP_RLE;
			break;
#endif
#if GDISP_NEED_IMAGE_BMP_16 || GDISP_NEED_IMAGE_BMP_32
		case 3:					// BI_BITFIELDS decoding
			if (priv->bitsperpixel < 16 || priv->bitsperpixel == 24)
				goto unsupportedcleanup;
			priv->bmpflags |= BMP_COMP_MASK;
			if (priv->bmpflags & BMP_V4)		// V4 stored the masks in the header
				offsetColorTable = 40+14;
			break;
#endif
		default:
			goto unsupportedcleanup;
		}
		priv->bitsperpixel = aword;
#if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_4_RLE || GDISP_NEED_IMAGE_BMP_8 || GDISP_NEED_IMAGE_BMP_8_RLE
		// Get the actual colors used
		adword = *(uint32_t *)(((uint8_t *)priv->buf)+28);
		CONVERT_FROM_DWORD_LE(adword);
		if (adword && adword < priv->palsize)
			priv->palsize = adword;
#endif
	} else
		goto baddatacleanup;

#if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_4_RLE || GDISP_NEED_IMAGE_BMP_8 || GDISP_NEED_IMAGE_BMP_8_RLE
	/* Load the palette tables */
	if (priv->bmpflags & BMP_PALETTE) {
		img->io.fns->seek(&img->io, offsetColorTable);

		if (!(priv->palette = (color_t *)gdispImageAlloc(img, priv->palsize*sizeof(color_t))))
			return GDISP_IMAGE_ERR_NOMEMORY;
		if (priv->bmpflags & BMP_V2) {
			for(aword = 0; aword < priv->palsize; aword++) {
				if (img->io.fns->read(&img->io, &priv->buf, 3) != 3) goto baddatacleanup;
				priv->palette[aword] = RGB2COLOR(((uint8_t *)priv->buf)[2], ((uint8_t *)priv->buf)[1], ((uint8_t *)priv->buf)[0]);
			}
		} else {
			for(aword = 0; aword < priv->palsize; aword++) {
				if (img->io.fns->read(&img->io, &priv->buf, 4) != 4) goto baddatacleanup;
				priv->palette[aword] = RGB2COLOR(((uint8_t *)priv->buf)[2], ((uint8_t *)priv->buf)[1], ((uint8_t *)priv->buf)[0]);
			}
		}

	}
#endif

#if GDISP_NEED_IMAGE_BMP_16 || GDISP_NEED_IMAGE_BMP_32
	/* Load the bit masks */
	if (priv->bmpflags & BMP_COMP_MASK) {
		img->io.fns->seek(&img->io, offsetColorTable);
		if (img->io.fns->read(&img->io, &priv->maskred, 4) != 4) goto baddatacleanup;
		CONVERT_FROM_DWORD_LE(priv->maskred);
		if (img->io.fns->read(&img->io, &priv->maskgreen, 4) != 4) goto baddatacleanup;
		CONVERT_FROM_DWORD_LE(priv->maskgreen);
		if (img->io.fns->read(&img->io, &priv->maskblue, 4) != 4) goto baddatacleanup;
		CONVERT_FROM_DWORD_LE(priv->maskblue);
		if (priv->bmpflags & BMP_V4) {
			if (img->io.fns->read(&img->io, &priv->maskalpha, 4) != 4) goto baddatacleanup;
			CONVERT_FROM_DWORD_LE(priv->maskalpha);
		} else
			priv->maskalpha = 0;
	} else if (priv->bitsperpixel == 16) {
		priv->bmpflags |= BMP_COMP_MASK;
		priv->maskred = 0x7C00;
		priv->maskgreen = 0x03E0;
		priv->maskblue = 0x001F;
		priv->maskalpha = 0;
	} else if (priv->bitsperpixel == 32) {
		priv->bmpflags |= BMP_COMP_MASK;
		priv->maskred = 0x00FF0000;
		priv->maskgreen = 0x0000FF00;
		priv->maskblue = 0x000000FF;
		priv->maskalpha = 0;
	}

	/* We need to adjust the masks and calculate the shift values so the result scales 0 -> 255 */
	if (priv->bmpflags & BMP_COMP_MASK) {
		priv->shiftred = 0;
		priv->shiftgreen = 0;
		priv->shiftblue = 0;
		if (priv->maskred) {
			if (priv->maskred < 256)
				for(adword = priv->maskred;  adword < 128; priv->shiftred--, adword <<= 1);
			else
				for(adword = priv->maskred;  adword > 255; priv->shiftred++, adword >>= 1);
		}
		if (priv->maskgreen) {
			if (priv->maskgreen < 256)
				for(adword = priv->maskgreen;  adword < 128; priv->shiftgreen--, adword <<= 1);
			else
				for(adword = priv->maskgreen;  adword > 255; priv->shiftgreen++, adword >>= 1);
		}
		if (priv->maskblue) {
			if (priv->maskblue < 256)
				for(adword = priv->maskblue;  adword < 128; priv->shiftblue--, adword <<= 1);
			else
				for(adword = priv->maskblue;  adword > 255; priv->shiftblue++, adword >>= 1);
		}
		if (priv->maskalpha) {
			if (priv->maskalpha < 256)
				for(adword = priv->maskalpha;  adword < 128; priv->shiftalpha--, adword <<= 1);
			else
				for(adword = priv->maskalpha;  adword > 255; priv->shiftalpha++, adword >>= 1);
		}
	}
#endif

	return GDISP_IMAGE_ERR_OK;

baddatacleanup:
	gdispImageClose_BMP(img);				// Clean up the private data area
	return GDISP_IMAGE_ERR_BADDATA;			// Oops - something wrong

unsupportedcleanup:
	gdispImageClose_BMP(img);				// Clean up the private data area
	return GDISP_IMAGE_ERR_UNSUPPORTED;		// Not supported
}

void gdispImageClose_BMP(gdispImage *img) {
	if (img->priv) {
#if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_4_RLE || GDISP_NEED_IMAGE_BMP_8 || GDISP_NEED_IMAGE_BMP_8_RLE
		if (img->priv->palette)
			gdispImageFree(img, (void *)img->priv->palette, img->priv->palsize*sizeof(color_t));
#endif
		if (img->priv->frame0cache)
			gdispImageFree(img, (void *)img->priv->frame0cache, img->width*img->height*sizeof(pixel_t));
		gdispImageFree(img, (void *)img->priv, sizeof(gdispImagePrivate));
		img->priv = 0;
	}
	img->io.fns->close(&img->io);
}

static coord_t getPixels(gdispImage *img, coord_t x) {
	gdispImagePrivate *	priv;
	color_t *			pc;
	coord_t				len;

	priv = img->priv;
	pc = priv->buf;
	len = 0;

	switch(priv->bitsperpixel) {
#if GDISP_NEED_IMAGE_BMP_1
	case 1:
		{
		uint8_t		b[4];
		uint8_t		m;

			priv = img->priv;
			pc = priv->buf;
			len = 0;

			while(x < img->width && len <= BLIT_BUFFER_SIZE-32) {
				if (img->io.fns->read(&img->io, &b, 4) != 4)
					return 0;

				for(m=0x80; m; m >>= 1, pc++)
					pc[0] = priv->palette[(m&b[0]) ? 1 : 0];
				for(m=0x80; m; m >>= 1, pc++)
					pc[0] = priv->palette[(m&b[1]) ? 1 : 0];
				for(m=0x80; m; m >>= 1, pc++)
					pc[0] = priv->palette[(m&b[2]) ? 1 : 0];
				for(m=0x80; m; m >>= 1, pc++)
					pc[0] = priv->palette[(m&b[3]) ? 1 : 0];
				len += 32;
				x += 32;
			}
		}
		return len;
#endif

#if GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_4_RLE
	case 4:
	#if GDISP_NEED_IMAGE_BMP_4_RLE
		#if GDISP_NEED_IMAGE_BMP_4
		if (priv->bmpflags & BMP_COMP_RLE)
		#endif
		{
			uint8_t		b[4];

			while(x < img->width) {
				if (priv->bmpflags & BMP_RLE_ENC) {
					while (priv->rlerun && len <= BLIT_BUFFER_SIZE-2 && x < img->width) {
						*pc++ = priv->palette[priv->rlecode >> 4];
						priv->rlerun--;
						len++;
						x++;
						if (priv->rlerun) {
							*pc++ = priv->palette[priv->rlecode & 0x0F];
							priv->rlerun--;
							len++;
							x++;
						}
					}
					if (priv->rlerun)			// Return if we have more run to do
						return len;
				} else if (priv->bmpflags & BMP_RLE_ABS) {
					while (priv->rlerun && len <= BLIT_BUFFER_SIZE-2 && x < img->width) {
						if (img->io.fns->read(&img->io, &b, 1) != 1)
							return 0;
						*pc++ = priv->palette[b[0] >> 4];
						priv->rlerun--;
						len++;
						x++;
						if (priv->rlerun) {
							*pc++ = priv->palette[b[0] & 0x0F];
							priv->rlerun--;
							len++;
							x++;
						}
					}
					if (priv->rlerun)			// Return if we have more run to do
						return len;
					if ((img->io.pos - priv->frame0pos)&1) {	// Make sure we are on a word boundary
						if (img->io.fns->read(&img->io, &b, 1) != 1)
							return 0;
					}
				}

				// We have finished the current run - read a new run
				priv->bmpflags &= ~(BMP_RLE_ENC|BMP_RLE_ABS);

				// There are always at least 2 bytes in an RLE code
				if (img->io.fns->read(&img->io, &b, 2) != 2)
					return 0;

				if (b[0]) {								// Encoded mode
					priv->rlerun = b[0];
					priv->rlecode = b[1];
					priv->bmpflags |= BMP_RLE_ENC;
				} else if (b[1] == 0) {					// End of line
					if (x < img->width) {
						priv->rlerun = img->width - x;
						priv->rlecode = 0;					// Who knows what color this should really be
						priv->bmpflags |= BMP_RLE_ENC;
					}
				} else if (b[1] == 1) {					// End of file
					return len;
				} else if (b[1] == 2) {					// Delta x, y
					// There are always at least 2 bytes in an RLE code
					if (img->io.fns->read(&img->io, &b, 2) != 2)
						return 0;
					priv->rlerun = b[0] + (uint16_t)b[1] * img->width;
					priv->rlecode = 0;					// Who knows what color this should really be
					priv->bmpflags |= BMP_RLE_ENC;
				} else {								// Absolute mode
					priv->rlerun = b[1];
					priv->bmpflags |= BMP_RLE_ABS;
				}
			}
			return len;
		}
	#endif
	#if GDISP_NEED_IMAGE_BMP_4
		{
			uint8_t		b[4];

			while(x < img->width && len <= BLIT_BUFFER_SIZE-8) {
				if (img->io.fns->read(&img->io, &b, 4) != 4)
					return 0;

				*pc++ = priv->palette[b[0] >> 4];
				*pc++ = priv->palette[b[0] & 0x0F];
				*pc++ = priv->palette[b[1] >> 4];
				*pc++ = priv->palette[b[1] & 0x0F];
				*pc++ = priv->palette[b[2] >> 4];
				*pc++ = priv->palette[b[2] & 0x0F];
				*pc++ = priv->palette[b[3] >> 4];
				*pc++ = priv->palette[b[3] & 0x0F];
				len += 8;
				x += 8;
			}
			return len;
		}
	#endif
#endif

#if GDISP_NEED_IMAGE_BMP_8 || GDISP_NEED_IMAGE_BMP_8_RLE
	case 8:
	#if GDISP_NEED_IMAGE_BMP_8_RLE
		#if GDISP_NEED_IMAGE_BMP_8
		if (priv->bmpflags & BMP_COMP_RLE)
		#endif
		{
			uint8_t		b[4];

			while(x < img->width) {
				if (priv->bmpflags & BMP_RLE_ENC) {
					while (priv->rlerun && len < BLIT_BUFFER_SIZE && x < img->width) {
						*pc++ = priv->palette[priv->rlecode];
						priv->rlerun--;
						len++;
						x++;
					}
					if (priv->rlerun)			// Return if we have more run to do
						return len;
				} else if (priv->bmpflags & BMP_RLE_ABS) {
					while (priv->rlerun && len < BLIT_BUFFER_SIZE && x < img->width) {
						if (img->io.fns->read(&img->io, &b, 1) != 1)
							return 0;
						*pc++ = priv->palette[b[0]];
						priv->rlerun--;
						len++;
						x++;
					}
					if (priv->rlerun)			// Return if we have more run to do
						return len;
					if ((img->io.pos - priv->frame0pos)&1) {	// Make sure we are on a word boundary
						if (img->io.fns->read(&img->io, &b, 1) != 1)
							return 0;
					}
				}

				// We have finished the current run - read a new run
				priv->bmpflags &= ~(BMP_RLE_ENC|BMP_RLE_ABS);

				// There are always at least 2 bytes in an RLE code
				if (img->io.fns->read(&img->io, &b, 2) != 2)
					return 0;

				if (b[0]) {								// Encoded mode
					priv->rlerun = b[0];
					priv->rlecode = b[1];
					priv->bmpflags |= BMP_RLE_ENC;
				} else if (b[1] == 0) {					// End of line
					if (x < img->width) {
						priv->rlerun = img->width - x;
						priv->rlecode = 0;					// Who knows what color this should really be
						priv->bmpflags |= BMP_RLE_ENC;
					}
				} else if (b[1] == 1) {					// End of file
					return len;
				} else if (b[1] == 2) {					// Delta x, y
					// There are always at least 2 bytes in an RLE code
					if (img->io.fns->read(&img->io, &b, 2) != 2)
						return GDISP_IMAGE_ERR_BADDATA;
					priv->rlerun = b[0] + (uint16_t)b[1] * img->width;
					priv->rlecode = 0;					// Who knows what color this should really be
					priv->bmpflags |= BMP_RLE_ENC;
				} else {								// Absolute mode
					priv->rlerun = b[1];
					priv->bmpflags |= BMP_RLE_ABS;
				}
			}
			return len;
		}
	#endif
	#if GDISP_NEED_IMAGE_BMP_8
		{
			uint8_t		b[4];

			while(x < img->width && len <= BLIT_BUFFER_SIZE-4) {
				if (img->io.fns->read(&img->io, &b, 4) != 4)
					return 0;

				*pc++ = priv->palette[b[0]];
				*pc++ = priv->palette[b[1]];
				*pc++ = priv->palette[b[2]];
				*pc++ = priv->palette[b[3]];
				len += 4;
				x += 4;
			}
			return len;
		}
	#endif
#endif

#if GDISP_NEED_IMAGE_BMP_16
	case 16:
		{
		uint16_t	w[2];
		color_t		r, g, b;

			while(x < img->width && len <= BLIT_BUFFER_SIZE-2) {
				if (img->io.fns->read(&img->io, &w, 4) != 4)
					return 0;
				CONVERT_FROM_WORD_LE(w[0]);
				CONVERT_FROM_WORD_LE(w[1]);
				if (priv->shiftred < 0)
					r = (color_t)((w[0] & priv->maskred) << -priv->shiftred);
				else
					r = (color_t)((w[0] & priv->maskred) >> priv->shiftred);
				if (priv->shiftgreen < 0)
					g = (color_t)((w[0] & priv->maskgreen) << -priv->shiftgreen);
				else
					g = (color_t)((w[0] & priv->maskgreen) >> priv->shiftgreen);
				if (priv->shiftblue < 0)
					b = (color_t)((w[0] & priv->maskblue) << -priv->shiftblue);
				else
					b = (color_t)((w[0] & priv->maskblue) >> priv->shiftblue);
				/* We don't support alpha yet */
				*pc++ = RGB2COLOR(r, g, b);
				if (priv->shiftred < 0)
					r = (color_t)((w[1] & priv->maskred) << -priv->shiftred);
				else
					r = (color_t)((w[1] & priv->maskred) >> priv->shiftred);
				if (priv->shiftgreen < 0)
					g = (color_t)((w[1] & priv->maskgreen) << -priv->shiftgreen);
				else
					g = (color_t)((w[1] & priv->maskgreen) >> priv->shiftgreen);
				if (priv->shiftblue < 0)
					b = (color_t)((w[1] & priv->maskblue) << -priv->shiftblue);
				else
					b = (uint8_t)((w[1] & priv->maskblue) >> priv->shiftblue);
				/* We don't support alpha yet */
				*pc++ = RGB2COLOR(r, g, b);
				x += 2;
				len += 2;
			}
		}
		return len;
#endif

#if GDISP_NEED_IMAGE_BMP_24
	case 24:
		{
		uint8_t		b[3];

			while(x < img->width && len < BLIT_BUFFER_SIZE) {
				if (img->io.fns->read(&img->io, &b, 3) != 3)
					return 0;
				*pc++ = RGB2COLOR(b[2], b[1], b[0]);
				x++;
				len++;
			}

			if (x >= img->width) {
				// Make sure we have read a multiple of 4 bytes for the line
				if ((x & 3) && img->io.fns->read(&img->io, &b, x & 3) != (x & 3))
					return 0;
			}
		}
		return len;
#endif

#if GDISP_NEED_IMAGE_BMP_32
	case 32:
		{
		uint32_t	dw;
		color_t		r, g, b;

			while(x < img->width && len < BLIT_BUFFER_SIZE) {
				if (img->io.fns->read(&img->io, &dw, 4) != 4)
					return 0;
				CONVERT_FROM_DWORD_LE(dw);
				if (priv->shiftred < 0)
					r = (color_t)((dw & priv->maskred) << -priv->shiftred);
				else
					r = (color_t)((dw & priv->maskred) >> priv->shiftred);
				if (priv->shiftgreen < 0)
					g = (color_t)((dw & priv->maskgreen) << -priv->shiftgreen);
				else
					g = (color_t)((dw & priv->maskgreen) >> priv->shiftgreen);
				if (priv->shiftblue < 0)
					b = (color_t)((dw & priv->maskblue) << -priv->shiftblue);
				else
					b = (color_t)((dw & priv->maskblue) >> priv->shiftblue);
				/* We don't support alpha yet */
				*pc++ = RGB2COLOR(r, g, b);
				x++;
				len++;
			}
		}
		return len;
#endif

	default:
		return len;
	}
}

gdispImageError gdispImageCache_BMP(gdispImage *img) {
	gdispImagePrivate *	priv;
	color_t *			pcs;
	color_t *			pcd;
	coord_t				pos, x, y;
	size_t				len;

	/* If we are already cached - just return OK */
	priv = img->priv;
	if (priv->frame0cache)
		return GDISP_IMAGE_ERR_OK;

	/* We need to allocate the cache */
	len = img->width * img->height * sizeof(pixel_t);
	priv->frame0cache = (pixel_t *)gdispImageAlloc(img, len);
	if (!priv->frame0cache)
		return GDISP_IMAGE_ERR_NOMEMORY;

	/* Read the entire bitmap into cache */
	img->io.fns->seek(&img->io, priv->frame0pos);
#if GDISP_NEED_IMAGE_BMP_4_RLE || GDISP_NEED_IMAGE_BMP_8_RLE
	priv->rlerun = 0;
	priv->rlecode = 0;
#endif

	if (priv->bmpflags & BMP_TOP_TO_BOTTOM) {
		for(y = 0, pcd = priv->frame0cache; y < img->height; y++) {
			x = 0; pos = 0;
			while(x < img->width) {
				if (!pos) {
					if (!(pos = getPixels(img, x)))
						return GDISP_IMAGE_ERR_BADDATA;
					pcs = priv->buf;
				}
				*pcd++ = *pcs++;
				x++; pos--;
			}
		}
	} else {
		for(y = img->height-1, pcd = priv->frame0cache + img->width*(img->height-1); y >= 0; y--, pcd -= 2*img->width) {
			x = 0; pos = 0;
			while(x < img->width) {
				if (!pos) {
					if (!(pos = getPixels(img, x)))
						return GDISP_IMAGE_ERR_BADDATA;
					pcs = priv->buf;
				}
				*pcd++ = *pcs++;
				x++; pos--;
			}
		}
	}

	return GDISP_IMAGE_ERR_OK;
}

gdispImageError gdispImageDraw_BMP(gdispImage *img, coord_t x, coord_t y, coord_t cx, coord_t cy, coord_t sx, coord_t sy) {
	gdispImagePrivate *	priv;
	coord_t				mx, my;
	coord_t				pos, len, st;

	priv = img->priv;

	/* Check some reasonableness */
	if (sx >= img->width || sy >= img->height) return GDISP_IMAGE_ERR_OK;
	if (sx + cx > img->width) cx = img->width - sx;
	if (sy + cy > img->height) cy = img->height - sy;

	/* Draw from the image cache - if it exists */
	if (priv->frame0cache) {
		gdispBlitAreaEx(x, y, cx, cy, sx, sy, img->width, priv->frame0cache);
		return GDISP_IMAGE_ERR_OK;
	}

	/* Start decoding from the beginning */
	img->io.fns->seek(&img->io, priv->frame0pos);
#if GDISP_NEED_IMAGE_BMP_4_RLE || GDISP_NEED_IMAGE_BMP_8_RLE
	priv->rlerun = 0;
	priv->rlecode = 0;
#endif

	if (priv->bmpflags & BMP_TOP_TO_BOTTOM) {
		for(my = 0; my < img->height; my++) {
			mx = 0;
			while(mx < img->width) {
				if (!(pos = getPixels(img, mx)))
					return GDISP_IMAGE_ERR_BADDATA;
				if (my >= sy && my < sy+cy && mx < sx+cx && mx+pos >= sx) {
					st = mx < sx ? sx - mx : 0;
					len = pos-st;
					if (mx+st+len > sx+cx) len = sx+cx-mx-st;
					if (len == 1)
						gdispDrawPixel(x+mx+st-sx, y+my-sy, priv->buf[st]);
					else
						gdispBlitAreaEx(x+mx+st-sx, y+my-sy, len, 1, st, 0, pos, priv->buf);
				}
				mx += pos;
			}
		}
	} else {
		for(my = img->height-1; my >= 0; my--) {
			mx = 0;
			while(mx < img->width) {
				if (!(pos = getPixels(img, mx)))
					return GDISP_IMAGE_ERR_BADDATA;
				if (my >= sy && my < sy+cy && mx < sx+cx && mx+pos >= sx) {
					st = mx < sx ? sx - mx : 0;
					len = pos-st;
					if (mx+st+len > sx+cx) len = sx+cx-mx-st;
					if (len == 1)
						gdispDrawPixel(x+mx+st-sx, y+my-sy, priv->buf[st]);
					else
						gdispBlitAreaEx(x+mx+st-sx, y+my-sy, len, 1, st, 0, pos, priv->buf);
				}
				mx += pos;
			}
		}
	}

	return GDISP_IMAGE_ERR_OK;
}

delaytime_t gdispImageNext_BMP(gdispImage *img) {
	(void) img;

	/* No more frames/pages */
	return TIME_INFINITE;
}

#endif /* GFX_USE_GDISP && GDISP_NEED_IMAGE && GDISP_NEED_IMAGE_BMP */
/** @} */