UV是色差分量,UV为0就会全是绿色,全为0x80的时候才能看到灰度图。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int show_hex(char* begin, int len)
{
int i = 0;
for (i = 0; i < len; i++)
{
printf("%02x ", begin[i]);
}
printf("\n");
return 0;
}
int write_file(const char* path, char* buffer, int len)
{
FILE* fp = fopen(path, "w+");
assert(fp);
int ret = fwrite(buffer, 1, len, fp);
assert(ret == len);
fclose(fp);
return 0;
}
int cover(char* dst, int d_width, int d_height, int x, int y, char* src, int s_width, int s_height)
{
int i = 0;
int j = 0;
for (i = 0; i < s_height; i++)
{
int idx = x*d_width + y;
for (j = 0; j < s_width; j++)
{
dst[idx++] = src[i*s_width+j];
}
x++;
}
return 0;
}
int main()
{
int width = 1280;
int height = 720;
int size = width*height*1.5;
char* buffer = (char*)malloc(size);
assert(buffer);
memset(buffer, 0x80, size);// uv 需设置为0x80
memset(buffer, 0xff, width*height); // 设置Y 为0xff(白色)
char tmp[16*16];
memset(tmp, 0, 16*16); // 设置cover 为0(黑色)
cover(buffer, width, height, 0, 0, tmp, 16, 16);
write_file("test.yuv", buffer, size);
free(buffer);
return 0;
}