Refactoring RenderOutputDev and test diff

This commit is contained in:
Svetlana Kulikova
2024-08-08 18:42:43 +03:00
parent 6554185ec7
commit 8d6e15627b
2 changed files with 414 additions and 1463 deletions

View File

@ -36,6 +36,7 @@
#include "../../DesktopEditor/fontengine/ApplicationFontsWorker.h"
#include "../../DesktopEditor/xmlsec/src/include/CertificateCommon.h"
#include "../../DesktopEditor/graphics/MetafileToGraphicsRenderer.h"
#include "../../DesktopEditor/graphics/pro/Graphics.h"
#include "../PdfFile.h"
#include "../../DjVuFile/DjVu.h"
@ -51,6 +52,10 @@ public:
std::wstring wsSrcFile;
std::wstring wsDstFile;
std::wstring strDirIn;
std::wstring strDirOut;
std::wstring strDiffs;
public:
static void SetUpTestSuite()
{
@ -130,6 +135,10 @@ public:
wsSrcFile = NSFile::GetProcessDirectory() + L"/test.pdf";
wsDstFile = NSFile::GetProcessDirectory() + L"/test2.pdf";
strDirIn = NSFile::GetProcessDirectory() + L"/resI";
strDirOut = NSFile::GetProcessDirectory() + L"/resO";
strDiffs = NSFile::GetProcessDirectory() + L"/resD";
pdfFile = new CPdfFile(pApplicationFonts);
pdfFile->SetTempDirectory(wsTempDir);
}
@ -248,7 +257,7 @@ TEST_F(CPdfFileTest, SetMetaData)
TEST_F(CPdfFileTest, ConvertToRaster)
{
//GTEST_SKIP();
GTEST_SKIP();
LoadFromFile();
@ -257,7 +266,7 @@ TEST_F(CPdfFileTest, ConvertToRaster)
for (i = 0; i < pdfFile->GetPagesCount(); i++)
{
pdfFile->GetPageInfo(i, &dWidth, &dHeight, &dPageDpiX, &dPageDpiY);
pdfFile->ConvertToRaster(i, NSFile::GetProcessDirectory() + L"/res/res" + std::to_wstring(i) + L".png", 4, dWidth, dHeight, true, pdfFile->GetFontManager());
pdfFile->ConvertToRaster(i, NSFile::GetProcessDirectory() + L"/resO/res" + std::to_wstring(i) + L".png", 4, dWidth, dHeight, true, pdfFile->GetFontManager());
}
}
@ -408,3 +417,191 @@ TEST_F(CPdfFileTest, ChangePasswordToPassword)
LoadFromFile();
EXPECT_HRESULT_SUCCEEDED(pdfFile->ChangePassword(wsDstFile, L"123456"));
}
TEST_F(CPdfFileTest, ImgDiff)
{
//GTEST_SKIP();
LoadFromFile();
int nCountInPages = pdfFile->GetPagesCount();
for (int nPage = 0; nPage < nCountInPages; ++nPage)
{
std::wstring sPageI = strDirIn + L"/res" + std::to_wstring(nPage) + L".png";
std::wstring sPageO = strDirOut + L"/res" + std::to_wstring(nPage) + L".png";
std::wstring sPageDiff = strDiffs + L"/res" + std::to_wstring(nPage) + L".png";
CBgraFrame frameI;
frameI.OpenFile(sPageI);
CBgraFrame frameO;
frameO.OpenFile(sPageO);
int nW_I = frameI.get_Width();
int nH_I = frameI.get_Height();
int nW_O = frameO.get_Width();
int nH_O = frameO.get_Height();
if (nW_I != nW_O || nH_I != nH_O)
{
if (!NSDirectory::Exists(strDiffs))
NSDirectory::CreateDirectories(CorrectPathW(strDiffs));
std::wstring sFilePagesDiff = sPageDiff;
NSFile::CFileBinary oFile;
oFile.CreateFileW(sPageDiff);
oFile.WriteStringUTF8(L"sizes!");
oFile.CloseFile();
continue;
}
BYTE* pDataI = frameI.get_Data();
BYTE* pDataO = frameO.get_Data();
size_t sizeMemory = 4 * nW_I * nH_I;
if (0 == memcmp(pDataI, pDataO, sizeMemory))
continue;
int nEpsilonEps = 3;
int nEpsilonNatural = 5;
int nDivExist = 0;
for (int indexPixH = 0; indexPixH < nH_I; indexPixH++)
{
for (int indexPixW = 0; indexPixW < nW_I; indexPixW++)
{
if (pDataI[0] != pDataO[0] || pDataI[1] != pDataO[1] || pDataI[2] != pDataO[2])
{
// test epsilon natural
if ((abs(pDataI[0] - pDataO[0]) < nEpsilonNatural) &&
(abs(pDataI[1] - pDataO[1]) < nEpsilonNatural) &&
(abs(pDataI[2] - pDataO[2]) < nEpsilonNatural))
{
pDataI += 4;
pDataO += 4;
continue;
}
// test epsilon left, right, top, bottom
int nEpsUp = nEpsilonEps;
if (indexPixH > 0)
{
BYTE* pByteI = frameI.get_Data() + 4 * (indexPixH - 1) * nW_I + 4 * indexPixW;
if ((abs(pByteI[0] - pDataO[0]) < nEpsilonEps) &&
(abs(pByteI[1] - pDataO[1]) < nEpsilonEps) &&
(abs(pByteI[2] - pDataO[2]) < nEpsilonEps))
{
nEpsUp = nEpsilonEps - 1;
}
}
int nEpsDown = nEpsilonEps;
if (indexPixH < (nH_I - 1))
{
BYTE* pByteI = frameI.get_Data() + 4 * (indexPixH + 1) * nW_I + 4 * indexPixW;
if ((abs(pByteI[0] - pDataO[0]) < nEpsilonEps) &&
(abs(pByteI[1] - pDataO[1]) < nEpsilonEps) &&
(abs(pByteI[2] - pDataO[2]) < nEpsilonEps))
{
nEpsDown = nEpsilonEps - 1;
}
}
int nEpsLeft = nEpsilonEps;
if (indexPixW > 0)
{
BYTE* pByteI = pDataI - 4;
if ((abs(pByteI[0] - pDataO[0]) < nEpsilonEps) &&
(abs(pByteI[1] - pDataO[1]) < nEpsilonEps) &&
(abs(pByteI[2] - pDataO[2]) < nEpsilonEps))
{
nEpsLeft = nEpsilonEps - 1;
}
}
int nEpsRight = nEpsilonEps;
if (indexPixW < (nW_I - 1))
{
BYTE* pByteI = pDataI + 4;
if ((abs(pByteI[0] - pDataO[0]) < nEpsilonEps) &&
(abs(pByteI[1] - pDataO[1]) < nEpsilonEps) &&
(abs(pByteI[2] - pDataO[2]) < nEpsilonEps))
{
nEpsRight = nEpsilonEps - 1;
}
}
if ((nEpsLeft < nEpsilonEps) ||
(nEpsRight < nEpsilonEps) ||
(nEpsUp < nEpsilonEps) ||
(nEpsDown < nEpsilonEps))
{
pDataI += 4;
pDataO += 4;
continue;
}
++nDivExist;
if (pDataO[0] == 0x00 && pDataO[1] == 0x00 && pDataO[2] == 0xFF)
{
pDataO[0] = 0xFF;
pDataO[1] = 0x00;
pDataO[2] = 0x00;
}
else
{
pDataO[0] = 0x00;
pDataO[1] = 0x00;
pDataO[2] = 0xFF;
}
}
pDataI += 4;
pDataO += 4;
}
}
if (nDivExist > 7)
{
if (!NSDirectory::Exists(strDiffs))
NSDirectory::CreateDirectories(CorrectPathW(strDiffs));
CBgraFrame frameOSrc;
frameOSrc.OpenFile(sPageO);
BYTE* pData1 = frameI.get_Data();
BYTE* pData2 = frameOSrc.get_Data();
BYTE* pData3 = frameO.get_Data();
int nRowW = 4 * nW_I;
BYTE* pDataAll = new BYTE[3 * nRowW * nH_I];
BYTE* pDataAllSrc = pDataAll;
for (int j = 0; j < nH_I; j++)
{
memcpy(pDataAll, pData1, nRowW);
pDataAll += nRowW;
pData1 += nRowW;
memcpy(pDataAll, pData2, nRowW);
pDataAll += nRowW;
pData2 += nRowW;
memcpy(pDataAll, pData3, nRowW);
pDataAll += nRowW;
pData3 += nRowW;
}
CBgraFrame oFrameAll;
oFrameAll.put_Data(pDataAllSrc);
oFrameAll.put_Width(3 * nW_I);
oFrameAll.put_Height(nH_I);
oFrameAll.put_Stride(-3 * nRowW);
oFrameAll.SaveFile(sPageDiff, 4);
}
}
}