From 80b5d91ba4bb96d107ee7a4066b8141b40e76be2 Mon Sep 17 00:00:00 2001 From: Bodmer Date: Wed, 11 Oct 2017 00:59:14 +0100 Subject: [PATCH] Fix right edge jpeg image corruption Jpeg images with non integer number of MCU pixels showed corrupted right edge. Fix is to concatenate pixels into a contiguous block. Examples corrected. --- .../TFT_flash_jpg.ino} | 22 ++++++++++++++++++- .../{TFT_FLASH_Jpeg => TFT_flash_jpg}/jpeg1.h | 0 .../{TFT_FLASH_Jpeg => TFT_flash_jpg}/jpeg2.h | 0 .../{TFT_FLASH_Jpeg => TFT_flash_jpg}/jpeg3.h | 0 .../{TFT_FLASH_Jpeg => TFT_flash_jpg}/jpeg4.h | 0 .../480 x 320/TFT_flash_jpg/TFT_flash_jpg.ino | 21 +++++++++++++++++- 6 files changed, 41 insertions(+), 2 deletions(-) rename examples/160 x 128/{TFT_FLASH_Jpeg/TFT_FLASH_Jpeg.ino => TFT_flash_jpg/TFT_flash_jpg.ino} (94%) rename examples/160 x 128/{TFT_FLASH_Jpeg => TFT_flash_jpg}/jpeg1.h (100%) rename examples/160 x 128/{TFT_FLASH_Jpeg => TFT_flash_jpg}/jpeg2.h (100%) rename examples/160 x 128/{TFT_FLASH_Jpeg => TFT_flash_jpg}/jpeg3.h (100%) rename examples/160 x 128/{TFT_FLASH_Jpeg => TFT_flash_jpg}/jpeg4.h (100%) diff --git a/examples/160 x 128/TFT_FLASH_Jpeg/TFT_FLASH_Jpeg.ino b/examples/160 x 128/TFT_flash_jpg/TFT_flash_jpg.ino similarity index 94% rename from examples/160 x 128/TFT_FLASH_Jpeg/TFT_FLASH_Jpeg.ino rename to examples/160 x 128/TFT_flash_jpg/TFT_flash_jpg.ino index 7d0f662..4da9af4 100644 --- a/examples/160 x 128/TFT_FLASH_Jpeg/TFT_FLASH_Jpeg.ino +++ b/examples/160 x 128/TFT_flash_jpg/TFT_flash_jpg.ino @@ -130,12 +130,32 @@ void renderJPEG(int xpos, int ypos) { int mcu_x = JpegDec.MCUx * mcu_w + xpos; // Calculate coordinates of top left corner of current MCU int mcu_y = JpegDec.MCUy * mcu_h + ypos; - // check if the image block size needs to be changed for the right and bottom edges + + // check if the image block size needs to be changed for the right edge if (mcu_x + mcu_w <= max_x) win_w = mcu_w; else win_w = min_w; + + // check if the image block size needs to be changed for the bottom edge if (mcu_y + mcu_h <= max_y) win_h = mcu_h; else win_h = min_h; + // copy pixels into a contiguous block + if (win_w != mcu_w) + { + uint16_t *cImg; + int p = 0; + cImg = pImg + win_w; + for (int h = 1; h < win_h; h++) + { + p += mcu_w; + for (int w = 0; w < win_w; w++) + { + *cImg = *(pImg + w + p); + cImg++; + } + } + } + // draw image MCU block only if it will fit on the screen if (( mcu_x + win_w ) <= tft.width() && ( mcu_y + win_h ) <= tft.height()) { diff --git a/examples/160 x 128/TFT_FLASH_Jpeg/jpeg1.h b/examples/160 x 128/TFT_flash_jpg/jpeg1.h similarity index 100% rename from examples/160 x 128/TFT_FLASH_Jpeg/jpeg1.h rename to examples/160 x 128/TFT_flash_jpg/jpeg1.h diff --git a/examples/160 x 128/TFT_FLASH_Jpeg/jpeg2.h b/examples/160 x 128/TFT_flash_jpg/jpeg2.h similarity index 100% rename from examples/160 x 128/TFT_FLASH_Jpeg/jpeg2.h rename to examples/160 x 128/TFT_flash_jpg/jpeg2.h diff --git a/examples/160 x 128/TFT_FLASH_Jpeg/jpeg3.h b/examples/160 x 128/TFT_flash_jpg/jpeg3.h similarity index 100% rename from examples/160 x 128/TFT_FLASH_Jpeg/jpeg3.h rename to examples/160 x 128/TFT_flash_jpg/jpeg3.h diff --git a/examples/160 x 128/TFT_FLASH_Jpeg/jpeg4.h b/examples/160 x 128/TFT_flash_jpg/jpeg4.h similarity index 100% rename from examples/160 x 128/TFT_FLASH_Jpeg/jpeg4.h rename to examples/160 x 128/TFT_flash_jpg/jpeg4.h diff --git a/examples/480 x 320/TFT_flash_jpg/TFT_flash_jpg.ino b/examples/480 x 320/TFT_flash_jpg/TFT_flash_jpg.ino index 39232a0..b3d7b96 100644 --- a/examples/480 x 320/TFT_flash_jpg/TFT_flash_jpg.ino +++ b/examples/480 x 320/TFT_flash_jpg/TFT_flash_jpg.ino @@ -146,12 +146,31 @@ void renderJPEG(int xpos, int ypos) { int mcu_x = JpegDec.MCUx * mcu_w + xpos; // Calculate coordinates of top left corner of current MCU int mcu_y = JpegDec.MCUy * mcu_h + ypos; - // check if the image block size needs to be changed for the right and bottom edges + // check if the image block size needs to be changed for the right edge if (mcu_x + mcu_w <= max_x) win_w = mcu_w; else win_w = min_w; + + // check if the image block size needs to be changed for the bottom edge if (mcu_y + mcu_h <= max_y) win_h = mcu_h; else win_h = min_h; + // copy pixels into a contiguous block + if (win_w != mcu_w) + { + uint16_t *cImg; + int p = 0; + cImg = pImg + win_w; + for (int h = 1; h < win_h; h++) + { + p += mcu_w; + for (int w = 0; w < win_w; w++) + { + *cImg = *(pImg + w + p); + cImg++; + } + } + } + // calculate how many pixels must be drawn uint32_t mcu_pixels = win_w * win_h;