File: mask.cc

package info (click to toggle)
gri 2.4.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,540 kB
  • ctags: 1,966
  • sloc: cpp: 32,542; lisp: 3,243; perl: 806; makefile: 548; sh: 253
file content (86 lines) | stat: -rw-r--r-- 2,144 bytes parent folder | download
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
#include <string>
#include "gr.hh"
#include "extern.hh"
#include "image_ex.hh"

bool            maskCmd(void);

/* `mask image [to {uservalue .u.}|{imagevalue .i.}]' */
bool
maskCmd()
{
    unsigned int    i, j;
    unsigned int    width, height;
    unsigned char   replacement;
    int             imagevalue;
    double          uservalue;
    if (!image_exists()) {
	demonstrate_command_usage();
	err("First `read image' or `convert grid to image'");
	return false;
    }
    if (_imageMask.ras_width < 1) {
	demonstrate_command_usage();
	err("No image mask exists yet");
	return false;
    }
    if (!_imageTransform_exists) {
	demonstrate_command_usage();
	err("First `set image grayscale'");
	return false;
    }
    /* `mask image [to {uservalue .u.}|{imagevalue .i.}]' */
    switch (_nword) {
    case 2:
	/* `mask image */
	replacement = 0;
	break;
    case 5:
	/* `mask image [to {uservalue .u.}|{imagevalue .i.}]' */
	if (!word_is(2, "to")) {
	    demonstrate_command_usage();
	    err("Third word must be `to'");
	    return false;
	}
	if (word_is(3, "uservalue")) {
	    if (!getdnum(_word[4], &uservalue)) {
		demonstrate_command_usage();
		err("Can't read .uservalue.");
		return false;
	    }
	    replacement = value_to_image(uservalue);
	} else if (word_is(3, "imagevalue")) {
	    if (!getinum(_word[4], &imagevalue)) {
		demonstrate_command_usage();
		err("Can't read .imagevalue.");
		return false;
	    }
	    if (imagevalue < 0 || imagevalue > 255) {
		demonstrate_command_usage();
		err("Require .imagevalue. to be in range 0-255 inclusive");
		return false;
	    }
	    replacement = imagevalue;
	} else {
	    demonstrate_command_usage();
	    err("Fourth word must be `uservalue' or `imagevalue'");
	    return false;
	}
	break;
    default:
	demonstrate_command_usage();
	NUMBER_WORDS_ERROR;
	return false;
    }
    /* replace pixel by pixel */
    height = _image.ras_height;
    width = _image.ras_width;
    for (j = 0; j < height; j++) {
	for (i = 0; i < width; i++) {
	    if (*(_imageMask.image + i * height + j) == 1) {
		*(_image.image + i * height + j) = replacement;
	    }
	}
    }
    return true;
}