Let image loader backend decide how to process image buffer
[geeqie.git] / src / image-load-external.cc
1 /*
2  * Copyright (C) 2021 - The Geeqie Team
3  *
4  * Author: Colin Clark
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "image-load-external.h"
22
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <glib-object.h>
25 #include <glib.h>
26
27 #include "filedata.h"
28 #include "image-load.h"
29 #include "misc.h"
30 #include "options.h"
31 #include "ui-fileops.h"
32
33 namespace
34 {
35
36 struct ImageLoaderExternal {
37         ImageLoaderBackendCbAreaUpdated area_updated_cb;
38         ImageLoaderBackendCbSize size_cb;
39         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
40         gpointer data;
41         GdkPixbuf *pixbuf;
42         guint requested_width;
43         guint requested_height;
44         gboolean abort;
45 };
46
47 gboolean image_loader_external_write(gpointer loader, const guchar *, gsize &chunk_size, gsize count, GError **)
48 {
49         auto ld = static_cast<ImageLoaderExternal *>(loader);
50         auto il = static_cast<ImageLoader *>(ld->data);
51         gchar *cmd_line;
52         gchar *randname;
53         gchar *tilde_filename;
54
55         tilde_filename = expand_tilde(options->external_preview.extract);
56
57         randname = g_strdup("/tmp/geeqie_external_preview_XXXXXX");
58         g_mkstemp(randname);
59
60         cmd_line = g_strdup_printf("\"%s\" \"%s\" \"%s\"" , tilde_filename, il->fd->path, randname);
61
62         runcmd(cmd_line);
63
64         ld->pixbuf = gdk_pixbuf_new_from_file(randname, nullptr);
65
66         ld->area_updated_cb(loader, 0, 0, gdk_pixbuf_get_width(ld->pixbuf), gdk_pixbuf_get_height(ld->pixbuf), ld->data);
67
68         g_free(cmd_line);
69         unlink_file(randname);
70         g_free(randname);
71         g_free(tilde_filename);
72
73         chunk_size = count;
74         return TRUE;
75 }
76
77 gpointer image_loader_external_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
78 {
79         auto loader = g_new0(ImageLoaderExternal, 1);
80         loader->area_updated_cb = area_updated_cb;
81         loader->size_cb = size_cb;
82         loader->area_prepared_cb = area_prepared_cb;
83         loader->data = data;
84         return loader;
85 }
86
87 void image_loader_external_set_size(gpointer loader, int width, int height)
88 {
89         auto ld = static_cast<ImageLoaderExternal *>(loader);
90         ld->requested_width = width;
91         ld->requested_height = height;
92 }
93
94 GdkPixbuf* image_loader_external_get_pixbuf(gpointer loader)
95 {
96         auto ld = static_cast<ImageLoaderExternal *>(loader);
97         return ld->pixbuf;
98 }
99
100 gchar* image_loader_external_get_format_name(gpointer)
101 {
102         return g_strdup("external");
103 }
104
105 gchar** image_loader_external_get_format_mime_types(gpointer)
106 {
107         static const gchar *mime[] = {"application/octet-stream", nullptr};
108         return g_strdupv(const_cast<gchar **>(mime));
109 }
110
111 gboolean image_loader_external_close(gpointer, GError **)
112 {
113         return TRUE;
114 }
115
116 void image_loader_external_abort(gpointer loader)
117 {
118         auto ld = static_cast<ImageLoaderExternal *>(loader);
119         ld->abort = TRUE;
120 }
121
122 void image_loader_external_free(gpointer loader)
123 {
124         auto ld = static_cast<ImageLoaderExternal *>(loader);
125         if (ld->pixbuf) g_object_unref(ld->pixbuf);
126         g_free(ld);
127 }
128
129 } // namespace
130
131 void image_loader_backend_set_external(ImageLoaderBackend *funcs)
132 {
133         funcs->loader_new = image_loader_external_new;
134         funcs->set_size = image_loader_external_set_size;
135         funcs->write = image_loader_external_write;
136         funcs->get_pixbuf = image_loader_external_get_pixbuf;
137         funcs->close = image_loader_external_close;
138         funcs->abort = image_loader_external_abort;
139         funcs->free = image_loader_external_free;
140         funcs->get_format_name = image_loader_external_get_format_name;
141         funcs->get_format_mime_types = image_loader_external_get_format_mime_types;
142 }
143
144
145 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */