[cpp] view plain copy
- /********************************************************************
- filename: CImgLoader.cpp
- created: 2011-01-05
- author: firehood
- purpose: 文件加载类,将外部图片资源转化成绘制纹理时所需的位图数据
- 图片格式支持bmp、png、jpg.
- *********************************************************************/
- #include "StdAfx.h"
- #include "ImgLoader.h"
- // IImage Includer
- #include <imaging.h>
- #include <initguid.h>
- #include <imgguids.h>
- // IImage lib
- #pragma comment(lib, "Imaging.lib")
- CImgLoader::CImgLoader(void)
- {
- m_pImage = NULL;
- m_Width = 0;
- m_Height = 0;
- }
- CImgLoader::~CImgLoader(void)
- {
- }
- // 加载图片资源
- BOOL CImgLoader::Load(LPCTSTR lpFileName)
- {
- IImagingFactory *pImgFactory = NULL;
- IImage *pImage = NULL;
- IBitmapImage *pBmpImg = NULL;
- //
- CoInitializeEx(NULL, COINIT_MULTITHREADED);
- if (!SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **) &pImgFactory)))
- return FALSE;
- if (!SUCCEEDED(pImgFactory->CreateImageFromFile(lpFileName, &pImage)))
- return FALSE;
- // 获取图像大小信息
- ImageInfo ImgInfo;
- pImage->GetImageInfo(&ImgInfo);
- m_Width = ImgInfo.Width;
- m_Height = ImgInfo.Height;
- if (FAILED(pImgFactory->CreateBitmapFromImage(
- pImage,m_Width,m_Height,PixelFormat24bppRGB,
- InterpolationHintDefault,&pBmpImg)))
- {
- return FALSE;
- }
- RECT rect = {0,0,m_Width,m_Height};
- BitmapData *BmpData = new BitmapData;
- if (FAILED(pBmpImg->LockBits(&rect,
- ImageLockModeRead|ImageLockModeWrite,
- PixelFormat24bppRGB,BmpData)))
- {
- return FALSE;
- }
- int line = BmpData->Stride;
- LPBYTE lpData, lpLine, lpCurPixel;
- lpData = lpLine = (LPBYTE)BmpData->Scan0; // 获取BMP位图实际值的地址指针
- // 若为Bottom-Up(从下到上)的位图,则指向buffer的结尾
- // 若为Top-Down(从上到下)的位图,则指向buffer的开头
- // int nBytesPP = nBPP >> 3; // 左移三位即除以8,获取图像每像素字节数
- m_pImage = new unsigned char[m_Width * m_Height * 3];
- memset(m_pImage, 0, m_Width * m_Height * 3);
- if(m_pImage == NULL)
- return FALSE;
- if (line>0)
- {
- int pos = m_Width * m_Height * 3-1;
- for(int i = 0; i<m_Height; i++) // 行
- {
- lpLine = lpData + i * line; // 获取图像每一行地址指针
- for(int j = m_Width-1; j>-1; j--) // 列
- {
- lpCurPixel = lpLine + j * 3; // 获取每行每像素地址指针
- m_pImage[pos--] = *lpCurPixel ; // R
- m_pImage[pos--] = *(lpCurPixel + 1); // G
- m_pImage[pos--] = *(lpCurPixel + 2); // B
- }
- }
- }
- else
- {
- int pos = 0 ;
- for(int i = m_Height-1; i>-1; i--) // 行
- {
- lpLine = lpData + i * line; // 获取图像每一行地址指针
- for(int j = 0; j<m_Width; j++) // 列
- {
- lpCurPixel = lpLine + j * 3; // 获取每行每像素地址指针
- m_pImage[pos++] = *(lpCurPixel + 2); // R
- m_pImage[pos++] = *(lpCurPixel + 1); // G
- m_pImage[pos++] = *lpCurPixel; // B
- }
- }
- }
- if (FAILED(pBmpImg->UnlockBits(BmpData)))
- {
- return FALSE;
- }
- delete BmpData;
- pBmpImg = NULL;
- pImage->Release();
- pImgFactory->Release();
- return TRUE;
- }
- // 获取图片数据
- unsigned char* CImgLoader::GetBmpImage(void)
- {
- return m_pImage;
- }
- // 获取图像宽度
- int CImgLoader::Width()
- {
- return m_Width;
- }
- // 获取图像高度
- int CImgLoader::Height()
- {
- return m_Height;
- }
- // 释放图片资源
- void CImgLoader::Free()
- {
- if(m_pImage)
- {
- delete []m_pImage;
- m_pImage = NULL;
- }
- }
时间: 2024-10-26 04:01:30