Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support load jpg in basis encoder #352

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions encoder/basisu_enc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,26 @@ namespace basisu
return true;
}

bool load_jpg(const uint8_t *pBuf, size_t buf_size, image& img)
{
int width = 0, height = 0, actual_comps = 0;

jpgd::jpeg_decoder_mem_stream file_stream;
if (!file_stream.open(pBuf, buf_size))
return false;

uint8_t *pImage_data = jpgd::decompress_jpeg_image_from_stream(&file_stream, &width, &height, &actual_comps, 4, jpgd::jpeg_decoder::cFlagLinearChromaFiltering);

if (!pImage_data)
return false;

img.init(pImage_data, width, height, 4);

free(pImage_data);

return true;
}

bool load_image(const char* pFilename, image& img)
{
std::string ext(string_get_extension(std::string(pFilename)));
Expand Down
1 change: 1 addition & 0 deletions encoder/basisu_enc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3256,6 +3256,7 @@ namespace basisu
bool load_tga(const char* pFilename, image& img);
inline bool load_tga(const std::string &filename, image &img) { return load_tga(filename.c_str(), img); }

bool load_jpg(const uint8_t* pBuf, size_t buf_size, image& img);
bool load_jpg(const char *pFilename, image& img);
inline bool load_jpg(const std::string &filename, image &img) { return load_jpg(filename.c_str(), img); }

Expand Down
Binary file added webgl/encode_test/assets/kodim03.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 8 additions & 4 deletions webgl/encode_test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@


function loadArrayBuffer(uri, callback) {
var type = 0;
if(uri.endsWith(".png")) type = 1;
if(uri.endsWith(".jpg") || uri.endsWith(".jpeg")) type = 2;

log('Loading ' + uri + '...');
var xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";
xhr.open('GET', uri, true);
xhr.onreadystatechange = function(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
callback(xhr.response);
callback(xhr.response, type);
}
}
xhr.send(null);
Expand Down Expand Up @@ -410,7 +414,7 @@

var encodedBasisFile;

function PNGDataLoaded(data)
function ImageDataLoaded(data, type)
{
const { BasisFile, BasisEncoder, initializeBasis, encodeBasisTexture } = Module;

Expand All @@ -429,7 +433,7 @@
const qualityLevel = parseInt(elem('EncodeQuality').value, 10);
const uastcFlag = elem('EncodeUASTC').checked;

basisEncoder.setSliceSourceImage(0, new Uint8Array(data), 0, 0, true);
basisEncoder.setSliceSourceImage(0, new Uint8Array(data), 0, 0, type);
basisEncoder.setDebug(elem('Debug').checked);
basisEncoder.setComputeStats(elem('ComputeStats').checked);
basisEncoder.setPerceptual(elem('SRGB').checked);
Expand Down Expand Up @@ -479,7 +483,7 @@

function runEncodePNGFile() {
elem('logger').innerHTML = '';
loadArrayBuffer(elem('pngfile').value, PNGDataLoaded);
loadArrayBuffer(elem('pngfile').value, ImageDataLoaded);
}

function alphaBlend() { drawMode = 0; redraw(); }
Expand Down
19 changes: 9 additions & 10 deletions webgl/encoder/build/basis_encoder.js

Large diffs are not rendered by default.

Binary file modified webgl/encoder/build/basis_encoder.wasm
100644 → 100755
Binary file not shown.
Binary file added webgl/ktx2_encode_test/assets/kodim03.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 8 additions & 4 deletions webgl/ktx2_encode_test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@


function loadArrayBuffer(uri, callback) {
var type = 0;
if(uri.endsWith(".png")) type = 1;
if(uri.endsWith(".jpg") || uri.endsWith(".jpeg")) type = 2;

log('Loading ' + uri + '...');
var xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";
xhr.open('GET', uri, true);
xhr.onreadystatechange = function(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
callback(xhr.response);
callback(xhr.response, type);
}
}
xhr.send(null);
Expand Down Expand Up @@ -485,7 +489,7 @@

var encodedKTX2File;

function PNGDataLoaded(data)
function ImageDataLoaded(data, type)
{
const { BasisFile, BasisEncoder, initializeBasis, encodeBasisTexture } = Module;

Expand All @@ -508,7 +512,7 @@
basisEncoder.setKTX2UASTCSupercompression(true);
basisEncoder.setKTX2SRGBTransferFunc(true);

basisEncoder.setSliceSourceImage(0, new Uint8Array(data), 0, 0, true);
basisEncoder.setSliceSourceImage(0, new Uint8Array(data), 0, 0, type);
basisEncoder.setDebug(elem('Debug').checked);
basisEncoder.setComputeStats(elem('ComputeStats').checked);
basisEncoder.setPerceptual(elem('SRGB').checked);
Expand Down Expand Up @@ -558,7 +562,7 @@

function runEncodePNGFile() {
elem('logger').innerHTML = '';
loadArrayBuffer(elem('pngfile').value, PNGDataLoaded);
loadArrayBuffer(elem('pngfile').value, ImageDataLoaded);
}

function alphaBlend() { drawMode = 0; redraw(); }
Expand Down
21 changes: 17 additions & 4 deletions webgl/transcoder/basis_wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ class basis_encoder
{
}

bool set_slice_source_image(uint32_t slice_index, const emscripten::val& src_image_js_val, uint32_t src_image_width, uint32_t src_image_height, bool src_image_is_png)
bool set_slice_source_image(uint32_t slice_index, const emscripten::val& src_image_js_val, uint32_t src_image_width, uint32_t src_image_height, uint8_t src_image_type)
{
// Resize the source_images array if necessary
if (slice_index >= m_params.m_source_images.size())
Expand All @@ -1013,7 +1013,7 @@ class basis_encoder

// Now extract the source image.
image& src_img = m_params.m_source_images[slice_index];
if (src_image_is_png)
if (src_image_type == 1)
{
// It's a PNG file, so try and parse it.
if (!load_png(src_image_buf.data(), src_image_buf.size(), src_img, nullptr))
Expand All @@ -1024,6 +1024,19 @@ class basis_encoder
return false;
}

src_image_width = src_img.get_width();
src_image_height = src_img.get_height();
}
else if(src_image_type == 2)
{
if (!load_jpg(src_image_buf.data(), src_image_buf.size(), src_img))
{
#if BASISU_DEBUG_PRINTF
printf("basis_encoder::set_slice_source_image: Failed parsing provided JPG file!\n");
#endif
return false;
}

src_image_width = src_img.get_width();
src_image_height = src_img.get_height();
}
Expand Down Expand Up @@ -1716,8 +1729,8 @@ EMSCRIPTEN_BINDINGS(basis_codec) {
// If the input is a raster image, the buffer must be width*height*4 bytes in size. The raster image is stored in top down scanline order.
// The first texel is the top-left texel. The texel byte order in memory is R,G,B,A (R first at offset 0, A last at offset 3).
// slice_index is the slice to change. Valid range is [0,BASISU_MAX_SLICES-1].
.function("setSliceSourceImage", optional_override([](basis_encoder& self, uint32_t slice_index, const emscripten::val& src_image_js_val, uint32_t width, uint32_t height, bool src_image_is_png) {
return self.set_slice_source_image(slice_index, src_image_js_val, width, height, src_image_is_png);
.function("setSliceSourceImage", optional_override([](basis_encoder& self, uint32_t slice_index, const emscripten::val& src_image_js_val, uint32_t width, uint32_t height, uint8_t src_image_type) {
return self.set_slice_source_image(slice_index, src_image_js_val, width, height, src_image_type);
}))

// If true, the encoder will output a UASTC texture, otherwise a ETC1S texture.
Expand Down