重新实现

bestlqiang 4 days ago
parent 9168eb2f6a
commit 79816f7845

@ -29,8 +29,8 @@
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<UseOfMfc>Dynamic</UseOfMfc>
<CharacterSet>MultiByte</CharacterSet>
<UseOfMfc>Static</UseOfMfc>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
@ -187,6 +187,7 @@
<ItemGroup>
<ClInclude Include="CcdDataImageProcessing.h" />
<ClInclude Include="CcdDataImageProcessingDlg.h" />
<ClInclude Include="CsvToImg.h" />
<ClInclude Include="FileMgr.h" />
<ClInclude Include="Resource.h" />
<ClInclude Include="stdafx.h" />
@ -195,6 +196,7 @@
<ItemGroup>
<ClCompile Include="CcdDataImageProcessing.cpp" />
<ClCompile Include="CcdDataImageProcessingDlg.cpp" />
<ClCompile Include="CsvToImg.cpp" />
<ClCompile Include="FileMgr.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>

@ -36,6 +36,9 @@
<ClInclude Include="FileMgr.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CsvToImg.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CcdDataImageProcessing.cpp">
@ -50,6 +53,9 @@
<ClCompile Include="FileMgr.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CsvToImg.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="CcdDataImageProcessing.rc">

@ -10,19 +10,7 @@
#define FILE_CCD_IMAGE_INFO_PATH _T("ccd_image_info.csv")
#define FILE_CCD_DATAT_PATH _T("ccd_data_path.csv")
// 颜色定义
#define RED RGB(255, 0, 0)
#define ORANGE RGB(255, 165, 0)
#define YELLOW RGB(255, 255, 0)
#define GREEN RGB(0, 255, 0)
#define CYAN RGB(0, 255, 255)
#define BLUE RGB(0, 0, 255)
#define PURPLE RGB(128, 0, 128)
#define GREY RGB(169, 169, 169)
#define PINK RGB(255, 192, 203)
#define BLACK RGB(0, 0, 0)
#define WHITE RGB(255, 255, 255)
#define BROWN RGB(165, 42, 42)
// 控件ID映射
const int minEnergyCtrlIds[] = { IDC_MIN_ENERGY_EDIT1, IDC_MIN_ENERGY_EDIT2, IDC_MIN_ENERGY_EDIT3,
@ -53,8 +41,8 @@ const TCHAR* colorNames[] = {
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
ColorSchemeManager m_colorSchemeManager;
std::map<const int, int> m_proportionEnergy;
//ColorSchemeManager m_colorSchemeManager;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
@ -171,11 +159,12 @@ void CCcdDataImageProcessingDlg::DoDataExchange(CDataExchange* pDX)
DDX_Control(pDX, IDC_CCD_DATA_PATH_EDIT, m_CddDataPath);
DDX_Control(pDX, IDC_PIC_CTRL, m_PicCtrl);
}
BEGIN_MESSAGE_MAP(CCcdDataImageProcessingDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
// ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_IMAGE_INFO_SAVE_BUTTON, &CCcdDataImageProcessingDlg::OnBnClickedImageInfoSaveButton)
ON_BN_CLICKED(IDC_DLG_MIN_BUTTON, &CCcdDataImageProcessingDlg::OnBnClickedDlgMinButton)
@ -184,6 +173,7 @@ BEGIN_MESSAGE_MAP(CCcdDataImageProcessingDlg, CDialogEx)
ON_BN_CLICKED(IDC_SLECT_PATH_BUTTON, &CCcdDataImageProcessingDlg::OnBnClickedSlectPathButton)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_SAVE_IMAGE_BUTTON, &CCcdDataImageProcessingDlg::OnBnClickedSaveImageButton)
ON_WM_TIMER()
END_MESSAGE_MAP()
@ -223,7 +213,7 @@ BOOL CCcdDataImageProcessingDlg::OnInitDialog()
OnInitEnergyInfo();
OnInitCcdDataPath();
OnInitAll();
SetTimer(0, 500, NULL);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
@ -313,7 +303,7 @@ void CCcdDataImageProcessingDlg::OnBnClickedImageInfoSaveButton()
}
// 提示保存成功
AfxMessageBox(_T("CCD图像颜色方案配比信息已保存"));
//AfxMessageBox(_T("CCD图像颜色方案配比信息已保存"),MB_TOPMOST);
}
@ -570,17 +560,9 @@ void CCcdDataImageProcessingDlg::OnBnClickedSlectPathButton()
void CCcdDataImageProcessingDlg::OnBnClickedImageGenerateButton()
{
// TODO: 执行
// 清除上一次的绘制内容
m_data.clear(); // 清空之前的数据
CWaitCursor wait;
CString cddDataPathInfo;
GetDlgItemText(IDC_CCD_DATA_PATH_EDIT, cddDataPathInfo);
CStringA cstrA(cddDataPathInfo); // 将 CString 转换为 CStringA
std::string stdStr = cstrA.GetString(); // 将 CStringA 转换为 std::string
CFileMgr fileMgr;
fileMgr.ReadFileToStrVec(stdStr, m_data); // 假设文件名是 energy_data.csv
m_colorSchemeManager.addScheme("Scheme1"); // 添加多个颜色方案
//---获取所有方案参数--------------------------------------------
@ -620,42 +602,42 @@ void CCcdDataImageProcessingDlg::OnBnClickedImageGenerateButton()
float maxEnergyValue = _tstof(maxEnergy); // 转换为 float
// 设置颜色
COLORREF color = WHITE; // 默认颜色为白色
COLORREF color = RGB_WHITE; // 默认颜色为白色
if (colorCombo == colorNames[0]) { // Red
color = RED;
color = RGB_RED;
}
else if (colorCombo == colorNames[1]) { // Orange
color = ORANGE;
color = RGB_ORANGE;
}
else if (colorCombo == colorNames[2]) { // Yellow
color = YELLOW;
color = RGB_YELLOW;
}
else if (colorCombo == colorNames[3]) { // Green
color = GREEN;
color = RGB_GREEN;
}
else if (colorCombo == colorNames[4]) { // Cyan
color = CYAN;
color = RGB_CYAN;
}
else if (colorCombo == colorNames[5]) { // Blue
color = BLUE;
color = RGB_BLUE;
}
else if (colorCombo == colorNames[6]) { // Purple
color = PURPLE;
color = RGB_PURPLE;
}
else if (colorCombo == colorNames[7]) { // Grey
color = GREY;
color = RGB_GREY;
}
else if (colorCombo == colorNames[8]) { // Pink
color = PINK;
color = RGB_PINK;
}
else if (colorCombo == colorNames[9]) { // Black
color = BLACK;
color = RGB_BLACK;
}
else if (colorCombo == colorNames[10]) { // White
color = WHITE;
color = RGB_WHITE;
}
else if (colorCombo == colorNames[11]) { // Brown
color = BROWN;
color = RGB_BROWN;
}
// 根据选择的能量区间添加范围
@ -667,143 +649,154 @@ void CCcdDataImageProcessingDlg::OnBnClickedImageGenerateButton()
//---画图--------------------------------------------------------
m_colorSchemeManager.setCurrentScheme("Scheme1"); // 设置当前颜色方案为 Scheme1
gCsvToImg->m_colorSchemeManager = m_colorSchemeManager;
gCsvToImg->ReadCsv2Img(cddDataPathInfo);
// 重绘对话框并强制刷新
Invalidate(); // 标记窗口为无效区域
UpdateWindow(); // 立即刷新窗口
printf("11");
}
// 根据能量值设置颜色
COLORREF CCcdDataImageProcessingDlg::GetColorFromEnergy(float energy)
auto Db2CString=[](float val)
{
if (m_colorSchemeManager.currentScheme == nullptr) {
return WHITE;
}
return m_colorSchemeManager.currentScheme->getColor(energy);
}
CString str;
str.Format("%.3f", val);
return str;
};
auto &currentScheme = gCsvToImg->m_colorSchemeManager.currentScheme;
void CCcdDataImageProcessingDlg::OnPaint()
//CEdit 控件中的占比值 (没全部启用时会抛异常)
try
{
CPaintDC dc(this); // 设备上下文
CRect clientRect;
GetClientRect(&clientRect); // 获取客户区域矩形
// 设置背景色
dc.FillSolidRect(clientRect, RGB(255, 255, 255));
// 设置字体
CFont font;
font.CreatePointFont(120, _T("Arial"));
dc.SelectObject(&font);
// 计算矩阵的行数和列数
int numRows = m_data.size();
int numCols = (numRows > 0) ? m_data[0].size() : 0;
// 如果数据为空,提前返回
if (numRows == 0 || numCols == 0) {
dc.TextOut(10, 10, _T("No data available"));
return;
}
// 获取窗口的宽度和高度
int maxImageWidth = clientRect.Width(); // 使用窗口的宽度
int maxImageHeight = clientRect.Height(); // 使用窗口的高度
// 计算每个数据块的大小(正方形)
float pointSize;
if (numCols > 0) {
// 根据列数和窗口宽度动态计算每个数据块的大小
float maxPointSizeWidth = static_cast<float>(maxImageWidth) / numCols; // 计算每个数据块在宽度上的最大大小
pointSize = static_cast<int>(maxPointSizeWidth); // 选择宽度限制的大小
}
else {
pointSize = 10; // 默认大小
}
// 计算最大高度可用空间,动态计算每个数据块的高度
int pointSizeHeight;
if (numRows > 0) {
// 根据行数和窗口高度动态计算每个数据块的大小
float maxPointSizeHeight = static_cast<float>(maxImageHeight) / numRows; // 计算每个数据块在高度上的最大大小
pointSizeHeight = static_cast<int>(maxPointSizeHeight); // 选择高度限制的大小
}
else {
pointSizeHeight = 10; // 默认大小
}
// 取最小值,确保数据点能够适应窗口
if (pointSize > pointSizeHeight)pointSize = pointSizeHeight;
// 如果计算出的 pointSize 太小,设置一个最小值
if (pointSize < 0.5) {
pointSize = 0.5; // 确保每个数据点至少为 1
}
// 计算偏移量,使图像右贴右边,上边贴上边
int offsetX = 0;
int offsetY = 0;
// 遍历并绘制每个数据点
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col < numCols; ++col) {
// 确保每个元素都有效
if (row < m_data.size() && col < m_data[row].size()) {
try {
float energy = std::stof(m_data[row][col]); // 将字符串转换为浮动数
// 数据统计
if (energy < m_minEnergy) m_minEnergy = energy; // 更新最小值
if (energy > m_maxEnergy) m_maxEnergy = energy; // 更新最大值
m_NumEnergy++;
// 获取能量值对应的颜色
COLORREF color = GetColorFromEnergy(energy);
dc.SetDCBrushColor(color); // 设置画刷颜色
// 计算当前数据点位置(添加偏移量)
float scaledX = col * pointSize + offsetX;
float scaledY = row * pointSize + offsetY;
// 绘制正方形数据块
CRect rect(scaledX, scaledY, scaledX + pointSize, scaledY + pointSize);
dc.FillSolidRect(rect, color);
}
catch (const std::invalid_argument& e) {
// 处理无效的字符串,设置默认值或跳过
continue; // 如果数据无法转换为 float跳过当前元素
}
}
}
m_ProportionEnergy1.SetWindowText(Db2CString(currentScheme->ranges.at(0).dPercent));
m_ProportionEnergy2.SetWindowText(Db2CString(currentScheme->ranges.at(1).dPercent));
m_ProportionEnergy3.SetWindowText(Db2CString(currentScheme->ranges.at(2).dPercent));
m_ProportionEnergy4.SetWindowText(Db2CString(currentScheme->ranges.at(3).dPercent));
m_ProportionEnergy5.SetWindowText(Db2CString(currentScheme->ranges.at(4).dPercent));
m_ProportionEnergy6.SetWindowText(Db2CString(currentScheme->ranges.at(5).dPercent));
m_ProportionEnergy7.SetWindowText(Db2CString(currentScheme->ranges.at(6).dPercent));
m_ProportionEnergy8.SetWindowText(Db2CString(currentScheme->ranges.at(7).dPercent));
m_ProportionEnergy9.SetWindowText(Db2CString(currentScheme->ranges.at(8).dPercent));
m_ProportionEnergy10.SetWindowText(Db2CString(currentScheme->ranges.at(9).dPercent));
}
catch (...){}
// 显示最大最小能量值
CString str;
str.Format(_T("Min Energy: %.2f, Max Energy: %.2f"), m_minEnergy, m_maxEnergy);
dc.TextOut(0, 1300, str);
UpProportionEnergy();
UpdateWindow();
}
COLORREF ColorScheme::getColor(float energy)
// 根据能量值设置颜色
COLORREF CCcdDataImageProcessingDlg::GetColorFromEnergy(float energy)
{
for (const auto& range : ranges) {
if (energy >= range.minEnergy && energy <= range.maxEnergy) {
//HWND hwd = (HWND)range.proportionEnergy;
//CString str;
//GetDlgItemText(range.proportionEnergy, str); // 获取控件指针
m_proportionEnergy[range.proportionEnergy]++;
return range.color;
if (m_colorSchemeManager.currentScheme == nullptr) {
return RGB_WHITE;
}
return m_colorSchemeManager.currentScheme->getColor(energy);
}
return WHITE; // 默认返回白色
}
//void CCcdDataImageProcessingDlg::OnPaint()
//{
//
//
// gCsvToImg->ShowLastImg(&m_PicCtrl);
// return;
//
// CPaintDC dc(this); // 设备上下文
// CRect clientRect;
// GetClientRect(&clientRect); // 获取客户区域矩形
//
// // 设置背景色
// dc.FillSolidRect(clientRect, RGB(255, 255, 255));
//
// // 设置字体
// CFont font;
// font.CreatePointFont(120, _T("Arial"));
// dc.SelectObject(&font);
//
// // 计算矩阵的行数和列数
// int numRows = m_data.size();
// int numCols = (numRows > 0) ? m_data[0].size() : 0;
//
// // 如果数据为空,提前返回
// if (numRows == 0 || numCols == 0) {
// dc.TextOut(10, 10, _T("No data available"));
// return;
// }
//
// // 获取窗口的宽度和高度
// int maxImageWidth = clientRect.Width(); // 使用窗口的宽度
// int maxImageHeight = clientRect.Height(); // 使用窗口的高度
//
// // 计算每个数据块的大小(正方形)
// float pointSize;
// if (numCols > 0) {
// // 根据列数和窗口宽度动态计算每个数据块的大小
// float maxPointSizeWidth = static_cast<float>(maxImageWidth) / numCols; // 计算每个数据块在宽度上的最大大小
// pointSize = static_cast<int>(maxPointSizeWidth); // 选择宽度限制的大小
// }
// else {
// pointSize = 10; // 默认大小
// }
//
// // 计算最大高度可用空间,动态计算每个数据块的高度
// int pointSizeHeight;
// if (numRows > 0) {
// // 根据行数和窗口高度动态计算每个数据块的大小
// float maxPointSizeHeight = static_cast<float>(maxImageHeight) / numRows; // 计算每个数据块在高度上的最大大小
// pointSizeHeight = static_cast<int>(maxPointSizeHeight); // 选择高度限制的大小
// }
// else {
// pointSizeHeight = 10; // 默认大小
// }
//
// // 取最小值,确保数据点能够适应窗口
// if (pointSize > pointSizeHeight)pointSize = pointSizeHeight;
//
// // 如果计算出的 pointSize 太小,设置一个最小值
// if (pointSize < 0.5) {
// pointSize = 0.5; // 确保每个数据点至少为 1
// }
//
// // 计算偏移量,使图像右贴右边,上边贴上边
// int offsetX = 0;
// int offsetY = 0;
//
// // 遍历并绘制每个数据点
// for (int row = 0; row < numRows; ++row) {
// for (int col = 0; col < numCols; ++col) {
// // 确保每个元素都有效
// if (row < m_data.size() && col < m_data[row].size()) {
// try {
// float energy = std::stof(m_data[row][col]); // 将字符串转换为浮动数
//
// // 数据统计
// if (energy < m_minEnergy) m_minEnergy = energy; // 更新最小值
// if (energy > m_maxEnergy) m_maxEnergy = energy; // 更新最大值
//
// m_NumEnergy++;
//
// // 获取能量值对应的颜色
// COLORREF color = GetColorFromEnergy(energy);
// dc.SetDCBrushColor(color); // 设置画刷颜色
//
// // 计算当前数据点位置(添加偏移量)
// float scaledX = col * pointSize + offsetX;
// float scaledY = row * pointSize + offsetY;
//
// // 绘制正方形数据块
// CRect rect(scaledX, scaledY, scaledX + pointSize, scaledY + pointSize);
// dc.FillSolidRect(rect, color);
// }
// catch (const std::invalid_argument& e) {
// // 处理无效的字符串,设置默认值或跳过
// continue; // 如果数据无法转换为 float跳过当前元素
// }
// }
// }
// }
//
// // 显示最大最小能量值
// CString str;
// str.Format(_T("Min Energy: %.2f, Max Energy: %.2f"), m_minEnergy, m_maxEnergy);
// dc.TextOut(0, 1300, str);
// UpProportionEnergy();
//}
void CCcdDataImageProcessingDlg::OnBnClickedSaveImageButton()
{
@ -815,7 +808,8 @@ void CCcdDataImageProcessingDlg::OnBnClickedSaveImageButton()
CString savePath = saveFileDlg.GetPathName();
// 2. 调用图像保存函数
SaveImage(savePath);
gCsvToImg->SaveLastImg(savePath);
//SaveImage(savePath);
}
}
@ -851,3 +845,14 @@ void CCcdDataImageProcessingDlg::SaveImage(const CString& savePath)
}
}
void CCcdDataImageProcessingDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
gCsvToImg->ShowLastImg(&m_PicCtrl);
CDialogEx::OnTimer(nIDEvent);
}

