comparison Core/Images/ImageProcessing.cpp @ 4082:2e1564f57542

ImageProcessing::FitSizeKeepAspectRatio()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 23 Jun 2020 07:39:23 +0200
parents f18eaade6153
children
comparison
equal deleted inserted replaced
4080:f18eaade6153 4082:2e1564f57542
2488 { 2488 {
2489 std::unique_ptr<ImageAccessor> target(new Image(source.GetFormat(), width, height, false)); 2489 std::unique_ptr<ImageAccessor> target(new Image(source.GetFormat(), width, height, false));
2490 FitSize(*target, source); 2490 FitSize(*target, source);
2491 return target.release(); 2491 return target.release();
2492 } 2492 }
2493
2494
2495 ImageAccessor* ImageProcessing::FitSizeKeepAspectRatio(const ImageAccessor& source,
2496 unsigned int width,
2497 unsigned int height)
2498 {
2499 std::unique_ptr<ImageAccessor> target(new Image(source.GetFormat(), width, height, false));
2500 Set(*target, 0);
2501
2502 if (width != 0 &&
2503 height != 0 &&
2504 source.GetWidth() != 0 &&
2505 source.GetHeight() != 0)
2506 {
2507 float ratio = std::min(static_cast<float>(width) / static_cast<float>(source.GetWidth()),
2508 static_cast<float>(height) / static_cast<float>(source.GetHeight()));
2509
2510 unsigned int resizedWidth = static_cast<unsigned int>(
2511 boost::math::iround(ratio * static_cast<float>(source.GetWidth())));
2512
2513 unsigned int resizedHeight = static_cast<unsigned int>(
2514 boost::math::iround(ratio * static_cast<float>(source.GetHeight())));
2515
2516 std::unique_ptr<ImageAccessor> resized(FitSize(source, resizedWidth, resizedHeight));
2517
2518 ImageAccessor region;
2519 target->GetRegion(region, (width - resizedWidth) / 2,
2520 (height - resizedHeight) / 2, resizedWidth, resizedHeight);
2521 Copy(region, *resized);
2522 }
2523
2524 return target.release();
2525 }
2493 } 2526 }