esp_jpg_decode.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "rom/tjpgd.h"
  16. #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
  17. #include "esp32-hal-log.h"
  18. #define TAG ""
  19. #else
  20. #include "esp_log.h"
  21. static const char* TAG = "esp_jpg_decode";
  22. #endif
  23. typedef struct {
  24. jpg_scale_t scale;
  25. jpg_reader_cb reader;
  26. jpg_writer_cb writer;
  27. void * arg;
  28. size_t len;
  29. size_t index;
  30. } esp_jpg_decoder_t;
  31. static const char * jd_errors[] = {
  32. "Succeeded",
  33. "Interrupted by output function",
  34. "Device error or wrong termination of input stream",
  35. "Insufficient memory pool for the image",
  36. "Insufficient stream input buffer",
  37. "Parameter error",
  38. "Data format error",
  39. "Right format but not supported",
  40. "Not supported JPEG standard"
  41. };
  42. static uint32_t _jpg_write(JDEC *decoder, void *bitmap, JRECT *rect)
  43. {
  44. uint16_t x = rect->left;
  45. uint16_t y = rect->top;
  46. uint16_t w = rect->right + 1 - x;
  47. uint16_t h = rect->bottom + 1 - y;
  48. uint8_t *data = (uint8_t *)bitmap;
  49. esp_jpg_decoder_t * jpeg = (esp_jpg_decoder_t *)decoder->device;
  50. if (jpeg->writer) {
  51. return jpeg->writer(jpeg->arg, x, y, w, h, data);
  52. }
  53. return 0;
  54. }
  55. static uint32_t _jpg_read(JDEC *decoder, uint8_t *buf, uint32_t len)
  56. {
  57. esp_jpg_decoder_t * jpeg = (esp_jpg_decoder_t *)decoder->device;
  58. if (jpeg->len && len > (jpeg->len - jpeg->index)) {
  59. len = jpeg->len - jpeg->index;
  60. }
  61. if (len) {
  62. len = jpeg->reader(jpeg->arg, jpeg->index, buf, len);
  63. if (!len) {
  64. ESP_LOGE(TAG, "Read Fail at %u/%u", jpeg->index, jpeg->len);
  65. }
  66. jpeg->index += len;
  67. }
  68. return len;
  69. }
  70. esp_err_t esp_jpg_decode(size_t len, jpg_scale_t scale, jpg_reader_cb reader, jpg_writer_cb writer, void * arg)
  71. {
  72. static uint8_t work[3100];
  73. JDEC decoder;
  74. esp_jpg_decoder_t jpeg;
  75. jpeg.len = len;
  76. jpeg.reader = reader;
  77. jpeg.writer = writer;
  78. jpeg.arg = arg;
  79. jpeg.scale = scale;
  80. jpeg.index = 0;
  81. JRESULT jres = jd_prepare(&decoder, _jpg_read, work, 3100, &jpeg);
  82. if(jres != JDR_OK){
  83. ESP_LOGE(TAG, "JPG Header Parse Failed! %s", jd_errors[jres]);
  84. return ESP_FAIL;
  85. }
  86. uint16_t output_width = decoder.width / (1 << (uint8_t)(jpeg.scale));
  87. uint16_t output_height = decoder.height / (1 << (uint8_t)(jpeg.scale));
  88. //output start
  89. writer(arg, 0, 0, output_width, output_height, NULL);
  90. //output write
  91. jres = jd_decomp(&decoder, _jpg_write, (uint8_t)jpeg.scale);
  92. //output end
  93. writer(arg, output_width, output_height, output_width, output_height, NULL);
  94. if (jres != JDR_OK) {
  95. ESP_LOGE(TAG, "JPG Decompression Failed! %s", jd_errors[jres]);
  96. return ESP_FAIL;
  97. }
  98. //check if all data has been consumed.
  99. if (len && jpeg.index < len) {
  100. _jpg_read(&decoder, NULL, len - jpeg.index);
  101. }
  102. return ESP_OK;
  103. }