1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
#include "image.h"
#include "moon.h"
#include "pngwrite.h"
#include "stars.h"
#include "fail.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define VERSION "1.3"
static inline void usage( char *name )
{
char *base = name;
char *c;
for( c = name+1; *c; c++)
{
if( *(c-1) == '/' ) base = c;
}
printf( "this is pngphoon version " VERSION "\n\n"
"usage: %s options (* = mandatory)\n"
"\t-w # width*\n"
"\t-h # height*\n"
"\t-f $ filename*\n"
"\t-x # number of moons in x axis (for multihead configurations)\n"
"\t-y # number of moons in y axis (for multihead configurations)\n"
"\t-s # star density\n"
"\t-F $ foreground color\n"
"\t-B $ background color\n"
"\t-b black (no earthlight)\n"
"\t-S southern hemisphere (rotate image 180 degrees)\n"
"\t-q quiet (disable warnings)\n", base );
exit(0);
}
static png_color hex2col( const char *hexstr, const char *colname )
{
int convfail = 0;
png_color retval;
char *endstr;
unsigned long colval = strtoul( hexstr, &endstr, 16 );
if( *endstr )
{
convfail = 1;
}
else
{
switch( endstr-hexstr )
{
case 3:
retval.red = ((colval & 0xf00) >> 8) * 0x11;
retval.green = ((colval & 0x0f0) >> 4) * 0x11;
retval.blue = ((colval & 0x00f) ) * 0x11;
break;
case 6:
retval.red = (colval & 0xff0000) >> 16;
retval.green = (colval & 0x00ff00) >> 8;
retval.blue = (colval & 0x0000ff);
break;
default:
convfail = 1;
}
}
if( convfail )
{
fail( "%s color '%s' does not match 3 or 6 digit hex code\n",
colname, hexstr );
}
return retval;
}
int main( int argc, char *argv[] )
{
unsigned int width = 0;
unsigned int height = 0;
unsigned int drawheight = 0;
unsigned int xmoons = 1;
unsigned int ymoons = 1;
int stars = 50;
char *bgcolor = NULL;
char *fgcolor = NULL;
char *filename = NULL;
int black = 0;
int southern = 0;
image_t *image = NULL;
moon_t *moon = NULL;
int c;
unsigned int x;
unsigned int y;
png_color palette[2] = { { 0x00, 0x00, 0x00 }, { 0xff, 0xff, 0xff } };
opterr = 0;
if( argc == 1 )
{
usage( argv[0] );
}
while( (c = getopt( argc, argv, "w:h:x:y:s:F:B:f:bSq" )) != EOF )
{
switch (c)
{
case 'w':
width = strtoul( optarg, NULL, 0 );
break;
case 'h':
height = strtoul( optarg, NULL, 0 );
break;
case 'f':
filename = optarg;
break;
case 'x':
xmoons = strtoul( optarg, NULL, 0 );
break;
case 'y':
ymoons = strtoul( optarg, NULL, 0 );
break;
case 's':
stars = strtoul( optarg, NULL, 0 );
break;
case 'F':
fgcolor = optarg;
break;
case 'B':
bgcolor = optarg;
break;
case 'b':
black = 1;
break;
case 'S':
southern = 1;
break;
case 'q':
disablewarn();
break;
default:
fail( "unknown option: '%c'\n", optopt );
}
}
if( !width || !height || !filename )
{
fail( "you need to specify at least width, height and filename\n" );
}
moon = mooncreate();
if( width < moon->width )
{
warn( "image not wide enough for anything, just creating stars\n" );
xmoons = 0;
}
if( width < moon->width * xmoons )
{
warn( "decreasing number of moons in x axis from %d ", xmoons );
while( width < moon->width * --xmoons );
warn( "to %d moons.\n", xmoons );
}
drawheight = height;
if( height < moon->height )
{
warn( "image not high enough for moon, using truncate mode\n" );
drawheight = moon->height;
ymoons = 1;
}
else
{
if( height < moon->height * ymoons )
{
warn( "decreasing number of moons in y axis from %d ", ymoons );
/* this is not nice, but works: decreasing number of moons until they fit */
while( width < (moon->width * --ymoons) );
warn( "to %d moons.\n", ymoons );
}
}
image = imagecreate( width, drawheight );
if( (xmoons > 0) && (ymoons > 0) )
{
scarymonster( image, stars );
for( y = (drawheight/ymoons)/2; y < height; y += (drawheight/ymoons) )
{
for( x = (width/xmoons)/2; x < width; x += (width/xmoons) )
{
mooncopy( image, moon, x, y, black );
}
}
}
else
{
scarymonster( image, stars * 2 );
}
if( southern )
{
imageflip( image );
}
if( bgcolor )
{
palette[0] = hex2col( bgcolor, "background" );
}
if( fgcolor )
{
palette[1] = hex2col( fgcolor, "foreground" );
}
pngwrite( image, filename, height, (png_colorp) &palette[0] );
imagedestroy( image );
moondestroy( moon );
return 0;
}
|