_convert_test.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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++) PLOTBYTE(bp[2]);
  33. bp += 3 - 1;
  34. i += 3 - 1;
  35. // Uncompressed byte
  36. } else {
  37. PLOTBYTE(*bp);
  38. }
  39. }
  40. // Not compressed
  41. } else {
  42. for(unsigned int i = 0; i < img->len; i++, bp++) PLOTBYTE(*bp);
  43. }
  44. }
  45. #undef PLOTBYTE
  46. //+============================================================================
  47. int main(void) {
  48. show(&img_zzz);
  49. return 0;
  50. }