更新的解决方案(不需要 Raster 和 ColorModel 的替代方案)
我的旧解决方案(见下文)仍然需要 Rasters 和 ColorModels,这确实让我感到困扰。我的解决方案遇到了挑战,所以我花了更多时间寻找替代方案。所以我现在能想到的最好的事情是:
try {
final FileInputStream fileInputStream = new FileInputStream("dice.png");
final BufferedImage image = ImageIO.read(fileInputStream);
fileInputStream.close(); // ImageIO.read does not close the input stream
final BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
convertedImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
final FileOutputStream fileOutputStream = new FileOutputStream("dice-test.jpg");
final boolean canWrite = ImageIO.write(convertedImage, "jpg", fileOutputStream);
fileOutputStream.close(); // ImageIO.write does not close the output stream
if (!canWrite) {
throw new IllegalStateException("Failed to write image.");
}
} catch (IOException e) {
e.printStackTrace();
}
我像以前一样得到了 BufferedImage 的副本。它或多或少做同样的事情,但实际上您可以更轻松地重用 ColorModel 和 Raster。
drawImage()
似乎照顾了我之前手动做的大部分事情。而且由于它一直是标准的java库代码,它似乎确实是一个更好的方法。
请注意,您最终会得到一个类型为 Image 的图像BufferedImage.TYPE_INT_RGB
。虽然它似乎适用于、 和类型jpg
,但我不确定其他文件格式或具有不同存储 ColorModel 的文件会发生什么 - 信息可能会丢失(例如 4 个颜色通道到 3 个)。对于提到的类型,我们不需要 alpha 通道,即使我们从或转换为(它将是 Color.WHITE)。
png
gif
gif
jpg
png
旧解决方案
我对我的第一个设计不满意,而且它也没有按应有的方式工作。
因此,我从头开始创建了一个。我最终得到了一个小sRGB
文件转换器。您可以从png
to转换,jpg
反之亦然(编辑:gif
也添加了支持)。如果您想处理其他类型,请随时进一步扩展。您可以或多或少以相同的方式添加它。它也可能适用于其他文件类型,但我还没有测试过它们。幸运的是,这似乎sRGB
很常见。
太棒了。我不知道您可以生产多少组合和变体(调色板、精度、质量、黑白等),也不知道它们共享哪些共同属性。
也许这对你来说已经足够好了。也许不吧。至少这对我来说是一个很好的练习。
这个解决方案绝不是完美的。结果看起来还不错。文件类型转换有效,文件大小也小于png
.
try {
final String fileName = "dice.png";
final BufferedImage inputImage = ImageIO.read(new FileInputStream(fileName));
final boolean isSRGB = inputImage.getColorModel().getColorSpace().isCS_sRGB();
final String outputFormat = "gif";
if (!isSRGB) {
throw new IllegalArgumentException("Please provide an image that supports sRGB.");
}
final WritableRaster raster = createRaster(inputImage);
final ColorModel colorModel = createColorModel(inputImage);
final BufferedImage outputImage = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
final String outputFileName = fileName + "-converted." + outputFormat;
final boolean writeResult = ImageIO.write(outputImage, outputFormat, new FileOutputStream(outputFileName));
if (!writeResult) {
throw new IllegalStateException("Could not convert file: " + fileName + " to format: " + outputFormat);
}
System.out.println(">> Created file: " + outputFileName);
} catch (Exception e) {
e.printStackTrace();
}
@NotNull
public static ColorModel createColorModel(@NotNull BufferedImage bufferedImage) {
Objects.requireNonNull(bufferedImage);
final int type = bufferedImage.getType();
boolean isAlphaPremultiplied = false;
int transparency = Transparency.OPAQUE;
if (type == BufferedImage.TYPE_3BYTE_BGR) {
isAlphaPremultiplied = true;
}
return new ComponentColorModel(
ColorModel.getRGBdefault().getColorSpace(),
false, isAlphaPremultiplied, transparency,
bufferedImage.getData().getDataBuffer().getDataType()
);
}
@NotNull
public static WritableRaster createRaster(@NotNull BufferedImage bufferedImage) {
Objects.requireNonNull(bufferedImage);
final int type = bufferedImage.getType();
final int width = bufferedImage.getWidth();
final int height = bufferedImage.getHeight();
final int pixelStride = 3;
int[] offset = new int[]{0, 1, 2};
DataBufferByte dataBufferByte;
if (type == BufferedImage.TYPE_4BYTE_ABGR || type == BufferedImage.TYPE_BYTE_INDEXED) {
int dataIndex = 0;
final byte[] data = new byte[height * width * pixelStride];
final int bitmask = 0xff;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
final int rgb = bufferedImage.getRGB(x, y);
final int blue = bitmask & rgb;
final int green = bitmask & (rgb >> 8);
final int red = bitmask & (rgb >> 16);
if (rgb == 0) {
data[dataIndex++] = (byte) bitmask;
data[dataIndex++] = (byte) bitmask;
data[dataIndex++] = (byte) bitmask;
} else {
data[dataIndex++] = (byte) red;
data[dataIndex++] = (byte) green;
data[dataIndex++] = (byte) blue;
}
}
}
dataBufferByte = new DataBufferByte(data, data.length);
} else if (type == BufferedImage.TYPE_3BYTE_BGR) {
dataBufferByte = (DataBufferByte) bufferedImage.getRaster().getDataBuffer();
offset = new int[]{2, 1, 0};
} else {
throw new IllegalArgumentException("Cannot create raster for unsupported image type.");
}
return Raster.createInterleavedRaster(
dataBufferByte, width, height,
pixelStride * width, pixelStride,
offset,
null
);
}
编辑:添加了对 gif 的支持。