libjpeg庫支持解碼內存中的jpeg數據

找到jdatasrc.c這個文件(從命名可以看出其跟輸入數據相關),在my_source_mgr這個結構體的上方添加一個結構體(jpeg內存塊描述相關)如下:
typedef struct{
UINT8* img_buffer;
long buffer_size;
long pos;
}BUFF_JPG;

然後將my_source_mgr結構的定義修改如下:
typedef struct {
struct jpeg_source_mgr pub; /* public fields */
union{
BUFF_JPG jpg; /* jpeg image buffer */
FILE * infile; /* source stream */
};
JOCTET * buffer; /* start of buffer */
boolean start_of_file; /* have we gotten any data yet? */
} my_source_mgr;
接着在文件中定義相應的回調函數,可先用“Ctrl+F”查找“METHODDEF(boolean)”,然後添加在其上方,代碼如下:
/*
* This function will read the jpeg memery block to fill the library buffer.
*/
METHODDEF(boolean)
jpg_fill_input_buffer (j_decompress_ptr cinfo)
{
my_src_ptr src = (my_src_ptr) cinfo->src;
size_t nbytes;

if(src->jpg.img_buffer == NULL || src->jpg.pos >= src->jpg.buffer_size){
nbytes = -1;
}
else {
nbytes = (src->jpg.pos + INPUT_BUF_SIZE > src->jpg.buffer_size ?
src->jpg.buffer_size - src->jpg.pos : INPUT_BUF_SIZE);
memcpy(src->buffer, src->jpg.img_buffer + src->jpg.pos, nbytes);
src->jpg.pos += nbytes;
}

if (nbytes <= 0) {
if (src->start_of_file) /* Treat empty input file as fatal error */
ERREXIT(cinfo, JERR_INPUT_EMPTY);
WARNMS(cinfo, JWRN_JPEG_EOF);
/* Insert a fake EOI marker */
src->buffer[0] = (JOCTET) 0xFF;
src->buffer[1] = (JOCTET) JPEG_EOI;
nbytes = 2;
}

src->pub.next_input_byte = src->buffer;
src->pub.bytes_in_buffer = nbytes;
src->start_of_file = FALSE;

return TRUE;
}
做完上面這些,我們需要實現一個供用戶用到解碼內存中jpeg數據時初始化source manager的接口,添加位置和上面類似,代碼如下:
/*
* This function improve the library can use the jpeg memory block as source.
*/
GLOBAL(void)
jpeg_stdio_buffer_src (j_decompress_ptr cinfo, UINT8 * buffer, long size)
{
my_src_ptr src;

if (cinfo->src == NULL) { /* first time for this JPEG object? */
cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
SIZEOF(my_source_mgr));
src = (my_src_ptr) cinfo->src;
src->buffer = (JOCTET *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
INPUT_BUF_SIZE * SIZEOF(JOCTET));
}

src = (my_src_ptr) cinfo->src;
src->pub.init_source = init_source;
src->pub.fill_input_buffer = jpg_fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
src->pub.term_source = term_source;
//src->infile = infile;
src->jpg.img_buffer = buffer;
src->jpg.buffer_size = size;
src->jpg.pos = 0;
src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
src->pub.next_input_byte = NULL; /* until buffer loaded */
}
這樣我們在jdatasrc.c這個文件所要做的工作就做完了,其實修改的工作也可以說做的八九不離十了,只需要在jpeglib.h文件中將接口暴露出來即可,添加位置也是跟上面很類似的,不再贅述,代碼如下:
EXTERN(void) jpeg_stdio_buffer_src JPP((j_decompress_ptr cinfo, UINT8 * buffer, long size));

做完上面這些就大功告成了,編譯出來的jpeg.lib已經支持解碼內存中的jpeg數據了,jpeg_stdio_buffer_src這個函數就是相應的接口。

發佈了33 篇原創文章 · 獲贊 30 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章