Canola  0.8.D001
test_button_shader/main.cc
Go to the documentation of this file.
00001 //
00002 // canola - canon canola 1614p emulator
00003 // Copyright (C) 2011 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, version 3, as
00007 // published by the Free Software Foundation.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License along
00015 // with this program. If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 #include <lib/ac/stdio.h>
00019 #include <lib/ac/stdlib.h>
00020 #include <lib/ac/unistd.h>
00021 #include <libexplain/output.h>
00022 #include <libexplain/program_name.h>
00023 #include <libexplain/strtol.h>
00024 
00025 #include <lib/image.h>
00026 #include <lib/print_version.h>
00027 
00028 
00029 static void
00030 usage(void)
00031 {
00032     const char *prog = explain_program_name_get();
00033     fprintf(stderr, "Usage: %s [ <option>... ]\n", prog);
00034     fprintf(stderr, "       %s -V\n", prog);
00035     exit(1);
00036 }
00037 
00038 
00039 static inline int
00040 min(int a, int b)
00041 {
00042     return (a < b ? a : b);
00043 }
00044 
00045 
00046 int
00047 main(int argc, char **argv)
00048 {
00049     int width = 64;
00050     int height = 64;
00051     const char *outfile = 0;
00052     for (;;)
00053     {
00054         int c = getopt(argc, argv, "h:o:Vw:");
00055         if (c == EOF)
00056             break;
00057         switch (c)
00058         {
00059         case 'h':
00060             height = explain_strtol_or_die(optarg, 0, 0);
00061             break;
00062 
00063         case 'V':
00064             print_version();
00065             return 0;
00066 
00067         case 'o':
00068             outfile = optarg;
00069             break;
00070 
00071         case 'w':
00072             width = explain_strtol_or_die(optarg, 0, 0);
00073             break;
00074 
00075         default:
00076             usage();
00077         }
00078     }
00079     if (width < 16 || height < 16)
00080     {
00081         explain_output_error_and_die
00082         (
00083             "geometry %dx%d not acceptable",
00084             width,
00085             height
00086         );
00087     }
00088     if (!outfile)
00089     {
00090         explain_output_error_and_die
00091         (
00092             "please specify an output file using the -o<filename> option"
00093         );
00094     }
00095     if (optind != argc)
00096         usage();
00097 
00098     icon_pixel top(1., 1., 1., 1.);
00099     icon_pixel left(1., 0., 0., 1.);
00100     icon_pixel bottom(0., 0., 0., 1.);
00101     icon_pixel right(0., 1., 0., 1.);
00102     icon_pixel plateau(0., 0., 1., 1.);
00103     int border = min(height / 4, width / 4);
00104     image::pointer img = image::create_blank_from_file(outfile, width, height);
00105     for (int y = 0; y < height; ++y)
00106     {
00107         for (int x = 0; x < width; ++x)
00108         {
00109             int x2 = width - x;
00110             int y2 = height - y;
00111             icon_pixel clr;
00112             if (x < border && y - x > 0)
00113                 clr = left;
00114             else if (x2 < border)
00115                 clr = right;
00116             else if (y < border && x2 - y > 0)
00117                 clr = top;
00118             else if (y2 < border)
00119                 clr = bottom;
00120             else
00121                 clr = plateau;
00122             img->set_pixel(x, y, clr);
00123         }
00124     }
00125     img->save_to_file(outfile);
00126 
00127     return 0;
00128 }