Tony Teveris
Tony Teveris

Reputation: 195

Writing IWICBtmap to file - incorrect resolution

I have the following code where I read an image (JPG for test), specify a clipping rectangle area and write the "clipped" area out to a file.

My test image is 320 x 240 pixels @ 300dpi. When I read it in all indications say that it is that size but when I write it out the result image is 102 x 76 and looking a the file properties I see no H/V resolution.

Now 320/102 = 3.1372 and 300/96 = 3.125 so is there something with screen resolution vs image?

This entire subject of writing out a IWICBitmap has been a boxing match from the beginning. Why is it this hard?

Thanks a bunch

IWICImagingFactory *pImageFactory = GfxAgent::WICImagingFactory::GetInstance().GetFactory();

   D2D1_SIZE_U sizeFrame = D2D1::SizeU(imageRect.Width(), imageRect.Height());

   CComPtr<IWICBitmap> pWICBitmap;
   hr = pImageFactory->CreateBitmap(imageRect.Width(), imageRect.Height(),
      GUID_WICPixelFormat32bppPBGRA,
      WICBitmapCacheOnLoad,
      &pWICBitmap
      );

   D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties();
   rtProps.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
   rtProps.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
   rtProps.usage = D2D1_RENDER_TARGET_USAGE_NONE;

   // define the render target
   CComPtr<ID2D1RenderTarget> pRenderTarget = 0;
   hr = m_pDirect2dFactory->CreateWicBitmapRenderTarget(pWICBitmap, rtProps, &pRenderTarget);

   CComPtr<ID2D1Bitmap> imageS;
   hr = GfxAgent::ImageUtilities::LoadImageFromFile(pRenderTarget, m_imgPath, 0, 0, 0, &imageS, &resX, &resY);

   if (hr != S_OK)
   {
   }

   // get format of image we just read
   D2D1_PIXEL_FORMAT fmt = imageS->GetPixelFormat();

   CComPtr<ID2D1Bitmap> imageD;
   D2D1_SIZE_U bitmapPixelSize = D2D1::SizeU(imageRect.Width(), imageRect.Height());

   // create destination image of "clipped" source image
   hr = pRenderTarget->CreateBitmap(bitmapPixelSize, D2D1::BitmapProperties(
      D2D1::PixelFormat(fmt.format, fmt.alphaMode),
      (float)resX, (float)resY), &imageD);

   D2D1_POINT_2U topleft = D2D1::Point2U(0, 0);
   D2D1_RECT_U srcRect = D2D1::RectU(imageRect.left, imageRect.top, imageRect.right, imageRect.bottom);
   // get the "clipped" source
   hr = imageD->CopyFromBitmap(&topleft, imageS, &srcRect);

   if (hr != S_OK)
   {
   }

   CComPtr<IWICBitmapEncoder> pEncoder;
   CComPtr<IWICBitmapFrameEncode> pFrame;
   CComPtr<IWICStream> pStream;

   WICPixelFormatGUID format = GUID_WICPixelFormat32bppPBGRA;

   // draw the "clipped" image into the render target (WIC image)
   if (SUCCEEDED(hr)) {
      pRenderTarget->BeginDraw();
      pRenderTarget->Clear();
      pRenderTarget->DrawBitmap(imageD);
      hr = pRenderTarget->EndDraw();
   }

   // now proceed to write the "clipped" image to a file
   if (SUCCEEDED(hr)) {
      hr = pImageFactory->CreateStream(&pStream);
   }

   if (SUCCEEDED(hr)) {
      hr = pStream->InitializeFromFilename(MultiByteToUnicode(szNewFileName).c_str(), GENERIC_WRITE);
   }

   if (SUCCEEDED(hr)) {
      hr = pImageFactory->CreateEncoder(GUID_ContainerFormatJpeg, NULL, &pEncoder);
   }

   if (SUCCEEDED(hr)) {
      hr = pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
   }

   if (SUCCEEDED(hr)) {
      hr = pEncoder->CreateNewFrame(&pFrame, NULL);
   }

   if (SUCCEEDED(hr)) {
      hr = pFrame->Initialize(NULL);
   }

   if (SUCCEEDED(hr)) {
      hr = pFrame->SetSize((UINT)imageD->GetSize().width, (UINT)imageD->GetSize().height);
   }

   if (SUCCEEDED(hr)) {
      hr = pFrame->SetPixelFormat(&format);
   }

   if (SUCCEEDED(hr)) {
      hr = pFrame->WriteSource(pWICBitmap, NULL);
   }

   if (SUCCEEDED(hr)) {
      hr = pFrame->Commit();
   }

   if (SUCCEEDED(hr)) {
      hr = pEncoder->Commit();
   }

