Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# TargaSaver.cpp

Go to the documentation of this file.

    00001 //------------------------------------------------------------------------------
    00002 // Lamp : Open source game middleware
    00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009 //
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018 //------------------------------------------------------------------------------
    00019
    00020 /** @file
    00021  * Targaセーバ実装
    00022  * @author Junpee
    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     // イメージID無し
    00150     writer_->writeUChar(0);
    00151     // カラーマップ非対応
    00152     writer_->writeUChar(0);
    00153     // イメージタイプはカラーマップ無し非圧縮RGB
    00154     writer_->writeUChar(2);
    00155     // カラーマップ詳細情報無し
    00156     writer_->writeShort(0);
    00157     writer_->writeShort(0);
    00158     writer_->writeUChar(0);
    00159     // X開始点
    00160     writer_->writeShort(0);
    00161     // Y開始点
    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 } // End of namespace Lamp
    00188 //------------------------------------------------------------------------------