00001 #ifndef IMG_QT_IO_H_
00002 #define IMG_QT_IO_H_
00003
00004 #include <fstream>
00005
00006
00007 #include <QImage>
00008
00009 #include "wrap/qt/img_qt_convert.h"
00010 #include "img/img_io.h"
00011
00012 namespace img {
00013
00014 template<typename ScalarType, bool Safe>
00015 inline void openQtY(const QString filename, Image<1,ScalarType,Safe> &image)
00016 {
00017 convert_QImage_to_Y(QImage(filename),image);
00018 }
00019
00020 template<typename ScalarType, bool Safe>
00021 inline void openQtRGB(const QString filename, Image<3,ScalarType,Safe> &image)
00022 {
00023 convert_QImage_to_RGB(QImage(filename),image);
00024 }
00025
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 template<typename ScalarType, bool Safe>
00052 inline bool saveQtY(const Image<1,ScalarType,Safe> &image,const QString filename)
00053 {
00054 QImage qimage;
00055 convert_Y_to_QImage(image,qimage);
00056 bool success = qimage.save(filename);
00057 assert(success);
00058 if(Safe){
00059 if(!success) throw ImageException("cannot save image");
00060 }
00061 return success;
00062 }
00063
00064 template<typename ScalarType, bool Safe>
00065 inline bool saveQtRGB(const Image<3,ScalarType,Safe> &image,const QString filename)
00066 {
00067 QImage qimage;
00068 convert_RGB_to_QImage(image,qimage);
00069 bool success = qimage.save(filename);
00070 assert(success);
00071 if(Safe){
00072 if(!success) throw ImageException("cannot save image");
00073 }
00074 return success;
00075 }
00076
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 template<typename ScalarType, bool Safe>
00110 inline bool savePGM(const Image<1,ScalarType,Safe> &image, const QString filename)
00111 {
00112 assert(image.isValid());
00113 assert(image.attributes.hasRange(ScalarType(0.0),ScalarType(255.0)));
00114 assert(filename.endsWith(".pgm",Qt::CaseInsensitive));
00115 if(Safe){
00116 if(!image.isValid()) throw ImageException("Invalid image");
00117 if(!image.attributes.hasRange(ScalarType(0.0),ScalarType(255.0))) throw ImageException("Invalid range attribute");
00118 if(!filename.endsWith(".pgm",Qt::CaseInsensitive)) throw ImageException("filename is not .pgm");
00119 }
00120 using namespace std;
00121 ofstream file (filename.toStdString().c_str(), ios::out|ios::binary|ios::trunc);
00122 if (file.is_open()) {
00123 QString header = QString("P5 %1 %2 255\n").arg(image.width()).arg(image.height());
00124 file << header.toStdString().c_str();
00125
00126 for (int y_coord = 0; y_coord < image.height(); ++y_coord)
00127 for (int x_coord = 0; x_coord < image.width(); ++x_coord){
00128 unsigned char v = static_cast<unsigned char>(valueAsInt(clampValue(image.getValue(x_coord,y_coord,0))));
00129 file << v;
00130 }
00131
00132 file.close();
00133 return true;
00134 }
00135 if(Safe)
00136 throw ImageException("Unable to open file");
00137 return false;
00138 }
00139
00140
00141
00142 template<typename ScalarType, bool Safe>
00143 inline void open_and_normalize_range_Y(const QString filename, Image<1,ScalarType,Safe> &range_0_1_image)
00144 {
00145 Image<1,ScalarType,Safe> range_0_255_image;
00146 openQtY(filename,range_0_255_image);
00147 convert_range_0_255_to_0_1(range_0_255_image,range_0_1_image);
00148 }
00149
00150 template<typename ScalarType, bool Safe>
00151 inline void open_and_normalize_range_RGB(const QString filename, Image<3,ScalarType,Safe> &range_0_1_image)
00152 {
00153 Image<3,ScalarType,Safe> range_0_255_image;
00154 openQtRGB(filename,range_0_255_image);
00155 convert_range_0_255_to_0_1(range_0_255_image,range_0_1_image);
00156 }
00157
00158 template<typename ScalarType, bool Safe>
00159 inline void open_normalize_range_and_SRGB_linearize_RGB(const QString filename, Image<3,ScalarType,Safe> &linear_image)
00160 {
00161 Image<3,ScalarType,Safe> range_0_1_image;
00162 open_and_normalize_range_RGB(filename, range_0_1_image);
00163 range_0_1_image.attributes.setColorspace(img::SRGB);
00164 convert_gamma_precompensated_srgb_to_linear_srgb(range_0_1_image,linear_image);
00165 }
00166
00167 template<typename ScalarType, bool Safe>
00168 inline bool adjust_range_and_save_PGM(const Image<1,ScalarType,Safe> &range_0_1_image, const QString filename)
00169 {
00170 Image<1,ScalarType,Safe> range_0_255_image;
00171 convert_range_0_1_to_0_255(range_0_1_image,range_0_255_image);
00172 return savePGM(range_0_255_image,filename);
00173 }
00174
00175 template<typename ScalarType, bool Safe>
00176 inline bool SRGB_compress_adjust_range_and_save_PGM(const Image<1,ScalarType,Safe> &linear_image, const QString filename)
00177 {
00178 Image<1,ScalarType,Safe> range_0_1_image;
00179 convert_linear_srgb_to_gamma_precompensated_srgb(linear_image,range_0_1_image);
00180 return adjust_range_and_save_PGM(range_0_1_image,filename);
00181 }
00182
00183 template<typename ScalarType, bool Safe>
00184 inline bool adjust_range_and_save_RGB(const Image<3,ScalarType,Safe> &range_0_1_image, const QString filename)
00185 {
00186 Image<3,ScalarType,Safe> range_0_255_image;
00187 convert_range_0_1_to_0_255(range_0_1_image,range_0_255_image);
00188 return saveQtRGB(range_0_255_image,filename);
00189 }
00190
00191 template<typename ScalarType, bool Safe>
00192 inline bool SRGB_compress_adjust_range_and_save_RGB(const Image<3,ScalarType,Safe> &linear_image,const QString filename)
00193 {
00194 Image<3,ScalarType,Safe> range_0_1_image;
00195 convert_linear_srgb_to_gamma_precompensated_srgb(linear_image,range_0_1_image);
00196 return adjust_range_and_save_RGB(range_0_1_image,filename);
00197 }
00198
00199 template<typename ScalarType, bool Safe>
00200 inline bool adjust_range_and_save_Y(const Image<1,ScalarType,Safe> &range_0_1_image, const QString filename)
00201 {
00202 Image<1,ScalarType,Safe> range_0_255_image;
00203 convert_range_0_1_to_0_255(range_0_1_image,range_0_255_image);
00204 return saveQtY(range_0_255_image,filename);
00205 }
00206
00207 template<typename ScalarType, bool Safe>
00208 inline bool SRGB_compress_adjust_range_and_save_Y(const Image<1,ScalarType,Safe> &linear_image,const QString filename)
00209 {
00210 Image<1,ScalarType,Safe> range_0_1_image;
00211 convert_linear_srgb_to_gamma_precompensated_srgb(linear_image,range_0_1_image);
00212 return adjust_range_and_save_Y(range_0_1_image,filename);
00213 }
00214
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 }
00263
00264 #endif