Bayer转换算法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Bayer转换算法[转]
/* raw.h */
#ifndef __RAW_H__
#define __RAW_H__
#define GP_OK 1
typedef enum {
BAYER_TILE_RGGB = 0,
BAYER_TILE_GRBG = 1,
BAYER_TILE_BGGR = 2,
BAYER_TILE_GBRG = 3,
BAYER_TILE_RGGB_INTERLACED = 4,
BAYER_TILE_GRBG_INTERLACED = 5,
BAYER_TILE_BGGR_INTERLACED = 6,
BAYER_TILE_GBRG_INTERLACED = 7,
} BayerTile;
int gp_bayer_expand (unsigned char *input, int w, int h, unsigned char *output, BayerTile tile);
int gp_bayer_decode (unsigned char *input, int w, int h, unsigned char *output, BayerTile tile);
int gp_bayer_interpolate (unsigned char *image, int w, int h, BayerTile tile); #endif
/* raw.c */
#include "raw.h"
static const int tile_colours[8][4] = {
{0, 1, 1, 2},
{1, 0, 2, 1},
{2, 1, 1, 0},
{1, 2, 0, 1},
{0, 1, 1, 2},
{1, 0, 2, 1},
{2, 1, 1, 0},
{1, 2, 0, 1}};
#define RED 0
#define GREEN 1
#define BLUE 2
static int
gp_bayer_accrue (unsigned char *image, int w, int h, int x0, int y0,
int x1, int y1, int x2, int y2, int x3, int y3, int colour);
int
gp_bayer_expand (unsigned char *input, int w, int h, unsigned char *output, BayerTile tile)
{
int x, y, i;
int colour, bayer;
unsigned char *ptr = input;
switch (tile) {
case BAYER_TILE_RGGB:
case BAYER_TILE_GRBG:
case BAYER_TILE_BGGR:
case BAYER_TILE_GBRG:
for (y = 0; y < h; ++y)
for (x = 0; x < w; ++x, ++ptr)
{
bayer = (x&1?0:1) + (y&1?0:2);
colour = tile_colours[tile][bayer];
i = (y * w + x) * 3;
output[i+RED] = 0;
output[i+GREEN] = 0;
output[i+BLUE] = 0;
output[i+colour] = *ptr;
}
break;
case BAYER_TILE_RGGB_INTERLACED:
case BAYER_TILE_GRBG_INTERLACED:
case BAYER_TILE_BGGR_INTERLACED:
case BAYER_TILE_GBRG_INTERLACED:
for (y = 0; y < h; ++y, ptr+=w)
for (x = 0; x < w; ++x)
{
bayer = (x&1?0:1) + (y&1?0:2);
colour = tile_colours[tile][bayer];
i = (y * w + x) * 3;
output[i+RED] = 0;
output[i+GREEN] = 0;
output[i+BLUE] = 0;
output[i+colour] = (x&1)? ptr[x>>1]:ptr[(w>>1)+(x>>1)];
}
break;
}
return (GP_OK);
}
#define AD(x, y, w) ((y)*(w)*3+3*(x))
int
gp_bayer_interpolate (unsigned char *image, int w, int h, BayerTile tile) {
int x, y, bayer;
int p0, p1, p2, p3;
int value, div ;
switch (tile) {
default:
case BAYER_TILE_RGGB:
case BAYER_TILE_RGGB_INTERLACED:
p0 = 0; p1 = 1; p2 = 2; p3 = 3;
break;
case BAYER_TILE_GRBG:
case BAYER_TILE_GRBG_INTERLACED:
p0 = 1; p1 = 0; p2 = 3; p3 = 2;
break;
case BAYER_TILE_BGGR:
case BAYER_TILE_BGGR_INTERLACED:
p0 = 3; p1 = 2; p2 = 1; p3 = 0;
break;
case BAYER_TILE_GBRG:
case BAYER_TILE_GBRG_INTERLACED:
p0 = 2; p1 = 3; p2 = 0; p3 = 1;
break;
}