home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / perspective_tool.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  9.0 KB  |  321 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <glib.h>
  22.  
  23. #include "apptypes.h"
  24.  
  25. #include "appenv.h"
  26. #include "drawable.h"
  27. #include "gdisplay.h"
  28. #include "gimage_mask.h"
  29. #include "info_dialog.h"
  30. #include "perspective_tool.h"
  31. #include "selection.h"
  32. #include "transform_tool.h"
  33. #include "undo.h"
  34.  
  35. #include "libgimp/gimpintl.h"
  36.  
  37. #include "tile_manager_pvt.h"
  38.  
  39.  
  40. /*  storage for information dialog fields  */
  41. static gchar  matrix_row_buf [3][MAX_INFO_BUF];
  42.  
  43. /*  forward function declarations  */
  44. static void   perspective_tool_recalc (Tool *, void *);
  45. static void   perspective_tool_motion (Tool *, void *);
  46. static void   perspective_info_update (Tool *);
  47.  
  48. TileManager *
  49. perspective_tool_transform (Tool           *tool,
  50.                 gpointer        gdisp_ptr,
  51.                 TransformState  state)
  52. {
  53.   GDisplay      *gdisp;
  54.   TransformCore *transform_core;
  55.  
  56.   gdisp = (GDisplay *) gdisp_ptr;
  57.   transform_core = (TransformCore *) tool->private;
  58.  
  59.   switch (state)
  60.     {
  61.     case INIT:
  62.       if (!transform_info)
  63.     {
  64.       transform_info =
  65.         info_dialog_new (_("Perspective Transform Information"),
  66.                  gimp_standard_help_func,
  67.                  "tools/transform_perspective.html");
  68.       info_dialog_add_label (transform_info, _("Matrix:"),
  69.                  matrix_row_buf[0]);
  70.       info_dialog_add_label (transform_info, "", matrix_row_buf[1]);
  71.       info_dialog_add_label (transform_info, "", matrix_row_buf[2]);
  72.     }
  73.       gtk_widget_set_sensitive (GTK_WIDGET (transform_info->shell), TRUE);
  74.  
  75.       transform_core->trans_info [X0] = (double) transform_core->x1;
  76.       transform_core->trans_info [Y0] = (double) transform_core->y1;
  77.       transform_core->trans_info [X1] = (double) transform_core->x2;
  78.       transform_core->trans_info [Y1] = (double) transform_core->y1;
  79.       transform_core->trans_info [X2] = (double) transform_core->x1;
  80.       transform_core->trans_info [Y2] = (double) transform_core->y2;
  81.       transform_core->trans_info [X3] = (double) transform_core->x2;
  82.       transform_core->trans_info [Y3] = (double) transform_core->y2;
  83.  
  84.       return NULL;
  85.       break;
  86.  
  87.     case MOTION:
  88.       perspective_tool_motion (tool, gdisp_ptr);
  89.       perspective_tool_recalc (tool, gdisp_ptr);
  90.       break;
  91.  
  92.     case RECALC:
  93.       perspective_tool_recalc (tool, gdisp_ptr);
  94.       break;
  95.  
  96.     case FINISH:
  97.       /*  Let the transform core handle the inverse mapping  */
  98.       gtk_widget_set_sensitive (GTK_WIDGET (transform_info->shell), FALSE);
  99.       return
  100.     perspective_tool_perspective (gdisp->gimage,
  101.                       gimage_active_drawable (gdisp->gimage),
  102.                       gdisp,
  103.                       transform_core->original,
  104.                       transform_tool_smoothing (),
  105.                       transform_core->transform);
  106.       break;
  107.     }
  108.  
  109.   return NULL;
  110. }
  111.  
  112. Tool *
  113. tools_new_perspective_tool (void)
  114. {
  115.   Tool          *tool;
  116.   TransformCore *private;
  117.  
  118.   tool = transform_core_new (PERSPECTIVE, TRUE);
  119.  
  120.   private = tool->private;
  121.  
  122.   /*  set the rotation specific transformation attributes  */
  123.   private->trans_func = perspective_tool_transform;
  124.   private->trans_info[X0] = 0;
  125.   private->trans_info[Y0] = 0;
  126.   private->trans_info[X1] = 0;
  127.   private->trans_info[Y1] = 0;
  128.   private->trans_info[X2] = 0;
  129.   private->trans_info[Y2] = 0;
  130.   private->trans_info[X3] = 0;
  131.   private->trans_info[Y3] = 0;
  132.  
  133.   /*  assemble the transformation matrix  */
  134.   gimp_matrix3_identity (private->transform);
  135.  
  136.   return tool;
  137. }
  138.  
  139. void
  140. tools_free_perspective_tool (Tool *tool)
  141. {
  142.   transform_core_free (tool);
  143. }
  144.  
  145. static void
  146. perspective_info_update (Tool *tool)
  147. {
  148.   TransformCore *transform_core;
  149.   gint           i;
  150.   
  151.   transform_core = (TransformCore *) tool->private;
  152.  
  153.   for (i = 0; i < 3; i++)
  154.     {
  155.       gchar *p = matrix_row_buf[i];
  156.       gint   j;
  157.       
  158.       for (j = 0; j < 3; j++)
  159.     {
  160.       p += g_snprintf (p, MAX_INFO_BUF - (p - matrix_row_buf[i]),
  161.                "%10.3g", transform_core->transform[i][j]);
  162.     }
  163.     }
  164.  
  165.   info_dialog_update (transform_info);
  166.   info_dialog_popup (transform_info);
  167.  
  168.   return;
  169. }
  170.  
  171. static void
  172. perspective_tool_motion (Tool *tool,
  173.              void *gdisp_ptr)
  174. {
  175.   GDisplay      *gdisp;
  176.   TransformCore *transform_core;
  177.   gint            diff_x, diff_y;
  178.  
  179.   gdisp = (GDisplay *) gdisp_ptr;
  180.   transform_core = (TransformCore *) tool->private;
  181.  
  182.   diff_x = transform_core->curx - transform_core->lastx;
  183.   diff_y = transform_core->cury - transform_core->lasty;
  184.  
  185.   switch (transform_core->function)
  186.     {
  187.     case HANDLE_1:
  188.       transform_core->trans_info [X0] += diff_x;
  189.       transform_core->trans_info [Y0] += diff_y;
  190.       break;
  191.     case HANDLE_2:
  192.       transform_core->trans_info [X1] += diff_x;
  193.       transform_core->trans_info [Y1] += diff_y;
  194.       break;
  195.     case HANDLE_3:
  196.       transform_core->trans_info [X2] += diff_x;
  197.       transform_core->trans_info [Y2] += diff_y;
  198.       break;
  199.     case HANDLE_4:
  200.       transform_core->trans_info [X3] += diff_x;
  201.       transform_core->trans_info [Y3] += diff_y;
  202.       break;
  203.     default:
  204.       break;
  205.     }
  206. }
  207.  
  208. static void
  209. perspective_tool_recalc (Tool *tool,
  210.              void *gdisp_ptr)
  211. {
  212.   TransformCore *transform_core;
  213.   GDisplay      *gdisp;
  214.   GimpMatrix3    m;
  215.   gdouble        cx, cy;
  216.   gdouble        scalex, scaley;
  217.  
  218.   gdisp = (GDisplay *) tool->gdisp_ptr;
  219.   transform_core = (TransformCore *) tool->private;
  220.  
  221.   /*  determine the perspective transform that maps from
  222.    *  the unit cube to the trans_info coordinates
  223.    */
  224.   perspective_find_transform (transform_core->trans_info, m);
  225.  
  226.   cx     = transform_core->x1;
  227.   cy     = transform_core->y1;
  228.   scalex = 1.0;
  229.   scaley = 1.0;
  230.  
  231.   if (transform_core->x2 - transform_core->x1)
  232.     scalex = 1.0 / (transform_core->x2 - transform_core->x1);
  233.   if (transform_core->y2 - transform_core->y1)
  234.     scaley = 1.0 / (transform_core->y2 - transform_core->y1);
  235.  
  236.   /*  assemble the transformation matrix  */
  237.   gimp_matrix3_identity  (transform_core->transform);
  238.   gimp_matrix3_translate (transform_core->transform, -cx, -cy);
  239.   gimp_matrix3_scale     (transform_core->transform, scalex, scaley);
  240.   gimp_matrix3_mult      (m, transform_core->transform);
  241.  
  242.   /*  transform the bounding box  */
  243.   transform_core_transform_bounding_box (tool);
  244.  
  245.   /*  update the information dialog  */
  246.   perspective_info_update (tool);
  247. }
  248.  
  249. void
  250. perspective_find_transform (gdouble     *coords,
  251.                 GimpMatrix3  matrix)
  252. {
  253.   gdouble dx1, dx2, dx3, dy1, dy2, dy3;
  254.   gdouble det1, det2;
  255.  
  256.   dx1 = coords[X1] - coords[X3];
  257.   dx2 = coords[X2] - coords[X3];
  258.   dx3 = coords[X0] - coords[X1] + coords[X3] - coords[X2];
  259.  
  260.   dy1 = coords[Y1] - coords[Y3];
  261.   dy2 = coords[Y2] - coords[Y3];
  262.   dy3 = coords[Y0] - coords[Y1] + coords[Y3] - coords[Y2];
  263.  
  264.   /*  Is the mapping affine?  */
  265.   if ((dx3 == 0.0) && (dy3 == 0.0))
  266.     {
  267.       matrix[0][0] = coords[X1] - coords[X0];
  268.       matrix[0][1] = coords[X3] - coords[X1];
  269.       matrix[0][2] = coords[X0];
  270.       matrix[1][0] = coords[Y1] - coords[Y0];
  271.       matrix[1][1] = coords[Y3] - coords[Y1];
  272.       matrix[1][2] = coords[Y0];
  273.       matrix[2][0] = 0.0;
  274.       matrix[2][1] = 0.0;
  275.     }
  276.   else
  277.     {
  278.       det1 = dx3 * dy2 - dy3 * dx2;
  279.       det2 = dx1 * dy2 - dy1 * dx2;
  280.       matrix[2][0] = det1 / det2;
  281.       det1 = dx1 * dy3 - dy1 * dx3;
  282.       det2 = dx1 * dy2 - dy1 * dx2;
  283.       matrix[2][1] = det1 / det2;
  284.  
  285.       matrix[0][0] = coords[X1] - coords[X0] + matrix[2][0] * coords[X1];
  286.       matrix[0][1] = coords[X2] - coords[X0] + matrix[2][1] * coords[X2];
  287.       matrix[0][2] = coords[X0];
  288.  
  289.       matrix[1][0] = coords[Y1] - coords[Y0] + matrix[2][0] * coords[Y1];
  290.       matrix[1][1] = coords[Y2] - coords[Y0] + matrix[2][1] * coords[Y2];
  291.       matrix[1][2] = coords[Y0];
  292.     }
  293.  
  294.   matrix[2][2] = 1.0;
  295. }
  296.  
  297. TileManager *
  298. perspective_tool_perspective (GImage       *gimage,
  299.                   GimpDrawable *drawable,
  300.                   GDisplay     *gdisp,
  301.                   TileManager  *float_tiles,
  302.                   gboolean      interpolation,
  303.                   GimpMatrix3   matrix)
  304. {
  305.   gimp_progress *progress;
  306.   TileManager   *ret;
  307.  
  308.   progress = progress_start (gdisp, _("Perspective..."), FALSE, NULL, NULL);
  309.  
  310.   ret = transform_core_do (gimage, drawable, float_tiles,
  311.                interpolation, matrix,
  312.                progress ? progress_update_and_flush :
  313.                (progress_func_t) NULL,
  314.                progress);
  315.  
  316.   if (progress)
  317.     progress_end (progress);
  318.  
  319.   return ret;
  320. }
  321.