Commit 10b32404 authored by Ilya.Kirillov's avatar Ilya.Kirillov Committed by Alexander Trofimov

Добавлены исходники чтения и записи Jpeg2000, сделано, если формат явно...

Добавлены исходники чтения и записи Jpeg2000, сделано, если формат явно указан, то чтение идет через данную библиотеку.

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@62514 954022d7-b5bf-4e40-9824-e11837661b57
parent f993d2a6
......@@ -7269,6 +7269,7 @@ DesktopEditor/mac_build/raster/raster.xcodeproj/xcuserdata svnc_tsvn_003alogmins
DesktopEditor/mac_build/raster/raster.xcodeproj/xcuserdata/alexey.musinov.xcuserdatad svnc_tsvn_003alogminsize=5
DesktopEditor/mac_build/raster/raster.xcodeproj/xcuserdata/alexey.musinov.xcuserdatad/xcschemes svnc_tsvn_003alogminsize=5
DesktopEditor/raster svn_global_002dignores=*%0A*.ncb%0A*.user%0A
DesktopEditor/raster/Jp2 svnc_tsvn_003alogminsize=5
DesktopEditor/raster/Metafile svnc_tsvn_003alogminsize=5 svn_global_002dignores
DesktopEditor/raster/Metafile/Common svnc_tsvn_003alogminsize=5
DesktopEditor/raster/Metafile/Emf svnc_tsvn_003alogminsize=5
#include "BgraFrame.h"
#include "../common/File.h"
#include "../cximage/CxImage/ximage.h"
#include "Jp2/J2kFile.h"
bool CBgraFrame::OpenFile(const std::wstring& strFileName, unsigned int nFileType)
{
NSFile::CFileBinary oFile;
if (!oFile.OpenFile(strFileName))
return false;
if (CXIMAGE_FORMAT_JP2 == nFileType)
{
Jpeg2000::CJ2kFile oJ2;
return oJ2.Open(this, strFileName, std::wstring(L""));
}
else
{
NSFile::CFileBinary oFile;
if (!oFile.OpenFile(strFileName))
return false;
CxImage img;
if( !img.Decode( oFile.GetFileNative(), nFileType ) )
return false;
CxImage img;
CxImageToMediaFrame( img );
return true;
if (!img.Decode(oFile.GetFileNative(), nFileType))
return false;
CxImageToMediaFrame(img);
return true;
}
}
bool CBgraFrame::SaveFile(const std::wstring& strFileName, unsigned int nFileType)
......@@ -22,20 +31,20 @@ bool CBgraFrame::SaveFile(const std::wstring& strFileName, unsigned int nFileTyp
NSFile::CFileBinary oFile;
if (!oFile.CreateFileW(strFileName))
return false;
uint32_t lStride = 4 * m_lWidth;
uint32_t lBitsPerPixel = 4;
if (0 != m_lStride)
{
lStride = (m_lStride > 0) ? (uint32_t)m_lStride : (uint32_t)(-m_lStride);
lBitsPerPixel = lStride / m_lWidth;
}
uint32_t lStride = 4 * m_lWidth;
uint32_t lBitsPerPixel = 4;
if (0 != m_lStride)
{
lStride = (m_lStride > 0) ? (uint32_t)m_lStride : (uint32_t)(-m_lStride);
lBitsPerPixel = lStride / m_lWidth;
}
CxImage img;
if (!img.CreateFromArray(m_pData, m_lWidth, m_lHeight, lBitsPerPixel * 8, lStride, (m_lStride >= 0) ? true : false))
return false;
if (!img.Encode( oFile.GetFileNative(), nFileType ))
if (!img.Encode(oFile.GetFileNative(), nFileType))
return false;
oFile.CloseFile();
......
This diff is collapsed.
This diff is collapsed.
#pragma once
#include "Types.h"
#include "Utils.h"
namespace Jpeg2000
{
//-------------------------------------------------------------------------------------------------------------------------------
// Image
//-------------------------------------------------------------------------------------------------------------------------------
Image* Image_CreateEmpty()
{
Image *pImage = (Image*)Malloc(sizeof(Image));
return pImage;
}
void Image_Destroy(Image *pImage)
{
if (pImage)
{
if (pImage->pComponents)
{
for (int nIndex = 0; nIndex < pImage->nCsiz; nIndex++)
{
ImageComponent *pImageComp = &pImage->pComponents[nIndex];
Free(pImageComp->pData);
}
Free(pImage->pComponents);
}
Free(pImage);
}
}
Image* Image_Create(int nComponentsCount, ImageComponentParams *pCompParams, ColorSpace eColorSpace)
{
Image *pImage = NULL;
pImage = (Image*)Malloc(sizeof(Image));
if (pImage)
{
pImage->eColorSpace = eColorSpace;
pImage->nCsiz = nComponentsCount;
//
pImage->pComponents = (ImageComponent*)Malloc(pImage->nCsiz * sizeof(ImageComponent));
if (!pImage->pComponents)
{
Image_Destroy(pImage);
return NULL;
}
for (int nCurComponent = 0; nCurComponent < nComponentsCount; nCurComponent++)
{
ImageComponent *pComponent = &pImage->pComponents[nCurComponent];
pComponent->nXRsiz = pCompParams[nCurComponent].nXRsiz;
pComponent->nYRsiz = pCompParams[nCurComponent].nYRsiz;
pComponent->nWidth = pCompParams[nCurComponent].nWidth;
pComponent->nHeight = pCompParams[nCurComponent].nHeight;
pComponent->nXOsiz = pCompParams[nCurComponent].nXoffset;
pComponent->nYOsiz = pCompParams[nCurComponent].nYoffset;
pComponent->nPrecision = pCompParams[nCurComponent].nPrecision;
pComponent->nBPP = pCompParams[nCurComponent].nBPP;
pComponent->nSigned = pCompParams[nCurComponent].nSigned;
pComponent->pData = (int*)Malloc(pComponent->nWidth * pComponent->nHeight * sizeof(int));
if (!pComponent->pData)
{
Image_Destroy(pImage);
return NULL;
}
}
}
else
{
// TO DO:
}
return pImage;
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#pragma once
#include "Types.h"
#include "ArithmeticCoder.h"
#include "DWT.h"
#include "JpgEvent.h"
#include "Image.h"
#include "J2k.h"
#include "Jp2.h"
#include "Jpt.h"
#include "Mj2.h"
#include "PacketIterator.h"
#include "Raw.h"
#include "Reader.h"
#include "TagTree.h"
#include "Tile.h"
#include "Tier1.h"
#include "Tier2.h"
#include "Utils.h"
#include <string>
#include "../../common/File.h"
namespace Jpeg2000
{
//-------------------------------------------------------------------------------------------------------------------------------
// Jpeg2000 -> Image
//-------------------------------------------------------------------------------------------------------------------------------
Image* Jp2ToImage(const std::wstring& wsFileName, DecoderParams* pDecoderParams)
{
DInfo *pDInfo = (DInfo*)Malloc(sizeof(DInfo));
if (!pDInfo)
return NULL;
pDInfo->bIsDecompressor = true;
pDInfo->pJp2 = (void*)Jp2_CreateDecompress((PCommon)pDInfo);
if (!pDInfo->pJp2)
{
Free(pDInfo);
return NULL;
}
pDInfo->pJ2k = NULL;
pDInfo->eCodecFormat = codecJP2;
Jp2_SetupDecoder((Jp2Stream*)pDInfo->pJp2, pDecoderParams);
if (JP2_ERROR_NO_ERROR != pDInfo->nErrorCode)
{
Jp2_DestroyDecompress((Jp2Stream*)pDInfo->pJp2);
Free(pDInfo);
return NULL;
}
CReader *pStream = (CReader *)new CReaderFile(wsFileName, 1);
if (!pStream)
{
Jp2_DestroyDecompress((Jp2Stream*)pDInfo->pJp2);
Free(pDInfo);
return NULL;
}
Image *pImage = Jp2_Decode((Jp2Stream*)pDInfo->pJp2, pStream); // pImage = NULL
delete (pStream);
Jp2_DestroyDecompress((Jp2Stream*)pDInfo->pJp2);
Free(pDInfo);
return pImage;
}
Image* J2kToImage(const std::wstring& wsFileName, DecoderParams* pDecoderParams)
{
DInfo *pDInfo = (DInfo*)Malloc(sizeof(DInfo));
if (!pDInfo)
return NULL;
pDInfo->bIsDecompressor = true;
pDInfo->pJ2k = (void*)J2k_CreateDecompress((PCommon)pDInfo);
if (!pDInfo->pJ2k)
{
Free(pDInfo);
return NULL;
}
pDInfo->pJp2 = NULL;
pDInfo->eCodecFormat = codecJ2K;
J2k_SetupDecoder((J2kCodestream*)pDInfo->pJ2k, pDecoderParams);
if (JP2_ERROR_NO_ERROR != pDInfo->nErrorCode)
{
J2k_DestroyDecompress((J2kCodestream*)pDInfo->pJ2k);
Free(pDInfo);
return NULL;
}
CReader *pStream = (CReader *)new CReaderFile(wsFileName, 1);
if (!pStream)
{
J2k_DestroyDecompress((J2kCodestream*)pDInfo->pJ2k);
Free(pDInfo);
return NULL;
}
Image *pImage = J2k_Decode((J2kCodestream*)pDInfo->pJ2k, pStream); // pImage = NULL
delete (pStream);
J2k_DestroyDecompress((J2kCodestream*)pDInfo->pJ2k);
Free(pDInfo);
return pImage;
}
Image* JptToImage(const std::wstring& wsFileName, DecoderParams* pDecoderParams)
{
DInfo *pDInfo = (DInfo*)Malloc(sizeof(DInfo));
if (!pDInfo)
return NULL;
pDInfo->bIsDecompressor = true;
pDInfo->pJ2k = (void*)J2k_CreateDecompress((PCommon)pDInfo);
if (!pDInfo->pJ2k)
{
Free(pDInfo);
return NULL;
}
pDInfo->pJp2 = NULL;
pDInfo->eCodecFormat = codecJPT;
J2k_SetupDecoder((J2kCodestream*)pDInfo->pJ2k, pDecoderParams);
if (JP2_ERROR_NO_ERROR != pDInfo->nErrorCode)
{
J2k_DestroyDecompress((J2kCodestream*)pDInfo->pJ2k);
Free(pDInfo);
return NULL;
}
CReader *pStream = (CReader *)new CReaderFile(wsFileName, 1);
if (!pStream)
{
J2k_DestroyDecompress((J2kCodestream*)pDInfo->pJ2k);
Free(pDInfo);
return NULL;
}
Image *pImage = J2k_DecodeJptStream((J2kCodestream*)pDInfo->pJ2k, pStream); // pImage = NULL
delete (pStream);
J2k_DestroyDecompress((J2kCodestream*)pDInfo->pJ2k);
Free(pDInfo);
return NULL;
}
Image* Mj2ToImage(const std::wstring& wsFileName, DecoderParams* pDecoderParams)
{
DInfo *pDInfo = (DInfo*)Malloc(sizeof(DInfo));
if (!pDInfo)
return NULL;
pDInfo->bIsDecompressor = true;
pDInfo->pMj2 = (void*)Mj2_CreateDecompress((PCommon)pDInfo);
if (!pDInfo->pMj2)
{
Free(pDInfo);
return NULL;
}
pDInfo->pJ2k = ((Mj2_Movie*)pDInfo->pMj2)->pJ2k;
pDInfo->eCodecFormat = codecMj2;
Mj2_SetupDecoder((Mj2_Movie*)pDInfo->pMj2, pDecoderParams);
if (JP2_ERROR_NO_ERROR != pDInfo->nErrorCode)
{
Mj2_DestroyDecompress((Mj2_Movie*)pDInfo->pMj2);
Free(pDInfo);
return NULL;
}
CReader *pStream = (CReader *)new CReaderFile(wsFileName, 1);
if (!pStream)
{
Mj2_DestroyDecompress((Mj2_Movie*)pDInfo->pMj2);
Free(pDInfo);
return NULL;
}
Image *pImage = Mj2_Decode((Mj2_Movie*)pDInfo->pMj2, pStream); // pImage = NULL
delete (pStream);
Mj2_DestroyDecompress((Mj2_Movie*)pDInfo->pMj2);
Free(pDInfo);
return pImage;
}
//-------------------------------------------------------------------------------------------------------------------------------
// Image -> Jpeg2000
//-------------------------------------------------------------------------------------------------------------------------------
bool ImageToJ2k(Image *pImage, const std::wstring& wsFilePath, EncoderParams* pEncoderParams)
{
int nCodeStreamLen = 0;
CReader *pStream = NULL;
CInfo *pCInfo = (CInfo*)Malloc(sizeof(CInfo));
if (!pCInfo)
return false;
pCInfo->pJ2k = (void*)J2k_CreateCompress((PCommon)pCInfo);
if (!pCInfo->pJ2k)
{
Free(pCInfo);
return false;
}
pCInfo->pJp2 = NULL;
pCInfo->eCodecFormat = codecJ2K;
pCInfo->bIsDecompressor = false;
J2k_SetupEncoder((J2kCodestream*)pCInfo->pJ2k, pEncoderParams, pImage);
if (JP2_ERROR_NO_ERROR != pCInfo->nErrorCode)
{
J2k_DestroyCompress((J2kCodestream*)pCInfo->pJ2k);
Free(pCInfo);
return false;
}
pStream = (CReader *)new CReaderFile(wsFilePath, 0);
if (!pStream)
{
J2k_DestroyCompress((J2kCodestream*)pCInfo->pJ2k);
Free(pCInfo);
return false;
}
bool bSuccess = J2k_Encode((J2kCodestream*)pCInfo->pJ2k, pStream, pImage, NULL);
if (!bSuccess)
{
delete (pStream);
J2k_DestroyCompress((J2kCodestream*)pCInfo->pJ2k);
Free(pCInfo);
return false;
}
nCodeStreamLen = pStream->Tell();
//if (!oFile.CreateFileW(wsFilePath))
//{
// delete (pStream);
// J2k_DestroyCompress((J2kCodestream*)pCInfo->pJ2k);
// Free(pCInfo);
// return false;
//}
//FILE* pFile = oFile.GetFileNative();
//fwrite(pStream->GetOwner(), 1, nCodeStreamLen, pFile);
//fclose(pFile);
delete (pStream);
J2k_DestroyCompress((J2kCodestream*)pCInfo->pJ2k);
Free(pCInfo);
return true;
}
bool ImageToJ2p(Image *pImage, const std::wstring& wsFilePath, EncoderParams* pEncoderParams)
{
int nCodeStreamLen = 0;
CReader *pStream = NULL;
CInfo *pCInfo = (CInfo*)Malloc(sizeof(CInfo));
if (!pCInfo)
return false;
pCInfo->pJp2 = (void*)Jp2_CreateCompress((PCommon)pCInfo);
if (!pCInfo->pJp2)
{
Free(pCInfo);
return false;
}
pCInfo->pJ2k = NULL;
pCInfo->eCodecFormat = codecJP2;
pCInfo->bIsDecompressor = false;
Jp2_SetupEncoder((Jp2Stream*)pCInfo->pJp2, pEncoderParams, pImage);
if (JP2_ERROR_NO_ERROR != pCInfo->nErrorCode)
{
Jp2_DestroyCompress((Jp2Stream*)pCInfo->pJp2);
Free(pCInfo);
return false;
}
pStream =(CReader *) new CReaderStream(NULL, 0);
if (!pStream)
{
Jp2_DestroyCompress((Jp2Stream*)pCInfo->pJp2);
Free(pCInfo);
return false;
}
bool bSuccess = Jp2_Encode((Jp2Stream*)pCInfo->pJp2, pStream, pImage, NULL);
if (!bSuccess)
{
delete (pStream);
Jp2_DestroyCompress((Jp2Stream*)pCInfo->pJp2);
Free(pCInfo);
return false;
}
nCodeStreamLen = pStream->Tell();
NSFile::CFileBinary oFile;
if (!oFile.CreateFileW(wsFilePath))
{
delete (pStream);
Jp2_DestroyCompress((Jp2Stream*)pCInfo->pJp2);
Free(pCInfo);
return false;
}
FILE* pFile = oFile.GetFileNative();
fwrite(pStream->GetOwner(), 1, nCodeStreamLen, pFile);
fclose(pFile);
delete (pStream);
Jp2_DestroyCompress((Jp2Stream*)pCInfo->pJp2);
Free(pCInfo);
return true;
}
}
\ No newline at end of file
This diff is collapsed.
#pragma once
#include "Types.h"
namespace Jpeg2000
{
//-------------------------------------------------------------------------------------------------------------------------------
// Events
//-------------------------------------------------------------------------------------------------------------------------------
bool Event_Message(int nEventType, const char *sFormatString, ...)
{
if (sFormatString != NULL)
{
char sMessage[MSG_SIZE]={};
va_list sArg;
memset(sMessage, 0, MSG_SIZE);
va_start(sArg, sFormatString);
int nLength = (strlen(sFormatString) > MSG_SIZE) ? MSG_SIZE : strlen(sFormatString);
vsprintf(sMessage, sFormatString, sArg);
va_end(sArg);
// TODO: print sMessage/*, pCodecInfo->pClientData*/
}
return true;
}
}
\ No newline at end of file
#pragma once
#include "Types.h"
#include "Reader.h"
namespace Jpeg2000
{
//-------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------
static unsigned int JPT_ReadVBASInfo(CReader *pStream, unsigned int nValue)
{
unsigned char nElement = pStream->Read(1);
while ((nElement >> 7) == 1)
{
nValue = (nValue << 7);
nValue |= (nElement & 0x7f);
nElement = pStream->Read(1);
}
nValue = (nValue << 7);
nValue |= (nElement & 0x7f);
return nValue;
}
static void JPT_ReInitMessageHeader(JPTMessageHeader *pHeader)
{
pHeader->nId = 0;
pHeader->nLastByte = 0;
pHeader->nMessageOffset = 0;
pHeader->nMessageLength = 0;
}
//-------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------
void JPT_InitMessageHeader(JPTMessageHeader *pHeader)
{
pHeader->nId = 0;
pHeader->nLastByte = 0;
pHeader->nClassId = 0;
pHeader->nCSnId = 0;
pHeader->nMessageOffset = 0;
pHeader->nMessageLength = 0;
pHeader->nLayerNB = 0;
}
void JPT_ReadMessageHeader(PCommon pInfo, CReader *pStream, JPTMessageHeader *pHeader)
{
JPT_ReInitMessageHeader(pHeader);
// VBAS : Bin-ID
unsigned char nElement = pStream->Read(1);
// See for Class and CSn
unsigned char nClass = 0, nCSn = 0;
switch ((nElement >> 5) & 0x03)
{
case 0:
Event_Message(EVT_ERROR, "Forbidden value encounter in message header !!\n");
break;
case 1:
nClass = 0;
nCSn = 0;
break;
case 2:
nClass = 1;
nCSn = 0;
break;
case 3:
nClass = 1;
nCSn = 1;
break;
default:
break;
}
// [A.2.1 ISO/IEC FCD 15444-9]
if (((nElement >> 4) & 0x01) == 1)
pHeader->nLastByte = 1;
// In-class identifier
pHeader->nId |= (nElement & 0x0f);
if ((nElement >> 7) == 1)
pHeader->nId = JPT_ReadVBASInfo(pStream, pHeader->nId);
// VBAS : Class
if (nClass == 1)
{
pHeader->nClassId = 0;
pHeader->nClassId = JPT_ReadVBASInfo(pStream, pHeader->nClassId);
}
// VBAS : CSn
if (nCSn == 1)
{
pHeader->nCSnId = 0;
pHeader->nCSnId = JPT_ReadVBASInfo(pStream, pHeader->nCSnId);
}
// VBAS : Msg_offset
pHeader->nMessageOffset = JPT_ReadVBASInfo(pStream, pHeader->nMessageOffset);
// VBAS : Msg_length
pHeader->nMessageLength = JPT_ReadVBASInfo(pStream, pHeader->nMessageLength);
// VBAS : Aux
if ((pHeader->nClassId & 0x01) == 1)
{
pHeader->nLayerNB = 0;
pHeader->nLayerNB = JPT_ReadVBASInfo(pStream, pHeader->nLayerNB);
}
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
#pragma once
#include "Types.h"
#include "Utils.h"
namespace Jpeg2000
{
//-------------------------------------------------------------------------------------------------------------------------------
Raw* Raw_Create()
{
Raw *pRaw = (Raw*)Malloc(sizeof(Raw));
return pRaw;
}
void Raw_Destroy(Raw *pRaw)
{
Free(pRaw);
}
int Raw_BytesCount(Raw *pRaw)
{
return pRaw->pBufferPointer - pRaw->pBufferStart;
}
void Raw_InitDecoder(Raw *pRaw, unsigned char *pBuffer, int nLen)
{
pRaw->pBufferStart = pBuffer;
pRaw->nMaxLen = nLen;
pRaw->nLen = 0;
pRaw->nBuffer = 0;
pRaw->nFreeBitsCount = 0;
}
int Raw_Decode(Raw *pRaw)
{
if (pRaw->nFreeBitsCount == 0)
{
pRaw->nFreeBitsCount = 8;
if (pRaw->nLen == pRaw->nMaxLen)
{
pRaw->nBuffer = 0xff;
}
else
{
if (pRaw->nBuffer == 0xff)
{
pRaw->nFreeBitsCount = 7;
}
pRaw->nBuffer = *(pRaw->pBufferStart + pRaw->nLen);
pRaw->nLen++;
}
}
pRaw->nFreeBitsCount--;
int nResult = (pRaw->nBuffer >> pRaw->nFreeBitsCount) & 0x01;
return nResult;
}
}
\ No newline at end of file
This diff is collapsed.
#pragma once
#include "../../common/File.h"
namespace Jpeg2000
{
//-------------------------------------------------------------------------------------------------------------------------------
// Byte input-output (Stream IO)
//-------------------------------------------------------------------------------------------------------------------------------
#define STREAM_READ 0x0001 //
#define STREAM_WRITE 0x0002 //
typedef struct TByteIO
{
int nMode; // ( STREAM_READ OPJ_STREAM_WRITE )
unsigned char *pBuffer; //
int nLength; //
unsigned char *pStart; //
unsigned char *pEnd; //
unsigned char *pCurPos; //
} ByteIO;
class CReader
{
public:
virtual ~CReader(void){}
virtual bool Open(void* sStream, int nSizeStream){ return false; }
virtual void Close(){}
virtual void Read(BYTE* pData, int nSize){}
virtual unsigned int Read(int nSize){ return 0; }
virtual void Skip(int nSize){}
virtual void Seek(int nPosition){}
virtual unsigned int Write(unsigned int nValue, int nLen){ return 0; }
virtual int Tell(){ return 0; }
virtual int GetLeftSize(){ return 0; }
virtual void* GetOwner(){ return NULL; }
};
class CReaderStream : CReader
{
public:
CReaderStream(void);
CReaderStream(void* sStream, int nSizeStream);
virtual ~CReaderStream(void);
bool Open(void* sStream, int nSizeStream);
void Close();
void Read(BYTE* pData, int nSize);
unsigned int Read(int nSize);
void Skip(int nSize);
void Seek(int nPosition);
unsigned int Write(unsigned int nValue, int nLen);
int Tell();
int GetLeftSize();
void* GetOwner();
protected:
ByteIO* m_pStream;
int m_nPosition;
int m_nSkiped;
int m_nSize;
};
class CReaderFile : CReader
{
public:
CReaderFile(void);
CReaderFile(const std::wstring& wsFileName, long lMode);
virtual ~CReaderFile(void);
bool Open(const std::wstring& wsFileName, int nSizeStream);
void Close();
void Read(BYTE* pData, int nSize);
unsigned int Read(int nSize);
void Skip(int nSize);
void Seek(int nPosition);
unsigned int Write(unsigned int nValue, int nLen);
int Tell();
int GetLeftSize();
void* GetOwner();
protected:
NSFile::CFileBinary m_oFile;
FILE* m_pFile;
int m_nPosition;
int m_nSkiped;
int m_nSize;
};
}
\ No newline at end of file
#pragma once
#include "Types.h"
#include "JpgEvent.h"
#include "Utils.h"
namespace Jpeg2000
{
//-------------------------------------------------------------------------------------------------------------------------------
// Bit input-output stream (BitIO)
//-------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------
static int BitIO_ByteOut(BitIO *pBIO)
{
pBIO->nBuffer = (pBIO->nBuffer << 8) & 0xffff;
pBIO->nValidBitsCount = pBIO->nBuffer == 0xff00 ? 7 : 8;
if (pBIO->pCurPos >= pBIO->pEnd)
{
return 1;
}
*pBIO->pCurPos++ = pBIO->nBuffer >> 8;
return 0;
}
static int BitIO_ByteIn(BitIO *pBIO)
{
pBIO->nBuffer = (pBIO->nBuffer << 8) & 0xffff;
pBIO->nValidBitsCount = pBIO->nBuffer == 0xff00 ? 7 : 8;
if (pBIO->pCurPos >= pBIO->pEnd)
{
return 1;
}
pBIO->nBuffer |= *pBIO->pCurPos++;
return 0;
}
static void BitIO_PutBit(BitIO *pBIO, int nBit)
{
if (0 == pBIO->nValidBitsCount)
{
BitIO_ByteOut(pBIO);
}
pBIO->nValidBitsCount--;
pBIO->nBuffer |= nBit << pBIO->nValidBitsCount;
}
static int BitIO_GetBit(BitIO *pBIO)
{
if (0 == pBIO->nValidBitsCount)
{
BitIO_ByteIn(pBIO);
}
pBIO->nValidBitsCount--;
return (pBIO->nBuffer >> pBIO->nValidBitsCount) & 1;
}
//-------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------
BitIO* BitIO_Create()
{
BitIO *pBIO = (BitIO*)Malloc(sizeof(BitIO));
return pBIO;
}
void BitIO_Destroy(BitIO *pBIO)
{
Free(pBIO);
}
int BitIO_WrittenBytesCount(BitIO *pBIO)
{
return (pBIO->pCurPos - pBIO->pStart);
}
void BitIO_InitEncoder(BitIO *pBIO, unsigned char *pBuffer, int nLen)
{
pBIO->pStart = pBuffer;
pBIO->pEnd = pBuffer + nLen;
pBIO->pCurPos = pBuffer;
pBIO->nBuffer = 0;
pBIO->nValidBitsCount = 8;
}
void BitIO_InitDecoder(BitIO *pBIO, unsigned char *pBuffer, int nLen)
{
pBIO->pStart = pBuffer;
pBIO->pEnd = pBuffer + nLen;
pBIO->pCurPos = pBuffer;
pBIO->nBuffer = 0;
pBIO->nValidBitsCount = 0;
}
void BitIO_Write(BitIO *pBIO, int nValue, int nLen)
{
for (int nIndex = nLen - 1; nIndex >= 0; nIndex--)
{
BitIO_PutBit(pBIO, (nValue >> nIndex) & 1);
}
}
int BitIO_Read(BitIO *pBIO, int nLen)
{
int nResult = 0;
for (int nIndex = nLen - 1; nIndex >= 0; nIndex--)
{
nResult += BitIO_GetBit(pBIO) << nIndex;
}
return nResult;
}
int BitIO_Flush(BitIO *pBIO)
{
pBIO->nValidBitsCount = 0;
if (BitIO_ByteOut(pBIO))
{
return 1;
}
if (7 == pBIO->nValidBitsCount)
{
pBIO->nValidBitsCount = 0;
if (BitIO_ByteOut(pBIO))
{
return 1;
}
}
return 0;
}
int BitIO_InAlign(BitIO *pBIO)
{
pBIO->nValidBitsCount = 0;
if ((pBIO->nBuffer & 0xff) == 0xff)
{
if (BitIO_ByteIn(pBIO))
{
return 1;
}
pBIO->nValidBitsCount = 0;
}
return 0;
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment