Fix deprecation warning for poppler >= 0.82
[geeqie.git] / src / image-load-zxscr.cc
1 /*
2  * Copyright (C) 2021 - The Geeqie Team
3  *
4  * Author: Dusan Gallo
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-zxscr.h"
22
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <glib-object.h>
25 #include <glib.h>
26
27 #include "debug.h"
28 #include "image-load.h"
29
30 namespace
31 {
32
33 struct ImageLoaderZXSCR {
34         ImageLoaderBackendCbAreaUpdated area_updated_cb;
35         ImageLoaderBackendCbSize size_cb;
36         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
37         gpointer data;
38         GdkPixbuf *pixbuf;
39         guint requested_width;
40         guint requested_height;
41         gboolean abort;
42 };
43
44 constexpr guchar palette[2][8][3] = {
45         {
46                 {0x00, 0x00, 0x00},
47                 {0x00, 0x00, 0xbf},
48                 {0xbf, 0x00, 0x00},
49                 {0xbf, 0x00, 0xbf},
50                 {0x00, 0xbf, 0x00},
51                 {0x00, 0xbf, 0xbf},
52                 {0xbf, 0xbf, 0x00},
53                 {0xbf, 0xbf, 0xbf}
54         }, {
55                 {0x00, 0x00, 0x00},
56                 {0x00, 0x00, 0xff},
57                 {0xff, 0x00, 0x00},
58                 {0xff, 0x00, 0xff},
59                 {0x00, 0xff, 0x00},
60                 {0x00, 0xff, 0xff},
61                 {0xff, 0xff, 0x00},
62                 {0xff, 0xff, 0xff}
63         }
64 };
65
66 void free_buffer(guchar *pixels, gpointer)
67 {
68         g_free(pixels);
69 }
70
71 gboolean image_loader_zxscr_write(gpointer loader, const guchar *buf, gsize &chunk_size, gsize count, GError **)
72 {
73         auto ld = static_cast<ImageLoaderZXSCR *>(loader);
74         guint8 *pixels;
75         gint width;
76         gint height;
77         gint row;
78         gint col;
79         gint mrow;
80         gint pxs;
81         gint i;
82         guint8 attr;
83         guint8 bright;
84         guint8 ink;
85         guint8 paper;
86         guint8 *ptr;
87
88         if (count != 6144 && count != 6912)
89                 {
90                 log_printf("Error: zxscr reader error\n");
91                 return FALSE;
92                 }
93
94         width = 256;
95         height = 192;
96
97         pixels = static_cast<guint8 *>(g_try_malloc(width * height * 3));
98
99         if (!pixels)
100                 {
101                 log_printf("Error: zxscr reader error\n");
102                 return FALSE;
103                 }
104
105         ld->pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, FALSE, 8, width, height, width * 3, free_buffer, nullptr);
106
107         if (!ld->pixbuf)
108                 {
109                 g_free(pixels);
110                 DEBUG_1("Insufficient memory to open ZXSCR file");
111                 return FALSE;
112                 }
113         //let's decode screen
114         for (row = 0; row < 24; row++)
115                 for (col = 0; col < 32; col++)
116                         {
117                         if (count == 6144)
118                                 {
119                                 //if we have pixels only, make default white ink on black paper
120                                 bright = 0x01;
121                                 ink = 0x07;
122                                 paper = 0x00;
123                                 }
124                         else
125                                 {
126                                 attr = buf[6144 + row * 32 + col];
127                                 bright = (attr >> 6) & 0x01;
128                                 ink = attr & 0x07;
129                                 paper = ((attr >> 3) & 0x07);
130                                 }
131                         ptr = pixels + (row * 256 + col) * 8 * 3;
132
133                         for (mrow = 0; mrow < 8; mrow ++)
134                                 {
135                                 pxs = buf[(row / 8) * 2048 + mrow * 256 + (row % 8) * 32 + col];
136                                 for (i = 0; i < 8; i++)
137                                         {
138                                         if (pxs & 0x80)
139                                                 {
140                                                 *ptr++ = palette[bright][ink][0];       //r
141                                                 *ptr++ = palette[bright][ink][1];       //g
142                                                 *ptr++ = palette[bright][ink][2];       //b
143                                                 }
144                                         else
145                                                 {
146                                                 *ptr++ = palette[bright][paper][0];
147                                                 *ptr++ = palette[bright][paper][1];
148                                                 *ptr++ = palette[bright][paper][2];
149                                                 }
150                                         pxs <<= 1;
151                                         }
152                                 ptr += (31 * 8 * 3);
153                                 }
154                         }
155
156         ld->area_updated_cb(loader, 0, 0, width, height, ld->data);
157
158         chunk_size = count;
159         return TRUE;
160 }
161
162 gpointer image_loader_zxscr_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
163 {
164         auto loader = g_new0(ImageLoaderZXSCR, 1);
165         loader->area_updated_cb = area_updated_cb;
166         loader->size_cb = size_cb;
167         loader->area_prepared_cb = area_prepared_cb;
168         loader->data = data;
169         return loader;
170 }
171
172 void image_loader_zxscr_set_size(gpointer loader, int width, int height)
173 {
174         auto ld = static_cast<ImageLoaderZXSCR *>(loader);
175         ld->requested_width = width;
176         ld->requested_height = height;
177 }
178
179 GdkPixbuf *image_loader_zxscr_get_pixbuf(gpointer loader)
180 {
181         auto ld = static_cast<ImageLoaderZXSCR *>(loader);
182         return ld->pixbuf;
183 }
184
185 gchar *image_loader_zxscr_get_format_name(gpointer)
186 {
187         return g_strdup("zxscr");
188 }
189
190 gchar **image_loader_zxscr_get_format_mime_types(gpointer)
191 {
192         static const gchar *mime[] = {"application/octet-stream", nullptr};
193         return g_strdupv(const_cast<gchar **>(mime));
194 }
195
196 gboolean image_loader_zxscr_close(gpointer, GError **)
197 {
198         return TRUE;
199 }
200
201 void image_loader_zxscr_abort(gpointer loader)
202 {
203         auto ld = static_cast<ImageLoaderZXSCR *>(loader);
204         ld->abort = TRUE;
205 }
206
207 void image_loader_zxscr_free(gpointer loader)
208 {
209         auto ld = static_cast<ImageLoaderZXSCR *>(loader);
210         if (ld->pixbuf) g_object_unref(ld->pixbuf);
211         g_free(ld);
212 }
213
214 } // namespace
215
216 void image_loader_backend_set_zxscr(ImageLoaderBackend *funcs)
217 {
218         funcs->loader_new = image_loader_zxscr_new;
219         funcs->set_size = image_loader_zxscr_set_size;
220         funcs->write = image_loader_zxscr_write;
221         funcs->get_pixbuf = image_loader_zxscr_get_pixbuf;
222         funcs->close = image_loader_zxscr_close;
223         funcs->abort = image_loader_zxscr_abort;
224         funcs->free = image_loader_zxscr_free;
225         funcs->get_format_name = image_loader_zxscr_get_format_name;
226         funcs->get_format_mime_types = image_loader_zxscr_get_format_mime_types;
227 }
228 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */