Integrate QOI into web-app

This commit is contained in:
robo-mop
2023-10-16 20:22:53 +05:30
parent d2a656f0bb
commit 4a1bcec5af
11 changed files with 300 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
CODEC_URL = https://github.com/phoboslab/qoi/archive/8d35d93cdca85d2868246c2a8a80a1e2c16ba2a8.tar.gz
CODEC_DIR = node_modules/qoi
CODEC_BUILD_DIR:= $(CODEC_DIR)/build
ENVIRONMENT = worker
OUT_JS = enc/qoi_enc.js dec/qoi_dec.js
OUT_WASM := $(OUT_JS:.js=.wasm)
.PHONY: all clean
all: $(OUT_JS)
# Define dependencies for all variations of build artifacts.
enc/qoi_enc.js dec/qoi_dec.js: $(CODEC_DIR)/qoi.h
$(filter enc/%,$(OUT_JS)): enc/qoi_enc.cpp
$(filter dec/%,$(OUT_JS)): dec/qoi_dec.cpp
# ALL .js FILES
$(OUT_JS):
@echo ">> Making $@"
$(LD) \
$(LDFLAGS) \
--bind \
-s ENVIRONMENT=$(ENVIRONMENT) \
-s EXPORT_ES6=1 \
-o $@ \
$+
# ALL .o FILES
%.o: $(CODEC_DIR)
$(info )
$(info Making .o files)
$(info $$ + = $+)
$(info $$ @ = $@)
$(info )
$(CXX) -c \
$(CXXFLAGS) \
-I $(CODEC_DIR) \
-o $@ \
$<
# CREATE DIRECTORY
$(CODEC_DIR):
mkdir -p $(CODEC_DIR)
curl -sL $(CODEC_URL) | tar xz --strip 1 -C $(CODEC_DIR)
clean:
$(RM) $(OUT_JS) $(OUT_WASM)
$(MAKE) -C $(CODEC_DIR) clean
test: $(CODEC_BUILD_DIR)/libqoi.a
gcc -o bruh.out test.cpp $+

View File

@@ -0,0 +1,25 @@
#include <emscripten/bind.h>
#include <emscripten/val.h>
#define QOI_IMPLEMENTATION
#include "qoi.h"
using namespace emscripten;
thread_local const val Uint8ClampedArray = val::global("Uint8ClampedArray");
thread_local const val ImageData = val::global("ImageData");
val decode(std::string qoiimage) {
val result = val::null();
const int N = 1000;
int data[N] = {0};
result = ImageData.new_(Uint8ClampedArray.new_(typed_memory_view(N, data)), 20, 50);
return result;
}
EMSCRIPTEN_BINDINGS(my_module) {
function("decode", &decode);
}

View File

@@ -0,0 +1,37 @@
#include <emscripten/bind.h>
#include <emscripten/val.h>
#define QOI_IMPLEMENTATION
#include "qoi.h"
using namespace emscripten;
struct QoiOptions {
int quality;
bool randombool;
};
thread_local const val Uint8Array = val::global("Uint8Array");
val encode(std::string buffer, int width, int height, QoiOptions options) {
printf("Starting encode!");
printf("quality = %d\n", options.quality);
printf("randombool = %s\n", options.randombool ? "true" : "false");
auto js_result = val::null();
const int N = 100;
int* data = (int*)malloc(N * sizeof(int));
js_result = Uint8Array.new_(typed_memory_view(N, data));
return js_result;
}
EMSCRIPTEN_BINDINGS(my_module) {
value_object<QoiOptions>("QoiOptions")
.field("quality", &QoiOptions::quality)
.field("randombool", &QoiOptions::randombool);
function("encode", &encode);
}

17
codecs/qoi/enc/qoi_enc.d.ts vendored Normal file
View File

@@ -0,0 +1,17 @@
export interface EncodeOptions {
quality: number;
randombool: boolean;
}
export interface QoiModule extends EmscriptenWasm.Module {
encode(
data: BufferSource,
width: number,
height: number,
options: EncodeOptions,
): Uint8Array;
}
declare var moduleFactory: EmscriptenWasm.ModuleFactory<QoiModule>;
export default moduleFactory;