gds_image.c 14 KB

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