| 283 | */ |
| 284 | |
| 285 | ILfloat Maximize(Box *cube, ILubyte dir, ILint first, ILint last, ILint *cut, |
| 286 | ILint whole_r, ILint whole_g, ILint whole_b, ILint whole_w) |
| 287 | { |
| 288 | ILint half_r, half_g, half_b, half_w; |
| 289 | ILint base_r, base_g, base_b, base_w; |
| 290 | ILint i; |
| 291 | ILfloat temp, max; |
| 292 | |
| 293 | base_r = Bottom(cube, dir, mr); |
| 294 | base_g = Bottom(cube, dir, mg); |
| 295 | base_b = Bottom(cube, dir, mb); |
| 296 | base_w = Bottom(cube, dir, wt); |
| 297 | max = 0.0; |
| 298 | *cut = -1; |
| 299 | |
| 300 | for (i = first; i < last; ++i) { |
| 301 | half_r = base_r + Top(cube, dir, i, mr); |
| 302 | half_g = base_g + Top(cube, dir, i, mg); |
| 303 | half_b = base_b + Top(cube, dir, i, mb); |
| 304 | half_w = base_w + Top(cube, dir, i, wt); |
| 305 | // Now half_x is sum over lower half of Box, if split at i |
| 306 | if (half_w == 0) { // subBox could be empty of pixels! |
| 307 | continue; // never split into an empty Box |
| 308 | } |
| 309 | else { |
| 310 | temp = ((ILfloat)half_r*half_r + (ILfloat)half_g * half_g + |
| 311 | (ILfloat)half_b*half_b) / half_w; |
| 312 | } |
| 313 | |
| 314 | half_r = whole_r - half_r; |
| 315 | half_g = whole_g - half_g; |
| 316 | half_b = whole_b - half_b; |
| 317 | half_w = whole_w - half_w; |
| 318 | if (half_w == 0) { // subBox could be empty of pixels! |
| 319 | continue; // never split into an empty Box |
| 320 | } |
| 321 | else { |
| 322 | temp += ((ILfloat)half_r*half_r + (ILfloat)half_g * half_g + |
| 323 | (ILfloat)half_b*half_b) / half_w; |
| 324 | } |
| 325 | |
| 326 | if (temp > max) { |
| 327 | max = temp; |
| 328 | *cut = i; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return max; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | ILint Cut(Box *set1, Box *set2) |