mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-14 09:39:15 +00:00
First compiling encoder
This commit is contained in:
1
codecs/basis/LICENSE.codec.md
Normal file
1
codecs/basis/LICENSE.codec.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
76
codecs/basis/Makefile
Normal file
76
codecs/basis/Makefile
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
CODEC_URL := https://github.com/BinomialLLC/basis_universal/archive/refs/tags/v1.15_rel2.tar.gz
|
||||||
|
CODEC_DIR := node_modules/basis
|
||||||
|
BUILD_DIR := build
|
||||||
|
ENVIRONMENT = node
|
||||||
|
|
||||||
|
OUT_JS := enc/basis_enc.js # enc/basis_node_enc.js
|
||||||
|
OUT_WASM := $(OUT_JS:.js=.wasm)
|
||||||
|
|
||||||
|
CODEC_CPP_SOURCE_FILES := \
|
||||||
|
encoder/basisu_comp.cpp \
|
||||||
|
encoder/basisu_enc.cpp \
|
||||||
|
encoder/basisu_backend.cpp \
|
||||||
|
encoder/basisu_basis_file.cpp \
|
||||||
|
encoder/basisu_etc.cpp \
|
||||||
|
encoder/basisu_uastc_enc.cpp \
|
||||||
|
encoder/basisu_gpu_texture.cpp \
|
||||||
|
encoder/basisu_frontend.cpp \
|
||||||
|
encoder/basisu_bc7enc.cpp \
|
||||||
|
encoder/basisu_pvrtc1_4.cpp \
|
||||||
|
encoder/basisu_astc_decomp.cpp \
|
||||||
|
encoder/basisu_global_selector_palette_helpers.cpp \
|
||||||
|
encoder/basisu_resampler.cpp \
|
||||||
|
encoder/jpgd.cpp \
|
||||||
|
encoder/lodepng.cpp \
|
||||||
|
transcoder/basisu_transcoder.cpp
|
||||||
|
|
||||||
|
CODEC_C_SOURCE_FILES := \
|
||||||
|
encoder/apg_bmp.c \
|
||||||
|
zstd/zstd.c
|
||||||
|
|
||||||
|
CODEC_CPP_OBJECT_FILES := $(CODEC_CPP_SOURCE_FILES:.cpp=.o)
|
||||||
|
CODEC_C_OBJECT_FILES := $(CODEC_C_SOURCE_FILES:.c=.o)
|
||||||
|
CODEC_CPP_OBJECT_FILE_PATHS := $(addprefix $(CODEC_DIR)/, $(CODEC_CPP_OBJECT_FILES))
|
||||||
|
CODEC_C_OBJECT_FILE_PATHS := $(addprefix $(CODEC_DIR)/, $(CODEC_C_OBJECT_FILES))
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
|
|
||||||
|
all: $(OUT_JS)
|
||||||
|
|
||||||
|
# Define dependencies for all variations of build artifacts.
|
||||||
|
$(filter enc/%,$(OUT_JS)): enc/basis_enc.cpp
|
||||||
|
# $(filter dec/%,$(OUT_JS)): dec/mozjpeg_dec.cpp
|
||||||
|
|
||||||
|
# enc/mozjpeg_node_enc.js dec/mozjpeg_node_dec.js: ENVIRONMENT = node
|
||||||
|
|
||||||
|
%.js: $(CODEC_CPP_OBJECT_FILE_PATHS) $(CODEC_C_OBJECT_FILE_PATHS)
|
||||||
|
$(CXX) \
|
||||||
|
-I $(CODEC_DIR)/encoder \
|
||||||
|
${CXXFLAGS} \
|
||||||
|
${LDFLAGS} \
|
||||||
|
--closure 1 \
|
||||||
|
--bind \
|
||||||
|
-s ALLOW_MEMORY_GROWTH=1 \
|
||||||
|
-s MODULARIZE=1 \
|
||||||
|
-s TEXTDECODER=2 \
|
||||||
|
-s ENVIRONMENT=$(ENVIRONMENT) \
|
||||||
|
-s EXPORT_ES6=1 \
|
||||||
|
-o $@ \
|
||||||
|
$+
|
||||||
|
|
||||||
|
$(CODEC_CPP_OBJECT_FILES): $(CODEC_DIR)
|
||||||
|
$(CXX) \
|
||||||
|
-o $@ \
|
||||||
|
-c $(@:.o=.cpp)
|
||||||
|
|
||||||
|
$(CODEC_C_OBJECT_FILES): $(CODEC_DIR)
|
||||||
|
$(CC) \
|
||||||
|
-o $@ \
|
||||||
|
-c $(@:.o=.c)
|
||||||
|
|
||||||
|
$(CODEC_DIR):
|
||||||
|
mkdir -p $@
|
||||||
|
curl -sL $(CODEC_URL) | tar xz --strip 1 -C $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) $(OUT_JS) $(OUT_WASM)
|
||||||
61
codecs/basis/enc/basis_enc.cpp
Normal file
61
codecs/basis/enc/basis_enc.cpp
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
#include <emscripten/bind.h>
|
||||||
|
#include <emscripten/val.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include "basisu_comp.h"
|
||||||
|
#include "basisu_enc.h"
|
||||||
|
|
||||||
|
using namespace emscripten;
|
||||||
|
using namespace basisu;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
thread_local const val Uint8Array = val::global("Uint8Array");
|
||||||
|
|
||||||
|
val encode(std::string image_in, int image_width, int image_height/*, MozJpegOptions opts*/) {
|
||||||
|
basis_compressor_params params;
|
||||||
|
basis_compressor compressor;
|
||||||
|
image img = image(reinterpret_cast<const uint8_t*>(image_in.c_str()), image_width, image_height, 4);
|
||||||
|
// We don’t need the encoder to read/decode files from the filesystem
|
||||||
|
params.m_read_source_images = false;
|
||||||
|
// Writing is unnecessary, too
|
||||||
|
params.m_read_source_images = false;
|
||||||
|
// Generate .basis file
|
||||||
|
params.m_uastc = true;
|
||||||
|
params.m_compression_level = 2; /* 0-4 */
|
||||||
|
params.m_source_images.push_back(img);
|
||||||
|
|
||||||
|
if(!compressor.init(params)) {
|
||||||
|
return val(std::string("Well something went wrong during init"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(compressor.process() != 0) {
|
||||||
|
return val(std::string("Well something went wrong during processing"));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto comp_data = compressor.get_output_basis_file();
|
||||||
|
auto js_result = Uint8Array.new_(typed_memory_view(comp_data.size(), &comp_data[0]));
|
||||||
|
// Not sure if there is anything to free here
|
||||||
|
return js_result;
|
||||||
|
}
|
||||||
|
|
||||||
|
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("encode", &encode);
|
||||||
|
}
|
||||||
3183
codecs/basis/enc/basis_enc.js
generated
Normal file
3183
codecs/basis/enc/basis_enc.js
generated
Normal file
File diff suppressed because it is too large
Load Diff
BIN
codecs/basis/enc/basis_enc.wasm
Executable file
BIN
codecs/basis/enc/basis_enc.wasm
Executable file
Binary file not shown.
5
codecs/basis/package.json
Normal file
5
codecs/basis/package.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"scripts": {
|
||||||
|
"build": "../build-cpp.sh"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user