|
The FromJPEG function
#include <stdio.h>
#include "ImgJPEG.h"
#include "ImgAlloc.h"
#include ".\JPEG\jpeglib.h"
void jpeg_FromJPEG(void*& pBuf,unsigned long& BufSz,ImgJPEGInfo& Info)
{
jpeg_decompress_struct cinfo;
jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_buffer_src(&cinfo, (unsigned char*)pBuf, BufSz);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
Info.XDim = cinfo.image_width;
Info.YDim = cinfo.image_height;
Info.LineSize = (Info.XDim * 3);
char Pack;
char Rem = (char) (Info.LineSize % 4);
if(Rem != 0)
Rem = (4 - Rem);
Info.LineSize += Rem;
Info.Quality = 50;
unsigned long OutBufSz = Info.LineSize * Info.YDim;
unsigned char* pOutBuf = (unsigned char*) img_malloc(OutBufSz);
int row_stride = cinfo.output_width * cinfo.output_components;
JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
while(cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, buffer, 1);
unsigned char* pInsert = &(pOutBuf[((cinfo.output_height - 1) - (cinfo.output_scanline-1)) * Info.LineSize]);
img_memcpy(pInsert,buffer[0],row_stride);
pInsert += row_stride;
for(Pack=0;Pack<Rem;Pack++)
{
*pInsert = 0;
pInsert++;
}
}
jpeg_finish_decompress(&cinfo);
img_free(pBuf);
BufSz = OutBufSz;
pBuf = pOutBuf;
pOutBuf = NULL;
jpeg_destroy_decompress(&cinfo);
}
|