00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "CSPadPixCoords/Image2D.h"
00017
00018
00019
00020
00021
00022 #include <stdint.h>
00023 #include <iostream>
00024 #include <fstream>
00025
00026 using namespace std;
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 namespace CSPadPixCoords {
00041
00042
00043
00044
00045
00046
00047 template <typename T>
00048 Image2D<T>::Image2D (const T* data, size_t nrows, size_t ncols) :
00049 m_data(data),
00050 m_nrows(nrows),
00051 m_ncols(ncols)
00052 {
00053
00054 m_nrows_transposed = ncols;
00055 m_ncols_transposed = nrows;
00056 }
00057
00058
00059
00060 template <typename T>
00061 inline
00062 T Image2D<T>::getValue (int row, int col)
00063 {
00064 return m_data[row*m_ncols + col];
00065
00066 }
00067
00068
00069
00070 template <typename T>
00071 inline
00072 T Image2D<T>::flipud (int row, int col)
00073 {
00074 return getValue(m_nrows-row-1, col);
00075 }
00076
00077
00078
00079 template <typename T>
00080 inline
00081 T Image2D<T>::fliplr (int row, int col)
00082 {
00083 return getValue(row, m_ncols-col-1);
00084 }
00085
00086
00087
00088 template <typename T>
00089 inline
00090 T Image2D<T>::transpose (int row, int col)
00091 {
00092 int col_transposed = row;
00093 int row_transposed = col;
00094
00095 if (col < (int)m_ncols_transposed and row < (int)m_nrows_transposed )
00096 return getValue(row_transposed, col_transposed);
00097 else
00098 {
00099 cout << "Image2D<T>::transpose: "
00100 << " row=" << row_transposed
00101 << " col=" << col_transposed
00102 << " nrows=" << m_nrows_transposed
00103 << " ncols=" << m_ncols_transposed
00104 << cout;
00105 return 0;
00106 }
00107 }
00108
00109
00110
00111 template <typename T>
00112 inline
00113 T Image2D<T>::rot000 (int row, int col)
00114 {
00115 return getValue(row,col);
00116 }
00117
00118
00119
00120 template <typename T>
00121 inline
00122 T Image2D<T>::rot090 (int row, int col)
00123 {
00124 int col_transposed = row;
00125 int row_transposed = col;
00126 return getValue(row_transposed, m_ncols-col_transposed-1);
00127 }
00128
00129
00130
00131 template <typename T>
00132 inline
00133 T Image2D<T>::rot270 (int row, int col)
00134 {
00135 int col_transposed = row;
00136 int row_transposed = col;
00137 return getValue(m_nrows-row_transposed-1, col_transposed);
00138 }
00139
00140
00141
00142 template <typename T>
00143 inline
00144 T Image2D<T>::rot180 (int row, int col)
00145 {
00146 return getValue(m_nrows-row-1, m_ncols-col-1);
00147 }
00148
00149
00150
00151 template <typename T>
00152 inline
00153 T Image2D<T>::rotN90 (int row, int col, int N)
00154 {
00155 if (N==0) return rot000(row,col);
00156 else if (N==1) return rot090(row,col);
00157 else if (N==2) return rot180(row,col);
00158 else if (N==3) return rot270(row,col);
00159 else return rot000(row,col);
00160 }
00161
00162
00163
00164 template <typename T>
00165 inline
00166 size_t Image2D<T>::getNCols(int Nx90)
00167 {
00168 if ( (Nx90+2)%2 == 0 ) return m_ncols;
00169 else return m_nrows;
00170 }
00171
00172
00173
00174 template <typename T>
00175 inline
00176 size_t Image2D<T>::getNRows(int Nx90)
00177 {
00178 if ( (Nx90+2)%2 == 0 ) return m_nrows;
00179 else return m_ncols;
00180 }
00181
00182
00183
00184
00185 template <typename T>
00186 void Image2D<T>::printImage (int Nx90)
00187 {
00188 cout << "Image2D<T>: Rotation by 90*" << Nx90 << "=" << Nx90*90 << " degree" << endl;
00189
00190 cout << " ncols=" << this->getNCols(Nx90)
00191 << " nrows=" << this->getNRows(Nx90)
00192 << endl;
00193
00194 for (size_t row = 0; row < getNRows(Nx90); row+=20) {
00195 for (size_t col = 0; col < getNCols(Nx90); col+=20) {
00196
00197 cout << this->rotN90 (row,col,Nx90) << " ";
00198 }
00199 cout << endl;
00200 }
00201 }
00202
00203
00204
00205 template <typename T>
00206 void Image2D<T>::printEntireImage (int Nx90)
00207 {
00208 cout << "Image2D<T>: Rotation by 90*" << Nx90 << "=" << Nx90*90 << " degree" << endl;
00209
00210 cout << " ncols=" << this->getNCols(Nx90)
00211 << " nrows=" << this->getNRows(Nx90)
00212 << endl;
00213
00214 for (size_t row = 0; row < getNRows(Nx90); row++) {
00215 for (size_t col = 0; col < getNCols(Nx90); col++) {
00216
00217 cout << this->rotN90 (row,col,Nx90) << " ";
00218 }
00219 cout << endl;
00220 }
00221 }
00222
00223
00224
00225 template <typename T>
00226 void Image2D<T>::saveImageInFile (const std::string &fname, int Nx90)
00227 {
00228 cout << "Image2D<T>::saveImageInFile: ";
00229
00230 ofstream file;
00231 file.open(fname.c_str(),ios_base::out);
00232
00233 for (size_t row = 0; row < getNRows(Nx90); row++) {
00234 for (size_t col = 0; col < getNCols(Nx90); col++) {
00235
00236 file << this->rotN90 (row,col,Nx90) << " ";
00237 }
00238 file << endl;
00239 }
00240
00241 file.close();
00242 cout << "The 2x1 image (ncols,nrows="
00243 << this->getNCols(Nx90) << ","
00244 << this->getNRows(Nx90)
00245 << " with rotation by 90*" << Nx90 << "=" << Nx90*90 << " degree)"
00246 << " is saved in file " << fname << endl;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255 template <typename T>
00256 Image2D<T>::~Image2D ()
00257 {
00258
00259 }
00260
00261
00262
00263
00264
00265
00266 template class CSPadPixCoords::Image2D<uint16_t>;
00267 template class CSPadPixCoords::Image2D<int16_t>;
00268 template class CSPadPixCoords::Image2D<double>;
00269 template class CSPadPixCoords::Image2D<float>;
00270 template class CSPadPixCoords::Image2D<int>;
00271 template class CSPadPixCoords::Image2D<uint8_t>;
00272
00273
00274 }
00275
00276