QXRD  0.11.16
qcepmaskdata.cpp
Go to the documentation of this file.
1 #include "qcepmaskdata.h"
2 #include "qcepallocator.h"
3 
5  : QcepImageData<short>(saver, width, height, def, parent)
6 {
7  set_Type("Mask Data");
8 }
9 
11 {
12 }
13 
15 {
16  return m_Image.data();
17 }
18 
20 {
21  int height = get_Height();
22  int width = get_Width();
23 
24  for (int j=0; j<height; j++) {
25  for (int i=0; i<width; i++) {
26  dest -> setMaskValue(i,j, maskValue(i,j));
27  }
28  }
29 
30  dest -> set_Title(get_Title()+" copy");
31 //
32 // setMask(from -> m_Mask);
33 }
34 
35 bool QcepMaskData::maskValue(int x, int y) const
36 {
37  if (x >= 0 && x < get_Width() && y >= 0 && y < get_Height()) {
38  return m_Image.value((get_Height()-y-1)*get_Width()+x);
39  }
40 
41  return defaultValue();
42 }
43 
44 void QcepMaskData::setMaskValue(int x, int y, bool mval)
45 {
46  if (x >= 0 && x < get_Width() && y >= 0 && y < get_Height()) {
47  m_Image[(get_Height()-y-1)*get_Width()+x] = mval;
48  }
49 }
50 
52 {
53  double cx = get_Width()/2, cy = get_Height()/2;
54  for (int y=0; y<get_Height(); y++) {
55  double dy = (((double)y)-cy)/cy;
56  double dx = sqrt(1-dy*dy);
57  int x0 = (int) (cx-dx*cx);
58  int x1 = (int) (cx+dx*cx);
59 
60  for (int x=0; x<x0; x++) {
61  setMaskValue(x,y,0);
62  }
63 
64  for (int x=x0; x<x1; x++) {
65  setMaskValue(x,y,1);
66  }
67 
68  for (int x=x1; x<get_Width(); x++) {
69  setMaskValue(x,y,0);
70  }
71  }
72 }
73 
75 {
76  m_Image.fill(0);
77 }
78 
80 {
81  m_Image.fill(1);
82 }
83 
85 {
86  int total = m_Image.count();
87 // bool *msk = m_Mask.data();
88 
89  for (int i=0; i<total; i++) {
90  m_Image[i] = !m_Image[i];
91  }
92 }
93 
95 {
96  QcepMaskDataPtr copy(QcepAllocator::newMask(QcepAllocator::AlwaysAllocate, get_Width(), get_Height(), 0, parent));
97 
98  copyMaskTo(copy);
99 
100  return copy;
101 }
102 
104 {
105  QcepMaskDataPtr copy(copyMask(this));
106 
107  int height = get_Height();
108  int width = get_Width();
109 
110  for (int j=0; j<height; j++) {
111  for (int i=0; i<width; i++) {
112  short res = maskValue(i,j);
113  for (int ii=-1; ii<2; ii++) {
114  for (int jj=-1; jj<2; jj++) {
115  res = qMin(res, (short) copy->maskValue(i+ii, j+jj));
116  }
117  }
118  setMaskValue(i,j, res);
119  }
120  }
121 }
122 
124 {
125  QcepMaskDataPtr copy(copyMask(this));
126 
127  int height = get_Height();
128  int width = get_Width();
129 
130  for (int j=0; j<height; j++) {
131  for (int i=0; i<width; i++) {
132  short res = maskValue(i,j);
133  for (int ii=-1; ii<2; ii++) {
134  for (int jj=-1; jj<2; jj++) {
135  res = qMax(res, (short) copy->maskValue(i+ii, j+jj));
136  }
137  }
138  setMaskValue(i,j, res);
139  }
140  }
141 }
142 
144 {
145  int height = get_Height();
146  int width = get_Width();
147 
148  for (int j=0; j<height; j++) {
149  for (int i=0; i<width; i++) {
150  setMaskValue(i,j, maskValue(i,j) & mask->maskValue(i,j));
151  }
152  }
153 }
154 
156 {
157  int height = get_Height();
158  int width = get_Width();
159 
160  for (int j=0; j<height; j++) {
161  for (int i=0; i<width; i++) {
162  setMaskValue(i,j, maskValue(i,j) | mask->maskValue(i,j));
163  }
164  }
165 }
166 
168 {
169  int height = get_Height();
170  int width = get_Width();
171 
172  for (int j=0; j<height; j++) {
173  for (int i=0; i<width; i++) {
174  setMaskValue(i,j, maskValue(i,j) ^ mask->maskValue(i,j));
175  }
176  }
177 }
178 
180 {
181  int height = get_Height();
182  int width = get_Width();
183 
184  for (int j=0; j<height; j++) {
185  for (int i=0; i<width; i++) {
186  setMaskValue(i,j, maskValue(i,j) & !mask->maskValue(i,j));
187  }
188  }
189 }
190 
192 {
193  int height = get_Height();
194  int width = get_Width();
195 
196  for (int j=0; j<height; j++) {
197  for (int i=0; i<width; i++) {
198  setMaskValue(i,j, maskValue(i,j) | !mask->maskValue(i,j));
199  }
200  }
201 }
202 
204 {
205  int height = get_Height();
206  int width = get_Width();
207 
208  for (int j=0; j<height; j++) {
209  for (int i=0; i<width; i++) {
210  setMaskValue(i,j, maskValue(i,j) ^ !mask->maskValue(i,j));
211  }
212  }
213 }
214 
215 void QcepMaskData::maskCircle(double cx, double cy, double r, bool val)
216 {
217  for (int j=0; j<=r; j++) {
218  double dy = (((double)j)/r);
219  if (fabs(dy)<1.0) {
220  double dx = sqrt(1-dy*dy);
221  int x0 = (int) (cx - dx*r);
222  int x1 = (int) (cx + dx*r);
223 
224  for (int i=x0; i<x1; i++) {
225  setMaskValue(i, (int)(cy+j), val);
226  setMaskValue(i, (int)(cy-j), val);
227  }
228  }
229  }
230 }
231 
233 {
234  QString res;
235 
236  int total = m_Image.count();
237  int totalSet = 0;
238 
239  for (int i=0; i<total; i++) {
240  if (m_Image[i]) {
241  totalSet++;
242  }
243  }
244 
245  return tr("%1/%2 pixels set").arg(totalSet).arg(total);
246 }
247 
249 {
250  int total = m_Image.count();
251  int totalSet = 0;
252 
253  for (int i=0; i<total; i++) {
254  if (m_Image[i] == 0) {
255  totalSet++;
256  }
257  }
258 
259  return totalSet;
260 }
261 
263 {
264  int total = m_Image.count();
265  int totalSet = 0;
266 
267  for (int i=0; i<total; i++) {
268  if (m_Image[i] == 1) {
269  totalSet++;
270  }
271  }
272 
273  return totalSet;
274 }
275 
277 {
278  return countUnmaskedPixels();
279 }
280 
282 {
283  int height = get_Height();
284  int width = get_Width();
285  int th_height = ThumbnailHeight;
286  int th_width = ThumbnailWidth;
287  double sc_height = height/th_height;
288  double sc_width = width/th_width;
289  int threshold = sc_height*sc_width;
290  double scale = qMin(sc_height, sc_width);
291  th_width = width/scale;
292  th_height = height/scale;
293 
294  QImage res(th_width, th_height, QImage::Format_RGB32);
295 
296  for (int j=0; j<th_height; j++) {
297  for (int i=0; i<th_width; i++) {
298  int tot=0;
299  for (int jsc = 0; jsc<sc_height; jsc++) {
300  for (int isc = 0; isc<sc_width; isc++) {
301  if (maskValue(i*scale+isc, j*scale+jsc)) {
302  tot++;
303  }
304  }
305  }
306 
307  int val = tot*255/(threshold+1);
308 
309  res.setPixel(i,th_height-j-1, qRgb(255-val,0,0));
310  }
311  }
312 
313  return res;
314 }
315 
317 {
318  return QSize(ThumbnailWidth, ThumbnailHeight);
319 }
320 
321 template <typename T>
322 void QcepMaskData::setMaskRange(QSharedPointer< QcepImageData<T> > image, T min, T max, bool inRange, bool setTo)
323 {
324  int nRows = image -> get_Height();
325  int nCols = image -> get_Width();
326 
327  for (int y=0; y<nRows; y++) {
328  for (int x=0; x<nCols; x++) {
329  T v = image -> value(x,y);
330 
331  if (((v >= min) && (v < max)) == inRange) {
332  setValue(x,y,setTo);
333  }
334  }
335  }
336 }
337 
338 template <typename T>
339 void QcepMaskData::hideMaskRange(QSharedPointer< QcepImageData<T> > image, T min, T max)
340 {
341  setMaskRange<T>(image, min, max, true, false);
342 }
343 
344 template <typename T>
345 void QcepMaskData::showMaskRange(QSharedPointer< QcepImageData<T> > image, T min, T max)
346 {
347  setMaskRange<T>(image, min, max, true, true);
348 }
349 
350 template void QcepMaskData::hideMaskRange<double>(QSharedPointer<QcepImageData<double> >, double, double);
351 template void QcepMaskData::showMaskRange<double>(QSharedPointer<QcepImageData<double> >, double, double);
void hideMaskAll()
short * mask()
void xorMask(QcepMaskDataPtr mask)
void setMaskValue(int x, int y, bool mval)
int countUnmaskedPixels() const
void orMask(QcepMaskDataPtr mask)
int countOverflowPixels() const
void setCircularMask()
void xorNotMask(QcepMaskDataPtr mask)
void andMask(QcepMaskDataPtr mask)
short value(int x, int y) const
QImage thumbnailImage() const
QcepMaskData(QcepSettingsSaverWPtr saver, int width, int height, int def, QcepObject *parent)
Definition: qcepmaskdata.cpp:4
QcepMaskDataPtr copyMask(QcepObject *parent)
void orNotMask(QcepMaskDataPtr mask)
bool maskValue(int x, int y) const
void andNotMask(QcepMaskDataPtr mask)
void setValue(int x, int y, short val)
short defaultValue() const
QSize thumbnailImageSize() const
void copyMaskTo(QcepMaskDataPtr dest)
int countMaskedPixels() const
QVector< short > m_Image
void hideMaskRange(QSharedPointer< QcepImageData< T > > image, T min, T max)
static QcepMaskDataPtr newMask(AllocationStrategy strat, int width, int height, int def, QcepObject *parent)
QSharedPointer< QcepMaskData > QcepMaskDataPtr
void maskCircle(double cx, double cy, double r, bool val)
QString summary()
void setMaskRange(QSharedPointer< QcepImageData< T > > image, T min, T max, bool inRange, bool setTo)
void showMaskAll()
void invertMask()
QWeakPointer< QcepSettingsSaver > QcepSettingsSaverWPtr
void showMaskRange(QSharedPointer< QcepImageData< T > > image, T min, T max)
QcepSettingsSaverWPtr saver()