initial commit
[goodguy/history.git] / cinelerra-5.0 / guicast / xfer.C
1
2 // Compression coefficients straight out of jpeglib
3 #define R_TO_Y    0.29900
4 #define G_TO_Y    0.58700
5 #define B_TO_Y    0.11400
6
7 #define R_TO_U    -0.16874
8 #define G_TO_U    -0.33126
9 #define B_TO_U    0.50000
10
11 #define R_TO_V    0.50000
12 #define G_TO_V    -0.41869
13 #define B_TO_V    -0.08131
14
15 // Decompression coefficients straight out of jpeglib
16 #define V_TO_R    1.40200
17 #define V_TO_G    -0.71414
18
19 #define U_TO_G    -0.34414
20 #define U_TO_B    1.77200
21
22 int BC_Xfer::rtoy_tab[0x100], BC_Xfer::gtoy_tab[0x100], BC_Xfer::btoy_tab[0x100];
23 int BC_Xfer::rtou_tab[0x100], BC_Xfer::gtou_tab[0x100], BC_Xfer::btou_tab[0x100];
24 int BC_Xfer::rtov_tab[0x100], BC_Xfer::gtov_tab[0x100], BC_Xfer::btov_tab[0x100];
25 int BC_Xfer::vtor_tab[0x100], BC_Xfer::vtog_tab[0x100];
26 int BC_Xfer::utog_tab[0x100], BC_Xfer::utob_tab[0x100];
27 float BC_Xfer::vtor_float_tab[0x100], BC_Xfer::vtog_float_tab[0x100];
28 float BC_Xfer::utog_float_tab[0x100], BC_Xfer::utob_float_tab[0x100];
29 int BC_Xfer::rtoy_tab16[0x10000], BC_Xfer::gtoy_tab16[0x10000], BC_Xfer::btoy_tab16[0x10000];
30 int BC_Xfer::rtou_tab16[0x10000], BC_Xfer::gtou_tab16[0x10000], BC_Xfer::btou_tab16[0x10000];
31 int BC_Xfer::rtov_tab16[0x10000], BC_Xfer::gtov_tab16[0x10000], BC_Xfer::btov_tab16[0x10000];
32 int BC_Xfer::vtor_tab16[0x10000], BC_Xfer::vtog_tab16[0x10000];
33 int BC_Xfer::utog_tab16[0x10000], BC_Xfer::utob_tab16[0x10000];
34 float BC_Xfer::v16tor_float_tab[0x10000], BC_Xfer::v16tog_float_tab[0x10000];
35 float BC_Xfer::u16tog_float_tab[0x10000], BC_Xfer::u16tob_float_tab[0x10000];
36
37 BC_Xfer::Tables BC_Xfer::tables;
38
39 void BC_Xfer::init()
40 {
41         for( int i=0; i<0x100; ++i ) {
42                 rtoy_tab[i] = (int)(R_TO_Y * 0x10000 * i);
43                 rtou_tab[i] = (int)(R_TO_U * 0x10000 * i);
44                 rtov_tab[i] = (int)(R_TO_V * 0x10000 * i);
45
46                 gtoy_tab[i] = (int)(G_TO_Y * 0x10000 * i);
47                 gtou_tab[i] = (int)(G_TO_U * 0x10000 * i);
48                 gtov_tab[i] = (int)(G_TO_V * 0x10000 * i);
49
50                 btoy_tab[i] = (int)(B_TO_Y * 0x10000 * i);
51                 btou_tab[i] = (int)(B_TO_U * 0x10000 * i) + 0x800000;
52                 btov_tab[i] = (int)(B_TO_V * 0x10000 * i) + 0x800000;
53         }
54
55         for( int i=0; i<0x10000; ++i ) {
56                 rtoy_tab16[i] = (int)(R_TO_Y * 0x100 * i);
57                 rtou_tab16[i] = (int)(R_TO_U * 0x100 * i);
58                 rtov_tab16[i] = (int)(R_TO_V * 0x100 * i);
59
60                 gtoy_tab16[i] = (int)(G_TO_Y * 0x100 * i);
61                 gtou_tab16[i] = (int)(G_TO_U * 0x100 * i);
62                 gtov_tab16[i] = (int)(G_TO_V * 0x100 * i);
63
64                 btoy_tab16[i] = (int)(B_TO_Y * 0x100 * i);
65                 btou_tab16[i] = (int)(B_TO_U * 0x100 * i) + 0x800000;
66                 btov_tab16[i] = (int)(B_TO_V * 0x100 * i) + 0x800000;
67         }
68
69         for( int i=-0x80; i<0x80; ++i ) {
70                 vtor_tab[i+0x80] = (int)(V_TO_R * 0x10000 * i);
71                 vtog_tab[i+0x80] = (int)(V_TO_G * 0x10000 * i);
72                 utog_tab[i+0x80] = (int)(U_TO_G * 0x10000 * i);
73                 utob_tab[i+0x80] = (int)(U_TO_B * 0x10000 * i);
74         }
75
76         for( int i=-0x80; i<0x80; ++i ) {
77                 vtor_float_tab[i+0x80] = V_TO_R * i / 0xff;
78                 vtog_float_tab[i+0x80] = V_TO_G * i / 0xff;
79                 utog_float_tab[i+0x80] = U_TO_G * i / 0xff;
80                 utob_float_tab[i+0x80] = U_TO_B * i / 0xff;
81         }
82
83         for( int i=-0x8000; i<0x8000; ++i ) {
84                 vtor_tab16[i+0x8000] = (int)(V_TO_R * 0x100 * i);
85                 vtog_tab16[i+0x8000] = (int)(V_TO_G * 0x100 * i);
86                 utog_tab16[i+0x8000] = (int)(U_TO_G * 0x100 * i);
87                 utob_tab16[i+0x8000] = (int)(U_TO_B * 0x100 * i);
88         }
89
90         for( int i=-0x8000; i<0x8000; ++i ) {
91                 v16tor_float_tab[i+0x8000] = V_TO_R * i / 0xffff;
92                 v16tog_float_tab[i+0x8000] = V_TO_G * i / 0xffff;
93                 u16tog_float_tab[i+0x8000] = U_TO_G * i / 0xffff;
94                 u16tob_float_tab[i+0x8000] = U_TO_B * i / 0xffff;
95         }
96 }
97
98 void BC_Xfer::init(
99         uint8_t **output_rows, int out_colormodel, int out_x, int out_y, int out_w, int out_h,
100                 uint8_t *out_yp, uint8_t *out_up, uint8_t *out_vp, uint8_t *out_ap, int out_rowspan,
101         uint8_t **input_rows, int in_colormodel, int in_x, int in_y, int in_w, int in_h,
102                 uint8_t *in_yp, uint8_t *in_up, uint8_t *in_vp, uint8_t *in_ap, int in_rowspan,
103         int bg_color)
104 {
105         // prevent bounds errors on poorly dimensioned macro pixel formats
106         switch( in_colormodel ) {
107         case BC_YUV422:   in_w &= ~1;               break;  // 2x1
108         case BC_YUV420P:  in_w &= ~1;  in_h &= ~1;  break;  // 2x2
109         case BC_YUV422P:  in_w &= ~1;               break;  // 2x1
110         case BC_YUV410P:  in_w &= ~3;  in_h &= ~3;  break;  // 4x4
111         case BC_YUV411P:  in_w &= ~3;               break;  // 4x1
112         }
113         switch( out_colormodel ) {
114         case BC_YUV422:  out_w &= ~1;               break;
115         case BC_YUV420P: out_w &= ~1; out_h &= ~1;  break;
116         case BC_YUV422P: out_w &= ~1;               break;
117         case BC_YUV410P: out_w &= ~3; out_h &= ~3;  break;
118         case BC_YUV411P: out_w &= ~3;               break;
119         }
120         this->output_rows = output_rows;
121         this->input_rows = input_rows;
122         this->out_yp = out_yp;
123         this->out_up = out_up;
124         this->out_vp = out_vp;
125         this->out_ap = out_ap;
126         this->in_yp = in_yp;
127         this->in_up = in_up;
128         this->in_vp = in_vp;
129         this->in_ap = in_ap;
130         this->in_x = in_x;
131         this->in_y = in_y;
132         this->in_w = in_w;
133         this->in_h = in_h;
134         this->out_x = out_x;
135         this->out_y = out_y;
136         this->out_w = out_w;
137         this->out_h = out_h;
138         this->in_colormodel = in_colormodel;
139         switch( in_colormodel ) {
140         case BC_RGB_FLOATP:
141         case BC_RGBA_FLOATP:
142                 in_rowspan = in_w * sizeof(float);
143                 break;
144         }
145         this->total_in_w = in_rowspan;
146         this->out_colormodel = out_colormodel;
147         switch( out_colormodel ) {
148         case BC_RGB_FLOATP:
149         case BC_RGBA_FLOATP:
150                 out_rowspan = out_w * sizeof(float);
151                 break;
152         }
153         this->total_out_w = out_rowspan;
154         this->bg_color = bg_color;
155         this->bg_r = (bg_color>>16) & 0xff;
156         this->bg_g = (bg_color>>8) & 0xff;
157         this->bg_b = (bg_color>>0) & 0xff;
158
159         this->in_pixelsize = BC_CModels::calculate_pixelsize(in_colormodel);
160         this->out_pixelsize = BC_CModels::calculate_pixelsize(out_colormodel);
161         this->scale = (out_w != in_w) || (in_x != 0);
162
163 /* + 1 so we don't overflow when calculating in advance */ 
164         column_table = new int[out_w+1];
165         double hscale = (double)in_w/out_w;
166         for( int i=0; i<out_w; ++i ) {
167                         column_table[i] = ((int)(hscale * i) + in_x) * in_pixelsize;
168                         if( in_colormodel == BC_YUV422 ) column_table[i] &= ~3;
169         }
170         double vscale = (double)in_h/out_h;
171         row_table = new int[out_h];
172         for( int i=0; i<out_h; ++i )
173                 row_table[i] = (int)(vscale * i) + in_y;
174 }
175
176 BC_Xfer::BC_Xfer(uint8_t **output_rows, uint8_t **input_rows,
177         uint8_t *out_yp, uint8_t *out_up, uint8_t *out_vp,
178         uint8_t *in_yp, uint8_t *in_up, uint8_t *in_vp,
179         int in_x, int in_y, int in_w, int in_h, int out_x, int out_y, int out_w, int out_h,
180         int in_colormodel, int out_colormodel, int bg_color, int in_rowspan, int out_rowspan)
181 {
182         init(output_rows, out_colormodel, out_x, out_y, out_w, out_h,
183                 out_yp, out_up, out_vp, 0, out_rowspan,
184              input_rows, in_colormodel, in_x, in_y, in_w, in_h,
185                 in_yp, in_up, in_vp, 0, in_rowspan,  bg_color);
186 }
187
188 BC_Xfer::BC_Xfer(
189         uint8_t **output_ptrs, int out_colormodel,
190                 int out_x, int out_y, int out_w, int out_h, int out_rowspan,
191         uint8_t **input_ptrs, int in_colormodel,
192                 int in_x, int in_y, int in_w, int in_h, int in_rowspan,
193         int bg_color)
194 {
195         uint8_t *out_yp = 0, *out_up = 0, *out_vp = 0, *out_ap = 0;
196         uint8_t *in_yp = 0,  *in_up = 0,  *in_vp = 0,  *in_ap = 0;
197         if( BC_CModels::is_planar(in_colormodel) ) {
198                 in_yp = input_ptrs[0]; in_up = input_ptrs[1]; in_vp = input_ptrs[2];
199                 if( BC_CModels::has_alpha(in_colormodel) ) in_ap = input_ptrs[3];
200         }
201         if( BC_CModels::is_planar(out_colormodel) ) {
202                 out_yp = output_ptrs[0]; out_up = output_ptrs[1]; out_vp = output_ptrs[2];
203                 if( BC_CModels::has_alpha(out_colormodel) ) out_ap = output_ptrs[3];
204         }
205         init(output_ptrs, out_colormodel, out_x, out_y, out_w, out_h,
206                  out_yp, out_up, out_vp, out_ap, out_rowspan,
207              input_ptrs, in_colormodel, in_x, in_y, in_w, in_h,
208                  in_yp, in_up, in_vp, in_ap, in_rowspan,  bg_color);
209 }
210
211 BC_Xfer::~BC_Xfer()
212 {
213         delete [] column_table;
214         delete [] row_table;
215 }
216
217 void BC_CModels::transfer(unsigned char **output_rows, unsigned char **input_rows,
218         unsigned char *out_yp, unsigned char *out_up, unsigned char *out_vp,
219         unsigned char *in_yp, unsigned char *in_up, unsigned char *in_vp,
220         int in_x, int in_y, int in_w, int in_h, int out_x, int out_y, int out_w, int out_h,
221         int in_colormodel, int out_colormodel, int bg_color, int in_rowspan, int out_rowspan)
222 {
223         BC_Xfer xfer(output_rows, input_rows,
224                 out_yp, out_up, out_vp, in_yp, in_up, in_vp,
225                 in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h,
226                 in_colormodel, out_colormodel, bg_color, in_rowspan, out_rowspan);
227         xfer.xfer();
228 }
229
230 void BC_CModels::transfer(
231         uint8_t **output_ptrs, int out_colormodel,
232                 int out_x, int out_y, int out_w, int out_h, int out_rowspan,
233         uint8_t **input_ptrs, int in_colormodel,
234                 int in_x, int in_y, int in_w, int in_h, int in_rowspan,  int bg_color)
235 {
236         BC_Xfer xfer(
237                 output_ptrs, out_colormodel, out_x, out_y, out_w, out_h, out_rowspan,
238                 input_ptrs, in_colormodel, in_x, in_y, in_w, in_h, in_rowspan,  bg_color);
239         xfer.xfer();
240 }
241
242 // specialized functions
243
244 //  in bccmdl.py:  specialize("bc_rgba8888", "bc_transparency", "XFER_rgba8888_to_transparency")
245 void BC_Xfer::XFER_rgba8888_to_transparency()
246 {
247   for( unsigned i=0; i<out_h; ++i ) {
248     uint8_t *outp = output_rows[i + out_y] + out_x * out_pixelsize;
249     uint8_t *inp_row = input_rows[row_table[i]];
250     int bit_no = 0, bit_vec = 0;
251     for( unsigned j=0; j<out_w; ) {
252       uint8_t *inp = inp_row + column_table[j];
253       if( inp[3] < 127 ) bit_vec |= 0x01 << bit_no;
254       bit_no = ++j & 7;
255       if( !bit_no ) { *outp++ = bit_vec;  bit_vec = 0; }
256     }
257     if( bit_no ) *outp = bit_vec;
258   } 
259 }
260