More info

      D2D1_POINT_2U topleft = D2D1::Point2U(0, 0);
   D2D1_RECT_U srcRect = D2D1::RectU(imageRect.left, imageRect.top, imageRect.right, imageRect.bottom);
   // get the "clipped" source
   hr = imageD->CopyFromBitmap(&topleft, imageS, &srcRect);

   if (hr != S_OK)
   {
   }

   UINT wD, hD;
   wD = (UINT)imageD->GetSize().width;
   hD = (UINT)imageD->GetSize().height;

The imageRect is correct LTRB = 0,0,320,240

but after the copy wD = 102 and hD = 76

Why?

Here is some more info

CComPtr<ID2D1Bitmap> imageD;
   D2D1_SIZE_U bitmapPixelSize = D2D1::SizeU(imageRect.Width(), imageRect.Height());

   // create destination image of "clipped" source image
   hr = pRenderTarget->CreateBitmap(bitmapPixelSize, D2D1::BitmapProperties(
      D2D1::PixelFormat(fmt.format, fmt.alphaMode),
      (float)resX, (float)resY), &imageD);

   UINT wD, hD;
   wD = (UINT)imageD->GetSize().width;
   hD = (UINT)imageD->GetSize().height;

The bitmapPixelSize is correct 320 x 240, resX and Y are 300

    format= DXGI_FORMAT_B8G8R8A8_UNORM
    alphaMode   D2D1_ALPHA_MODE_PREMULTIPLIED

wD = 102 and hD = 76 - Why?

Latest code

