Skip to content

Commit 3cc713e

Browse files
committed
Reduce memory consumption
1 parent 1ac36b9 commit 3cc713e

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/WinIMergeLib/ImgDiffBuffer.hpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,30 +405,43 @@ class DataForDiff
405405
{
406406
public:
407407
DataForDiff(const Image& img, double colorDistanceThreshold)
408-
: m_colorDistanceThreshold(colorDistanceThreshold)
409-
, m_recsize(img.width() * 4)
410-
, m_recnum(img.height())
408+
: m_img(img), m_colorDistanceThreshold(colorDistanceThreshold)
411409
{
412-
m_data.resize(m_recsize * m_recnum);
413-
for (unsigned i = 0; i < m_recnum; i++)
414-
memcpy(m_data.data() + i * m_recsize, img.scanLine(i), m_recsize);
410+
reverse();
415411
}
416-
unsigned size() const { return m_recsize * m_recnum; }
417-
const char* data() const { return m_data.data(); }
412+
~DataForDiff()
413+
{
414+
reverse();
415+
}
416+
void reverse()
417+
{
418+
unsigned w = m_img.width();
419+
unsigned h = m_img.height();
420+
std::vector<BYTE> tmp(w * 4);
421+
for (unsigned i = 0; i < h / 2; ++i)
422+
{
423+
memcpy(tmp.data(), m_img.scanLine(i), w * 4);
424+
memcpy(const_cast<BYTE *>(m_img.scanLine(i)), m_img.scanLine(h - i - 1), w * 4);
425+
memcpy(const_cast<BYTE *>(m_img.scanLine(h - i - 1)), tmp.data(), w * 4);
426+
}
427+
}
428+
unsigned size() const { return m_img.height() * m_img.width() * 4; }
429+
const char* data() const { return reinterpret_cast<const char *>(m_img.scanLine(m_img.height() - 1)); }
418430
const char* next(const char* scanline) const
419431
{
420-
return scanline + m_recsize;
432+
return scanline + m_img.width() * 4;
421433
}
422434
bool equals(const char* scanline1, unsigned size1,
423435
const char* scanline2, unsigned size2) const
424436
{
425-
return alineEquals(reinterpret_cast<const unsigned char *>(scanline1), size1 / 4, reinterpret_cast<const unsigned char*>(scanline2), size2 / 4, m_colorDistanceThreshold);
437+
return alineEquals(reinterpret_cast<const unsigned char *>(scanline1), size1 / 4,
438+
reinterpret_cast<const unsigned char *>(scanline2), size2 / 4, m_colorDistanceThreshold);
426439
}
427440
unsigned long hash(const char* scanline) const
428441
{
429442
unsigned long ha = 5381;
430443
const char* begin = scanline;
431-
const char* end = begin + m_recsize;
444+
const char* end = begin + m_img.width() * 4;
432445

433446
if (m_colorDistanceThreshold > 0.0)
434447
{
@@ -453,9 +466,7 @@ class DataForDiff
453466
}
454467

455468
private:
456-
unsigned m_recnum;
457-
unsigned m_recsize;
458-
std::vector<char> m_data;
469+
const Image& m_img;
459470
double m_colorDistanceThreshold;
460471
};
461472

0 commit comments

Comments
 (0)