| 1 |
|
| 2 |
|
| 3 |
static fixed_t BoxFixed(const fixed_t x,const fixed_t ARGUNUSED(support)) |
| 4 |
{ |
| 5 |
if (x < -FIXED_HALF) |
| 6 |
return 0; |
| 7 |
if (x < FIXED_HALF) |
| 8 |
return FIXED_1; |
| 9 |
return 0; |
| 10 |
} |
| 11 |
|
| 12 |
static fixed_t TriangleFixed(const fixed_t x,const fixed_t ARGUNUSED(support)) |
| 13 |
{ |
| 14 |
if (x < -FIXED_1) |
| 15 |
return 0; |
| 16 |
if (x < 0) |
| 17 |
return(FIXED_1+x); |
| 18 |
if (x < FIXED_1) |
| 19 |
return(FIXED_1-x); |
| 20 |
return 0; |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
static void |
| 27 |
image_downsize_gm_horizontal_filter_fixed_point(image *im, ImageInfo *source, ImageInfo *destination, |
| 28 |
const fixed_t x_factor, const FilterInfoFixed *filter_info, ContributionInfoFixed *contribution, int rotate) |
| 29 |
{ |
| 30 |
fixed_t scale, support; |
| 31 |
int x; |
| 32 |
int dstX = 0; |
| 33 |
int dstW = destination->columns; |
| 34 |
|
| 35 |
if (im->width_padding) { |
| 36 |
dstX = im->width_padding; |
| 37 |
dstW = im->width_inner; |
| 38 |
} |
| 39 |
|
| 40 |
scale = MAX(fixed_div(FIXED_1, x_factor), FIXED_1); |
| 41 |
support = fixed_mul(scale, filter_info->support); |
| 42 |
if (support <= FIXED_HALF) { |
| 43 |
|
| 44 |
support = FIXED_HALF + FIXED_EPSILON; |
| 45 |
scale = FIXED_1; |
| 46 |
} |
| 47 |
scale = fixed_div(FIXED_1, scale); |
| 48 |
|
| 49 |
for (x = dstX; (x < dstX + dstW); x++) { |
| 50 |
fixed_t center, density; |
| 51 |
int n, start, stop, y; |
| 52 |
|
| 53 |
center = fixed_div(int_to_fixed(x - dstX) + FIXED_HALF, x_factor); |
| 54 |
start = fixed_to_int(MAX(center - support + FIXED_HALF, 0)); |
| 55 |
stop = fixed_to_int(MIN(center + support + FIXED_HALF, int_to_fixed(source->columns))); |
| 56 |
density = 0; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
for (n = 0; n < (stop - start); n++) { |
| 61 |
contribution[n].pixel = start + n; |
| 62 |
contribution[n].weight = filter_info->function(fixed_mul(scale, (int_to_fixed(start) + int_to_fixed(n) - center + FIXED_HALF)), filter_info->support); |
| 63 |
density += contribution[n].weight; |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
if ((density != 0) && (density != FIXED_1)) { |
| 68 |
|
| 69 |
int i; |
| 70 |
|
| 71 |
density = fixed_div(FIXED_1, density); |
| 72 |
for (i = 0; i < n; i++) { |
| 73 |
contribution[i].weight = fixed_mul(contribution[i].weight, density); |
| 74 |
|
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
for (y = 0; y < destination->rows; y++) { |
| 79 |
fixed_t weight; |
| 80 |
fixed_t red = 0, green = 0, blue = 0, alpha = 0; |
| 81 |
pix p; |
| 82 |
int j; |
| 83 |
register int i; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if (im->has_alpha) { |
| 88 |
fixed_t normalize = 0; |
| 89 |
|
| 90 |
for (i = 0; i < n; i++) { |
| 91 |
j = (int)((y * source->columns) + contribution[i].pixel); |
| 92 |
weight = contribution[i].weight; |
| 93 |
p = source->buf[j]; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
red += fixed_mul(weight, int_to_fixed(COL_RED(p))); |
| 107 |
green += fixed_mul(weight, int_to_fixed(COL_GREEN(p))); |
| 108 |
blue += fixed_mul(weight, int_to_fixed(COL_BLUE(p))); |
| 109 |
alpha += fixed_mul(weight, int_to_fixed(COL_ALPHA(p))); |
| 110 |
normalize += weight; |
| 111 |
} |
| 112 |
|
| 113 |
normalize = fixed_div(FIXED_1, (ABS(normalize) <= FIXED_EPSILON ? FIXED_1 : normalize)); |
| 114 |
red = fixed_mul(red, normalize); |
| 115 |
green = fixed_mul(green, normalize); |
| 116 |
blue = fixed_mul(blue, normalize); |
| 117 |
} |
| 118 |
else { |
| 119 |
for (i = 0; i < n; i++) { |
| 120 |
j = (int)((y * source->columns) + contribution[i].pixel); |
| 121 |
weight = contribution[i].weight; |
| 122 |
p = source->buf[j]; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
red += fixed_mul(weight, int_to_fixed(COL_RED(p))); |
| 132 |
green += fixed_mul(weight, int_to_fixed(COL_GREEN(p))); |
| 133 |
blue += fixed_mul(weight, int_to_fixed(COL_BLUE(p))); |
| 134 |
} |
| 135 |
|
| 136 |
alpha = FIXED_255; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if (rotate && im->orientation != ORIENTATION_NORMAL) { |
| 149 |
int ox, oy; |
| 150 |
|
| 151 |
image_get_rotated_coords(im, x, y, &ox, &oy); |
| 152 |
|
| 153 |
if (im->orientation >= 5) { |
| 154 |
|
| 155 |
destination->buf[(oy * destination->rows) + ox] = COL_FULL( |
| 156 |
ROUND_FIXED_TO_INT(red), |
| 157 |
ROUND_FIXED_TO_INT(green), |
| 158 |
ROUND_FIXED_TO_INT(blue), |
| 159 |
ROUND_FIXED_TO_INT(alpha) |
| 160 |
); |
| 161 |
} |
| 162 |
else { |
| 163 |
destination->buf[(oy * destination->columns) + ox] = COL_FULL( |
| 164 |
ROUND_FIXED_TO_INT(red), |
| 165 |
ROUND_FIXED_TO_INT(green), |
| 166 |
ROUND_FIXED_TO_INT(blue), |
| 167 |
ROUND_FIXED_TO_INT(alpha) |
| 168 |
); |
| 169 |
} |
| 170 |
} |
| 171 |
else { |
| 172 |
destination->buf[(y * destination->columns) + x] = COL_FULL( |
| 173 |
ROUND_FIXED_TO_INT(red), |
| 174 |
ROUND_FIXED_TO_INT(green), |
| 175 |
ROUND_FIXED_TO_INT(blue), |
| 176 |
ROUND_FIXED_TO_INT(alpha) |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
static void |
| 184 |
image_downsize_gm_vertical_filter_fixed_point(image *im, ImageInfo *source, ImageInfo *destination, |
| 185 |
const fixed_t y_factor, const FilterInfoFixed *filter_info, ContributionInfoFixed *contribution, int rotate) |
| 186 |
{ |
| 187 |
fixed_t scale, support; |
| 188 |
int y; |
| 189 |
int dstY = 0; |
| 190 |
int dstH = destination->rows; |
| 191 |
|
| 192 |
if (im->height_padding) { |
| 193 |
dstY = im->height_padding; |
| 194 |
dstH = im->height_inner; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
scale = MAX(fixed_div(FIXED_1, y_factor), FIXED_1); |
| 201 |
support = fixed_mul(scale, filter_info->support); |
| 202 |
if (support <= FIXED_HALF) { |
| 203 |
|
| 204 |
support = FIXED_HALF + FIXED_EPSILON; |
| 205 |
scale = FIXED_1; |
| 206 |
} |
| 207 |
scale = fixed_div(FIXED_1, scale); |
| 208 |
|
| 209 |
for (y = dstY; (y < dstY + dstH); y++) { |
| 210 |
fixed_t center, density; |
| 211 |
int n, start, stop, x; |
| 212 |
|
| 213 |
center = fixed_div(int_to_fixed(y - dstY) + FIXED_HALF, y_factor); |
| 214 |
start = fixed_to_int(MAX(center - support + FIXED_HALF, 0)); |
| 215 |
stop = fixed_to_int(MIN(center + support + FIXED_HALF, int_to_fixed(source->rows))); |
| 216 |
density = 0; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
for (n = 0; n < (stop - start); n++) { |
| 221 |
contribution[n].pixel = start + n; |
| 222 |
contribution[n].weight = filter_info->function(fixed_mul(scale, (int_to_fixed(start) + int_to_fixed(n) - center + FIXED_HALF)), filter_info->support); |
| 223 |
density += contribution[n].weight; |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
if ((density != 0) && (density != FIXED_1)) { |
| 228 |
|
| 229 |
int i; |
| 230 |
|
| 231 |
density = fixed_div(FIXED_1, density); |
| 232 |
for (i = 0; i < n; i++) { |
| 233 |
contribution[i].weight = fixed_mul(contribution[i].weight, density); |
| 234 |
|
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
for (x = 0; x < destination->columns; x++) { |
| 239 |
fixed_t weight; |
| 240 |
fixed_t red = 0, green = 0, blue = 0, alpha = 0; |
| 241 |
pix p; |
| 242 |
int j; |
| 243 |
register int i; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if (im->has_alpha) { |
| 248 |
fixed_t normalize = 0; |
| 249 |
|
| 250 |
for (i = 0; i < n; i++) { |
| 251 |
j = (int)((contribution[i].pixel * source->columns) + x); |
| 252 |
weight = contribution[i].weight; |
| 253 |
p = source->buf[j]; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
red += fixed_mul(weight, int_to_fixed(COL_RED(p))); |
| 267 |
green += fixed_mul(weight, int_to_fixed(COL_GREEN(p))); |
| 268 |
blue += fixed_mul(weight, int_to_fixed(COL_BLUE(p))); |
| 269 |
alpha += fixed_mul(weight, int_to_fixed(COL_ALPHA(p))); |
| 270 |
normalize += weight; |
| 271 |
} |
| 272 |
|
| 273 |
normalize = fixed_div(FIXED_1, (ABS(normalize) <= FIXED_EPSILON ? FIXED_1 : normalize)); |
| 274 |
red = fixed_mul(red, normalize); |
| 275 |
green = fixed_mul(green, normalize); |
| 276 |
blue = fixed_mul(blue, normalize); |
| 277 |
} |
| 278 |
else { |
| 279 |
for (i = 0; i < n; i++) { |
| 280 |
j = (int)((contribution[i].pixel * source->columns) + x); |
| 281 |
weight = contribution[i].weight; |
| 282 |
p = source->buf[j]; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
red += fixed_mul(weight, int_to_fixed(COL_RED(p))); |
| 292 |
green += fixed_mul(weight, int_to_fixed(COL_GREEN(p))); |
| 293 |
blue += fixed_mul(weight, int_to_fixed(COL_BLUE(p))); |
| 294 |
} |
| 295 |
|
| 296 |
alpha = FIXED_255; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if (rotate && im->orientation != ORIENTATION_NORMAL) { |
| 309 |
int ox, oy; |
| 310 |
|
| 311 |
image_get_rotated_coords(im, x, y, &ox, &oy); |
| 312 |
|
| 313 |
if (im->orientation >= 5) { |
| 314 |
|
| 315 |
destination->buf[(oy * destination->rows) + ox] = COL_FULL( |
| 316 |
ROUND_FIXED_TO_INT(red), |
| 317 |
ROUND_FIXED_TO_INT(green), |
| 318 |
ROUND_FIXED_TO_INT(blue), |
| 319 |
ROUND_FIXED_TO_INT(alpha) |
| 320 |
); |
| 321 |
} |
| 322 |
else { |
| 323 |
destination->buf[(oy * destination->columns) + ox] = COL_FULL( |
| 324 |
ROUND_FIXED_TO_INT(red), |
| 325 |
ROUND_FIXED_TO_INT(green), |
| 326 |
ROUND_FIXED_TO_INT(blue), |
| 327 |
ROUND_FIXED_TO_INT(alpha) |
| 328 |
); |
| 329 |
} |
| 330 |
} |
| 331 |
else { |
| 332 |
destination->buf[(y * destination->columns) + x] = COL_FULL( |
| 333 |
ROUND_FIXED_TO_INT(red), |
| 334 |
ROUND_FIXED_TO_INT(green), |
| 335 |
ROUND_FIXED_TO_INT(blue), |
| 336 |
ROUND_FIXED_TO_INT(alpha) |
| 337 |
); |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
void |
| 344 |
image_downsize_gm_fixed_point(image *im) |
| 345 |
{ |
| 346 |
|
| 347 |
float x_factor, y_factor; |
| 348 |
float support, x_support, y_support; |
| 349 |
int columns, rows; |
| 350 |
int order; |
| 351 |
int filter; |
| 352 |
ContributionInfoFixed *contribution; |
| 353 |
ImageInfo source, destination; |
| 354 |
|
| 355 |
static const FilterInfoFixed |
| 356 |
filters[SincFilter+1] = |
| 357 |
{ |
| 358 |
{ BoxFixed, 0 }, |
| 359 |
{ BoxFixed, 0 }, |
| 360 |
{ BoxFixed, FIXED_HALF }, |
| 361 |
{ TriangleFixed, FIXED_1 }, |
| 362 |
|
| 363 |
}; |
| 364 |
|
| 365 |
columns = im->target_width; |
| 366 |
rows = im->target_height; |
| 367 |
filter = TriangleFilter; |
| 368 |
|
| 369 |
DEBUG_TRACE("Resizing with filter %d\n", filter); |
| 370 |
|
| 371 |
|
| 372 |
order = (((float)columns * (im->height + rows)) > |
| 373 |
((float)rows * (im->width + columns))); |
| 374 |
|
| 375 |
if (im->width_padding) |
| 376 |
x_factor = (float)im->width_inner / im->width; |
| 377 |
else |
| 378 |
x_factor = (float)im->target_width / im->width; |
| 379 |
|
| 380 |
if (im->height_padding) |
| 381 |
y_factor = (float)im->height_inner / im->height; |
| 382 |
else |
| 383 |
y_factor = (float)im->target_height / im->height; |
| 384 |
|
| 385 |
x_support = BLUR * MAX(1.0 / x_factor, 1.0) * fixed_to_int(filters[filter].support); |
| 386 |
y_support = BLUR * MAX(1.0 / y_factor, 1.0) * fixed_to_int(filters[filter].support); |
| 387 |
support = MAX(x_support, y_support); |
| 388 |
if (support < fixed_to_int(filters[filter].support)) |
| 389 |
support = fixed_to_int(filters[filter].support); |
| 390 |
|
| 391 |
DEBUG_TRACE("ContributionInfoFixed allocated for %ld items\n", (size_t)(2.0 * MAX(support, 0.5) + 3)); |
| 392 |
New(0, contribution, (size_t)(2.0 * MAX(support, 0.5) + 3), ContributionInfoFixed); |
| 393 |
|
| 394 |
DEBUG_TRACE("order %d, x_factor %f, y_factor %f, support %f\n", order, x_factor, y_factor, support); |
| 395 |
|
| 396 |
source.rows = im->height; |
| 397 |
source.columns = im->width; |
| 398 |
source.buf = im->pixbuf; |
| 399 |
|
| 400 |
if (order) { |
| 401 |
DEBUG_TRACE("Allocating temporary buffer size %ld\n", im->target_width * im->height * sizeof(pix)); |
| 402 |
New(0, im->tmpbuf, im->target_width * im->height, pix); |
| 403 |
|
| 404 |
|
| 405 |
image_bgcolor_fill(im->tmpbuf, im->target_width * im->height, im->bgcolor); |
| 406 |
|
| 407 |
|
| 408 |
destination.rows = im->height; |
| 409 |
destination.columns = im->target_width; |
| 410 |
destination.buf = im->tmpbuf; |
| 411 |
image_downsize_gm_horizontal_filter_fixed_point(im, &source, &destination, float_to_fixed(x_factor), &filters[filter], contribution, 0); |
| 412 |
|
| 413 |
|
| 414 |
source.rows = destination.rows; |
| 415 |
source.columns = destination.columns; |
| 416 |
source.buf = destination.buf; |
| 417 |
|
| 418 |
destination.rows = im->target_height; |
| 419 |
destination.buf = im->outbuf; |
| 420 |
image_downsize_gm_vertical_filter_fixed_point(im, &source, &destination, float_to_fixed(y_factor), &filters[filter], contribution, 1); |
| 421 |
} |
| 422 |
else { |
| 423 |
DEBUG_TRACE("Allocating temporary buffer size %ld\n", im->width * im->target_height * sizeof(pix)); |
| 424 |
New(0, im->tmpbuf, im->width * im->target_height, pix); |
| 425 |
|
| 426 |
|
| 427 |
image_bgcolor_fill(im->tmpbuf, im->width * im->target_height, im->bgcolor); |
| 428 |
|
| 429 |
|
| 430 |
destination.rows = im->target_height; |
| 431 |
destination.columns = im->width; |
| 432 |
destination.buf = im->tmpbuf; |
| 433 |
image_downsize_gm_vertical_filter_fixed_point(im, &source, &destination, float_to_fixed(y_factor), &filters[filter], contribution, 0); |
| 434 |
|
| 435 |
|
| 436 |
source.rows = destination.rows; |
| 437 |
source.columns = destination.columns; |
| 438 |
source.buf = destination.buf; |
| 439 |
|
| 440 |
destination.columns = im->target_width; |
| 441 |
destination.buf = im->outbuf; |
| 442 |
image_downsize_gm_horizontal_filter_fixed_point(im, &source, &destination, float_to_fixed(x_factor), &filters[filter], contribution, 1); |
| 443 |
} |
| 444 |
|
| 445 |
Safefree(im->tmpbuf); |
| 446 |
Safefree(contribution); |
| 447 |
} |