~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to source/blender/nodes/intern/CMP_nodes/CMP_filter.c

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * $Id: CMP_filter.c 12931 2007-12-17 18:20:48Z theeth $
 
3
 *
 
4
 * ***** BEGIN GPL LICENSE BLOCK *****
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version. 
 
10
 *
 
11
 * This program 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 this program; if not, write to the Free Software Foundation,
 
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 *
 
20
 * The Original Code is Copyright (C) 2006 Blender Foundation.
 
21
 * All rights reserved.
 
22
 *
 
23
 * The Original Code is: all of this file.
 
24
 *
 
25
 * Contributor(s): none yet.
 
26
 *
 
27
 * ***** END GPL LICENSE BLOCK *****
 
28
 */
 
29
 
 
30
#include "../CMP_util.h"
 
31
 
 
32
/* **************** FILTER  ******************** */
 
33
static bNodeSocketType cmp_node_filter_in[]= {
 
34
        {       SOCK_VALUE, 1, "Fac",                   1.0f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
 
35
        {       SOCK_RGBA, 1, "Image",                  0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
 
36
        {       -1, 0, ""       }
 
37
};
 
38
static bNodeSocketType cmp_node_filter_out[]= {
 
39
        {       SOCK_RGBA, 0, "Image",                  0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
 
40
        {       -1, 0, ""       }
 
41
};
 
42
 
 
43
static void do_filter_edge(CompBuf *out, CompBuf *in, float *filter, float fac)
 
44
{
 
45
        float *row1, *row2, *row3;
 
46
        float *fp, f1, f2, mfac= 1.0f-fac;
 
47
        int rowlen, x, y, c, pix= in->type;
 
48
        
 
49
        rowlen= in->x;
 
50
        
 
51
        for(y=0; y<in->y; y++) {
 
52
                /* setup rows */
 
53
                if(y==0) row1= in->rect;
 
54
                else row1= in->rect + pix*(y-1)*rowlen;
 
55
                
 
56
                row2= in->rect + y*pix*rowlen;
 
57
                
 
58
                if(y==in->y-1) row3= row2;
 
59
                else row3= row2 + pix*rowlen;
 
60
                
 
61
                fp= out->rect + pix*y*rowlen;
 
62
                
 
63
                if(pix==CB_RGBA) {
 
64
                        QUATCOPY(fp, row2);
 
65
                        fp+= pix;
 
66
                        
 
67
                        for(x=2; x<rowlen; x++) {
 
68
                                for(c=0; c<3; c++) {
 
69
                                        f1= filter[0]*row1[0] + filter[1]*row1[4] + filter[2]*row1[8] + filter[3]*row2[0] + filter[4]*row2[4] + filter[5]*row2[8] + filter[6]*row3[0] + filter[7]*row3[4] + filter[8]*row3[8];
 
70
                                        f2= filter[0]*row1[0] + filter[3]*row1[4] + filter[6]*row1[8] + filter[1]*row2[0] + filter[4]*row2[4] + filter[7]*row2[8] + filter[2]*row3[0] + filter[5]*row3[4] + filter[8]*row3[8];
 
71
                                        fp[0]= mfac*row2[4] + fac*sqrt(f1*f1 + f2*f2);
 
72
                                        fp++; row1++; row2++; row3++;
 
73
                                }
 
74
                                fp[0]= row2[4];
 
75
                                /* no alpha... will clear it completely */
 
76
                                fp++; row1++; row2++; row3++;
 
77
                        }
 
78
                        QUATCOPY(fp, row2+4);
 
79
                }
 
80
                else if(pix==CB_VAL) {
 
81
                        for(x=2; x<rowlen; x++) {
 
82
                                f1= filter[0]*row1[0] + filter[1]*row1[1] + filter[2]*row1[2] + filter[3]*row2[0] + filter[4]*row2[1] + filter[5]*row2[2] + filter[6]*row3[0] + filter[7]*row3[1] + filter[8]*row3[2];
 
83
                                f2= filter[0]*row1[0] + filter[3]*row1[1] + filter[6]*row1[2] + filter[1]*row2[0] + filter[4]*row2[1] + filter[7]*row2[2] + filter[2]*row3[0] + filter[5]*row3[1] + filter[8]*row3[2];
 
84
                                fp[0]= mfac*row2[1] + fac*sqrt(f1*f1 + f2*f2);
 
85
                                fp++; row1++; row2++; row3++;
 
86
                        }
 
87
                }
 
88
        }
 
89
}
 
90
 
 
91
static void do_filter3(CompBuf *out, CompBuf *in, float *filter, float fac)
 
92
{
 
93
        float *row1, *row2, *row3;
 
94
        float *fp, mfac= 1.0f-fac;
 
95
        int rowlen, x, y, c;
 
96
        int pixlen= in->type;
 
97
        
 
98
        rowlen= in->x;
 
99
        
 
100
        for(y=0; y<in->y; y++) {
 
101
                /* setup rows */
 
102
                if(y==0) row1= in->rect;
 
103
                else row1= in->rect + pixlen*(y-1)*rowlen;
 
104
                
 
105
                row2= in->rect + y*pixlen*rowlen;
 
106
                
 
107
                if(y==in->y-1) row3= row2;
 
108
                else row3= row2 + pixlen*rowlen;
 
109
                
 
110
                fp= out->rect + pixlen*(y)*rowlen;
 
111
                
 
112
                if(pixlen==1) {
 
113
                        fp[0]= row2[0];
 
114
                        fp+= 1;
 
115
                        
 
116
                        for(x=2; x<rowlen; x++) {
 
117
                                fp[0]= mfac*row2[1] + fac*(filter[0]*row1[0] + filter[1]*row1[1] + filter[2]*row1[2] + filter[3]*row2[0] + filter[4]*row2[1] + filter[5]*row2[2] + filter[6]*row3[0] + filter[7]*row3[1] + filter[8]*row3[2]);
 
118
                                fp++; row1++; row2++; row3++;
 
119
                        }
 
120
                        fp[0]= row2[1];
 
121
                }
 
122
                else if(pixlen==2) {
 
123
                        fp[0]= row2[0];
 
124
                        fp[1]= row2[1];
 
125
                        fp+= 2;
 
126
                        
 
127
                        for(x=2; x<rowlen; x++) {
 
128
                                for(c=0; c<2; c++) {
 
129
                                        fp[0]= mfac*row2[2] + fac*(filter[0]*row1[0] + filter[1]*row1[2] + filter[2]*row1[4] + filter[3]*row2[0] + filter[4]*row2[2] + filter[5]*row2[4] + filter[6]*row3[0] + filter[7]*row3[2] + filter[8]*row3[4]);
 
130
                                        fp++; row1++; row2++; row3++;
 
131
                                }
 
132
                        }
 
133
                        fp[0]= row2[2];
 
134
                        fp[1]= row2[3];
 
135
                }
 
136
                else if(pixlen==3) {
 
137
                        VECCOPY(fp, row2);
 
138
                        fp+= 3;
 
139
                        
 
140
                        for(x=2; x<rowlen; x++) {
 
141
                                for(c=0; c<3; c++) {
 
142
                                        fp[0]= mfac*row2[3] + fac*(filter[0]*row1[0] + filter[1]*row1[3] + filter[2]*row1[6] + filter[3]*row2[0] + filter[4]*row2[3] + filter[5]*row2[6] + filter[6]*row3[0] + filter[7]*row3[3] + filter[8]*row3[6]);
 
143
                                        fp++; row1++; row2++; row3++;
 
144
                                }
 
145
                        }
 
146
                        VECCOPY(fp, row2+3);
 
147
                }
 
148
                else {
 
149
                        QUATCOPY(fp, row2);
 
150
                        fp+= 4;
 
151
                        
 
152
                        for(x=2; x<rowlen; x++) {
 
153
                                for(c=0; c<4; c++) {
 
154
                                        fp[0]= mfac*row2[4] + fac*(filter[0]*row1[0] + filter[1]*row1[4] + filter[2]*row1[8] + filter[3]*row2[0] + filter[4]*row2[4] + filter[5]*row2[8] + filter[6]*row3[0] + filter[7]*row3[4] + filter[8]*row3[8]);
 
155
                                        fp++; row1++; row2++; row3++;
 
156
                                }
 
157
                        }
 
158
                        QUATCOPY(fp, row2+4);
 
159
                }
 
160
        }
 
161
}
 
162
 
 
163
 
 
164
static void node_composit_exec_filter(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
 
165
{
 
166
        static float soft[9]= {1/16.0f, 2/16.0f, 1/16.0f, 2/16.0f, 4/16.0f, 2/16.0f, 1/16.0f, 2/16.0f, 1/16.0f};
 
167
        float sharp[9]= {-1,-1,-1,-1,9,-1,-1,-1,-1};
 
168
        float laplace[9]= {-1/8.0f, -1/8.0f, -1/8.0f, -1/8.0f, 1.0f, -1/8.0f, -1/8.0f, -1/8.0f, -1/8.0f};
 
169
        float sobel[9]= {1,2,1,0,0,0,-1,-2,-1};
 
170
        float prewitt[9]= {1,1,1,0,0,0,-1,-1,-1};
 
171
        float kirsch[9]= {5,5,5,-3,-3,-3,-2,-2,-2};
 
172
        float shadow[9]= {1,2,1,0,1,0,-1,-2,-1};
 
173
        
 
174
        if(out[0]->hasoutput==0) return;
 
175
        
 
176
        /* stack order in: Image */
 
177
        /* stack order out: Image */
 
178
        
 
179
        if(in[1]->data) {
 
180
                /* make output size of first available input image */
 
181
                CompBuf *cbuf= in[1]->data;
 
182
                CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, cbuf->type, 1); /* allocs */
 
183
                
 
184
                /* warning note: xof and yof are applied in pixelprocessor, but should be copied otherwise? */
 
185
                stackbuf->xof= cbuf->xof;
 
186
                stackbuf->yof= cbuf->yof;
 
187
                
 
188
                switch(node->custom1) {
 
189
                        case CMP_FILT_SOFT:
 
190
                                do_filter3(stackbuf, cbuf, soft, in[0]->vec[0]);
 
191
                                break;
 
192
                        case CMP_FILT_SHARP:
 
193
                                do_filter3(stackbuf, cbuf, sharp, in[0]->vec[0]);
 
194
                                break;
 
195
                        case CMP_FILT_LAPLACE:
 
196
                                do_filter3(stackbuf, cbuf, laplace, in[0]->vec[0]);
 
197
                                break;
 
198
                        case CMP_FILT_SOBEL:
 
199
                                do_filter_edge(stackbuf, cbuf, sobel, in[0]->vec[0]);
 
200
                                break;
 
201
                        case CMP_FILT_PREWITT:
 
202
                                do_filter_edge(stackbuf, cbuf, prewitt, in[0]->vec[0]);
 
203
                                break;
 
204
                        case CMP_FILT_KIRSCH:
 
205
                                do_filter_edge(stackbuf, cbuf, kirsch, in[0]->vec[0]);
 
206
                                break;
 
207
                        case CMP_FILT_SHADOW:
 
208
                                do_filter3(stackbuf, cbuf, shadow, in[0]->vec[0]);
 
209
                                break;
 
210
                }
 
211
                        
 
212
                out[0]->data= stackbuf;
 
213
        }
 
214
}
 
215
 
 
216
 
 
217
bNodeType cmp_node_filter= {
 
218
        /* *next,*prev */       NULL, NULL,
 
219
        /* type code   */       CMP_NODE_FILTER,
 
220
        /* name        */       "Filter",
 
221
        /* width+range */       80, 40, 120,
 
222
        /* class+opts  */       NODE_CLASS_OP_FILTER, NODE_OPTIONS,
 
223
        /* input sock  */       cmp_node_filter_in,
 
224
        /* output sock */       cmp_node_filter_out,
 
225
        /* storage     */       "", 
 
226
        /* execfunc    */       node_composit_exec_filter,
 
227
        /* butfunc     */       NULL,
 
228
        /* initfunc    */       NULL,
 
229
        /* freestoragefunc    */        NULL,
 
230
        /* copystoragefunc    */        NULL,
 
231
        /* id          */       NULL
 
232
        
 
233
};
 
234
 
 
235