optimization

This commit is contained in:
Kulikova Svetlana
2021-07-21 18:04:44 +03:00
parent 9ec56fb323
commit 6f33e529e5
7 changed files with 79 additions and 20 deletions

View File

@ -52,6 +52,8 @@ bool CDjVuFile::LoadFromFile(const std::wstring& file, const std::wstring& optio
bool CDjVuFile::LoadFromMemory(BYTE* data, DWORD length, const std::wstring& options,
const std::wstring& owner_password, const std::wstring& user_password)
{
if (m_pImplementation)
return m_pImplementation->LoadFromMemory(data, length, options);
return false;
}
@ -89,6 +91,8 @@ void CDjVuFile::DrawPageOnRenderer(IRenderer* pRenderer, int nPageIndex, bool* p
}
BYTE* CDjVuFile::ConvertToPixels(int nPageIndex, int nRasterW, int nRasterH)
{
if (m_pImplementation)
return m_pImplementation->ConvertToPixels(nPageIndex, nRasterW, nRasterH);
return NULL;
}
void CDjVuFile::ConvertToRaster(int nPageIndex, const std::wstring& wsDstPath, int nImageType, const int nRasterW, const int nRasterH)

View File

@ -115,6 +115,22 @@ bool CDjVuFileImplementation::LoadFromFile(const std::wstring& wsS
return true;
}
bool CDjVuFileImplementation::LoadFromMemory(BYTE* data, DWORD length, const std::wstring& wsXmlOptions)
{
m_pDoc = NULL;
try
{
GP<ByteStream> stream = ByteStream::create(data, (size_t)length);
m_pDoc = DjVuDocument::create(stream);
m_pDoc->wait_get_pages_num();
}
catch (...)
{
return false;
}
return true;
}
void CDjVuFileImplementation::Close()
{
}
@ -202,6 +218,51 @@ void CDjVuFileImplementation::DrawPageOnRenderer(IRenderer* pRende
// белая страница
}
}
BYTE* CDjVuFileImplementation::ConvertToPixels(int nPageIndex, const int& nRasterW, const int& nRasterH)
{
if (!m_pApplicationFonts)
return NULL;
NSFonts::IFontManager *pFontManager = m_pApplicationFonts->GenerateFontManager();
NSFonts::IFontsCache* pFontCache = NSFonts::NSFontCache::Create();
pFontCache->SetStreams(m_pApplicationFonts->GetStreams());
pFontManager->SetOwnerCache(pFontCache);
NSGraphics::IGraphicsRenderer* pRenderer = NSGraphics::Create();
pRenderer->SetFontManager(pFontManager);
double dPageDpiX, dPageDpiY;
double dWidth, dHeight;
GetPageInfo(nPageIndex, &dWidth, &dHeight, &dPageDpiX, &dPageDpiX);
int nWidth = (nRasterW > 0) ? nRasterW : ((int)dWidth * 96 / dPageDpiX);
int nHeight = (nRasterH > 0) ? nRasterH : ((int)dHeight * 96 / dPageDpiX);
BYTE* pBgraData = new BYTE[nWidth * nHeight * 4];
if (!pBgraData)
return NULL;
memset(pBgraData, 0xff, nWidth * nHeight * 4);
CBgraFrame oFrame;
oFrame.put_Data(pBgraData);
oFrame.put_Width(nWidth);
oFrame.put_Height(nHeight);
oFrame.put_Stride(-4 * nWidth);
pRenderer->CreateFromBgraFrame(&oFrame);
pRenderer->SetSwapRGB(true);
pRenderer->put_Width(dWidth);
pRenderer->put_Height(dHeight);
bool bBreak = false;
DrawPageOnRenderer(pRenderer, nPageIndex, &bBreak);
RELEASEINTERFACE(pFontManager);
RELEASEOBJECT(pRenderer);
oFrame.ClearNoAttack();
return pBgraData;
}
void CDjVuFileImplementation::ConvertToRaster(int nPageIndex, const std::wstring& wsDstPath, int nImageType, const int& nRasterW, const int& nRasterH)
{
if (!m_pApplicationFonts)

View File

@ -69,12 +69,14 @@ public:
~CDjVuFileImplementation();
bool LoadFromFile(const std::wstring& wsSrcFileName, const std::wstring& wsXmlOptions = L"");
bool LoadFromMemory(BYTE* data, DWORD length, const std::wstring& wsXmlOptions = L"");
void Close();
std::wstring GetTempDirectory() const;
void SetTempDirectory(const std::wstring& wsDirectory);
int GetPagesCount() const;
void GetPageInfo(int nPageIndex, double* pdWidth, double* pdHeight, double* pdDpiX, double* pdDpiY) const;
void DrawPageOnRenderer(IRenderer* pRenderer, int nPageIndex, bool* pBreak);
BYTE* ConvertToPixels(int nPageIndex, const int& nRasterW = -1, const int& nRasterH = -1);
void ConvertToRaster(int nPageIndex, const std::wstring& wsDstPath, int nImageType, const int& nRasterW = -1, const int& nRasterH = -1);
void ConvertToPdf(const std::wstring& wsDstPath);

View File

@ -226,7 +226,7 @@ int main(int argc, char *argv[])
int pages_count = *info;
if (pages_count > 0)
{
unsigned char* pixmap = DJVU_GetPixmap(file, 0, 300, 300);
unsigned char* pixmap = DJVU_GetPixmap(file, 0, info[1] * 96 / info[3], info[2] * 96 / info[3]);
delete [] pixmap;
}

View File

@ -125,14 +125,6 @@ void CZipBuffer::move(const std::string& sSrc, const std::string& sDst)
if (it != m_arrFiles.end())
it->m_sPath = sDst;
}
// Возвращает вектор путей в архиве
std::vector<std::string> CZipBuffer::getPaths()
{
std::vector<std::string> oRes;
for (CFile& oFile : m_arrFiles)
oRes.push_back(oFile.m_sPath);
return oRes;
}
// Сохраняет архив в переданную память, полученные данные необходимо освободить
void CZipBuffer::save(BYTE*& data, DWORD& length)
{

View File

@ -28,6 +28,8 @@ private:
};
// Вектор файлов в архиве
std::vector<CFile> m_arrFiles;
friend class CZipFolderMemory;
public:
// Создает архив в памяти
CZipBuffer()

