Generic Image Library: scaling.hpp Source File

###  | #  |

scaling.hpp
1//
2// Copyright 2019 Olzhas Zhumabek <anonymous.from.applecity@gmail.com>
3//
4// Use, modification and distribution are subject to the Boost Software License,
5// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7//
8#ifndef BOOST_GIL_IMAGE_PROCESSING_SCALING_HPP
9#define BOOST_GIL_IMAGE_PROCESSING_SCALING_HPP
10
11#include <boost/gil/image_view.hpp>
12#include <boost/gil/rgb.hpp>
13#include <boost/gil/pixel.hpp>
14#include <boost/gil/image_processing/numeric.hpp>
15
16namespace boost { namespace gil {
17
36template <typename ImageView>
37void lanczos_at(
38 ImageView input_view,
39 ImageView output_view,
40 typename ImageView::x_coord_t source_x,
41 typename ImageView::y_coord_t source_y,
42 typename ImageView::x_coord_t target_x,
43 typename ImageView::y_coord_t target_y,
44 std::ptrdiff_t a)
45{
46 using x_coord_t = typename ImageView::x_coord_t;
47 using y_coord_t = typename ImageView::y_coord_t;
48 using pixel_t = typename std::remove_reference<decltype(std::declval<ImageView>()(0, 0))>::type;
49
50 // C++11 doesn't allow auto in lambdas
51 using channel_t = typename std::remove_reference
52 <
53 decltype(std::declval<pixel_t>().at(std::integral_constant<int, 0>{}))
54 >::type;
55
56 pixel_t result_pixel;
57 static_transform(result_pixel, result_pixel, [](channel_t) {
58 return static_cast<channel_t>(0);
59 });
60 auto x_zero = static_cast<x_coord_t>(0);
61 auto x_one = static_cast<x_coord_t>(1);
62 auto y_zero = static_cast<y_coord_t>(0);
63 auto y_one = static_cast<y_coord_t>(1);
64
65 for (y_coord_t y_i = (std::max)(source_y - static_cast<y_coord_t>(a) + y_one, y_zero);
66 y_i <= (std::min)(source_y + static_cast<y_coord_t>(a), input_view.height() - y_one);
67 ++y_i)
68 {
69 for (x_coord_t x_i = (std::max)(source_x - static_cast<x_coord_t>(a) + x_one, x_zero);
70 x_i <= (std::min)(source_x + static_cast<x_coord_t>(a), input_view.width() - x_one);
71 ++x_i)
72 {
73 double lanczos_response = lanczos(source_x - x_i, a) * lanczos(source_y - y_i, a);
74 auto op = [lanczos_response](channel_t prev, channel_t next)
75 {
76 return static_cast<channel_t>(prev + next * lanczos_response);
77 };
78 static_transform(result_pixel, input_view(source_x, source_y), result_pixel, op);
79 }
80 }
81
82 output_view(target_x, target_y) = result_pixel;
83}
84
94template <typename ImageView>
95 void scale_lanczos(ImageView input_view, ImageView output_view, std::ptrdiff_t a)
96{
97 double scale_x = (static_cast<double>(output_view.width()))
98 / static_cast<double>(input_view.width());
99 double scale_y = (static_cast<double>(output_view.height()))
100 / static_cast<double>(input_view.height());
101
102 using x_coord_t = typename ImageView::x_coord_t;
103 using y_coord_t = typename ImageView::y_coord_t;
104 for (y_coord_t y = 0; y < output_view.height(); ++y)
105 {
106 for (x_coord_t x = 0; x < output_view.width(); ++x)
107 {
108 lanczos_at(input_view, output_view, x / scale_x, y / scale_y, x, y, a);
109 }
110 }
111}
112
113}} // namespace boost::gil
114
115#endif
boost::gil::scale_lanczos
void scale_lanczos(ImageView input_view, ImageView output_view, std::ptrdiff_t a)
Complete Lanczos algorithm.
Definition: scaling.hpp:95
boost::gil::lanczos
double lanczos(double x, std::ptrdiff_t a)
Lanczos response at point x.
Definition: numeric.hpp:46
boost
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36