1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.imaging.formats.tiff; 18 19 /** 20 * Collects and stores a set of simple statistics from the input raster. 21 */ 22 public class TiffRasterStatistics { 23 24 private final int nSample; 25 private final int nNull; 26 private final float minValue; 27 private final float maxValue; 28 private final float meanValue; 29 private final float excludedValue; 30 31 /** 32 * Constructs an instance of this class, tabulating results from the input raster data. 33 * 34 * @param raster the input data 35 * @param excludedValue an optional value to ignore; use Float.NaN if no value is to be ignored. 36 */ 37 TiffRasterStatistics(final TiffRasterData raster, final float excludedValue) { 38 this.excludedValue = excludedValue; 39 float vMin = Float.POSITIVE_INFINITY; 40 float vMax = Float.NEGATIVE_INFINITY; 41 double vSum = 0; 42 int nS = 0; 43 int nN = 0; 44 final float[] data = raster.getData(); 45 for (final float test : data) { 46 if (Float.isNaN(test)) { 47 nN++; 48 continue; 49 } 50 if (test == excludedValue) { 51 continue; 52 } 53 54 nS++; 55 vSum += test; 56 if (test < vMin) { 57 vMin = test; 58 } 59 if (test > vMax) { 60 vMax = test; 61 } 62 } 63 64 minValue = vMin; 65 maxValue = vMax; 66 nSample = nS; 67 nNull = nN; 68 if (nSample == 0) { 69 meanValue = 0; 70 } else { 71 meanValue = (float) (vSum / nSample); 72 } 73 } 74 75 /** 76 * Gets the count of the number of null samples in the collection. 77 * 78 * @return the a positive number, potentially zero 79 */ 80 public int getCountOfNulls() { 81 return nNull; 82 } 83 84 /** 85 * Gets the count of the number of non-null and non-excluded samples in the collection. 86 * 87 * @return the a positive number, potentially zero 88 */ 89 public int getCountOfSamples() { 90 return nSample; 91 } 92 93 /** 94 * Gets the value that was set for exclusion, or a Float.NaN if not was set. 95 * 96 * @return the excluded value (if any). 97 */ 98 public float getExcludedValue() { 99 return excludedValue; 100 } 101 102 /** 103 * Gets the maximum value found in the source data 104 * 105 * @return the maximum value found in the source data 106 */ 107 public float getMaxValue() { 108 return maxValue; 109 } 110 111 /** 112 * Gets the mean value for all sample values in the raster. Null-data values and excluded values are not considered. 113 * 114 * @return the mean value of the samples 115 */ 116 public float getMeanValue() { 117 return meanValue; 118 } 119 120 /** 121 * Gets the minimum value found in the source data 122 * 123 * @return the minimum value found in the source data 124 */ 125 public float getMinValue() { 126 return minValue; 127 } 128 129 /** 130 * Indicates if a sample value was set to be deliberately excluded from the statistics. 131 * 132 * @return true if a value was set for exclusion; otherwise, false 133 */ 134 public boolean isAnExcludedValueSet() { 135 return !Float.isNaN(excludedValue); 136 } 137 }