Java BufferedImage Operation applyGrayDecode(BufferedImage rgbImage, int bitsPerComponent, float[] decode)

Here you can find the source of applyGrayDecode(BufferedImage rgbImage, int bitsPerComponent, float[] decode)

Description

apply Gray Decode

License

Apache License

Declaration

public static BufferedImage applyGrayDecode(BufferedImage rgbImage, int bitsPerComponent, float[] decode) 

Method Source Code

//package com.java2s;
/*/*  w ww  . j  a  va 2s .com*/
 * Copyright 2006-2017 ICEsoft Technologies Canada Corp.
 *
 * Licensed 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.
 */

import java.awt.image.*;

public class Main {
    protected static final int[] GRAY_1_BIT_INDEX_TO_RGB_REVERSED = new int[] { 0xFFFFFFFF, 0xFF000000 };
    protected static final int[] GRAY_1_BIT_INDEX_TO_RGB = new int[] { 0xFF000000, 0xFFFFFFFF };
    protected static final int[] GRAY_2_BIT_INDEX_TO_RGB = new int[] { 0xFF000000, 0xFF555555, 0xFFAAAAAA,
            0xFFFFFFFF };
    protected static final int[] GRAY_4_BIT_INDEX_TO_RGB = new int[] { 0xFF000000, 0xFF111111, 0xFF222222,
            0xFF333333, 0xFF444444, 0xFF555555, 0xFF666666, 0xFF777777, 0xFF888888, 0xFF999999, 0xFFAAAAAA,
            0xFFBBBBBB, 0xFFCCCCCC, 0xFFDDDDDD, 0xFFEEEEEE, 0xFFFFFFFF };

    public static BufferedImage applyGrayDecode(BufferedImage rgbImage, int bitsPerComponent, float[] decode) {
        WritableRaster wr = rgbImage.getRaster();
        int[] cmap = null;
        if (bitsPerComponent == 1) {
            boolean defaultDecode = 0.0f == decode[0];
            cmap = defaultDecode ? GRAY_1_BIT_INDEX_TO_RGB : GRAY_1_BIT_INDEX_TO_RGB_REVERSED;
        } else if (bitsPerComponent == 2) {
            cmap = GRAY_2_BIT_INDEX_TO_RGB;
        } else if (bitsPerComponent == 4) {
            cmap = GRAY_4_BIT_INDEX_TO_RGB;
        }
        ColorModel cm = new IndexColorModel(bitsPerComponent, cmap.length, cmap, 0, false, -1,
                wr.getDataBuffer().getDataType());
        rgbImage = new BufferedImage(cm, wr, false, null);
        return rgbImage;
    }
}

Related

  1. appendImages(BufferedImage leftImage, BufferedImage rightImage)
  2. applyAlphaMask(BufferedImage buffer, BufferedImage alphaMask, int imgHeight)
  3. applyExplicitSMask(BufferedImage baseImage, BufferedImage sMaskImage)
  4. applyGrayscaleMaskToAlpha(BufferedImage image, BufferedImage mask)
  5. applyMask(BufferedImage img, Color keyColor)
  6. applyMaskImage(BufferedImage src, int x, int y, int w, int h)
  7. applyShadow(BufferedImage image)