NV12
一个6*4的nv12格式的图片的三个分量YUV的内存形式:
虽然nv12的像素大小是width*height,但是必须为nv12图片分配width*height*3/2的内存空间,因为Y分量所占的内存为width*height,而U,V分量各占width*height/4,见上图即一目了然。
nv12分量YUV分量内存操作:可以用双重for循环遍历nv12各个分量的内存分布,从而实现给nv12图片赋予不同的颜色。
由内存YUV分量内存分布图可以知道:
0 ~ width*height 为Y分量存储的内存范围。
width*height ~ width*height*3/2 为UV交叉存储的内存范围。
操作Y分量:buf[j * width + k]
操作U分量 : buf[j / 2 * width + k - k % 2 + width * height]
操作V分量 : buf[j / 2 * width + k - k % 2 + width * height + 1]
在nv12格式的图片上画一个锁定某个目标的矩形方框图
#include <stdio.h>
#include <stdlib.h>
int nv12_border(char *pic, int pic_w, int pic_h, int rect_x, int rect_y, int rect_w, int rect_h, int R, int G, int B);
int main()
{
/* Set up the nv12's pixel size */
const int pic_w = 1920;
const int pic_h = 1080;
/* Set up test data's path and output data's path */
char *inputPathname = "/home/flypei/work/nv12-marked-rect/data/videotestsrc_1920x1080.nv12";
char *outputPathname = "./outputFile.nv12";
FILE *fin = fopen(inputPathname , "rb+");
FILE *fout = fopen(outputPathname, "wb+");
/* Allocate memory for nv12 */
unsigned char *buf = (unsigned char *)malloc(pic_w * pic_h * 3 / 2);
/* Read file data to buffer */
fread(buf, 1, pic_w * pic_h * 3 / 2, fin);
/* Draw rectangle border to nv12 */
nv12_border(buf, pic_w, pic_h, 500, 500, 300, 400, 0, 0, 255);
/* Write data of buf to fout */
fwrite(buf, 1, pic_w * pic_h * 3 / 2, fout);
/* Free the allocation memory */
free(buf);
/* Close the file */
fclose(fin);
fclose(fout);
return 0;
}
int nv12_border(char *pic, int pic_w, int pic_h, int rect_x, int rect_y, int rect_w, int rect_h, int R, int G, int B)
{
/* Set up the rectangle border size */
co