-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathimage-load-zxscr.cc
200 lines (173 loc) · 4.45 KB
/
image-load-zxscr.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (C) 2021 - The Geeqie Team
*
* Author: Dusan Gallo
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "image-load-zxscr.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib-object.h>
#include <glib.h>
#include "image-load.h"
namespace
{
struct ImageLoaderZXSCR : public ImageLoaderBackend
{
public:
~ImageLoaderZXSCR() override;
void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
GdkPixbuf *get_pixbuf() override;
gchar *get_format_name() override;
gchar **get_format_mime_types() override;
private:
AreaUpdatedCb area_updated_cb;
gpointer data;
GdkPixbuf *pixbuf;
};
constexpr guchar palette[2][8][3] = {
{
{0x00, 0x00, 0x00},
{0x00, 0x00, 0xbf},
{0xbf, 0x00, 0x00},
{0xbf, 0x00, 0xbf},
{0x00, 0xbf, 0x00},
{0x00, 0xbf, 0xbf},
{0xbf, 0xbf, 0x00},
{0xbf, 0xbf, 0xbf}
}, {
{0x00, 0x00, 0x00},
{0x00, 0x00, 0xff},
{0xff, 0x00, 0x00},
{0xff, 0x00, 0xff},
{0x00, 0xff, 0x00},
{0x00, 0xff, 0xff},
{0xff, 0xff, 0x00},
{0xff, 0xff, 0xff}
}
};
void free_buffer(guchar *pixels, gpointer)
{
g_free(pixels);
}
gboolean ImageLoaderZXSCR::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
{
guint8 *pixels;
gint width;
gint height;
gint row;
gint col;
gint mrow;
gint pxs;
gint i;
guint8 attr;
guint8 bright;
guint8 ink;
guint8 paper;
guint8 *ptr;
if (count != 6144 && count != 6912)
{
log_printf("Error: zxscr reader error\n");
return FALSE;
}
width = 256;
height = 192;
pixels = static_cast<guint8 *>(g_try_malloc(width * height * 3));
if (!pixels)
{
log_printf("Error: zxscr reader error\n");
return FALSE;
}
pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, FALSE, 8, width, height, width * 3, free_buffer, nullptr);
if (!pixbuf)
{
g_free(pixels);
DEBUG_1("Insufficient memory to open ZXSCR file");
return FALSE;
}
//let's decode screen
for (row = 0; row < 24; row++)
for (col = 0; col < 32; col++)
{
if (count == 6144)
{
//if we have pixels only, make default white ink on black paper
bright = 0x01;
ink = 0x07;
paper = 0x00;
}
else
{
attr = buf[6144 + (row * 32) + col];
bright = (attr >> 6) & 0x01;
ink = attr & 0x07;
paper = ((attr >> 3) & 0x07);
}
ptr = pixels + (row * 256 + col) * 8 * 3;
for (mrow = 0; mrow < 8; mrow ++)
{
pxs = buf[((row / 8) * 2048) + (mrow * 256) + ((row % 8) * 32) + col];
for (i = 0; i < 8; i++)
{
if (pxs & 0x80)
{
*ptr++ = palette[bright][ink][0]; //r
*ptr++ = palette[bright][ink][1]; //g
*ptr++ = palette[bright][ink][2]; //b
}
else
{
*ptr++ = palette[bright][paper][0];
*ptr++ = palette[bright][paper][1];
*ptr++ = palette[bright][paper][2];
}
pxs <<= 1;
}
ptr += (31 * 8 * 3);
}
}
area_updated_cb(nullptr, 0, 0, width, height, data);
chunk_size = count;
return TRUE;
}
void ImageLoaderZXSCR::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
{
this->area_updated_cb = area_updated_cb;
this->data = data;
}
GdkPixbuf *ImageLoaderZXSCR::get_pixbuf()
{
return pixbuf;
}
gchar *ImageLoaderZXSCR::get_format_name()
{
return g_strdup("zxscr");
}
gchar **ImageLoaderZXSCR::get_format_mime_types()
{
static const gchar *mime[] = {"application/octet-stream", nullptr};
return g_strdupv(const_cast<gchar **>(mime));
}
ImageLoaderZXSCR::~ImageLoaderZXSCR()
{
if (pixbuf) g_object_unref(pixbuf);
}
} // namespace
std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_zxscr()
{
return std::make_unique<ImageLoaderZXSCR>();
}
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */