comparison Core/Images/ImageProcessing.cpp @ 3549:fab5777f4dd4

ImageProcessing::FitSize()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Oct 2019 18:09:27 +0200
parents dabe17e23e23
children 0f5f9a5eed25
comparison
equal deleted inserted replaced
3547:dabe17e23e23 3549:fab5777f4dd4
2219 kernel[3] = 4; 2219 kernel[3] = 4;
2220 kernel[4] = 1; 2220 kernel[4] = 1;
2221 2221
2222 SeparableConvolution(image, kernel, 2, kernel, 2); 2222 SeparableConvolution(image, kernel, 2, kernel, 2);
2223 } 2223 }
2224
2225
2226 void ImageProcessing::FitSize(ImageAccessor& target,
2227 const ImageAccessor& source)
2228 {
2229 if (target.GetWidth() == 0 ||
2230 target.GetHeight() == 0)
2231 {
2232 return;
2233 }
2234
2235 if (source.GetWidth() == target.GetWidth() &&
2236 source.GetHeight() == target.GetHeight())
2237 {
2238 Copy(target, source);
2239 return;
2240 }
2241
2242 Set(target, 0);
2243
2244 // Preserve the aspect ratio
2245 float cw = static_cast<float>(source.GetWidth());
2246 float ch = static_cast<float>(source.GetHeight());
2247 float r = std::min(
2248 static_cast<float>(target.GetWidth()) / cw,
2249 static_cast<float>(target.GetHeight()) / ch);
2250
2251 unsigned int sw = std::min(static_cast<unsigned int>(boost::math::iround(cw * r)), target.GetWidth());
2252 unsigned int sh = std::min(static_cast<unsigned int>(boost::math::iround(ch * r)), target.GetHeight());
2253 Image resized(target.GetFormat(), sw, sh, false);
2254
2255 //ImageProcessing::SmoothGaussian5x5(source);
2256 ImageProcessing::Resize(resized, source);
2257
2258 assert(target.GetWidth() >= resized.GetWidth() &&
2259 target.GetHeight() >= resized.GetHeight());
2260 unsigned int offsetX = (target.GetWidth() - resized.GetWidth()) / 2;
2261 unsigned int offsetY = (target.GetHeight() - resized.GetHeight()) / 2;
2262
2263 ImageAccessor region;
2264 target.GetRegion(region, offsetX, offsetY, resized.GetWidth(), resized.GetHeight());
2265 ImageProcessing::Copy(region, resized);
2266 }
2267
2268
2269 ImageAccessor* ImageProcessing::FitSize(const ImageAccessor& source,
2270 unsigned int width,
2271 unsigned int height)
2272 {
2273 std::auto_ptr<ImageAccessor> target(new Image(source.GetFormat(), width, height, false));
2274 FitSize(*target, source);
2275 return target.release();
2276 }
2224 } 2277 }