Skip to content

Commit

Permalink
Support comparison between different resolutions
Browse files Browse the repository at this point in the history
Signed-off-by: Kyuwon Kim <[email protected]>
  • Loading branch information
chammoru committed May 10, 2022
1 parent 402ff0a commit 857138f
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 60 deletions.
19 changes: 11 additions & 8 deletions Comparer/ComparerDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void CComparerDoc::LoadSourceImage(ComparerPane *pane)
}
}

pane->OpenFrmSrc();
pane->OpenFrmSrc(mSortedCscInfo, mW, mH);

CMainFrame* pMainFrm = static_cast<CMainFrame*>(AfxGetMainWnd());
double fps = pane->GetFps();
Expand Down Expand Up @@ -314,29 +314,32 @@ void CComparerDoc::RefleshPaneImages(ComparerPane *pane, bool settingChanged)

void CComparerDoc::ProcessDocument(ComparerPane *pane)
{
int w = 0, h = 0;
bool success = pane->GetResolution(pane->pathName , &w, &h);
int srcW = 0, srcH = 0;
bool success = pane->GetResolution(pane->pathName , &srcW, &srcH);
if (!success) {
LOGWRN("FrmSrc failed to get resolution info");
return;
}
pane->srcW = srcW;
pane->srcH = srcH;

bool settingChanged = false;
if (mW != w || mH != h) {
LOGINF("new width (%d) and height (%d)", w, h);
if (!mDiffRes && (mW != srcW || mH != srcH)) {
LOGINF("new width (%d) and height (%d)", srcW, srcH);
for (auto other : GetOtherPanes(pane)) {
if (!other->pathName.IsEmpty())
other->Release();
}

mW = w;
mH = h;
mW = srcW;
mH = srcH;

settingChanged = true;
}

bool doReisze = srcW != mW || srcH != mH;
CComparerView* pView = pane->pView;
const struct qcsc_info* ci = pane->GetColorSpace(pane->pathName, mSortedCscInfo);
const struct qcsc_info* ci = pane->GetColorSpace(pane->pathName, mSortedCscInfo, doReisze);
if (ci == NULL) {
LOGWRN("color space is not found");
} else {
Expand Down
14 changes: 8 additions & 6 deletions Comparer/ComparerPane.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ struct SQPane
csc2yuv420 = NULL;
}

void OpenFrmSrc(const CString &pathName)
void OpenFrmSrc(const CString &pathName, struct qcsc_info* sortedCscInfo,
int srcW, int srcH, int dstW, int dstH)
{
closeFrmSrcs();

auto it = std::begin(frmSrcs);
for (; it != std::end(frmSrcs); it++) {
bool ok = (*it)->Open(pathName);
bool ok = (*it)->Open(pathName, sortedCscInfo, srcW, srcH, dstW, dstH);
if (ok) {
frmSrc = *it;
break;
Expand Down Expand Up @@ -118,9 +119,9 @@ struct SQPane
}

inline const struct qcsc_info* GetColorSpace(CString &pathName,
struct qcsc_info* sortedCscInfo) {
struct qcsc_info* sortedCscInfo, bool doResize) {
for (auto it = std::begin(frmSrcs); it != std::end(frmSrcs); it++) {
const qcsc_info* ci = (*it)->GetColorSpace(pathName, sortedCscInfo);
const qcsc_info* ci = (*it)->GetColorSpace(pathName, sortedCscInfo, doResize);
if (ci != NULL)
return ci;
}
Expand All @@ -147,6 +148,7 @@ struct ComparerPane : public SQPane
CString pathName;
int bufOffset2, bufOffset3;
CComparerView *pView;
int srcW, srcH;

long frames;

Expand All @@ -163,9 +165,9 @@ struct ComparerPane : public SQPane
{
}

void OpenFrmSrc()
void OpenFrmSrc(struct qcsc_info* sortedCscInfo, int dstW, int dstH)
{
SQPane::OpenFrmSrc(pathName);
SQPane::OpenFrmSrc(pathName, sortedCscInfo, srcW, srcH, dstW, dstH);
frames = QMAX(frmSrc->GetFrameNum(), 1);
}

Expand Down
3 changes: 2 additions & 1 deletion Comparer/FileScanThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ SmpError FileScanThread::readyToRun()
const ComparerPane *pane = &mDoc->mPane[i];
SQPane *scanInfo = &mScanInfo[i];
scanInfo->closeFrmSrcs();
scanInfo->OpenFrmSrc(pane->pathName);
scanInfo->origSceneSize = pane->origSceneSize;
if (scanInfo->origSceneSize > scanInfo->origBufSize) {
if (scanInfo->origBuf)
Expand All @@ -62,6 +61,8 @@ SmpError FileScanThread::readyToRun()
scanInfo->csc2yuv420 = pane->csc2yuv420;
scanInfo->csc2rgb888 = pane->csc2rgb888;
scanInfo->csLoadInfo = pane->csLoadInfo;
scanInfo->OpenFrmSrc(pane->pathName, mDoc->mSortedCscInfo,
pane->srcW, pane->srcH, mDoc->mW, mDoc->mH);
}

mFrmCmpStrategy = mDoc->mFrmCmpStrategy;
Expand Down
7 changes: 4 additions & 3 deletions Comparer/FrmSrc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ class FrmSrc
public:
FrmSrc(SQPane* pane, bool fixedResolution);
virtual ~FrmSrc() {}
virtual bool Open(const CString& filePath) = 0;
virtual bool Open(const CString& filePath, const struct qcsc_info* sortedCscInfo,
int srcW, int srcH, int dstW, int dstH) = 0;
virtual int GetFrameNum() = 0;
virtual void Release() = 0;
virtual bool GetResolution(CString &pathName, int* w, int* h) = 0;
virtual const struct qcsc_info* GetColorSpace(CString &pathName,
struct qcsc_info* sortedCscInfo) = 0;
virtual const struct qcsc_info* GetColorSpace(const CString &pathName,
const struct qcsc_info* sortedCscInfo, bool doReisze) = 0;
virtual bool IsAvailable() = 0;
virtual bool FillSceneBuf(BYTE *origBuf) = 0;
virtual long GetNextFrameID() = 0;
Expand Down
2 changes: 2 additions & 0 deletions Comparer/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ void CMainFrame::RefreshAllViews()
for (int i = 0; i < mViews; i++) {
ComparerPane* pane = &pDoc->mPane[i];
if (pane->isAvail()) {
pane->srcW = pDoc->mW;
pane->srcH = pDoc->mH;
pDoc->RefleshPaneImages(pane, true);
refreshed = true;
break;
Expand Down
13 changes: 9 additions & 4 deletions Comparer/MatFrmSrc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ MatFrmSrc::~MatFrmSrc()
Release();
}

bool MatFrmSrc::Open(const CString& filePath)
bool MatFrmSrc::Open(const CString& filePath, const struct qcsc_info* sortedCscInfo,
int srcW, int srcH, int dstW, int dstH)
{
String str = CT2A(filePath.GetString());
mOcvMat = imread(str);
return mOcvMat.data != NULL;
bool success = mOcvMat.data != NULL;
if (success && (mOcvMat.cols != dstW || mOcvMat.rows != dstH)) {
resize(mOcvMat, mOcvMat, Size(dstW, dstH));
}
return success;
}

int MatFrmSrc::GetFrameNum()
Expand Down Expand Up @@ -44,8 +49,8 @@ bool MatFrmSrc::GetResolution(CString &pathName, int* w, int* h)
return true;
}

const struct qcsc_info* MatFrmSrc::GetColorSpace(CString &pathName,
struct qcsc_info* sortedCscInfo)
const struct qcsc_info* MatFrmSrc::GetColorSpace(const CString &pathName,
const struct qcsc_info* sortedCscInfo, bool doReisze)
{
String str = CT2A(pathName.GetString());
Mat ocvMat = imread(str);
Expand Down
7 changes: 4 additions & 3 deletions Comparer/MatFrmSrc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class MatFrmSrc : public FrmSrc
MatFrmSrc(SQPane *pane);

virtual ~MatFrmSrc();
virtual bool Open(const CString& filePath);
virtual bool Open(const CString& filePath, const struct qcsc_info* sortedCscInfo,
int srcW, int srcH, int dstW, int dstH);
virtual int GetFrameNum();
virtual void Release();
virtual bool GetResolution(CString &pathName, int* w, int* h);
virtual const struct qcsc_info* GetColorSpace(CString &pathName,
struct qcsc_info* sortedCscInfo);
virtual const struct qcsc_info* GetColorSpace(const CString &pathName,
const struct qcsc_info* sortedCscInfo, bool doReisze);
virtual bool IsAvailable();
virtual bool FillSceneBuf(BYTE* origBuf);
virtual long GetNextFrameID();
Expand Down
77 changes: 65 additions & 12 deletions Comparer/RawFrmSrc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
#include "QCommon.h"
#include "ComparerUtil.h"

using namespace cv;

RawFrmSrc::RawFrmSrc(SQPane *pane)
: FrmSrc(pane, false)
, mFileSize(0L)
, mOrigSceneSize(0L)
, mResizeSceneSize(0L)
, mResizeBuf(nullptr)
{}

RawFrmSrc::~RawFrmSrc()
{
Release();
}

bool RawFrmSrc::Open(const CString& filePath)
bool RawFrmSrc::Open(const CString& filePath, const struct qcsc_info* sortedCscInfo,
int srcW, int srcH, int dstW, int dstH)
{
CFileException e;
BOOL ok = mFile.Open(filePath,
Expand All @@ -27,6 +33,21 @@ bool RawFrmSrc::Open(const CString& filePath)
}

mFileSize = mFile.GetLength();
mSrcW = srcW;
mSrcH = srcH;
mDstW = dstW;
mDstH = dstH;

mSrcCscInfo = GetColorSpace(filePath, sortedCscInfo, false);
mOrigSceneSize = mSrcCscInfo->cs_load_info(mSrcW, mSrcH, nullptr, nullptr);

if ((mSrcW != mDstW || mSrcH != mDstH) && mOrigSceneSize > mResizeSceneSize) {
if (mResizeBuf != nullptr)
delete[] mResizeBuf;

mResizeSceneSize = mOrigSceneSize;
mResizeBuf = new BYTE[mResizeSceneSize];
}

return true;
}
Expand All @@ -41,9 +62,13 @@ void RawFrmSrc::Release()
if (IsAvailable())
mFile.Close();
mFileSize = 0;
mOrigSceneSize = 0;
mResizeSceneSize = 0;
delete[] mResizeBuf;
mResizeBuf = nullptr;
}

static CString parseLowerFileName(CString& pathName)
static CString parseLowerFileName(const CString& pathName)
{
CString fileName = getBaseName(pathName);
fileName.MakeLower();
Expand All @@ -66,43 +91,71 @@ bool RawFrmSrc::GetResolution(CString &pathName, int* w, int* h)
return true;
}

const struct qcsc_info* RawFrmSrc::GetColorSpace(CString &pathName,
struct qcsc_info* sortedCscInfo)
const struct qcsc_info* RawFrmSrc::GetColorSpace(const CString &pathName,
const struct qcsc_info* sortedCscInfo, bool doReisze)
{
char szFileName[MAX_PATH + 1];
CString fileName = parseLowerFileName(pathName);
strncpy_s(szFileName, CT2A(fileName), MAX_PATH);

return q1::image_find_cs(sortedCscInfo, szFileName);
if (doReisze) {
return q1::image_find_cs(sortedCscInfo, "bgr888");
} else {
return q1::image_find_cs(sortedCscInfo, szFileName);
}
}

bool RawFrmSrc::IsAvailable()
{
return mFile != CFile::hFileNull && mFileSize > 0;
}

bool RawFrmSrc::FillSceneBuf(BYTE* origBuf)
{
UINT nRead = mFile.Read(origBuf, UINT(mPane->origSceneSize));
if (nRead < mPane->origSceneSize) {
static void readFromFile(CFile &file, BYTE *Buf, size_t size) {
UINT nRead = file.Read(Buf, UINT(size));
if (nRead < size) {
LOGWRN("The remaining data is too short to be one frame");

// initialize the remaining buffer
memset(origBuf + nRead, 0, mPane->origSceneSize - nRead);
memset(Buf + nRead, 0, size - nRead);
}
}

bool RawFrmSrc::FillSceneBuf(BYTE* origBuf)
{
if (mSrcW != mDstW || mSrcH != mDstH) {
LOGWRN("resize");
readFromFile(mFile, mResizeBuf, mResizeSceneSize);

Mat matSrcTemp(mSrcH, mSrcW, CV_8UC3);
int bufOffset2 = 0, bufOffset3 = 0;
mSrcCscInfo->cs_load_info(mSrcW, mSrcH, &bufOffset2, &bufOffset3);
mSrcCscInfo->csc2rgb888(matSrcTemp.ptr(),
mResizeBuf,
mResizeBuf + bufOffset2,
mResizeBuf + bufOffset3, ROUNDUP_DWORD(mSrcW), mSrcW, mSrcH);

Mat matDstTemp(mDstH, mDstW, CV_8UC3, origBuf, mDstW * (size_t)QIMG_DST_RGB_BYTES);
resize(matSrcTemp, matDstTemp, Size(mDstW, mDstH));
matSrcTemp.release();
memcpy(origBuf, matDstTemp.ptr(), matDstTemp.total() * matDstTemp.elemSize());
matDstTemp.release();
} else {
LOGWRN("normal");
readFromFile(mFile, origBuf, mOrigSceneSize);
}

return true;
}

long RawFrmSrc::GetNextFrameID()
{
return long(mFile.GetPosition() / mPane->origSceneSize);
return long(mFile.GetPosition() / mOrigSceneSize);
}

bool RawFrmSrc::SetNextFrameID(long frameID)
{
try {
ULONGLONG pos = ULONGLONG(frameID) * mPane->origSceneSize;
ULONGLONG pos = ULONGLONG(frameID) * mOrigSceneSize;
mFile.Seek(pos, CFile::begin);
} catch (CFileException *e) {
LOGWRN("Failed to Seek to frameID %ld", frameID);
Expand Down
12 changes: 8 additions & 4 deletions Comparer/RawFrmSrc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ class RawFrmSrc : public FrmSrc
RawFrmSrc(SQPane *pane);

virtual ~RawFrmSrc();
virtual bool Open(const CString& filePath);
virtual bool Open(const CString& filePath, const struct qcsc_info* sortedCscInfo,
int srcW, int srcH, int dstW, int dstH);
virtual int GetFrameNum();
virtual void Release();
virtual bool GetResolution(CString &pathName, int* w, int* h);
virtual const struct qcsc_info* GetColorSpace(CString &pathName,
struct qcsc_info* sortedCscInfo);
virtual const struct qcsc_info* GetColorSpace(const CString &pathName,
const struct qcsc_info* sortedCscInfo, bool doReisze);
virtual bool IsAvailable();
virtual bool FillSceneBuf(BYTE* origBuf);
virtual long GetNextFrameID();
Expand All @@ -22,5 +23,8 @@ class RawFrmSrc : public FrmSrc
private:
CFile mFile;
ULONGLONG mFileSize;
size_t mOrigSceneSize, mResizeSceneSize;
int mSrcW, mSrcH, mDstW, mDstH;
const struct qcsc_info* mSrcCscInfo;
BYTE* mResizeBuf;
};

Loading

0 comments on commit 857138f

Please sign in to comment.