最近想用C++在windows下实现一个基本的图像查看器功能,目前只想到了使用GDI或OpenGL两种方式。由于实在不想用GDI的API了,就用OpenGL的方式实现了一下基本的显示功能。
用GDAL读取图像,这样就能与图像格式无关。OpenGL的glDrawPixels()函数也能实现图像显示,但是现在高版本的OpenGL都采用glTexImage2D()贴纹理的方式了,也不用考虑图像大小是否是2的N次方,或者4字节对齐的问题。具体实现如下:
// ImageShow.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "ImageShow.h"
#include <iostream>
#include <gl\glew.h> // 包含最新的gl.h,glu.h库
#include <gl\freeglut.h> // 包含OpenGL实用库
#include <gdal_priv.h>
using namespace std;
unsigned int texture; // 纹理对象
unsigned char* imgBuf = nullptr;
int imgWidth;
int imgHeight;
void ReadImage()
{
GDALAllRegister();
GDALDataset* img = (GDALDataset *)GDALOpen("lena.bmp", GA_ReadOnly);
//GDALDataset* img = (GDALDataset *)GDALOpen("dst.tif", GA_ReadOnly);
if (img