| 1 | /**************************************************************************/ |
| 2 | /* image_loader_libjpeg_turbo.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "image_loader_libjpeg_turbo.h" |
| 32 | |
| 33 | #include <turbojpeg.h> |
| 34 | |
| 35 | Error jpeg_turbo_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) { |
| 36 | tjhandle tj_instance = tj3Init(TJINIT_DECOMPRESS); |
| 37 | if (tj_instance == NULL) { |
| 38 | return FAILED; |
| 39 | } |
| 40 | |
| 41 | if (tj3DecompressHeader(tj_instance, p_buffer, p_buffer_len) < 0) { |
| 42 | tj3Destroy(tj_instance); |
| 43 | return ERR_FILE_CORRUPT; |
| 44 | } |
| 45 | |
| 46 | const unsigned int width = tj3Get(tj_instance, TJPARAM_JPEGWIDTH); |
| 47 | const unsigned int height = tj3Get(tj_instance, TJPARAM_JPEGHEIGHT); |
| 48 | const TJCS colorspace = (TJCS)tj3Get(tj_instance, TJPARAM_COLORSPACE); |
| 49 | |
| 50 | if (tj3Get(tj_instance, TJPARAM_PRECISION) > 8) { |
| 51 | // Proceed anyway and convert to rgb8? |
| 52 | tj3Destroy(tj_instance); |
| 53 | return ERR_UNAVAILABLE; |
| 54 | } |
| 55 | |
| 56 | TJPF tj_pixel_format; |
| 57 | Image::Format gd_pixel_format; |
| 58 | if (colorspace == TJCS_GRAY) { |
| 59 | tj_pixel_format = TJPF_GRAY; |
| 60 | gd_pixel_format = Image::FORMAT_L8; |
| 61 | } else { |
| 62 | // Force everything else (RGB, CMYK etc) into RGB8. |
| 63 | tj_pixel_format = TJPF_RGB; |
| 64 | gd_pixel_format = Image::FORMAT_RGB8; |
| 65 | } |
| 66 | |
| 67 | Vector<uint8_t> data; |
| 68 | data.resize(width * height * tjPixelSize[tj_pixel_format]); |
| 69 | |
| 70 | if (tj3Decompress8(tj_instance, p_buffer, p_buffer_len, data.ptrw(), 0, tj_pixel_format) < 0) { |
| 71 | tj3Destroy(tj_instance); |
| 72 | return ERR_FILE_CORRUPT; |
| 73 | } |
| 74 | |
| 75 | tj3Destroy(tj_instance); |
| 76 | p_image->set_data(width, height, false, gd_pixel_format, data); |
| 77 | return OK; |
| 78 | } |
| 79 | |
| 80 | Error ImageLoaderLibJPEGTurbo::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) { |
| 81 | Vector<uint8_t> src_image; |
| 82 | uint64_t src_image_len = f->get_length(); |
| 83 | ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT); |
| 84 | src_image.resize(src_image_len); |
| 85 | |
| 86 | uint8_t *w = src_image.ptrw(); |
| 87 | |
| 88 | f->get_buffer(&w[0], src_image_len); |
| 89 | |
| 90 | Error err = jpeg_turbo_load_image_from_buffer(p_image.ptr(), w, src_image_len); |
| 91 | |
| 92 | return err; |
| 93 | } |
| 94 | |
| 95 | void ImageLoaderLibJPEGTurbo::get_recognized_extensions(List<String> *p_extensions) const { |
| 96 | p_extensions->push_back("jpg"); |
| 97 | p_extensions->push_back("jpeg"); |
| 98 | } |
| 99 | |
| 100 | static Ref<Image> _jpeg_turbo_mem_loader_func(const uint8_t *p_data, int p_size) { |
| 101 | Ref<Image> img; |
| 102 | img.instantiate(); |
| 103 | Error err = jpeg_turbo_load_image_from_buffer(img.ptr(), p_data, p_size); |
| 104 | ERR_FAIL_COND_V(err, Ref<Image>()); |
| 105 | return img; |
| 106 | } |
| 107 | |
| 108 | static Vector<uint8_t> _jpeg_turbo_buffer_save_func(const Ref<Image> &p_img, float p_quality) { |
| 109 | Vector<uint8_t> output; |
| 110 | |
| 111 | ERR_FAIL_COND_V(p_img.is_null() || p_img->is_empty(), output); |
| 112 | |
| 113 | Ref<Image> image = p_img->duplicate(); |
| 114 | if (image->is_compressed()) { |
| 115 | Error error = image->decompress(); |
| 116 | ERR_FAIL_COND_V_MSG(error != OK, output, "Couldn't decompress image."); |
| 117 | } |
| 118 | |
| 119 | if (image->get_format() != Image::FORMAT_RGB8) { |
| 120 | // Allow grayscale L8? |
| 121 | image = image->duplicate(); |
| 122 | image->convert(Image::FORMAT_RGB8); |
| 123 | } |
| 124 | |
| 125 | tjhandle tj_instance = tj3Init(TJINIT_COMPRESS); |
| 126 | ERR_FAIL_COND_V_MSG(tj_instance == NULL, output, "Couldn't create tjhandle"); |
| 127 | |
| 128 | if (tj3Set(tj_instance, TJPARAM_QUALITY, (int)(p_quality * 100)) < 0) { |
| 129 | tj3Destroy(tj_instance); |
| 130 | ERR_FAIL_V_MSG(output, "Couldn't set jpg quality"); |
| 131 | } |
| 132 | |
| 133 | if (tj3Set(tj_instance, TJPARAM_PRECISION, 8) < 0) { |
| 134 | tj3Destroy(tj_instance); |
| 135 | ERR_FAIL_V_MSG(output, "Couldn't set jpg precision"); |
| 136 | } |
| 137 | |
| 138 | if (tj3Set(tj_instance, TJPARAM_SUBSAMP, TJSAMP_420) < 0) { |
| 139 | tj3Destroy(tj_instance); |
| 140 | ERR_FAIL_V_MSG(output, "Couldn't set jpg subsamples"); |
| 141 | } |
| 142 | |
| 143 | // If the godot image format is `Image::FORMAT_L8` we could set the appropriate |
| 144 | // color space here rather than defaulting to RGB. |
| 145 | |
| 146 | unsigned char *jpeg_buff = NULL; |
| 147 | size_t jpeg_size = 0; |
| 148 | int code = tj3Compress8( |
| 149 | tj_instance, |
| 150 | image->get_data().ptr(), |
| 151 | image->get_width(), |
| 152 | 0, |
| 153 | image->get_height(), |
| 154 | TJPF_RGB, |
| 155 | &jpeg_buff, |
| 156 | &jpeg_size); |
| 157 | |
| 158 | if (code < 0) { |
| 159 | tj3Destroy(tj_instance); |
| 160 | tj3Free(jpeg_buff); |
| 161 | ERR_FAIL_V_MSG(output, "Couldn't compress jpg"); |
| 162 | } |
| 163 | |
| 164 | output.resize(jpeg_size); |
| 165 | memcpy(output.ptrw(), jpeg_buff, jpeg_size); |
| 166 | |
| 167 | tj3Destroy(tj_instance); |
| 168 | tj3Free(jpeg_buff); |
| 169 | |
| 170 | return output; |
| 171 | } |
| 172 | |
| 173 | static Error _jpeg_turbo_save_func(const String &p_path, const Ref<Image> &p_img, float p_quality) { |
| 174 | Error err; |
| 175 | Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err); |
| 176 | ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save JPG at path: '%s'.", p_path)); |
| 177 | |
| 178 | Vector<uint8_t> data = _jpeg_turbo_buffer_save_func(p_img, p_quality); |
| 179 | ERR_FAIL_COND_V(data.size() == 0, FAILED); |
| 180 | ERR_FAIL_COND_V_MSG(!file->store_buffer(data.ptr(), data.size()), FAILED, "Failed writing jpg to file"); |
| 181 | |
| 182 | return OK; |
| 183 | } |
| 184 | |
| 185 | ImageLoaderLibJPEGTurbo::ImageLoaderLibJPEGTurbo() { |
| 186 | Image::_jpg_mem_loader_func = _jpeg_turbo_mem_loader_func; |
| 187 | Image::save_jpg_func = _jpeg_turbo_save_func; |
| 188 | Image::save_jpg_buffer_func = _jpeg_turbo_buffer_save_func; |
| 189 | } |