1 #include "debayer.h"
3 /* pretty simple but astonishingly very effective "debayer" function
4  */
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;
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);
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);
32                         float y_[4] = {y1 / pix_max,
33                                        (y2 + y2p)/2 / pix_max,
34                                        (y1 + y1n)/2 / pix_max,
35                                        y2 / pix_max};
37                         int j;
38                         int yc = 0;
40                         o = out + (2*height-1-2*y)*2*4*width
41                                 + x*2*4;
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                         }
51                         o = out + (2*height-1-2*y)*2*4*width
52                                 + x*2*4 - 2*4*width;
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 }
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;
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);
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);
87                         float y = (y1 + y2)/2 / pix_max;
89                         *o++ = r_ + y;
90                         *o++ = g_ + y;
91                         *o++ = b_ + y;
92                         *o++ = 1.0;
93                 }
94         }
95 }
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;
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;
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);
120                         float y = (y1 + y2)/2 / pix_max;
122                         *o++ = r_ + y;
123                         *o++ = g_ + y;
124                         *o++ = b_ + y;
125                         *o++ = 1.0;
126                 }
127         }
128 }