如何检查 jpeg 是 RGB 还是 CMYK 格式?

发布于 2024-10-08 14:02:10 字数 55 浏览 0 评论 0原文

我正在使用 c++、gdi+、WINDOWS。 如何检查 jpeg 是 RGB 还是 CMYK?

I am using c++, gdi+,WINDOWS.
How to check whether a jpeg is a RGB or CMYK?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟到的我 2024-10-15 14:02:10

如果您使用 IJG libjpeg 库,则可以打开 JPEG 文件,读取标头,并查看它是灰度(单色)、RGB 还是 CMYK。 (实际上,有几个免费的 JPEG 库,但 IJG libjpeg 可能是最常用的。)您可以在以下位置找到 libjpeg 源:

http://www.ijg.org/files/jpegsrc.v8c.tar.gz

如何读取标头的大致示例如下:

struct jpeg_decompress_struct dinfo;
FILE* file = fopen(fname, "r");

/* Step 1: allocate and initialize JPEG decompression object */
jpeg_create_decompress(&dinfo);

/* Step 2: specify data source (eg, a file) */
jpeg_stdio_src(&dinfo, file);

/* Step 3: read file parameters with jpeg_read_header() */
(void) jpeg_read_header(dinfo, TRUE);

/* Step 4: set parameters for decompression
 * In this example, we don't need to change any of the 
 * defaults set by jpeg_read_header(), so we do nothing here.
 */
if (dinfo->jpeg_color_space == JCS_CMYK || dinfo->jpeg_color_space == JCS_YCCK) {
    // CMYK
} else if (dinfo->jpeg_color_space == JCS_RGB || dinfo->jpeg_color_space == JCS_YCbCr) {
    // RGB
} else if (dinfo->jpeg_color_space == JCS_GRAYSCALE) {
    // Grayscale
} else {
    ERREXIT(dinfo, JERR_CONVERSION_NOTIMPL);
    // error condition here...
}

/* You can skip the other steps involved in decompressing an image */

/* Step 8: Release JPEG decompression object */
jpeg_destroy_decompress(dinfo);

If you use the IJG libjpeg library, you can open the JPEG file, read the header, and see whether is it grayscale (monochrome), RGB, or CMYK. (Actually, there are several free JPEG libraries, but the IJG libjpeg is probably the most commonly used.) You can find libjpeg sources at:

http://www.ijg.org/files/jpegsrc.v8c.tar.gz

An approximate example of how to read the header would be something like:

struct jpeg_decompress_struct dinfo;
FILE* file = fopen(fname, "r");

/* Step 1: allocate and initialize JPEG decompression object */
jpeg_create_decompress(&dinfo);

/* Step 2: specify data source (eg, a file) */
jpeg_stdio_src(&dinfo, file);

/* Step 3: read file parameters with jpeg_read_header() */
(void) jpeg_read_header(dinfo, TRUE);

/* Step 4: set parameters for decompression
 * In this example, we don't need to change any of the 
 * defaults set by jpeg_read_header(), so we do nothing here.
 */
if (dinfo->jpeg_color_space == JCS_CMYK || dinfo->jpeg_color_space == JCS_YCCK) {
    // CMYK
} else if (dinfo->jpeg_color_space == JCS_RGB || dinfo->jpeg_color_space == JCS_YCbCr) {
    // RGB
} else if (dinfo->jpeg_color_space == JCS_GRAYSCALE) {
    // Grayscale
} else {
    ERREXIT(dinfo, JERR_CONVERSION_NOTIMPL);
    // error condition here...
}

/* You can skip the other steps involved in decompressing an image */

/* Step 8: Release JPEG decompression object */
jpeg_destroy_decompress(dinfo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文