重新实现
parent
9168eb2f6a
commit
93993e09b2
Binary file not shown.
@ -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;
|
@ -1 +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
|
||||
|
|
Binary file not shown.
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue