Path: blob/master/modules/jpg/image_loader_libjpeg_turbo.cpp
10277 views
/**************************************************************************/1/* image_loader_libjpeg_turbo.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "image_loader_libjpeg_turbo.h"3132#include <turbojpeg.h>3334Error jpeg_turbo_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {35tjhandle tj_instance = tj3Init(TJINIT_DECOMPRESS);36if (tj_instance == NULL) {37return FAILED;38}3940if (tj3DecompressHeader(tj_instance, p_buffer, p_buffer_len) < 0) {41tj3Destroy(tj_instance);42return ERR_FILE_CORRUPT;43}4445const unsigned int width = tj3Get(tj_instance, TJPARAM_JPEGWIDTH);46const unsigned int height = tj3Get(tj_instance, TJPARAM_JPEGHEIGHT);47const TJCS colorspace = (TJCS)tj3Get(tj_instance, TJPARAM_COLORSPACE);4849if (tj3Get(tj_instance, TJPARAM_PRECISION) > 8) {50// Proceed anyway and convert to rgb8?51tj3Destroy(tj_instance);52return ERR_UNAVAILABLE;53}5455TJPF tj_pixel_format;56Image::Format gd_pixel_format;57if (colorspace == TJCS_GRAY) {58tj_pixel_format = TJPF_GRAY;59gd_pixel_format = Image::FORMAT_L8;60} else {61// Force everything else (RGB, CMYK etc) into RGB8.62tj_pixel_format = TJPF_RGB;63gd_pixel_format = Image::FORMAT_RGB8;64}6566Vector<uint8_t> data;67data.resize(width * height * tjPixelSize[tj_pixel_format]);6869if (tj3Decompress8(tj_instance, p_buffer, p_buffer_len, data.ptrw(), 0, tj_pixel_format) < 0) {70tj3Destroy(tj_instance);71return ERR_FILE_CORRUPT;72}7374tj3Destroy(tj_instance);75p_image->set_data(width, height, false, gd_pixel_format, data);76return OK;77}7879Error ImageLoaderLibJPEGTurbo::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {80Vector<uint8_t> src_image;81uint64_t src_image_len = f->get_length();82ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);83src_image.resize(src_image_len);8485uint8_t *w = src_image.ptrw();8687f->get_buffer(&w[0], src_image_len);8889Error err = jpeg_turbo_load_image_from_buffer(p_image.ptr(), w, src_image_len);9091return err;92}9394void ImageLoaderLibJPEGTurbo::get_recognized_extensions(List<String> *p_extensions) const {95p_extensions->push_back("jpg");96p_extensions->push_back("jpeg");97}9899static Ref<Image> _jpeg_turbo_mem_loader_func(const uint8_t *p_data, int p_size) {100Ref<Image> img;101img.instantiate();102Error err = jpeg_turbo_load_image_from_buffer(img.ptr(), p_data, p_size);103ERR_FAIL_COND_V(err, Ref<Image>());104return img;105}106107static Vector<uint8_t> _jpeg_turbo_buffer_save_func(const Ref<Image> &p_img, float p_quality) {108Vector<uint8_t> output;109110ERR_FAIL_COND_V(p_img.is_null() || p_img->is_empty(), output);111112Ref<Image> image = p_img->duplicate();113if (image->is_compressed()) {114Error error = image->decompress();115ERR_FAIL_COND_V_MSG(error != OK, output, "Couldn't decompress image.");116}117118if (image->get_format() != Image::FORMAT_RGB8) {119// Allow grayscale L8?120image = image->duplicate();121image->convert(Image::FORMAT_RGB8);122}123124tjhandle tj_instance = tj3Init(TJINIT_COMPRESS);125ERR_FAIL_COND_V_MSG(tj_instance == NULL, output, "Couldn't create tjhandle");126127if (tj3Set(tj_instance, TJPARAM_QUALITY, (int)(p_quality * 100)) < 0) {128tj3Destroy(tj_instance);129ERR_FAIL_V_MSG(output, "Couldn't set jpg quality");130}131132if (tj3Set(tj_instance, TJPARAM_PRECISION, 8) < 0) {133tj3Destroy(tj_instance);134ERR_FAIL_V_MSG(output, "Couldn't set jpg precision");135}136137if (tj3Set(tj_instance, TJPARAM_SUBSAMP, TJSAMP_420) < 0) {138tj3Destroy(tj_instance);139ERR_FAIL_V_MSG(output, "Couldn't set jpg subsamples");140}141142// If the godot image format is `Image::FORMAT_L8` we could set the appropriate143// color space here rather than defaulting to RGB.144145unsigned char *jpeg_buff = NULL;146size_t jpeg_size = 0;147int code = tj3Compress8(148tj_instance,149image->get_data().ptr(),150image->get_width(),1510,152image->get_height(),153TJPF_RGB,154&jpeg_buff,155&jpeg_size);156157if (code < 0) {158tj3Destroy(tj_instance);159tj3Free(jpeg_buff);160ERR_FAIL_V_MSG(output, "Couldn't compress jpg");161}162163output.resize(jpeg_size);164memcpy(output.ptrw(), jpeg_buff, jpeg_size);165166tj3Destroy(tj_instance);167tj3Free(jpeg_buff);168169return output;170}171172static Error _jpeg_turbo_save_func(const String &p_path, const Ref<Image> &p_img, float p_quality) {173Error err;174Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);175ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save JPG at path: '%s'.", p_path));176177Vector<uint8_t> data = _jpeg_turbo_buffer_save_func(p_img, p_quality);178ERR_FAIL_COND_V(data.size() == 0, FAILED);179ERR_FAIL_COND_V_MSG(!file->store_buffer(data.ptr(), data.size()), FAILED, "Failed writing jpg to file");180181return OK;182}183184ImageLoaderLibJPEGTurbo::ImageLoaderLibJPEGTurbo() {185Image::_jpg_mem_loader_func = _jpeg_turbo_mem_loader_func;186Image::save_jpg_func = _jpeg_turbo_save_func;187Image::save_jpg_buffer_func = _jpeg_turbo_buffer_save_func;188}189190191