fullscreen segv fix, popup for 4opts preview, renderfarm print fix, pan widget upgrad...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / affine.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifdef HAVE_GL
23 #define GL_GLEXT_PROTOTYPES
24 #include <GL/gl.h>
25 #endif
26
27 #include "affine.h"
28 #include "interp.h"
29 #include "clip.h"
30 #include "vframe.h"
31
32
33 #include <math.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 AffineMatrix::AffineMatrix()
39 {
40         bzero(values, sizeof(values));
41 }
42
43 void AffineMatrix::identity()
44 {
45         bzero(values, sizeof(values));
46         values[0][0] = 1;
47         values[1][1] = 1;
48         values[2][2] = 1;
49 }
50
51 void AffineMatrix::translate(double x, double y)
52 {
53         double g = values[2][0];
54         double h = values[2][1];
55         double i = values[2][2];
56         values[0][0] += x * g;
57         values[0][1] += x * h;
58         values[0][2] += x * i;
59         values[1][0] += y * g;
60         values[1][1] += y * h;
61         values[1][2] += y * i;
62 }
63
64 void AffineMatrix::scale(double x, double y)
65 {
66         values[0][0] *= x;
67         values[0][1] *= x;
68         values[0][2] *= x;
69
70         values[1][0] *= y;
71         values[1][1] *= y;
72         values[1][2] *= y;
73 }
74
75 void AffineMatrix::multiply(AffineMatrix *dst)
76 {
77         AffineMatrix tmp;
78
79         for( int i=0; i<3; ++i ) {
80                 double t1 = values[i][0], t2 = values[i][1], t3 = values[i][2];
81                 for( int j=0; j<3; ++j ) {
82                         tmp.values[i][j]  = t1 * dst->values[0][j];
83                         tmp.values[i][j] += t2 * dst->values[1][j];
84                         tmp.values[i][j] += t3 * dst->values[2][j];
85                 }
86         }
87         dst->copy_from(&tmp);
88 }
89
90 double AffineMatrix::determinant()
91 {
92         double determinant;
93
94         determinant  =
95                 values[0][0] * (values[1][1] * values[2][2] - values[1][2] * values[2][1]);
96         determinant -=
97                 values[1][0] * (values[0][1] * values[2][2] - values[0][2] * values[2][1]);
98         determinant +=
99                 values[2][0] * (values[0][1] * values[1][2] - values[0][2] * values[1][1]);
100
101         return determinant;
102 }
103
104 void AffineMatrix::invert(AffineMatrix *dst)
105 {
106         double det_1;
107
108         det_1 = determinant();
109
110         if(det_1 == 0.0)
111         return;
112
113         det_1 = 1.0 / det_1;
114
115         dst->values[0][0] =
116                 (values[1][1] * values[2][2] - values[1][2] * values[2][1]) * det_1;
117
118         dst->values[1][0] =
119                 - (values[1][0] * values[2][2] - values[1][2] * values[2][0]) * det_1;
120
121         dst->values[2][0] =
122                 (values[1][0] * values[2][1] - values[1][1] * values[2][0]) * det_1;
123
124         dst->values[0][1] =
125                 - (values[0][1] * values[2][2] - values[0][2] * values[2][1] ) * det_1;
126
127         dst->values[1][1] =
128                 (values[0][0] * values[2][2] - values[0][2] * values[2][0]) * det_1;
129
130         dst->values[2][1] =
131                 - (values[0][0] * values[2][1] - values[0][1] * values[2][0]) * det_1;
132
133         dst->values[0][2] =
134                 (values[0][1] * values[1][2] - values[0][2] * values[1][1]) * det_1;
135
136         dst->values[1][2] =
137                 - (values[0][0] * values[1][2] - values[0][2] * values[1][0]) * det_1;
138
139         dst->values[2][2] =
140                 (values[0][0] * values[1][1] - values[0][1] * values[1][0]) * det_1;
141 }
142
143 void AffineMatrix::copy_from(AffineMatrix *src)
144 {
145         memcpy(&values[0][0], &src->values[0][0], sizeof(values));
146 }
147
148 void AffineMatrix::set_matrix(
149         double in_x1, double in_y1, double in_x2, double in_y2,
150         double out_x1, double out_y1, double out_x2, double out_y2,
151         double out_x3, double out_y3, double out_x4, double out_y4)
152 {
153         double scalex = in_x2 > in_x1 ? 1./(in_x2 - in_x1) : 1.0;
154         double scaley = in_y2 > in_y1 ? 1./(in_y2 - in_y1) : 1.0;
155         double dx1 = out_x2 - out_x4, dx2 = out_x3 - out_x4;
156         double dx3 = out_x1 - out_x2 + out_x4 - out_x3;
157
158         double dy1 = out_y2 - out_y4, dy2 = out_y3 - out_y4;
159         double dy3 = out_y1 - out_y2 + out_y4 - out_y3;
160         double det = dx1 * dy2 - dy1 * dx2;
161         if( !det ) { identity();  return; }
162
163         AffineMatrix m;
164         m.values[2][0] = (dx3 * dy2 - dy3 * dx2) / det;
165         m.values[2][1] = (dx1 * dy3 - dy1 * dx3) / det;
166         m.values[0][0] = out_x2 - out_x1 + m.values[2][0] * out_x2;
167         m.values[0][1] = out_x3 - out_x1 + m.values[2][1] * out_x3;
168         m.values[0][2] = out_x1;
169         m.values[1][0] = out_y2 - out_y1 + m.values[2][0] * out_y2;
170         m.values[1][1] = out_y3 - out_y1 + m.values[2][1] * out_y3;
171         m.values[1][2] = out_y1;
172         m.values[2][2] = 1.0;
173
174         identity();
175         translate(-in_x1, -in_y1);
176         scale(scalex, scaley);
177         m.multiply(this);
178 }
179
180 void AffineMatrix::transform_point(float x,
181         float y,
182         float *newx,
183         float *newy)
184 {
185         double w;
186
187         w = values[2][0] * x + values[2][1] * y + values[2][2];
188         w = !w ? 1 : 1/w;
189
190         *newx = (values[0][0] * x + values[0][1] * y + values[0][2]) * w;
191         *newy = (values[1][0] * x + values[1][1] * y + values[1][2]) * w;
192 }
193
194 void AffineMatrix::dump()
195 {
196         printf("AffineMatrix::dump\n");
197         printf("%f %f %f\n", values[0][0], values[0][1], values[0][2]);
198         printf("%f %f %f\n", values[1][0], values[1][1], values[1][2]);
199         printf("%f %f %f\n", values[2][0], values[2][1], values[2][2]);
200 }
201
202
203
204
205
206 AffinePackage::AffinePackage()
207  : LoadPackage()
208 {
209 }
210
211
212
213
214 AffineUnit::AffineUnit(AffineEngine *server)
215  : LoadClient(server)
216 {
217         this->server = server;
218 }
219
220
221 static inline float transform_cubic(float dx,
222                 float p0, float p1, float p2, float p3)
223 {
224 /* Catmull-Rom - not bad */
225         float result = ((( (- p0 + 3*p1 - 3*p2 + p3) * dx +
226                          ( 2*p0 - 5*p1 + 4*p2 - p3 ) ) * dx +
227                          ( - p0 + p2 ) ) * dx + (p1 + p1) ) / 2;
228 // printf("%f %f %f %f %f\n", result, p0, p1, p2, p3);
229         return result;
230 }
231
232 static inline float transform_linear(float dx,
233                 float p1, float p2)
234 {
235         float result = p1 * (1-dx) + p2 * dx;
236         return result;
237 }
238
239
240 void AffineUnit::process_package(LoadPackage *package)
241 {
242         AffinePackage *pkg = (AffinePackage*)package;
243         int min_in_x = server->in_x;
244         int min_in_y = server->in_y;
245         int max_in_x = server->in_x + server->in_w - 1;
246         int max_in_y = server->in_y + server->in_h - 1;
247
248
249 // printf("AffineUnit::process_package %d %d %d %d %d\n",
250 // __LINE__,
251 // min_in_x,
252 // min_in_y,
253 // max_in_x,
254 // max_in_y);
255         int min_out_x = server->out_x;
256         //int min_out_y = server->out_y;
257         int max_out_x = server->out_x + server->out_w;
258         //int max_out_y = server->out_y + server->out_h;
259
260 // Amount to shift the input coordinates relative to the output coordinates
261 // To get the pivots to line up
262         int pivot_offset_x = server->in_pivot_x - server->out_pivot_x;
263         int pivot_offset_y = server->in_pivot_y - server->out_pivot_y;
264
265 // Calculate real coords
266         float out_x1, out_y1, out_x2, out_y2, out_x3, out_y3, out_x4, out_y4;
267         if( server->mode == AffineEngine::STRETCH ||
268             server->mode == AffineEngine::PERSPECTIVE ||
269             server->mode == AffineEngine::ROTATE ||
270             server->mode == AffineEngine::TRANSFORM ) {
271                 out_x1 = (float)server->in_x + (float)server->x1 * server->in_w / 100;
272                 out_y1 = (float)server->in_y + (float)server->y1 * server->in_h / 100;
273                 out_x2 = (float)server->in_x + (float)server->x2 * server->in_w / 100;
274                 out_y2 = (float)server->in_y + (float)server->y2 * server->in_h / 100;
275                 out_x3 = (float)server->in_x + (float)server->x3 * server->in_w / 100;
276                 out_y3 = (float)server->in_y + (float)server->y3 * server->in_h / 100;
277                 out_x4 = (float)server->in_x + (float)server->x4 * server->in_w / 100;
278                 out_y4 = (float)server->in_y + (float)server->y4 * server->in_h / 100;
279         }
280         else {
281                 out_x1 = (float)server->in_x + (float)server->x1 * server->in_w / 100;
282                 out_y1 = server->in_y;
283                 out_x2 = out_x1 + server->in_w;
284                 out_y2 = server->in_y;
285                 out_x4 = (float)server->in_x + (float)server->x4 * server->in_w / 100;
286                 out_y4 = server->in_y + server->in_h;
287                 out_x3 = out_x4 + server->in_w;
288                 out_y3 = server->in_y + server->in_h;
289         }
290
291
292
293 // Rotation with OpenGL uses a simple quad.
294         if( server->mode == AffineEngine::ROTATE &&
295             server->use_opengl ) {
296 #ifdef HAVE_GL
297                 out_x1 -= pivot_offset_x;  out_y1 -= pivot_offset_y;
298                 out_x2 -= pivot_offset_x;  out_y2 -= pivot_offset_y;
299                 out_x3 -= pivot_offset_x;  out_y3 -= pivot_offset_y;
300                 out_x4 -= pivot_offset_x;  out_y4 -= pivot_offset_y;
301
302                 server->output->to_texture();
303                 server->output->enable_opengl();
304                 server->output->init_screen();
305                 server->output->bind_texture(0);
306                 server->output->clear_pbuffer();
307
308                 int texture_w = server->output->get_texture_w();
309                 int texture_h = server->output->get_texture_h();
310                 float output_h = server->output->get_h();
311                 float in_x1 = (float)server->in_x / texture_w;
312                 float in_x2 = (float)(server->in_x + server->in_w) / texture_w;
313                 float in_y1 = (float)server->in_y / texture_h;
314                 float in_y2 = (float)(server->in_y + server->in_h) / texture_h;
315
316 // printf("%f %f %f %f\n%f,%f %f,%f %f,%f %f,%f\n", in_x1, in_y1, in_x2, in_y2,
317 // out_x1, out_y1, out_x2, out_y2, out_x3, out_y3, out_x4, out_y4);
318
319                 glBegin(GL_QUADS);
320                 glNormal3f(0, 0, 1.0);
321
322                 glTexCoord2f(in_x1, in_y1);
323                 glVertex3f(out_x1, -output_h+out_y1, 0);
324
325                 glTexCoord2f(in_x2, in_y1);
326                 glVertex3f(out_x2, -output_h+out_y2, 0);
327
328                 glTexCoord2f(in_x2, in_y2);
329                 glVertex3f(out_x3, -output_h+out_y3, 0);
330
331                 glTexCoord2f(in_x1, in_y2);
332                 glVertex3f(out_x4, -output_h+out_y4, 0);
333
334
335                 glEnd();
336
337                 server->output->set_opengl_state(VFrame::SCREEN);
338 #endif
339         }
340         else
341         if( server->mode == AffineEngine::PERSPECTIVE ||
342             server->mode == AffineEngine::SHEER ||
343             server->mode == AffineEngine::ROTATE ||
344             server->mode == AffineEngine::TRANSFORM ) {
345                 AffineMatrix matrix;
346                 float temp;
347 // swap points 3 & 4
348                 temp = out_x4;
349                 out_x4 = out_x3;
350                 out_x3 = temp;
351                 temp = out_y4;
352                 out_y4 = out_y3;
353                 out_y3 = temp;
354
355
356
357
358
359                 if( server->mode != AffineEngine::TRANSFORM ) {
360                         matrix.set_matrix(server->in_x, server->in_y,
361                                 server->in_x + server->in_w,
362                                 server->in_y + server->in_h,
363                                 out_x1, out_y1, out_x2, out_y2,
364                                 out_x3, out_y3, out_x4, out_y4);
365                 }
366                 else {
367                         matrix.copy_from(&server->matrix);
368                 }
369
370 //printf("AffineUnit::process_package %d\n%f %f %f\n%f %f %f\n%f %f %f\n", __LINE__,
371 // matrix.values[0][0], matrix.values[0][1], matrix.values[0][2],
372 // matrix.values[1][0], matrix.values[1][1], matrix.values[1][2],
373 // matrix.values[2][0], matrix.values[2][1], matrix.values[2][2]);
374                 int reverse = !server->forward;
375                 float tx, ty, tw;
376                 float xinc, yinc, winc;
377                 AffineMatrix m, im;
378                 float ttx = 0, tty = 0;
379                 int tx1 = 0, ty1 = 0, tx2 = 0, ty2 = 0;
380
381                 if(reverse) {
382                         m.copy_from(&matrix);
383                         m.invert(&im);
384                         matrix.copy_from(&im);
385                 }
386                 else {
387                         matrix.invert(&m);
388                 }
389
390                 float dx1 = 0, dy1 = 0;
391                 float dx2 = 0, dy2 = 0;
392                 float dx3 = 0, dy3 = 0;
393                 float dx4 = 0, dy4 = 0;
394                 matrix.transform_point(server->in_x, server->in_y, &dx1, &dy1);
395                 matrix.transform_point(server->in_x + server->in_w, server->in_y, &dx2, &dy2);
396                 matrix.transform_point(server->in_x, server->in_y + server->in_h, &dx3, &dy3);
397                 matrix.transform_point(server->in_x + server->in_w, server->in_y + server->in_h, &dx4, &dy4);
398
399 //printf("AffineUnit::process_package 1 y1=%d y2=%d\n", pkg->y1, pkg->y2);
400 //printf("AffineUnit::process_package 1 %f %f %f %f\n", dy1, dy2, dy3, dy4);
401 // printf("AffineUnit::process_package %d use_opengl=%d\n",
402 // __LINE__, server->use_opengl);
403
404                 if( server->use_opengl &&
405                     server->interpolation == AffineEngine::AF_DEFAULT ) {
406 #ifdef HAVE_GL
407                         static const char *affine_frag =
408                                 "uniform sampler2D tex;\n"
409                                 "uniform mat3 affine_matrix;\n"
410                                 "uniform vec2 texture_extents;\n"
411                                 "uniform vec2 image_extents;\n"
412                                 "uniform vec4 border_color;\n"
413                                 "void main()\n"
414                                 "{\n"
415                                 "       vec2 outcoord = gl_TexCoord[0].st;\n"
416                                 "       outcoord *= texture_extents;\n"
417                                 "       mat3 coord_matrix = mat3(\n"
418                                 "               outcoord.x, outcoord.y, 1.0, \n"
419                                 "               outcoord.x, outcoord.y, 1.0, \n"
420                                 "               outcoord.x, outcoord.y, 1.0);\n"
421                                 "       mat3 incoord_matrix = affine_matrix * coord_matrix;\n"
422                                 "       vec2 incoord = vec2(incoord_matrix[0][0], incoord_matrix[0][1]);\n"
423                                 "       incoord /= incoord_matrix[0][2];\n"
424                                 "       incoord /= texture_extents;\n"
425                                 "       if(incoord.x > image_extents.x || incoord.y > image_extents.y)\n"
426                                 "               gl_FragColor = border_color;\n"
427                                 "       else\n"
428                                 "               gl_FragColor = texture2D(tex, incoord);\n"
429                                 "}\n";
430
431                         float affine_matrix[9] = {
432                                 (float)m.values[0][0], (float)m.values[1][0], (float)m.values[2][0],
433                                 (float)m.values[0][1], (float)m.values[1][1], (float)m.values[2][1],
434                                 (float)m.values[0][2], (float)m.values[1][2], (float)m.values[2][2]
435                         };
436
437
438                         server->output->to_texture();
439                         server->output->enable_opengl();
440                         unsigned int frag_shader = VFrame::make_shader(0, affine_frag, 0);
441                         if( frag_shader > 0 ) {
442                                 glUseProgram(frag_shader);
443                                 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
444                                 glUniformMatrix3fv(glGetUniformLocation(frag_shader, "affine_matrix"),
445                                         1, 0, affine_matrix);
446                                 glUniform2f(glGetUniformLocation(frag_shader, "texture_extents"),
447                                         (GLfloat)server->output->get_texture_w(),
448                                         (GLfloat)server->output->get_texture_h());
449                                 glUniform2f(glGetUniformLocation(frag_shader, "image_extents"),
450                                         (GLfloat)server->output->get_w() / server->output->get_texture_w(),
451                                         (GLfloat)server->output->get_h() / server->output->get_texture_h());
452                                 float border_color[] = { 0, 0, 0, 0 };
453                                 if(BC_CModels::is_yuv(server->output->get_color_model())) {
454                                         border_color[1] = 0.5;
455                                         border_color[2] = 0.5;
456                                 }
457                                 if(!BC_CModels::has_alpha(server->output->get_color_model())) {
458                                         border_color[3] = 1.0;
459                                 }
460
461                                 glUniform4fv(glGetUniformLocation(frag_shader, "border_color"),
462                                         1, (GLfloat*)border_color);
463                                 server->output->init_screen();
464                                 server->output->bind_texture(0);
465                                 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
466                                 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color);
467                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
468                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
469                                 server->output->draw_texture();
470                                 glUseProgram(0);
471                                 server->output->set_opengl_state(VFrame::SCREEN);
472                         }
473                         return;
474 #endif // HAVE_GL
475                 }
476
477 #define ROUND(x) ((int)((x > 0) ? (x) + 0.5 : (x) - 0.5))
478 #define MIN4(a,b,c,d) MIN(MIN(MIN(a,b),c),d)
479 #define MAX4(a,b,c,d) MAX(MAX(MAX(a,b),c),d)
480
481         tx1 = ROUND(MIN4(dx1 - pivot_offset_x, dx2 - pivot_offset_x, dx3 - pivot_offset_x, dx4 - pivot_offset_x));
482         ty1 = ROUND(MIN4(dy1 - pivot_offset_y, dy2 - pivot_offset_y, dy3 - pivot_offset_y, dy4 - pivot_offset_y));
483
484         tx2 = ROUND(MAX4(dx1 - pivot_offset_x, dx2 - pivot_offset_x, dx3 - pivot_offset_x, dx4 - pivot_offset_x));
485         ty2 = ROUND(MAX4(dy1 - pivot_offset_y, dy2 - pivot_offset_y, dy3 - pivot_offset_y, dy4 - pivot_offset_y));
486
487         CLAMP(ty1, pkg->y1, pkg->y2);
488         CLAMP(ty2, pkg->y1, pkg->y2);
489         CLAMP(tx1, server->out_x, server->out_x + server->out_w);
490         CLAMP(tx2, server->out_x, server->out_x + server->out_w);
491
492
493         xinc = m.values[0][0];
494         yinc = m.values[1][0];
495         winc = m.values[2][0];
496
497 //printf("AffineUnit::process_package 2 tx1=%d ty1=%d tx2=%d ty2=%d %f %f\n",
498 // tx1, ty1, tx2, ty2, out_x4, out_y4);
499 //printf("AffineUnit::process_package %d %d %d %d %d\n",
500 // __LINE__, min_in_x, max_in_x, min_in_y, max_in_y);
501
502 #define DO_INTERP(tag, interp, components, type, temp_type, chroma, max) \
503 case tag: { \
504     type **inp_rows = (type**)server->input->get_rows(); \
505     type **out_rows = (type**)server->output->get_rows(); \
506     float round_factor = sizeof(type) < 4 ? 0.5 : 0; \
507     INTERP_SETUP(inp_rows, max, min_in_x,min_in_y, max_in_x,max_in_y); \
508  \
509     for( int y=ty1; y<ty2; ++y ) { \
510         type *out_row = (type*)out_rows[y]; \
511         int x1 = tx1, x2 = tx2; \
512         if( x1 < min_out_x ) x1 = min_out_x; \
513         if( x2 > max_out_x ) x2 = max_out_x; \
514         tx = xinc * x1 + m.values[0][1] * (y + pivot_offset_y) + m.values[0][2] \
515             + pivot_offset_x * xinc; \
516         ty = yinc * x1 + m.values[1][1] * (y + pivot_offset_y) + m.values[1][2] \
517             + pivot_offset_x * yinc; \
518         tw = winc * x1 + m.values[2][1] * (y + pivot_offset_y) + m.values[2][2] \
519             + pivot_offset_x * winc; \
520         type *out = out_row + x1 * components; \
521  \
522         for( int x=x1; x<x2; ++x ) { \
523 /* Normalize homogeneous coords */ \
524             if( tw == 0.0 ) { ttx = 0.0; tty = 0.0; } \
525             else { ttx = tx / tw; tty = ty / tw; } \
526             interp##_SETUP(type, components, ttx, tty); \
527             *out++ = ((temp_type)interp##_interp(0, 0) + round_factor); \
528             interp##_next(); \
529             *out++ = ((temp_type)interp##_interp(chroma, chroma) + round_factor); \
530             interp##_next(); \
531             *out++ = ((temp_type)interp##_interp(chroma, chroma) + round_factor); \
532             if( components == 4 ) { \
533                 interp##_next(); \
534                 *out++ = ((temp_type)interp##_interp(0, 0) + round_factor); \
535             } \
536  \
537 /*  increment the transformed coordinates  */ \
538             tx += xinc;  ty += yinc;  tw += winc; \
539         } \
540     } \
541 } break
542
543 // printf("AffineUnit::process_package %d tx1=%d ty1=%d tx2=%d ty2=%d\n",
544 // __LINE__, tx1, ty1, tx2, ty2);
545
546                 switch( server->interpolation ) {
547                 case AffineEngine::AF_NEAREST:
548                         switch( server->input->get_color_model() ) {
549                         DO_INTERP( BC_RGB_FLOAT, nearest, 3, float, float, 0x0, 1.0);
550                         DO_INTERP( BC_RGB888, nearest, 3, unsigned char, int, 0x0, 0xff);
551                         DO_INTERP( BC_RGBA_FLOAT, nearest, 4, float, float, 0x0, 1.0);
552                         DO_INTERP( BC_RGBA8888, nearest, 4, unsigned char, int, 0x0, 0xff);
553                         DO_INTERP( BC_YUV888, nearest, 3, unsigned char, int, 0x80, 0xff);
554                         DO_INTERP( BC_YUVA8888, nearest, 4, unsigned char, int, 0x80, 0xff);
555                         DO_INTERP( BC_RGB161616, nearest, 3, uint16_t, int, 0x0, 0xffff);
556                         DO_INTERP( BC_RGBA16161616, nearest, 4, uint16_t, int, 0x0, 0xffff);
557                         DO_INTERP( BC_YUV161616, nearest, 3, uint16_t, int, 0x8000, 0xffff);
558                         DO_INTERP( BC_YUVA16161616, nearest, 4, uint16_t, int, 0x8000, 0xffff);
559                         }
560                         break;
561                 case AffineEngine::AF_LINEAR:
562                         switch( server->input->get_color_model() ) {
563                         DO_INTERP( BC_RGB_FLOAT, bi_linear, 3, float, float, 0x0, 1.0);
564                         DO_INTERP( BC_RGB888, bi_linear, 3, unsigned char, int, 0x0, 0xff);
565                         DO_INTERP( BC_RGBA_FLOAT, bi_linear, 4, float, float, 0x0, 1.0);
566                         DO_INTERP( BC_RGBA8888, bi_linear, 4, unsigned char, int, 0x0, 0xff);
567                         DO_INTERP( BC_YUV888, bi_linear, 3, unsigned char, int, 0x80, 0xff);
568                         DO_INTERP( BC_YUVA8888, bi_linear, 4, unsigned char, int, 0x80, 0xff);
569                         DO_INTERP( BC_RGB161616, bi_linear, 3, uint16_t, int, 0x0, 0xffff);
570                         DO_INTERP( BC_RGBA16161616, bi_linear, 4, uint16_t, int, 0x0, 0xffff);
571                         DO_INTERP( BC_YUV161616, bi_linear, 3, uint16_t, int, 0x8000, 0xffff);
572                         DO_INTERP( BC_YUVA16161616, bi_linear, 4, uint16_t, int, 0x8000, 0xffff);
573                         }
574                         break;
575                 default:
576                 case AffineEngine::AF_CUBIC:
577                         switch( server->input->get_color_model() ) {
578                         DO_INTERP( BC_RGB_FLOAT, bi_cubic, 3, float, float, 0x0, 1.0);
579                         DO_INTERP( BC_RGB888, bi_cubic, 3, unsigned char, int, 0x0, 0xff);
580                         DO_INTERP( BC_RGBA_FLOAT, bi_cubic, 4, float, float, 0x0, 1.0);
581                         DO_INTERP( BC_RGBA8888, bi_cubic, 4, unsigned char, int, 0x0, 0xff);
582                         DO_INTERP( BC_YUV888, bi_cubic, 3, unsigned char, int, 0x80, 0xff);
583                         DO_INTERP( BC_YUVA8888, bi_cubic, 4, unsigned char, int, 0x80, 0xff);
584                         DO_INTERP( BC_RGB161616, bi_cubic, 3, uint16_t, int, 0x0, 0xffff);
585                         DO_INTERP( BC_RGBA16161616, bi_cubic, 4, uint16_t, int, 0x0, 0xffff);
586                         DO_INTERP( BC_YUV161616, bi_cubic, 3, uint16_t, int, 0x8000, 0xffff);
587                         DO_INTERP( BC_YUVA16161616, bi_cubic, 4, uint16_t, int, 0x8000, 0xffff);
588                         }
589                         break;
590                 }
591         }
592         else
593         {
594                 int min_x = server->in_x * AFFINE_OVERSAMPLE;
595                 int min_y = server->in_y * AFFINE_OVERSAMPLE;
596                 int max_x = server->in_x * AFFINE_OVERSAMPLE + server->in_w * AFFINE_OVERSAMPLE - 1;
597                 int max_y = server->in_y * AFFINE_OVERSAMPLE + server->in_h * AFFINE_OVERSAMPLE - 1;
598                 float top_w = out_x2 - out_x1;
599                 float bottom_w = out_x3 - out_x4;
600                 float left_h = out_y4 - out_y1;
601                 float right_h = out_y3 - out_y2;
602                 float out_w_diff = bottom_w - top_w;
603                 float out_left_diff = out_x4 - out_x1;
604                 float out_h_diff = right_h - left_h;
605                 float out_top_diff = out_y2 - out_y1;
606                 float distance1 = DISTANCE(out_x1, out_y1, out_x2, out_y2);
607                 float distance2 = DISTANCE(out_x2, out_y2, out_x3, out_y3);
608                 float distance3 = DISTANCE(out_x3, out_y3, out_x4, out_y4);
609                 float distance4 = DISTANCE(out_x4, out_y4, out_x1, out_y1);
610                 float max_v = MAX(distance1, distance3);
611                 float max_h = MAX(distance2, distance4);
612                 float max_dimension = MAX(max_v, max_h);
613                 float min_dimension = MIN(server->in_h, server->in_w);
614                 float step = min_dimension / max_dimension / AFFINE_OVERSAMPLE;
615                 float x_f = server->in_x;
616                 float y_f = server->in_y;
617                 float h_f = server->in_h;
618                 float w_f = server->in_w;
619
620                 if(server->use_opengl) {
621                         return;
622                 }
623
624 // Projection
625 #define DO_STRETCH(tag, type, components) \
626 case tag: { \
627         type **in_rows = (type**)server->input->get_rows(); \
628         type **out_rows = (type**)server->temp->get_rows(); \
629  \
630         for(float in_y = pkg->y1; in_y < pkg->y2; in_y += step) \
631         { \
632                 int i = (int)in_y; \
633                 type *in_row = in_rows[i]; \
634                 for(float in_x = x_f; in_x < w_f; in_x += step) \
635                 { \
636                         int j = (int)in_x; \
637                         float in_x_fraction = (in_x - x_f) / w_f; \
638                         float in_y_fraction = (in_y - y_f) / h_f; \
639                         int out_x = (int)((out_x1 + \
640                                 out_left_diff * in_y_fraction + \
641                                 (top_w + out_w_diff * in_y_fraction) * in_x_fraction) *  \
642                                 AFFINE_OVERSAMPLE); \
643                         int out_y = (int)((out_y1 +  \
644                                 out_top_diff * in_x_fraction + \
645                                 (left_h + out_h_diff * in_x_fraction) * in_y_fraction) * \
646                                 AFFINE_OVERSAMPLE); \
647                         CLAMP(out_x, min_x, max_x); \
648                         CLAMP(out_y, min_y, max_y); \
649                         type *dst = out_rows[out_y] + out_x * components; \
650                         type *src = in_row + j * components; \
651                         dst[0] = src[0]; \
652                         dst[1] = src[1]; \
653                         dst[2] = src[2]; \
654                         if(components == 4) dst[3] = src[3]; \
655                 } \
656         } \
657 } break
658
659                 switch( server->input->get_color_model() ) {
660                 DO_STRETCH( BC_RGB_FLOAT, float, 3 );
661                 DO_STRETCH( BC_RGB888, unsigned char, 3 );
662                 DO_STRETCH( BC_RGBA_FLOAT, float, 4 );
663                 DO_STRETCH( BC_RGBA8888, unsigned char, 4 );
664                 DO_STRETCH( BC_YUV888, unsigned char, 3 );
665                 DO_STRETCH( BC_YUVA8888, unsigned char, 4 );
666                 DO_STRETCH( BC_RGB161616, uint16_t, 3 );
667                 DO_STRETCH( BC_RGBA16161616, uint16_t, 4 );
668                 DO_STRETCH( BC_YUV161616, uint16_t, 3 );
669                 DO_STRETCH( BC_YUVA16161616, uint16_t, 4 );
670                 }
671         }
672 }
673
674
675 AffineEngine::AffineEngine(int total_clients, int total_packages)
676  : LoadServer(total_clients, total_packages) //(1, 1)
677 {
678         user_in_viewport = 0;
679         user_in_pivot = 0;
680         user_out_viewport = 0;
681         user_out_pivot = 0;
682         use_opengl = 0;
683         in_x = in_y = in_w = in_h = 0;
684         out_x = out_y = out_w = out_h = 0;
685         in_pivot_x = in_pivot_y = 0;
686         out_pivot_x = out_pivot_y = 0;
687         interpolation = AF_DEFAULT;
688         this->total_packages = total_packages;
689 }
690
691 void AffineEngine::init_packages()
692 {
693         int y1 = 0, npkgs = get_total_packages();
694         for( int i=0; i<npkgs; ) {
695                 AffinePackage *package = (AffinePackage*)get_package(i);
696                 int y2 = out_y + (out_h * ++i / npkgs);
697                 package->y1 = y1;  package->y2 = y2;  y1 = y2;
698         }
699 }
700
701 LoadClient* AffineEngine::new_client()
702 {
703         return new AffineUnit(this);
704 }
705
706 LoadPackage* AffineEngine::new_package()
707 {
708         return new AffinePackage;
709 }
710
711 void AffineEngine::process(VFrame *output, VFrame *input, VFrame *temp, int mode,
712         float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4,
713         int forward)
714 {
715
716
717 // printf("AffineEngine::process %d %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
718 // __LINE__, x1, y1, x2, y2, x3, y3, x4, y4);
719 //
720 // printf("AffineEngine::process %d %d %d %d %d\n",
721 // __LINE__, in_x, in_y, in_w, in_h);
722 //
723 // printf("AffineEngine::process %d %d %d %d %d\n",
724 // __LINE__, out_x, out_y, out_w, out_h);
725 //
726 // printf("AffineEngine::process %d %d %d %d %d\n",
727 // __LINE__, in_pivot_x, in_pivot_y, out_pivot_x, out_pivot_y);
728 //
729 // printf("AffineEngine::process %d %d %d %d %d\n",
730 // __LINE__, user_in_pivot, user_out_pivot, user_in_viewport, user_out_viewport);
731
732         this->output = output;
733         this->input = input;
734         this->temp = temp;
735         this->mode = mode;
736         this->x1 = x1;  this->y1 = y1;
737         this->x2 = x2;  this->y2 = y2;
738         this->x3 = x3;  this->y3 = y3;
739         this->x4 = x4;  this->y4 = y4;
740         this->forward = forward;
741
742         if(!user_in_viewport) {
743                 in_x = 0;  in_y = 0;
744                 in_w = input->get_w();
745                 in_h = input->get_h();
746         }
747
748         if(!user_out_viewport) {
749                 out_x = 0;  out_y = 0;
750                 out_w = output->get_w();
751                 out_h = output->get_h();
752         }
753
754         if(use_opengl) {
755                 set_package_count(1);
756                 process_single();
757         }
758         else {
759                 set_package_count(total_packages);
760                 process_packages();
761         }
762 }
763
764 void AffineEngine::set_matrix(
765         double in_x1, double in_y1, double in_x2, double in_y2,
766         double out_x1, double out_y1, double out_x2, double out_y2,
767         double out_x3, double out_y3, double out_x4, double out_y4)
768 {
769         matrix.set_matrix(in_x1, in_y1, in_x2, in_y2,
770                 out_x1, out_y1, out_x2, out_y2, out_x3, out_y3, out_x4, out_y4);
771 }
772
773
774 void AffineEngine::rotate(VFrame *output,
775         VFrame *input,
776         float angle)
777 {
778         this->output = output;
779         this->input = input;
780         this->temp = 0;
781         this->mode = ROTATE;
782         this->forward = 1;
783
784         if( !user_in_viewport ) {
785                 in_x = 0;  in_y = 0;
786                 in_w = input->get_w();
787                 in_h = input->get_h();
788 // DEBUG
789 // in_x = 4;
790 // in_w = 2992;
791 // in_y = 4;
792 // in_h = 2992;
793 // printf("AffineEngine::rotate %d %d %d %d %d\n", __LINE__, in_x, in_w, in_y, in_h);
794         }
795
796         if( !user_in_pivot ) {
797                 in_pivot_x = in_x + in_w / 2;
798                 in_pivot_y = in_y + in_h / 2;
799         }
800
801         if( !user_out_viewport ) {
802                 out_x = 0;  out_y = 0;
803                 out_w = output->get_w();
804                 out_h = output->get_h();
805         }
806
807         if( !user_out_pivot ) {
808                 out_pivot_x = out_x + out_w / 2;
809                 out_pivot_y = out_y + out_h / 2;
810         }
811
812 // All subscripts are clockwise around the quadrangle
813         angle = angle * 2 * M_PI / 360;
814         double angle1 = atan2((double)(in_pivot_y - in_y), (double)(in_pivot_x - in_x)) + angle;
815         double angle2 = atan2((double)(in_x + in_w - in_pivot_x), (double)(in_pivot_y - in_y)) + angle;
816         double angle3 = atan2((double)(in_y + in_h - in_pivot_y), (double)(in_x + in_w - in_pivot_x)) + angle;
817         double angle4 = atan2((double)(in_pivot_x - in_x), (double)(in_y + in_h - in_pivot_y)) + angle;
818         double radius1 = DISTANCE(in_x, in_y, in_pivot_x, in_pivot_y);
819         double radius2 = DISTANCE(in_x + in_w, in_y, in_pivot_x, in_pivot_y);
820         double radius3 = DISTANCE(in_x + in_w, in_y + in_h, in_pivot_x, in_pivot_y);
821         double radius4 = DISTANCE(in_x, in_y + in_h, in_pivot_x, in_pivot_y);
822
823         x1 = ((in_pivot_x - in_x) - cos(angle1) * radius1) * 100 / in_w;
824         y1 = ((in_pivot_y - in_y) - sin(angle1) * radius1) * 100 / in_h;
825         x2 = ((in_pivot_x - in_x) + sin(angle2) * radius2) * 100 / in_w;
826         y2 = ((in_pivot_y - in_y) - cos(angle2) * radius2) * 100 / in_h;
827         x3 = ((in_pivot_x - in_x) + cos(angle3) * radius3) * 100 / in_w;
828         y3 = ((in_pivot_y - in_y) + sin(angle3) * radius3) * 100 / in_h;
829         x4 = ((in_pivot_x - in_x) - sin(angle4) * radius4) * 100 / in_w;
830         y4 = ((in_pivot_y - in_y) + cos(angle4) * radius4) * 100 / in_h;
831
832 // printf("AffineEngine::rotate angle=%f\n",
833 // angle);
834
835 //
836 // printf("     angle1=%f angle2=%f angle3=%f angle4=%f\n",
837 // angle1 * 360 / 2 / M_PI,  angle2 * 360 / 2 / M_PI,
838 // angle3 * 360 / 2 / M_PI,  angle4 * 360 / 2 / M_PI);
839 //
840 // printf("     radius1=%f radius2=%f radius3=%f radius4=%f\n",
841 // radius1, radius2, radius3, radius4);
842 //
843 // printf("     x1=%f y1=%f x2=%f y2=%f x3=%f y3=%f x4=%f y4=%f\n",
844 // x1 * w / 100, y1 * h / 100,
845 // x2 * w / 100, y2 * h / 100,
846 // x3 * w / 100, y3 * h / 100,
847 // x4 * w / 100, y4 * h / 100);
848
849         if(use_opengl) {
850                 set_package_count(1);
851                 process_single();
852         }
853         else {
854                 set_package_count(total_packages);
855                 process_packages();
856         }
857 }
858
859 void AffineEngine::set_in_viewport(int x, int y, int w, int h)
860 {
861         this->in_x = x;  this->in_y = y;
862         this->in_w = w;  this->in_h = h;
863         this->user_in_viewport = 1;
864 }
865
866 void AffineEngine::set_out_viewport(int x, int y, int w, int h)
867 {
868         this->out_x = x;  this->out_y = y;
869         this->out_w = w;  this->out_h = h;
870         this->user_out_viewport = 1;
871 }
872
873 void AffineEngine::set_viewport(int x, int y, int w, int h)
874 {
875         set_in_viewport(x, y, w, h);
876         set_out_viewport(x, y, w, h);
877 }
878
879 void AffineEngine::set_opengl(int value)
880 {
881         this->use_opengl = value;
882 }
883
884 void AffineEngine::set_in_pivot(int x, int y)
885 {
886         this->in_pivot_x = x;
887         this->in_pivot_y = y;
888         this->user_in_pivot = 1;
889 }
890
891 void AffineEngine::set_out_pivot(int x, int y)
892 {
893         this->out_pivot_x = x;
894         this->out_pivot_y = y;
895         this->user_out_pivot = 1;
896 }
897
898 void AffineEngine::set_pivot(int x, int y)
899 {
900         set_in_pivot(x, y);
901         set_out_pivot(x, y);
902 }
903
904 void AffineEngine::unset_pivot()
905 {
906         user_in_pivot = 0;
907         user_out_pivot = 0;
908 }
909
910 void AffineEngine::unset_viewport()
911 {
912         user_in_viewport = 0;
913         user_out_viewport = 0;
914 }
915
916
917 void AffineEngine::set_interpolation(int type)
918 {
919         interpolation = type;
920 }