1 // Copyright (C) 2007 Ole Laursen
2 // Copyright (C) 2009, 2011, 2014 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 std::vector<PixMask*>
disassemble_row(const Glib::ustring & file,int no,bool & broken)22 disassemble_row(const Glib::ustring &file, int no, bool &broken)
23 {
24 Glib::RefPtr<Gdk::Pixbuf> row;
25 try
26 {
27 if(Gtk::Main::instance())
28 row = Gdk::Pixbuf::create_from_file(file);
29 else
30 broken = true;
31 }
32 catch (const Glib::Exception &ex)
33 {
34 broken = true;
35 }
36
37 if (broken || !row)
38 {
39 std::vector<PixMask*> empty;
40 return empty;
41 }
42
43 std::vector<Glib::RefPtr<Gdk::Pixbuf> > images;
44 images.reserve(no);
45
46 int h = row->get_height();
47 int w = row->get_width() / no;
48
49 // disassemble row
50 for (int x = 0; x < no; ++x)
51 {
52 Glib::RefPtr<Gdk::Pixbuf> buf
53 = Gdk::Pixbuf::create(row->get_colorspace(),
54 row->get_has_alpha(),
55 row->get_bits_per_sample(),
56 w, h);
57
58 row->copy_area(x * w, 0, w, h, buf, 0, 0);
59
60 images.push_back(buf);
61 }
62
63 std::vector<PixMask*> pixmasks;
64 for (unsigned int i = 0; i < images.size(); i++)
65 pixmasks.push_back(PixMask::create(images[i]));
66
67 return pixmasks;
68 }
69
70 std::vector<PixMask*>
disassemble_row(Glib::RefPtr<Gdk::Pixbuf> row,int no,bool first_half_height)71 disassemble_row(Glib::RefPtr<Gdk::Pixbuf> row, int no, bool first_half_height)
72 {
73 std::vector<Glib::RefPtr<Gdk::Pixbuf> > images;
74 images.reserve(no);
75
76 int h = row->get_height() / 2;
77 int w = row->get_width() / no;
78
79 int s = 0;
80 if (first_half_height == false)
81 s = h;
82 // disassemble row
83 for (int x = 0; x < no; ++x)
84 {
85 Glib::RefPtr<Gdk::Pixbuf> buf
86 = Gdk::Pixbuf::create(row->get_colorspace(),
87 row->get_has_alpha(),
88 row->get_bits_per_sample(),
89 w, h);
90
91 row->copy_area(x * w, s, w, h, buf, 0, 0);
92
93 images.push_back(buf);
94 }
95
96 std::vector<PixMask*> pixmasks;
97 for (unsigned int i = 0; i < images.size(); i++)
98 pixmasks.push_back(PixMask::create(images[i]));
99 return pixmasks;
100 }
101
102 std::vector<PixMask*>
disassemble_row(const Glib::ustring & file,int no,bool first_half_height,bool & broken)103 disassemble_row(const Glib::ustring &file, int no, bool first_half_height, bool &broken)
104 {
105 Glib::RefPtr<Gdk::Pixbuf> row;
106 try
107 {
108 row = Gdk::Pixbuf::create_from_file(file);
109 }
110 catch (const Glib::Exception &ex)
111 {
112 broken = true;
113 }
114
115 if (broken || !row)
116 {
117 std::vector<PixMask*> empty;
118 return empty;
119 }
120 return disassemble_row(row, no, first_half_height);
121 }
122
image_width_is_multiple_of_image_height(const Glib::ustring file)123 bool image_width_is_multiple_of_image_height(const Glib::ustring file)
124 {
125 Glib::RefPtr<Gdk::Pixbuf> row;
126 try
127 {
128 row = Gdk::Pixbuf::create_from_file(file);
129 }
130 catch (const Glib::Exception &ex)
131 {
132 return false;
133 }
134
135 guint32 width = row->get_width();
136 guint32 height = row->get_height();
137
138 if ((width % height) != 0)
139 return false;
140 return true;
141 }
142
get_image_width_and_height(const Glib::ustring & file,guint32 & width,guint32 & height,bool & broken)143 void get_image_width_and_height (const Glib::ustring &file, guint32 &width, guint32 &height, bool &broken)
144 {
145 Glib::RefPtr<Gdk::Pixbuf> row;
146 try
147 {
148 row = Gdk::Pixbuf::create_from_file(file);
149 }
150 catch (const Glib::Exception &ex)
151 {
152 broken = true;
153 return;
154 }
155 width = row->get_width();
156 height = row->get_height();
157 }
158