README.md edited online with Bitbucket
[gdash.git] / src / gfx / pixbuffactory.cpp
blob95d176123b5f657c96574a6bbd4256b358033497
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
19 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <glib/gi18n.h>
25 #include <stdexcept>
26 #include <memory>
28 #include "gfx/pixbuffactory.hpp"
29 #include "misc/autogfreeptr.hpp"
30 #include "gfx/pixbuf.hpp"
31 #include "gfx/pixbufmanip.hpp"
33 /* scale2x is not translated: the license says that we should call it in its original name. */
34 // TRANSLATORS: you can translate "nearest neighbor" to "nearest" if the resulting
35 // string would be too long otherwise.
36 const char *gd_scaling_names[] = {N_("Nearest neighbor"), "Scale2x", "HQX", NULL};
39 /* check the arrays on start */
40 static class _init {
41 public:
42 _init() {
43 g_assert(G_N_ELEMENTS(gd_scaling_names) == GD_SCALING_MAX + 1); /* +1 is the terminating NULL */
45 } _init;
48 /* scales a pixbuf with the appropriate scaling type. */
49 Pixbuf *PixbufFactory::create_scaled(const Pixbuf &src, int scaling_factor, GdScalingType scaling_type, bool pal_emulation) const {
50 Pixbuf *scaled = this->create(src.get_width() * scaling_factor, src.get_height() * scaling_factor);
51 switch (scaling_factor) {
52 case 1:
53 src.copy(*scaled, 0, 0);
54 break;
55 case 2:
56 switch (scaling_type) {
57 case GD_SCALING_NEAREST:
58 scale2xnearest(src, *scaled);
59 break;
60 case GD_SCALING_SCALE2X:
61 scale2x(src, *scaled);
62 break;
63 case GD_SCALING_HQX:
64 hq2x(src, *scaled);
65 break;
66 case GD_SCALING_MAX:
67 g_assert_not_reached();
68 break;
70 break;
71 case 3:
72 switch (scaling_type) {
73 case GD_SCALING_NEAREST:
74 scale3xnearest(src, *scaled);
75 break;
76 case GD_SCALING_SCALE2X:
77 scale3x(src, *scaled);
78 break;
79 case GD_SCALING_HQX:
80 hq3x(src, *scaled);
81 break;
82 case GD_SCALING_MAX:
83 g_assert_not_reached();
84 break;
86 break;
87 case 4:
88 switch (scaling_type) {
89 case GD_SCALING_NEAREST:
90 /* 2x nearest applied twice. */
92 std::auto_ptr<Pixbuf> scale2x(this->create(src.get_width() * 2, src.get_height() * 2));
93 scale2xnearest(src, *scale2x);
94 scale2xnearest(*scale2x, *scaled);
96 break;
97 case GD_SCALING_SCALE2X:
98 /* scale2x applied twice. */
100 std::auto_ptr<Pixbuf> scale2xpb(this->create(src.get_width() * 2, src.get_height() * 2));
101 scale2x(src, *scale2xpb);
102 scale2x(*scale2xpb, *scaled);
104 break;
105 case GD_SCALING_HQX:
106 hq4x(src, *scaled);
107 break;
108 case GD_SCALING_MAX:
109 g_assert_not_reached();
110 break;
112 break;
113 default:
114 g_assert_not_reached();
115 break;
118 if (pal_emulation)
119 pal_emulate(*scaled);
121 return scaled;
125 Pixbuf *PixbufFactory::create_from_base64(const char *base64) const {
126 gsize len;
127 AutoGFreePtr<guchar> decoded(g_base64_decode(base64, &len));
128 return create_from_inline(len, decoded);