/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @author Oleg V. Khaschansky * @version $Revision$ */ package org.apache.harmony.awt.gl.color; import java.awt.color.ColorSpace; import java.awt.color.ICC_Profile; import java.awt.image.DataBuffer; import java.awt.image.Raster; import java.awt.image.SampleModel; import java.awt.image.WritableRaster; /** * This class provides functionality for scaling color data when * ranges of the source and destination color values differs. */ public class ColorScaler { private static final float MAX_SHORT = 0xFFFF; private static final float MAX_SIGNED_SHORT = 0x7FFF; private static final float MAX_XYZ = 1f + (32767f/32768f); // Cached values for scaling color data private float[] channelMinValues = null; private float[] channelMulipliers = null; // for scale private float[] invChannelMulipliers = null; // for unscale int nColorChannels = 0; // For scaling rasters, false if transfer type is double or float boolean isTTypeIntegral = false; /** * Loads scaling data for raster. Note, if profile pf is null, * for non-integral data types multipliers are not initialized. * @param r - raster * @param pf - profile which helps to determine the ranges of the color data */ public void loadScalingData(Raster r, ICC_Profile pf) { boolean isSrcTTypeIntegral = r.getTransferType() != DataBuffer.TYPE_FLOAT && r.getTransferType() != DataBuffer.TYPE_DOUBLE; if (isSrcTTypeIntegral) loadScalingData(r.getSampleModel()); else if (pf != null) loadScalingData(pf); } /** * Use this method only for integral transfer types. * Extracts min/max values from the sample model * @param sm - sample model */ public void loadScalingData(SampleModel sm) { // Supposing integral transfer type isTTypeIntegral = true; nColorChannels = sm.getNumBands(); channelMinValues = new float[nColorChannels]; channelMulipliers = new float[nColorChannels]; invChannelMulipliers = new float[nColorChannels]; boolean isSignedShort = (sm.getTransferType() == DataBuffer.TYPE_SHORT); float maxVal; for (int i=0; i