00001 #ifndef IMG_QT_CONVERT_H_
00002 #define IMG_QT_CONVERT_H_
00003
00004
00005
00006 #include <QImage>
00007
00008 namespace img {
00009
00010 template<typename ScalarType, bool Safe>
00011 inline void convert_QImage_to_Y(const QImage &source, Image<1,ScalarType,Safe> &destination)
00012 {
00013 assert(!source.isNull());
00014 if(Safe){
00015 if(source.isNull()) throw ImageException("Null source image");
00016 }
00017 destination.setZero(source.width(),source.height());
00018 for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00019 for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00020 destination.setValue(x_coord, y_coord, 0, qGray(source.pixel(x_coord, y_coord)) );
00021 }
00022 destination.attributes.setRange(ScalarType(0.0),ScalarType(255.0));
00023 }
00024
00025 template<typename ScalarType, bool Safe>
00026 inline void convert_QImage_to_RGB(const QImage &source, Image<3,ScalarType,Safe> &destination)
00027 {
00028 assert(!source.isNull());
00029 if(Safe){
00030 if(source.isNull()) throw ImageException("Null source image");
00031 }
00032 destination.setZero(source.width(),source.height());
00033 for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00034 for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00035 QRgb qpixel = source.pixel(x_coord, y_coord);
00036 destination.setValue(x_coord, y_coord, 0, qRed(qpixel) );
00037 destination.setValue(x_coord, y_coord, 1, qGreen(qpixel) );
00038 destination.setValue(x_coord, y_coord, 2, qBlue(qpixel) );
00039 }
00040 destination.attributes.setRange(ScalarType(0.0),ScalarType(255.0));
00041 }
00042
00043 template<typename ScalarType, bool Safe>
00044 inline void convert_QImage_to_RGBA(const QImage &source, Image<4,ScalarType,Safe> &destination)
00045 {
00046 assert(!source.isNull());
00047 if(Safe){
00048 if(source.isNull()) throw ImageException("Null source image");
00049 }
00050 destination.setZero(source.width(),source.height());
00051 for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00052 for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00053 QRgb qpixel = source.pixel(x_coord, y_coord);
00054 destination.setValue(x_coord, y_coord, 0, qRed(qpixel) );
00055 destination.setValue(x_coord, y_coord, 1, qGreen(qpixel) );
00056 destination.setValue(x_coord, y_coord, 2, qBlue(qpixel) );
00057 destination.setValue(x_coord, y_coord, 3, qAlpha(qpixel) );
00058 }
00059 destination.attributes.setRange(ScalarType(0.0),ScalarType(255.0));
00060 }
00061
00062 template<typename ScalarType, bool Safe>
00063 inline void convert_Y_to_QImage(const Image<1,ScalarType,Safe> &source, QImage &destination)
00064 {
00065 assert(source.isValid());
00066 assert(source.attributes.hasRange(0,255));
00067 if(Safe){
00068 if(!source.isValid()) throw ImageException("Invalid source image");
00069 if(!source.attributes.hasRange(0,255)) throw ImageException("Invalid range attribute");
00070 }
00071 destination=QImage(source.width(),source.height(),QImage::Format_RGB32);
00072 for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00073 for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00074 int Y = valueAsInt(clampValue(source.getValue(x_coord,y_coord,0)));
00075 destination.setPixel(x_coord,y_coord,qRgb(Y,Y,Y));
00076 }
00077 }
00078
00079 template<typename ScalarType, bool Safe>
00080 inline void convert_RGB_to_QImage(const Image<3,ScalarType,Safe> &source, QImage &destination)
00081 {
00082 assert(source.isValid());
00083 assert(source.attributes.hasRange(ScalarType(0.0),ScalarType(255.0)));
00084 if(Safe){
00085 if(!source.isValid()) throw ImageException("Invalid source image");
00086 if(!source.attributes.hasRange(ScalarType(0.0),ScalarType(255.0))) throw ImageException("Invalid range attribute");
00087 }
00088 destination=QImage(source.width(),source.height(),QImage::Format_RGB32);
00089 for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00090 for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00091 destination.setPixel(x_coord,y_coord,qRgb(valueAsInt(clampValue(source.getValue(x_coord,y_coord,0))),
00092 valueAsInt(clampValue(source.getValue(x_coord,y_coord,1))),
00093 valueAsInt(clampValue(source.getValue(x_coord,y_coord,2))) ));
00094 }
00095 }
00096
00097 template<typename ScalarType, bool Safe>
00098 inline void convert_RGBA_to_QImage(const Image<4,ScalarType,Safe> &source, QImage &destination)
00099 {
00100 assert(source.isValid());
00101 assert(source.attributes.hasRange(ScalarType(0.0),ScalarType(255.0)));
00102 if(Safe){
00103 if(!source.isValid()) throw ImageException("Invalid source image");
00104 if(!source.attributes.hasRange(ScalarType(0.0),ScalarType(255.0))) throw ImageException("Invalid range attribute");
00105 }
00106 destination=QImage(source.width(),source.height(),QImage::Format_ARGB32);
00107 for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00108 for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00109 destination.setPixel(x_coord,y_coord,qRgba(valueAsInt(clampValue(source.getValue(x_coord,y_coord,0))),
00110 valueAsInt(clampValue(source.getValue(x_coord,y_coord,1))),
00111 valueAsInt(clampValue(source.getValue(x_coord,y_coord,2))),
00112 valueAsInt(clampValue(source.getValue(x_coord,y_coord,3))) ));
00113 }
00114 }
00115
00116 }
00117
00118 #endif