Isis 3 Programmer Reference
Buffer.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7
8#include "PixelType.h"
9#include "Buffer.h"
10#include "IException.h"
11#include "Message.h"
12
13#include <iostream>
14
15using namespace std;
16
17namespace Isis {
18
25 Buffer::Buffer() : p_sample(0), p_nsamps(0), p_line(0), p_nlines(0),
26 p_band(0), p_nbands(0), p_npixels(0), p_buf(0),
27 p_pixelType(None), p_rawbuf(0) { }
28
29
41 Buffer::Buffer(const int nsamps, const int nlines,
42 const int nbands, const Isis::PixelType type) :
43 p_nsamps(nsamps), p_nlines(nlines),
44 p_nbands(nbands), p_pixelType(type) {
45
46 p_sample = p_line = p_band = 0;
47
48 if(p_nsamps <= 0) {
49 string message = "Invalid value for sample dimensions (nsamps)";
50 throw IException(IException::Programmer, message, _FILEINFO_);
51 }
52 if(p_nlines <= 0) {
53 string message = "Invalid value for line dimensions (nlines)";
54 throw IException(IException::Programmer, message, _FILEINFO_);
55 }
56 if(p_nbands <= 0) {
57 string message = "Invalid value for band dimensions (nbands)";
58 throw IException(IException::Programmer, message, _FILEINFO_);
59 }
60
62
63 Allocate();
64 }
65
66
69 try {
70 if(p_buf) {
71 delete [] p_buf;
72 }
73 if(p_rawbuf) {
74 delete [](char *) p_rawbuf;
75 }
76 }
77 catch(...) {
78
79 }
80 }
81
82
89 Buffer &Buffer::operator=(const double &d) {
90 for(int i = 0 ; i < p_npixels ; i++) {
91 p_buf[i] = d;
92 }
93 return (*this);
94 }
95
96
106 void Buffer::SetBasePosition(const int start_sample,
107 const int start_line,
108 const int start_band) {
109 SetBaseSample(start_sample);
110 SetBaseLine(start_line);
111 SetBaseBand(start_band);
112 }
113
114
127 int Buffer::Sample(const int index) const {
128 return (index % p_nsamps) + p_sample;
129 }
130
131
145 int Buffer::Line(const int index) const {
146 int sub_index = index % (p_nsamps * p_nlines);
147 return sub_index / p_nsamps + p_line;
148 }
149
150
162 int Buffer::Band(const int index) const {
163 return index / (p_nsamps * p_nlines) + p_band;
164 }
165
166
177 void Buffer::Position(const int index, int &i_samp, int &i_line,
178 int &i_band) const {
179 i_samp = Sample(index);
180 i_line = Line(index);
181 i_band = Band(index);
182 }
183
184
197 int Buffer::Index(const int i_samp, const int i_line, const int i_band)
198 const {
199 if((i_samp < p_sample) || (i_samp > (p_sample + p_nsamps - 1))) {
200 QString message = Message::ArraySubscriptNotInRange(i_samp);
201 throw IException(IException::Programmer, message, _FILEINFO_);
202 }
203
204 if((i_line < p_line) || (i_line > (p_line + p_nlines - 1))) {
205 QString message = Message::ArraySubscriptNotInRange(i_line);
206 throw IException(IException::Programmer, message, _FILEINFO_);
207 }
208
209 if((i_band < p_band) || (i_band > (p_band + p_nbands - 1))) {
210 QString message = Message::ArraySubscriptNotInRange(i_band);
211 throw IException(IException::Programmer, message, _FILEINFO_);
212 }
213
214 // Got a valid reference location so compute the index and return
215 int index = (i_band - p_band) * (p_nlines * p_nsamps) +
216 (i_line - p_line) * (p_nsamps) +
217 (i_samp - p_sample);
218 return (index);
219 }
220
221
231 double Buffer::at(const int index) const {
232 if(index < 0) {
233 QString message = Message::ArraySubscriptNotInRange(index);
234 throw IException(IException::Programmer, message, _FILEINFO_);
235 }
236 else if(index >= p_npixels) {
237 QString message = Message::ArraySubscriptNotInRange(index);
238 throw IException(IException::Programmer, message, _FILEINFO_);
239 }
240
241 return p_buf[index];
242 }
243
244
255 void Buffer::Copy(const Buffer &in, bool includeRawBuf) {
256 if(p_npixels != in.size()) {
257 string message = "Input and output buffers are not the same size";
258 throw IException(IException::Programmer, message, _FILEINFO_);
259 }
260
261 if(includeRawBuf && p_pixelType != in.PixelType()) {
262 string message = "Input and output buffers are not the same pixel type";
263 throw IException(IException::Programmer, message, _FILEINFO_);
264 }
265
266 size_t n = sizeof(double);
267 n = n * (size_t) p_npixels;
268 memcpy(p_buf, in.p_buf, n);
269
270 if (includeRawBuf) {
272 n = n * (size_t) p_npixels;
273 memcpy(p_rawbuf, in.p_rawbuf, n);
274 }
275 }
276
277
286 bool isSubareaOfIn = (p_npixels <= in.size());
287 isSubareaOfIn &= (p_sample >= in.p_sample);
288 isSubareaOfIn &= (p_line >= in.p_line);
289 isSubareaOfIn &= (p_band >= in.p_band);
290
291 int endSample = p_sample + p_nsamps - 1;
292 int otherEndSample = in.p_sample + in.p_nsamps - 1;
293
294 int endLine = p_line + p_nlines - 1;
295 int otherEndLine = in.p_line + in.p_nlines - 1;
296
297 int endBand = p_band + p_nbands - 1;
298 int otherEndBand = in.p_band + in.p_nbands - 1;
299
300 isSubareaOfIn &= (endSample <= otherEndSample);
301 isSubareaOfIn &= (endLine <= otherEndLine);
302 isSubareaOfIn &= (endBand <= otherEndBand);
303
304 if (isSubareaOfIn) {
305 for (int i = 0; i < size(); i++) {
306 (*this)[i] = in[in.Index(Sample(i), Line(i), Band(i))];
307 }
308 }
309
310 return isSubareaOfIn;
311 }
312
313
320 Buffer::Buffer(const Buffer &rhs) :
321 p_nsamps(rhs.p_nsamps), p_nlines(rhs.p_nlines),
322 p_nbands(rhs.p_nbands), p_pixelType(rhs.p_pixelType) {
323
324 p_sample = rhs.p_sample;
325 p_line = rhs.p_line;
326 p_band = rhs.p_band;
327
328 p_npixels = rhs.p_npixels;
329
330 Allocate();
331 Copy(rhs);
332 }
333
334
341 p_buf = NULL;
342 p_rawbuf = NULL;
343 try {
344 p_buf = new double [p_npixels];
345 size_t n = Isis::SizeOf(p_pixelType);
346 n = n * (size_t) p_npixels;
347 p_rawbuf = new char[n];
348 }
349 catch(...) {
350 try {
351 if(p_buf) {
352 delete [] p_buf;
353 p_buf = NULL;
354 }
355 if(p_rawbuf) {
356 delete [](char *)p_rawbuf;
357 p_rawbuf = NULL;
358 }
359 }
360 catch(...) {
361 p_buf = NULL;
362 p_rawbuf = NULL;
363 }
364 QString message = Message::MemoryAllocationFailed();
365 throw IException(IException::Unknown, message, _FILEINFO_);
366 }
367 }
368}
Buffer for reading and writing cube data.
Definition Buffer.h:53
int size() const
Returns the total number of pixels in the shape buffer.
Definition Buffer.h:97
void * p_rawbuf
The raw dm read from the disk.
Definition Buffer.h:212
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:106
int Line(const int index=0) const
Returns the line position associated with a shape buffer index.
Definition Buffer.cpp:145
Buffer()
Default constructor for proper initialization purposes.
Definition Buffer.cpp:25
int p_npixels
Number of pixels (nsamps * nlines * nbands)
Definition Buffer.h:207
void Allocate()
Size or resize the memory buffer.
Definition Buffer.cpp:340
int p_nlines
Number of lines to read/write.
Definition Buffer.h:202
int p_nbands
Number of bands to read/write.
Definition Buffer.h:205
Buffer & operator=(const double &d)
Assign the entire buffer to a constant double value.
Definition Buffer.cpp:89
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:285
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:177
void SetBaseLine(const int start_line)
This method is used to set the base line position of the shape buffer.
Definition Buffer.h:183
double * p_buf
Shape buffer allocated to the size of npixels for handling reads/writes.
Definition Buffer.h:208
int Band(const int index=0) const
Returns the band position associated with a shape buffer index.
Definition Buffer.cpp:162
int p_sample
Starting sample to read/write.
Definition Buffer.h:198
const Isis::PixelType p_pixelType
The pixel type of the raw buffer.
Definition Buffer.h:211
int p_band
Starting band to read/write.
Definition Buffer.h:204
void Copy(const Buffer &in, bool includeRawBuf=true)
Allows copying of the buffer contents to another Buffer.
Definition Buffer.cpp:255
double at(const int index) const
Returns the value in the shape buffer at the given index.
Definition Buffer.cpp:231
int p_nsamps
Number of samples to read/write.
Definition Buffer.h:199
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:197
int p_line
Starting line to read/write.
Definition Buffer.h:201
int Sample(const int index=0) const
Returns the sample position associated with a shape buffer index.
Definition Buffer.cpp:127
void SetBaseBand(const int start_band)
This method is used to set the base band position of the shape buffer.
Definition Buffer.h:193
void SetBaseSample(const int start_samp)
This method is used to set the base sample position of the shape buffer.
Definition Buffer.h:173
~Buffer()
Destroys the Buffer object and frees shape buffer.
Definition Buffer.cpp:68
Isis exception class.
Definition IException.h:91
@ Unknown
A type of error that cannot be classified as any of the other error types.
Definition IException.h:118
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
QString ArraySubscriptNotInRange(int index)
This error should be used when an Isis object or application is checking array bounds and the legal r...
QString MemoryAllocationFailed()
This error should be used when an error accrues during a memory allocation such as "new".
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
int SizeOf(Isis::PixelType pixelType)
Returns the number of bytes of the specified PixelType.
Definition PixelType.h:46
PixelType
Enumerations for Isis Pixel Types.
Definition PixelType.h:27
Namespace for the standard library.