forked from GoogleChromeLabs/squoosh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the Quite OK Image (QOI) Codec (GoogleChromeLabs#1384)
- Loading branch information
1 parent
f374068
commit d87eff7
Showing
19 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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) | ||
|
||
$(filter enc/%,$(OUT_JS)): enc/qoi_enc.o | ||
$(filter dec/%,$(OUT_JS)): dec/qoi_dec.o | ||
|
||
# ALL .js FILES | ||
$(OUT_JS): | ||
$(LD) \ | ||
$(LDFLAGS) \ | ||
--bind \ | ||
-s ENVIRONMENT=$(ENVIRONMENT) \ | ||
-s EXPORT_ES6=1 \ | ||
-o $@ \ | ||
$+ | ||
|
||
# ALL .o FILES | ||
%.o: %.cpp $(CODEC_DIR) | ||
$(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#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) { | ||
qoi_desc desc; | ||
uint8_t* rgba = (uint8_t*)qoi_decode(qoiimage.c_str(), qoiimage.length(), &desc, 4); | ||
|
||
// Resultant width and height stored in descriptor | ||
int decodedWidth = desc.width; | ||
int decodedHeight = desc.height; | ||
|
||
val result = ImageData.new_( | ||
Uint8ClampedArray.new_(typed_memory_view(4 * decodedWidth * decodedHeight, rgba)), | ||
decodedWidth, decodedHeight); | ||
free(rgba); | ||
|
||
return result; | ||
} | ||
|
||
EMSCRIPTEN_BINDINGS(my_module) { | ||
function("decode", &decode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <emscripten/bind.h> | ||
#include <emscripten/val.h> | ||
|
||
#define QOI_IMPLEMENTATION | ||
#include "qoi.h" | ||
|
||
using namespace emscripten; | ||
|
||
struct QoiOptions {}; | ||
|
||
thread_local const val Uint8Array = val::global("Uint8Array"); | ||
|
||
val encode(std::string buffer, int width, int height, QoiOptions options) { | ||
int compressedSizeInBytes; | ||
qoi_desc desc; | ||
desc.width = width; | ||
desc.height = height; | ||
desc.channels = 4; | ||
desc.colorspace = QOI_SRGB; | ||
|
||
uint8_t* encodedData = (uint8_t*)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)); | ||
free(encodedData); | ||
|
||
return js_result; | ||
} | ||
|
||
EMSCRIPTEN_BINDINGS(my_module) { | ||
value_object<QoiOptions>("QoiOptions"); | ||
|
||
function("encode", &encode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export interface EncodeOptions {} | ||
|
||
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; |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "qoi", | ||
"scripts": { | ||
"build": "../build-cpp.sh" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import qoiDecoder, { 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) { | ||
emscriptenModule = initEmscriptenModule(qoiDecoder); | ||
} | ||
|
||
const [module, data] = await Promise.all([ | ||
emscriptenModule, | ||
blobToArrayBuffer(blob), | ||
]); | ||
|
||
const result = module.decode(data); | ||
if (!result) throw new Error('Decoding error'); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { EncodeOptions } from '../shared/meta'; | ||
import type WorkerBridge from 'client/lazy-app/worker-bridge'; | ||
|
||
export function encode( | ||
signal: AbortSignal, | ||
workerBridge: WorkerBridge, | ||
imageData: ImageData, | ||
options: EncodeOptions, | ||
) { | ||
return workerBridge.qoiEncode(signal, imageData, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Copyright 2020 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/// <reference path="../../../../../missing-types.d.ts" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright 2020 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { EncodeOptions } from 'codecs/qoi/enc/qoi_enc'; | ||
export { EncodeOptions }; | ||
|
||
export const label = 'QOI'; | ||
export const mimeType = 'image/qoi'; | ||
export const extension = 'qoi'; | ||
export const defaultOptions: EncodeOptions = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Copyright 2020 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/// <reference path="../../../../../missing-types.d.ts" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Copyright 2020 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/// <reference path="../../../../../missing-types.d.ts" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright 2020 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import qoiEncoder, { QoiModule } from 'codecs/qoi/enc/qoi_enc'; | ||
import type { EncodeOptions } from '../shared/meta'; | ||
import { initEmscriptenModule } from 'features/worker-utils'; | ||
|
||
let emscriptenModule: Promise<QoiModule>; | ||
|
||
async function init() { | ||
return initEmscriptenModule(qoiEncoder); | ||
} | ||
|
||
export default async function encode( | ||
data: ImageData, | ||
options: EncodeOptions, | ||
): Promise<ArrayBuffer> { | ||
if (!emscriptenModule) { | ||
emscriptenModule = init(); | ||
} | ||
|
||
const module = await emscriptenModule; | ||
const resultView = module.encode(data.data, data.width, data.height, options); | ||
// wasm can’t run on SharedArrayBuffers, so we hard-cast to ArrayBuffer. | ||
return resultView.buffer as ArrayBuffer; | ||
} |