Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class hresult_exception : public std::exception {
- HRESULT hr;
- public:
- hresult_exception(HRESULT hr_) : hr(hr_) {}
- };
- struct hresult_thrower {
- hresult_thrower& operator<<(HRESULT hr) {
- if (hr != S_OK)
- throw hresult_exception(hr);
- return *this;
- }
- };
- void png_encoder::save_png(std::wstring const &filename, image const &image) {
- hresult_thrower th;
- com_ptr<IWICStream> stream;
- factory->CreateStream(stream.inject());
- th << stream->InitializeFromFilename(filename.c_str(), GENERIC_WRITE);
- com_ptr<IWICBitmapEncoder> encoder;
- th << factory->CreateEncoder(GUID_ContainerFormatPng, 0, encoder.inject());
- th << encoder->Initialize(stream.get(), WICBitmapEncoderNoCache);
- com_ptr<IWICBitmapFrameEncode> bitmap_frame;
- com_ptr<IPropertyBag2> property_bag;
- th << encoder->CreateNewFrame(bitmap_frame.inject(), property_bag.inject());
- WICPixelFormatGUID formatGUID = GUID_WICPixelFormat24bppRGB;
- th << bitmap_frame->Initialize(property_bag.get());
- th << bitmap_frame->SetSize(image.width(), image.height());
- th << bitmap_frame->SetPixelFormat(&formatGUID);
- th << bitmap_frame->WritePixels(image.height(), image.stride(), image.buffer_size(), image.buffer());
- th << bitmap_frame->Commit();
- th << encoder->Commit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement