| 1 | /* cluster.c |
| 2 | * Copyright (C) 2008 binarymillenium |
| 3 | * This file is a Frei0r plugin. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program 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 General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <assert.h> |
| 22 | #include <math.h> |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | #include "frei0r.h"cxx . . $ `<file>/include/frei0r.h`/ |
| 27 | #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/ |
| 28 | |
| 29 | #define MAXNUMcxx . . $ `src/filter/cluster/cluster.c:29:9`! 40 |
| 30 | |
| 31 | struct cluster_centercxx . . $ cluster_center# |
| 32 | { |
| 33 | int xcxx . . $ cluster_center#x.; |
| 34 | int ycxx . . $ cluster_center#y.; |
| 35 | |
| 36 | |
| 37 | unsigned char rcxx . . $ cluster_center#r.; |
| 38 | unsigned char gcxx . . $ cluster_center#g.; |
| 39 | unsigned char bcxx . . $ cluster_center#b.; |
| 40 | |
| 41 | |
| 42 | /// aggregate color and positions |
| 43 | float aggr_rcxx . . $ cluster_center#aggr_r.; |
| 44 | float aggr_gcxx . . $ cluster_center#aggr_g.; |
| 45 | float aggr_bcxx . . $ cluster_center#aggr_b.; |
| 46 | float aggr_xcxx . . $ cluster_center#aggr_x.; |
| 47 | float aggr_ycxx . . $ cluster_center#aggr_y.; |
| 48 | |
| 49 | /// number of pixels in the cluster |
| 50 | float numpixcxx . . $ cluster_center#numpix.; |
| 51 | }; |
| 52 | |
| 53 | typedef struct cluster_instancecxx . . $ cluster_instance# |
| 54 | { |
| 55 | unsigned int widthcxx . . $ cluster_instance#width.; |
| 56 | unsigned int heightcxx . . $ cluster_instance#height.; |
| 57 | |
| 58 | /// number of clusters, must be smaller than maxnum |
| 59 | unsigned int numcxx . . $ cluster_instance#num.; |
| 60 | float dist_weightcxx . . $ cluster_instance#dist_weight.; |
| 61 | //float color_weight; |
| 62 | |
| 63 | struct cluster_centercxx . . $ cluster_center# clusterscxx . . $ cluster_instance#clusters.[MAXNUMcxx . . $ `src/filter/cluster/cluster.c:29:9`!]; |
| 64 | } cluster_instance_tcxx . . $ cluster_instance_t#; |
| 65 | |
| 66 | |
| 67 | int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).() |
| 68 | { |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).() |
| 73 | { /* no initialization required */ } |
| 74 | |
| 75 | void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).(f0r_plugin_info_tcxx . . $ f0r_plugin_info_t#* inverterInfo) |
| 76 | { |
| 77 | inverterInfo->namecxx . . $ f0r_plugin_info#name. = "K-Means Clustering"; |
| 78 | inverterInfo->authorcxx . . $ f0r_plugin_info#author. = "binarymillenium"; |
| 79 | inverterInfo->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_FILTERcxx . . $ `include/frei0r.h:247:9`!; |
| 80 | inverterInfo->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!; |
| 81 | inverterInfo->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!; |
| 82 | inverterInfo->major_versioncxx . . $ f0r_plugin_info#major_version. = 0; |
| 83 | inverterInfo->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 1; |
| 84 | inverterInfo->num_paramscxx . . $ f0r_plugin_info#num_params. = 2; |
| 85 | inverterInfo->explanationcxx . . $ f0r_plugin_info#explanation. = "Clusters of a source image by color and spatial distance"; |
| 86 | } |
| 87 | |
| 88 | void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).(f0r_param_info_tcxx . . $ f0r_param_info_t#* info, int param_index) |
| 89 | { |
| 90 | switch(param_index) |
| 91 | { |
| 92 | case 0: |
| 93 | info->namecxx . . $ f0r_param_info#name. = "Num"; |
| 94 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!; |
| 95 | info->explanationcxx . . $ f0r_param_info#explanation. = "The number of clusters"; |
| 96 | break; |
| 97 | case 1: |
| 98 | info->namecxx . . $ f0r_param_info#name. = "Dist weight"; |
| 99 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!; |
| 100 | info->explanationcxx . . $ f0r_param_info#explanation. = "The weight on distance"; |
| 101 | break; |
| 102 | #if 0 |
| 103 | case 2: |
| 104 | info->name = "Color weight"; |
| 105 | info->type = F0R_PARAM_DOUBLE; |
| 106 | info->explanation = "The weight on color"; |
| 107 | break; |
| 108 | #endif |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | f0r_instance_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned int width, unsigned int height) |
| 113 | { |
| 114 | cluster_instance_tcxx . . $ cluster_instance_t#* inst = (cluster_instance_tcxx . . $ cluster_instance_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*inst)); |
| 115 | |
| 116 | inst->widthcxx . . $ cluster_instance#width. = width; inst->heightcxx . . $ cluster_instance#height. = height; |
| 117 | |
| 118 | inst->numcxx . . $ cluster_instance#num. = MAXNUMcxx . . $ `src/filter/cluster/cluster.c:29:9`!/2; |
| 119 | inst->dist_weightcxx . . $ cluster_instance#dist_weight. = 0.5; |
| 120 | //inst->color_weight = 1.0; |
| 121 | |
| 122 | int k; |
| 123 | for (k = 0; k < MAXNUMcxx . . $ `src/filter/cluster/cluster.c:29:9`!; k++) { |
| 124 | struct cluster_centercxx . . $ cluster_center#* cc = &inst->clusterscxx . . $ cluster_instance#clusters.[k]; |
| 125 | |
| 126 | int x = randcxx . . $ rand(b126dc7c1de90089).()%inst->widthcxx . . $ cluster_instance#width.; |
| 127 | int y = randcxx . . $ rand(b126dc7c1de90089).()%inst->heightcxx . . $ cluster_instance#height.; |
| 128 | |
| 129 | cc->xcxx . . $ cluster_center#x. = x; |
| 130 | cc->ycxx . . $ cluster_center#y. = y; |
| 131 | |
| 132 | /* |
| 133 | const unsigned char* src2 = (unsigned char*)(&inframe[x+inst->width*y]); |
| 134 | |
| 135 | inst->clusters[k].r = src2[0]; |
| 136 | inst->clusters[k].g = src2[1]; |
| 137 | inst->clusters[k].b = src2[2]; |
| 138 | */ |
| 139 | inst->clusterscxx . . $ cluster_instance#clusters.[k].rcxx . . $ cluster_center#r. = randcxx . . $ rand(b126dc7c1de90089).()%255; |
| 140 | inst->clusterscxx . . $ cluster_instance#clusters.[k].gcxx . . $ cluster_center#g. = randcxx . . $ rand(b126dc7c1de90089).()%255; |
| 141 | inst->clusterscxx . . $ cluster_instance#clusters.[k].bcxx . . $ cluster_center#b. = randcxx . . $ rand(b126dc7c1de90089).()%255; |
| 142 | |
| 143 | cc->numpixcxx . . $ cluster_center#numpix. = 0; |
| 144 | cc->aggr_xcxx . . $ cluster_center#aggr_x. = 0; |
| 145 | cc->aggr_ycxx . . $ cluster_center#aggr_y. = 0; |
| 146 | cc->aggr_rcxx . . $ cluster_center#aggr_r. = 0; |
| 147 | cc->aggr_gcxx . . $ cluster_center#aggr_g. = 0; |
| 148 | cc->aggr_bcxx . . $ cluster_center#aggr_b. = 0; |
| 149 | } |
| 150 | |
| 151 | return (f0r_instance_tcxx . . $ f0r_instance_t#)inst; |
| 152 | } |
| 153 | |
| 154 | void f0r_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance) |
| 155 | { |
| 156 | freecxx . . $ free(5d22bdacc48458e8).(instance); |
| 157 | } |
| 158 | |
| 159 | void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, |
| 160 | f0r_param_tcxx . . $ f0r_param_t# param, int param_index) |
| 161 | { |
| 162 | assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance); |
| 163 | cluster_instance_tcxx . . $ cluster_instance_t#* inst = (cluster_instance_tcxx . . $ cluster_instance_t#*)instance; |
| 164 | |
| 165 | switch(param_index) |
| 166 | { |
| 167 | unsigned int val; |
| 168 | float fval; |
| 169 | case 0: |
| 170 | /* val is 0-1.0 */ |
| 171 | fval = ((*((double*)param) )); |
| 172 | |
| 173 | val = (int) (fval*MAXNUMcxx . . $ `src/filter/cluster/cluster.c:29:9`!); |
| 174 | |
| 175 | if (val > MAXNUMcxx . . $ `src/filter/cluster/cluster.c:29:9`!) val = MAXNUMcxx . . $ `src/filter/cluster/cluster.c:29:9`!; |
| 176 | if (val < 0) val = 0; |
| 177 | |
| 178 | if (val != inst->numcxx . . $ cluster_instance#num.) |
| 179 | { |
| 180 | inst->numcxx . . $ cluster_instance#num. = val; |
| 181 | } |
| 182 | break; |
| 183 | |
| 184 | case 1: |
| 185 | /* val is 0-1.0 */ |
| 186 | //fval = 2.0 * ((*((double*)param) ) - 0.5); |
| 187 | fval = ((*((double*)param) ) ); |
| 188 | |
| 189 | if (fval != inst->dist_weightcxx . . $ cluster_instance#dist_weight.) |
| 190 | { |
| 191 | inst->dist_weightcxx . . $ cluster_instance#dist_weight. = fval; |
| 192 | } |
| 193 | break; |
| 194 | |
| 195 | #if 0 |
| 196 | case 2: |
| 197 | /* val is 0-1.0 */ |
| 198 | //fval = 2.0 * ((*((double*)param) ) - 0.5); |
| 199 | fval = ((*((double*)param) ) ); |
| 200 | |
| 201 | if (fval != inst->color_weight) |
| 202 | { |
| 203 | inst->color_weight = fval; |
| 204 | } |
| 205 | break; |
| 206 | #endif |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, |
| 211 | f0r_param_tcxx . . $ f0r_param_t# param, int param_index) |
| 212 | { |
| 213 | assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance); |
| 214 | cluster_instance_tcxx . . $ cluster_instance_t#* inst = (cluster_instance_tcxx . . $ cluster_instance_t#*)instance; |
| 215 | |
| 216 | switch(param_index) |
| 217 | { |
| 218 | case 0: |
| 219 | *((double*)param) = (double) ( (inst->numcxx . . $ cluster_instance#num.) )/MAXNUMcxx . . $ `src/filter/cluster/cluster.c:29:9`!; |
| 220 | break; |
| 221 | case 1: |
| 222 | *((double*)param) = (double) ( (inst->dist_weightcxx . . $ cluster_instance#dist_weight.)); |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | float find_distcxx . . $ find_dist(b231d33755045e6b).(int r1, int g1, int b1, int x1, int y1, |
| 228 | int r2, int g2, int b2, int x2, int y2, |
| 229 | float max_space_dist, float dist_weight) //, float color_weight) |
| 230 | { |
| 231 | /// make this a define? |
| 232 | float max_color_dist = sqrtf(255*255*3); |
| 233 | |
| 234 | int dr = r1-r2; |
| 235 | int dg = g1-g2; |
| 236 | int db = b1-b2; |
| 237 | |
| 238 | float color_dist = sqrtf(dr*dr + dg*dg + db*db)/max_color_dist; |
| 239 | |
| 240 | int dx = x1-x2; |
| 241 | int dy = y1-y2; |
| 242 | float space_dist = sqrtf(dx*dx + dy*dy)/max_space_dist; |
| 243 | |
| 244 | /// add parameter weighting later |
| 245 | //return sqrtf(color_weight*color_dist*color_dist + dist_weight*space_dist*space_dist); |
| 246 | return sqrtf((1.0-dist_weight)*color_dist*color_dist + dist_weight*space_dist*space_dist); |
| 247 | } |
| 248 | |
| 249 | void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time, |
| 250 | const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe) |
| 251 | { |
| 252 | assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance); |
| 253 | cluster_instance_tcxx . . $ cluster_instance_t#* inst = (cluster_instance_tcxx . . $ cluster_instance_t#*)instance; |
| 254 | |
| 255 | unsigned int x,y,k; |
| 256 | |
| 257 | float max_space_dist = sqrtf(inst->widthcxx . . $ cluster_instance#width.*inst->widthcxx . . $ cluster_instance#width. + inst->heightcxx . . $ cluster_instance#height.*inst->heightcxx . . $ cluster_instance#height.); |
| 258 | |
| 259 | /* |
| 260 | if (inst->has_initted) { |
| 261 | inst->has_initted = true; |
| 262 | } |
| 263 | */ |
| 264 | |
| 265 | for (y=0; y < inst->heightcxx . . $ cluster_instance#height.; ++y) { |
| 266 | for (x=0; x < inst->widthcxx . . $ cluster_instance#width.; ++x) { |
| 267 | |
| 268 | const unsigned char* src2 = (unsigned char*)( &inframe[x+inst->widthcxx . . $ cluster_instance#width.*y]); |
| 269 | unsigned char* dst2 = (unsigned char*)(&outframe[x+inst->widthcxx . . $ cluster_instance#width.*y]); |
| 270 | |
| 271 | float dist = max_space_dist; |
| 272 | int dist_ind = 0; |
| 273 | |
| 274 | for (k = 0; k < inst->numcxx . . $ cluster_instance#num.; k++) { |
| 275 | struct cluster_centercxx . . $ cluster_center# *cc = &inst->clusterscxx . . $ cluster_instance#clusters.[k]; |
| 276 | |
| 277 | float kdist = find_distcxx . . $ find_dist(b231d33755045e6b).(src2[0], src2[1], src2[2], x,y, |
| 278 | cc->rcxx . . $ cluster_center#r., cc->gcxx . . $ cluster_center#g., cc->bcxx . . $ cluster_center#b., cc->xcxx . . $ cluster_center#x., cc->ycxx . . $ cluster_center#y., |
| 279 | max_space_dist, inst->dist_weightcxx . . $ cluster_instance#dist_weight.); //, inst->color_weight); |
| 280 | |
| 281 | if (kdist < dist) { |
| 282 | dist = kdist; |
| 283 | dist_ind = k; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | struct cluster_centercxx . . $ cluster_center#* cc = &inst->clusterscxx . . $ cluster_instance#clusters.[dist_ind]; |
| 288 | cc->aggr_xcxx . . $ cluster_center#aggr_x. += x; |
| 289 | cc->aggr_ycxx . . $ cluster_center#aggr_y. += y; |
| 290 | cc->aggr_rcxx . . $ cluster_center#aggr_r. += src2[0]; |
| 291 | cc->aggr_gcxx . . $ cluster_center#aggr_g. += src2[1]; |
| 292 | cc->aggr_bcxx . . $ cluster_center#aggr_b. += src2[2]; |
| 293 | cc->numpixcxx . . $ cluster_center#numpix. += 1.0; |
| 294 | |
| 295 | dst2[0] = cc->rcxx . . $ cluster_center#r.; |
| 296 | dst2[1] = cc->gcxx . . $ cluster_center#g.; |
| 297 | dst2[2] = cc->bcxx . . $ cluster_center#b.; |
| 298 | dst2[3] = src2[3]; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /// update cluster_centers |
| 303 | for (k = 0; k < inst->numcxx . . $ cluster_instance#num.; k++) { |
| 304 | |
| 305 | struct cluster_centercxx . . $ cluster_center#* cc = &inst->clusterscxx . . $ cluster_instance#clusters.[k]; |
| 306 | |
| 307 | if (cc->numpixcxx . . $ cluster_center#numpix. > 0) { |
| 308 | cc->xcxx . . $ cluster_center#x. = (int) (cc->aggr_xcxx . . $ cluster_center#aggr_x./cc->numpixcxx . . $ cluster_center#numpix.); |
| 309 | cc->ycxx . . $ cluster_center#y. = (int) (cc->aggr_ycxx . . $ cluster_center#aggr_y./cc->numpixcxx . . $ cluster_center#numpix.); |
| 310 | cc->rcxx . . $ cluster_center#r. = (unsigned char) (cc->aggr_rcxx . . $ cluster_center#aggr_r./cc->numpixcxx . . $ cluster_center#numpix.); |
| 311 | cc->gcxx . . $ cluster_center#g. = (unsigned char) (cc->aggr_gcxx . . $ cluster_center#aggr_g./cc->numpixcxx . . $ cluster_center#numpix.); |
| 312 | cc->bcxx . . $ cluster_center#b. = (unsigned char) (cc->aggr_bcxx . . $ cluster_center#aggr_b./cc->numpixcxx . . $ cluster_center#numpix.); |
| 313 | |
| 314 | //printf("%d, %d %d %d\t", k, (unsigned int)cc->r, (unsigned int)cc->g, (unsigned int)cc->b); |
| 315 | //printf("%g, %g %g %g\n", cc->numpix, cc->aggr_r, cc->aggr_g, cc->aggr_b); |
| 316 | } |
| 317 | |
| 318 | cc->numpixcxx . . $ cluster_center#numpix. = 0; |
| 319 | cc->aggr_xcxx . . $ cluster_center#aggr_x. = 0; |
| 320 | cc->aggr_ycxx . . $ cluster_center#aggr_y. = 0; |
| 321 | cc->aggr_rcxx . . $ cluster_center#aggr_r. = 0; |
| 322 | cc->aggr_gcxx . . $ cluster_center#aggr_g. = 0; |
| 323 | cc->aggr_bcxx . . $ cluster_center#aggr_b. = 0; |
| 324 | } |
| 325 | //printf("\n"); |
| 326 | } |
| 327 |