本文主要是介绍WIC 图像处理初体验——读取像素的值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先放上运行结果:
可以发现红绿蓝是从后往前的。
必须以C++方式编译代码!
// 参考资料:https://learn.microsoft.com/zh-cn/windows/win32/wic/-wic-lh
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <wincodec.h>int main(void)
{CoInitialize(nullptr);IWICImagingFactory* fac;CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fac));IWICBitmapDecoder* dec;fac->CreateDecoderFromFilename(L"test.bmp", nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &dec);IWICBitmapFrameDecode* fram;dec->GetFrame(0, &fram);IWICBitmap* bmp;fac->CreateBitmapFromSource(fram, WICBitmapCacheOnDemand, &bmp);UINT w, h;bmp->GetSize(&w, &h);printf("宽 %u 高 %u\n", w, h);IWICBitmapLock* lck;WICRect rc = { .X = 0, .Y = 0, .Width = (INT)w, .Height = (INT)h };bmp->Lock(nullptr, WICBitmapLockRead, &lck);UINT stride;lck->GetStride(&stride);printf("步长 %u\n", stride);UINT bufsiz;BYTE* v;lck->GetDataPointer(&bufsiz, &v);puts("每个像素的 RGB 值:");for (UINT i = 0; i < h; i++) {printf("第 %u 行\n", i);BYTE* row = v + (UINT64)i * stride;for (UINT j = 0; j < w; j++) {BYTE* p = row + 3 * j;printf("%-4hhu%-4hhu%-4hhu\n", p[0], p[1], p[2]);}putchar('\n');}lck->Release();bmp->Release();fram->Release();dec->Release();fac->Release();CoUninitialize();return 0;
}
这篇关于WIC 图像处理初体验——读取像素的值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!