| 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>cxx . . $ `<file>/include/frei0r.hpp`/ |
| 29 | |
| 30 | #define REDcxx . . $ `src/filter/cartoon/cartoon.cpp:30:9`!(n) ((n>>16) & 0x000000FF) |
| 31 | #define GREENcxx . . $ `src/filter/cartoon/cartoon.cpp:31:9`!(n) ((n>>8) & 0x000000FF) |
| 32 | #define BLUEcxx . . $ `src/filter/cartoon/cartoon.cpp:32:9`!(n) (n & 0x000000FF) |
| 33 | #define RGBcxx . . $ `src/filter/cartoon/cartoon.cpp:33:9`!(r,g,b) ((r<<16) + (g <<8) + (b)) |
| 34 | |
| 35 | #define BOOSTcxx . . $ `src/filter/cartoon/cartoon.cpp:35:9`!(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 structcxx . . $ $anonymous_type_17cc58fe37a22c59_0# { |
| 43 | int16_t wcxx . . $ $anonymous_type_17cc58fe37a22c59_0#w.; |
| 44 | int16_t hcxx . . $ $anonymous_type_17cc58fe37a22c59_0#h.; |
| 45 | uint8_t bppcxx . . $ $anonymous_type_17cc58fe37a22c59_0#bpp.; |
| 46 | uint32_t sizecxx . . $ $anonymous_type_17cc58fe37a22c59_0#size.; |
| 47 | } ScreenGeometrycxx . . $ ScreenGeometry#; |
| 48 | |
| 49 | #define PIXELATcxx . . $ `src/filter/cartoon/cartoon.cpp:49:9`!(x1,y1,s,inst) ((s)+(x1)+ inst->yprecal[y1])// (y1)*(geo->w))) |
| 50 | #define GMERRORcxx . . $ `src/filter/cartoon/cartoon.cpp:50:9`!(cc1,cc2) ((((REDcxx . . $ `src/filter/cartoon/cartoon.cpp:30:9`!(cc1)-REDcxx . . $ `src/filter/cartoon/cartoon.cpp:30:9`!(cc2))*(REDcxx . . $ `src/filter/cartoon/cartoon.cpp:30:9`!(cc1)-REDcxx . . $ `src/filter/cartoon/cartoon.cpp:30:9`!(cc2))) + \ |
| 51 | ((GREENcxx . . $ `src/filter/cartoon/cartoon.cpp:31:9`!(cc1)-GREENcxx . . $ `src/filter/cartoon/cartoon.cpp:31:9`!(cc2)) *(GREENcxx . . $ `src/filter/cartoon/cartoon.cpp:31:9`!(cc1)-GREENcxx . . $ `src/filter/cartoon/cartoon.cpp:31:9`!(cc2))) + \ |
| 52 | ((BLUEcxx . . $ `src/filter/cartoon/cartoon.cpp:32:9`!(cc1)-BLUEcxx . . $ `src/filter/cartoon/cartoon.cpp:32:9`!(cc2))*(BLUEcxx . . $ `src/filter/cartoon/cartoon.cpp:32:9`!(cc1)-BLUEcxx . . $ `src/filter/cartoon/cartoon.cpp:32:9`!(cc2))))) |
| 53 | |
| 54 | class Cartooncxx . . $ Cartoon#: public frei0rcxx . . $ frei0r/::filtercxx . . $ frei0r/filter# { |
| 55 | public: |
| 56 | |
| 57 | double triplevelcxx . . $ Cartoon#triplevel.; |
| 58 | double diffspacecxx . . $ Cartoon#diffspace.; |
| 59 | |
| 60 | Cartooncxx . . $ Cartoon#Cartoon(b4d55b1286ff73e8).(unsigned int width, unsigned int height) { |
| 61 | int c; |
| 62 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(triplevelcxx . . $ Cartoon#triplevel., "triplevel"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "level of trip: mapped to [0,1] asymptotical"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 63 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(diffspacecxx . . $ Cartoon#diffspace., "diffspace"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "difference space: a value from 0 to 256 (mapped to [0,1])"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 64 | |
| 65 | geocxx . . $ Cartoon#geo. = new ScreenGeometrycxx . . $ ScreenGeometry#(); |
| 66 | geocxx . . $ Cartoon#geo.->wcxx . . $ $anonymous_type_17cc58fe37a22c59_0#w. = width; |
| 67 | geocxx . . $ Cartoon#geo.->hcxx . . $ $anonymous_type_17cc58fe37a22c59_0#h. = height; |
| 68 | geocxx . . $ Cartoon#geo.->sizecxx . . $ $anonymous_type_17cc58fe37a22c59_0#size. = width*height*sizeof(uint32_t); |
| 69 | |
| 70 | if ( geocxx . . $ Cartoon#geo.->sizecxx . . $ $anonymous_type_17cc58fe37a22c59_0#size. > 0 ) { |
| 71 | prePixBuffercxx . . $ Cartoon#prePixBuffer. = (int32_t*)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(geocxx . . $ Cartoon#geo.->sizecxx . . $ $anonymous_type_17cc58fe37a22c59_0#size.); |
| 72 | conBuffercxx . . $ Cartoon#conBuffer. = (int32_t*)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(geocxx . . $ Cartoon#geo.->sizecxx . . $ $anonymous_type_17cc58fe37a22c59_0#size.); |
| 73 | |
| 74 | yprecalcxx . . $ Cartoon#yprecal. = (int*)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(geocxx . . $ Cartoon#geo.->hcxx . . $ $anonymous_type_17cc58fe37a22c59_0#h.*2*sizeof(int)); |
| 75 | } |
| 76 | for(c=0;c<geocxx . . $ Cartoon#geo.->hcxx . . $ $anonymous_type_17cc58fe37a22c59_0#h.*2;c++) |
| 77 | yprecalcxx . . $ Cartoon#yprecal.[c] = geocxx . . $ Cartoon#geo.->wcxx . . $ $anonymous_type_17cc58fe37a22c59_0#w.*c; |
| 78 | for(c=0;c<256;c++) |
| 79 | powprecalcxx . . $ Cartoon#powprecal.[c] = c*c; |
| 80 | blackcxx . . $ Cartoon#black. = 0xFF000000; |
| 81 | |
| 82 | triplevelcxx . . $ Cartoon#triplevel. = 1 - 1 / (1000 + 1); |
| 83 | diffspacecxx . . $ Cartoon#diffspace. = 1 / 256.; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | ~cxx . . $ Cartoon#`~Cartoon`(49f6e7a06ebc5aa8).Cartooncxx . . $ Cartoon#() { |
| 88 | if ( geocxx . . $ Cartoon#geo.->sizecxx . . $ $anonymous_type_17cc58fe37a22c59_0#size. > 0 ) { |
| 89 | freecxx . . $ free(5d22bdacc48458e8).(prePixBuffercxx . . $ Cartoon#prePixBuffer.); |
| 90 | freecxx . . $ free(5d22bdacc48458e8).(conBuffercxx . . $ Cartoon#conBuffer.); |
| 91 | freecxx . . $ free(5d22bdacc48458e8).(yprecalcxx . . $ Cartoon#yprecal.); |
| 92 | } |
| 93 | delete geocxx . . $ Cartoon#geo.; |
| 94 | } |
| 95 | |
| 96 | virtual void updatecxx . . $ Cartoon#update(da7b4a17ceb4e697).(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_diffspacecxx . . $ Cartoon#m_diffspace. = diffspacecxx . . $ Cartoon#diffspace. * 256; |
| 103 | |
| 104 | for (x=m_diffspacecxx . . $ Cartoon#m_diffspace.;x<geocxx . . $ Cartoon#geo.->wcxx . . $ $anonymous_type_17cc58fe37a22c59_0#w.-(1+m_diffspacecxx . . $ Cartoon#m_diffspace.);x++) { |
| 105 | |
| 106 | for (y=m_diffspacecxx . . $ Cartoon#m_diffspace.;y<geocxx . . $ Cartoon#geo.->hcxx . . $ $anonymous_type_17cc58fe37a22c59_0#h.-(1+m_diffspacecxx . . $ Cartoon#m_diffspace.);y++) { |
| 107 | |
| 108 | t = GetMaxContrastcxx . . $ Cartoon#GetMaxContrast(c586110e256d58f).((int32_t*)in,x,y); |
| 109 | if (t > 1 / (1 - triplevelcxx . . $ Cartoon#triplevel.) - 1) { |
| 110 | |
| 111 | // Make a border pixel |
| 112 | *(out+x+yprecalcxx . . $ Cartoon#yprecal.[y]) = blackcxx . . $ Cartoon#black.; |
| 113 | |
| 114 | } else { |
| 115 | |
| 116 | // Copy original color |
| 117 | *(out+x+yprecalcxx . . $ Cartoon#yprecal.[y]) = *(in+x+yprecalcxx . . $ Cartoon#yprecal.[y]); |
| 118 | FlattenColorcxx . . $ Cartoon#FlattenColor(11b0e290e57a7e53).((int32_t*)out+x+yprecalcxx . . $ Cartoon#yprecal.[y]); |
| 119 | |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | ScreenGeometrycxx . . $ ScreenGeometry# *geocxx . . $ Cartoon#geo.; |
| 127 | /* buffer where to copy the screen |
| 128 | a pointer to it is being given back by process() */ |
| 129 | |
| 130 | int32_t *prePixBuffercxx . . $ Cartoon#prePixBuffer.; |
| 131 | int32_t *conBuffercxx . . $ Cartoon#conBuffer.; |
| 132 | int *yprecalcxx . . $ Cartoon#yprecal.; |
| 133 | uint16_t powprecalcxx . . $ Cartoon#powprecal.[256]; |
| 134 | int32_t blackcxx . . $ Cartoon#black.; |
| 135 | int m_diffspacecxx . . $ Cartoon#m_diffspace.; |
| 136 | |
| 137 | void FlattenColorcxx . . $ Cartoon#FlattenColor(11b0e290e57a7e53).(int32_t *c); |
| 138 | long GetMaxContrastcxx . . $ Cartoon#GetMaxContrast(c586110e256d58f).(int32_t *src,int x,int y); |
| 139 | }; |
| 140 | |
| 141 | |
| 142 | void Cartooncxx . . $ Cartoon#::FlattenColorcxx . . $ Cartoon#FlattenColor(11b0e290e57a7e53).(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 Cartooncxx . . $ Cartoon#::GetMaxContrastcxx . . $ Cartoon#GetMaxContrast(c586110e256d58f).(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 = *PIXELATcxx . . $ `src/filter/cartoon/cartoon.cpp:49:9`!(x-m_diffspace,y,src,this); |
| 159 | c2 = *PIXELATcxx . . $ `src/filter/cartoon/cartoon.cpp:49:9`!(x+m_diffspace,y,src,this); |
| 160 | error = GMERRORcxx . . $ `src/filter/cartoon/cartoon.cpp:50:9`!(c1,c2); |
| 161 | if (error>max) max = error; |
| 162 | |
| 163 | c1 = *PIXELATcxx . . $ `src/filter/cartoon/cartoon.cpp:49:9`!(x,y-m_diffspace,src,this); |
| 164 | c2 = *PIXELATcxx . . $ `src/filter/cartoon/cartoon.cpp:49:9`!(x,y+m_diffspace,src,this); |
| 165 | error = GMERRORcxx . . $ `src/filter/cartoon/cartoon.cpp:50:9`!(c1,c2); |
| 166 | if (error>max) max = error; |
| 167 | |
| 168 | c1 = *PIXELATcxx . . $ `src/filter/cartoon/cartoon.cpp:49:9`!(x-m_diffspace,y-m_diffspace,src,this); |
| 169 | c2 = *PIXELATcxx . . $ `src/filter/cartoon/cartoon.cpp:49:9`!(x+m_diffspace,y+m_diffspace,src,this); |
| 170 | error = GMERRORcxx . . $ `src/filter/cartoon/cartoon.cpp:50:9`!(c1,c2); |
| 171 | if (error>max) max = error; |
| 172 | |
| 173 | c1 = *PIXELATcxx . . $ `src/filter/cartoon/cartoon.cpp:49:9`!(x+m_diffspace,y-m_diffspace,src,this); |
| 174 | c2 = *PIXELATcxx . . $ `src/filter/cartoon/cartoon.cpp:49:9`!(x-m_diffspace,y+m_diffspace,src,this); |
| 175 | error = GMERRORcxx . . $ `src/filter/cartoon/cartoon.cpp:50:9`!(c1,c2); |
| 176 | if (error>max) max = error; |
| 177 | |
| 178 | return(max); |
| 179 | } |
| 180 | |
| 181 | frei0rcxx . . $ frei0r/::constructcxx . . $ frei0r/construct#<Cartooncxx . . $ Cartoon#> plugincxx . . $ plugin.("Cartoon"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 182 | "Cartoonify video, do a form of edge detect"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 183 | "Dries Pruimboom, Jaromil"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 184 | 2,2); |