Move third-party sources to separate sub-directory
[geeqie.git] / src / image-load-gdk.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: Vladimir Nadvornik
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "image-load-gdk.h"
23
24 #include <gdk-pixbuf/gdk-pixbuf.h>
25 #include <glib-object.h>
26 #include <glib.h>
27
28 #include "filedata.h"
29 #include "image-load.h"
30
31 namespace
32 {
33
34 struct ImageLoaderGdk : public ImageLoaderBackend
35 {
36 public:
37         ~ImageLoaderGdk() override;
38
39         void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
40         void set_size(int width, int height) override;
41         gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
42         GdkPixbuf *get_pixbuf() override;
43         gboolean close(GError **error) override;
44         gchar *get_format_name() override;
45         gchar **get_format_mime_types() override;
46
47 private:
48         GdkPixbufLoader *loader;
49 };
50
51 gchar *ImageLoaderGdk::get_format_name()
52 {
53         GdkPixbufFormat *format;
54
55         format = gdk_pixbuf_loader_get_format(loader);
56         if (format)
57                 {
58                 return gdk_pixbuf_format_get_name(format);
59                 }
60
61         return nullptr;
62 }
63
64 gchar **ImageLoaderGdk::get_format_mime_types()
65 {
66         return gdk_pixbuf_format_get_mime_types(gdk_pixbuf_loader_get_format(loader));
67 }
68
69 void ImageLoaderGdk::init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data)
70 {
71         auto il = static_cast<ImageLoader *>(data);
72
73         /** @FIXME gdk-pixbuf-loader does not recognize .xbm files unless
74          * the mime type is given. There should be a general case */
75         if (g_ascii_strncasecmp(il->fd->extension, ".xbm", 4) == 0)
76                 {
77                 loader = gdk_pixbuf_loader_new_with_mime_type("image/x-xbitmap", nullptr);
78                 }
79         else
80                 {
81                 loader = gdk_pixbuf_loader_new();
82                 }
83
84         g_signal_connect(G_OBJECT(loader), "area_updated", G_CALLBACK(area_updated_cb), data);
85         g_signal_connect(G_OBJECT(loader), "size_prepared", G_CALLBACK(size_prepared_cb), data);
86         g_signal_connect(G_OBJECT(loader), "area_prepared", G_CALLBACK(area_prepared_cb), data);
87 }
88
89 void ImageLoaderGdk::set_size(int width, int height)
90 {
91         gdk_pixbuf_loader_set_size(loader, width, height);
92 }
93
94 gboolean ImageLoaderGdk::write(const guchar *buf, gsize &chunk_size, gsize, GError **error)
95 {
96         return gdk_pixbuf_loader_write(loader, buf, chunk_size, error);
97 }
98
99 GdkPixbuf *ImageLoaderGdk::get_pixbuf()
100 {
101         return gdk_pixbuf_loader_get_pixbuf(loader);
102 }
103
104 gboolean ImageLoaderGdk::close(GError **error)
105 {
106         return gdk_pixbuf_loader_close(loader, error);
107 }
108
109 ImageLoaderGdk::~ImageLoaderGdk()
110 {
111         g_object_unref(G_OBJECT(loader));
112 }
113
114 } // namespace
115
116 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_default()
117 {
118         return std::make_unique<ImageLoaderGdk>();
119 }
120
121 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */