gds_image.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * (c) Philippe G. 2019, philippe_44@outlook.com
  3. *
  4. * This software is released under the MIT License.
  5. * https://opensource.org/licenses/MIT
  6. *
  7. */
  8. #include <string.h>
  9. #include "math.h"
  10. #include "esp32/rom/tjpgd.h"
  11. #include "esp_log.h"
  12. #include "gds.h"
  13. #include "gds_private.h"
  14. #include "gds_image.h"
  15. const char TAG[] = "ImageDec";
  16. #define SCRATCH_SIZE 3100
  17. //Data that is passed from the decoder function to the infunc/outfunc functions.
  18. typedef struct {
  19. const unsigned char *InData; // Pointer to jpeg data
  20. int InPos; // Current position in jpeg data
  21. int Width, Height;
  22. uint8_t Mode;
  23. union {
  24. void *OutData;
  25. struct { // DirectDraw
  26. struct GDS_Device *Device;
  27. int XOfs, YOfs;
  28. int XMin, YMin;
  29. int Depth;
  30. };
  31. };
  32. } JpegCtx;
  33. /****************************************************************************************
  34. * RGB conversion (24 bits 888: RRRRRRRRGGGGGGGGBBBBBBBB and 16 bits 565: RRRRRGGGGGGBBBBB = B31..B0)
  35. * so in other words for an array of 888 bytes: [0]=B, [1]=G, [2]=R, ...
  36. * monochrome (0.2125 * color.r) + (0.7154 * color.g) + (0.0721 * color.b)
  37. * grayscale (0.3 * R) + (0.59 * G) + (0.11 * B) )
  38. */
  39. static inline int Scaler332(uint8_t *Pixels) {
  40. return (Pixels[2] & ~0x1f) | ((Pixels[1] & ~0x1f) >> 3) | (Pixels[0] >> 6);
  41. }
  42. static inline int Scaler444(uint8_t *Pixels) {
  43. return ((Pixels[2] & ~0x0f) << 4) | (Pixels[1] & ~0x0f) | (Pixels[0] >> 4);
  44. }
  45. static inline int Scaler555(uint8_t *Pixels) {
  46. return ((Pixels[2] & ~0x07) << 7) | ((Pixels[1] & ~0x07) << 2) | (Pixels[0] >> 3);
  47. }
  48. static inline int Scaler565(uint8_t *Pixels) {
  49. return ((Pixels[2] & ~0x07) << 8) | ((Pixels[1] & ~0x03) << 3) | (Pixels[0] >> 3);
  50. }
  51. static inline int Scaler666(uint8_t *Pixels) {
  52. return ((Pixels[2] & ~0x03) << 10) | ((Pixels[1] & ~0x03) << 4) | (Pixels[0] >> 2);
  53. }
  54. static inline int Scaler888(uint8_t *Pixels) {
  55. return (Pixels[2] << 16) | (Pixels[1] << 8) | Pixels[0];
  56. }
  57. static inline int ScalerGray(uint8_t *Pixels) {
  58. return (Pixels[2] * 14 + Pixels[1] * 76 + Pixels[0] * 38) >> 7;
  59. }
  60. static unsigned InHandler(JDEC *Decoder, uint8_t *Buf, unsigned Len) {
  61. JpegCtx *Context = (JpegCtx*) Decoder->device;
  62. if (Buf) memcpy(Buf, Context->InData + Context->InPos, Len);
  63. Context->InPos += Len;
  64. return Len;
  65. }
  66. #define OUTHANDLER(F) \
  67. for (int y = Frame->top; y <= Frame->bottom; y++) { \
  68. for (int x = Frame->left; x <= Frame->right; x++) { \
  69. OutData[Context->Width * y + x] = F(Pixels); \
  70. Pixels += 3; \
  71. } \
  72. }
  73. #define OUTHANDLER24(F) \
  74. for (int y = Frame->top; y <= Frame->bottom; y++) { \
  75. uint8_t *p = OutData + (Context->Width * y + Frame->left) * 3; \
  76. for (int c = Frame->right - Frame->left; c-- >= 0;) { \
  77. uint32_t v = F(Pixels); \
  78. *p++ = v; *p++ = v >> 8; *p++ = v >> 16; \
  79. Pixels += 3; \
  80. } \
  81. }
  82. static unsigned OutHandler(JDEC *Decoder, void *Bitmap, JRECT *Frame) {
  83. JpegCtx *Context = (JpegCtx*) Decoder->device;
  84. uint8_t *Pixels = (uint8_t*) Bitmap;
  85. // decoded image is RGB888
  86. if (Context->Mode == GDS_RGB888) {
  87. uint8_t *OutData = (uint8_t*) Context->OutData;
  88. OUTHANDLER24(Scaler888);
  89. } else if (Context->Mode == GDS_RGB666) {
  90. uint8_t *OutData = (uint8_t*) Context->OutData;
  91. OUTHANDLER24(Scaler666);
  92. } else if (Context->Mode == GDS_RGB565) {
  93. uint16_t *OutData = (uint16_t*) Context->OutData;
  94. OUTHANDLER(Scaler565);
  95. } else if (Context->Mode == GDS_RGB555) {
  96. uint16_t *OutData = (uint16_t*) Context->OutData;
  97. OUTHANDLER(Scaler555);
  98. } else if (Context->Mode == GDS_RGB444) {
  99. uint16_t *OutData = (uint16_t*) Context->OutData;
  100. OUTHANDLER(Scaler444);
  101. } else if (Context->Mode == GDS_RGB332) {
  102. uint8_t *OutData = (uint8_t*) Context->OutData;
  103. OUTHANDLER(Scaler332);
  104. } else if (Context->Mode <= GDS_GRAYSCALE) {
  105. uint8_t *OutData = (uint8_t*) Context->OutData;
  106. OUTHANDLER(ScalerGray);
  107. }
  108. return 1;
  109. }
  110. // Convert the RGB888 to destination color plane, use DrawPixel and not "fast"
  111. // version as X,Y may be beyond screen
  112. #define OUTHANDLERDIRECT(F,S) \
  113. for (int y = Frame->top; y <= Frame->bottom; y++) { \
  114. if (y < Context->YMin) continue; \
  115. for (int x = Frame->left; x <= Frame->right; x++) { \
  116. if (x < Context->XMin) continue; \
  117. DrawPixel( Context->Device, x + Context->XOfs, y + Context->YOfs, F(Pixels) >> S); \
  118. Pixels += 3; \
  119. } \
  120. }
  121. static unsigned OutHandlerDirect(JDEC *Decoder, void *Bitmap, JRECT *Frame) {
  122. JpegCtx *Context = (JpegCtx*) Decoder->device;
  123. uint8_t *Pixels = (uint8_t*) Bitmap;
  124. int Shift = 8 - Context->Depth;
  125. // decoded image is RGB888, shift only make sense for grayscale
  126. if (Context->Mode == GDS_RGB888) {
  127. OUTHANDLERDIRECT(Scaler888, 0);
  128. } else if (Context->Mode == GDS_RGB666) {
  129. OUTHANDLERDIRECT(Scaler666, 0);
  130. } else if (Context->Mode == GDS_RGB565) {
  131. OUTHANDLERDIRECT(Scaler565, 0);
  132. } else if (Context->Mode == GDS_RGB555) {
  133. OUTHANDLERDIRECT(Scaler555, 0);
  134. } else if (Context->Mode == GDS_RGB444) {
  135. OUTHANDLERDIRECT(Scaler444, 0);
  136. } else if (Context->Mode == GDS_RGB332) {
  137. OUTHANDLERDIRECT(Scaler332, 0);
  138. } else if (Context->Mode <= GDS_GRAYSCALE) {
  139. OUTHANDLERDIRECT(ScalerGray, Shift);
  140. }
  141. return 1;
  142. }
  143. //Decode the embedded image into pixel lines that can be used with the rest of the logic.
  144. static void* DecodeJPEG(uint8_t *Source, int *Width, int *Height, float Scale, bool SizeOnly, int RGB_Mode) {
  145. JDEC Decoder;
  146. JpegCtx Context;
  147. char *Scratch = calloc(SCRATCH_SIZE, 1);
  148. if (!Scratch) {
  149. ESP_LOGE(TAG, "Cannot allocate workspace");
  150. return NULL;
  151. }
  152. Context.OutData = NULL;
  153. Context.InData = Source;
  154. Context.InPos = 0;
  155. //Prepare and decode the jpeg.
  156. int Res = jd_prepare(&Decoder, InHandler, Scratch, SCRATCH_SIZE, (void*) &Context);
  157. if (Width) *Width = Decoder.width;
  158. if (Height) *Height = Decoder.height;
  159. Decoder.scale = Scale;
  160. if (Res == JDR_OK && !SizeOnly) {
  161. if (RGB_Mode <= GDS_RGB332) Context.OutData = malloc(Decoder.width * Decoder.height);
  162. else if (RGB_Mode < GDS_RGB666) Context.OutData = malloc(Decoder.width * Decoder.height * 2);
  163. else if (RGB_Mode <= GDS_RGB888) Context.OutData = malloc(Decoder.width * Decoder.height * 3);
  164. // find the scaling factor
  165. uint8_t N = 0, ScaleInt = ceil(1.0 / Scale);
  166. ScaleInt--; ScaleInt |= ScaleInt >> 1; ScaleInt |= ScaleInt >> 2; ScaleInt++;
  167. while (ScaleInt >>= 1) N++;
  168. if (N > 3) {
  169. ESP_LOGW(TAG, "Image will not fit %dx%d", Decoder.width, Decoder.height);
  170. N = 3;
  171. }
  172. // ready to decode
  173. if (Context.OutData) {
  174. Context.Width = Decoder.width / (1 << N);
  175. Context.Height = Decoder.height / (1 << N);
  176. Context.Mode = RGB_Mode;
  177. if (Width) *Width = Context.Width;
  178. if (Height) *Height = Context.Height;
  179. Res = jd_decomp(&Decoder, OutHandler, N);
  180. if (Res != JDR_OK) {
  181. ESP_LOGE(TAG, "Image decoder: jd_decode failed (%d)", Res);
  182. }
  183. } else {
  184. ESP_LOGE(TAG, "Can't allocate bitmap %dx%d or invalid mode %d", Decoder.width, Decoder.height, RGB_Mode);
  185. }
  186. } else if (!SizeOnly) {
  187. ESP_LOGE(TAG, "Image decoder: jd_prepare failed (%d)", Res);
  188. }
  189. // free scratch area
  190. if (Scratch) free(Scratch);
  191. return Context.OutData;
  192. }
  193. void* GDS_DecodeJPEG(uint8_t *Source, int *Width, int *Height, float Scale, int RGB_Mode) {
  194. return DecodeJPEG(Source, Width, Height, Scale, false, RGB_Mode);
  195. }
  196. void GDS_GetJPEGSize(uint8_t *Source, int *Width, int *Height) {
  197. DecodeJPEG(Source, Width, Height, 1, true, -1);
  198. }
  199. /****************************************************************************************
  200. * RGB conversion (24 bits: RRRRRRRRGGGGGGGGBBBBBBBB and 16 bits 565: RRRRRGGGGGGBBBBB = B31..B0)
  201. * so in other words for an array of 888 bytes: [0]=B, [1]=G, [2]=R, ...
  202. * monochrome (0.2125 * color.r) + (0.7154 * color.g) + (0.0721 * color.b)
  203. * grayscale (0.3 * R) + (0.59 * G) + (0.11 * B) )
  204. */
  205. static inline int ToGray888(uint8_t **Pixel) {
  206. uint32_t v = *(*Pixel)++; v |= *(*Pixel)++ << 8; v |= *(*Pixel)++ << 16;
  207. return (((v & 0xff) * 14) + ((v >> 8) & 0xff) * 76 + ((v >> 16) * 38) + 1) >> 7;
  208. }
  209. static inline int ToGray666(uint8_t **Pixel) {
  210. uint32_t v = *(*Pixel)++; v |= *(*Pixel)++ << 8; v |= *(*Pixel)++ << 16;
  211. return (((v & 0x3f) * 14) + ((v >> 6) & 0x3f) * 76 + ((v >> 12) * 38) + 1) >> 7;
  212. }
  213. static inline int ToGray565(uint16_t **Pixel) {
  214. uint16_t v = *(*Pixel)++;
  215. return ((((v & 0x1f) * 14) << 1) + ((v >> 5) & 0x3f) * 76 + (((v >> 11) * 38) << 1) + 1) >> 7;
  216. }
  217. static inline int ToGray555(uint16_t **Pixel) {
  218. uint16_t v = *(*Pixel)++;
  219. return ((v & 0x1f) * 14 + ((v >> 5) & 0x1f) * 76 + (v >> 10) * 38) >> 7;
  220. }
  221. static inline int ToGray444(uint16_t **Pixel) {
  222. uint16_t v = *(*Pixel)++;
  223. return ((v & 0x0f) * 14 + ((v >> 4) & 0x0f) * 76 + (v >> 8) * 38) >> 7;
  224. }
  225. static inline int ToGray332(uint8_t **Pixel) {
  226. uint8_t v = *(*Pixel)++;
  227. return ((((v & 0x3) * 14) << 1) + ((v >> 2) & 0x7) * 76 + (v >> 5) * 38 + 1) >> 7;
  228. }
  229. static inline int ToSelf(uint8_t **Pixel) {
  230. return *(*Pixel)++;
  231. }
  232. #define DRAW_GRAYRGB(S,F) \
  233. if (Scale > 0) { \
  234. for (int r = 0; r < Height; r++) { \
  235. for (int c = 0; c < Width; c++) { \
  236. DrawPixel( Device, c + x, r + y, F(S) >> Scale); \
  237. } \
  238. } \
  239. } else { \
  240. for (int r = 0; r < Height; r++) { \
  241. for (int c = 0; c < Width; c++) { \
  242. DrawPixel( Device, c + x, r + y, F(S) << -Scale); \
  243. } \
  244. } \
  245. }
  246. #define DRAW_RGB(T) \
  247. T *S = (T*) Image; \
  248. for (int r = 0; r < Height; r++) { \
  249. for (int c = 0; c < Width; c++) { \
  250. DrawPixel(Device, c + x, r + y, *S++); \
  251. } \
  252. }
  253. #define DRAW_RGB24 \
  254. uint8_t *S = (uint8_t*) Image; \
  255. for (int r = 0; r < Height; r++) { \
  256. for (int c = 0; c < Width; c++) { \
  257. uint32_t v = *S++; v |= *S++ << 8; v |= *S++ << 16; \
  258. DrawPixel(Device, c + x, r + y, v); \
  259. } \
  260. }
  261. /****************************************************************************************
  262. * Decode the embedded image into pixel lines that can be used with the rest of the logic.
  263. */
  264. void GDS_DrawRGB( struct GDS_Device* Device, uint8_t *Image, int x, int y, int Width, int Height, int RGB_Mode ) {
  265. // don't do anything if driver supplies a draw function
  266. if (Device->DrawRGB) {
  267. Device->DrawRGB( Device, Image, x, y, Width, Height, RGB_Mode );
  268. Device->Dirty = true;
  269. return;
  270. }
  271. // RGB type displays
  272. if (Device->Mode > GDS_GRAYSCALE) {
  273. // image must match the display mode!
  274. if (Device->Mode != RGB_Mode) {
  275. ESP_LOGE(TAG, "non-matching display & image mode %u %u", Device->Mode, RGB_Mode);
  276. return;
  277. }
  278. if (RGB_Mode == GDS_RGB332) {
  279. DRAW_RGB(uint8_t);
  280. } else if (RGB_Mode < GDS_RGB666) {
  281. DRAW_RGB(uint16_t);
  282. } else {
  283. DRAW_RGB24;
  284. }
  285. Device->Dirty = true;
  286. return;
  287. }
  288. // set the right scaler when displaying grayscale
  289. if (RGB_Mode <= GDS_GRAYSCALE) {
  290. int Scale = 8 - Device->Depth;
  291. DRAW_GRAYRGB(&Image,ToSelf);
  292. } else if (RGB_Mode == GDS_RGB332) {
  293. int Scale = 3 - Device->Depth;
  294. DRAW_GRAYRGB(&Image,ToGray332);
  295. } else if (RGB_Mode < GDS_RGB666) {
  296. if (RGB_Mode == GDS_RGB565) {
  297. int Scale = 6 - Device->Depth;
  298. DRAW_GRAYRGB((uint16_t**)&Image,ToGray565);
  299. } else if (RGB_Mode == GDS_RGB555) {
  300. int Scale = 5 - Device->Depth;
  301. DRAW_GRAYRGB((uint16_t**)&Image,ToGray555);
  302. } else if (RGB_Mode == GDS_RGB444) {
  303. int Scale = 4 - Device->Depth;
  304. DRAW_GRAYRGB((uint16_t**)&Image,ToGray444)
  305. }
  306. } else {
  307. if (RGB_Mode == GDS_RGB666) {
  308. int Scale = 6 - Device->Depth;
  309. DRAW_GRAYRGB(&Image,ToGray666);
  310. } else if (RGB_Mode == GDS_RGB888) {
  311. int Scale = 8 - Device->Depth;
  312. DRAW_GRAYRGB(&Image,ToGray888);
  313. }
  314. }
  315. Device->Dirty = true;
  316. }
  317. /****************************************************************************************
  318. * Decode the embedded image into pixel lines that can be used with the rest of the logic.
  319. */
  320. bool GDS_DrawJPEG(struct GDS_Device* Device, uint8_t *Source, int x, int y, int Fit) {
  321. JDEC Decoder;
  322. JpegCtx Context;
  323. bool Ret = false;
  324. char *Scratch = calloc(SCRATCH_SIZE, 1);
  325. if (!Scratch) {
  326. ESP_LOGE(TAG, "Cannot allocate workspace");
  327. return NULL;
  328. }
  329. // Populate fields of the JpegCtx struct.
  330. Context.InData = Source;
  331. Context.InPos = 0;
  332. Context.XOfs = x;
  333. Context.YOfs = y;
  334. Context.Device = Device;
  335. Context.Depth = Device->Depth;
  336. //Prepare and decode the jpeg.
  337. int Res = jd_prepare(&Decoder, InHandler, Scratch, SCRATCH_SIZE, (void*) &Context);
  338. Context.Width = Decoder.width;
  339. Context.Height = Decoder.height;
  340. if (Res == JDR_OK) {
  341. uint8_t N = 0;
  342. // do we need to fit the image
  343. if (Fit & GDS_IMAGE_FIT) {
  344. float XRatio = (Device->Width - x) / (float) Decoder.width, YRatio = (Device->Height - y) / (float) Decoder.height;
  345. uint8_t Ratio = XRatio < YRatio ? ceil(1/XRatio) : ceil(1/YRatio);
  346. Ratio--; Ratio |= Ratio >> 1; Ratio |= Ratio >> 2; Ratio++;
  347. while (Ratio >>= 1) N++;
  348. if (N > 3) {
  349. ESP_LOGW(TAG, "Image will not fit %dx%d", Decoder.width, Decoder.height);
  350. N = 3;
  351. }
  352. Context.Width /= 1 << N;
  353. Context.Height /= 1 << N;
  354. }
  355. // then place it
  356. if (Fit & GDS_IMAGE_CENTER_X) Context.XOfs = (Device->Width + x - Context.Width) / 2;
  357. else if (Fit & GDS_IMAGE_RIGHT) Context.XOfs = Device->Width - Context.Width;
  358. if (Fit & GDS_IMAGE_CENTER_Y) Context.YOfs = (Device->Height + y - Context.Height) / 2;
  359. else if (Fit & GDS_IMAGE_BOTTOM) Context.YOfs = Device->Height - Context.Height;
  360. Context.XMin = x - Context.XOfs;
  361. Context.YMin = y - Context.YOfs;
  362. Context.Mode = Device->Mode;
  363. // do decompress & draw
  364. Res = jd_decomp(&Decoder, OutHandlerDirect, N);
  365. if (Res == JDR_OK) {
  366. Device->Dirty = true;
  367. Ret = true;
  368. } else {
  369. ESP_LOGE(TAG, "Image decoder: jd_decode failed (%d)", Res);
  370. }
  371. } else {
  372. ESP_LOGE(TAG, "Image decoder: jd_prepare failed (%d)", Res);
  373. }
  374. // free scratch area
  375. if (Scratch) free(Scratch);
  376. return Ret;
  377. }