port from perforce
This commit is contained in:
230
evoke-64k/ev09/pictool/pngopt.cpp
Normal file
230
evoke-64k/ev09/pictool/pngopt.cpp
Normal file
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
** Haaf's Game Engine 1.81
|
||||
** Copyright (C) 2003-2008, Relish Games
|
||||
** hge.relishgames.com
|
||||
**
|
||||
** PNG Images Optimizer
|
||||
*/
|
||||
|
||||
|
||||
#include "pngopt.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
HGE *hge = 0;
|
||||
|
||||
|
||||
struct color
|
||||
{
|
||||
unsigned char r;
|
||||
unsigned char g;
|
||||
unsigned char b;
|
||||
unsigned char a;
|
||||
};
|
||||
|
||||
|
||||
struct filelist
|
||||
{
|
||||
char filename[256];
|
||||
filelist* next;
|
||||
};
|
||||
|
||||
filelist *files=0;
|
||||
|
||||
|
||||
bool convert(char *filename);
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
HANDLE hSearch;
|
||||
WIN32_FIND_DATA SearchData;
|
||||
int nfiles=0;
|
||||
bool done=false;
|
||||
char *buf, filename[512];
|
||||
filelist *newFile, *nextFile;
|
||||
|
||||
printf("\nBlaPic\nby TGGC\n\n");
|
||||
|
||||
if(argc!=2)
|
||||
{
|
||||
printf("Usage: Bla.EXE <wildcard>\n\n");
|
||||
printf("makes datablocks...\n");
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
hSearch=FindFirstFile(argv[1], &SearchData);
|
||||
nextFile=0;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
if(hSearch==INVALID_HANDLE_VALUE || done)
|
||||
{
|
||||
FindClose(hSearch);
|
||||
break;
|
||||
}
|
||||
|
||||
if(!(SearchData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
|
||||
{
|
||||
strcpy_s(filename, argv[1]);
|
||||
buf=strrchr(filename, '\\');
|
||||
if(!buf) buf=filename; else buf++;
|
||||
strcpy_s(buf,256, SearchData.cFileName);
|
||||
newFile=new filelist;
|
||||
strcpy_s(newFile->filename,filename);
|
||||
newFile->next=0;
|
||||
if(nextFile) nextFile->next=newFile;
|
||||
else files=newFile;
|
||||
nextFile=newFile;
|
||||
}
|
||||
|
||||
done=!FindNextFile(hSearch, &SearchData);
|
||||
}
|
||||
|
||||
hge=hgeCreate(HGE_VERSION);
|
||||
hge->System_SetState(HGE_USESOUND, false);
|
||||
hge->System_SetState(HGE_WINDOWED, true);
|
||||
hge->System_SetState(HGE_SCREENWIDTH, 320);
|
||||
hge->System_SetState(HGE_SCREENHEIGHT, 200);
|
||||
hge->System_SetState(HGE_SHOWSPLASH, false);
|
||||
|
||||
if(!hge->System_Initiate())
|
||||
{
|
||||
hge->Release();
|
||||
printf("\nCan't initiate HGE.\n\n",nfiles);
|
||||
return 0;
|
||||
}
|
||||
|
||||
newFile=files;
|
||||
while(newFile)
|
||||
{
|
||||
if(convert(newFile->filename)) nfiles++;
|
||||
nextFile=newFile->next;
|
||||
delete newFile;
|
||||
newFile=nextFile;
|
||||
}
|
||||
|
||||
hge->System_Shutdown();
|
||||
hge->Release();
|
||||
printf("\n%d image(s) successfully optimized.\n\n",nfiles);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool convert(char *filename)
|
||||
{
|
||||
HTEXTURE tex;
|
||||
int width, height, pitch;
|
||||
color *buf;
|
||||
|
||||
printf("%s - ", filename);
|
||||
|
||||
tex = hge->Texture_Load(filename);
|
||||
if(!tex) { printf("Can't load texture.\n"); return false; }
|
||||
|
||||
width = hge->Texture_GetWidth(tex, true);
|
||||
height = hge->Texture_GetHeight(tex, true);
|
||||
pitch = hge->Texture_GetWidth(tex, false);
|
||||
|
||||
buf=(color *)hge->Texture_Lock(tex, false);
|
||||
if(!buf) { printf("Can't lock texture.\n"); return false; }
|
||||
|
||||
std::vector< bool > vecData;
|
||||
|
||||
for( int i=0; i<height; i++)
|
||||
{
|
||||
for( int j=0; j<width; j++)
|
||||
{
|
||||
vecData.push_back( buf[i*pitch+j].r + buf[i*pitch+j].g + buf[i*pitch+j].b < 3 * 127 );
|
||||
}
|
||||
}
|
||||
|
||||
std::vector< int > vecPixelY;
|
||||
std::vector< int > vecPixelX;
|
||||
vecPixelY.resize( height );
|
||||
vecPixelX.resize( width );
|
||||
|
||||
for( int i=0; i<height; i++)
|
||||
{
|
||||
for( int j=0; j<width; j++)
|
||||
{
|
||||
if( vecData[ i*width+j ] )
|
||||
{
|
||||
vecPixelY[ i ]++;
|
||||
vecPixelX[ j ]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int iStartX= 0;
|
||||
int iLastX= width - 1;
|
||||
int iStartY= 0;
|
||||
int iLastY= height - 1;
|
||||
|
||||
while( vecPixelX[ iStartX ] == 0 && iStartX <= iLastX )
|
||||
{
|
||||
iStartX++;
|
||||
}
|
||||
while( vecPixelX[ iLastX ] == 0 && iStartX <= iLastX )
|
||||
{
|
||||
iLastX--;
|
||||
}
|
||||
while( vecPixelY[ iStartY ] == 0 && iStartY <= iLastY )
|
||||
{
|
||||
iStartY++;
|
||||
}
|
||||
while( vecPixelY[ iLastY ] == 0 && iStartY <= iLastY )
|
||||
{
|
||||
iLastY--;
|
||||
}
|
||||
|
||||
std::string str( filename );
|
||||
str= str.substr( 0, str.length() - 4 );
|
||||
std::string strFile= "..\\";
|
||||
strFile+= str;
|
||||
strFile+= ".h";
|
||||
|
||||
int iOutWidth= 1 + iLastX - iStartX;
|
||||
int iOutHeight= 1 + iLastY - iStartY;
|
||||
|
||||
if( iOutWidth > 255 || iOutHeight > 255 )
|
||||
{
|
||||
printf("too large.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ofstream ofs( strFile.c_str() );
|
||||
ofs << "const unsigned char g_" << str.c_str() << "[]=\n";
|
||||
ofs << "{\n";
|
||||
ofs << "\t" << iOutWidth << "," << iOutHeight << ",\n";
|
||||
int iBitted= 0;
|
||||
int iCount= 0;
|
||||
for( int i= iStartY; i <= iLastY; i++)
|
||||
{
|
||||
for( int j= iStartX; j<= iLastX; j++)
|
||||
{
|
||||
iBitted+= vecData[ i*width+j ] ? 1 : 0;
|
||||
if( iCount % 8 == 7 || iCount + 1 == iOutWidth * iOutHeight )
|
||||
{
|
||||
while( iCount % 8 < 7 )
|
||||
{
|
||||
iBitted*= 2;
|
||||
iCount++;
|
||||
}
|
||||
ofs << "\t" << iBitted << ",\n";
|
||||
iBitted= 0;
|
||||
}
|
||||
iBitted*= 2;
|
||||
iCount++;
|
||||
}
|
||||
}
|
||||
|
||||
ofs << "};\n";
|
||||
|
||||
hge->Texture_Unlock(tex);
|
||||
|
||||
printf("Ok\n");
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user