esp_jpg_decode.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "esp_jpg_decode.h"
  15. #include "esp_system.h"
  16. #if ESP_IDF_VERSION_MAJOR >= 4 // IDF 4+
  17. #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
  18. #include "esp32/rom/tjpgd.h"
  19. #else
  20. #error Target CONFIG_IDF_TARGET is not supported
  21. #endif
  22. #else // ESP32 Before IDF 4.0
  23. #include "rom/tjpgd.h"
  24. #endif
  25. #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
  26. #include "esp32-hal-log.h"
  27. #define TAG ""
  28. #else
  29. #include "esp_log.h"
  30. static const char* TAG = "esp_jpg_decode";
  31. #endif
  32. typedef struct {
  33. jpg_scale_t scale;
  34. jpg_reader_cb reader;
  35. jpg_writer_cb writer;
  36. void * arg;
  37. size_t len;
  38. size_t index;
  39. } esp_jpg_decoder_t;
  40. static const char * jd_errors[] = {
  41. "Succeeded",
  42. "Interrupted by output function",
  43. "Device error or wrong termination of input stream",
  44. "Insufficient memory pool for the image",
  45. "Insufficient stream input buffer",
  46. "Parameter error",
  47. "Data format error",
  48. "Right format but not supported",
  49. "Not supported JPEG standard"
  50. };
  51. static uint32_t _jpg_write(JDEC *decoder, void *bitmap, JRECT *rect)
  52. {
  53. uint16_t x = rect->left;
  54. uint16_t y = rect->top;
  55. uint16_t w = rect->right + 1 - x;
  56. uint16_t h = rect->bottom + 1 - y;
  57. uint8_t *data = (uint8_t *)bitmap;
  58. esp_jpg_decoder_t * jpeg = (esp_jpg_decoder_t *)decoder->device;
  59. if (jpeg->writer) {
  60. return jpeg->writer(jpeg->arg, x, y, w, h, data);
  61. }
  62. return 0;
  63. }
  64. static uint32_t _jpg_read(JDEC *decoder, uint8_t *buf, uint32_t len)
  65. {
  66. esp_jpg_decoder_t * jpeg = (esp_jpg_decoder_t *)decoder->device;
  67. if (jpeg->len && len > (jpeg->len - jpeg->index)) {
  68. len = jpeg->len - jpeg->index;
  69. }
  70. if (len) {
  71. len = jpeg->reader(jpeg->arg, jpeg->index, buf, len);
  72. if (!len) {
  73. ESP_LOGE(TAG, "Read Fail at %u/%u", jpeg->index, jpeg->len);
  74. }
  75. jpeg->index += len;
  76. }
  77. return len;
  78. }
  79. esp_err_t esp_jpg_decode(size_t len, jpg_scale_t scale, jpg_reader_cb reader, jpg_writer_cb writer, void * arg)
  80. {
  81. static uint8_t work[3100];
  82. JDEC decoder;
  83. esp_jpg_decoder_t jpeg;
  84. jpeg.len = len;
  85. jpeg.reader = reader;
  86. jpeg.writer = writer;
  87. jpeg.arg = arg;
  88. jpeg.scale = scale;
  89. jpeg.index = 0;
  90. JRESULT jres = jd_prepare(&decoder, _jpg_read, work, 3100, &jpeg);
  91. if(jres != JDR_OK){
  92. ESP_LOGE(TAG, "JPG Header Parse Failed! %s", jd_errors[jres]);
  93. return ESP_FAIL;
  94. }
  95. uint16_t output_width = decoder.width / (1 << (uint8_t)(jpeg.scale));
  96. uint16_t output_height = decoder.height / (1 << (uint8_t)(jpeg.scale));
  97. //output start
  98. writer(arg, 0, 0, output_width, output_height, NULL);
  99. //output write
  100. jres = jd_decomp(&decoder, _jpg_write, (uint8_t)jpeg.scale);
  101. //output end
  102. writer(arg, output_width, output_height, output_width, output_height, NULL);
  103. if (jres != JDR_OK) {
  104. ESP_LOGE(TAG, "JPG Decompression Failed! %s", jd_errors[jres]);
  105. return ESP_FAIL;
  106. }
  107. //check if all data has been consumed.
  108. if (len && jpeg.index < len) {
  109. _jpg_read(&decoder, NULL, len - jpeg.index);
  110. }
  111. return ESP_OK;
  112. }