File: image.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 (185 lines) | stat: -rw-r--r-- 4,530 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
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
#include	<stdio.h>
#include	<string.h>
#include	"extern.hh"

/*
 * Image things (shared by other files through image_ex.h)
 */
double          _image0 = 0.0, _image255 = 0.0;	/* uservalue <-> [0,255]
						 * scale */
double          _image_missing_color_red = 1.0;	/* for missing data */
double          _image_missing_color_green = 1.0; /* for missing data */
double          _image_missing_color_blue = 1.0; /* for missing data */
double          _image_llx, _image_lly, _image_urx, _image_ury;	/* coords */
double         *_imageHist;
int             _image_color_model = 0;	/* 0=bw 1=rgb */
bool            _imageTransform_exists = false;
bool            _imageHist_exists = false;
IMAGE           _image, _imageMask;
unsigned char  *_imageTransform;

static bool     x_image_scale_defined = false;
static bool     y_image_scale_defined = false;
void            show_image_transform(void);	/* for debugging */

void 
show_image_transform()
{
    if (_imageTransform_exists) {
	int             i;
	printf("_imageTransform[0-255] is:\n");
	for (i = 0; i < 256; i++) {
	    printf("im_tr[%d] = %d\n", i, _imageTransform[i]);
	}
    } else {
	printf("_imageTransform[] NOT DEFINED YET\n");
    }
}

bool
define_image_scales(double llx, double lly, double urx, double ury)
{
    if (llx != urx) {
	_image_llx = llx;
	_image_urx = urx;
	x_image_scale_defined = true;
    }
    if (lly != ury) {
	_image_lly = lly;
	_image_ury = ury;
	y_image_scale_defined = true;
    }
    return true;
}

bool
image_scales_defined()
{
    return ((x_image_scale_defined && y_image_scale_defined) ? true : false);
}

/*
 * Blank out image; make it be bw, not color.
 */
bool
blank_image()
{
    register int    i;
    unsigned char   val;
    _image.ras_width = _image.ras_height = _image.ras_length = 0;
    _image_llx = _image_lly = 0.0;
    _image_urx = _image_ury = 0.0;
    _image0 = _image255 = 0.0;	/* used by image_range_exists() */
    _image_color_model = 0;
    x_image_scale_defined = false;
    y_image_scale_defined = false;
    _imageHist_exists = false;
    /*
     * Make transform into an even ramp, for each or R, G, B.  Note that val
     * will wrap around at 255.
     */
    for (val = 0, i = 0; i < 768; i++, val++)
	_imageTransform[i] = val;
    _imageTransform_exists = true;
    return true;
}

bool
blank_imageMask()
{
    _imageMask.ras_width = _imageMask.ras_height = _imageMask.ras_length = 0;
    return true;
}

// Tell if the image range exists (created by `set image range').
bool
image_range_exists()
{
    return ((_image0 != 0.0 || _image255 != 0.0) ? true : false);
}

bool
image_exists()
{
    return ((_image.ras_length > 0) ? true : false);
}

bool
imageMask_exists()
{
    return ((_imageMask.ras_length > 0) ? true : false);
}

/*
 * Calculate histogram of image, normalized to sum to 1 over all image
 * values.  Histogram is defined in _imageHist[]; for example, _imageHist[0]
 * is the normalized fraction of pixels with value 0.
 * 
 * NOTE this code assumes 8bit images; to change that, change the symbol NUM,
 * and change the unsigned char references to whatever makes sense.
 */
#define NUM 256			/* change if not 8 bit images */
bool
calculate_image_histogram()
{
    long            good = 0, mhis[NUM];
    int             i;
    if (!image_exists()) {
	err("no image exists");
	return false;
    }
    if (_imageHist_exists)
	return true;
    for (i = 0; i < NUM; i++)
	mhis[i] = 0;
    if (imageMask_exists()) {
	long            max = _image.ras_width * _image.ras_height;
	unsigned char  *im = _image.image;
	unsigned char  *imMask = _imageMask.image;
	for (i = 0; i < max; i++) {
	    if (!*(imMask)) {
		mhis[*im]++;
		good++;
	    }
	    im++;
	    imMask++;
	}
    } else {
	long            max = _image.ras_width * _image.ras_height;
	unsigned char  *im = _image.image;
	for (i = 0; i < max; i++) {
	    mhis[*im]++;
	    good++;
	    im++;
	}
    }
    for (i = 0; i < NUM; i++)
	_imageHist[i] = (double) mhis[i] / (double) good;
    _imageHist_exists = true;
    return true;
}

#undef NUM

double
image_to_value(int c)
{
    return (double) (_image0 + c * (_image255 - _image0) / 255.0);
}

/* value_to_image() -- conv double to uchar image value */
/*
 * XREF convert_grid_to_imageCmd()
 */
unsigned char
value_to_image(double v)
{
    int             I;
    I = (int) floor(0.5 + (255.0 * (v - _image0) / (_image255 - _image0)));
    if (I < 0)
	return ((unsigned char) 0);
    else if (I > 255)
	return ((unsigned char) 255);
    else
	return ((unsigned char) I);
}