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 (3090B)

      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 int _x, _y, _x_bound, _y_bound;
     14 
     15 static void jpegCloseHttpStream(void *pHandle)
     16 {
     17   // printf("jpegCloseHttpStream\n");
     18   // WiFiClient *http_stream = (WiFiClient *)pHandle;
     19   // do nothing
     20 }
     21 
     22 static int32_t readStream(WiFiClient *http_stream, uint8_t *pBuf, int32_t iLen)
     23 {
     24   uint8_t wait = 0;
     25   size_t a = http_stream->available();
     26   size_t r = 0;
     27   while ((r < iLen) && (wait < 10))
     28   {
     29     if (a)
     30     {
     31       wait = 0; // reset wait count once available
     32       r += http_stream->readBytes(pBuf + r, iLen - r);
     33       // printf("1st byte: %d, 2nd byte: %d, last byte: %d, iLen: %d, r: %d, wait: %d\n", pBuf[0], pBuf[1], pBuf[r - 1], iLen, r, wait);
     34     }
     35     else
     36     {
     37       delay(100);
     38       wait++;
     39     }
     40     a = http_stream->available();
     41   }
     42   return r;
     43 }
     44 
     45 static int32_t jpegReadHttpStream(JPEGFILE *pFile, uint8_t *pBuf, int32_t iLen)
     46 {
     47   // printf("jpegReadHttpStream, iLen: %d\n", iLen);
     48   WiFiClient *http_stream = (WiFiClient *)pFile->fHandle;
     49   return readStream(http_stream, pBuf, iLen);
     50 }
     51 
     52 static int32_t jpegSeekHttpStream(JPEGFILE *pFile, int32_t iPosition)
     53 {
     54   // printf("jpegSeekHttpStream, pFile->iPos: %d, iPosition: %d\n", pFile->iPos, iPosition);
     55   WiFiClient *http_stream = (WiFiClient *)pFile->fHandle;
     56   http_stream->readBytes((uint8_t *)nullptr, iPosition - pFile->iPos);
     57   return iPosition;
     58 }
     59 
     60 static int jpegOpenHttpStreamWithBuffer(WiFiClient *http_stream, uint8_t *buf, int32_t dataSize, JPEG_DRAW_CALLBACK *jpegDrawCallback)
     61 {
     62   int32_t r = readStream(http_stream, buf, dataSize);
     63   if (r != dataSize)
     64   {
     65     return 0;
     66   }
     67   return _jpeg.openRAM(buf, dataSize, jpegDrawCallback);
     68 }
     69 
     70 static int jpegOpenHttpStream(WiFiClient *http_stream, int32_t dataSize, JPEG_DRAW_CALLBACK *jpegDrawCallback)
     71 {
     72   return _jpeg.open(http_stream, dataSize, jpegCloseHttpStream, jpegReadHttpStream, jpegSeekHttpStream, jpegDrawCallback);
     73 }
     74 
     75 static int jpegDraw(bool useBigEndian,
     76                     int x, int y, int widthLimit, int heightLimit)
     77 {
     78   _x = x;
     79   _y = y;
     80   _x_bound = _x + widthLimit - 1;
     81   _y_bound = _y + heightLimit - 1;
     82 
     83   // scale to fit height
     84   int _scale;
     85   int iMaxMCUs;
     86   float ratio = (float)_jpeg.getHeight() / heightLimit;
     87   if (ratio <= 1)
     88   {
     89     _scale = 0;
     90     iMaxMCUs = widthLimit / 16;
     91   }
     92   else if (ratio <= 2)
     93   {
     94     _scale = JPEG_SCALE_HALF;
     95     iMaxMCUs = widthLimit / 8;
     96   }
     97   else if (ratio <= 4)
     98   {
     99     _scale = JPEG_SCALE_QUARTER;
    100     iMaxMCUs = widthLimit / 4;
    101   }
    102   else
    103   {
    104     _scale = JPEG_SCALE_EIGHTH;
    105     iMaxMCUs = widthLimit / 2;
    106   }
    107   _jpeg.setMaxOutputSize(iMaxMCUs);
    108   if (useBigEndian)
    109   {
    110     _jpeg.setPixelType(RGB565_BIG_ENDIAN);
    111   }
    112   int decode_result = _jpeg.decode(x, y, _scale);
    113   _jpeg.close();
    114 
    115   return decode_result;
    116 }
    117 
    118 #endif // _JPEGFUNC_H_