Debian Codegraph @

src/filter/cartoon/cartoon.cpp

raw symbols SCIP
1 /* Cartoon filter
2 * main algorithm: (c) Copyright 2003 Dries Pruimboom <[email protected]>
3 * further optimizations and frei0r port by Denis Rojo <[email protected]>
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
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (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.
13 * Please refer to the GNU Public License for more details.
14 *
15 * You should have received a copy of the GNU Public License along with
16 * this source code; if not, write to:
17 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * "$Id: cartoon.c 193 2004-06-01 11:00:25Z jaromil $"
20 *
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <math.h>
27
28 #include <frei0r.hpp>
29
30 #define RED(n) ((n>>16) & 0x000000FF)
31 #define GREEN(n) ((n>>8) & 0x000000FF)
32 #define BLUE(n) (n & 0x000000FF)
33 #define RGB(r,g,b) ((r<<16) + (g <<8) + (b))
34
35 #define BOOST(n) { \
36 if((*p = *p<<4)<0)>>n; \
37 *(p+1) = (*(p+1)<<4)>>n; \
38 *(p+2) = (*(p+2)<<4)>>n; }
39
40 /* setup some data to identify the plugin */
41
42 typedef struct {
43 int16_t w;
44 int16_t h;
45 uint8_t bpp;
46 uint32_t size;
47 } ScreenGeometry;
48
49 #define PIXELAT(x1,y1,s,inst) ((s)+(x1)+ inst->yprecal[y1])// (y1)*(geo->w)))
50 #define GMERROR(cc1,cc2) ((((RED(cc1)-RED(cc2))*(RED(cc1)-RED(cc2))) + \
51 ((GREEN(cc1)-GREEN(cc2)) *(GREEN(cc1)-GREEN(cc2))) + \
52 ((BLUE(cc1)-BLUE(cc2))*(BLUE(cc1)-BLUE(cc2)))))
53
54 class Cartoon: public frei0r::filter {
55 public:
56
57 double triplevel;
58 double diffspace;
59
60 Cartoon(unsigned int width, unsigned int height) {
61 int c;
62 register_param(triplevel, "triplevel", "level of trip: mapped to [0,1] asymptotical");
63 register_param(diffspace, "diffspace", "difference space: a value from 0 to 256 (mapped to [0,1])");
64
65 geo = new ScreenGeometry();
66 geo->w = width;
67 geo->h = height;
68 geo->size = width*height*sizeof(uint32_t);
69
70 if ( geo->size > 0 ) {
71 prePixBuffer = (int32_t*)malloc(geo->size);
72 conBuffer = (int32_t*)malloc(geo->size);
73
74 yprecal = (int*)malloc(geo->h*2*sizeof(int));
75 }
76 for(c=0;c<geo->h*2;c++)
77 yprecal[c] = geo->w*c;
78 for(c=0;c<256;c++)
79 powprecal[c] = c*c;
80 black = 0xFF000000;
81
82 triplevel = 1 - 1 / (1000 + 1);
83 diffspace = 1 / 256.;
84
85 }
86
87 ~Cartoon() {
88 if ( geo->size > 0 ) {
89 free(prePixBuffer);
90 free(conBuffer);
91 free(yprecal);
92 }
93 delete geo;
94 }
95
96 virtual void update(double time,
97 uint32_t* out,
98 const uint32_t* in) {
99 // Cartoonify picture, do a form of edge detect
100 int x, y, t;
101
102 m_diffspace = diffspace * 256;
103
104 for (x=m_diffspace;x<geo->w-(1+m_diffspace);x++) {
105
106 for (y=m_diffspace;y<geo->h-(1+m_diffspace);y++) {
107
108 t = GetMaxContrast((int32_t*)in,x,y);
109 if (t > 1 / (1 - triplevel) - 1) {
110
111 // Make a border pixel
112 *(out+x+yprecal[y]) = black;
113
114 } else {
115
116 // Copy original color
117 *(out+x+yprecal[y]) = *(in+x+yprecal[y]);
118 FlattenColor((int32_t*)out+x+yprecal[y]);
119
120 }
121 }
122 }
123 }
124
125 private:
126 ScreenGeometry *geo;
127 /* buffer where to copy the screen
128 a pointer to it is being given back by process() */
129
130 int32_t *prePixBuffer;
131 int32_t *conBuffer;
132 int *yprecal;
133 uint16_t powprecal[256];
134 int32_t black;
135 int m_diffspace;
136
137 void FlattenColor(int32_t *c);
138 long GetMaxContrast(int32_t *src,int x,int y);
139 };
140
141
142 void Cartoon::FlattenColor(int32_t *c) {
143 // (*c) = RGB(40*(RED(*c)/40),40*(GREEN(*c)/40),40*(BLUE(*c)/40)); */
144 uint8_t *p;
145 p = (uint8_t*)c;
146 (*p) = ((*p)>>5)<<5; p++;
147 (*p) = ((*p)>>5)<<5; p++;
148 (*p) = ((*p)>>5)<<5;
149 }
150
151
152
153 long Cartoon::GetMaxContrast(int32_t *src,int x,int y) {
154 int32_t c1,c2;
155 long error,max=0;
156
157 /* Assumes PrePixelModify has been run */
158 c1 = *PIXELAT(x-m_diffspace,y,src,this);
159 c2 = *PIXELAT(x+m_diffspace,y,src,this);
160 error = GMERROR(c1,c2);
161 if (error>max) max = error;
162
163 c1 = *PIXELAT(x,y-m_diffspace,src,this);
164 c2 = *PIXELAT(x,y+m_diffspace,src,this);
165 error = GMERROR(c1,c2);
166 if (error>max) max = error;
167
168 c1 = *PIXELAT(x-m_diffspace,y-m_diffspace,src,this);
169 c2 = *PIXELAT(x+m_diffspace,y+m_diffspace,src,this);
170 error = GMERROR(c1,c2);
171 if (error>max) max = error;
172
173 c1 = *PIXELAT(x+m_diffspace,y-m_diffspace,src,this);
174 c2 = *PIXELAT(x-m_diffspace,y+m_diffspace,src,this);
175 error = GMERROR(c1,c2);
176 if (error>max) max = error;
177
178 return(max);
179 }
180
181 frei0r::construct<Cartoon> plugin("Cartoon",
182 "Cartoonify video, do a form of edge detect",
183 "Dries Pruimboom, Jaromil",
184 2,2);