转自:https://blog.****.net/LG1259156776/article/details/52006457?locationNum=10&fps=1
RGB组合格式
名字
RGB组合格式
描述
此格式用来匹配PC图形帧缓存。每个像素占据8,16,24或32个位,他们都是组合像素格式,其意为在内存中所有像素数据都是相邻排列的。当使用这些格式之一时,驱动应该上报颜色空间为V4L2_COLORSPACE_SRGB。
表2.6 组合RGB图像格式
定义 | 码 | Byte 0 | Byte 1 | Byte 2 | Byte 3 |
---|---|---|---|---|---|
V4L2_PIX_FMT_RGB332 | RGB1 | r2 r1 r0 g2 g1 g0 b1 b0 | - | - | - |
V4L2_PIX_FMT_RGB444 | R444 | g3 g2 g1 g0 b3 b2 b1 b0 | a3 a2 a1 a0 r3 r2 r1 r0 | - | - |
V4L2_PIX_FMT_RGB555 | RGBO | g2 g1 g0 b4 b3 b2 b1 b0 | a r4 r3 r2 r1 r0 g4 g3 | - | - |
V4L2_PIX_FMT_RGB565 | RGBP | g2 g1 g0 b4 b3 b2 b1 b0 | r4 r3 r2 r1 r0 g5 g4 g3 | - | - |
V4L2_PIX_FMT_RGB555X | RGBQ | a r4 r3 r2 r1 r0 g4 g3 | g2 g1 g0 b4 b3 b2 b1 b0 | - | - |
V4L2_PIX_FMT_RGB565X | RGBR | r4 r3 r2 r1 r0 g5 g4 g3 | g2 g1 g0 b4 b3 b2 b1 b0 | - | - |
V4L2_PIX_FMT_BGR666 | BGRH | b5 b4 b3 b2 b1 b0 g5 g4 | g3 g2 g1 g0 r5 r4 r3 r2 | r1 r0 | - |
V4L2_PIX_FMT_BGR24 | BGR3 | b7 b6 b5 b4 b3 b2 b1 b0 | g7 g6 g5 g4 g3 g2 g1 g0 | r7 r6 r5 r4 r3 r2 r1 r0 | - |
V4L2_PIX_FMT_RGB24 | RGB3 | r7 r6 r5 r4 r3 r2 r1 r0 | g7 g6 g5 g4 g3 g2 g1 g0 | b7 b6 b5 b4 b3 b2 b1 b0 | - |
V4L2_PIX_FMT_BGR32 | RGR4 | b7 b6 b5 b4 b3 b2 b1 b0 | g7 g6 g5 g4 g3 g2 g1 g0 | r7 r6 r5 r4 r3 r2 r1 r0 | a7 a6 a5 a4 a3 a2 a1 a0 |
V4L2_PIX_FMT_RGB32 | RGB4 | a7 a6 a5 a4 a3 a2 a1 a0 | r7 r6 r5 r4 r3 r2 r1 r0 | g7 g6 g5 g4 g3 g2 g1 g0 | b7 b6 b5 b4 b3 b2 b1 b0 |
第7位是符号位,a(alpha)的值在读取驱动时并未定义,所以编写驱动时忽略它吧,除非是在Overlay或输出Overlay时候协商了透明度,又或是在使用V4L2_CID_ALPHA_COMPONENT控制视频捕捉时候设置透明度部分。
例2.2 V4L2_PIX_FMT_BGR24 4 x 4像素图像
-
start + 00: B00 G00 R00 B01 G01 R01 B02 G02 R02 B03 G03 R03
-
start + 12: B10 G10 R10 B11 G11 R11 B12 G12 R12 B13 G13 R13
-
start + 24: B20 G20 R20 B21 G21 R21 B22 G22 R22 B23 G23 R23
-
start + 36: B30 G30 R30 B31 G31 R31 B32 G32 R32 B33 G33 R33
V4L2_PIX_FMT_SBGGR8('BA81')
名字
V4L2_PIX_FMT_SBGGR8 贝尔RGB格式
描述
这一般是数字相机的本地格式,与CCD设备的传感器阵列对应。每个像素只有一个值或是红色、绿色、蓝色,丢失的部分必须由相邻像素插值而来。第一行从左到右由蓝色和绿色组成,第二行是绿色和红色,每两行/列以此类推。
例2.3 V4L2_PIX_FMT_SBGGR8 4 x 4像素图像
-
start + 0: B00 G01 B02 G03
-
start + 4: G10 R11 G12 R13
-
start + 8: B20 G21 B22 G23
-
start +12: G30 R31 G32 R33
V4L2_PIX_FMT_SGBRG8 ('GBRG')
名字
V4L2_PIX_FMT_SGBRG8 贝尔RGB格式
描述
贝尔格式描述基本相同,只是像素排列规则不同。此格式是第一行从做到有包含绿色和蓝色,第二行是红色和绿色,每两行/列依此类推。
例2.4 V4L2_PIX_FMT_SGBRG8 4 x 4像素图像
-
start + 0: G00 B01 G02 B03
-
start + 4: R10 G11 R12 G13
-
start + 8: G20 B21 G22 B23
-
start +12: R30 G31 R32 G33
V4L2_PIX_FMT_SGRBG8 ('GRBG')
名字
V4L2_PIX_FMT_SGRBG8 贝尔RGB格式
描述
第一行从左到右包含绿色和蓝色,第二行包含红色和绿色,每两行/列依此类推。
例2.5 V4L2_PIX_FMT_SGRBG8 4 x 4像素图像
-
start + 0: G00 R01 G02 R03
-
start + 4: R10 B11 R12 B13
-
start + 8: G20 R21 G22 R23
-
start +12: R30 B31 R32 B33
V4L2_PIX_FMT_SRGGB8 ('RGGB')
名字
V4L2_PIX_FMT_SRGGB8 贝尔RGB格式
描述
第一行从左到右包含红色和绿色,第二行包含绿色和蓝色,每两行/列依此类推。
例2.6 V4L2_PIX_FMT_SRGGB8 4 x 4像素图像
-
start + 0: R00 G01 R02 G03
-
start + 4: G10 B11 G12 B13
-
start + 8: R20 G21 R22 G23
-
start +12: G30 B31 G32 B33
V4L2_PIX_FMT_SBGGR16 ('BYR2')
名字
V4L2_PIX_FMT_SBGGR16 贝尔RGB格式
描述
此格式与V4L2_PIX_FMT_SBGGR8很相似,不同是每个像素拥有16位深度。低数据存储在小地址内存中(低位机)。实际采样精度可能会比16位低,必须每个像素10位,值范围0~1023(如MT9T001)。
例2.7 V4L2_PIX_FMT_SBGGR16 4 x 4像素图像
-
start + 0: B00low B00high G01low G01high B02low B02high G03low G03high
-
start + 8: G10low G10high R11low R11high G12low G12high R13low R13high
-
start +16: B20low B20high G21low G21high B22low B22high G23low G23high
-
start +24: G30low G30high R31low R31high G32low G32high R33low R33high
V4L2_PIX_FMT_SRGGB10 ('RG10'), V4L2_PIX_FMT_SGRBG10 ('BA10'), V4L2_PIX_FMT_SGBRG10 ('GB10'), V4L2_PIX_FMT_SBGGR10 ('BG10')
名字
V4L2_PIX_FMT_SRGGB10 ('RG10'), V4L2_PIX_FMT_SGRBG10 ('BA10'), V4L2_PIX_FMT_SGBRG10 ('GB10'), V4L2_PIX_FMT_SBGGR10 ('BG10') 10bit贝尔格式扩展的16位格式
描述
这4个像素格式是每个颜色10位的raw sRGB/贝尔格式。每个颜色部分都存储在16位字段中,高6位是不用的,填充0。每n像素行包含n/2个绿色取样和n/2个蓝色取样或红色取样,红色和蓝色行交替。字节以低位机序列存储。他们通常是来描述如GRGR...BGBG...或RGRG...GBGB...等等。以下是其中的一个例子。
例2.8 V4L2_PIX_FMT_SBGGR10 4 x 4像素图像
-
每个代表一个字节,高字节的高6位为0
-
start + 0: B00low B00high G01low G01high B02low B02high G03low G03high
-
start + 8: G10low G10high R11low R11high G12low G12high R13low R13high
-
start +16: B20low B20high G21low G21high B22low B22high G23low G23high
-
start +24: G30low G30high R31low R31high G32low G32high R33low R33high
V4L2_PIX_FMT_SBGGR10ALAW8 ('aBA8'), V4L2_PIX_FMT_SGBRG10ALAW8 ('aGA8'), V4L2_PIX_FMT_SGRBG10ALAW8 ('agA8'), V4L2_PIX_FMT_SRGGB10ALAW8 ('aRA8')
名字
V4L2_PIX_FMT_SBGGR10ALAW8 , V4L2_PIX_FMT_SGBRG10ALAW8 , V4L2_PIX_FMT_SGRBG10ALAW8 , V4L2_PIX_FMT_SRGGB10ALAW8 10位贝尔格式压缩至8位。
描述
这些格式是每个颜色10位的raw sRGB/贝尔格式压缩到8位,使用A-LAW算法。每个颜色部分在内存中占据8位,其他与V4L2_PIX_FMT_SRGGB8特性类似。
V4L2_PIX_FMT_SBGGR10DPCM8 ('bBA8'), V4L2_PIX_FMT_SGBRG10DPCM8 ('bGA8'), V4L2_PIX_FMT_SGRBG10DPCM8 ('BD10'), V4L2_PIX_FMT_SRGGB10DPCM8 ('bRA8')
名字
V4L2_PIX_FMT_SBGGR10DPCM8, V4L2_PIX_FMT_SGBRG10DPCM8, V4L2_PIX_FMT_SGRBG10DPCM8, V4L2_PIX_FMT_SRGGB10DPCM8 10位贝尔格式压缩至8位。
描述
使用DPCM将每个颜色部分压缩至8位。DPCM调整是有损耗的,每个颜色在内存中占用8位,其他部分与V4L2_PIX_FMT_SRGGB10 ('RG10'), V4L2_PIX_FMT_SGRBG10 ('BA10'), V4L2_PIX_FMT_SGBRG10 ('GB10'), V4L2_PIX_FMT_SBGGR10 ('BG10')类似。
V4L2_PIX_FMT_SRGGB12 ('RG12'), V4L2_PIX_FMT_SGRBG12 ('BA12'), V4L2_PIX_FMT_SGBRG12 ('GB12'), V4L2_PIX_FMT_SBGGR12 ('BG12')
名字
V4L2_PIX_FMT_SRGGB12, V4L2_PIX_FMT_SGRBG12, V4L2_PIX_FMT_SGBRG12, V4L2_PIX_FMT_SBGGR12 12位贝尔格式扩展为16位
描述
每个颜色12位的raw sRGB/贝尔格式,每个颜色部分被保存在16位的字段中,高6位为0。每个n像素行包含n/2个绿色取样及n/2个蓝色或红色取样,红、蓝行交替。字节按低位机顺序存储在内存中,通常用来描述如GEGE...BGBG...或RGRG...RBRB...等。下边是其中一个的例子。
例2.9 V4L2_PIX_FMT_SBGGR12 4 x 4像素图片
-
start + 0: B00low B00high G01low G01high B02low B02high G03low G03high
-
start + 8: G10low G10high R11low R11high G12low G12high R13low R13high
-
start +16: B20low B20high G21low G21high B22low B22high G23low G23high
-
start +24: G30low G30high R31low R31high G32low G32high R33low R33high
.........................................................................................................................
以下是根据上面的文档写的转换代码:RGGB/BGGR转RGB
#include <fcntl.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> static void raw10_to_raw8(char *in, char *out, int width, int height)
{
int i,j;
char temp;
//RAW10 --> RAW8
for(j = ; j < height; j++)
{
for(i = ; i < width * ; i = i + )
{
temp = ;
temp = in[j * width * + i]; //low
out[j * width + i / ] = temp; /*
temp = 0;
temp = in[j * width * 2 + i] >> 2; //low
temp |= in[j * width * 2 + i + 1] << 6; //high
out[j * width + i / 2] = temp;
*/
}
} //printf("aa0 %x %x %x %x \n", out[0], out[1], out[2], out[3]);
//printf("aa1 %x %x %x %x \n", out[width], out[width+1], out[width+2], out[width+3]);
} #if 0
static int rggb2rgb(char *in, char *out, int width, int height)
{
/*
R G1 ---> RG1B RG1B
G2 B ---> RG2B RG2B
*/
int i,j; //RGRGRG
for(j = ; j < height; j= j + )
{
for(i = ; i < width * ; i = i + )
{
//(0,0)
out[(j * width * ) + (i + )] = in[(j * width) + (i / )]; //R
out[(j * width * ) + (i + )] = in[(j * width) + (i / ) + ]; //G1
out[(j * width * ) + (i + )] = in[((j+) * width) + (i / ) + ];//B //(0,1) == (0,0)
out[(j * width * ) + (i + )] = in[(j * width) + (i / )]; //R
out[(j * width * ) + (i + )] = in[(j * width) + (i / ) + ]; //G1
out[(j * width * ) + (i + )] = in[((j+) * width) + (i / ) + ];//B
}
} //GBGBGB
for(j = ; j < height; j= j + )
{
for(i = ; i < width * ; i = i + )
{
//(1,0)
out[(j * width * ) + (i + )] = in[((j-) * width) + (i / )]; //R
out[(j * width * ) + (i + )] = in[(j * width) + (i / )]; //G2
out[(j * width * ) + (i + )] = in[(j * width) + (i / ) + ]; //B //(1,0) == (1,1)
out[(j * width * ) + (i + )] = in[((j-) * width) + (i / )]; //R
out[(j * width * ) + (i + )] = in[(j * width) + (i / )]; //G2
out[(j * width * ) + (i + )] = in[(j * width) + (i / ) + ]; //B
}
}
//printf("000 %x %x %x %x \n", out[0], out[1], out[2], out[3]);
//printf("111 %x %x %x %x \n", out[width], out[width+1], out[width+2], out[width+3]); return width * height * ;
}
#endif #if 1
static int bggr2rgb(char *in, char *out, int width, int height)
{
/*
B G1 ---> RG1B RG1B
G2 R ---> RG2B RG2B
*/
int i,j; //BGBGBG
for(j = ; j < height; j= j + )
{
for(i = ; i < width * ; i = i + )
{
//(0,0)
out[(j * width * ) + (i + )] = in[((j+) * width) + (i / ) + ];//R
out[(j * width * ) + (i + )] = in[(j * width) + (i / ) + ]; //G1
out[(j * width * ) + (i + )] = in[(j * width) + (i / )]; //B //(0,1) == (0,0)
out[(j * width * ) + (i + )] = in[((j+) * width) + (i / ) + ];//R
out[(j * width * ) + (i + )] = in[(j * width) + (i / ) + ]; //G1
out[(j * width * ) + (i + )] = in[(j * width) + (i / )]; //B
}
} //GRGRGR
for(j = ; j < height; j= j + )
{
for(i = ; i < width * ; i = i + )
{
//(1,0)
out[(j * width * ) + (i + )] = in[(j * width) + (i / ) + ]; //R
out[(j * width * ) + (i + )] = in[(j * width) + (i / )]; //G2
out[(j * width * ) + (i + )] = in[((j-) * width) + (i / )]; //B //(1,0) == (1,1)
out[(j * width * ) + (i + )] = in[(j * width) + (i / ) + ]; //R
out[(j * width * ) + (i + )] = in[(j * width) + (i / )]; //G2
out[(j * width * ) + (i + )] = in[((j-) * width) + (i / )]; //B
}
}
//printf("000 %x %x %x %x \n", out[0], out[1], out[2], out[3]);
//printf("111 %x %x %x %x \n", out[width], out[width+1], out[width+2], out[width+3]); return width * height * ;
}
#endif static void usage( char * name )
{
printf("usage: %s\n", name);
printf(" --input,-i input file\n");
printf(" --output,-o output file\n");
printf(" --width,-w image width (pixels)\n");
printf(" --height,-v image height (pixels)\n");
printf(" --help,-h this helpful message\n");
} int main( int argc, char ** argv )
{
int width = ;
int height = ;
int size_in = ;
int size_out = ;
char c;
int optidx = ; char *infile = NULL;
char *outfile = NULL;
FILE *input_fd = NULL;
FILE *output_fd = NULL; char *buff_tmp = NULL;
char *buff_in = NULL;
char *buff_out = NULL; int param_num = ;
while (param_num--)
{
optidx = ;
struct option longopt[] = {
{"input",,NULL,'i'},
{"output",,NULL,'o'},
{"width",,NULL,'w'},
{"height",,NULL,'v'},
{"help",,NULL,'h'},
{,,,}
}; c = getopt_long(argc, argv, "i:o:w:v:h", longopt, &optidx);
if (c == -)
break; switch ( c )
{
case 'i':
infile = strdup( optarg );
break;
case 'o':
outfile = strdup( optarg );
break;
case 'w':
width = strtol( optarg, NULL, );
break;
case 'v':
height = strtol( optarg, NULL, );
break;
case 'h':
usage(argv[]);
return ;
break;
default:
printf("lsc bad arg %c\n", c);
usage(argv[]);
//return 1;
}
}
// arguments: infile outfile width height
if( infile == NULL || outfile == NULL || width == || height == )
{
printf("Bad parameter\n");
usage(argv[]);
return ;
} buff_tmp = calloc(width * height, sizeof(char));
buff_in = calloc(width * height * , sizeof(char));
buff_out = calloc(width * height * , sizeof(char));
if(NULL == buff_tmp || NULL == buff_in || NULL == buff_out)
{
printf("malloc error\n");
goto end;
}
input_fd = fopen(infile, "r");
if(NULL == input_fd)
{
printf("Problem opening input: %s\n", infile);
return ;
} output_fd = fopen(outfile, "w");
if(NULL == output_fd)
{
printf("Problem opening output: %s\n", outfile);
return ;
} //读文件
size_in = fread(buff_in, , width * height * , input_fd);
if(size_in != width * height * )
{
printf("error!! size_in != width * height 2, size_in= %d\n", size_in);
goto end;
} raw10_to_raw8(buff_in, buff_tmp, width, height); size_out = bggr2rgb(buff_tmp, buff_out, width, height); //size_out = rggb2rgb(buff_tmp, buff_out, width, height); //写入数据到文件
fwrite(buff_out, , size_out, output_fd); end:
if(input_fd)
fclose(input_fd);
if(output_fd)
fclose(output_fd); if(NULL == buff_tmp)
{
free(buff_tmp);
}
if(NULL == buff_in)
{
free(buff_in);
}
if(NULL == buff_out)
{
free(buff_out);
} return ;
}