java - 我可以使用 libjpeg 读取带有 alpha channel 的 JPEG 吗?

标签 java jpeg file-format libjpeg color-space

关于带有 alpha channel 的 JPEG 是否有效似乎存在一些争论。我一直认为正确的答案是 that in the JPEG FAQ ,本质上是“否”。 (这在 another question on Stack Overflow 中得到重申。)

但是,Sun 的 ImageIO 库中的 Java JPEGImageWriter 将愉快地使用 alpha channel 写入和读取灰度和 RGB 图像,即使到目前为止我在 Linux 上几乎没有尝试过可以正确加载此类 JPEG 的应用程序。这在过去被报告为错误,但 Sun 的回应是 these are valid files :

This is not an Image I/O bug, but rather a deficiency in the other applications the submitter mentions. The IIO JPEGImageWriter is able to write images with a color model that contains an alpha channel (referred to in the IJG native source code as the "NIFTY" color spaces, such as RGBA, YCbCrA, etc.), but many applications are not aware of these color spaces. So even though these images written by the IIO JPEG writer are compliant with the JPEG specification (which is blind to the various color space possiblities), some applications may not recognize color spaces that contain an alpha channel and may throw an error or render a corrupted image, as the submitter describes.

Developers wishing to maintain compatibility with these other alpha-unaware applications should write images that do not contain an alpha channel (such as TYPE_INT_RGB). Developers who want the capability of writing/reading an image containing an alpha channel in the JPEG format can do so using the Image I/O API, but need to be aware that many native applications out there are not quite compliant with the YCbCrA and RGBA formats.

For more information, see the Image I/O JPEG Metadata Format Specification and Usage Notes: http://java.sun.com/j2se/1.4.1/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html

Closing as "not a bug". xxxxx@xxxxx 2003-03-24

我正在使用创建此类文件的 Java 应用程序,并希望编写一些 C 代码来尽快加载这些文件。 (本质上,问题在于 Java ImageIO 库在解压缩这些文件时速度非常慢,我们希望通过 JNI 将加载器替换为 native 代码以改善这一点——目前这是一个性能瓶颈。)

此处有一些示例文件 - 向 coulrophobic 的任何人致歉:

在这里你可以看到尝试使用各种 Linux 软件查看灰度 + alpha 和 RGB + alpha 图像的结果,我相信这些软件使用 libjpeg:

grayscale image with alpha channel view with various programs http://mythic-beasts.com/~mark/all-alpha-bridges.png

RGB image with alpha channel view with various programs
(来源:mark at mythic-beasts.com)

因此,在每种情况下,颜色空间似乎都被误解了。 jpeglib.h 中唯一允许的值是:

/* Known color spaces. */

typedef enum {
        JCS_UNKNOWN,            /* error/unspecified */
        JCS_GRAYSCALE,          /* monochrome */
        JCS_RGB,                /* red/green/blue */
        JCS_YCbCr,              /* Y/Cb/Cr (also known as YUV) */
        JCS_CMYK,               /* C/M/Y/K */
        JCS_YCCK                /* Y/Cb/Cr/K */
} J_COLOR_SPACE;

...这看起来不太乐观。

如果我使用来自 libjpegexample.c 的稍微修改版本加载这些图像,cinfo.jpeg_color_spacecinfo.out_color_space 读取header后每张图片如下:

gray-normal.jpg: jpeg_color_space is JCS_GRAYSCALE, out_color_space is JCS_GRAYSCALE
gray-alpha.jpg: jpeg_color_space is JCS_CMYK, out_color_space is JCS_CMYK

rgb-normal.jpg: jpeg_color_space is JCS_YCbCr, out_color_space is JCS_RGB
rgb-alpha.jpg: jpeg_color_space is JCS_CMYK, out_color_space is JCS_CMYK

所以,我的问题是:

  • 能否使用 libjpeg 正确读取这些文件?
  • 如果没有,是否有我可以使用的替代 C 库来处理它们?

显然,对于更普遍的问题,至少还有两个其他解决方案:

  1. 更改软件以输出普通 JPEG + 表示 alpha channel 的 PNG 文件
  2. 以某种方式提高 Sun 的 ImageIO 的性能

...但是第一个会涉及大量的代码更改,并且不清楚如何处理后者。无论如何,我认为如何使用 libjpeg 加载此类文件的问题可能会引起更普遍的兴趣。

如有任何建议,我们将不胜感激。

最佳答案

即使您将图像存储为 4 channel jpeg 图像,我也没有知道如何在 jpeg 文件中指定颜色格式的标准化方法。

JFIF 标准采用 YCbCr。

关于java - 我可以使用 libjpeg 读取带有 alpha channel 的 JPEG 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6212897/

相关文章:

image - 如何将图像保存为具有给定输出文件大小的 PNG 或 JPG 文件? (首选 C#)

c# - 以渐进格式保存 JPEG

python - 压缩存档,可快速访问单个文件

.net - 版本控制友好、可扩展的二进制文件格式

java - 使用带位图的 Universal ImageLoader 显示图像

java - 读取文件内容

java - 为什么这个 BufferedReader 没有读取指定的 UTF-8 格式?

java - BouncycaSTLe RSA key 大小和填充

java - 如何在 C++ 或 C# 或 Java 中创建 jpegs(live) 流?实时传输协议(protocol)?

image - 任何人都可以识别这种图像格式吗?