mirror of
https://github.com/pocketpy/pocketpy
synced 2025-12-16 15:00:19 +00:00
Update cute_png.c
This commit is contained in:
parent
480eb6cf36
commit
db201f3f1f
@ -4,6 +4,14 @@
|
|||||||
#define CUTE_PNG_IMPLEMENTATION
|
#define CUTE_PNG_IMPLEMENTATION
|
||||||
#include "cute_png.h"
|
#include "cute_png.h"
|
||||||
|
|
||||||
|
#if PY_SYS_PLATFORM == 5
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <linux/fb.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
static bool cute_png_loads(int argc, py_Ref argv) {
|
static bool cute_png_loads(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
PY_CHECK_ARG_TYPE(0, tp_bytes);
|
PY_CHECK_ARG_TYPE(0, tp_bytes);
|
||||||
@ -192,28 +200,62 @@ static bool cute_png_Image__to_rgb565_file(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARG_TYPE(1, tp_str);
|
PY_CHECK_ARG_TYPE(1, tp_str);
|
||||||
cp_image_t* image = py_touserdata(argv);
|
cp_image_t* image = py_touserdata(argv);
|
||||||
const char* path = py_tostr(py_arg(1));
|
const char* path = py_tostr(py_arg(1));
|
||||||
FILE* fp = fopen(path, "wb");
|
|
||||||
if(fp == NULL) return OSError("cannot open file '%s' for writing", path);
|
#define CONVERT_TO_RGB565(__block) \
|
||||||
size_t size = 0;
|
size_t size = 0; \
|
||||||
for(int y = 0; y < image->h; y++) {
|
for(int y = 0; y < image->h; y++) { \
|
||||||
for(int x = 0; x < image->w; x++) {
|
for(int x = 0; x < image->w; x++) { \
|
||||||
size_t idx = y * image->w + x;
|
size_t idx = y * image->w + x; \
|
||||||
cp_pixel_t pixel = image->pix[idx];
|
cp_pixel_t pixel = image->pix[idx]; \
|
||||||
uint16_t r = (pixel.r >> 3) & 0x1F;
|
uint16_t r = (pixel.r >> 3) & 0x1F; \
|
||||||
uint16_t g = (pixel.g >> 2) & 0x3F;
|
uint16_t g = (pixel.g >> 2) & 0x3F; \
|
||||||
uint16_t b = (pixel.b >> 3) & 0x1F;
|
uint16_t b = (pixel.b >> 3) & 0x1F; \
|
||||||
uint16_t rgb565 = (r << 11) | (g << 5) | b;
|
uint16_t rgb565 = (r << 11) | (g << 5) | b; \
|
||||||
// use little-endian
|
__block \
|
||||||
size_t delta = fwrite(&rgb565, 1, 2, fp);
|
} \
|
||||||
size += delta;
|
}
|
||||||
if(delta != 2) {
|
|
||||||
|
#if PY_SYS_PLATFORM == 5
|
||||||
|
if(strcmp(path, "/dev/fb0") == 0) {
|
||||||
|
static struct fb_fix_screeninfo finfo;
|
||||||
|
static uint8_t* vmem_base;
|
||||||
|
if(vmem_base == NULL) {
|
||||||
|
int fbdev_fd = open(path, O_RDWR);
|
||||||
|
if(fbdev_fd < 0) return OSError("open() '/dev/fb0' failed");
|
||||||
|
fcntl(fbdev_fd, F_SETFD, fcntl(fbdev_fd, F_GETFD) | FD_CLOEXEC);
|
||||||
|
if(ioctl(fbdev_fd, FBIOGET_FSCREENINFO, &finfo) < 0) {
|
||||||
|
close(fbdev_fd);
|
||||||
|
return OSError("ioctl() '/dev/fb0' failed");
|
||||||
|
}
|
||||||
|
vmem_base = mmap(0, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, 0);
|
||||||
|
if(vmem_base == MAP_FAILED) {
|
||||||
|
vmem_base = NULL;
|
||||||
|
close(fbdev_fd);
|
||||||
|
return OSError("mmap() '/dev/fb0' failed");
|
||||||
|
}
|
||||||
|
close(fbdev_fd);
|
||||||
|
}
|
||||||
|
CONVERT_TO_RGB565(
|
||||||
|
// use little endian
|
||||||
|
if(size + 2 > finfo.smem_len) {
|
||||||
py_newint(py_retval(), size);
|
py_newint(py_retval(), size);
|
||||||
return true;
|
return true;
|
||||||
}
|
} memcpy(vmem_base + size, &rgb565, 2);
|
||||||
}
|
size += 2;)
|
||||||
|
py_newint(py_retval(), size);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
FILE* fp = fopen(path, "wb");
|
||||||
|
if(fp == NULL) return OSError("cannot open file '%s' for writing", path);
|
||||||
|
CONVERT_TO_RGB565(
|
||||||
|
// use little endian
|
||||||
|
size += fwrite(&rgb565, 1, 2, fp);)
|
||||||
|
fclose(fp);
|
||||||
py_newint(py_retval(), size);
|
py_newint(py_retval(), size);
|
||||||
return true;
|
return true;
|
||||||
|
#undef CONVERT_TO_RGB565
|
||||||
}
|
}
|
||||||
|
|
||||||
void pk__add_module_cute_png() {
|
void pk__add_module_cute_png() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user