SMIL  1.0.4
DImage.hxx
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _IMAGE_HXX
31 #define _IMAGE_HXX
34 #include <iostream>
35 #include <string>
36 #include <iomanip>
37 
38 #include "Core/include/DCoreEvents.h"
39 #include "Base/include/private/DMeasures.hpp"
40 #include "Base/include/private/DImageArith.hpp"
41 #include "IO/include/private/DImageIO.hxx"
42 #include "Gui/include/DGuiInstance.h"
43 
44 #ifdef SMIL_WRAP_RGB
45 #include "NSTypes/RGB/include/DRGB.h"
46 #endif // SMIL_WRAP_RGB
47 
48 #ifdef SMIL_WRAP_BIT
49 #include "NSTypes/Bit/include/DBit.h"
50 #endif // SMIL_WRAP_BIT
51 
52 namespace smil
53 {
54  template <class T> Image<T>::Image() : BaseImage("Image")
55  {
56  init();
57  }
58 
59  template <class T>
60  Image<T>::Image(size_t w, size_t h, size_t d) : BaseImage("Image")
61  {
62  init();
63  setSize(w, h, d);
64  }
65 
66  template <class T>
67  Image<T>::Image(const Image<T> &rhs, bool cloneData) : BaseImage(rhs)
68  {
69  init();
70  if (cloneData)
71  this->clone(rhs);
72  else
73  this->setSize(rhs);
74  }
75 
76  template <class T>
77  Image<T>::Image(const ResImage<T> &rhs, bool cloneData) : BaseImage(rhs)
78  {
79  init();
80  if (cloneData)
81  this->clone(rhs);
82  else
83  this->setSize(rhs);
84  }
85 
86  template <class T>
87  template <class T2>
88  Image<T>::Image(const Image<T2> &rhs, bool cloneData) : BaseImage(rhs)
89  {
90  init();
91  if (cloneData)
92  this->clone(rhs);
93  else
94  setSize(rhs);
95  }
96 
97  template <class T>
98  Image<T>::Image(BaseImage *_im, bool stealIdentity) : BaseImage("Image")
99  {
100  init();
101 
102  if (_im == NULL)
103  return;
104 
105  Image *im = castBaseImage(_im, T());
106  if (im == NULL)
107  return;
108 
109  if (!stealIdentity) {
110  setSize(*im);
111  return;
112  } else
113  drain(im, true);
114  }
115 
116  template <class T> void Image<T>::drain(Image<T> *im, bool deleteSrc)
117  {
118  if (allocated)
119  deallocate();
120 
121  this->width = im->width;
122  this->height = im->height;
123  this->depth = im->depth;
124 
125  this->sliceCount = im->sliceCount;
126  this->lineCount = im->lineCount;
127  this->pixelCount = im->pixelCount;
128 
129  this->pixels = im->pixels;
130  this->slices = im->slices;
131  this->lines = im->lines;
132 
133  this->allocated = im->allocated;
134  this->allocatedSize = im->allocatedSize;
135 
136  this->name = im->name;
137  this->viewer = im->viewer;
138 
139  im->allocated = false;
140 
141  if (deleteSrc)
142  delete im;
143  }
144 
145  template <class T> void Image<T>::clone(const Image<T> &rhs)
146  {
147  bool isAlloc = rhs.isAllocated();
148  this->setSize(rhs, isAlloc);
149  if (isAlloc)
150  copyLine<T>(rhs.getPixels(), getPixelCount(), this->pixels);
151  modified();
152  }
153 
154  template <class T>
155  template <class T2>
156  void Image<T>::clone(const Image<T2> &rhs)
157  {
158  bool isAlloc = rhs.isAllocated();
159  this->setSize(rhs, isAlloc);
160  if (isAlloc)
161  copy(rhs, *this);
162  modified();
163  }
164 
165  template <class T> Image<T>::Image(const char *fileName) : BaseImage("Image")
166  {
167  triggerEvents = true;
168  init();
169  read(fileName, *this);
170  }
171 
172  template <class T> Image<T>::~Image()
173  {
174  if (viewer)
175  delete viewer;
176  viewer = NULL;
177 
178  this->deallocate();
179  }
180 
181  template <class T> void Image<T>::init()
182  {
183  this->slices = NULL;
184  this->lines = NULL;
185  this->pixels = NULL;
186 
187  this->dataTypeSize = sizeof(pixelType);
188 
189  this->viewer = NULL;
190 
191  parentClass::init();
192  }
193 
194  template <class T> RES_T Image<T>::load(const char *fileName)
195  {
196  return read(fileName, *this);
197  }
198 
199  template <class T> RES_T Image<T>::save(const char *fileName)
200  {
201  return write(*this, fileName);
202  }
203 
204  template <class T> void Image<T>::modified()
205  {
206  if (!this->updatesEnabled)
207  return;
208 
209  BaseImageEvent event(this);
210  onModified.trigger(&event);
211  }
212 
213  template <class T> void Image<T>::setName(const char *_name)
214  {
215  parentClass::setName(_name);
216 
217  if (viewer)
218  viewer->setName(_name);
219  }
220 
221  template <class T> void Image<T>::createViewer()
222  {
223  if (viewer)
224  return;
225 
226  viewer = Gui::getInstance()->createDefaultViewer<T>(*this);
227  }
228 
229  template <class T> ImageViewer<T> *Image<T>::getViewer()
230  {
231  createViewer();
232  return viewer;
233  }
234 
235  template <class T> bool Image<T>::isVisible()
236  {
237  return (viewer && viewer->isVisible());
238  }
239 
240  template <class T> void Image<T>::hide()
241  {
242  if (viewer)
243  viewer->hide();
244  }
245 
246  template <class T> void Image<T>::show(const char *_name, bool labelImage)
247  {
248  if (isVisible())
249  return;
250 
251  if (!this->allocated) {
252  ERR_MSG("Image isn't allocated !");
253  return;
254  }
255 
256  parentClass::show();
257 
258  createViewer();
259 
260  if (_name)
261  setName(_name);
262 
263  if (!viewer)
264  return;
265 
266  if (!labelImage)
267  viewer->show();
268  else
269  viewer->showLabel();
270  }
271 
272  template <class T> void Image<T>::showLabel(const char *_name)
273  {
274  show(_name, true);
275  }
276 
277  template <class T> void Image<T>::showNormal(const char *_name)
278  {
279  if (_name)
280  setName(_name);
281  if (isVisible())
282  viewer->show();
283  else
284  show(_name, false);
285  }
286 
287  template <class T>
288  RES_T Image<T>::setSize(size_t w, size_t h, size_t d, bool doAllocate)
289  {
290  if (w == this->width && h == this->height && d == this->depth)
291  return RES_OK;
292 
293  if (this->allocated)
294  this->deallocate();
295 
296  this->width = w;
297  this->height = h;
298  this->depth = d;
299 
300  this->sliceCount = d;
301  this->lineCount = d * h;
302  this->pixelCount = this->lineCount * w;
303 
304  if (doAllocate)
305  ASSERT((this->allocate() == RES_OK));
306 
307  if (viewer)
308  viewer->setImage(*this);
309 
310  this->modified();
311 
312  return RES_OK;
313  }
314 
315  template <class T> RES_T Image<T>::allocate()
316  {
317  if (this->allocated)
318  return RES_ERR_BAD_ALLOCATION;
319 
320  this->pixels = createAlignedBuffer<T>(pixelCount);
321  // pixels = new pixelType[pixelCount];
322 
323  ASSERT((this->pixels != NULL), "Can't allocate image",
324  RES_ERR_BAD_ALLOCATION);
325 
326  this->allocated = true;
327  this->allocatedSize = this->pixelCount * sizeof(T);
328 
329  this->restruct();
330 
331  return RES_OK;
332  }
333 
334  template <class T> RES_T Image<T>::restruct(void)
335  {
336  if (this->slices)
337  delete[] this->slices;
338  if (this->lines)
339  delete[] this->lines;
340 
341  this->lines = new lineType[lineCount];
342  this->slices = new sliceType[sliceCount];
343 
344  sliceType cur_line = this->lines;
345  volType cur_slice = this->slices;
346 
347  size_t pixelsPerSlice = this->width * this->height;
348 
349  for (size_t k = 0; k < depth; k++, cur_slice++) {
350  *cur_slice = cur_line;
351 
352  for (size_t j = 0; j < height; j++, cur_line++)
353  *cur_line = pixels + k * pixelsPerSlice + j * width;
354  }
355 
356  return RES_OK;
357  }
358 
359  template <class T> RES_T Image<T>::deallocate()
360  {
361  if (!this->allocated)
362  return RES_OK;
363 
364  if (this->slices)
365  delete[] this->slices;
366  if (this->lines)
367  delete[] this->lines;
368  if (this->pixels)
369  deleteAlignedBuffer<T>(pixels);
370 
371  this->slices = NULL;
372  this->lines = NULL;
373  this->pixels = NULL;
374 
375  this->allocated = false;
376  this->allocatedSize = 0;
377 
378  return RES_OK;
379  }
380 
381 #ifndef SWIGPYTHON
382  template <class T> void Image<T>::toArray(T outArray[])
383  {
384  for (size_t i = 0; i < pixelCount; i++)
385  outArray[i] = pixels[i];
386  }
387 
388  template <class T> void Image<T>::fromArray(const T inArray[])
389  {
390  for (size_t i = 0; i < pixelCount; i++)
391  pixels[i] = inArray[i];
392  modified();
393  }
394 
395  template <class T> void Image<T>::toCharArray(signed char outArray[])
396  {
397  for (size_t i = 0; i < pixelCount; i++)
398  outArray[i] = pixels[i];
399  }
400 
401  template <class T> void Image<T>::fromCharArray(const signed char inArray[])
402  {
403  for (size_t i = 0; i < pixelCount; i++)
404  pixels[i] = inArray[i];
405  modified();
406  }
407 
408  template <class T> void Image<T>::toIntArray(int outArray[])
409  {
410  for (size_t i = 0; i < pixelCount; i++)
411  outArray[i] = pixels[i];
412  }
413 
414  template <class T> void Image<T>::fromIntArray(const int inArray[])
415  {
416  for (size_t i = 0; i < pixelCount; i++)
417  pixels[i] = inArray[i];
418  modified();
419  }
420 #endif // SWIGPYTHON
421 
422  template <class T> vector<int> Image<T>::toIntVector()
423  {
424  vector<int> vec;
425  for (size_t i = 0; i < pixelCount; i++)
426  vec.push_back(pixels[i]);
427  return vec;
428  }
429 
430  template <class T> void Image<T>::fromIntVector(vector<int> inVector)
431  {
432  ASSERT((inVector.size() == pixelCount),
433  "Vector length doesn't match image size.", );
434  for (size_t i = 0; i < min(pixelCount, inVector.size()); i++)
435  pixels[i] = inVector[i];
436  modified();
437  }
438 
439  template <class T> string Image<T>::toString()
440  {
441  string buf;
442  for (size_t i = 0; i < pixelCount; i++)
443  buf.push_back(pixels[i]);
444  return buf;
445  }
446 
447  template <class T> void Image<T>::fromString(string pixVals)
448  {
449  ASSERT((pixVals.size() == pixelCount),
450  "String length doesn't match image size.", );
451  for (size_t i = 0; i < pixelCount; i++)
452  pixels[i] = pixVals[i];
453  modified();
454  }
455 
456  template <class T>
457  void Image<T>::printSelf(ostream &os, bool displayPixVals, bool hexaGrid,
458  string indent) const
459  {
460 #if DEBUG_LEVEL > 1
461  cout << "Image::printSelf: " << this << endl;
462 #endif // DEBUG_LEVEL > 1
463  if (name != "")
464  os << indent << "Image name: " << name << endl;
465 
466  if (depth > 1)
467  os << indent << "3D image" << endl;
468  else
469  os << indent << "2D image" << endl;
470 
471  T *dum = NULL;
472  os << indent << "Data type: " << getDataTypeAsString<T>(dum) << endl;
473 
474  if (depth > 1)
475  os << indent << "Size: " << width << "x" << height << "x" << depth << endl;
476  else
477  os << indent << "Size: " << width << "x" << height << endl;
478 
479  if (allocated)
480  os << indent << "Allocated (" << displayBytes(allocatedSize) << ")" << endl;
481  else
482  os << indent << "Not allocated" << endl;
483 
484  if (displayPixVals) {
485  std::stringstream tStr;
486  tStr << (long) ImDtTypes<T>::max();
487  size_t tSsize = tStr.str().size();
488  if (hexaGrid)
489  tSsize = size_t(tSsize * 1.5);
490 
491  os << indent << "Pixel values:" << endl;
492  size_t i, j, k;
493 
494  for (k = 0; k < depth; k++) {
495  for (j = 0; j < height; j++) {
496  if (hexaGrid && j % 2)
497  os << setw(tSsize / 2 + 1) << " ";
498  for (i = 0; i < width; i++)
499  os << setw(tSsize + 1) << ImDtTypes<T>::toString(getPixel(i, j, k))
500  << ",";
501  os << endl;
502  }
503  os << endl;
504  }
505  os << endl;
506  }
507 
508  os << endl;
509  }
510 
511  template <class T> SharedImage<T> Image<T>::getSlice(size_t sliceNum) const
512  {
513  if (sliceNum >= this->depth) {
514  ERR_MSG("sliceNum > image depth");
515  return SharedImage<T>();
516  }
517  return SharedImage<T>(*this->getSlices()[sliceNum], this->width,
518  this->height, 1);
519  }
520 
521  // OPERATORS
522 
523  template <class T> void operator<<(ostream &os, const Image<T> &im)
524  {
525  im.printSelf(os);
526  }
527 
528  template <class T> Image<T> &Image<T>::operator<<(const char *s)
529  {
530  read(s, *this);
531  return *this;
532  }
533 
534  template <class T> Image<T> &Image<T>::operator>>(const char *s)
535  {
536  write(*this, s);
537  return *this;
538  }
539 
540  template <class T> Image<T> &Image<T>::operator<<(const Image<T> &rhs)
541  {
542  copy(rhs, *this);
543  return *this;
544  }
545 
546  template <class T> Image<T> &Image<T>::operator<<(const T &value)
547  {
548  fill(*this, value);
549  return *this;
550  }
551 
552  template <class T> ResImage<T> Image<T>::operator~() const
553  {
554  ResImage<T> im(*this);
555  inv(*this, im);
556  return im;
557  }
558 
559  template <class T> ResImage<T> Image<T>::operator-() const
560  {
561  ResImage<T> im(*this);
562  inv(*this, im);
563  return im;
564  }
565 
566  template <class T> ResImage<T> Image<T>::operator+(const Image<T> &rhs)
567  {
568  ResImage<T> im(*this);
569  add(*this, rhs, im);
570  return im;
571  }
572 
573  template <class T> ResImage<T> Image<T>::operator+(const T &value)
574  {
575  ResImage<T> im(*this);
576  add(*this, value, im);
577  return im;
578  }
579 
580  template <class T> Image<T> &Image<T>::operator+=(const Image<T> &rhs)
581  {
582  add(*this, rhs, *this);
583  return *this;
584  }
585 
586  template <class T> Image<T> &Image<T>::operator+=(const T &value)
587  {
588  add(*this, value, *this);
589  return *this;
590  }
591 
592  template <class T> ResImage<T> Image<T>::operator-(const Image<T> &rhs)
593  {
594  ResImage<T> im(*this);
595  sub(*this, rhs, im);
596  return im;
597  }
598 
599  template <class T> ResImage<T> Image<T>::operator-(const T &value)
600  {
601  ResImage<T> im(*this);
602  sub(*this, value, im);
603  return im;
604  }
605 
606  template <class T> Image<T> &Image<T>::operator-=(const Image<T> &rhs)
607  {
608  sub(*this, rhs, *this);
609  return *this;
610  }
611 
612  template <class T> Image<T> &Image<T>::operator-=(const T &value)
613  {
614  sub(*this, value, *this);
615  return *this;
616  }
617 
618  template <class T> ResImage<T> Image<T>::operator*(const Image<T> &rhs)
619  {
620  ResImage<T> im(*this);
621  mul(*this, rhs, im);
622  return im;
623  }
624 
625  template <class T> ResImage<T> Image<T>::operator*(const T &value)
626  {
627  ResImage<T> im(*this);
628  mul(*this, value, im);
629  return im;
630  }
631 
632  template <class T> Image<T> &Image<T>::operator*=(const Image<T> &rhs)
633  {
634  mul(*this, rhs, *this);
635  return *this;
636  }
637 
638  template <class T> Image<T> &Image<T>::operator*=(const T &value)
639  {
640  mul(*this, value, *this);
641  return *this;
642  }
643 
644  template <class T> ResImage<T> Image<T>::operator/(const Image<T> &rhs)
645  {
646  ResImage<T> im(*this);
647  div(*this, rhs, im);
648  return im;
649  }
650 
651  template <class T> ResImage<T> Image<T>::operator/(const T &value)
652  {
653  ResImage<T> im(*this);
654  div(*this, value, im);
655  return im;
656  }
657 
658  template <class T> Image<T> &Image<T>::operator/=(const Image<T> &rhs)
659  {
660  div(*this, rhs, *this);
661  return *this;
662  }
663 
664  template <class T> Image<T> &Image<T>::operator/=(const T &value)
665  {
666  div(*this, value, *this);
667  return *this;
668  }
669 
670  template <class T> ResImage<T> Image<T>::operator==(const Image<T> &rhs)
671  {
672  ResImage<T> im(*this);
673  equ(*this, rhs, im);
674  return im;
675  }
676 
677  template <class T> ResImage<T> Image<T>::operator!=(const Image<T> &rhs)
678  {
679  ResImage<T> im(*this);
680  diff(*this, rhs, im);
681  return im;
682  }
683 
684  template <class T> ResImage<T> Image<T>::operator<(const Image<T> &rhs)
685  {
686  ResImage<T> im(*this);
687  low(*this, rhs, im);
688  return im;
689  }
690 
691  template <class T> ResImage<T> Image<T>::operator<(const T &value)
692  {
693  ResImage<T> im(*this);
694  low(*this, value, im);
695  return im;
696  }
697 
698  template <class T> ResImage<T> Image<T>::operator<=(const Image<T> &rhs)
699  {
700  ResImage<T> im(*this);
701  lowOrEqu(*this, rhs, im);
702  return im;
703  }
704 
705  template <class T> ResImage<T> Image<T>::operator<=(const T &value)
706  {
707  ResImage<T> im(*this);
708  lowOrEqu(*this, value, im);
709  return im;
710  }
711 
712  template <class T> ResImage<T> Image<T>::operator>(const Image<T> &rhs)
713  {
714  ResImage<T> im(*this);
715  grt(*this, rhs, im);
716  return im;
717  }
718 
719  template <class T> ResImage<T> Image<T>::operator>(const T &value)
720  {
721  ResImage<T> im(*this);
722  grt(*this, value, im);
723  return im;
724  }
725 
726  template <class T> ResImage<T> Image<T>::operator>=(const Image<T> &rhs)
727  {
728  ResImage<T> im(*this);
729  grtOrEqu(*this, rhs, im);
730  return im;
731  }
732 
733  template <class T> ResImage<T> Image<T>::operator>=(const T &value)
734  {
735  ResImage<T> im(*this);
736  grtOrEqu(*this, value, im);
737  return im;
738  }
739 
740  template <class T> ResImage<T> Image<T>::operator|(const Image<T> &rhs)
741  {
742  ResImage<T> im(*this);
743  sup(*this, rhs, im);
744  return im;
745  }
746 
747  template <class T> ResImage<T> Image<T>::operator|(const T &value)
748  {
749  ResImage<T> im(*this);
750  sup(*this, value, im);
751  return im;
752  }
753 
754  template <class T> Image<T> &Image<T>::operator|=(const Image<T> &rhs)
755  {
756  sup(*this, rhs, *this);
757  return *this;
758  }
759 
760  template <class T> Image<T> &Image<T>::operator|=(const T &value)
761  {
762  sup(*this, value, *this);
763  return *this;
764  }
765 
766  template <class T> ResImage<T> Image<T>::operator&(const Image<T> &rhs)
767  {
768  ResImage<T> im(*this);
769  inf(*this, rhs, im);
770  return im;
771  }
772 
773  template <class T> ResImage<T> Image<T>::operator&(const T &value)
774  {
775  ResImage<T> im(*this);
776  inf(*this, value, im);
777  return im;
778  }
779 
780  template <class T> Image<T> &Image<T>::operator&=(const Image<T> &rhs)
781  {
782  inf(*this, rhs, *this);
783  return *this;
784  }
785 
786  template <class T> Image<T> &Image<T>::operator&=(const T &value)
787  {
788  inf(*this, value, *this);
789  return *this;
790  }
791 
792  template <class T> Image<T>::operator bool()
793  {
794  return vol(*this) == ImDtTypes<T>::max() * pixelCount;
795  }
796 
797  template <class T> Image<T> &Image<T>::operator<<(const lineType &tab)
798  {
799  for (size_t i = 0; i < pixelCount; i++)
800  pixels[i] = tab[i];
801  modified();
802  return *this;
803  }
804 
805  template <class T> Image<T> &Image<T>::operator<<(vector<T> &vect)
806  {
807  typename vector<T>::iterator it = vect.begin();
808  typename vector<T>::iterator it_end = vect.end();
809 
810  for (size_t i = 0; i < pixelCount; i++, it++) {
811  if (it == it_end)
812  break;
813  pixels[i] = *it;
814  }
815  modified();
816  return *this;
817  }
818 
819  template <class T> Image<T> &Image<T>::operator>>(vector<T> &vect)
820  {
821  vect.clear();
822  for (size_t i = 0; i < pixelCount; i++) {
823  vect.push_back(pixels[i]);
824  }
825  return *this;
826  }
827 
828  typedef Image<UINT8> Image_UINT8;
829  typedef Image<UINT16> Image_UINT16;
830  typedef Image<UINT32> Image_UINT32;
831  typedef Image<bool> Image_bool;
832 
833 } // namespace smil
834 
835 #if defined SWIGPYTHON && defined USE_NUMPY && defined(SWIG_WRAP_CORE)
836 #include "Core/include/DNumpy.h"
837 
838 namespace smil
839 {
840  template <class T> PyObject *Image<T>::getNumArray(bool c_contigous)
841  {
842  npy_intp d[3];
843  int dim = this->getDimension();
844  if (dim == 3) {
845  d[0] = this->getDepth();
846  d[1] = this->getHeight();
847  d[2] = this->getWidth();
848  } else {
849  d[0] = this->getHeight();
850  d[1] = this->getWidth();
851  }
852  PyObject *array = PyArray_SimpleNewFromData(dim, d, getNumpyType(*this),
853  this->getPixels());
854 
855  if (c_contigous) {
856  return array;
857  } else {
858  npy_intp t2[] = {1, 0};
859  npy_intp t3[] = {2, 1, 0};
860  PyArray_Dims trans_dims;
861  if (dim == 3)
862  trans_dims.ptr = t3;
863  else
864  trans_dims.ptr = t2;
865  trans_dims.len = dim;
866 
867  PyObject *res = PyArray_Transpose((PyArrayObject *) array, &trans_dims);
868  Py_DECREF(array);
869  return res;
870  }
871  }
872 
873  template <class T> void Image<T>::fromNumArray(PyObject *obj)
874  {
875  PyArrayObject *arr = NULL;
876  PyArray_Descr *descr = NULL;
877 
878  if (PyArray_GetArrayParamsFromObject(obj, NULL, 1, &descr, NULL, NULL, &arr,
879  NULL) != 0) {
880  ERR_MSG("Input must be a NumPy array");
881  return;
882  }
883  descr = PyArray_DESCR(arr);
884  if (descr && descr->type_num != getNumpyType(*this)) {
885  ERR_MSG("Wrong input NumPy array data type");
886  return;
887  }
888  int ndim = PyArray_NDIM(arr);
889  if (ndim > 3) {
890  ERR_MSG("Numpy array has more than three axes");
891  return;
892  }
893  npy_intp *dims = PyArray_DIMS(arr);
894  for (int i = 0; i < 3; i++) {
895  if (i >= ndim)
896  dims[i] = 1;
897  }
898 
899  setSize(dims[0], dims[1], dims[2]);
900 #if 0
901  T *data = (T*)PyArray_DATA(arr);
902  if (data)
903  {
904  for (size_t i=0;i<pixelCount;i++)
905  pixels[i] = data[i];
906  }
907 #else
908 
909 #if 0
910  for (npy_intp i = 0; i < dims[0]; i++) {
911  for (npy_intp j = 0; j < dims[1]; j++) {
912  for (npy_intp k = 0; k < dims[2]; k++) {
913  T *v = (T *) PyArray_GETPTR3(arr, i, j, k);
914  setPixel(i, j, k, *v);
915  }
916  }
917  }
918 #else
919  npy_intp ind[3];
920  for (ind[0] = 0; ind[0] < dims[0]; ind[0]++) {
921  for (ind[1] = 0; ind[1] < dims[1]; ind[1]++) {
922  for (ind[2] = 0; ind[2] < dims[2]; ind[2]++) {
923  T *v = (T *) PyArray_GetPtr(arr, ind);
924  setPixel(ind[0], ind[1], ind[2], *v);
925  }
926  }
927  }
928 #endif
929 #endif
930  modified();
931  }
932 
933 } // namespace smil
934 
935 #endif // defined SWIGPYTHON && defined USE_NUMPY
938 #endif // _IMAGE_HXX
Image()
Default constructor.
void fromNumArray(PyObject *array)
fromNumArray() -
PyObject * getNumArray(bool c_contigous=false)
getNumArray() -
RES_T mul(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
mul() - Multiply two images
Definition: DImageArith.hpp:749
RES_T low(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
low() - Lower operator
Definition: DImageArith.hpp:604
RES_T sub(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
sub() - Subtraction between two images
Definition: DImageArith.hpp:160
RES_T grtOrEqu(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
grtOrEqu() - Greater or equal operator
Definition: DImageArith.hpp:560
RES_T div(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
div() - Division between two images
Definition: DImageArith.hpp:690
RES_T lowOrEqu(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
lowOrEqu() - Lower or equal operator
Definition: DImageArith.hpp:648
RES_T diff(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
diff() - Difference between two images.
Definition: DImageArith.hpp:448
RES_T inv(const Image< T > &imIn, Image< T > &imOut)
inv() - Invert an image.
Definition: DImageArith.hpp:70
RES_T sup(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
sup() - Sup of two images
Definition: DImageArith.hpp:254
RES_T inf(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
inf() - Inf of two images
Definition: DImageArith.hpp:305
RES_T add(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
add() - Addition (with saturation check)
Definition: DImageArith.hpp:89
RES_T equ(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
equ() - Equality operator (pixel by pixel)
Definition: DImageArith.hpp:366
RES_T grt(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
grt() - Greater operator
Definition: DImageArith.hpp:516
RES_T fill(Image< T > &imOut, const T &value)
fill() - Fill an image with a given value.
Definition: DImageArith.hpp:1456
bool operator==(const vector< Diedge< NodeT, WeightT >> &e1, const vector< Diedge< NodeT, WeightT >> &e2)
Check if two vectors of edges are equal.
Definition: DDigraph.hpp:166
RES_T write(const Image< T > &image, const char *filename)
Write image into file.
Definition: DImageIO.hxx:184
RES_T read(const char *filename, Image< T > &image)
Read image file.
Definition: DImageIO.hxx:107
double vol(const Image< T > &imIn)
vol() - Volume of an image
Definition: DMeasures.hpp:129
RES_T copy(const Image< T1 > &imIn, size_t startX, size_t startY, size_t startZ, size_t sizeX, size_t sizeY, size_t sizeZ, Image< T2 > &imOut, size_t outStartX=0, size_t outStartY=0, size_t outStartZ=0)
copy() - Copy image (or a zone) into an output image
Definition: DImageTransform.hpp:84
RES_T clone(const Image< T > &imIn, Image< T > &imOut)
clone() - Clone an image
Definition: DImageTransform.hpp:265