image_loader_jpegd.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*************************************************************************/
  2. /* image_loader_jpegd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "os/os.h"
  32. #include "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. int comps = decoder.get_num_components();
  44. if (comps == 3)
  45. comps = 4; //weird
  46. if (decoder.begin_decoding() != jpgd::JPGD_SUCCESS)
  47. return ERR_FILE_CORRUPT;
  48. const int dst_bpl = image_width * comps;
  49. DVector<uint8_t> data;
  50. data.resize(dst_bpl * image_height);
  51. DVector<uint8_t>::Write dw = data.write();
  52. jpgd::uint8 *pImage_data = (jpgd::uint8 *)dw.ptr();
  53. for (int y = 0; y < image_height; y++) {
  54. const jpgd::uint8 *pScan_line;
  55. jpgd::uint scan_line_len;
  56. if (decoder.decode((const void **)&pScan_line, &scan_line_len) != jpgd::JPGD_SUCCESS) {
  57. return ERR_FILE_CORRUPT;
  58. }
  59. jpgd::uint8 *pDst = pImage_data + y * dst_bpl;
  60. memcpy(pDst, pScan_line, dst_bpl);
  61. }
  62. //all good
  63. Image::Format fmt;
  64. if (comps == 1)
  65. fmt = Image::FORMAT_GRAYSCALE;
  66. else
  67. fmt = Image::FORMAT_RGBA;
  68. dw = DVector<uint8_t>::Write();
  69. p_image->create(image_width, image_height, 0, fmt, data);
  70. return OK;
  71. }
  72. Error ImageLoaderJPG::load_image(Image *p_image, FileAccess *f) {
  73. DVector<uint8_t> src_image;
  74. int src_image_len = f->get_len();
  75. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  76. src_image.resize(src_image_len);
  77. DVector<uint8_t>::Write w = src_image.write();
  78. f->get_buffer(&w[0], src_image_len);
  79. f->close();
  80. Error err = jpeg_load_image_from_buffer(p_image, w.ptr(), src_image_len);
  81. w = DVector<uint8_t>::Write();
  82. return err;
  83. }
  84. void ImageLoaderJPG::get_recognized_extensions(List<String> *p_extensions) const {
  85. p_extensions->push_back("jpg");
  86. p_extensions->push_back("jpeg");
  87. }
  88. static Image _jpegd_mem_loader_func(const uint8_t *p_png, int p_size) {
  89. Image img;
  90. Error err = jpeg_load_image_from_buffer(&img, p_png, p_size);
  91. if (err)
  92. ERR_PRINT("Couldn't initialize ImageLoaderJPG with the given resource.");
  93. return img;
  94. }
  95. ImageLoaderJPG::ImageLoaderJPG() {
  96. Image::_jpg_mem_loader_func = _jpegd_mem_loader_func;
  97. }