MinMaxOpImage.java 15.8 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
/*
 * ESO Archive
 *
 * $Id: MinMaxOpImage.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 javax.media.jai.ROI;
import javax.media.jai.StatisticsOpImage;


/**
 * This operation is used to get the min and max pixel values, like the JAI extrema
 * operation, except that it ignores pixels with a given bad pixel value and is designed
 * (currently) only to work with single banded images. The bad pixel value is specified
 * as a double. If the value is Double.NaN, it is ignored. Also, any pixel values
 * that are NaNs are ignored.
 * <p>
 * MinMaxOpImage is an extension of StatisticsOpImage that takes
 * a region of interest (ROI), two integer parameters (xPeriod and yPeriod),
 * a bad pixel value (ignore), and one source image and calculates image min
 * and max values, ignoring any bad pixels.
 */
class MinMaxOpImage extends StatisticsOpImage {

    // Note: about the base class, Daniel Rice <Daniel.Rice@Eng.Sun.COM> wrote:
    // xStart, yStart, xPeriod, and yPeriod define a uniform but sparse set of
    // sample points. You might want to accumulate statistics on several regions
    // of interest, but each time using the same subgrid for consistency.
    // maxWidth and maxHeight don't affect the results, they just set up a contract
    // with the future caller of accumulateStatictics which may be of use to the
    // implementer of a particular statistics op who need to allocate temporary
    // storage.

    /** value of the pixels to ignore */
    private double ignore;

    /**
     *  The statistics operation names
     */
    private static final String[] opNames = {
        "minmax"
    };


    /**
     * Constructs an MinMaxOpImage.
     *
     * @param source    a RenderedImage.
     * @param roi       The region of interest
     * @param xPeriod   skip this many pixels in the X direction
     * @param yPeriod   skip this many pixels in the Y direction
     * @param ignore    ignore any pixels with this value
     */
    public MinMaxOpImage(RenderedImage source,
                         ROI roi, Integer xPeriod, Integer yPeriod,
                         Double ignore) {
        // XXX JAI 1.0.2 super(source, roi, 0, 0, xPeriod.intValue(), yPeriod.intValue(), source.getWidth(), source.getHeight());
        super(source, roi, 0, 0, xPeriod, yPeriod);
        this.ignore = ignore;
    }


    /**
     * Update the min and max 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 min and max 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;
        }

        if (x0 >= x1 || y0 >= y1) return;

        // 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;
                    getMinMaxByte(data, ignore, 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;
                    getMinMaxShort(data, ignore, 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;
                    getMinMaxUShort(data, ignore, 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;
                    getMinMaxInt(data, ignore, x0, y0, x1, y1, w, stats);
                }
                break;

            case DataBuffer.TYPE_FLOAT:
            case DataBuffer.TYPE_DOUBLE:
                {
                    getMinMax(source.getDataBuffer(), ignore, x0, y0, x1, y1, w, stats);
                }
                break;

            default:
                throw new IllegalArgumentException("MinMax not implemented for this data type");
        }
    }

    /**
     * Get the min and max pixel values in the given region and write
     * them to the given array (Byte version).
     *
     * @param data The image data.
     * @param ignore The value of the pixels to ignore
     * @param w The width of the source image.
     * @param stats array to hold the results.
     */
    void getMinMaxByte(byte[] data, short ignore, int x0, int y0, int x1, int y1, int w,
                       double[] stats) {
        short min, max;
        if (!Double.isNaN(stats[0])) {
            min = (short) stats[0];
            max = (short) stats[1];
        } else {
            min = data[0];
            // check for ignores
            if (min == ignore) {
                done:
                for (int i = x0; i <= x1; i += xPeriod) {
                    for (int j = y0; j <= y1; j += yPeriod) {
                        min = data[j * w + i];
                        if (min == ignore)
                            continue;
                        break done;
                    }
                }
            }
            if (min == ignore) {
                min = 0;
            }
            max = min;
        }

        for (int i = x0; i <= x1; i += xPeriod) {
            for (int j = y0; j <= y1; j += yPeriod) {
                byte val = data[j * w + i];
                if (val == ignore)
                    continue;
                if (val < min)
                    min = val;
                else if (val > max) {
                    max = val;
                }
            }
        }
        stats[0] = min;
        stats[1] = max;
    }

