Official websites use .gov
A .gov website belongs to an official government organization in the United States.

Secure .gov websites use HTTPS
A lock ( ) or https:// means you’ve safely connected to the .gov website. Share sensitive information only on official, secure websites.

Isis 3 Programmer Reference
Buffer.cpp
1
5
6/* SPDX-License-Identifier: CC0-1.0 */
7
8#include "Buffer.h"
9#include "IException.h"
10#include "Message.h"
11#include "PixelType.h"
12#include "SpecialPixel.h"
13
14#include <iostream>
15
16using namespace std;
17
18namespace Isis {
19
27 p_band(0), p_nbands(0), p_npixels(0), p_buf(0),
28 p_pixelType(None), p_rawbuf(0) { }
29
30
42 Buffer::Buffer(const int nsamps, const int nlines,
43 const int nbands, const Isis::PixelType type) :
44 p_nsamps(nsamps), p_nlines(nlines),
45 p_nbands(nbands), p_pixelType(type) {
46
47 p_sample = p_line = p_band = 0;
48
49 if(p_nsamps <= 0) {
50 string message = "Invalid value for sample dimensions (nsamps)";
51 throw IException(IException::Programmer, message, _FILEINFO_);
52 }
53 if(p_nlines <= 0) {
54 string message = "Invalid value for line dimensions (nlines)";
55 throw IException(IException::Programmer, message, _FILEINFO_);
56 }
57 if(p_nbands <= 0) {
58 string message = "Invalid value for band dimensions (nbands)";
59 throw IException(IException::Programmer, message, _FILEINFO_);
60 }
61
63
64 Allocate();
65 }
66
67
70 try {
71 if(p_buf) {
72 delete [] p_buf;
73 }
74 if(p_rawbuf) {
75 delete [](char *) p_rawbuf;
76 }
77 }
78 catch(...) {
79
80 }
81 }
82
83
90 Buffer &Buffer::operator=(const double &d) {
91 for(int i = 0 ; i < p_npixels ; i++) {
92 p_buf[i] = d;
93 }
94 return (*this);
95 }
96
97
107 void Buffer::SetBasePosition(const int start_sample,
108 const int start_line,
109 const int start_band) {
110 SetBaseSample(start_sample);
111 SetBaseLine(start_line);
112 SetBaseBand(start_band);
113 }
114
115
128 int Buffer::Sample(const int index) const {
129 return (index % p_nsamps) + p_sample;
130 }
131
132
146 int Buffer::Line(const int index) const {
147 int sub_index = index % (p_nsamps * p_nlines);
148 return sub_index / p_nsamps + p_line;
149 }
150
151
163 int Buffer::Band(const int index) const {
164 return index / (p_nsamps * p_nlines) + p_band;
165 }
166
167
178 void Buffer::Position(const int index, int &i_samp, int &i_line,
179 int &i_band) const {
180 i_samp = Sample(index);
181 i_line = Line(index);
182 i_band = Band(index);
183 }
184
185
198 int Buffer::Index(const int i_samp, const int i_line, const int i_band)
199 const {
200 if((i_samp < p_sample) || (i_samp > (p_sample + p_nsamps - 1))) {
201 QString message = Message::ArraySubscriptNotInRange(i_samp);
202 throw IException(IException::Programmer, message, _FILEINFO_);
203 }
204
205 if((i_line < p_line) || (i_line > (p_line + p_nlines - 1))) {
206 QString message = Message::ArraySubscriptNotInRange(i_line);
207 throw IException(IException::Programmer, message, _FILEINFO_);
208 }
209
210 if((i_band < p_band) || (i_band > (p_band + p_nbands - 1))) {
211 QString message = Message::ArraySubscriptNotInRange(i_band);
212 throw IException(IException::Programmer, message, _FILEINFO_);
213 }
214
215 // Got a valid reference location so compute the index and return
216 int index = (i_band - p_band) * (p_nlines * p_nsamps) +
217 (i_line - p_line) * (p_nsamps) +
218 (i_samp - p_sample);
219 return (index);
220 }
221
222
232 double Buffer::at(const int index) const {
233 if(index < 0) {
234 QString message = Message::ArraySubscriptNotInRange(index);
235 throw IException(IException::Programmer, message, _FILEINFO_);
236 }
237 else if(index >= p_npixels) {
238 QString message = Message::ArraySubscriptNotInRange(index);
239 throw IException(IException::Programmer, message, _FILEINFO_);
240 }
241
242 return p_buf[index];
243 }
244
245
256 void Buffer::Copy(const Buffer &in, bool includeRawBuf) {
257 if(p_npixels != in.size()) {
258 string message = "Input and output buffers are not the same size";
259 throw IException(IException::Programmer, message, _FILEINFO_);
260 }
261
262 if(includeRawBuf && p_pixelType != in.PixelType()) {
263 string message = "Input and output buffers are not the same pixel type";
264 throw IException(IException::Programmer, message, _FILEINFO_);
265 }
266
267 size_t n = sizeof(double);
268 n = n * (size_t) p_npixels;
269 memcpy(p_buf, in.p_buf, n);
270
271 if (includeRawBuf) {
272 n = Isis::SizeOf(p_pixelType);
273 n = n * (size_t) p_npixels;
274 memcpy(p_rawbuf, in.p_rawbuf, n);
275 }
276 }
277
278
287 bool isSubareaOfIn = true;
288
289 // If one rectangle is on left side of other
290 // if (l1.x > r2.x || l2.x > r1.x)
291 if (p_line >= in.p_line + in.p_nlines ||
292 in.p_line >= p_line + p_nlines)
293 isSubareaOfIn = false;
294
295 // If one rectangle is above other
296 // if (r1.y > l2.y || r2.y > l1.y)
297 if (p_sample >= in.p_sample + in.p_nsamps ||
298 in.p_sample >= p_sample + p_nsamps)
299 isSubareaOfIn = false;
300
301 if (isSubareaOfIn) {
302 int topLine = max(in.p_line, p_line);
303 int topSamp = max(in.p_sample, p_sample);
304
305 int bottomLine = min(in.p_line + in.p_nlines, p_line + p_nlines);
306 int bottomSamp = min(in.p_sample + in.p_nsamps, p_sample + p_nsamps);
307
308 int firstBand = max(in.p_band, p_band);
309 int lastBand = min(in.p_band + in.p_nbands, p_band + p_nbands);
310
311 for (int b = firstBand; b < lastBand; b++) {
312 for (int i = topLine; i < bottomLine; i++) {
313 for (int j = topSamp; j < bottomSamp; j++) {
314 try {
315 (*this)[Index(j, i, b)] = in[in.Index(j, i, b)];
316 }
317 catch(...) {
318 (*this)[Index(j, i, b)] = NULL8;
319 }
320 }
321 }
322 }
323 }
324
325 return isSubareaOfIn;
326 }
327
328
335 Buffer::Buffer(const Buffer &rhs) :
338
339 p_sample = rhs.p_sample;
340 p_line = rhs.p_line;
341 p_band = rhs.p_band;
342
343 p_npixels = rhs.p_npixels;
344
345 Allocate();
346 Copy(rhs);
347 }
348
349
356 p_buf = NULL;
357 p_rawbuf = NULL;
358 try {
359 p_buf = new double [p_npixels];
360 size_t n = Isis::SizeOf(p_pixelType);
361 n = n * (size_t) p_npixels;
362 p_rawbuf = new char[n];
363 }
364 catch(...) {
365 try {
366 if(p_buf) {
367 delete [] p_buf;
368 p_buf = NULL;
369 }
370 if(p_rawbuf) {
371 delete [](char *)p_rawbuf;
372 p_rawbuf = NULL;
373 }
374 }
375 catch(...) {
376 p_buf = NULL;
377 p_rawbuf = NULL;
378 }
379 QString message = Message::MemoryAllocationFailed();
380 throw IException(IException::Unknown, message, _FILEINFO_);
381 }
382 }
383}
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:107
int Line(const int index=0) const
Returns the line position associated with a shape buffer index.
Definition Buffer.cpp:146
Buffer()
Default constructor for proper initialization purposes.
Definition Buffer.cpp:26
int p_npixels
Number of pixels (nsamps * nlines * nbands)
Definition Buffer.h:207
void Allocate()
Size or resize the memory buffer.
Definition Buffer.cpp:355
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:90
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:286
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:178
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
Isis::PixelType PixelType() const
Returns the raw buffer pixel type.
Definition Buffer.h:160
int Band(const int index=0) const
Returns the band position associated with a shape buffer index.
Definition Buffer.cpp:163
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:256
double at(const int index) const
Returns the value in the shape buffer at the given index.
Definition Buffer.cpp:232
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:198
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:128
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:69
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.