mirror of
https://github.com/pocketpy/pocketpy
synced 2025-12-16 06:50:16 +00:00
minor fix
This commit is contained in:
parent
0c004a9a16
commit
550f8c9d0e
@ -81,6 +81,18 @@ static bool cute_png_Image__from_bytes_STATIC(int argc, py_Ref argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool cute_png_Image__from_file_STATIC(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(1);
|
||||||
|
PY_CHECK_ARG_TYPE(0, tp_str);
|
||||||
|
const char* path = py_tostr(argv);
|
||||||
|
cp_image_t image = cp_load_png(path);
|
||||||
|
if(image.pix == NULL) return ValueError("cute_png: %s", cp_error_reason);
|
||||||
|
cp_image_t* ud =
|
||||||
|
py_newobject(py_retval(), py_gettype("cute_png", py_name("Image")), 0, sizeof(cp_image_t));
|
||||||
|
*ud = image;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static bool cute_png_Image__width(int argc, py_Ref argv) {
|
static bool cute_png_Image__width(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
cp_image_t* image = py_touserdata(argv);
|
cp_image_t* image = py_touserdata(argv);
|
||||||
@ -161,35 +173,28 @@ static bool cute_png_Image__to_png_bytes(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
static bool cute_png_Image__to_png_file(int argc, py_Ref argv) {
|
static bool cute_png_Image__to_png_file(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(2);
|
PY_CHECK_ARGC(2);
|
||||||
|
PY_CHECK_ARG_TYPE(1, tp_str);
|
||||||
cp_image_t* image = py_touserdata(argv);
|
cp_image_t* image = py_touserdata(argv);
|
||||||
size_t size = 0;
|
const char* path = py_tostr(py_arg(1));
|
||||||
|
FILE* fp = fopen(path, "wb");
|
||||||
py_Type tp_FileIO = py_gettype("io", py_name("FileIO"));
|
if(fp == NULL) return OSError("cannot open file '%s' for writing", path);
|
||||||
if(tp_FileIO != tp_nil) {
|
|
||||||
PY_CHECK_ARG_TYPE(1, tp_FileIO);
|
|
||||||
FILE** ud = py_touserdata(py_arg(1));
|
|
||||||
FILE* fp = *ud;
|
|
||||||
|
|
||||||
cp_saved_png_t saved_image = cp_save_png_to_memory(image);
|
cp_saved_png_t saved_image = cp_save_png_to_memory(image);
|
||||||
assert(saved_image.data != NULL);
|
assert(saved_image.data != NULL);
|
||||||
size = fwrite(saved_image.data, saved_image.size, 1, fp);
|
size_t size = fwrite(saved_image.data, saved_image.size, 1, fp);
|
||||||
CUTE_PNG_FREE(saved_image.data);
|
CUTE_PNG_FREE(saved_image.data);
|
||||||
}
|
fclose(fp);
|
||||||
py_newint(py_retval(), size);
|
py_newint(py_retval(), size);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool cute_png_Image__to_rgb565_file(int argc, py_Ref argv) {
|
static bool cute_png_Image__to_rgb565_file(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(2);
|
PY_CHECK_ARGC(2);
|
||||||
|
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));
|
||||||
|
FILE* fp = fopen(path, "wb");
|
||||||
|
if(fp == NULL) return OSError("cannot open file '%s' for writing", path);
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
|
||||||
py_Type tp_FileIO = py_gettype("io", py_name("FileIO"));
|
|
||||||
if(tp_FileIO != tp_nil) {
|
|
||||||
PY_CHECK_ARG_TYPE(1, tp_FileIO);
|
|
||||||
FILE** ud = py_touserdata(py_arg(1));
|
|
||||||
FILE* fp = *ud;
|
|
||||||
|
|
||||||
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;
|
||||||
@ -199,7 +204,11 @@ static bool cute_png_Image__to_rgb565_file(int argc, py_Ref argv) {
|
|||||||
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
|
// use little-endian
|
||||||
size += fwrite(&rgb565, 1, 2, fp);
|
size_t delta = fwrite(&rgb565, 1, 2, fp);
|
||||||
|
size += delta;
|
||||||
|
if(delta != 2) {
|
||||||
|
py_newint(py_retval(), size);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -217,6 +226,7 @@ void pk__add_module_cute_png() {
|
|||||||
|
|
||||||
py_bindmethod(tp_image, "__new__", cute_png_Image__new__);
|
py_bindmethod(tp_image, "__new__", cute_png_Image__new__);
|
||||||
py_bindstaticmethod(tp_image, "from_bytes", cute_png_Image__from_bytes_STATIC);
|
py_bindstaticmethod(tp_image, "from_bytes", cute_png_Image__from_bytes_STATIC);
|
||||||
|
py_bindstaticmethod(tp_image, "from_file", cute_png_Image__from_file_STATIC);
|
||||||
|
|
||||||
py_bindproperty(tp_image, "width", cute_png_Image__width, NULL);
|
py_bindproperty(tp_image, "width", cute_png_Image__width, NULL);
|
||||||
py_bindproperty(tp_image, "height", cute_png_Image__height, NULL);
|
py_bindproperty(tp_image, "height", cute_png_Image__height, NULL);
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
from array2d import array2d
|
from array2d import array2d
|
||||||
from vmath import color32
|
from vmath import color32
|
||||||
from io import FileIO
|
|
||||||
|
|
||||||
class Image:
|
class Image:
|
||||||
@property
|
@property
|
||||||
@ -12,14 +11,16 @@ class Image:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_bytes(data: bytes) -> "Image": ...
|
def from_bytes(data: bytes) -> "Image": ...
|
||||||
|
@staticmethod
|
||||||
|
def from_file(path: str) -> "Image": ...
|
||||||
|
|
||||||
def setpixel(self, x: int, y: int, color: color32) -> None: ...
|
def setpixel(self, x: int, y: int, color: color32) -> None: ...
|
||||||
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_png_bytes(self) -> bytes: ...
|
def to_png_bytes(self) -> bytes: ...
|
||||||
def to_png_file(self, file: FileIO) -> int: ...
|
def to_png_file(self, path: str) -> int: ...
|
||||||
def to_rgb565_file(self, file: FileIO) -> int: ...
|
def to_rgb565_file(self, path: str) -> int: ...
|
||||||
|
|
||||||
|
|
||||||
def loads(data: bytes) -> array2d[color32]: ...
|
def loads(data: bytes) -> array2d[color32]: ...
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user