Here you can find the source of applyGrayDecode(BufferedImage rgbImage, int bitsPerComponent, float[] decode)
public static BufferedImage applyGrayDecode(BufferedImage rgbImage, int bitsPerComponent, float[] decode)
//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; } }