forked from mackron/dr_libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdr_pcx.h
796 lines (667 loc) · 25.6 KB
/
dr_pcx.h
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
// PCX image loader. Public domain. See "unlicense" statement at the end of this file.
// dr_pcx - v0.2 - 2016-10-28
//
// David Reid - mackron@gmail.com
// USAGE
//
// dr_pcx is a single-file library. To use it, do something like the following in one .c file.
// #define DR_PCX_IMPLEMENTATION
// #include "dr_pcx.h"
//
// You can then #include this file in other parts of the program as you would with any other header file. Do something like
// the following to load and decode an image:
//
// int width;
// int height;
// int components
// dr_uint8* pImageData = drpcx_load_file("my_image.pcx", DR_FALSE, &width, &height, &components, 0);
// if (pImageData == NULL) {
// // Failed to load image.
// }
//
// ...
//
// drpcx_free(pImageData);
//
// The boolean parameter (second argument in the above example) is whether or not the image should be flipped upside down.
//
//
//
// OPTIONS
// #define these options before including this file.
//
// #define DR_PCX_NO_STDIO
// Disable drpcx_load_file().
//
//
//
// QUICK NOTES
// - 2-bpp/4-plane and 4-bpp/1-plane formats have not been tested.
#ifndef dr_pcx_h
#define dr_pcx_h
#ifndef DR_SIZED_TYPES_DEFINED
#define DR_SIZED_TYPES_DEFINED
#if defined(_MSC_VER) && _MSC_VER < 1600
typedef signed char dr_int8;
typedef unsigned char dr_uint8;
typedef signed short dr_int16;
typedef unsigned short dr_uint16;
typedef signed int dr_int32;
typedef unsigned int dr_uint32;
typedef signed __int64 dr_int64;
typedef unsigned __int64 dr_uint64;
#else
#include <stdint.h>
typedef int8_t dr_int8;
typedef uint8_t dr_uint8;
typedef int16_t dr_int16;
typedef uint16_t dr_uint16;
typedef int32_t dr_int32;
typedef uint32_t dr_uint32;
typedef int64_t dr_int64;
typedef uint64_t dr_uint64;
#endif
typedef dr_int8 dr_bool8;
typedef dr_int32 dr_bool32;
#define DR_TRUE 1
#define DR_FALSE 0
#endif
#ifdef __cplusplus
extern "C" {
#endif
// Callback for when data is read. Return value is the number of bytes actually read.
typedef size_t (* drpcx_read_proc)(void* userData, void* bufferOut, size_t bytesToRead);
// Loads a PCX file using the given callbacks.
dr_uint8* drpcx_load(drpcx_read_proc onRead, void* pUserData, dr_bool32 flipped, int* x, int* y, int* internalComponents, int desiredComponents);
// Frees memory returned by drpcx_load() and family.
void drpcx_free(void* pReturnValueFromLoad);
#ifndef DR_PCX_NO_STDIO
// Loads an PCX file from an actual file.
dr_uint8* drpcx_load_file(const char* filename, dr_bool32 flipped, int* x, int* y, int* internalComponents, int desiredComponents);
#endif
// Helper for loading an PCX file from a block of memory.
dr_uint8* drpcx_load_memory(const void* data, size_t dataSize, dr_bool32 flipped, int* x, int* y, int* internalComponents, int desiredComponents);
#ifdef __cplusplus
}
#endif
#endif // dr_pcx_h
///////////////////////////////////////////////////////////////////////////////
//
// IMPLEMENTATION
//
///////////////////////////////////////////////////////////////////////////////
#ifdef DR_PCX_IMPLEMENTATION
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifndef DR_PCX_NO_STDIO
#include <stdio.h>
static size_t drpcx__on_read_stdio(void* pUserData, void* bufferOut, size_t bytesToRead)
{
return fread(bufferOut, 1, bytesToRead, (FILE*)pUserData);
}
dr_uint8* drpcx_load_file(const char* filename, dr_bool32 flipped, int* x, int* y, int* internalComponents, int desiredComponents)
{
FILE* pFile;
#ifdef _MSC_VER
if (fopen_s(&pFile, filename, "rb") != 0) {
return DR_FALSE;
}
#else
pFile = fopen(filename, "rb");
if (pFile == NULL) {
return DR_FALSE;
}
#endif
dr_uint8* pImageData = drpcx_load(drpcx__on_read_stdio, pFile, flipped, x, y, internalComponents, desiredComponents);
fclose(pFile);
return pImageData;
}
#endif // DR_PCX_NO_STDIO
typedef struct
{
// A pointer to the beginning of the data. We use a char as the type here for easy offsetting.
const unsigned char* data;
size_t dataSize;
size_t currentReadPos;
} drpcx_memory;
static size_t drpcx__on_read_memory(void* pUserData, void* bufferOut, size_t bytesToRead)
{
drpcx_memory* memory = (drpcx_memory*)pUserData;
assert(memory != NULL);
assert(memory->dataSize >= memory->currentReadPos);
size_t bytesRemaining = memory->dataSize - memory->currentReadPos;
if (bytesToRead > bytesRemaining) {
bytesToRead = bytesRemaining;
}
if (bytesToRead > 0) {
memcpy(bufferOut, memory->data + memory->currentReadPos, bytesToRead);
memory->currentReadPos += bytesToRead;
}
return bytesToRead;
}
dr_uint8* drpcx_load_memory(const void* data, size_t dataSize, dr_bool32 flipped, int* x, int* y, int* internalComponents, int desiredComponents)
{
drpcx_memory memory;
memory.data = (const unsigned char*)data;
memory.dataSize = dataSize;
memory.currentReadPos = 0;
return drpcx_load(drpcx__on_read_memory, &memory, flipped, x, y, internalComponents, desiredComponents);
}
typedef struct
{
dr_uint8 header;
dr_uint8 version;
dr_uint8 encoding;
dr_uint8 bpp;
dr_uint16 left;
dr_uint16 top;
dr_uint16 right;
dr_uint16 bottom;
dr_uint16 hres;
dr_uint16 vres;
dr_uint8 palette16[48];
dr_uint8 reserved1;
dr_uint8 bitPlanes;
dr_uint16 bytesPerLine;
dr_uint16 paletteType;
dr_uint16 screenSizeH;
dr_uint16 screenSizeV;
dr_uint8 reserved2[54];
} drpcx_header;
typedef struct
{
drpcx_read_proc onRead;
void* pUserData;
dr_bool32 flipped;
drpcx_header header;
dr_uint32 width;
dr_uint32 height;
dr_uint32 components; // 3 = RGB; 4 = RGBA. Only 3 and 4 are supported.
dr_uint8* pImageData;
} drpcx;
static dr_uint8 drpcx__read_byte(drpcx* pPCX)
{
dr_uint8 byte = 0;
pPCX->onRead(pPCX->pUserData, &byte, 1);
return byte;
}
static dr_uint8* drpcx__row_ptr(drpcx* pPCX, dr_uint32 row)
{
dr_uint32 stride = pPCX->width * pPCX->components;
dr_uint8* pRow = pPCX->pImageData;
if (pPCX->flipped) {
pRow += (pPCX->height - row - 1) * stride;
} else {
pRow += row * stride;
}
return pRow;
}
static dr_uint8 drpcx__rle(drpcx* pPCX, dr_uint8* pRLEValueOut)
{
dr_uint8 rleCount;
dr_uint8 rleValue;
rleValue = drpcx__read_byte(pPCX);
if ((rleValue & 0xC0) == 0xC0) {
rleCount = rleValue & 0x3F;
rleValue = drpcx__read_byte(pPCX);
} else {
rleCount = 1;
}
*pRLEValueOut = rleValue;
return rleCount;
}
dr_bool32 drpcx__decode_1bit(drpcx* pPCX)
{
dr_uint8 rleCount = 0;
dr_uint8 rleValue = 0;
switch (pPCX->header.bitPlanes)
{
case 1:
{
for (dr_uint32 y = 0; y < pPCX->height; ++y)
{
dr_uint8* pRow = drpcx__row_ptr(pPCX, y);
for (dr_uint32 x = 0; x < pPCX->header.bytesPerLine; ++x)
{
if (rleCount == 0) {
rleCount = drpcx__rle(pPCX, &rleValue);
}
rleCount -= 1;
for (int bit = 0; (bit < 8) && ((x*8 + bit) < pPCX->width); ++bit)
{
dr_uint8 mask = (1 << (7 - bit));
dr_uint8 paletteIndex = (rleValue & mask) >> (7 - bit);
pRow[0] = paletteIndex * 255;
pRow[1] = paletteIndex * 255;
pRow[2] = paletteIndex * 255;
pRow += 3;
}
}
}
return DR_TRUE;
} break;
case 2:
case 3:
case 4:
{
for (dr_uint32 y = 0; y < pPCX->height; ++y)
{
for (dr_uint32 c = 0; c < pPCX->header.bitPlanes; ++c)
{
dr_uint8* pRow = drpcx__row_ptr(pPCX, y);
for (dr_uint32 x = 0; x < pPCX->header.bytesPerLine; ++x)
{
if (rleCount == 0) {
rleCount = drpcx__rle(pPCX, &rleValue);
}
rleCount -= 1;
for (int bit = 0; (bit < 8) && ((x*8 + bit) < pPCX->width); ++bit)
{
dr_uint8 mask = (1 << (7 - bit));
dr_uint8 paletteIndex = (rleValue & mask) >> (7 - bit);
pRow[0] |= ((paletteIndex & 0x01) << c);
pRow += pPCX->components;
}
}
}
dr_uint8* pRow = drpcx__row_ptr(pPCX, y);
for (dr_uint32 x = 0; x < pPCX->width; ++x)
{
dr_uint8 paletteIndex = pRow[0];
for (dr_uint32 c = 0; c < pPCX->components; ++c) {
pRow[c] = pPCX->header.palette16[paletteIndex*3 + c];
}
pRow += pPCX->components;
}
}
return DR_TRUE;
}
default: return DR_FALSE;
}
}
dr_bool32 drpcx__decode_2bit(drpcx* pPCX)
{
dr_uint8 rleCount = 0;
dr_uint8 rleValue = 0;
switch (pPCX->header.bitPlanes)
{
case 1:
{
dr_uint8 paletteCGA[48];
paletteCGA[ 0] = 0x00; paletteCGA[ 1] = 0x00; paletteCGA[ 2] = 0x00; // #000000
paletteCGA[ 3] = 0x00; paletteCGA[ 4] = 0x00; paletteCGA[ 5] = 0xAA; // #0000AA
paletteCGA[ 6] = 0x00; paletteCGA[ 7] = 0xAA; paletteCGA[ 8] = 0x00; // #00AA00
paletteCGA[ 9] = 0x00; paletteCGA[10] = 0xAA; paletteCGA[11] = 0xAA; // #00AAAA
paletteCGA[12] = 0xAA; paletteCGA[13] = 0x00; paletteCGA[14] = 0x00; // #AA0000
paletteCGA[15] = 0xAA; paletteCGA[16] = 0x00; paletteCGA[17] = 0xAA; // #AA00AA
paletteCGA[18] = 0xAA; paletteCGA[19] = 0x55; paletteCGA[20] = 0x00; // #AA5500
paletteCGA[21] = 0xAA; paletteCGA[22] = 0xAA; paletteCGA[23] = 0xAA; // #AAAAAA
paletteCGA[24] = 0x55; paletteCGA[25] = 0x55; paletteCGA[26] = 0x55; // #555555
paletteCGA[27] = 0x55; paletteCGA[28] = 0x55; paletteCGA[29] = 0xFF; // #5555FF
paletteCGA[30] = 0x55; paletteCGA[31] = 0xFF; paletteCGA[32] = 0x55; // #55FF55
paletteCGA[33] = 0x55; paletteCGA[34] = 0xFF; paletteCGA[35] = 0xFF; // #55FFFF
paletteCGA[36] = 0xFF; paletteCGA[37] = 0x55; paletteCGA[38] = 0x55; // #FF5555
paletteCGA[39] = 0xFF; paletteCGA[40] = 0x55; paletteCGA[41] = 0xFF; // #FF55FF
paletteCGA[42] = 0xFF; paletteCGA[43] = 0xFF; paletteCGA[44] = 0x55; // #FFFF55
paletteCGA[45] = 0xFF; paletteCGA[46] = 0xFF; paletteCGA[47] = 0xFF; // #FFFFFF
dr_uint8 cgaBGColor = pPCX->header.palette16[0] >> 4;
dr_uint8 i = (pPCX->header.palette16[3] & 0x20) >> 5;
dr_uint8 p = (pPCX->header.palette16[3] & 0x40) >> 6;
//dr_uint8 c = (pPCX->header.palette16[3] & 0x80) >> 7; // Color or monochrome. How is monochrome handled?
for (dr_uint32 y = 0; y < pPCX->height; ++y)
{
dr_uint8* pRow = drpcx__row_ptr(pPCX, y);
for (dr_uint32 x = 0; x < pPCX->header.bytesPerLine; ++x)
{
if (rleCount == 0) {
rleCount = drpcx__rle(pPCX, &rleValue);
}
rleCount -= 1;
for (int bit = 0; bit < 4; ++bit)
{
if (x*4 + bit < pPCX->width)
{
dr_uint8 mask = (3 << ((3 - bit) * 2));
dr_uint8 paletteIndex = (rleValue & mask) >> ((3 - bit) * 2);
dr_uint8 cgaIndex;
if (paletteIndex == 0) { // Background.
cgaIndex = cgaBGColor;
} else { // Foreground
cgaIndex = (((paletteIndex << 1) + p) + (i << 3));
}
pRow[0] = paletteCGA[cgaIndex*3 + 0];
pRow[1] = paletteCGA[cgaIndex*3 + 1];
pRow[2] = paletteCGA[cgaIndex*3 + 2];
pRow += 3;
}
}
}
}
// TODO: According to http://www.fysnet.net/pcxfile.htm, we should use the palette at the end of the file
// instead of the standard CGA palette if the version is equal to 5. With my test files the palette
// at the end of the file does not exist. Research this one.
if (pPCX->header.version == 5) {
dr_uint8 paletteMarker = drpcx__read_byte(pPCX);
if (paletteMarker == 0x0C) {
// TODO: Implement Me.
}
}
return DR_TRUE;
};
case 4:
{
// NOTE: This is completely untested. If anybody knows where I can get a test file please let me know or send it through to me!
// TODO: Test Me.
for (dr_uint32 y = 0; y < pPCX->height; ++y)
{
for (dr_uint32 c = 0; c < pPCX->header.bitPlanes; ++c)
{
dr_uint8* pRow = drpcx__row_ptr(pPCX, y);
for (dr_uint32 x = 0; x < pPCX->header.bytesPerLine; ++x)
{
if (rleCount == 0) {
rleCount = drpcx__rle(pPCX, &rleValue);
}
rleCount -= 1;
for (int bitpair = 0; (bitpair < 4) && ((x*4 + bitpair) < pPCX->width); ++bitpair)
{
dr_uint8 mask = (4 << (3 - bitpair));
dr_uint8 paletteIndex = (rleValue & mask) >> (3 - bitpair);
pRow[0] |= ((paletteIndex & 0x03) << (c*2));
pRow += pPCX->components;
}
}
}
dr_uint8* pRow = drpcx__row_ptr(pPCX, y);
for (dr_uint32 x = 0; x < pPCX->width; ++x)
{
dr_uint8 paletteIndex = pRow[0];
for (dr_uint32 c = 0; c < pPCX->header.bitPlanes; ++c) {
pRow[c] = pPCX->header.palette16[paletteIndex*3 + c];
}
pRow += pPCX->components;
}
}
return DR_TRUE;
};
default: return DR_FALSE;
}
}
dr_bool32 drpcx__decode_4bit(drpcx* pPCX)
{
// NOTE: This is completely untested. If anybody knows where I can get a test file please let me know or send it through to me!
// TODO: Test Me.
if (pPCX->header.bitPlanes > 1) {
return DR_FALSE;
}
dr_uint8 rleCount = 0;
dr_uint8 rleValue = 0;
for (dr_uint32 y = 0; y < pPCX->height; ++y)
{
for (dr_uint32 c = 0; c < pPCX->header.bitPlanes; ++c)
{
dr_uint8* pRow = drpcx__row_ptr(pPCX, y);
for (dr_uint32 x = 0; x < pPCX->header.bytesPerLine; ++x)
{
if (rleCount == 0) {
rleCount = drpcx__rle(pPCX, &rleValue);
}
rleCount -= 1;
for (int nibble = 0; (nibble < 2) && ((x*2 + nibble) < pPCX->width); ++nibble)
{
dr_uint8 mask = (4 << (1 - nibble));
dr_uint8 paletteIndex = (rleValue & mask) >> (1 - nibble);
pRow[0] |= ((paletteIndex & 0x0F) << (c*4));
pRow += pPCX->components;
}
}
}
dr_uint8* pRow = drpcx__row_ptr(pPCX, y);
for (dr_uint32 x = 0; x < pPCX->width; ++x)
{
dr_uint8 paletteIndex = pRow[0];
for (dr_uint32 c = 0; c < pPCX->components; ++c) {
pRow[c] = pPCX->header.palette16[paletteIndex*3 + c];
}
pRow += pPCX->components;
}
}
return DR_TRUE;
}
dr_bool32 drpcx__decode_8bit(drpcx* pPCX)
{
dr_uint8 rleCount = 0;
dr_uint8 rleValue = 0;
dr_uint32 stride = pPCX->width * pPCX->components;
switch (pPCX->header.bitPlanes)
{
case 1:
{
for (dr_uint32 y = 0; y < pPCX->height; ++y)
{
dr_uint8* pRow = drpcx__row_ptr(pPCX, y);
for (dr_uint32 x = 0; x < pPCX->header.bytesPerLine; ++x)
{
if (rleCount == 0) {
rleCount = drpcx__rle(pPCX, &rleValue);
}
rleCount -= 1;
if (x < pPCX->width) {
pRow[0] = rleValue;
pRow[1] = rleValue;
pRow[2] = rleValue;
pRow += 3;
}
}
}
// At this point we can know if we are dealing with a palette or a grayscale image by checking the next byte. If ti's equal to 0x0C, we
// need to do a simple palette lookup.
dr_uint8 paletteMarker = drpcx__read_byte(pPCX);
if (paletteMarker == 0x0C)
{
// A palette is present - we need to do a second pass.
dr_uint8 palette256[768];
if (pPCX->onRead(pPCX->pUserData, palette256, sizeof(palette256)) != sizeof(palette256)) {
return DR_FALSE;
}
for (dr_uint32 y = 0; y < pPCX->height; ++y)
{
dr_uint8* pRow = pPCX->pImageData + (y * stride);
for (dr_uint32 x = 0; x < pPCX->width; ++x)
{
dr_uint8 index = pRow[0];
pRow[0] = palette256[index*3 + 0];
pRow[1] = palette256[index*3 + 1];
pRow[2] = palette256[index*3 + 2];
pRow += 3;
}
}
}
return DR_TRUE;
}
case 3:
case 4:
{
for (dr_uint32 y = 0; y < pPCX->height; ++y)
{
for (dr_uint32 c = 0; c < pPCX->components; ++c)
{
dr_uint8* pRow = drpcx__row_ptr(pPCX, y);
for (dr_uint32 x = 0; x < pPCX->header.bytesPerLine; ++x)
{
if (rleCount == 0) {
rleCount = drpcx__rle(pPCX, &rleValue);
}
rleCount -= 1;
if (x < pPCX->width) {
pRow[c] = rleValue;
pRow += pPCX->components;
}
}
}
}
return DR_TRUE;
}
}
return DR_TRUE;
}
dr_uint8* drpcx_load(drpcx_read_proc onRead, void* pUserData, dr_bool32 flipped, int* x, int* y, int* internalComponents, int desiredComponents)
{
if (onRead == NULL) return NULL;
if (desiredComponents > 4) return NULL;
drpcx pcx;
pcx.onRead = onRead;
pcx.pUserData = pUserData;
pcx.flipped = flipped;
if (onRead(pUserData, &pcx.header, sizeof(pcx.header)) != sizeof(pcx.header)) {
return NULL; // Failed to read the header.
}
if (pcx.header.header != 10) {
return NULL; // Not a PCX file.
}
if (pcx.header.encoding != 1) {
return NULL; // Not supporting non-RLE encoding. Would assume a value of 0 indicates raw, unencoded, but that is apparently never used.
}
if (pcx.header.bpp != 1 && pcx.header.bpp != 2 && pcx.header.bpp != 4 && pcx.header.bpp != 8) {
return NULL; // Unsupported pixel format.
}
pcx.width = pcx.header.right - pcx.header.left + 1;
pcx.height = pcx.header.bottom - pcx.header.top + 1;
pcx.components = (pcx.header.bpp == 8 && pcx.header.bitPlanes == 4) ? 4 : 3;
size_t dataSize = pcx.width * pcx.height * pcx.components;
pcx.pImageData = (dr_uint8*)calloc(1, dataSize); // <-- Clearing to zero is important! Required for proper decoding.
if (pcx.pImageData == NULL) {
return NULL; // Failed to allocate memory.
}
dr_bool32 result = DR_FALSE;
switch (pcx.header.bpp)
{
case 1:
{
result = drpcx__decode_1bit(&pcx);
} break;
case 2:
{
result = drpcx__decode_2bit(&pcx);
} break;
case 4:
{
result = drpcx__decode_4bit(&pcx);
} break;
case 8:
{
result = drpcx__decode_8bit(&pcx);
} break;
}
if (!result) {
free(pcx.pImageData);
return NULL;
}
// There's an annoying amount of branching when loading PCX files so for simplicity I'm doing the component conversion as
// a second pass.
if (desiredComponents == 0) desiredComponents = pcx.components;
if (desiredComponents != (int)pcx.components) {
dr_uint8* pNewImageData = (dr_uint8*)malloc(pcx.width * pcx.height * desiredComponents);
if (pNewImageData == NULL) {
free(pcx.pImageData);
return NULL;
}
dr_uint8* pSrcData = pcx.pImageData;
dr_uint8* pDstData = pNewImageData;
if (desiredComponents < (int)pcx.components) {
// We're reducing the number of components. Just drop the excess.
for (dr_uint32 i = 0; i < pcx.width*pcx.height; ++i) {
for (int c = 0; c < desiredComponents; ++c) {
pDstData[c] = pSrcData[c];
}
pSrcData += pcx.components;
pDstData += desiredComponents;
}
} else {
// We're increasing the number of components. Always ensure the alpha channel is set to 0xFF.
if (pcx.components == 1) {
for (dr_uint32 i = 0; i < pcx.width*pcx.height; ++i) {
for (int c = 0; c < desiredComponents; ++c) {
pDstData[c] = pSrcData[0];
}
pSrcData += pcx.components;
pDstData += desiredComponents;
}
} else if (pcx.components == 2) {
for (dr_uint32 i = 0; i < pcx.width*pcx.height; ++i) {
pDstData[0] = pSrcData[0];
pDstData[1] = pSrcData[1];
pDstData[2] = 0x00;
if (desiredComponents == 4) pDstData[3] = 0xFF;
pSrcData += pcx.components;
pDstData += desiredComponents;
}
} else {
assert(pcx.components == 3);
assert(desiredComponents == 4);
for (dr_uint32 i = 0; i < pcx.width*pcx.height; ++i) {
pDstData[0] = pSrcData[0];
pDstData[1] = pSrcData[1];
pDstData[2] = pSrcData[2];
pDstData[3] = 0xFF;
pSrcData += pcx.components;
pDstData += desiredComponents;
}
}
}
free(pcx.pImageData);
pcx.pImageData = pNewImageData;
}
if (x) *x = pcx.width;
if (y) *y = pcx.height;
if (internalComponents) *internalComponents = pcx.components;
return pcx.pImageData;
}
void drpcx_free(void* pReturnValueFromLoad)
{
free(pReturnValueFromLoad);
}
#endif // DR_PCX_IMPLEMENTATION
// REVISION HISTORY
//
// v0.2 - 2016-10-28
// - API CHANGE: Add a parameter to drpcx_load() and family to control the number of output components.
// - Use custom sized types rather than built-in ones to improve support for older MSVC compilers.
//
// v0.1c - 2016-10-23
// - A minor change to dr_bool8 and dr_bool32 types.
//
// v0.1b - 2016-10-11
// - Use dr_bool32 instead of the built-in "bool" type. The reason for this change is that it helps maintain API/ABI consistency
// between C and C++ builds.
//
// v0.1a - 2016-09-18
// - Change date format to ISO 8601 (YYYY-MM-DD)
//
// v0.1 - 2016-05-04
// - Initial versioned release.
// TODO
// - Test 2-bpp/4-plane and 4-bpp/1-plane formats.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/