fixed bitmap n tested
This commit is contained in:
parent
cd4ff4fd25
commit
47c83e929a
1 changed files with 73 additions and 54 deletions
|
|
@ -77,7 +77,7 @@ void setup(void) {
|
||||||
|
|
||||||
if (!SD.begin(SD_CS)) {
|
if (!SD.begin(SD_CS)) {
|
||||||
Serial.println("failed!");
|
Serial.println("failed!");
|
||||||
return;
|
while(1);
|
||||||
}
|
}
|
||||||
Serial.println("SD OK!");
|
Serial.println("SD OK!");
|
||||||
|
|
||||||
|
|
@ -87,6 +87,7 @@ void setup(void) {
|
||||||
void loop() {
|
void loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// This function opens a Windows Bitmap (BMP) file and
|
// This function opens a Windows Bitmap (BMP) file and
|
||||||
// displays it at the given coordinates. It's sped up
|
// displays it at the given coordinates. It's sped up
|
||||||
// by reading many pixels worth of data at a time
|
// by reading many pixels worth of data at a time
|
||||||
|
|
@ -97,7 +98,7 @@ void loop() {
|
||||||
|
|
||||||
#define BUFFPIXEL 20
|
#define BUFFPIXEL 20
|
||||||
|
|
||||||
void bmpDraw(char *filename, uint8_t x, uint8_t y) {
|
void bmpDraw(char *filename, int16_t x, int16_t y) {
|
||||||
|
|
||||||
File bmpFile;
|
File bmpFile;
|
||||||
int bmpWidth, bmpHeight; // W+H in pixels
|
int bmpWidth, bmpHeight; // W+H in pixels
|
||||||
|
|
@ -108,40 +109,40 @@ void bmpDraw(char *filename, uint8_t x, uint8_t y) {
|
||||||
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
|
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
|
||||||
boolean goodBmp = false; // Set to true on valid header parse
|
boolean goodBmp = false; // Set to true on valid header parse
|
||||||
boolean flip = true; // BMP is stored bottom-to-top
|
boolean flip = true; // BMP is stored bottom-to-top
|
||||||
int w, h, row, col;
|
int w, h, row, col, x2, y2, bx1, by1;
|
||||||
uint8_t r, g, b;
|
uint8_t r, g, b;
|
||||||
uint32_t pos = 0, startTime = millis();
|
uint32_t pos = 0, startTime = millis();
|
||||||
|
|
||||||
if((x >= tft.width()) || (y >= tft.height())) return;
|
if((x >= tft.width()) || (y >= tft.height())) return;
|
||||||
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.print("Loading image '");
|
Serial.print(F("Loading image '"));
|
||||||
Serial.print(filename);
|
Serial.print(filename);
|
||||||
Serial.println('\'');
|
Serial.println('\'');
|
||||||
|
|
||||||
// Open requested file on SD card
|
// Open requested file on SD card
|
||||||
if ((bmpFile = SD.open(filename)) == NULL) {
|
if ((bmpFile = SD.open(filename)) == NULL) {
|
||||||
Serial.print("File not found");
|
Serial.print(F("File not found"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse BMP header
|
// Parse BMP header
|
||||||
if(read16(bmpFile) == 0x4D42) { // BMP signature
|
if(read16(bmpFile) == 0x4D42) { // BMP signature
|
||||||
Serial.print("File size: "); Serial.println(read32(bmpFile));
|
Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
|
||||||
(void)read32(bmpFile); // Read & ignore creator bytes
|
(void)read32(bmpFile); // Read & ignore creator bytes
|
||||||
bmpImageoffset = read32(bmpFile); // Start of image data
|
bmpImageoffset = read32(bmpFile); // Start of image data
|
||||||
Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
|
Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
|
||||||
// Read DIB header
|
// Read DIB header
|
||||||
Serial.print("Header size: "); Serial.println(read32(bmpFile));
|
Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
|
||||||
bmpWidth = read32(bmpFile);
|
bmpWidth = read32(bmpFile);
|
||||||
bmpHeight = read32(bmpFile);
|
bmpHeight = read32(bmpFile);
|
||||||
if(read16(bmpFile) == 1) { // # planes -- must be '1'
|
if(read16(bmpFile) == 1) { // # planes -- must be '1'
|
||||||
bmpDepth = read16(bmpFile); // bits per pixel
|
bmpDepth = read16(bmpFile); // bits per pixel
|
||||||
Serial.print("Bit Depth: "); Serial.println(bmpDepth);
|
Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
|
||||||
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
|
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
|
||||||
|
|
||||||
goodBmp = true; // Supported BMP format -- proceed!
|
goodBmp = true; // Supported BMP format -- proceed!
|
||||||
Serial.print("Image size: ");
|
Serial.print(F("Image size: "));
|
||||||
Serial.print(bmpWidth);
|
Serial.print(bmpWidth);
|
||||||
Serial.print('x');
|
Serial.print('x');
|
||||||
Serial.println(bmpHeight);
|
Serial.println(bmpHeight);
|
||||||
|
|
@ -157,13 +158,30 @@ void bmpDraw(char *filename, uint8_t x, uint8_t y) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Crop area to be loaded
|
// Crop area to be loaded
|
||||||
w = bmpWidth;
|
x2 = x + bmpWidth - 1; // Lower-right corner
|
||||||
|
y2 = y + bmpHeight - 1;
|
||||||
|
if((x2 >= 0) && (y2 >= 0)) { // On screen?
|
||||||
|
w = bmpWidth; // Width/height of section to load/display
|
||||||
h = bmpHeight;
|
h = bmpHeight;
|
||||||
if((x+w-1) >= tft.width()) w = tft.width() - x;
|
bx1 = by1 = 0; // UL coordinate in BMP file
|
||||||
if((y+h-1) >= tft.height()) h = tft.height() - y;
|
if(x < 0) { // Clip left
|
||||||
|
bx1 = -x;
|
||||||
|
x = 0;
|
||||||
|
w = x2 + 1;
|
||||||
|
}
|
||||||
|
if(y < 0) { // Clip top
|
||||||
|
by1 = -y;
|
||||||
|
y = 0;
|
||||||
|
h = y2 + 1;
|
||||||
|
}
|
||||||
|
if(x2 >= tft.width()) w = tft.width() - x; // Clip right
|
||||||
|
if(y2 >= tft.height()) h = tft.height() - y; // Clip bottom
|
||||||
|
|
||||||
|
// Set TFT address window to clipped image bounds
|
||||||
|
tft.startWrite(); // Requires start/end transaction now
|
||||||
|
tft.setAddrWindow(x, y, w, h);
|
||||||
|
|
||||||
for (row=0; row<h; row++) { // For each scanline...
|
for (row=0; row<h; row++) { // For each scanline...
|
||||||
tft.goTo(x, y+row);
|
|
||||||
|
|
||||||
// Seek to start of scan line. It might seem labor-
|
// Seek to start of scan line. It might seem labor-
|
||||||
// intensive to be doing this on every line, but this
|
// intensive to be doing this on every line, but this
|
||||||
|
|
@ -172,33 +190,34 @@ void bmpDraw(char *filename, uint8_t x, uint8_t y) {
|
||||||
// place if the file position actually needs to change
|
// place if the file position actually needs to change
|
||||||
// (avoids a lot of cluster math in SD library).
|
// (avoids a lot of cluster math in SD library).
|
||||||
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
|
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
|
||||||
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
|
pos = bmpImageoffset + (bmpHeight - 1 - (row + by1)) * rowSize;
|
||||||
else // Bitmap is stored top-to-bottom
|
else // Bitmap is stored top-to-bottom
|
||||||
pos = bmpImageoffset + row * rowSize;
|
pos = bmpImageoffset + (row + by1) * rowSize;
|
||||||
|
pos += bx1 * 3; // Factor in starting column (bx1)
|
||||||
if(bmpFile.position() != pos) { // Need seek?
|
if(bmpFile.position() != pos) { // Need seek?
|
||||||
|
tft.endWrite(); // End TFT transaction
|
||||||
bmpFile.seek(pos);
|
bmpFile.seek(pos);
|
||||||
buffidx = sizeof(sdbuffer); // Force buffer reload
|
buffidx = sizeof(sdbuffer); // Force buffer reload
|
||||||
|
tft.startWrite(); // Start new TFT transaction
|
||||||
}
|
}
|
||||||
|
|
||||||
// optimize by setting pins now
|
|
||||||
for (col=0; col<w; col++) { // For each pixel...
|
for (col=0; col<w; col++) { // For each pixel...
|
||||||
// Time to read more pixel data?
|
// Time to read more pixel data?
|
||||||
if (buffidx >= sizeof(sdbuffer)) { // Indeed
|
if (buffidx >= sizeof(sdbuffer)) { // Indeed
|
||||||
|
tft.endWrite(); // End TFT transaction
|
||||||
bmpFile.read(sdbuffer, sizeof(sdbuffer));
|
bmpFile.read(sdbuffer, sizeof(sdbuffer));
|
||||||
buffidx = 0; // Set index to beginning
|
buffidx = 0; // Set index to beginning
|
||||||
|
tft.startWrite(); // Start new TFT transaction
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert pixel from BMP to TFT format, push to display
|
// Convert pixel from BMP to TFT format, push to display
|
||||||
b = sdbuffer[buffidx++];
|
b = sdbuffer[buffidx++];
|
||||||
g = sdbuffer[buffidx++];
|
g = sdbuffer[buffidx++];
|
||||||
r = sdbuffer[buffidx++];
|
r = sdbuffer[buffidx++];
|
||||||
|
tft.writePixel(tft.color565(r,g,b));
|
||||||
//tft.drawPixel(x+col, y+row, tft.Color565(r,g,b));
|
|
||||||
// optimized!
|
|
||||||
tft.pushColor(tft.Color565(r,g,b));
|
|
||||||
} // end pixel
|
} // end pixel
|
||||||
} // end scanline
|
} // end scanline
|
||||||
Serial.print("Loaded in ");
|
tft.endWrite(); // End last TFT transaction
|
||||||
|
} // end onscreen
|
||||||
|
Serial.print(F("Loaded in "));
|
||||||
Serial.print(millis() - startTime);
|
Serial.print(millis() - startTime);
|
||||||
Serial.println(" ms");
|
Serial.println(" ms");
|
||||||
} // end goodBmp
|
} // end goodBmp
|
||||||
|
|
@ -206,21 +225,21 @@ void bmpDraw(char *filename, uint8_t x, uint8_t y) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bmpFile.close();
|
bmpFile.close();
|
||||||
if(!goodBmp) Serial.println("BMP format not recognized.");
|
if(!goodBmp) Serial.println(F("BMP format not recognized."));
|
||||||
}
|
}
|
||||||
|
|
||||||
// These read 16- and 32-bit types from the SD card file.
|
// These read 16- and 32-bit types from the SD card file.
|
||||||
// BMP data is stored little-endian, Arduino is little-endian too.
|
// BMP data is stored little-endian, Arduino is little-endian too.
|
||||||
// May need to reverse subscript order if porting elsewhere.
|
// May need to reverse subscript order if porting elsewhere.
|
||||||
|
|
||||||
uint16_t read16(File f) {
|
uint16_t read16(File &f) {
|
||||||
uint16_t result;
|
uint16_t result;
|
||||||
((uint8_t *)&result)[0] = f.read(); // LSB
|
((uint8_t *)&result)[0] = f.read(); // LSB
|
||||||
((uint8_t *)&result)[1] = f.read(); // MSB
|
((uint8_t *)&result)[1] = f.read(); // MSB
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t read32(File f) {
|
uint32_t read32(File &f) {
|
||||||
uint32_t result;
|
uint32_t result;
|
||||||
((uint8_t *)&result)[0] = f.read(); // LSB
|
((uint8_t *)&result)[0] = f.read(); // LSB
|
||||||
((uint8_t *)&result)[1] = f.read();
|
((uint8_t *)&result)[1] = f.read();
|
||||||
Loading…
Reference in a new issue