Use libjpeg-turbo for improved jpg compatibility and speed

Co-authored-by: Rémi Verschelde <rverschelde@gmail.com>
This commit is contained in:
Daniel Kinsman
2025-03-17 15:10:36 +11:00
committed by Rémi Verschelde
parent cc948984ad
commit a0cc41b5ed
131 changed files with 50882 additions and 5276 deletions

View File

@ -6,23 +6,99 @@ Import("env_modules")
env_jpg = env_modules.Clone()
# Thirdparty source files
thirdparty_obj = []
# Not unbundled for now as they are not commonly available as shared library
thirdparty_dir = "#thirdparty/jpeg-compressor/"
thirdparty_sources = [
"jpgd.cpp",
"jpge.cpp",
]
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
thirdparty_dir = "#thirdparty/libjpeg-turbo"
env_jpg.Prepend(CPPEXTPATH=[thirdparty_dir])
thirdparty_sources_common = [
"jaricom.c",
"jcapimin.c",
"jcarith.c",
"jchuff.c",
"jcicc.c",
"jcinit.c",
"jcmarker.c",
"jcmaster.c",
"jcomapi.c",
"jcparam.c",
"jcphuff.c",
"jctrans.c",
"jdapimin.c",
"jdarith.c",
"jdatadst.c",
"jdatadst-tj.c",
"jdatasrc.c",
"jdatasrc-tj.c",
"jdhuff.c",
"jdicc.c",
"jdinput.c",
"jdmarker.c",
"jdmaster.c",
"jdphuff.c",
"jdtrans.c",
"jerror.c",
"jfdctflt.c",
"jmemmgr.c",
"jmemnobs.c",
"jpeg_nbits.c",
"transupp.c",
"turbojpeg.c",
]
thirdparty_sources_bit_dependent = [
"jcapistd.c",
"jccoefct.c",
"jccolor.c",
"jcdctmgr.c",
"jcmainct.c",
"jcprepct.c",
"jcsample.c",
"jdcoefct.c",
"jdcolor.c",
"jdapistd.c",
"jddctmgr.c",
"jdmainct.c",
"jdmerge.c",
"jdpostct.c",
"jdsample.c",
"jfdctfst.c",
"jfdctint.c",
"jidctflt.c",
"jidctfst.c",
"jidctint.c",
"jidctred.c",
"jutils.c",
"jquant1.c",
"jquant2.c",
]
thirdparty_sources_by_bits = {
8: list(thirdparty_sources_bit_dependent),
12: list(thirdparty_sources_bit_dependent),
}
def source_paths(files):
return [thirdparty_dir + "/src/" + f for f in files]
env_jpg.Prepend(CPPEXTPATH=[thirdparty_dir + "/src"])
def add_bit_depth(bit_depth: int):
env_bit_depth = env_jpg.Clone()
env_bit_depth.disable_warnings()
env_bit_depth["OBJSUFFIX"] = f"_{bit_depth}{env_bit_depth['OBJSUFFIX']}"
env_bit_depth.Append(CPPDEFINES=[f"BITS_IN_JSAMPLE={bit_depth}"])
env_bit_depth.add_source_files(thirdparty_obj, source_paths(thirdparty_sources_by_bits[bit_depth]))
add_bit_depth(8)
add_bit_depth(12)
env_thirdparty = env_jpg.Clone()
env_thirdparty.disable_warnings()
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)
env_thirdparty.add_source_files(thirdparty_obj, source_paths(thirdparty_sources_common))
env.modules_sources += thirdparty_obj
# Godot source files

View File

