#include "icon.h" #include using namespace std; Icon::Icon() { for( int x = 0; x < 16; x++ ) { palette[x] = RGB(); for( int y = 0; y < 16; y++ ) { pixels[x][y] = 0; } } } int Icon::pixel( int x, int y ) { x = x < 0 || x >= 16 ? 0 : x; y = y < 0 || y >= 16 ? 0 : y; return pixels[x][y]; } RGB Icon::pixelRGB( int x, int y ) { x = x < 0 || x >= 16 ? 0 : x; y = y < 0 || y >= 16 ? 0 : y; return paletteColor( pixels[x][y] ); } void Icon::pixel( int x, int y, int colorIndex ) { x = x < 0 || x >= 16 ? 0 : x; y = y < 0 || y >= 16 ? 0 : y; pixels[x][y] = colorIndex; } RGB Icon::paletteColor( int index ) { index = index < 0 || index >= 16 ? 0 : index; return palette[index]; } void Icon::paletteColor( int index, RGB color ) { index = index < 0 || index >= 16 ? 0 : index; palette[index] = color; } bool Icon::exportXPM( char* fileName ) { FILE *xpm = fopen( fileName, "w" ); if( xpm < 0 ) { return false; } fprintf( xpm, "/* XPM */\n" ); fprintf( xpm, "static char *psx[] = {\n" ); fprintf( xpm, "/* columns rows colors chars-per-pixel */\n" ); fprintf( xpm, "\"16 16 16 1\",\n" ); // Palette Conversion char paletteXPM[17] = " .XoO+@#$%&*=-;:"; for( int i = 0; i < 16; i++ ) { RGB col = paletteColor( i ); fprintf( xpm, "\"%c c #%.2x%.2x%.2x\",\n", paletteXPM[i], col.r, col.g, col.b ); } fprintf( xpm, "/* pixels */\n" ); // Pixel Conversion for( int y = 0; y < 16; y++ ) { fprintf( xpm, "\"" ); for( int x = 0; x < 16; x++ ) { fprintf( xpm, "%c", paletteXPM[ pixel( x, y ) ] ); } if( y < 15 ) { fprintf( xpm, "\",\n" ); } else { fprintf( xpm, "\"\n}" ); } } fclose( xpm ); return true; }