image_loader_libjpeg_turbo.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include "image_loader_libjpeg_turbo.h"
  31. #include <turbojpeg.h>
  32. Error jpeg_turbo_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
  33. tjhandle tj_instance = tj3Init(TJINIT_DECOMPRESS);
  34. if (tj_instance == NULL) {
  35. return FAILED;
  36. }
  37. if (tj3DecompressHeader(tj_instance, p_buffer, p_buffer_len) < 0) {
  38. tj3Destroy(tj_instance);
  39. return ERR_FILE_CORRUPT;
  40. }
  41. const unsigned int width = tj3Get(tj_instance, TJPARAM_JPEGWIDTH);
  42. const unsigned int height = tj3Get(tj_instance, TJPARAM_JPEGHEIGHT);
  43. const TJCS colorspace = (TJCS)tj3Get(tj_instance, TJPARAM_COLORSPACE);
  44. if (tj3Get(tj_instance, TJPARAM_PRECISION) > 8) {
  45. // Proceed anyway and convert to rgb8?
  46. tj3Destroy(tj_instance);
  47. return ERR_UNAVAILABLE;
  48. }
  49. TJPF tj_pixel_format;
  50. Image::Format gd_pixel_format;
  51. if (colorspace == TJCS_GRAY) {
  52. tj_pixel_format = TJPF_GRAY;
  53. gd_pixel_format = Image::FORMAT_L8;
  54. } else {
  55. // Force everything else (RGB, CMYK etc) into RGB8.
  56. tj_pixel_format = TJPF_RGB;
  57. gd_pixel_format = Image::FORMAT_RGB8;
  58. }
  59. Vector<uint8_t> data;
  60. data.resize(width * height * tjPixelSize[tj_pixel_format]);
  61. if (tj3Decompress8(tj_instance, p_buffer, p_buffer_len, data.ptrw(), 0, tj_pixel_format) < 0) {
  62. tj3Destroy(tj_instance);
  63. return ERR_FILE_CORRUPT;
  64. }
  65. tj3Destroy(tj_instance);
  66. p_image->set_data(width, height, false, gd_pixel_format, data);
  67. return OK;
  68. }
  69. Error ImageLoaderLibJPEGTurbo::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
  70. Vector<uint8_t> src_image;
  71. uint64_t src_image_len = f->get_length();
  72. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  73. src_image.resize(src_image_len);
  74. uint8_t *w = src_image.ptrw();
  75. f->get_buffer(&w[0], src_image_len);
  76. Error err = jpeg_turbo_load_image_from_buffer(p_image.ptr(), w, src_image_len);
  77. return err;
  78. }
  79. void ImageLoaderLibJPEGTurbo::get_recognized_extensions(List<String> *p_extensions) const {
  80. p_extensions->push_back("jpg");
  81. p_extensions->push_back("jpeg");
  82. }
  83. static Ref<Image> _jpeg_turbo_mem_loader_func(const uint8_t *p_png, int p_size) {
  84. Ref<Image> img;
  85. img.instantiate();
  86. Error err = jpeg_turbo_load_image_from_buffer(img.ptr(), p_png, p_size);
  87. ERR_FAIL_COND_V(err, Ref<Image>());
  88. return img;
  89. }
  90. static Vector<uint8_t> _jpeg_turbo_buffer_save_func(const Ref<Image> &p_img, float p_quality) {
  91. Vector<uint8_t> output;
  92. ERR_FAIL_COND_V(p_img.is_null() || p_img->is_empty(), output);
  93. Ref<Image> image = p_img->duplicate();
  94. if (image->is_compressed()) {
  95. Error error = image->decompress();
  96. ERR_FAIL_COND_V_MSG(error != OK, output, "Couldn't decompress image.");
  97. }
  98. if (image->get_format() != Image::FORMAT_RGB8) {
  99. // Allow grayscale L8?
  100. image = image->duplicate();
  101. image->convert(Image::FORMAT_RGB8);
  102. }
  103. tjhandle tj_instance = tj3Init(TJINIT_COMPRESS);
  104. ERR_FAIL_COND_V_MSG(tj_instance == NULL, output, "Couldn't create tjhandle");
  105. if (tj3Set(tj_instance, TJPARAM_QUALITY, (int)(p_quality * 100)) < 0) {
  106. tj3Destroy(tj_instance);
  107. ERR_FAIL_V_MSG(output, "Couldn't set jpg quality");
  108. }
  109. if (tj3Set(tj_instance, TJPARAM_PRECISION, 8) < 0) {
  110. tj3Destroy(tj_instance);
  111. ERR_FAIL_V_MSG(output, "Couldn't set jpg precision");
  112. }
  113. if (tj3Set(tj_instance, TJPARAM_SUBSAMP, TJSAMP_420) < 0) {
  114. tj3Destroy(tj_instance);
  115. ERR_FAIL_V_MSG(output, "Couldn't set jpg subsamples");
  116. }
  117. // If the godot image format is `Image::FORMAT_L8` we could set the appropriate
  118. // color space here rather than defaulting to RGB.
  119. unsigned char *jpeg_buff = NULL;
  120. size_t jpeg_size = 0;
  121. int code = tj3Compress8(
  122. tj_instance,
  123. image->get_data().ptr(),
  124. image->get_width(),
  125. 0,
  126. image->get_height(),
  127. TJPF_RGB,
  128. &jpeg_buff,
  129. &jpeg_size);
  130. if (code < 0) {
  131. tj3Destroy(tj_instance);
  132. tj3Free(jpeg_buff);
  133. ERR_FAIL_V_MSG(output, "Couldn't compress jpg");
  134. }
  135. output.resize(jpeg_size);
  136. memcpy(output.ptrw(), jpeg_buff, jpeg_size);
  137. tj3Destroy(tj_instance);
  138. tj3Free(jpeg_buff);
  139. return output;
  140. }
  141. static Error _jpeg_turbo_save_func(const String &p_path, const Ref<Image> &p_img, float p_quality) {
  142. Error err;
  143. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  144. ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save JPG at path: '%s'.", p_path));
  145. Vector<uint8_t> data = _jpeg_turbo_buffer_save_func(p_img, p_quality);
  146. ERR_FAIL_COND_V(data.size() == 0, FAILED);
  147. ERR_FAIL_COND_V_MSG(!file->store_buffer(data.ptr(), data.size()), FAILED, "Failed writing jpg to file");
  148. return OK;
  149. }
  150. ImageLoaderLibJPEGTurbo::ImageLoaderLibJPEGTurbo() {
  151. Image::_jpg_mem_loader_func = _jpeg_turbo_mem_loader_func;
  152. Image::save_jpg_func = _jpeg_turbo_save_func;
  153. Image::save_jpg_buffer_func = _jpeg_turbo_buffer_save_func;
  154. }