diff --git a/3rd/cute_png/src/cute_png.c b/3rd/cute_png/src/cute_png.c index 23191555..b0a0f629 100644 --- a/3rd/cute_png/src/cute_png.c +++ b/3rd/cute_png/src/cute_png.c @@ -152,6 +152,25 @@ static bool cute_png_Image__clear(int argc, py_Ref argv) { 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() { py_GlobalRef mod = py_newmodule("cute_png"); 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, "getpixel", cute_png_Image__getpixel); py_bindmethod(tp_image, "clear", cute_png_Image__clear); + + py_bindmethod(tp_image, "to_rgb565", cute_png_Image__to_rgb565); } \ No newline at end of file diff --git a/include/typings/cute_png.pyi b/include/typings/cute_png.pyi index f9d3d9a3..cbbef7e9 100644 --- a/include/typings/cute_png.pyi +++ b/include/typings/cute_png.pyi @@ -16,5 +16,7 @@ class Image: def getpixel(self, x: int, y: int) -> color32: ... def clear(self, color: color32) -> None: ... + def to_rgb565(self) -> bytes: ... + def loads(data: bytes) -> array2d[color32]: ... def dumps(image: array2d[color32]) -> bytes: ... diff --git a/include/typings/vmath.pyi b/include/typings/vmath.pyi index 72d7e99e..4b27115e 100644 --- a/include/typings/vmath.pyi +++ b/include/typings/vmath.pyi @@ -214,7 +214,6 @@ class color32: def to_vec3i(self) -> vec3i: ... def to_rgb565(self) -> int: ... - def to_bgr565(self) -> int: ... def ansi_fg(self, text: str) -> str: ... def ansi_bg(self, text: str) -> str: ... diff --git a/src/modules/vmath.c b/src/modules/vmath.c index 930cd058..6171b978 100644 --- a/src/modules/vmath.c +++ b/src/modules/vmath.c @@ -988,18 +988,6 @@ static bool color32_to_rgb565(int argc, py_Ref argv) { 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) { PY_CHECK_ARGC(2); 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_vec3i", color32_to_vec3i); 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_bg", color32_ansi_bg); py_bindfunc(mod, "rgb", vmath_rgb);