Debian Codegraph @

src/filter/premultiply/premultiply.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2018 Dan Dennedy <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "frei0r.hpp"
20 #include "frei0r/math.h"
21
22 class Premultiply : public frei0r::filter
23 {
24
25 public:
26
27 Premultiply(unsigned int width, unsigned int height)
28 : m_unpremultiply(0)
29 {
30 register_param(m_unpremultiply, "unpremultiply", "Whether to unpremultiply instead");
31 }
32
33 ~Premultiply()
34 {
35 }
36
37 virtual void update(double,
38 uint32_t* out,
39 const uint32_t* in)
40 {
41 uint8_t *src = (uint8_t*) in;
42 uint8_t *dst = (uint8_t*) out;
43 unsigned int n = width * height + 1;
44 if (!m_unpremultiply) {
45 // premultiply
46 while (--n) {
47 uint8_t a = src[3];
48 dst[0] = (src[0] * a) >> 8;
49 dst[1] = (src[1] * a) >> 8;
50 dst[2] = (src[2] * a) >> 8;
51 dst[3] = a;
52 src += 4;
53 dst += 4;
54 }
55 } else {
56 // unpremultiply
57 while (--n) {
58 uint8_t a = src[3];
59 if (a > 0 && a < 255) {
60 dst[0] = MIN((src[0] << 8) / a, 255);
61 dst[1] = MIN((src[1] << 8) / a, 255);
62 dst[2] = MIN((src[2] << 8) / a, 255);
63 } else {
64 dst[0] = src[0];
65 dst[1] = src[1];
66 dst[2] = src[2];
67 }
68 dst[3] = a;
69 src += 4;
70 dst += 4;
71 }
72 }
73 }
74
75 private:
76 bool m_unpremultiply;
77
78 };
79
80 frei0r::construct<Premultiply> plugin("Premultiply or Unpremultiply",
81 "Multiply (or divide) each color component by the pixel's alpha value",
82 "Dan Dennedy",
83 0, 2,
84 F0R_COLOR_MODEL_RGBA8888);