initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / image-helpers.cpp
1 //  Copyright (C) 2007 Ole Laursen
2 //  Copyright (C) 2009 Ben Asselstine
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU Library General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
17 //  02110-1301, USA.
18
19 #include "image-helpers.h"
20
21 Glib::RefPtr<Gdk::Pixmap> to_pixmap(Glib::RefPtr<Gdk::Pixbuf> pixbuf)
22 {
23   Glib::RefPtr<Gdk::Pixmap> p = Gdk::Pixmap::create(Glib::RefPtr<Gdk::Drawable>(0), pixbuf->get_width(), pixbuf->get_height(), 24);
24   pixbuf->render_to_drawable_alpha(p, 0, 0, 0, 0, pixbuf->get_width(),
25                                  pixbuf->get_height(), Gdk::PIXBUF_ALPHA_BILEVEL,
26                                  123, Gdk::RGB_DITHER_NONE, 0, 0);
27   //p = Gdk::Pixmap::create(Glib::RefPtr<Gdk::Drawable>(0), 
28                           //pixbuf->get_width(), pixbuf->get_height(), 24);
29   //p->draw_pixbuf(pixbuf, 0, 0, 0, 0, pixbuf->get_width(), pixbuf->get_height(),
30                  //Gdk::RGB_DITHER_NORMAL, 0, 0);
31   return p;
32 }
33
34 std::vector<PixMask*>
35 disassemble_row(const std::string &file, int no)
36 {
37     Glib::RefPtr<Gdk::Pixbuf> row = Gdk::Pixbuf::create_from_file(file);
38
39     std::vector<Glib::RefPtr<Gdk::Pixbuf> > images;
40     images.reserve(no);
41   
42     int h = row->get_height();
43     int w = row->get_width() / no;
44
45     // disassemble row
46     for (int x = 0; x < no; ++x) {
47         Glib::RefPtr<Gdk::Pixbuf> buf
48             = Gdk::Pixbuf::create(row->get_colorspace(),
49                                   row->get_has_alpha(),
50                                   row->get_bits_per_sample(),
51                                   w, h);
52
53         row->copy_area(x * w, 0, w, h, buf, 0, 0);
54     
55         images.push_back(buf);
56     }
57     
58     std::vector<PixMask*> pixmasks;
59     for (unsigned int i = 0; i < images.size(); i++)
60       pixmasks.push_back(PixMask::create(images[i]));
61
62     return pixmasks;
63 }
64 std::vector<PixMask*>
65 disassemble_row(const std::string &file, int no, bool first_half_height)
66 {
67     Glib::RefPtr<Gdk::Pixbuf> row = Gdk::Pixbuf::create_from_file(file);
68
69     std::vector<Glib::RefPtr<Gdk::Pixbuf> > images;
70     images.reserve(no);
71   
72     int h = row->get_height() / 2;
73     int w = row->get_width() / no;
74
75     int s = 0;
76     if (first_half_height == false)
77       s = h;
78     // disassemble row
79     for (int x = 0; x < no; ++x) {
80         Glib::RefPtr<Gdk::Pixbuf> buf
81             = Gdk::Pixbuf::create(row->get_colorspace(),
82                                   row->get_has_alpha(),
83                                   row->get_bits_per_sample(),
84                                   w, h);
85
86         row->copy_area(x * w, s, w, h, buf, 0, 0);
87     
88         images.push_back(buf);
89     }
90     
91     std::vector<PixMask*> pixmasks;
92     for (unsigned int i = 0; i < images.size(); i++)
93       pixmasks.push_back(PixMask::create(images[i]));
94     return pixmasks;
95 }
96 int get_pwidth(Glib::RefPtr<Gdk::Pixmap> pixmap)
97 {
98   int width = 0, height = 0;
99   pixmap->get_size(width, height);
100   return width;
101 }
102 int get_pheight(Glib::RefPtr<Gdk::Pixmap> pixmap)
103 {
104   int width = 0, height = 0;
105   pixmap->get_size(width, height);
106   return height;
107 }
108 Glib::RefPtr<Gdk::Pixmap> scale (Glib::RefPtr<Gdk::Pixmap> pixmap, int w, int h)
109 {
110   return pixmap;
111   //return to_pixmap(to_pixbuf(pixmap)->scale_simple(w, h, Gdk::INTERP_BILINEAR));
112 }