@ -1,14 +1,14 @@
// CcdDataImageProcessingDlg.h : 头文件
//
#include "CsvToImg.h"
#pragma once
#include "afxwin.h"
#include <windows.h>
#include "FileMgr.h"
#include <map>
#include <vector>
#define WHITE RGB(255, 255, 255) // 定义 WHITE 颜色
// CCcdDataImageProcessingDlg 对话框
class CCcdDataImageProcessingDlg : public CDialogEx
@ -33,7 +33,7 @@ protected:
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
// afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
@ -95,6 +95,7 @@ public:
CStatic m_StaticCtrl;
// 数据成员
ColorSchemeManager m_colorSchemeManager;
std::vector<std::vector<std::string>> m_data; // 存储读取的数据
float m_minEnergy, m_maxEnergy;
float m_SumEnergy, m_NumEnergy;
@ -120,56 +121,8 @@ public:
BOOL FileExists(const CString& strFilePath);
COLORREF GetColorFromEnergy(float energy);// 根据能量值设置颜色
CStatic m_PicCtrl;
afx_msg void OnTimer(UINT_PTR nIDEvent);
};
// 颜色范围结构体
struct ColorRange {
float minEnergy;
float maxEnergy;
COLORREF color;
const int proportionEnergy;
ColorRange(float minE, float maxE, COLORREF col, const int proportionE)
: minEnergy(minE), maxEnergy(maxE), color(col), proportionEnergy(proportionE){}
};
// 颜色方案类
class ColorScheme {
public:
std::string name; // 方案的名称
std::vector<ColorRange> ranges; // 存储能量范围和对应的颜色
// 默认构造函数
ColorScheme() : name("") {}
// 带参数构造函数
ColorScheme(std::string schemeName) : name(schemeName) {}
// 添加能量范围
void addRange(float minEnergy, float maxEnergy, COLORREF color, const int proportionEnergy) {
ranges.push_back(ColorRange{ minEnergy, maxEnergy, color ,proportionEnergy});
}
// 根据能量值返回对应的颜色
COLORREF getColor(float energy);
};
// 颜色方案管理类
class ColorSchemeManager {
public:
std::map<std::string, ColorScheme> schemes;
ColorScheme* currentScheme;
ColorSchemeManager() : currentScheme(nullptr) {}
void addScheme(const std::string& name) {
schemes[name] = ColorScheme(name);
}
void setCurrentScheme(const std::string& name) {
currentScheme = &schemes[name];
}
};
extern ColorSchemeManager m_colorSchemeManager;
extern std::map<const int, int> m_proportionEnergy;

