?? libmng_jpeg.c
字號:
MNG_FREE (pData, pData->pJPEGrow2, pData->iJPEGrowlen2)
MNG_FREE (pData, pData->pJPEGrow, pData->iJPEGrowlen)
/* whatever we were doing ... */
/* we don't anymore ... */
pData->bJPEGcompress = MNG_FALSE;
pData->bJPEGdecompress = MNG_FALSE;
pData->bJPEGhasheader = MNG_FALSE;
pData->bJPEGdecostarted = MNG_FALSE;
pData->bJPEGscanstarted = MNG_FALSE;
pData->bJPEGscanending = MNG_FALSE;
pData->bJPEGdecompress2 = MNG_FALSE;
pData->bJPEGhasheader2 = MNG_FALSE;
pData->bJPEGdecostarted2 = MNG_FALSE;
pData->bJPEGscanstarted2 = MNG_FALSE;
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_CLEANUP, MNG_LC_END)
#endif
return MNG_NOERROR;
}
/* ************************************************************************** */
/* * * */
/* * JPEG decompression routines (JDAT) * */
/* * * */
/* ************************************************************************** */
#ifdef MNG_INCLUDE_JNG_READ
mng_retcode mngjpeg_decompressinit (mng_datap pData)
{
#if defined(MNG_INCLUDE_IJG6B) && defined(MNG_USE_SETJMP)
mng_retcode iRetcode;
#endif
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSINIT, MNG_LC_START)
#endif
#ifdef MNG_INCLUDE_IJG6B
/* allocate and initialize a JPEG decompression object */
pData->pJPEGdinfo->err = jpeg_std_error (pData->pJPEGderr);
#ifdef MNG_USE_SETJMP /* setup local JPEG error-routines */
pData->pJPEGderr->error_exit = mng_error_exit;
pData->pJPEGderr->output_message = mng_output_message;
iRetcode = setjmp (pData->sErrorbuf);/* setup local JPEG error-recovery */
if (iRetcode != 0) /* got here from longjmp ? */
MNG_ERRORJ (pData, iRetcode) /* then IJG-lib issued an error */
#endif /* MNG_USE_SETJMP */
/* allocate and initialize a JPEG decompression object (continued) */
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSINIT, MNG_LC_JPEG_CREATE_DECOMPRESS)
#endif
jpeg_create_decompress (pData->pJPEGdinfo);
pData->bJPEGdecompress = MNG_TRUE; /* indicate it's initialized */
/* specify the source of the compressed data (eg, a file) */
/* no, not a file; we have buffered input */
pData->pJPEGdinfo->src = pData->pJPEGdsrc;
/* use the default handler */
pData->pJPEGdinfo->src->resync_to_restart = jpeg_resync_to_restart;
/* setup local source routine & parms */
pData->pJPEGdinfo->src->init_source = mng_init_source;
pData->pJPEGdinfo->src->fill_input_buffer = mng_fill_input_buffer;
pData->pJPEGdinfo->src->skip_input_data = mng_skip_input_data;
pData->pJPEGdinfo->src->term_source = mng_term_source;
pData->pJPEGdinfo->src->next_input_byte = pData->pJPEGcurrent;
pData->pJPEGdinfo->src->bytes_in_buffer = pData->iJPEGbufremain;
#endif /* MNG_INCLUDE_IJG6B */
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSINIT, MNG_LC_END)
#endif
return MNG_NOERROR;
}
#endif /* MNG_INCLUDE_JNG_READ */
/* ************************************************************************** */
#ifdef MNG_INCLUDE_JNG_READ
mng_retcode mngjpeg_decompressdata (mng_datap pData,
mng_uint32 iRawsize,
mng_uint8p pRawdata)
{
mng_retcode iRetcode;
mng_uint32 iRemain;
mng_uint8p pWork;
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_START)
#endif
#if defined (MNG_INCLUDE_IJG6B) && defined(MNG_USE_SETJMP)
iRetcode = setjmp (pData->sErrorbuf);/* initialize local JPEG error-recovery */
if (iRetcode != 0) /* got here from longjmp ? */
MNG_ERRORJ (pData, iRetcode) /* then IJG-lib issued an error */
#endif
pWork = pRawdata;
iRemain = iRawsize;
if (pData->iJPEGtoskip) /* JPEG-lib told us to skip some more data ? */
{
if (iRemain > pData->iJPEGtoskip) /* enough data in this buffer ? */
{
iRemain -= pData->iJPEGtoskip; /* skip enough to access the next byte */
pWork += pData->iJPEGtoskip;
pData->iJPEGtoskip = 0; /* no more to skip then */
}
else
{
pData->iJPEGtoskip -= iRemain; /* skip all data in the buffer */
iRemain = 0; /* and indicate this accordingly */
}
/* the skip set current-pointer to NULL ! */
pData->pJPEGcurrent = pData->pJPEGbuf;
}
while (iRemain) /* repeat until no more input-bytes */
{ /* need to shift anything ? */
if ((pData->pJPEGcurrent > pData->pJPEGbuf) &&
(pData->pJPEGcurrent - pData->pJPEGbuf + pData->iJPEGbufremain + iRemain > pData->iJPEGbufmax))
{
if (pData->iJPEGbufremain > 0) /* then do so */
MNG_COPY (pData->pJPEGbuf, pData->pJPEGcurrent, pData->iJPEGbufremain)
pData->pJPEGcurrent = pData->pJPEGbuf;
}
/* does the remaining input fit into the buffer ? */
if (pData->iJPEGbufremain + iRemain <= pData->iJPEGbufmax)
{ /* move the lot */
MNG_COPY ((pData->pJPEGcurrent + pData->iJPEGbufremain), pWork, iRemain)
pData->iJPEGbufremain += iRemain;/* adjust remaining_bytes counter */
iRemain = 0; /* and indicate there's no input left */
}
else
{ /* calculate what does fit */
mng_uint32 iFits = pData->iJPEGbufmax - pData->iJPEGbufremain;
if (iFits <= 0) /* no space is just bugger 'm all */
MNG_ERROR (pData, MNG_JPEGBUFTOOSMALL)
/* move that */
MNG_COPY ((pData->pJPEGcurrent + pData->iJPEGbufremain), pWork, iFits)
pData->iJPEGbufremain += iFits; /* adjust remain_bytes counter */
iRemain -= iFits; /* and the input-parms */
pWork += iFits;
}
#ifdef MNG_INCLUDE_IJG6B
pData->pJPEGdinfo->src->next_input_byte = pData->pJPEGcurrent;
pData->pJPEGdinfo->src->bytes_in_buffer = pData->iJPEGbufremain;
if (!pData->bJPEGhasheader) /* haven't got the header yet ? */
{
/* call jpeg_read_header() to obtain image info */
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_READ_HEADER)
#endif
if (jpeg_read_header (pData->pJPEGdinfo, TRUE) != JPEG_SUSPENDED)
{ /* indicate the header's oke */
pData->bJPEGhasheader = MNG_TRUE;
/* let's do some sanity checks ! */
if ((pData->pJPEGdinfo->image_width != pData->iDatawidth ) ||
(pData->pJPEGdinfo->image_height != pData->iDataheight) )
MNG_ERROR (pData, MNG_JPEGPARMSERR)
if ( ((pData->iJHDRcolortype == MNG_COLORTYPE_JPEGGRAY ) ||
(pData->iJHDRcolortype == MNG_COLORTYPE_JPEGGRAYA) ) &&
(pData->pJPEGdinfo->jpeg_color_space != JCS_GRAYSCALE ) )
MNG_ERROR (pData, MNG_JPEGPARMSERR)
if ( ((pData->iJHDRcolortype == MNG_COLORTYPE_JPEGCOLOR ) ||
(pData->iJHDRcolortype == MNG_COLORTYPE_JPEGCOLORA) ) &&
(pData->pJPEGdinfo->jpeg_color_space != JCS_YCbCr ) )
MNG_ERROR (pData, MNG_JPEGPARMSERR)
/* indicate whether or not it's progressive */
pData->bJPEGprogressive = (mng_bool)jpeg_has_multiple_scans (pData->pJPEGdinfo);
/* progressive+alpha can't display "on-the-fly"!! */
if ((pData->bJPEGprogressive) &&
((pData->iJHDRcolortype == MNG_COLORTYPE_JPEGGRAYA ) ||
(pData->iJHDRcolortype == MNG_COLORTYPE_JPEGCOLORA) ))
pData->fDisplayrow = MNG_NULL;
/* allocate a row of JPEG-samples */
if (pData->pJPEGdinfo->jpeg_color_space == JCS_YCbCr)
pData->iJPEGrowlen = pData->pJPEGdinfo->image_width * RGB_PIXELSIZE;
else
pData->iJPEGrowlen = pData->pJPEGdinfo->image_width;
MNG_ALLOC (pData, pData->pJPEGrow, pData->iJPEGrowlen)
pData->iJPEGrgbrow = 0; /* quite empty up to now */
}
pData->pJPEGcurrent = (mng_uint8p)pData->pJPEGdinfo->src->next_input_byte;
pData->iJPEGbufremain = (mng_uint32)pData->pJPEGdinfo->src->bytes_in_buffer;
}
/* decompress not started ? */
if ((pData->bJPEGhasheader) && (!pData->bJPEGdecostarted))
{
/* set parameters for decompression */
if (pData->bJPEGprogressive) /* progressive display ? */
pData->pJPEGdinfo->buffered_image = TRUE;
/* jpeg_start_decompress(...); */
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_START_DECOMPRESS)
#endif
if (jpeg_start_decompress (pData->pJPEGdinfo) == TRUE)
/* indicate it started */
pData->bJPEGdecostarted = MNG_TRUE;
pData->pJPEGcurrent = (mng_uint8p)pData->pJPEGdinfo->src->next_input_byte;
pData->iJPEGbufremain = (mng_uint32)pData->pJPEGdinfo->src->bytes_in_buffer;
}
/* process some scanlines ? */
if ((pData->bJPEGhasheader) && (pData->bJPEGdecostarted) &&
((!jpeg_input_complete (pData->pJPEGdinfo)) ||
(pData->pJPEGdinfo->output_scanline < pData->pJPEGdinfo->output_height) ||
((pData->bJPEGprogressive) && (pData->bJPEGscanending))))
{
mng_int32 iLines = 0;
/* for (each output pass) */
do
{ /* address the row output buffer */
JSAMPROW pRow = (JSAMPROW)pData->pJPEGrow;
/* init new pass ? */
if ((pData->bJPEGprogressive) && (!pData->bJPEGscanstarted))
{
pData->bJPEGscanstarted = MNG_TRUE;
/* adjust output decompression parameters if required */
/* nop */
/* start a new output pass */
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_START_OUTPUT)
#endif
jpeg_start_output (pData->pJPEGdinfo, pData->pJPEGdinfo->input_scan_number);
pData->iJPEGrow = 0; /* start at row 0 in the image again */
}
/* while (scan lines remain to be read) */
if ((!pData->bJPEGprogressive) || (!pData->bJPEGscanending))
{
do
{
/* jpeg_read_scanlines(...); */
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_READ_SCANLINES)
#endif
iLines = jpeg_read_scanlines (pData->pJPEGdinfo, (JSAMPARRAY)&pRow, 1);
pData->pJPEGcurrent = (mng_uint8p)pData->pJPEGdinfo->src->next_input_byte;
pData->iJPEGbufremain = (mng_uint32)pData->pJPEGdinfo->src->bytes_in_buffer;
if (iLines > 0) /* got something ? */
{
if (pData->fStorerow2) /* store in object ? */
{
iRetcode = ((mng_storerow)pData->fStorerow2) (pData);
if (iRetcode) /* on error bail out */
return iRetcode;
}
}
}
while ((pData->pJPEGdinfo->output_scanline < pData->pJPEGdinfo->output_height) &&
(iLines > 0)); /* until end-of-image or not enough input-data */
}
/* terminate output pass */
if ((pData->bJPEGprogressive) &&
(pData->pJPEGdinfo->output_scanline >= pData->pJPEGdinfo->output_height))
{
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_FINISH_OUTPUT)
#endif
if (jpeg_finish_output (pData->pJPEGdinfo) != JPEG_SUSPENDED)
{ /* this scan has ended */
pData->bJPEGscanstarted = MNG_FALSE;
pData->bJPEGscanending = MNG_FALSE;
}
else
{
pData->bJPEGscanending = MNG_TRUE;
}
}
}
while ((!jpeg_input_complete (pData->pJPEGdinfo)) &&
(iLines > 0) && (!pData->bJPEGscanending));
}
/* end of image ? */
if ((pData->bJPEGhasheader) && (pData->bJPEGdecostarted) &&
(!pData->bJPEGscanending) && (jpeg_input_complete (pData->pJPEGdinfo)) &&
(pData->pJPEGdinfo->input_scan_number == pData->pJPEGdinfo->output_scan_number))
{
/* jpeg_finish_decompress(...); */
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_FINISH_DECOMPRESS)
#endif
if (jpeg_finish_decompress (pData->pJPEGdinfo) == TRUE)
{ /* indicate it's done */
pData->bJPEGhasheader = MNG_FALSE;
pData->bJPEGdecostarted = MNG_FALSE;
pData->pJPEGcurrent = (mng_uint8p)pData->pJPEGdinfo->src->next_input_byte;
pData->iJPEGbufremain = (mng_uint32)pData->pJPEGdinfo->src->bytes_in_buffer;
/* remaining fluff is an error ! */
if ((pData->iJPEGbufremain > 0) || (iRemain > 0))
MNG_ERROR (pData, MNG_TOOMUCHJDAT)
}
}
#endif /* MNG_INCLUDE_IJG6B */
}
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_END)
#endif
return MNG_NOERROR;
}
#endif /* MNG_INCLUDE_JNG_READ */
/* ************************************************************************** */
#ifdef MNG_INCLUDE_JNG_READ
mng_retcode mngjpeg_decompressfree (mng_datap pData)
{
#if defined(MNG_INCLUDE_IJG6B) && defined(MNG_USE_SETJMP)
mng_retcode iRetcode;
#endif
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSFREE, MNG_LC_START)
#endif
#ifdef MNG_INCLUDE_IJG6B
#ifdef MNG_USE_SETJMP
iRetcode = setjmp (pData->sErrorbuf);/* setup local JPEG error-recovery */
if (iRetcode != 0) /* got here from longjmp ? */
MNG_ERRORJ (pData, iRetcode) /* then IJG-lib issued an error */
#endif
/* free the row of JPEG-samples*/
MNG_FREE (pData, pData->pJPEGrow, pData->iJPEGrowlen)
/* release the JPEG decompression object */
#ifdef MNG_SUPPORT_TRACE
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -