Skip to content

Instantly share code, notes, and snippets.

@yuiAs
Created December 15, 2015 09:47
Show Gist options
  • Select an option

  • Save yuiAs/f6b440625ca307be3f0b to your computer and use it in GitHub Desktop.

Select an option

Save yuiAs/f6b440625ca307be3f0b to your computer and use it in GitHub Desktop.
#include <SDKDDKVer.h>
#include <Windows.h>
#include <wincodec.h>
#include <comdef.h>
#include <wrl.h>
HRESULT test()
{
HRESULT hr;
Microsoft::WRL::ComPtr<IWICImagingFactory> wicFactory;
hr = CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&wicFactory));
if (FAILED(hr)) {
return hr;
}
Microsoft::WRL::ComPtr<IWICBitmapDecoder> wicDecoder;
hr = wicFactory->CreateDecoderFromFilename(L"BwYnbOQCIAANnZ9.jpg", nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &wicDecoder);
if (FAILED(hr)) {
return hr;
}
// BitmapFrameDecoderは BitmapSouceを継承
Microsoft::WRL::ComPtr<IWICBitmapFrameDecode> wicFrameDecode;
hr = wicDecoder->GetFrame(0, &wicFrameDecode);
if (FAILED(hr)) {
return hr;
}
UINT w, h;
WICPixelFormatGUID pixelFormat;
hr = wicFrameDecode->GetSize(&w, &h);
if (FAILED(hr)) {
return hr;
} else {
wprintf(L"w=%d h=%d\n", w, h);
}
hr = wicFrameDecode->GetPixelFormat(&pixelFormat);
if (FAILED(hr)) {
return hr;
} else {
WCHAR s[MAX_PATH];
StringFromGUID2(pixelFormat, s, MAX_PATH);
wprintf(L"%s\n", s);
}
Microsoft::WRL::ComPtr<IWICStream> wicOutputStream;
hr = wicFactory->CreateStream(&wicOutputStream);
if (FAILED(hr)) {
return hr;
}
hr = wicOutputStream->InitializeFromFilename(L"outputFile.bmp", GENERIC_WRITE);
if (FAILED(hr)) {
return hr;
}
Microsoft::WRL::ComPtr<IWICBitmapEncoder> wicEncoder;
hr = wicFactory->CreateEncoder(GUID_ContainerFormatBmp, nullptr, &wicEncoder);
if (FAILED(hr)) {
return hr;
}
hr = wicEncoder->Initialize(wicOutputStream.Get(), WICBitmapEncoderNoCache);
if (FAILED(hr)) {
return hr;
}
Microsoft::WRL::ComPtr<IWICBitmapFrameEncode> wicFrameEncode;
hr = wicEncoder->CreateNewFrame(&wicFrameEncode, nullptr);
if (FAILED(hr)) {
return hr;
}
hr = wicFrameEncode->Initialize(nullptr);
if (FAILED(hr)) {
return hr;
}
hr = wicFrameEncode->SetSize(w, h);
if (FAILED(hr)) {
return hr;
}
hr = wicFrameEncode->SetPixelFormat(&pixelFormat);
if (FAILED(hr)) {
return hr;
}
hr = wicFrameEncode->WriteSource(wicFrameDecode.Get(), nullptr);
if (FAILED(hr)) {
return hr;
}
hr = wicFrameEncode->Commit();
if (FAILED(hr)) {
return hr;
}
hr = wicEncoder->Commit();
if (FAILED(hr)) {
return hr;
}
return hr;
}
int main()
{
HRESULT hr;
hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED|COINIT_DISABLE_OLE1DDE);
if (FAILED(hr)) {
return static_cast<int>(hr);
}
hr = test();
if (FAILED(hr)) {
return static_cast<int>(hr);
}
CoUninitialize();
return static_cast<int>(hr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment