1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
// Return the minimum of two values a and b
#define minimum(a,b) (((a) < (b)) ? (a) : (b))
#define USE_SPI_BUFFER // Comment out to use slower 16 bit pushColor()
//====================================================================================
// Opens the image file and prime the Jpeg decoder
//====================================================================================
bool drawJpeg(String filename, int xpos, int ypos) {
Serial.print(F("Drawing file: ")); Serial.println(filename);
// Open the named file (the Jpeg decoder library will close it after rendering image)
#ifdef pin_sd
Serial.print("SD ");
Serial.println(filename);
File jpegFile = SD.open( filename, FILE_READ); // or, file handle reference for SD library
#else
Serial.print("Spiffs ");
Serial.println(filename);
//fs::File jpegFile = SPIFFS.open( filename, "r"); // File handle reference for SPIFFS
fs::File jpegFile = LittleFS.open( filename, "r"); // File handle reference for SPIFFS
#endif
if ( !jpegFile ) {
Serial.print(F("ERROR: File \""));
Serial.print(filename);
Serial.println (F("\" not found!"));
jpegFile.close();
return false;
}
// Use one of the three following methods to initialise the decoder:
//boolean decoded = JpegDec.decodeFsFile(jpegFile); // Pass a SPIFFS file handle to the decoder,
#ifdef pin_sd
boolean decoded = JpegDec.decodeSdFile(jpegFile); // or pass the SD file handle to the decoder,
#else
boolean decoded = JpegDec.decodeFsFile(filename); // ou transmettre le nom du fichier (le premier / distingue les fichiers SPIFFS)
#endif // Note: the filename can be a String or character array type
if (decoded) {
// print information about the image to the serial port
//jpegInfo();
//if (tft2_act) jpegRender2(xpos, ypos);
// else
jpegRender(xpos, ypos);
}
else {
Serial.println(F("Jpeg file format not supported!"));
jpegFile.close();
return false;
}
jpegFile.close();
return true;
}
//====================================================================================
// 2 TFT jpegRender
//====================================================================================
#if defined (two_tft)
void jpegRender(int xpos, int ypos) {
// retrieve infomration about the image
//uint16_t *pImg;
//uint16_t mcu_w = JpegDec.MCUWidth;
//uint16_t mcu_h = JpegDec.MCUHeight;
uint32_t *pImg;
uint32_t mcu_w = JpegDec.MCUWidth;
uint32_t mcu_h = JpegDec.MCUHeight;
uint32_t max_x = JpegDec.width;
uint32_t max_y = JpegDec.height;
// Les images Jpeg sont dessinées sous la forme d'un ensemble de blocs d'images (tuiles) appelés unités de codage minimales (MCU).
// Généralement, ces MCU sont des blocs de 16x16 pixels.
// Déterminez la largeur et la hauteur des blocs d'image des bords droit et inférieur.
uint32_t min_w = minimum(mcu_w, max_x % mcu_w);
uint32_t min_h = minimum(mcu_h, max_y % mcu_h);
// save the current image block size
uint32_t win_w = mcu_w;
uint32_t win_h = mcu_h;
// record the current time so we can measure how long it takes to draw an image
uint32_t drawTime = millis();
// save the coordinate of the right and bottom edges to assist image cropping
// to the screen size
max_x += xpos;
max_y += ypos;
// read each MCU block until there are no more
while ( JpegDec.read()) {
//while ( JpegDec.readSwappedBytes()) {
// save a pointer to the image block
pImg = JpegDec.pImage;
// calculate where the image block should be drawn on the screen
int mcu_x = JpegDec.MCUx * mcu_w + xpos;
int mcu_y = JpegDec.MCUy * mcu_h + ypos;
// 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)
{
for (int h = 1; h < win_h-1; h++)
{
memcpy(pImg + h * win_w, pImg + (h + 1) * mcu_w, win_w << 1);
}
}
// draw image MCU block only if it will fit on the screen
//-------------------
if (tft2_act) //active tft №2
{
if ( ( mcu_x + win_w) <= tft2.width() && ( mcu_y + win_h) <= tft2.height())
{
tft2.drawRGBBitmap(mcu_x, mcu_y, pImg, win_w, win_h);
}
else if ( ( mcu_y + win_h) >= tft2.height()) JpegDec.abort();
}
else
{
if ( ( mcu_x + win_w) <= tft.width() && ( mcu_y + win_h) <= tft.height())
{
tft.drawRGBBitmap(mcu_x, mcu_y, pImg, win_w, win_h);
}
else if ( ( mcu_y + win_h) >= tft.height()) JpegDec.abort();
}
//--------------------
}
}
//---------------------------------------------------------------------------
// 1 TFT jpegRender
//---------------------------------------------------------------------------
#else //1 tft
void jpegRender(int xpos, int ypos) {
// retrieve infomration about the image
uint16_t *pImg;
uint16_t mcu_w = JpegDec.MCUWidth;
uint16_t mcu_h = JpegDec.MCUHeight;
uint32_t max_x = JpegDec.width;
uint32_t max_y = JpegDec.height;
// Jpeg images are draw as a set of image block (tiles) called Minimum Coding Units (MCUs)
// Typically these MCUs are 16x16 pixel blocks
// Determine the width and height of the right and bottom edge image blocks
uint32_t min_w = minimum(mcu_w, max_x % mcu_w);
uint32_t min_h = minimum(mcu_h, max_y % mcu_h);
// save the current image block size
uint32_t win_w = mcu_w;
uint32_t win_h = mcu_h;
// save the coordinate of the right and bottom edges to assist image cropping
// to the screen size
max_x += xpos;
max_y += ypos;
// read each MCU block until there are no more
while (JpegDec.read()) {
// save a pointer to the image block
pImg = JpegDec.pImage ;
// calculate where the image block should be drawn on the screen
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 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;
tft.startWrite();
// 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())
{
// Now set a MCU bounding window on the TFT to push pixels into (x, y, x + width - 1, y + height - 1)
tft.setAddrWindow(mcu_x, mcu_y, win_w, win_h);
// Write all MCU pixels to the TFT window
while (mcu_pixels--) {
// Push each pixel to the TFT MCU area
tft.pushColor(*pImg++);
}
}
else if ( (mcu_y + win_h) >= tft.height()) JpegDec.abort(); // Image has run off bottom of screen so abort decoding
tft.endWrite();
}
}
#endif // 1 TFT |