home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / MapObject / mapobject_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  14.0 KB  |  358 lines

  1. /* MapObject 1.2.0 -- image filter plug-in for The Gimp program
  2.  *
  3.  * Copyright (C) 1996-98 Tom Bech
  4.  * Copyright (C) 1996-98 Federico Mena Quintero
  5.  *
  6.  * E-mail: tomb@gimp.org (Tom) or quartic@gimp.org (Federico)
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21.  */
  22.  
  23. #include <gtk/gtk.h>
  24.  
  25. #include <gck/gck.h>
  26.  
  27. #include <libgimp/gimp.h>
  28. #include <libgimp/gimpui.h>
  29.  
  30. #include "arcball.h"
  31. #include "mapobject_ui.h"
  32. #include "mapobject_image.h"
  33. #include "mapobject_apply.h"
  34. #include "mapobject_preview.h"
  35. #include "mapobject_main.h"
  36.  
  37. #include "config.h"
  38. #include "libgimp/stdplugins-intl.h"
  39.  
  40.  
  41. /* Global variables */
  42. /* ================ */
  43.  
  44. MapObjectValues mapvals;
  45.  
  46. /******************/
  47. /* Implementation */
  48. /******************/
  49.  
  50. /*************************************/
  51. /* Set parameters to standard values */
  52. /*************************************/
  53.  
  54. static void
  55. set_default_settings (void)
  56. {
  57.   gint i;
  58.  
  59.   gimp_vector3_set (&mapvals.viewpoint,  0.5, 0.5, 2.0);
  60.   gimp_vector3_set (&mapvals.firstaxis,  1.0, 0.0, 0.0);
  61.   gimp_vector3_set (&mapvals.secondaxis, 0.0, 1.0, 0.0);
  62.   gimp_vector3_set (&mapvals.normal,     0.0, 0.0, 1.0);
  63.   gimp_vector3_set (&mapvals.position,   0.5, 0.5, 0.0);
  64.   gimp_vector3_set (&mapvals.lightsource.position,  -0.5, -0.5, 2.0);
  65.   gimp_vector3_set (&mapvals.lightsource.direction, -1.0, -1.0, 1.0);
  66.   gimp_vector3_set (&mapvals.scale,      0.5, 0.5, 0.5);
  67.  
  68.   mapvals.maptype = MAP_PLANE;
  69.  
  70.   mapvals.pixeltreshold   = 0.25;
  71.   mapvals.alpha           = 0.0;
  72.   mapvals.beta            = 0.0;
  73.   mapvals.gamma           = 0.0;
  74.   mapvals.maxdepth        = 3.0;
  75.   mapvals.radius          = 0.25;
  76.   mapvals.cylinder_radius = 0.25;
  77.   mapvals.cylinder_length = 1.0;
  78.  
  79.   mapvals.preview_zoom_factor = 0;
  80.  
  81.   mapvals.lightsource.type = POINT_LIGHT;
  82.  
  83.   mapvals.antialiasing           = TRUE;
  84.   mapvals.create_new_image       = FALSE;
  85.   mapvals.transparent_background = FALSE;
  86.   mapvals.tiled                  = FALSE;
  87.   mapvals.showgrid               = FALSE;
  88.   mapvals.tooltips_enabled       = TRUE;
  89.  
  90.   mapvals.lightsource.intensity = 1.0;
  91.   gck_rgb_set (&mapvals.lightsource.color, 1.0, 1.0, 1.0);
  92.  
  93.   mapvals.material.ambient_int  = 0.3;
  94.   mapvals.material.diffuse_int  = 1.0;
  95.   mapvals.material.diffuse_ref  = 0.5;
  96.   mapvals.material.specular_ref = 0.5;
  97.   mapvals.material.highlight    = 27.0;
  98.   
  99.   for (i = 0; i < 6; i++)
  100.     mapvals.boxmap_id[i] = -1;
  101.  
  102.   for (i = 0; i < 2; i++)
  103.     mapvals.cylindermap_id[i] = -1;
  104. }
  105.  
  106. static void
  107. check_drawables (GimpDrawable *drawable)
  108. {
  109.   gint i;
  110.  
  111.   /* Check that boxmap images are valid */
  112.   /* ================================== */
  113.  
  114.   for (i = 0; i < 6; i++)
  115.     {
  116.       if (mapvals.boxmap_id[i] == -1)
  117.         mapvals.boxmap_id[i] = drawable->id;
  118.       else if (mapvals.boxmap_id[i] != -1 &&
  119.            gimp_drawable_image_id (mapvals.boxmap_id[i]) == -1)
  120.         mapvals.boxmap_id[i] = drawable->id;
  121.       else if (gimp_drawable_is_gray (mapvals.boxmap_id[i]))
  122.         mapvals.boxmap_id[i] = drawable->id;
  123.     }
  124.  
  125.   /* Check that cylindermap images are valid */
  126.   /* ======================================= */
  127.  
  128.   for (i = 0; i < 2; i++)
  129.     {
  130.       if (mapvals.cylindermap_id[i] == -1)
  131.         mapvals.cylindermap_id[i] = drawable->id;
  132.       else if (mapvals.cylindermap_id[i]!=-1 && 
  133.                gimp_drawable_image_id (mapvals.cylindermap_id[i]) == -1)
  134.         mapvals.cylindermap_id[i] = drawable->id;
  135.       else if (gimp_drawable_is_gray (mapvals.cylindermap_id[i]))
  136.         mapvals.cylindermap_id[i] = drawable->id;
  137.     }
  138. }
  139.  
  140. static void
  141. query (void)
  142. {
  143.   static GimpParamDef args[] =
  144.   {
  145.     { GIMP_PDB_INT32,    "run_mode",              "Interactive (0), non-interactive (1)" },
  146.     { GIMP_PDB_IMAGE,    "image",                 "Input image" },
  147.     { GIMP_PDB_DRAWABLE, "drawable",              "Input drawable" },
  148.     { GIMP_PDB_INT32,    "maptype",               "Type of mapping (0=plane,1=sphere,2=box,3=cylinder)" },
  149.     { GIMP_PDB_FLOAT,    "viewpoint_x",           "Position of viewpoint (x,y,z)" },
  150.     { GIMP_PDB_FLOAT,    "viewpoint_y",           "Position of viewpoint (x,y,z)" },
  151.     { GIMP_PDB_FLOAT,    "viewpoint_z",           "Position of viewpoint (x,y,z)" },
  152.     { GIMP_PDB_FLOAT,    "position_x",            "Object position (x,y,z)" },
  153.     { GIMP_PDB_FLOAT,    "position_y",            "Object position (x,y,z)" },
  154.     { GIMP_PDB_FLOAT,    "position_z",            "Object position (x,y,z)" },
  155.     { GIMP_PDB_FLOAT,    "firstaxis_x",           "First axis of object [x,y,z]" },
  156.     { GIMP_PDB_FLOAT,    "firstaxis_y",           "First axis of object [x,y,z]" },
  157.     { GIMP_PDB_FLOAT,    "firstaxis_z",           "First axis of object [x,y,z]" },
  158.     { GIMP_PDB_FLOAT,    "secondaxis_x",          "Second axis of object [x,y,z]" },
  159.     { GIMP_PDB_FLOAT,    "secondaxis_y",          "Second axis of object [x,y,z]" },
  160.     { GIMP_PDB_FLOAT,    "secondaxis_z",          "Second axis of object [x,y,z]" },
  161.     { GIMP_PDB_FLOAT,    "rotationangle_x",       "Rotation about X axis in degrees" },
  162.     { GIMP_PDB_FLOAT,    "rotationangle_y",       "Rotation about Y axis in degrees" },
  163.     { GIMP_PDB_FLOAT,    "rotationangle_z",       "Rotation about Z axis in degrees" },
  164.     { GIMP_PDB_INT32,    "lighttype",             "Type of lightsource (0=point,1=directional,3=none)" },
  165.     { GIMP_PDB_COLOR,    "lightcolor",            "Lightsource color (r,g,b)" },
  166.     { GIMP_PDB_FLOAT,    "lightposition_x",       "Lightsource position (x,y,z)" },
  167.     { GIMP_PDB_FLOAT,    "lightposition_y",       "Lightsource position (x,y,z)" },
  168.     { GIMP_PDB_FLOAT,    "lightposition_z",       "Lightsource position (x,y,z)" },
  169.     { GIMP_PDB_FLOAT,    "lightdirection_x",      "Lightsource direction [x,y,z]" },
  170.     { GIMP_PDB_FLOAT,    "lightdirection_y",      "Lightsource direction [x,y,z]" },
  171.     { GIMP_PDB_FLOAT,    "lightdirection_z",      "Lightsource direction [x,y,z]" },
  172.     { GIMP_PDB_FLOAT,    "ambient_intensity",     "Material ambient intensity (0..1)" },
  173.     { GIMP_PDB_FLOAT,    "diffuse_intensity",     "Material diffuse intensity (0..1)" },
  174.     { GIMP_PDB_FLOAT,    "diffuse_reflectivity",  "Material diffuse reflectivity (0..1)" },
  175.     { GIMP_PDB_FLOAT,    "specular_reflectivity", "Material specular reflectivity (0..1)" },
  176.     { GIMP_PDB_FLOAT,    "highlight",             "Material highlight (0..->), note: it's expotential" },
  177.     { GIMP_PDB_INT32,    "antialiasing",          "Apply antialiasing (TRUE/FALSE)" },
  178.     { GIMP_PDB_INT32,    "tiled",                 "Tile source image (TRUE/FALSE)" },
  179.     { GIMP_PDB_INT32,    "newimage",              "Create a new image (TRUE/FALSE)" },
  180.     { GIMP_PDB_INT32,    "transparentbackground", "Make background transparent (TRUE/FALSE)" },
  181.     { GIMP_PDB_FLOAT,    "radius",                "Sphere/cylinder radius (only used when maptype=1 or 3)" },
  182.     { GIMP_PDB_FLOAT,    "x_scale",               "Box x size (0..->)" },
  183.     { GIMP_PDB_FLOAT,    "y_scale",               "Box y size (0..->)" },
  184.     { GIMP_PDB_FLOAT,    "z_scale",               "Box z size (0..->)"},
  185.     { GIMP_PDB_FLOAT,    "cylinder_length",       "Cylinder length (0..->)"},
  186.     { GIMP_PDB_DRAWABLE, "box_front_drawable",    "Box front face (set these to -1 if not used)" },
  187.     { GIMP_PDB_DRAWABLE, "box_back_drawable",     "Box back face" },
  188.     { GIMP_PDB_DRAWABLE, "box_top_drawable",      "Box top face" },
  189.     { GIMP_PDB_DRAWABLE, "box_bottom_drawable",   "Box bottom face" },
  190.     { GIMP_PDB_DRAWABLE, "box_left_drawable",     "Box left face" },
  191.     { GIMP_PDB_DRAWABLE, "box_right_drawable",    "Box right face" },
  192.     { GIMP_PDB_DRAWABLE, "cyl_top_drawable",      "Cylinder top face (set these to -1 if not used)" },
  193.     { GIMP_PDB_DRAWABLE, "cyl_bottom_drawable",   "Cylinder bottom face" }      
  194.   };
  195.   static gint nargs = sizeof (args) / sizeof (args[0]);
  196.  
  197.   INIT_I18N();
  198.  
  199.   gimp_install_procedure ("plug_in_map_object",
  200.               "Maps a picture to a object (plane, sphere, box or cylinder)",
  201.               "No help yet",
  202.               "Tom Bech & Federico Mena Quintero",
  203.               "Tom Bech & Federico Mena Quintero",
  204.               "Version 1.2.0, July 16 1998",
  205.               N_("<Image>/Filters/Map/Map Object..."),
  206.               "RGB*",
  207.               GIMP_PLUGIN,
  208.               nargs, 0,
  209.               args, NULL);
  210. }
  211.  
  212. static void
  213. run (gchar   *name,
  214.      gint     nparams,
  215.      GimpParam  *param,
  216.      gint    *nreturn_vals,
  217.      GimpParam **return_vals)
  218. {
  219.   static GimpParam values[1];
  220.   GimpDrawable *drawable;
  221.   GimpRunModeType run_mode;
  222.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  223.   gint i;
  224.  
  225.   run_mode = param[0].data.d_int32;
  226.  
  227.   if (run_mode == GIMP_RUN_INTERACTIVE)
  228.     {
  229.       INIT_I18N_UI();
  230.     }
  231.   else
  232.     {
  233.       INIT_I18N();
  234.     }
  235.  
  236.   values[0].type = GIMP_PDB_STATUS;
  237.   values[0].data.d_status = status;
  238.  
  239.   *nreturn_vals = 1;
  240.   *return_vals = values;
  241.  
  242.   /* Set default values */
  243.   /* ================== */
  244.  
  245.   set_default_settings ();
  246.  
  247.   /* Get the specified drawable */
  248.   /* ========================== */
  249.  
  250.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  251.  
  252.   switch (run_mode)
  253.     {
  254.       case GIMP_RUN_INTERACTIVE:
  255.         
  256.         /* Possibly retrieve data */
  257.         /* ====================== */
  258.     
  259.         gimp_get_data ("plug_in_map_object", &mapvals);
  260.         check_drawables (drawable);
  261.         if (main_dialog (drawable))
  262.       {
  263.         compute_image ();
  264.  
  265.         gimp_set_data ("plug_in_map_object",
  266.                &mapvals, sizeof (MapObjectValues));
  267.       }
  268.         break;
  269.  
  270.       case GIMP_RUN_WITH_LAST_VALS:
  271.         gimp_get_data ("plug_in_map_object", &mapvals);
  272.         check_drawables (drawable);
  273.         image_setup (drawable, FALSE);
  274.         compute_image ();
  275.         break;
  276.  
  277.       case GIMP_RUN_NONINTERACTIVE:
  278.         if (nparams != 49)
  279.       {
  280.         status = GIMP_PDB_CALLING_ERROR;
  281.       }
  282.         else
  283.           {
  284.             mapvals.maptype                 = (MapType)param[3].data.d_int32;
  285.             mapvals.viewpoint.x             = param[4].data.d_float;
  286.             mapvals.viewpoint.y             = param[5].data.d_float;
  287.             mapvals.viewpoint.z             = param[6].data.d_float;
  288.             mapvals.position.x              = param[7].data.d_float;
  289.             mapvals.position.y              = param[8].data.d_float;
  290.             mapvals.position.z              = param[9].data.d_float;
  291.             mapvals.firstaxis.x             = param[10].data.d_float;
  292.             mapvals.firstaxis.y             = param[11].data.d_float;
  293.             mapvals.firstaxis.z             = param[12].data.d_float;
  294.             mapvals.secondaxis.x            = param[13].data.d_float;
  295.             mapvals.secondaxis.y            = param[14].data.d_float;
  296.             mapvals.secondaxis.z            = param[15].data.d_float;
  297.             mapvals.alpha                   = param[16].data.d_float;
  298.             mapvals.beta                    = param[17].data.d_float;
  299.             mapvals.gamma                   = param[18].data.d_float;
  300.             mapvals.lightsource.type        = (LightType)param[19].data.d_int32;
  301.             mapvals.lightsource.color.r     = param[20].data.d_color.red;
  302.             mapvals.lightsource.color.g     = param[20].data.d_color.green;
  303.             mapvals.lightsource.color.b     = param[20].data.d_color.blue;
  304.             mapvals.lightsource.position.x  = param[21].data.d_float;
  305.             mapvals.lightsource.position.y  = param[22].data.d_float;
  306.             mapvals.lightsource.position.z  = param[23].data.d_float;
  307.             mapvals.lightsource.direction.x = param[24].data.d_float;
  308.             mapvals.lightsource.direction.y = param[25].data.d_float;
  309.             mapvals.lightsource.direction.z = param[26].data.d_float;
  310.             mapvals.material.ambient_int    = param[27].data.d_float;
  311.             mapvals.material.diffuse_int    = param[28].data.d_float;
  312.             mapvals.material.diffuse_ref    = param[29].data.d_float;
  313.             mapvals.material.specular_ref   = param[30].data.d_float;
  314.             mapvals.material.highlight      = param[31].data.d_float;
  315.             mapvals.antialiasing            = (gint)param[32].data.d_int32;
  316.             mapvals.tiled                   = (gint)param[33].data.d_int32;
  317.             mapvals.create_new_image        = (gint)param[34].data.d_int32;
  318.             mapvals.transparent_background  = (gint)param[35].data.d_int32;
  319.             mapvals.radius                  = param[36].data.d_float;
  320.             mapvals.cylinder_radius         = param[36].data.d_float;
  321.             mapvals.scale.x                 = param[37].data.d_float;
  322.             mapvals.scale.y                 = param[38].data.d_float;
  323.             mapvals.scale.z                 = param[39].data.d_float;
  324.             mapvals.cylinder_length         = param[40].data.d_float;
  325.  
  326.             for (i = 0; i < 6; i++)
  327.               mapvals.boxmap_id[i] =
  328.         gimp_drawable_get (param[41+i].data.d_drawable)->id;
  329.  
  330.             for (i = 0; i < 2; i++)
  331.               mapvals.cylindermap_id[i] =
  332.         gimp_drawable_get (param[47+i].data.d_drawable)->id;
  333.  
  334.             check_drawables (drawable);
  335.             image_setup (drawable, FALSE);
  336.             compute_image ();
  337.           }
  338.         break;
  339.     }
  340.  
  341.   values[0].data.d_status = status;
  342.   
  343.   if (run_mode != GIMP_RUN_NONINTERACTIVE)
  344.     gimp_displays_flush ();
  345.  
  346.   gimp_drawable_detach (drawable);
  347. }
  348.  
  349. GimpPlugInInfo PLUG_IN_INFO =
  350. {
  351.   NULL,  /* init_proc  */
  352.   NULL,  /* quit_proc  */
  353.   query, /* query_proc */
  354.   run,   /* run_proc   */
  355. };
  356.  
  357. MAIN ()
  358.