grass  7.8.6
About: GRASS (Geographic Resources Analysis Support System) is a raster- and vector-based GIS, image processing system, graphics production system and spatial modeling system.
  Fossies Dox: grass-7.8.6.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

fpxdr.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/types.h>
5#include <unistd.h>
6
7#include <grass/raster.h>
8
9#include "raster3d_intern.h"
10
11/*---------------------------------------------------------------------------*/
12
13int Rast3d_is_xdr_null_num(const void *num, int isFloat)
14{
15 static const char null_bytes[8] = {
16 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
17 };
18
19 return memcmp(num, null_bytes, isFloat ? 4 : 8) == 0;
20}
21
22/*---------------------------------------------------------------------------*/
23
24int Rast3d_is_xdr_null_float(const float *f)
25{
26 return Rast3d_is_xdr_null_num(f, 1);
27}
28
29/*---------------------------------------------------------------------------*/
30
31int Rast3d_is_xdr_null_double(const double *d)
32{
33 return Rast3d_is_xdr_null_num(d, 0);
34}
35
36/*---------------------------------------------------------------------------*/
37
38void Rast3d_set_xdr_null_num(void *num, int isFloat)
39{
40 static const char null_bytes[8] = {
41 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
42 };
43
44 memcpy(num, null_bytes, isFloat ? 4 : 8);
45}
46
47/*---------------------------------------------------------------------------*/
48
50{
52}
53
54/*---------------------------------------------------------------------------*/
55
57{
59}
60
61/*---------------------------------------------------------------------------*/
62
63static size_t xdr_off;
64
65int Rast3d_init_fp_xdr(RASTER3D_Map * map, int misuseBytes)
66
67
68
69 /* nof addtl bytes allocated for the xdr array so that */
70 /* the array can also be (mis)used for other purposes */
71{
72 if (xdr == NULL) {
74 map->numLengthIntern) +
75 misuseBytes;
77 if (xdr == NULL) {
78 Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_malloc");
79 return 0;
80 }
81 }
82 else if (map->tileSize * RASTER3D_MAX(map->numLengthExtern,
83 map->numLengthIntern) + misuseBytes
84 > xdrLength) {
86 map->numLengthIntern) +
87 misuseBytes;
89 if (xdr == NULL) {
90 Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_realloc");
91 return 0;
92 }
93 }
94
95 return 1;
96}
97
98/*---------------------------------------------------------------------------*/
99
100static void *xdrTmp;
102static double tmpValue, *tmp;
103
105{
106 xdrTmp = xdr;
107 useXdr = map->useXdr;
108 srcType = sType;
109
110 if (map->useXdr == RASTER3D_USE_XDR)
111 xdr_off = 0;
112
113 type = map->type;
114 isFloat = (type == FCELL_TYPE);
117 tmp = &tmpValue;
118
119 return 1;
120}
121
122/*---------------------------------------------------------------------------*/
123
124static int xdr_put(const void *src)
125{
126 if (isFloat) {
128 return 0;
129 G_xdr_put_float((char*)xdr + xdr_off, src);
131 }
132 else {
134 return 0;
135 G_xdr_put_double((char*)xdr + xdr_off, src);
137 }
138 return 1;
139}
140
141/*---------------------------------------------------------------------------*/
142
143int Rast3d_copy_to_xdr(const void *src, int nofNum)
144{
145 int i;
146
147 if (useXdr == RASTER3D_NO_XDR) {
148 Rast3d_copy_values(src, 0, srcType, xdrTmp, 0, type, nofNum);
150 return 1;
151 }
152
153 for (i = 0; i < nofNum; i++, src = G_incr_void_ptr(src, eltLength)) {
154
158 }
159 else {
160 if (type == srcType) {
161 if (!xdr_put(src)) {
162 Rast3d_error("Rast3d_copy_to_xdr: writing xdr failed");
163 return 0;
164 }
165 }
166 else {
167 if (type == FCELL_TYPE)
168 *((float *)tmp) = (float)*((double *)src);
169 else
170 *((double *)tmp) = (double)*((float *)src);
171 if (!xdr_put(tmp)) {
172 Rast3d_error("Rast3d_copy_to_xdr: writing xdr failed");
173 return 0;
174 }
175 }
176 }
177
179 }
180
181 return 1;
182}
183
184/*---------------------------------------------------------------------------*/
185
187{
188 xdrTmp = xdr;
189 useXdr = map->useXdr;
190 dstType = dType;
191
193 xdr_off = 0;
194
195 type = map->type;
196 isFloat = (type == FCELL_TYPE);
199 tmp = &tmpValue;
200
201 return 1;
202}
203
204/*---------------------------------------------------------------------------*/
205
206static int xdr_get(void *src)
207{
208 if (isFloat) {
210 return 0;
211 G_xdr_get_float(src, (char*)xdr + xdr_off);
213 }
214 else {
216 return 0;
217 G_xdr_get_double(src, (char*)xdr + xdr_off);
219 }
220 return 1;
221}
222
223/*---------------------------------------------------------------------------*/
224
225int Rast3d_copy_from_xdr(int nofNum, void *dst)
226{
227 int i;
228
229 if (useXdr == RASTER3D_NO_XDR) {
230 Rast3d_copy_values(xdrTmp, 0, type, dst, 0, dstType, nofNum);
232 return 1;
233 }
234
235 for (i = 0; i < nofNum; i++, dst = G_incr_void_ptr(dst, eltLength)) {
236
240 }
241 else {
242 if (type == dstType) {
243 if (!xdr_get(dst)) {
244 Rast3d_error("Rast3d_copy_from_xdr: reading xdr failed");
245 return 0;
246 }
247 }
248 else {
249 if (!xdr_get(tmp)) {
250 Rast3d_error("Rast3d_copy_from_xdr: reading xdr failed");
251 return 0;
252 }
253 if (type == FCELL_TYPE)
254 *((double *)dst) = (double)*((float *)tmp);
255 else
256 *((float *)dst) = (float)*((double *)tmp);
257 }
258 }
259
261 }
262
263 return 1;
264}
#define NULL
Definition: ccmath.h:32
static int src[2][2]
Definition: raster.c:25
static double dst[2][2]
Definition: raster.c:26
static size_t xdr_off
Definition: fpxdr.c:63
static int dstType
Definition: fpxdr.c:101
static int xdr_put(const void *src)
Definition: fpxdr.c:124
static void * xdrTmp
Definition: fpxdr.c:100
int Rast3d_init_copy_from_xdr(RASTER3D_Map *map, int dType)
Definition: fpxdr.c:186
int Rast3d_init_fp_xdr(RASTER3D_Map *map, int misuseBytes)
Definition: fpxdr.c:65
void Rast3d_set_xdr_null_num(void *num, int isFloat)
Definition: fpxdr.c:38
int Rast3d_init_copy_to_xdr(RASTER3D_Map *map, int sType)
Definition: fpxdr.c:104
void Rast3d_set_xdr_null_float(float *f)
Definition: fpxdr.c:56
static double * tmp
Definition: fpxdr.c:102
static int srcType
Definition: fpxdr.c:101
void Rast3d_set_xdr_null_double(double *d)
Definition: fpxdr.c:49
int Rast3d_is_xdr_null_float(const float *f)
Definition: fpxdr.c:24
static int xdr_get(void *src)
Definition: fpxdr.c:206
int Rast3d_is_xdr_null_num(const void *num, int isFloat)
Definition: fpxdr.c:13
static int externLength
Definition: fpxdr.c:101
int Rast3d_copy_from_xdr(int nofNum, void *dst)
Definition: fpxdr.c:225
static int isFloat
Definition: fpxdr.c:101
static int type
Definition: fpxdr.c:101
static int useXdr
Definition: fpxdr.c:101
static int eltLength
Definition: fpxdr.c:101
int Rast3d_is_xdr_null_double(const double *d)
Definition: fpxdr.c:31
int Rast3d_copy_to_xdr(const void *src, int nofNum)
Definition: fpxdr.c:143
static double tmpValue
Definition: fpxdr.c:102
void * G_incr_void_ptr(const void *ptr, size_t size)
Advance void pointer.
Definition: alloc.c:186
void G_xdr_put_float(void *dst, const float *src)
Definition: xdr.c:78
void G_xdr_get_double(double *dst, const void *src)
Definition: xdr.c:83
void G_xdr_put_double(void *dst, const double *src)
Definition: xdr.c:88
void G_xdr_get_float(float *dst, const void *src)
Definition: xdr.c:73
int Rast3d_is_null_value_num(const void *n, int type)
Definition: null.c:12
void Rast3d_set_null_value(void *c, int nofElts, int type)
Fills the vector pointed to by c with nofElts NULL-values of type.
Definition: null.c:35
void * Rast3d_malloc(int nBytes)
Same as malloc (nBytes), except that in case of error Rast3d_error() is invoked.
Definition: alloc.c:22
void * Rast3d_realloc(void *ptr, int nBytes)
Same as realloc (ptr, nBytes), except that in case of error Rast3d_error() is invoked.
Definition: alloc.c:48
void Rast3d_error(const char *msg,...)
Definition: error.c:71
void * xdr
Definition: header.c:13
int xdrLength
Definition: header.c:14
int Rast3d_length(int t)
Definition: misc.c:78
void Rast3d_copy_values(const void *src, int offsSrc, int typeSrc, void *dst, int offsDst, int typeDst, int nElts)
Definition: misc.c:53
int Rast3d_extern_length(int t)
Definition: misc.c:90
#define RASTER3D_USE_XDR
#define RASTER3D_XDR_DOUBLE_LENGTH
#define RASTER3D_NO_XDR
#define RASTER3D_XDR_FLOAT_LENGTH
#define RASTER3D_MAX(a, b)
#define FCELL_TYPE
Definition: raster.h:12
int numLengthExtern
Definition: raster3d.h:169
int tileSize
Definition: raster3d.h:176
int numLengthIntern
Definition: raster3d.h:172
static float d[4][4]
Definition: trans.c:49