All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
BlockUtils.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/raster/BlockUtils.cpp
22 
23  \brief Utility functions for dealing with raster data blocks.
24 */
25 
26 // TerraLib
27 #include "../datatype/Enums.h"
28 #include "BlockUtils.h"
29 #include "Exception.h"
30 
31 namespace te
32 {
33  namespace rst
34  {
35  /*!
36  \struct CInt16
37 
38  \brief A simple type for use by driver to cast raster data blocks.
39  */
40  struct CInt16
41  {
42  short r;
43  short i;
44  };
45 
46  /*!
47  \struct CInt32
48 
49  \brief A simple type for use by driver to cast raster data blocks.
50  */
51  struct CInt32
52  {
53  int r;
54  int i;
55  };
56 
57  /*!
58  \struct CFloat
59 
60  \brief A simple type for use by driver to cast raster data blocks.
61  */
62  struct CFloat
63  {
64  float r;
65  float i;
66  };
67 
68  /*!
69  \struct CDouble
70 
71  \brief A simple type for use by driver to cast raster data blocks.
72  */
73  struct CDouble
74  {
75  double r;
76  double i;
77  };
78 
79  } // end namespace rst
80 } // end namespace te
81 
82 void te::rst::DummyGetValue(int /*index*/, void* /*buffer*/, double* /*value*/)
83 {
84 }
85 
86 void te::rst::DummySetValue(int /*index*/, void* /*buffer*/, const double* /*value*/)
87 {
88 }
89 
90 void te::rst::Get4bits(int index, void* buffer, double* value)
91 {
92  unsigned int bitshift = (index % 2) * 4;
93  unsigned char ucvalue = ((unsigned char*) buffer) [index >> 1]; // index >> 1 is used to divide the index by 2
94  *value = (double) ( (ucvalue >> bitshift) & 15 ); // '& 15' is used to get only first 4 bits (mask 00001111)
95 }
96 
97 void te::rst::Get2bits(int index, void* buffer, double* value)
98 {
99  unsigned int bitshift = (index % 4) * 2;
100  unsigned char ucvalue = ((unsigned char*) buffer) [index >> 2]; // index >> 2 is used to divide the index by 4
101  *value = (double) ( ( ucvalue >> bitshift ) & 3 ); // '& 3' is used to get only first 2 bits (mask 00000011)
102 }
103 
104 void te::rst::Get1bit(int index, void* buffer, double* value)
105 {
106  unsigned int bitshift = (index % 8);
107  unsigned char ucvalue = ((unsigned char*) buffer) [index >> 3]; // index >> 3 is used to divide the index by 8
108  *value = (double) ( ( ucvalue >> bitshift ) & 1 ); // '& 1' is used to get only first bit (mask 00000001)
109 }
110 
111 void te::rst::GetUChar(int index, void* buffer, double* value)
112 {
113  *value = (double)((unsigned char*)buffer)[index];
114 }
115 
116 void te::rst::GetChar(int index, void* buffer, double* value)
117 {
118  *value = (double)((char*)buffer)[index];
119 }
120 
121 void te::rst::GetUInt16(int index, void* buffer, double* value)
122 {
123  *value = (double)((unsigned short*)buffer)[index];
124 }
125 
126 void te::rst::GetInt16(int index, void* buffer, double* value)
127 {
128  *value = (double)((short*)buffer)[index];
129 }
130 
131 void te::rst::GetUInt32(int index, void* buffer, double* value)
132 {
133  *value = (double)((unsigned int*)buffer)[index];
134 }
135 
136 void te::rst::GetInt32(int index, void* buffer, double* value)
137 {
138  *value = (double)((int*)buffer)[index];
139 }
140 
141 void te::rst::GetFloat(int index, void* buffer, double* value)
142 {
143  *value = (double)((float*)buffer)[index];
144 }
145 
146 void te::rst::GetDouble(int index, void* buffer, double* value)
147 {
148  *value = ((double*)buffer)[index];
149 }
150 
151 void te::rst::GetCRInt16(int index, void* buffer, double* value)
152 {
153  *value = (double)(((CInt16*)buffer)[index]).r;
154 }
155 
156 void te::rst::GetCIInt16(int index, void* buffer, double* value)
157 {
158  *value = (double)(((CInt16*)buffer)[index]).i;
159 }
160 
161 void te::rst::GetCRInt32(int index, void* buffer, double* value)
162 {
163  *value = (double)(((CInt32*)buffer)[index]).r;
164 }
165 
166 void te::rst::GetCIInt32(int index, void* buffer, double* value)
167 {
168  *value = (double)(((CInt32*)buffer)[index]).i;
169 }
170 
171 void te::rst::GetCRFloat(int index, void* buffer, double* value)
172 {
173  *value = (double)(((CFloat*)buffer)[index]).r;
174 }
175 
176 void te::rst::GetCIFloat(int index, void* buffer, double* value)
177 {
178  *value = (double)(((CFloat*)buffer)[index]).i;
179 }
180 
181 void te::rst::GetCRDouble(int index, void* buffer, double* value)
182 {
183  *value = (((CDouble*)buffer)[index]).r;
184 }
185 
186 void te::rst::GetCIDouble(int index, void* buffer, double* value)
187 {
188  *value = (((CDouble*)buffer)[index]).i;
189 }
190 
191 void te::rst::Set1bit(int index, void* buffer, const double* value)
192 {
193  unsigned int bitshift = (index % 8);
194  unsigned char firstbit = ((unsigned char) *value) & 1; // '& 1' is used to get only first bit (mask 00000001)
195  ((unsigned char*)buffer)[index >> 3] &= ~(1u << bitshift); // clear bit value, index >> 3 is used to divide the index by 8
196  ((unsigned char*)buffer)[index >> 3] |= (firstbit << bitshift); // set new value, index >> 3 is used to divide the index by 8
197 }
198 
199 void te::rst::Set2bits(int index, void* buffer, const double* value)
200 {
201  unsigned int bitshift = (index % 4) * 2;
202  unsigned char first2bits = ((unsigned char) *value) & 3; // '& 3' is used to get only first 2 bits (mask 00000011)
203  ((unsigned char*)buffer)[index >> 2] &= ~(3u << bitshift); // clear bit values, index >> 2 is used to divide the index by 4
204  ((unsigned char*)buffer)[index >> 2] |= (first2bits << bitshift); // set new value, index >> 2 is used to divide the index by 4
205 }
206 
207 void te::rst::Set4bits(int index, void* buffer, const double* value)
208 {
209  unsigned int bitshift = (index % 2) * 4;
210  unsigned char first4bits = ((unsigned char) *value) & 15; // '& 15' is used to get only first 4 bits (mask 00001111)
211  ((unsigned char*)buffer)[index >> 1] &= ~(15u << bitshift); // clear bit values, index >> 1 is used to divide the index by 2
212  ((unsigned char*)buffer)[index >> 1] |= (first4bits << bitshift); // set new value, index >> 1 is used to divide the index by 2
213 }
214 
215 void te::rst::SetUChar(int index, void* buffer, const double* value)
216 {
217  ((unsigned char*)buffer)[index] = (unsigned char) *value;
218 }
219 
220 void te::rst::SetChar(int index, void* buffer, const double* value)
221 {
222  ((char*)buffer)[index] = (char) *value;
223 }
224 
225 void te::rst::SetUInt16(int index, void* buffer, const double* value)
226 {
227  ((unsigned short*)buffer)[index] = (unsigned short) *value;
228 }
229 
230 void te::rst::SetInt16(int index, void* buffer, const double* value)
231 {
232  ((short*)buffer)[index] = (short) *value;
233 }
234 
235 void te::rst::SetUInt32(int index, void* buffer, const double* value)
236 {
237  ((unsigned int*)buffer)[index] = (unsigned int) *value;
238 }
239 
240 void te::rst::SetInt32(int index, void* buffer, const double* value)
241 {
242  ((int*)buffer)[index] = (int) *value;
243 }
244 
245 void te::rst::SetFloat(int index, void* buffer, const double* value)
246 {
247  ((float*)buffer)[index] = (float) *value;
248 }
249 
250 void te::rst::SetDouble(int index, void* buffer, const double* value)
251 {
252  ((double*)buffer)[index] = (double) *value;
253 }
254 
255 void te::rst::SetCRInt16(int index, void* buffer, const double* value)
256 {
257  (((CInt16*)buffer)[index]).r = (short) *value;
258 }
259 
260 void te::rst::SetCIInt16(int index, void* buffer, const double* value)
261 {
262  (((CInt16*)buffer)[index]).i = (short)(*(value));
263 }
264 
265 void te::rst::SetCRInt32(int index, void* buffer, const double* value)
266 {
267  (((CInt32*)buffer)[index]).r = (int) *value;
268 }
269 
270 void te::rst::SetCIInt32(int index, void* buffer, const double* value)
271 {
272  (((CInt32*)buffer)[index]).i = (int)(*(value));
273 }
274 
275 void te::rst::SetCRFloat(int index, void* buffer, const double* value)
276 {
277  (((CFloat*)buffer)[index]).r = (float) *value;
278 }
279 
280 void te::rst::SetCIFloat(int index, void* buffer, const double* value)
281 {
282  (((CFloat*)buffer)[index]).i = (float) *(value);
283 }
284 
285 void te::rst::SetCRDouble(int index, void* buffer, const double* value)
286 {
287  (((CDouble*)buffer)[index]).r = *value;
288 }
289 
290 void te::rst::SetCIDouble(int index, void* buffer, const double* value)
291 {
292  (((CDouble*)buffer)[index]).i = *value;
293 }
294 
296  SetBufferValueFPtr *sb, SetBufferValueFPtr *sbi, int type)
297 {
298  switch(type)
299  {
300  case te::dt::R4BITS_TYPE:
301  *gb = te::rst::Get4bits;
302  *gbi = te::rst::DummyGetValue;
303  *sb = te::rst::Set4bits;
304  *sbi = te::rst::DummySetValue;
305  break;
306 
307  case te::dt::R2BITS_TYPE:
308  *gb = te::rst::Get2bits;
309  *gbi = te::rst::DummyGetValue;
310  *sb = te::rst::Set2bits;
311  *sbi = te::rst::DummySetValue;
312  break;
313 
314  case te::dt::R1BIT_TYPE:
315  *gb = te::rst::Get1bit;
316  *gbi = te::rst::DummyGetValue;
317  *sb = te::rst::Set1bit;
318  *sbi = te::rst::DummySetValue;
319  break;
320 
321  case te::dt::UCHAR_TYPE:
322  *gb = te::rst::GetUChar;
323  *gbi = te::rst::DummyGetValue;
324  *sb = te::rst::SetUChar;
325  *sbi = te::rst::DummySetValue;
326  break;
327 
328  case te::dt::CHAR_TYPE:
329  *gb = te::rst::GetChar;
330  *gbi = te::rst::DummyGetValue;
331  *sb = te::rst::SetChar;
332  *sbi = te::rst::DummySetValue;
333  break;
334 
335  case te::dt::UINT16_TYPE:
336  *gb = te::rst::GetUInt16;
337  *gbi = te::rst::DummyGetValue;
338  *sb = te::rst::SetUInt16;
339  *sbi = te::rst::DummySetValue;
340  break;
341 
342  case te::dt::INT16_TYPE:
343  *gb = te::rst::GetInt16;
344  *gbi = te::rst::DummyGetValue;
345  *sb = te::rst::SetInt16;
346  *sbi = te::rst::DummySetValue;
347  break;
348 
349  case te::dt::UINT32_TYPE:
350  *gb = te::rst::GetUInt32;
351  *gbi = te::rst::DummyGetValue;
352  *sb = te::rst::SetUInt32;
353  *sbi = te::rst::DummySetValue;
354  break;
355 
356  case te::dt::INT32_TYPE:
357  *gb = te::rst::GetInt32;
358  *gbi = te::rst::DummyGetValue;
359  *sb = te::rst::SetInt32;
360  *sbi = te::rst::DummySetValue;
361  break;
362 
363  case te::dt::FLOAT_TYPE:
364  *gb = te::rst::GetFloat;
365  *gbi = te::rst::DummyGetValue;
366  *sb = te::rst::SetFloat;
367  *sbi = te::rst::DummySetValue;
368  break;
369 
370  case te::dt::DOUBLE_TYPE:
371  *gb = te::rst::GetDouble;
372  *gbi = te::rst::DummyGetValue;
373  *sb = te::rst::SetDouble;
374  *sbi = te::rst::DummySetValue;
375  break;
376 
377  case te::dt::CINT16_TYPE:
378  *gb = te::rst::GetCRInt16;
379  *gbi = te::rst::GetCIInt16;
380  *sb = te::rst::SetCRInt16;
381  *sbi = te::rst::SetCIInt16;
382  break;
383 
384  case te::dt::CINT32_TYPE:
385  *gb = te::rst::GetCRInt32;
386  *gbi = te::rst::GetCIInt32;
387  *sb = te::rst::SetCRInt32;
388  *sbi = te::rst::SetCIInt32;
389  break;
390 
391  case te::dt::CFLOAT_TYPE:
392  *gb = te::rst::GetCRFloat;
393  *gbi = te::rst::GetCIFloat;
394  *sb = te::rst::SetCRFloat;
395  *sbi = te::rst::SetCIFloat;
396  break;
397 
399  *gb = te::rst::GetCRDouble;
400  *gbi = te::rst::GetCIDouble;
401  *sb = te::rst::SetCRDouble;
402  *sbi = te::rst::SetCIDouble;
403  break;
404 
405  default:
406  throw te::rst::Exception("Invalid data type");
407  }
408 }
TERASTEREXPORT void Get1bit(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:104
TERASTEREXPORT void Set2bits(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:199
TERASTEREXPORT void SetCIDouble(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:290
A simple type for use by driver to cast raster data blocks.
Definition: BlockUtils.cpp:73
TERASTEREXPORT void GetCRInt32(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:161
TERASTEREXPORT void GetInt32(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:136
TERASTEREXPORT void DummySetValue(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:86
TERASTEREXPORT void GetCRFloat(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:171
TERASTEREXPORT void SetCRInt32(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:265
TERASTEREXPORT void Set4bits(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:207
TERASTEREXPORT void GetCRInt16(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:151
TERASTEREXPORT void SetCRFloat(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:275
TERASTEREXPORT void GetCIFloat(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:176
TERASTEREXPORT void GetFloat(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:141
Utility functions for dealing with raster data blocks.
TERASTEREXPORT void SetInt16(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:230
TERASTEREXPORT void GetCIInt32(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:166
An exception class for the Raster module.
TERASTEREXPORT void GetUInt16(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:121
TERASTEREXPORT void GetCIDouble(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:186
TERASTEREXPORT void GetInt16(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:126
void(* SetBufferValueFPtr)(int index, void *buffer, const double *value)
The type of function used to extract data from a buffer.
Definition: BlockUtils.h:40
TERASTEREXPORT void SetDouble(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:250
TERASTEREXPORT void SetCIFloat(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:280
TERASTEREXPORT void Set1bit(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:191
TERASTEREXPORT void SetFloat(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:245
TERASTEREXPORT void Get4bits(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:90
A simple type for use by driver to cast raster data blocks.
Definition: BlockUtils.cpp:62
TERASTEREXPORT void SetInt32(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:240
TERASTEREXPORT void SetCRInt16(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:255
TERASTEREXPORT void SetCIInt16(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:260
TERASTEREXPORT void SetUInt16(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:225
TERASTEREXPORT void GetDouble(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:146
TERASTEREXPORT void Get2bits(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:97
TERASTEREXPORT void GetChar(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:116
A simple type for use by driver to cast raster data blocks.
Definition: BlockUtils.cpp:40
TERASTEREXPORT void GetUChar(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:111
TERASTEREXPORT void SetUInt32(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:235
TERASTEREXPORT void GetCRDouble(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:181
A simple type for use by driver to cast raster data blocks.
Definition: BlockUtils.cpp:51
TERASTEREXPORT void GetCIInt16(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:156
TERASTEREXPORT void GetUInt32(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:131
void(* GetBufferValueFPtr)(int index, void *buffer, double *value)
The type of function used to extract data from a buffer.
Definition: BlockUtils.h:37
TERASTEREXPORT void SetUChar(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:215
TERASTEREXPORT void SetChar(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:220
TERASTEREXPORT void DummyGetValue(int index, void *buffer, double *value)
Definition: BlockUtils.cpp:82
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
TERASTEREXPORT void SetCIInt32(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:270
TERASTEREXPORT void SetCRDouble(int index, void *buffer, const double *value)
Definition: BlockUtils.cpp:285