private static void CalculateMinMaxCut(OSGeo.GDAL.Band rasterBand, double minPercentage, double maxPercentage, out int minCutValue, out int maxCutValue) { double[] minMax = new double[2]; rasterBand.ComputeRasterMinMax(minMax, 0); int min = (int)minMax[0]; int max = (int)minMax[1]; int[] histogram = new int[max - min]; rasterBand.GetHistogram(min, max, max - min, histogram, 1, 0, ProgressFunc, ""); int totalPixels = 0; for (int bucket = 0; bucket < histogram.Length; ++bucket) { totalPixels += histogram[bucket]; } double minCut = totalPixels * minPercentage; minCutValue = int.MaxValue; bool minCutSet = false; double maxCut = totalPixels * maxPercentage; maxCutValue = 0; bool maxCutSet = false; int pixelCount = 0; for (int bucket = 0; bucket < histogram.Length; ++bucket) { pixelCount += histogram[bucket]; if (pixelCount >= minCut && !minCutSet) { minCutValue = bucket + min; minCutSet = true; } if (pixelCount >= maxCut && !maxCutSet) { maxCutValue = bucket + min; maxCutSet = true; } } }