Bitmap/PPM conversion through a pipe: Difference between revisions

Added Wren
(Added Wren)
Line 487:
set img [image create photo -filename filename.ppm]
output_jpeg $img filename.jpg</lang>
 
=={{header|Wren}}==
{{libheader|DOME}}
As DOME doesn't have a method for calling an external process (''ImageMagick'' in this case), we first need to create a small plug-in in C to add this functionality.
<lang c>/* gcc -O3 -std=c11 -shared -o pipeconv.so -fPIC -I./include pipeconv.c */
 
#include <stdlib.h>
#include <string.h>
#include "dome.h"
 
static DOME_API_v0* core;
static WREN_API_v0* wren;
 
static const char* source = ""
"class PipeConv {\n"
"foreign static convert(from, to) \n"
"} \n";
 
void C_convert(WrenVM* vm) {
const char *from = wren->getSlotString(vm, 1);
const char *to = wren->getSlotString(vm, 2);
char command[strlen(from) + strlen(to) + 10];
strcpy(command, "convert ");
strcat(command, from);
strcat(command, " ");
strcat(command, to);
int res = system(command);
}
 
DOME_EXPORT DOME_Result PLUGIN_onInit(DOME_getAPIFunction DOME_getAPI, DOME_Context ctx) {
core = DOME_getAPI(API_DOME, DOME_API_VERSION);
wren = DOME_getAPI(API_WREN, WREN_API_VERSION);
core->registerModule(ctx, "pipeconv", source);
core->registerClass(ctx, "pipeconv", "PipeConv", NULL, NULL);
core->registerFn(ctx, "pipeconv", "static PipeConv.convert(_,_)", C_convert);
return DOME_RESULT_SUCCESS;
}
 
DOME_EXPORT DOME_Result PLUGIN_preUpdate(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
 
DOME_EXPORT DOME_Result PLUGIN_postUpdate(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
 
DOME_EXPORT DOME_Result PLUGIN_preDraw(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
 
DOME_EXPORT DOME_Result PLUGIN_postDraw(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
 
DOME_EXPORT DOME_Result PLUGIN_onShutdown(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}</lang>
This assumes that the ''dome.h'' header file is copied to an ''include'' sub-directory of the current one and that the resulting ''pipeconv.so'' shared library file is created in the latter.
 
We can now use this plug-in in the following script which calls ''ImageMagick'' to convert the ''output.ppm'' file to a ''jpg'' file and then loads the latter and displays it.
 
<lang ecmascript>import "graphics" for Canvas, ImageData
import "dome" for Window
import "plugin" for Plugin
 
Plugin.load("pipeconv")
 
import "pipeconv" for PipeConv
 
class ConvertPPM {
construct new(fileName, fileName2, width, height) {
Window.resize(width, height)
Canvas.resize(width, height)
Window.title = "Convert PPM file via pipe"
// convert .ppm file to .jpg via a pipe
PipeConv.convert(fileName, fileName2)
// load and display .jpg file
var image = ImageData.loadFromFile(fileName2)
image.draw(0, 0)
}
 
init() {}
 
update() {}
 
draw(alpha) {}
}
 
var Game = ConvertPPM.new("output.ppm", "output_piped.jpg", 350, 350)</lang>
 
=={{header|zkl}}==
9,482

edits