MCPcopy Create free account
hub / github.com/Aleksoid1978/VideoRenderer / SaveToBMP

Function SaveToBMP

Source/Helper.cpp:1019–1073  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1017}
1018
1019HRESULT SaveToBMP(BYTE* src, const UINT src_pitch, const UINT width, const UINT height, const UINT bitdepth, const wchar_t* filename)
1020{
1021 if (!src || !filename) {
1022 return E_POINTER;
1023 }
1024
1025 if (!src_pitch || !width || !height || (bitdepth != 8 && bitdepth != 32)) {
1026 return E_ABORT;
1027 }
1028
1029 const UINT tablecolors = (bitdepth == 8) ? 256 : 0;
1030 const UINT dst_pitch = width * bitdepth / 8;
1031 const UINT len = dst_pitch * height;
1032
1033 std::unique_ptr<BYTE[]> dib(new(std::nothrow) BYTE[sizeof(BITMAPINFOHEADER) + tablecolors * 4 + len]);
1034
1035 if (dib) {
1036 BITMAPINFOHEADER* bih = (BITMAPINFOHEADER*)dib.get();
1037 ZeroMemory(bih, sizeof(BITMAPINFOHEADER));
1038 bih->biSize = sizeof(BITMAPINFOHEADER);
1039 bih->biWidth = width;
1040 bih->biHeight = -(LONG)height; // top-down RGB bitmap
1041 bih->biBitCount = bitdepth;
1042 bih->biPlanes = 1;
1043 bih->biSizeImage = DIBSIZE(*bih);
1044 bih->biClrUsed = tablecolors;
1045
1046 BYTE* p = (BYTE*)(bih + 1);
1047 for (unsigned i = 0; i < tablecolors; i++) {
1048 *p++ = (BYTE)i;
1049 *p++ = (BYTE)i;
1050 *p++ = (BYTE)i;
1051 *p++ = 0;
1052 }
1053
1054 CopyPlaneAsIs(height, p, dst_pitch, src, src_pitch);
1055
1056 BITMAPFILEHEADER bfh;
1057 bfh.bfType = 0x4d42;
1058 bfh.bfOffBits = sizeof(bfh) + sizeof(BITMAPINFOHEADER) + tablecolors * 4;
1059 bfh.bfSize = bfh.bfOffBits + len;
1060 bfh.bfReserved1 = bfh.bfReserved2 = 0;
1061
1062 FILE* fp;
1063 if (_wfopen_s(&fp, filename, L"wb") == 0) {
1064 fwrite(&bfh, sizeof(bfh), 1, fp);
1065 fwrite(dib.get(), sizeof(BITMAPINFOHEADER) + tablecolors * 4 + len, 1, fp);
1066 fclose(fp);
1067
1068 return S_OK;
1069 }
1070 }
1071
1072 return E_FAIL;
1073}
1074
1075HRESULT SaveToImage(BYTE* src, const UINT pitch, const UINT width, const UINT height, const UINT bitdepth, const std::wstring_view filename)
1076{

Callers 2

Dump4ByteSurfaceFunction · 0.70
DumpTexture2DFunction · 0.70

Calls 1

CopyPlaneAsIsFunction · 0.85

Tested by

no test coverage detected