1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
|
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "appenv.h"
#include "actionarea.h"
#include "drawable.h"
#include "general.h"
#include "gdisplay.h"
#include "histogram.h"
#include "image_map.h"
#include "interface.h"
#include "threshold.h"
#define TEXT_WIDTH 45
#define HISTOGRAM_WIDTH 256
#define HISTOGRAM_HEIGHT 150
typedef struct _Threshold Threshold;
struct _Threshold
{
int x, y; /* coords for last mouse click */
};
typedef struct _ThresholdDialog ThresholdDialog;
struct _ThresholdDialog
{
GtkWidget *shell;
GtkWidget *low_threshold_text;
GtkWidget *high_threshold_text;
Histogram *histogram;
GimpDrawable *drawable;
ImageMap image_map;
int color;
int low_threshold;
int high_threshold;
gint preview;
};
/* threshold action functions */
static void threshold_button_press (Tool *, GdkEventButton *, gpointer);
static void threshold_button_release (Tool *, GdkEventButton *, gpointer);
static void threshold_motion (Tool *, GdkEventMotion *, gpointer);
static void threshold_cursor_update (Tool *, GdkEventMotion *, gpointer);
static void threshold_control (Tool *, int, gpointer);
static ThresholdDialog * threshold_new_dialog (void);
static void threshold_preview (ThresholdDialog *);
static void threshold_ok_callback (GtkWidget *, gpointer);
static void threshold_cancel_callback (GtkWidget *, gpointer);
static gint threshold_delete_callback (GtkWidget *, GdkEvent *, gpointer);
static void threshold_preview_update (GtkWidget *, gpointer);
static void threshold_low_threshold_text_update (GtkWidget *, gpointer);
static void threshold_high_threshold_text_update (GtkWidget *, gpointer);
static void *threshold_options = NULL;
static ThresholdDialog *threshold_dialog = NULL;
static void threshold (PixelRegion *, PixelRegion *, void *);
static void threshold_histogram_info (PixelRegion *, PixelRegion *, HistogramValues, void *);
static void threshold_histogram_range (int, int, HistogramValues, void *);
static Argument * threshold_invoker (Argument *);
/* threshold machinery */
static void
threshold (PixelRegion *srcPR,
PixelRegion *destPR,
void *user_data)
{
ThresholdDialog *td;
unsigned char *src, *s;
unsigned char *dest, *d;
int has_alpha, alpha;
int w, h, b;
int value;
td = (ThresholdDialog *) user_data;
h = srcPR->h;
src = srcPR->data;
dest = destPR->data;
has_alpha = (srcPR->bytes == 2 || srcPR->bytes == 4);
alpha = has_alpha ? srcPR->bytes - 1 : srcPR->bytes;
while (h--)
{
w = srcPR->w;
s = src;
d = dest;
while (w--)
{
if (td->color)
{
value = MAX (s[RED_PIX], s[GREEN_PIX]);
value = MAX (value, s[BLUE_PIX]);
value = (value >= td->low_threshold && value <= td->high_threshold ) ? 255 : 0;
}
else
value = (s[GRAY_PIX] >= td->low_threshold && s[GRAY_PIX] <= td->high_threshold) ? 255 : 0;
for (b = 0; b < alpha; b++)
d[b] = value;
if (has_alpha)
d[alpha] = s[alpha];
s += srcPR->bytes;
d += destPR->bytes;
}
src += srcPR->rowstride;
dest += destPR->rowstride;
}
}
static void
threshold_histogram_info (PixelRegion *srcPR,
PixelRegion *maskPR,
HistogramValues values,
void *user_data)
{
ThresholdDialog *td;
unsigned char *src, *s;
unsigned char *mask, *m;
int w, h;
int value;
mask = NULL;
m = NULL;
td = (ThresholdDialog *) user_data;
h = srcPR->h;
src = srcPR->data;
if (maskPR)
mask = maskPR->data;
while (h--)
{
w = srcPR->w;
s = src;
if (maskPR)
m = mask;
while (w--)
{
if (td->color)
{
value = MAX (s[RED_PIX], s[GREEN_PIX]);
value = MAX (value, s[BLUE_PIX]);
}
else
value = s[GRAY_PIX];
if (maskPR)
values[HISTOGRAM_VALUE][value] += (double) *m / 255.0;
else
values[HISTOGRAM_VALUE][value] += 1.0;
s += srcPR->bytes;
if (maskPR)
m += maskPR->bytes;
}
src += srcPR->rowstride;
if (maskPR)
mask += maskPR->rowstride;
}
}
static void
threshold_histogram_range (int start,
int end,
HistogramValues values,
void *user_data)
{
ThresholdDialog *td;
char text[12];
td = (ThresholdDialog *) user_data;
td->low_threshold = start;
td->high_threshold = end;
sprintf (text, "%d", start);
gtk_entry_set_text (GTK_ENTRY (td->low_threshold_text), text);
sprintf (text, "%d", end);
gtk_entry_set_text (GTK_ENTRY (td->high_threshold_text), text);
if (td->preview)
threshold_preview (td);
}
/* threshold action functions */
static void
threshold_button_press (Tool *tool,
GdkEventButton *bevent,
gpointer gdisp_ptr)
{
GDisplay *gdisp;
gdisp = gdisp_ptr;
tool->drawable = gimage_active_drawable (gdisp->gimage);
}
static void
threshold_button_release (Tool *tool,
GdkEventButton *bevent,
gpointer gdisp_ptr)
{
}
static void
threshold_motion (Tool *tool,
GdkEventMotion *mevent,
gpointer gdisp_ptr)
{
}
static void
threshold_cursor_update (Tool *tool,
GdkEventMotion *mevent,
gpointer gdisp_ptr)
{
GDisplay *gdisp;
gdisp = (GDisplay *) gdisp_ptr;
gdisplay_install_tool_cursor (gdisp, GDK_TOP_LEFT_ARROW);
}
static void
threshold_control (Tool *tool,
int action,
gpointer gdisp_ptr)
{
Threshold * thresh;
thresh = (Threshold *) tool->private;
switch (action)
{
case PAUSE :
break;
case RESUME :
break;
case HALT :
if (threshold_dialog)
{
active_tool->preserve = TRUE;
image_map_abort (threshold_dialog->image_map);
active_tool->preserve = FALSE;
threshold_dialog->image_map = NULL;
threshold_cancel_callback (NULL, (gpointer) threshold_dialog);
}
break;
}
}
Tool *
tools_new_threshold ()
{
Tool * tool;
Threshold * private;
/* The tool options */
if (!threshold_options)
threshold_options = tools_register_no_options (THRESHOLD, "Threshold Options");
/* The threshold dialog */
if (!threshold_dialog)
threshold_dialog = threshold_new_dialog ();
else
if (!GTK_WIDGET_VISIBLE (threshold_dialog->shell))
gtk_widget_show (threshold_dialog->shell);
tool = (Tool *) g_malloc (sizeof (Tool));
private = (Threshold *) g_malloc (sizeof (Threshold));
tool->type = THRESHOLD;
tool->state = INACTIVE;
tool->scroll_lock = 1; /* Disallow scrolling */
tool->auto_snap_to = TRUE;
tool->private = (void *) private;
tool->button_press_func = threshold_button_press;
tool->button_release_func = threshold_button_release;
tool->motion_func = threshold_motion;
tool->arrow_keys_func = standard_arrow_keys_func;
tool->cursor_update_func = threshold_cursor_update;
tool->control_func = threshold_control;
tool->preserve = FALSE;
return tool;
}
void
tools_free_threshold (Tool *tool)
{
Threshold * thresh;
thresh = (Threshold *) tool->private;
/* Close the color select dialog */
if (threshold_dialog)
threshold_cancel_callback (NULL, (gpointer) threshold_dialog);
g_free (thresh);
}
void
threshold_initialize (void *gdisp_ptr)
{
GDisplay *gdisp;
gdisp = (GDisplay *) gdisp_ptr;
if (drawable_indexed (gimage_active_drawable (gdisp->gimage)))
{
g_message ("Threshold does not operate on indexed drawables.");
return;
}
/* The threshold dialog */
if (!threshold_dialog)
threshold_dialog = threshold_new_dialog ();
else
if (!GTK_WIDGET_VISIBLE (threshold_dialog->shell))
gtk_widget_show (threshold_dialog->shell);
threshold_dialog->drawable = gimage_active_drawable (gdisp->gimage);
threshold_dialog->color = drawable_color (threshold_dialog->drawable);
threshold_dialog->image_map = image_map_create (gdisp_ptr, threshold_dialog->drawable);
histogram_update (threshold_dialog->histogram,
threshold_dialog->drawable,
threshold_histogram_info,
(void *) threshold_dialog);
histogram_range (threshold_dialog->histogram,
threshold_dialog->low_threshold,
threshold_dialog->high_threshold);
if (threshold_dialog->preview)
threshold_preview (threshold_dialog);
}
/****************************/
/* Select by Color dialog */
/****************************/
/* the action area structure */
static ActionAreaItem action_items[] =
{
{ "OK", threshold_ok_callback, NULL, NULL },
{ "Cancel", threshold_cancel_callback, NULL, NULL }
};
static ThresholdDialog *
threshold_new_dialog ()
{
ThresholdDialog *td;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *label;
GtkWidget *frame;
GtkWidget *toggle;
td = g_malloc (sizeof (ThresholdDialog));
td->preview = TRUE;
td->low_threshold = 127;
td->high_threshold = 255;
/* The shell and main vbox */
td->shell = gtk_dialog_new ();
gtk_window_set_wmclass (GTK_WINDOW (td->shell), "threshold", "Gimp");
gtk_window_set_title (GTK_WINDOW (td->shell), "Threshold");
/* handle the wm close signal */
gtk_signal_connect (GTK_OBJECT (td->shell), "delete_event",
GTK_SIGNAL_FUNC (threshold_delete_callback),
td);
vbox = gtk_vbox_new (FALSE, 2);
gtk_container_border_width (GTK_CONTAINER (vbox), 2);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (td->shell)->vbox), vbox, TRUE, TRUE, 0);
/* Horizontal box for threshold text widget */
hbox = gtk_hbox_new (TRUE, 2);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
label = gtk_label_new ("Threshold Range: ");
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
gtk_widget_show (label);
/* low threshold text */
td->low_threshold_text = gtk_entry_new ();
gtk_entry_set_text (GTK_ENTRY (td->low_threshold_text), "127");
gtk_widget_set_usize (td->low_threshold_text, TEXT_WIDTH, 25);
gtk_box_pack_start (GTK_BOX (hbox), td->low_threshold_text, FALSE, FALSE, 0);
gtk_signal_connect (GTK_OBJECT (td->low_threshold_text), "changed",
(GtkSignalFunc) threshold_low_threshold_text_update,
td);
gtk_widget_show (td->low_threshold_text);
/* high threshold text */
td->high_threshold_text = gtk_entry_new ();
gtk_entry_set_text (GTK_ENTRY (td->high_threshold_text), "255");
gtk_widget_set_usize (td->high_threshold_text, TEXT_WIDTH, 25);
gtk_box_pack_start (GTK_BOX (hbox), td->high_threshold_text, FALSE, FALSE, 0);
gtk_signal_connect (GTK_OBJECT (td->high_threshold_text), "changed",
(GtkSignalFunc) threshold_high_threshold_text_update,
td);
gtk_widget_show (td->high_threshold_text);
gtk_widget_show (hbox);
/* The threshold histogram */
hbox = gtk_hbox_new (TRUE, 2);
gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, FALSE, 0);
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, FALSE, 0);
td->histogram = histogram_create (HISTOGRAM_WIDTH, HISTOGRAM_HEIGHT,
threshold_histogram_range, (void *) td);
gtk_container_add (GTK_CONTAINER (frame), td->histogram->histogram_widget);
gtk_widget_show (td->histogram->histogram_widget);
gtk_widget_show (frame);
gtk_widget_show (hbox);
/* Horizontal box for preview */
hbox = gtk_hbox_new (TRUE, 2);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
/* The preview toggle */
toggle = gtk_check_button_new_with_label ("Preview");
gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (toggle), td->preview);
gtk_box_pack_start (GTK_BOX (hbox), toggle, TRUE, FALSE, 0);
gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
(GtkSignalFunc) threshold_preview_update,
td);
gtk_widget_show (label);
gtk_widget_show (toggle);
gtk_widget_show (hbox);
/* The action area */
action_items[0].user_data = td;
action_items[1].user_data = td;
build_action_area (GTK_DIALOG (td->shell), action_items, 2, 0);
gtk_widget_show (vbox);
gtk_widget_show (td->shell);
/* This code is so far removed from the histogram creation because the
function histogram_range requires a non-NULL drawable, and that
doesn't happen until after the top-level dialog is shown. */
histogram_range (td->histogram, td->low_threshold, td->high_threshold);
return td;
}
static void
threshold_preview (ThresholdDialog *td)
{
if (!td->image_map)
g_message ("threshold_preview(): No image map");
image_map_apply (td->image_map, threshold, (void *) td);
}
static void
threshold_ok_callback (GtkWidget *widget,
gpointer client_data)
{
ThresholdDialog *td;
td = (ThresholdDialog *) client_data;
if (GTK_WIDGET_VISIBLE (td->shell))
gtk_widget_hide (td->shell);
active_tool->preserve = TRUE;
if (!td->preview)
image_map_apply (td->image_map, threshold, (void *) td);
if (td->image_map)
image_map_commit (td->image_map);
active_tool->preserve = FALSE;
td->image_map = NULL;
}
static gint
threshold_delete_callback (GtkWidget *w,
GdkEvent *e,
gpointer client_data)
{
threshold_cancel_callback (w, client_data);
return TRUE;
}
static void
threshold_cancel_callback (GtkWidget *widget,
gpointer client_data)
{
ThresholdDialog *td;
td = (ThresholdDialog *) client_data;
if (GTK_WIDGET_VISIBLE (td->shell))
gtk_widget_hide (td->shell);
if (td->image_map)
{
active_tool->preserve = TRUE;
image_map_abort (td->image_map);
active_tool->preserve = FALSE;
gdisplays_flush ();
}
td->image_map = NULL;
}
static void
threshold_preview_update (GtkWidget *w,
gpointer data)
{
ThresholdDialog *td;
td = (ThresholdDialog *) data;
if (GTK_TOGGLE_BUTTON (w)->active)
{
td->preview = TRUE;
threshold_preview (td);
}
else
td->preview = FALSE;
}
static void
threshold_low_threshold_text_update (GtkWidget *w,
gpointer data)
{
ThresholdDialog *td;
char *str;
int value;
td = (ThresholdDialog *) data;
str = gtk_entry_get_text (GTK_ENTRY (w));
value = BOUNDS (((int) atof (str)), 0, td->high_threshold);
if (value != td->low_threshold)
{
td->low_threshold = value;
histogram_range (td->histogram,
td->low_threshold,
td->high_threshold);
if (td->preview)
threshold_preview (td);
}
}
static void
threshold_high_threshold_text_update (GtkWidget *w,
gpointer data)
{
ThresholdDialog *td;
char *str;
int value;
td = (ThresholdDialog *) data;
str = gtk_entry_get_text (GTK_ENTRY (w));
value = BOUNDS (((int) atof (str)), td->low_threshold, 255);
if (value != td->high_threshold)
{
td->high_threshold = value;
histogram_range (td->histogram,
td->low_threshold,
td->high_threshold);
if (td->preview)
threshold_preview (td);
}
}
/* The threshold procedure definition */
ProcArg threshold_args[] =
{
{ PDB_IMAGE,
"image",
"the image"
},
{ PDB_DRAWABLE,
"drawable",
"the drawable"
},
{ PDB_INT32,
"low_threshold",
"the low threshold value: (0 <= low_threshold <= 255)"
},
{ PDB_INT32,
"high_threshold",
"the high threshold value: (0 <= high_threshold <= 255)"
}
};
ProcRecord threshold_proc =
{
"gimp_threshold",
"Threshold the specified drawable",
"This procedures generates a threshold map of the specified drawable. All pixels between the values of 'low_threshold' and 'high_threshold' are replaced with white, and all other pixels with black.",
"Spencer Kimball & Peter Mattis",
"Spencer Kimball & Peter Mattis",
"1997",
PDB_INTERNAL,
/* Input arguments */
4,
threshold_args,
/* Output arguments */
0,
NULL,
/* Exec method */
{ { threshold_invoker } },
};
static Argument *
threshold_invoker (args)
Argument *args;
{
PixelRegion srcPR, destPR;
int success = TRUE;
ThresholdDialog td;
GImage *gimage;
GimpDrawable *drawable;
int low_threshold;
int high_threshold;
int int_value;
int x1, y1, x2, y2;
void *pr;
drawable = NULL;
low_threshold = 0;
high_threshold = 0;
/* the gimage */
if (success)
{
int_value = args[0].value.pdb_int;
if (! (gimage = gimage_get_ID (int_value)))
success = FALSE;
}
/* the drawable */
if (success)
{
int_value = args[1].value.pdb_int;
drawable = drawable_get_ID (int_value);
if (drawable == NULL || gimage != drawable_gimage (drawable))
success = FALSE;
}
/* make sure the drawable is not indexed color */
if (success)
success = ! drawable_indexed (drawable);
/* low threhsold */
if (success)
{
int_value = args[2].value.pdb_int;
if (int_value >= 0 && int_value < 256)
low_threshold = int_value;
else
success = FALSE;
}
/* high threhsold */
if (success)
{
int_value = args[3].value.pdb_int;
if (int_value >= 0 && int_value < 256)
high_threshold = int_value;
else
success = FALSE;
}
if (success)
success = (low_threshold < high_threshold);
/* arrange to modify the levels */
if (success)
{
td.color = drawable_color (drawable);
td.low_threshold = low_threshold;
td.high_threshold = high_threshold;
/* The application should occur only within selection bounds */
drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
pixel_region_init (&srcPR, drawable_data (drawable), x1, y1, (x2 - x1), (y2 - y1), FALSE);
pixel_region_init (&destPR, drawable_shadow (drawable), x1, y1, (x2 - x1), (y2 - y1), TRUE);
for (pr = pixel_regions_register (2, &srcPR, &destPR); pr != NULL; pr = pixel_regions_process (pr))
threshold (&srcPR, &destPR, (void *) &td);
drawable_merge_shadow (drawable, TRUE);
drawable_update (drawable, x1, y1, (x2 - x1), (y2 - y1));
}
return procedural_db_return_args (&threshold_proc, success);
}
|