From bba81c89769671fcea60cbe4585ee5478062239c Mon Sep 17 00:00:00 2001 From: Bodmer Date: Sun, 24 Sep 2023 21:09:12 +0100 Subject: [PATCH] Update for ESP8266 and latest Arduino board support package ESP8266 board support package 2.3.0 or later required --- README.md | 2 +- .../SPIFFS_Jpeg/SPIFFS_functions.ino | 2 +- library.json | 2 +- library.properties | 2 +- src/JPEGDecoder.cpp | 30 ++++++------ src/JPEGDecoder.h | 47 +++++++++---------- src/User_Config.h | 4 +- src/picojpeg.c | 25 +++++++--- 8 files changed, 64 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 6219474..86dfe31 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Arduino JPEGDecoder library News: October 2019 - *I have created a new Arduino compatible [Jpeg decoder library here.](https://github.com/Bodmer/TJpg_Decoder) This new library is based on TinyJpegDec, this is 60% faster on 32bit processors, it also has a much simpler interface.* -This Arduino library supports the rendering of Jpeg files stored both on SD card and in arrays within program memory (FLASH) onto a TFT display. In addition images stored in the SPIFFS (or LittleFS) Flash filing system or "PROGMEM" arrays can be used with the ESP8266 and ESP32 processors. For the ESP8266 use board Core 2.3.0 (or later) in the Arduino IDE to avoid File definition conflicts if the SPIFFS and SD libraries are used together. +This Arduino library supports the rendering of Jpeg files stored both on SD card and in arrays within program memory (FLASH) onto a TFT display. In addition images stored in the LittleFS Flash filing system or "PROGMEM" arrays can be used with the RP2040, ESP8266 and ESP32 processors. For the ESP8266 use board Core 2.3.0 (or later) in the Arduino IDE to avoid File definition conflicts if the SPIFFS and SD libraries are used together. The library works on the Arduino Due, ESP32 and ESP8266 (e.g. NodeMCU 1.0). Users have also reported success with the STM32 based processor boards. diff --git a/examples/Other libraries/SPIFFS_Jpeg/SPIFFS_functions.ino b/examples/Other libraries/SPIFFS_Jpeg/SPIFFS_functions.ino index e5e3f5a..cb6b92f 100644 --- a/examples/Other libraries/SPIFFS_Jpeg/SPIFFS_functions.ino +++ b/examples/Other libraries/SPIFFS_Jpeg/SPIFFS_functions.ino @@ -8,7 +8,7 @@ //==================================================================================== // Print a SPIFFS directory list (root directory) //==================================================================================== -#ifdef ESP8266 +#ifdef ARDUINO_ARCH_ESP8266 void listFiles(void) { Serial.println(); Serial.println("SPIFFS files found:"); diff --git a/library.json b/library.json index 022fd48..eb78522 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "JPEGDecoder", - "version": "1.8.1", + "version": "2.0.0", "keywords": "jpeg, jpg, decoder, TFT", "description": "A JPEG decoder library, tested on Mega, Due and ESP8266", "repository": diff --git a/library.properties b/library.properties index 932f95e..0d9f8c7 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=JPEGDecoder -version=1.8.1 +version=2.0.0 author=Bodmer , Makoto Kurauchi, Rich Geldreich maintainer=Bodmer sentence= Jpeg decoder tested with Arduino Mega, Arduino Due and ESP8266 based NodeMCU 1.0 diff --git a/src/JPEGDecoder.cpp b/src/JPEGDecoder.cpp index 87430b8..1b8c7b8 100644 --- a/src/JPEGDecoder.cpp +++ b/src/JPEGDecoder.cpp @@ -34,6 +34,8 @@ Bodmer (20/1/17): Prevent deleting the pImage pointer twice (causes an exception tidy up code. Bodmer (24/1/17): Correct greyscale images, update examples + +Bodmer (26/2/22): Removed deprecated SPIFFS */ #include "JPEGDecoder.h" @@ -65,18 +67,18 @@ uint8_t JPEGDecoder::pjpeg_callback(uint8_t* pBuf, uint8_t buf_size, uint8_t *pB uint8_t JPEGDecoder::pjpeg_need_bytes_callback(uint8_t* pBuf, uint8_t buf_size, uint8_t *pBytes_actually_read, void *pCallback_data) { uint n; - //pCallback_data; + pCallback_data = pCallback_data; // Supress warning n = jpg_min(g_nInFileSize - g_nInFileOfs, buf_size); if (jpg_source == JPEG_ARRAY) { // We are handling an array - for (int i = 0; i < n; i++) { + for (uint i = 0; i < n; i++) { pBuf[i] = pgm_read_byte(jpg_data++); //Serial.println(pBuf[i],HEX); } } -#ifdef LOAD_SPIFFS +#ifdef LOAD_FLASH_FS if (jpg_source == JPEG_FS_FILE) g_pInFileFs.read(pBuf,n); // else we are handling a file #endif @@ -270,10 +272,10 @@ int JPEGDecoder::readSwappedBytes(void) { } -// Generic file call for SD or SPIFFS, uses leading / to distinguish SPIFFS files +// Generic file call for SD or Little_FS, uses leading / to distinguish Little_FS files int JPEGDecoder::decodeFile(const char *pFilename){ -#if defined (ESP8266) || defined (ESP32) +#if defined (ARDUINO_ARCH_ESP8266) || defined (ESP32) #if defined (LOAD_SD_LIBRARY) || defined (LOAD_SDFAT_LIBRARY) if (*pFilename == '/') #endif @@ -289,7 +291,7 @@ int JPEGDecoder::decodeFile(const char *pFilename){ int JPEGDecoder::decodeFile(const String& pFilename){ -#if defined (ESP8266) || defined (ESP32) +#if defined (ARDUINO_ARCH_ESP8266) || defined (ESP32) #if defined (LOAD_SD_LIBRARY) || defined (LOAD_SDFAT_LIBRARY) if (pFilename.charAt(0) == '/') #endif @@ -304,32 +306,32 @@ int JPEGDecoder::decodeFile(const String& pFilename){ } -#ifdef LOAD_SPIFFS +#ifdef LOAD_FLASH_FS -// Call specific to SPIFFS +// Call specific to Little_FS int JPEGDecoder::decodeFsFile(const char *pFilename) { - fs::File pInFile = SPIFFS.open( pFilename, "r"); + fs::File pInFile = LittleFS.open( pFilename, "r"); return decodeFsFile(pInFile); } int JPEGDecoder::decodeFsFile(const String& pFilename) { - fs::File pInFile = SPIFFS.open( pFilename, "r"); + fs::File pInFile = LittleFS.open( pFilename, "r"); return decodeFsFile(pInFile); } -int JPEGDecoder::decodeFsFile(fs::File jpgFile) { // This is for the SPIFFS library +int JPEGDecoder::decodeFsFile(fs::File jpgFile) { // This is for the Little_FS library g_pInFileFs = jpgFile; - jpg_source = JPEG_FS_FILE; // Flag to indicate a SPIFFS file + jpg_source = JPEG_FS_FILE; // Flag to indicate a Little_FS file if (!g_pInFileFs) { #ifdef DEBUG - Serial.println("ERROR: SPIFFS file not found!"); + Serial.println("ERROR: Little_FS file not found!"); #endif return -1; @@ -465,7 +467,7 @@ void JPEGDecoder::abort(void) { if(pImage) delete[] pImage; pImage = NULL; -#ifdef LOAD_SPIFFS +#ifdef LOAD_FLASH_FS if (jpg_source == JPEG_FS_FILE) if (g_pInFileFs) g_pInFileFs.close(); #endif diff --git a/src/JPEGDecoder.h b/src/JPEGDecoder.h index be6cc92..8c1018d 100644 --- a/src/JPEGDecoder.h +++ b/src/JPEGDecoder.h @@ -27,32 +27,31 @@ Latest version here: #define PROGMEM __attribute__((section(".fini2"))) #endif - #if defined (ESP8266) || defined (ESP32) - - //#include "arduino.h" - #include + #ifdef ESP32 // SDFAT library not compatible with ESP32 + //#undef LOAD_SD_LIBRARY + #undef LOAD_SDFAT_LIBRARY + #endif -// If the sketch has included FS.h without setting FS_NO_GLOBALS first then it is likely -// there will be a redefinition of 'class fs::File' error due to conflict with the -// SD library, so we can't load the SD library. (At 12/1/18 this appears to be fixed) - //#if !defined (FS_NO_GLOBALS) && defined (FS_H) - //#undef LOAD_SD_LIBRARY - //#undef LOAD_SDFAT_LIBRARY - //#endif - - #ifdef ESP32 // SDFAT library not compatible with ESP32 - //#undef LOAD_SD_LIBRARY - #undef LOAD_SDFAT_LIBRARY + // New ESP8266 board package uses ARDUINO_ARCH_ESP8266 + // old package defined ESP8266 + #if defined (ESP8266) + #ifndef ARDUINO_ARCH_ESP8266 + #define ARDUINO_ARCH_ESP8266 #endif + #endif - #define LOAD_SPIFFS - #define FS_NO_GLOBALS + #if defined (ARDUINO_ARCH_ESP8266) || defined (ESP32) + #define LOAD_FLASH_FS + #include #include - - #ifdef ESP32 - #include "SPIFFS.h" // ESP32 only - #endif - + #include + #define SPIFFS LittleFS + #elif defined (ARDUINO_ARCH_RP2040) + #define LOAD_FLASH_FS + #include + #include + #define SPIFFS LittleFS + #define TJPGD_LOAD_FFS #endif #if defined (LOAD_SD_LIBRARY) || defined (LOAD_SDFAT_LIBRARY) @@ -90,7 +89,7 @@ class JPEGDecoder { #if defined (LOAD_SD_LIBRARY) || defined (LOAD_SDFAT_LIBRARY) File g_pInFileSd; #endif -#ifdef LOAD_SPIFFS +#ifdef LOAD_FLASH_FS fs::File g_pInFileFs; #endif pjpeg_scan_type_t scan_type; @@ -144,7 +143,7 @@ class JPEGDecoder { int decodeSdFile (File g_pInFile); #endif -#ifdef LOAD_SPIFFS +#ifdef LOAD_FLASH_FS int decodeFsFile (const char *pFilename); int decodeFsFile (const String& pFilename); int decodeFsFile (fs::File g_pInFile); diff --git a/src/User_Config.h b/src/User_Config.h index 4bce9aa..75d90a9 100644 --- a/src/User_Config.h +++ b/src/User_Config.h @@ -29,7 +29,7 @@ File jpegFile = SD.open( filename, FILE_READ); // duplicate definitions in the SD library and the SPIFFS library. -#ifdef ESP8266 +#ifdef ARDUINO_ARCH_ESP8266 // Uncomment out the next #define if you want the bytes swapped in the image blocks // returned by read(). @@ -38,5 +38,5 @@ File jpegFile = SD.open( filename, FILE_READ); // the sketch. Images will look psychedelic with wrong colours if the SPI transmit byte // order is not right for your sketch! - // #define SWAP_BYTES // Deprecated, only included for backwards compatibility + #define SWAP_BYTES // Deprecated, only included for backwards compatibility #endif diff --git a/src/picojpeg.c b/src/picojpeg.c index 6fc1ae5..e6157f9 100644 --- a/src/picojpeg.c +++ b/src/picojpeg.c @@ -211,7 +211,9 @@ static uint8 gMaxMCUXSize; static uint8 gMaxMCUYSize; static uint16 gMaxMCUSPerRow; static uint16 gMaxMCUSPerCol; -static uint16 gNumMCUSRemaining; + +static uint16 gNumMCUSRemainingX, gNumMCUSRemainingY; + static uint8 gMCUOrg[6]; static pjpeg_need_bytes_callback_t g_pNeedBytesCallback; @@ -1025,6 +1027,7 @@ static uint8 processRestart(void) //------------------------------------------------------------------------------ // FIXME: findEOI() is not actually called at the end of the image // (it's optional, and probably not needed on embedded devices) +/* static uint8 findEOI(void) { uint8 c; @@ -1048,6 +1051,7 @@ static uint8 findEOI(void) return 0; } +*/ //------------------------------------------------------------------------------ static uint8 checkHuffTables(void) { @@ -1196,7 +1200,10 @@ static uint8 initFrame(void) gMaxMCUSPerRow = (gImageXSize + (gMaxMCUXSize - 1)) >> ((gMaxMCUXSize == 8) ? 3 : 4); gMaxMCUSPerCol = (gImageYSize + (gMaxMCUYSize - 1)) >> ((gMaxMCUYSize == 8) ? 3 : 4); - gNumMCUSRemaining = gMaxMCUSPerRow * gMaxMCUSPerCol; + // This can overflow on large JPEG's. + //gNumMCUSRemaining = gMaxMCUSPerRow * gMaxMCUSPerCol; + gNumMCUSRemainingX = gMaxMCUSPerRow; + gNumMCUSRemainingY = gMaxMCUSPerCol; return 0; } @@ -1207,7 +1214,7 @@ static uint8 initFrame(void) #define PJPG_DCT_SCALE (1U << PJPG_DCT_SCALE_BITS) -#define PJPG_DESCALE(x) PJPG_ARITH_SHIFT_RIGHT_N_16(((x) + (1U << (PJPG_DCT_SCALE_BITS - 1))), PJPG_DCT_SCALE_BITS) +#define PJPG_DESCALE(x) PJPG_ARITH_SHIFT_RIGHT_N_16(((x) + (1 << (PJPG_DCT_SCALE_BITS - 1))), PJPG_DCT_SCALE_BITS) #define PJPG_WFIX(x) ((x) * PJPG_DCT_SCALE + 0.5f) @@ -2267,14 +2274,20 @@ unsigned char pjpeg_decode_mcu(void) if (gCallbackStatus) return gCallbackStatus; - if (!gNumMCUSRemaining) + if ((!gNumMCUSRemainingX) && (!gNumMCUSRemainingY)) return PJPG_NO_MORE_BLOCKS; - + status = decodeNextMCU(); if ((status) || (gCallbackStatus)) return gCallbackStatus ? gCallbackStatus : status; - gNumMCUSRemaining--; + gNumMCUSRemainingX--; + if (!gNumMCUSRemainingX) + { + gNumMCUSRemainingY--; + if (gNumMCUSRemainingY > 0) + gNumMCUSRemainingX = gMaxMCUSPerRow; + } return 0; }