@ -0,0 +1,139 @@
#include "stdafx.h"
#include "CsvToImg.h"
CCsvToImg * gCsvToImg = new CCsvToImg;
std::map<const int, int> m_proportionEnergy;
int ColorRange::AllCnt = 0;
CCsvToImg::CCsvToImg()
{
}
CCsvToImg::~CCsvToImg()
{
}
bool CCsvToImg::ReadCsv2Img(CString CsvPath)
{
m_colorSchemeManager.currentScheme->RestRangeCnt();
int Rows = 0, Cols = 0;
float* pData= GetCsvData(CsvPath, Rows, Cols);
CreateImgFromData(pData, Rows, Cols, m_Img);
delete []pData;
return false;
}
bool CCsvToImg::ShowLastImg(CWnd * pSHowWnd)
{
CSingleLock sLck(&m_Section, TRUE);
if (m_Img.IsNull())
return false;
CDC* pDC = pSHowWnd->GetDC();
HDC hDc = pDC->m_hDC;
RECT WndRect;
pSHowWnd->GetClientRect(&WndRect);
pDC->SetStretchBltMode(HALFTONE);
m_Img.StretchBlt(hDc, WndRect);
ReleaseDC(pSHowWnd->m_hWnd, hDc);
return true;
}
bool CCsvToImg::SaveLastImg(const CString & savePath)
{
if (m_Img.IsNull())
return false;
m_Img.Save(savePath);
}
float * CCsvToImg::GetCsvData(const char * path, int & Rows, int & Cols)
{
std::ifstream file;
std::string sFilePath = path;
file.open(sFilePath, std::ifstream::in | std::ofstream::binary);
if (!file.good())
return NULL;
std::stringstream buffer;
buffer << file.rdbuf();
string sFile = buffer.str();
const int size = sFile.size();
const char *str = sFile.c_str();
float * p = new float[size]();
int start = 0;
int end = 0;
int RowCnt = 0;
int idx = 0;
for (int i = 0; i < size; i++)
{
if (str[i] == ',')
{
char s[64]{ 0 };
memcpy(s, str + start, end);
p[idx++] = atof(s);
start = i + 1;
end = 0;
}
else if (str[i] == '\r')
{
char s[64]{ 0 };
memcpy(s, str + start, end);
p[idx++] = atof(s);
start = i + 2;
++i;
end = 0;
RowCnt++;
}
else
{
end++;
}
}
int ColCnt = idx / RowCnt;
Cols = ColCnt;
Rows = RowCnt;
return p;
}
bool CCsvToImg::CreateImgFromData(float * pDataOrg, int Rows, int Cols, CImage & image)
{
CSingleLock sLck(&m_Section, TRUE);
image.Destroy();
image.Create(Rows, Cols,24);
byte* pSourceData = (byte*)image.GetBits();
int image_pitch = 3;
int r = 0, g = 0, b = 0;
int idx = 0;
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
image.SetPixel(i, j, m_colorSchemeManager.currentScheme->getColor(*pDataOrg++));
//½øÐÐRGBÉèÖÃ
/*if (pDataOrg[idx++]<100)
{
image.SetPixel(i, j, RGB_GREEN);
/ **(pSourceData + j * image_pitch + i * 3) = 0;//R
*(pSourceData + j * image_pitch + i * 3 + 1) = 0;//G
*(pSourceData + j * image_pitch + i * 3 + 2) = 255;* /
}
else
{
image.SetPixel(i, j, RGB_RED);
} */
}
}
m_colorSchemeManager.currentScheme->CalRangesPercent();
return true;
}

