设置nv12的矩形边框

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
  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值