in endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/RecoloredImage.java [194:375]
static RenderedImage stretchColorRamp(final ImageProcessor processor, RenderedImage source,
final Map<String,?> modifiers)
{
/*
* Images having more than one band (without any band marked as the single band to show) are probably
* RGB images. It would be possible to stretch the Red, Green and Blue bands separately, but current
* implementation don't do that because we do not have yet a clear use case.
*/
final int visibleBand = ImageUtilities.getVisibleBand(source);
if (visibleBand < 0) {
return source;
}
/*
* Main use case: color model is (probably) an IndexColorModel or ScaledColorModel instance,
* or something we can handle in the same way.
*/
RenderedImage statsSource = source;
Statistics[] statsAllBands = null;
Statistics statistics = null;
Shape areaOfInterest = null;
Number[] nodataValues = null;
SampleDimension range = null;
double minimum = Double.NaN;
double maximum = Double.NaN;
double deviations = Double.POSITIVE_INFINITY;
/*
* Extract and validate parameter values.
* No calculation started at this stage.
*/
if (modifiers != null) {
final Number minValue = Containers.property(modifiers, "minimum", Number.class);
final Number maxValue = Containers.property(modifiers, "maximum", Number.class);
if (minValue != null) minimum = minValue.doubleValue();
if (maxValue != null) maximum = maxValue.doubleValue();
if (minimum >= maximum) {
throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalRange_2, minValue, maxValue));
}
{ // For keeping `value` in local scope.
final Number value = Containers.property(modifiers, "multStdDev", Number.class);
if (value != null) {
deviations = value.doubleValue();
ArgumentChecks.ensureStrictlyPositive("multStdDev", deviations);
}
}
areaOfInterest = Containers.property(modifiers, "areaOfInterest", Shape.class);
Object value = modifiers.get("nodataValues");
if (value != null) {
if (value instanceof Number) {
nodataValues = new Number[] {(Number) value};
} else if (value instanceof Number[]) {
nodataValues = (Number[]) value;
} else {
throw illegalPropertyType(modifiers, "nodataValues", value);
}
}
value = modifiers.get("statistics");
if (value != null) {
if (value instanceof RenderedImage) {
statsSource = (RenderedImage) value;
} else if (value instanceof Statistics) {
statistics = (Statistics) value;
} else if (value instanceof Statistics[]) {
// Undocumented: one element per band, will keep only the visible band.
statsAllBands = (Statistics[]) value;
} else {
throw illegalPropertyType(modifiers, "statistics", value);
}
}
value = modifiers.get("sampleDimensions");
if (value != null) {
range = getSampleDimension(value, visibleBand);
if (range == null) {
throw illegalPropertyType(modifiers, "sampleDimensions", value);
}
}
}
/*
* If minimum and maximum values were not explicitly specified, compute them from statistics.
* If the range is not valid, then the image will be silently returned as-is.
*/
if (Double.isNaN(minimum) || Double.isNaN(maximum)) {
if (statistics == null) {
if (statsAllBands == null) {
final var sampleFilters = new DoubleUnaryOperator[visibleBand + 1];
sampleFilters[visibleBand] = ImageProcessor.filterNodataValues(nodataValues);
statsAllBands = processor.valueOfStatistics(statsSource, areaOfInterest, sampleFilters);
}
if (statsAllBands != null && visibleBand < statsAllBands.length) {
statistics = statsAllBands[visibleBand];
}
}
if (statistics != null) {
deviations *= statistics.standardDeviation(true);
final double mean = statistics.mean();
if (Double.isNaN(minimum)) minimum = Math.max(statistics.minimum(), mean - deviations);
if (Double.isNaN(maximum)) maximum = Math.min(statistics.maximum(), mean + deviations);
}
}
if (!(minimum < maximum)) { // Use ! for catching NaN.
return source;
}
/*
* Finished to collect information. Derive a new color model from the existing one.
*/
final ColorModel cm;
if (source.getColorModel() instanceof IndexColorModel) {
/*
* Get the range of indices of RGB values that can be used for interpolations.
* We want to exclude qualitative categories (no data, clouds, forests, etc.).
* In the vast majority of cases, we have at most one quantitative category.
* But if there is 2 or more, then we select the one having largest intersection
* with the [minimum … maximum] range.
*/
final var icm = (IndexColorModel) source.getColorModel();
final int size = icm.getMapSize();
int validMin = 0;
int validMax = size - 1; // Inclusive.
if (range == null) {
range = getSampleDimension(source.getProperty(PlanarImage.SAMPLE_DIMENSIONS_KEY), visibleBand);
}
if (range != null) {
double span = 0;
for (final Category category : range.getCategories()) {
if (category.isQuantitative()) {
final NumberRange<?> r = category.getSampleRange();
final double min = Math.max(r.getMinDouble(true), 0);
final double max = Math.min(r.getMaxDouble(true), size - 1);
final double s = Math.min(max, maximum) - Math.max(min, minimum); // Intersection.
if (s > span) {
validMin = (int) min;
validMax = (int) max;
span = s;
}
}
}
}
/*
* Create a copy of RGB codes and replace values in the range of the quantitative category.
* Values for other categories (qualitative) are left unmodified.
*/
final int start = Math.max((int) minimum, validMin);
final int end = Math.min((int) maximum, validMax); // Inclusive.
final int[] ARGB = new int[size];
icm.getRGBs(ARGB); // Initialize to a copy of current colors.
Arrays.fill(ARGB, validMin, start, icm.getRGB(validMin)); // Part of quantitative category outside the new range.
Arrays.fill(ARGB, end+1, validMax+1, icm.getRGB(validMax));
final float scale = (float) ((validMax - validMin) / (maximum - minimum));
for (int i = start; i <= end; i++) {
ARGB[i] = icm.getRGB(Math.round((i - start) * scale) + validMin);
}
final SampleModel sm = source.getSampleModel();
cm = ColorModelFactory.createIndexColorModel(null, 0, sm.getNumBands(),
visibleBand, ARGB, icm.hasAlpha(), icm.getTransparentPixel());
} else {
/*
* Wraps the given image with its colors ramp scaled between the given bounds. If the given image is
* already using a color ramp for the given range of values, then that image is returned unchanged.
*/
final SampleModel sm = source.getSampleModel();
cm = ColorModelFactory.createGrayScale(sm.getDataType(), sm.getNumBands(), visibleBand, minimum, maximum);
}
/*
* Verify if an existing ancestor already have the specified color model.
* If not, built the new `RecoloredImage` here.
*/
for (;;) {
if (cm.equals(source.getColorModel())) {
if (source instanceof RecoloredImage) {
final var colored = (RecoloredImage) source;
if (colored.minimum != minimum || colored.maximum != maximum) {
continue;
}
}
return source;
} else if (source instanceof RecoloredImage) {
source = ((RecoloredImage) source).source;
} else {
break;
}
}
return ImageProcessor.unique(new RecoloredImage(source, cm, minimum, maximum));
}