View File

@ -289,8 +289,7 @@ public:
virtual bool exists(const std::wstring& path)
{
std::string sPath = getLocalFilePathA(path);
std::vector<std::string> sPaths = m_zlib->getPaths();
return std::find(sPaths.begin(), sPaths.end(), sPath) != sPaths.end();
return std::find_if(m_zlib->m_arrFiles.begin(), m_zlib->m_arrFiles.end(), [sPath](const CZipBuffer::CFile& file){ return file.m_sPath == sPath; }) != m_zlib->m_arrFiles.end();
}
// Удаляет файл по относительному пути в архиве
virtual void remove(const std::wstring& path)
@ -302,28 +301,27 @@ public:
virtual void createDirectory(const std::wstring& path)
{
}
// Возвращает вектор путей в архиве до файлов расположенной в папке
// Возвращает вектор путей расположенных в папке
virtual std::vector<std::wstring> getFiles(const std::wstring& path, bool bIsRecursion)
{
std::string sPath = getLocalFilePathA(path);
std::vector<std::string> sPaths = m_zlib->getPaths();
std::vector<std::wstring> sRes;
for (std::string& i : sPaths)
for (const CZipBuffer::CFile& i : m_zlib->m_arrFiles)
{
if (bIsRecursion)
{
if (i.find(sPath) == 0)
sRes.push_back(L'/' + UTF8_TO_U(i));
if (i.m_sPath.find(sPath) == 0)
sRes.push_back(L'/' + UTF8_TO_U(i.m_sPath));
}
else
{
size_t nFindDirectory = i.find(sPath);
size_t nFindDirectory = i.m_sPath.find(sPath);
if (nFindDirectory == 0)
{
nFindDirectory = i.find_first_of("\\/", sPath.length());
if (nFindDirectory != std::wstring::npos && i.find_first_of("\\/", nFindDirectory + 1) == std::wstring::npos)
sRes.push_back(L'/' + UTF8_TO_U(i));
nFindDirectory = i.m_sPath.find_first_of("\\/", sPath.length());
if (nFindDirectory != std::wstring::npos && i.m_sPath.find_first_of("\\/", nFindDirectory + 1) == std::wstring::npos)
sRes.push_back(L'/' + UTF8_TO_U(i.m_sPath));
}
}
}