private BufferedImage NonICCBIFilter(BufferedImage src, ColorSpace srcColorSpace, BufferedImage dst, ColorSpace dstColorSpace) { int w = src.Width; int h = src.Height; ICC_ColorSpace ciespace = (ICC_ColorSpace)ColorSpace.GetInstance(ColorSpace.CS_CIEXYZ); if (dst == null) { dst = CreateCompatibleDestImage(src, null); dstColorSpace = dst.ColorModel.ColorSpace; } else { if ((h != dst.Height) || (w != dst.Width)) { throw new IllegalArgumentException("Width or height of BufferedImages do not match"); } } Raster srcRas = src.Raster; WritableRaster dstRas = dst.Raster; ColorModel srcCM = src.ColorModel; ColorModel dstCM = dst.ColorModel; int srcNumComp = srcCM.NumColorComponents; int dstNumComp = dstCM.NumColorComponents; bool dstHasAlpha = dstCM.HasAlpha(); bool needSrcAlpha = srcCM.HasAlpha() && dstHasAlpha; ColorSpace[] list; if ((CSList == null) && (ProfileList.Length != 0)) { /* possible non-ICC src, some profiles, possible non-ICC dst */ bool nonICCSrc, nonICCDst; ICC_Profile srcProfile, dstProfile; if (!(srcColorSpace is ICC_ColorSpace)) { nonICCSrc = true; srcProfile = ciespace.Profile; } else { nonICCSrc = false; srcProfile = ((ICC_ColorSpace)srcColorSpace).Profile; } if (!(dstColorSpace is ICC_ColorSpace)) { nonICCDst = true; dstProfile = ciespace.Profile; } else { nonICCDst = false; dstProfile = ((ICC_ColorSpace)dstColorSpace).Profile; } /* make a new transform if needed */ if ((ThisTransform == null) || (ThisSrcProfile != srcProfile) || (ThisDestProfile != dstProfile)) { UpdateBITransform(srcProfile, dstProfile); } // process per scanline float maxNum = 65535.0f; // use 16-bit precision in CMM ColorSpace cs; int iccSrcNumComp; if (nonICCSrc) { cs = ciespace; iccSrcNumComp = 3; } else { cs = srcColorSpace; iccSrcNumComp = srcNumComp; } float[] srcMinVal = new float[iccSrcNumComp]; float[] srcInvDiffMinMax = new float[iccSrcNumComp]; for (int i = 0; i < srcNumComp; i++) { srcMinVal[i] = cs.GetMinValue(i); srcInvDiffMinMax[i] = maxNum / (cs.GetMaxValue(i) - srcMinVal[i]); } int iccDstNumComp; if (nonICCDst) { cs = ciespace; iccDstNumComp = 3; } else { cs = dstColorSpace; iccDstNumComp = dstNumComp; } float[] dstMinVal = new float[iccDstNumComp]; float[] dstDiffMinMax = new float[iccDstNumComp]; for (int i = 0; i < dstNumComp; i++) { dstMinVal[i] = cs.GetMinValue(i); dstDiffMinMax[i] = (cs.GetMaxValue(i) - dstMinVal[i]) / maxNum; } float[] dstColor; if (dstHasAlpha) { int size = ((dstNumComp + 1) > 3) ? (dstNumComp + 1) : 3; dstColor = new float[size]; } else { int size = (dstNumComp > 3) ? dstNumComp : 3; dstColor = new float[size]; } short[] srcLine = new short[w * iccSrcNumComp]; short[] dstLine = new short[w * iccDstNumComp]; Object pixel; float[] color; float[] alpha = null; if (needSrcAlpha) { alpha = new float[w]; } int idx; // process each scanline for (int y = 0; y < h; y++) { // convert src scanline pixel = null; color = null; idx = 0; for (int x = 0; x < w; x++) { pixel = srcRas.GetDataElements(x, y, pixel); color = srcCM.GetNormalizedComponents(pixel, color, 0); if (needSrcAlpha) { alpha[x] = color[srcNumComp]; } if (nonICCSrc) { color = srcColorSpace.ToCIEXYZ(color); } for (int i = 0; i < iccSrcNumComp; i++) { srcLine[idx++] = (short)((color[i] - srcMinVal[i]) * srcInvDiffMinMax[i] + 0.5f); } } // color convert srcLine to dstLine ThisTransform.colorConvert(srcLine, dstLine); // convert dst scanline pixel = null; idx = 0; for (int x = 0; x < w; x++) { for (int i = 0; i < iccDstNumComp; i++) { dstColor[i] = ((float)(dstLine[idx++] & 0xffff)) * dstDiffMinMax[i] + dstMinVal[i]; } if (nonICCDst) { color = srcColorSpace.FromCIEXYZ(dstColor); for (int i = 0; i < dstNumComp; i++) { dstColor[i] = color[i]; } } if (needSrcAlpha) { dstColor[dstNumComp] = alpha[x]; } else if (dstHasAlpha) { dstColor[dstNumComp] = 1.0f; } pixel = dstCM.GetDataElements(dstColor, 0, pixel); dstRas.SetDataElements(x, y, pixel); } } } else { /* possible non-ICC src, possible CSList, possible non-ICC dst */ // process per pixel int numCS; if (CSList == null) { numCS = 0; } else { numCS = CSList.Length; } float[] dstColor; if (dstHasAlpha) { dstColor = new float[dstNumComp + 1]; } else { dstColor = new float[dstNumComp]; } Object spixel = null; Object dpixel = null; float[] color = null; float[] tmpColor; // process each pixel for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { spixel = srcRas.GetDataElements(x, y, spixel); color = srcCM.GetNormalizedComponents(spixel, color, 0); tmpColor = srcColorSpace.ToCIEXYZ(color); for (int i = 0; i < numCS; i++) { tmpColor = CSList[i].FromCIEXYZ(tmpColor); tmpColor = CSList[i].ToCIEXYZ(tmpColor); } tmpColor = dstColorSpace.FromCIEXYZ(tmpColor); for (int i = 0; i < dstNumComp; i++) { dstColor[i] = tmpColor[i]; } if (needSrcAlpha) { dstColor[dstNumComp] = color[srcNumComp]; } else if (dstHasAlpha) { dstColor[dstNumComp] = 1.0f; } dpixel = dstCM.GetDataElements(dstColor, 0, dpixel); dstRas.SetDataElements(x, y, dpixel); } } } return(dst); }
/* color convert a Raster - handles byte, ushort, int, short, float, * or double transferTypes */ private WritableRaster NonICCRasterFilter(Raster src, WritableRaster dst) { if (CSList.Length != 2) { throw new IllegalArgumentException("Destination ColorSpace is undefined"); } if (src.NumBands != CSList[0].NumComponents) { throw new IllegalArgumentException("Numbers of source Raster bands and source color space " + "components do not match"); } if (dst == null) { dst = CreateCompatibleDestRaster(src); } else { if (src.Height != dst.Height || src.Width != dst.Width) { throw new IllegalArgumentException("Width or height of Rasters do not match"); } if (dst.NumBands != CSList[1].NumComponents) { throw new IllegalArgumentException("Numbers of destination Raster bands and destination " + "color space components do not match"); } } if (SrcMinVals == null) { GetMinMaxValsFromColorSpaces(CSList[0], CSList[1]); } SampleModel srcSM = src.SampleModel; SampleModel dstSM = dst.SampleModel; bool srcIsFloat, dstIsFloat; int srcTransferType = src.TransferType; int dstTransferType = dst.TransferType; if ((srcTransferType == DataBuffer.TYPE_FLOAT) || (srcTransferType == DataBuffer.TYPE_DOUBLE)) { srcIsFloat = true; } else { srcIsFloat = false; } if ((dstTransferType == DataBuffer.TYPE_FLOAT) || (dstTransferType == DataBuffer.TYPE_DOUBLE)) { dstIsFloat = true; } else { dstIsFloat = false; } int w = src.Width; int h = src.Height; int srcNumBands = src.NumBands; int dstNumBands = dst.NumBands; float[] srcScaleFactor = null; float[] dstScaleFactor = null; if (!srcIsFloat) { srcScaleFactor = new float[srcNumBands]; for (int i = 0; i < srcNumBands; i++) { if (srcTransferType == DataBuffer.TYPE_SHORT) { srcScaleFactor[i] = (SrcMaxVals[i] - SrcMinVals[i]) / 32767.0f; } else { srcScaleFactor[i] = (SrcMaxVals[i] - SrcMinVals[i]) / ((float)((1 << srcSM.GetSampleSize(i)) - 1)); } } } if (!dstIsFloat) { dstScaleFactor = new float[dstNumBands]; for (int i = 0; i < dstNumBands; i++) { if (dstTransferType == DataBuffer.TYPE_SHORT) { dstScaleFactor[i] = 32767.0f / (DstMaxVals[i] - DstMinVals[i]); } else { dstScaleFactor[i] = ((float)((1 << dstSM.GetSampleSize(i)) - 1)) / (DstMaxVals[i] - DstMinVals[i]); } } } int ys = src.MinY; int yd = dst.MinY; int xs, xd; float sample; float[] color = new float[srcNumBands]; float[] tmpColor; ColorSpace srcColorSpace = CSList[0]; ColorSpace dstColorSpace = CSList[1]; // process each pixel for (int y = 0; y < h; y++, ys++, yd++) { // get src scanline xs = src.MinX; xd = dst.MinX; for (int x = 0; x < w; x++, xs++, xd++) { for (int i = 0; i < srcNumBands; i++) { sample = src.GetSampleFloat(xs, ys, i); if (!srcIsFloat) { sample = sample * srcScaleFactor[i] + SrcMinVals[i]; } color[i] = sample; } tmpColor = srcColorSpace.ToCIEXYZ(color); tmpColor = dstColorSpace.FromCIEXYZ(tmpColor); for (int i = 0; i < dstNumBands; i++) { sample = tmpColor[i]; if (!dstIsFloat) { sample = (sample - DstMinVals[i]) * dstScaleFactor[i]; } dst.SetSample(xd, yd, i, sample); } } } return(dst); }