0. Visual Studio 2019可编译通过的代码如下:
RGB24格式的每个像素的三个分量是连续存储的。一帧宽高分别为w、h的RGB24图像一共占用wh3 Byte的存储空间。
RGB24格式规定首先存储第一个像素的R、G、B,然后存储第二个像素的R、G、B…以此类推,所以
后面的代码就一个简单的for循环遍历,依次把数据取出来。
上述调用函数的代码运行后,将会把一张分辨率为500x500的名称为cie1931_500x500.rgb的RGB24格式的像素数据文件分离成为三个文件:
output_r.y:R数据,分辨率为500x500。
output_g.y:G数据,分辨率为500x500。
output_b.y:B数据,分辨率为500x500。
extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#endif
}
extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
}
/**
* Calculate PSNR between 2 YUV420P file
* @param url1 Location of first Input YUV file.
* @param url2 Location of another Input YUV file.
* @param w Width of Input YUV file.
* @param h Height of Input YUV file.
* @param num Number of frames to process.
*/
/**
* Split R, G, B planes in RGB24 file.
* @param url Location of Input RGB file.
* @param w Width of Input RGB file.
* @param h Height of Input RGB file.
* @param num Number of frames to process.
*
*/
int simplest_rgb24_split(const char* url, int w, int h, int num) {
FILE* fp = fopen(url, "rb+");
FILE* fp1 = fopen("output_r.y", "wb+");
FILE* fp2 = fopen("output_g.y", "wb+");
FILE* fp3 = fopen("output_b.y", "wb+");
unsigned char* pic = (unsigned char*)malloc(w * h * 3);
for (int i = 0; i < num; i++) {
fread(pic, 1, w * h * 3, fp);
for (int j = 0; j < w * h * 3; j = j + 3) {
//R
fwrite(pic + j, 1, 1, fp1); //就RGB数据是依次存的,这样就依次取嘛..
//G
fwrite(pic + j + 1, 1, 1, fp2);
//B
fwrite(pic + j + 2, 1, 1, fp3);
}
}
free(pic);
fclose(fp);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
int main()
{
simplest_rgb24_split("cie1931_500x500.rgb", 500, 500, 1);
return 0;
}
1. 运行结果如下:
原图:
R分量:
分辨率Y、U、V都设置为500*500,像素格式选择Y分量。
G分量:
B分量:
参考链接:
- https://blog.csdn.net/leixiaohua1020/article/details/50534150