Skip to content

Commit

Permalink
Integrate QOI codec completely
Browse files Browse the repository at this point in the history
* Adds code for encoders and decoders
* Cleans up some obsolete QOI-related code
  • Loading branch information
aryanpingle committed Oct 17, 2023
1 parent 71f3419 commit 003d5d1
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 84 deletions.
12 changes: 8 additions & 4 deletions codecs/qoi/dec/qoi_dec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ 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();
qoi_desc desc;
uint8_t* rgba = (uint8_t*)qoi_decode(qoiimage.c_str(), qoiimage.length(), &desc, 4);

const int N = 1000;
int data[N] = {0};
// Resultant width and height stored in descriptor
int decodedWidth = desc.width;
int decodedHeight = desc.height;

result = ImageData.new_(Uint8ClampedArray.new_(typed_memory_view(N, data)), 20, 50);
val result = ImageData.new_(
Uint8ClampedArray.new_(typed_memory_view(4 * decodedWidth * decodedHeight, rgba)),
decodedWidth, decodedHeight);

return result;
}
Expand Down
7 changes: 7 additions & 0 deletions codecs/qoi/dec/qoi_dec.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface QOIModule extends EmscriptenWasm.Module {
decode(data: BufferSource): ImageData | null;
}

declare var moduleFactory: EmscriptenWasm.ModuleFactory<QOIModule>;

export default moduleFactory;
16 changes: 16 additions & 0 deletions codecs/qoi/dec/qoi_dec.js

Large diffs are not rendered by default.

Binary file added codecs/qoi/dec/qoi_dec.wasm
Binary file not shown.
33 changes: 15 additions & 18 deletions codecs/qoi/enc/qoi_enc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,29 @@

using namespace emscripten;

struct QoiOptions {
int quality;
bool randombool;
};
struct QoiOptions {};

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));
int compressedSizeInBytes;
qoi_desc desc;
desc.width = width;
desc.height = height;
desc.channels = 4;
desc.colorspace = QOI_SRGB;

void* encodedData = qoi_encode(buffer.c_str(), &desc, &compressedSizeInBytes);
if (encodedData == NULL)
return val::null();

auto js_result =
Uint8Array.new_(typed_memory_view(compressedSizeInBytes, (const uint8_t*)encodedData));
return js_result;
}

EMSCRIPTEN_BINDINGS(my_module) {
value_object<QoiOptions>("QoiOptions")
.field("quality", &QoiOptions::quality)
.field("randombool", &QoiOptions::randombool);
value_object<QoiOptions>("QoiOptions");

function("encode", &encode);
}
5 changes: 1 addition & 4 deletions codecs/qoi/enc/qoi_enc.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export interface EncodeOptions {
quality: number;
randombool: boolean;
}
export interface EncodeOptions {}

export interface QoiModule extends EmscriptenWasm.Module {
encode(
Expand Down
16 changes: 16 additions & 0 deletions codecs/qoi/enc/qoi_enc.js

Large diffs are not rendered by default.

Binary file added codecs/qoi/enc/qoi_enc.wasm
Binary file not shown.
3 changes: 3 additions & 0 deletions src/client/lazy-app/Compress/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ async function decodeImage(
if (mimeType === 'image/webp2') {
return await workerBridge.wp2Decode(signal, blob);
}
if (mimeType === 'image/qoi') {
return await workerBridge.qoiDecode(signal, blob);
}
}
// Otherwise fall through and try built-in decoding for a laugh.
return await builtinDecode(signal, blob);
Expand Down
20 changes: 20 additions & 0 deletions src/features/decoders/qoi/worker/qoiDecode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { QOIModule } from 'codecs/qoi/dec/qoi_dec';
import { initEmscriptenModule, blobToArrayBuffer } from 'features/worker-utils';

let emscriptenModule: Promise<QOIModule>;

export default async function decode(blob: Blob): Promise<ImageData> {
if (!emscriptenModule) {
const decoder = await import('codecs/qoi/dec/qoi_dec');
emscriptenModule = initEmscriptenModule(decoder.default);
}

const [module, data] = await Promise.all([
emscriptenModule,
blobToArrayBuffer(blob),
]);

const result = module.decode(data);
if (!result) throw new Error('Decoding error');
return result;
}
56 changes: 3 additions & 53 deletions src/features/encoders/qoi/client/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import { EncodeOptions } from '../shared/meta';
import type WorkerBridge from 'client/lazy-app/worker-bridge';
import { h, Component } from 'preact';
import {
inputFieldChecked,
inputFieldValueAsNumber,
preventDefault,
} from 'client/lazy-app/util';
import * as style from 'client/lazy-app/Compress/Options/style.css';
import linkState from 'linkstate';
import Range from 'client/lazy-app/Compress/Options/Range';
import Checkbox from 'client/lazy-app/Compress/Options/Checkbox';
import Expander from 'client/lazy-app/Compress/Options/Expander';
import Select from 'client/lazy-app/Compress/Options/Select';
import Revealer from 'client/lazy-app/Compress/Options/Revealer';
import { h, Component, Fragment } from 'preact';

export function encode(
signal: AbortSignal,
Expand All @@ -28,46 +16,8 @@ interface Props {
onChange(newOptions: EncodeOptions): void;
}

interface State {
showAdvanced: boolean;
}

export class Options extends Component<Props, {}> {
onChange = (event: Event) => {
const form = (event.currentTarget as HTMLInputElement).closest(
'form',
) as HTMLFormElement;

const options: EncodeOptions = {
quality: inputFieldValueAsNumber(form.quality),
randombool: inputFieldChecked(form.randombool),
};
this.props.onChange(options);
};

render({ options }: Props) {
return (
<form class={style.optionsSection} onSubmit={preventDefault}>
<div class={style.optionOneCell}>
<Range
name="quality"
min="0"
max="100"
value={options.quality}
onInput={this.onChange}
>
Quality:
</Range>
</div>
<label class={style.optionToggle}>
Random Bool
<Checkbox
name="randombool"
checked={options.randombool}
onChange={this.onChange}
/>
</label>
</form>
);
render() {
return <Fragment></Fragment>;
}
}
5 changes: 1 addition & 4 deletions src/features/encoders/qoi/shared/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ export { EncodeOptions };
export const label = 'QOI';
export const mimeType = 'image/qoi';
export const extension = 'qoi';
export const defaultOptions: EncodeOptions = {
quality: 75,
randombool: true,
};
export const defaultOptions: EncodeOptions = {};
1 change: 0 additions & 1 deletion src/features/encoders/qoi/worker/qoiEncode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default async function encode(

const module = await emscriptenModule;
const resultView = module.encode(data.data, data.width, data.height, options);
console.log(resultView);
// wasm can’t run on SharedArrayBuffers, so we hard-cast to ArrayBuffer.
return resultView.buffer as ArrayBuffer;
}

0 comments on commit 003d5d1

Please sign in to comment.