Debian Codegraph @

3rd/cute_png/src/cute_png.c

raw
1 #include "pocketpy.h"
2 #include <stdio.h>
3
4 #define CUTE_PNG_IMPLEMENTATION
5 #include "cute_png.h"
6
7 #if PY_SYS_PLATFORM == 5
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <sys/ioctl.h>
11 #include <sys/mman.h>
12 #include <linux/fb.h>
13 #endif
14
15 static bool cute_png_loads(int argc, py_Ref argv) {
16 PY_CHECK_ARGC(1);
17 PY_CHECK_ARG_TYPE(0, tp_bytes);
18 int size;
19 unsigned char* data = py_tobytes(argv, &size);
20 cp_image_t image = cp_load_png_mem(data, size);
21 if(image.pix == NULL) return ValueError("cp_load_png_mem() failed");
22 py_newarray2d(py_retval(), image.w, image.h);
23 for(int y = 0; y < image.h; y++) {
24 for(int x = 0; x < image.w; x++) {
25 cp_pixel_t pixel = image.pix[y * image.w + x];
26 py_ObjectRef slot = py_array2d_getitem(py_retval(), x, y);
27 c11_color32 color;
28 color.r = pixel.r;
29 color.g = pixel.g;
30 color.b = pixel.b;
31 color.a = pixel.a;
32 py_newcolor32(slot, color);
33 }
34 }
35 cp_free_png(&image);
36 return true;
37 }
38
39 static bool cute_png_dumps(int argc, py_Ref argv) {
40 PY_CHECK_ARGC(1);
41 PY_CHECK_ARG_TYPE(0, tp_array2d);
42 int width = py_array2d_getwidth(argv);
43 int height = py_array2d_getheight(argv);
44 cp_image_t image = cp_load_blank(width, height);
45 for(int y = 0; y < height; y++) {
46 for(int x = 0; x < width; x++) {
47 py_ObjectRef slot = py_array2d_getitem(argv, x, y);
48 if(!py_checktype(slot, tp_color32)) return false;
49 c11_color32 color = py_tocolor32(slot);
50 cp_pixel_t pixel = cp_make_pixel_a(color.r, color.g, color.b, color.a);
51 image.pix[y * width + x] = pixel;
52 }
53 }
54 cp_saved_png_t saved_image = cp_save_png_to_memory(&image);
55 assert(saved_image.data != NULL);
56 unsigned char* data = py_newbytes(py_retval(), saved_image.size);
57 memcpy(data, saved_image.data, saved_image.size);
58 CUTE_PNG_FREE(saved_image.data);
59 cp_free_png(&image);
60 return true;
61 }
62
63 static void cute_png_Image__dtor(void* ud) {
64 cp_image_t* image = (cp_image_t*)ud;
65 cp_free_png(image);
66 }
67
68 static bool cute_png_Image__new__(int argc, py_Ref argv) {
69 PY_CHECK_ARGC(3);
70 PY_CHECK_ARG_TYPE(1, tp_int);
71 PY_CHECK_ARG_TYPE(2, tp_int);
72 int width = py_toint(py_arg(1));
73 int height = py_toint(py_arg(2));
74 cp_image_t* ud = py_newobject(py_retval(), py_totype(argv), 0, sizeof(cp_image_t));
75 *ud = cp_load_blank(width, height);
76 return true;
77 }
78
79 static bool cute_png_Image__from_bytes_STATIC(int argc, py_Ref argv) {
80 PY_CHECK_ARGC(1);
81 PY_CHECK_ARG_TYPE(0, tp_bytes);
82 int size;
83 unsigned char* data = py_tobytes(argv, &size);
84 cp_image_t image = cp_load_png_mem(data, size);
85 if(image.pix == NULL) return ValueError("cp_load_png_mem() failed");
86 cp_image_t* ud =
87 py_newobject(py_retval(), py_gettype("cute_png", py_name("Image")), 0, sizeof(cp_image_t));
88 *ud = image;
89 return true;
90 }
91
92 static bool cute_png_Image__from_file_STATIC(int argc, py_Ref argv) {
93 PY_CHECK_ARGC(1);
94 PY_CHECK_ARG_TYPE(0, tp_str);
95 const char* path = py_tostr(argv);
96 cp_image_t image = cp_load_png(path);
97 if(image.pix == NULL) return ValueError("cp_load_png() failed");
98 cp_image_t* ud =
99 py_newobject(py_retval(), py_gettype("cute_png", py_name("Image")), 0, sizeof(cp_image_t));
100 *ud = image;
101 return true;
102 }
103
104 static bool cute_png_Image__width(int argc, py_Ref argv) {
105 PY_CHECK_ARGC(1);
106 cp_image_t* image = py_touserdata(argv);
107 py_newint(py_retval(), image->w);
108 return true;
109 }
110
111 static bool cute_png_Image__height(int argc, py_Ref argv) {
112 PY_CHECK_ARGC(1);
113 cp_image_t* image = py_touserdata(argv);
114 py_newint(py_retval(), image->h);
115 return true;
116 }
117
118 static bool cute_png_Image__setpixel(int argc, py_Ref argv) {
119 PY_CHECK_ARGC(4);
120 cp_image_t* image = py_touserdata(argv);
121 PY_CHECK_ARG_TYPE(1, tp_int);
122 PY_CHECK_ARG_TYPE(2, tp_int);
123 PY_CHECK_ARG_TYPE(3, tp_color32);
124 int x = py_toint(py_arg(1));
125 int y = py_toint(py_arg(2));
126 c11_color32 color = py_tocolor32(py_arg(3));
127 if(x < 0 || x >= image->w || y < 0 || y >= image->h) {
128 return IndexError("cute_png.Image: index out of range");
129 }
130 cp_pixel_t pixel = cp_make_pixel_a(color.r, color.g, color.b, color.a);
131 image->pix[y * image->w + x] = pixel;
132 py_newnone(py_retval());
133 return true;
134 }
135
136 static bool cute_png_Image__getpixel(int argc, py_Ref argv) {
137 PY_CHECK_ARGC(3);
138 cp_image_t* image = py_touserdata(argv);
139 PY_CHECK_ARG_TYPE(1, tp_int);
140 PY_CHECK_ARG_TYPE(2, tp_int);
141 int x = py_toint(py_arg(1));
142 int y = py_toint(py_arg(2));
143 if(x < 0 || x >= image->w || y < 0 || y >= image->h) {
144 return IndexError("cute_png.Image: index out of range");
145 }
146 cp_pixel_t pixel = image->pix[y * image->w + x];
147 c11_color32 color;
148 color.r = pixel.r;
149 color.g = pixel.g;
150 color.b = pixel.b;
151 color.a = pixel.a;
152 py_newcolor32(py_retval(), color);
153 return true;
154 }
155
156 static bool cute_png_Image__clear(int argc, py_Ref argv) {
157 PY_CHECK_ARGC(2);
158 cp_image_t* image = py_touserdata(argv);
159 PY_CHECK_ARG_TYPE(1, tp_color32);
160 c11_color32 color = py_tocolor32(py_arg(1));
161 cp_pixel_t pixel = cp_make_pixel_a(color.r, color.g, color.b, color.a);
162 for(int y = 0; y < image->h; y++) {
163 for(int x = 0; x < image->w; x++) {
164 image->pix[y * image->w + x] = pixel;
165 }
166 }
167 py_newnone(py_retval());
168 return true;
169 }
170
171 static bool cute_png_Image__to_png_bytes(int argc, py_Ref argv) {
172 PY_CHECK_ARGC(1);
173 cp_image_t* image = py_touserdata(argv);
174 cp_saved_png_t saved_image = cp_save_png_to_memory(image);
175 assert(saved_image.data != NULL);
176 unsigned char* data = py_newbytes(py_retval(), saved_image.size);
177 memcpy(data, saved_image.data, saved_image.size);
178 CUTE_PNG_FREE(saved_image.data);
179 return true;
180 }
181
182 static bool cute_png_Image__to_png_file(int argc, py_Ref argv) {
183 PY_CHECK_ARGC(2);
184 PY_CHECK_ARG_TYPE(1, tp_str);
185 cp_image_t* image = py_touserdata(argv);
186 const char* path = py_tostr(py_arg(1));
187 FILE* fp = fopen(path, "wb");
188 if(fp == NULL) return OSError("cannot open file '%s' for writing", path);
189 cp_saved_png_t saved_image = cp_save_png_to_memory(image);
190 assert(saved_image.data != NULL);
191 size_t size = fwrite(saved_image.data, saved_image.size, 1, fp);
192 CUTE_PNG_FREE(saved_image.data);
193 fclose(fp);
194 py_newint(py_retval(), size);
195 return true;
196 }
197
198 static bool cute_png_Image__to_rgb565_file(int argc, py_Ref argv) {
199 PY_CHECK_ARGC(2);
200 PY_CHECK_ARG_TYPE(1, tp_str);
201 cp_image_t* image = py_touserdata(argv);
202 const char* path = py_tostr(py_arg(1));
203
204 #define CONVERT_TO_RGB565(__block) \
205 size_t size = 0; \
206 for(int y = 0; y < image->h; y++) { \
207 for(int x = 0; x < image->w; x++) { \
208 size_t idx = y * image->w + x; \
209 cp_pixel_t pixel = image->pix[idx]; \
210 uint16_t r = (pixel.r >> 3) & 0x1F; \
211 uint16_t g = (pixel.g >> 2) & 0x3F; \
212 uint16_t b = (pixel.b >> 3) & 0x1F; \
213 uint16_t rgb565 = (r << 11) | (g << 5) | b; \
214 __block \
215 } \
216 }
217
218 #if PY_SYS_PLATFORM == 5
219 if(strcmp(path, "/dev/fb0") == 0) {
220 static struct fb_fix_screeninfo finfo;
221 static uint8_t* vmem_base;
222 if(vmem_base == NULL) {
223 int fbdev_fd = open(path, O_RDWR);
224 if(fbdev_fd < 0) return OSError("open() '/dev/fb0' failed");
225 fcntl(fbdev_fd, F_SETFD, fcntl(fbdev_fd, F_GETFD) | FD_CLOEXEC);
226 if(ioctl(fbdev_fd, FBIOGET_FSCREENINFO, &finfo) < 0) {
227 close(fbdev_fd);
228 return OSError("ioctl() '/dev/fb0' failed");
229 }
230 vmem_base = mmap(0, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, 0);
231 if(vmem_base == MAP_FAILED) {
232 vmem_base = NULL;
233 close(fbdev_fd);
234 return OSError("mmap() '/dev/fb0' failed");
235 }
236 close(fbdev_fd);
237 }
238 CONVERT_TO_RGB565(
239 // use little endian
240 if(size + 2 > finfo.smem_len) {
241 py_newint(py_retval(), size);
242 return true;
243 } memcpy(vmem_base + size, &rgb565, 2);
244 size += 2;)
245 py_newint(py_retval(), size);
246 return true;
247 }
248 #endif
249
250 FILE* fp = fopen(path, "wb");
251 if(fp == NULL) return OSError("cannot open file '%s' for writing", path);
252 CONVERT_TO_RGB565(
253 // use little endian
254 size += fwrite(&rgb565, 1, 2, fp);)
255 fclose(fp);
256 py_newint(py_retval(), size);
257 return true;
258 #undef CONVERT_TO_RGB565
259 }
260
261 static bool cute_png_Image__paste(int argc, py_Ref argv) {
262 PY_CHECK_ARGC(8);
263 PY_CHECK_ARG_TYPE(1, py_gettype("cute_png", py_name("Image"))); // src_img
264 PY_CHECK_ARG_TYPE(2, tp_vec2i); // src_pos
265 PY_CHECK_ARG_TYPE(3, tp_vec2i); // dst_pos
266 PY_CHECK_ARG_TYPE(4, tp_int); // width
267 PY_CHECK_ARG_TYPE(5, tp_int); // height
268 PY_CHECK_ARG_TYPE(6, tp_color32); // fg
269 PY_CHECK_ARG_TYPE(7, tp_color32); // bg
270
271 cp_image_t* dst_image = py_touserdata(argv);
272 cp_image_t* src_image = py_touserdata(py_arg(1));
273 c11_vec2i src_pos = py_tovec2i(py_arg(2));
274 c11_vec2i dst_pos = py_tovec2i(py_arg(3));
275 int width = py_toint(py_arg(4));
276 int height = py_toint(py_arg(5));
277 c11_color32 fg = py_tocolor32(py_arg(6));
278 c11_color32 bg = py_tocolor32(py_arg(7));
279
280 for(int y = 0; y < height; y++) {
281 for(int x = 0; x < width; x++) {
282 int src_x = src_pos.x + x;
283 int src_y = src_pos.y + y;
284 int dst_x = dst_pos.x + x;
285 int dst_y = dst_pos.y + y;
286
287 if(src_x >= src_image->w || src_y >= src_image->h) continue;
288 if(dst_x >= dst_image->w || dst_y >= dst_image->h) continue;
289
290 // fill with bg color first
291 cp_pixel_t dst_pixel = cp_make_pixel_a(bg.r, bg.g, bg.b, 255);
292
293 // override with fg * src_pixel
294 cp_pixel_t src_pixel = src_image->pix[src_y * src_image->w + src_x];
295 src_pixel.r = (src_pixel.r * (int)fg.r) / 255;
296 src_pixel.g = (src_pixel.g * (int)fg.g) / 255;
297 src_pixel.b = (src_pixel.b * (int)fg.b) / 255;
298
299 // alpha blend
300 if(src_pixel.a > 0) {
301 float alpha = src_pixel.a / 255.0f;
302 dst_pixel.r = (uint8_t)(src_pixel.r * alpha + dst_pixel.r * (1.0f - alpha));
303 dst_pixel.g = (uint8_t)(src_pixel.g * alpha + dst_pixel.g * (1.0f - alpha));
304 dst_pixel.b = (uint8_t)(src_pixel.b * alpha + dst_pixel.b * (1.0f - alpha));
305 }
306
307 dst_image->pix[dst_y * dst_image->w + dst_x] = dst_pixel;
308 }
309 }
310 py_newnone(py_retval());
311 return true;
312 }
313
314 void pk__add_module_cute_png() {
315 py_GlobalRef mod = py_newmodule("cute_png");
316 py_Type tp_image = py_newtype("Image", tp_object, mod, cute_png_Image__dtor);
317 py_tpsetfinal(tp_image);
318
319 py_bindfunc(mod, "loads", cute_png_loads);
320 py_bindfunc(mod, "dumps", cute_png_dumps);
321
322 py_bindmethod(tp_image, "__new__", cute_png_Image__new__);
323 py_bindstaticmethod(tp_image, "from_bytes", cute_png_Image__from_bytes_STATIC);
324 py_bindstaticmethod(tp_image, "from_file", cute_png_Image__from_file_STATIC);
325
326 py_bindproperty(tp_image, "width", cute_png_Image__width, NULL);
327 py_bindproperty(tp_image, "height", cute_png_Image__height, NULL);
328
329 py_bindmethod(tp_image, "setpixel", cute_png_Image__setpixel);
330 py_bindmethod(tp_image, "getpixel", cute_png_Image__getpixel);
331 py_bindmethod(tp_image, "clear", cute_png_Image__clear);
332
333 py_bindmethod(tp_image, "paste", cute_png_Image__paste);
334
335 py_bindmethod(tp_image, "to_png_bytes", cute_png_Image__to_png_bytes);
336 py_bindmethod(tp_image, "to_png_file", cute_png_Image__to_png_file);
337 py_bindmethod(tp_image, "to_rgb565_file", cute_png_Image__to_rgb565_file);
338 }