00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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 }
00091
00092 #endif