Canola  0.8.D001
lib/image/create_from_file.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/string.h>
00019 #include <libexplain/output.h>
00020 
00021 #include <lib/image/format/jpeg.h>
00022 #include <lib/image/format/png.h>
00023 #include <lib/sizeof.h>
00024 
00025 
00026 struct table_t
00027 {
00028     const char *name;
00029     image::pointer (*create)(const std::string &filename);
00030     image::pointer (*create_blank)(int width, int height);
00031 };
00032 
00033 
00034 static table_t table[] =
00035 {
00036     { "jpg", &image_format_jpeg::create, &image_format_jpeg::create_blank },
00037     { "jpeg", &image_format_jpeg::create, &image_format_jpeg::create_blank },
00038     { "png", &image_format_png::create, &image_format_png::create_blank },
00039 };
00040 
00041 
00042 static std::string
00043 extension(const std::string &path)
00044 {
00045     const char *s = path.c_str();
00046     const char *slash = strrchr(s, '/');
00047     const char *begin = (slash ? slash + 1 : s);
00048     const char *dot = strrchr(begin, '.');
00049     return (dot ? dot + 1 : "");
00050 }
00051 
00052 
00053 image::pointer
00054 image::create_from_file(const std::string &filename)
00055 {
00056     std::string ext = extension(filename);
00057 
00058     for (const table_t *tp = table; tp < ENDOF(table); ++tp)
00059     {
00060         if (std::string(tp->name) == ext)
00061             return tp->create(filename);
00062     }
00063 
00064     explain_output_error_and_die
00065     (
00066         "file extension \"%s\" is not in a recognised image format, try PNG",
00067         ext.c_str()
00068     );
00069     return image_format_png::create(filename);
00070 }
00071 
00072 
00073 image::pointer
00074 image::create_blank_from_file(const std::string &filename, int width,
00075     int height)
00076 {
00077     std::string ext = extension(filename);
00078 
00079     for (const table_t *tp = table; tp < ENDOF(table); ++tp)
00080     {
00081         if (std::string(tp->name) == ext)
00082             return tp->create_blank(width, height);
00083     }
00084 
00085     explain_output_error_and_die
00086     (
00087         "file extension \"%s\" is not in a recognised image format, try PNG",
00088         ext.c_str()
00089     );
00090     return image_format_png::create_blank(width, height);
00091 }
00092 
00093 
00094 bool
00095 image::known_file_extension(const std::string &ext)
00096 {
00097     for (const table_t *tp = table; tp < ENDOF(table); ++tp)
00098     {
00099         if (std::string(tp->name) == ext)
00100             return true;
00101     }
00102     return false;
00103 }