/* Generate PNG of a particular size and color. ./make_png width height > white.png ./make_png width height r g b > opaque.png ./make_png width height r g b a > full_color.png */ #include #include #include #include #include #ifdef _WIN32 #include #include #endif int main(int argc, char **argv) { int width = -1, height = -1, r = 255, g = 255, b = 255, a = 255, i; png_image image; png_bytep pixels; /* Parse and validate command line settings. */ if( argc >= 3 ) { width = atoi(argv[1]); height = atoi(argv[2]); if( argc >= 6 ) { r = atoi(argv[3]); g = atoi(argv[4]); b = atoi(argv[5]); if( argc >= 7 ) { a = atoi(argv[6]); if( argc > 7 ) width = -1; /* Too many arguments. */ } } } if( width <= 0 || height <= 0 || r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || a < 0 || a > 255 ) { printf("%s [ []] > output.png\n", *argv); return 1; } if( isatty(STDOUT_FILENO) ) { fputs("Output should be redirected to a PNG file.\n", stderr); return 1; } #ifdef _WIN32 setmode(STDOUT_FILENO, O_BINARY); #endif /* Generate output. */ memset(&image, 0, sizeof(image)); image.version = PNG_IMAGE_VERSION; image.format = PNG_FORMAT_RGBA; image.width = width; image.height = height; pixels = (png_bytep)malloc(width * height * 4); if( pixels == NULL ) { fputs("Out of memory.\n", stderr); return 1; } for(i = 0; i < width * height; i++) { pixels[i * 4] = r; pixels[i * 4 + 1] = g; pixels[i * 4 + 2] = b; pixels[i * 4 + 3] = a; } /* Write output. */ if( !png_image_write_to_stdio(&image, stdout, 0, pixels, 0, NULL) ) { fputs("Error writing output.\n", stderr); return 1; } free(pixels); return 0; }