home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / common / spread.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-28  |  13.2 KB  |  486 lines

  1. /* Spread --- image filter plug-in for The Gimp image manipulation program
  2.  * Copyright (C) 1997 Brian Degenhardt and Federico Mena Quintero
  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.  * Please direct all comments, questions, bug reports  etc to Brian Degenhardt
  19.  * bdegenha@ucsd.edu
  20.  *
  21.  * You can contact Federico Mena Quintero at quartic@polloux.fciencias.unam.mx
  22.  * You can contact the original The Gimp authors at gimp@xcf.berkeley.edu
  23.  */
  24. #include "config.h"
  25.  
  26. #include <time.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. #include <gtk/gtk.h>
  31.  
  32. #include <libgimp/gimp.h>
  33. #include <libgimp/gimpui.h>
  34.  
  35. #include "libgimp/stdplugins-intl.h"
  36.  
  37.  
  38. /* Some useful macros */
  39. #define SCALE_WIDTH 200
  40. #define TILE_CACHE_SIZE 16
  41. #define ENTRY_WIDTH 50
  42.  
  43. typedef struct
  44. {
  45.   gdouble spread_amount_x;
  46.   gdouble spread_amount_y;
  47. } SpreadValues;
  48.  
  49. typedef struct
  50. {
  51.   GtkWidget *size;
  52.   gint       run;
  53. } SpreadInterface;
  54.  
  55.  
  56. /* Declare local functions.
  57.  */
  58. static void      query  (void);
  59. static void      run    (gchar      *name,
  60.              gint        nparams,
  61.              GimpParam  *param,
  62.              gint       *nreturn_vals,
  63.              GimpParam **return_vals);
  64.  
  65. static void      spread (GimpDrawable *drawable);
  66.  
  67. static gint      spread_dialog          (gint32        image_ID,
  68.                      GimpDrawable *drawable);
  69. static void      spread_ok_callback     (GtkWidget    *widget,
  70.                          gpointer      data);
  71.  
  72. static GimpTile *   spread_pixel (GimpDrawable *drawable,
  73.                   GimpTile     *tile,
  74.                   gint          x1,
  75.                   gint          y1,
  76.                   gint          x2,
  77.                   gint          y2,
  78.                   gint          x,
  79.                   gint          y,
  80.                   gint         *row,
  81.                   gint         *col,
  82.                   guchar       *pixel);
  83.  
  84.  
  85. /***** Local vars *****/
  86.  
  87. GimpPlugInInfo PLUG_IN_INFO =
  88. {
  89.   NULL,  /* init_proc  */
  90.   NULL,  /* quit_proc  */
  91.   query, /* query_proc */
  92.   run,   /* run_proc   */
  93. };
  94.  
  95. static SpreadValues spvals =
  96. {
  97.   5,  /*  horizontal spread amount  */
  98.   5   /*  vertical spread amount    */
  99. };
  100.  
  101. static SpreadInterface pint =
  102. {
  103.   FALSE   /*  run  */
  104. };
  105.  
  106. /***** Functions *****/
  107.  
  108. MAIN ()
  109.  
  110. static void
  111. query (void)
  112. {
  113.   static GimpParamDef args[] =
  114.   {
  115.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  116.     { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
  117.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
  118.     { GIMP_PDB_FLOAT, "spread_amount_x", "Horizontal spread amount (0 <= spread_amount_x <= 200)" },
  119.     { GIMP_PDB_FLOAT, "spread_amount_y", "Vertical spread amount (0 <= spread_amount_y <= 200)" }
  120.   };
  121.   static gint nargs = sizeof (args) / sizeof (args[0]);
  122.  
  123.   gimp_install_procedure ("plug_in_spread",
  124.               "Spread the contents of the specified drawable",
  125.               "Spreads the pixels of the specified drawable.  "
  126.               "Pixels are randomly moved to another location whose "
  127.               "distance varies from the original by the horizontal "
  128.               "and vertical spread amounts ",
  129.               "Spencer Kimball and Peter Mattis, ported by Brian "
  130.               "Degenhardt and Federico Mena Quintero",
  131.               "Federico Mena Quintero and Brian Degenhardt",
  132.               "1997",
  133.               N_("<Image>/Filters/Noise/Spread..."),
  134.               "RGB*, GRAY*",
  135.               GIMP_PLUGIN,
  136.               nargs, 0,
  137.               args, NULL);
  138. }
  139.  
  140. static void
  141. run (gchar      *name,
  142.      gint        nparams,
  143.      GimpParam  *param,
  144.      gint       *nreturn_vals,
  145.      GimpParam **return_vals)
  146. {
  147.   static GimpParam values[1];
  148.   gint32 image_ID;
  149.   GimpDrawable *drawable;
  150.   GimpRunModeType run_mode;
  151.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  152.  
  153.   run_mode = param[0].data.d_int32;
  154.  
  155.   /*  Get the specified image and drawable  */
  156.   image_ID = param[1].data.d_image;
  157.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  158.  
  159.   *nreturn_vals = 1;
  160.   *return_vals = values;
  161.  
  162.   values[0].type = GIMP_PDB_STATUS;
  163.   values[0].data.d_status = status;
  164.  
  165.   switch (run_mode)
  166.     {
  167.     case GIMP_RUN_INTERACTIVE:
  168.       INIT_I18N_UI();
  169.       /*  Possibly retrieve data  */
  170.       gimp_get_data ("plug_in_spread", &spvals);
  171.  
  172.       /*  First acquire information with a dialog  */
  173.       if (! spread_dialog (image_ID, drawable))
  174.     return;
  175.       break;
  176.  
  177.     case GIMP_RUN_NONINTERACTIVE:
  178.       INIT_I18N();
  179.       /*  Make sure all the arguments are there!  */
  180.       if (nparams != 5)
  181.     {
  182.       status = GIMP_PDB_CALLING_ERROR;
  183.     }
  184.       else
  185.     {
  186.       spvals.spread_amount_x= param[3].data.d_float;
  187.           spvals.spread_amount_y = param[4].data.d_float;
  188.         }
  189.  
  190.       if ((status == GIMP_PDB_SUCCESS) &&
  191.       (spvals.spread_amount_x < 0 || spvals.spread_amount_x > 200) &&
  192.           (spvals.spread_amount_y < 0 || spvals.spread_amount_y > 200))
  193.     status = GIMP_PDB_CALLING_ERROR;
  194.       break;
  195.  
  196.     case GIMP_RUN_WITH_LAST_VALS:
  197.       INIT_I18N();
  198.       /*  Possibly retrieve data  */
  199.       gimp_get_data ("plug_in_spread", &spvals);
  200.       break;
  201.  
  202.     default:
  203.       break;
  204.     }
  205.  
  206.   if (status == GIMP_PDB_SUCCESS)
  207.     {
  208.       /*  Make sure that the drawable is gray or RGB color  */
  209.       if (gimp_drawable_is_rgb (drawable->id) ||
  210.       gimp_drawable_is_gray (drawable->id))
  211.     {
  212.       gimp_progress_init (_("Spreading..."));
  213.  
  214.       /*  set the tile cache size  */
  215.       gimp_tile_cache_ntiles (TILE_CACHE_SIZE);
  216.  
  217.       /*  run the spread effect  */
  218.       spread (drawable);
  219.  
  220.       if (run_mode != GIMP_RUN_NONINTERACTIVE)
  221.         gimp_displays_flush ();
  222.  
  223.       /*  Store data  */
  224.       if (run_mode == GIMP_RUN_INTERACTIVE)
  225.         gimp_set_data ("plug_in_spread", &spvals, sizeof (SpreadValues));
  226.     }
  227.       else
  228.     {
  229.       /* gimp_message ("spread: cannot operate on indexed color images"); */
  230.       status = GIMP_PDB_EXECUTION_ERROR;
  231.     }
  232.     }
  233.  
  234.   values[0].data.d_status = status;
  235.  
  236.   gimp_drawable_detach (drawable);
  237. }
  238.  
  239. static void
  240. spread (GimpDrawable *drawable)
  241. {
  242.   GimpPixelRgn  dest_rgn;
  243.   GimpTile     *tile = NULL;
  244.   gint      row = -1;
  245.   gint      col = -1;
  246.   gpointer  pr;
  247.  
  248.   gint    width, height;
  249.   gint    bytes;
  250.   guchar *destrow;
  251.   guchar *dest;
  252.   guchar  pixel[4][4];
  253.   gint    x1, y1, x2, y2;
  254.   gint    x, y;
  255.   gint    progress, max_progress;
  256.  
  257.   gdouble x_amount, y_amount;
  258.   gdouble angle;
  259.  
  260.   gint xdist, ydist;
  261.   gint xi, yi;
  262.  
  263.   gint k;
  264.   gint x_mod_value, x_sub_value;
  265.   gint y_mod_value, y_sub_value;
  266.   gint angle_mod_value, angle_sub_value;
  267.  
  268.   /* Get selection area */
  269.  
  270.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  271.  
  272.   width  = drawable->width;
  273.   height = drawable->height;
  274.   bytes  = drawable->bpp;
  275.  
  276.   progress     = 0;
  277.   max_progress = (x2 - x1) * (y2 - y1);
  278.  
  279.   x_amount = spvals.spread_amount_x;
  280.   y_amount = spvals.spread_amount_y;
  281.  
  282.   /* Initialize random stuff */
  283.   srand (time (NULL));
  284.   angle_mod_value = G_PI*2;
  285.   angle_sub_value = angle_mod_value / 2;
  286.   x_mod_value = x_amount + 1;
  287.   x_sub_value = x_mod_value / 2;
  288.   y_mod_value = y_amount + 1;
  289.   y_sub_value = y_mod_value / 2;
  290.  
  291.   /* Spread the image.  This is done by going through every pixel
  292.      in the source image and swapping it with some other random
  293.      pixel.  The random pixel is located within an ellipse that is
  294.      as high as the spread_amount_y parameter and as wide as the
  295.      spread_amount_x parameter.  This is done by randomly selecting
  296.      an angle and then multiplying the sine of the angle to a random
  297.      number whose range is between -spread_amount_x/2 and spread_amount_x/2.
  298.      The y coordinate is found by multiplying the cosine of the angle
  299.      to the random value generated from spread_amount_y.  The reason
  300.      that the spread is done this way is to make the end product more
  301.      random looking.  To see a result of this, compare spreading a
  302.      square with gimp 0.54 to spreading a square with this filter.
  303.      The corners are less sharp with this algorithm.
  304.   */
  305.  
  306.   /* Spread the image! */
  307.  
  308.   gimp_pixel_rgn_init (&dest_rgn, drawable,
  309.                x1, y1, (x2 - x1), (y2 - y1), TRUE, TRUE);
  310.   for (pr = gimp_pixel_rgns_register (1, &dest_rgn);
  311.        pr != NULL;
  312.        pr = gimp_pixel_rgns_process (pr))
  313.     {
  314.       destrow = dest_rgn.data;
  315.  
  316.       for (y = dest_rgn.y; y < (dest_rgn.y + dest_rgn.h); y++)
  317.     {
  318.       dest = destrow;
  319.  
  320.       for (x = dest_rgn.x; x < (dest_rgn.x + dest_rgn.w); x++)
  321.         {
  322.               /* get random angle, x distance, and y distance */
  323.               xdist = (rand () % x_mod_value) - x_sub_value;
  324.               ydist = (rand () % y_mod_value) - y_sub_value;
  325.               angle = (rand () % angle_mod_value) - angle_sub_value;
  326.  
  327.               xi = x + floor(sin(angle)*xdist);
  328.               yi = y + floor(cos(angle)*ydist);
  329.  
  330.               /* Only displace the pixel if it's within the bounds of the image. */
  331.               if ((xi >= 0) && (xi < width) && (yi >= 0) && (yi < height))
  332.                   tile = spread_pixel (drawable, tile,
  333.                        x1, y1, x2, y2, xi, yi,
  334.                        &row, &col, pixel[0]);
  335.           else
  336.               {
  337.               /* Else just copy it */
  338.                   tile = spread_pixel (drawable, tile,
  339.                        x1, y1, x2, y2, x, y,
  340.                        &row, &col, pixel[0]);
  341.               }
  342.  
  343.               for (k = 0; k < bytes; k++)
  344.                   *dest++ = pixel[0][k];
  345.             } /* for */
  346.  
  347.       destrow += dest_rgn.rowstride;;
  348.     } /* for */
  349.  
  350.       progress += dest_rgn.w * dest_rgn.h;
  351.       gimp_progress_update ((double) progress / (double) max_progress);
  352.     }  /* for  */
  353.  
  354.   if (tile)
  355.     gimp_tile_unref (tile, FALSE);
  356.  
  357.   /*  update the region  */
  358.   gimp_drawable_flush (drawable);
  359.   gimp_drawable_merge_shadow (drawable->id, TRUE);
  360.   gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
  361. } /* spread */
  362.  
  363.  
  364. static gint
  365. spread_dialog (gint32        image_ID,
  366.            GimpDrawable *drawable)
  367. {
  368.   GtkWidget *dlg;
  369.   GtkWidget *frame;
  370.   GtkWidget *size;
  371.   GimpUnit   unit;
  372.   gdouble    xres;
  373.   gdouble    yres;
  374.  
  375.   gimp_ui_init ("spread", FALSE);
  376.  
  377.   dlg = gimp_dialog_new (_("Spread"), "spread",
  378.              gimp_standard_help_func, "filters/spread.html",
  379.              GTK_WIN_POS_MOUSE,
  380.              FALSE, TRUE, FALSE,
  381.  
  382.              _("OK"), spread_ok_callback,
  383.              NULL, NULL, NULL, TRUE, FALSE,
  384.              _("Cancel"), gtk_widget_destroy,
  385.              NULL, 1, NULL, FALSE, TRUE,
  386.  
  387.              NULL);
  388.  
  389.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  390.               GTK_SIGNAL_FUNC (gtk_main_quit),
  391.               NULL);
  392.  
  393.   /*  parameter settings  */
  394.   frame = gtk_frame_new (_("Spread Amount"));
  395.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  396.   gtk_container_set_border_width (GTK_CONTAINER (frame), 6);
  397.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), frame, TRUE, TRUE, 0);
  398.  
  399.   /*  Get the image resolution and unit  */
  400.   gimp_image_get_resolution (image_ID, &xres, &yres);
  401.   unit = gimp_image_get_unit (image_ID);
  402.  
  403.   /* sizeentries */
  404.   size = gimp_coordinates_new (unit, "%a", TRUE, FALSE, 75, 
  405.                    GIMP_SIZE_ENTRY_UPDATE_SIZE,
  406.  
  407.                    spvals.spread_amount_x == spvals.spread_amount_y,
  408.                    FALSE,
  409.  
  410.                    _("Horizontal:"), spvals.spread_amount_x, xres,
  411.                    0, MAX (drawable->width, drawable->height),
  412.                    0, 0,
  413.  
  414.                    _("Vertical:"), spvals.spread_amount_y, yres,
  415.                    0, MAX (drawable->width, drawable->height),
  416.                    0, 0);
  417.   gtk_container_set_border_width (GTK_CONTAINER (size), 4);
  418.   gtk_container_add (GTK_CONTAINER (frame), size);
  419.  
  420.   pint.size = size;
  421.  
  422.   gtk_widget_show (size);
  423.   gtk_widget_show (frame);
  424.  
  425.   gtk_widget_show (dlg);
  426.  
  427.   gtk_main ();
  428.   gdk_flush ();
  429.  
  430.   return pint.run;
  431. }
  432.  
  433. static void
  434. spread_ok_callback (GtkWidget *widget,
  435.             gpointer   data)
  436. {
  437.   spvals.spread_amount_x =
  438.     gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (pint.size), 0);
  439.   spvals.spread_amount_y =
  440.     gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (pint.size), 1);
  441.  
  442.   pint.run = TRUE;
  443.  
  444.   gtk_widget_destroy (GTK_WIDGET (data));
  445. }
  446.  
  447. static GimpTile *
  448. spread_pixel (GimpDrawable *drawable,
  449.           GimpTile     *tile,
  450.           gint          x1,
  451.           gint          y1,
  452.           gint          x2,
  453.           gint          y2,
  454.           gint          x,
  455.           gint          y,
  456.           gint         *row,
  457.           gint         *col,
  458.           guchar       *pixel)
  459. {
  460.   static guchar empty_pixel[4] = {0, 0, 0, 0};
  461.   guchar *data;
  462.   gint    b;
  463.  
  464.   if (x >= x1 && y >= y1 && x < x2 && y < y2)
  465.     {
  466.       if ((x >> 6 != *col) || (y >> 6 != *row))
  467.     {
  468.       *col = x >> 6;
  469.       *row = y >> 6;
  470.       if (tile)
  471.         gimp_tile_unref (tile, FALSE);
  472.       tile = gimp_drawable_get_tile (drawable, FALSE, *row, *col);
  473.       gimp_tile_ref (tile);
  474.     }
  475.  
  476.       data = tile->data + tile->bpp * (tile->ewidth * (y % 64) + (x % 64));
  477.     }
  478.   else
  479.     data = empty_pixel;
  480.  
  481.   for (b = 0; b < drawable->bpp; b++)
  482.     pixel[b] = data[b];
  483.  
  484.   return tile;
  485. }
  486.