-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg_utils.cpp
More file actions
163 lines (157 loc) · 5.42 KB
/
Copy pathimg_utils.cpp
File metadata and controls
163 lines (157 loc) · 5.42 KB
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
#pragma comment(lib, "gdiplus.lib") // 链接GDI+库
#include "img_utils.h"
#include <algorithm>
#include <windows.h>
#include <gdiplus.h>
#include <iostream>
// --------------- 全局变量定义 ---------------
// (此处为变量的实际定义,extern 声明的变量需在此初始化)
std::vector<std::wstring> g_imagePaths; // 所有图片路径
std::vector<Bitmap*> g_imageBitmaps; // 已加载的图片
std::wstring g_currentDir; // 当前选择的图片目录
// --------------- 函数实现 ---------------
bool IsImageFile(const std::wstring& filename)
{
// 提取文件扩展名(转为小写判断)
std::wstring ext;
size_t dotPos = filename.find_last_of(L".");
if (dotPos != std::wstring::npos)
{
ext = filename.substr(dotPos + 1);
std::transform(ext.begin(), ext.end(), ext.begin(), ::towlower);
}
// 支持的图片格式列表
static const std::vector<std::wstring> supportedExts = {
L"jpg", L"jpeg", L"png", L"bmp", L"gif", L"tiff"
};
// 检查扩展名是否在支持列表中
return std::find(supportedExts.begin(), supportedExts.end(), ext) != supportedExts.end();
}
void LoadImagePaths(const std::wstring& dir)
{
// 1. 清空原有数据
g_imagePaths.clear();
ReleaseAllImages(); // 释放旧图片资源
// 2. 扫描目录下的所有文件
WIN32_FIND_DATAW findData;
std::wstring searchPath = dir + L"\\*.*"; // 匹配所有文件
HANDLE hFind = FindFirstFileW(searchPath.c_str(), &findData);
if (hFind == INVALID_HANDLE_VALUE)
{
MessageBoxW(NULL, L"目录访问失败!", L"错误", MB_ICONERROR);
return;
}
// 3. 遍历所有文件,筛选图片
do
{
// 跳过目录(只处理文件)
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
// 检查是否为图片文件
std::wstring fullPath = dir + L"\\" + findData.cFileName;
if (IsImageFile(fullPath))
{
g_imagePaths.push_back(fullPath);
}
} while (FindNextFileW(hFind, &findData));
FindClose(hFind); // 释放查找句柄
// 4. 按文件名排序(升序)
std::sort(g_imagePaths.begin(), g_imagePaths.end());
// 输出到调试窗口
WCHAR debugMsg[512];
swprintf_s(debugMsg, L"文件数量: %zu \ng_imagePaths[0]: %s\n", g_imagePaths.size(), g_imagePaths.at(0).c_str());
OutputDebugStringW(debugMsg);
// 5. 初始化图片指针容器(大小与路径一致,初始为nullptr)
g_imageBitmaps.resize(g_imagePaths.size(), nullptr);
// 6. 保存当前目录
g_currentDir = dir;
}
void PreloadImages(int centerIndex, int imgnum)
{
if (g_imagePaths.empty()) return;
// 计算预加载范围(中心前后各PRELOAD_COUNT张)
int start = max(0, centerIndex - imgnum);
int end = min((int)g_imagePaths.size() - 1, centerIndex + imgnum);
// 加载范围内未加载的图片
for (int i = 0; i < (int)g_imageBitmaps.size(); i++)
{
if (i >= start && i <= end)
{
if(g_imageBitmaps[i] == nullptr)
{
// 从路径加载图片(GDI+的Bitmap类支持多格式)
g_imageBitmaps[i] = new Bitmap(g_imagePaths[i].c_str());
// 检查加载是否失败
if (g_imageBitmaps[i]->GetLastStatus() != Ok)
{
delete g_imageBitmaps[i];
g_imageBitmaps[i] = nullptr; // 标记为加载失败
}
}
}
else
{
delete g_imageBitmaps[i];
g_imageBitmaps[i] = nullptr; // 置空避免野指针
}
}
}
void loadImages(int start, int imgnum)
{
if (g_imagePaths.empty()) return;
int end = min((int)g_imagePaths.size() - 1, start + imgnum);
// 加载范围内未加载的图片
for (int i = 0; i < (int)g_imageBitmaps.size(); i++)
{
if (i >= start && i <= end)
{
if (g_imageBitmaps[i] == nullptr)
{
// 从路径加载图片(GDI+的Bitmap类支持多格式)
g_imageBitmaps[i] = new Bitmap(g_imagePaths[i].c_str());
// 检查加载是否失败
if (g_imageBitmaps[i]->GetLastStatus() != Ok)
{
delete g_imageBitmaps[i];
g_imageBitmaps[i] = nullptr; // 标记为加载失败
}
}
}
else
{
delete g_imageBitmaps[i];
g_imageBitmaps[i] = nullptr; // 置空避免野指针
}
}
}
int GetScaledImageHeight(int index, int windowWidth)
{
// 检查索引和图片有效性
if (index < 0 || index >= (int)g_imageBitmaps.size() || g_imageBitmaps[index] == nullptr)
return 0;
// 获取图片原始尺寸
int origWidth = g_imageBitmaps[index]->GetWidth();
int origHeight = g_imageBitmaps[index]->GetHeight();
// 按窗口宽度等比例缩放(避免宽度超过窗口)
if (origWidth > 0 && windowWidth > 0)
{
int newheight = (int)((float)origHeight * (float)windowWidth / (float)origWidth);
std::wcout << index << ":" << newheight << std::endl;
return newheight;
}
return origHeight; // 缩放失败时返回原始高度
}
void ReleaseAllImages()
{
// 释放所有已加载的图片
for (size_t i = 0; i < g_imageBitmaps.size(); ++i) // 使用索引遍历,直接访问元素
{
Bitmap*& bmp = g_imageBitmaps[i]; // 显式声明为指针的引用(可修改)
if (bmp != nullptr)
{
delete bmp;
bmp = nullptr;
}
}
g_imageBitmaps.clear();
}