PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ test_raster_colormap()

static void test_raster_colormap ( )
static

Definition at line 882 of file cu_mapalgebra.c.

882 {
884 rt_raster rtn;
886 int x;
887 int y;
888 rt_colormap colormap = NULL;
889 double value;
890 int nodata;
891
892 raster = rt_raster_new(9, 9);
893 CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
894 band = cu_add_band(raster, PT_8BUI, 0, 0);
895 CU_ASSERT(band != NULL);
896 rt_band_set_nodata(band, 0, NULL);
897
898 for (y = 0; y < 9; y++) {
899 for (x = 0; x < 9; x++) {
900 rt_band_set_pixel(band, x, y, x, NULL);
901 }
902 }
903
904 colormap = (rt_colormap) rtalloc(sizeof(struct rt_colormap_t));
905 CU_ASSERT(colormap != NULL);
906 colormap->nentry = 3;
907 colormap->entry = (rt_colormap_entry) rtalloc(sizeof(struct rt_colormap_entry_t) * colormap->nentry);
908 CU_ASSERT(colormap->entry != NULL);
909
910 colormap->entry[0].isnodata = 0;
911 colormap->entry[0].value = 8;
912 colormap->entry[0].color[0] = 255;
913 colormap->entry[0].color[1] = 255;
914 colormap->entry[0].color[2] = 255;
915 colormap->entry[0].color[3] = 255;
916
917 colormap->entry[1].isnodata = 0;
918 colormap->entry[1].value = 3;
919 colormap->entry[1].color[0] = 127;
920 colormap->entry[1].color[1] = 127;
921 colormap->entry[1].color[2] = 127;
922 colormap->entry[1].color[3] = 255;
923
924 colormap->entry[2].isnodata = 0;
925 colormap->entry[2].value = 0;
926 colormap->entry[2].color[0] = 0;
927 colormap->entry[2].color[1] = 0;
928 colormap->entry[2].color[2] = 0;
929 colormap->entry[2].color[3] = 255;
930
931 /* 2 colors, 3 entries, INTERPOLATE */
932 colormap->ncolor = 2;
933 colormap->method = CM_INTERPOLATE;
934
935 rtn = rt_raster_colormap(
936 raster, 0,
937 colormap
938 );
939 CU_ASSERT(rtn != NULL);
940 CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
941
942 band = rt_raster_get_band(rtn, 0);
943 CU_ASSERT(band != NULL);
944 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
945 CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
946 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
947 CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
948 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
949 CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
950
951 cu_free_raster(rtn);
952
953 /* 4 colors, 3 entries, INTERPOLATE */
954 colormap->ncolor = 4;
955
956 rtn = rt_raster_colormap(
957 raster, 0,
958 colormap
959 );
960 CU_ASSERT(rtn != NULL);
961 CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
962 cu_free_raster(rtn);
963
964 /* 4 colors, 3 entries, EXACT */
965 colormap->method = CM_EXACT;
966
967 rtn = rt_raster_colormap(
968 raster, 0,
969 colormap
970 );
971 CU_ASSERT(rtn != NULL);
972 CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
973
974 band = rt_raster_get_band(rtn, 0);
975 CU_ASSERT(band != NULL);
976 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
977 CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
978 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
979 CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
980 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
981 CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
982 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 1, 0, &value, &nodata), ES_NONE);
983 CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
984 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 0, &value, &nodata), ES_NONE);
985 CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
986
987 cu_free_raster(rtn);
988
989 /* 4 colors, 3 entries, NEAREST */
990 colormap->method = CM_NEAREST;
991
992 rtn = rt_raster_colormap(
993 raster, 0,
994 colormap
995 );
996 CU_ASSERT(rtn != NULL);
997 CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
998
999 band = rt_raster_get_band(rtn, 0);
1000 CU_ASSERT(band != NULL);
1001 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
1002 CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
1003 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
1004 CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1005 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
1006 CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1007 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 1, 0, &value, &nodata), ES_NONE);
1008 CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
1009 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 2, 0, &value, &nodata), ES_NONE);
1010 CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1011 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 4, 0, &value, &nodata), ES_NONE);
1012 CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1013 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 0, &value, &nodata), ES_NONE);
1014 CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1015
1016 cu_free_raster(rtn);
1017
1018 /* 4 colors, 2 entries, NEAREST */
1019 colormap->nentry = 2;
1020 colormap->method = CM_NEAREST;
1021
1022 rtn = rt_raster_colormap(
1023 raster, 0,
1024 colormap
1025 );
1026 CU_ASSERT(rtn != NULL);
1027 CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
1028
1029 band = rt_raster_get_band(rtn, 0);
1030 CU_ASSERT(band != NULL);
1031 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
1032 CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1033 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
1034 CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1035 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
1036 CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1037 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 1, 0, &value, &nodata), ES_NONE);
1038 CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1039 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 2, 0, &value, &nodata), ES_NONE);
1040 CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1041 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 4, 0, &value, &nodata), ES_NONE);
1042 CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1043 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 0, &value, &nodata), ES_NONE);
1044 CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1045
1046 cu_free_raster(rtn);
1047
1048 rtdealloc(colormap->entry);
1049 rtdealloc(colormap);
1050
1051 cu_free_raster(raster);
1052
1053 /* new set of tests */
1054 raster = rt_raster_new(10, 10);
1055 CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
1056 band = cu_add_band(raster, PT_8BUI, 0, 0);
1057 CU_ASSERT(band != NULL);
1058 rt_band_set_nodata(band, 0, NULL);
1059
1060 for (y = 0; y < 10; y++) {
1061 for (x = 0; x < 10; x++) {
1062 rt_band_set_pixel(band, x, y, (x * y) + x, NULL);
1063 }
1064 }
1065
1066 colormap = (rt_colormap) rtalloc(sizeof(struct rt_colormap_t));
1067 CU_ASSERT(colormap != NULL);
1068 colormap->nentry = 10;
1069 colormap->entry = (rt_colormap_entry) rtalloc(sizeof(struct rt_colormap_entry_t) * colormap->nentry);
1070 CU_ASSERT(colormap->entry != NULL);
1071
1072 colormap->entry[0].isnodata = 0;
1073 colormap->entry[0].value = 90;
1074 colormap->entry[0].color[0] = 255;
1075 colormap->entry[0].color[1] = 255;
1076 colormap->entry[0].color[2] = 255;
1077 colormap->entry[0].color[3] = 255;
1078
1079 colormap->entry[1].isnodata = 0;
1080 colormap->entry[1].value = 80;
1081 colormap->entry[1].color[0] = 255;
1082 colormap->entry[1].color[1] = 227;
1083 colormap->entry[1].color[2] = 227;
1084 colormap->entry[1].color[3] = 255;
1085
1086 colormap->entry[2].isnodata = 0;
1087 colormap->entry[2].value = 70;
1088 colormap->entry[2].color[0] = 255;
1089 colormap->entry[2].color[1] = 198;
1090 colormap->entry[2].color[2] = 198;
1091 colormap->entry[2].color[3] = 255;
1092
1093 colormap->entry[3].isnodata = 0;
1094 colormap->entry[3].value = 60;
1095 colormap->entry[3].color[0] = 255;
1096 colormap->entry[3].color[1] = 170;
1097 colormap->entry[3].color[2] = 170;
1098 colormap->entry[3].color[3] = 255;
1099
1100 colormap->entry[4].isnodata = 0;
1101 colormap->entry[4].value = 50;
1102 colormap->entry[4].color[0] = 255;
1103 colormap->entry[4].color[1] = 142;
1104 colormap->entry[4].color[2] = 142;
1105 colormap->entry[4].color[3] = 255;
1106
1107 colormap->entry[5].isnodata = 0;
1108 colormap->entry[5].value = 40;
1109 colormap->entry[5].color[0] = 255;
1110 colormap->entry[5].color[1] = 113;
1111 colormap->entry[5].color[2] = 113;
1112 colormap->entry[5].color[3] = 255;
1113
1114 colormap->entry[6].isnodata = 0;
1115 colormap->entry[6].value = 30;
1116 colormap->entry[6].color[0] = 255;
1117 colormap->entry[6].color[1] = 85;
1118 colormap->entry[6].color[2] = 85;
1119 colormap->entry[6].color[3] = 255;
1120
1121 colormap->entry[7].isnodata = 0;
1122 colormap->entry[7].value = 20;
1123 colormap->entry[7].color[0] = 255;
1124 colormap->entry[7].color[1] = 57;
1125 colormap->entry[7].color[2] = 57;
1126 colormap->entry[7].color[3] = 255;
1127
1128 colormap->entry[8].isnodata = 0;
1129 colormap->entry[8].value = 10;
1130 colormap->entry[8].color[0] = 255;
1131 colormap->entry[8].color[1] = 28;
1132 colormap->entry[8].color[2] = 28;
1133 colormap->entry[8].color[3] = 255;
1134
1135 colormap->entry[9].isnodata = 0;
1136 colormap->entry[9].value = 0;
1137 colormap->entry[9].color[0] = 255;
1138 colormap->entry[9].color[1] = 0;
1139 colormap->entry[9].color[2] = 0;
1140 colormap->entry[9].color[3] = 255;
1141
1142 /* 2 colors, 3 entries, INTERPOLATE */
1143 colormap->ncolor = 4;
1144 colormap->method = CM_INTERPOLATE;
1145
1146 rtn = rt_raster_colormap(
1147 raster, 0,
1148 colormap
1149 );
1150 CU_ASSERT(rtn != NULL);
1151 CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
1152
1153 band = rt_raster_get_band(rtn, 2);
1154 CU_ASSERT(band != NULL);
1155 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
1156 CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
1157 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 5, 0, &value, &nodata), ES_NONE);
1158 CU_ASSERT_DOUBLE_EQUAL(value, 14, DBL_EPSILON);
1159 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 6, 0, &value, &nodata), ES_NONE);
1160 CU_ASSERT_DOUBLE_EQUAL(value, 17, DBL_EPSILON);
1161 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 9, 0, &value, &nodata), ES_NONE);
1162 CU_ASSERT_DOUBLE_EQUAL(value, 25, DBL_EPSILON);
1163
1164 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 2, 4, &value, &nodata), ES_NONE);
1165 CU_ASSERT_DOUBLE_EQUAL(value, 28, DBL_EPSILON);
1166 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 4, &value, &nodata), ES_NONE);
1167 CU_ASSERT_DOUBLE_EQUAL(value, 43, DBL_EPSILON);
1168 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 4, 4, &value, &nodata), ES_NONE);
1169 CU_ASSERT_DOUBLE_EQUAL(value, 57, DBL_EPSILON);
1170
1171 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 6, 9, &value, &nodata), ES_NONE);
1172 CU_ASSERT_DOUBLE_EQUAL(value, 170, DBL_EPSILON);
1173 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 9, &value, &nodata), ES_NONE);
1174 CU_ASSERT_DOUBLE_EQUAL(value, 198, DBL_EPSILON);
1175 CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 9, &value, &nodata), ES_NONE);
1176 CU_ASSERT_DOUBLE_EQUAL(value, 227, DBL_EPSILON);
1177
1178 cu_free_raster(rtn);
1179
1180 rtdealloc(colormap->entry);
1181 rtdealloc(colormap);
1182
1183 cu_free_raster(raster);
1184}
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition rt_context.c:191
rt_raster rt_raster_colormap(rt_raster raster, int nband, rt_colormap colormap)
Returns a new raster with up to four 8BUI bands (RGBA) from applying a colormap to the user-specified...
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition rt_band.c:1551
@ PT_8BUI
Definition librtcore.h:193
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition rt_raster.c:52
struct rt_colormap_entry_t * rt_colormap_entry
Definition librtcore.h:162
rt_errorstate rt_band_set_nodata(rt_band band, double val, int *converted)
Set nodata value.
Definition rt_band.c:892
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
Definition rt_band.c:1140
@ ES_NONE
Definition librtcore.h:182
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition rt_raster.c:376
void rtdealloc(void *mem)
Definition rt_context.c:206
struct rt_colormap_t * rt_colormap
Definition librtcore.h:163
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition rt_raster.c:385
int value
Definition genraster.py:62
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition rtrowdump.py:125
rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
void cu_free_raster(rt_raster raster)
uint8_t color[4]
Definition librtcore.h:2700
double value
Definition librtcore.h:2699
int isnodata
Definition librtcore.h:2698
Definition librtcore.h:2697
rt_colormap_entry entry
Definition librtcore.h:2712
uint16_t nentry
Definition librtcore.h:2711
enum rt_colormap_t::@13 method

References rt_colormap_entry_t::color, cu_add_band(), cu_free_raster(), rt_colormap_t::entry, ES_NONE, rt_colormap_entry_t::isnodata, rt_colormap_t::method, rt_colormap_t::ncolor, rt_colormap_t::nentry, PT_8BUI, rt_band_get_pixel(), rt_band_set_nodata(), rt_band_set_pixel(), rt_raster_colormap(), rt_raster_get_band(), rt_raster_get_num_bands(), rt_raster_new(), rtalloc(), rtdealloc(), and rt_colormap_entry_t::value.

Referenced by mapalgebra_suite_setup().

Here is the call graph for this function:
Here is the caller graph for this function: