// ******************************************************************************************************************
// This example lists the quality factor range for one of the LEAD Png Encoder (subformat,guidPixelFormat) pairs
// that support predefined quality factors
// See the Example Requirements for help in running this sample
// MyGetSubFormatFriendlyName gets the friendly name of the subformat
CString MyGetSubFormatFriendlyName(IWICLeadBitmapEncoder *piLeadBitmapEncoder, WICPixelFormatGUID guidPixelFormat, UINT uSubformatIndex)
{
CString csRet;
HRESULT hr = S_OK;
UINT uActual = 0;
WCHAR *pwzFriendlyName = NULL;
if (piLeadBitmapEncoder)
{
IWICBitmapEncoder *piBitmapEncoder = NULL;
// Get subformat friendly name
IFS(piLeadBitmapEncoder->GetSubFormatFriendlyName(guidPixelFormat, uSubformatIndex, 0, NULL, &uActual));
if (uActual > 0)
{
pwzFriendlyName = new WCHAR[uActual];
if (pwzFriendlyName)
{
IFS(piLeadBitmapEncoder->GetSubFormatFriendlyName(guidPixelFormat, uSubformatIndex, uActual, pwzFriendlyName, &uActual));
if (SUCCEEDED(hr))
csRet = pwzFriendlyName;
}
}
}
DELETE_POINTER(pwzFriendlyName);
return csRet;
}
HRESULT IWICLeadBitmapEncoder_GetQualityFactorRange(HWND hWnd)
{
HRESULT hr = S_OK;
IWICImagingFactory *piImagingFactory = NULL;
IWICBitmapEncoder *piBitmapEncoder = NULL;
IWICLeadBitmapEncoder *piLeadBitmapEncoder = NULL;
CString csExampleName = L"IWICLeadBitmapEncoder_GetQualityFactorPredefined.htm";
CString csMsg = csExampleName + "\n\n";
GUID guidContainerFormat = GUID_ContainerFormatLeadPng;
WICPixelFormatGUID guidPixelFormat = GUID_WICPixelFormat24bppBGR;
// Create a LEAD TIFF Bitmap Encoder
IFS(CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*) &piImagingFactory));
IFS(piImagingFactory->CreateEncoder(guidContainerFormat, NULL, &piBitmapEncoder));
// QueryInterface on the encoder to get the IWICLeadBitmapEncoder interface
IFS(piBitmapEncoder->QueryInterface(IID_WICLeadBitmapEncoder, reinterpret_cast<void**>(&piLeadBitmapEncoder)));
// **************************************************************************
// First, get the sub formats for the LEAD Png Encoder with 24 bits per pixel
// **************************************************************************
// first call is to get number of subformats
UINT uSubFormats = 0;
WICLeadSubFormat *pSubFormats = NULL;
IFS(piLeadBitmapEncoder->GetSubFormats(guidPixelFormat, 0, NULL, &uSubFormats));
if (SUCCEEDED(hr))
{
pSubFormats = new WICLeadSubFormat[uSubFormats];
if (pSubFormats)
{
// second call is to get the subformats
IFS(piLeadBitmapEncoder->GetSubFormats(guidPixelFormat, uSubFormats, pSubFormats, &uSubFormats));
}
}
// **************************************************************************
// Next, get the sub format flags for the sub-formats.
// Continue this until we find a subformat that supports WICLeadFlagQualityFactor
// **************************************************************************
INT nIndex = -1;
WICLeadSubFormatFlags uFlags = WICLeadFlagNone;
for (UINT i=0; i < uSubFormats; i++)
{
IFS(piLeadBitmapEncoder->GetSubFormatFlags(guidPixelFormat, i, &uFlags));
if (uFlags & WICLeadFlagQualityFactor)
nIndex = i;
}
// **************************************************************************
// If we found a subformat that supports WICLeadFlagQualityFactor,
// * print the the friendly name for the subformat
// * print the quality factor range
// **************************************************************************
BOOL bQualityFactorRangeSupported = FALSE;
INT nMinQualityFactor = 0;
INT nMaxQualityFactor = 0;
if (nIndex >= 0)
{
csMsg = csMsg + L"LEAD Png Encoder sub-format: " + MyGetSubFormatFriendlyName(piLeadBitmapEncoder, guidPixelFormat, nIndex) + L"\n\n";
IFS(piLeadBitmapEncoder->GetQualityFactorRange(guidPixelFormat, nIndex, &bQualityFactorRangeSupported, &nMinQualityFactor, &nMaxQualityFactor));
if (SUCCEEDED(hr) && bQualityFactorRangeSupported)
{
CString csTemp;
csTemp.Format(L"Quality Factor Range: %d to %d\n", nMinQualityFactor, nMaxQualityFactor);
csMsg = csMsg + csTemp;
}
}
RELEASE_INTERFACE(piLeadBitmapEncoder);
RELEASE_INTERFACE(piBitmapEncoder);
RELEASE_INTERFACE(piImagingFactory);
MessageBox(hWnd, csMsg, csExampleName, MB_OK);
return hr;
}