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