Files
core/DesktopEditor/raster/JBig2/source/JBig2File.cpp
Elen.Subbotina 3f3414e50c MetaFile из HtmlRenderer в raster, добавление Jp2 файлов в win32 проект raster
git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@62576 954022d7-b5bf-4e40-9824-e11837661b57
2016-05-21 00:03:16 +03:00

208 lines
4.4 KiB
C++

// JBig2File.cpp : Implementation of CJBig2File
#include "JBig2File.h"
#include "Encoder/jbig2encoder.h"
#if defined(_WIN32) || defined(_WIN64)
#include <tchar.h>
#endif
#include "../../../common/File.h"
CJBig2File::CJBig2File()
{
m_bDuplicateLineRemoval = false;
m_bPDFMode = true;
m_bSymbolMode = false;
m_bRefine = false;
m_bUpscale2x = false;
m_bUpscale4x = false;
m_bSegment = false;
m_dTreshold = 0.85;
m_nBwTreshold = 188;
}
bool CJBig2File::MemoryToJBig2(unsigned char* pBufferBGRA ,int BufferSize, int nWidth, int nHeight, std::wstring sDstFileName)
{
// check for valid input parameters
///////////////////////////////////////////////////////////
if ( NULL == pBufferBGRA ) return false;
int lBufferSize = BufferSize;
unsigned char *pSourceBuffer = pBufferBGRA;
PIX *pSource = pixCreate( nWidth, nHeight, 32 );
if ( !pSource ) return false;
for ( int nY = 0; nY < nHeight; nY++ )
{
for ( int nX = 0; nX < nWidth; nX++, pSourceBuffer += 3 )//todooo ñäåëàòü 3 ? 4
{
pixSetRGBPixel( pSource, nX, nY, pSourceBuffer[ 2 ], pSourceBuffer[ 1 ], pSourceBuffer[ 0 ] );
}
}
jbig2ctx *pContext = jbig2_init( m_dTreshold, 0.5, 0, 0, ! m_bPDFMode, m_bRefine ? 10 : -1 );
// Ïîêà ñäåëàåì çàïèñü îäíîé êàðòèíêè â JBig2
// TO DO: íàäî áóäåò ñäåëàòü çàïèñü íåñêîëüêèõ êàðòèíîê â 1 JBig2 ôàéë
// Óáèðàåì ColorMap
PIX *pPixL = NULL;
if ( NULL == ( pPixL = pixRemoveColormap( pSource, REMOVE_CMAP_BASED_ON_SRC ) ) )
{
pixDestroy( &pSource );
jbig2_destroy( pContext );
return false;
}
pixDestroy( &pSource );
PIX *pPixT = NULL;
if ( pPixL->d > 1 )
{
PIX *pGray = NULL;
if ( pPixL->d > 8 )
{
pGray = pixConvertRGBToGrayFast( pPixL );
if ( !pGray )
{
pixDestroy( &pSource );
jbig2_destroy( pContext );
return false;
}
}
else
{
pGray = pixClone( pPixL );
}
if ( m_bUpscale2x )
{
pPixT = pixScaleGray2xLIThresh( pGray, m_nBwTreshold );
}
else if ( m_bUpscale4x )
{
pPixT = pixScaleGray4xLIThresh( pGray, m_nBwTreshold );
}
else
{
pPixT = pixThresholdToBinary( pGray, m_nBwTreshold );
}
pixDestroy( &pGray );
}
else
{
pPixT = pixClone( pPixL );
}
if ( m_sOutputTreshold.length() > 0 )
{
pixWrite( m_sOutputTreshold.c_str(), pPixT, IFF_BMP );
}
if ( m_bSegment && pPixL->d > 1 )
{
PIX *pGraphics = segment_image( pPixT, pPixL );
if ( pGraphics )
{
char *sFilename;
asprintf( &sFilename, "%s.%04d.%s", m_sBaseName.c_str(), 0, ".bmp" );
pixWrite( sFilename, pGraphics, IFF_BMP );
free( sFilename );
}
if ( !pPixT )
{
// Íè÷åãî íå äåëàåì
return true;
}
}
pixDestroy( &pPixL );
if ( !m_bSymbolMode )
{
int nLength = 0;
uint8_t *pBuffer = jbig2_encode_generic( pPixT, !m_bPDFMode, 0, 0, m_bDuplicateLineRemoval, &nLength );
bool bRes = true;
NSFile::CFileBinary file;
if (file.CreateFileW(sDstFileName ) == true )
{
file.WriteFile(pBuffer, nLength);
file.CloseFile();
bRes = true;
}
else
bRes = false;
pixDestroy( &pPixT );
if ( pBuffer ) free( pBuffer );
jbig2_destroy( pContext );
return bRes;
}
int nNumPages = 1;
jbig2_add_page( pContext, pPixT );
pixDestroy( &pPixT );
int nLength = 0;
uint8_t *pBuffer = jbig2_pages_complete( pContext, &nLength );
if ( !pBuffer )
{
jbig2_destroy( pContext );
return false;
}
if ( m_bPDFMode )
{
std::wstring sFileName = sDstFileName;//m_sBaseName + _T(".sym");
NSFile::CFileBinary file;
if ( file.CreateFileW(sFileName) == false)
{
free( pBuffer );
jbig2_destroy( pContext );
return false;
}
file.WriteFile( pBuffer, nLength );
file.CloseFile();
}
free( pBuffer );
for ( int nIndex = 0; nIndex < nNumPages; ++nIndex )
{
pBuffer = jbig2_produce_page( pContext, nIndex, -1, -1, &nLength );
if ( m_bPDFMode )
{
std::wstring sFileName = m_sBaseName + L".0000";
NSFile::CFileBinary file;
if ( file.CreateFileW(sFileName) ==false)
{
free( pBuffer );
jbig2_destroy( pContext );
return false;
}
file.WriteFile( pBuffer, nLength );
file.CloseFile();
}
free( pBuffer );
}
jbig2_destroy( pContext );
return true;
}
// CJBig2File