Guest User

code

a guest
Jan 31st, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. void bmpDraw(char *filename, uint8_t x, uint8_t y) { // This is slightly complex
  2. int bmpWidth, bmpHeight; // W+H in pixels
  3. uint8_t bmpDepth; // Bit depth (currently must be 24)
  4. uint32_t bmpImageoffset; // Start of image data in file
  5. uint32_t rowSize; // Not always = bmpWidth; may have padding
  6. uint8_t sdbuffer[3*20]; // pixel buffer (R+G+B per pixel)
  7. uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  8. boolean goodBmp = false; // Set to true on valid header parse
  9. boolean flip = true; // BMP is stored bottom-to-top
  10. int w, h, row, col; // setting up integers for width, height, row and column.
  11. uint8_t r, g, b,lc; // unsigned 8 bit integers....
  12. bmpFile2 = SD.open(filename); // open the folder
  13. bmpFile2.rewindDirectory(); // go to the start of the directory
  14. while(true) {
  15. delay(1);
  16. lc++; //?
  17. File bmpFile = bmpFile2.openNextFile(); // read in the next file
  18. if (! bmpFile) { //If no file
  19. break; // stop
  20. }
  21.  
  22. uint32_t pos = 0;
  23. if(read16(bmpFile) == 0x4D42) { // If this has a BMP signature
  24. //Going through the header data, there are things we can read and ignore....
  25. (void)read32(bmpFile); //ignore this
  26. (void)read32(bmpFile); // Read & ignore creator bytes
  27. bmpImageoffset = read32(bmpFile); // Start of image data
  28. (void)read32(bmpFile); // Ignore
  29. bmpWidth = read32(bmpFile); //This retrieves the width of the bitmap in pixels
  30. bmpHeight = read32(bmpFile); //This retrieves the height of the bitmap in pixels
  31. if(read16(bmpFile) == 1) { // # planes -- must be '1'
  32. bmpDepth = read16(bmpFile); // bits per pixel
  33. if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  34. goodBmp = true; // Supported BMP format -- proceed!
  35. rowSize = (bmpWidth * 3 + 3) & ~3;
  36. if(bmpHeight < 0) {
  37. bmpHeight = -bmpHeight;
  38. flip = false;
  39. }
  40. w = bmpWidth;
  41. h = bmpHeight;
  42. for (row=0; row<h; row++) { // For each scanline...
  43. if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
  44. pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
  45. else // Bitmap is stored top-to-bottom
  46. pos = bmpImageoffset + row * rowSize;
  47. if(bmpFile.position() != pos) { // Need seek?
  48. bmpFile.seek(pos);
  49. buffidx = sizeof(sdbuffer); // Force buffer reload
  50. } // not sure about these bits....
  51.  
  52. for (col=0; col<w; col++) { // For each pixel...
  53. // Time to read more pixel data?
  54. if (buffidx >= sizeof(sdbuffer)) { // Indeed
  55. bmpFile.read(sdbuffer, sizeof(sdbuffer));
  56. buffidx = 0; // Set index to beginning
  57. }
  58. b = sdbuffer[buffidx++]; //blue first
  59. g = sdbuffer[buffidx++]; // then green
  60. r = sdbuffer[buffidx++]; // then red
  61. leds[(row*30)+col] = CRGB(g,r,b); // set led
  62. } // end pixel
  63. } // end scanline
  64. } // end goodBmp
  65. }
  66. }
  67. bmpFile.close(); // close file
  68. if(!goodBmp) {
  69. } else {
  70. LEDS.show();
  71. if(lc==10){
  72. lc = 0;
  73. checkSecond();
  74. }
  75. }
  76. }
  77. bmpFile2.close();
  78.  
  79. }
  80.  
  81. /// These bits are not mine, from somewhere....
  82.  
  83. uint16_t read16(File f) {
  84. uint16_t result;
  85. ((uint8_t *)&result)[0] = f.read(); // LSB
  86. ((uint8_t *)&result)[1] = f.read(); // MSB
  87. return result;
  88. }
  89.  
  90. uint32_t read32(File f) {
  91. uint32_t result;
  92. ((uint8_t *)&result)[0] = f.read(); // LSB
  93. ((uint8_t *)&result)[1] = f.read();
  94. ((uint8_t *)&result)[2] = f.read();
  95. ((uint8_t *)&result)[3] = f.read(); // MSB
  96. return result;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment