1 /*
2  * Bluescreen0r
3  * 2009 Hedde Bosman
4  *
5  * This source code  is free software; you can  redistribute it and/or
6  * modify it under the terms of the GNU Public License as published by
7  * the Free Software  Foundation; either version 3 of  the License, or
8  * (at your option) any later version.
9  *
10  * This source code is distributed in the hope that it will be useful,
11  * but  WITHOUT ANY  WARRANTY; without  even the  implied  warranty of
12  * MERCHANTABILITY or FITNESS FOR  A PARTICULAR PURPOSE.  Please refer
13  * to the GNU Public License for more details.
14  *
15  * You should  have received  a copy of  the GNU Public  License along
16  * with this source code; if  not, write to: Free Software Foundation,
17  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20 #include "frei0r.hpp"
21 
22 #include <algorithm>
23 #include <vector>
24 #include <utility>
25 #include <cassert>
26 
27 #include <iostream>
28 
29 #include <math.h>
30 
31 
32 class bluescreen0r : public frei0r::filter
33 {
34 private:
35 	double dist;
36 	f0r_param_color color;
37 	uint32_t r256,g256,b256;
38 
39 	// returns the distance to 'color', but does not normalize
distance(uint32_t pixel)40 	inline uint32_t distance(uint32_t pixel) {
41 		uint32_t d = 0;
42 		int t;
43 		t = ((pixel&0x00FF0000) >> 16) - b256;
44 		d += t*t;
45 		t = ((pixel&0x0000FF00) >> 8)  - g256;
46 		d += t*t;
47 		t = ((pixel&0x000000FF) >> 0)  - r256;
48 		d += t*t;
49 
50 		return (uint32_t) d; // no sqrtf
51 	}
52 public:
bluescreen0r(unsigned int width,unsigned int height)53 	bluescreen0r(unsigned int width, unsigned int height)
54 	{
55 		dist = 0.288;
56 
57 		color.r = 0;
58 		color.g = 0.94;
59 		color.b = 0;
60 
61 		register_param(color,  "Color",    "The color to make transparent (B G R)");
62 		register_param(dist, "Distance", "Distance to Color (127 is good)");
63 	}
64 
update(double time,uint32_t * out,const uint32_t * in)65 	virtual void update(double time,
66 	                    uint32_t* out,
67 		            const uint32_t* in) {
68 		const uint32_t* pixel	=in;
69 		uint32_t* outpixel= out;
70 
71 		uint32_t distInt = (uint32_t) (dist*dist*195075);
72 		uint32_t distInt2 = distInt/2;
73 
74 		r256=255*color.r;
75 		g256=255*color.g;
76 		b256=255*color.b;
77 
78 		while(pixel != in+size) {
79 			*outpixel= (*pixel & 0x00FFFFFF); // copy all except alpha
80 
81 			uint32_t d = distance(*pixel); // get distance
82 			unsigned char a = 255; // default alpha
83 			if (d < distInt) {
84 				a = 0;
85 				if (d > distInt2) {
86 					a = 256*(d-distInt2)/distInt2;
87 				}
88 			}
89 			*outpixel |= (a<<24);
90 
91 			++outpixel;
92 			++pixel;
93 		}
94 	}
95 };
96 
97 
98 frei0r::construct<bluescreen0r> plugin("bluescreen0r", "Color to alpha (blit SRCALPHA)", "Hedde Bosman",0,3,F0R_COLOR_MODEL_RGBA8888);
99 
100