Files
cell-image/psd.c
2026-02-21 03:07:55 -06:00

49 lines
1.6 KiB
C

#include "cell.h"
#include <string.h>
#include <stdlib.h>
#include "stb_image_common.h"
// PSD decoding (decode only - no encode support in stb_image_write)
JSValue js_psd_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 PSD 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 PSD: %s", stbi_failure_reason());
if (width <= 0 || height <= 0) {
stbi_image_free(data);
return JS_ThrowReferenceError(js, "decoded PSD 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);
}
static const JSCFunctionListEntry js_psd_funcs[] = {
MIST_FUNC_DEF(psd, decode, 1)
};
CELL_USE_FUNCS(js_psd_funcs)