Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void bmpDraw(char *filename, uint8_t x, uint8_t y) { // This is slightly complex
- int bmpWidth, bmpHeight; // W+H in pixels
- uint8_t bmpDepth; // Bit depth (currently must be 24)
- uint32_t bmpImageoffset; // Start of image data in file
- uint32_t rowSize; // Not always = bmpWidth; may have padding
- uint8_t sdbuffer[3*20]; // pixel buffer (R+G+B per pixel)
- uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
- boolean goodBmp = false; // Set to true on valid header parse
- boolean flip = true; // BMP is stored bottom-to-top
- int w, h, row, col; // setting up integers for width, height, row and column.
- uint8_t r, g, b,lc; // unsigned 8 bit integers....
- bmpFile2 = SD.open(filename); // open the folder
- bmpFile2.rewindDirectory(); // go to the start of the directory
- while(true) {
- delay(1);
- lc++; //?
- File bmpFile = bmpFile2.openNextFile(); // read in the next file
- if (! bmpFile) { //If no file
- break; // stop
- }
- uint32_t pos = 0;
- if(read16(bmpFile) == 0x4D42) { // If this has a BMP signature
- //Going through the header data, there are things we can read and ignore....
- (void)read32(bmpFile); //ignore this
- (void)read32(bmpFile); // Read & ignore creator bytes
- bmpImageoffset = read32(bmpFile); // Start of image data
- (void)read32(bmpFile); // Ignore
- bmpWidth = read32(bmpFile); //This retrieves the width of the bitmap in pixels
- bmpHeight = read32(bmpFile); //This retrieves the height of the bitmap in pixels
- if(read16(bmpFile) == 1) { // # planes -- must be '1'
- bmpDepth = read16(bmpFile); // bits per pixel
- if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
- goodBmp = true; // Supported BMP format -- proceed!
- rowSize = (bmpWidth * 3 + 3) & ~3;
- if(bmpHeight < 0) {
- bmpHeight = -bmpHeight;
- flip = false;
- }
- w = bmpWidth;
- h = bmpHeight;
- for (row=0; row<h; row++) { // For each scanline...
- if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
- pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
- else // Bitmap is stored top-to-bottom
- pos = bmpImageoffset + row * rowSize;
- if(bmpFile.position() != pos) { // Need seek?
- bmpFile.seek(pos);
- buffidx = sizeof(sdbuffer); // Force buffer reload
- } // not sure about these bits....
- for (col=0; col<w; col++) { // For each pixel...
- // Time to read more pixel data?
- if (buffidx >= sizeof(sdbuffer)) { // Indeed
- bmpFile.read(sdbuffer, sizeof(sdbuffer));
- buffidx = 0; // Set index to beginning
- }
- b = sdbuffer[buffidx++]; //blue first
- g = sdbuffer[buffidx++]; // then green
- r = sdbuffer[buffidx++]; // then red
- leds[(row*30)+col] = CRGB(g,r,b); // set led
- } // end pixel
- } // end scanline
- } // end goodBmp
- }
- }
- bmpFile.close(); // close file
- if(!goodBmp) {
- } else {
- LEDS.show();
- if(lc==10){
- lc = 0;
- checkSecond();
- }
- }
- }
- bmpFile2.close();
- }
- /// These bits are not mine, from somewhere....
- uint16_t read16(File f) {
- uint16_t result;
- ((uint8_t *)&result)[0] = f.read(); // LSB
- ((uint8_t *)&result)[1] = f.read(); // MSB
- return result;
- }
- uint32_t read32(File f) {
- uint32_t result;
- ((uint8_t *)&result)[0] = f.read(); // LSB
- ((uint8_t *)&result)[1] = f.read();
- ((uint8_t *)&result)[2] = f.read();
- ((uint8_t *)&result)[3] = f.read(); // MSB
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment