Guest User

map-object-main.cpp

a guest
Mar 3rd, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.65 KB | None | 0 0
  1. /* MapObject 1.2.0 -- image filter plug-in for GIMP
  2.  *
  3.  * Copyright (C) 1996-98 Tom Bech
  4.  * Copyright (C) 1996-98 Federico Mena Quintero
  5.  *
  6.  * E-mail: [email protected] (Tom) or [email protected] (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 3 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, see <http://www.gnu.org/licenses/>.
  20.  */
  21.  
  22. #include "config.h"
  23.  
  24. #include <gtk/gtk.h>
  25.  
  26. #include <libgimp/gimp.h>
  27. #include <libgimp/gimpui.h>
  28.  
  29. #include "map-object-ui.h"
  30. #include "map-object-image.h"
  31. #include "map-object-apply.h"
  32. #include "map-object-preview.h"
  33. #include "map-object-main.h"
  34.  
  35. #include "libgimp/stdplugins-intl.h"
  36.  
  37.  
  38. /* Global variables */
  39. /* ================ */
  40.  
  41. MapObjectValues mapvals;
  42.  
  43. /******************/
  44. /* Implementation */
  45. /******************/
  46.  
  47. /*************************************/
  48. /* Set parameters to standard values */
  49. /*************************************/
  50.  
  51. static void
  52. set_default_settings (void)
  53. {
  54.   gint i;
  55.  
  56.   gimp_vector3_set (&mapvals.viewpoint,  0.5, 0.5, 2.0);
  57.   gimp_vector3_set (&mapvals.firstaxis,  1.0, 0.0, 0.0);
  58.   gimp_vector3_set (&mapvals.secondaxis, 0.0, 1.0, 0.0);
  59.   gimp_vector3_set (&mapvals.normal,     0.0, 0.0, 1.0);
  60.   gimp_vector3_set (&mapvals.position,   0.5, 0.5, 0.0);
  61.   gimp_vector3_set (&mapvals.lightsource.position,  -0.5, -0.5, 2.0);
  62.   gimp_vector3_set (&mapvals.lightsource.direction, -1.0, -1.0, 1.0);
  63.   gimp_vector3_set (&mapvals.scale,      0.5, 0.5, 0.5);
  64.  
  65.   mapvals.maptype = MAP_PLANE;
  66.  
  67.   mapvals.pixeltreshold   = 0.25;
  68.   mapvals.alpha           = 0.0;
  69.   mapvals.beta            = 0.0;
  70.   mapvals.gamma           = 0.0;
  71.   mapvals.maxdepth        = 3.0;
  72.   mapvals.radius          = 0.25;
  73.   mapvals.cylinder_radius = 0.25;
  74.   mapvals.cylinder_length = 1.0;
  75.  
  76.   mapvals.zoom             = 1.0;
  77.   mapvals.lightsource.type = POINT_LIGHT;
  78.  
  79.   mapvals.antialiasing           = TRUE;
  80.   mapvals.create_new_image       = FALSE;
  81.   mapvals.create_new_layer       = FALSE;
  82.   mapvals.transparent_background = FALSE;
  83.   mapvals.tiled                  = FALSE;
  84.   mapvals.livepreview            = FALSE;
  85.   mapvals.showgrid               = TRUE;
  86.  
  87.   mapvals.lightsource.intensity = 1.0;
  88.   gimp_rgba_set (&mapvals.lightsource.color, 1.0, 1.0, 1.0, 1.0);
  89.  
  90.   mapvals.material.ambient_int  = 0.3;
  91.   mapvals.material.diffuse_int  = 1.0;
  92.   mapvals.material.diffuse_ref  = 0.5;
  93.   mapvals.material.specular_ref = 0.5;
  94.   mapvals.material.highlight    = 27.0;
  95.  
  96.   for (i = 0; i < 6; i++)
  97.     mapvals.boxmap_id[i] = -1;
  98.  
  99.   for (i = 0; i < 2; i++)
  100.     mapvals.cylindermap_id[i] = -1;
  101. }
  102.  
  103. static void
  104. check_drawables (GimpDrawable *drawable)
  105. {
  106.   gint i;
  107.  
  108.   /* Check that boxmap images are valid */
  109.   /* ================================== */
  110.  
  111.   for (i = 0; i < 6; i++)
  112.     {
  113.       if (mapvals.boxmap_id[i] == -1 ||
  114.           !gimp_item_is_valid (mapvals.boxmap_id[i]) ||
  115.           gimp_drawable_is_gray (mapvals.boxmap_id[i]))
  116.         mapvals.boxmap_id[i] = drawable->drawable_id;
  117.     }
  118.  
  119.   /* Check that cylindermap images are valid */
  120.   /* ======================================= */
  121.  
  122.   for (i = 0; i < 2; i++)
  123.     {
  124.       if (mapvals.cylindermap_id[i] == -1 ||
  125.           !gimp_item_is_valid (mapvals.cylindermap_id[i]) ||
  126.           gimp_drawable_is_gray (mapvals.cylindermap_id[i]))
  127.         mapvals.cylindermap_id[i] = drawable->drawable_id;
  128.     }
  129. }
  130.  
  131. static void
  132. query (void)
  133. {
  134.   static const GimpParamDef args[] =
  135.   {
  136.     { GIMP_PDB_INT32,    "run-mode",              "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
  137.     { GIMP_PDB_IMAGE,    "image",                 "Input image" },
  138.     { GIMP_PDB_DRAWABLE, "drawable",              "Input drawable" },
  139.     { GIMP_PDB_INT32,    "maptype",               "Type of mapping (0=plane,1=sphere,2=box,3=cylinder)" },
  140.     { GIMP_PDB_FLOAT,    "viewpoint-x",           "Position of viewpoint (x,y,z)" },
  141.     { GIMP_PDB_FLOAT,    "viewpoint-y",           "Position of viewpoint (x,y,z)" },
  142.     { GIMP_PDB_FLOAT,    "viewpoint-z",           "Position of viewpoint (x,y,z)" },
  143.     { GIMP_PDB_FLOAT,    "position-x",            "Object position (x,y,z)" },
  144.     { GIMP_PDB_FLOAT,    "position-y",            "Object position (x,y,z)" },
  145.     { GIMP_PDB_FLOAT,    "position-z",            "Object position (x,y,z)" },
  146.     { GIMP_PDB_FLOAT,    "firstaxis-x",           "First axis of object [x,y,z]" },
  147.     { GIMP_PDB_FLOAT,    "firstaxis-y",           "First axis of object [x,y,z]" },
  148.     { GIMP_PDB_FLOAT,    "firstaxis-z",           "First axis of object [x,y,z]" },
  149.     { GIMP_PDB_FLOAT,    "secondaxis-x",          "Second axis of object [x,y,z]" },
  150.     { GIMP_PDB_FLOAT,    "secondaxis-y",          "Second axis of object [x,y,z]" },
  151.     { GIMP_PDB_FLOAT,    "secondaxis-z",          "Second axis of object [x,y,z]" },
  152.     { GIMP_PDB_FLOAT,    "rotationangle-x",       "Rotation about X axis in degrees" },
  153.     { GIMP_PDB_FLOAT,    "rotationangle-y",       "Rotation about Y axis in degrees" },
  154.     { GIMP_PDB_FLOAT,    "rotationangle-z",       "Rotation about Z axis in degrees" },
  155.     { GIMP_PDB_INT32,    "lighttype",             "Type of lightsource (0=point,1=directional,2=none)" },
  156.     { GIMP_PDB_COLOR,    "lightcolor",            "Lightsource color (r,g,b)" },
  157.     { GIMP_PDB_FLOAT,    "lightposition-x",       "Lightsource position (x,y,z)" },
  158.     { GIMP_PDB_FLOAT,    "lightposition-y",       "Lightsource position (x,y,z)" },
  159.     { GIMP_PDB_FLOAT,    "lightposition-z",       "Lightsource position (x,y,z)" },
  160.     { GIMP_PDB_FLOAT,    "lightdirection-x",      "Lightsource direction [x,y,z]" },
  161.     { GIMP_PDB_FLOAT,    "lightdirection-y",      "Lightsource direction [x,y,z]" },
  162.     { GIMP_PDB_FLOAT,    "lightdirection-z",      "Lightsource direction [x,y,z]" },
  163.     { GIMP_PDB_FLOAT,    "ambient_intensity",     "Material ambient intensity (0..1)" },
  164.     { GIMP_PDB_FLOAT,    "diffuse_intensity",     "Material diffuse intensity (0..1)" },
  165.     { GIMP_PDB_FLOAT,    "diffuse_reflectivity",  "Material diffuse reflectivity (0..1)" },
  166.     { GIMP_PDB_FLOAT,    "specular_reflectivity", "Material specular reflectivity (0..1)" },
  167.     { GIMP_PDB_FLOAT,    "highlight",             "Material highlight (0..->), note: it's expotential" },
  168.     { GIMP_PDB_INT32,    "antialiasing",          "Apply antialiasing (TRUE/FALSE)" },
  169.     { GIMP_PDB_INT32,    "tiled",                 "Tile source image (TRUE/FALSE)" },
  170.     { GIMP_PDB_INT32,    "newimage",              "Create a new image (TRUE/FALSE)" },
  171.     { GIMP_PDB_INT32,    "transparentbackground", "Make background transparent (TRUE/FALSE)" },
  172.     { GIMP_PDB_FLOAT,    "radius",                "Sphere/cylinder radius (only used when maptype=1 or 3)" },
  173.     { GIMP_PDB_FLOAT,    "x-scale",               "Box x size (0..->)" },
  174.     { GIMP_PDB_FLOAT,    "y-scale",               "Box y size (0..->)" },
  175.     { GIMP_PDB_FLOAT,    "z-scale",               "Box z size (0..->)"},
  176.     { GIMP_PDB_FLOAT,    "cylinder-length",       "Cylinder length (0..->)"},
  177.     { GIMP_PDB_DRAWABLE, "box-front-drawable",    "Box front face (set these to -1 if not used)" },
  178.     { GIMP_PDB_DRAWABLE, "box-back-drawable",     "Box back face" },
  179.     { GIMP_PDB_DRAWABLE, "box-top-drawable",      "Box top face" },
  180.     { GIMP_PDB_DRAWABLE, "box-bottom-drawable",   "Box bottom face" },
  181.     { GIMP_PDB_DRAWABLE, "box-left-drawable",     "Box left face" },
  182.     { GIMP_PDB_DRAWABLE, "box-right-drawable",    "Box right face" },
  183.     { GIMP_PDB_DRAWABLE, "cyl-top-drawable",      "Cylinder top face (set these to -1 if not used)" },
  184.     { GIMP_PDB_DRAWABLE, "cyl-bottom-drawable",   "Cylinder bottom face" }
  185.   };
  186.  
  187.   gimp_install_procedure (PLUG_IN_PROC,
  188.                           N_("Map the image to an object (plane, sphere, box or cylinder)"),
  189.                           "No help yet",
  190.                           "Tom Bech & Federico Mena Quintero",
  191.                           "Tom Bech & Federico Mena Quintero",
  192.                           "Version 1.2.0, July 16 1998",
  193.                           N_("Map _Object..."),
  194.                           "RGB*",
  195.                           GIMP_PLUGIN,
  196.                           G_N_ELEMENTS (args), 0,
  197.                           args, NULL);
  198.  
  199.   gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Filters/Map");
  200. }
  201.  
  202. static void
  203. run (const gchar      *name,
  204.      gint              nparams,
  205.      const GimpParam  *param,
  206.      gint             *nreturn_vals,
  207.      GimpParam       **return_vals)
  208. {
  209.   static GimpParam   values[1];
  210.   GimpDrawable      *drawable;
  211.   GimpRunMode    run_mode;
  212.   GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
  213.   gint               i;
  214.  
  215.   run_mode = param[0].data.d_int32;
  216.  
  217.   INIT_I18N ();
  218.  
  219.   values[0].type = GIMP_PDB_STATUS;
  220.   values[0].data.d_status = status;
  221.  
  222.   *nreturn_vals = 1;
  223.   *return_vals = values;
  224.  
  225.   /* Set default values */
  226.   /* ================== */
  227.  
  228.   set_default_settings ();
  229.  
  230.   /* Get the specified drawable */
  231.   /* ========================== */
  232.  
  233.   image_id = param[1].data.d_int32;
  234.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  235.  
  236.   switch (run_mode)
  237.     {
  238.       case GIMP_RUN_INTERACTIVE:
  239.  
  240.         /* Possibly retrieve data */
  241.         /* ====================== */
  242.  
  243.         gimp_get_data (PLUG_IN_PROC, &mapvals);
  244.         check_drawables (drawable);
  245.         if (main_dialog (drawable))
  246.           {
  247.             compute_image ();
  248.  
  249.             gimp_set_data (PLUG_IN_PROC, &mapvals, sizeof (MapObjectValues));
  250.           }
  251.         break;
  252.  
  253.       case GIMP_RUN_WITH_LAST_VALS:
  254.         gimp_get_data (PLUG_IN_PROC, &mapvals);
  255.         check_drawables (drawable);
  256.         image_setup (drawable, FALSE);
  257.         compute_image ();
  258.         break;
  259.  
  260.       case GIMP_RUN_NONINTERACTIVE:
  261.         if (nparams != 49)
  262.           {
  263.             status = GIMP_PDB_CALLING_ERROR;
  264.           }
  265.         else
  266.           {
  267.             mapvals.maptype                 = (MapType) param[3].data.d_int32;
  268.             mapvals.viewpoint.x             = param[4].data.d_float;
  269.             mapvals.viewpoint.y             = param[5].data.d_float;
  270.             mapvals.viewpoint.z             = param[6].data.d_float;
  271.             mapvals.position.x              = param[7].data.d_float;
  272.             mapvals.position.y              = param[8].data.d_float;
  273.             mapvals.position.z              = param[9].data.d_float;
  274.             mapvals.firstaxis.x             = param[10].data.d_float;
  275.             mapvals.firstaxis.y             = param[11].data.d_float;
  276.             mapvals.firstaxis.z             = param[12].data.d_float;
  277.             mapvals.secondaxis.x            = param[13].data.d_float;
  278.             mapvals.secondaxis.y            = param[14].data.d_float;
  279.             mapvals.secondaxis.z            = param[15].data.d_float;
  280.             mapvals.alpha                   = param[16].data.d_float;
  281.             mapvals.beta                    = param[17].data.d_float;
  282.             mapvals.gamma                   = param[18].data.d_float;
  283.             mapvals.lightsource.type        = (LightType) param[19].data.d_int32;
  284.             mapvals.lightsource.color       = param[20].data.d_color;
  285.             mapvals.lightsource.position.x  = param[21].data.d_float;
  286.             mapvals.lightsource.position.y  = param[22].data.d_float;
  287.             mapvals.lightsource.position.z  = param[23].data.d_float;
  288.             mapvals.lightsource.direction.x = param[24].data.d_float;
  289.             mapvals.lightsource.direction.y = param[25].data.d_float;
  290.             mapvals.lightsource.direction.z = param[26].data.d_float;
  291.             mapvals.material.ambient_int    = param[27].data.d_float;
  292.             mapvals.material.diffuse_int    = param[28].data.d_float;
  293.             mapvals.material.diffuse_ref    = param[29].data.d_float;
  294.             mapvals.material.specular_ref   = param[30].data.d_float;
  295.             mapvals.material.highlight      = param[31].data.d_float;
  296.             mapvals.antialiasing            = (gint) param[32].data.d_int32;
  297.             mapvals.tiled                   = (gint) param[33].data.d_int32;
  298.             mapvals.create_new_image        = (gint) param[34].data.d_int32;
  299.             mapvals.transparent_background  = (gint) param[35].data.d_int32;
  300.             mapvals.radius                  = param[36].data.d_float;
  301.             mapvals.cylinder_radius         = param[36].data.d_float;
  302.             mapvals.scale.x                 = param[37].data.d_float;
  303.             mapvals.scale.y                 = param[38].data.d_float;
  304.             mapvals.scale.z                 = param[39].data.d_float;
  305.             mapvals.cylinder_length         = param[40].data.d_float;
  306.  
  307.             for (i = 0; i < 6; i++)
  308.               mapvals.boxmap_id[i] = param[41+i].data.d_drawable;
  309.  
  310.             for (i = 0; i < 2; i++)
  311.               mapvals.cylindermap_id[i] = param[47+i].data.d_drawable;
  312.  
  313.             check_drawables (drawable);
  314.             image_setup (drawable, FALSE);
  315.             compute_image ();
  316.           }
  317.         break;
  318.     }
  319.  
  320.   values[0].data.d_status = status;
  321.  
  322.   if (run_mode != GIMP_RUN_NONINTERACTIVE)
  323.     gimp_displays_flush ();
  324.  
  325.   gimp_drawable_detach (drawable);
  326. }
  327.  
  328. const GimpPlugInInfo PLUG_IN_INFO =
  329. {
  330.   NULL,  /* init_proc  */
  331.   NULL,  /* quit_proc  */
  332.   query, /* query_proc */
  333.   run,   /* run_proc   */
  334. };
  335.  
  336. MAIN ()
Advertisement
Add Comment
Please, Sign In to add comment