#include "stdafx.h" #include "CsvToImg.h" CCsvToImg * gCsvToImg = new CCsvToImg; std::map m_proportionEnergy; int ColorRange::AllCnt = 0; CCsvToImg::CCsvToImg() { } CCsvToImg::~CCsvToImg() { } bool CCsvToImg::ReadCsv2Img(CString CsvPath) { m_colorSchemeManager.currentScheme->ResetRangeCnt(); int Rows = 0, Cols = 0; float* pData= GetCsvData(CsvPath, Rows, Cols); CreateImgFromData(pData, Rows, Cols, m_Img); m_colorSchemeManager.currentScheme->CalRangesPercent(); 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(Cols, Rows,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(j, i, 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); } */ } } return true; }