This commit is contained in:
blueloveTH 2025-12-07 21:22:13 +08:00
parent 2ceea8b96d
commit d11c377f4c
4 changed files with 23 additions and 14 deletions

View File

@ -152,6 +152,25 @@ static bool cute_png_Image__clear(int argc, py_Ref argv) {
return true; return true;
} }
static bool cute_png_Image__to_rgb565(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
cp_image_t* image = py_touserdata(argv);
unsigned char* data = py_newbytes(py_retval(), image->w * image->h * 2);
for(int y = 0; y < image->h; y++) {
for(int x = 0; x < image->w; x++) {
size_t idx = y * image->w + x;
cp_pixel_t pixel = image->pix[idx];
uint16_t r = (pixel.r >> 3) & 0x1F;
uint16_t g = (pixel.g >> 2) & 0x3F;
uint16_t b = (pixel.b >> 3) & 0x1F;
uint16_t rgb565 = (r << 11) | (g << 5) | b;
data[idx * 2 + 0] = (rgb565 >> 8) & 0xFF;
data[idx * 2 + 1] = (rgb565 >> 0) & 0xFF;
}
}
return true;
}
void pk__add_module_cute_png() { void pk__add_module_cute_png() {
py_GlobalRef mod = py_newmodule("cute_png"); py_GlobalRef mod = py_newmodule("cute_png");
py_Type tp_image = py_newtype("Image", tp_object, mod, cute_png_Image__dtor); py_Type tp_image = py_newtype("Image", tp_object, mod, cute_png_Image__dtor);
@ -168,4 +187,6 @@ void pk__add_module_cute_png() {
py_bindmethod(tp_image, "setpixel", cute_png_Image__setpixel); py_bindmethod(tp_image, "setpixel", cute_png_Image__setpixel);
py_bindmethod(tp_image, "getpixel", cute_png_Image__getpixel); py_bindmethod(tp_image, "getpixel", cute_png_Image__getpixel);
py_bindmethod(tp_image, "clear", cute_png_Image__clear); py_bindmethod(tp_image, "clear", cute_png_Image__clear);
py_bindmethod(tp_image, "to_rgb565", cute_png_Image__to_rgb565);
} }

View File

@ -16,5 +16,7 @@ class Image:
def getpixel(self, x: int, y: int) -> color32: ... def getpixel(self, x: int, y: int) -> color32: ...
def clear(self, color: color32) -> None: ... def clear(self, color: color32) -> None: ...
def to_rgb565(self) -> bytes: ...
def loads(data: bytes) -> array2d[color32]: ... def loads(data: bytes) -> array2d[color32]: ...
def dumps(image: array2d[color32]) -> bytes: ... def dumps(image: array2d[color32]) -> bytes: ...

View File

@ -214,7 +214,6 @@ class color32:
def to_vec3i(self) -> vec3i: ... def to_vec3i(self) -> vec3i: ...
def to_rgb565(self) -> int: ... def to_rgb565(self) -> int: ...
def to_bgr565(self) -> int: ...
def ansi_fg(self, text: str) -> str: ... def ansi_fg(self, text: str) -> str: ...
def ansi_bg(self, text: str) -> str: ... def ansi_bg(self, text: str) -> str: ...

View File

@ -988,18 +988,6 @@ static bool color32_to_rgb565(int argc, py_Ref argv) {
return true; return true;
} }
static bool color32_to_bgr565(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
c11_color32 color = py_tocolor32(argv);
c11_color32_premult(&color);
uint16_t b = (color.b >> 3) & 0x1F;
uint16_t g = (color.g >> 2) & 0x3F;
uint16_t r = (color.r >> 3) & 0x1F;
uint16_t bgr565 = (b << 11) | (g << 5) | r;
py_newint(py_retval(), bgr565);
return true;
}
static bool color32__eq__(int argc, py_Ref argv) { static bool color32__eq__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
if(argv[1].type != tp_color32) { if(argv[1].type != tp_color32) {
@ -1304,7 +1292,6 @@ void pk__add_module_vmath() {
py_bindmethod(color32, "to_vec3", color32_to_vec3); py_bindmethod(color32, "to_vec3", color32_to_vec3);
py_bindmethod(color32, "to_vec3i", color32_to_vec3i); py_bindmethod(color32, "to_vec3i", color32_to_vec3i);
py_bindmethod(color32, "to_rgb565", color32_to_rgb565); py_bindmethod(color32, "to_rgb565", color32_to_rgb565);
py_bindmethod(color32, "to_bgr565", color32_to_bgr565);
py_bindmethod(color32, "ansi_fg", color32_ansi_fg); py_bindmethod(color32, "ansi_fg", color32_ansi_fg);
py_bindmethod(color32, "ansi_bg", color32_ansi_bg); py_bindmethod(color32, "ansi_bg", color32_ansi_bg);
py_bindfunc(mod, "rgb", vmath_rgb); py_bindfunc(mod, "rgb", vmath_rgb);