image_loader_jpegd.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "core/os/os.h"
  32. #include "core/print_string.h"
  33. #include <jpgd.h>
  34. #include <string.h>
  35. Error jpeg_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
  36. jpgd::jpeg_decoder_mem_stream mem_stream(p_buffer, p_buffer_len);
  37. jpgd::jpeg_decoder decoder(&mem_stream);
  38. if (decoder.get_error_code() != jpgd::JPGD_SUCCESS) {
  39. return ERR_CANT_OPEN;
  40. }
  41. const int image_width = decoder.get_width();
  42. const int image_height = decoder.get_height();
  43. const int comps = decoder.get_num_components();
  44. if (comps != 1 && comps != 3) {
  45. return ERR_FILE_CORRUPT;
  46. }
  47. if (decoder.begin_decoding() != jpgd::JPGD_SUCCESS) {
  48. return ERR_FILE_CORRUPT;
  49. }
  50. const int dst_bpl = image_width * comps;
  51. PoolVector<uint8_t> data;
  52. data.resize(dst_bpl * image_height);
  53. PoolVector<uint8_t>::Write dw = data.write();
  54. jpgd::uint8 *pImage_data = (jpgd::uint8 *)dw.ptr();
  55. for (int y = 0; y < image_height; y++) {
  56. const jpgd::uint8 *pScan_line;
  57. jpgd::uint scan_line_len;
  58. if (decoder.decode((const void **)&pScan_line, &scan_line_len) != jpgd::JPGD_SUCCESS) {
  59. return ERR_FILE_CORRUPT;
  60. }
  61. jpgd::uint8 *pDst = pImage_data + y * dst_bpl;
  62. if (comps == 1) {
  63. memcpy(pDst, pScan_line, dst_bpl);
  64. } else {
  65. // For images with more than 1 channel pScan_line will always point to a buffer
  66. // containing 32-bit RGBA pixels. Alpha is always 255 and we ignore it.
  67. for (int x = 0; x < image_width; x++) {
  68. pDst[0] = pScan_line[x * 4 + 0];
  69. pDst[1] = pScan_line[x * 4 + 1];
  70. pDst[2] = pScan_line[x * 4 + 2];
  71. pDst += 3;
  72. }
  73. }
  74. }
  75. //all good
  76. Image::Format fmt;
  77. if (comps == 1) {
  78. fmt = Image::FORMAT_L8;
  79. } else {
  80. fmt = Image::FORMAT_RGB8;
  81. }
  82. dw.release();
  83. p_image->create(image_width, image_height, false, fmt, data);
  84. return OK;
  85. }
  86. Error ImageLoaderJPG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
  87. PoolVector<uint8_t> src_image;
  88. uint64_t src_image_len = f->get_len();
  89. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  90. src_image.resize(src_image_len);
  91. PoolVector<uint8_t>::Write w = src_image.write();
  92. f->get_buffer(&w[0], src_image_len);
  93. f->close();
  94. Error err = jpeg_load_image_from_buffer(p_image.ptr(), w.ptr(), src_image_len);
  95. return err;
  96. }
  97. void ImageLoaderJPG::get_recognized_extensions(List<String> *p_extensions) const {
  98. p_extensions->push_back("jpg");
  99. p_extensions->push_back("jpeg");
  100. }
  101. static Ref<Image> _jpegd_mem_loader_func(const uint8_t *p_png, int p_size) {
  102. Ref<Image> img;
  103. img.instance();
  104. Error err = jpeg_load_image_from_buffer(img.ptr(), p_png, p_size);
  105. ERR_FAIL_COND_V(err, Ref<Image>());
  106. return img;
  107. }
  108. ImageLoaderJPG::ImageLoaderJPG() {
  109. Image::_jpg_mem_loader_func = _jpegd_mem_loader_func;
  110. }