Skip to content

Commit

Permalink
Update for ESP8266 and latest Arduino board support package
Browse files Browse the repository at this point in the history
ESP8266 board support package 2.3.0 or later required
  • Loading branch information
Bodmer committed Sep 24, 2023
1 parent c0fad79 commit bba81c8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion examples/Other libraries/SPIFFS_Jpeg/SPIFFS_functions.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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:");
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=JPEGDecoder
version=1.8.1
version=2.0.0
author=Bodmer <[email protected]>, Makoto Kurauchi, Rich Geldreich
maintainer=Bodmer
sentence= Jpeg decoder tested with Arduino Mega, Arduino Due and ESP8266 based NodeMCU 1.0
Expand Down
30 changes: 16 additions & 14 deletions src/JPEGDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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

Expand Down
47 changes: 23 additions & 24 deletions src/JPEGDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,31 @@ Latest version here:
#define PROGMEM __attribute__((section(".fini2")))
#endif

#if defined (ESP8266) || defined (ESP32)

//#include "arduino.h"
#include <pgmspace.h>
#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 <pgmspace.h>
#include <FS.h>

#ifdef ESP32
#include "SPIFFS.h" // ESP32 only
#endif

#include <LittleFS.h>
#define SPIFFS LittleFS
#elif defined (ARDUINO_ARCH_RP2040)
#define LOAD_FLASH_FS
#include <FS.h>
#include <LittleFS.h>
#define SPIFFS LittleFS
#define TJPGD_LOAD_FFS
#endif

#if defined (LOAD_SD_LIBRARY) || defined (LOAD_SDFAT_LIBRARY)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/User_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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().

Expand All @@ -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
25 changes: 19 additions & 6 deletions src/picojpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -1048,6 +1051,7 @@ static uint8 findEOI(void)
return 0;
}
*/
//------------------------------------------------------------------------------
static uint8 checkHuffTables(void)
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit bba81c8

Please sign in to comment.