TGA扩展文件结构由五部分组成:文件头、图像/颜色表数据、开发者自定义区域、扩展区域和文件尾。
由头文件中的二三字节00 02可以看出是未压缩的真彩色图像,00 00 00 00 18说明一个像素是24位,所以与RGB的格式非常相似,借鉴了之前RGB转YUV的方法。
代码如下:
#include<stdio.h>
#include<stdlib.h>
const int width = 256;
const int height = 256;
int main(int argc,char *argv[])
{
unsigned char* buffer_tga = new unsigned char[3 * width * height + 18];
unsigned char* buffer_rgb = new unsigned char[ 3 * width * height];
unsigned char* buffer_y = new unsigned char[width * height];
unsigned char* buffer_u = new unsigned char[width * height / 4];
unsigned char* buffer_v = new unsigned char[width * height / 4];
FILE* rgb=NULL;
FILE* yuv=NULL;
fopen_s(&rgb,argv[1], "rb");
if (rgb == NULL)
printf("cannot find rgb file\n");
fread(buffer_tga, sizeof(unsigned char), 3 * width * height+18,rgb);
for (int i = 0; i < 3 * width * height; i++)