1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include <jpeglib.h>
#include <jerror.h>
using namespace std;
/*
* Initialize source --- called by jpeg_read_header
* before any data is actually read.
*/
METHODDEF(void)
init_source (j_decompress_ptr cinfo)
{
}
/*
* Fill the input buffer --- called whenever buffer is emptied.
*
* If this procedure gets called, we have a buffer overrun condition -
* there is not enough data in the buffer to satisfy the decoder.
* The procedure just generates a warning and feeds the decoder a fake
* JPEG_EOI marker.
*/
METHODDEF(boolean)
fill_input_buffer (j_decompress_ptr cinfo)
{
struct jpeg_source_mgr * src = cinfo->src;
static JOCTET FakeEOI[] = { 0xFF, JPEG_EOI };
/* Generate warning */
WARNMS(cinfo, JWRN_JPEG_EOF);
/* Insert a fake EOI marker */
src->next_input_byte = FakeEOI;
src->bytes_in_buffer = 2;
return TRUE;
}
METHODDEF(void)
skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
struct jpeg_source_mgr * src = cinfo->src;
if(num_bytes >= (long)src->bytes_in_buffer)
{
fill_input_buffer(cinfo);
return;
}
src->bytes_in_buffer -= num_bytes;
src->next_input_byte += num_bytes;
}
/*
* Terminate source --- called by jpeg_finish_decompress
* after all data has been read. Often a no-op.
*
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
* application must deal with any cleanup that should happen even
* for error exit.
*/
METHODDEF(void)
term_source (j_decompress_ptr cinfo)
{
/* no work necessary here */
}
int cvCvtJPEGToBGR(X7sNetFrame *netframe, IplImage *im) {
//Declare JPEG structure
struct jpeg_source_mgr jsrc;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
if(im==NULL) {
printf("IplImage is NULL");
return -1;
}
//Initialize JPEG source
jsrc.bytes_in_buffer=netframe->data->length;
jsrc.next_input_byte=netframe->data->data;
//Setup function pointer
jsrc.init_source=init_source;
jsrc.fill_input_buffer = fill_input_buffer;
jsrc.skip_input_data = skip_input_data;
jsrc.resync_to_restart = jpeg_resync_to_restart; /* use default method */
jsrc.term_source = term_source;
//Initialize JPEG structure.
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
//Setup JPEG
cinfo.src=&jsrc;
//read the source datastream header markers
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
if(im->width != (int)cinfo.output_width ||
im->height != (int)cinfo.output_height ||
im->nChannels != cinfo.num_components) {
cout << "Size changed" << endl;
return -1;
}
uint8_t* line = (uint8_t*)im->imageData;
//For each row
for(int r=0;r<im->height;r++) {
line+=im->widthStep;
jpeg_read_scanlines(&cinfo,(JSAMPARRAY)&line,1);
}
/* wrap up decompression, destroy objects, free pointers and close open files */
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
//Finally, convert the image to BGR
cvConvertImage(im,im,CV_CVTIMG_SWAP_RB);
return 1;
} |
Partager