// // riffpal.cpp // // # palファイルの読み書き // #include #include #include bool LoadRIFFPalette(LPCTSTR,LPCTSTR); void SaveRIFFPalette(PLOGPALETTE,LPCTSTR); int __stdcall WinMain(HINSTANCE h,HINSTANCE hPrev,LPSTR cmd,int nShow) { char desktop[MAX_PATH]; ::SHGetSpecialFolderPath(NULL,desktop,CSIDL_DESKTOP,FALSE); char palfile[MAX_PATH]; char outputfile[MAX_PATH]; ::strcpy(palfile,desktop); ::strcpy(outputfile,desktop); ::strcat(palfile,"\\newpal.pal"); ::strcat(outputfile,"\\result.txt"); // グレイスケールのパレットを作成し、 // デスクトップにnewpal.palとして保存 PLOGPALETTE newpal= (PLOGPALETTE)(new BYTE[4+sizeof(PALETTEENTRY)*256]); newpal->palVersion = 0x300; newpal->palNumEntries= 256; for ( int i=0 ; i<256 ; ++i ) { PALETTEENTRY &entry= newpal->palPalEntry[i]; entry.peRed=entry.peGreen=entry.peBlue=255-i; entry.peFlags= 0; } SaveRIFFPalette(newpal,palfile); delete [] newpal; // newpal.palを読み込む // 関数内で、エントリをresult.txtに列挙 bool ret= LoadRIFFPalette(palfile,outputfile); if ( !ret ) ::MessageBox(NULL,"Loading Failed!", "riffpal",MB_ICONSTOP); return 0; } bool LoadRIFFPalette(LPCTSTR in_PalFile,LPCTSTR in_ResultFile) { // バイナリモードで開くのがミソ ifstream file(in_PalFile,ios::binary|ios::in); DWORD id= 0; file.read((char*)&id,4); if ( id!='FFIR' ) return false; DWORD datsize= 0; file.read((char*)&datsize,4); if ( datsize==0 ) return false; PBYTE pCore= new BYTE[datsize]; if ( pCore==NULL ) return false; file.read(pCore,datsize); file.close(); PBYTE p= pCore; ::memcpy((char*)&id,p,4); if ( id!=' LAP' ) return false; else p+=4; ::memcpy((char*)&id,p,4); if ( id!='atad' ) return false; else p+=4; ::memcpy((char*)&datsize,p,4); if ( datsize==0 ) return false; else p+=4; PLOGPALETTE pPal= (PLOGPALETTE)(new BYTE[datsize]); if ( pPal==NULL ) return false; ::memcpy(pPal,p,datsize); delete [] pCore; int entries= (datsize-4)/sizeof(PALETTEENTRY); if ( pPal->palVersion ==0x300 && pPal->palNumEntries==entries ) { ofstream result(in_ResultFile); char buf[30]; for ( int i=0 ; ipalPalEntry[i].peRed, pPal->palPalEntry[i].peGreen, pPal->palPalEntry[i].peBlue); result << buf; } result.close(); delete [] pPal; return true; } delete [] pPal; return false; } void SaveRIFFPalette(PLOGPALETTE in_pPal,LPCTSTR in_NewPal) { // バイナリモードで開くのがミソ DWORD datsize= 4+in_pPal->palNumEntries* sizeof(PALETTEENTRY); ofstream file(in_NewPal,ios::out|ios::binary); file << "RIFF"; datsize+=12; file.write((char*)&datsize,4); file << "PAL data"; datsize-=12; file.write((char*)&datsize,4); file.write((char*)in_pPal,datsize); file.close(); }