rework keyframe hide popup, keyframe auto render, textbox set_selection wide text
[goodguy/history.git] / cinelerra-5.1 / 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_UVY422:
108         case BC_YUV422:   in_w &= ~1;               break;  // 2x1
109         case BC_YUV420P:  in_w &= ~1;  in_h &= ~1;  break;  // 2x2
110         case BC_YUV422P:  in_w &= ~1;               break;  // 2x1
111         case BC_YUV410P:  in_w &= ~3;  in_h &= ~3;  break;  // 4x4
112         case BC_YUV411P:  in_w &= ~3;               break;  // 4x1
113         }
114         switch( out_colormodel ) {
115         case BC_UVY422:
116         case BC_YUV422:  out_w &= ~1;               break;
117         case BC_YUV420P: out_w &= ~1; out_h &= ~1;  break;
118         case BC_YUV422P: out_w &= ~1;               break;
119         case BC_YUV410P: out_w &= ~3; out_h &= ~3;  break;
120         case BC_YUV411P: out_w &= ~3;               break;
121         }
122         this->output_rows = output_rows;
123         this->input_rows = input_rows;
124         this->out_yp = out_yp;
125         this->out_up = out_up;
126         this->out_vp = out_vp;
127         this->out_ap = out_ap;
128         this->in_yp = in_yp;
129         this->in_up = in_up;
130         this->in_vp = in_vp;
131         this->in_ap = in_ap;
132         this->in_x = in_x;
133         this->in_y = in_y;
134         this->in_w = in_w;
135         this->in_h = in_h;
136         this->out_x = out_x;
137         this->out_y = out_y;
138         this->out_w = out_w;
139         this->out_h = out_h;
140         this->in_colormodel = in_colormodel;
141         switch( in_colormodel ) {
142         case BC_RGB_FLOATP:
143         case BC_RGBA_FLOATP:
144                 in_rowspan = in_w * sizeof(float);
145                 break;
146         }
147         this->total_in_w = in_rowspan;
148         this->out_colormodel = out_colormodel;
149         switch( out_colormodel ) {
150         case BC_RGB_FLOATP:
151         case BC_RGBA_FLOATP:
152                 out_rowspan = out_w * sizeof(float);
153                 break;
154         }
155         this->total_out_w = out_rowspan;
156         this->bg_color = bg_color;
157         this->bg_r = (bg_color>>16) & 0xff;
158         this->bg_g = (bg_color>>8) & 0xff;
159         this->bg_b = (bg_color>>0) & 0xff;
160
161         this->in_pixelsize = BC_CModels::calculate_pixelsize(in_colormodel);
162         this->out_pixelsize = BC_CModels::calculate_pixelsize(out_colormodel);
163         this->scale = (out_w != in_w) || (in_x != 0);
164
165 /* + 1 so we don't overflow when calculating in advance */ 
166         column_table = new int[out_w+1];
167         double hscale = (double)in_w/out_w;
168         for( int i=0; i<out_w; ++i ) {
169                         column_table[i] = ((int)(hscale * i) + in_x) * in_pixelsize;
170                         if( in_colormodel == BC_YUV422 || in_colormodel == BC_UVY422 )
171                                 column_table[i] &= ~3;
172         }
173         double vscale = (double)in_h/out_h;
174         row_table = new int[out_h];
175         for( int i=0; i<out_h; ++i )
176                 row_table[i] = (int)(vscale * i) + in_y;
177 }
178
179 BC_Xfer::BC_Xfer(uint8_t **output_rows, uint8_t **input_rows,
180         uint8_t *out_yp, uint8_t *out_up, uint8_t *out_vp,
181         uint8_t *in_yp, uint8_t *in_up, uint8_t *in_vp,
182         int in_x, int in_y, int in_w, int in_h, int out_x, int out_y, int out_w, int out_h,
183         int in_colormodel, int out_colormodel, int bg_color, int in_rowspan, int out_rowspan)
184 {
185         init(output_rows, out_colormodel, out_x, out_y, out_w, out_h,
186                 out_yp, out_up, out_vp, 0, out_rowspan,
187              input_rows, in_colormodel, in_x, in_y, in_w, in_h,
188                 in_yp, in_up, in_vp, 0, in_rowspan,  bg_color);
189 }
190
191 BC_Xfer::BC_Xfer(
192         uint8_t **output_ptrs, int out_colormodel,
193                 int out_x, int out_y, int out_w, int out_h, int out_rowspan,
194         uint8_t **input_ptrs, int in_colormodel,
195                 int in_x, int in_y, int in_w, int in_h, int in_rowspan,
196         int bg_color)
197 {
198         uint8_t *out_yp = 0, *out_up = 0, *out_vp = 0, *out_ap = 0;
199         uint8_t *in_yp = 0,  *in_up = 0,  *in_vp = 0,  *in_ap = 0;
200         if( BC_CModels::is_planar(in_colormodel) ) {
201                 in_yp = input_ptrs[0]; in_up = input_ptrs[1]; in_vp = input_ptrs[2];
202                 if( BC_CModels::has_alpha(in_colormodel) ) in_ap = input_ptrs[3];
203         }
204         if( BC_CModels::is_planar(out_colormodel) ) {
205                 out_yp = output_ptrs[0]; out_up = output_ptrs[1]; out_vp = output_ptrs[2];
206                 if( BC_CModels::has_alpha(out_colormodel) ) out_ap = output_ptrs[3];
207         }
208         init(output_ptrs, out_colormodel, out_x, out_y, out_w, out_h,
209                  out_yp, out_up, out_vp, out_ap, out_rowspan,
210              input_ptrs, in_colormodel, in_x, in_y, in_w, in_h,
211                  in_yp, in_up, in_vp, in_ap, in_rowspan,  bg_color);
212 }
213
214 BC_Xfer::~BC_Xfer()
215 {
216         delete [] column_table;
217         delete [] row_table;
218 }
219
220 void BC_CModels::transfer(unsigned char **output_rows, unsigned char **input_rows,
221         unsigned char *out_yp, unsigned char *out_up, unsigned char *out_vp,
222         unsigned char *in_yp, unsigned char *in_up, unsigned char *in_vp,
223         int in_x, int in_y, int in_w, int in_h, int out_x, int out_y, int out_w, int out_h,
224         int in_colormodel, int out_colormodel, int bg_color, int in_rowspan, int out_rowspan)
225 {
226         BC_Xfer xfer(output_rows, input_rows,
227                 out_yp, out_up, out_vp, in_yp, in_up, in_vp,
228                 in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h,
229                 in_colormodel, out_colormodel, bg_color, in_rowspan, out_rowspan);
230         xfer.xfer();
231 }
232
233 void BC_CModels::transfer(
234         uint8_t **output_ptrs, int out_colormodel,
235                 int out_x, int out_y, int out_w, int out_h, int out_rowspan,
236         uint8_t **input_ptrs, int in_colormodel,
237                 int in_x, int in_y, int in_w, int in_h, int in_rowspan,  int bg_color)
238 {
239         BC_Xfer xfer(
240                 output_ptrs, out_colormodel, out_x, out_y, out_w, out_h, out_rowspan,
241                 input_ptrs, in_colormodel, in_x, in_y, in_w, in_h, in_rowspan,  bg_color);
242         xfer.xfer();
243 }
244
245 // specialized functions
246
247 //  in bccmdl.py:  specialize("bc_rgba8888", "bc_transparency", "XFER_rgba8888_to_transparency")
248 void BC_Xfer::XFER_rgba8888_to_transparency()
249 {
250   for( unsigned i=0; i<out_h; ++i ) {
251     uint8_t *outp = output_rows[i + out_y] + out_x * out_pixelsize;
252     uint8_t *inp_row = input_rows[row_table[i]];
253     int bit_no = 0, bit_vec = 0;
254     for( unsigned j=0; j<out_w; ) {
255       uint8_t *inp = inp_row + column_table[j];
256       if( inp[3] < 127 ) bit_vec |= 0x01 << bit_no;
257       bit_no = ++j & 7;
258       if( !bit_no ) { *outp++ = bit_vec;  bit_vec = 0; }
259     }
260     if( bit_no ) *outp = bit_vec;
261   } 
262 }
263