Fix:图像角度

master
bestlqiang 1 year ago
parent 23b534e24d
commit 5363de48ef

@ -0,0 +1,154 @@
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <vector>
#include <thread>
#include <cctype>
#include <cstdint>
#include <utility>
// Windows 内存映射文件封装类
class CMappedFile
{
public:
explicit CMappedFile(const char* path)
{
m_hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
if (m_hFile == INVALID_HANDLE_VALUE)
return;
m_hMapping = CreateFileMappingA(m_hFile, nullptr, PAGE_READONLY, 0, 0, nullptr);
if (!m_hMapping)
{
CloseHandle(m_hFile);
m_hFile = INVALID_HANDLE_VALUE;
return;
}
m_data = static_cast<const char*>(MapViewOfFile(m_hMapping, FILE_MAP_READ, 0, 0, 0));
}
~CMappedFile()
{
if (m_data) UnmapViewOfFile(m_data);
if (m_hMapping) CloseHandle(m_hMapping);
if (m_hFile != INVALID_HANDLE_VALUE) CloseHandle(m_hFile);
}
bool IsValid() const { return m_data != nullptr; }
const char* Data() const { return m_data; }
size_t Size() const
{
LARGE_INTEGER li{};
li.LowPart = GetFileSize(m_hFile, reinterpret_cast<DWORD*>(&li.HighPart));
return li.QuadPart;
}
private:
HANDLE m_hFile = INVALID_HANDLE_VALUE;
HANDLE m_hMapping = nullptr;
const char* m_data = nullptr;
};
//光斑RawData多线程解析
class BeamCsvParserMT
{
public:
unsigned char* Load(const char* path, int& width, int& height)
{
m_file = std::make_unique<CMappedFile>(path);
if (!m_file->IsValid())
{
width = height = 0;
return nullptr;
}
size_t cols = DetectColumnCount(m_file->Data());
m_threadResults.resize(1);
m_threadResults[0].reserve(cols*cols);
ParseBlock(0, m_file->Size(), m_threadResults[0]);
size_t total = 0;
for (auto& v : m_threadResults)
total += v.size();
unsigned char* result = new unsigned char[total];
unsigned char* dst = result;
for (auto& v : m_threadResults)
{
memcpy(dst, v.data(), v.size());
dst += v.size();
}
height = static_cast<int>(total / cols);
width = static_cast<int>(cols);
return result;
}
private:
// ===============================
// 内部工具函数
// ===============================
size_t DetectColumnCount(const char* data) const
{
size_t cols = 1;
for (const char* p = data; *p != '\n' && *p != '\r' && *p != '\0'; ++p)
{
if (*p == ',') ++cols;
}
return cols;
}
std::vector<std::pair<size_t, size_t>>SplitIntoLineBlocks(const char* data, size_t size, size_t threadCount, size_t & Rows) const
{
std::vector<size_t> offsets{ 0 };//声明并放入一个0值
for (size_t i = 0; i < size; ++i)
{
if (data[i] == '\n')
offsets.emplace_back(i + 1);
}
if (offsets.back() < size)
offsets.emplace_back(size);
Rows = offsets.size() - 1;
size_t linesPerThread = Rows / threadCount;
std::vector<std::pair<size_t, size_t>> blocks;
for (size_t t = 0; t < threadCount; ++t)
{
size_t start = offsets[t * linesPerThread];
size_t end = (t == threadCount - 1) ? offsets.back() : offsets[(t + 1) * linesPerThread];
blocks.emplace_back(start, end);
}
return blocks;
}
void ParseBlock(size_t start, size_t end, std::vector<unsigned char>& out) const
{
const char* p = m_file->Data() + start;
const char* blockEnd = m_file->Data() + end;
const char* fieldStart = p;
char sVal[32]{ 0 };
while (p < blockEnd)
{
if (*p == ',' || *p == '\n' || *p == '\r')
{
memset(sVal, 0, 32);
memcpy(sVal, fieldStart, p - fieldStart);
out.emplace_back(static_cast<unsigned char>(min(atoi(sVal), 254)));
if (*p == '\r' && (p + 1 < blockEnd && *(p + 1) == '\n'))
++p;
fieldStart = p + 1;
}
++p;
}
}
private:
std::unique_ptr<CMappedFile> m_file;
std::vector<std::vector<unsigned char>> m_threadResults;
};

