5using Microsoft::WRL::ComPtr;
12 if(!source || !result)
return E_INVALIDARG;*result=
nullptr;
13 ComPtr<ID3D11Device> device;source->GetDevice(&device);ComPtr<ID3D11DeviceContext> context;device->GetImmediateContext(&context);
14 D3D11_TEXTURE2D_DESC original{};source->GetDesc(&original);
if(level>=original.MipLevels)
return E_INVALIDARG;
15 if(original.Format!=DXGI_FORMAT_R8G8B8A8_UNORM && original.Format!=DXGI_FORMAT_B8G8R8A8_UNORM
16 && original.Format!=DXGI_FORMAT_B8G8R8X8_UNORM)
return DXGI_ERROR_UNSUPPORTED;
17 D3D11_TEXTURE2D_DESC description=original;description.Width=(std::max)(1u,original.Width>>level);description.Height=(std::max)(1u,original.Height>>level);
18 description.MipLevels=description.ArraySize=1;description.SampleDesc={1,0};description.BindFlags=description.MiscFlags=0;
19 description.Usage=D3D11_USAGE_DEFAULT;description.CPUAccessFlags=0;
20 ComPtr<ID3D11Texture2D> resolved;
HRESULT hr;
21 if(original.SampleDesc.Count>1)
23 if(FAILED(hr=device->CreateTexture2D(&description,
nullptr,&resolved)))
return hr;
24 context->ResolveSubresource(resolved.Get(),0,source,level,description.Format);source=resolved.Get();level=0;
26 description.Usage=D3D11_USAGE_STAGING;description.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
27 ComPtr<ID3D11Texture2D> staging;
if(FAILED(hr=device->CreateTexture2D(&description,
nullptr,&staging)))
return hr;
28 context->CopySubresourceRegion(staging.Get(),0,0,0,0,source,level,
nullptr);
29 const RECT rectangle=crop ? *crop : RECT{0,0,LONG(description.Width),LONG(description.Height)};
30 if(rectangle.left<0 || rectangle.top<0 || rectangle.right>LONG(description.Width) || rectangle.bottom>LONG(description.Height)
31 || rectangle.right<=rectangle.left || rectangle.bottom<=rectangle.top)
return E_INVALIDARG;
32 const UINT width=UINT(rectangle.right-rectangle.left),height=UINT(rectangle.bottom-rectangle.top),pitch=width*4;
33 std::vector<uint8_t> pixels(
size_t(pitch)*height);D3D11_MAPPED_SUBRESOURCE mapped{};
34 if(FAILED(hr=context->Map(staging.Get(),0,D3D11_MAP_READ,0,&mapped)))
return hr;
35 for(UINT y=0;y<height;++y)
37 const auto* row=
static_cast<const uint8_t*
>(mapped.pData)+
size_t(y+rectangle.top)*mapped.RowPitch+size_t(rectangle.left)*4;
38 auto* destination=pixels.data()+size_t(y)*pitch;memcpy(destination,row,pitch);
39 if(original.Format==DXGI_FORMAT_R8G8B8A8_UNORM)
for(UINT x=0;x<width;++x)std::swap(destination[x*4],destination[x*4+2]);
40 if(original.Format==DXGI_FORMAT_B8G8R8X8_UNORM)
for(UINT x=0;x<width;++x)destination[x*4+3]=255;
42 context->Unmap(staging.Get(),0);
43 ComPtr<IWICImagingFactory> factory;
if(FAILED(hr=CoCreateInstance(CLSID_WICImagingFactory,
nullptr,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&factory))))
return hr;
44 ComPtr<IWICBitmap> bitmap;
if(FAILED(hr=factory->CreateBitmapFromMemory(width,height,GUID_WICPixelFormat32bppBGRA,pitch,UINT(pixels.size()),pixels.data(),&bitmap)))
return hr;
45 ComPtr<IStream> stream;
if(FAILED(hr=CreateStreamOnHGlobal(
nullptr,TRUE,&stream)))
return hr;
46 const GUID container=format==
ImageFile::PNG ? GUID_ContainerFormatPng : format==
ImageFile::JPEG ? GUID_ContainerFormatJpeg : GUID_ContainerFormatBmp;
47 ComPtr<IWICBitmapEncoder> encoder;
if(FAILED(hr=factory->CreateEncoder(container,
nullptr,&encoder)))
return hr;
48 if(FAILED(hr=encoder->Initialize(stream.Get(),WICBitmapEncoderNoCache)))
return hr;
49 ComPtr<IWICBitmapFrameEncode> frame;
if(FAILED(hr=encoder->CreateNewFrame(&frame,
nullptr)))
return hr;
50 if(FAILED(hr=frame->Initialize(
nullptr)) || FAILED(hr=frame->SetSize(width,height)))
return hr;
51 GUID pixelFormat=GUID_WICPixelFormat32bppBGRA;
if(FAILED(hr=frame->SetPixelFormat(&pixelFormat)))
return hr;
52 ComPtr<IWICFormatConverter> converter;
if(FAILED(hr=factory->CreateFormatConverter(&converter)))
return hr;
53 if(FAILED(hr=converter->Initialize(bitmap.Get(),pixelFormat,WICBitmapDitherTypeNone,
nullptr,0,WICBitmapPaletteTypeCustom)))
return hr;
54 if(FAILED(hr=frame->WriteSource(converter.Get(),
nullptr)) || FAILED(hr=frame->Commit()) || FAILED(hr=encoder->Commit()))
return hr;
55 STATSTG stat{};
if(FAILED(hr=stream->Stat(&stat,STATFLAG_NONAME)))
return hr;
if(stat.cbSize.HighPart)
return E_OUTOFMEMORY;
56 HGLOBAL memory=
nullptr;
if(FAILED(hr=GetHGlobalFromStream(stream.Get(),&memory)))
return hr;
57 const auto* encoded=
static_cast<const uint8_t*
>(GlobalLock(memory));
if(!encoded)
return HRESULT_FROM_WIN32(GetLastError());
58 auto output=std::make_unique<ImageBuffer>();
output->bytes.assign(encoded,encoded+stat.cbSize.LowPart);GlobalUnlock(memory);
59 *result=
output.release();
return S_OK;
63 if(!path)
return E_INVALIDARG;std::ofstream stream(std::filesystem::path(path),std::ios::binary);
64 if(!stream)
return HRESULT_FROM_WIN32(ERROR_WRITE_FAULT);
65 stream.write(
reinterpret_cast<const char*
>(data.bytes.data()),std::streamsize(data.bytes.size()));
return stream ? S_OK : HRESULT_FROM_WIN32(ERROR_WRITE_FAULT);
69 {
return surface ? Encode(surface->
Native(),surface->
Level(),crop,format,result) : E_INVALIDARG;}
72 ComPtr<ImageBuffer> data;
const HRESULT hr=
SaveSurfaceToMemory(&data,format,surface,
nullptr,crop);
return FAILED(hr) ? hr : Write(path,*data.Get());
76 if(!texture)
return E_INVALIDARG;ComPtr<ID3D11Texture2D> image;
HRESULT hr=texture->
Native()->QueryInterface(IID_PPV_ARGS(&image));
if(FAILED(hr))
return hr;
77 ComPtr<ImageBuffer> data;hr=Encode(image.Get(),0,
nullptr,format,&data);
return FAILED(hr) ? hr : Write(path,*data.Get());
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
ID3D11Texture2D * Native() const
ID3D11Resource * Native() const
HRESULT SaveSurfaceToFile(const wchar_t *path, ImageFile format, RenderSurface *surface, const void *, const RECT *crop)
HRESULT SaveSurfaceToMemory(ImageBuffer **result, ImageFile format, RenderSurface *surface, const void *, const RECT *crop)
HRESULT SaveTextureToFile(const wchar_t *path, ImageFile format, RenderTexture *texture, const void *)