    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.imaging.formats.tiff;
    018
    019/**
    020 * Collects and stores a set of simple statistics from the input raster.
    021 */
    022public class TiffRasterStatistics {
    023
    024    private final int nSample;
    025    private final int nNull;
    026    private final float minValue;
    027    private final float maxValue;
    028    private final float meanValue;
    029    private final float excludedValue;
    030
    031    /**
    032     * Constructs an instance of this class, tabulating results from the input
    033     * raster data.
    034     *
    035     * @param raster the input data
    036     * @param excludedValue an optional value to ignore; use Float&#46;NaN if no
    037     * value is to be ignored.
    038     */
    039    TiffRasterStatistics(TiffRasterData raster, float excludedValue) {
    040        this.excludedValue = excludedValue;
    041        float vMin = Float.POSITIVE_INFINITY;
    042        float vMax = Float.NEGATIVE_INFINITY;
    043        double vSum = 0;
    044        int nS = 0;
    045        int nN = 0;
    046        float[] data = raster.getData();
    047        for (int i = 0; i < data.length; i++) {
    048            float test = data[i];
    049            if (Float.isNaN(test)) {
    050                nN++;
    051                continue;
    052            }
    053            if (test == excludedValue) {
    054                continue;
    055            }
    056
    057            nS++;
    058            vSum += test;
    059            if (test < vMin) {
    060                vMin = test;
    061            }
    062            if (test > vMax) {
    063                vMax = test;
    064            }
    065        }
    066
    067        minValue = vMin;
    068        maxValue = vMax;
    069        nSample = nS;
    070        nNull = nN;
    071        if (nSample == 0) {
    072            meanValue = 0;
    073        } else {
    074            meanValue = (float) (vSum / nSample);
    075        }
    076    }
    077
    078    /**
    079     * Get the count of the number of non-null and non-excluded samples in the
    080     * collection.
    081     *
    082     * @return the a positive number, potentially zero
    083     */
    084    public int getCountOfSamples() {
    085        return nSample;
    086    }
    087
    088    /**
    089     * Get the count of the number of null samples in the collection.
    090     *
    091     * @return the a positive number, potentially zero
    092     */
    093    public int getCountOfNulls() {
    094        return nNull;
    095    }
    096
    097    /**
    098     * Get the minimum value found in the source data
    099     *
    100     * @return the minimum value found in the source data
    101     */
    102    public float getMinValue() {
    103        return minValue;
    104    }
    105
    106    /**
    107     * Get the maximum value found in the source data
    108     *
    109     * @return the maximum value found in the source data
    110     */
    111    public float getMaxValue() {
    112        return maxValue;
    113    }
    114
    115    /**
    116     * Get the mean value for all sample values in the raster. Null-data values
    117     * and excluded values are not considered.
    118     *
    119     * @return the mean value of the samples
    120     */
    121    public float getMeanValue() {
    122        return meanValue;
    123    }
    124
    125    /**
    126     * Indicates if a sample value was set to be deliberately excluded from the
    127     * statistics.
    128     *
    129     * @return true if a value was set for exclusion; otherwise, false
    130     */
    131    public boolean isAnExcludedValueSet() {
    132        return !Float.isNaN(excludedValue);
    133    }
    134
    135    /**
    136     * Get the value that was set for exclusion, or a Float&#46;NaN if not was
    137     * set.
    138     *
    139     * @return the excluded value (if any).
    140     */
    141    public float getExcludedValue() {
    142        return excludedValue;
    143    }
    144}