@ -1,209 +0,0 @@
/**************************************************************************/
/* image_loader_jpegd.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "image_loader_jpegd.h"
#include <jpgd.h>
#include <jpge.h>
#include <string.h>
Error jpeg_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
jpgd::jpeg_decoder_mem_stream mem_stream(p_buffer, p_buffer_len);
jpgd::jpeg_decoder decoder(&mem_stream);
if (decoder.get_error_code() != jpgd::JPGD_SUCCESS) {
return ERR_CANT_OPEN;
}
const int image_width = decoder.get_width();
const int image_height = decoder.get_height();
const int comps = decoder.get_num_components();
if (comps != 1 && comps != 3) {
return ERR_FILE_CORRUPT;
}
if (decoder.begin_decoding() != jpgd::JPGD_SUCCESS) {
return ERR_FILE_CORRUPT;
}
const int dst_bpl = image_width * comps;
Vector<uint8_t> data;
data.resize(dst_bpl * image_height);
uint8_t *dw = data.ptrw();
jpgd::uint8 *pImage_data = (jpgd::uint8 *)dw;
for (int y = 0; y < image_height; y++) {
const jpgd::uint8 *pScan_line;
jpgd::uint scan_line_len;
if (decoder.decode((const void **)&pScan_line, &scan_line_len) != jpgd::JPGD_SUCCESS) {
return ERR_FILE_CORRUPT;
}
jpgd::uint8 *pDst = pImage_data + y * dst_bpl;
if (comps == 1) {
memcpy(pDst, pScan_line, dst_bpl);
} else {
// For images with more than 1 channel pScan_line will always point to a buffer
// containing 32-bit RGBA pixels. Alpha is always 255 and we ignore it.
for (int x = 0; x < image_width; x++) {
pDst[0] = pScan_line[x * 4 + 0];
pDst[1] = pScan_line[x * 4 + 1];
pDst[2] = pScan_line[x * 4 + 2];
pDst += 3;
}
}
}
//all good
Image::Format fmt;
if (comps == 1) {
fmt = Image::FORMAT_L8;
} else {
fmt = Image::FORMAT_RGB8;
}
p_image->set_data(image_width, image_height, false, fmt, data);
return OK;
}
Error ImageLoaderJPG::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
Vector<uint8_t> src_image;
uint64_t src_image_len = f->get_length();
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
src_image.resize(src_image_len);
uint8_t *w = src_image.ptrw();
f->get_buffer(&w[0], src_image_len);
Error err = jpeg_load_image_from_buffer(p_image.ptr(), w, src_image_len);
return err;
}
void ImageLoaderJPG::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("jpg");
p_extensions->push_back("jpeg");
}
static Ref<Image> _jpegd_mem_loader_func(const uint8_t *p_png, int p_size) {
Ref<Image> img;
img.instantiate();
Error err = jpeg_load_image_from_buffer(img.ptr(), p_png, p_size);
ERR_FAIL_COND_V(err, Ref<Image>());
return img;
}
class ImageLoaderJPGOSFile : public jpge::output_stream {
public:
Ref<FileAccess> f;
virtual bool put_buf(const void *Pbuf, int len) {
f->store_buffer((const uint8_t *)Pbuf, len);
return true;
}
};
class ImageLoaderJPGOSBuffer : public jpge::output_stream {
public:
Vector<uint8_t> *buffer = nullptr;
virtual bool put_buf(const void *Pbuf, int len) {
uint32_t base = buffer->size();
buffer->resize(base + len);
memcpy(buffer->ptrw() + base, Pbuf, len);
return true;
}
};
static Error _jpgd_save_to_output_stream(jpge::output_stream *p_output_stream, const Ref<Image> &p_img, float p_quality) {
ERR_FAIL_COND_V(p_img.is_null() || p_img->is_empty(), ERR_INVALID_PARAMETER);
Ref<Image> image = p_img->duplicate();
if (image->is_compressed()) {
Error error = image->decompress();
ERR_FAIL_COND_V_MSG(error != OK, error, "Couldn't decompress image.");
}
if (image->get_format() != Image::FORMAT_RGB8) {
image = image->duplicate();
image->convert(Image::FORMAT_RGB8);
}
jpge::params p;
p.m_quality = CLAMP(p_quality * 100, 1, 100);
jpge::jpeg_encoder enc;
enc.init(p_output_stream, image->get_width(), image->get_height(), 3, p);
const uint8_t *src_data = image->get_data().ptr();
for (int i = 0; i < image->get_height(); i++) {
if (!enc.process_scanline(&src_data[i * image->get_width() * 3])) {
return FAILED;
}
}
if (enc.process_scanline(nullptr)) {
return OK;
} else {
return FAILED;
}
}
static Vector<uint8_t> _jpgd_buffer_save_func(const Ref<Image> &p_img, float p_quality) {
Vector<uint8_t> output;
ImageLoaderJPGOSBuffer ob;
ob.buffer = &output;
if (_jpgd_save_to_output_stream(&ob, p_img, p_quality) != OK) {
return Vector<uint8_t>();
}
return output;
}
static Error _jpgd_save_func(const String &p_path, const Ref<Image> &p_img, float p_quality) {
Error err;
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save JPG at path: '%s'.", p_path));
ImageLoaderJPGOSFile ob;
ob.f = file;
return _jpgd_save_to_output_stream(&ob, p_img, p_quality);
}
ImageLoaderJPG::ImageLoaderJPG() {
Image::_jpg_mem_loader_func = _jpegd_mem_loader_func;
Image::save_jpg_func = _jpgd_save_func;
Image::save_jpg_buffer_func = _jpgd_buffer_save_func;
}

View File

@ -0,0 +1,191 @@
/**************************************************************************/
/* image_loader_libjpeg_turbo.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "image_loader_libjpeg_turbo.h"
#include <turbojpeg.h>
#include <string.h>
Error jpeg_turbo_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
tjhandle tj_instance = tj3Init(TJINIT_DECOMPRESS);
if (tj_instance == NULL) {
return FAILED;
}
if (tj3DecompressHeader(tj_instance, p_buffer, p_buffer_len) < 0) {
tj3Destroy(tj_instance);
return ERR_FILE_CORRUPT;
}
const unsigned int width = tj3Get(tj_instance, TJPARAM_JPEGWIDTH);
const unsigned int height = tj3Get(tj_instance, TJPARAM_JPEGHEIGHT);
const TJCS colorspace = (TJCS)tj3Get(tj_instance, TJPARAM_COLORSPACE);
if (tj3Get(tj_instance, TJPARAM_PRECISION) > 8) {
// Proceed anyway and convert to rgb8?
tj3Destroy(tj_instance);
return ERR_UNAVAILABLE;
}
TJPF tj_pixel_format;
Image::Format gd_pixel_format;
if (colorspace == TJCS_GRAY) {
tj_pixel_format = TJPF_GRAY;
gd_pixel_format = Image::FORMAT_L8;
} else {
// Force everything else (RGB, CMYK etc) into RGB8.
tj_pixel_format = TJPF_RGB;
gd_pixel_format = Image::FORMAT_RGB8;
}
Vector<uint8_t> data;
data.resize(width * height * tjPixelSize[tj_pixel_format]);
if (tj3Decompress8(tj_instance, p_buffer, p_buffer_len, data.ptrw(), 0, tj_pixel_format) < 0) {
tj3Destroy(tj_instance);
return ERR_FILE_CORRUPT;
}
tj3Destroy(tj_instance);
p_image->set_data(width, height, false, gd_pixel_format, data);
return OK;
}
Error ImageLoaderLibJPEGTurbo::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
Vector<uint8_t> src_image;
uint64_t src_image_len = f->get_length();
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
src_image.resize(src_image_len);
uint8_t *w = src_image.ptrw();
f->get_buffer(&w[0], src_image_len);
Error err = jpeg_turbo_load_image_from_buffer(p_image.ptr(), w, src_image_len);
return err;
}
void ImageLoaderLibJPEGTurbo::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("jpg");
p_extensions->push_back("jpeg");
}
static Ref<Image> _jpeg_turbo_mem_loader_func(const uint8_t *p_png, int p_size) {
Ref<Image> img;
img.instantiate();
Error err = jpeg_turbo_load_image_from_buffer(img.ptr(), p_png, p_size);
ERR_FAIL_COND_V(err, Ref<Image>());
return img;
}
static Vector<uint8_t> _jpeg_turbo_buffer_save_func(const Ref<Image> &p_img, float p_quality) {
Vector<uint8_t> output;
ERR_FAIL_COND_V(p_img.is_null() || p_img->is_empty(), output);
Ref<Image> image = p_img->duplicate();
if (image->is_compressed()) {
Error error = image->decompress();
ERR_FAIL_COND_V_MSG(error != OK, output, "Couldn't decompress image.");
}
if (image->get_format() != Image::FORMAT_RGB8) {
// Allow grayscale L8?
image = image->duplicate();
image->convert(Image::FORMAT_RGB8);
}
tjhandle tj_instance = tj3Init(TJINIT_COMPRESS);
ERR_FAIL_COND_V_MSG(tj_instance == NULL, output, "Couldn't create tjhandle");
if (tj3Set(tj_instance, TJPARAM_QUALITY, (int)(p_quality * 100)) < 0) {
tj3Destroy(tj_instance);
ERR_FAIL_V_MSG(output, "Couldn't set jpg quality");
}
if (tj3Set(tj_instance, TJPARAM_PRECISION, 8) < 0) {
tj3Destroy(tj_instance);
ERR_FAIL_V_MSG(output, "Couldn't set jpg precision");
}
if (tj3Set(tj_instance, TJPARAM_SUBSAMP, TJSAMP_420) < 0) {
tj3Destroy(tj_instance);
ERR_FAIL_V_MSG(output, "Couldn't set jpg subsamples");
}
// If the godot image format is `Image::FORMAT_L8` we could set the appropriate
// color space here rather than defaulting to RGB.
unsigned char *jpeg_buff = NULL;
size_t jpeg_size = 0;
int code = tj3Compress8(
tj_instance,
image->get_data().ptr(),
image->get_width(),
0,
image->get_height(),
TJPF_RGB,
&jpeg_buff,
&jpeg_size);
if (code < 0) {
tj3Destroy(tj_instance);
tj3Free(jpeg_buff);
ERR_FAIL_V_MSG(output, "Couldn't compress jpg");
}
output.resize(jpeg_size);
memcpy(output.ptrw(), jpeg_buff, jpeg_size);
tj3Destroy(tj_instance);
tj3Free(jpeg_buff);
return output;
}
static Error _jpeg_turbo_save_func(const String &p_path, const Ref<Image> &p_img, float p_quality) {
Error err;
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save JPG at path: '%s'.", p_path));
Vector<uint8_t> data = _jpeg_turbo_buffer_save_func(p_img, p_quality);
ERR_FAIL_COND_V(data.size() == 0, FAILED);
ERR_FAIL_COND_V_MSG(!file->store_buffer(data.ptr(), data.size()), FAILED, "Failed writing jpg to file");
return OK;
}
ImageLoaderLibJPEGTurbo::ImageLoaderLibJPEGTurbo() {
Image::_jpg_mem_loader_func = _jpeg_turbo_mem_loader_func;
Image::save_jpg_func = _jpeg_turbo_save_func;
Image::save_jpg_buffer_func = _jpeg_turbo_buffer_save_func;
}

View File

@ -1,5 +1,5 @@
/**************************************************************************/
/* image_loader_jpegd.h */
/* image_loader_libjpeg_turbo.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
@ -32,9 +32,9 @@
#include "core/io/image_loader.h"
class ImageLoaderJPG : public ImageFormatLoader {
class ImageLoaderLibJPEGTurbo : public ImageFormatLoader {
public:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderJPG();
ImageLoaderLibJPEGTurbo();
};

View File

@ -30,17 +30,17 @@
#include "register_types.h"
#include "image_loader_jpegd.h"
#include "image_loader_libjpeg_turbo.h"
static Ref<ImageLoaderJPG> image_loader_jpg;
static Ref<ImageLoaderLibJPEGTurbo> image_loader_libjpeg_turbo;
void initialize_jpg_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
image_loader_jpg.instantiate();
ImageLoader::add_image_format_loader(image_loader_jpg);
image_loader_libjpeg_turbo.instantiate();
ImageLoader::add_image_format_loader(image_loader_libjpeg_turbo);
}
void uninitialize_jpg_module(ModuleInitializationLevel p_level) {
@ -48,6 +48,6 @@ void uninitialize_jpg_module(ModuleInitializationLevel p_level) {
return;
}
ImageLoader::remove_image_format_loader(image_loader_jpg);
image_loader_jpg.unref();
ImageLoader::remove_image_format_loader(image_loader_libjpeg_turbo);
image_loader_libjpeg_turbo.unref();
}