PostGIS  2.5.0dev-r@@SVN_REVISION@@
cu_pixtype.c
Go to the documentation of this file.
1 /*
2  * PostGIS Raster - Raster Types for PostGIS
3  * http://trac.osgeo.org/postgis/wiki/WKTRaster
4  *
5  * Copyright (C) 2012 Regents of the University of California
6  * <bkpark@ucdavis.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  */
23 
24 #include "CUnit/Basic.h"
25 #include "cu_tester.h"
26 
27 static void test_pixtype_size() {
28  CU_ASSERT_EQUAL(rt_pixtype_size(PT_1BB), 1);
29  CU_ASSERT_EQUAL(rt_pixtype_size(PT_2BUI), 1);
30  CU_ASSERT_EQUAL(rt_pixtype_size(PT_4BUI), 1);
31  CU_ASSERT_EQUAL(rt_pixtype_size(PT_8BUI), 1);
32  CU_ASSERT_EQUAL(rt_pixtype_size(PT_8BSI), 1);
33  CU_ASSERT_EQUAL(rt_pixtype_size(PT_16BUI), 2);
34  CU_ASSERT_EQUAL(rt_pixtype_size(PT_16BSI), 2);
35  CU_ASSERT_EQUAL(rt_pixtype_size(PT_32BUI), 4);
36  CU_ASSERT_EQUAL(rt_pixtype_size(PT_32BSI), 4);
37  CU_ASSERT_EQUAL(rt_pixtype_size(PT_32BF), 4);
38  CU_ASSERT_EQUAL(rt_pixtype_size(PT_64BF), 8);
39 
40  CU_ASSERT_EQUAL(rt_pixtype_size(PT_END), -1);
41 }
42 
43 static void test_pixtype_alignment() {
44  /* rt_pixtype_alignment() just forwards to rt_pixtype_size() */
45 }
46 
47 static void test_pixtype_name() {
48  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_1BB), "1BB");
49  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_2BUI), "2BUI");
50  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_4BUI), "4BUI");
51  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_8BUI), "8BUI");
52  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_8BSI), "8BSI");
53  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_16BUI), "16BUI");
54  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_16BSI), "16BSI");
55  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_32BUI), "32BUI");
56  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_32BSI), "32BSI");
57  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_32BF), "32BF");
58  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_64BF), "64BF");
59 
60  CU_ASSERT_STRING_EQUAL(rt_pixtype_name(PT_END), "Unknown");
61 }
62 
64  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("1BB"), PT_1BB);
65  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("2BUI"), PT_2BUI);
66  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("4BUI"), PT_4BUI);
67  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("8BUI"), PT_8BUI);
68  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("8BSI"), PT_8BSI);
69  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("16BUI"), PT_16BUI);
70  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("16BSI"), PT_16BSI);
71  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("32BUI"), PT_32BUI);
72  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("32BSI"), PT_32BSI);
73  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("32BF"), PT_32BF);
74  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("64BF"), PT_64BF);
75 
76  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("END"), PT_END);
77  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("1bb"), PT_END);
78  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("1bB"), PT_END);
79  CU_ASSERT_EQUAL(rt_pixtype_index_from_name("3BUI"), PT_END);
80 }
81 
83  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_1BB), rt_util_clamp_to_1BB((double) CHAR_MIN), DBL_EPSILON);
84  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_2BUI), rt_util_clamp_to_2BUI((double) CHAR_MIN), DBL_EPSILON);
85  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_4BUI), rt_util_clamp_to_4BUI((double) CHAR_MIN), DBL_EPSILON);
86  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_8BUI), rt_util_clamp_to_8BUI((double) CHAR_MIN), DBL_EPSILON);
87  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_8BSI), rt_util_clamp_to_8BSI((double) SCHAR_MIN), DBL_EPSILON);
88  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_16BUI), rt_util_clamp_to_16BUI((double) SHRT_MIN), DBL_EPSILON);
89  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_16BSI), rt_util_clamp_to_16BSI((double) SHRT_MIN), DBL_EPSILON);
90  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_32BUI), rt_util_clamp_to_32BUI((double) INT_MIN), DBL_EPSILON);
91  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_32BSI), rt_util_clamp_to_32BSI((double) INT_MIN), DBL_EPSILON);
92  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_32BF), -FLT_MAX, DBL_EPSILON);
93  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_64BF), -DBL_MAX, DBL_EPSILON);
94 
95  CU_ASSERT_DOUBLE_EQUAL(rt_pixtype_get_min_value(PT_END), rt_util_clamp_to_8BUI((double) CHAR_MIN), DBL_EPSILON);
96 }
97 
99  int isequal = 0;
100 
101  /* 1BB */
102  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_1BB, 0, 0, &isequal), ES_NONE);
103  CU_ASSERT(isequal);
104  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_1BB, 0, 1, &isequal), ES_NONE);
105  CU_ASSERT(!isequal);
106  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_1BB, 1, 0, &isequal), ES_NONE);
107  CU_ASSERT(!isequal);
108  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_1BB, 1, 1, &isequal), ES_NONE);
109  CU_ASSERT(isequal);
110  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_1BB, 0, 2, &isequal), ES_NONE);
111  CU_ASSERT(!isequal);
112  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_1BB, 0, -9999, &isequal), ES_NONE);
113  CU_ASSERT(isequal);
114 
115  /* 2BUI */
116  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_2BUI, 0, 0, &isequal), ES_NONE);
117  CU_ASSERT(isequal);
118  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_2BUI, 0, 1, &isequal), ES_NONE);
119  CU_ASSERT(!isequal);
120  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_2BUI, 0, 3, &isequal), ES_NONE);
121  CU_ASSERT(!isequal);
122  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_2BUI, 1, 1, &isequal), ES_NONE);
123  CU_ASSERT(isequal);
124  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_2BUI, 3, 2, &isequal), ES_NONE);
125  CU_ASSERT(!isequal);
126  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_2BUI, 4, 0, &isequal), ES_NONE);
127  CU_ASSERT(!isequal);
128  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_2BUI, -1, 0, &isequal), ES_NONE);
129  CU_ASSERT(isequal);
130 
131  /* 4BUI */
132  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_4BUI, 10, 10, &isequal), ES_NONE);
133  CU_ASSERT(isequal);
134  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_4BUI, 10, 1, &isequal), ES_NONE);
135  CU_ASSERT(!isequal);
136  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_4BUI, 0, 15, &isequal), ES_NONE);
137  CU_ASSERT(!isequal);
138  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_4BUI, 15, 15, &isequal), ES_NONE);
139  CU_ASSERT(isequal);
140  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_4BUI, 0, 16, &isequal), ES_NONE);
141  CU_ASSERT(!isequal);
142  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_4BUI, 16, 15, &isequal), ES_NONE);
143  CU_ASSERT(isequal);
144 
145  /* 8BUI */
146  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BUI, 155, 155, &isequal), ES_NONE);
147  CU_ASSERT(isequal);
148  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BUI, 155, 255, &isequal), ES_NONE);
149  CU_ASSERT(!isequal);
150  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BUI, 0, 155, &isequal), ES_NONE);
151  CU_ASSERT(!isequal);
152  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BUI, -1, -1, &isequal), ES_NONE);
153  CU_ASSERT(isequal);
154  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BUI, 0, -1, &isequal), ES_NONE);
155  CU_ASSERT(isequal);
156  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BUI, 256, 255, &isequal), ES_NONE);
157  CU_ASSERT(isequal);
158 
159  /* 8BSI */
160  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BSI, 120, 120, &isequal), ES_NONE);
161  CU_ASSERT(isequal);
162  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BSI, -120, 120, &isequal), ES_NONE);
163  CU_ASSERT(!isequal);
164  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BSI, -10, -10, &isequal), ES_NONE);
165  CU_ASSERT(isequal);
166  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BSI, -128, -128, &isequal), ES_NONE);
167  CU_ASSERT(isequal);
168  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BSI, -128, 128, &isequal), ES_NONE);
169  CU_ASSERT(!isequal);
170  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BSI, -129, -128, &isequal), ES_NONE);
171  CU_ASSERT(isequal);
172  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_8BSI, 129, 128, &isequal), ES_NONE);
173  CU_ASSERT(isequal);
174 
175  /* 16BUI */
176  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BUI, 65535, 65535, &isequal), ES_NONE);
177  CU_ASSERT(isequal);
178  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BUI, 0, 0, &isequal), ES_NONE);
179  CU_ASSERT(isequal);
180  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BUI, 12345, 12344, &isequal), ES_NONE);
181  CU_ASSERT(!isequal);
182  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BUI, 0, 65535, &isequal), ES_NONE);
183  CU_ASSERT(!isequal);
184  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BUI, 65536, -1, &isequal), ES_NONE);
185  CU_ASSERT(!isequal);
186  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BUI, -9999, 0, &isequal), ES_NONE);
187  CU_ASSERT(isequal);
188 
189  /* 16BSI */
190  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BSI, -32000, -32000, &isequal), ES_NONE);
191  CU_ASSERT(isequal);
192  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BSI, -32767, -32767, &isequal), ES_NONE);
193  CU_ASSERT(isequal);
194  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BSI, 32767, 32768, &isequal), ES_NONE);
195  CU_ASSERT(isequal);
196  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BSI, 32766, 32768, &isequal), ES_NONE);
197  CU_ASSERT(!isequal);
198  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BSI, 0, -32768, &isequal), ES_NONE);
199  CU_ASSERT(!isequal);
200  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_16BSI, 32767, -32767, &isequal), ES_NONE);
201  CU_ASSERT(!isequal);
202 
203  /* 32BUI */
204  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_32BUI, 4294967295UL, 4294967295UL, &isequal), ES_NONE);
205  CU_ASSERT(isequal);
206  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_32BUI, 4294967296ULL, 4294967295UL, &isequal), ES_NONE);
207  CU_ASSERT(isequal);
208 
209  /* 32BSI */
210  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_32BSI, 2147483647, 2147483647, &isequal), ES_NONE);
211  CU_ASSERT(isequal);
212  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_32BSI, 2147483648UL, 2147483647, &isequal), ES_NONE);
213  CU_ASSERT(isequal);
214 
215  /* 32BF */
216  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_32BF, 65535.5, 65535.5, &isequal), ES_NONE);
217  CU_ASSERT(isequal);
218  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_32BF, 0.0060000000521540, 0.0060000000521540, &isequal), ES_NONE);
219  CU_ASSERT(isequal);
220 
221  /* 64BF */
222  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_64BF, 65535.5, 65535.5, &isequal), ES_NONE);
223  CU_ASSERT(isequal);
224  CU_ASSERT_EQUAL(rt_pixtype_compare_clamped_values(PT_64BF, 0.0060000000521540, 0.0060000000521540, &isequal), ES_NONE);
225  CU_ASSERT(isequal);
226 
227  CU_ASSERT_NOT_EQUAL(rt_pixtype_compare_clamped_values(PT_END, 1, 1, &isequal), ES_NONE);
228 }
229 
230 /* register tests */
231 void pixtype_suite_setup(void);
233 {
234  CU_pSuite suite = CU_add_suite("pixtype", NULL, NULL);
241 }
242 
int32_t rt_util_clamp_to_32BSI(double value)
Definition: rt_util.c:69
static void test_pixtype_size()
Definition: cu_pixtype.c:27
uint32_t rt_util_clamp_to_32BUI(double value)
Definition: rt_util.c:74
void pixtype_suite_setup(void)
Definition: cu_pixtype.c:232
static void test_pixtype_get_min_value()
Definition: cu_pixtype.c:82
uint16_t rt_util_clamp_to_16BUI(double value)
Definition: rt_util.c:64
rt_pixtype rt_pixtype_index_from_name(const char *pixname)
Definition: rt_pixel.c:80
#define PG_ADD_TEST(suite, testfunc)
int8_t rt_util_clamp_to_8BSI(double value)
Definition: rt_util.c:49
static void test_pixtype_name()
Definition: cu_pixtype.c:47
static void test_pixtype_compare_clamped_values()
Definition: cu_pixtype.c:98
double rt_pixtype_get_min_value(rt_pixtype pixtype)
Return minimum value possible for pixel type.
Definition: rt_pixel.c:148
static void test_pixtype_index_from_name()
Definition: cu_pixtype.c:63
uint8_t rt_util_clamp_to_4BUI(double value)
Definition: rt_util.c:44
uint8_t rt_util_clamp_to_8BUI(double value)
Definition: rt_util.c:54
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition: rt_pixel.c:39
uint8_t rt_util_clamp_to_1BB(double value)
Definition: rt_util.c:34
static void test_pixtype_alignment()
Definition: cu_pixtype.c:43
const char * rt_pixtype_name(rt_pixtype pixtype)
Definition: rt_pixel.c:110
uint8_t rt_util_clamp_to_2BUI(double value)
Definition: rt_util.c:39
rt_errorstate rt_pixtype_compare_clamped_values(rt_pixtype pixtype, double val, double refval, int *isequal)
Test to see if two values are equal when clamped.
Definition: rt_pixel.c:200
int16_t rt_util_clamp_to_16BSI(double value)
Definition: rt_util.c:59