2 * Copyright (C) 2021 - The Geeqie Team
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.
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.
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.
21 #include "image-load-zxscr.h"
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <glib-object.h>
28 #include "image-load.h"
33 struct ImageLoaderZXSCR : public ImageLoaderBackend
36 ~ImageLoaderZXSCR() override;
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;
45 AreaUpdatedCb area_updated_cb;
51 constexpr guchar palette[2][8][3] = {
73 void free_buffer(guchar *pixels, gpointer)
78 gboolean ImageLoaderZXSCR::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
94 if (count != 6144 && count != 6912)
96 log_printf("Error: zxscr reader error\n");
103 pixels = static_cast<guint8 *>(g_try_malloc(width * height * 3));
107 log_printf("Error: zxscr reader error\n");
111 pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, FALSE, 8, width, height, width * 3, free_buffer, nullptr);
116 DEBUG_1("Insufficient memory to open ZXSCR file");
119 //let's decode screen
120 for (row = 0; row < 24; row++)
121 for (col = 0; col < 32; col++)
125 //if we have pixels only, make default white ink on black paper
132 attr = buf[6144 + row * 32 + col];
133 bright = (attr >> 6) & 0x01;
135 paper = ((attr >> 3) & 0x07);
137 ptr = pixels + (row * 256 + col) * 8 * 3;
139 for (mrow = 0; mrow < 8; mrow ++)
141 pxs = buf[(row / 8) * 2048 + mrow * 256 + (row % 8) * 32 + col];
142 for (i = 0; i < 8; i++)
146 *ptr++ = palette[bright][ink][0]; //r
147 *ptr++ = palette[bright][ink][1]; //g
148 *ptr++ = palette[bright][ink][2]; //b
152 *ptr++ = palette[bright][paper][0];
153 *ptr++ = palette[bright][paper][1];
154 *ptr++ = palette[bright][paper][2];
162 area_updated_cb(nullptr, 0, 0, width, height, data);
168 void ImageLoaderZXSCR::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
170 this->area_updated_cb = area_updated_cb;
174 GdkPixbuf *ImageLoaderZXSCR::get_pixbuf()
179 gchar *ImageLoaderZXSCR::get_format_name()
181 return g_strdup("zxscr");
184 gchar **ImageLoaderZXSCR::get_format_mime_types()
186 static const gchar *mime[] = {"application/octet-stream", nullptr};
187 return g_strdupv(const_cast<gchar **>(mime));
190 ImageLoaderZXSCR::~ImageLoaderZXSCR()
192 if (pixbuf) g_object_unref(pixbuf);
197 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_zxscr()
199 return std::make_unique<ImageLoaderZXSCR>();
201 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */