image_loader_jpegd.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**************************************************************************/
  2. /* image_loader_jpegd.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_jpegd.h"
  31. #include <jpgd.h>
  32. #include <jpge.h>
  33. #include <string.h>
  34. Error jpeg_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
  35. jpgd::jpeg_decoder_mem_stream mem_stream(p_buffer, p_buffer_len);
  36. jpgd::jpeg_decoder decoder(&mem_stream);
  37. if (decoder.get_error_code() != jpgd::JPGD_SUCCESS) {
  38. return ERR_CANT_OPEN;
  39. }
  40. const int image_width = decoder.get_width();
  41. const int image_height = decoder.get_height();
  42. const int comps = decoder.get_num_components();
  43. if (comps != 1 && comps != 3) {
  44. return ERR_FILE_CORRUPT;
  45. }
  46. if (decoder.begin_decoding() != jpgd::JPGD_SUCCESS) {
  47. return ERR_FILE_CORRUPT;
  48. }
  49. const int dst_bpl = image_width * comps;
  50. Vector<uint8_t> data;
  51. data.resize(dst_bpl * image_height);
  52. uint8_t *dw = data.ptrw();
  53. jpgd::uint8 *pImage_data = (jpgd::uint8 *)dw;
  54. for (int y = 0; y < image_height; y++) {
  55. const jpgd::uint8 *pScan_line;
  56. jpgd::uint scan_line_len;
  57. if (decoder.decode((const void **)&pScan_line, &scan_line_len) != jpgd::JPGD_SUCCESS) {
  58. return ERR_FILE_CORRUPT;
  59. }
  60. jpgd::uint8 *pDst = pImage_data + y * dst_bpl;
  61. if (comps == 1) {
  62. memcpy(pDst, pScan_line, dst_bpl);
  63. } else {
  64. // For images with more than 1 channel pScan_line will always point to a buffer
  65. // containing 32-bit RGBA pixels. Alpha is always 255 and we ignore it.
  66. for (int x = 0; x < image_width; x++) {
  67. pDst[0] = pScan_line[x * 4 + 0];
  68. pDst[1] = pScan_line[x * 4 + 1];
  69. pDst[2] = pScan_line[x * 4 + 2];
  70. pDst += 3;
  71. }
  72. }
  73. }
  74. //all good
  75. Image::Format fmt;
  76. if (comps == 1) {
  77. fmt = Image::FORMAT_L8;
  78. } else {
  79. fmt = Image::FORMAT_RGB8;
  80. }
  81. p_image->set_data(image_width, image_height, false, fmt, data);
  82. return OK;
  83. }
  84. Error ImageLoaderJPG::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
  85. Vector<uint8_t> src_image;
  86. uint64_t src_image_len = f->get_length();
  87. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  88. src_image.resize(src_image_len);
  89. uint8_t *w = src_image.ptrw();
  90. f->get_buffer(&w[0], src_image_len);
  91. Error err = jpeg_load_image_from_buffer(p_image.ptr(), w, src_image_len);
  92. return err;
  93. }
  94. void ImageLoaderJPG::get_recognized_extensions(List<String> *p_extensions) const {
  95. p_extensions->push_back("jpg");
  96. p_extensions->push_back("jpeg");
  97. }
  98. static Ref<Image> _jpegd_mem_loader_func(const uint8_t *p_png, int p_size) {
  99. Ref<Image> img;
  100. img.instantiate();
  101. Error err = jpeg_load_image_from_buffer(img.ptr(), p_png, p_size);
  102. ERR_FAIL_COND_V(err, Ref<Image>());
  103. return img;
  104. }
  105. class ImageLoaderJPGOSFile : public jpge::output_stream {
  106. public:
  107. Ref<FileAccess> f;
  108. virtual bool put_buf(const void *Pbuf, int len) {
  109. f->store_buffer((const uint8_t *)Pbuf, len);
  110. return true;
  111. }
  112. };
  113. class ImageLoaderJPGOSBuffer : public jpge::output_stream {
  114. public:
  115. Vector<uint8_t> *buffer = nullptr;
  116. virtual bool put_buf(const void *Pbuf, int len) {
  117. uint32_t base = buffer->size();
  118. buffer->resize(base + len);
  119. memcpy(buffer->ptrw() + base, Pbuf, len);
  120. return true;
  121. }
  122. };
  123. static Error _jpgd_save_to_output_stream(jpge::output_stream *p_output_stream, const Ref<Image> &p_img, float p_quality) {
  124. ERR_FAIL_COND_V(p_img.is_null() || p_img->is_empty(), ERR_INVALID_PARAMETER);
  125. Ref<Image> image = p_img->duplicate();
  126. if (image->is_compressed()) {
  127. Error error = image->decompress();
  128. ERR_FAIL_COND_V_MSG(error != OK, error, "Couldn't decompress image.");
  129. }
  130. if (image->get_format() != Image::FORMAT_RGB8) {
  131. image = image->duplicate();
  132. image->convert(Image::FORMAT_RGB8);
  133. }
  134. jpge::params p;
  135. p.m_quality = CLAMP(p_quality * 100, 1, 100);
  136. jpge::jpeg_encoder enc;
  137. enc.init(p_output_stream, image->get_width(), image->get_height(), 3, p);
  138. const uint8_t *src_data = image->get_data().ptr();
  139. for (int i = 0; i < image->get_height(); i++) {
  140. if (!enc.process_scanline(&src_data[i * image->get_width() * 3])) {
  141. return FAILED;
  142. }
  143. }
  144. if (enc.process_scanline(nullptr)) {
  145. return OK;
  146. } else {
  147. return FAILED;
  148. }
  149. }
  150. static Vector<uint8_t> _jpgd_buffer_save_func(const Ref<Image> &p_img, float p_quality) {
  151. Vector<uint8_t> output;
  152. ImageLoaderJPGOSBuffer ob;
  153. ob.buffer = &output;
  154. if (_jpgd_save_to_output_stream(&ob, p_img, p_quality) != OK) {
  155. return Vector<uint8_t>();
  156. }
  157. return output;
  158. }
  159. static Error _jpgd_save_func(const String &p_path, const Ref<Image> &p_img, float p_quality) {
  160. Error err;
  161. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  162. ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save JPG at path: '%s'.", p_path));
  163. ImageLoaderJPGOSFile ob;
  164. ob.f = file;
  165. return _jpgd_save_to_output_stream(&ob, p_img, p_quality);
  166. }
  167. ImageLoaderJPG::ImageLoaderJPG() {
  168. Image::_jpg_mem_loader_func = _jpegd_mem_loader_func;
  169. Image::save_jpg_func = _jpgd_save_func;
  170. Image::save_jpg_buffer_func = _jpgd_buffer_save_func;
  171. }