00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "tgaio.h"
00025 #include "error.h"
00026 #include "color.h"
00027
00028 using namespace lux;
00029
00030 namespace lux
00031 {
00032
00033 void WriteTargaImage(int channeltype, bool savezbuf, const string &name, vector<RGBColor> &pixels,
00034 vector<float> &alpha, int xPixelCount, int yPixelCount,
00035 int xResolution, int yResolution,
00036 int xPixelStart, int yPixelStart) {
00037
00038 FILE* tgaFile = fopen(name.c_str(),"wb");
00039 if (!tgaFile) {
00040 std::stringstream ss;
00041 ss<< "Cannot open file '"<<name<<"' for output";
00042 luxError(LUX_SYSTEM, LUX_SEVERE, ss.str().c_str());
00043 return;
00044 }
00045
00046
00047
00048 char header[18];
00049 memset(header, 0, sizeof(char) * 18);
00050
00051 if(channeltype > 0)
00052 header[2] = 2;
00053 else
00054 header[2] = 3;
00055
00056 short xResShort = xPixelCount;
00057 header[13] = (char) (xResShort >> 8);
00058 header[12] = xResShort & 0xFF;
00059 short yResShort = yPixelCount;
00060 header[15] = (char) (yResShort >> 8);
00061 header[14] = yResShort & 0xFF;
00062 if(channeltype == 0)
00063 header[16] = 8;
00064 else if(channeltype == 1)
00065 header[16] = 24;
00066 else
00067 header[16] = 32;
00068
00069
00070 for (int i = 0; i < 18; ++i)
00071 fputc(header[i], tgaFile);
00072
00073
00074 for (int i = yPixelCount - 1; i >= 0 ; --i) {
00075 for (int j = 0; j < xPixelCount; ++j) {
00076 if(channeltype == 0) {
00077
00078 float c = (0.3f * pixels[i * xPixelCount + j].c[0]) +
00079 (0.59f * pixels[i * xPixelCount + j].c[1]) +
00080 (0.11f * pixels[i * xPixelCount + j].c[2]);
00081 fputc(static_cast<unsigned char>(Clamp(256 * c, 0.f, 255.f)), tgaFile);
00082 } else {
00083
00084 fputc(static_cast<unsigned char>(Clamp(256 * pixels[i * xPixelCount + j].c[2], 0.f, 255.f)), tgaFile);
00085 fputc(static_cast<unsigned char>(Clamp(256 * pixels[i * xPixelCount + j].c[1], 0.f, 255.f)), tgaFile);
00086 fputc(static_cast<unsigned char>(Clamp(256 * pixels[i * xPixelCount + j].c[0], 0.f, 255.f)), tgaFile);
00087 if(channeltype == 2)
00088 fputc(static_cast<unsigned char>(Clamp(256 * alpha[(i * xPixelCount + j)], 0.f, 255.f)), tgaFile);
00089 }
00090 }
00091 }
00092
00093 fclose(tgaFile);
00094 }
00095
00096 }
00097
00098