thibthibaut
1/29/2020 - 8:54 AM

Image stuff

Image stuff

const uint8_t rb528_table[32] = {
      0,   8,  16,  25,  33,  41,  49,  58,
     66,  74,  82,  90,  99, 107, 115, 123,
    132, 140, 148, 156, 165, 173, 181, 189,
    197, 206, 214, 222, 230, 239, 247, 255
};

const uint8_t g628_table[64] = {
      0,   4,   8,  12,  16,  20,  24,  28,
     32,  36,  40,  45,  49,  53,  57,  61,
     65,  69,  73,  77,  81,  85,  89,  93,
     97, 101, 105, 109, 113, 117, 121, 125,
    130, 134, 138, 142, 146, 150, 154, 158,
    162, 166, 170, 174, 178, 182, 186, 190,
    194, 198, 202, 206, 210, 215, 219, 223,
    227, 231, 235, 239, 243, 247, 251, 255
};

#define RGB565_TO_R5(pix) { \
(uint8_t)(0x1f & ((pix)>>11)) \
}
#define RGB565_TO_G6(pix) { \
(uint8_t)(0x3f & ((pix)>>5)) \
}
#define RGB565_TO_B5(pix) { \
(uint8_t)(0x1f & (pix) ) \
}

typedef enum image_type {
	GREYSCALE = 0,
	RGB565,
	RGB888
} image_type_t;

typedef struct image {
	image_type_t type;
	uint16_t width;
	uint16_t height;
	void *data;
} image_t;


void resize565_and_convert_to_888(const image_t *src, image_t *dst){

	int srcW = src->width;
	int srcH = src->height;
	int dstW = dst->width;
	int dstH = dst->height;

	int x_ratio = (int)((srcW<<16)/dstW)+1;
	int y_ratio = (int)((srcH<<16)/dstH)+1;

	// Get input and output buffers
	uint16_t* rgb565_buffer = (uint16_t*) &src->data[0];
	uint8_t* rgb888_buffer = &dst->data[0];

	for (int y=0, i=0; y<dstH; y++)
	{
		int sy = (y*y_ratio)>>16;
		for (int x=0; x<dstW; x++, i+=3)
		{
			int sx = (x*x_ratio)>>16;

			uint16_t src_pix = rgb565_buffer[(sy*srcW) + sx];
			uint8_t r5 = RGB565_TO_R5(src_pix);
			uint8_t g6 = RGB565_TO_G6(src_pix);
			uint8_t b5 = RGB565_TO_B5(src_pix);

			uint8_t r8 = rb528_table[r5];
			uint8_t g8 = g628_table[g6];
			uint8_t b8 = rb528_table[b5];

			rgb888_buffer[i + 0] = r8;
			rgb888_buffer[i + 1] = g8;
			rgb888_buffer[i + 2] = b8;
		}
	}
}