0
我使用的libjpeg庫讀取和一個JPEG複製到我寫在C++提取RGB從JPEG圖像,使用libjepg
我有一個顯示緩衝器,其是被稱爲一個數據類型的一個矢量的編輯程序ColorData
所有ColorData由3個float(RGB)
這裏是我的代碼打開的JPEG文件
PixelBuffer * IOManager::load_jpg_to_pixel_buffer(const char *file_name){
struct jpeg_decompress_struct cinfo;
FILE * infile;
JSAMPARRAY buffer;
if ((infile = fopen(file_name, "rb")) == NULL) {
std::cout << "Could not open the jpg file: " << file_name << std::endl;
return nullptr;
}
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
int width = static_cast<int>(cinfo.output_width);
int height = static_cast<int>(cinfo.output_height);
std::cout << typeid(cinfo.colormap).name() << std::endl;
std::cout << "Width: " << width << "Height: " << height << std::endl;
PixelBuffer * image_buffer = new PixelBuffer(width, height, ColorData());
std::cout << cinfo.output_components << std::endl;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, cinfo.output_width * cinfo.output_components, 1);
/* Step 6: while (scan lines remain to be read) */
/* jpeg_read_scanlines(...); */
/* Here we use the library's state variable cinfo.output_scanline as the
* loop counter, so that we don't have to keep track ourselves.
*/
while (cinfo.output_scanline < cinfo.output_height) {
/* jpeg_read_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could ask for
* more than one scanline at a time if that's more convenient.
*/
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
/* Assume put_scanline_someplace wants a pointer and sample count. */
}
return nullptr;
}
我怎樣才能從t RGB值他jpeg使用libjpeg?
JPEG文件[編碼的YCbCr(https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion),可能與[色度子採樣](https://en.wikipedia.org/wiki/Chroma_subsampling)。您必須編寫必要的顏色轉換代碼,或者使用可以爲您完成的庫。 – Cornstalks
@Cornstalks,libjpeg具有內置的現成轉換,您只需指定適當的'out_color_space' – Dmitry
@Dmitry:感謝您的更正! – Cornstalks