www.pudn.com > Ray_Tracing_Materials.rar > TgaImage.cpp, change:2005-03-02,size:1610b
/*
Class Name:
TgaImage.
Created by:
Allen Sherrod (Programming Ace of www.UltimateGameProgramming.com).
Description:
This function can save a tga image.
*/
#include"TgaImage.h"
bool WriteTGA(char *file, int width, int height, unsigned char *outImage)
{
FILE *pFile = 0;
unsigned char tgaHeader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned char header[6];
unsigned char bits = 0;
int colorMode = 0;
unsigned char tempColors = 0;
// Open file for output.
pFile = fopen(file, "wb");
// Check if the file opened or not.
if(!pFile) { fclose(pFile); return false; }
// Set the color mode, and the bit depth.
colorMode = 3;
bits = 24;
header[0] = width % 256; header[1] = width / 256; header[2] = height % 256;
header[3] = height / 256; header[4] = bits; header[5] = 0;
char uselessChar = 0;
fwrite(tgaHeader, sizeof(tgaHeader), 1, pFile);
fwrite(header, sizeof(header), 1, pFile);
// Now switch image from RGB to BGR.
for(int index = 0; index < width * height * colorMode; index += colorMode)
{
tempColors = outImage[index];
outImage[index] = outImage[index + 2];
outImage[index + 2] = tempColors;
}
// Finally write the image.
fwrite(outImage, width * height * colorMode, 1, pFile);
// close the file.
fclose(pFile);
return true;
}
// Copyright February 2005
// All Rights Reserved!
// Allen Sherrod
// ProgrammingAce@UltimateGameProgramming.com
// www.UltimateGameProgramming.com