00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "LampBasic.h"
00026 #include "Core/Codec/Tga/TargaSaver.h"
00027 #include "Core/InputOutput/BinaryFileWriter.h"
00028
00029 namespace Lamp{
00030
00031
00032 const char TargaSaver::footer_[] = "TRUEVISION-XFILE.";
00033
00034
00035
00036 TargaSaver::TargaSaver(){
00037 }
00038
00039
00040 TargaSaver::~TargaSaver(){
00041 }
00042
00043
00044 void TargaSaver::save(
00045 BinaryWriter* writer, const DimensionI& size, const Color3c* colors){
00046 writer_ = writer;
00047 size_ = size;
00048
00049 writeHeader(false);
00050
00051 int bufferSize = size_.width * size_.height * sizeof(Color3c);
00052 u_char* buffer = new u_char[bufferSize];
00053 u_char* writeAddress = buffer;
00054 for(int i = size_.height - 1; i >= 0; i--){
00055 int offset = i * size_.width;
00056 for(int j = 0; j < size_.width; j++){
00057 const Color3c& color = colors[offset + j];
00058 *writeAddress = color.b;
00059 writeAddress++;
00060 *writeAddress = color.g;
00061 writeAddress++;
00062 *writeAddress = color.r;
00063 writeAddress++;
00064 }
00065 }
00066 writer_->writeBytes(buffer, bufferSize);
00067 delete[] buffer;
00068
00069 writeFooter();
00070 }
00071
00072
00073 void TargaSaver::save(
00074 BinaryWriter* writer, const DimensionI& size, const Color4c* colors){
00075 writer_ = writer;
00076 size_ = size;
00077
00078 writeHeader(true);
00079
00080 int bufferSize = size_.width * size_.height * sizeof(Color4c);
00081 u_char* buffer = new u_char[bufferSize];
00082 u_char* writeAddress = buffer;
00083 for(int i = size_.height - 1; i >= 0; i--){
00084 int offset = i * size_.width;
00085 for(int j = 0; j < size_.width; j++){
00086 const Color4c& color = colors[offset + j];
00087 *writeAddress = color.b;
00088 writeAddress++;
00089 *writeAddress = color.g;
00090 writeAddress++;
00091 *writeAddress = color.r;
00092 writeAddress++;
00093 *writeAddress = color.a;
00094 writeAddress++;
00095 }
00096 }
00097 writer_->writeBytes(buffer, bufferSize);
00098 delete[] buffer;
00099
00100 writeFooter();
00101 }
00102
00103
00104 void TargaSaver::save(
00105 BinaryWriter* writer, int width, int height, const Color3c* colors){
00106 save(writer, DimensionI(width, height), colors);
00107 }
00108
00109
00110 void TargaSaver::save(
00111 BinaryWriter* writer, int width, int height, const Color4c* colors){
00112 save(writer, DimensionI(width, height), colors);
00113 }
00114
00115
00116 void TargaSaver::save(
00117 const String& filePath, const DimensionI& size, const Color3c* colors){
00118 BinaryFileWriter* binaryFileWriter = new BinaryFileWriter(filePath);
00119 save(binaryFileWriter, size, colors);
00120 delete binaryFileWriter;
00121 }
00122
00123
00124 void TargaSaver::save(
00125 const String& filePath, const DimensionI& size, const Color4c* colors){
00126 BinaryFileWriter* binaryFileWriter = new BinaryFileWriter(filePath);
00127 save(binaryFileWriter, size, colors);
00128 delete binaryFileWriter;
00129 }
00130
00131
00132 void TargaSaver::save(
00133 const String& filePath, int width, int height, const Color3c* colors){
00134 BinaryFileWriter* binaryFileWriter = new BinaryFileWriter(filePath);
00135 save(binaryFileWriter, DimensionI(width, height), colors);
00136 delete binaryFileWriter;
00137 }
00138
00139
00140 void TargaSaver::save(
00141 const String& filePath, int width, int height, const Color4c* colors){
00142 BinaryFileWriter* binaryFileWriter = new BinaryFileWriter(filePath);
00143 save(binaryFileWriter, DimensionI(width, height), colors);
00144 delete binaryFileWriter;
00145 }
00146
00147
00148 void TargaSaver::writeHeader(bool hasAlpha){
00149
00150 writer_->writeUChar(0);
00151
00152 writer_->writeUChar(0);
00153
00154 writer_->writeUChar(2);
00155
00156 writer_->writeShort(0);
00157 writer_->writeShort(0);
00158 writer_->writeUChar(0);
00159
00160 writer_->writeShort(0);
00161
00162 writer_->writeShort(0);
00163
00164 writer_->writeShort(size_.width);
00165
00166 writer_->writeShort(size_.height);
00167 if(hasAlpha){
00168
00169 writer_->writeUChar(32);
00170
00171 writer_->writeUChar(8);
00172 }else{
00173
00174 writer_->writeUChar(24);
00175
00176 writer_->writeUChar(0);
00177 }
00178 }
00179
00180
00181 void TargaSaver::writeFooter(){
00182 writer_->writeUInt(0);
00183 writer_->writeUInt(0);
00184 writer_->writeBytes(footer_, 18);
00185 }
00186
00187 }
00188