1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "stdafx.h"
#include "Thread_SaveFullImg.h"
 
static void TightCopy(std::vector<BYTE>& vDst, const BYTE* pSrc, int nWidth, int nHeight, int nStride, int nBpp)
{
    const int nTight = nWidth * nBpp;
    vDst.resize(size_t(nTight) * nHeight);
 
    if (vDst.empty() || nHeight < 1 || nWidth < 1 || nBpp < 1) {
        g_pLog->DisplayMessage(_T("TightCopy: Invalid parameters, nWidth=%d, nHeight=%d, nBpp=%d"), nWidth, nHeight, nBpp);
        return;
    }
 
    // Èç¹û nStride µÈÓÚ nTight£¬ËµÃ÷Ô´Êý¾ÝÊǽôÃÜÐд洢
    if (nStride == nTight) {
        memcpy(vDst.data(), pSrc, vDst.size());
        return;
    }
 
    // Èç¹û nStride Ð¡ÓÚ nTight£¬ËµÃ÷Ô´Êý¾Ý²»ÊǽôÃÜÐд洢
    if (nStride < nTight) {
        g_pLog->DisplayMessage(_T("TightCopy: Invalid nStride %d < nTight %d"), nStride, nTight);
        return;
    }
 
    // Èç¹û nStride ´óÓÚ nTight£¬ÖðÐи´ÖÆ
    for (int y = 0; y < nHeight; ++y) {
        memcpy(vDst.data() + size_t(y) * nTight, pSrc + size_t(y) * nStride, nTight);
    }
}
 
CThread_SaveFullImg::CThread_SaveFullImg()
{
    m_bStop = false;
}
 
CThread_SaveFullImg::~CThread_SaveFullImg()
{
    StopThread();
}
 
void CThread_SaveFullImg::CreateThread()
{
    if (m_Thread.joinable()) { 
        return;
    }
 
    m_bStop = false;
    m_Thread = std::thread([this] { ThreadProc(); });
}
 
void CThread_SaveFullImg::StopThread()
{
    {
        std::lock_guard<std::mutex> oLock(m_Mtx);
        m_bStop = true;
    }
 
    m_Cv.notify_all();
    if (m_Thread.joinable()) { 
        m_Thread.join();
    }
}
 
void CThread_SaveFullImg::Enqueue(SaveImgJob&& oJob)
{
    {
        std::lock_guard<std::mutex> oLock(m_Mtx);
        m_Q.push(std::move(oJob));
    }
    m_Cv.notify_one();
}
 
size_t CThread_SaveFullImg::GetPendingCount() const
{
    std::lock_guard<std::mutex> oLock(m_Mtx);
    return m_Q.size();
}
 
void CThread_SaveFullImg::ThreadProc()
{
    while (true) {
        SaveImgJob oJob; {
            std::unique_lock<std::mutex> oLock(m_Mtx);
            m_Cv.wait(oLock, [this] { 
                return m_bStop || !m_Q.empty();
            });
 
            if (m_bStop && m_Q.empty()) { 
                break;
            }
 
            oJob = std::move(m_Q.front());
            m_Q.pop();
        }
 
        try {
            SaveFullImageModern(oJob);
        }
        catch (...) {
            g_pLog->DisplayMessage(_T("Async save failed: %s"), oJob.strPath.c_str());
        }
    }
}
 
BOOL CThread_SaveFullImg::SaveFullImageModern(const SaveImgJob& job)
{
    int iSide = job.nDimension;
    if (iSide < 0 || iSide > MAX_DIMENSION_COUNT - 1) { 
        g_pLog->DisplayMessage(_T("Save Full Image Fail(%S, %d), invalid side index"), job.strPath.c_str(), job.nDimension);
        return FALSE;
    }
 
    if (job.vData.empty() || job.nHeight < 100 || job.nWidth < 100 || job.nBpp < 0) {
        g_pLog->DisplayMessage(_T("Save Full Image Fail(%S, %s, %d, %d)"), job.strPath.c_str(), PANEL_SIDE[iSide], job.nWidth, job.nHeight);
        return FALSE;
    }
 
    if (job.nStride < job.nWidth * job.nBpp) {
        g_pLog->DisplayMessage(_T("Save Full Image Fail(%S), stride(%d) < width*bpp(%d)"), job.strPath.c_str(), job.nStride, job.nWidth * job.nBpp);
        return FALSE;
    }
 
    int nQuality = job.nQuality;
    if (nQuality < 0) { 
        nQuality = 0;
    }
    if (nQuality > 100) { 
        nQuality = 100;
    }
 
    double dTmp = 70 / 100.0;
    double dRatio = 1.0 - dTmp;
    dRatio = dRatio - 0.01 < 0.0 ? 1.0 : dRatio;
 
    g_pLog->DisplayMessage(_T("Save Full Image Start(%S, %s, %d, %d, %d)"), job.strPath.c_str(), PANEL_SIDE[job.nDimension], job.nStartY, job.nHeight, nQuality);
 
    int nStartY = job.nStartY;
    if (nStartY < 0) { 
        nStartY = 0;
    }
 
    if (nStartY > job.nHeight - 1) {
        nStartY = job.nHeight - 1;
    }
 
    int cvType;
    switch (job.nBpp)
    {
    case 1: cvType = CV_8UC1; break;
    case 3: cvType = CV_8UC3; break;
    default:
        g_pLog->DisplayMessage(_T("Save Full Image Fail(%S), unsupported nBpp=%d"), job.strPath.c_str(), job.nBpp);
        return FALSE;
    }
 
    const BYTE* pBase = job.vData.data() + size_t(nStartY) * size_t(job.nStride);
    const int nHAvail = job.nHeight - nStartY;
    if (nHAvail < 100) {
        g_pLog->DisplayMessage(_T("Save Full Image Skip(%S, %s), nHAvail=%d too small"), job.strPath.c_str(), PANEL_SIDE[iSide], nHAvail);
        return FALSE;
    }
 
    // Ô­Ê¼Í¼ÏñÊý¾Ýת cv::Mat
    cv::Mat origin(nHAvail, job.nWidth, cvType, (void*)pBase, size_t(job.nStride));
 
    // Ëõ·ÅͼÏñ
    const int nOutW = std::max(1, static_cast<int>(origin.cols * dRatio));
    const int nOutH = std::max(1, static_cast<int>(origin.rows * dRatio));
    cv::Mat img;
    if (nOutW == origin.cols && nOutH == origin.rows) {
        img = origin;
    }
    else {
        cv::resize(origin, img, cv::Size(nOutW, nOutH), 0, 0, cv::INTER_LINEAR);
    }
 
    // ÉèÖàJPEG Ñ¹Ëõ²ÎÊý
    std::vector<int> params = { cv::IMWRITE_JPEG_QUALITY, nQuality };
    try {
        if (!cv::imwrite(job.strPath, img, params)) {
            g_pLog->DisplayMessage(_T("Save Full Image Fail(%S, %s, %d), imwrite failed"), job.strPath.c_str(), PANEL_SIDE[iSide], nStartY);
            return FALSE;
        }
    }
    catch (...) {
        g_pLog->DisplayMessage(_T("Save Full Image Fail(%S, %s, %d), exception during imwrite"), job.strPath.c_str(), PANEL_SIDE[iSide], nStartY);
        return FALSE;
    }
 
    g_pLog->DisplayMessage(_T("Save Full Image Success(%S, %s, %d, %d, %d)"), job.strPath.c_str(), PANEL_SIDE[iSide], nStartY, job.nHeight, nQuality);
 
    return TRUE;
}