HRESULT LoadPSD( LPSTR strFilename ) // 讀取PSD文件
{
DWORD dwWidth, dwHeight; // 寬高
long lSurfWidth = m_Rect.right - m_Rect.left;
long lSurfHeight = m_Rect.bottom - m_Rect.top;
WORD CompressionType; // 壓縮類型
HDC hDC;
FILE *fpPSD;
WORD ChannelCount; // 通道數(shù)
// 打開PSD文件
if ( ( fpPSD = fopen ( strFilename, "rb" ) ) == NULL ) {
return E_FAIL;
}
// 頭四個字節(jié)為"8BPS"
char signature[5];
signature[0] = fgetc( fpPSD );
signature[1] = fgetc( fpPSD );
signature[2] = fgetc( fpPSD );
signature[3] = fgetc( fpPSD );
signature[4] = '\0';
if ( strcmp( signature,"8BPS" ) != 0 ) {
return E_FAIL;
}
// 版本必須為1
if ( Read16( fpPSD ) != 1 ) {
return E_FAIL;
}
// 跳過一些數(shù)據(jù) (總是0)
Read32( fpPSD );
Read16( fpPSD );
// 讀取通道數(shù)
ChannelCount = Read16( fpPSD );
// 確定至少有一個通道
if ( ( ChannelCount < 0 ) || ( ChannelCount > MAX_PSD_CHANNELS ) ) {
return E_FAIL;
}
// 讀入寬和高
dwHeight = Read32( fpPSD );
dwWidth = Read32( fpPSD );
if ( dwWidth != ( DWORD )lSurfWidth || dwHeight != ( DWORD )lSurfHeight ) {
return E_FAIL;
}
// 只讀入8位通道
if ( Read16( fpPSD ) != 8 ) {
return E_FAIL;
}
// 確定模式為RGB.
// 可能值:
// 0: 位圖
// 1: 灰階
// 2: 索引
// 3: RGB
// 4: CMYK
// 7: Multichannel
// 8: Duotone
// 9: Lab
if ( Read16( fpPSD ) != 3 ) {
return E_FAIL;
}
// 跳過數(shù)據(jù)(如調(diào)色板)
int ModeDataCount = Read32( fpPSD );
if ( ModeDataCount )
fseek( fpPSD, ModeDataCount, SEEK_CUR );
// 跳過數(shù)據(jù)(如:pen tool paths, etc)
int ResourceDataCount = Read32( fpPSD );
if ( ResourceDataCount )
fseek( fpPSD, ResourceDataCount, SEEK_CUR );
// 條過保留數(shù)據(jù)
int ReservedDataCount = Read32( fpPSD );
if ( ReservedDataCount )
fseek( fpPSD, ReservedDataCount, SEEK_CUR );
// 0: 非壓縮
// 1: RLE壓縮
CompressionType = Read16( fpPSD );
if ( CompressionType > 1 ) {
return E_FAIL;
}
BYTE* PSDPixels = new BYTE[ ( lSurfWidth * lSurfHeight ) * 4 ];
// 解包數(shù)據(jù)
UnPackPSD( fpPSD, lSurfWidth, lSurfHeight, PSDPixels, ChannelCount, CompressionType );
fclose( fpPSD );
// 復制信息
BITMAPINFO BitmapInfo;
ZeroMemory( &BitmapInfo, sizeof( BitmapInfo ) );
BitmapInfo.bmiHeader.biSize = sizeof( BitmapInfo.bmiHeader );
BitmapInfo.bmiHeader.biWidth = lSurfWidth;
BitmapInfo.bmiHeader.biHeight = -lSurfHeight;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32;
m_lpDDS7->GetDC( &hDC );
int rc = StretchDIBits( hDC,
0,
0,
lSurfWidth,
lSurfHeight,
0,
0,
lSurfWidth,
lSurfHeight,
PSDPixels,
&BitmapInfo,
DIB_RGB_COLORS,
SRCCOPY );
m_lpDDS7->ReleaseDC( hDC );
if ( rc == GDI_ERROR ) {
H_ARRAY_DELETE( PSDPixels );
#ifdef _DEBUG
g_pHERR->OutDebugMsg( 3, H2DSERR_INVALID_PSD );
#endif
return E_FAIL;
}
// 是否讀取Alpha混合通道
if( ChannelCount > 3 ) {
m_pbAlphaMask = new BYTE[ lSurfWidth * lSurfHeight ];
for ( int x = 0; x < lSurfWidth; x++ )
for ( int y = 0; y < lSurfHeight; y++ ) {
m_pbAlphaMask[ ( y * lSurfWidth ) + x ] =
PSDPixels[ ( ( ( y * lSurfHeight ) + x ) * 4 ) + 3 ];
}
}
else {
m_pbAlphaMask = NULL;
}
H_ARRAY_DELETE( PSDPixels );
return DD_OK;
}
// PSD文件解包
void CHades2DSurface::UnPackPSD( FILE *fp, // fp為PSD文件指針,
DWORD dwWidth, // dwWidth、dwHeight為寬高,
DWORD dwHeight,
BYTE* pixels, // pixels為解包目標指針,
WORD ChannelCnt, // ChannelCnt為通道數(shù),
WORD Compression ) // Compression位壓縮類型。
{
int Default[4] = { 0, 0, 0, 255 };
int chn[4] = { 2, 1, 0, 3};
int PixelCount = dwWidth * dwHeight;
if ( Compression ) {
fseek( fp, dwHeight * ChannelCnt * 2, SEEK_CUR );
for ( int c = 0; c < 4; c++ ) {
int pn = 0;
int channel = chn[c];
if ( channel >= ChannelCnt ) {
for ( pn=0; pn < PixelCount ;pn++ ) {
pixels[ ( pn * 4 ) + channel ] = Default[ channel ];
}
}
else // 非壓縮
{
int count = 0;
while( count < PixelCount ) {
int len = fgetc( fp );
if( len == 128 ) { }
else if ( len < 128 ) // 非RLE
{
len++;
count += len;
while(len) {
pixels[ ( pn * 4 ) + channel ] = fgetc( fp );
pn++;
len--;
}
}
else if ( len > 128 ) // RLE打包
{
len ^= 0x0FF;
len += 2;
unsigned char val = fgetc( fp );
count += len;
while( len ) {
pixels[ ( pn * 4 ) + channel ] = val;
pn++;
len--;
}
}
}
}
}
}
else
{
for ( int c=0; c < 4; c++ ) {
int channel = chn[c];
if ( channel > ChannelCnt ) {
for( int pn = 0; pn < PixelCount; pn++ ) {
pixels[ ( pn * 4 ) + channel ] = Default[ channel ];
}
}
else {
for( int n = 0; n < PixelCount; n++ ) {
pixels[ ( n * 4 ) + channel ] = fgetc( fp );
}
}
}
}
}