从 IWICBitmapSource、IWICBitmap、IWICBitmapDecoder 获取位图 BitsPerPixel(无论在何处)

Spa*_*ott 2 bitmap directdraw direct2d

我想主题已经说明了一切。但在一些细节上,我正在加载、操作、然后显示位图,并且是在 GDI 中完成的。现在我想增加一些速度,因为它反复发生。

许多年前,我将 DirectX 用于其他用途,但我所看到的一切都表明要摆脱 DirectX,转向 Direct2D。所以是时候扔掉所有这些知识并重新开始了。真正的 MS 方式;-)

好吧,为了操作位图,在进行渲染之前,我发现可以使用“Lock()”来获取它。界面上的功能也会告诉我尺寸。但我还需要了解 BBP,并跨步。

在有人说“你为什么不使用 ::GetBBP()... DUH”之前,经过数小时的 MSDN 和其他来源搜索后,我一直无法远程找到类似的东西。还有很多非常令人困惑的 COM 接口。

我唯一能找到的是 GetPixelFormat(),它返回一个 GUID,然后我编写大约 150 个“if (...)”语句来比较它。这样我就可以测试它的三个值,如果不是其中一个值则拒绝(1,8,32),这几乎不是处理这个问题的有效方法。

GetPixelFormat() 也不告诉我步幅。

有没有办法做到这一点?

(位图也是未压缩的,所以我什至不需要通过 IWICBitmapDecoder 运行它们,但我还没有弄清楚如何简单地告诉 IWICBitmap“这是一个 blob,将其用作大小为 xy 的位图”)

感谢您的任何帮助。

-斯科特

sod*_*ick 6

上面的答案是正确的方法,绝对不需要创建 50 行 if 语句。我必须承认这是我首先做的,但是如果你得到一个新的格式怎么办?你不会有答案。这样做总是会有效的。

感谢上面发布此答案的人提供的解决方案,但伪代码计算步幅有点错误。步长本质上是对所有位进行编码所需的字节数。因此,如果剩余的字节数少于 8 位,则需要再获取 1 个字节来管理它们。

这是 C++ 的答案,大部分是从 WIC 文档复制的。该文档非常出色,但正如所指出的,没有如何获取 IWICPixelFormatInfo 实例的示例。这是我无法得到的部分。

获取 BitsPerPixel 和 Stride:

首先:获取一个 IWICBitmapFrameDecode 作为 Bitmap 源

// Initialize COM.
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

IWICImagingFactory *piFactory = NULL;
IWICBitmapDecoder *piDecoder = NULL;
IWICBitmapFrameDecode *pIDecoderFrame  = NULL;
GUID *pPixelFormat = NULL; 

// Create the COM imaging factory.
if (SUCCEEDED(hr))
{
    hr = CoCreateInstance(CLSID_WICImagingFactory,
    NULL, CLSCTX_INPROC_SERVER,
    IID_PPV_ARGS(&piFactory));
}

// Create the decoder.
if (SUCCEEDED(hr))
{
    hr = piFactory->CreateDecoderFromFilename(L"test.tif", NULL, GENERIC_READ,
        WICDecodeMetadataCacheOnDemand, //For JPEG lossless decoding/encoding.
        &piDecoder);
}

// Retrieve the First frame from the image (tif might have more than 1)
if (SUCCEEDED(hr))
{
   hr = pIDecoder->GetFrame(0, &pIDecoderFrame);
}
Run Code Online (Sandbox Code Playgroud)

接下来获取像素格式和 BitsPerPixel

//////////////////////////////////////////////////////////////////////////////////
//// IMPORTANT PART OF THE ANSWER
//////////////////////////////////////////////////////////////////////////////////

// Get the Pixel Format
IDecoderFrame.GetPixelFormat(&pPixelFormat);

// Now get a POINTER to an instance of the Pixel Format    
IWICComponentInfo *pIComponentInfo = NULL;
if (SUCCEEDED(hr))
{
    hr = piFactory->CreateComponentInfo(pPixelFormat, &pIComponentInfo);
}

// Get IWICPixelFormatInfo from IWICComponentInfo
IWICPixelFormatInfo *pIPixelFormatInfo;

hr = pIComponentInfo->QueryInterface(__uuidof(IWICPixelFormatInfo), reinterpret_cast<void**>(&pIPixelFormatInfo));

// Now get the Bits Per Pixel
UInt32 bitsPerPixel;
if (SUCCEEDED(hr))
{
   hr = pIPixelFormatInfo.GetBitsPerPixel(&bitsPerPixel);
}
Run Code Online (Sandbox Code Playgroud)

你有两个选择:

//Manually Calculate the Stride from the Bits Per Pixel.
UINT width;
UINT height;
if (SUCCEEDED(hr))
{
   hr = IDecoderFrame.GetSize(&width, &height);
}
float totalPixels = bitsPerPixel * width + 7; // +7 forces to next byte if needed
UINT stride = totalPixels / 8;

// ... now do something with stride-You can stop here if you dont need the bitmap

//////////////////////////////////////////////////////////////////////////////////
Run Code Online (Sandbox Code Playgroud)

或者,如果您想使用该图像,您可以创建它......

// Alternative is to get a lock, by you need to actually create the bitmap
// by calling CreateBitmapFromSource.  IF you do want to create the bitmap,
// then by all means create one.  


IWICBitmap *pIBitmap = NULL;
IWICBitmapLock *pILock = NULL;


WICRect rcLock = { 0, 0, width, height }; // from GetSize earlier

// Create the bitmap from the image frame.
if (SUCCEEDED(hr))
{
   hr = m_pIWICFactory->CreateBitmapFromSource(
      pIDecoderFrame,          // Create a bitmap from the image frame
      WICBitmapCacheOnDemand,  // Cache metadata when needed
      &pIBitmap);              // Pointer to the bitmap
}

if (SUCCEEDED(hr))
{
   hr = pIBitmap->Lock(&rcLock, WICBitmapLockWrite, &pILock);   
}

// Now get the stride from the lock.
piLock.GetStride(&stride);

/// END OF ANSWER
/////////////////////////////////////////////////////////////////////////////////
Run Code Online (Sandbox Code Playgroud)

别忘了收拾一下...

SafeRelease(&pILock);
...
Run Code Online (Sandbox Code Playgroud)

更新:2020 年。对于托马斯。

当我回答这个问题时,我实际上正在编写 Delphi 代码,所以我正在翻译回 c++ :

给定这个 Pascal 标头:

function CreateComponentInfo(const clsidComponent: TGUID;
      out ppIInfo: IWICComponentInfo): HRESULT; stdcall;
Run Code Online (Sandbox Code Playgroud)

大概翻译成:

HRESULT CreateComponentInfo(pGUID *clsidComponent, pIWICComponentInfo *ppIInfo);
Run Code Online (Sandbox Code Playgroud)

我怀疑我在上面的示例中没有正确引用指针。

这是工作的 Delphi 代码(对象 Pascal)

// from the already created frame interface
var lPixelFormat: TGUID;
iFrame.GetPixelFormat(lPixelFormat);   
if WCIUnSuccessful(lImagingFactory.CreateComponentInfo(lPixelFormat,
  iComponentInfo)) then
  exit;
iPixelformatInfo := iComponentInfo as IWICPixelFormatInfo;
if WCIUnSuccessful(iPixelformatInfo.GetBitsPerPixel(result.BitsPerPixel))
then
  exit;
    
Run Code Online (Sandbox Code Playgroud)

所以调用可以使用 GUID(如果你的指针正确的话)