File: img_qt_convert.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (118 lines) | stat: -rwxr-xr-x 5,200 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef IMG_QT_CONVERT_H_
#define IMG_QT_CONVERT_H_

// implementation of conversione between qimage and basic image types

#include <QImage>

namespace img {

template<typename ScalarType, bool Safe> 
inline void convert_QImage_to_Y(const QImage &source, Image<1,ScalarType,Safe> &destination)
{
  assert(!source.isNull());
  if(Safe){
    if(source.isNull())  throw ImageException("Null source image");
  }
  destination.setZero(source.width(),source.height());
  for (int y_coord = 0; y_coord < source.height(); ++y_coord)
    for (int x_coord = 0; x_coord < source.width(); ++x_coord){
      destination.setValue(x_coord, y_coord, 0, qGray(source.pixel(x_coord, y_coord)) );
    }
  destination.attributes.setRange(ScalarType(0.0),ScalarType(255.0));
}

template<typename ScalarType, bool Safe> 
inline void convert_QImage_to_RGB(const QImage &source, Image<3,ScalarType,Safe> &destination)
{
  assert(!source.isNull());
  if(Safe){
    if(source.isNull())  throw ImageException("Null source image");
  }
  destination.setZero(source.width(),source.height());
  for (int y_coord = 0; y_coord < source.height(); ++y_coord)
    for (int x_coord = 0; x_coord < source.width(); ++x_coord){
      QRgb qpixel = source.pixel(x_coord, y_coord);
      destination.setValue(x_coord, y_coord, 0, qRed(qpixel) );
      destination.setValue(x_coord, y_coord, 1, qGreen(qpixel) );
      destination.setValue(x_coord, y_coord, 2, qBlue(qpixel) );
    }
  destination.attributes.setRange(ScalarType(0.0),ScalarType(255.0));
}

template<typename ScalarType, bool Safe> 
inline void convert_QImage_to_RGBA(const QImage &source, Image<4,ScalarType,Safe> &destination)
{
  assert(!source.isNull());
  if(Safe){
    if(source.isNull())  throw ImageException("Null source image");
  }
  destination.setZero(source.width(),source.height());
  for (int y_coord = 0; y_coord < source.height(); ++y_coord)
    for (int x_coord = 0; x_coord < source.width(); ++x_coord){
      QRgb qpixel = source.pixel(x_coord, y_coord);
      destination.setValue(x_coord, y_coord, 0, qRed(qpixel) );
      destination.setValue(x_coord, y_coord, 1, qGreen(qpixel) );
      destination.setValue(x_coord, y_coord, 2, qBlue(qpixel) );
      destination.setValue(x_coord, y_coord, 3, qAlpha(qpixel) );
    }
  destination.attributes.setRange(ScalarType(0.0),ScalarType(255.0));
}

template<typename ScalarType, bool Safe> 
inline void convert_Y_to_QImage(const Image<1,ScalarType,Safe> &source, QImage &destination)
{
  assert(source.isValid());
  assert(source.attributes.hasRange(0,255));
  if(Safe){
    if(!source.isValid()) throw ImageException("Invalid source image");
    if(!source.attributes.hasRange(0,255)) throw ImageException("Invalid range attribute");
  }
  destination=QImage(source.width(),source.height(),QImage::Format_RGB32);
  for (int y_coord = 0; y_coord < source.height(); ++y_coord)
    for (int x_coord = 0; x_coord < source.width(); ++x_coord){
      int Y = valueAsInt(clampValue(source.getValue(x_coord,y_coord,0)));
  	  destination.setPixel(x_coord,y_coord,qRgb(Y,Y,Y));
    }
}

template<typename ScalarType, bool Safe> 
inline void convert_RGB_to_QImage(const Image<3,ScalarType,Safe> &source, QImage &destination)
{
  assert(source.isValid());
  assert(source.attributes.hasRange(ScalarType(0.0),ScalarType(255.0)));
  if(Safe){
    if(!source.isValid()) throw ImageException("Invalid source image");
    if(!source.attributes.hasRange(ScalarType(0.0),ScalarType(255.0))) throw ImageException("Invalid range attribute");
  }
  destination=QImage(source.width(),source.height(),QImage::Format_RGB32);
  for (int y_coord = 0; y_coord < source.height(); ++y_coord)
    for (int x_coord = 0; x_coord < source.width(); ++x_coord){
  	  destination.setPixel(x_coord,y_coord,qRgb(valueAsInt(clampValue(source.getValue(x_coord,y_coord,0))),
  	                                            valueAsInt(clampValue(source.getValue(x_coord,y_coord,1))),
  	                                            valueAsInt(clampValue(source.getValue(x_coord,y_coord,2))) ));
    }
}

template<typename ScalarType, bool Safe> 
inline void convert_RGBA_to_QImage(const Image<4,ScalarType,Safe> &source, QImage &destination)
{
  assert(source.isValid());
  assert(source.attributes.hasRange(ScalarType(0.0),ScalarType(255.0)));
  if(Safe){
    if(!source.isValid()) throw ImageException("Invalid source image");
    if(!source.attributes.hasRange(ScalarType(0.0),ScalarType(255.0))) throw ImageException("Invalid range attribute");
  }
  destination=QImage(source.width(),source.height(),QImage::Format_ARGB32);
  for (int y_coord = 0; y_coord < source.height(); ++y_coord)
    for (int x_coord = 0; x_coord < source.width(); ++x_coord){
  	  destination.setPixel(x_coord,y_coord,qRgba(valueAsInt(clampValue(source.getValue(x_coord,y_coord,0))),
  	                                             valueAsInt(clampValue(source.getValue(x_coord,y_coord,1))),
  	                                             valueAsInt(clampValue(source.getValue(x_coord,y_coord,2))),
  	                                             valueAsInt(clampValue(source.getValue(x_coord,y_coord,3))) ));
    }
}

} //end namespace img

#endif /*IMG_QT_CONVERT_H_*/