home *** CD-ROM | disk | FTP | other *** search
/ ftp.sunet.sepub/pictures / 2014.11.ftp.sunet.se-pictures.tar / ftp.sunet.se / pub / pictures / ACiD-artpacks / programs / unix / editors / gimp-plugins-unstable-0_99_23_tar.gz / gimp-plugins-unstable-0_99_23_tar / gimp-plugins-unstable-0.99.23 / figures / figures.c next >
C/C++ Source or Header  |  1998-02-19  |  14KB  |  443 lines

  1. /* The GIMP -- an image manipulation program * Copyright (C) 1995 Spencer
  2.  * Kimball and Peter Mattis * * This program is free software; you can
  3.  * redistribute it and/or modify * it under the terms of the GNU General
  4.  * Public License as published by * the Free Software Foundation; either
  5.  * version 2 of the License, or * (at your option) any later version. * *
  6.  * This program is distributed in the hope that it will be useful, * but
  7.  * WITHOUT ANY WARRANTY; without even the implied warranty of *
  8.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU
  9.  * General Public License for more details. * * You should have received a
  10.  * copy of the GNU General Public License * along with this program; if not,
  11.  * write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA
  12.  * 02139, USA. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <time.h>
  16. #include "libgimp/gimp.h"
  17. #include "gtk/gtk.h"
  18.  
  19. /* Declare local functions. */
  20. static void query(void);
  21. static void run(char *name,
  22.                                 int nparams,
  23.                                 GParam * param,
  24.                                 int *nreturn_vals,
  25.                                 GParam ** return_vals);
  26. static gint dialog();
  27.  
  28. static void doit(GDrawable * drawable);
  29.  
  30. GPlugInInfo PLUG_IN_INFO =
  31. {
  32.     NULL,                                                    /* init_proc */
  33.     NULL,                                                    /* quit_proc */
  34.     query,                                                /* query_proc */
  35.     run,                                                    /* run_proc */
  36. };
  37.  
  38. gint bytes;
  39. gint sx1, sy1, sx2, sy2;
  40. int run_flag = 0;
  41.  
  42. typedef struct {
  43.     gint min_width, max_width;
  44.     gint min_height, max_height;
  45.     float density;
  46. } config;
  47.  
  48. config my_config =
  49. {
  50.     10, 30,                                                /* min_width, max_width */
  51.     10, 30,                                                /* min_height, max_height */
  52.     4                                                            /* density */
  53. };
  54.  
  55.  
  56. MAIN()
  57.  
  58. static void query()
  59. {
  60.     static GParamDef args[] =
  61.     {
  62.         {PARAM_INT32, "run_mode", "Interactive, non-interactive"},
  63.         {PARAM_IMAGE, "image", "Input image (unused)"},
  64.         {PARAM_DRAWABLE, "drawable", "Input drawable"},
  65.         {PARAM_FLOAT, "density", "Density"},
  66.         {PARAM_INT32, "min_width", "Min. width"},
  67.         {PARAM_INT32, "max_width", "Max. width"},
  68.         {PARAM_INT32, "min_height", "Min. height"},
  69.         {PARAM_INT32, "max_height", "Max. height"},
  70.     };
  71.     static GParamDef *return_vals = NULL;
  72.     static int nargs = sizeof(args) / sizeof(args[0]);
  73.     static int nreturn_vals = 0;
  74.  
  75.     gimp_install_procedure("plug_in_figures",
  76.                    "Draws lots of rectangles.",
  77.                    "Can be nice to use as \"textures\".",
  78.                    "Tim Newsome",
  79.                    "Tim Newsome",
  80.                    "1997",
  81.                    "<Image>/Filters/Render/Figures",
  82.                    "RGB*, GRAY*",
  83.                    PROC_PLUG_IN,
  84.                    nargs, nreturn_vals,
  85.                    args, return_vals);
  86. }
  87.  
  88. static void run(char *name, int n_params, GParam * param, int *nreturn_vals,
  89.                                 GParam ** return_vals)
  90. {
  91.     static GParam values[1];
  92.     GDrawable *drawable;
  93.     GRunModeType run_mode;
  94.     GStatusType status = STATUS_SUCCESS;
  95.  
  96.     *nreturn_vals = 1;
  97.     *return_vals = values;
  98.  
  99.     run_mode = param[0].data.d_int32;
  100.  
  101.     if (run_mode == RUN_NONINTERACTIVE) {
  102.         if (n_params != 8) {
  103.             status = STATUS_CALLING_ERROR;
  104.         } else {
  105.             my_config.density = param[3].data.d_float;
  106.             my_config.min_width = param[4].data.d_int32;
  107.             my_config.max_width = param[5].data.d_int32;
  108.             my_config.min_height = param[6].data.d_int32;
  109.             my_config.max_height = param[7].data.d_int32;
  110.         }
  111.     } else {
  112.         /*  Possibly retrieve data  */
  113.         gimp_get_data("plug_in_figures", &my_config);
  114.  
  115.         if (run_mode == RUN_INTERACTIVE) {
  116.             /* Oh boy. We get to do a dialog box, because we can't really expect the
  117.              * user to set us up with the right values using gdb.
  118.              */
  119.             if (!dialog()) {
  120.                 /* The dialog was closed, or something similarly evil happened. */
  121.                 status = STATUS_EXECUTION_ERROR;
  122.             }
  123.         }
  124.     }
  125.  
  126.     if (status == STATUS_SUCCESS) {
  127.         /*  Get the specified drawable  */
  128.         drawable = gimp_drawable_get(param[2].data.d_drawable);
  129.  
  130.         /*  Make sure that the drawable is gray or RGB color  */
  131.         if (gimp_drawable_color(drawable->id) || gimp_drawable_gray(drawable->id)) {
  132.             gimp_progress_init("Drawing Figures...");
  133.             gimp_tile_cache_ntiles(2 * (drawable->width / gimp_tile_width() + 1));
  134.  
  135.             srand(time(NULL));
  136.             doit(drawable);
  137.  
  138.             if (run_mode != RUN_NONINTERACTIVE)
  139.                     gimp_displays_flush();
  140.  
  141.             if (run_mode == RUN_INTERACTIVE)
  142.                 gimp_set_data("plug_in_figures", &my_config, sizeof(my_config));
  143.         } else {
  144.             status = STATUS_EXECUTION_ERROR;
  145.         }
  146.         gimp_drawable_detach(drawable);
  147.     }
  148.  
  149.     values[0].type = PARAM_STATUS;
  150.     values[0].data.d_status = status;
  151. }
  152.  
  153. /*
  154.  * Draws a rectangle in region, using tmp as tempspace, with center x, y,
  155.  * width w, height h, with color color, and opaqueness a.
  156.  */
  157. static void draw_rectangle(guchar * tmp, GPixelRgn * region, int x, int y,
  158.                                                      int w, int h, guchar color[], float a)
  159. {
  160.     int i, j, k;
  161.     int rx1, rx2, ry1, ry2;
  162.  
  163.     rx1 = x - w / 2;
  164.     rx2 = rx1 + w;
  165.     ry1 = y - h / 2;
  166.     ry2 = ry1 + h;
  167.  
  168.     rx1 = rx1 < sx1 ? sx1 : rx1;
  169.     ry1 = ry1 < sy1 ? sy1 : ry1;
  170.     rx2 = rx2 > sx2 ? sx2 : rx2;
  171.     ry2 = ry2 > sy2 ? sy2 : ry2;
  172.  
  173.     gimp_pixel_rgn_get_rect(region, tmp, rx1, ry1, rx2 - rx1, ry2 - ry1);
  174.     for (j = 0; j < (rx2 - rx1); j++) {
  175.         for (k = 0; k < (ry2 - ry1); k++) {
  176.             for (i = 0; i < bytes; i++) {
  177.                 tmp[(j + (rx2 - rx1) * k) * bytes + i] =
  178.                     (float) tmp[(j + (rx2 - rx1) * k) * bytes + i] * (1 - a) +
  179.                     (float) color[i] * a;
  180.             }
  181.         }
  182.     }
  183.     gimp_pixel_rgn_set_rect(region, tmp, rx1, ry1, rx2 - rx1, ry2 - ry1);
  184. }
  185.  
  186. static void doit(GDrawable * drawable)
  187. {
  188.     GPixelRgn srcPR, destPR;
  189.     gint width, height;
  190.     int x, y, w, h;
  191.     int i, j;
  192.     guchar *tmp, *copybuf;
  193.     guchar color[4];
  194.     int objects;
  195.  
  196.     /* Get the input area. This is the bounding box of the selection in
  197.      *  the image (or the entire image if there is no selection). Only
  198.      *  operating on the input area is simply an optimization. It doesn't
  199.      *  need to be done for correct operation. (It simply makes it go
  200.      *  faster, since fewer pixels need to be operated on).
  201.      */
  202.     gimp_drawable_mask_bounds(drawable->id, &sx1, &sy1, &sx2, &sy2);
  203.  
  204.     /* Get the size of the input image. (This will/must be the same
  205.      *  as the size of the output image.
  206.      */
  207.     width = drawable->width;
  208.     height = drawable->height;
  209.     bytes = drawable->bpp;
  210.  
  211.     tmp = (guchar *) malloc(my_config.max_width * my_config.max_height * bytes);
  212.     if (tmp == NULL) {
  213.         return;
  214.     }
  215.     /*  initialize the pixel regions  */
  216.     gimp_pixel_rgn_init(&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);
  217.     gimp_pixel_rgn_init(&destPR, drawable, 0, 0, width, height, TRUE, TRUE);
  218.  
  219.     /* First off, copy the old one to the new one. */
  220.     copybuf = malloc(width * bytes);
  221.     for (i = sy1; i < sy2; i++) {
  222.         gimp_pixel_rgn_get_row(&srcPR, copybuf, sx1, i, width);
  223.         gimp_pixel_rgn_set_row(&destPR, copybuf, sx1, i, width);
  224.     }
  225.     free(copybuf);
  226.  
  227.     objects = my_config.density * (float) (width * height) /
  228.         (float) (((my_config.min_width + my_config.max_width) / 2) *
  229.                          ((my_config.min_height + my_config.max_height)) / 2);
  230.  
  231.     for (i = 0; i < objects; i++) {
  232.         w = my_config.min_width + rand() * (double) (my_config.max_width -
  233.                                                                      my_config.min_width) / (double) RAND_MAX;
  234.         h = my_config.min_height + rand() * (double) (my_config.max_height -
  235.                                                                     my_config.min_height) / (double) RAND_MAX;
  236.         x = sx1 + rand() * (double) width / (double) RAND_MAX;
  237.         y = sy1 + rand() * (double) height / (double) RAND_MAX;
  238.         for (j = 0; j < bytes; j++) {
  239.             color[j] = rand() * 255. / (double) RAND_MAX;
  240.         }
  241.         draw_rectangle(tmp, &destPR, x, y, w, h, color,
  242.                                      rand() / (double) RAND_MAX);
  243.         if (!(i % 64)) {
  244.             gimp_progress_update((double) i / (double) objects);
  245.         }
  246.     }
  247.  
  248.     /*  update the timred region  */
  249.     gimp_drawable_flush(drawable);
  250.     gimp_drawable_merge_shadow(drawable->id, TRUE);
  251.     gimp_drawable_update(drawable->id, sx1, sy1, sx2 - sx1, sy2 - sy1);
  252. }
  253.  
  254. /***************************************************
  255.  * GUI stuff
  256.  */
  257.  
  258. static void close_callback(GtkWidget * widget, gpointer data)
  259. {
  260.     gtk_main_quit();
  261. }
  262.  
  263. static void ok_callback(GtkWidget * widget, gpointer data)
  264. {
  265.     run_flag = 1;
  266.     gtk_widget_destroy(GTK_WIDGET(data));
  267. }
  268.  
  269. static void entry_callback(GtkWidget * widget, gpointer data)
  270. {
  271.     if (data == &my_config.density)
  272.         my_config.density = atof(gtk_entry_get_text(GTK_ENTRY(widget)));
  273.     else if (data == &my_config.min_width)
  274.         my_config.min_width = atoi(gtk_entry_get_text(GTK_ENTRY(widget)));
  275.     else if (data == &my_config.max_width)
  276.         my_config.max_width = atoi(gtk_entry_get_text(GTK_ENTRY(widget)));
  277.     else if (data == &my_config.min_height)
  278.         my_config.min_height = atoi(gtk_entry_get_text(GTK_ENTRY(widget)));
  279.     else if (data == &my_config.max_height)
  280.         my_config.max_height = atoi(gtk_entry_get_text(GTK_ENTRY(widget)));
  281. }
  282.  
  283. static gint dialog()
  284. {
  285.     GtkWidget *dlg;
  286.     GtkWidget *button;
  287.     GtkWidget *label;
  288.     GtkWidget *entry;
  289.     GtkWidget *table;
  290.     gchar buffer[12];
  291.     gchar **argv;
  292.     gint argc;
  293.  
  294.     argc = 1;
  295.     argv = g_new(gchar *, 1);
  296.     argv[0] = g_strdup("plasma");
  297.  
  298.     gtk_init(&argc, &argv);
  299.     gtk_rc_parse (gimp_gtkrc ());
  300.  
  301.     dlg = gtk_dialog_new();
  302.     gtk_window_set_title(GTK_WINDOW(dlg), "Figures");
  303.     gtk_window_position(GTK_WINDOW(dlg), GTK_WIN_POS_MOUSE);
  304.     gtk_signal_connect(GTK_OBJECT(dlg), "destroy",
  305.                                          (GtkSignalFunc) close_callback, NULL);
  306.  
  307.     /*  Action area  */
  308.     button = gtk_button_new_with_label("OK");
  309.     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  310.     gtk_signal_connect(GTK_OBJECT(button), "clicked",
  311.                                          (GtkSignalFunc) ok_callback,
  312.                                          dlg);
  313.     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dlg)->action_area), button, TRUE, TRUE, 0);
  314.     gtk_widget_grab_default(button);
  315.     gtk_widget_show(button);
  316.  
  317.     button = gtk_button_new_with_label("Cancel");
  318.     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  319.     gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
  320.                                                         (GtkSignalFunc) gtk_widget_destroy,
  321.                                                         GTK_OBJECT(dlg));
  322.     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dlg)->action_area), button, TRUE, TRUE, 0);
  323.     gtk_widget_show(button);
  324.  
  325.     /* The main table */
  326.     /* Set its size (y, x) */
  327.     table = gtk_table_new(5, 3, FALSE);
  328.     gtk_container_border_width(GTK_CONTAINER(table), 10);
  329.     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dlg)->vbox), table, TRUE, TRUE, 0);
  330.     gtk_widget_show(table);
  331.  
  332.     gtk_table_set_row_spacings(GTK_TABLE(table), 10);
  333.     gtk_table_set_col_spacings(GTK_TABLE(table), 10);
  334.  
  335.     /*********************
  336.      * The density entry *
  337.      *********************/
  338.     label = gtk_label_new("Density:");
  339.     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
  340.     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2, GTK_FILL, GTK_FILL, 0,
  341.             0);
  342.     gtk_widget_show(label);
  343.  
  344.     entry = gtk_entry_new();
  345.     gtk_table_attach(GTK_TABLE(table), entry, 1, 3, 1, 2,
  346.                                      GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  347.     gtk_widget_set_usize(entry, 50, 0);
  348.     sprintf(buffer, "%0.2f", my_config.density);
  349.     gtk_entry_set_text(GTK_ENTRY(entry), buffer);
  350.     gtk_signal_connect(GTK_OBJECT(entry), "changed",
  351.                                          (GtkSignalFunc) entry_callback,
  352.                                          &my_config.density);
  353.     gtk_widget_show(entry);
  354.  
  355.     /**********************
  356.      * The min/max labels *
  357.      **********************/
  358.     label = gtk_label_new("Min.");
  359.     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
  360.     gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3, GTK_FILL, GTK_FILL, 0,
  361.             0);
  362.     gtk_widget_show(label);
  363.  
  364.     label = gtk_label_new("Max.");
  365.     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
  366.     gtk_table_attach(GTK_TABLE(table), label, 2, 3, 2, 3, GTK_FILL, GTK_FILL, 0,
  367.             0);
  368.     gtk_widget_show(label);
  369.  
  370.     /************************
  371.      * The min_width entry: *
  372.      ************************/
  373.     label = gtk_label_new("Width:");
  374.     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
  375.     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4, GTK_FILL, GTK_FILL, 0,
  376.             0);
  377.     gtk_widget_show(label);
  378.  
  379.     entry = gtk_entry_new();
  380.     gtk_table_attach(GTK_TABLE(table), entry, 1, 2, 3, 4, GTK_EXPAND | GTK_FILL,
  381.             GTK_EXPAND | GTK_FILL, 0, 0);
  382.     gtk_widget_set_usize(entry, 50, 0);
  383.     sprintf(buffer, "%i", my_config.min_width);
  384.     gtk_entry_set_text(GTK_ENTRY(entry), buffer);
  385.     gtk_signal_connect(GTK_OBJECT(entry), "changed",
  386.             (GtkSignalFunc) entry_callback, &my_config.min_width);
  387.     gtk_widget_show(entry);
  388.  
  389.     /************************
  390.      * The max_width entry: *
  391.      ************************/
  392.     entry = gtk_entry_new();
  393.     gtk_table_attach(GTK_TABLE(table), entry, 2, 3, 3, 4, GTK_EXPAND | GTK_FILL,
  394.             GTK_EXPAND | GTK_FILL, 0, 0);
  395.     gtk_widget_set_usize(entry, 50, 0);
  396.     sprintf(buffer, "%i", my_config.max_width);
  397.     gtk_entry_set_text(GTK_ENTRY(entry), buffer);
  398.     gtk_signal_connect(GTK_OBJECT(entry), "changed",
  399.             (GtkSignalFunc) entry_callback, &my_config.max_width);
  400.     gtk_widget_show(entry);
  401.  
  402.     gtk_widget_show(dlg);
  403.  
  404.     /************************
  405.      * The min_height entry: *
  406.      ************************/
  407.     label = gtk_label_new("Height:");
  408.     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
  409.     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 4, 5, GTK_FILL, GTK_FILL, 0,
  410.             0);
  411.     gtk_widget_show(label);
  412.  
  413.     entry = gtk_entry_new();
  414.     gtk_table_attach(GTK_TABLE(table), entry, 1, 2, 4, 5, GTK_EXPAND | GTK_FILL,
  415.             GTK_EXPAND | GTK_FILL, 0, 0);
  416.     gtk_widget_set_usize(entry, 50, 0);
  417.     sprintf(buffer, "%i", my_config.min_height);
  418.     gtk_entry_set_text(GTK_ENTRY(entry), buffer);
  419.     gtk_signal_connect(GTK_OBJECT(entry), "changed",
  420.             (GtkSignalFunc) entry_callback, &my_config.min_height);
  421.     gtk_widget_show(entry);
  422.  
  423.     /************************
  424.      * The max_height entry: *
  425.      ************************/
  426.     entry = gtk_entry_new();
  427.     gtk_table_attach(GTK_TABLE(table), entry, 2, 3, 4, 5, GTK_EXPAND | GTK_FILL,
  428.             GTK_EXPAND | GTK_FILL, 0, 0);
  429.     gtk_widget_set_usize(entry, 50, 0);
  430.     sprintf(buffer, "%i", my_config.max_height);
  431.     gtk_entry_set_text(GTK_ENTRY(entry), buffer);
  432.     gtk_signal_connect(GTK_OBJECT(entry), "changed",
  433.             (GtkSignalFunc) entry_callback, &my_config.max_height);
  434.     gtk_widget_show(entry);
  435.  
  436.     gtk_widget_show(dlg);
  437.  
  438.     gtk_main();
  439.     gdk_flush();
  440.  
  441.     return run_flag;
  442. }
  443.