#include "cell.h" #include #include #include "stb_image_common.h" // PNG decoding JSValue js_png_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv) { size_t len; void *raw = js_get_blob_data(js, &len, argv[0]); if (raw == NULL) return JS_EXCEPTION; if (!raw) return JS_ThrowReferenceError(js, "could not get PNG data from blob"); int n, width, height; void *data = stbi_load_from_memory(raw, len, &width, &height, &n, 4); if (!data) return JS_ThrowReferenceError(js, "failed to decode PNG: %s", stbi_failure_reason()); if (width <= 0 || height <= 0) { stbi_image_free(data); return JS_ThrowReferenceError(js, "decoded PNG has invalid size: %dx%d", width, height); } int pitch = width * 4; size_t pixels_size = pitch * height; JS_FRAME(js); JS_ROOT(obj, JS_NewObject(js)); JS_SetPropertyStr(js, obj.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, obj.val, "height", JS_NewInt32(js, height)); JSValue fmt_str = JS_NewString(js, "rgba32"); JS_SetPropertyStr(js, obj.val, "format", fmt_str); JS_SetPropertyStr(js, obj.val, "pitch", JS_NewInt32(js, pitch)); JSValue pixels_blob = js_new_blob_stoned_copy(js, data, pixels_size); JS_SetPropertyStr(js, obj.val, "pixels", pixels_blob); JS_SetPropertyStr(js, obj.val, "depth", JS_NewInt32(js, 8)); JS_SetPropertyStr(js, obj.val, "hdr", JS_NewBool(js, 0)); stbi_image_free(data); JS_RETURN(obj.val); } // PNG encoding JSValue js_png_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv) { if (argc < 1) return JS_ThrowTypeError(js, "png.encode requires an image object"); JSValue width_val = JS_GetPropertyStr(js, argv[0], "width"); JSValue height_val = JS_GetPropertyStr(js, argv[0], "height"); if (JS_IsNull(width_val) || JS_IsNull(height_val)) { return JS_ThrowTypeError(js, "png.encode requires width and height properties"); } int width, height; if (JS_ToInt32(js, &width, width_val) < 0 || JS_ToInt32(js, &height, height_val) < 0) { return JS_ThrowTypeError(js, "width and height must be numbers"); } if (width < 1 || height < 1) return JS_ThrowRangeError(js, "width and height must be at least 1"); JSValue pixels_val = JS_GetPropertyStr(js, argv[0], "pixels"); size_t pixel_len; void *pixel_data = js_get_blob_data(js, &pixel_len, pixels_val); if (pixel_data == NULL) return JS_EXCEPTION; if (!pixel_data) return JS_ThrowTypeError(js, "pixels blob has no data"); size_t required_size = width * height * 4; if (pixel_len < required_size) return JS_ThrowRangeError(js, "pixels buffer too small (need %zu, got %zu)", required_size, pixel_len); stbi_mem_buf buf = {0}; stbi_write_png_to_func(stbi_mem_write, &buf, width, height, 4, pixel_data, width * 4); if (!buf.data || buf.size == 0) { if (buf.data) free(buf.data); return JS_ThrowInternalError(js, "failed to encode PNG"); } JS_FRAME(js); JS_ROOT(result, JS_NewObject(js)); JS_SetPropertyStr(js, result.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, result.val, "height", JS_NewInt32(js, height)); JSValue enc_fmt = JS_NewString(js, "png"); JS_SetPropertyStr(js, result.val, "format", enc_fmt); JSValue enc_blob = js_new_blob_stoned_copy(js, buf.data, buf.size); JS_SetPropertyStr(js, result.val, "pixels", enc_blob); free(buf.data); JS_RETURN(result.val); } static const JSCFunctionListEntry js_png_funcs[] = { MIST_FUNC_DEF(png, decode, 1), MIST_FUNC_DEF(png, encode, 1) }; CELL_USE_FUNCS(js_png_funcs)