win32加载图片获得像素值

  在写光栅渲染器时,需要加载图片获得像素以便进行纹理插值,试了几种方法发现下面这种比价简单,效率也可以接受

  Texture2D是我自己定义的类,其中m_pixelBuffer是一个动态二维数组,每个元素为ZCFLOAT3(自定义类型用来保存颜色rgb值)。

 1 #include "LoadBitmap.h"
 2 #include <windows.h>
 3 #include <gdiplus.h>
 4 
 5 #include <iostream>
 6 #include <fstream>
 7 #include <sstream>
 8 
 9 #pragma comment(lib, "gdiplus.lib")
10 using namespace std;
11 using namespace Gdiplus;
12 
13 Texture2D MathUtil::LoadBitmapToColorArray(wstring filePath)
14 {
15     GdiplusStartupInput gdiplusstartupinput;
16     ULONG_PTR gdiplustoken;
17     GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, nullptr);
18 
19     Bitmap* bmp = new Bitmap(filePath.c_str());
20     if (!bmp)
21     {
22         MessageBox(nullptr, "error", "picture path is null!", MB_OK);
23         delete bmp;
24         GdiplusShutdown(gdiplustoken);
25         return Texture2D(0,0);
26     }
27     else
28     {
29         UINT height = bmp->GetHeight();
30         UINT width = bmp->GetWidth();
31         //Texture2D
32         Texture2D texture(width, height);
33 
34         Color color;
35 
36         for (int y = 0; y < height; y++)
37             for (int x = 0; x < width; x++)
38             {
39                 bmp->GetPixel(x, y, &color);
40 
41                 texture.m_pixelBuffer[x][y] = ZCFLOAT3(
42                     color.GetRed() / 255.f,
43                     color.GetGreen() / 255.f,
44                     color.GetBlue() / 255.f
45                 );
46             }
47 
48         delete bmp;
49         GdiplusShutdown(gdiplustoken);
50         return texture;
51     }
52     
53 }
原文地址:https://www.cnblogs.com/zhangbaochong/p/5745197.html