ImageIO APIのjaiのtiff実装に問題がありましたが、同じように聞こえます(これが役立つかどうかはわかりません)。
基本的に、カラーモデルを「シングルピクセルパックサンプルモデル」に変換します:P
これは私のコードではありません、私はそれを信用していません、私はそれをしばらく前にネットで見つけました、そして私はどこを思い出せないのか心配です(私はそれを検索しようとしましたが、適切な参照を見つけられませんでした。
/*******************************************************************************
*
* It seems that SinglePixelPackedSampleModel is the only fast mode when a
* color profile is converted. This is probably a bug (that has nothing to do
* with bugs 4886071 and 4705399).
* Note that grayscale images (TYPE_GRAY) are not converted.
*
******************************************************************************/
public static BufferedImage convertToSinglePixelPackedSampleModel(BufferedImage image) {
long time = System.currentTimeMillis();
WritableRaster sourceRaster = image.getRaster();
ColorModel colorModel = image.getColorModel();
ICC_ColorSpace colorSpace = (ICC_ColorSpace) colorModel.getColorSpace();
final SampleModel ssmd = sourceRaster.getSampleModel();
if (colorSpace.getType() == ColorSpace.TYPE_GRAY) {
logger.info(">>>> TYPE_GRAY, not converting");
} else if (!(ssmd instanceof PixelInterleavedSampleModel)) {
logger.info(">>>> sourceSampleModel is " + ssmd.getClass() + ", not converting");
} else {
PixelInterleavedSampleModel sourceSampleModel = (PixelInterleavedSampleModel) ssmd;
int[] bitMasks = new int[]{0x00ff0000, 0x0000ff00, 0x000000ff};
SinglePixelPackedSampleModel sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, image.getWidth(),
image.getHeight(), bitMasks);
WritableRaster destRaster = Raster.createWritableRaster(sampleModel, null);
DataBufferInt destDataBuffer = (DataBufferInt) destRaster.getDataBuffer();
int[] destBuffer = destDataBuffer.getData();
int[] bandOffsets = sourceSampleModel.getBandOffsets();
for (int i = 0; i < bandOffsets.length; i++) {
bandOffsets[i] += ((-sourceRaster.getSampleModelTranslateX() * sourceSampleModel.getPixelStride())
- (sourceRaster.getSampleModelTranslateY() * sourceSampleModel.getScanlineStride()));
}
DataBuffer sourceDataBuffer = sourceRaster.getDataBuffer();
if (sourceDataBuffer instanceof DataBufferUShort) {
convertUShortDataBuffer(image, (DataBufferUShort) sourceDataBuffer, sourceSampleModel, bandOffsets, destBuffer);
} else if (sourceDataBuffer instanceof DataBufferByte) {
convertByteDataBuffer(image, (DataBufferByte) sourceDataBuffer, sourceSampleModel, bandOffsets, destBuffer);
} else {
throw new IllegalArgumentException("Cannot deal with " + sourceDataBuffer.getClass());
}
String sourceProfileName = getICCProfileName(colorSpace.getProfile());
if (sourceProfileName.equals("Nikon sRGB 4.0.0.3001")) {
logger.warn(">>>> Workaround #1094403: using sRGB instead of " + sourceProfileName);
colorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(ColorSpace.CS_LINEAR_RGB));
}
colorModel = new DirectColorModel(colorSpace, 24, bitMasks[0], bitMasks[1], bitMasks[2], 0, false, DataBuffer.TYPE_INT);
image = new BufferedImage(colorModel, destRaster, false, null);
}
time = System.currentTimeMillis() - time;
logger.info(">>>> convertToSinglePixelPackedSampleModel() completed ok in " + time + " msec");
return image;
}
/**
* @param image
* @param sourceDataBuffer
* @param sourceSampleModel
* @param bandOffsets
* @param destBuffer
*/
protected static void convertByteDataBuffer(BufferedImage image, DataBufferByte sourceDataBuffer,
PixelInterleavedSampleModel sourceSampleModel, int[] bandOffsets, int[] destBuffer) {
int base = 0;
int i = 0;
byte[] sourceBuffer = sourceDataBuffer.getData();
int pixelStride = sourceSampleModel.getPixelStride();
for (int y = 0; y < image.getHeight(); y++) {
int j = base;
for (int x = 0; x < image.getWidth(); x++) {
int r = (sourceBuffer[j + bandOffsets[0]] & 0xff);
int g = (sourceBuffer[j + bandOffsets[1]] & 0xff);
int b = (sourceBuffer[j + bandOffsets[2]] & 0xff);
destBuffer[i++] = (r << 16) | (g << 8) | b;
j += pixelStride;
}
base += sourceSampleModel.getScanlineStride();
}
}
protected static void convertUShortDataBuffer(BufferedImage image, DataBufferUShort sourceDataBuffer,
PixelInterleavedSampleModel sourceSampleModel, int[] bandOffsets, int[] destBuffer) {
int base = 0;
int i = 0;
short[] sourceBuffer = sourceDataBuffer.getData();
for (int y = 0; y < image.getHeight(); y++) {
int j = base;
for (int x = 0; x < image.getWidth(); x++) {
int r = (sourceBuffer[j + bandOffsets[0]] & 0xffff) >> 8;
int g = (sourceBuffer[j + bandOffsets[1]] & 0xffff) >> 8;
int b = (sourceBuffer[j + bandOffsets[2]] & 0xffff) >> 8;
destBuffer[i++] = (r << 16) | (g << 8) | b;
j += 3;
}
base += sourceSampleModel.getScanlineStride();
}
}
// public static ICC_Profile getICCProfile(RenderedImage image) {
//
// ColorSpace colorSpace = image.getColorModel().getColorSpace();
//
// if (colorSpace instanceof ICC_ColorSpace) {
//
// ICC_ColorSpace iccColorSpace = (ICC_ColorSpace) colorSpace;
//
// return iccColorSpace.getProfile();
//
// }
//
// return null;
//
// }
public static String getICCProfileName(ICC_Profile profile) {
if (profile == null) {
return null;
}
byte[] xx = profile.getData(ICC_Profile.icSigProfileDescriptionTag);
int offset = 12;
int count;
for (count = 1; xx[offset + count] != 0; count++) {
;
}
return new String(xx, 0, offset, count);
}
基本的には、を呼び出すだけ.convertToSinglePixelPackedSampleModel(image)
です。
(比較的大きなTIFF画像)のレンダリング時間を数分から数秒未満に短縮しました:P
ps-これは、元のコードhttp://www.koders.com/java/fidFE1D69AFE6930A514D5E189310AB10A3DFD43F78.aspxを見つけた場所だと思います