soc-2008-mxcurioni: merged changes to revision 15163
[blender.git] / extern / libredcode / debayer.c
1 #include "debayer.h"
2
3 /* pretty simple but astonishingly very effective "debayer" function 
4  */
5
6 void redcode_ycbcr2rgb_fullscale(
7         int ** planes, int width, int height, float * out)
8 {
9         int x,y;
10         int pix_max = 4096;
11         int mask = pix_max - 1;
12         float Kb = 0.0722;
13         float Kr = 0.2126;
14         float *o;
15
16         for (y = 0; y < height; y++) {
17                 for (x = 0; x < width; x++) {
18                         int i = x + y*width;
19                         int i_p = (y > 0) ? i-width : i;
20                         int i_n = (y < (height-1)) ? i + width : i;
21                         float y1n = planes[0][i_n] & mask;
22                         float y1  = planes[0][i] & mask;
23                         float cb  = (planes[1][i] & mask)   - pix_max/2;
24                         float cr  = (planes[2][i] & mask)   - pix_max/2;
25                         float y2  = (planes[3][i] & mask);
26                         float y2p = (planes[3][i_p] & mask);
27
28                         float b_ = cb * (1.0 - Kb)/(pix_max/2);
29                         float r_ = cr * (1.0 - Kr)/(pix_max/2);
30                         float g_ = (- Kr * r_ - Kb * b_)/(1.0 - Kr - Kb);
31                 
32                         float y_[4] = {y1 / pix_max, 
33                                        (y2 + y2p)/2 / pix_max, 
34                                        (y1 + y1n)/2 / pix_max, 
35                                        y2 / pix_max};
36
37                         int j;
38                         int yc = 0;
39
40                         o = out + (2*height-1-2*y)*2*4*width 
41                                 + x*2*4;
42
43                         for (j = 0; j < 8; j += 4) {
44                                 o[j+0] = r_ + y_[yc];
45                                 o[j+1] = g_ + y_[yc];
46                                 o[j+2] = b_ + y_[yc];
47                                 o[j+3] = 1.0;
48                                 yc++;
49                         }
50                         
51                         o = out + (2*height-1-2*y)*2*4*width 
52                                 + x*2*4 - 2*4*width;
53
54                         for (j = 0; j < 8; j += 4) {
55                                 o[j+0] = r_ + y_[yc];
56                                 o[j+1] = g_ + y_[yc];
57                                 o[j+2] = b_ + y_[yc];
58                                 o[j+3] = 1.0;
59                                 yc++;
60                         }
61                 }
62         }
63 }
64
65 void redcode_ycbcr2rgb_halfscale(
66         int ** planes, int width, int height, float * out)
67 {
68         int x,y;
69         int pix_max = 4096;
70         int mask = pix_max - 1;
71         float Kb = 0.0722;
72         float Kr = 0.2126;
73
74         for (y = 0; y < height; y++) {
75                 float *o = out + width * (height - y - 1);
76                 for (x = 0; x < width; x++) {
77                         int i = y*height + x;
78                         float y1  = (planes[0][i] & mask);
79                         float cb  = (planes[1][i] & mask)  - pix_max/2;
80                         float cr  = (planes[2][i] & mask)  - pix_max/2;
81                         float y2  = (planes[3][i] & mask);
82
83                         float b_ = cb * (1.0 - Kb)/(pix_max/2);
84                         float r_ = cr * (1.0 - Kr)/(pix_max/2);
85                         float g_ = (- Kr * r_ - Kb * b_)/(1.0 - Kr - Kb);
86                         
87                         float y = (y1 + y2)/2 / pix_max;
88
89                         *o++ = r_ + y;
90                         *o++ = g_ + y;
91                         *o++ = b_ + y;
92                         *o++ = 1.0;
93                 }
94         }
95 }
96
97
98 void redcode_ycbcr2rgb_quarterscale(
99         int ** planes, int width, int height, float * out)
100 {
101         int x,y;
102         int pix_max = 4096;
103         int mask = pix_max - 1;
104         float Kb = 0.0722;
105         float Kr = 0.2126;
106
107         for (y = 0; y < height; y += 2) {
108                 float *o = out + (width/2) * (height/2 - y/2 - 1);
109                 for (x = 0; x < width; x += 2) {
110                         int i = y * width + x;
111                         float y1  = planes[0][i] & mask;
112                         float cb  = (planes[1][i] & mask)  - pix_max/2;
113                         float cr  = (planes[2][i] & mask)  - pix_max/2;
114                         float y2  = planes[3][i] & mask;
115
116                         float b_ = cb * (1.0 - Kb)/(pix_max/2);
117                         float r_ = cr * (1.0 - Kr)/(pix_max/2);
118                         float g_ = (- Kr * r_ - Kb * b_)/(1.0 - Kr - Kb);
119                         
120                         float y = (y1 + y2)/2 / pix_max;
121                         
122                         *o++ = r_ + y;
123                         *o++ = g_ + y;
124                         *o++ = b_ + y;
125                         *o++ = 1.0;
126                 }
127         }
128 }
129