annotate png.c @ 1673:ab3b465c052c

Fix Windows implentation of get_config_dir() so config file gets saved to the right place. Fix location for sticky_path file on all platforms
author Michael Pavone <pavone@retrodev.com>
date Thu, 03 Jan 2019 19:30:41 -0800
parents 31effaadf877
children 81eebbe6b2e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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"
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 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
8 static const char ihdr[] = {'I', 'H', 'D', 'R'};
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 static const char plte[] = {'P', 'L', 'T', 'E'};
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 static const char idat[] = {'I', 'D', 'A', 'T'};
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 static const char iend[] = {'I', 'E', 'N', 'D'};
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 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
14 COLOR_GRAY,
1532
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 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
16 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
17 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
18 COLOR_TRUE_ALPHA=6
1532
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 };
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 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
22 {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 uint8_t tmp[4] = {size >> 24, size >> 16, size >> 8, size};
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 uint8_t warn = 0;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 warn = warn || (sizeof(tmp) != fwrite(tmp, 1, sizeof(tmp), f));
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 warn = warn || (4 != fwrite(id, 1, 4, f));
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 if (size) {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 warn = warn || (size != fwrite(buffer, 1, size, f));
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 uint32_t crc = crc32(0, NULL, 0);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 crc = crc32(crc, id, 4);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 if (size) {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 crc = crc32(crc, buffer, size);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 tmp[0] = crc >> 24;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 tmp[1] = crc >> 16;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 tmp[2] = crc >> 8;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 tmp[3] = crc;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 warn = warn || (sizeof(tmp) != fwrite(tmp, 1, sizeof(tmp), f));
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 if (warn) {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 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
43 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 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
47 {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 uint8_t chunk[13] = {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 width >> 24, width >> 16, width >> 8, width,
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 height >> 24, height >> 16, height >> 8, height,
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 8, color_type, 0, 0, 0
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 };
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 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
54 fputs("Error writing PNG magic\n", stderr);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 write_chunk(f, ihdr, chunk, sizeof(chunk));
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 void save_png24(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
60 {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 uint32_t idat_size = (1 + width*3) * height;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 uint8_t *idat_buffer = malloc(idat_size);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 uint32_t *pixel = buffer;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 uint8_t *cur = idat_buffer;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 for (uint32_t y = 0; y < height; y++)
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67 //save filter type
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 *(cur++) = 0;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 uint32_t *start = pixel;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 for (uint32_t x = 0; x < width; x++, pixel++)
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 uint32_t value = *pixel;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 *(cur++) = value >> 16;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 *(cur++) = value >> 8;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 *(cur++) = value;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 pixel = start + pitch / sizeof(uint32_t);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 write_header(f, width, height, COLOR_TRUE);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 uLongf compress_buffer_size = idat_size + 5 * (idat_size/16383 + 1) + 3;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 uint8_t *compressed = malloc(compress_buffer_size);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82 compress(compressed, &compress_buffer_size, idat_buffer, idat_size);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 free(idat_buffer);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 write_chunk(f, idat, compressed, compress_buffer_size);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 write_chunk(f, iend, NULL, 0);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 free(compressed);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
87 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
88
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
89 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
90 {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
91 uint32_t palette[256];
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 uint8_t pal_buffer[256*3];
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93 uint32_t num_pal = 0;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94 uint32_t index_size = (1 + width) * height;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95 uint8_t *index_buffer = malloc(index_size);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 uint8_t *cur = index_buffer;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 uint32_t *pixel = buffer;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 for (uint32_t y = 0; y < height; y++)
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
99 {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 //save filter type
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101 *(cur++) = 0;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 uint32_t *start = pixel;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 for (uint32_t x = 0; x < width; x++, pixel++, cur++)
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105 uint32_t value = (*pixel) & 0xFFFFFF;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 uint32_t i;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 for (i = 0; i < num_pal; i++)
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
109 if (palette[i] == value) {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110 break;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
111 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
112 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
113 if (i == num_pal) {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
114 if (num_pal == 256) {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
115 free(index_buffer);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
116 save_png24(f, buffer, width, height, pitch);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
117 return;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 palette[i] = value;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
120 num_pal++;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
121 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 *cur = i;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
123 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
124 pixel = start + pitch / sizeof(uint32_t);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126 write_header(f, width, height, COLOR_INDEXED);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
127 cur = pal_buffer;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
128 for (uint32_t i = 0; i < num_pal; i++)
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
129 {
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
130 *(cur++) = palette[i] >> 16;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
131 *(cur++) = palette[i] >> 8;
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
132 *(cur++) = palette[i];
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
133 }
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
134 write_chunk(f, plte, pal_buffer, num_pal * 3);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
135 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
136 uint8_t *compressed = malloc(compress_buffer_size);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
137 compress(compressed, &compress_buffer_size, index_buffer, index_size);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
138 free(index_buffer);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
139 write_chunk(f, idat, compressed, compress_buffer_size);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
140 write_chunk(f, iend, NULL, 0);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
141 free(compressed);
b505083dcd87 Added png screenshot support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
142 }
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
143
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
144 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
145 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
146
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
147 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
148 {
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
149 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
150 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
151
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
152 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
153 {
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
154 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
155 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
156 } 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
157 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
158 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
159 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
160
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
161 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
162 {
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
163 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
164 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
165 } 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
166 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
167 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
168 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
169
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
170 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
171 {
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
172 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
173 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
174 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
175 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
176
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
177 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
178 {
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
179 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
180 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
181 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
182 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
183 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
184 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
185 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
186 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
187 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
188 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
189 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
190 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
191
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
192 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
193 {
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
194 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
195 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
196 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
197 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
198 } 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
199 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
200 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
201 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
202 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
203 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
204
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
205 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
206 {
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
207 uint8_t value = filter(*cur, *last, bpp, 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
208 (*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
209 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
210 (*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
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 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
213 }
0ec89dadb36d Add 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 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
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 uint8_t red = filter(*cur, *last, bpp, 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
218 (*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
219 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
220 (*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
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 uint8_t green = filter(*cur, *last, bpp, 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 (*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
224 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
225 (*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
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 uint8_t blue = filter(*cur, *last, bpp, 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
228 (*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
229 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
230 (*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
231 }
0ec89dadb36d Add 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 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
233 }
0ec89dadb36d Add 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
0ec89dadb36d Add 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 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
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 uint8_t value = filter(*cur, *last, bpp, 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
238 (*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
239 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
240 (*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
241 }
0ec89dadb36d Add 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 uint8_t alpha = filter(*cur, *last, bpp, 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
243 (*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
244 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
245 (*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
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 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
248 }
0ec89dadb36d Add 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
0ec89dadb36d Add 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 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
251 {
0ec89dadb36d Add 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 uint8_t red = filter(*cur, *last, bpp, 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
253 (*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
254 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
255 (*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
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 uint8_t green = filter(*cur, *last, bpp, 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
258 (*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
259 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
260 (*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
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 blue = filter(*cur, *last, bpp, 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
263 (*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
264 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
265 (*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
266 }
0ec89dadb36d Add 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 uint8_t alpha = filter(*cur, *last, bpp, 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
268 (*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
269 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
270 (*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
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 return alpha << 24 | 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
273 }
0ec89dadb36d Add 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
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
275 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
276
0ec89dadb36d Add 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 #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
278 #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
279 #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
280 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
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 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
283 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
284 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
285 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
286 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
287 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
288 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
289 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
290 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
291 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
292 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
293 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
294 {
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
295 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
296 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
297 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
298 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
299 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
300 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
301 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
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 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
304 *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
305 *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
306 *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
307 *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
308 *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
309 *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
310 *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
311 *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
312 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
313 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
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 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
316 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
317 //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
318 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
319 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
320 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
321 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
322 //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
323 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
324 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
325 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
326 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
327 //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
328 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
329 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
330 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
331 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
332 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
333 //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
334 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
335 }
0ec89dadb36d Add 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 += 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
337 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
338 } 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
339 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
340 //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
341 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
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 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
344 //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
345 } 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
346 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
347 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
348 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
349 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
350 } 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
351 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
352 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
353 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
354 }
0ec89dadb36d Add 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 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
356 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
357 } 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
358 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
359 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
360 }
0ec89dadb36d Add 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 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
362 } 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
363 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
364 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
365 }
0ec89dadb36d Add 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 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
367 //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
368 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
369 }
0ec89dadb36d Add 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 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
371 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
372 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
373 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
374 {
0ec89dadb36d Add 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 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
376 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
377 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
378 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
379 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
380 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
381 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
382 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
383 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
384 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
385 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
386 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
387 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
388 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
389 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
390 }
0ec89dadb36d Add 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 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
392 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
393 }
0ec89dadb36d Add 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 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
395 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
396 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
397 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
398 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
399 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
400 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
401 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
402 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
403 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
404 }
0ec89dadb36d Add 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 //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
406 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
407 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
408 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
409 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
410 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
411 }
0ec89dadb36d Add 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 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
413 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
414 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
415 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
416 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
417 {
0ec89dadb36d Add 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 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
419 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
420 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
421 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
422 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
423 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
424 }
0ec89dadb36d Add code for loading PNG images. Added 360 controller image. WIP work on gamepad mapping UI
Michael Pavone <pavone@retrodev.com>
parents: 1532
diff changeset
425 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
426 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
427 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
428 {
0ec89dadb36d Add 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 *(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
430 }
0ec89dadb36d Add 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 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
432 }
1592
31effaadf877 Fix some memory errors (mostly leaks) identified by valgrind
Michael Pavone <pavone@retrodev.com>
parents: 1569
diff changeset
433 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
434 } 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
435 //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
436 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
437 }
0ec89dadb36d Add 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 //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
440 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
441 }
0ec89dadb36d Add 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 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
443 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
444 }
0ec89dadb36d Add 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 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
446 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
447 }