001/* 002 * Copyright 2010-2015 Institut Pasteur. 003 * 004 * This file is part of Icy. 005 * 006 * Icy is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Icy is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Icy. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package icy.image.colormodel; 020 021import icy.image.lut.LUT; 022import icy.type.DataType; 023 024/** 025 * @author stephane 026 */ 027public class IntColorModel extends IcyColorModel 028{ 029 /** 030 * Create a new IntColorModel 031 * 032 * @param numComponents 033 * number of color component 034 * @param bits 035 */ 036 public IntColorModel(int numComponents, int[] bits) 037 { 038 super(numComponents, DataType.INT, bits); 039 } 040 041 @Override 042 public int getRGB(Object pixel) 043 { 044 final int[] pix = (int[]) pixel; 045 final int[] scaledData = new int[numComponents]; 046 047 for (int comp = 0; comp < numComponents; comp++) 048 scaledData[comp] = (int) colormapScalers[comp].scale(pix[comp]); 049 050 return getIcyColorSpace().toRGBUnnorm(scaledData); 051 } 052 053 /** 054 * Same as getRGB but by using the specified LUT instead of internal one 055 * 056 * @see java.awt.image.ColorModel#getRGB(java.lang.Object) 057 */ 058 @Override 059 public int getRGB(Object pixel, LUT lut) 060 { 061 final int[] pix = (int[]) pixel; 062 final int[] scaledData = new int[numComponents]; 063 064 for (int comp = 0; comp < numComponents; comp++) 065 scaledData[comp] = (int) lut.getLutChannel(comp).getScaler().scale(pix[comp]); 066 067 return lut.getColorSpace().toRGBUnnorm(scaledData); 068 } 069 070 @Override 071 public int[] getComponents(Object pixel, int[] components, int offset) 072 { 073 final int[] result; 074 075 if (components == null) 076 result = new int[offset + numComponents]; 077 else 078 { 079 if ((components.length - offset) < numComponents) 080 throw new IllegalArgumentException("Length of components array < number of components in model"); 081 082 result = components; 083 } 084 085 final int data[] = (int[]) pixel; 086 final int len = data.length; 087 088 for (int i = 0; i < len; i++) 089 result[offset + i] = data[i]; 090 091 return result; 092 } 093 094 @Override 095 public Object getDataElements(int[] components, int offset, Object obj) 096 { 097 if ((components.length - offset) < numComponents) 098 throw new IllegalArgumentException("Component array too small" + " (should be " + numComponents); 099 100 final int[] pixel; 101 final int len = components.length; 102 103 if (obj == null) 104 pixel = new int[numComponents]; 105 else 106 pixel = (int[]) obj; 107 108 for (int i = 0; i < len; i++) 109 pixel[i] = components[offset + i]; 110 111 return pixel; 112 } 113 114 @Override 115 public Object getDataElements(float[] normComponents, int offset, Object obj) 116 { 117 final int[] pixel; 118 119 if (obj == null) 120 pixel = new int[numComponents]; 121 else 122 pixel = (int[]) obj; 123 124 for (int c = 0, nc = offset; c < numComponents; c++, nc++) 125 pixel[c] = (int) normalScalers[c].unscale(normComponents[nc]); 126 127 return pixel; 128 } 129 130 @Override 131 public float[] getNormalizedComponents(Object pixel, float[] normComponents, int normOffset) 132 { 133 final float[] result; 134 135 if (normComponents == null) 136 result = new float[numComponents + normOffset]; 137 else 138 result = normComponents; 139 140 final int[] data = (int[]) pixel; 141 142 for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) 143 result[nc] = (float) normalScalers[c].scale(data[c]); 144 145 return result; 146 } 147}