* Use stack allocated objects instead of heap allocated; since they're safer (don't require explicit deallocation), and it's obvious that the only reason why they where heap allocated is probably that whoever wrote that piece of code was addicted to the "operator new"
* Use std::ostream instead of FILE* for PCXHandler::WritePCX * Change utility functions EncodeLine and EncodedPut to work with std::ostream instead of FILE* now git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@2197 4a71c877-e1ca-e34f-864e-861f7616d084master
parent
2fcc6e82c2
commit
595c4838ac
|
@ -846,11 +846,18 @@ BOOL CHeightMap::WriteHeightMap(char *FileName)
|
|||
Palette[i].peFlags = 0;
|
||||
}
|
||||
|
||||
PCXHandler *TestPCX = new PCXHandler();
|
||||
if(TestPCX->Create(m_MapWidth,m_MapHeight,Bits,Palette)) {
|
||||
TestPCX->WritePCX(FileName);
|
||||
PCXHandler TestPCX;
|
||||
if(TestPCX.Create(m_MapWidth,m_MapHeight,Bits,Palette))
|
||||
{
|
||||
std::ofstream output(FileName, std::ios_base::binary);
|
||||
if(!output.is_open())
|
||||
{
|
||||
MessageBox(NULL, FileName, "Unable to create file.", MB_OK);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
TestPCX.WritePCX(output);
|
||||
}
|
||||
delete TestPCX;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
@ -1059,11 +1066,18 @@ BOOL CHeightMap::WriteTileIDMap(char *FullPath)
|
|||
Palette[i].peFlags = 0;
|
||||
}
|
||||
|
||||
PCXHandler *TestPCX = new PCXHandler();
|
||||
if(TestPCX->Create(m_MapWidth,m_MapHeight,Bits,Palette)) {
|
||||
TestPCX->WritePCX(FullPath);
|
||||
PCXHandler TestPCX;
|
||||
if(TestPCX.Create(m_MapWidth,m_MapHeight,Bits,Palette))
|
||||
{
|
||||
std::ofstream output(FullPath, std::ios_base::binary);
|
||||
if(!output.is_open())
|
||||
{
|
||||
MessageBox(NULL, FullPath, "Unable to create file.", MB_OK);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
TestPCX.WritePCX(output);
|
||||
}
|
||||
delete TestPCX;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
|
|
@ -35,8 +35,8 @@
|
|||
|
||||
// Forward declarations of internal utility functions
|
||||
static inline WORD EncodedGet(WORD *pbyt, WORD *pcnt, std::istream& input);
|
||||
static inline WORD EncodeLine(const char* inBuff, WORD inLen, FILE *fid);
|
||||
static inline WORD EncodedPut(UBYTE byt, UBYTE cnt, FILE *fid);
|
||||
static inline WORD EncodeLine(const char* inBuff, WORD inLen, std::ostream& output);
|
||||
static inline WORD EncodedPut(UBYTE byt, UBYTE cnt, std::ostream& output);
|
||||
|
||||
// Round the given value up to the nearest power of 2.
|
||||
int Power2(int value)
|
||||
|
@ -204,7 +204,6 @@ bool PCXHandler::ReadPCX(std::istream& input, DWORD Flags)
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool PCXHandler::Create(int Width,int Height,void *Bits, PALETTEENTRY *Palette)
|
||||
{
|
||||
_Header.Manufacturer = 10;
|
||||
|
@ -282,43 +281,33 @@ bool PCXHandler::Create(int Width,int Height,void *Bits, PALETTEENTRY *Palette)
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool PCXHandler::WritePCX(char *FilePath)
|
||||
bool PCXHandler::WritePCX(std::ostream& output)
|
||||
{
|
||||
FILE *fid = fopen(FilePath,"wb");
|
||||
if(!fid)
|
||||
{
|
||||
MessageBox( NULL, FilePath, "Unable to create file.", MB_OK );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write the PCX header.
|
||||
fwrite(&_Header, sizeof(_Header), 1, fid);
|
||||
output.write(reinterpret_cast<const char*>(&_Header), sizeof(_Header));
|
||||
|
||||
// Encode and write the body.
|
||||
char* Ptr = reinterpret_cast<char*>(_DIBBits);
|
||||
for(unsigned int i = 0; i < GetBitmapHeight(); ++i)
|
||||
{
|
||||
EncodeLine(Ptr, (WORD)GetBitmapWidth(), fid);
|
||||
EncodeLine(Ptr, (WORD)GetBitmapWidth(), output);
|
||||
Ptr += GetBitmapWidth();
|
||||
}
|
||||
|
||||
// Write the palette.
|
||||
putc(12, fid);
|
||||
unsigned int PaletteSize = 1 << _Header.BitsPerPixel;
|
||||
// Magic number for palette identification
|
||||
output.put(12);
|
||||
const unsigned int PaletteSize = 1 << _Header.BitsPerPixel;
|
||||
for (i = 0; i < PaletteSize; ++i)
|
||||
{
|
||||
putc(_Palette[i].peRed, fid);
|
||||
putc(_Palette[i].peGreen, fid);
|
||||
putc(_Palette[i].peBlue, fid);
|
||||
output.put(_Palette[i].peRed);
|
||||
output.put(_Palette[i].peGreen);
|
||||
output.put(_Palette[i].peBlue);
|
||||
}
|
||||
|
||||
fclose(fid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/* This procedure reads one encoded block from the image file and
|
||||
stores a count and data byte. Result:
|
||||
0 = valid data stored
|
||||
|
@ -354,7 +343,7 @@ unsigned char *inBuff; pointer to scanline data
|
|||
int inLen; length of raw scanline in bytes
|
||||
FILE *fp; file to be written to
|
||||
*/
|
||||
static inline WORD EncodeLine(const char* inBuff, WORD inLen, FILE *fid)
|
||||
static inline WORD EncodeLine(const char* inBuff, WORD inLen, std::ostream& output)
|
||||
{ /* returns number of bytes written into outBuff, 0 if failed */
|
||||
|
||||
WORD i;
|
||||
|
@ -373,7 +362,7 @@ static inline WORD EncodeLine(const char* inBuff, WORD inLen, FILE *fid)
|
|||
++runCount; /* it encodes */
|
||||
if (runCount == 63)
|
||||
{
|
||||
if (!(i = EncodedPut(last, runCount, fid)))
|
||||
if (!(i = EncodedPut(last, runCount, output)))
|
||||
return 0;
|
||||
|
||||
total += i;
|
||||
|
@ -384,7 +373,7 @@ static inline WORD EncodeLine(const char* inBuff, WORD inLen, FILE *fid)
|
|||
{ /* thisone != last */
|
||||
if (runCount)
|
||||
{
|
||||
if (!(i = EncodedPut(last, runCount, fid)))
|
||||
if (!(i = EncodedPut(last, runCount, output)))
|
||||
return 0;
|
||||
|
||||
total += i;
|
||||
|
@ -397,7 +386,7 @@ static inline WORD EncodeLine(const char* inBuff, WORD inLen, FILE *fid)
|
|||
|
||||
if (runCount)
|
||||
{ /* finish up */
|
||||
if (!(i = EncodedPut(last, runCount, fid)))
|
||||
if (!(i = EncodedPut(last, runCount, output)))
|
||||
return 0;
|
||||
|
||||
return total + i;
|
||||
|
@ -406,31 +395,36 @@ static inline WORD EncodeLine(const char* inBuff, WORD inLen, FILE *fid)
|
|||
return total;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* subroutine for writing an encoded byte pair
|
||||
(or single byte if it doesn't encode) to a file
|
||||
unsigned char byt, cnt;
|
||||
FILE *fid;
|
||||
*/
|
||||
static inline WORD EncodedPut(UBYTE byt, UBYTE cnt, FILE *fid) /* returns count of bytes written, 0 if err */
|
||||
static inline WORD EncodedPut(UBYTE byt, UBYTE cnt, std::ostream& output) /* returns count of bytes written, 0 if err */
|
||||
{
|
||||
if(cnt)
|
||||
{
|
||||
if((cnt==1) && (0xc0 != (0xc0&byt)))
|
||||
if (cnt == 1
|
||||
&& 0xc0 != (0xc0 & byt))
|
||||
{
|
||||
if(EOF == putc((int)byt, fid))
|
||||
return 0; /* disk write error (probably full) */
|
||||
output.put(byt);
|
||||
if (output.bad()
|
||||
|| output.fail())
|
||||
return 0; /* write error (disk is probably full) */
|
||||
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(EOF == putc((int)0xC0 | cnt, fid))
|
||||
return 0; /* disk write error */
|
||||
output.put(0xC0 | cnt);
|
||||
if (output.bad()
|
||||
|| output.fail())
|
||||
return 0; /* write error (disk is probably full) */
|
||||
|
||||
if(EOF == putc((int)byt, fid))
|
||||
return 0; /* disk write error */
|
||||
output.put(byt);
|
||||
if (output.bad()
|
||||
|| output.fail())
|
||||
return 0; /* write error (disk is probably full) */
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ class PCXHandler {
|
|||
|
||||
bool Create(int Width,int Height,void *Bits,PALETTEENTRY *Palette);
|
||||
bool ReadPCX(std::istream& input, DWORD Flags = 0);
|
||||
bool WritePCX(char *FilePath);
|
||||
bool WritePCX(std::ostream& output);
|
||||
|
||||
inline unsigned int GetBitmapWidth()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue