CutLevelOpImage.java
18.4 KB
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
/*
* ESO Archive
*
* $Id: CutLevelOpImage.java,v 1.2 2009/02/20 23:10:11 abrighto Exp $
*
* who when what
* -------------- ---------- ----------------------------------------
* Allan Brighton 1999/05/03 Created
*/
package jsky.image.operator;
import java.awt.geom.Rectangle2D;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.awt.image.DataBufferShort;
import java.awt.image.DataBufferUShort;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.DataBufferFloat;
import javax.media.jai.ROI;
import javax.media.jai.StatisticsOpImage;
/**
* CutLevelOpImage is an extension of StatisticsOpImage that takes
* a region of interest (ROI), a bad pixel value to ignore, a median value to use
* in place of bad pixels, and a source image, and calculates the low and high
* image cut levels, using a median filter algorithm.
* <p>
* This class currently only works with single banded images.
*/
class CutLevelOpImage extends StatisticsOpImage {
/** bad pixel value */
private double ignore;
/** Median pixel value to use in place of bad pixels */
private double median;
/**
* The operation names
*/
private static final String[] opNames = {
"cutlevel"
};
/**
* Constructs an CutLevelOpImage.
*
* @param source a RenderedImage.
* @param roi The region of interest
* @param ignore ignore any pixels with this value
* @param median median value to replace bad pixels
*/
public CutLevelOpImage(RenderedImage source,
ROI roi, Double ignore, Double median) {
super(source, roi, 0, 0, 1, 1);
this.ignore = ignore;
this.median = median;
}
/**
* Update the low and high cut values for the specified region, using the current parameters.
*
* @param name the name of the statistic to be gathered.
*
* @param source a Raster containing source pixels.
* The dimensions of the Raster will not exceed maxWidth x maxHeight.
*
* @param ar an array of two doubles to hold the low and high cut values (created by createStatistics())
*/
protected void accumulateStatistics(String name, Raster source, Object ar) {
double[] stats = (double[]) ar;
DataBuffer dbuf = source.getDataBuffer();
// clip the region to the intersection of the ROI with the source tile
Rectangle2D rect = roi.getBounds().createIntersection(source.getBounds());
//System.out.println("XXX accumulateStatistics: ROI = " + roi.getBounds() + ", source = " + source.getBounds() + ", intersect = " + rect);
int x0 = Math.max((int) rect.getX() - source.getMinX(), 0);
int y0 = Math.max((int) rect.getY() - source.getMinY(), 0);
int x1 = x0 + (int) rect.getWidth() - 1;
int y1 = y0 + (int) rect.getHeight() - 1;
int w = source.getWidth();
// int h = source.getHeight();
// ignore pixels from the border
if (xPeriod < width / 2 && yPeriod < height / 2) {
x0 += xPeriod;
y0 += yPeriod;
x1 -= xPeriod;
y1 -= yPeriod;
}
// XXX for now, only do the default bank. (How to treat multiple banks?)
switch (dbuf.getDataType()) {
case DataBuffer.TYPE_BYTE:
{
DataBufferByte dataBuffer = (DataBufferByte) source.getDataBuffer();
byte[] data = dataBuffer.getData();
short ignore = (short) this.ignore;
short median = (short) this.median;
getCutLevelsByte(data, ignore, median, x0, y0, x1, y1, w, stats);
}
break;
case DataBuffer.TYPE_SHORT:
{
DataBufferShort dataBuffer = (DataBufferShort) source.getDataBuffer();
short[] data = dataBuffer.getData();
short ignore = (short) this.ignore;
short median = (short) this.median;
getCutLevelsShort(data, ignore, median, x0, y0, x1, y1, w, stats);
}
break;
case DataBuffer.TYPE_USHORT:
{
DataBufferUShort dataBuffer = (DataBufferUShort) source.getDataBuffer();
short[] data = dataBuffer.getData();
int ignore = (int) this.ignore;
int median = (int) this.median;
getCutLevelsUShort(data, ignore, median, x0, y0, x1, y1, w, stats);
}
break;
case DataBuffer.TYPE_INT:
{
DataBufferInt dataBuffer = (DataBufferInt) source.getDataBuffer();
int[] data = dataBuffer.getData();
int ignore = (int) this.ignore;
int median = (int) this.median;
getCutLevelsInt(data, ignore, median, x0, y0, x1, y1, w, stats);
}
break;
case DataBuffer.TYPE_FLOAT:
case DataBuffer.TYPE_DOUBLE:
{
getCutLevels(source.getDataBuffer(), ignore, median, x0, y0, x1, y1, w, stats);
}
break;
default:
throw new IllegalArgumentException("CutLevel not implemented for this data type");
}
}
/**
* Get the median low and high pixel values in the given region and write
* them to the given CutLevel object (Byte version).
*
* A median filter algorithm is used here to calculate suitable cut
* levels for displaying the image.
*
* @param data The image data.
* @param ignore The value of the pixels to ignore
* @param median The value to use for bad pixels (normally: (max+min)/2.)
* @param x0 The coordinates of the area to examine.
* @param y0 The coordinates of the area to examine.
* @param x1 The coordinates of the area to examine.
* @param y1 The coordinates of the area to examine.
* @param w The width of the source image.
* @param stats array to hold the results.
*/
void getCutLevelsByte(byte[] data, short ignore, short median, int x0, int y0, int x1, int y1, int w,
double[] stats) {
int nmed = 7; // length of median filter
int xskip = nmed * 3, yskip = 3;
int i, j, k, l, p;
short tmp, val, lcut, hcut;
short[] medary = new short[nmed];
if (!Double.isNaN(stats[0])) {
lcut = (short) stats[0];
hcut = (short) stats[1];
} else {
lcut = median;
hcut = median;
}
if (x1 - x0 <= nmed || y1 - y0 <= nmed)
return;
for (i = y0; i <= y1; i += yskip) {
for (j = x0; j <= x1; j += xskip) {
p = i * w + j;
// get array for finding meadian
for (k = 0; k < nmed; k++) {
medary[k] = (short) (data[p++] & 0xff);
// ignore ignore pixels
if (medary[k] == ignore) {
medary[k] = median;
}
}
// get meadian value
for (k = 0; k < nmed; k++) {
for (l = k; l < nmed; l++) {
if (medary[k] < medary[l]) {
tmp = medary[l];
medary[l] = medary[k];
medary[k] = tmp;
}
}
}
val = medary[nmed / 2];
// compare meadian with lcut, hcut
if (val < lcut)
lcut = val;
if (val > hcut)
hcut = val;
}
}
stats[0] = lcut;
stats[1] = hcut;
}
/**
* Get the median low and high pixel values in the given region and write
* them to the given array (Short version).
*
* A median filter algorithm is used here to calculate suitable cut
* levels for displaying the image.
*
* @param data The image data.
* @param ignore The value of the ignore pixel, if known.
* @param median The value to use for bad pixels (normally: (max+min)/2.)
* @param x0 The coordinates of the area to examine.
* @param y0 The coordinates of the area to examine.
* @param x1 The coordinates of the area to examine.
* @param y1 The coordinates of the area to examine.
* @param w The width of the source image.
* @param stats array to hold the results.
*/
void getCutLevelsShort(short[] data, short ignore, short median, int x0, int y0, int x1, int y1, int w,
double[] stats) {
int nmed = 7; // length of median filter
int xskip = nmed * 3, yskip = 3; // skip pixels for speed
int i, j, k, l, p;
short tmp, val, lcut, hcut;
short[] medary = new short[nmed];
if (!Double.isNaN(stats[0])) {
lcut = (short) stats[0];
hcut = (short) stats[1];
} else {
lcut = median;
hcut = median;
}
x1 -= nmed;
if (x1 - x0 <= nmed || y1 - y0 <= nmed)
return;
for (i = y0; i <= y1; i += yskip) {
for (j = x0; j <= x1; j += xskip) {
p = i * w + j;
// get array for finding meadian
for (k = 0; k < nmed; k++) {
medary[k] = data[p++];
// ignore ignore pixels
if (medary[k] == ignore) {
medary[k] = median;
}
}
// get meadian value
for (k = 0; k < nmed; k++) {
for (l = k; l < nmed; l++) {
if (medary[k] < medary[l]) {
tmp = medary[l];
medary[l] = medary[k];
medary[k] = tmp;
}
}
}
val = medary[nmed / 2];
// compare meadian with lcut, hcut
if (val < lcut)
lcut = val;
if (val > hcut)
hcut = val;
}
}
stats[0] = lcut;
stats[1] = hcut;
}
/**
* Get the median low and high pixel values in the given region and write
* them to the given array (UShort version).
*
* A median filter algorithm is used here to calculate suitable cut
* levels for displaying the image.
*
* @param data The image data.
* @param ignore The value of the ignore pixel, if known.
* @param median The value to use for bad pixels (normally: (max+min)/2.)
* @param x0 The coordinates of the area to examine.
* @param y0 The coordinates of the area to examine.
* @param x1 The coordinates of the area to examine.
* @param y1 The coordinates of the area to examine.
* @param w The width of the source image.
* @param stats array to hold the results.
*/
void getCutLevelsUShort(short[] data, int ignore, int median, int x0, int y0, int x1, int y1, int w,
double[] stats) {
int nmed = 7; // length of median filter
int xskip = nmed * 3, yskip = 3; // skip pixels for speed
int i, j, k, l, p;
int tmp, val, lcut, hcut;
int[] medary = new int[nmed];
if (!Double.isNaN(stats[0])) {
lcut = (int) stats[0];
hcut = (int) stats[1];
} else {
lcut = median;
hcut = median;
}
if (x1 - x0 <= nmed || y1 - y0 <= nmed)
return;
for (i = y0; i <= y1; i += yskip) {
for (j = x0; j <= x1; j += xskip) {
p = i * w + j;
// get array for finding meadian
for (k = 0; k < nmed; k++) {
medary[k] = data[p++] & 0xffff;
// ignore ignore pixels
if (medary[k] == ignore) {
medary[k] = median;
}
}
// get meadian value
for (k = 0; k < nmed; k++) {
for (l = k; l < nmed; l++) {
if (medary[k] < medary[l]) {
tmp = medary[l];
medary[l] = medary[k];
medary[k] = tmp;
}
}
}
val = medary[nmed / 2];
// compare meadian with lcut, hcut
if (val < lcut)
lcut = val;
if (val > hcut)
hcut = val;
}
}
stats[0] = lcut;
stats[1] = hcut;
}
/**
* Get the median low and high pixel values in the given region and write
* them to the given array (Int version).
*
* A median filter algorithm is used here to calculate suitable cut
* levels for displaying the image.
*
* @param data The image data.
* @param ignore The value of the ignore pixel, if known.
* @param median The value to use for bad pixels (normally: (max+min)/2.)
* @param x0 The coordinates of the area to examine.
* @param y0 The coordinates of the area to examine.
* @param x1 The coordinates of the area to examine.
* @param y1 The coordinates of the area to examine.
* @param w The width of the source image.
* @param stats array to hold the results.
*/
void getCutLevelsInt(int[] data, int ignore, int median, int x0, int y0, int x1, int y1, int w,
double[] stats) {
int nmed = 7; // length of median filter
int xskip = nmed * 3, yskip = 3; // skip pixels for speed
int i, j, k, l, p;
int tmp, val, lcut, hcut;
int[] medary = new int[nmed];
if (!Double.isNaN(stats[0])) {
lcut = (int) stats[0];
hcut = (int) stats[1];
} else {
lcut = median;
hcut = median;
}
if (x1 - x0 <= nmed || y1 - y0 <= nmed)
return;
for (i = y0; i <= y1; i += yskip) {
for (j = x0; j <= x1; j += xskip) {
p = i * w + j;
// get array for finding meadian
for (k = 0; k < nmed; k++) {
medary[k] = data[p++];
// ignore ignore pixels
if (medary[k] == ignore) {
medary[k] = median;
}
}
// get meadian value
for (k = 0; k < nmed; k++) {
for (l = k; l < nmed; l++) {
if (medary[k] < medary[l]) {
tmp = medary[l];
medary[l] = medary[k];
medary[k] = tmp;
}
}
}
val = medary[nmed / 2];
// compare meadian with lcut, hcut
if (val < lcut)
lcut = val;
if (val > hcut)
hcut = val;
}
}
stats[0] = lcut;
stats[1] = hcut;
}
/**
* Get the median low and high pixel values in the given region and write
* them to the given array (Float/Double version).
*
* A median filter algorithm is used here to calculate suitable cut
* levels for displaying the image.
*
* @param data The image data.
* @param ignore The value of the ignore pixel, if known.
* @param median The value to use for bad pixels (normally: (max+min)/2.)
* @param x0 The coordinates of the area to examine.
* @param y0 The coordinates of the area to examine.
* @param x1 The coordinates of the area to examine.
* @param y1 The coordinates of the area to examine.
* @param w The width of the source image.
* @param stats array to hold the results.
*/
void getCutLevels(DataBuffer data, double ignore,
double median, int x0, int y0, int x1, int y1, int w,
double[] stats) {
int nmed = 7; // length of median filter
int xskip = nmed * 3, yskip = 3; // skip pixels for speed
int i, j, k, l, p;
double tmp, val, lcut, hcut;
double[] medary = new double[nmed];
if (!Double.isNaN(stats[0])) {
lcut = stats[0];
hcut = stats[1];
} else {
lcut = median;
hcut = median;
}
if (x1 - x0 <= nmed || y1 - y0 <= nmed)
return;
for (i = y0; i <= y1; i += yskip) {
for (j = x0; j <= x1; j += xskip) {
p = i * w + j;
// get array for finding meadian
for (k = 0; k < nmed; k++) {
medary[k] = data.getElemDouble(p++);
// ignore ignore pixels
if (Double.isNaN(medary[k]) || (medary[k] == ignore)) {
medary[k] = median;
}
}
// get meadian value
for (k = 0; k < nmed; k++) {
for (l = k; l < nmed; l++) {
if (medary[k] < medary[l]) {
tmp = medary[l];
medary[l] = medary[k];
medary[k] = tmp;
}
}
}
val = medary[nmed / 2];
// compare meadian with lcut, hcut
if (val < lcut)
lcut = val;
if (val > hcut)
hcut = val;
}
}
stats[0] = lcut;
stats[1] = hcut;
}
/**
* Returns an object that will be used to gather the named statistic.
*
* @param name the name of the statistic to be gathered.
*/
protected Object createStatistics(java.lang.String name) {
double[] ar = new double[2];
ar[0] = ar[1] = Double.NaN; // initial values are undefined
return ar;
}
/**
* Returns a list of names of statistics understood by this image.
*/
protected String[] getStatisticsNames() {
return opNames;
}
}