From 04932b5231de4c3874206087f27bc931f038a794 Mon Sep 17 00:00:00 2001 From: Thomas Ballmann Date: Thu, 19 Mar 2020 17:35:49 +0100 Subject: [PATCH] very basic jpeg image decoder working #7 --- src/image.cpp | 22 +++++++++++++ src/imageJPEG.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 2 +- 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/imageJPEG.cpp diff --git a/src/image.cpp b/src/image.cpp index c839246..c6f0992 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -1,9 +1,16 @@ #include "image.h" #include "imageWBMP.h" #include "imagePNG.h" +#include "imageJPEG.h" structImageProcess ImageProcess; +void setupImage() +{ + setupImagePNG(); + setupImageJPEG(); +} + void ImageNew(int x, int y, int w, int h, bool dithering) { Serial.printf("ImageNew: x: %d, y: %d, dithering: %d \n", x, y, dithering); @@ -34,6 +41,13 @@ void ImageWriteBuffer(uint8_t buff[], size_t c) pngOpenFramebuffer(); } + else if (memcmp(buff, ImageHeaderJPEG, sizeof(ImageHeaderJPEG) - 1) == 0) + { + Serial.println(" image format: JPEG"); + ImageProcess.format = 4; + + jpegOpenFramebuffer(); + } else { ImageProcess.format = 1; @@ -58,6 +72,10 @@ void ImageWriteBuffer(uint8_t buff[], size_t c) case 3: pngWriteFramebuffer(0, buff, c); break; + // JPEG + case 4: + jpegWriteFramebuffer(0, buff, c); + break; } } @@ -74,6 +92,10 @@ void ImageFlushBuffer() case 3: pngFlushFramebuffer(); break; + // JPEG + case 4: + jpegFlushFramebuffer(); + break; } // clear settings diff --git a/src/imageJPEG.cpp b/src/imageJPEG.cpp new file mode 100644 index 0000000..07aa2ed --- /dev/null +++ b/src/imageJPEG.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include "imageJPEG.h" +#include "display.h" + +File tmpFileBuffer; +bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap); + +void setupImageJPEG() +{ + Serial.println("setupJPEG"); + + // The jpeg image can be scaled by a factor of 1, 2, 4, or 8 + TJpgDec.setJpgScale(1); + + // The decoder must be given the exact name of the rendering function above + TJpgDec.setCallback(tft_output); +} + +void jpegOpenFramebuffer() +{ + displayOpen(); + + SPIFFS.remove("/tmp.jpeg"); + + tmpFileBuffer = SPIFFS.open("/tmp.jpeg", FILE_WRITE); + if (!tmpFileBuffer) + { + Serial.println("Failed to open file for writing"); + } +} + +void jpegWriteFramebuffer(int offset, uint8_t bitmap[], int c) +{ + //Serial.println("jpegWriteFramebuffer"); + if (tmpFileBuffer) + { + tmpFileBuffer.write(bitmap, c); + } +} + +void jpegFlushFramebuffer() +{ + //Serial.println("jpegFlushFramebuffer"); + if (tmpFileBuffer) + { + tmpFileBuffer.close(); + TJpgDec.drawFsJpg(0, 0, "/tmp.jpeg"); + } +} + +// This next function will be called during decoding of the jpeg file to +// render each block to the TFT. If you use a different TFT library +// you will need to adapt this function to suit. +bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap) +{ + if (true) + { + Serial.print("tft_output: x = "); + Serial.print(x); + Serial.print(" y = "); + Serial.print(y); + Serial.print(" w = "); + Serial.print(w); + Serial.print(" h = "); + Serial.println(h); + } + + // Stop further decoding as image is running off bottom of screen + if (y >= display.height()) + return 0; + + // This might work instead if you adapt the sketch to use the Adafruit_GFX library + display.drawRGBBitmap(x, y, bitmap, w, h); + + // Return 1 to decode next block + return 1; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 14e76e7..a5358e6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,7 +35,7 @@ void setup() setupSettings(); setupDevice(); - setupImagePNG(); + setupImage(); setupWlan(); if (wlan_isConnected())