    /**
     * Get the min and max pixel values in the given region and write
     * them to the given array (Short version).
     *
     * @param data The image data.
     * @param ignore The value of the pixels to ignore
     * @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 getMinMaxShort(short[] data, short ignore, int x0, int y0, int x1, int y1, int w,
                        double[] stats) {

        short min, max;
        if (!Double.isNaN(stats[0])) {
            min = (short) stats[0];
            max = (short) stats[1];
        } else {
            min = data[0];
            // check for ignores
            if (min == ignore) {
                done:
                for (int i = x0; i <= x1; i += xPeriod) {
                    for (int j = y0; j <= y1; j += yPeriod) {
                        min = data[j * w + i];
                        if (min == ignore)
                            continue;
                        break done;
                    }
                }
            }
            if (min == ignore) {
                min = 0;
            }
            max = min;
        }

        for (int i = x0; i <= x1; i += xPeriod) {
            for (int j = y0; j <= y1; j += yPeriod) {
                short val = data[j * w + i];
                if (val == ignore)
                    continue;
                if (val < min)
                    min = val;
                else if (val > max) {
                    max = val;
                }
            }
        }
        stats[0] = min;
        stats[1] = max;
    }

    /**
     * Get the min and max pixel values in the given region and write
     * them to the given array (UShort version).
     *
     * @param data The image data.
     * @param ignore The value of the pixels to ignore
     * @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 getMinMaxUShort(short[] data, int ignore, int x0, int y0, int x1, int y1, int w,
                         double[] stats) {

        int min, max;
        if (!Double.isNaN(stats[0])) {
            min = (int) stats[0];
            max = (int) stats[1];
        } else {
            min = data[0] & 0xffff;

            // check for ignores
            if (min == ignore) {
                done:
                for (int i = x0; i <= x1; i += xPeriod) {
                    for (int j = y0; j <= y1; j += yPeriod) {
                        min = data[j * w + i];
                        if (min == ignore)
                            continue;
                        break done;
                    }
                }
            }
            if (min == ignore) {
                min = 0;
            }
            max = min;
        }

        for (int i = x0; i <= x1; i += xPeriod) {
            for (int j = y0; j <= y1; j += yPeriod) {
                int val = data[j * w + i] & 0xffff;
                if (val == ignore)
                    continue;
                if (val < min)
                    min = val;
                else if (val > max) {
                    max = val;
                }
            }
        }
        stats[0] = min;
        stats[1] = max;
    }


    /**
     * Get the min and max pixel values in the given region and write
     * them to the given array (Int version).
     *
     * @param data The image data.
     * @param ignore The value of the pixels to ignore
     * @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 getMinMaxInt(int[] data, int ignore, int x0, int y0, int x1, int y1, int w,
                      double[] stats) {
        int min, max;
        if (!Double.isNaN(stats[0])) {
            min = (int) stats[0];
            max = (int) stats[1];
        } else {
            min = data[0];
            // check for ignores
            if (min == ignore) {
                done:
                for (int i = x0; i <= x1; i += xPeriod) {
                    for (int j = y0; j <= y1; j += yPeriod) {
                        min = data[j * w + i];
                        if (min == ignore)
                            continue;
                        break done;
                    }
                }
            }
            if (min == ignore) {
                min = 0;
            }
            max = min;
        }

        for (int i = x0; i <= x1; i += xPeriod) {
            for (int j = y0; j <= y1; j += yPeriod) {
                int val = data[j * w + i];
                if (val == ignore)
                    continue;
                if (val < min)
                    min = val;
                else if (val > max) {
                    max = val;
                }
            }
        }
        stats[0] = min;
        stats[1] = max;
    }

    /**
     * Get the min and max pixel values in the given region and write
     * them to the given array (Float/Double version).
     *
     * @param data The image data.
     * @param ignore The value of the pixels to ignore
     * @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 getMinMax(DataBuffer data, double ignore,
            int x0, int y0, int x1, int y1, int w, double[] stats) {
        double min, max, mean;
        if (!Double.isNaN(stats[0])) {
            min = stats[0];
            max = stats[1];
            mean = stats[2];
        } else {
            min = data.getElemDouble(0);

            // check for NaNs and ignores
            if (Double.isNaN(min) || (min == ignore)) {
                done:
                for (int i = x0; i <= x1; i += xPeriod) {
                    for (int j = y0; j <= y1; j += yPeriod) {
                        min = data.getElemDouble(j * w + i);
                        if (Double.isNaN(min) || (min == ignore))
                            continue;
                        break done;
                    }
                }
            }
            if (Double.isNaN(min) || (min == ignore)) {
                min = 0.0;
            }
            max = mean = min;
        }

        double sum = 0;
        int count = 0;
        for (int i = x0; i <= x1; i += xPeriod) {
            for (int j = y0; j <= y1; j += yPeriod) {
                double val = data.getElemDouble(j * w + i);
                if (Double.isNaN(val) || (val == ignore))
                    continue;
                count++;
                sum += val;
                if (val < min)
                    min = val;
                else if (val > max) {
                    max = val;
                }
            }
        }
        stats[0] = min;
        stats[1] = max;
        stats[2] = ((sum/count) + mean)/2;  // calculate the running average
    }


    /**
     * 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[3];
        ar[0] = ar[1] = ar[2] = Double.NaN; // initial values are undefined
        return ar;
    }

    /**
     * Returns a list of names of statistics understood by this image.
     */
    protected String[] getStatisticsNames() {
        return opNames;
    }
}