|
Isis 3.0 Object Programmers' Reference |
Home |
00001 00024 #include "PixelType.h" 00025 #include "Buffer.h" 00026 #include "iException.h" 00027 #include "Message.h" 00028 00029 #include <iostream> 00030 00031 using namespace std; 00032 namespace Isis { 00033 00040 Buffer::Buffer() : p_sample(0), p_nsamps(0), p_line(0), p_nlines(0), 00041 p_band(0), p_nbands(0), p_npixels(0), p_buf(0), 00042 p_pixelType(None), p_rawbuf(0) { } 00043 00055 Buffer::Buffer(const int nsamps, const int nlines, 00056 const int nbands, const Isis::PixelType type) : 00057 p_nsamps(nsamps), p_nlines(nlines), 00058 p_nbands(nbands), p_pixelType(type) { 00059 00060 p_sample = p_line = p_band = 0; 00061 00062 if (p_nsamps <= 0) { 00063 string message = "Invalid value for sample dimensions (nsamps)"; 00064 throw Isis::iException::Message(Isis::iException::Programmer,message,_FILEINFO_); 00065 } 00066 if (p_nlines <= 0) { 00067 string message = "Invalid value for line dimensions (nlines)"; 00068 throw Isis::iException::Message(Isis::iException::Programmer,message,_FILEINFO_); 00069 } 00070 if (p_nbands <= 0) { 00071 string message = "Invalid value for band dimensions (nbands)"; 00072 throw Isis::iException::Message(Isis::iException::Programmer,message,_FILEINFO_); 00073 } 00074 00075 p_npixels = p_nsamps * p_nlines * p_nbands; 00076 00077 Allocate(); 00078 } 00079 00081 Buffer::~Buffer() { 00082 try { 00083 if (p_buf) { 00084 delete [] p_buf; 00085 } 00086 if (p_rawbuf) { 00087 delete [] (char *) p_rawbuf; 00088 } 00089 } catch (...) { 00090 00091 } 00092 } 00093 00100 Buffer::Buffer &Buffer::operator=(const double &d) { 00101 for (int i = 0 ; i < p_npixels ; i++) { p_buf[i] = d; } 00102 return (*this); 00103 } 00104 00114 void Buffer::SetBasePosition(const int start_sample, 00115 const int start_line, 00116 const int start_band) { 00117 SetBaseSample(start_sample); 00118 SetBaseLine(start_line); 00119 SetBaseBand(start_band); 00120 } 00121 00134 int Buffer::Sample (const int index) const { 00135 return (index % p_nsamps) + p_sample; 00136 } 00137 00151 int Buffer::Line (const int index) const { 00152 int sub_index = index % (p_nsamps * p_nlines); 00153 return sub_index / p_nsamps + p_line; 00154 } 00155 00167 int Buffer::Band (const int index) const { 00168 return index / (p_nsamps * p_nlines) + p_band; 00169 } 00170 00181 void Buffer::Position (const int index, int &i_samp, int &i_line, 00182 int &i_band) const { 00183 i_samp = Sample(index); 00184 i_line = Line(index); 00185 i_band = Band(index); 00186 } 00187 00200 int Buffer::Index(const int i_samp, const int i_line, const int i_band) 00201 const throw(Isis::iException &) { 00202 00203 if ((i_samp < p_sample) || (i_samp > (p_sample+p_nsamps-1))) { 00204 string message = Isis::Message::ArraySubscriptNotInRange(i_samp); 00205 throw Isis::iException::Message(Isis::iException::Programmer,message,_FILEINFO_); 00206 } 00207 00208 if ((i_line < p_line) || (i_line > (p_line+p_nlines-1))) { 00209 string message = Isis::Message::ArraySubscriptNotInRange(i_line); 00210 throw Isis::iException::Message(Isis::iException::Programmer,message,_FILEINFO_); 00211 } 00212 00213 if ((i_band < p_band) || (i_band > (p_band+p_nbands-1))) { 00214 string message = Isis::Message::ArraySubscriptNotInRange(i_band); 00215 throw Isis::iException::Message(Isis::iException::Programmer,message,_FILEINFO_); 00216 } 00217 00218 // Got a valid reference location so compute the index and return 00219 int index = (i_band - p_band) * (p_nlines * p_nsamps) + 00220 (i_line - p_line) * (p_nsamps) + 00221 (i_samp - p_sample); 00222 return (index); 00223 } 00224 00234 double Buffer::at(const int index) const { 00235 if (index < 0) { 00236 string message = Isis::Message::ArraySubscriptNotInRange(index); 00237 throw Isis::iException::Message(Isis::iException::Programmer,message,_FILEINFO_); 00238 } 00239 else if (index >= p_npixels) { 00240 string message = Isis::Message::ArraySubscriptNotInRange(index); 00241 throw Isis::iException::Message(Isis::iException::Programmer,message,_FILEINFO_); 00242 } 00243 00244 return p_buf[index]; 00245 } 00246 00255 void Buffer::Copy (const Buffer &in) { 00256 if (p_npixels != in.size()) { 00257 string message = "Input and output buffers are not the same size"; 00258 throw Isis::iException::Message(Isis::iException::Programmer,message,_FILEINFO_); 00259 } 00260 00261 size_t n = sizeof(double); 00262 n = n * (size_t) p_npixels; 00263 memcpy (p_buf, in.p_buf, n); 00264 00265 n = Isis::SizeOf(p_pixelType); 00266 n = n * (size_t) p_npixels; 00267 memcpy (p_rawbuf, in.p_rawbuf, n); 00268 } 00269 00276 Buffer::Buffer (const Buffer &rhs) : 00277 p_nsamps(rhs.p_nsamps), p_nlines(rhs.p_nlines), 00278 p_nbands(rhs.p_nbands), p_pixelType(rhs.p_pixelType) { 00279 00280 p_sample = rhs.p_sample; 00281 p_line = rhs.p_line; 00282 p_band = rhs.p_band; 00283 00284 p_npixels = rhs.p_npixels; 00285 00286 Allocate(); 00287 Copy (rhs); 00288 } 00289 00295 void Buffer::Allocate () { 00296 p_buf = NULL; 00297 p_rawbuf = NULL; 00298 try { 00299 p_buf = new double [p_npixels]; 00300 size_t n = Isis::SizeOf(p_pixelType); 00301 n = n * (size_t) p_npixels; 00302 p_rawbuf = new char[n]; 00303 } 00304 catch (...) { 00305 try { 00306 if(p_buf) { 00307 delete [] p_buf; 00308 p_buf = NULL; 00309 } 00310 if(p_rawbuf) { 00311 delete [] (char *)p_rawbuf; 00312 p_rawbuf = NULL; 00313 } 00314 } catch(...) { 00315 p_buf = NULL; 00316 p_rawbuf = NULL; 00317 } 00318 string message = Isis::Message::MemoryAllocationFailed(); 00319 throw Isis::iException::Message(Isis::iException::System,message,_FILEINFO_); 00320 } 00321 } 00322 }