acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

JpegFunc.h (2874B)

      1 /*******************************************************************************
      2  * JPEGDEC related function
      3  *
      4  * Dependent libraries:
      5  * JPEGDEC: https://github.com/bitbank2/JPEGDEC.git
      6  ******************************************************************************/
      7 #ifndef _JPEGFUNC_H_
      8 #define _JPEGFUNC_H_
      9 
     10 #include <JPEGDEC.h>
     11 
     12 static JPEGDEC _jpeg;
     13 static File _f;
     14 static int _x, _y, _x_bound, _y_bound;
     15 
     16 static void *jpegOpenFile(const char *szFilename, int32_t *pFileSize)
     17 {
     18     // Serial.println("jpegOpenFile");
     19 #if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
     20     _f = SD.open(szFilename, "r");
     21 #elif defined(TARGET_RP2040)
     22     _f = LittleFS.open(szFilename, "r");
     23     // _f = SDFS.open(szFilename, "r");
     24 #elif defined(ESP32)
     25     // _f = FFat.open(szFilename, "r");
     26     _f = LittleFS.open(szFilename, "r");
     27     // _f = SPIFFS.open(szFilename, "r");
     28     // _f = SD.open(szFilename, "r");
     29     // _f = SD_MMC.open(szFilename, "r");
     30 #elif defined(ESP8266)
     31     _f = LittleFS.open(szFilename, "r");
     32     // _f = SD.open(szFilename, "r");
     33 #else
     34     _f = SD.open(szFilename, FILE_READ);
     35 #endif
     36     *pFileSize = _f.size();
     37     return &_f;
     38 }
     39 
     40 static void jpegCloseFile(void *pHandle)
     41 {
     42     // Serial.println("jpegCloseFile");
     43     File *f = static_cast<File *>(pHandle);
     44     f->close();
     45 }
     46 
     47 static int32_t jpegReadFile(JPEGFILE *pFile, uint8_t *pBuf, int32_t iLen)
     48 {
     49     // Serial.printf("jpegReadFile, iLen: %d\n", iLen);
     50     File *f = static_cast<File *>(pFile->fHandle);
     51     size_t r = f->read(pBuf, iLen);
     52     return r;
     53 }
     54 
     55 static int32_t jpegSeekFile(JPEGFILE *pFile, int32_t iPosition)
     56 {
     57     // Serial.printf("jpegSeekFile, pFile->iPos: %d, iPosition: %d\n", pFile->iPos, iPosition);
     58     File *f = static_cast<File *>(pFile->fHandle);
     59     f->seek(iPosition);
     60     return iPosition;
     61 }
     62 
     63 static void jpegDraw(
     64     const char *filename, JPEG_DRAW_CALLBACK *jpegDrawCallback, bool useBigEndian,
     65     int x, int y, int widthLimit, int heightLimit)
     66 {
     67     _x = x;
     68     _y = y;
     69     _x_bound = _x + widthLimit - 1;
     70     _y_bound = _y + heightLimit - 1;
     71 
     72     _jpeg.open(filename, jpegOpenFile, jpegCloseFile, jpegReadFile, jpegSeekFile, jpegDrawCallback);
     73 
     74     // scale to fit height
     75     int _scale;
     76     int iMaxMCUs;
     77     float ratio = (float)_jpeg.getHeight() / heightLimit;
     78     if (ratio <= 1)
     79     {
     80         _scale = 0;
     81         iMaxMCUs = widthLimit / 16;
     82     }
     83     else if (ratio <= 2)
     84     {
     85         _scale = JPEG_SCALE_HALF;
     86         iMaxMCUs = widthLimit / 8;
     87     }
     88     else if (ratio <= 4)
     89     {
     90         _scale = JPEG_SCALE_QUARTER;
     91         iMaxMCUs = widthLimit / 4;
     92     }
     93     else
     94     {
     95         _scale = JPEG_SCALE_EIGHTH;
     96         iMaxMCUs = widthLimit / 2;
     97     }
     98     _jpeg.setMaxOutputSize(iMaxMCUs);
     99     if (useBigEndian)
    100     {
    101         _jpeg.setPixelType(RGB565_BIG_ENDIAN);
    102     }
    103     _jpeg.decode(x, y, _scale);
    104     _jpeg.close();
    105 }
    106 
    107 #endif // _JPEGFUNC_H_