_convert_images.c 3.8 KB

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