@ -0,0 +1,127 @@
#pragma once
#include <sstream>
#include <fstream>
#include <vector>
#include <map>
#define RGB_WHITE RGB(255, 255, 255) // 定义 WHITE 颜色
// 颜色定义
#define RGB_RED RGB(255, 0, 0)
#define RGB_ORANGE RGB(255, 165, 0)
#define RGB_YELLOW RGB(255, 255, 0)
#define RGB_GREEN RGB(0, 255, 0)
#define RGB_CYAN RGB(0, 255, 255)
#define RGB_BLUE RGB(0, 0, 255)
#define RGB_PURPLE RGB(128, 0, 128)
#define RGB_GREY RGB(169, 169, 169)
#define RGB_PINK RGB(255, 192, 203)
#define RGB_BLACK RGB(0, 0, 0)
#define RGB_WHITE RGB(255, 255, 255)
#define RGB_BROWN RGB(165, 42, 42)
using namespace std;
extern std::map<const int, int> m_proportionEnergy;
// 颜色范围结构体
struct ColorRange
{
float minEnergy=0;
float maxEnergy=0;
COLORREF color;
const int proportionEnergy=0;
ColorRange(float minE, float maxE, COLORREF col, const int proportionE)
: minEnergy(minE), maxEnergy(maxE), color(col), proportionEnergy(proportionE) {}
int Cnt = 0;
static int AllCnt ;
float dPercent = 0;
};
// 颜色方案类
class ColorScheme {
public:
std::string name; // 方案的名称
std::vector<ColorRange> ranges; // 存储能量范围和对应的颜色
// 默认构造函数
ColorScheme() : name("") {}
// 带参数构造函数
ColorScheme(std::string schemeName) : name(schemeName) {}
// 添加能量范围
void addRange(float minEnergy, float maxEnergy, COLORREF color, const int proportionEnergy)
{
ranges.emplace_back( minEnergy, maxEnergy, color ,proportionEnergy);
}
// 根据能量值返回对应的颜色
COLORREF getColor(float energy)
{
for (auto& range : ranges) {
if (energy >= range.minEnergy && energy <= range.maxEnergy)
{
m_proportionEnergy[range.proportionEnergy]++;
range.Cnt++;
ColorRange::AllCnt++;
return range.color;
}
}
ColorRange::AllCnt++;
return RGB_WHITE; // 默认返回白色
}
void RestRangeCnt()
{
ColorRange::AllCnt = 0;
for (auto& range : ranges)
{
range.Cnt = 0;
}
}
void CalRangesPercent()
{
for (auto& range : ranges)
{
range.dPercent = double(range.Cnt*100) / ColorRange::AllCnt;
}
}
};
// 颜色方案管理类
class ColorSchemeManager
{
public:
std::map<std::string, ColorScheme> schemes;
ColorScheme* currentScheme;
ColorSchemeManager() : currentScheme(nullptr) {}
void addScheme(const std::string& name)
{
schemes[name] = ColorScheme(name);
}
void setCurrentScheme(const std::string& name)
{
currentScheme = &schemes[name];
}
};
class CCsvToImg
{
public:
CCsvToImg();
virtual ~CCsvToImg();
bool ReadCsv2Img(CString CsvPath);
bool ShowLastImg(CWnd * pSHowWnd);
bool SaveLastImg(const CString& savePath);
private:
float * GetCsvData(const char * path, int & Rows, int & Cols);
bool CreateImgFromData(float * pDataOrg, int Rows, int Cols, CImage & OutImg);
CCriticalSection m_Section;
public:
ColorSchemeManager m_colorSchemeManager;
CImage m_Img;
};
extern CCsvToImg * gCsvToImg;

