* number of bits per component and decode array
*
* @param bpc the number of bits per component
*/
private ColorModel createColorModel() {
PDFColorSpace cs = getColorSpace();
if (cs instanceof IndexedColor) {
IndexedColor ics = (IndexedColor) cs;
byte[] components = ics.getColorComponents();
int num = ics.getCount();
// process the decode array
if (decode != null) {
byte[] normComps = new byte[components.length];
// move the components array around
for (int i = 0; i < num; i++) {
byte[] orig = new byte[1];
orig[0] = (byte) i;
float[] res = normalize(orig, null, 0);
int idx = (int) res[0];
normComps[i * 3] = components[idx * 3];
normComps[(i * 3) + 1] = components[(idx * 3) + 1];
normComps[(i * 3) + 2] = components[(idx * 3) + 2];
}
components = normComps;
}
// make sure the size of the components array is 2 ^ numBits
// since if it's not, Java will complain
int correctCount = 1 << getBitsPerComponent();
if (correctCount < num) {
byte[] fewerComps = new byte[correctCount * 3];
System.arraycopy(components, 0, fewerComps, 0, correctCount * 3);
components = fewerComps;
num = correctCount;
}
if (colorKeyMask == null || colorKeyMask.length == 0) {
return new IndexColorModel(getBitsPerComponent(), num, components,
0, false);
} else {
byte[] aComps = new byte[num * 4];
int idx = 0;
for (int i = 0; i < num; i++) {
aComps[idx++] = components[(i * 3)];
aComps[idx++] = components[(i * 3) + 1];
aComps[idx++] = components[(i * 3) + 2];
aComps[idx++] = (byte) 0xFF;
}
for (int i = 0; i < colorKeyMask.length; i += 2) {
for (int j = colorKeyMask[i]; j <= colorKeyMask[i + 1]; j++) {
aComps[(j * 4) + 3] = 0; // make transparent
}
}
return new IndexColorModel(getBitsPerComponent(), num, aComps,
0, true);
}
} else {
int[] bits = new int[cs.getNumComponents()];
for (int i = 0; i < bits.length; i++) {
bits[i] = getBitsPerComponent();
}
return decode != null ?
new DecodeComponentColorModel(cs.getColorSpace(), bits) :
new PdfComponentColorModel(cs.getColorSpace(), bits);
}
}