Update for encoder API changes

This commit is contained in:
Surma
2020-08-17 18:21:09 +01:00
committed by Ingvar Stepanyan
parent b8d0d5202d
commit 18f7591c6a
7 changed files with 231 additions and 14616 deletions

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@@ -5,46 +5,53 @@
using namespace emscripten;
thread_local const val Uint8Array = val::global("Uint8Array");
struct JXLOptions {
// 1 = slowest
// 7 = fastest
int speed;
};
uint8_t *result;
val encode(std::string image, int width, int height, JXLOptions options) {
jxl::CompressParams cparams;
jxl::PassesEncoderState passes_enc_state;
jxl::CodecInOut io;
jxl::PaddedBytes bytes;
jxl::ImageBundle *main = &io.Main();
jxl::ImageBundle* main = &io.Main();
cparams.speed_tier = static_cast<jxl::SpeedTier>(options.speed);
cparams.color_transform = jxl::ColorTransform::kNone;
uint8_t *inBuffer = (uint8_t *)image.c_str();
io.metadata.SetUintSamples(8);
io.metadata.SetAlphaBits(8);
auto result = main->SetFromSRGB(
width, height, false, true, true, (uint8_t *)image.c_str(),
(uint8_t *)image.c_str() + width * height * 4);
uint8_t* inBuffer = (uint8_t*)image.c_str();
auto result =
main->SetFromSRGB(width, height, /*is_gray*/ false, /*has_alpha*/ true,
/*alpha_is_premultiplied*/ true, /*pixels*/ (uint8_t*)image.c_str(),
/* end */ (uint8_t*)image.c_str() + width * height * 4);
if (!result) {
return val::null();
}
if (!EncodeFile(cparams, &io, &passes_enc_state, &bytes)) {
return val::null();
auto js_result = val::null();
if (EncodeFile(cparams, &io, &passes_enc_state, &bytes)) {
js_result = Uint8Array.new_(typed_memory_view(bytes.size(), bytes.data()));
}
return val(typed_memory_view(bytes.size(), bytes.data()));
// TODO: Any freeing that needs to be done?
//
return js_result;
}
void free_result() { delete result; }
void free_result() {
}
EMSCRIPTEN_BINDINGS(my_module) {
value_object<JXLOptions>("JXLOptions")
.field("speed", &JXLOptions::speed);
value_object<JXLOptions>("JXLOptions").field("speed", &JXLOptions::speed);
function("encode", &encode);
function("free_result", &free_result);
}

View File

@@ -2,7 +2,6 @@ import { EncodeOptions } from '../../src/codecs/jxl/encoder-meta';
interface JXLModule extends EmscriptenWasm.Module {
encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array;
free_result(): void;
}

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@@ -11,8 +11,6 @@ export async function encode(data: ImageData, options: EncodeOptions): Promise<A
const module = await emscriptenModule;
console.log(options);
const resultView = module.encode(data.data, data.width, data.height, options);
const result = new Uint8Array(resultView);
module.free_result();
// wasm cant run on SharedArrayBuffers, so we hard-cast to ArrayBuffer.
return result.buffer as ArrayBuffer;