Implementation of extracting JPEG from memory

Source: Internet
Author: User


First, Introduction

JPEG codec program on the network there are many, there are many ready-made classes such as the famous cximage, or the use of OPENCV. But these Tooltik basically only provides the decoding from the hard disk to open the JPEG image, sometimes if the JPEG image data is loading from the memory, cannot use these once very convenient and efficient interface. For example, the interface provided by OPENCV is: Cvapi (iplimage*) cvloadimage (const char* filename, int iscolor cv_default (cv_load_image_color));

Ii. Solutions:

With RAMDisk memory virtual hard disk, the transmission of the JPG file exists virtual disk, and then cvloadimage load analysis. OpenCV 2.0 provides this functionality. (I'm faking OpenCV 1.1, and I haven't used either of these functions)
Imdecode
Mat imdecode (const mat& BUF, int flags);
BUF the input array of vector of bytes
Flags the same flags as in Imread
The function reads image from the specified buffer in memory. If the buffer is too a short or
Contains invalid data, the empty matrix would be returned.
Imread for the list of supported formats and the flags description.
Imencode
BOOL Imencode (const string& EXT, const mat& IMG,
vector<uchar>& buf,
Const vector<int>& params=vector<int> ());
Ext The file extension that defines the output format
IMG The image to be written
BUF the output buffer; Resized to fit the compressed image
params the format-specific parameters; Imwrite
The function compresses the image and stores it in the memory buffer, which are resized to fit
The result. Imwrite for the list of supported formats and the flags description. Looking at some code on the Internet, I used jpeglib to change an interface. OpenCV also has jpeglib, if you want to use the Jpeglib attention in OpenCV to configure Have_jpeg. I put this interface into the OPENCV to do it once and for all, and other projects do not need to copy and paste to use this interface.

First, make a function declaration in highgui.h.

/**********************************************************************/
by XJF 2009-11-09
Convert a JPEG image to BMP from a buffer
Cause we can use OPENCV to convert a JPEG image from file by easily
Using Cvloadimage ()
The API in the Grfmt_jpeg.cpp

Cvapi (iplimage*) cvjpeg2ipl (char *jpegdata, int jpegsize);

The implementation of the function I put it in the grfmt_jpeg.cpp.

/**********************************************************************/
void Mem_init_source (J_decompress_ptr cinfo)
{
Cinfo->src->bytes_in_buffer = G_buf_len;
Cinfo->src->next_input_byte = (unsigned char*) g_buf;
}
Boolean Mem_fill_input_buffer (J_decompress_ptr cinfo)
{
return true;
}
void Mem_skip_input_data (J_decompress_ptr cinfo, long num_bytes)
{
Cinfo->src->bytes_in_buffer-= num_bytes;
Cinfo->src->next_input_byte + = Num_bytes;
}
Boolean Mem_resync_to_restart (j_decompress_ptr cinfo, int desired)
{
Return Jpeg_resync_to_restart (cinfo, desired);
}
void Mem_term_source (J_decompress_ptr cinfo)
{
}

/**********************************************************************/
@jpegData: In-memory JPEG image data stream

@jpegSize: Data Flow size
Cv_impl iplimage*
CVJPEG2IPL (char *jpegdata, int jpegsize)
{
Iplimage *_pimg = 0;

Jpeg_source_mgr Jmgr;
struct Jpeg_error_mgr jerr;
struct Jpeg_decompress_struct cinfo;

Cinfo.err = Jpeg_std_error (&jerr);
Jpeg_create_decompress (&cinfo);

Jmgr.init_source = Mem_init_source;
Jmgr.fill_input_buffer = Mem_fill_input_buffer;
Jmgr.skip_input_data = Mem_skip_input_data;
Jmgr.resync_to_restart = Mem_resync_to_restart;
Jmgr.term_source = Mem_term_source;
Jmgr.next_input_byte = (unsigned char*) jpegdata;
Jmgr.bytes_in_buffer = jpegsize;

CINFO.SRC = &jmgr;
Jpeg_read_header (&cinfo, TRUE);
Jpeg_start_decompress (&cinfo);

int nrowsize = Cinfo.output_width * cinfo.output_components;
int W =cinfo.output_width;
int h =cinfo.output_height;

Char *bmpbuffer=new char[h*w*3];
Jsamparray pbuffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, 1, nrowsize, 1);
while (Cinfo.output_scanline < cinfo.output_height)
{
Jpeg_read_scanlines (&cinfo, pbuffer, 1);

int start=nrowsize* (CINFO.OUTPUT_SCANLINE-1);
for (int i=0;i<nrowsize;i++)
{
Bmpbuffer[start+i]=pbuffer[0][i];
}
}
Jpeg_finish_decompress (&cinfo);
Jpeg_destroy_decompress (&cinfo);

_pimg = Cvcreateimage (Cvsize (w,h), 8, 3);
for (h=0; h<_pimg->height; h++)
{
for (w=0; w<_pimg->widthstep; w+=3)
{
* (_pimg->imagedata+h*_pimg->widthstep+w+0) =* (bmpbuffer+h*_pimg->widthstep+w+2);
* (_pimg->imagedata+h*_pimg->widthstep+w+1) =* (bmpbuffer+h*_pimg->widthstep+w+1);
* (_pimg->imagedata+h*_pimg->widthstep+w+2) =* (bmpbuffer+h*_pimg->widthstep+w+0);
}
}

Delete Bmpbuffer;

Bmpbuffer=null;
return _pimg;
}

Need to recompile OPENCV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.