Skip to content

Commit 7e22e83

Browse files
committed
修复窗口状态变化导致图像缩放复位
1 parent 6fedf55 commit 7e22e83

File tree

10 files changed

+86
-62
lines changed

10 files changed

+86
-62
lines changed

Social preview.png

232 KB
Loading

ico.ico

-1.63 KB
Binary file not shown.

ico.png

-608 Bytes
Loading

ico.psd

-3.53 KB
Binary file not shown.

jarkViewer/jarkViewer.aps

-3.27 KB
Binary file not shown.

jarkViewer/jarkViewer.ico

-1.63 KB
Binary file not shown.

jarkViewer/jarkViewer.rc

-6 Bytes
Binary file not shown.

jarkViewer/jarkViewer.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<LinkTimeCodeGeneration>UseFastLinkTimeCodeGeneration</LinkTimeCodeGeneration>
136136
<StackReserveSize>8388608</StackReserveSize>
137137
<ProgramDatabaseFile />
138+
<EnableUAC>false</EnableUAC>
138139
</Link>
139140
<ResourceCompile>
140141
<Culture>0x0804</Culture>

jarkViewer/small.ico

-1.63 KB
Binary file not shown.

jarkViewer/src/jarkViewer.cpp

Lines changed: 85 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
*/
1717

18-
const wstring appName = L"JarkViewer v1.15";
18+
const wstring appName = L"JarkViewer v1.16";
1919

2020

2121

@@ -79,6 +79,23 @@ struct CurImageParameter {
7979
zoomCur = ZOOM_BASE;
8080
}
8181
}
82+
83+
void handleNewSize(int winWidth = 0, int winHeight = 0) {
84+
if (winHeight == 0 || winWidth == 0 || framesPtr == nullptr)
85+
return;
86+
87+
//适应显示窗口宽高的缩放比例
88+
int64_t zoomFitWindow = std::min(winWidth * ZOOM_BASE / width, winHeight * ZOOM_BASE / height);
89+
90+
zoomList = ZOOM_LIST;
91+
if (!Utils::is_power_of_two(zoomFitWindow) || zoomFitWindow < ZOOM_LIST.front() || zoomFitWindow > ZOOM_LIST.back())
92+
zoomList.push_back(zoomFitWindow);
93+
else {
94+
if (zoomIndex >= zoomList.size())
95+
zoomIndex = zoomList.size() - 1;
96+
}
97+
std::sort(zoomList.begin(), zoomList.end());
98+
}
8299
};
83100