@ -37,12 +37,12 @@ void CFileMgr::ReadFileToStrVec(const std::string& filePath, std::vector<std::ve
// 分割当前行
while ((valueEnd = std::find(valueStart, lineEnd, ',')) != lineEnd) {
row.push_back(std::string(valueStart, valueEnd));
row.emplace_back(std::string(valueStart, valueEnd));
valueStart = valueEnd + 1;
}
// 添加最后一个字段
row.push_back(std::string(valueStart, lineEnd));
row.emplace_back(std::string(valueStart, lineEnd));
// 将行数据添加到结果中
data.push_back(row);

@ -1 +1 @@
D:\programTest\
C:\Users\Administrator\Desktop\Beam.csv
1 D:\programTest\ C:\Users\Administrator\Desktop\Beam.csv

@ -1,11 +1,11 @@
Plan,MinEnergy,MaxEnergy,Color,TF,Proportion(%)
1,0,400,Black,True,47.46%
2,400,800,Blue,True,0.94%
3,800,1200,Brown,True,0.65%
4,1200,1600,Cyan,True,0.27%
5,1600,2000,Green,True,0.27%
6,2000,2400,Orange,True,1.28%
7,2400,2800,Pink,True,32.12%
8,2800,3200,Purple,True,17.00%
9,3200,3600,Red,True,0.00%
10,3600,6000,White,True,0.00%
1,0,22,Blue,True,0
2,23,300,Red,True,0
3,301,600,Green,True,0
4,0,0,Black,False,0
5,0,0,Black,False,0
6,0,0,Black,False,0
7,0,0,Black,False,0
8,0,0,Black,False,0
9,0,0,Black,False,0
10,0,0,Black,False,0

1 Plan MinEnergy MaxEnergy Color TF Proportion(%)
2 1 0 400 22 Black Blue True 47.46% 0
3 2 400 23 800 300 Blue Red True 0.94% 0
4 3 800 301 1200 600 Brown Green True 0.65% 0
5 4 1200 0 1600 0 Cyan Black True False 0.27% 0
6 5 1600 0 2000 0 Green Black True False 0.27% 0
7 6 2000 0 2400 0 Orange Black True False 1.28% 0
8 7 2400 0 2800 0 Pink Black True False 32.12% 0
9 8 2800 0 3200 0 Purple Black True False 17.00% 0
10 9 3200 0 3600 0 Red Black True False 0.00% 0
11 10 3600 0 6000 0 White Black True False 0.00% 0

Binary file not shown.
Loading…
Cancel
Save