Canola  0.8.D001
lib/program_card/functor/image/png.cc
Go to the documentation of this file.
00001 //
00002 // canola - canon canola 1614p emulator
00003 // Copyright (C) 2011, 2012 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/math.h>
00019 #include <cairomm/context.h>
00020 #include <cairomm/surface.h>
00021 
00022 #include <lib/program_card/functor/image/png.h>
00023 #include <lib/std_string_printf.h>
00024 
00025 
00026 program_card_functor_image_png::~program_card_functor_image_png()
00027 {
00028 }
00029 
00030 
00031 program_card_functor_image_png::program_card_functor_image_png(
00032     const std::string &fn
00033 ) :
00034     filename(fn)
00035 {
00036 }
00037 
00038 
00039 program_card_functor_image::pointer
00040 program_card_functor_image_png::create(const std::string &fn)
00041 {
00042     return pointer(new program_card_functor_image_png(fn));
00043 }
00044 
00045 
00046 void
00047 program_card_functor_image_png::operator()(unsigned card_num,
00048     unsigned card_count, const unsigned char *data, size_t data_size)
00049 {
00050     set_position(card_num, card_count);
00051     double dots_per_inch = 300;
00052 
00053     //
00054     // Create an image surface to draw on.
00055     //
00056     int w = floor(0.5 + (1360./600.) * dots_per_inch);
00057     int h = floor(0.5 + (4425./600.) * dots_per_inch);
00058     Cairo::RefPtr<Cairo::Surface> img =
00059         Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, w, h);
00060     Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(img);
00061 
00062     // The card drawing functions assume units are in points, just like
00063     // PostScript uses.
00064     double s = dots_per_inch / 72.;
00065     cr->scale(s, s);
00066 
00067     //
00068     // Draw the card image.
00069     //
00070     draw(cr, data, data_size);
00071 
00072     //
00073     // Write the image to the file.
00074     //
00075     std::string fn = filename;
00076     if (card_count > 1)
00077     {
00078         if (fn.size() > 4 && fn.substr(fn.size() - 4, 4) == ".png")
00079             fn = fn.substr(0, fn.size() - 4);
00080         fn += std_string_printf("-%u-of-%u.png", card_num, card_count);
00081     }
00082     img->write_to_png(fn);
00083 }