_convert_images.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <gui/gui.h> // GUI (screen/keyboard) API
  2. #include "images.h"
  3. //----------------------------------------------------------------------------- ----------------------------------------
  4. static Canvas* _canvas;
  5. static uint8_t _tlx;
  6. static uint8_t _tly;
  7. static uint8_t _x;
  8. static uint8_t _y;
  9. static const image_t* _img;
  10. static bool _blk;
  11. static Color _set;
  12. static Color _clr;
  13. //+============================================================================
  14. static
  15. void _showByteSet (const uint8_t b)
  16. {
  17. for (uint8_t m = 0x80; m; m >>= 1) {
  18. if (b & m) // plot only SET bits
  19. canvas_draw_dot(_canvas, (_tlx +_x), (_tly +_y)) ;
  20. if ( ((++_x) == _img->w) && !(_x = 0) && ((++_y) == _img->h) ) break ;
  21. }
  22. }
  23. //+============================================================================
  24. static
  25. void _showByteClr (const uint8_t b)
  26. {
  27. for (uint8_t m = 0x80; m; m >>= 1) {
  28. if (!(b & m)) // plot only CLR bits
  29. canvas_draw_dot(_canvas, (_tlx +_x), (_tly +_y)) ;
  30. if ( ((++_x) == _img->w) && !(_x = 0) && ((++_y) == _img->h) ) break ;
  31. }
  32. }
  33. //+============================================================================
  34. static
  35. void _showByteAll (const uint8_t b)
  36. {
  37. for (uint8_t m = 0x80; m; m >>= 1) {
  38. if ((!!(b & m)) ^ _blk) { // Change colour only when required
  39. canvas_set_color(_canvas, ((b & m) ? _set : _clr));
  40. _blk = !_blk;
  41. }
  42. canvas_draw_dot(_canvas, (_tlx +_x), (_tly +_y)) ;
  43. if ( ((++_x) == _img->w) && !(_x = 0) && ((++_y) == _img->h) ) break ;
  44. }
  45. }
  46. //+============================================================================
  47. // available modes are SHOW_SET_BLK - plot image pixels that are SET in BLACK
  48. // SHOW_XOR - same as SET_BLACK
  49. // SHOW_SET_WHT - plot image pixels that are SET in WHITE
  50. // SHOW_CLR_BLK - plot image pixels that are CLEAR in BLACK
  51. // SHOW_CLR_WHT - plot image pixels that are CLEAR in WHITE
  52. // SHOW_ALL - plot all images pixels as they are
  53. // SHOW_ALL_INV - plot all images pixels inverted
  54. //
  55. void show (Canvas* const canvas, const uint8_t tlx, const uint8_t tly,
  56. const image_t* img, const showMode_t mode)
  57. {
  58. void(*fnShow)(const uint8_t) = NULL;
  59. const uint8_t* bp = img->data;
  60. // code size optimisation
  61. switch (mode & SHOW_INV_) {
  62. case SHOW_NRM_:
  63. _set = ColorBlack;
  64. _clr = ColorWhite;
  65. break;
  66. case SHOW_INV_:
  67. _set = ColorWhite;
  68. _clr = ColorBlack;
  69. break;
  70. case SHOW_BLK_:
  71. canvas_set_color(canvas, ColorBlack);
  72. break;
  73. case SHOW_WHT_:
  74. canvas_set_color(canvas, ColorWhite);
  75. break;
  76. }
  77. switch (mode & SHOW_INV_) {
  78. case SHOW_NRM_:
  79. case SHOW_INV_:
  80. fnShow = _showByteAll;
  81. canvas_set_color(canvas, ColorWhite);
  82. _blk = 0;
  83. break;
  84. case SHOW_BLK_:
  85. case SHOW_WHT_:
  86. switch (mode & SHOW_ALL_) {
  87. case SHOW_SET_:
  88. fnShow = _showByteSet;
  89. break;
  90. case SHOW_CLR_:
  91. fnShow = _showByteClr;
  92. break;
  93. }
  94. break;
  95. }
  96. furi_check(fnShow);
  97. // I want nested functions!
  98. _canvas = canvas;
  99. _img = img;
  100. _tlx = tlx;
  101. _tly = tly;
  102. _x = 0;
  103. _y = 0;
  104. // Compressed
  105. if (img->c) {
  106. for (unsigned int i = 0; i < img->len; i++, bp++) {
  107. // Compressed data? {tag, length, value}
  108. if (*bp == img->tag) {
  109. for (uint16_t c = 0; c < bp[1]; c++) fnShow(bp[2]) ;
  110. bp += 3 -1;
  111. i += 3 -1;
  112. // Uncompressed byte
  113. } else {
  114. fnShow(*bp);
  115. }
  116. }
  117. // Not compressed
  118. } else {
  119. for (unsigned int i = 0; i < img->len; i++, bp++) fnShow(*bp) ;
  120. }
  121. }