You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

507 lines
12 KiB

#include "video.h"
#include "debayer.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <string>
std::string toBits(unsigned int i, int maxbits);
// macros to make code better readable
#define CE (*(src))
#define UP (*(src-src_w))
#define DN (*(src+src_w))
#define LE (*(src-1))
#define RI (*(src+1))
#define UPLE (*(src-src_w-1))
#define UPRI (*(src-src_w+1))
#define DNLE (*(src+src_w-1))
#define DNRI (*(src+src_w+1))
#define BITCONV(d) ((d>>8) & 0xff)
#define STORE *(dst++) = BITCONV(r); *(dst++) = BITCONV(g); *(dst++) = BITCONV(b); src++;
#define STORE8 *(dst++) = (r & 0xff); *(dst++) = (g & 0xff); *(dst++) = (b & 0xff); src++;
/*
The function converts a 16bit GRBG 2x2 CFA coded image into an 8bit
RGB image by bilinear interpolation.
*/
void debayer_grbg16 (uint16_t * src, int src_w, int src_h,
uint8_t * dst, int dst_w, int dst_h) {
// GG RR GG RR
// BB GG BB GG
// GG RR GG RR
// BB GG BB GG
uint32_t r, g, b;
int xs, ys;
// start with upper left pixel (green)
r = RI;
g = CE;
b = DN;
STORE;
// upper first line, starts with RR GG RR ...
for (xs = 1; xs < src_w - 1; xs+=2) {
// red pixel
r = CE;
g = (LE + RI + DN) / 3;
b = (DNLE + DNRI) / 2;
STORE;
// green pixel
r = (LE + RI) / 2;
g = CE;
b = DN;
STORE;
}
// upper right pixel (red)
r = CE;
g = (DN + LE) / 2;
b = DNLE;
STORE;
// go through the "body" of the image
for (ys = 1; ys < src_h - 1; ys+=2) {
// every second line with BB GG BB GG (start at 2nd line)
// left hand pixel (blue)
r = (UPRI + DNRI) / 2;
g = (UP + DN + RI) / 3;
b = CE;
STORE;
for (xs = 1; xs < src_w - 1; xs+=2) {
// green pixel
r = (UP + DN) / 2;
g = CE;
b = (LE + RI) / 2;
STORE;
// blue pixel
r = (UPLE + UPRI + DNLE + DNRI) / 4;
g = (LE + RI + UP + DN) / 4;
b = CE;
STORE;
}
// last pixel in line (green)
r = (UP + DN) / 2;
g = CE;
b = LE;
STORE;
// every second line with GG RR GG RR ... (start at 3rd line)
// left hand pixel (green)
r = RI;
g = CE;
b = (UP + DN) / 2;
STORE;
for (xs = 1; xs < src_w - 1; xs+=2) {
// red pixel
r = CE;
g = (LE + RI + UP + DN) / 4;
b = (UPLE + UPRI + DNLE + DNRI) / 4;
STORE;
// green pixel
r = (LE + RI) / 2;
g = CE;
b = (UP + DN) / 2;
STORE;
}
// last pixel in line (red)
r = CE;
g = (UP + DN + LE) / 3;
b = (UPLE + DNLE) / 2;
STORE;
}
// bottom left pixel
r = UPRI;
g = (UP + RI) / 2;
b = CE;
STORE;
// last line starting with GG BB GG ...
for (xs = 1; xs < src_w - 1; xs+=2) {
// green pixel
r = UP;
g = CE;
b = (LE + RI) / 2;
STORE;
// blue pixel
r = (UPLE + UPRI) / 2;
g = (LE + UP + RI) / 2;
b = CE;
STORE;
}
// bottom right pixel (green)
r = UP;
g = CE;
b = LE;
STORE;
}
/*
The function converts a 8bit GRBG 2x2 CFA coded image into an 8bit
RGB image by bilinear interpolation.
*/
void debayer_gbrg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, int dst_w, int dst_h) {
debayer_grbg8 (src, src_w, src_h, dst, dst_w, dst_h);
};
void debayer_rggb8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, int dst_w, int dst_h) {
debayer_grbg8 (src, src_w, src_h, dst, dst_w, dst_h);
};
void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, int dst_w, int dst_h) {
// GG RR GG RR
// BB GG BB GG
// GG RR GG RR
// BB GG BB GG
uint16_t r, g, b;
int xs, ys;
// start with upper left pixel (green)
r = RI;
g = CE;
b = DN;
STORE8;
// upper first line, starts with RR GG RR ...
for (xs = 1; xs < src_w - 1; xs+=2) {
// red pixel
r = CE;
g = (LE + RI + DN) / 3;
b = (DNLE + DNRI) / 2;
STORE8;
// green pixel
r = (LE + RI) / 2;
g = CE;
b = DN;
STORE8;
}
// upper right pixel (red)
r = CE;
g = (DN + LE) / 2;
b = DNLE;
STORE8;
// go through the "body" of the image
for (ys = 1; ys < src_h - 1; ys+=2) {
// every second line with BB GG BB GG (start at 2nd line)
// left hand pixel (blue)
r = (UPRI + DNRI) / 2;
g = (UP + DN + RI) / 3;
b = CE;
STORE8;
for (xs = 1; xs < src_w - 1; xs+=2) {
// green pixel
r = (UP + DN) / 2;
g = CE;
b = (LE + RI) / 2;
STORE8;
// blue pixel
r = (UPLE + UPRI + DNLE + DNRI) / 4;
g = (LE + RI + UP + DN) / 4;
b = CE;
STORE8;
}
// last pixel in line (green)
r = (UP + DN) / 2;
g = CE;
b = LE;
STORE8;
// every second line with GG RR GG RR ... (start at 3rd line)
// left hand pixel (green)
r = RI;
g = CE;
b = (UP + DN) / 2;
STORE8;
for (xs = 1; xs < src_w - 1; xs+=2) {
// red pixel
r = CE;
g = (LE + RI + UP + DN) / 4;
b = (UPLE + UPRI + DNLE + DNRI) / 4;
STORE8;
// green pixel
r = (LE + RI) / 2;
g = CE;
b = (UP + DN) / 2;
STORE8;
}
// last pixel in line (red)
r = CE;
g = (UP + DN + LE) / 3;
b = (UPLE + DNLE) / 2;
STORE8;
}
// bottom left pixel
r = UPRI;
g = (UP + RI) / 2;
b = CE;
STORE8;
// last line starting with GG BB GG ...
for (xs = 1; xs < src_w - 1; xs+=2) {
// green pixel
r = UP;
g = CE;
b = (LE + RI) / 2;
STORE8;
// blue pixel
r = (UPLE + UPRI) / 2;
g = (LE + UP + RI) / 2;
b = CE;
STORE8;
}
// bottom right pixel (green)
r = UP;
g = CE;
b = LE;
STORE8;
}
int MAX(int a, int m) { if (a > m) return m; else return a; };
#define PRGGB10P_U ((uint16_t)(*(psrc-src_w*2*10/8)))
#define PRGGB10P_D ((uint16_t)(*(psrc+src_w*2*10/8)))
#define PRGGB10P_L ((uint16_t)(*(psrc-2)*10/8))
#define PRGGB10P_R ((uint16_t)(*(psrc+2)*10/8))
#define PRGGB10P_UL ((uint16_t)(*(psrc-2-src_w*2*10/8)))
#define PRGGB10P_UR ((uint16_t)(*(psrc+2-src_w*2*10/8)))
#define PRGGB10P_DL ((uint16_t)(*(psrc-2-src_w*2*10/8)))
#define PRGGB10P_DR ((uint16_t)(*(psrc+2+src_w*2*10/8)))
void debayer_rggb10packet (uint8_t * src, int src_w, int src_h,
uint8_t * dst, int dst_w, int dst_h) {
int s, d;
int xs, ys, xd, yd;
unsigned char r, g, b;
int max = dst_w * dst_h * 3;
unsigned char *pdst;
unsigned char *psrc;
int shift;
printf ("%s:%d src size:%dx%d dst size:%dx%d\n", __FILE__, __LINE__, src_w, src_h, dst_w, dst_h);
// RR GG RR GG HH RR
// GG BB GG BB HH GG
// RR GG RR GG HH RR
// GG BB GG BB HH GG
for (ys = 0, yd = 0, psrc = src, pdst = dst; ys < dst_h; ys++, yd++) {
r = 0;
g = 0;
b = 0;
for (xs = 0, xd = 0; xd < dst_w; xs++, xd++) {
r = 0; b = 0; g = 0;
if (ys > 0 && ys < src_h-1 && xs > 0 && xs < src_w-1) {
if (ys&1) {
if (xs&1) {
b = *psrc;
g = (PRGGB10P_L + PRGGB10P_R + PRGGB10P_U + PRGGB10P_D)/4;
r = (PRGGB10P_UL + PRGGB10P_DL + PRGGB10P_UR + PRGGB10P_UL)/4;
}
else {
b = (PRGGB10P_L + PRGGB10P_R)/2;
g = *psrc;
r = (PRGGB10P_U + PRGGB10P_D)/2;
}
}
else {
if (xs&1) {
b = (PRGGB10P_U + PRGGB10P_D)/2;
g = *psrc;
r = (PRGGB10P_L + PRGGB10P_R)/2;
}
else {
g = (PRGGB10P_L + PRGGB10P_R + PRGGB10P_U + PRGGB10P_D)/4;
b = (PRGGB10P_UL + PRGGB10P_DL + PRGGB10P_UR + PRGGB10P_UL)/4;
r = *psrc;
}
}
}
if (pdst - dst > max) {
printf ("debayer error. dpst out of bounds size:%dx%d pos:%dx%d \n", dst_w, dst_h, xd, yd);
}
if ((ys == 10 || ys == 11) && xs < 10) printf ("%dx%d - %d, %d, %d\n", xs, ys, r, g, b);
pdst[0] = MAX(r, 0xff);
pdst[1] = MAX(g, 0xff);
pdst[2] = MAX(b, 0xff);
if (yd < 100 && xd < 300) {
pdst[0] = 0; if (xd < 100) pdst[0] = 255;
pdst[1] = 0; if (xd > 100 && xd < 200) pdst[1] = 255;
pdst[2] = 0; if (xd > 200 && xd < 300) pdst[2] = 255;
}
pdst += 3;
psrc++;
if (xs && (xs % 4 == 0)) psrc++;
}
}
};
std::string toBits(unsigned int i, int maxbits) {
int b;
std::string out = "";
for (b = 0; b < maxbits; b++) {
if (b % 8 == 0 && b) out = ":" + out;
if (i % 2) out = "1" + out;
else out = "0" + out;
i = i >> 1;
}
return out;
}
inline int swap16(uint16_t i) {
uint16_t r;
*(((unsigned char *)&r)) = *(((unsigned char *)&i)+1);
*(((unsigned char *)&r)+1) = *(((unsigned char *)&i));
return r;
}
#define PRGGB10_U (unsigned int)(*(psrc-src_w))
#define PRGGB10_D (unsigned int)(*(psrc+src_w))
#define PRGGB10_L (unsigned int)(*(psrc-1))
#define PRGGB10_R (unsigned int)(*(psrc+1))
#define PRGGB10_UL (unsigned int)(*(psrc-1-src_w))
#define PRGGB10_UR (unsigned int)(*(psrc+1-src_w))
#define PRGGB10_DL (unsigned int)(*(psrc-1+src_w))
#define PRGGB10_DR (unsigned int)(*(psrc+1+src_w))
void debayer_gbrg10 (uint8_t * src, int src_w, int src_h, uint8_t * dst, int dst_w, int dst_h) {
// GG BB GG BB
// RR GG RR GG
// GG BB GG BB
// RR GG RR GG
int xs, ys, xd, yd;
uint32_t r, g, b, data;
int max = dst_w * dst_h * 3;
uint8_t *pdst;
uint16_t *psrc;
int max_r = 0;
int max_g = 0;
int max_b = 0;
for (ys = 0, yd = 0, psrc = (uint16_t*)src, pdst = dst; ys < dst_h; ys++, yd++) {
r = 0;
g = 0;
b = 0;
for (xs = 0, xd = 0; xd < dst_w; xs++, xd++) {
if (ys > 0 && ys < src_h-1 && xs > 0 && xs < src_w-1) {
r = 0; b = 0; g = 0;
if (ys&1) {
if (xs&1) {
b = (unsigned int)(PRGGB10_U + PRGGB10_D) >> 1;
g = *psrc;
r = (unsigned int)(PRGGB10_L + PRGGB10_R) >> 1;
}
else {
b = (unsigned int)(PRGGB10_UL + PRGGB10_DL + PRGGB10_UR + PRGGB10_UL) >> 2;
g = (unsigned int)(PRGGB10_L + PRGGB10_R + PRGGB10_U + PRGGB10_D) >> 2;
r = *psrc;
}
}
else {
if (xs&1) {
b = *psrc;
g = (unsigned int)(PRGGB10_L + PRGGB10_R + PRGGB10_U + PRGGB10_D) >> 2;
r = (unsigned int)(PRGGB10_UL + PRGGB10_DL + PRGGB10_UR + PRGGB10_UL) >> 2;
}
else {
b = (unsigned int)(PRGGB10_L + PRGGB10_R) >> 1;
g = *psrc;
r = (unsigned int)(PRGGB10_U + PRGGB10_D) >> 1;
}
}
}
pdst[0] = (r >> 2);
pdst[1] = (g >> 2);
pdst[2] = (b >> 2);
// if (yd < 100 && xd < 300) {
// pdst[0] = 0; if (xd < 100) pdst[0] = 255;
// pdst[1] = 0; if (xd > 100 && xd < 200) pdst[1] = 255;
// pdst[2] = 0; if (xd > 200 && xd < 300) pdst[2] = 255;
// }
pdst += 3;
psrc += 1;
}
}
};
void debayer_rggb10 (uint8_t *src, int src_w, int src_h, uint8_t * dst, int dst_w, int dst_h) {
// RR GG RR GG
// GG BB GG BB
// RR GG RR GG
// GG BB GG BB
int xs, ys, xd, yd;
uint32_t r, g, b, data;
int max = dst_w * dst_h * 3;
uint8_t *pdst;
uint16_t *psrc;
int max_r = 0;
int max_g = 0;
int max_b = 0;
for (ys = 0, yd = 0, psrc = (uint16_t*)src, pdst = dst; ys < dst_h; ys++, yd++) {
r = 0;
g = 0;
b = 0;
for (xs = 0, xd = 0; xd < dst_w; xs++, xd++) {
r = 0; b = 0; g = 0;
if (ys > 0 && ys < src_h-1 && xs > 0 && xs < src_w-1) {
if (ys&1) {
if (xs&1) {
b = *psrc;
g = (unsigned int)(PRGGB10_L + PRGGB10_R + PRGGB10_U + PRGGB10_D) >> 2;
r = (unsigned int)(PRGGB10_UL + PRGGB10_DL + PRGGB10_UR + PRGGB10_UL) >> 2;
}
else {
b = (unsigned int)(PRGGB10_L + PRGGB10_R) >> 1;
g = *psrc;
r = (unsigned int)(PRGGB10_U + PRGGB10_D) >> 1;
}
}
else {
if (xs&1) {
b = (unsigned int)(PRGGB10_U + PRGGB10_D) >> 1;
g = *psrc;
r = (unsigned int)(PRGGB10_L + PRGGB10_R) >> 1;
}
else {
g = (unsigned int)(PRGGB10_L + PRGGB10_R + PRGGB10_U + PRGGB10_D) >> 2;
b = (unsigned int)(PRGGB10_UL + PRGGB10_DL + PRGGB10_UR + PRGGB10_UL) >> 2;
r = *psrc;
}
}
}
pdst[0] = (r >> 2);
pdst[1] = (g >> 2);
pdst[2] = (b >> 2);
// if (yd < 100 && xd < 300) {
// pdst[0] = 0; if (xd < 100) pdst[0] = 255;
// pdst[1] = 0; if (xd > 100 && xd < 200) pdst[1] = 255;
// pdst[2] = 0; if (xd > 200 && xd < 300) pdst[2] = 255;
// }
pdst += 3;
psrc += 1;
}
}
};