Skip to content

Commit

Permalink
Add LoadImageColors and LoadImagePalette
Browse files Browse the repository at this point in the history
  • Loading branch information
baudaux committed Nov 6, 2024
1 parent 6bfdad9 commit 7941a72
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 26 deletions.
12 changes: 6 additions & 6 deletions api/raylib_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -7490,7 +7490,7 @@
{
"name": "LoadImageColors",
"description": "Load color data from image as a Color array (RGBA - 32bit)",
"returnType": "Color *",
"returnType": "Color[]",
"params": [
{
"type": "Image",
Expand All @@ -7501,7 +7501,7 @@
{
"name": "LoadImagePalette",
"description": "Load colors palette from image as a Color array (RGBA - 32bit)",
"returnType": "Color *",
"returnType": "Color[]",
"params": [
{
"type": "Image",
Expand All @@ -7523,7 +7523,7 @@
"returnType": "void",
"params": [
{
"type": "Color *",
"type": "Color[]",
"name": "colors"
}
]
Expand All @@ -7534,7 +7534,7 @@
"returnType": "void",
"params": [
{
"type": "Color *",
"type": "Color[]",
"name": "colors"
}
]
Expand Down Expand Up @@ -11157,7 +11157,7 @@
{
"name": "LoadWaveSamples",
"description": "Load samples data from wave as a 32bit float data array",
"returnType": "float *",
"returnType": "float[]",
"params": [
{
"type": "Wave",
Expand All @@ -11171,7 +11171,7 @@
"returnType": "void",
"params": [
{
"type": "float *",
"type": "float[]",
"name": "samples"
}
]
Expand Down
129 changes: 111 additions & 18 deletions src/raylib_qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4079,9 +4079,6 @@ static JSValue js_Shader_get(JSContext *ctx, JSValueConst this_val, int magic)
if (!s)
return JS_EXCEPTION;

//BB
emscripten_log(EM_LOG_CONSOLE, "js_Shader_get: %d", magic);


if (magic == 0) {
return JS_NewUint32(ctx, (unsigned int) s->id);
Expand All @@ -4098,8 +4095,6 @@ static JSValue js_Shader_set(JSContext *ctx, JSValueConst this_val, JSValue val,
if (!s)
return JS_EXCEPTION;

//BB
emscripten_log(EM_LOG_CONSOLE, "js_Shader_set: %d", magic);

if (magic == 0) {
unsigned int v0;
Expand Down Expand Up @@ -8084,7 +8079,7 @@ static JSValue js_SetShaderValue(JSContext *ctx, JSValueConst this_val, int argc

int arg3;
JS_ToInt32(ctx, &arg3, argv[3]);

switch(arg3) {

case 0: //float
Expand All @@ -8108,12 +8103,11 @@ static JSValue js_SetShaderValue(JSContext *ctx, JSValueConst this_val, int argc

break;
}

default:
break;
}

return JS_UNDEFINED;


}

static JSValue js_SetShaderValueV(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
Expand Down Expand Up @@ -8690,6 +8684,7 @@ const char * arg0 = (const char *)JS_ToCString(ctx, argv[0]);
}

return ret;

}

static JSValue js_SaveFileData(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
Expand Down Expand Up @@ -15557,6 +15552,89 @@ static JSValue js_GenImageText(JSContext *ctx, JSValueConst this_val, int argc,

}

static JSValue js_LoadImageColors(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{

Image * argptr0 = (Image *)JS_GetOpaque2(ctx, argv[0], js_Image_class_id);
if (argptr0 == NULL) return JS_EXCEPTION;

Color * colors = LoadImageColors(*argptr0);

JSValue ret = JS_NewArray(ctx);

for(int i = 0; i < argptr0->width*argptr0->height; i++) {

JSValue col = JS_NewObjectClass(ctx, js_Color_class_id);
JS_SetOpaque(col, &colors[i]);

JS_SetPropertyUint32(ctx, ret, i, col);
}

return ret;

}

static JSValue js_LoadImagePalette(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{

Image * argptr0 = (Image *)JS_GetOpaque2(ctx, argv[0], js_Image_class_id);
if (argptr0 == NULL) return JS_EXCEPTION;

int arg1;
JS_ToInt32(ctx, &arg1, argv[1]);

JSValue arg2_js = JS_GetPropertyStr(ctx, argv[2], "colorCount");
int arg2;
JS_ToInt32(ctx, &arg2, arg2_js);

Color * colors = LoadImagePalette(*argptr0, arg1, &arg2);

JSValue ret = JS_NewArray(ctx);

for(int i = 0; i < arg2; i++) {

JSValue col = JS_NewObjectClass(ctx, js_Color_class_id);
JS_SetOpaque(col, &colors[i]);

JS_SetPropertyUint32(ctx, ret, i, col);
}

JS_SetPropertyStr(ctx, argv[2], "colorCount", JS_NewInt32(ctx, arg2));

return ret;

}

static JSValue js_UnloadImageColors(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{


// Get pointer to first element
JSValue val = JS_GetPropertyUint32(ctx, argv[0], 0);

Color * ptr = (Color *)JS_GetOpaque2(ctx, val, js_Color_class_id);

UnloadImageColors(ptr);

return JS_UNDEFINED;

}

static JSValue js_UnloadImagePalette(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{


// Get pointer to first element
JSValue val = JS_GetPropertyUint32(ctx, argv[0], 0);

Color * ptr = (Color *)JS_GetOpaque2(ctx, val, js_Color_class_id);

UnloadImagePalette(ptr);

return JS_UNDEFINED;

}

static JSValue js_GetImageAlphaBorder(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
Image * argptr0 = (Image *)JS_GetOpaque2(ctx, argv[0], js_Image_class_id);
Expand Down Expand Up @@ -21430,6 +21508,8 @@ static JSValue js_LoadMaterials(JSContext *ctx, JSValueConst this_val, int argc,
JS_SetPropertyUint32(ctx, ret, i, obj);
}

JS_SetPropertyStr(ctx, argv[1], "materialCount", JS_NewInt32(ctx, arg1));

return ret;


Expand Down Expand Up @@ -21578,6 +21658,7 @@ static JSValue js_UnloadModelAnimations(JSContext *ctx, JSValueConst this_val, i
{


// Get pointer to first element
JSValue val = JS_GetPropertyUint32(ctx, argv[0], 0);

ModelAnimation * ptr = (ModelAnimation *)JS_GetOpaque2(ctx, val, js_ModelAnimation_class_id);
Expand Down Expand Up @@ -22494,17 +22575,25 @@ static JSValue js_WaveFormat(JSContext *ctx, JSValueConst this_val, int argc, JS

static JSValue js_LoadWaveSamples(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
Wave * argptr0 = (Wave *)JS_GetOpaque2(ctx, argv[0], js_Wave_class_id);
if (argptr0 == NULL) return JS_EXCEPTION;


Wave * argptr0 = (Wave *)JS_GetOpaque2(ctx, argv[0], js_Wave_class_id);
if (argptr0 == NULL) return JS_EXCEPTION;

Wave arg0 = *argptr0;
Wave arg0 = *argptr0;

float * retBuf = LoadWaveSamples(arg0);
float * retVal = LoadWaveSamples(arg0);

JSValue ret = JS_NULL;

if (retVal) {

JSValue ret = JS_NewArray(ctx);
//TODO
return ret;
ret = JS_NewArrayBufferCopy(ctx, (const uint8_t*)retVal, argptr0->frameCount*argptr0->channels*sizeof(float));

UnloadWaveSamples(retVal);
}

return ret;

}

Expand Down Expand Up @@ -30657,6 +30746,10 @@ static const JSCFunctionListEntry js_raylib_funcs[] = {
JS_CFUNC_DEF("GenImagePerlinNoise", 5, js_GenImagePerlinNoise ),
JS_CFUNC_DEF("GenImageCellular", 3, js_GenImageCellular ),
JS_CFUNC_DEF("GenImageText", 3, js_GenImageText ),
JS_CFUNC_DEF("LoadImageColors", 1, js_LoadImageColors ),
JS_CFUNC_DEF("LoadImagePalette", 3, js_LoadImagePalette ),
JS_CFUNC_DEF("UnloadImageColors", 1, js_UnloadImageColors ),
JS_CFUNC_DEF("UnloadImagePalette", 1, js_UnloadImagePalette ),
JS_CFUNC_DEF("GetImageAlphaBorder", 2, js_GetImageAlphaBorder ),
JS_CFUNC_DEF("GetImageColor", 3, js_GetImageColor ),
JS_CFUNC_DEF("LoadTexture", 1, js_LoadTexture ),
Expand Down Expand Up @@ -32094,4 +32187,4 @@ JSModuleDef *js_init_module(JSContext *ctx, const char *module_name)


return m;
}
}
Loading

0 comments on commit 7941a72

Please sign in to comment.