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 / borderaverage.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  12.4 KB  |  469 lines

  1. /* borderaverage 0.01 - image processing plug-in for the Gimp 1.0 API
  2.  *
  3.  * Copyright (C) 1998 Philipp Klaus (webmaster@access.ch)
  4.  *
  5.  *
  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.
  10.  *
  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.
  15.  *
  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.
  19.  */
  20. #include "config.h"
  21.  
  22. #include <stdlib.h>
  23.  
  24. #include <gtk/gtk.h>
  25.  
  26. #include <libgimp/gimp.h>
  27. #include <libgimp/gimpui.h>
  28.  
  29. #include "libgimp/stdplugins-intl.h"
  30.  
  31.  
  32. /* Declare local functions.
  33.  */
  34. static void      query  (void);
  35. static void      run    (gchar     *name,
  36.              gint       nparams,
  37.              GimpParam    *param,
  38.              gint      *nreturn_vals,
  39.              GimpParam   **return_vals);
  40.  
  41. static void      borderaverage (GimpDrawable *drawable,
  42.                 guchar    *res_r,
  43.                 guchar    *res_g,
  44.                 guchar    *res_b);
  45.  
  46. static gint      borderaverage_dialog (void);
  47.  
  48. static void      add_new_color (gint    bytes,
  49.                 guchar *buffer,
  50.                 gint   *cube,
  51.                 gint    bucket_expo);
  52.  
  53. GimpPlugInInfo PLUG_IN_INFO =
  54. {
  55.   NULL,  /* init  */
  56.   NULL,  /* quit  */
  57.   query, /* query */
  58.   run,   /* run   */
  59. };
  60.  
  61. static gint  borderaverage_thickness       = 3;
  62. static gint  borderaverage_bucket_exponent = 4;
  63.  
  64. struct borderaverage_data
  65. {
  66.   gint  thickness;
  67.   gint  bucket_exponent;
  68. }
  69. borderaverage_data =
  70. {
  71.   3,
  72.   4
  73. };
  74.  
  75.  
  76. MAIN ()
  77.  
  78. static void
  79. query (void)
  80. {
  81.   static GimpParamDef args[] =
  82.   {
  83.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  84.     { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
  85.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
  86.     { GIMP_PDB_INT32, "thickness", "Border size to take in count" },
  87.     { GIMP_PDB_INT32, "bucket_exponent", "Bits for bucket size (default=4: 16 Levels)" },
  88.   };
  89.   static GimpParamDef return_vals[] = 
  90.   {
  91.     { GIMP_PDB_INT32, "num_channels", "Number of color channels returned (always 3)" },
  92.     { GIMP_PDB_INT8ARRAY, "color_vals", "The average color of the specified border"},
  93.   };
  94.   static int nargs = sizeof (args) / sizeof (args[0]);
  95.   static int nreturn_vals = sizeof (return_vals) / sizeof (return_vals[0]);
  96.  
  97.   INIT_I18N();
  98.  
  99.   gimp_install_procedure ("plug_in_borderaverage",
  100.               "Borderaverage",
  101.               "",
  102.               "Philipp Klaus",
  103.               "Internet Access AG",
  104.               "1998",
  105.               N_("<Image>/Filters/Colors/Border Average..."),
  106.               "RGB*",
  107.               GIMP_PLUGIN,
  108.               nargs, nreturn_vals,
  109.               args, return_vals);
  110. }
  111.  
  112. static void
  113. run (gchar   *name,
  114.      gint     nparams,
  115.      GimpParam  *param,
  116.      gint    *nreturn_vals,
  117.      GimpParam **return_vals)
  118. {
  119.   static GimpParam values[3];
  120.   GimpDrawable *drawable;
  121.   GimpRunModeType run_mode;
  122.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  123.   gint8        *result_color;
  124.  
  125.   INIT_I18N_UI();
  126.  
  127.   run_mode = param[0].data.d_int32;
  128.     
  129.   /* get the return memory */
  130.   result_color = (gint8 *) g_new(gint8, 3);
  131.  
  132.   /*    Get the specified drawable    */
  133.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  134.  
  135.   switch (run_mode)
  136.     {
  137.     case GIMP_RUN_INTERACTIVE:
  138.       gimp_get_data ("plug_in_borderaverage", &borderaverage_data);
  139.       borderaverage_thickness       = borderaverage_data.thickness;
  140.       borderaverage_bucket_exponent = borderaverage_data.bucket_exponent;
  141.       if (! borderaverage_dialog ())
  142.     status = GIMP_PDB_EXECUTION_ERROR;
  143.       break;
  144.  
  145.     case GIMP_RUN_NONINTERACTIVE:
  146.       if (nparams != 5)
  147.     status = GIMP_PDB_CALLING_ERROR;
  148.       if (status == GIMP_PDB_SUCCESS)
  149.     {
  150.       borderaverage_thickness       = param[3].data.d_int32;
  151.       borderaverage_bucket_exponent = param[4].data.d_int32;
  152.     }
  153.       break;
  154.  
  155.     case GIMP_RUN_WITH_LAST_VALS:
  156.       gimp_get_data ("plug_in_borderaverage", &borderaverage_data);
  157.       borderaverage_thickness       = borderaverage_data.thickness;
  158.       borderaverage_bucket_exponent = borderaverage_data.bucket_exponent;
  159.       break;
  160.  
  161.     default:
  162.       break;
  163.     }
  164.  
  165.   if (status == GIMP_PDB_SUCCESS)
  166.     {
  167.       /*  Make sure that the drawable is RGB color  */
  168.       if (gimp_drawable_is_rgb (drawable->id))
  169.     {
  170.       gimp_progress_init ( _("Border Average..."));
  171.       borderaverage (drawable,
  172.              &result_color[0], &result_color[1], &result_color[2]);
  173.  
  174.       if (run_mode != GIMP_RUN_NONINTERACTIVE)
  175.         {
  176.           gimp_palette_set_foreground (result_color[0],
  177.                        result_color[1],
  178.                        result_color[2]);
  179.         }
  180.       if (run_mode == GIMP_RUN_INTERACTIVE)
  181.         {
  182.           borderaverage_data.thickness       = borderaverage_thickness;
  183.           borderaverage_data.bucket_exponent = borderaverage_bucket_exponent;
  184.           gimp_set_data ("plug_in_borderaverage",
  185.                  &borderaverage_data, sizeof (borderaverage_data));
  186.         }
  187.     }
  188.       else
  189.     {
  190.       status = GIMP_PDB_EXECUTION_ERROR;
  191.     }
  192.     }
  193.   *nreturn_vals = 3;
  194.   *return_vals = values;
  195.  
  196.   values[0].type = GIMP_PDB_STATUS;
  197.   values[0].data.d_status = status;
  198.     
  199.   values[1].type = GIMP_PDB_INT32;
  200.   values[1].data.d_int32 = 3;
  201.     
  202.   values[2].type = GIMP_PDB_INT8ARRAY;
  203.   values[2].data.d_int8array = result_color;
  204.  
  205.   gimp_drawable_detach (drawable);
  206. }
  207.  
  208. static void
  209. borderaverage (GimpDrawable *drawable, 
  210.            guchar    *res_r, 
  211.            guchar    *res_g, 
  212.            guchar    *res_b) 
  213. {
  214.   gint         width;
  215.   gint         height;
  216.   gint         x1, x2, y1, y2;
  217.   gint         bytes;
  218.   gint         max;
  219.  
  220.   guchar       r, g, b;
  221.  
  222.   guchar      *buffer;
  223.   gint         bucket_num, bucket_expo, bucket_rexpo;
  224.  
  225.   gint        *cube;
  226.  
  227.   gint         row, col, i,j,k; /* index variables */
  228.  
  229.   GimpPixelRgn    myPR;
  230.  
  231.   /* allocate and clear the cube before */
  232.   bucket_expo = borderaverage_bucket_exponent;
  233.   bucket_rexpo = 8 - bucket_expo;
  234.   cube = g_new (gint, 1 << (bucket_rexpo * 3));
  235.   bucket_num = 1 << bucket_rexpo;
  236.         
  237.   for (i = 0; i < bucket_num; i++)
  238.     {
  239.       for (j = 0; j < bucket_num; j++)
  240.     {
  241.       for (k = 0; k < bucket_num; k++)
  242.         {
  243.           cube[(i << (bucket_rexpo << 1)) + (j << bucket_rexpo) + k] = 0;
  244.         }
  245.     }
  246.     }
  247.  
  248.   /*  Get the input area. This is the bounding box of the selection in
  249.    *  the image (or the entire image if there is no selection). Only
  250.    *  operating on the input area is simply an optimization. It doesn't
  251.    *  need to be done for correct operation. (It simply makes it go
  252.    *  faster, since fewer pixels need to be operated on).
  253.    */
  254.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  255.  
  256.   /*  Get the size of the input image. (This will/must be the same
  257.    *  as the size of the output image.
  258.    */
  259.   width = drawable->width;
  260.   height = drawable->height;
  261.   bytes = drawable->bpp;
  262.  
  263.   /*  allocate row buffer  */
  264.   buffer = g_new (guchar, (x2 - x1) * bytes);
  265.  
  266.   /*  initialize the pixel regions  */
  267.   gimp_pixel_rgn_init (&myPR, drawable, 0, 0, width, height, FALSE, FALSE);
  268.  
  269.   /*  loop through the rows, performing our magic*/
  270.   for (row = y1; row < y2; row++)
  271.     {
  272.       gimp_pixel_rgn_get_row (&myPR, buffer, x1, row, (x2-x1));
  273.     
  274.       if (row < y1 + borderaverage_thickness ||
  275.       row >= y2 - borderaverage_thickness)
  276.     {
  277.       /* add the whole row */
  278.       for (col = 0; col < ((x2 - x1) * bytes); col += bytes)
  279.         {
  280.           add_new_color (bytes, &buffer[col], cube, bucket_expo);
  281.         }
  282.     }
  283.       else
  284.     {
  285.       /* add the left border */
  286.       for (col = 0; col < (borderaverage_thickness * bytes); col += bytes)
  287.         {
  288.           add_new_color (bytes, &buffer[col], cube, bucket_expo);
  289.         }
  290.       /* add the right border */
  291.       for (col = ((x2 - x1 - borderaverage_thickness) * bytes);
  292.            col < ((x2 - x1) * bytes); col += bytes)
  293.         {
  294.           add_new_color (bytes, &buffer[col], cube, bucket_expo);
  295.         }
  296.     }
  297.  
  298.       if ((row % 5) == 0)
  299.     gimp_progress_update ((double) row / (double) (y2 - y1));
  300.     }
  301.  
  302.   max = 0; r = 0; g = 0; b = 0;
  303.  
  304.   /* get max of cube */
  305.   for (i = 0; i < bucket_num; i++)
  306.     {
  307.       for (j = 0; j < bucket_num; j++)
  308.     {
  309.       for (k = 0; k < bucket_num; k++)
  310.         {
  311.           if (cube[(i << (bucket_rexpo << 1)) +
  312.               (j << bucket_rexpo) + k] > max)
  313.         {
  314.           max = cube[(i << (bucket_rexpo << 1)) +
  315.                 (j << bucket_rexpo) + k];
  316.           r = (i<<bucket_expo) + (1<<(bucket_expo - 1));
  317.           g = (j<<bucket_expo) + (1<<(bucket_expo - 1));
  318.           b = (k<<bucket_expo) + (1<<(bucket_expo - 1));
  319.         }
  320.         }
  321.     }
  322.     }
  323.  
  324.   /* return the color */
  325.   *res_r = r;
  326.   *res_g = g;
  327.   *res_b = b;
  328.  
  329.   g_free (buffer);
  330.   g_free (cube);
  331. }
  332.  
  333. static void 
  334. add_new_color (gint    bytes, 
  335.            guchar *buffer, 
  336.            gint   *cube, 
  337.            gint    bucket_expo) 
  338. {
  339.   guchar r, g, b;
  340.   gint     bucket_rexpo;
  341.  
  342.   bucket_rexpo = 8 - bucket_expo;
  343.   r = buffer[0] >>bucket_expo;
  344.   if (bytes > 1)
  345.     {
  346.       g = buffer[1] >>bucket_expo;
  347.     }
  348.   else
  349.     {
  350.       g = 0;
  351.     }
  352.   if (bytes > 2)
  353.     {
  354.       b = buffer[2] >>bucket_expo;
  355.     }
  356.   else
  357.     {
  358.       b = 0;
  359.     }
  360.   cube[(r << (bucket_rexpo << 1)) + (g << bucket_rexpo) + b]++;
  361. }
  362.  
  363. static gboolean run_flag = FALSE;
  364.  
  365. static void
  366. borderaverage_ok_callback (GtkWidget *widget,
  367.                gpointer   data)
  368. {
  369.   run_flag = TRUE;
  370.  
  371.   gtk_widget_destroy (GTK_WIDGET (data));
  372. }
  373.  
  374. static gint 
  375. borderaverage_dialog (void)
  376. {
  377.   GtkWidget *dlg;
  378.   GtkWidget *frame;
  379.   GtkWidget *vbox;
  380.   GtkWidget *hbox;
  381.   GtkWidget *label;
  382.   GtkWidget *spinbutton;
  383.   GtkObject *adj;
  384.   GtkWidget *menu;
  385.  
  386.   gimp_ui_init ("borderaverage", FALSE);
  387.  
  388.   dlg = gimp_dialog_new (_("Borderaverage"), "borderaverage",
  389.              gimp_standard_help_func, "filters/borderaverage.html",
  390.              GTK_WIN_POS_MOUSE,
  391.              FALSE, TRUE, FALSE,
  392.  
  393.              _("OK"), borderaverage_ok_callback,
  394.              NULL, NULL, NULL, TRUE, FALSE,
  395.              _("Cancel"), gtk_widget_destroy,
  396.              NULL, 1, NULL, FALSE, TRUE,
  397.  
  398.              NULL);
  399.  
  400.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  401.               GTK_SIGNAL_FUNC (gtk_main_quit),
  402.               NULL);
  403.  
  404.   vbox = gtk_vbox_new (FALSE, 4);
  405.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
  406.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox, TRUE, TRUE, 0);
  407.   gtk_widget_show (vbox);
  408.  
  409.   frame = gtk_frame_new (_("Border Size"));
  410.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  411.   gtk_widget_show (frame);
  412.  
  413.   hbox = gtk_hbox_new (FALSE, 4);
  414.   gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
  415.   gtk_container_add (GTK_CONTAINER (frame), hbox);
  416.   gtk_widget_show (hbox);
  417.  
  418.   label = gtk_label_new (_("Thickness:"));
  419.   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  420.   gtk_widget_show (label);
  421.  
  422.   spinbutton = gimp_spin_button_new (&adj, borderaverage_thickness,
  423.                      0, 256, 1, 5, 0, 0, 0);
  424.   gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  425.   gtk_widget_show (spinbutton);
  426.  
  427.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  428.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  429.               &borderaverage_thickness);
  430.  
  431.   frame = gtk_frame_new (_("Number of Colors"));
  432.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  433.   gtk_widget_show (frame);
  434.  
  435.   hbox = gtk_hbox_new (FALSE, 4);
  436.   gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
  437.   gtk_container_add (GTK_CONTAINER (frame), hbox);
  438.   gtk_widget_show (hbox);
  439.  
  440.   label = gtk_label_new (_("Bucket Size:"));
  441.   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  442.   gtk_widget_show (label);
  443.  
  444.   menu = gimp_option_menu_new2 (FALSE, gimp_menu_item_update,
  445.                 &borderaverage_bucket_exponent,
  446.                 (gpointer) borderaverage_bucket_exponent,
  447.  
  448.                 _("1 (nonsense?)"),   (gpointer) 0, NULL,
  449.                 "2",                  (gpointer) 1, NULL,
  450.                 "4",                  (gpointer) 2, NULL,
  451.                 "8",                  (gpointer) 3, NULL,
  452.                 "16",                 (gpointer) 4, NULL,
  453.                 "32",                 (gpointer) 5, NULL,
  454.                 "64",                 (gpointer) 6, NULL,
  455.                 "128",                (gpointer) 7, NULL,
  456.                 _("256 (nonsense?)"), (gpointer) 8, NULL,
  457.  
  458.                 NULL);
  459.   gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  460.   gtk_widget_show (menu);
  461.  
  462.   gtk_widget_show (dlg);
  463.  
  464.   gtk_main ();
  465.   gdk_flush ();
  466.  
  467.   return run_flag;
  468. }
  469.