1 /*
2
3 test18.c -- RasterLite2 Test Case
4
5 Author: Alessandro Furieri <a.furieri@lqt.it>
6
7 ------------------------------------------------------------------------------
8
9 Version: MPL 1.1/GPL 2.0/LGPL 2.1
10
11 The contents of this file are subject to the Mozilla Public License Version
12 1.1 (the "License"); you may not use this file except in compliance with
13 the License. You may obtain a copy of the License at
14 http://www.mozilla.org/MPL/
15
16 Software distributed under the License is distributed on an "AS IS" basis,
17 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
18 for the specific language governing rights and limitations under the
19 License.
20
21 The Original Code is the SpatiaLite library
22
23 The Initial Developer of the Original Code is Alessandro Furieri
24
25 Portions created by the Initial Developer are Copyright (C) 2013
26 the Initial Developer. All Rights Reserved.
27
28 Contributor(s):
29
30 Alternatively, the contents of this file may be used under the terms of
31 either the GNU General Public License Version 2 or later (the "GPL"), or
32 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33 in which case the provisions of the GPL or the LGPL are applicable instead
34 of those above. If you wish to allow use of your version of this file only
35 under the terms of either the GPL or the LGPL, and not to allow others to
36 use your version of this file under the terms of the MPL, indicate your
37 decision by deleting the provisions above and replace them with the notice
38 and other provisions required by the GPL or the LGPL. If you do not delete
39 the provisions above, a recipient may use your version of this file under
40 the terms of any one of the MPL, the GPL or the LGPL.
41 */
42 #include <stdlib.h>
43 #include <stdio.h>
44
45 #include "rasterlite2/rasterlite2.h"
46
47 static int
antiEndian()48 antiEndian ()
49 {
50 /* ensures to always encode in the opposite endian order */
51 union cvt
52 {
53 unsigned char byte[4];
54 int int_value;
55 } convert;
56 convert.int_value = 1;
57 if (convert.byte[0] == 0)
58 return 1;
59 return 0;
60 }
61
62 static int
naturalEndian()63 naturalEndian ()
64 {
65 /* ensures to always encode in the natural endian order */
66 union cvt
67 {
68 unsigned char byte[4];
69 int int_value;
70 } convert;
71 convert.int_value = 1;
72 if (convert.byte[0] == 0)
73 return 0;
74 return 1;
75 }
76
77 static rl2SectionPtr
create_grid_double()78 create_grid_double ()
79 {
80 /* creating a synthetic "grid_dbl" image */
81 rl2SectionPtr scn;
82 rl2RasterPtr rst;
83 int row;
84 int col;
85 int bufsize = 1024 * 1024 * sizeof (double);
86 double sample = -12000.5;
87 unsigned char *bufpix = malloc (bufsize);
88 double *p = (double *) bufpix;
89
90 if (bufpix == NULL)
91 return NULL;
92
93 for (row = 0; row < 1024; row++)
94 {
95 for (col = 0; col < 1024; col++)
96 {
97 *p++ = sample;
98 sample += 0.5;
99 }
100 }
101
102 rst =
103 rl2_create_raster (1024, 1024, RL2_SAMPLE_DOUBLE, RL2_PIXEL_DATAGRID, 1,
104 bufpix, bufsize, NULL, NULL, 0, NULL);
105 if (rst == NULL)
106 goto error;
107
108 scn = rl2_create_section ("grid_dbl", RL2_COMPRESSION_NONE,
109 RL2_TILESIZE_UNDEFINED, RL2_TILESIZE_UNDEFINED,
110 rst);
111 if (scn == NULL)
112 rl2_destroy_raster (rst);
113 return scn;
114 error:
115 free (bufpix);
116 return NULL;
117 }
118
119 static int
test_grid_double(rl2SectionPtr img)120 test_grid_double (rl2SectionPtr img)
121 {
122 /* testing GRID-DOUBLE buffer functions */
123 unsigned char *ubuffer;
124 double *buffer;
125 int buf_size;
126 int width = 1024;
127 double *p_data1;
128 double *p_data2;
129 double sample;
130 unsigned char sample_type;
131 unsigned char pixel_type;
132 unsigned char num_bands;
133 int is_transparent;
134 rl2PixelPtr pxl;
135 rl2RasterPtr rst;
136
137 rst = rl2_get_section_raster (img);
138 if (rst == NULL)
139 {
140 fprintf (stderr, "GRID-DBL invalid raster pointer\n");
141 return 0;
142 }
143
144 if (rl2_raster_data_to_RGB (rst, &ubuffer, &buf_size) != RL2_ERROR)
145 {
146 fprintf (stderr, "Unexpected result - get RGB data: GRID-DBL\n");
147 return 0;
148 }
149
150 if (rl2_raster_data_to_RGBA (rst, &ubuffer, &buf_size) != RL2_ERROR)
151 {
152 fprintf (stderr, "Unexpected result - get RGBA data: GRID-DBL\n");
153 return 0;
154 }
155
156 if (rl2_raster_data_to_ARGB (rst, &ubuffer, &buf_size) != RL2_ERROR)
157 {
158 fprintf (stderr, "Unexpected result - get ARGB data: GRID-DBL\n");
159 return 0;
160 }
161
162 if (rl2_raster_data_to_BGR (rst, &ubuffer, &buf_size) != RL2_ERROR)
163 {
164 fprintf (stderr, "Unexpected result - get BGR data: GRID-DBL\n");
165 return 0;
166 }
167
168 if (rl2_raster_data_to_BGRA (rst, &ubuffer, &buf_size) != RL2_ERROR)
169 {
170 fprintf (stderr, "Unexpected result - get BGRA data: GRID-DBL\n");
171 return 0;
172 }
173
174 if (rl2_raster_data_to_double (rst, &buffer, &buf_size) != RL2_OK)
175 {
176 fprintf (stderr, "Unable to get DOUBLE data: GRID-DBL\n");
177 return 0;
178 }
179 p_data1 = buffer + (20 * width) + 20;
180 p_data2 = buffer + (720 * width) + 510;
181 if (*p_data1 != -1750.5)
182 {
183 fprintf (stderr, "Unexpected DOUBLE pixel #1: GRID-DBL\n");
184 return 0;
185 }
186 if (*p_data2 != 356894.5)
187 {
188 fprintf (stderr, "Unexpected DOUBLE pixel #2: GRID-DBL\n");
189 return 0;
190 }
191 rl2_free (buffer);
192
193 pxl = rl2_create_raster_pixel (rst);
194 if (pxl == NULL)
195 {
196 fprintf (stderr, "Unable to create Pixel for Raster: GRID-DBL\n");
197 return 0;
198 }
199
200 if (rl2_get_raster_pixel (rst, pxl, 20, 20) != RL2_OK)
201 {
202 fprintf (stderr,
203 "Unable to get Pixel (20,20) from Raster: GRID-DBL\n");
204 return 0;
205 }
206
207 if (rl2_get_pixel_type (pxl, &sample_type, &pixel_type, &num_bands) !=
208 RL2_OK)
209 {
210 fprintf (stderr, "Unable to create get Pixel Type: GRID-DBL\n");
211 return 0;
212 }
213
214 if (sample_type != RL2_SAMPLE_DOUBLE)
215 {
216 fprintf (stderr, "Unexpected Pixel SampleType: GRID-DBL\n");
217 return 0;
218 }
219
220 if (pixel_type != RL2_PIXEL_DATAGRID)
221 {
222 fprintf (stderr, "Unexpected Pixel Type: GRID-DBL\n");
223 return 0;
224 }
225
226 if (num_bands != 1)
227 {
228 fprintf (stderr, "Unexpected Pixel # Bands: GRID-DBL\n");
229 return 0;
230 }
231
232 if (rl2_get_pixel_sample_double (pxl, &sample) != RL2_OK)
233 {
234 fprintf (stderr, "Unexpected Pixel DOUBLE: GRID-DBL\n");
235 return 0;
236 }
237
238 if (sample != -1750.5)
239 {
240 fprintf (stderr,
241 "Unexpected Pixel DOUBLE Sample value %1.f: GRID-DBL\n",
242 sample);
243 return 0;
244 }
245
246 if (rl2_is_pixel_transparent (pxl, &is_transparent) != RL2_OK)
247 {
248 fprintf (stderr, "Unable to get Pixel Transparency: GRID-DBL\n");
249 return 0;
250 }
251
252 if (is_transparent != RL2_FALSE)
253 {
254 fprintf (stderr, "Unexpected Pixel Transparency: GRID-DBL\n");
255 return 0;
256 }
257
258 if (rl2_is_pixel_opaque (pxl, &is_transparent) != RL2_OK)
259
260 {
261 fprintf (stderr, "Unable to get Pixel Opacity: GRID-DBL\n");
262 return 0;
263 }
264
265 if (is_transparent != RL2_TRUE)
266
267 {
268 fprintf (stderr, "Unexpected Pixel Opacity: GRID-DBL\n");
269 return 0;
270 }
271
272 if (rl2_set_pixel_sample_double (pxl, sample) != RL2_OK)
273 {
274 fprintf (stderr, "Unexpected Pixel SetSample DOUBLE: GRID-DBL\n");
275 return 0;
276 }
277
278 if (rl2_set_raster_pixel (rst, pxl, 20, 20) != RL2_OK)
279 {
280 fprintf (stderr,
281 "Unable to set Pixel (20,20) into Raster: GRID-DBL\n");
282 return 0;
283 }
284
285 rl2_destroy_pixel (pxl);
286
287 return 1;
288 }
289
290 int
main(int argc,char * argv[])291 main (int argc, char *argv[])
292 {
293 rl2SectionPtr img;
294 rl2RasterPtr raster;
295 rl2RasterStatisticsPtr stats;
296 unsigned char *blob_odd;
297 int blob_odd_sz;
298 unsigned char *blob_even;
299 int blob_even_sz;
300 unsigned char *anti_odd;
301 int anti_odd_sz;
302 unsigned char *anti_even;
303 int anti_even_sz;
304 unsigned char *blob_stat;
305 int blob_stat_size;
306 int endian = naturalEndian ();
307 int anti_endian = antiEndian ();
308
309 if (argc > 1 || argv[0] == NULL)
310 argc = 1; /* silencing stupid compiler warnings */
311
312 img = create_grid_double ();
313 if (img == NULL)
314 {
315 fprintf (stderr, "Unable to create image: grid_double\n");
316 return -1;
317 }
318
319 if (!test_grid_double (img))
320 return -2;
321
322 raster = rl2_get_section_raster (img);
323 if (raster == NULL)
324 {
325 fprintf (stderr, "Unable to retrieve the raster pointer\n");
326 return -3;
327 }
328
329 if (rl2_raster_encode
330 (raster, RL2_COMPRESSION_NONE, &blob_odd, &blob_odd_sz, &blob_even,
331 &blob_even_sz, 0, endian) != RL2_OK)
332 {
333 fprintf (stderr, "Unable to Encode - uncompressed\n");
334 return -4;
335 }
336
337 stats =
338 rl2_get_raster_statistics (blob_odd, blob_odd_sz, blob_even,
339 blob_even_sz, NULL, NULL);
340 if (stats == NULL)
341 {
342 fprintf (stderr, "Unable to get Raster Statistics\n");
343 return -100;
344 }
345 if (rl2_serialize_dbms_raster_statistics
346 (stats, &blob_stat, &blob_stat_size) != RL2_OK)
347 {
348 fprintf (stderr, "Unable to serialize Raster Statistics\n");
349 return -101;
350 }
351 rl2_destroy_raster_statistics (stats);
352 stats = rl2_deserialize_dbms_raster_statistics (blob_stat, blob_stat_size);
353 if (stats == NULL)
354 {
355 fprintf (stderr, "Unable to deserialize Raster Statistics\n");
356 return -102;
357 }
358 free (blob_stat);
359 rl2_destroy_raster_statistics (stats);
360
361 if (rl2_raster_encode
362 (raster, RL2_COMPRESSION_NONE, &anti_odd, &anti_odd_sz, &anti_even,
363 &anti_even_sz, 0, anti_endian) != RL2_OK)
364 {
365 fprintf (stderr, "Unable to Encode - uncompressed\n");
366 return -5;
367 }
368
369 rl2_destroy_section (img);
370
371 raster =
372 rl2_raster_decode (RL2_SCALE_1, blob_odd, blob_odd_sz, blob_even,
373 blob_even_sz, NULL);
374 if (raster == NULL)
375 {
376 fprintf (stderr, "Unable to Decode 1:1 - uncompressed\n");
377 return -6;
378 }
379 rl2_destroy_raster (raster);
380
381 raster =
382 rl2_raster_decode (RL2_SCALE_2, blob_odd, blob_odd_sz, blob_even,
383 blob_even_sz, NULL);
384 if (raster == NULL)
385 {
386 fprintf (stderr, "Unable to Decode 1:2 - uncompressed\n");
387 return -7;
388 }
389 rl2_destroy_raster (raster);
390
391 raster =
392 rl2_raster_decode (RL2_SCALE_4, blob_odd, blob_odd_sz, blob_even,
393 blob_even_sz, NULL);
394 if (raster == NULL)
395 {
396 fprintf (stderr, "Unable to Decode 1:4 - uncompressed\n");
397 return -8;
398 }
399 rl2_destroy_raster (raster);
400
401 raster =
402 rl2_raster_decode (RL2_SCALE_8, blob_odd, blob_odd_sz, blob_even,
403 blob_even_sz, NULL);
404 if (raster == NULL)
405 {
406 fprintf (stderr, "Unable to Decode 1:8 - uncompressed\n");
407 return -9;
408 }
409 rl2_destroy_raster (raster);
410 free (blob_odd);
411 free (blob_even);
412
413 raster =
414 rl2_raster_decode (RL2_SCALE_1, anti_odd, anti_odd_sz, anti_even,
415 anti_even_sz, NULL);
416 if (raster == NULL)
417 {
418 fprintf (stderr,
419 "Unable to Decode 1:1 - uncompressed (anti-endian)\n");
420 return -10;
421 }
422 rl2_destroy_raster (raster);
423
424 raster =
425 rl2_raster_decode (RL2_SCALE_2, anti_odd, anti_odd_sz, anti_even,
426 anti_even_sz, NULL);
427 if (raster == NULL)
428 {
429 fprintf (stderr,
430 "Unable to Decode 1:2 - uncompressed (anti-endian)\n");
431 return -11;
432 }
433 rl2_destroy_raster (raster);
434
435 raster =
436 rl2_raster_decode (RL2_SCALE_4, anti_odd, anti_odd_sz, anti_even,
437 anti_even_sz, NULL);
438 if (raster == NULL)
439 {
440 fprintf (stderr,
441 "Unable to Decode 1:4 - uncompressed (anti-endian)\n");
442 return -12;
443 }
444 rl2_destroy_raster (raster);
445
446 raster =
447 rl2_raster_decode (RL2_SCALE_8, anti_odd, anti_odd_sz, anti_even,
448 anti_even_sz, NULL);
449 if (raster == NULL)
450 {
451 fprintf (stderr,
452 "Unable to Decode 1:8 - uncompressed (anti-endian)\n");
453 return -13;
454 }
455 rl2_destroy_raster (raster);
456 free (anti_odd);
457 free (anti_even);
458
459 return 0;
460 }
461