Size functions returning 0 sometimes?
Sarah-C opened this issue · 3 comments
Sarah-C commented
Hi bitbank2!
I've been adding some resizing code to my picture viewer, and one picture's suspiciously 0 in width and height.
M5EPD initializing...OK
Reached end of directory, restart!
Filename /2021-08-17_15-14-05.jpg
Image size:
960
540
Next image.
Filename /2021-08-19_15-26-28.jpg
Image size:
400
267
Next image.
Filename /solar-flare.en.jpg
Image size:
0 <<<< should be 1200
0 <<<<< should be 720
I think my code is correct, and it's something about the picture itself?
void drawImage(char *fileName) {
jpeg.open(fileName, myOpen, myClose, myRead, mySeek, JPEGDraw);
int width = jpeg.getWidth();
int height = jpeg.getHeight();
Serial.println("Image size:");
Serial.println(width);
Serial.println(height);
// Too big? Then scale.
uint8_t scaling = 0; // Get's or'd with options for the following:
uint8_t options = 0;
/*Decoder options
JPEG_AUTO_ROTATE 1
JPEG_SCALE_HALF 2
JPEG_SCALE_QUARTER 4
JPEG_SCALE_EIGHTH 8
JPEG_LE_PIXELS 16
JPEG_EXIF_THUMBNAIL 32
JPEG_LUMA_ONLY 64
*/
if (width > 960 || height > 540) {
// Try half size.
width >>= 1;
height >>= 1;
Serial.println("Trying half size:");
Serial.println(width);
Serial.println(height);
scaling = JPEG_SCALE_HALF;
if (width > 960 || height > 540) {
// Try quarter size.
width >>= 1;
height >>= 1;
scaling = JPEG_SCALE_QUARTER;
Serial.println("Trying quarter size:");
Serial.println(width);
Serial.println(height);
if (width > 960 || height > 540) {
// Try eigth size.
width >>= 1;
height >>= 1;
scaling = JPEG_SCALE_EIGHTH;
Serial.println("Trying eighth size:");
Serial.println(width);
Serial.println(height);
if (width > 960 || height > 540) {
Serial.println("Still too big after scaling attempt, skipping.");
// Still too big, cancel operation, and don't pause for any length of time for next image.
waitingTime = 1;
return;
} // Too big.
} // Eighth size.
} // Quarter size.
} // Half size.
// For when this image is smaller than the last - make sure no old image can be seen on the boundaries.
canvas.fillCanvas(15);
offsetX = (960 - width) >> 1;
offsetY = (540 - height) >> 1;
jpeg.setPixelType(FOUR_BIT_DITHERED);
jpeg.decodeDither(ditherSpace, options || scaling);
jpeg.close();
canvas.drawRightString(fileName, 960, 540, 1);
drawTempHumidityBattery();
canvas.pushCanvas(0, 0, UPDATE_MODE_GC16);
waitingTime = WaitingTimeStart;
}
Exhibit A:
Sarah-C commented
bitbank2 commented
It doesn't return a size because it fails the open function. Please add code to check all return codes. It's compressed with the PROGRESSIVE option and that's not supported by JPEGDEC because you would need to have enough RAM to hold the entire uncompressed image in RAM. With BASELINE images, JPEGDEC can decompress them a little at a time.
Sarah-C commented
Check return codes on open...... Understood!
Thank you. =)