]> git.sesse.net Git - casparcg/blob - modules/image/util/image_algorithms.h
set svn:eol-style native on .h and .cpp files
[casparcg] / modules / image / util / image_algorithms.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #pragma once
23
24 #include <common/tweener.h>
25
26 #include <cmath>
27 #include <boost/foreach.hpp>
28
29 namespace caspar { namespace image {
30
31 /**
32  * Helper for calculating the color of a pixel given any number of of other
33  * pixels (each with their own weight).
34  */
35 class rgba_weighting
36 {
37         int r, g, b, a;
38         int total_weight;
39 public:
40         rgba_weighting()
41                 : r(0), g(0), b(0), a(0), total_weight(0)
42         {
43         }
44
45         template<class RGBAPixel>
46         inline void add_pixel(const RGBAPixel& pixel, uint8_t weight)
47         {
48                 r += pixel.r() * weight;
49                 g += pixel.g() * weight;
50                 b += pixel.b() * weight;
51                 a += pixel.a() * weight;
52
53                 total_weight += weight;
54         }
55
56         template<class RGBAPixel>
57         inline void store_result(RGBAPixel& pixel)
58         {
59                 pixel.r() = static_cast<uint8_t>(r / total_weight);
60                 pixel.g() = static_cast<uint8_t>(g / total_weight);
61                 pixel.b() = static_cast<uint8_t>(b / total_weight);
62                 pixel.a() = static_cast<uint8_t>(a / total_weight);
63         }
64 };
65
66 template<class T>
67 std::vector<T> get_tweened_values(const core::tweener& tweener, size_t num_values, T from, T to)
68 {
69         std::vector<T> result;
70         result.reserve(num_values);
71
72         double start = static_cast<double>(from);
73         double delta = static_cast<double>(to - from);
74         double duration = static_cast<double>(num_values);
75
76         for (double t = 0; t < duration; ++t)
77         {
78                 result.push_back(static_cast<T>(tweener(t, start, delta, duration - 1.0)));
79         }
80
81         return std::move(result);
82 }
83
84 /**
85  * Blur a source image and store the blurred result in a destination image.
86  * <p>
87  * The blur is done by weighting each relative pixel from a destination pixel
88  * position using a vector of relative x-y pairs. The further away a related
89  * pixel is the less weight it gets. A tweener is used to calculate the actual
90  * weights of each related pixel.
91  *
92  * @param src                      The source view. Has to model the ImageView
93  *                                 concept and have a pixel type modelling the
94  *                                 RGBAPixel concept.
95  * @param dst                      The destination view. Has to model the
96  *                                 ImageView concept and have a pixel type
97  *                                 modelling the RGBAPixel concept.
98  * @param motion_trail_coordinates The relative x-y positions to weight in for
99  *                                 each pixel.
100  * @param tweener                  The tweener to use for calculating the
101  *                                 weights of each relative position in the
102  *                                 motion trail.
103  */
104 template<class SrcView, class DstView>
105 void blur(
106         const SrcView& src,
107         DstView& dst,
108         const std::vector<std::pair<int, int>> motion_trail_coordinates, 
109         const core::tweener& tweener)
110 {
111         auto blur_px = motion_trail_coordinates.size();
112         auto tweened_weights_y = get_tweened_values<uint8_t>(tweener, blur_px + 2, 255, 0);
113         tweened_weights_y.pop_back();
114         tweened_weights_y.erase(tweened_weights_y.begin());
115
116         auto src_end = src.end();
117         auto dst_iter = dst.begin();
118
119         for (auto src_iter = src.begin(); src_iter != src_end; ++src_iter, ++dst_iter)
120         {
121                 rgba_weighting w;
122
123                 for (int i = 0; i < blur_px; ++i)
124                 {
125                         auto& coordinate = motion_trail_coordinates[i];
126                         auto other_pixel = src.relative(src_iter, coordinate.first, coordinate.second);
127
128                         if (other_pixel == nullptr)
129                                 break;
130
131                         w.add_pixel(*other_pixel, tweened_weights_y[i]);
132                 }
133
134                 w.add_pixel(*src_iter, 255);
135                 w.store_result(*dst_iter);
136         }
137 }
138
139 /**
140  * Calculate relative x-y coordinates of a straight line with a given angle and
141  * a given number of points.
142  *
143  * @param num_pixels    The number of pixels/points to create.
144  * @param angle_radians The angle of the line in radians.
145  *
146  * @return the x-y pairs.
147  */
148 std::vector<std::pair<int, int>> get_line_points(int num_pixels, double angle_radians)
149 {
150         std::vector<std::pair<int, int>> line_points;
151         line_points.reserve(num_pixels);
152
153         double delta_x = std::cos(angle_radians);
154         double delta_y = -std::sin(angle_radians); // In memory is revered
155         double max_delta = std::max(std::abs(delta_x), std::abs(delta_y));
156         double amplification = 1.0 / max_delta;
157         delta_x *= amplification;
158         delta_y *= amplification;
159
160         for (int i = 1; i <= num_pixels; ++i)
161                 line_points.push_back(std::make_pair(
162                         static_cast<int>(std::floor(delta_x * static_cast<double>(i) + 0.5)), 
163                         static_cast<int>(std::floor(delta_y * static_cast<double>(i) + 0.5))));
164
165         return std::move(line_points);
166 }
167
168 /**
169  * Directionally blur a source image modelling the ImageView concept and store
170  * the blurred image to a destination image also modelling the ImageView
171  * concept.
172  * <p>
173  * The pixel type of the views must model the RGBAPixel concept.
174  *
175  * @param src           The source image view. Has to model the ImageView
176  *                      concept and have a pixel type that models RGBAPixel.
177  * @param dst           The destiation image view. Has to model the ImageView
178  *                      concept and have a pixel type that models RGBAPixel.
179  * @param angle_radians The angle in radians to directionally blur the image.
180  * @param blur_px       The number of pixels of the blur.
181  * @param tweener       The tweener to use to create a pixel weighting curve
182  *                      with.
183  */
184 template<class SrcView, class DstView>
185 void blur(
186         const SrcView& src,
187         DstView& dst,
188         double angle_radians,
189         int blur_px, 
190         const core::tweener& tweener)
191 {
192         auto motion_trail = get_line_points(blur_px, angle_radians);
193
194         blur(src, dst, motion_trail, tweener);
195 }
196
197 /**
198  * Premultiply with alpha for each pixel in an ImageView. The modifications is
199  * done in place. The pixel type of the ImageView must model the RGBAPixel
200  * concept.
201  *
202  * @param view_to_modify The image view to premultiply in place. Has to model
203  *                       the ImageView concept and have a pixel type that
204  *                       models RGBAPixel.
205  */
206 template<class SrcDstView>
207 void premultiply(SrcDstView& view_to_modify)
208 {
209         std::for_each(view_to_modify.begin(), view_to_modify.end(), [&](SrcDstView::pixel_type& pixel)
210         {
211                 int alpha = static_cast<int>(pixel.a());
212
213                 if (alpha != 255) // Performance optimization
214                 {
215                         // We don't event try to premultiply 0 since it will be unaffected.
216                         if (pixel.r())
217                                 pixel.r() = static_cast<uint8_t>(static_cast<int>(pixel.r()) * alpha / 255);
218
219                         if (pixel.g())
220                                 pixel.g() = static_cast<uint8_t>(static_cast<int>(pixel.g()) * alpha / 255);
221
222                         if (pixel.b())
223                                 pixel.b() = static_cast<uint8_t>(static_cast<int>(pixel.b()) * alpha / 255);
224                 }
225         });
226 }
227
228 }}