gds_image.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 XMin, YMin;
  21. int Depth;
  22. };
  23. };
  24. } JpegCtx;
  25. static unsigned InHandler(JDEC *Decoder, uint8_t *Buf, unsigned Len) {
  26. JpegCtx *Context = (JpegCtx*) Decoder->device;
  27. if (Buf) memcpy(Buf, Context->InData + Context->InPos, Len);
  28. Context->InPos += Len;
  29. return Len;
  30. }
  31. static unsigned OutHandler(JDEC *Decoder, void *Bitmap, JRECT *Frame) {
  32. JpegCtx *Context = (JpegCtx*) Decoder->device;
  33. uint8_t *Pixels = (uint8_t*) Bitmap;
  34. for (int y = Frame->top; y <= Frame->bottom; y++) {
  35. for (int x = Frame->left; x <= Frame->right; x++) {
  36. // Convert the 888 to RGB565
  37. uint16_t Value = (*Pixels++ & ~0x07) << 8;
  38. Value |= (*Pixels++ & ~0x03) << 3;
  39. Value |= *Pixels++ >> 3;
  40. Context->OutData[Context->Width * y + x] = Value;
  41. }
  42. }
  43. return 1;
  44. }
  45. static unsigned OutHandlerDirect(JDEC *Decoder, void *Bitmap, JRECT *Frame) {
  46. JpegCtx *Context = (JpegCtx*) Decoder->device;
  47. uint8_t *Pixels = (uint8_t*) Bitmap;
  48. int Shift = 8 - Context->Depth;
  49. for (int y = Frame->top; y <= Frame->bottom; y++) {
  50. if (y < Context->YMin) continue;
  51. for (int x = Frame->left; x <= Frame->right; x++) {
  52. if (x < Context->XMin) continue;
  53. // Convert the 888 to RGB565
  54. int Value = ((Pixels[0]*11 + Pixels[1]*59 + Pixels[2]*30) / 100) >> Shift;
  55. Pixels += 3;
  56. // used DrawPixel and not "fast" version as X,Y may be beyond screen
  57. GDS_DrawPixel( Context->Device, x + Context->XOfs, y + Context->YOfs, Value);
  58. }
  59. }
  60. return 1;
  61. }
  62. //Decode the embedded image into pixel lines that can be used with the rest of the logic.
  63. static uint16_t* DecodeJPEG(uint8_t *Source, int *Width, int *Height, float Scale, bool SizeOnly) {
  64. JDEC Decoder;
  65. JpegCtx Context;
  66. char *Scratch = calloc(SCRATCH_SIZE, 1);
  67. if (!Scratch) {
  68. ESP_LOGE(TAG, "Cannot allocate workspace");
  69. return NULL;
  70. }
  71. Context.OutData = NULL;
  72. Context.InData = Source;
  73. Context.InPos = 0;
  74. //Prepare and decode the jpeg.
  75. int Res = jd_prepare(&Decoder, InHandler, Scratch, SCRATCH_SIZE, (void*) &Context);
  76. if (Width) *Width = Decoder.width;
  77. if (Height) *Height = Decoder.height;
  78. Decoder.scale = Scale;
  79. if (Res == JDR_OK && !SizeOnly) {
  80. Context.OutData = malloc(Decoder.width * Decoder.height * sizeof(uint16_t));
  81. // find the scaling factor
  82. uint8_t N = 0, ScaleInt = ceil(1.0 / Scale);
  83. ScaleInt--; ScaleInt |= ScaleInt >> 1; ScaleInt |= ScaleInt >> 2; ScaleInt++;
  84. while (ScaleInt >>= 1) N++;
  85. if (N > 3) {
  86. ESP_LOGW(TAG, "Image will not fit %dx%d", Decoder.width, Decoder.height);
  87. N = 3;
  88. }
  89. // ready to decode
  90. if (Context.OutData) {
  91. Context.Width = Decoder.width / (1 << N);
  92. Context.Height = Decoder.height / (1 << N);
  93. if (Width) *Width = Context.Width;
  94. if (Height) *Height = Context.Height;
  95. Res = jd_decomp(&Decoder, OutHandler, N);
  96. if (Res != JDR_OK) {
  97. ESP_LOGE(TAG, "Image decoder: jd_decode failed (%d)", Res);
  98. }
  99. } else {
  100. ESP_LOGE(TAG, "Can't allocate bitmap %dx%d", Decoder.width, Decoder.height);
  101. }
  102. } else if (!SizeOnly) {
  103. ESP_LOGE(TAG, "Image decoder: jd_prepare failed (%d)", Res);
  104. }
  105. // free scratch area
  106. if (Scratch) free(Scratch);
  107. return Context.OutData;
  108. }
  109. uint16_t* GDS_DecodeJPEG(uint8_t *Source, int *Width, int *Height, float Scale) {
  110. return DecodeJPEG(Source, Width, Height, Scale, false);
  111. }
  112. void GDS_GetJPEGSize(uint8_t *Source, int *Width, int *Height) {
  113. DecodeJPEG(Source, Width, Height, 1, true);
  114. }
  115. /****************************************************************************************
  116. * Simply draw a RGB 16bits image
  117. * monochrome (0.2125 * color.r) + (0.7154 * color.g) + (0.0721 * color.b)
  118. * grayscale (0.3 * R) + (0.59 * G) + (0.11 * B) )
  119. */
  120. void GDS_DrawRGB16( struct GDS_Device* Device, uint16_t *Image, int x, int y, int Width, int Height, int RGB_Mode ) {
  121. if (Device->DrawRGB16) {
  122. Device->DrawRGB16( Device, Image, x, y, Width, Height, RGB_Mode );
  123. } else {
  124. switch(RGB_Mode) {
  125. case GDS_RGB565:
  126. // 6 bits pixels to be placed. Use a linearized structure for a bit of optimization
  127. if (Device->Depth < 6) {
  128. int Scale = 6 - Device->Depth;
  129. for (int r = 0; r < Height; r++) {
  130. for (int c = 0; c < Width; c++) {
  131. int pixel = *Image++;
  132. pixel = ((((pixel & 0x1f) * 11) << 1) + ((pixel >> 5) & 0x3f) * 59 + (((pixel >> 11) * 30) << 1) + 1) / 100;
  133. GDS_DrawPixel( Device, c + x, r + y, pixel >> Scale);
  134. }
  135. }
  136. } else {
  137. int Scale = Device->Depth - 6;
  138. for (int r = 0; r < Height; r++) {
  139. for (int c = 0; c < Width; c++) {
  140. int pixel = *Image++;
  141. pixel = ((((pixel & 0x1f) * 11) << 1) + ((pixel >> 5) & 0x3f) * 59 + (((pixel >> 11) * 30) << 1) + 1) / 100;
  142. GDS_DrawPixel( Device, c + x, r + y, pixel << Scale);
  143. }
  144. }
  145. }
  146. break;
  147. case GDS_RGB555:
  148. // 5 bits pixels to be placed Use a linearized structure for a bit of optimization
  149. if (Device->Depth < 5) {
  150. int Scale = 5 - Device->Depth;
  151. for (int r = 0; r < Height; r++) {
  152. for (int c = 0; c < Width; c++) {
  153. int pixel = *Image++;
  154. pixel = ((pixel & 0x1f) * 11 + ((pixel >> 5) & 0x1f) * 59 + (pixel >> 10) * 30) / 100;
  155. GDS_DrawPixel( Device, c + x, r + y, pixel >> Scale);
  156. }
  157. }
  158. } else {
  159. int Scale = Device->Depth - 5;
  160. for (int r = 0; r < Height; r++) {
  161. for (int c = 0; c < Width; c++) {
  162. int pixel = *Image++;
  163. pixel = ((pixel & 0x1f) * 11 + ((pixel >> 5) & 0x1f) * 59 + (pixel >> 10) * 30) / 100;
  164. GDS_DrawPixel( Device, c + x, r + y, pixel << Scale);
  165. }
  166. }
  167. }
  168. break;
  169. case GDS_RGB444:
  170. // 4 bits pixels to be placed
  171. if (Device->Depth < 4) {
  172. int Scale = 4 - Device->Depth;
  173. for (int r = 0; r < Height; r++) {
  174. for (int c = 0; c < Width; c++) {
  175. int pixel = *Image++;
  176. pixel = (pixel & 0x0f) * 11 + ((pixel >> 4) & 0x0f) * 59 + (pixel >> 8) * 30;
  177. GDS_DrawPixel( Device, c + x, r + y, pixel >> Scale);
  178. }
  179. }
  180. } else {
  181. int Scale = Device->Depth - 4;
  182. for (int r = 0; r < Height; r++) {
  183. for (int c = 0; c < Width; c++) {
  184. int pixel = *Image++;
  185. pixel = (pixel & 0x0f) * 11 + ((pixel >> 4) & 0x0f) * 59 + (pixel >> 8) * 30;
  186. GDS_DrawPixel( Device, c + x, r + y, pixel << Scale);
  187. }
  188. }
  189. }
  190. break;
  191. }
  192. }
  193. Device->Dirty = true;
  194. }
  195. /****************************************************************************************
  196. * Simply draw a RGB 8 bits image (R:3,G:3,B:2) or plain grayscale
  197. * monochrome (0.2125 * color.r) + (0.7154 * color.g) + (0.0721 * color.b)
  198. * grayscale (0.3 * R) + (0.59 * G) + (0.11 * B) )
  199. */
  200. void GDS_DrawRGB8( struct GDS_Device* Device, uint8_t *Image, int x, int y, int Width, int Height, int RGB_Mode ) {
  201. if (Device->DrawRGB8) {
  202. Device->DrawRGB8( Device, Image, x, y, Width, Height, RGB_Mode );
  203. } else if (RGB_Mode == GDS_GRAYSCALE) {
  204. // 8 bits pixels
  205. int Scale = 8 - Device->Depth;
  206. for (int r = 0; r < Height; r++) {
  207. for (int c = 0; c < Width; c++) {
  208. GDS_DrawPixel( Device, c + x, r + y, *Image++ >> Scale);
  209. }
  210. }
  211. } else if (Device->Depth < 3) {
  212. // 3 bits pixels to be placed
  213. int Scale = 3 - Device->Depth;
  214. for (int r = 0; r < Height; r++) {
  215. for (int c = 0; c < Width; c++) {
  216. int pixel = *Image++;
  217. pixel = ((((pixel & 0x3) * 11) << 1) + ((pixel >> 2) & 0x7) * 59 + (pixel >> 5) * 30 + 1) / 100;
  218. GDS_DrawPixel( Device, c + x, r + y, pixel >> Scale);
  219. }
  220. }
  221. } else {
  222. // 3 bits pixels to be placed
  223. int Scale = Device->Depth - 3;
  224. for (int r = 0; r < Height; r++) {
  225. for (int c = 0; c < Width; c++) {
  226. int pixel = *Image++;
  227. pixel = ((((pixel & 0x3) * 11) << 1) + ((pixel >> 2) & 0x7) * 59 + (pixel >> 5) * 30 + 1) / 100;
  228. GDS_DrawPixel( Device, c + x, r + y, pixel << Scale);
  229. }
  230. }
  231. }
  232. Device->Dirty = true;
  233. }
  234. //Decode the embedded image into pixel lines that can be used with the rest of the logic.
  235. bool GDS_DrawJPEG( struct GDS_Device* Device, uint8_t *Source, int x, int y, int Fit) {
  236. JDEC Decoder;
  237. JpegCtx Context;
  238. bool Ret = false;
  239. char *Scratch = calloc(SCRATCH_SIZE, 1);
  240. if (!Scratch) {
  241. ESP_LOGE(TAG, "Cannot allocate workspace");
  242. return NULL;
  243. }
  244. // Populate fields of the JpegCtx struct.
  245. Context.InData = Source;
  246. Context.InPos = 0;
  247. Context.XOfs = x;
  248. Context.YOfs = y;
  249. Context.Device = Device;
  250. Context.Depth = Device->Depth;
  251. //Prepare and decode the jpeg.
  252. int Res = jd_prepare(&Decoder, InHandler, Scratch, SCRATCH_SIZE, (void*) &Context);
  253. Context.Width = Decoder.width;
  254. Context.Height = Decoder.height;
  255. if (Res == JDR_OK) {
  256. uint8_t N = 0;
  257. // do we need to fit the image
  258. if (Fit & GDS_IMAGE_FIT) {
  259. float XRatio = (Device->Width - x) / (float) Decoder.width, YRatio = (Device->Height - y) / (float) Decoder.height;
  260. uint8_t Ratio = XRatio < YRatio ? ceil(1/XRatio) : ceil(1/YRatio);
  261. Ratio--; Ratio |= Ratio >> 1; Ratio |= Ratio >> 2; Ratio++;
  262. while (Ratio >>= 1) N++;
  263. if (N > 3) {
  264. ESP_LOGW(TAG, "Image will not fit %dx%d", Decoder.width, Decoder.height);
  265. N = 3;
  266. }
  267. Context.Width /= 1 << N;
  268. Context.Height /= 1 << N;
  269. }
  270. // then place it
  271. if (Fit & GDS_IMAGE_CENTER_X) Context.XOfs = (Device->Width + x - Context.Width) / 2;
  272. else if (Fit & GDS_IMAGE_RIGHT) Context.XOfs = Device->Width - Context.Width;
  273. if (Fit & GDS_IMAGE_CENTER_Y) Context.YOfs = (Device->Height + y - Context.Height) / 2;
  274. else if (Fit & GDS_IMAGE_BOTTOM) Context.YOfs = Device->Height - Context.Height;
  275. Context.XMin = x - Context.XOfs;
  276. Context.YMin = y - Context.YOfs;
  277. // do decompress & draw
  278. Res = jd_decomp(&Decoder, OutHandlerDirect, N);
  279. if (Res == JDR_OK) {
  280. Device->Dirty = true;
  281. Ret = true;
  282. } else {
  283. ESP_LOGE(TAG, "Image decoder: jd_decode failed (%d)", Res);
  284. }
  285. } else {
  286. ESP_LOGE(TAG, "Image decoder: jd_prepare failed (%d)", Res);
  287. }
  288. // free scratch area
  289. if (Scratch) free(Scratch);
  290. return Ret;
  291. }