2010-12-20 95 views

回答

3

如果使用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); 
相關問題