_convert_test.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include "images.h"
  4. //-----------------------------------------------------------------------------
  5. // This will be the plot function out of your graphics library
  6. //
  7. #define PLOT(x, y, c) \
  8. do { \
  9. printf("%s", (c ? "#" : ".")); \
  10. if(x == img->w - 1) printf("\n"); \
  11. } while(0)
  12. //+============================================================================
  13. // The pain we endure to avoid code duplication cleanly
  14. //
  15. #define PLOTBYTE(b) \
  16. do { \
  17. for(uint8_t m = 0x80; m; m >>= 1) { \
  18. PLOT(x, y, (b & m)); \
  19. if(((++x) == img->w) && !(x = 0) && ((++y) == img->h)) break; \
  20. } \
  21. } while(0)
  22. void show(const image_t* img) {
  23. // Some variables
  24. const uint8_t* bp = img->data;
  25. unsigned int x = 0;
  26. unsigned int y = 0;
  27. // Compressed
  28. if(img->c) {
  29. for(unsigned int i = 0; i < img->len; i++, bp++) {
  30. // Compressed data? {tag, length, value}
  31. if(*bp == img->tag) {
  32. for(uint16_t c = 0; c < bp[1]; c++)
  33. PLOTBYTE(bp[2]);
  34. bp += 3 - 1;
  35. i += 3 - 1;
  36. // Uncompressed byte
  37. } else {
  38. PLOTBYTE(*bp);
  39. }
  40. }
  41. // Not compressed
  42. } else {
  43. for(unsigned int i = 0; i < img->len; i++, bp++)
  44. PLOTBYTE(*bp);
  45. }
  46. }
  47. #undef PLOTBYTE
  48. //+============================================================================
  49. int main(void) {
  50. show(&img_zzz);
  51. return 0;
  52. }