Mercurial > repos > blastem
annotate png.c @ 2386:9f178feb3cb0
Prefix most hex-formatted values output by debugger with $ for consistency with number literal parsing
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 21 Nov 2023 21:35:14 -0800 |
parents | 0111c8344477 |
children |
rev | line source |
---|---|
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #include <stdint.h> |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 #include <stdlib.h> |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 #include <stdio.h> |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
4 #include <string.h> |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 #include "zlib/zlib.h" |
2295
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
6 #include "png.h" |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 static const char png_magic[] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n'}; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 static const char ihdr[] = {'I', 'H', 'D', 'R'}; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 static const char plte[] = {'P', 'L', 'T', 'E'}; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 static const char idat[] = {'I', 'D', 'A', 'T'}; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 static const char iend[] = {'I', 'E', 'N', 'D'}; |
2295
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
13 static const char actl[] = {'a', 'c', 'T', 'L'}; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
14 static const char fctl[] = {'f', 'c', 'T', 'L'}; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
15 static const char fdat[] = {'f', 'd', 'A', 'T'}; |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 enum { |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
18 COLOR_GRAY, |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 COLOR_TRUE = 2, |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
20 COLOR_INDEXED, |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
21 COLOR_GRAY_ALPHA, |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
22 COLOR_TRUE_ALPHA=6 |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 }; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 static void write_chunk(FILE *f, const char*id, uint8_t *buffer, uint32_t size) |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 uint8_t tmp[4] = {size >> 24, size >> 16, size >> 8, size}; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 uint8_t warn = 0; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 warn = warn || (sizeof(tmp) != fwrite(tmp, 1, sizeof(tmp), f)); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 warn = warn || (4 != fwrite(id, 1, 4, f)); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 if (size) { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 warn = warn || (size != fwrite(buffer, 1, size, f)); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 uint32_t crc = crc32(0, NULL, 0); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 crc = crc32(crc, id, 4); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 if (size) { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 crc = crc32(crc, buffer, size); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 tmp[0] = crc >> 24; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 tmp[1] = crc >> 16; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 tmp[2] = crc >> 8; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 tmp[3] = crc; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 warn = warn || (sizeof(tmp) != fwrite(tmp, 1, sizeof(tmp), f)); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 if (warn) { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 fprintf(stderr, "Failure during write of %c%c%c%c chunk\n", id[0], id[1], id[2], id[3]); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 static void write_header(FILE *f, uint32_t width, uint32_t height, uint8_t color_type) |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 uint8_t chunk[13] = { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 width >> 24, width >> 16, width >> 8, width, |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 height >> 24, height >> 16, height >> 8, height, |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 8, color_type, 0, 0, 0 |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 }; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 if (sizeof(png_magic) != fwrite(png_magic, 1, sizeof(png_magic), f)) { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 fputs("Error writing PNG magic\n", stderr); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 write_chunk(f, ihdr, chunk, sizeof(chunk)); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 |
2295
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
63 void save_png24_frame(FILE *f, uint32_t *buffer, apng_state *apng, uint32_t width, uint32_t height, uint32_t pitch) |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 uint32_t idat_size = (1 + width*3) * height; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 uint8_t *idat_buffer = malloc(idat_size); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 uint32_t *pixel = buffer; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 uint8_t *cur = idat_buffer; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 for (uint32_t y = 0; y < height; y++) |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 //save filter type |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 *(cur++) = 0; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 uint32_t *start = pixel; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 for (uint32_t x = 0; x < width; x++, pixel++) |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 uint32_t value = *pixel; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 *(cur++) = value >> 16; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 *(cur++) = value >> 8; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 *(cur++) = value; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 pixel = start + pitch / sizeof(uint32_t); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 } |
2295
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
83 |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 uLongf compress_buffer_size = idat_size + 5 * (idat_size/16383 + 1) + 3; |
2295
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
85 uint32_t offset = 0; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
86 if (apng) { |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
87 uint8_t chunk[26] = { |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
88 apng->sequence_number >> 24, apng->sequence_number >> 16, |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
89 apng->sequence_number >> 8, apng->sequence_number, |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
90 width >> 24, width >> 16, width >> 8, width, |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
91 height >> 24, height >> 16, height >> 8, height, |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
92 0, 0, 0, 0, //x offset |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
93 0, 0, 0, 0, //y offset |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
94 apng->delay_num >> 8, apng->delay_num, |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
95 apng->delay_den >> 8, apng->delay_den, |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
96 0, 0 //dispose and blend ops |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
97 }; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
98 write_chunk(f, fctl, chunk, sizeof(chunk)); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
99 apng->sequence_number++; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
100 apng->num_frames++; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
101 if (apng->sequence_number > 1) { |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
102 offset = sizeof(uint32_t); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
103 compress_buffer_size += offset; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
104 } |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
105 } |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 uint8_t *compressed = malloc(compress_buffer_size); |
2295
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
107 compress_buffer_size -= offset; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
108 compress(compressed + offset, &compress_buffer_size, idat_buffer, idat_size); |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
109 free(idat_buffer); |
2295
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
110 if (offset) { |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
111 cur = compressed; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
112 *(cur++) = apng->sequence_number >> 24; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
113 *(cur++) = apng->sequence_number >> 16; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
114 *(cur++) = apng->sequence_number >> 8; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
115 *(cur++) = apng->sequence_number; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
116 apng->sequence_number++; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
117 } |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
118 write_chunk(f, offset ? fdat : idat, compressed, compress_buffer_size + offset); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
119 free(compressed); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
120 } |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
121 |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
122 apng_state* start_apng(FILE *f, uint32_t width, uint32_t height, float frame_rate) |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
123 { |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
124 write_header(f, width, height, COLOR_TRUE); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
125 apng_state *apng = calloc(1, sizeof(apng_state)); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
126 uint8_t chunk[] = { |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
127 0, 0, 0, 0, |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
128 0, 0, 0, 1 |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
129 }; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
130 apng->num_frame_offset = ftell(f) + 8; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
131 write_chunk(f, actl, chunk, sizeof(chunk)); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
132 apng->delay_num = 65535.0f / frame_rate; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
133 apng->delay_den = frame_rate * apng->delay_num; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
134 return apng; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
135 } |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
136 |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
137 void end_apng(FILE *f, apng_state *apng) |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
138 { |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 write_chunk(f, iend, NULL, 0); |
2295
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
140 fseek(f, apng->num_frame_offset, SEEK_SET); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
141 uint8_t bytes[] = { |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
142 apng->num_frames >> 24, apng->num_frames >> 16, |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
143 apng->num_frames >> 8, apng->num_frames |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
144 }; |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
145 fwrite(bytes, 1, sizeof(bytes), f); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
146 fclose(f); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
147 free(apng); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
148 } |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
149 |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
150 void save_png24(FILE *f, uint32_t *buffer, uint32_t width, uint32_t height, uint32_t pitch) |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
151 { |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
152 write_header(f, width, height, COLOR_TRUE); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
153 save_png24_frame(f, buffer, NULL, width, height, pitch); |
eb45ad9d8a3f
WIP "video" recording in APNG format
Michael Pavone <pavone@retrodev.com>
parents:
2019
diff
changeset
|
154 write_chunk(f, iend, NULL, 0); |
1532
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
156 |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
157 void save_png(FILE *f, uint32_t *buffer, uint32_t width, uint32_t height, uint32_t pitch) |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
158 { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
159 uint32_t palette[256]; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
160 uint8_t pal_buffer[256*3]; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
161 uint32_t num_pal = 0; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
162 uint32_t index_size = (1 + width) * height; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 uint8_t *index_buffer = malloc(index_size); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 uint8_t *cur = index_buffer; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 uint32_t *pixel = buffer; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 for (uint32_t y = 0; y < height; y++) |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
168 //save filter type |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
169 *(cur++) = 0; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
170 uint32_t *start = pixel; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
171 for (uint32_t x = 0; x < width; x++, pixel++, cur++) |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
172 { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
173 uint32_t value = (*pixel) & 0xFFFFFF; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
174 uint32_t i; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
175 for (i = 0; i < num_pal; i++) |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
176 { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
177 if (palette[i] == value) { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
178 break; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
179 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
180 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
181 if (i == num_pal) { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 if (num_pal == 256) { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
183 free(index_buffer); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 save_png24(f, buffer, width, height, pitch); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 return; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
186 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
187 palette[i] = value; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 num_pal++; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
190 *cur = i; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
191 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
192 pixel = start + pitch / sizeof(uint32_t); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
193 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
194 write_header(f, width, height, COLOR_INDEXED); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
195 cur = pal_buffer; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
196 for (uint32_t i = 0; i < num_pal; i++) |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
197 { |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
198 *(cur++) = palette[i] >> 16; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
199 *(cur++) = palette[i] >> 8; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
200 *(cur++) = palette[i]; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
201 } |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
202 write_chunk(f, plte, pal_buffer, num_pal * 3); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
203 uLongf compress_buffer_size = index_size + 5 * (index_size/16383 + 1) + 3; |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 uint8_t *compressed = malloc(compress_buffer_size); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
205 compress(compressed, &compress_buffer_size, index_buffer, index_size); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
206 free(index_buffer); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
207 write_chunk(f, idat, compressed, compress_buffer_size); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
208 write_chunk(f, iend, NULL, 0); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
209 free(compressed); |
b505083dcd87
Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
210 } |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
211 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
212 typedef uint8_t (*filter_fun)(uint8_t *cur, uint8_t *last, uint8_t bpp, uint32_t x); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
213 typedef uint32_t (*pixel_fun)(uint8_t **cur, uint8_t **last, uint8_t bpp, uint32_t x, filter_fun); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
214 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
215 static uint8_t filter_none(uint8_t *cur, uint8_t *last, uint8_t bpp, uint32_t x) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
216 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
217 return *cur; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
218 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
219 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
220 static uint8_t filter_sub(uint8_t *cur, uint8_t *last, uint8_t bpp, uint32_t x) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
221 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
222 if (x) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
223 return *cur + *(cur - bpp); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
224 } else { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
225 return *cur; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
226 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
227 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
228 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
229 static uint8_t filter_up(uint8_t *cur, uint8_t *last, uint8_t bpp, uint32_t x) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
230 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
231 if (last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
232 return *cur + *last; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
233 } else { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
234 return *cur; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
235 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
236 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
237 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
238 static uint8_t filter_avg(uint8_t *cur, uint8_t *last, uint8_t bpp, uint32_t x) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
239 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
240 uint8_t prev = x ? *(cur - bpp) : 0; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
241 uint8_t prior = last ? *last : 0; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
242 return *cur + ((prev + prior) >> 1); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
243 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
244 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
245 static uint8_t paeth(uint8_t a, uint8_t b, uint8_t c) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
246 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
247 int32_t p = a + b - c; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
248 int32_t pa = abs(p - a); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
249 int32_t pb = abs(p - b); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
250 int32_t pc = abs(p - c); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
251 if (pa <= pb && pa <= pc) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
252 return a; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
253 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
254 if (pb <= pc) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
255 return b; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
256 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
257 return c; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
258 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
259 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
260 static uint8_t filter_paeth(uint8_t *cur, uint8_t *last, uint8_t bpp, uint32_t x) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
261 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
262 uint8_t prev, prev_prior; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
263 if (x) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
264 prev = *(cur - bpp); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
265 prev_prior = *(last - bpp); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
266 } else { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
267 prev = prev_prior = 0; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
268 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
269 uint8_t prior = last ? *last : 0; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
270 return *cur + paeth(prev, prior, prev_prior); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
271 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
272 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
273 static uint32_t pixel_gray(uint8_t **cur, uint8_t **last, uint8_t bpp, uint32_t x, filter_fun filter) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
274 { |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
275 uint8_t value = **cur = filter(*cur, *last, bpp, x); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
276 (*cur)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
277 if (*last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
278 (*last)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
279 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
280 return 0xFF000000 | value << 16 | value << 8 | value; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
281 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
282 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
283 static uint32_t pixel_true(uint8_t **cur, uint8_t **last, uint8_t bpp, uint32_t x, filter_fun filter) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
284 { |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
285 uint8_t red = **cur = filter(*cur, *last, bpp, x); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
286 (*cur)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
287 if (*last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
288 (*last)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
289 } |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
290 uint8_t green = **cur = filter(*cur, *last, bpp, x); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
291 (*cur)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
292 if (*last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
293 (*last)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
294 } |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
295 uint8_t blue = **cur = filter(*cur, *last, bpp, x); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
296 (*cur)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
297 if (*last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
298 (*last)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
299 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
300 return 0xFF000000 | red << 16 | green << 8 | blue; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
301 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
302 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
303 static uint32_t pixel_gray_alpha(uint8_t **cur, uint8_t **last, uint8_t bpp, uint32_t x, filter_fun filter) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
304 { |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
305 uint8_t value = **cur = filter(*cur, *last, bpp, x); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
306 (*cur)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
307 if (*last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
308 (*last)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
309 } |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
310 uint8_t alpha = **cur = filter(*cur, *last, bpp, x); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
311 (*cur)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
312 if (*last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
313 (*last)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
314 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
315 return alpha << 24 | value << 16 | value << 8 | value; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
316 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
317 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
318 static uint32_t pixel_true_alpha(uint8_t **cur, uint8_t **last, uint8_t bpp, uint32_t x, filter_fun filter) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
319 { |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
320 uint8_t red = **cur = filter(*cur, *last, bpp, x); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
321 (*cur)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
322 if (*last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
323 (*last)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
324 } |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
325 uint8_t green = **cur = filter(*cur, *last, bpp, x); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
326 (*cur)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
327 if (*last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
328 (*last)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
329 } |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
330 uint8_t blue = **cur = filter(*cur, *last, bpp, x); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
331 (*cur)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
332 if (*last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
333 (*last)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
334 } |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
335 uint8_t alpha = **cur = filter(*cur, *last, bpp, x); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
336 (*cur)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
337 if (*last) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
338 (*last)++; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
339 } |
2346
0111c8344477
Fix some issues identified by asan/ubsan
Michael Pavone <pavone@retrodev.com>
parents:
2295
diff
changeset
|
340 return ((uint32_t)alpha) << 24 | red << 16 | green << 8 | blue; |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
341 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
342 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
343 static filter_fun filters[] = {filter_none, filter_sub, filter_up, filter_avg, filter_paeth}; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
344 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
345 #define MIN_CHUNK_SIZE 12 |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
346 #define MIN_IHDR_SIZE 0xD |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
347 #define MAX_SUPPORTED_DIM 32767 //chosen to avoid possibility of overflow when calculating uncompressed size |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
348 uint32_t *load_png(uint8_t *buffer, uint32_t buf_size, uint32_t *width, uint32_t *height) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
349 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
350 if (buf_size < sizeof(png_magic) || memcmp(buffer, png_magic, sizeof(png_magic))) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
351 return NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
352 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
353 uint32_t cur = sizeof(png_magic); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
354 uint8_t has_header = 0; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
355 uint8_t bits, color_type, comp_type, filter_type, interlace; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
356 uint8_t *idat_buf = NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
357 uint8_t idat_needs_free = 0; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
358 uint32_t idat_size; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
359 uint32_t *out = NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
360 uint32_t *palette = NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
361 while(cur + MIN_CHUNK_SIZE <= buf_size) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
362 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
363 uint32_t chunk_size = buffer[cur++] << 24; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
364 chunk_size |= buffer[cur++] << 16; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
365 chunk_size |= buffer[cur++] << 8; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
366 chunk_size |= buffer[cur++]; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
367 if (!memcmp(ihdr, buffer + cur, sizeof(ihdr))) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
368 if (chunk_size < MIN_IHDR_SIZE || cur + MIN_IHDR_SIZE > buf_size) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
369 return NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
370 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
371 cur += sizeof(ihdr); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
372 *width = buffer[cur++] << 24; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
373 *width |= buffer[cur++] << 16; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
374 *width |= buffer[cur++] << 8; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
375 *width |= buffer[cur++]; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
376 *height = buffer[cur++] << 24; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
377 *height |= buffer[cur++] << 16; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
378 *height |= buffer[cur++] << 8; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
379 *height |= buffer[cur++]; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
380 if (*width > MAX_SUPPORTED_DIM || *height > MAX_SUPPORTED_DIM) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
381 return NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
382 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
383 bits = buffer[cur++]; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
384 if (bits != 8) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
385 //only support 8-bits per element for now |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
386 return NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
387 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
388 color_type = buffer[cur++]; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
389 if (color_type > COLOR_TRUE_ALPHA || color_type == 1 || color_type == 5) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
390 //reject invalid color type |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
391 return NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
392 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
393 comp_type = buffer[cur++]; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
394 if (comp_type) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
395 //only compression type 0 is defined by the spec |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
396 return NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
397 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
398 filter_type = buffer[cur++]; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
399 interlace = buffer[cur++]; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
400 if (interlace) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
401 //interlacing not supported for now |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
402 return NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
403 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
404 cur += chunk_size - MIN_IHDR_SIZE; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
405 has_header = 1; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
406 } else { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
407 if (!has_header) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
408 //IHDR is required to be the first chunk, fail if it isn't |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
409 break; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
410 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
411 if (!memcmp(plte, buffer + cur, sizeof(plte))) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
412 //TODO: implement paletted images |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
413 } else if (!memcmp(idat, buffer + cur, sizeof(idat))) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
414 cur += sizeof(idat); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
415 if (idat_buf) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
416 if (idat_needs_free) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
417 idat_buf = realloc(idat_buf, idat_size + chunk_size); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
418 } else { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
419 uint8_t *tmp = idat_buf; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
420 idat_buf = malloc(idat_size + chunk_size); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
421 memcpy(idat_buf, tmp, idat_size); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
422 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
423 memcpy(idat_buf + idat_size, buffer + cur, chunk_size); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
424 idat_size += chunk_size; |
2019
81eebbe6b2e3
Fix some bugs in PNG decoder
Michael Pavone <pavone@retrodev.com>
parents:
1592
diff
changeset
|
425 idat_needs_free = 1; |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
426 } else { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
427 idat_buf = buffer + cur; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
428 idat_size = chunk_size; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
429 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
430 cur += chunk_size; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
431 } else if (!memcmp(iend, buffer + cur, sizeof(iend))) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
432 if (!idat_buf) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
433 break; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
434 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
435 if (!palette && color_type == COLOR_INDEXED) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
436 //indexed color, but no PLTE chunk found |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
437 return NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
438 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
439 uLongf uncompressed_size = *width * *height; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
440 uint8_t bpp; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
441 pixel_fun pixel; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
442 switch (color_type) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
443 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
444 case COLOR_GRAY: |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
445 uncompressed_size *= bits / 8; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
446 bpp = bits/8; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
447 pixel = pixel_gray; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
448 break; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
449 case COLOR_TRUE: |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
450 uncompressed_size *= 3 * bits / 8; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
451 bpp = 3 * bits/8; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
452 pixel = pixel_true; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
453 break; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
454 case COLOR_INDEXED: { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
455 uint32_t pixels_per_byte = 8 / bits; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
456 uncompressed_size = (*width / pixels_per_byte) * *height; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
457 if (*width % pixels_per_byte) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
458 uncompressed_size += *height; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
459 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
460 bpp = 1; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
461 break; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
462 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
463 case COLOR_GRAY_ALPHA: |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
464 uncompressed_size *= bits / 4; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
465 bpp = bits / 4; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
466 pixel = pixel_gray_alpha; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
467 break; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
468 case COLOR_TRUE_ALPHA: |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
469 uncompressed_size *= bits / 2; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
470 bpp = bits / 2; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
471 pixel = pixel_true_alpha; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
472 break; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
473 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
474 //add filter type byte |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
475 uncompressed_size += *height; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
476 uint8_t *decomp_buffer = malloc(uncompressed_size); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
477 if (Z_OK != uncompress(decomp_buffer, &uncompressed_size, idat_buf, idat_size)) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
478 free(decomp_buffer); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
479 break; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
480 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
481 out = calloc(*width * *height, sizeof(uint32_t)); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
482 uint32_t *cur_pixel = out; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
483 uint8_t *cur_byte = decomp_buffer; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
484 uint8_t *last_line = NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
485 for (uint32_t y = 0; y < *height; y++) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
486 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
487 uint8_t filter_type = *(cur_byte++); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
488 if (filter_type >= sizeof(filters)/sizeof(*filters)) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
489 free(out); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
490 out = NULL; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
491 free(decomp_buffer); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
492 break; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
493 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
494 filter_fun filter = filters[filter_type]; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
495 uint8_t *line_start = cur_byte; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
496 for (uint32_t x = 0; x < *width; x++) |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
497 { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
498 *(cur_pixel++) = pixel(&cur_byte, &last_line, bpp, x, filter); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
499 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
500 last_line = line_start; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
501 } |
1592
31effaadf877
Fix some memory errors (mostly leaks) identified by valgrind
Michael Pavone <pavone@retrodev.com>
parents:
1569
diff
changeset
|
502 free(decomp_buffer); |
1569
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
503 } else { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
504 //skip uncrecognized chunks |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
505 cur += 4 + chunk_size; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
506 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
507 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
508 //skip CRC for now |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
509 cur += sizeof(uint32_t); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
510 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
511 if (idat_needs_free) { |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
512 free(idat_buf); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
513 } |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
514 free(palette); |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
515 return out; |
0ec89dadb36d
Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents:
1532
diff
changeset
|
516 } |