|
|
|
@ -2,8 +2,13 @@
|
|
|
|
|
#include "imageWBMP.h"
|
|
|
|
|
#include "imagePNG.h"
|
|
|
|
|
#include "imageJPEG.h"
|
|
|
|
|
#include "display.h"
|
|
|
|
|
|
|
|
|
|
structImageProcess ImageProcess;
|
|
|
|
|
// TODO use dynamic display width
|
|
|
|
|
static constexpr int MAX_WIDTH = 640;
|
|
|
|
|
static int16_t curRowDelta[MAX_WIDTH + 1];
|
|
|
|
|
static int16_t nextRowDelta[MAX_WIDTH + 1];
|
|
|
|
|
|
|
|
|
|
void setupImage()
|
|
|
|
|
{
|
|
|
|
@ -20,6 +25,9 @@ void ImageNew(int x, int y, int w, int h, bool dithering)
|
|
|
|
|
ImageProcess.w = w;
|
|
|
|
|
ImageProcess.h = h;
|
|
|
|
|
ImageProcess.dithering = dithering;
|
|
|
|
|
|
|
|
|
|
memset(curRowDelta, 0, sizeof(curRowDelta));
|
|
|
|
|
memset(nextRowDelta, 0, sizeof(nextRowDelta));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageWriteBuffer(uint8_t buff[], size_t c)
|
|
|
|
@ -105,4 +113,73 @@ void ImageFlushBuffer()
|
|
|
|
|
ImageProcess.w = 0;
|
|
|
|
|
ImageProcess.h = 0;
|
|
|
|
|
ImageProcess.dithering = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageProcessPixel(uint32_t x, uint32_t y, uint8_t rgba[4])
|
|
|
|
|
{
|
|
|
|
|
//uint32_t b = color & 0x001F; // 5 bits blue
|
|
|
|
|
//uint32_t g = color & 0x07E0; // 6 bits green
|
|
|
|
|
//uint32_t r = color & 0xF800; // 5 bits red
|
|
|
|
|
uint8_t r = rgba[0]; // 0 - 255
|
|
|
|
|
uint8_t g = rgba[1]; // 0 - 255
|
|
|
|
|
uint8_t b = rgba[2]; // 0 - 255
|
|
|
|
|
|
|
|
|
|
int16_t gray = round(r * 0.3 + g * 0.59 + b * 0.11);
|
|
|
|
|
int16_t blackOrWhite;
|
|
|
|
|
|
|
|
|
|
// Add errors to color if there are
|
|
|
|
|
if (ImageProcess.dithering)
|
|
|
|
|
{
|
|
|
|
|
gray += curRowDelta[x];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gray <= 127)
|
|
|
|
|
{
|
|
|
|
|
blackOrWhite = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
blackOrWhite = 255;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ImageProcess.dithering)
|
|
|
|
|
{
|
|
|
|
|
int16_t oldPixel = gray;
|
|
|
|
|
int16_t newPixel = blackOrWhite;
|
|
|
|
|
|
|
|
|
|
int err = oldPixel - newPixel;
|
|
|
|
|
|
|
|
|
|
if (x > 0)
|
|
|
|
|
{
|
|
|
|
|
nextRowDelta[x - 1] += err * 3 / 16;
|
|
|
|
|
}
|
|
|
|
|
nextRowDelta[x] += err * 5 / 16;
|
|
|
|
|
nextRowDelta[x + 1] += err * 1 / 16;
|
|
|
|
|
curRowDelta[x + 1] += err * 7 / 16;
|
|
|
|
|
|
|
|
|
|
if (x == 0 && y > 0)
|
|
|
|
|
{
|
|
|
|
|
// new line
|
|
|
|
|
memcpy(curRowDelta, nextRowDelta, sizeof(curRowDelta));
|
|
|
|
|
memset(nextRowDelta, 0, sizeof(nextRowDelta));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displayWritePixel(ImageProcess.x + x, ImageProcess.y + y, blackOrWhite);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t drawRGB24toRGB565(uint8_t r, uint8_t g, uint8_t b)
|
|
|
|
|
{
|
|
|
|
|
return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t drawRGB565toBGRA32(uint16_t color)
|
|
|
|
|
{
|
|
|
|
|
uint32_t bits = (uint32_t)color;
|
|
|
|
|
uint32_t blue = bits & 0x001F; // 5 bits blue
|
|
|
|
|
uint32_t green = bits & 0x07E0; // 6 bits green
|
|
|
|
|
uint32_t red = bits & 0xF800; // 5 bits red
|
|
|
|
|
|
|
|
|
|
// Return shifted bits with alpha set to 0xFF
|
|
|
|
|
return (red << 8) | (green << 5) | (blue << 3) | 0xFF000000;
|
|
|
|
|
}
|