private BufferedImage CreateCompatibleDestImage(BufferedImage src, ColorModel destCM, ColorSpace destCS) { BufferedImage image; if (destCM == null) { ColorModel srcCM = src.ColorModel; int nbands = destCS.NumComponents; bool hasAlpha = srcCM.HasAlpha(); if (hasAlpha) { nbands += 1; } int[] nbits = new int[nbands]; for (int i = 0; i < nbands; i++) { nbits[i] = 8; } destCM = new ComponentColorModel(destCS, nbits, hasAlpha, srcCM.AlphaPremultiplied, srcCM.Transparency, DataBuffer.TYPE_BYTE); } int w = src.Width; int h = src.Height; image = new BufferedImage(destCM, destCM.CreateCompatibleWritableRaster(w, h), destCM.AlphaPremultiplied, null); return(image); }
/// <summary> /// Creates a zeroed destination image with the correct size and number of /// bands. If destCM is <code>null</code>, an appropriate /// <code>ColorModel</code> will be used. </summary> /// <param name="src"> Source image for the filter operation. </param> /// <param name="destCM"> the destination's <code>ColorModel</code>, which /// can be <code>null</code>. </param> /// <returns> a filtered destination <code>BufferedImage</code>. </returns> public virtual BufferedImage CreateCompatibleDestImage(BufferedImage src, ColorModel destCM) { BufferedImage image; int w = src.Width; int h = src.Height; int transferType = DataBuffer.TYPE_BYTE; if (destCM == null) { ColorModel cm = src.ColorModel; Raster raster = src.Raster; if (cm is ComponentColorModel) { DataBuffer db = raster.DataBuffer; bool hasAlpha = cm.HasAlpha(); bool isPre = cm.AlphaPremultiplied; int trans = cm.Transparency; int[] nbits = null; if (Ltable is ByteLookupTable) { if (db.DataType == db.TYPE_USHORT) { // Dst raster should be of type byte if (hasAlpha) { nbits = new int[2]; if (trans == cm.BITMASK) { nbits[1] = 1; } else { nbits[1] = 8; } } else { nbits = new int[1]; } nbits[0] = 8; } // For byte, no need to change the cm } else if (Ltable is ShortLookupTable) { transferType = DataBuffer.TYPE_USHORT; if (db.DataType == db.TYPE_BYTE) { if (hasAlpha) { nbits = new int[2]; if (trans == cm.BITMASK) { nbits[1] = 1; } else { nbits[1] = 16; } } else { nbits = new int[1]; } nbits[0] = 16; } } if (nbits != null) { cm = new ComponentColorModel(cm.ColorSpace, nbits, hasAlpha, isPre, trans, transferType); } } image = new BufferedImage(cm, cm.CreateCompatibleWritableRaster(w, h), cm.AlphaPremultiplied, null); } else { image = new BufferedImage(destCM, destCM.CreateCompatibleWritableRaster(w, h), destCM.AlphaPremultiplied, null); } return(image); }