1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <cstdio>
#include <fstream>
#include <windows.h>
#include "Header.h"
using namespace std;
int main(int argc, char** argv)
{
FILE* TGAFile, * yuvFile;
HEADER Hd;
int Width, Height, pxCount;
char* TGAFileName = NULL;
char* yuvFileName = NULL;
TGAFileName = argv[1];
yuvFileName = argv[2];
if (fopen_s(&TGAFile, TGAFileName, "rb") == 0)
{
cout << "File opened! " << TGAFileName << "." << endl;
}
else
{
cout << "File not opened! " << TGAFileName << "." << endl;
exit(0);
}
if (fopen_s(&yuvFile, yuvFileName, "wb+") == 0)
{
cout << "File opened! " << yuvFileName << "." << endl;
}
else
{
cout << "File not opened! " << yuvFileName << "." << endl;
exit(0);
}
ReadTGAHeader(&Hd, TGAFile);
Width = Hd.Height;
Height = Hd.Width;
pxCount = Width * Height;
unsigned char* RGB = (unsigned char*)malloc(sizeof(unsigned char) * (Width * Height * 3));
unsigned char* Y, * U, * V;
unsigned char* R, * G, * B;
R = (unsigned char*)malloc(sizeof(unsigned char) * (Width * Height));
G = (unsigned char*)malloc(sizeof(unsigned char) * (Width * Height));
B = (unsigned char*)malloc(sizeof(unsigned char) * (Width * Height));
Y = (unsigned char*)malloc(sizeof(char) * (Width * Height));
U = (unsigned char*)malloc(sizeof(char) * (Width * Height / 4));
V = (unsigned char*)malloc(sizeof(char) * (Width * Height / 4));
DATA* RGBaData = NULL;
RGBaData = new DATA[Hd.Width * Hd.Height];
memset(RGBaData, 0, Hd.Height * Hd.Width);
RGB = new unsigned char[Hd.Width * Hd.Height * 3];
memset(RGB, 0, Hd.Height * Hd.Width * 3);
int Ost = 0;
Ost += Hd.IdLength;
Ost += Hd.ColourMapType * Hd.ColourMapLength * Hd.ColourMapDepth;
fseek(TGAFile, Ost, SEEK_CUR);
ReadColourData(&Hd, RGBaData, TGAFile);
Width = Hd.Width;
Height = Hd.Height;
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
int RGBPxNum = (Height - 1 - i) * Width + j;
int TGAPxNum = i * Width + j;
RGB[3 * RGBPxNum + 2] = RGBaData[TGAPxNum].R;
RGB[3 * RGBPxNum + 1] = RGBaData[TGAPxNum].G;
RGB[3 * RGBPxNum] = RGBaData[TGAPxNum].B;
}
}
RGB2YUV(RGB, Y, U, V, Width, Height);
free(RGB);
fwrite(Y, sizeof(unsigned char), Width * Height, yuvFile);
fwrite(U, sizeof(unsigned char), Width * Height / 4, yuvFile);
fwrite(V, sizeof(unsigned char), Width * Height / 4, yuvFile);
fclose(yuvFile);
}
| |