开发者

How to check whether a jpeg is a RGB or CMYK format?

I am using c++, gdi+,WINDOWS. How to check 开发者_Go百科whether a jpeg is a RGB or CMYK?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