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.NaN if no
037     * value is to be ignored.
038     */
039    TiffRasterStatistics(final TiffRasterData raster, final 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        final float[] data = raster.getData();
047        for (final float test : data) {
048            if (Float.isNaN(test)) {
049                nN++;
050                continue;
051            }
052            if (test == excludedValue) {
053                continue;
054            }
055
056            nS++;
057            vSum += test;
058            if (test < vMin) {
059                vMin = test;
060            }
061            if (test > vMax) {
062                vMax = test;
063            }
064        }
065
066        minValue = vMin;
067        maxValue = vMax;
068        nSample = nS;
069        nNull = nN;
070        if (nSample == 0) {
071            meanValue = 0;
072        } else {
073            meanValue = (float) (vSum / nSample);
074        }
075    }
076
077    /**
078     * Get the count of the number of non-null and non-excluded samples in the
079     * collection.
080     *
081     * @return the a positive number, potentially zero
082     */
083    public int getCountOfSamples() {
084        return nSample;
085    }
086
087    /**
088     * Get the count of the number of null samples in the collection.
089     *
090     * @return the a positive number, potentially zero
091     */
092    public int getCountOfNulls() {
093        return nNull;
094    }
095
096    /**
097     * Get the minimum value found in the source data
098     *
099     * @return the minimum value found in the source data
100     */
101    public float getMinValue() {
102        return minValue;
103    }
104
105    /**
106     * Get the maximum value found in the source data
107     *
108     * @return the maximum value found in the source data
109     */
110    public float getMaxValue() {
111        return maxValue;
112    }
113
114    /**
115     * Get the mean value for all sample values in the raster. Null-data values
116     * and excluded values are not considered.
117     *
118     * @return the mean value of the samples
119     */
120    public float getMeanValue() {
121        return meanValue;
122    }
123
124    /**
125     * Indicates if a sample value was set to be deliberately excluded from the
126     * statistics.
127     *
128     * @return true if a value was set for exclusion; otherwise, false
129     */
130    public boolean isAnExcludedValueSet() {
131        return !Float.isNaN(excludedValue);
132    }
133
134    /**
135     * Get the value that was set for exclusion, or a Float&#46;NaN if not was
136     * set.
137     *
138     * @return the excluded value (if any).
139     */
140    public float getExcludedValue() {
141        return excludedValue;
142    }
143}