【发布时间】:2017-06-22 15:24:03
【问题描述】:
我正在使用一些第三方软件将图像转换为 PDF,并且我注意到一些膨胀的文件大小。经过一番挖掘,我确认没有保留图像的颜色模型。黑白(1 位)图像正在转换为 RGB 颜色模型。
通过库挖掘显示了一些颜色模型检测:
switch (awtColorSpace.getType()) {
case ColorSpace.TYPE_RGB:
return PDDeviceRGB.INSTANCE;
case ColorSpace.TYPE_GRAY:
return PDDeviceGray.INSTANCE;
case ColorSpace.TYPE_CMYK:
return PDDeviceCMYK.INSTANCE;
default:
throw new UnsupportedOperationException("color space not implemented: "
+ awtColorSpace.getType());
}
这些图像总是以 RGB 格式返回。我决定编写一些测试,它们似乎证实了这一点:
package com.acme;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.*;
public class ImageColorDetectionTest {
@Test
public void colorImage() throws Exception {
// Colorspace: sRGB, Depth: 8-bit, Channel depth: Red: 8-bit Green: 8-bit Blue: 8-bit
BufferedImage image = readImage("/color.png");
assertEquals(ColorSpace.TYPE_RGB, image.getColorModel().getColorSpace().getType());
}
@Test
public void greyscaleImage() throws Exception {
// Colorspace: Gray, Depth: 8-bit, Channel depth: Gray: 8-bit
BufferedImage image = readImage("/greyscale.png");
assertEquals(ColorSpace.TYPE_GRAY, image.getColorModel().getColorSpace().getType());
}
@Test
public void blackAndWhiteImage() throws Exception {
// Colorspace: Gray, Depth: 8/1-bit, Channel depth: Gray: 1-bit
BufferedImage image = readImage("/bw.png");
assertEquals(ColorSpace.TYPE_GRAY, image.getColorModel().getColorSpace().getType());
}
protected BufferedImage readImage(String path) throws IOException {
try (InputStream content = this.getClass().getResourceAsStream(path)) {
return ImageIO.read(content);
}
}
}
blackAndWhiteImage 测试总是失败。颜色空间类型为 5 (RGB)。这是 JDK 中的错误还是我在这里遗漏了一些基本的东西?
测试图片:
色彩空间:sRGB,深度:8 位,通道深度:红色:8 位绿色:8 位蓝色:8 位
Imagemagick 识别:
magick identify -verbose bw.png
Image: bw.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: PseudoClass
Geometry: 329x247+0+0
Units: Undefined
Type: Bilevel
Base type: Palette
Endianess: Undefined
Colorspace: Gray
Depth: 8/1-bit
Channel depth:
Gray: 1-bit
Channel statistics:
Pixels: 81263
Gray:
min: 0 (0)
max: 255 (1)
mean: 110.66 (0.433961)
standard deviation: 126.384 (0.495623)
kurtosis: -1.92901
skewness: 0.266484
entropy: 0.98738
Colors: 2
Histogram:
45998: ( 0, 0, 0) #000000 gray(0)
35265: (255,255,255) #FFFFFF gray(255)
Colormap entries: 2
Colormap:
0: ( 0, 0, 0,255) #000000FF graya(0,1)
1: (255,255,255,255) #FFFFFFFF graya(255,1)
Rendering intent: Undefined
Gamma: 0.45455
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Matte color: grey74
Background color: white
Border color: srgb(223,223,223)
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 329x247+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2017-06-22T09:33:09-05:00
date:modify: 2017-06-22T09:33:09-05:00
png:bKGD: chunk was found (see Background color, above)
png:cHRM: chunk was found (see Chromaticity, above)
png:gAMA: gamma=0.45455 (See Gamma, above)
png:IHDR.bit-depth-orig: 1
png:IHDR.bit_depth: 1
png:IHDR.color-type-orig: 0
png:IHDR.color_type: 0 (Grayscale)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 329, 247
png:text: 2 tEXt/zTXt/iTXt chunks were found
png:tIME: 2017-06-21T10:12:36Z
signature: 689d59f57ef9b4d58011f92e26f937d9d58cf1ca1ccbcaad6bad7bdd0552fcfa
Artifacts:
verbose: true
Tainted: False
Filesize: 3.69KB
Number pixels: 81.3K
User time: 0.000u
Elapsed time: 0:01.000
Version: ImageMagick 7.0.5-4 Q16 x86_64 2017-03-25 http://www.imagemagick.org
【问题讨论】:
标签: java javax.imageio