?? libmng_chunk_io.c
字號:
/* ************************************************************************** */
#ifndef MNG_BIGENDIAN_SUPPORTED
/* ************************************************************************** */
mng_uint32 mng_get_uint32 (mng_uint8p pBuf)
{
mng_uint32 i = ((mng_uint32)(*pBuf) << 24) +
((mng_uint32)(*(pBuf + 1)) << 16) +
((mng_uint32)(*(pBuf + 2)) << 8) +
(mng_uint32)(*(pBuf + 3));
return (i);
}
/* ************************************************************************** */
mng_int32 mng_get_int32 (mng_uint8p pBuf)
{
mng_int32 i = ((mng_int32)(*pBuf) << 24) +
((mng_int32)(*(pBuf + 1)) << 16) +
((mng_int32)(*(pBuf + 2)) << 8) +
(mng_int32)(*(pBuf + 3));
return (i);
}
/* ************************************************************************** */
mng_uint16 mng_get_uint16 (mng_uint8p pBuf)
{
mng_uint16 i = (mng_uint16)(((mng_uint16)(*pBuf) << 8) +
(mng_uint16)(*(pBuf + 1)));
return (i);
}
/* ************************************************************************** */
void mng_put_uint32 (mng_uint8p pBuf,
mng_uint32 i)
{
*pBuf = (mng_uint8)((i >> 24) & 0xff);
*(pBuf+1) = (mng_uint8)((i >> 16) & 0xff);
*(pBuf+2) = (mng_uint8)((i >> 8) & 0xff);
*(pBuf+3) = (mng_uint8)(i & 0xff);
}
/* ************************************************************************** */
void mng_put_int32 (mng_uint8p pBuf,
mng_int32 i)
{
*pBuf = (mng_uint8)((i >> 24) & 0xff);
*(pBuf+1) = (mng_uint8)((i >> 16) & 0xff);
*(pBuf+2) = (mng_uint8)((i >> 8) & 0xff);
*(pBuf+3) = (mng_uint8)(i & 0xff);
}
/* ************************************************************************** */
void mng_put_uint16 (mng_uint8p pBuf,
mng_uint16 i)
{
*pBuf = (mng_uint8)((i >> 8) & 0xff);
*(pBuf+1) = (mng_uint8)(i & 0xff);
}
/* ************************************************************************** */
#endif /* !MNG_BIGENDIAN_SUPPORTED */
/* ************************************************************************** */
/* * * */
/* * Helper routines to simplify chunk-data extraction * */
/* * * */
/* ************************************************************************** */
#ifdef MNG_INCLUDE_READ_PROCS
/* ************************************************************************** */
MNG_LOCAL mng_uint8p find_null (mng_uint8p pIn)
{
mng_uint8p pOut = pIn;
while (*pOut) /* the read_graphic routine has made sure there's */
pOut++; /* always at least 1 zero-byte in the buffer */
return pOut;
}
/* ************************************************************************** */
#if !defined(MNG_SKIPCHUNK_iCCP) && !defined(MNG_SKIPCHUNK_zTXt) && !defined(MNG_SKIPCHUNK_iTXt)
MNG_LOCAL mng_retcode inflate_buffer (mng_datap pData,
mng_uint8p pInbuf,
mng_uint32 iInsize,
mng_uint8p *pOutbuf,
mng_uint32 *iOutsize,
mng_uint32 *iRealsize)
{
mng_retcode iRetcode = MNG_NOERROR;
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_INFLATE_BUFFER, MNG_LC_START)
#endif
if (iInsize) /* anything to do ? */
{
*iOutsize = iInsize * 3; /* estimate uncompressed size */
/* and allocate a temporary buffer */
MNG_ALLOC (pData, *pOutbuf, *iOutsize)
do
{
mngzlib_inflateinit (pData); /* initialize zlib */
/* let zlib know where to store the output */
pData->sZlib.next_out = *pOutbuf;
/* "size - 1" so we've got space for the
zero-termination of a possible string */
pData->sZlib.avail_out = *iOutsize - 1;
/* ok; let's inflate... */
iRetcode = mngzlib_inflatedata (pData, iInsize, pInbuf);
/* determine actual output size */
*iRealsize = (mng_uint32)pData->sZlib.total_out;
mngzlib_inflatefree (pData); /* zlib's done */
if (iRetcode == MNG_BUFOVERFLOW) /* not enough space ? */
{ /* then get some more */
MNG_FREEX (pData, *pOutbuf, *iOutsize)
*iOutsize = *iOutsize + iInsize;
MNG_ALLOC (pData, *pOutbuf, *iOutsize)
}
while ((iRetcode == MNG_BUFOVERFLOW) &&
(*iOutsize < 20 * iInsize));
if (!iRetcode) /* if oke ? */
*((*pOutbuf) + *iRealsize) = 0; /* then put terminator zero */
}
else
{
*pOutbuf = 0; /* nothing to do; then there's no output */
*iOutsize = 0;
*iRealsize = 0;
}
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_INFLATE_BUFFER, MNG_LC_END)
#endif
return iRetcode;
}
#endif
/* ************************************************************************** */
#endif /* MNG_INCLUDE_READ_PROCS */
/* ************************************************************************** */
/* * * */
/* * Helper routines to simplify chunk writing * */
/* * * */
/* ************************************************************************** */
#ifdef MNG_INCLUDE_WRITE_PROCS
/* ************************************************************************** */
#if !defined(MNG_SKIPCHUNK_iCCP) && !defined(MNG_SKIPCHUNK_zTXt) && !defined(MNG_SKIPCHUNK_iTXt)
MNG_LOCAL mng_retcode deflate_buffer (mng_datap pData,
mng_uint8p pInbuf,
mng_uint32 iInsize,
mng_uint8p *pOutbuf,
mng_uint32 *iOutsize,
mng_uint32 *iRealsize)
{
mng_retcode iRetcode = MNG_NOERROR;
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_DEFLATE_BUFFER, MNG_LC_START)
#endif
if (iInsize) /* anything to do ? */
{
*iOutsize = (iInsize * 5) >> 2; /* estimate compressed size */
/* and allocate a temporary buffer */
MNG_ALLOC (pData, *pOutbuf, *iOutsize)
do
{
mngzlib_deflateinit (pData); /* initialize zlib */
/* let zlib know where to store the output */
pData->sZlib.next_out = *pOutbuf;
pData->sZlib.avail_out = *iOutsize;
/* ok; let's deflate... */
iRetcode = mngzlib_deflatedata (pData, iInsize, pInbuf);
/* determine actual output size */
*iRealsize = pData->sZlib.total_out;
mngzlib_deflatefree (pData); /* zlib's done */
if (iRetcode == MNG_BUFOVERFLOW) /* not enough space ? */
{ /* then get some more */
MNG_FREEX (pData, *pOutbuf, *iOutsize)
*iOutsize = *iOutsize + (iInsize >> 1);
MNG_ALLOC (pData, *pOutbuf, *iOutsize)
}
} /* repeat if we didn't have enough space */
while (iRetcode == MNG_BUFOVERFLOW);
}
else
{
*pOutbuf = 0; /* nothing to do; then there's no output */
*iOutsize = 0;
*iRealsize = 0;
}
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_DEFLATE_BUFFER, MNG_LC_END)
#endif
return iRetcode;
}
#endif
/* ************************************************************************** */
MNG_LOCAL mng_retcode write_raw_chunk (mng_datap pData,
mng_chunkid iChunkname,
mng_uint32 iRawlen,
mng_uint8p pRawdata)
{
mng_uint32 iCrc;
mng_uint32 iWritten;
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_WRITE_RAW_CHUNK, MNG_LC_START)
#endif
/* temporary buffer ? */
if ((pRawdata != 0) && (pRawdata != pData->pWritebuf+8))
{ /* store length & chunktype in default buffer */
mng_put_uint32 (pData->pWritebuf, iRawlen);
mng_put_uint32 (pData->pWritebuf+4, (mng_uint32)iChunkname);
if (pData->iCrcmode & MNG_CRC_OUTPUT)
{
if ((pData->iCrcmode & MNG_CRC_OUTPUT) == MNG_CRC_OUTPUT_GENERATE)
{ /* calculate the crc */
iCrc = update_crc (pData, 0xffffffffL, pData->pWritebuf+4, 4);
iCrc = update_crc (pData, iCrc, pRawdata, iRawlen) ^ 0xffffffffL;
} else {
iCrc = 0; /* dummy crc */
} /* store in default buffer */
mng_put_uint32 (pData->pWritebuf+8, iCrc);
}
/* write the length & chunktype */
if (!pData->fWritedata ((mng_handle)pData, pData->pWritebuf, 8, &iWritten))
MNG_ERROR (pData, MNG_APPIOERROR)
if (iWritten != 8) /* disk full ? */
MNG_ERROR (pData, MNG_OUTPUTERROR)
/* write the temporary buffer */
if (!pData->fWritedata ((mng_handle)pData, pRawdata, iRawlen, &iWritten))
MNG_ERROR (pData, MNG_APPIOERROR)
if (iWritten != iRawlen) /* disk full ? */
MNG_ERROR (pData, MNG_OUTPUTERROR)
if (pData->iCrcmode & MNG_CRC_OUTPUT)
{ /* write the crc */
if (!pData->fWritedata ((mng_handle)pData, pData->pWritebuf+8, 4, &iWritten))
MNG_ERROR (pData, MNG_APPIOERROR)
if (iWritten != 4) /* disk full ? */
MNG_ERROR (pData, MNG_OUTPUTERROR)
}
}
else
{ /* prefix with length & chunktype */
mng_put_uint32 (pData->pWritebuf, iRawlen);
mng_put_uint32 (pData->pWritebuf+4, (mng_uint32)iChunkname);
if (pData->iCrcmode & MNG_CRC_OUTPUT)
{
if ((pData->iCrcmode & MNG_CRC_OUTPUT) == MNG_CRC_OUTPUT_GENERATE)
/* calculate the crc */
iCrc = mng_crc (pData, pData->pWritebuf+4, iRawlen + 4);
else
iCrc = 0; /* dummy crc */
/* add it to the buffer */
mng_put_uint32 (pData->pWritebuf + iRawlen + 8, iCrc);
/* write it in a single pass */
if (!pData->fWritedata ((mng_handle)pData, pData->pWritebuf, iRawlen + 12, &iWritten))
MNG_ERROR (pData, MNG_APPIOERROR)
if (iWritten != iRawlen + 12) /* disk full ? */
MNG_ERROR (pData, MNG_OUTPUTERROR)
} else {
if (!pData->fWritedata ((mng_handle)pData, pData->pWritebuf, iRawlen + 8, &iWritten))
MNG_ERROR (pData, MNG_APPIOERROR)
if (iWritten != iRawlen + 8) /* disk full ? */
MNG_ERROR (pData, MNG_OUTPUTERROR)
}
}
#ifdef MNG_SUPPORT_TRACE
MNG_TRACE (pData, MNG_FN_WRITE_RAW_CHUNK, MNG_LC_END)
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -