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