PCMBuffer.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00025 //
00026 // Inner includes
00027 //
00028 
00029 #include "../include/PCMBuffer.h"
00030 
00031 #include "../include/PCMStringConverter.h"
00032 
00033 #include "../include/PCMNode.h"
00034 
00035 //
00036 // Outer includes
00037 //
00038 
00039 #include <IL/il.h>
00040 #include <IL/ilu.h>
00041 
00042 namespace ParCompMark
00043 {
00044 
00045   //
00046   // Constructors & destructor
00047   //
00048 
00049   Buffer::Buffer()
00050         // You have to initialize the following attributes:
00051         // - mParent
00052         // - mLeft
00053         // - mTop
00054         // - mWidth
00055         // - mHeight
00056         // - mOwnPointers
00057         // - mColour
00058         // - mDepth
00059         // - mDepthFormat
00060         // - mOutputRowPixel
00061         // - mInitialized
00062   {
00063         Except(INVALID_OPERATION_ERROR, "Buffer::Buffer()",
00064            "Default constructor for Squirrel compatibility. Calling this constructor always raises an exception.");
00065   }
00066 
00067  /*----------------------------------------------------------------------*/
00068 
00069   Buffer::Buffer(const PCuint & left, const PCuint & top, const PCuint & width,
00070                  const PCuint & height, const PCuint & depthFormat, Node * parent):
00071         // Initialize parent node 
00072    mParent(parent               /* Parent node */
00073         )
00074         // You have to initialize the following attributes:
00075         // - mParent
00076         // - mLeft
00077         // - mTop
00078         // - mWidth
00079         // - mHeight
00080         // - mOwnPointers
00081         // - mColour
00082         // - mDepth
00083         // - mDepthFormat
00084         // - mOutputRowPixel
00085         // - mInitialized
00086   {
00087         mLeft = left;
00088         mTop = top;
00089         mWidth = width;
00090         mHeight = height;
00091         mOwnPointers = true;
00092         mColour = 0;
00093         mDepth = 0;
00094         mDepthFormat = depthFormat;
00095         mOutputRowPixel = 0;
00096         mInitialized = false;
00097 
00098         Logger::getInstance()->log(Logger::NOTICE, "Buffer on node `" + mParent->getName() + "\' has been created.");
00099   }
00100 
00101  /*----------------------------------------------------------------------*/
00102 
00103   Buffer::~Buffer()
00104   {
00105         if(mInitialized)
00106          finalize();
00107 
00108         Logger::getInstance()->log(Logger::NOTICE, "Buffer on node `" + mParent->getName() + "\' has been destroyed.");
00109   }
00110 
00111  /*----------------------------------------------------------------------*/
00112 
00113   //
00114   // Methods
00115   //
00116 
00117   void Buffer::initialize()
00118   {
00119         mColour = new PCuint[mWidth * mHeight];
00120 
00121         switch (mDepthFormat)
00122         {
00123          case PC_PF_Z32I:
00124                 mDepth = new PCuint[mWidth * mHeight];
00125                 break;
00126          case PC_PF_Z32F:
00127                 mDepth = new float[mWidth * mHeight];
00128 
00129                 break;
00130          default:
00131                 Except(INVALID_ENUM_ERROR, "Buffer::initialize()",
00132                    "Depth format `" + StringConverter::toString(mDepthFormat) + "\' on node `" +
00133                    mParent->getName() + "\' is invalid.");
00134         }
00135 
00136         mInitialized = true;
00137 
00138         Logger::getInstance()->log(Logger::DEBUG, "Buffer on node `" + mParent->getName() + "\' has been initialized.");
00139   }
00140 
00141  /*----------------------------------------------------------------------*/
00142 
00143   void Buffer::finalize()
00144   {
00145         Assert(mInitialized, INVALID_OPERATION_ERROR, "Buffer::finalize()");
00146 
00147         freeBuffers();
00148 
00149         mInitialized = false;
00150 
00151         Logger::getInstance()->log(Logger::DEBUG, "Buffer on node `" + mParent->getName() + "\' has been finalized.");
00152   }
00153 
00154  /*----------------------------------------------------------------------*/
00155 
00156   void Buffer::saveToFile(const std::string & filename)
00157   {
00158         ilInit();
00159         iluInit();
00160         ilEnable(IL_FILE_OVERWRITE);
00161 
00162         // Create img
00163         ILuint img;
00164 
00165         ilGenImages(1, &img);
00166         ilBindImage(img);
00167 
00168         // Set image
00169         ilTexImage(mWidth,      // Width
00170                  mHeight,       // Height
00171                  1,             // Depth
00172                  4,             // Number of channels
00173                  IL_BGRA,       // Format
00174                  IL_UNSIGNED_BYTE,      // Type
00175                  (ILvoid *) mColour     // Data
00176          );
00177 
00178         // Save image
00179         ilSaveImage((char *) filename.c_str());
00180 
00181         // Delete img
00182         ilDeleteImages(1, &img);
00183   }
00184 
00185  /*----------------------------------------------------------------------*/
00186 
00187   void Buffer::freeBuffers()
00188   {
00189         if(mOwnPointers && mColour)
00190         {
00191          delete[]mColour;
00192 
00193          mColour = 0;
00194         }
00195 
00196         if(mOwnPointers && mDepth)
00197         {
00198          switch (mDepthFormat)
00199          {
00200                 case PC_PF_Z32I:
00201                  delete[](PCuint *) mDepth;
00202                  break;
00203                 case PC_PF_Z32F:
00204                  delete[](float *) mDepth;
00205 
00206                  break;
00207                 default:
00208                  Except(INTERNAL_ERROR, "Buffer::finalize()",
00209                          "Something has changed depth format on node `" + mParent->getName() +
00210                          "\' to invalid value: `" + StringConverter::toString(mDepthFormat) + "\' .");
00211          }
00212          mDepth = 0;
00213         }
00214   }
00215 
00216  /*----------------------------------------------------------------------*/
00217 
00218 }