Fix warnings in exiv2
[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 : public ImageLoaderBackend
34 {
35 public:
36         ~ImageLoaderZXSCR() override;
37
38         void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
39         gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
40         GdkPixbuf *get_pixbuf() override;
41         gchar *get_format_name() override;
42         gchar **get_format_mime_types() override;
43
44 private:
45         AreaUpdatedCb area_updated_cb;
46         gpointer data;
47
48         GdkPixbuf *pixbuf;
49 };
50
51 constexpr guchar palette[2][8][3] = {
52         {
53                 {0x00, 0x00, 0x00},
54                 {0x00, 0x00, 0xbf},
55                 {0xbf, 0x00, 0x00},
56                 {0xbf, 0x00, 0xbf},
57                 {0x00, 0xbf, 0x00},
58                 {0x00, 0xbf, 0xbf},
59                 {0xbf, 0xbf, 0x00},
60                 {0xbf, 0xbf, 0xbf}
61         }, {
62                 {0x00, 0x00, 0x00},
63                 {0x00, 0x00, 0xff},
64                 {0xff, 0x00, 0x00},
65                 {0xff, 0x00, 0xff},
66                 {0x00, 0xff, 0x00},
67                 {0x00, 0xff, 0xff},
68                 {0xff, 0xff, 0x00},
69                 {0xff, 0xff, 0xff}
70         }
71 };
72
73 void free_buffer(guchar *pixels, gpointer)
74 {
75         g_free(pixels);
76 }
77
78 gboolean ImageLoaderZXSCR::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
79 {
80         guint8 *pixels;
81         gint width;
82         gint height;
83         gint row;
84         gint col;
85         gint mrow;
86         gint pxs;
87         gint i;
88         guint8 attr;
89         guint8 bright;
90         guint8 ink;
91         guint8 paper;
92         guint8 *ptr;
93
94         if (count != 6144 && count != 6912)
95                 {
96                 log_printf("Error: zxscr reader error\n");
97                 return FALSE;
98                 }
99
100         width = 256;
101         height = 192;
102
103         pixels = static_cast<guint8 *>(g_try_malloc(width * height * 3));
104
105         if (!pixels)
106                 {
107                 log_printf("Error: zxscr reader error\n");
108                 return FALSE;
109                 }
110
111         pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, FALSE, 8, width, height, width * 3, free_buffer, nullptr);
112
113         if (!pixbuf)
114                 {
115                 g_free(pixels);
116                 DEBUG_1("Insufficient memory to open ZXSCR file");
117                 return FALSE;
118                 }
119         //let's decode screen
120         for (row = 0; row < 24; row++)
121                 for (col = 0; col < 32; col++)
122                         {
123                         if (count == 6144)
124                                 {
125                                 //if we have pixels only, make default white ink on black paper
126                                 bright = 0x01;
127                                 ink = 0x07;
128                                 paper = 0x00;
129                                 }
130                         else
131                                 {
132                                 attr = buf[6144 + row * 32 + col];
133                                 bright = (attr >> 6) & 0x01;
134                                 ink = attr & 0x07;
135                                 paper = ((attr >> 3) & 0x07);
136                                 }
137                         ptr = pixels + (row * 256 + col) * 8 * 3;
138
139                         for (mrow = 0; mrow < 8; mrow ++)
140                                 {
141                                 pxs = buf[(row / 8) * 2048 + mrow * 256 + (row % 8) * 32 + col];
142                                 for (i = 0; i < 8; i++)
143                                         {
144                                         if (pxs & 0x80)
145                                                 {
146                                                 *ptr++ = palette[bright][ink][0];       //r
147                                                 *ptr++ = palette[bright][ink][1];       //g
148                                                 *ptr++ = palette[bright][ink][2];       //b
149                                                 }
150                                         else
151                                                 {
152                                                 *ptr++ = palette[bright][paper][0];
153                                                 *ptr++ = palette[bright][paper][1];
154                                                 *ptr++ = palette[bright][paper][2];
155                                                 }
156                                         pxs <<= 1;
157                                         }
158                                 ptr += (31 * 8 * 3);
159                                 }
160                         }
161
162         area_updated_cb(nullptr, 0, 0, width, height, data);
163
164         chunk_size = count;
165         return TRUE;
166 }
167
168 void ImageLoaderZXSCR::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
169 {
170         this->area_updated_cb = area_updated_cb;
171         this->data = data;
172 }
173
174 GdkPixbuf *ImageLoaderZXSCR::get_pixbuf()
175 {
176         return pixbuf;
177 }
178
179 gchar *ImageLoaderZXSCR::get_format_name()
180 {
181         return g_strdup("zxscr");
182 }
183
184 gchar **ImageLoaderZXSCR::get_format_mime_types()
185 {
186         static const gchar *mime[] = {"application/octet-stream", nullptr};
187         return g_strdupv(const_cast<gchar **>(mime));
188 }
189
190 ImageLoaderZXSCR::~ImageLoaderZXSCR()
191 {
192         if (pixbuf) g_object_unref(pixbuf);
193 }
194
195 } // namespace
196
197 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_zxscr()
198 {
199         return std::make_unique<ImageLoaderZXSCR>();
200 }
201 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */