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

@ -4,13 +4,13 @@
#define THORVG_SW_RASTER_SUPPORT
#define THORVG_SVG_LOADER_SUPPORT
#define THORVG_PNG_LOADER_SUPPORT
#define THORVG_JPG_LOADER_SUPPORT
#ifndef WEB_ENABLED
#define THORVG_THREAD_SUPPORT
#endif
// Added conditionally if webp module is enabled.
// Added conditionally if respective modules are enabled.
//#define THORVG_WEBP_LOADER_SUPPORT
//#define THORVG_JPG_LOADER_SUPPORT
// For internal debugging:
//#define THORVG_LOG_ENABLED

View File

@ -21,6 +21,7 @@
*/
#include <memory.h>
#include <turbojpeg.h>
#include "tvgJpgLoader.h"
/************************************************************************/
@ -29,57 +30,71 @@
void JpgLoader::clear()
{
jpgdDelete(decoder);
if (freeData) free(data);
decoder = nullptr;
data = nullptr;
size = 0;
freeData = false;
}
void JpgLoader::run(unsigned tid)
{
surface.buf8 = jpgdDecompress(decoder);
surface.stride = static_cast<uint32_t>(w);
surface.w = static_cast<uint32_t>(w);
surface.h = static_cast<uint32_t>(h);
surface.cs = ColorSpace::ARGB8888;
surface.channelSize = sizeof(uint32_t);
surface.premultiplied = true;
clear();
}
/************************************************************************/
/* External Class Implementation */
/************************************************************************/
JpgLoader::JpgLoader() : ImageLoader(FileType::Jpg)
{
jpegDecompressor = tjInitDecompress();
}
JpgLoader::~JpgLoader()
{
done();
clear();
free(surface.buf8);
tjDestroy(jpegDecompressor);
//This image is shared with raster engine.
tjFree(surface.buf8);
}
bool JpgLoader::open(const string& path)
{
#ifdef THORVG_FILE_IO_SUPPORT
int width, height;
decoder = jpgdHeader(path.c_str(), &width, &height);
if (!decoder) return false;
auto jpegFile = fopen(path.c_str(), "rb");
if (!jpegFile) return false;
auto ret = false;
//determine size
if (fseek(jpegFile, 0, SEEK_END) < 0) goto finalize;
if (((size = ftell(jpegFile)) < 1)) goto finalize;
if (fseek(jpegFile, 0, SEEK_SET)) goto finalize;
data = (unsigned char *) malloc(size);
if (!data) goto finalize;
freeData = true;
if (fread(data, size, 1, jpegFile) < 1) goto failure;
int width, height, subSample, colorSpace;
if (tjDecompressHeader3(jpegDecompressor, data, size, &width, &height, &subSample, &colorSpace) < 0) {
TVGERR("JPG LOADER", "%s", tjGetErrorStr());
goto failure;
}
w = static_cast<float>(width);
h = static_cast<float>(height);
ret = true;
return true;
goto finalize;
failure:
clear();
finalize:
fclose(jpegFile);
return ret;
#else
return false;
#endif
@ -88,50 +103,62 @@ bool JpgLoader::open(const string& path)
bool JpgLoader::open(const char* data, uint32_t size, bool copy)
{
int width, height, subSample, colorSpace;
if (tjDecompressHeader3(jpegDecompressor, (unsigned char *) data, size, &width, &height, &subSample, &colorSpace) < 0) return false;
if (copy) {
this->data = (char *) malloc(size);
this->data = (unsigned char *) malloc(size);
if (!this->data) return false;
memcpy((char *)this->data, data, size);
memcpy((unsigned char *)this->data, data, size);
freeData = true;
} else {
this->data = (char *) data;
this->data = (unsigned char *) data;
freeData = false;
}
int width, height;
decoder = jpgdHeader(this->data, size, &width, &height);
if (!decoder) return false;
w = static_cast<float>(width);
h = static_cast<float>(height);
this->size = size;
return true;
}
bool JpgLoader::read()
{
if (!LoadModule::read()) return true;
if (!decoder || w == 0 || h == 0) return false;
if (w == 0 || h == 0) return false;
TaskScheduler::request(this);
//determine the image format
TJPF format;
if (cs == ColorSpace::ARGB8888 || cs == ColorSpace::ARGB8888S) {
format = TJPF_BGRX;
surface.cs = ColorSpace::ARGB8888;
} else {
format = TJPF_RGBX;
surface.cs = ColorSpace::ABGR8888;
}
auto image = (unsigned char *)tjAlloc(static_cast<int>(w) * static_cast<int>(h) * tjPixelSize[format]);
if (!image) return false;
//decompress jpg image
if (tjDecompress2(jpegDecompressor, data, size, image, static_cast<int>(w), 0, static_cast<int>(h), format, 0) < 0) {
TVGERR("JPG LOADER", "%s", tjGetErrorStr());
tjFree(image);
image = nullptr;
return false;
}
//setup the surface
surface.buf8 = image;
surface.stride = w;
surface.w = w;
surface.h = h;
surface.channelSize = sizeof(uint32_t);
surface.premultiplied = true;
clear();
return true;
}
bool JpgLoader::close()
{
if (!LoadModule::close()) return false;
this->done();
return true;
}
RenderSurface* JpgLoader::bitmap()
{
this->done();
return ImageLoader::bitmap();
}

View File

@ -24,19 +24,12 @@
#define _TVG_JPG_LOADER_H_
#include "tvgLoader.h"
#include "tvgTaskScheduler.h"
#include "tvgJpgd.h"
class JpgLoader : public ImageLoader, public Task
using tjhandle = void*;
//TODO: Use Task?
class JpgLoader : public ImageLoader
{
private:
jpeg_decoder* decoder = nullptr;
char* data = nullptr;
bool freeData = false;
void clear();
void run(unsigned tid) override;
public:
JpgLoader();
~JpgLoader();
@ -44,9 +37,14 @@ public:
bool open(const string& path) override;
bool open(const char* data, uint32_t size, bool copy) override;
bool read() override;
bool close() override;
RenderSurface* bitmap() override;
private:
void clear();
tjhandle jpegDecompressor;
unsigned char* data = nullptr;
unsigned long size = 0;
bool freeData = false;
};
#endif //_TVG_JPG_LOADER_H_

File diff suppressed because it is too large Load Diff

View File

@ -1,35 +0,0 @@
/*
* Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved.
* 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.
*/
// jpgd.h - C++ class for JPEG decompression.
// Public domain, Rich Geldreich <richgel99@gmail.com>
#ifndef _TVG_JPGD_H_
#define _TVG_JPGD_H_
class jpeg_decoder;
jpeg_decoder* jpgdHeader(const char* data, int size, int* width, int* height);
jpeg_decoder* jpgdHeader(const char* filename, int* width, int* height);
unsigned char* jpgdDecompress(jpeg_decoder* decoder);
void jpgdDelete(jpeg_decoder* decoder);
#endif //_TVG_JPGD_H_

View File

@ -43,13 +43,13 @@ cat << EOF > ../inc/config.h
#define THORVG_SW_RASTER_SUPPORT
#define THORVG_SVG_LOADER_SUPPORT
#define THORVG_PNG_LOADER_SUPPORT
#define THORVG_JPG_LOADER_SUPPORT
#ifndef WEB_ENABLED
#define THORVG_THREAD_SUPPORT
#endif
// Added conditionally if webp module is enabled.
// Added conditionally if respective modules are enabled.
//#define THORVG_WEBP_LOADER_SUPPORT
//#define THORVG_JPG_LOADER_SUPPORT
// For internal debugging:
//#define THORVG_LOG_ENABLED
@ -71,8 +71,7 @@ mkdir ../src/loaders
cp -rv src/loaders/svg src/loaders/raw ../src/loaders/
cp -rv src/loaders/external_png ../src/loaders/
cp -rv src/loaders/external_webp ../src/loaders/
# Not using external jpg as it's turbojpeg, which we don't have.
cp -rv src/loaders/jpg ../src/loaders/
cp -rv src/loaders/external_jpg ../src/loaders/
popd
rm -rf tmp