Add test to JXL decoder

This commit is contained in:
Surma
2020-02-06 13:59:53 -08:00
committed by Ingvar Stepanyan
parent de1eb76226
commit 3697635bae
6 changed files with 44 additions and 9 deletions

View File

@@ -28,7 +28,7 @@ test -n "$SKIP_LIBJXL" || (
mkdir -p build
cd build
emcmake cmake ../
emmake make jpegxl-static brunslidec-static brunslicommon-static brunslienc-static
emmake make jpegxl-static
)
echo "============================================="

View File

@@ -1,7 +1,7 @@
<!doctype html>
<script src='avif_dec.js'></script>
<script src='jxl_dec.js'></script>
<script>
const Module = avif_dec();
const Module = jxl_dec();
async function loadFile(src) {
const resp = await fetch(src);
@@ -9,7 +9,7 @@
}
Module.onRuntimeInitialized = async _ => {
const image = await loadFile('../example.avif');
const image = await loadFile('../example.jxl');
const result = Module.decode(image);
console.log(result.width, result.height, result.buffer);
const imageData = new ImageData(new Uint8ClampedArray(result.buffer), result.width, result.height);

Binary file not shown.

View File

@@ -14,7 +14,18 @@ public:
RawImage(val b, int w, int h) : buffer(b), width(w), height(h) {}
};
val decode(std::string data) {
uint8_t clamp(float v, float min, float max) {
if (v < min) {
return min;
}
if (v > max) {
return max;
}
return v;
}
uint8_t *result;
RawImage decode(std::string data) {
jxl::Span<const uint8_t> compressed((uint8_t *)data.c_str(), data.length());
jxl::DecompressParams dparams;
// jxl::ThreadPool pool;
@@ -22,12 +33,35 @@ val decode(std::string data) {
jxl::AuxOut aux_out;
if (!DecodeFile(dparams, compressed, &io, &aux_out, NULL)) {
printf("FAIL\n");
return RawImage(val::null(), -1, -1);
}
printf("YAY\n");
return val(1);
jxl::ImageBundle *main = &io.Main();
if (!main->HasColor()) {
return RawImage(val::null(), -1, -1);
}
const jxl::Image3F *buffer = &main->color();
int width = buffer->xsize();
int height = buffer->ysize();
result = new uint8_t[width * height * 4];
for (int y = 0; y < height; y++) {
const float *red = buffer->PlaneRow(0, y);
const float *green = buffer->PlaneRow(1, y);
const float *blue = buffer->PlaneRow(2, y);
for (int x = 0; x < width; x++) {
int pixelOffset = width * y + x;
result[pixelOffset * 4 + 0] = clamp(red[x], 0, 255);
result[pixelOffset * 4 + 1] = clamp(green[x], 0, 255);
result[pixelOffset * 4 + 2] = clamp(blue[x], 0, 255);
result[pixelOffset * 4 + 3] = 255;
}
}
return RawImage(val(typed_memory_view(width * height * 4, result)), width,
height);
}
void free_result() { delete result; }
EMSCRIPTEN_BINDINGS(my_module) {
class_<RawImage>("RawImage")
.property("buffer", &RawImage::buffer)
@@ -35,4 +69,5 @@ EMSCRIPTEN_BINDINGS(my_module) {
.property("height", &RawImage::height);
function("decode", &decode);
function("free_result", &free_result);
}

File diff suppressed because one or more lines are too long

Binary file not shown.