Isis 3.0 Programmer Reference
Back | Home
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 
269  void Buffer::Copy(const Buffer &in, bool includeRawBuf) {
270  if(p_npixels != in.size()) {
271  string message = "Input and output buffers are not the same size";
273  }
274 
275  if(includeRawBuf && p_pixelType != in.PixelType()) {
276  string message = "Input and output buffers are not the same pixel type";
278  }
279 
280  size_t n = sizeof(double);
281  n = n * (size_t) p_npixels;
282  memcpy(p_buf, in.p_buf, n);
283 
284  if (includeRawBuf) {
286  n = n * (size_t) p_npixels;
287  memcpy(p_rawbuf, in.p_rawbuf, n);
288  }
289  }
290 
291 
299  bool Buffer::CopyOverlapFrom(const Buffer &in) {
300  bool isSubareaOfIn = (p_npixels <= in.size());
301  isSubareaOfIn &= (p_sample >= in.p_sample);
302  isSubareaOfIn &= (p_line >= in.p_line);
303  isSubareaOfIn &= (p_band >= in.p_band);
304 
305  int endSample = p_sample + p_nsamps - 1;
306  int otherEndSample = in.p_sample + in.p_nsamps - 1;
307 
308  int endLine = p_line + p_nlines - 1;
309  int otherEndLine = in.p_line + in.p_nlines - 1;
310 
311  int endBand = p_band + p_nbands - 1;
312  int otherEndBand = in.p_band + in.p_nbands - 1;
313 
314  isSubareaOfIn &= (endSample <= otherEndSample);
315  isSubareaOfIn &= (endLine <= otherEndLine);
316  isSubareaOfIn &= (endBand <= otherEndBand);
317 
318  if (isSubareaOfIn) {
319  for (int i = 0; i < size(); i++) {
320  (*this)[i] = in[in.Index(Sample(i), Line(i), Band(i))];
321  }
322  }
323 
324  return isSubareaOfIn;
325  }
326 
327 
334  Buffer::Buffer(const Buffer &rhs) :
335  p_nsamps(rhs.p_nsamps), p_nlines(rhs.p_nlines),
336  p_nbands(rhs.p_nbands), p_pixelType(rhs.p_pixelType) {
337 
338  p_sample = rhs.p_sample;
339  p_line = rhs.p_line;
340  p_band = rhs.p_band;
341 
342  p_npixels = rhs.p_npixels;
343 
344  Allocate();
345  Copy(rhs);
346  }
347 
348 
355  p_buf = NULL;
356  p_rawbuf = NULL;
357  try {
358  p_buf = new double [p_npixels];
359  size_t n = Isis::SizeOf(p_pixelType);
360  n = n * (size_t) p_npixels;
361  p_rawbuf = new char[n];
362  }
363  catch(...) {
364  try {
365  if(p_buf) {
366  delete [] p_buf;
367  p_buf = NULL;
368  }
369  if(p_rawbuf) {
370  delete [](char *)p_rawbuf;
371  p_rawbuf = NULL;
372  }
373  }
374  catch(...) {
375  p_buf = NULL;
376  p_rawbuf = NULL;
377  }
378  QString message = Message::MemoryAllocationFailed();
379  throw IException(IException::Unknown, message, _FILEINFO_);
380  }
381  }
382 }
void Allocate()
Size or resize the memory buffer.
Definition: Buffer.cpp:354
Buffer for reading and writing cube data.
Definition: Buffer.h:68
int Line(const int index=0) const
Returns the line position associated with a shape buffer index.
Definition: Buffer.cpp:161
void SetBaseBand(const int start_band)
This method is used to set the base band position of the shape buffer.
Definition: Buffer.h:208
const Isis::PixelType p_pixelType
The pixel type of the raw buffer.
Definition: Buffer.h:226
void * p_rawbuf
The raw dm read from the disk.
Definition: Buffer.h:227
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
void Copy(const Buffer &in, bool includeRawBuf=true)
Allows copying of the buffer contents to another Buffer.
Definition: Buffer.cpp:269
int p_line
Starting line to read/write.
Definition: Buffer.h:216
int Sample(const int index=0) const
Returns the sample position associated with a shape buffer index.
Definition: Buffer.cpp:143
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:154
QString MemoryAllocationFailed()
This error should be used when an error accrues during a memory allocation such as &quot;new&quot;...
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:213
int p_npixels
Number of pixels (nsamps * nlines * nbands)
Definition: Buffer.h:222
int Band(const int index=0) const
Returns the band position associated with a shape buffer index.
Definition: Buffer.cpp:178
double * p_buf
Shape buffer allocated to the size of npixels for handling reads/writes.
Definition: Buffer.h:223
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:38
A type of error that cannot be classified as any of the other error types.
Definition: IException.h:126
Isis::PixelType PixelType() const
Returns the raw buffer pixel type.
Definition: Buffer.h:175
int p_nbands
Number of bands to read/write.
Definition: Buffer.h:220
void SetBaseSample(const int start_samp)
This method is used to set the base sample position of the shape buffer.
Definition: Buffer.h:188
int p_band
Starting band to read/write.
Definition: Buffer.h:219
int p_nlines
Number of lines to read/write.
Definition: Buffer.h:217
void SetBaseLine(const int start_line)
This method is used to set the base line position of the shape buffer.
Definition: Buffer.h:198
QString ArraySubscriptNotInRange(int index)
This error should be used when an Isis object or application is checking array bounds and the legal r...
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
int size() const
Returns the total number of pixels in the shape buffer.
Definition: Buffer.h:112
Isis exception class.
Definition: IException.h:99
Buffer()
Default constructor for proper initialization purposes.
Definition: Buffer.cpp:41
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:214
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:299
Buffer & operator=(const double &d)
Assign the entire buffer to a constant double value.
Definition: Buffer.cpp:105
double at(const int index) const
Returns the value in the shape buffer at the given index.
Definition: Buffer.cpp:247

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:14:55