ShPngImageImpl.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
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., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef SHPNGIMAGEIMPL_HPP
00021 #define SHPNGIMAGEIMPL_HPP
00022 
00023 #include "ShPngImage.hpp"
00024 #include "ShDllExport.hpp"
00025 
00026 namespace ShUtil {
00027 
00028 using namespace SH;
00029 using namespace std;
00030 
00031 struct ShPngImage {
00032   static float* read_PNG(const string& filename, int& width, int& height, 
00033                          int& elements);
00034   static void write_PNG(const string& filename, const float* data, 
00035                         int inverse_alpha, int width, int height, int elements);
00036   static void write_PNG16(const string& filename, const float* data, 
00037                           int inverse_alpha, int width, int height, int elements);
00038 };
00039 
00041 template<typename T>
00042 float* float_copy(const ShTypedImage<T>& image)
00043 {
00044   int array_length = image.width() * image.height() * image.elements();
00045   float* float_data = new float[array_length];
00046   
00047   const T* storage_data = image.data();
00048   for (int i=0; i < array_length; i++) {
00049     float_data[i] = storage_data[i];
00050   }
00051 
00052   return float_data;
00053 }
00054 
00055 // Public functions
00056 
00057 template<typename T>
00058 void load_PNG(ShTypedImage<T>& image, const string& filename)
00059 {
00060   int width, height, depth;
00061   float* png_data = ShPngImage::read_PNG(filename, width, height, depth);
00062 
00063   image.set_size(width, height, depth);
00064   T* storage_data = image.data();
00065 
00066   int array_length = width * height * depth;
00067   for (int i=0; i < array_length; i++) {
00068     storage_data[i] = png_data[i];
00069   }
00070 
00071   delete [] png_data;
00072 }
00073 
00074 template<typename T>
00075 void save_PNG(const ShTypedImage<T>& image, const string& filename, int inverse_alpha)
00076 {
00077   float* float_data = float_copy(image);
00078   ShPngImage::write_PNG(filename, float_data, inverse_alpha, image.width(), image.height(), image.elements());
00079   delete [] float_data;
00080 }
00081 
00082 template<typename T>
00083 void save_PNG16(const ShTypedImage<T>& image, const string& filename, int inverse_alpha)
00084 {
00085   float* float_data = float_copy(image);
00086   ShPngImage::write_PNG16(filename, float_data, inverse_alpha, image.width(), image.height(), image.elements());
00087   delete [] float_data;
00088 }
00089 
00090 } // namespace
00091 
00092 #endif // SHPNGIMAGEIMPL_HPP

Generated on Wed Nov 9 15:29:38 2005 for Sh by  doxygen 1.4.5