Let image loader backend decide how to process image buffer
[geeqie.git] / src / image-load-collection.cc
1 /*
2  * Copyright (C) 20018 - 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-collection.h"
22
23 #include <unistd.h>
24
25 #include <cstdio>
26
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <glib-object.h>
29 #include <glib.h>
30
31 #include "cache.h"
32 #include "filedata.h"
33 #include "image-load.h"
34 #include "misc.h"
35 #include "options.h"
36 #include "ui-fileops.h"
37
38 namespace
39 {
40
41 struct ImageLoaderCOLLECTION {
42         ImageLoaderBackendCbAreaUpdated area_updated_cb;
43         ImageLoaderBackendCbSize size_cb;
44         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
45         gpointer data;
46         GdkPixbuf *pixbuf;
47         guint requested_width;
48         guint requested_height;
49         gboolean abort;
50 };
51
52 gboolean image_loader_collection_write(gpointer loader, const guchar *, gsize &chunk_size, gsize count, GError **)
53 {
54         auto ld = static_cast<ImageLoaderCOLLECTION *>(loader);
55         auto il = static_cast<ImageLoader *>(ld->data);
56
57         #define LINE_LENGTH 1000
58
59         gboolean ret = FALSE;
60         gchar *randname;
61         gchar *cmd_line;
62         FILE *fp = nullptr;
63         gint line_count = 0;
64         GString *file_names = g_string_new(nullptr);
65         gchar line[LINE_LENGTH];
66         gchar **split_line = nullptr;
67         gchar *cache_found;
68         gchar *pathl;
69
70         if (runcmd("which montage >/dev/null 2>&1") == 0)
71                 {
72                 pathl = path_from_utf8(il->fd->path);
73                 fp = fopen(pathl, "r");
74                 g_free(pathl);
75                 if (fp)
76                         {
77                         while(fgets(line, LINE_LENGTH, fp) && line_count < options->thumbnails.collection_preview)
78                                 {
79                                 if (line[0] && line[0] != '#')
80                                         {
81                                         split_line = g_strsplit(line, "\"", 4);
82                                         cache_found = cache_find_location(CACHE_TYPE_THUMB, split_line[1]);
83                                         if (cache_found)
84                                                 {
85                                                 g_string_append_printf(file_names, "\"%s\" ", cache_found);
86                                                 line_count++;
87                                                 }
88                                         g_free(cache_found);
89                                         }
90                                         if (split_line)
91                                                 {
92                                                 g_strfreev(split_line);
93                                                 }
94                                         split_line = nullptr;
95                                 }
96                         fclose(fp);
97
98                         if (file_names->len > 0)
99                                 {
100                                 randname = g_strdup("/tmp/geeqie_collection_XXXXXX.png");
101                                 g_mkstemp(randname);
102
103                                 cmd_line = g_strdup_printf("montage %s -geometry %dx%d+1+1 %s >/dev/null 2>&1", file_names->str, options->thumbnails.max_width, options->thumbnails.max_height, randname);
104
105                                 runcmd(cmd_line);
106                                 ld->pixbuf = gdk_pixbuf_new_from_file(randname, nullptr);
107                                 if (ld->pixbuf)
108                                         {
109                                         ld->area_updated_cb(loader, 0, 0, gdk_pixbuf_get_width(ld->pixbuf), gdk_pixbuf_get_height(ld->pixbuf), ld->data);
110                                         }
111
112                                 unlink(randname);
113                                 g_free(randname);
114                                 g_free(cmd_line);
115
116                                 ret = TRUE;
117                                 }
118
119                         g_string_free(file_names, TRUE);
120                         }
121                 }
122         chunk_size = count;
123         return ret;
124 }
125
126 gpointer image_loader_collection_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
127 {
128         auto loader = g_new0(ImageLoaderCOLLECTION, 1);
129         loader->area_updated_cb = area_updated_cb;
130         loader->size_cb = size_cb;
131         loader->area_prepared_cb = area_prepared_cb;
132         loader->data = data;
133         return loader;
134 }
135
136 void image_loader_collection_set_size(gpointer loader, int width, int height)
137 {
138         auto ld = static_cast<ImageLoaderCOLLECTION *>(loader);
139         ld->requested_width = width;
140         ld->requested_height = height;
141 }
142
143 GdkPixbuf* image_loader_collection_get_pixbuf(gpointer loader)
144 {
145         auto ld = static_cast<ImageLoaderCOLLECTION *>(loader);
146         return ld->pixbuf;
147 }
148
149 gchar* image_loader_collection_get_format_name(gpointer)
150 {
151         return g_strdup("collection");
152 }
153
154 gchar** image_loader_collection_get_format_mime_types(gpointer)
155 {
156         static const gchar *mime[] = {"image/png", nullptr};
157         return g_strdupv(const_cast<gchar **>(mime));
158 }
159
160 gboolean image_loader_collection_close(gpointer, GError **)
161 {
162         return TRUE;
163 }
164
165 void image_loader_collection_abort(gpointer loader)
166 {
167         auto ld = static_cast<ImageLoaderCOLLECTION *>(loader);
168         ld->abort = TRUE;
169 }
170
171 void image_loader_collection_free(gpointer loader)
172 {
173         auto ld = static_cast<ImageLoaderCOLLECTION *>(loader);
174         if (ld->pixbuf) g_object_unref(ld->pixbuf);
175         g_free(ld);
176 }
177
178 } // namespace
179
180 void image_loader_backend_set_collection(ImageLoaderBackend *funcs)
181 {
182         funcs->loader_new = image_loader_collection_new;
183         funcs->set_size = image_loader_collection_set_size;
184         funcs->write = image_loader_collection_write;
185         funcs->get_pixbuf = image_loader_collection_get_pixbuf;
186         funcs->close = image_loader_collection_close;
187         funcs->abort = image_loader_collection_abort;
188         funcs->free = image_loader_collection_free;
189         funcs->get_format_name = image_loader_collection_get_format_name;
190         funcs->get_format_mime_types = image_loader_collection_get_format_mime_types;
191 }
192
193 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */