4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #define GL_GLEXT_PROTOTYPES
37 AffineMatrix::AffineMatrix()
39 bzero(values, sizeof(values));
42 void AffineMatrix::identity()
44 bzero(values, sizeof(values));
50 void AffineMatrix::translate(double x, double y)
52 double g = values[2][0];
53 double h = values[2][1];
54 double i = values[2][2];
55 values[0][0] += x * g;
56 values[0][1] += x * h;
57 values[0][2] += x * i;
58 values[1][0] += y * g;
59 values[1][1] += y * h;
60 values[1][2] += y * i;
63 void AffineMatrix::scale(double x, double y)
74 void AffineMatrix::multiply(AffineMatrix *dst)
80 for (i = 0; i < 3; i++)
85 for (j = 0; j < 3; j++)
87 tmp.values[i][j] = t1 * dst->values[0][j];
88 tmp.values[i][j] += t2 * dst->values[1][j];
89 tmp.values[i][j] += t3 * dst->values[2][j];
95 double AffineMatrix::determinant()
100 values[0][0] * (values[1][1] * values[2][2] - values[1][2] * values[2][1]);
102 values[1][0] * (values[0][1] * values[2][2] - values[0][2] * values[2][1]);
104 values[2][0] * (values[0][1] * values[1][2] - values[0][2] * values[1][1]);
109 void AffineMatrix::invert(AffineMatrix *dst)
113 det_1 = determinant();
121 (values[1][1] * values[2][2] - values[1][2] * values[2][1]) * det_1;
124 - (values[1][0] * values[2][2] - values[1][2] * values[2][0]) * det_1;
127 (values[1][0] * values[2][1] - values[1][1] * values[2][0]) * det_1;
130 - (values[0][1] * values[2][2] - values[0][2] * values[2][1] ) * det_1;
133 (values[0][0] * values[2][2] - values[0][2] * values[2][0]) * det_1;
136 - (values[0][0] * values[2][1] - values[0][1] * values[2][0]) * det_1;
139 (values[0][1] * values[1][2] - values[0][2] * values[1][1]) * det_1;
142 - (values[0][0] * values[1][2] - values[0][2] * values[1][0]) * det_1;
145 (values[0][0] * values[1][1] - values[0][1] * values[1][0]) * det_1;
148 void AffineMatrix::copy_from(AffineMatrix *src)
150 memcpy(&values[0][0], &src->values[0][0], sizeof(values));
153 void AffineMatrix::transform_point(float x,
160 w = values[2][0] * x + values[2][1] * y + values[2][2];
167 *newx = (values[0][0] * x + values[0][1] * y + values[0][2]) * w;
168 *newy = (values[1][0] * x + values[1][1] * y + values[1][2]) * w;
171 void AffineMatrix::dump()
173 printf("AffineMatrix::dump\n");
174 printf("%f %f %f\n", values[0][0], values[0][1], values[0][2]);
175 printf("%f %f %f\n", values[1][0], values[1][1], values[1][2]);
176 printf("%f %f %f\n", values[2][0], values[2][1], values[2][2]);
183 AffinePackage::AffinePackage()
191 AffineUnit::AffineUnit(AffineEngine *server)
194 this->server = server;
205 void AffineUnit::calculate_matrix(
218 AffineMatrix *result)
224 scalex = scaley = 1.0;
226 if((in_x2 - in_x1) > 0)
227 scalex = 1.0 / (double)(in_x2 - in_x1);
229 if((in_y2 - in_y1) > 0)
230 scaley = 1.0 / (double)(in_y2 - in_y1);
232 /* Determine the perspective transform that maps from
233 * the unit cube to the transformed coordinates
235 double dx1, dx2, dx3, dy1, dy2, dy3;
238 dx1 = out_x2 - out_x4;
239 dx2 = out_x3 - out_x4;
240 dx3 = out_x1 - out_x2 + out_x4 - out_x3;
242 dy1 = out_y2 - out_y4;
243 dy2 = out_y3 - out_y4;
244 dy3 = out_y1 - out_y2 + out_y4 - out_y3;
245 // printf("AffineUnit::calculate_matrix %f %f %f %f %f %f\n",
254 /* Is the mapping affine? */
255 if((dx3 == 0.0) && (dy3 == 0.0))
257 matrix.values[0][0] = out_x2 - out_x1;
258 matrix.values[0][1] = out_x4 - out_x2;
259 matrix.values[0][2] = out_x1;
260 matrix.values[1][0] = out_y2 - out_y1;
261 matrix.values[1][1] = out_y4 - out_y2;
262 matrix.values[1][2] = out_y1;
263 matrix.values[2][0] = 0.0;
264 matrix.values[2][1] = 0.0;
268 det1 = dx3 * dy2 - dy3 * dx2;
269 det2 = dx1 * dy2 - dy1 * dx2;
270 matrix.values[2][0] = det1 / det2;
271 det1 = dx1 * dy3 - dy1 * dx3;
272 det2 = dx1 * dy2 - dy1 * dx2;
273 matrix.values[2][1] = det1 / det2;
275 matrix.values[0][0] = out_x2 - out_x1 + matrix.values[2][0] * out_x2;
276 matrix.values[0][1] = out_x3 - out_x1 + matrix.values[2][1] * out_x3;
277 matrix.values[0][2] = out_x1;
279 matrix.values[1][0] = out_y2 - out_y1 + matrix.values[2][0] * out_y2;
280 matrix.values[1][1] = out_y3 - out_y1 + matrix.values[2][1] * out_y3;
281 matrix.values[1][2] = out_y1;
284 matrix.values[2][2] = 1.0;
286 // printf("AffineUnit::calculate_matrix 1 %f %f\n", dx3, dy3);
290 result->translate(-in_x1, -in_y1);
291 result->scale(scalex, scaley);
292 matrix.multiply(result);
293 // double test[3][3] = { { 0.0896, 0.0, 0.0 },
294 // { 0.0, 0.0896, 0.0 },
295 // { -0.00126, 0.0, 1.0 } };
296 // memcpy(&result->values[0][0], test, sizeof(test));
297 // printf("AffineUnit::calculate_matrix 4 %p\n", result);
303 float AffineUnit::transform_cubic(float dx,
309 /* Catmull-Rom - not bad */
310 float result = ((( ( - jm1 + 3.0 * j - 3.0 * jp1 + jp2 ) * dx +
311 ( 2.0 * jm1 - 5.0 * j + 4.0 * jp1 - jp2 ) ) * dx +
312 ( - jm1 + jp1 ) ) * dx + (j + j) ) / 2.0;
313 // printf("%f %f %f %f %f\n",
325 void AffineUnit::process_package(LoadPackage *package)
327 AffinePackage *pkg = (AffinePackage*)package;
328 int min_in_x = server->in_x;
329 int min_in_y = server->in_y;
330 int max_in_x = server->in_x + server->in_w - 1;
331 int max_in_y = server->in_y + server->in_h - 1;
334 // printf("AffineUnit::process_package %d %d %d %d %d\n",
340 int min_out_x = server->out_x;
341 //int min_out_y = server->out_y;
342 int max_out_x = server->out_x + server->out_w;
343 //int max_out_y = server->out_y + server->out_h;
345 // Amount to shift the input coordinates relative to the output coordinates
346 // To get the pivots to line up
347 int pivot_offset_x = server->in_pivot_x - server->out_pivot_x;
348 int pivot_offset_y = server->in_pivot_y - server->out_pivot_y;
350 // Calculate real coords
351 float out_x1, out_y1, out_x2, out_y2, out_x3, out_y3, out_x4, out_y4;
352 if(server->mode == AffineEngine::STRETCH ||
353 server->mode == AffineEngine::PERSPECTIVE ||
354 server->mode == AffineEngine::ROTATE ||
355 server->mode == AffineEngine::TRANSFORM)
357 out_x1 = (float)server->in_x + (float)server->x1 * server->in_w / 100;
358 out_y1 = (float)server->in_y + (float)server->y1 * server->in_h / 100;
359 out_x2 = (float)server->in_x + (float)server->x2 * server->in_w / 100;
360 out_y2 = (float)server->in_y + (float)server->y2 * server->in_h / 100;
361 out_x3 = (float)server->in_x + (float)server->x3 * server->in_w / 100;
362 out_y3 = (float)server->in_y + (float)server->y3 * server->in_h / 100;
363 out_x4 = (float)server->in_x + (float)server->x4 * server->in_w / 100;
364 out_y4 = (float)server->in_y + (float)server->y4 * server->in_h / 100;
368 out_x1 = (float)server->in_x + (float)server->x1 * server->in_w / 100;
369 out_y1 = server->in_y;
370 out_x2 = out_x1 + server->in_w;
371 out_y2 = server->in_y;
372 out_x4 = (float)server->in_x + (float)server->x4 * server->in_w / 100;
373 out_y4 = server->in_y + server->in_h;
374 out_x3 = out_x4 + server->in_w;
375 out_y3 = server->in_y + server->in_h;
380 // Rotation with OpenGL uses a simple quad.
381 if(server->mode == AffineEngine::ROTATE &&
385 server->output->to_texture();
386 server->output->enable_opengl();
387 server->output->init_screen();
388 server->output->bind_texture(0);
389 server->output->clear_pbuffer();
391 int texture_w = server->output->get_texture_w();
392 int texture_h = server->output->get_texture_h();
393 float output_h = server->output->get_h();
394 float in_x1 = (float)server->in_x / texture_w;
395 float in_x2 = (float)(server->in_x + server->in_w) / texture_w;
396 float in_y1 = (float)server->in_y / texture_h;
397 float in_y2 = (float)(server->in_y + server->in_h) / texture_h;
399 // printf("%f %f %f %f\n%f,%f %f,%f %f,%f %f,%f\n", in_x1, in_y1, in_x2, in_y2,
400 // out_x1, out_y1, out_x2, out_y2, out_x3, out_y3, out_x4, out_y4);
403 glNormal3f(0, 0, 1.0);
405 glTexCoord2f(in_x1, in_y1);
406 glVertex3f(out_x1, -output_h+out_y1, 0);
408 glTexCoord2f(in_x2, in_y1);
409 glVertex3f(out_x2, -output_h+out_y2, 0);
411 glTexCoord2f(in_x2, in_y2);
412 glVertex3f(out_x3, -output_h+out_y3, 0);
414 glTexCoord2f(in_x1, in_y2);
415 glVertex3f(out_x4, -output_h+out_y4, 0);
420 server->output->set_opengl_state(VFrame::SCREEN);
424 if(server->mode == AffineEngine::PERSPECTIVE ||
425 server->mode == AffineEngine::SHEER ||
426 server->mode == AffineEngine::ROTATE ||
427 server->mode == AffineEngine::TRANSFORM)
443 if(server->mode != AffineEngine::TRANSFORM)
448 server->in_x + server->in_w,
449 server->in_y + server->in_h,
462 matrix.copy_from(&server->matrix);
465 // printf("AffineUnit::process_package %d\n%f %f %f\n%f %f %f\n%f %f %f\n",
467 // matrix.values[0][0],
468 // matrix.values[0][1],
469 // matrix.values[0][2],
470 // matrix.values[1][0],
471 // matrix.values[1][1],
472 // matrix.values[1][2],
473 // matrix.values[2][0],
474 // matrix.values[2][1],
475 // matrix.values[2][2]);
477 int reverse = !server->forward;
479 float xinc, yinc, winc;
481 float ttx = 0, tty = 0;
482 int itx = 0, ity = 0;
483 int tx1 = 0, ty1 = 0, tx2 = 0, ty2 = 0;
487 m.copy_from(&matrix);
489 matrix.copy_from(&im);
501 float dx1 = 0, dy1 = 0;
502 float dx2 = 0, dy2 = 0;
503 float dx3 = 0, dy3 = 0;
504 float dx4 = 0, dy4 = 0;
505 matrix.transform_point(server->in_x, server->in_y, &dx1, &dy1);
506 matrix.transform_point(server->in_x + server->in_w, server->in_y, &dx2, &dy2);
507 matrix.transform_point(server->in_x, server->in_y + server->in_h, &dx3, &dy3);
508 matrix.transform_point(server->in_x + server->in_w, server->in_y + server->in_h, &dx4, &dy4);
510 //printf("AffineUnit::process_package 1 y1=%d y2=%d\n", pkg->y1, pkg->y2);
511 //printf("AffineUnit::process_package 1 %f %f %f %f\n", dy1, dy2, dy3, dy4);
512 // printf("AffineUnit::process_package %d use_opengl=%d\n",
513 // __LINE__, server->use_opengl);
519 if(server->use_opengl)
522 static const char *affine_frag =
523 "uniform sampler2D tex;\n"
524 "uniform mat3 affine_matrix;\n"
525 "uniform vec2 texture_extents;\n"
526 "uniform vec2 image_extents;\n"
527 "uniform vec4 border_color;\n"
530 " vec2 outcoord = gl_TexCoord[0].st;\n"
531 " outcoord *= texture_extents;\n"
532 " mat3 coord_matrix = mat3(\n"
533 " outcoord.x, outcoord.y, 1.0, \n"
534 " outcoord.x, outcoord.y, 1.0, \n"
535 " outcoord.x, outcoord.y, 1.0);\n"
536 " mat3 incoord_matrix = affine_matrix * coord_matrix;\n"
537 " vec2 incoord = vec2(incoord_matrix[0][0], incoord_matrix[0][1]);\n"
538 " incoord /= incoord_matrix[0][2];\n"
539 " incoord /= texture_extents;\n"
540 " if(incoord.x > image_extents.x || incoord.y > image_extents.y)\n"
541 " gl_FragColor = border_color;\n"
543 " gl_FragColor = texture2D(tex, incoord);\n"
546 float affine_matrix[9] = {
547 (float)m.values[0][0], (float)m.values[1][0], (float)m.values[2][0],
548 (float)m.values[0][1], (float)m.values[1][1], (float)m.values[2][1],
549 (float)m.values[0][2], (float)m.values[1][2], (float)m.values[2][2]
553 server->output->to_texture();
554 server->output->enable_opengl();
555 unsigned int frag_shader = VFrame::make_shader(0,
560 glUseProgram(frag_shader);
561 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
562 glUniformMatrix3fv(glGetUniformLocation(frag_shader, "affine_matrix"),
566 glUniform2f(glGetUniformLocation(frag_shader, "texture_extents"),
567 (GLfloat)server->output->get_texture_w(),
568 (GLfloat)server->output->get_texture_h());
569 glUniform2f(glGetUniformLocation(frag_shader, "image_extents"),
570 (GLfloat)server->output->get_w() / server->output->get_texture_w(),
571 (GLfloat)server->output->get_h() / server->output->get_texture_h());
572 float border_color[] = { 0, 0, 0, 0 };
573 if(BC_CModels::is_yuv(server->output->get_color_model()))
575 border_color[1] = 0.5;
576 border_color[2] = 0.5;
578 if(!BC_CModels::has_alpha(server->output->get_color_model()))
580 border_color[3] = 1.0;
583 glUniform4fv(glGetUniformLocation(frag_shader, "border_color"),
585 (GLfloat*)border_color);
586 server->output->init_screen();
587 server->output->bind_texture(0);
588 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color);
589 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
590 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
591 server->output->draw_texture();
593 server->output->set_opengl_state(VFrame::SCREEN);
604 #define ROUND(x) ((int)((x > 0) ? (x) + 0.5 : (x) - 0.5))
605 #define MIN4(a,b,c,d) MIN(MIN(MIN(a,b),c),d)
606 #define MAX4(a,b,c,d) MAX(MAX(MAX(a,b),c),d)
608 tx1 = ROUND(MIN4(dx1 - pivot_offset_x, dx2 - pivot_offset_x, dx3 - pivot_offset_x, dx4 - pivot_offset_x));
609 ty1 = ROUND(MIN4(dy1 - pivot_offset_y, dy2 - pivot_offset_y, dy3 - pivot_offset_y, dy4 - pivot_offset_y));
611 tx2 = ROUND(MAX4(dx1 - pivot_offset_x, dx2 - pivot_offset_x, dx3 - pivot_offset_x, dx4 - pivot_offset_x));
612 ty2 = ROUND(MAX4(dy1 - pivot_offset_y, dy2 - pivot_offset_y, dy3 - pivot_offset_y, dy4 - pivot_offset_y));
614 CLAMP(ty1, pkg->y1, pkg->y2);
615 CLAMP(ty2, pkg->y1, pkg->y2);
616 CLAMP(tx1, server->out_x, server->out_x + server->out_w);
617 CLAMP(tx2, server->out_x, server->out_x + server->out_w);
620 xinc = m.values[0][0];
621 yinc = m.values[1][0];
622 winc = m.values[2][0];
624 //printf("AffineUnit::process_package 2 tx1=%d ty1=%d tx2=%d ty2=%d %f %f\n", tx1, ty1, tx2, ty2, out_x4, out_y4);
625 //printf("AffineUnit::process_package %d %d %d %d %d\n",
632 #define CUBIC_ROW(in_row, chroma_offset) \
633 transform_cubic(dx, \
634 in_row[col1_offset] - chroma_offset, \
635 in_row[col2_offset] - chroma_offset, \
636 in_row[col3_offset] - chroma_offset, \
637 in_row[col4_offset] - chroma_offset)
640 #define TRANSFORM(components, type, temp_type, chroma_offset, max) \
642 type **in_rows = (type**)server->input->get_rows(); \
643 float round_factor = 0.0; \
644 if(sizeof(type) < 4) round_factor = 0.5; \
645 for(int y = ty1; y < ty2; y++) \
647 type *out_row = (type*)server->output->get_rows()[y]; \
651 tx = xinc * (tx1 + 0.5) + \
652 m.values[0][1] * (y + pivot_offset_y + 0.5) + \
654 pivot_offset_x * xinc; \
655 ty = yinc * (tx1 + 0.5) + \
656 m.values[1][1] * (y + pivot_offset_y + 0.5) + \
658 pivot_offset_x * yinc; \
659 tw = winc * (tx1 + 0.5) + \
660 m.values[2][1] * (y + pivot_offset_y + 0.5) + \
662 pivot_offset_x * winc; \
667 m.values[0][1] * (y + pivot_offset_y) + \
669 pivot_offset_x * xinc; \
671 m.values[1][1] * (y + pivot_offset_y) + \
673 pivot_offset_x * yinc; \
675 m.values[2][1] * (y + pivot_offset_y) + \
677 pivot_offset_x * winc; \
681 out_row += tx1 * components; \
682 for(int x = tx1; x < tx2; x++) \
684 /* Normalize homogeneous coords */ \
704 int row1 = ity - 1; \
706 int row3 = ity + 1; \
707 int row4 = ity + 2; \
708 CLAMP(row1, min_in_y, max_in_y); \
709 CLAMP(row2, min_in_y, max_in_y); \
710 CLAMP(row3, min_in_y, max_in_y); \
711 CLAMP(row4, min_in_y, max_in_y); \
713 /* Set destination pixels if in clipping region */ \
718 if(itx >= min_in_x && \
723 type *src = in_rows[ity] + itx * components; \
724 *out_row++ = *src++; \
725 *out_row++ = *src++; \
726 *out_row++ = *src++; \
727 if(components == 4) *out_row++ = *src; \
730 /* Fill with chroma */ \
733 *out_row++ = chroma_offset; \
734 *out_row++ = chroma_offset; \
735 if(components == 4) *out_row++ = 0; \
739 /* Bicubic algorithm */ \
744 /* clipping region */ \
745 if ((itx + 2) >= min_in_x && \
746 (itx - 1) <= max_in_x && \
747 (ity + 2) >= min_in_y && \
748 (ity - 1) <= max_in_y) \
752 /* the fractional error */ \
756 /* Row and column offsets in cubic block */ \
757 int col1 = itx - 1; \
759 int col3 = itx + 1; \
760 int col4 = itx + 2; \
761 CLAMP(col1, min_in_x, max_in_x); \
762 CLAMP(col2, min_in_x, max_in_x); \
763 CLAMP(col3, min_in_x, max_in_x); \
764 CLAMP(col4, min_in_x, max_in_x); \
765 int col1_offset = col1 * components; \
766 int col2_offset = col2 * components; \
767 int col3_offset = col3 * components; \
768 int col4_offset = col4 * components; \
770 type *row1_ptr = in_rows[row1]; \
771 type *row2_ptr = in_rows[row2]; \
772 type *row3_ptr = in_rows[row3]; \
773 type *row4_ptr = in_rows[row4]; \
774 temp_type r, g, b, a; \
776 r = (temp_type)(transform_cubic(dy, \
777 CUBIC_ROW(row1_ptr, 0x0), \
778 CUBIC_ROW(row2_ptr, 0x0), \
779 CUBIC_ROW(row3_ptr, 0x0), \
780 CUBIC_ROW(row4_ptr, 0x0)) + \
787 g = (temp_type)(transform_cubic(dy, \
788 CUBIC_ROW(row1_ptr, chroma_offset), \
789 CUBIC_ROW(row2_ptr, chroma_offset), \
790 CUBIC_ROW(row3_ptr, chroma_offset), \
791 CUBIC_ROW(row4_ptr, chroma_offset)) + \
793 g += chroma_offset; \
799 b = (temp_type)(transform_cubic(dy, \
800 CUBIC_ROW(row1_ptr, chroma_offset), \
801 CUBIC_ROW(row2_ptr, chroma_offset), \
802 CUBIC_ROW(row3_ptr, chroma_offset), \
803 CUBIC_ROW(row4_ptr, chroma_offset)) + \
805 b += chroma_offset; \
807 if(components == 4) \
813 a = (temp_type)(transform_cubic(dy, \
814 CUBIC_ROW(row1_ptr, 0x0), \
815 CUBIC_ROW(row2_ptr, 0x0), \
816 CUBIC_ROW(row3_ptr, 0x0), \
817 CUBIC_ROW(row4_ptr, 0x0)) + \
821 if(sizeof(type) < 4) \
823 *out_row++ = CLIP(r, 0, max); \
824 *out_row++ = CLIP(g, 0, max); \
825 *out_row++ = CLIP(b, 0, max); \
826 if(components == 4) *out_row++ = CLIP(a, 0, max); \
833 if(components == 4) *out_row++ = a; \
837 /* Fill with chroma */ \
840 *out_row++ = chroma_offset; \
841 *out_row++ = chroma_offset; \
842 if(components == 4) *out_row++ = 0; \
847 out_row += components; \
850 /* increment the transformed coordinates */ \
861 // printf("AffineUnit::process_package %d tx1=%d ty1=%d tx2=%d ty2=%d\n",
862 // __LINE__, tx1, ty1, tx2, ty2);
863 switch(server->input->get_color_model())
866 TRANSFORM(3, float, float, 0x0, 1.0)
869 TRANSFORM(3, unsigned char, int, 0x0, 0xff)
872 TRANSFORM(4, float, float, 0x0, 1.0)
875 TRANSFORM(4, unsigned char, int, 0x0, 0xff)
879 // TRANSFORM(3, unsigned char, int, 0x80, 0xff)
882 unsigned char **in_rows = (unsigned char**)server->input->get_rows();
883 float round_factor = 0.0;
884 if(sizeof(unsigned char) < 4) round_factor = 0.5;
885 for(int y = ty1; y < ty2; y++)
887 unsigned char *out_row = (unsigned char*)server->output->get_rows()[y];
891 tx = xinc * (tx1 + 0.5) +
892 m.values[0][1] * (y + pivot_offset_y + 0.5) +
894 pivot_offset_x * xinc;
895 ty = yinc * (tx1 + 0.5) +
896 m.values[1][1] * (y + pivot_offset_y + 0.5) +
898 pivot_offset_x * yinc;
899 tw = winc * (tx1 + 0.5) +
900 m.values[2][1] * (y + pivot_offset_y + 0.5) +
902 pivot_offset_x * winc;
907 m.values[0][1] * (y + pivot_offset_y) +
909 pivot_offset_x * xinc;
911 m.values[1][1] * (y + pivot_offset_y) +
913 pivot_offset_x * yinc;
915 m.values[2][1] * (y + pivot_offset_y) +
917 pivot_offset_x * winc;
922 for(int x = tx1; x < tx2; x++)
924 /* Normalize homogeneous coords */
948 CLAMP(row1, min_in_y, max_in_y);
949 CLAMP(row2, min_in_y, max_in_y);
950 CLAMP(row3, min_in_y, max_in_y);
951 CLAMP(row4, min_in_y, max_in_y);
953 /* Set destination pixels if in clipping region */
958 if(itx >= min_in_x &&
963 unsigned char *src = in_rows[ity] + itx * 3;
967 if(3 == 4) *out_row++ = *src;
970 /* Fill with chroma */
975 if(3 == 4) *out_row++ = 0;
979 /* Bicubic algorithm */
984 /* clipping region */
985 if ((itx + 2) >= min_in_x &&
986 (itx - 1) <= max_in_x &&
987 (ity + 2) >= min_in_y &&
988 (ity - 1) <= max_in_y)
992 /* the fractional error */
996 /* Row and column offsets in cubic block */
1001 CLAMP(col1, min_in_x, max_in_x);
1002 CLAMP(col2, min_in_x, max_in_x);
1003 CLAMP(col3, min_in_x, max_in_x);
1004 CLAMP(col4, min_in_x, max_in_x);
1005 int col1_offset = col1 * 3;
1006 int col2_offset = col2 * 3;
1007 int col3_offset = col3 * 3;
1008 int col4_offset = col4 * 3;
1010 unsigned char *row1_ptr = in_rows[row1];
1011 unsigned char *row2_ptr = in_rows[row2];
1012 unsigned char *row3_ptr = in_rows[row3];
1013 unsigned char *row4_ptr = in_rows[row4];
1016 r = (int)(transform_cubic(dy,
1017 CUBIC_ROW(row1_ptr, 0x0),
1018 CUBIC_ROW(row2_ptr, 0x0),
1019 CUBIC_ROW(row3_ptr, 0x0),
1020 CUBIC_ROW(row4_ptr, 0x0)) +
1027 g = (int)(transform_cubic(dy,
1028 CUBIC_ROW(row1_ptr, 0x80),
1029 CUBIC_ROW(row2_ptr, 0x80),
1030 CUBIC_ROW(row3_ptr, 0x80),
1031 CUBIC_ROW(row4_ptr, 0x80)) +
1039 b = (int)(transform_cubic(dy,
1040 CUBIC_ROW(row1_ptr, 0x80),
1041 CUBIC_ROW(row2_ptr, 0x80),
1042 CUBIC_ROW(row3_ptr, 0x80),
1043 CUBIC_ROW(row4_ptr, 0x80)) +
1053 a = (int)(transform_cubic(dy,
1054 CUBIC_ROW(row1_ptr, 0x0),
1055 CUBIC_ROW(row2_ptr, 0x0),
1056 CUBIC_ROW(row3_ptr, 0x0),
1057 CUBIC_ROW(row4_ptr, 0x0)) +
1061 if(sizeof(unsigned char) < 4)
1063 *out_row++ = CLIP(r, 0, 0xff);
1064 *out_row++ = CLIP(g, 0, 0xff);
1065 *out_row++ = CLIP(b, 0, 0xff);
1066 if(3 == 4) *out_row++ = CLIP(a, 0, 0xff);
1073 if(3 == 4) *out_row++ = a;
1077 /* Fill with chroma */
1082 if(3 == 4) *out_row++ = 0;
1090 /* increment the transformed coordinates */
1100 TRANSFORM(4, unsigned char, int, 0x80, 0xff)
1103 TRANSFORM(3, uint16_t, int, 0x0, 0xffff)
1105 case BC_RGBA16161616:
1106 TRANSFORM(4, uint16_t, int, 0x0, 0xffff)
1109 TRANSFORM(3, uint16_t, int, 0x8000, 0xffff)
1111 case BC_YUVA16161616:
1112 TRANSFORM(4, uint16_t, int, 0x8000, 0xffff)
1119 int min_x = server->in_x * AFFINE_OVERSAMPLE;
1120 int min_y = server->in_y * AFFINE_OVERSAMPLE;
1121 int max_x = server->in_x * AFFINE_OVERSAMPLE + server->in_w * AFFINE_OVERSAMPLE - 1;
1122 int max_y = server->in_y * AFFINE_OVERSAMPLE + server->in_h * AFFINE_OVERSAMPLE - 1;
1123 float top_w = out_x2 - out_x1;
1124 float bottom_w = out_x3 - out_x4;
1125 float left_h = out_y4 - out_y1;
1126 float right_h = out_y3 - out_y2;
1127 float out_w_diff = bottom_w - top_w;
1128 float out_left_diff = out_x4 - out_x1;
1129 float out_h_diff = right_h - left_h;
1130 float out_top_diff = out_y2 - out_y1;
1131 float distance1 = DISTANCE(out_x1, out_y1, out_x2, out_y2);
1132 float distance2 = DISTANCE(out_x2, out_y2, out_x3, out_y3);
1133 float distance3 = DISTANCE(out_x3, out_y3, out_x4, out_y4);
1134 float distance4 = DISTANCE(out_x4, out_y4, out_x1, out_y1);
1135 float max_v = MAX(distance1, distance3);
1136 float max_h = MAX(distance2, distance4);
1137 float max_dimension = MAX(max_v, max_h);
1138 float min_dimension = MIN(server->in_h, server->in_w);
1139 float step = min_dimension / max_dimension / AFFINE_OVERSAMPLE;
1140 float x_f = server->in_x;
1141 float y_f = server->in_y;
1142 float h_f = server->in_h;
1143 float w_f = server->in_w;
1147 if(server->use_opengl)
1155 #define DO_STRETCH(type, components) \
1157 type **in_rows = (type**)server->input->get_rows(); \
1158 type **out_rows = (type**)server->temp->get_rows(); \
1160 for(float in_y = pkg->y1; in_y < pkg->y2; in_y += step) \
1162 int i = (int)in_y; \
1163 type *in_row = in_rows[i]; \
1164 for(float in_x = x_f; in_x < w_f; in_x += step) \
1166 int j = (int)in_x; \
1167 float in_x_fraction = (in_x - x_f) / w_f; \
1168 float in_y_fraction = (in_y - y_f) / h_f; \
1169 int out_x = (int)((out_x1 + \
1170 out_left_diff * in_y_fraction + \
1171 (top_w + out_w_diff * in_y_fraction) * in_x_fraction) * \
1172 AFFINE_OVERSAMPLE); \
1173 int out_y = (int)((out_y1 + \
1174 out_top_diff * in_x_fraction + \
1175 (left_h + out_h_diff * in_x_fraction) * in_y_fraction) * \
1176 AFFINE_OVERSAMPLE); \
1177 CLAMP(out_x, min_x, max_x); \
1178 CLAMP(out_y, min_y, max_y); \
1179 type *dst = out_rows[out_y] + out_x * components; \
1180 type *src = in_row + j * components; \
1184 if(components == 4) dst[3] = src[3]; \
1189 switch(server->input->get_color_model())
1192 DO_STRETCH(float, 3)
1195 DO_STRETCH(unsigned char, 3)
1198 DO_STRETCH(float, 4)
1201 DO_STRETCH(unsigned char, 4)
1204 DO_STRETCH(unsigned char, 3)
1207 DO_STRETCH(unsigned char, 4)
1210 DO_STRETCH(uint16_t, 3)
1212 case BC_RGBA16161616:
1213 DO_STRETCH(uint16_t, 4)
1216 DO_STRETCH(uint16_t, 3)
1218 case BC_YUVA16161616:
1219 DO_STRETCH(uint16_t, 4)
1234 AffineEngine::AffineEngine(int total_clients,
1238 total_clients, total_packages
1241 user_in_viewport = 0;
1243 user_out_viewport = 0;
1246 in_x = in_y = in_w = in_h = 0;
1247 out_x = out_y = out_w = out_h = 0;
1248 in_pivot_x = in_pivot_y = 0;
1249 out_pivot_x = out_pivot_y = 0;
1250 this->total_packages = total_packages;
1253 void AffineEngine::init_packages()
1255 for(int i = 0; i < get_total_packages(); i++)
1257 AffinePackage *package = (AffinePackage*)get_package(i);
1258 package->y1 = out_y + (out_h * i / get_total_packages());
1259 package->y2 = out_y + (out_h * (i + 1) / get_total_packages());
1263 LoadClient* AffineEngine::new_client()
1265 return new AffineUnit(this);
1268 LoadPackage* AffineEngine::new_package()
1270 return new AffinePackage;
1273 void AffineEngine::process(VFrame *output,
1289 // printf("AffineEngine::process %d %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
1300 // printf("AffineEngine::process %d %d %d %d %d\n",
1302 // in_x, in_y, in_w, in_h);
1304 // printf("AffineEngine::process %d %d %d %d %d\n",
1306 // out_x, out_y, out_w, out_h);
1308 // printf("AffineEngine::process %d %d %d %d %d\n",
1310 // in_pivot_x, in_pivot_y, out_pivot_x, out_pivot_y);
1312 // printf("AffineEngine::process %d %d %d %d %d\n",
1316 // user_in_viewport,
1317 // user_out_viewport);
1319 this->output = output;
1320 this->input = input;
1331 this->forward = forward;
1334 if(!user_in_viewport)
1338 in_w = input->get_w();
1339 in_h = input->get_h();
1342 if(!user_out_viewport)
1346 out_w = output->get_w();
1347 out_h = output->get_h();
1352 set_package_count(1);
1357 set_package_count(total_packages);
1365 void AffineEngine::rotate(VFrame *output,
1369 this->output = output;
1370 this->input = input;
1372 this->mode = ROTATE;
1375 if(!user_in_viewport)
1379 in_w = input->get_w();
1380 in_h = input->get_h();
1386 // printf("AffineEngine::rotate %d %d %d %d %d\n", __LINE__, in_x, in_w, in_y, in_h);
1391 in_pivot_x = in_x + in_w / 2;
1392 in_pivot_y = in_y + in_h / 2;
1395 if(!user_out_viewport)
1399 out_w = output->get_w();
1400 out_h = output->get_h();
1405 out_pivot_x = out_x + out_w / 2;
1406 out_pivot_y = out_y + out_h / 2;
1409 // All subscripts are clockwise around the quadrangle
1410 angle = angle * 2 * M_PI / 360;
1411 double angle1 = atan((double)(in_pivot_y - in_y) / (double)(in_pivot_x - in_x)) + angle;
1412 double angle2 = atan((double)(in_x + in_w - in_pivot_x) / (double)(in_pivot_y - in_y)) + angle;
1413 double angle3 = atan((double)(in_y + in_h - in_pivot_y) / (double)(in_x + in_w - in_pivot_x)) + angle;
1414 double angle4 = atan((double)(in_pivot_x - in_x) / (double)(in_y + in_h - in_pivot_y)) + angle;
1415 double radius1 = DISTANCE(in_x, in_y, in_pivot_x, in_pivot_y);
1416 double radius2 = DISTANCE(in_x + in_w, in_y, in_pivot_x, in_pivot_y);
1417 double radius3 = DISTANCE(in_x + in_w, in_y + in_h, in_pivot_x, in_pivot_y);
1418 double radius4 = DISTANCE(in_x, in_y + in_h, in_pivot_x, in_pivot_y);
1420 x1 = ((in_pivot_x - in_x) - cos(angle1) * radius1) * 100 / in_w;
1421 y1 = ((in_pivot_y - in_y) - sin(angle1) * radius1) * 100 / in_h;
1422 x2 = ((in_pivot_x - in_x) + sin(angle2) * radius2) * 100 / in_w;
1423 y2 = ((in_pivot_y - in_y) - cos(angle2) * radius2) * 100 / in_h;
1424 x3 = ((in_pivot_x - in_x) + cos(angle3) * radius3) * 100 / in_w;
1425 y3 = ((in_pivot_y - in_y) + sin(angle3) * radius3) * 100 / in_h;
1426 x4 = ((in_pivot_x - in_x) - sin(angle4) * radius4) * 100 / in_w;
1427 y4 = ((in_pivot_y - in_y) + cos(angle4) * radius4) * 100 / in_h;
1429 // printf("AffineEngine::rotate angle=%f\n",
1433 // printf(" angle1=%f angle2=%f angle3=%f angle4=%f\n",
1434 // angle1 * 360 / 2 / M_PI,
1435 // angle2 * 360 / 2 / M_PI,
1436 // angle3 * 360 / 2 / M_PI,
1437 // angle4 * 360 / 2 / M_PI);
1439 // printf(" radius1=%f radius2=%f radius3=%f radius4=%f\n",
1445 // printf(" x1=%f y1=%f x2=%f y2=%f x3=%f y3=%f x4=%f y4=%f\n",
1457 set_package_count(1);
1462 set_package_count(total_packages);
1467 void AffineEngine::set_matrix(AffineMatrix *matrix)
1469 for(int i = 0; i < 3; i++)
1471 for(int j = 0; j < 3; j++)
1473 this->matrix.values[i][j] = matrix->values[i][j];
1478 void AffineEngine::set_in_viewport(int x, int y, int w, int h)
1484 this->user_in_viewport = 1;
1487 void AffineEngine::set_out_viewport(int x, int y, int w, int h)
1493 this->user_out_viewport = 1;
1496 void AffineEngine::set_opengl(int value)
1498 this->use_opengl = value;
1501 void AffineEngine::set_in_pivot(int x, int y)
1503 this->in_pivot_x = x;
1504 this->in_pivot_y = y;
1505 this->user_in_pivot = 1;
1508 void AffineEngine::set_out_pivot(int x, int y)
1510 this->out_pivot_x = x;
1511 this->out_pivot_y = y;
1512 this->user_out_pivot = 1;
1515 void AffineEngine::unset_pivot()
1521 void AffineEngine::unset_viewport()
1523 user_in_viewport = 0;
1524 user_out_viewport = 0;