初始化

This commit is contained in:
2025-11-09 14:10:48 +08:00
commit 97cc26a21d
12 changed files with 1699 additions and 0 deletions

194
Z_3/PicReader.h Normal file
View File

@@ -0,0 +1,194 @@
/******************************************************************
* <20><>ע<EFBFBD>⣡ *
* <20><>ͷ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>WinAPI<50><49>WIC<49>ײ㺯<D7B2><E3BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> *
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD>¹涨 *
* <20><>ͷ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>κ<EFBFBD>û<EFBFBD><C3BB>TO-DO<44>ĵط<C4B5><D8B7><EFBFBD><EFBFBD>㲻Ҫ<E3B2BB>޸ģ<DEB8><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⣬ *
* <20>뼰ʱ<EBBCB0><CAB1>ϵ<EFBFBD><CFB5>ʦ<EFBFBD><CAA6><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD> *
* ÿһ<C3BF><D2BB>TO-DO<44><4F><EFBFBD><EFBFBD>TO-DO<44><4F>˵<EFBFBD><CBB5> (TO-DO) END <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿ<EFBFBD><C9BF>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD> *
* readPic()<29><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>WinAPI<50>еķ<D0B5><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD>ͼƬ<CDBC><C6AC>ȡΪRGBA<42><41> *
* bitmap<61><70><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><E2B2A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>޸<EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӵﵽ<D3B4><EFB5BD>ȡ<EFBFBD>Ҷ<EFBFBD>ͼ<EFBFBD><CDBC> *
* Ŀ<>ġ<EFBFBD> *
* getData()<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>Ƶĺ<C6B5><C4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һάBYTE<54><45><EFBFBD><EFBFBD>ת<EFBFBD><D7AA> *
* <20><><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD>Array<61>ࡣ *
* testReader()<29><>demo<6D><6F><EFBFBD><EFBFBD><E1B9A9>ȡ<EFBFBD><C8A1><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>˼·<CBBC><C2B7> *
******************************************************************/
#ifndef PIC_READER_H
#define PIC_READER_H
#include <windows.h>
#include <wincodec.h>
#include <commdlg.h>
template <typename T>
inline void SafeRelease(T *&p) {
if (nullptr != p) {
p->Release();
p = nullptr;
}
}
class PicReader {
public:
PicReader();
~PicReader();
void readPic(LPCSTR);
void /*TO-DO<44><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>޸ķ<DEB8><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> END*/ getData(/*TO-DO<44><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>޸Ĵ<DEB8><C4B4><EFBFBD> END*/);
void testReader(BYTE *&,UINT &, UINT &);
private:
void init();
bool checkHR(HRESULT);
void quitWithError(LPCSTR);
HWND hWnd;
HANDLE hFile;
IWICImagingFactory *m_pIWICFactory;
IWICFormatConverter *m_pConvertedSourceBitmap;
/*TO-DO<44><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܻ<EFBFBD><DCBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD>Ա END*/
};
PicReader::PicReader() : m_pConvertedSourceBitmap(nullptr), m_pIWICFactory(nullptr) {
init();
}
PicReader::~PicReader() {
if (hFile != NULL) CloseHandle(hFile);
SafeRelease(m_pConvertedSourceBitmap);
SafeRelease(m_pIWICFactory);
CoUninitialize();
}
bool PicReader::checkHR(HRESULT hr) {
return (hr < 0);
}
void PicReader::quitWithError(LPCSTR message) {
MessageBoxA(hWnd, message, "Application Error", MB_ICONEXCLAMATION | MB_OK);
quick_exit(0xffffffff);
}
void PicReader::init() {
hWnd = GetForegroundWindow();
// Enables the terminate-on-corruption feature.
HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);
HRESULT hr = S_OK;
//Init the WIC
hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
// Create WIC factory
hr = CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&m_pIWICFactory)
);
// Throw error if create factor failed
if (checkHR(hr)) { quitWithError("Init Reader Failed"); }
}
void PicReader::readPic(LPCSTR fileName) {
HRESULT hr = S_OK;
// Create a File Handle (WinAPI method not std c)
if (hFile != NULL) CloseHandle(hFile);
hFile = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
quitWithError("Cannot find such file, please retry or check the access");
}
// Create a decoder
IWICBitmapDecoder *pDecoder = nullptr;
hr = m_pIWICFactory->CreateDecoderFromFileHandle((ULONG_PTR)hFile, nullptr, WICDecodeMetadataCacheOnDemand, &pDecoder);
if (checkHR(hr)) { quitWithError("Create Decoder Failed"); }
// Retrieve the first frame of the image from the decoder
IWICBitmapFrameDecode *pFrame = nullptr;
hr = pDecoder->GetFrame(0, &pFrame);
if (checkHR(hr)) { quitWithError("Get Frame Failed"); }
// Format convert the frame to 32bppRGBA
SafeRelease(m_pConvertedSourceBitmap);
hr = m_pIWICFactory->CreateFormatConverter(&m_pConvertedSourceBitmap);
if (checkHR(hr)) { quitWithError("Get Format Converter Failed"); }
hr = m_pConvertedSourceBitmap->Initialize(pFrame, GUID_WICPixelFormat32bppRGBA, WICBitmapDitherTypeNone, nullptr, 0.f, WICBitmapPaletteTypeCustom);
if (checkHR(hr)) { quitWithError("Init Bitmap Failed"); }
// Clean memory
SafeRelease(pDecoder);
SafeRelease(pFrame);
}
void /*TO-DO<44><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>޸ķ<DEB8><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> END*/ PicReader::getData(/*TO-DO<44><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>޸Ĵ<DEB8><C4B4><EFBFBD> END*/) {
HRESULT hr = S_OK;
// Get the size of Image
UINT x, y;
hr = m_pConvertedSourceBitmap->GetSize(&x, &y);
if (checkHR(hr)) { quitWithError("Check Bitmap Size Failed"); }
// Create the buffer of pixels, the type of BYTE is unsigned char
BYTE *data;
data = new BYTE[x * y * 4];
memset(data, 0, x * y * 4);
// Copy the pixels to the buffer
UINT stride = x * 4;
hr = m_pConvertedSourceBitmap->CopyPixels(nullptr, stride, x * y * 4, data);
if (checkHR(hr)) { quitWithError("Copy Pixels Failed"); }
/******************************************************************
* TO-DO: *
* *
* ʵ<><CAB5>һ<EFBFBD><D2BB>Array<61><EFBFBD><E0A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>dataת<61><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Array<61><79> *
* *
* <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Bitmap Copy<70><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>ÿ4<C3BF><34>Ϊһ<CEAA><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> *
* <20><><EFBFBD><EFBFBD>Ϊһ<CEAA><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊͼ<CEAA><CDBC><EFBFBD><EFBFBD>(<28><>*<2A><>*4)<29><>һά<D2BB><CEAC><EFBFBD><EFBFBD> *
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ų<EFBFBD>Ϊ R G B A R G B A R G B A..... *
* *
* <20><>ע<EFBFBD><20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD>Ķ<EFBFBD><C4B6>Ӵ˿<D3B4>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>TO-DO ENDλ<44>õĴ<C3B5><C4B4>룡 *
******************************************************************/
delete[] data;
/******************************************************************
* TO-DO END *
******************************************************************/
// Close the file handle
CloseHandle(hFile);
hFile = NULL;
}
void PicReader::testReader(BYTE* &_out, UINT& _x, UINT& _y){
HRESULT hr = S_OK;
// Get the size of Image
UINT x, y;
hr = m_pConvertedSourceBitmap->GetSize(&x, &y);
if (checkHR(hr)) { quitWithError("Check Bitmap Size Failed"); }
// Create the buffer of pixels, the type of BYTE is unsigned char
BYTE *data;
data = new BYTE[x * y * 4];
memset(data, 0, x * y * 4);
// Copy the pixels to the buffer
UINT stride = x * 4;
hr = m_pConvertedSourceBitmap->CopyPixels(nullptr, stride, x * y * 4, data);
if (checkHR(hr)) { quitWithError("Copy Pixels Failed"); }
_out = data; _x = x; _y = y;
// Close the file handle
CloseHandle(hFile);
hFile = NULL;
}
#endif