| 1 | //  Classes image heritant de TMatrix<T> | 
|---|
| 2 | //                       R.Ansari, C.Magneville 07/2000 | 
|---|
| 3 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA | 
|---|
| 4 |  | 
|---|
| 5 | #include "sopnamsp.h" | 
|---|
| 6 | #include "machdefs.h" | 
|---|
| 7 | #include <stdio.h> | 
|---|
| 8 | #include <stdlib.h> | 
|---|
| 9 | #include "pexceptions.h" | 
|---|
| 10 | #include "cimage.h" | 
|---|
| 11 | #include "datatype.h" | 
|---|
| 12 | #include <typeinfo> | 
|---|
| 13 |  | 
|---|
| 14 | /*! | 
|---|
| 15 | \class SOPHYA::Image | 
|---|
| 16 | \ingroup NTools | 
|---|
| 17 | This class specializes the Matrix class, for representing intensity | 
|---|
| 18 | (or grey-level) images. It adds the possibility of defining pixel | 
|---|
| 19 | size and offset. The convention for the acces operator is also | 
|---|
| 20 | changed, compared to the matrix class. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | //! Default constructor - Pixel array not allocated | 
|---|
| 24 | template <class T> | 
|---|
| 25 | Image<T>::Image() | 
|---|
| 26 | // Constructeur par defaut. | 
|---|
| 27 | : TMatrix<T>() | 
|---|
| 28 | { | 
|---|
| 29 | SetOrg(); | 
|---|
| 30 | SetPixelSize(); | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | //! Constructor, with specification of the image size, pixel size and offsets | 
|---|
| 34 | template <class T> | 
|---|
| 35 | Image<T>::Image(sa_size_t sizx, sa_size_t sizy, r_8 szpx, r_8 szpy, r_8 orgx, r_8 orgy) | 
|---|
| 36 | // Constructeur par defaut. | 
|---|
| 37 | : TMatrix<T>(sizy, sizx) | 
|---|
| 38 | { | 
|---|
| 39 | SetOrg(orgx, orgy); | 
|---|
| 40 | SetPixelSize(szpx, szpy); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | //! Copy constructor | 
|---|
| 44 | template <class T> | 
|---|
| 45 | Image<T>::Image(const Image<T>& a) | 
|---|
| 46 | : TMatrix<T>(a) | 
|---|
| 47 | { | 
|---|
| 48 | SetOrg(a.XOrg(), a.YOrg() ); | 
|---|
| 49 | SetPixelSize(a.XPixSize(), a.YPixSize()); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | //! Copy constructor with possibility of duplicating the pixel array | 
|---|
| 54 | /*! | 
|---|
| 55 | \param share : if true, the data is shared, duplicated if false. | 
|---|
| 56 | */ | 
|---|
| 57 | template <class T> | 
|---|
| 58 | Image<T>::Image(const Image<T>& a, bool share) | 
|---|
| 59 | : TMatrix<T>(a, share) | 
|---|
| 60 | { | 
|---|
| 61 | SetOrg(a.XOrg(), a.YOrg() ); | 
|---|
| 62 | SetPixelSize(a.XPixSize(), a.YPixSize()); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | //! Destructor | 
|---|
| 66 | template <class T> | 
|---|
| 67 | Image<T>::~Image() | 
|---|
| 68 | { | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 |  | 
|---|
| 72 | // -------------------------------------------------------- | 
|---|
| 73 | //   Les objets delegues pour la gestion de persistance | 
|---|
| 74 | // -------------------------------------------------------- | 
|---|
| 75 | /*! | 
|---|
| 76 | \class SOPHYA::FIO_Image | 
|---|
| 77 | \ingroup Image | 
|---|
| 78 | Class for persistent management of Image | 
|---|
| 79 |  | 
|---|
| 80 | */ | 
|---|
| 81 | /////////////////////////////////////////////////////////// | 
|---|
| 82 |  | 
|---|
| 83 | //! Default constructor | 
|---|
| 84 | template <class T> | 
|---|
| 85 | FIO_Image<T>::FIO_Image() | 
|---|
| 86 | : FIO_TArray<T>() | 
|---|
| 87 | { | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | //! Constructor from the file \b filename | 
|---|
| 92 | template <class T> | 
|---|
| 93 | FIO_Image<T>::FIO_Image(string const & filename) | 
|---|
| 94 | : FIO_TArray<T>(filename) | 
|---|
| 95 | { | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | //! Constructor from the Image \b obj | 
|---|
| 99 | template <class T> | 
|---|
| 100 | FIO_Image<T>::FIO_Image(const Image<T> & obj) | 
|---|
| 101 | : FIO_TArray<T>() | 
|---|
| 102 | { | 
|---|
| 103 | this->dobj = new Image<T>(obj, true); | 
|---|
| 104 | this->ownobj=true; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | //! Connect with a Image \b obj | 
|---|
| 108 | template <class T> | 
|---|
| 109 | FIO_Image<T>::FIO_Image(Image<T> * obj) | 
|---|
| 110 | : FIO_TArray<T>(obj) | 
|---|
| 111 | { | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 | //! Connect Image \b o | 
|---|
| 116 | template <class T> | 
|---|
| 117 | void FIO_Image<T>::SetDataObj(AnyDataObj & o) | 
|---|
| 118 | { | 
|---|
| 119 | Image<T> * po = dynamic_cast< Image<T> * >(&o); | 
|---|
| 120 | if (po == NULL) { | 
|---|
| 121 | char buff[160]; | 
|---|
| 122 | sprintf(buff,"FIO_Image<%s>::SetDataObj(%s) - Object type  error ! ", | 
|---|
| 123 | DataTypeInfo<T>::getTypeName().c_str(), typeid(o).name()); | 
|---|
| 124 | throw TypeMismatchExc(PExcLongMessage(buff)); | 
|---|
| 125 | } | 
|---|
| 126 | if (this->ownobj && this->dobj) delete this->dobj; | 
|---|
| 127 | this->dobj = po; this->ownobj = false; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | template <class T> | 
|---|
| 131 | void FIO_Image<T>::ReadSelf(PInPersist& is) | 
|---|
| 132 | { | 
|---|
| 133 | if (this->dobj == NULL) this->dobj = new Image<T>; | 
|---|
| 134 | Image<T> * img = dynamic_cast<Image<T> * > (this->dobj); | 
|---|
| 135 | // On lit les 3 premiers uint_4 | 
|---|
| 136 | //  0: Numero de version,  1 : reserve | 
|---|
| 137 | uint_4 itab[3]; | 
|---|
| 138 | is.Get(itab,3); | 
|---|
| 139 |  | 
|---|
| 140 | // Image<T> part data | 
|---|
| 141 | r_8 orgx, orgy; | 
|---|
| 142 | is.Get(orgx); | 
|---|
| 143 | is.Get(orgy); | 
|---|
| 144 | img->SetOrg(orgx, orgy); | 
|---|
| 145 | r_8 szx, szy; | 
|---|
| 146 | is.Get(szx); | 
|---|
| 147 | is.Get(szy); | 
|---|
| 148 | img->SetPixelSize(szx, szy); | 
|---|
| 149 |  | 
|---|
| 150 | // Reading the TArray part | 
|---|
| 151 | FIO_TArray<T>::ReadSelf(is); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | template <class T> | 
|---|
| 155 | void FIO_Image<T>::WriteSelf(POutPersist& os) const | 
|---|
| 156 | { | 
|---|
| 157 | if (this->dobj == NULL)   return; | 
|---|
| 158 | Image<T> * img = dynamic_cast<Image<T> * > (this->dobj); | 
|---|
| 159 | //  On ecrit 3 uint_4 .... | 
|---|
| 160 | uint_4 itab[3]; | 
|---|
| 161 | itab[0] = 1;  // Numero de version a 1 | 
|---|
| 162 | itab[1] = 0; | 
|---|
| 163 | itab[2] = 0; | 
|---|
| 164 | os.Put(itab,3); | 
|---|
| 165 |  | 
|---|
| 166 | // Image<T> part data | 
|---|
| 167 | os.Put(img->XOrg()); | 
|---|
| 168 | os.Put(img->XOrg()); | 
|---|
| 169 | os.Put(img->XPixSize()); | 
|---|
| 170 | os.Put(img->YPixSize()); | 
|---|
| 171 |  | 
|---|
| 172 | // Writing the TArray part | 
|---|
| 173 | FIO_TArray<T>::WriteSelf(os); | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | /////////////////////////////////////////////////////////////// | 
|---|
| 177 | #ifdef __CXX_PRAGMA_TEMPLATES__ | 
|---|
| 178 | #pragma define_template Image<uint_2> | 
|---|
| 179 | #pragma define_template Image<int_2> | 
|---|
| 180 | #pragma define_template Image<int_4> | 
|---|
| 181 | #pragma define_template Image<int_8> | 
|---|
| 182 | #pragma define_template Image<r_4> | 
|---|
| 183 | #pragma define_template Image<r_8> | 
|---|
| 184 | #pragma define_template Image< complex<r_4> > | 
|---|
| 185 | #pragma define_template Image< complex<r_8> > | 
|---|
| 186 |  | 
|---|
| 187 | #pragma define_template FIO_Image<uint_2> | 
|---|
| 188 | #pragma define_template FIO_Image<int_2> | 
|---|
| 189 | #pragma define_template FIO_Image<int_4> | 
|---|
| 190 | #pragma define_template FIO_Image<int_8> | 
|---|
| 191 | #pragma define_template FIO_Image<r_4> | 
|---|
| 192 | #pragma define_template FIO_Image<r_8> | 
|---|
| 193 | #pragma define_template FIO_Image< complex<r_4> > | 
|---|
| 194 | #pragma define_template FIO_Image< complex<r_8> > | 
|---|
| 195 | #endif | 
|---|
| 196 |  | 
|---|
| 197 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES) | 
|---|
| 198 | namespace SOPHYA { | 
|---|
| 199 | template class Image<uint_2>; | 
|---|
| 200 | template class Image<int_2>; | 
|---|
| 201 | template class Image<int_4>; | 
|---|
| 202 | template class Image<int_8>; | 
|---|
| 203 | template class Image<r_4>; | 
|---|
| 204 | template class Image<r_8>; | 
|---|
| 205 | template class Image< complex<r_4> >; | 
|---|
| 206 | template class Image< complex<r_8> >; | 
|---|
| 207 |  | 
|---|
| 208 | template class FIO_Image<uint_2>; | 
|---|
| 209 | template class FIO_Image<int_2>; | 
|---|
| 210 | template class FIO_Image<int_4>; | 
|---|
| 211 | template class FIO_Image<int_8>; | 
|---|
| 212 | template class FIO_Image<r_4>; | 
|---|
| 213 | template class FIO_Image<r_8>; | 
|---|
| 214 | template class FIO_Image< complex<r_4> >; | 
|---|
| 215 | template class FIO_Image< complex<r_8> >; | 
|---|
| 216 | } | 
|---|
| 217 | #endif | 
|---|