generates a scaled bitmap with dimensions the new bmp will have the same aspect ratio and will be centered in the box
| 361 | // generates a scaled bitmap with dimensions the new bmp will have the |
| 362 | // same aspect ratio and will be centered in the box |
| 363 | bool Bmp8::ScaleFrom(Bmp8 *bmp, bool isotropic) { |
| 364 | int x_num; |
| 365 | int x_denom; |
| 366 | int y_num; |
| 367 | int y_denom; |
| 368 | int xoff; |
| 369 | int yoff; |
| 370 | int xsrc; |
| 371 | int ysrc; |
| 372 | int xdest; |
| 373 | int ydest; |
| 374 | int xst_src = 0; |
| 375 | int yst_src = 0; |
| 376 | int xend_src = bmp->wid_ - 1; |
| 377 | int yend_src = bmp->hgt_ - 1; |
| 378 | int wid_src; |
| 379 | int hgt_src; |
| 380 | |
| 381 | // src dimensions |
| 382 | wid_src = xend_src - xst_src + 1, |
| 383 | hgt_src = yend_src - yst_src + 1; |
| 384 | |
| 385 | // scale to maintain aspect ratio if required |
| 386 | if (isotropic) { |
| 387 | if ((wid_ * hgt_src) > (hgt_ * wid_src)) { |
| 388 | x_num = y_num = hgt_; |
| 389 | x_denom = y_denom = hgt_src; |
| 390 | } else { |
| 391 | x_num = y_num = wid_; |
| 392 | x_denom = y_denom = wid_src; |
| 393 | } |
| 394 | } else { |
| 395 | x_num = wid_; |
| 396 | y_num = hgt_; |
| 397 | x_denom = wid_src; |
| 398 | y_denom = hgt_src; |
| 399 | } |
| 400 | |
| 401 | // compute offsets needed to center new bmp |
| 402 | xoff = (wid_ - ((x_num * wid_src) / x_denom)) / 2; |
| 403 | yoff = (hgt_ - ((y_num * hgt_src) / y_denom)) / 2; |
| 404 | |
| 405 | // scale up |
| 406 | if (y_num > y_denom) { |
| 407 | for (ydest = yoff; ydest < (hgt_ - yoff); ydest++) { |
| 408 | // compute un-scaled y |
| 409 | ysrc = static_cast<int>(0.5 + (1.0 * (ydest - yoff) * |
| 410 | y_denom / y_num)); |
| 411 | if (ysrc < 0 || ysrc >= hgt_src) { |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | for (xdest = xoff; xdest < (wid_ - xoff); xdest++) { |
| 416 | // compute un-scaled y |
| 417 | xsrc = static_cast<int>(0.5 + (1.0 * (xdest - xoff) * |
| 418 | x_denom / x_num)); |
| 419 | if (xsrc < 0 || xsrc >= wid_src) { |
| 420 | continue; |