Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1.  
  2. class hresult_exception : public std::exception {
  3. HRESULT hr;
  4.  
  5. public:
  6. hresult_exception(HRESULT hr_) : hr(hr_) {}
  7. };
  8.  
  9. struct hresult_thrower {
  10. hresult_thrower& operator<<(HRESULT hr) {
  11. if (hr != S_OK)
  12. throw hresult_exception(hr);
  13.  
  14. return *this;
  15. }
  16. };
  17.  
  18. void png_encoder::save_png(std::wstring const &filename, image const &image) {
  19. hresult_thrower th;
  20.  
  21. com_ptr<IWICStream> stream;
  22. factory->CreateStream(stream.inject());
  23. th << stream->InitializeFromFilename(filename.c_str(), GENERIC_WRITE);
  24.  
  25. com_ptr<IWICBitmapEncoder> encoder;
  26. th << factory->CreateEncoder(GUID_ContainerFormatPng, 0, encoder.inject());
  27. th << encoder->Initialize(stream.get(), WICBitmapEncoderNoCache);
  28.  
  29. com_ptr<IWICBitmapFrameEncode> bitmap_frame;
  30. com_ptr<IPropertyBag2> property_bag;
  31. th << encoder->CreateNewFrame(bitmap_frame.inject(), property_bag.inject());
  32.  
  33. WICPixelFormatGUID formatGUID = GUID_WICPixelFormat24bppRGB;
  34. th << bitmap_frame->Initialize(property_bag.get());
  35. th << bitmap_frame->SetSize(image.width(), image.height());
  36. th << bitmap_frame->SetPixelFormat(&formatGUID);
  37. th << bitmap_frame->WritePixels(image.height(), image.stride(), image.buffer_size(), image.buffer());
  38. th << bitmap_frame->Commit();
  39.  
  40. th << encoder->Commit();
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement