您好,登錄后才能下訂單哦!
[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;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。