Create Uint8ClampedArray from C++

This commit is contained in:
Ingvar Stepanyan
2020-05-14 16:52:47 +01:00
committed by Ingvar Stepanyan
parent c39383333f
commit 1f35c40d3f
3 changed files with 16 additions and 41 deletions

View File

@@ -14,16 +14,9 @@ int version() {
(((LIQ_VERSION / 1) % 100) << 0); (((LIQ_VERSION / 1) % 100) << 0);
} }
class RawImage { const val Uint8ClampedArray = val::global("Uint8ClampedArray");
public:
val buffer;
int width;
int height;
RawImage(val b, int w, int h) : buffer(b), width(w), height(h) {} val quantize(std::string rawimage,
};
RawImage quantize(std::string rawimage,
int image_width, int image_width,
int image_height, int image_height,
int num_colors, int num_colors,
@@ -51,8 +44,9 @@ RawImage quantize(std::string rawimage,
liq_result_destroy(res); liq_result_destroy(res);
liq_image_destroy(image); liq_image_destroy(image);
liq_attr_destroy(attr); liq_attr_destroy(attr);
return {val(typed_memory_view(image_width * image_height * 4, result)), image_width, val js_result = Uint8ClampedArray.new_(typed_memory_view(image_width * image_height * 4, result));
image_height}; free(result);
return js_result;
} }
const liq_color zx_colors[] = { const liq_color zx_colors[] = {
@@ -78,7 +72,7 @@ const liq_color zx_colors[] = {
* two colours must both be 'regular' or 'bright'. Black exists as both regular * two colours must both be 'regular' or 'bright'. Black exists as both regular
* and bright. * and bright.
*/ */
RawImage zx_quantize(std::string rawimage, int image_width, int image_height, float dithering) { val zx_quantize(std::string rawimage, int image_width, int image_height, float dithering) {
const uint8_t* image_buffer = (uint8_t*)rawimage.c_str(); const uint8_t* image_buffer = (uint8_t*)rawimage.c_str();
int size = image_width * image_height; int size = image_width * image_height;
int bytes_per_pixel = 4; int bytes_per_pixel = 4;
@@ -194,7 +188,7 @@ RawImage zx_quantize(std::string rawimage, int image_width, int image_height, fl
liq_set_max_colors(attr, 2); liq_set_max_colors(attr, 2);
liq_image_add_fixed_color(image, zx_colors[first_color_index]); liq_image_add_fixed_color(image, zx_colors[first_color_index]);
liq_image_add_fixed_color(image, zx_colors[second_color_index]); liq_image_add_fixed_color(image, zx_colors[second_color_index]);
liq_result *res = nullptr; liq_result* res = nullptr;
liq_image_quantize(image, attr, &res); liq_image_quantize(image, attr, &res);
liq_set_dithering_level(res, dithering); liq_set_dithering_level(res, dithering);
liq_write_remapped_image(res, image, image8bit, size); liq_write_remapped_image(res, image, image8bit, size);
@@ -221,22 +215,13 @@ RawImage zx_quantize(std::string rawimage, int image_width, int image_height, fl
} }
free(image8bit); free(image8bit);
return {val(typed_memory_view(image_width * image_height * 4, result)), image_width, val js_result = Uint8ClampedArray.new_(typed_memory_view(size * 4, result));
image_height};
}
void free_result(uint8_t* result) {
free(result); free(result);
return js_result;
} }
EMSCRIPTEN_BINDINGS(my_module) { EMSCRIPTEN_BINDINGS(my_module) {
class_<RawImage>("RawImage")
.property("buffer", &RawImage::buffer)
.property("width", &RawImage::width)
.property("height", &RawImage::height);
function("quantize", &quantize); function("quantize", &quantize);
function("zx_quantize", &zx_quantize); function("zx_quantize", &zx_quantize);
function("version", &version); function("version", &version);
function("free_result", &free_result, allow_raw_pointers());
} }

View File

@@ -1,12 +1,6 @@
interface RawImage {
buffer: Uint8Array;
width: number;
height: number;
}
interface QuantizerModule extends EmscriptenWasm.Module { interface QuantizerModule extends EmscriptenWasm.Module {
quantize(data: BufferSource, width: number, height: number, numColors: number, dither: number): RawImage; quantize(data: BufferSource, width: number, height: number, numColors: number, dither: number): Uint8ClampedArray;
zx_quantize(data: BufferSource, width: number, height: number, dither: number): RawImage; zx_quantize(data: BufferSource, width: number, height: number, dither: number): Uint8ClampedArray;
free_result(ptr: number): void; free_result(ptr: number): void;
} }

View File

@@ -15,9 +15,5 @@ export async function process(data: ImageData, opts: QuantizeOptions): Promise<I
: :
module.quantize(data.data, data.width, data.height, opts.maxNumColors, opts.dither); module.quantize(data.data, data.width, data.height, opts.maxNumColors, opts.dither);
const imgData = new ImageData(new Uint8ClampedArray(result.buffer), result.width, result.height); return new ImageData(new Uint8ClampedArray(result), data.width, data.height);
module.free_result(imgData.data.byteOffset);
return imgData;
} }