mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-15 18:19:47 +00:00
Trying to make the decoder work
This commit is contained in:
@@ -3,7 +3,7 @@ CODEC_DIR := node_modules/basis
|
|||||||
BUILD_DIR := build
|
BUILD_DIR := build
|
||||||
ENVIRONMENT = worker
|
ENVIRONMENT = worker
|
||||||
|
|
||||||
OUT_JS := enc/basis_enc.js # enc/basis_node_enc.js
|
OUT_JS := enc/basis_enc.js dec/basis_dec.js
|
||||||
OUT_WASM := $(OUT_JS:.js=.wasm)
|
OUT_WASM := $(OUT_JS:.js=.wasm)
|
||||||
|
|
||||||
override CXXFLAGS += -O3
|
override CXXFLAGS += -O3
|
||||||
@@ -43,13 +43,14 @@ all: $(CODEC_DIR) $(OUT_JS)
|
|||||||
|
|
||||||
# Define dependencies for all variations of build artifacts.
|
# Define dependencies for all variations of build artifacts.
|
||||||
$(filter enc/%,$(OUT_JS)): enc/basis_enc.cpp
|
$(filter enc/%,$(OUT_JS)): enc/basis_enc.cpp
|
||||||
# $(filter dec/%,$(OUT_JS)): dec/mozjpeg_dec.cpp
|
$(filter dec/%,$(OUT_JS)): dec/basis_dec.cpp
|
||||||
|
|
||||||
# enc/mozjpeg_node_enc.js dec/mozjpeg_node_dec.js: ENVIRONMENT = node
|
# enc/mozjpeg_node_enc.js dec/mozjpeg_node_dec.js: ENVIRONMENT = node
|
||||||
|
|
||||||
%.js: $(CODEC_CPP_OBJECT_FILE_PATHS) $(CODEC_C_OBJECT_FILE_PATHS)
|
%.js: $(CODEC_CPP_OBJECT_FILE_PATHS) $(CODEC_C_OBJECT_FILE_PATHS)
|
||||||
$(CXX) \
|
$(CXX) \
|
||||||
-I $(CODEC_DIR)/encoder \
|
-I $(CODEC_DIR)/encoder \
|
||||||
|
-I $(CODEC_DIR)/transcoder \
|
||||||
${CXXFLAGS} \
|
${CXXFLAGS} \
|
||||||
${LDFLAGS} \
|
${LDFLAGS} \
|
||||||
--closure 1 \
|
--closure 1 \
|
||||||
|
|||||||
24
codecs/basis/asdf.html
Normal file
24
codecs/basis/asdf.html
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<input type="file" id="file">
|
||||||
|
<button id="go">Go</button>
|
||||||
|
<script type="module">
|
||||||
|
import factory from "./enc/basis_enc.js";
|
||||||
|
|
||||||
|
const {file, go} = document.all;
|
||||||
|
async function main() {
|
||||||
|
const instance = await factory();
|
||||||
|
const bitmap = await createImageBitmap(file.files[0]);
|
||||||
|
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
ctx.drawImage(bitmap, 0, 0);
|
||||||
|
const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
||||||
|
const result = instance.encode(imgData.data, imgData.width, imgData.height);
|
||||||
|
const blob = new File([result], "lol.basis");
|
||||||
|
const u = URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement("a")
|
||||||
|
a.download = "lol.basis";
|
||||||
|
a.href = u;
|
||||||
|
a.click();
|
||||||
|
}
|
||||||
|
go.onclick = main;
|
||||||
|
</script>
|
||||||
60
codecs/basis/dec/basis_dec.cpp
Normal file
60
codecs/basis/dec/basis_dec.cpp
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#include <emscripten/bind.h>
|
||||||
|
#include <emscripten/val.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include "basisu_global_selector_palette.h"
|
||||||
|
#include "basisu_transcoder.h"
|
||||||
|
|
||||||
|
using namespace emscripten;
|
||||||
|
using namespace basisu;
|
||||||
|
|
||||||
|
thread_local const val Uint8ClampedArray = val::global("Uint8ClampedArray");
|
||||||
|
thread_local const val ImageData = val::global("ImageData");
|
||||||
|
|
||||||
|
val decode(std::string data) {
|
||||||
|
basist::basisu_transcoder_init();
|
||||||
|
|
||||||
|
basist::etc1_global_selector_codebook sel_codebook = basist::etc1_global_selector_codebook(
|
||||||
|
basist::g_global_selector_cb_size, basist::g_global_selector_cb);
|
||||||
|
// basis_data data = basis_data(sel_codebook);
|
||||||
|
basist::basisu_transcoder transcoder = basist::basisu_transcoder(&sel_codebook);
|
||||||
|
|
||||||
|
const void* dataPtr = reinterpret_cast<const void*>(data.c_str());
|
||||||
|
auto dataSize = data.size();
|
||||||
|
basist::basisu_image_info info;
|
||||||
|
transcoder.get_image_info(dataPtr, dataSize, info, 0 /* image_index */);
|
||||||
|
|
||||||
|
auto buffer = std::vector<uint8_t>(info.m_width * info.m_height * 4);
|
||||||
|
auto ok = transcoder.transcode_image_level(dataPtr, dataSize,
|
||||||
|
/* image_index */ 0, /* level_index */ 0,
|
||||||
|
buffer.data(), buffer.size() / 4,
|
||||||
|
basist::transcoder_texture_format::cTFRGBA32);
|
||||||
|
if (!ok) {
|
||||||
|
return val(std::string("Could not decode"));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto img_data_data = Uint8ClampedArray.new_(typed_memory_view(buffer.size(), buffer.data()));
|
||||||
|
auto imgData = ImageData.new_(img_data_data, info.m_width, info.m_height);
|
||||||
|
return imgData;
|
||||||
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_BINDINGS(my_module) {
|
||||||
|
// value_object<MozJpegOptions>("MozJpegOptions")
|
||||||
|
// .field("quality", &MozJpegOptions::quality)
|
||||||
|
// .field("baseline", &MozJpegOptions::baseline)
|
||||||
|
// .field("arithmetic", &MozJpegOptions::arithmetic)
|
||||||
|
// .field("progressive", &MozJpegOptions::progressive)
|
||||||
|
// .field("optimize_coding", &MozJpegOptions::optimize_coding)
|
||||||
|
// .field("smoothing", &MozJpegOptions::smoothing)
|
||||||
|
// .field("color_space", &MozJpegOptions::color_space)
|
||||||
|
// .field("quant_table", &MozJpegOptions::quant_table)
|
||||||
|
// .field("trellis_multipass", &MozJpegOptions::trellis_multipass)
|
||||||
|
// .field("trellis_opt_zero", &MozJpegOptions::trellis_opt_zero)
|
||||||
|
// .field("trellis_opt_table", &MozJpegOptions::trellis_opt_table)
|
||||||
|
// .field("trellis_loops", &MozJpegOptions::trellis_loops)
|
||||||
|
// .field("chroma_subsample", &MozJpegOptions::chroma_subsample)
|
||||||
|
// .field("auto_subsample", &MozJpegOptions::auto_subsample)
|
||||||
|
// .field("separate_chroma_quality", &MozJpegOptions::separate_chroma_quality)
|
||||||
|
// .field("chroma_quality", &MozJpegOptions::chroma_quality);
|
||||||
|
|
||||||
|
function("decode", &decode);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user