All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Band.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/memory/Band.cpp
22 
23  \brief Band implementation for the In-Memory Raster.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "../raster/BandProperty.h"
29 #include "../raster/Utils.h"
30 #include "Band.h"
31 #include "Exception.h"
32 #include "Raster.h"
33 
34 // STL
35 #include <cassert>
36 #include <cstring>
37 
38 te::mem::Band::Band(Raster* r, te::rst::BandProperty* p, std::size_t idx, void* externalBuffer)
39  : te::rst::Band(p, idx),
40  m_raster(r),
41  m_buff(0),
42  m_getBuff(0),
43  m_getBuffI(0),
44  m_setBuff(0),
45  m_setBuffI(0),
46  m_ncols(0),
47  m_nrows(0),
48  m_blksize(0),
49  m_releaseBuffer(true)
50 {
51  int pxlsize = te::rst::GetPixelSize(m_property->getType());
52 
54 
55 // fixing block sizes for special cases (1, 2, and 4 bits raster data type)
57  m_blksize = te::rst::Round(m_blksize / 2);
59  m_blksize = te::rst::Round(m_blksize / 4);
61  m_blksize = te::rst::Round(m_blksize / 8);
62 
63  if(externalBuffer == 0)
64  m_buff = new unsigned char[m_blksize];
65  else
66  {
67  m_buff = static_cast<unsigned char*>(externalBuffer);
68  m_releaseBuffer = false;
69  }
70 
73 
75 }
76 
78  : te::rst::Band(rhs),
79  m_raster(0),
80  m_buff(0),
81  m_getBuff(rhs.m_getBuff),
82  m_getBuffI(rhs.m_getBuffI),
83  m_setBuff(rhs.m_setBuff),
84  m_setBuffI(rhs.m_setBuffI),
85  m_ncols(rhs.m_ncols),
86  m_nrows(rhs.m_nrows),
87  m_blksize(rhs.m_blksize),
88  m_releaseBuffer(true)
89 {
90  m_buff = new unsigned char[m_blksize];
91 
92  memcpy(m_buff, rhs.m_buff, m_blksize);
93 }
94 
96 {
97  if(m_releaseBuffer)
98  delete [] m_buff;
99 }
100 
102 {
103  return m_raster;
104 }
105 
107 {
108  if(&rhs != this)
109  {
110  if(m_blksize != rhs.m_blksize)
111  throw Exception(TE_TR("You can not assign a band to another one with different block size"));
112 
114 
115  memcpy(m_buff, rhs.m_buff, m_blksize);
116 
117  m_getBuff = rhs.m_getBuff;
118  m_getBuffI = rhs.m_getBuffI;
119  m_setBuff = rhs.m_setBuff;
120  m_setBuffI = rhs.m_setBuffI;
121  m_ncols = rhs.m_ncols;
122  m_nrows = rhs.m_nrows;
123  }
124 
125  return *this;
126 }
127 
128 void te::mem::Band::getValue(unsigned int c, unsigned int r, double& value) const
129 {
130  int pos = c + r * m_ncols;
131 
132  m_getBuff(pos, m_buff, &value);
133 }
134 
135 void te::mem::Band::setValue(unsigned int c, unsigned int r, const double value)
136 {
137  int pos = c + r * m_ncols;
138 
139  m_setBuff(pos, m_buff, &value);
140 }
141 
142 void te::mem::Band::getIValue(unsigned int c, unsigned int r, double& value) const
143 {
144  int pos = c + r * m_ncols;
145 
146  m_getBuffI(pos, m_buff, &value);
147 }
148 
149 void te::mem::Band::setIValue(unsigned int c, unsigned int r, const double value)
150 {
151  int pos = c + r * m_ncols;
152 
153  m_setBuffI(pos, m_buff, &value);
154 }
155 
156 void te::mem::Band::read(int x, int y, void* buffer) const
157 {
158  assert(x == 0 && y == 0);
159 
160  memcpy(buffer, m_buff, m_blksize);
161 }
162 
163 void* te::mem::Band::read(int x, int y)
164 {
165  assert(x == 0 && y == 0);
166 
167  return m_buff;
168 }
169 
170 void te::mem::Band::write(int x, int y, void* buffer)
171 {
172  assert(x == 0 && y == 0);
173 
174  memcpy(m_buff, buffer, m_blksize);
175 }
176 
178 {
179  m_raster = r;
180 }
181 
183 {
184  return m_blksize;
185 }
void getIValue(unsigned int c, unsigned int r, double &value) const
Returns the imaginary attribute value in a complex band of a cell.
Definition: Band.cpp:142
A raster class for memory.
Definition: Raster.h:44
te::rst::GetBufferValueFPtr m_getBuffI
A pointer to a function that helps to extract the imaginary part value from a specific buffer data ty...
Definition: Band.h:89
void setIValue(unsigned int c, unsigned int r, const double value)
Sets the imaginary attribute value in a complex band of a cell.
Definition: Band.cpp:149
A raster band description.
Definition: BandProperty.h:61
int m_nrows
Buffered number of rows.
Definition: Band.h:93
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
virtual Band & operator=(const Band &rhs)
Assignment operator.
Definition: Band.cpp:48
int m_blksize
The data block size.
Definition: Band.h:94
~Band()
Virtual destructor.
Definition: Band.cpp:95
void setRaster(Raster *r)
Definition: Band.cpp:177
int getBlockSize() const
It returns the number of bytes ocuppied by a data block.
Definition: Band.cpp:182
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
An exception class for the TerraLib In-Memory Data Access driver.
te::rst::SetBufferValueFPtr m_setBuffI
A pointer to a function that helps to insert the imaginary part value into a specific buffer data typ...
Definition: Band.h:91
te::rst::GetBufferValueFPtr m_getBuff
A pointer to a function that helps to extract a double or complex value from a specific buffer data t...
Definition: Band.h:88
Band(Raster *r, te::rst::BandProperty *p, std::size_t idx, void *externalBuffer=0)
Definition: Band.cpp:38
void read(int x, int y, void *buffer) const
It reads a data block to the specified buffer.
Definition: Band.cpp:156
void setValue(unsigned int c, unsigned int r, const double value)
Sets the cell attribute value.
Definition: Band.cpp:135
TERASTEREXPORT int Round(double val)
Round a double value to a integer value.
Definition: Utils.cpp:298
te::rst::SetBufferValueFPtr m_setBuff
A pointer to a function that helps to insert a double or complex value into a specific buffer data ty...
Definition: Band.h:90
te::rst::Raster * getRaster() const
Returns the associated raster.
Definition: Band.cpp:101
Band & operator=(const Band &rhs)
Definition: Band.cpp:106
An abstract class for raster data strucutures.
Definition: Raster.h:71
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
int m_blkw
Block width (pixels).
Definition: BandProperty.h:143
bool m_releaseBuffer
A flag that indicates if the buffer must be released when the band goes out of scope.
Definition: Band.h:95
A raster class for memory.
TERASTEREXPORT int GetPixelSize(int datatype)
Returns the byte size of a given datatype.
Definition: Utils.cpp:80
Band implementation for the In-Memory Raster.
Definition: Band.h:46
int m_ncols
Buffered number of cols.
Definition: Band.h:92
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
unsigned char * m_buff
A pointer to a block buffer.
Definition: Band.h:87
int m_blkh
Block height (pixels).
Definition: BandProperty.h:144
void getValue(unsigned int c, unsigned int r, double &value) const
Returns the cell attribute value.
Definition: Band.cpp:128
void write(int x, int y, void *buffer)
It writes a data block from the specified buffer.
Definition: Band.cpp:170
TERASTEREXPORT void SetBlockFunctions(GetBufferValueFPtr *gb, GetBufferValueFPtr *gbi, SetBufferValueFPtr *sb, SetBufferValueFPtr *sbi, int type)
Sets the pointers to functions that helps to extract a double or complex value from a specific buffer...
Definition: BlockUtils.cpp:295
BandProperty * m_property
The band information.
Definition: Band.h:474