gds_image.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <string.h>
  2. #include "math.h"
  3. #include "esp32/rom/tjpgd.h"
  4. #include "esp_log.h"
  5. #include "gds.h"
  6. #include "gds_private.h"
  7. #include "gds_image.h"
  8. const char TAG[] = "ImageDec";
  9. #define SCRATCH_SIZE 3100
  10. //Data that is passed from the decoder function to the infunc/outfunc functions.
  11. typedef struct {
  12. const unsigned char *InData; // Pointer to jpeg data
  13. int InPos; // Current position in jpeg data
  14. int Width, Height;
  15. union {
  16. uint16_t *OutData; // Decompress
  17. struct { // DirectDraw
  18. struct GDS_Device * Device;
  19. int XOfs, YOfs;
  20. int Depth;
  21. };
  22. };
  23. } JpegCtx;
  24. static unsigned InHandler(JDEC *Decoder, uint8_t *Buf, unsigned Len) {
  25. JpegCtx *Context = (JpegCtx*) Decoder->device;
  26. if (Buf) memcpy(Buf, Context->InData + Context->InPos, Len);
  27. Context->InPos += Len;
  28. return Len;
  29. }
  30. static unsigned OutHandler(JDEC *Decoder, void *Bitmap, JRECT *Frame) {
  31. JpegCtx *Context = (JpegCtx*) Decoder->device;
  32. uint8_t *Pixels = (uint8_t*) Bitmap;
  33. for (int y = Frame->top; y <= Frame->bottom; y++) {
  34. if (y < Context->YOfs) continue;
  35. for (int x = Frame->left; x <= Frame->right; x++) {
  36. if (x < Context->XOfs) continue;
  37. // Convert the 888 to RGB565
  38. uint16_t Value = (*Pixels++ & ~0x07) << 8;
  39. Value |= (*Pixels++ & ~0x03) << 3;
  40. Value |= *Pixels++ >> 3;
  41. Context->OutData[Context->Width * y + x] = Value;
  42. }
  43. }
  44. return 1;
  45. }
  46. static unsigned OutHandlerDirect(JDEC *Decoder, void *Bitmap, JRECT *Frame) {
  47. JpegCtx *Context = (JpegCtx*) Decoder->device;
  48. uint8_t *Pixels = (uint8_t*) Bitmap;
  49. int Shift = 8 - Context->Depth;
  50. for (int y = Frame->top; y <= Frame->bottom; y++) {
  51. for (int x = Frame->left; x <= Frame->right; x++) {
  52. // Convert the 888 to RGB565
  53. int Value = ((Pixels[0]*11 + Pixels[1]*59 + Pixels[2]*30) / 100) >> Shift;
  54. Pixels += 3;
  55. // used DrawPixel and not "fast" version as X,Y may be beyond screen
  56. GDS_DrawPixel( Context->Device, Context->XOfs + x, Context->YOfs + y, Value);
  57. }
  58. }
  59. return 1;
  60. }
  61. //Decode the embedded image into pixel lines that can be used with the rest of the logic.
  62. static uint16_t* DecodeJPEG(uint8_t *Source, int *Width, int *Height, float Scale, bool SizeOnly) {
  63. JDEC Decoder;
  64. JpegCtx Context;
  65. char *Scratch = calloc(SCRATCH_SIZE, 1);
  66. if (!Scratch) {
  67. ESP_LOGE(TAG, "Cannot allocate workspace");
  68. return NULL;
  69. }
  70. Context.OutData = NULL;
  71. Context.InData = Source;
  72. Context.InPos = 0;
  73. //Prepare and decode the jpeg.
  74. int Res = jd_prepare(&Decoder, InHandler, Scratch, SCRATCH_SIZE, (void*) &Context);
  75. if (Width) *Width = Decoder.width;
  76. if (Height) *Height = Decoder.height;
  77. Decoder.scale = Scale;
  78. if (Res == JDR_OK && !SizeOnly) {
  79. // find the scaling factor
  80. Context.OutData = malloc(Decoder.width * Decoder.height * sizeof(uint16_t));
  81. uint8_t N = 0, ScaleInt = ceil(1.0 / Scale);
  82. ScaleInt--; ScaleInt |= ScaleInt >> 1; ScaleInt |= ScaleInt >> 2; ScaleInt++;
  83. while (ScaleInt >>= 1) N++;
  84. // ready to decode
  85. if (Context.OutData) {
  86. Context.Width = Decoder.width / (1 << N);
  87. Context.Height = Decoder.height / (1 << N);
  88. if (Width) *Width = Context.Width;
  89. if (Height) *Height = Context.Height;
  90. Res = jd_decomp(&Decoder, OutHandler, N > 3 ? 3 : N);
  91. if (Res != JDR_OK) {
  92. ESP_LOGE(TAG, "Image decoder: jd_decode failed (%d)", Res);
  93. }
  94. } else {
  95. ESP_LOGE(TAG, "Can't allocate bitmap %dx%d", Decoder.width, Decoder.height);
  96. }
  97. } else if (!SizeOnly) {
  98. ESP_LOGE(TAG, "Image decoder: jd_prepare failed (%d)", Res);
  99. }
  100. // free scratch area
  101. if (Scratch) free(Scratch);
  102. return Context.OutData;
  103. }
  104. uint16_t* GDS_DecodeJPEG(uint8_t *Source, int *Width, int *Height, float Scale) {
  105. return DecodeJPEG(Source, Width, Height, Scale, false);
  106. }
  107. void GDS_GetJPEGSize(uint8_t *Source, int *Width, int *Height) {
  108. DecodeJPEG(Source, Width, Height, 1, true);
  109. }
  110. /****************************************************************************************
  111. * Simply draw a RGB565 image
  112. * monoschrome (0.2125 * color.r) + (0.7154 * color.g) + (0.0721 * color.b)
  113. * grayscale (0.3 * R) + (0.59 * G) + (0.11 * B) )
  114. */
  115. void GDS_DrawRGB16( struct GDS_Device* Device, uint16_t *Image, int x, int y, int Width, int Height, int RGB_Mode ) {
  116. if (Device->DrawRGB16) {
  117. Device->DrawRGB16( Device, x, y, Width, Height, RGB_Mode, Image );
  118. } else {
  119. int Scale = Device->Depth < 5 ? 5 - Device->Depth : 0;
  120. switch(RGB_Mode) {
  121. case GDS_RGB565:
  122. for (int c = 0; c < Width; c++) {
  123. for (int r = 0; r < Height; r++) {
  124. int pixel = Image[Width*r + c];
  125. pixel = ((pixel & 0x1f) * 11 + ((((pixel >> 5) & 0x3f) * 59) >> 1) + (pixel >> 11) * 30) / 100;
  126. GDS_DrawPixel( Device, c + x, r + y, pixel >> Scale);
  127. }
  128. }
  129. break;
  130. case GDS_RGB555:
  131. for (int c = 0; c < Width; c++) {
  132. for (int r = 0; r < Height; r++) {
  133. int pixel = Image[Width*r + c];
  134. pixel = ((pixel & 0x1f) * 11 + ((pixel >> 5) & 0x1f) * 59 + (pixel >> 10) * 30) / 100;
  135. GDS_DrawPixel( Device, c + x, r + y, pixel >> Scale);
  136. }
  137. }
  138. break;
  139. case GDS_RGB444:
  140. for (int c = 0; c < Width; c++) {
  141. for (int r = 0; r < Height; r++) {
  142. int pixel = Image[Width*r + c];
  143. pixel = (pixel & 0x0f) * 11 + ((pixel >> 4) & 0x0f) * 59 + (pixel >> 8) * 30;
  144. GDS_DrawPixel( Device, c + x, r + y, pixel >> (Scale - 1));
  145. }
  146. }
  147. break;
  148. }
  149. }
  150. }
  151. //Decode the embedded image into pixel lines that can be used with the rest of the logic.
  152. bool GDS_DrawJPEG( struct GDS_Device* Device, uint8_t *Source, int x, int y, int Fit) {
  153. JDEC Decoder;
  154. JpegCtx Context;
  155. bool Ret = false;
  156. char *Scratch = calloc(SCRATCH_SIZE, 1);
  157. if (!Scratch) {
  158. ESP_LOGE(TAG, "Cannot allocate workspace");
  159. return NULL;
  160. }
  161. // Populate fields of the JpegCtx struct.
  162. Context.InData = Source;
  163. Context.InPos = 0;
  164. Context.XOfs = x;
  165. Context.YOfs = y;
  166. Context.Device = Device;
  167. Context.Depth = Device->Depth;
  168. //Prepare and decode the jpeg.
  169. int Res = jd_prepare(&Decoder, InHandler, Scratch, SCRATCH_SIZE, (void*) &Context);
  170. Context.Width = Decoder.width;
  171. Context.Height = Decoder.height;
  172. if (Res == JDR_OK) {
  173. uint8_t N = 0;
  174. // do we need to fit the image
  175. if (Fit & GDS_IMAGE_FIT) {
  176. float XRatio = (Device->Width - x) / (float) Decoder.width, YRatio = (Device->Height - y) / (float) Decoder.height;
  177. uint8_t Ratio = XRatio < YRatio ? ceil(1/XRatio) : ceil(1/YRatio);
  178. Ratio--; Ratio |= Ratio >> 1; Ratio |= Ratio >> 2; Ratio++;
  179. while (Ratio >>= 1) N++;
  180. Context.Width /= 1 << N;
  181. Context.Height /= 1 << N;
  182. }
  183. // then place it
  184. if (Fit & GDS_IMAGE_CENTER_X) Context.XOfs = x + ((Device->Width - x) - Context.Width) / 2;
  185. if (Fit & GDS_IMAGE_CENTER_Y) Context.YOfs = y + ((Device->Height - y) - Context.Height) / 2;
  186. // do decompress & draw
  187. Res = jd_decomp(&Decoder, OutHandlerDirect, N > 3 ? 3 : N);
  188. if (Res == JDR_OK) {
  189. Ret = true;
  190. } else {
  191. ESP_LOGE(TAG, "Image decoder: jd_decode failed (%d)", Res);
  192. }
  193. } else {
  194. ESP_LOGE(TAG, "Image decoder: jd_prepare failed (%d)", Res);
  195. }
  196. // free scratch area
  197. if (Scratch) free(Scratch);
  198. return Ret;
  199. }