2013-02-27 390 views
1

我對libjpeg jpeg_read_scanlines的工作方式感到困惑。我的理解是,它逐行解壓JPEG,並創建一個解壓縮的像素緩衝區。困惑於libjpeg:jpeg_read_scanlines

典型用法是一樣的東西:

jpeg_decompress_struct cinfo; 

... 

unsigned char* image = new unsigned char[cinfo.image_width * cinfo.image_height]; 
unsigned char* ptr = image; 
int row_stride = cinfo.image_width; 

while (cinfo.output_scanline < cinfo.image_height) 
{ 
    jpeg_read_scanlines(&cinfo, &ptr, 1); 
    ptr += row_stride; 
} 


問:我感到困惑的輸出緩衝區大小。在所有的example code我看到哪個使用jpeg_read_scanlines,輸出緩衝區的大小是width X height,其中寬度和高度指的是JPEG文件的尺寸。所以對於一個10x10的JPEG文件,我們會有一個100字節的輸出緩衝區。

但是...不是每個RGB像素的大小3個字節(24位)?那麼不應該將未壓縮的數據實際上設爲width X height X 3字節?

爲什麼不是這樣?

我注意到在使用jpeg_write_scanlines的代碼時,要壓縮的緩衝區是ISwidth X height X 3。那麼爲什麼使用jpeg_read_scanlines的緩衝區只有width X height

+0

您正在閱讀的例子不正確。注意'row_stride'變量。 – 2013-02-27 03:58:10

+1

@n.m。在示例代碼中,'row_stride'是'cinfo.output_width * cinfo.output_components;'。因爲'cinfo.output_components'是'1',row_stride等同於'cinfo.output_width' – Channel72 2013-02-27 04:12:58

+2

灰度圖像只有1。 – 2013-02-27 04:44:12

回答

2

你只是讀1線在時間線

jpeg_read_scanlines(&cinfo, &ptr, 1);

所以你只需要行

unsigned char* image = new unsigned char[cinfo.image_width * cinfo.image_height];

unsigned char* image = new unsigned char[cinfo.image_width * cinfo.image_components];

緩衝區的開始被重新用於每條掃描線。你當前的大部分緩衝區實際上是未使用的。

0

對於RGB數據,output_components將爲3(R,G,B)。

下面是一些libjpeg.txt相關文件:

output_width  image width and height, as scaled 
    output_height 
    out_color_components # of color components in out_color_space 
    output_components # of color components returned per pixel 
    colormap  the selected colormap, if any 
    actual_number_of_colors  number of entries in colormap 

output_components is 1 (a colormap index) when quantizing colors; otherwise it 
equals out_color_components. It is the number of JSAMPLE values that will be 
emitted per pixel in the output arrays. 

Typically you will need to allocate data buffers to hold the incoming image. 
You will need output_width * output_components JSAMPLEs per scanline in your 
output buffer, and a total of output_height scanlines will be returned.