forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgrob.h
321 lines (270 loc) · 10.3 KB
/
grob.h
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#ifndef GROB_H
# define GROB_H
// ****************************************************************************
// grob.h DB48X project
// ****************************************************************************
//
// File Description:
//
// Graphic objects, representing a bitmap in memory
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2023 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X 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.
// ****************************************************************************
#include "blitter.h"
#include "list.h"
#include "object.h"
#include "runtime.h"
#include "target.h"
#include "renderer.h"
#include "settings.h"
GCP(grob);
struct grob : object
// ----------------------------------------------------------------------------
// Representation of a graphic object
// ----------------------------------------------------------------------------
{
using pixsize = blitter::size;
using surface = blitter::surface<blitter::mode::MONOCHROME_REVERSE>;
using pattern = blitter::pattern<blitter::mode::MONOCHROME_REVERSE>;
grob(id type, pixsize w, pixsize h, gcbytes bits): object(type)
// ------------------------------------------------------------------------
// Graphic object constructor
// ------------------------------------------------------------------------
{
byte *p = (byte *) payload();
p = leb128(p, w);
p = leb128(p, h);
size_t ds = datasize(type, w, h);
byte_p s = bits;
while (ds--)
*p++ = *s++;
}
grob(id type, pixsize w, pixsize h): object(type)
// ------------------------------------------------------------------------
// Graphic object constructor
// ------------------------------------------------------------------------
{
byte *p = (byte *) payload();
p = leb128(p, w);
p = leb128(p, h);
size_t ds = datasize(type, w, h);
while (ds--)
*p++ = 0;
}
static size_t required_memory(id type, pixsize w, pixsize h)
// ------------------------------------------------------------------------
// Compute required grob memory for the given parameters
// ------------------------------------------------------------------------
{
size_t bodysize = bytesize(type, w, h);
return leb128size(type) + bodysize;
}
static size_t required_memory(id type,
pixsize w,
pixsize h,
gcutf8 UNUSED bytes)
// ------------------------------------------------------------------------
// Compute required grob memory for the given parameters
// ------------------------------------------------------------------------
{
size_t bs = bytesize(type, w, h);
return leb128size(type) + bs;
}
static grob_p make(pixsize w, pixsize h)
// ------------------------------------------------------------------------
// Build a grob from the given parameters
// ------------------------------------------------------------------------
{
return rt.make<grob>(w, h);
}
static grob_p make(pixsize w, pixsize h, byte_p bits)
// ------------------------------------------------------------------------
// Build a grob from the given parameters
// ------------------------------------------------------------------------
{
return rt.make<grob>(w, h, bits);
}
static size_t bytesize(id type, pixsize w, pixsize h)
// ------------------------------------------------------------------------
// Compute the number of bytes required for a bitmap
// ------------------------------------------------------------------------
{
size_t ds = datasize(type, w, h);
return leb128size(w) + leb128size(h) + ds;
}
static size_t datasize(id type, pixsize w, pixsize h)
// ------------------------------------------------------------------------
// Compute the number of bytes required for a bitmap
// ------------------------------------------------------------------------
{
return type == ID_grob ? (w + 7) / 8 * h : (w * h + 7) / 8;
}
pixsize width() const
// ------------------------------------------------------------------------
// Return the width of a grob
// ------------------------------------------------------------------------
{
byte_p p = payload();
return leb128<pixsize>(p);
}
pixsize height() const
// ------------------------------------------------------------------------
// Return the height of a grob
// ------------------------------------------------------------------------
{
byte_p p = payload();
p = leb128skip(p); // Skip width
return leb128<pixsize>(p);
}
byte_p pixels(pixsize *width, pixsize *height, size_t *datalen = 0) const
// ------------------------------------------------------------------------
// Return the byte pointer to the data in the grob
// ------------------------------------------------------------------------
{
byte_p p = payload();
pixsize w = leb128<pixsize>(p);
pixsize h = leb128<pixsize>(p);
if (width)
*width = w;
if (height)
*height = h;
if (datalen)
*datalen = datasize(type(), w, h);
return p;
}
surface pixels() const
// ------------------------------------------------------------------------
// Return a blitter surface for the grob
// ------------------------------------------------------------------------
{
pixsize w = 0;
pixsize h = 0;
byte_p bitmap = pixels(&w, &h);
pixsize scanline = type() == ID_grob ? (w + 7) / 8 * 8 : w;
return surface((pixword *) bitmap, w, h, scanline);
}
using blitop = blitter::blitop;
static object::result command(blitop op);
// ------------------------------------------------------------------------
// Shared code for GXor, GOr, GAnd
// ------------------------------------------------------------------------
typedef grob_p(*grob1_fn)(grob_r x);
typedef grob_p(*grob2_fn)(grob_r y, grob_r x);
typedef grob_p(*grobop_fn)(pixsize height);
static object::result command(grob1_fn gfn);
static object::result command(grob2_fn gfn);
static object::result command(grobop_fn gfn);
// ------------------------------------------------------------------------
// Shared code for GraphicAppend, GraphicStack, etc
// ------------------------------------------------------------------------
// Extract a subimage
grob_p extract(object_r first, object_r last) const;
grob_p extract(coord x1, coord y1, coord x2, coord y2) const;
public:
OBJECT_DECL(grob);
PARSE_DECL(grob);
SIZE_DECL(grob);
RENDER_DECL(grob);
GRAPH_DECL(grob);
};
struct bitmap : grob
// ----------------------------------------------------------------------------
// DB48X optimized bitmap representation
// ----------------------------------------------------------------------------
{
bitmap(id ty, pixsize w, pixsize h, gcbytes bits) : grob(ty, w, h, bits) {}
bitmap(id type, pixsize w, pixsize h): grob(type, w, h) {}
static grob_p make(pixsize w, pixsize h)
// ------------------------------------------------------------------------
// Build a grob from the given parameters
// ------------------------------------------------------------------------
{
return rt.make<bitmap>(w, h);
}
static grob_p make(pixsize w, pixsize h, byte_p bits)
// ------------------------------------------------------------------------
// Build a grob from the given parameters
// ------------------------------------------------------------------------
{
return rt.make<bitmap>(w, h, bits);
}
public:
OBJECT_DECL(bitmap);
// PARSE_DECL(bitmap) is managed by grob class
SIZE_DECL(bitmap);
RENDER_DECL(bitmap);
};
struct grapher
// ----------------------------------------------------------------------------
// Information about graphing environment
// ----------------------------------------------------------------------------
{
using font_id = settings::font_id;
grapher(size w = LCD_W,
size h = LCD_H,
font_id f = Settings.ResultFont(),
grob::pattern fg = Settings.Foreground(),
grob::pattern bg = Settings.Background(),
bool stack = false,
bool expr = false,
bool graph = false)
: maxw(w),
maxh(h),
start(sys_current_ms()),
duration(Settings.GraphingTimeLimit()),
voffset(0),
font(f),
foreground(fg),
background(bg),
stack(stack),
expression(expr),
graph(graph)
{}
grapher(const grapher &other) = default;
grob_p grob(size w, size h)
{
if (w <= maxw && h <= maxh && sys_current_ms() - start <= duration)
return grob::make(w, h);
return nullptr;
}
bool reduce_font()
{
if (sys_current_ms() - start > duration)
return false;
font_id next = settings::smaller_font(font);
if (next == font)
return false;
font = next;
return true;
}
size maxw;
size maxh;
uint start;
uint duration;
coord voffset;
font_id font;
grob::pattern foreground;
grob::pattern background;
bool stack;
bool expression;
bool graph;
};
#endif // GROB_H