IWICImagingFactory *pImageFactory = GfxAgent::WICImagingFactory::GetInstance().GetFactory();

   D2D1_SIZE_U sizeFrame = D2D1::SizeU(imageRect.Width(), imageRect.Height());

   CComPtr<IWICBitmap> pWICBitmap;
   hr = pImageFactory->CreateBitmap(imageRect.Width(), imageRect.Height(),
      GUID_WICPixelFormat32bppPBGRA,
      WICBitmapCacheOnLoad,
      &pWICBitmap
      );

   // sanity check
   UINT wicW, wicH;
   pWICBitmap->GetSize(&wicW, &wicH);

   D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties();
   rtProps.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
   rtProps.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
   rtProps.usage = D2D1_RENDER_TARGET_USAGE_NONE;

   // define the render target
   CComPtr<ID2D1RenderTarget> pRenderTarget = 0;
   hr = m_pDirect2dFactory->CreateWicBitmapRenderTarget(pWICBitmap, rtProps, &pRenderTarget);

   CComPtr<ID2D1Bitmap> imageS;
   hr = GfxAgent::ImageUtilities::LoadImageFromFile(pRenderTarget, m_imgPath, 0, 0, 0, &imageS, &resX, &resY);

   if (hr != S_OK)
   {
   }

   // set new image resolution same as source
   pWICBitmap->SetResolution(resX, resY);

   // get format of image we just read
   D2D1_PIXEL_FORMAT fmt = imageS->GetPixelFormat();

   CComPtr<ID2D1Bitmap> imageD;
   D2D1_SIZE_U bitmapPixelSize = D2D1::SizeU(imageRect.Width(), imageRect.Height());

   // create destination image of "clipped" source image
   hr = pRenderTarget->CreateBitmap(bitmapPixelSize, D2D1::BitmapProperties(
      D2D1::PixelFormat(fmt.format, fmt.alphaMode),
      (float)resX, (float)resY), &imageD);

   D2D1_POINT_2U topleft = D2D1::Point2U(0, 0);
   D2D1_RECT_U srcRect = D2D1::RectU(imageRect.left, imageRect.top, imageRect.right, imageRect.bottom);
   // get the "clipped" source
   hr = imageD->CopyFromBitmap(&topleft, imageS, &srcRect);

   if (hr != S_OK)
   {
   }

   // just a sanity check (pixels NOT DIPS)
   D2D1_SIZE_U sourcePixelSize = imageS->GetPixelSize();
   D2D1_SIZE_U destPixelSize = imageD->GetPixelSize();

   CComPtr<IWICBitmapEncoder> pEncoder;
   CComPtr<IWICBitmapFrameEncode> pFrame;
   CComPtr<IWICStream> pStream;

   WICPixelFormatGUID format = GUID_WICPixelFormat32bppPBGRA;

   // draw the "clipped" image into the render target (WIC image)
   if (SUCCEEDED(hr)) {
      pRenderTarget->BeginDraw();
      pRenderTarget->Clear();
      pRenderTarget->DrawBitmap(imageD);
      hr = pRenderTarget->EndDraw();
   }

   // now proceed to write the "clipped" image to a file
   if (SUCCEEDED(hr)) {
      hr = pImageFactory->CreateStream(&pStream);
   }

   if (SUCCEEDED(hr)) {
      hr = pStream->InitializeFromFilename(MultiByteToUnicode(szNewFileName).c_str(), GENERIC_WRITE);
   }

   if (SUCCEEDED(hr)) {
      hr = pImageFactory->CreateEncoder(GUID_ContainerFormatJpeg, NULL, &pEncoder);
   }

   if (SUCCEEDED(hr)) {
      hr = pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
   }

   if (SUCCEEDED(hr)) {
      hr = pEncoder->CreateNewFrame(&pFrame, NULL);
   }

   if (SUCCEEDED(hr)) {
      hr = pFrame->Initialize(NULL);
   }

   if (SUCCEEDED(hr)) {
      hr = pFrame->SetSize(destPixelSize.width, destPixelSize.height);
   }

   if (SUCCEEDED(hr)) {
      hr = pFrame->SetPixelFormat(&format);
   }

   if (SUCCEEDED(hr)) {
      hr = pFrame->WriteSource(pWICBitmap, NULL);
   }

   if (SUCCEEDED(hr)) {
      hr = pFrame->Commit();
   }

   if (SUCCEEDED(hr)) {
      hr = pEncoder->Commit();
   }

Upvotes: 2

Views: 742

Answers (2)

Tony Teveris
Tony Teveris

Reputation: 195

Knowing what the original image's resolution is when I create the render target from the WICBitmap I used the following properties

   D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties();
   rtProps.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
   rtProps.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
   rtProps.usage = D2D1_RENDER_TARGET_USAGE_NONE;
   rtProps.dpiX = (float)m_img.GetResolutionX();
   rtProps.dpiY = (float)m_img.GetResolutionY();

the key was using the resolution of the original image in the create, attempting to set them later did nothing. Roman gave me the "hint" - Thanks

Upvotes: 0

Roman Ryltsov
Roman Ryltsov

Reputation: 69734

ID2D1Bitmap::GetSize gets you DIPs:

Returns the size, in device-independent pixels (DIPs), of the bitmap.

A DIP is 1/96 of an inch. To retrieve the size in device pixels, use the ID2D1Bitmap::GetPixelSize method.

In your case the size is 320 px * 96 dpi/px / 300 dpi = 102.4 device-independent pixels. The same along Y axis.

Upvotes: 1

Related Questions