I have this object that grabs rectangle from a bitmap and blits it to the screen. It works fine, never gives any issues but its bloody slow. So, now that I'm on a machine with some RAM, how about allocating a line buffer and letting it do things line by line? Instead of pixel by pixel. The logic is already setup for this kind of thing, I just need to pass in a correctly sized buffer.
And it just blows up!
I think I'm not allocating the buffer correctly. I want a buffer that is an array of colorObj(s). Then I can poke colors into them one at a time. Once I set them all, I can switch over and blit them to the screen one at time.
I get the buffer, I see things run 'till I want to poke in some data and.. It locks up.
drawBitmap() is what is called originally. It sets the stage and gets things ready for blitting line by line.
drawLine() is called for each line. The original grabs one pixel at time, reads it then writes it to the screen.
The new drawLine() is passed in an array of colorObj(s) this should allow it to grab the entire line before switching hardware for drawing. But it just blows up.
Thanks millions.
-jim lee
// Your standard do it one at a time.
void bmpPipe::drawLine(File bmpFile,int x,int y) {
colorObj thePixal;
uint8_t buf[COLOR_BUF_SIZE];
int trace;
int endTrace;
endTrace = x+sourceRect.width;
for (trace=x;trace<endTrace; trace++) { // Ok, trace does x..x+width.
bmpFile.read(buf,pixBytes); // Grab a pixel.
thePixal.setColor(buf[2],buf[1],buf[0]); // Load colorObj.
screen->drawPixel(trace,y,&thePixal); // Spat it out to the screen.
}
}
// If we were able to allocate a line buffer for the colors, try it line at a time.
void bmpPipe::drawLine(File bmpFile,int x,int y,colorObj* colorBuff) {
uint8_t buf[COLOR_BUF_SIZE];
int trace;
int endTrace;
int i;
i = 0;
endTrace = x+sourceRect.width;
Serial.println("Filling buffer");Serial.flush();
for (trace=x;trace<endTrace; trace++) { // Ok, trace does x..x+width.
bmpFile.read(buf,pixBytes); // Grab a pixel.
colorBuff[i].setColor(buf[2],buf[1],buf[0]); // Load colorObj.
i++;
}
Serial.println("Drawing buffer to screen");Serial.flush();
i = 0;
for (trace=x;trace<endTrace; trace++) { // Ok, trace does x..x+width.
screen->drawPixel(trace,y,&(colorBuff[i])); // Spat it out to the screen.
i++;
}
}
void bmpPipe::drawBitmap(int x,int y) {
File bmpFile;
int trace;
int endY;
int srcY;
colorObj* colorBuff;
colorObj aColor;
colorBuff = (colorObj*)malloc(sizeof(colorObj)*sourceRect.width); // Have a shot at grabbing aline buffer.
if (colorBuff) { // We get one?
for (int i=0;i<sourceRect.width;i++) { // Initialize it with default color objects.
colorBuff[i] = aColor; // Stuffing in the i-th object.
}
}
Serial.println("buffer allocated");Serial.flush();
if (haveInfo) { // We have valid bmp info.
bmpFile = SD.open(filePath); // Open up the file.
if (bmpFile) { // If we opened it.
endY = y+sourceRect.height; // Start calculating endpoints and things.
srcY = sourceRect.y;
for (trace=y; trace<endY;trace++) { // Ready to pull data through to the screen.
bmpFile.seek(filePtr(x,srcY++)); // Position the file pointer to the line of pixels we want.
if (colorBuff) { // Now, if we were able to allocate a buffer, we'll use it.
drawLine(bmpFile,x,trace,colorBuff); // Fancy buffered line draw.
} else { // Ir if not..
drawLine(bmpFile,x,trace); // Standard old pixel by pixel draw. (Least it works.)
}
}
bmpFile.close(); // Drawing is done for now. Close the file.
}
}
if (colorBuff) { free(colorBuff); } // So if we did get that buffer free it.
}