84101
const vector<int64_t> CurImageParameter::ZOOM_LIST = {
@@ -341,70 +358,59 @@ class JarkViewerApp : public D2D1App {
341358
}
342359

343360
void OnResize(UINT width, UINT height) {
344-
static int w = 0, h = 0;
345-
if (w == width && h == height)
346-
return;
347-
348-
w = width;
349-
h = height;
350361
operateQueue.push({ ActionENUM::newSize, (int)width, (int)height });
351362
}
352363

353-
uint32_t getSrcPx(const cv::Mat& srcImg, int srcX, int srcY, int mainX, int mainY) const {
354-
switch (srcImg.channels()) {
355-
case 3: {
356-
cv::Vec3b srcPx = srcImg.at<cv::Vec3b>(srcY, srcX);
364+
uint32_t getSrcPx3(const cv::Mat& srcImg, int srcX, int srcY, int mainX, int mainY) const {
365+
cv::Vec3b srcPx = srcImg.at<cv::Vec3b>(srcY, srcX);
357366

358-
intUnion ret = 255;
359-
if (curPar.zoomCur < curPar.ZOOM_BASE && srcY > 0 && srcX > 0) { // 简单临近像素平均
360-
cv::Vec3b px0 = srcImg.at<cv::Vec3b>(srcY - 1, srcX - 1);
361-
cv::Vec3b px1 = srcImg.at<cv::Vec3b>(srcY - 1, srcX);
362-
cv::Vec3b px2 = srcImg.at<cv::Vec3b>(srcY, srcX - 1);
363-
for (int i = 0; i < 3; i++)
364-
ret[i] = (px0[i] + px1[i] + px2[i] + srcPx[i]) / 4;
367+
intUnion ret = 255;
368+
if (curPar.zoomCur < curPar.ZOOM_BASE && srcY > 0 && srcX > 0) { // 简单临近像素平均
369+
cv::Vec3b px0 = srcImg.at<cv::Vec3b>(srcY - 1, srcX - 1);
370+
cv::Vec3b px1 = srcImg.at<cv::Vec3b>(srcY - 1, srcX);
371+
cv::Vec3b px2 = srcImg.at<cv::Vec3b>(srcY, srcX - 1);
372+
for (int i = 0; i < 3; i++)
373+
ret[i] = (px0[i] + px1[i] + px2[i] + srcPx[i]) / 4;
365374

366-
return ret.u32;
367-
}
368-
ret[0] = srcPx[0];
369-
ret[1] = srcPx[1];
370-
ret[2] = srcPx[2];
371375
return ret.u32;
372376
}
373-
case 4: {
374-
auto srcPtr = (intUnion*)srcImg.ptr();
375-
int srcW = srcImg.cols;
376-
377-
intUnion srcPx = srcPtr[srcW * srcY + srcX];
378-
intUnion bgPx = ((mainX / BG_GRID_WIDTH + mainY / BG_GRID_WIDTH) & 1) ?
379-
GRID_DARK : GRID_LIGHT;
380-
381-
intUnion px;
382-
if (curPar.zoomCur < curPar.ZOOM_BASE && srcY > 0 && srcX > 0) {
383-
intUnion srcPx1 = srcPtr[srcW * (srcY - 1) + srcX - 1];
384-
intUnion srcPx2 = srcPtr[srcW * (srcY - 1) + srcX];
385-
intUnion srcPx3 = srcPtr[srcW * (srcY)+srcX - 1];
386-
for (int i = 0; i < 4; i++)
387-
px[i] = (srcPx1[i] + srcPx2[i] + srcPx3[i] + srcPx[i]) / 4;
388-
}
389-
else {
390-
px = srcPx;
391-
}
377+
ret[0] = srcPx[0];
378+
ret[1] = srcPx[1];
379+
ret[2] = srcPx[2];
380+
return ret.u32;
381+
}
392382

393-
if (px[3] == 0) return bgPx.u32;
394-
else if (px[3] == 255) return intUnion(px[0], px[1], px[2], 255).u32;;
383+
uint32_t getSrcPx4(const cv::Mat& srcImg, int srcX, int srcY, int mainX, int mainY) const {
395384

396-
const int alpha = px[3];
397-
intUnion ret = alpha;
398-
for (int i = 0; i < 3; i++)
399-
ret[i] = (bgPx[i] * (255 - alpha) + px[i] * alpha) / 256;
400-
return ret.u32;
385+
auto srcPtr = (intUnion*)srcImg.ptr();
386+
int srcW = srcImg.cols;
387+
388+
intUnion srcPx = srcPtr[srcW * srcY + srcX];
389+
intUnion bgPx = ((mainX / BG_GRID_WIDTH + mainY / BG_GRID_WIDTH) & 1) ?
390+
GRID_DARK : GRID_LIGHT;
391+
392+
if (srcPx[3] == 0) return bgPx.u32;
393+
394+
intUnion px;
395+
if (curPar.zoomCur < curPar.ZOOM_BASE && srcY > 0 && srcX > 0) { // 简单临近像素平均
396+
intUnion srcPx1 = srcPtr[srcW * (srcY - 1) + srcX - 1];
397+
intUnion srcPx2 = srcPtr[srcW * (srcY - 1) + srcX];
398+
intUnion srcPx3 = srcPtr[srcW * srcY + srcX - 1];
399+
for (int i = 0; i < 4; i++)
400+
px[i] = (srcPx1[i] + srcPx2[i] + srcPx3[i] + srcPx[i]) / 4;
401401
}
402+
else {
403+
px = srcPx;
402404
}
403405

404-
return ((mainX / BG_GRID_WIDTH + mainY / BG_GRID_WIDTH) & 1) ?
405-
GRID_DARK : GRID_LIGHT;
406-
}
406+
if (px[3] == 255) return px.u32;
407407

408+
const int alpha = px[3];
409+
intUnion ret = alpha;
410+
for (int i = 0; i < 3; i++)
411+
ret[i] = (bgPx[i] * (255 - alpha) + px[i] * alpha) / 256;
412+
return ret.u32;
413+
}
408414

409415
void drawCanvas(const cv::Mat& srcImg, cv::Mat& canvas) const {
410416

@@ -428,15 +434,27 @@ class JarkViewerApp : public D2D1App {
428434
if (yEnd > canvasH) yEnd = canvasH;
429435

430436
memset(canvas.ptr(), BG_COLOR, 4ULL * canvasH * canvasW);
431-
// 1360*768 1-10ms
437+
432438
auto ptr = (uint32_t*)canvas.ptr();
433-
for (int y = yStart; y < yEnd; y++)
434-
for (int x = xStart; x < xEnd; x++) {
435-
const int srcX = (int)(((int64_t)x - deltaW) * curPar.ZOOM_BASE / curPar.zoomCur);
436-
const int srcY = (int)(((int64_t)y - deltaH) * curPar.ZOOM_BASE / curPar.zoomCur);
437-
if (0 <= srcX && srcX < srcW && 0 <= srcY && srcY < srcH)
438-
ptr[y * canvasW + x] = getSrcPx(srcImg, srcX, srcY, x, y);
439-
}
439+
440+
if (srcImg.channels() == 3) {
441+
for (int y = yStart; y < yEnd; y++)
442+
for (int x = xStart; x < xEnd; x++) {
443+
const int srcX = (int)(((int64_t)x - deltaW) * curPar.ZOOM_BASE / curPar.zoomCur);
444+
const int srcY = (int)(((int64_t)y - deltaH) * curPar.ZOOM_BASE / curPar.zoomCur);
445+
if (0 <= srcX && srcX < srcW && 0 <= srcY && srcY < srcH)
446+
ptr[y * canvasW + x] = getSrcPx3(srcImg, srcX, srcY, x, y);
447+
}
448+
}
449+
else if (srcImg.channels() == 4) {
450+
for (int y = yStart; y < yEnd; y++)
451+
for (int x = xStart; x < xEnd; x++) {
452+
const int srcX = (int)(((int64_t)x - deltaW) * curPar.ZOOM_BASE / curPar.zoomCur);
453+
const int srcY = (int)(((int64_t)y - deltaH) * curPar.ZOOM_BASE / curPar.zoomCur);
454+
if (0 <= srcX && srcX < srcW && 0 <= srcY && srcY < srcH)
455+
ptr[y * canvasW + x] = getSrcPx4(srcImg, srcX, srcY, x, y);
456+
}
457+
}
440458
}
441459

442460
void DrawScene() {
@@ -465,8 +483,12 @@ class JarkViewerApp : public D2D1App {
465483
switch (operateAction.action)
466484
{
467485
case ActionENUM::newSize: {
486+
if (operateAction.width == 0 || operateAction.height == 0)
487+
break;
488+
489+
if (winWidth == operateAction.width && winHeight == operateAction.height)
490+
break;
468491

469-
//D2D1_SIZE_U displaySize = {operateAction.width, operateAction.height};// m_pD2DDeviceContext->GetPixelSize();
470492
winWidth = operateAction.width;
471493
winHeight = operateAction.height;
472494

@@ -476,7 +498,7 @@ class JarkViewerApp : public D2D1App {
476498
CreateWindowSizeDependentResources();
477499
}
478500

479-
curPar.Init(winWidth, winHeight);
501+
curPar.handleNewSize(winWidth, winHeight);
480502
} break;
481503

482504
case ActionENUM::preImg: {
@@ -631,6 +653,7 @@ int WINAPI wWinMain(
631653
AllocConsole();
632654
FILE* stream;
633655
freopen_s(&stream, "CON", "w", stdout);//重定向输入流
656+
freopen_s(&stream, "CON", "w", stderr);//重定向输入流
634657

635658
SetConsoleCP(CP_UTF8);
636659
SetConsoleOutputCP(CP_UTF8);

0 commit comments

Comments
 (0)