File diff suppressed because one or more lines are too long

@ -19,10 +19,12 @@ bool CCsvToImg::ReadCsv2Img(CString CsvPath)
m_colorSchemeManager.currentScheme->ResetRangeCnt();
int Rows = 0, Cols = 0;
float* pData= GetCsvData(CsvPath, Rows, Cols);
if (!pData)
return false;
CreateImgFromData(pData, Rows, Cols, m_Img);
m_colorSchemeManager.currentScheme->CalRangesPercent();
delete []pData;
return false;
return true;
}
bool CCsvToImg::ShowLastImg(CWnd * pSHowWnd)
@ -47,21 +49,15 @@ bool CCsvToImg::SaveLastImg(const CString & savePath)
m_Img.Save(savePath);
}
#include "BeamCsvParserMT.h"
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())
CMappedFile mFile(path);
if (!mFile.IsValid())
return NULL;
std::stringstream buffer;
buffer << file.rdbuf();
string sFile = buffer.str();
const int size = sFile.size();
const char *str = sFile.c_str();
const int size = mFile.Size();
const char *str = mFile.Data();
float * p = new float[size]();
@ -108,31 +104,15 @@ bool CCsvToImg::CreateImgFromData(float * pDataOrg, int Rows, int Cols, CImage
{
CSingleLock sLck(&m_Section, TRUE);
image.Destroy();
image.Create(Rows, Cols,24);
byte* pSourceData = (byte*)image.GetBits();
int image_pitch = 3;
image.Create(Cols, Rows,24);
int r = 0, g = 0, b = 0;
int idx = 0;
BYTE r = 0, g = 0, b = 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->getColor(*pDataOrg++,r,g,b);
image.SetPixelRGB(j, i, r,g,b);
}
}
return true;

@ -28,11 +28,24 @@ struct ColorRange
COLORREF color;
const int proportionEnergy=0;
ColorRange(float minE, float maxE, COLORREF col, const int proportionE)
: minEnergy(minE), maxEnergy(maxE), color(col), proportionEnergy(proportionE) {}
: minEnergy(minE), maxEnergy(maxE), color(col), proportionEnergy(proportionE)
{
m_r = GetRValue(color);
m_g = GetGValue(color);
m_b = GetBValue(color);
}
void GetRGB(BYTE &r, BYTE &g, BYTE &b)
{
r = m_r; g = m_g; b = m_b;
}
int Cnt = 0;
static int AllCnt ;
float dPercent = 0;
BYTE m_r = 0;
BYTE m_g = 0;
BYTE m_b = 0;
};
// 颜色方案类
@ -69,6 +82,24 @@ public:
return RGB_WHITE; // 默认返回白色
}
// 根据能量值返回对应的颜色
void getColor(float energy, BYTE &r, BYTE &g, BYTE &b)
{
for (auto& range : ranges)
{
if (energy >= range.minEnergy && energy <= range.maxEnergy)
{
m_proportionEnergy[range.proportionEnergy]++;
range.Cnt++;
ColorRange::AllCnt++;
range.GetRGB(r,g,b);
return;
}
}
ColorRange::AllCnt++;
r = 0;g = 0;b = 0;// 默认返回黑色
}
void ResetRangeCnt()//重置各Range内数据量
{
ColorRange::AllCnt = 0;

@ -1 +0,0 @@
C:\Users\Administrator\Desktop\Beam.csv
1 C:\Users\Administrator\Desktop\Beam.csv

@ -0,0 +1 @@
E:\WorkProjects\CsvToImg\Debug\Beam.csv
1 E:\WorkProjects\CsvToImg\Debug\Beam.csv
Loading…
Cancel
Save