Isis 3 Programmer Reference
Buffer.cpp
Go to the documentation of this file.
1 
24 #include "PixelType.h"
25 #include "Buffer.h"
26 #include "IException.h"
27 #include "Message.h"
28 
29 #include <iostream>
30 
31 using namespace std;
32 
33 namespace Isis {
34 
41  Buffer::Buffer() : p_sample(0), p_nsamps(0), p_line(0), p_nlines(0),
42  p_band(0), p_nbands(0), p_npixels(0), p_buf(0),
43  p_pixelType(None), p_rawbuf(0) { }
44 
45 
57  Buffer::Buffer(const int nsamps, const int nlines,
58  const int nbands, const Isis::PixelType type) :
59  p_nsamps(nsamps), p_nlines(nlines),
60  p_nbands(nbands), p_pixelType(type) {
61 
62  p_sample = p_line = p_band = 0;
63 
64  if(p_nsamps <= 0) {
65  string message = "Invalid value for sample dimensions (nsamps)";
67  }
68  if(p_nlines <= 0) {
69  string message = "Invalid value for line dimensions (nlines)";
71  }
72  if(p_nbands <= 0) {
73  string message = "Invalid value for band dimensions (nbands)";
75  }
76 
78 
79  Allocate();
80  }
81 
82 
85  try {
86  if(p_buf) {
87  delete [] p_buf;
88  }
89  if(p_rawbuf) {
90  delete [](char *) p_rawbuf;
91  }
92  }
93  catch(...) {
94 
95  }
96  }
97 
98 
105  Buffer &Buffer::operator=(const double &d) {
106  for(int i = 0 ; i < p_npixels ; i++) {
107  p_buf[i] = d;
108  }
109  return (*this);
110  }
111 
112 
122  void Buffer::SetBasePosition(const int start_sample,
123  const int start_line,
124  const int start_band) {
125  SetBaseSample(start_sample);
126  SetBaseLine(start_line);
127  SetBaseBand(start_band);
128  }
129 
130 
143  int Buffer::Sample(const int index) const {
144  return (index % p_nsamps) + p_sample;
145  }
146 
147 
161  int Buffer::Line(const int index) const {
162  int sub_index = index % (p_nsamps * p_nlines);
163  return sub_index / p_nsamps + p_line;
164  }
165 
166 
178  int Buffer::Band(const int index) const {
179  return index / (p_nsamps * p_nlines) + p_band;
180  }
181 
182 
193  void Buffer::Position(const int index, int &i_samp, int &i_line,
194  int &i_band) const {
195  i_samp = Sample(index);
196  i_line = Line(index);
197  i_band = Band(index);
198  }
199 
200 
213  int Buffer::Index(const int i_samp, const int i_line, const int i_band)
214  const {
215  if((i_samp < p_sample) || (i_samp > (p_sample + p_nsamps - 1))) {
216  QString message = Message::ArraySubscriptNotInRange(i_samp);
218  }
219 
220  if((i_line < p_line) || (i_line > (p_line + p_nlines - 1))) {
221  QString message = Message::ArraySubscriptNotInRange(i_line);
223  }
224 
225  if((i_band < p_band) || (i_band > (p_band + p_nbands - 1))) {
226  QString message = Message::ArraySubscriptNotInRange(i_band);
228  }
229 
230  // Got a valid reference location so compute the index and return
231  int index = (i_band - p_band) * (p_nlines * p_nsamps) +
232  (i_line - p_line) * (p_nsamps) +
233  (i_samp - p_sample);
234  return (index);
235  }
236 
237 
247  double Buffer::at(const int index) const {
248  if(index < 0) {
249  QString message = Message::ArraySubscriptNotInRange(index);
251  }
252  else if(index >= p_npixels) {
253  QString message = Message::ArraySubscriptNotInRange(index);
255  }
256 
257  return p_buf[index];
258  }
259 
260 
271  void Buffer::Copy(const Buffer &in, bool includeRawBuf) {
272  if(p_npixels != in.size()) {
273  string message = "Input and output buffers are not the same size";
275  }
276 
277  if(includeRawBuf && p_pixelType != in.PixelType()) {
278  string message = "Input and output buffers are not the same pixel type";
280  }
281 
282  size_t n = sizeof(double);
283  n = n * (size_t) p_npixels;
284  memcpy(p_buf, in.p_buf, n);
285 
286  if (includeRawBuf) {
288  n = n * (size_t) p_npixels;
289  memcpy(p_rawbuf, in.p_rawbuf, n);
290  }
291  }
292 
293 
301  bool Buffer::CopyOverlapFrom(const Buffer &in) {
302  bool isSubareaOfIn = (p_npixels <= in.size());
303  isSubareaOfIn &= (p_sample >= in.p_sample);
304  isSubareaOfIn &= (p_line >= in.p_line);
305  isSubareaOfIn &= (p_band >= in.p_band);
306 
307  int endSample = p_sample + p_nsamps - 1;
308  int otherEndSample = in.p_sample + in.p_nsamps - 1;
309 
310  int endLine = p_line + p_nlines - 1;
311  int otherEndLine = in.p_line + in.p_nlines - 1;
312 
313  int endBand = p_band + p_nbands - 1;
314  int otherEndBand = in.p_band + in.p_nbands - 1;
315 
316  isSubareaOfIn &= (endSample <= otherEndSample);
317  isSubareaOfIn &= (endLine <= otherEndLine);
318  isSubareaOfIn &= (endBand <= otherEndBand);
319 
320  if (isSubareaOfIn) {
321  for (int i = 0; i < size(); i++) {
322  (*this)[i] = in[in.Index(Sample(i), Line(i), Band(i))];
323  }
324  }
325 
326  return isSubareaOfIn;
327  }
328 
329 
336  Buffer::Buffer(const Buffer &rhs) :
337  p_nsamps(rhs.p_nsamps), p_nlines(rhs.p_nlines),
338  p_nbands(rhs.p_nbands), p_pixelType(rhs.p_pixelType) {
339 
340  p_sample = rhs.p_sample;
341  p_line = rhs.p_line;
342  p_band = rhs.p_band;
343 
344  p_npixels = rhs.p_npixels;
345 
346  Allocate();
347  Copy(rhs);
348  }
349 
350 
357  p_buf = NULL;
358  p_rawbuf = NULL;
359  try {
360  p_buf = new double [p_npixels];
361  size_t n = Isis::SizeOf(p_pixelType);
362  n = n * (size_t) p_npixels;
363  p_rawbuf = new char[n];
364  }
365  catch(...) {
366  try {
367  if(p_buf) {
368  delete [] p_buf;
369  p_buf = NULL;
370  }
371  if(p_rawbuf) {
372  delete [](char *)p_rawbuf;
373  p_rawbuf = NULL;
374  }
375  }
376  catch(...) {
377  p_buf = NULL;
378  p_rawbuf = NULL;
379  }
380  QString message = Message::MemoryAllocationFailed();
381  throw IException(IException::Unknown, message, _FILEINFO_);
382  }
383  }
384 }
void Allocate()
Size or resize the memory buffer.
Definition: Buffer.cpp:356
Buffer for reading and writing cube data.
Definition: Buffer.h:69
int Band(const int index=0) const
Returns the band position associated with a shape buffer index.
Definition: Buffer.cpp:178
void SetBaseBand(const int start_band)
This method is used to set the base band position of the shape buffer.
Definition: Buffer.h:209
const Isis::PixelType p_pixelType
The pixel type of the raw buffer.
Definition: Buffer.h:227
void * p_rawbuf
The raw dm read from the disk.
Definition: Buffer.h:228
int SizeOf(Isis::PixelType pixelType)
Returns the number of bytes of the specified PixelType.
Definition: PixelType.h:62
void SetBasePosition(const int start_sample, const int start_line, const int start_band)
This method is used to set the base position of the shape buffer.
Definition: Buffer.cpp:122
double at(const int index) const
Returns the value in the shape buffer at the given index.
Definition: Buffer.cpp:247
Namespace for the standard library.
void Copy(const Buffer &in, bool includeRawBuf=true)
Allows copying of the buffer contents to another Buffer.
Definition: Buffer.cpp:271
int p_line
Starting line to read/write.
Definition: Buffer.h:217
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
int size() const
Returns the total number of pixels in the shape buffer.
Definition: Buffer.h:113
QString MemoryAllocationFailed()
This error should be used when an error accrues during a memory allocation such as "new"...
PixelType
Enumerations for Isis Pixel Types.
Definition: PixelType.h:43
~Buffer()
Destroys the Buffer object and frees shape buffer.
Definition: Buffer.cpp:84
int p_sample
Starting sample to read/write.
Definition: Buffer.h:214
int p_npixels
Number of pixels (nsamps * nlines * nbands)
Definition: Buffer.h:223
int Sample(const int index=0) const
Returns the sample position associated with a shape buffer index.
Definition: Buffer.cpp:143
Isis::PixelType PixelType() const
Returns the raw buffer pixel type.
Definition: Buffer.h:176
double * p_buf
Shape buffer allocated to the size of npixels for handling reads/writes.
Definition: Buffer.h:224
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A type of error that cannot be classified as any of the other error types.
Definition: IException.h:134
int Line(const int index=0) const
Returns the line position associated with a shape buffer index.
Definition: Buffer.cpp:161
int p_nbands
Number of bands to read/write.
Definition: Buffer.h:221
void SetBaseSample(const int start_samp)
This method is used to set the base sample position of the shape buffer.
Definition: Buffer.h:189
int p_band
Starting band to read/write.
Definition: Buffer.h:220
int p_nlines
Number of lines to read/write.
Definition: Buffer.h:218
void SetBaseLine(const int start_line)
This method is used to set the base line position of the shape buffer.
Definition: Buffer.h:199
QString ArraySubscriptNotInRange(int index)
This error should be used when an Isis object or application is checking array bounds and the legal r...
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
Buffer()
Default constructor for proper initialization purposes.
Definition: Buffer.cpp:41
int Index(const int i_samp, const int i_line, const int i_band) const
Given a sample, line, and band position, this returns the appropriate index in the shape buffer...
Definition: Buffer.cpp:213
void Position(const int index, int &i_samp, int &i_line, int &i_band) const
Returns the sample, line, and band position associated with a shape buffer index. ...
Definition: Buffer.cpp:193
int p_nsamps
Number of samples to read/write.
Definition: Buffer.h:215
bool CopyOverlapFrom(const Buffer &in)
Allows copying of the buffer contents of a larger buffer to another same size or smaller Buffer...
Definition: Buffer.cpp:301
Buffer & operator=(const double &d)
Assign the entire buffer to a constant double value.
Definition: Buffer.cpp:105