-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfb.c
More file actions
219 lines (186 loc) · 5.17 KB
/
bfb.c
File metadata and controls
219 lines (186 loc) · 5.17 KB
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
/* Copyright 2020 Edwin Watkeys */
/* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: */
/* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software. */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "unicode.h"
#include "bfb.h"
extern int init_bfb (
bfb *b,
int dot_width, int dot_height,
unsigned short default_block)
{
b->width = (dot_width+1)>>1;
b->height = (dot_height+3)>>2;
b->blocks = malloc(b->width * b->height * sizeof(*b->blocks));
if (b->blocks == NULL)
return -1;
bfb_clear(b, default_block);
return 0;
}
extern void finalize_bfb(bfb *b)
{
free(b->blocks);
}
extern void bfb_clear(bfb *b, unsigned short block_value)
{
int i;
for (i = 0; i < b->width * b->height; i++) {
bfb_block *block = &b->blocks[i];
block->pattern = block_value;
block->sgr1 = 0;
block->sgr2 = 0;
block->sgr3 = 0;
}
}
extern void bfb_home(bfb *b, FILE *fp)
{
fprintf(fp, "\x1b[%dA\x1b[G", b->height);
}
extern void bfb_fput(bfb *b, FILE *fp)
{
int row, col;
int prev_sgr1 = 0;
int prev_sgr2 = 0;
int prev_sgr3 = 0;
fputs("\x1b[0m", fp);
for(row = 0; row < b->height; row++) {
for(col = 0; col < b->width; col++) {
size_t offset = row*b->width + col;
bfb_block *block = &b->blocks[offset];
int sgr1 = block->sgr1;
int sgr2 = block->sgr2;
int sgr3 = block->sgr3;
if (sgr1 != prev_sgr1) {
fprintf(fp, "\x1b[%dm", sgr1 );
prev_sgr1 = sgr1;
}
if ((sgr2 != prev_sgr2)
&& (sgr2 != prev_sgr1))
{
fprintf(fp, "\x1b[%dm", sgr2 );
prev_sgr2 = sgr2;
}
if ((sgr3 != prev_sgr3)
&& (sgr3 != prev_sgr2)
&& (sgr3 != prev_sgr1))
{
fprintf(fp, "\x1b[%dm", sgr3 );
prev_sgr3 = sgr3;
}
unicode_fput_codepoint(0x2800 + block->pattern, fp);
}
fputc('\n', fp);
}
fputs("\x1b[0m", fp);
}
static size_t mask(int x, int y)
{
static unsigned short offsets[] = {0, 3, 1, 4, 2, 5, 6, 7};
return 1 << offsets[(y << 1) | x];
}
extern void bfb_resolve_pt(bfb_pt *pt)
{
pt->char_col = pt->x >> 1;
pt->char_row = pt->y >> 2;
pt->mask = mask(pt->x & 0x01, pt->y & 0x03);
pt->block = NULL;
}
extern void bfb_plot(bfb *b, int x, int y, int is_on)
{
bfb_pt pt = { x, y };
bfb_resolve_pt(&pt);
if ((pt.char_col >= 0)
&& (pt.char_col < b->width)
&& (pt.char_row >= 0)
&& (pt.char_row < b->height)) {
if (is_on)
b->blocks[pt.char_row * b->width + pt.char_col].pattern
|= pt.mask;
else
b->blocks[pt.char_row * b->width + pt.char_col].pattern
&= pt.mask ^ 0xff;
}
}
static void bfb_peek(bfb *b, bfb_pt *pt)
{
bfb_resolve_pt(pt);
if ((pt->char_col >= 0)
&& (pt->char_col < b->width)
&& (pt->char_row >= 0)
&& (pt->char_row < b->height))
pt->block = &b->blocks[pt->char_row * b->width + pt->char_col];
else
pt->block = NULL;
}
int bfb_isset(bfb *b, int x, int y)
{
bfb_pt pt = { x, y };
bfb_peek(b, &pt);
return ((pt.block != NULL)
&&((pt.block->pattern & pt.mask) != 0));
}
extern void bfb_set_chunk_attrs(
bfb *b,
int dot_x, int dot_y,
unsigned int sgr1,
unsigned int sgr2,
unsigned int sgr3)
{
bfb_pt pt = { dot_x, dot_y };
bfb_peek(b, &pt);
if (pt.block != NULL)
{
pt.block->sgr1 = sgr1;
pt.block->sgr2 = sgr2;
pt.block->sgr3 = sgr3;
}
}
extern void bfb_pt_set(bfb_pt *pt)
{
if (pt->block != NULL)
pt->block->pattern |= pt->mask;
}
extern void bfb_pt_reset(bfb_pt *pt)
{
if (pt->block != NULL)
pt->block->pattern &= pt->mask ^ 0xff;
}
extern void bfb_blit(
bfb *dest, const void *src,
int at_dest_x, int at_dest_y,
bfb_xfer_fn transfer_fn,
unsigned int src_width,
unsigned int src_height,
double x_scale, double y_scale,
void *refcon)
{
int i, j;
int w_steps = (int)((double)src_width * x_scale);
int h_steps = (int)((double)src_height * y_scale);
for (i = 0; i < w_steps; i++) {
for (j = 0; j < h_steps; j++) {
int src_x = (int)round((double)i / x_scale);
int src_y = (int)round((double)j / y_scale);
bfb_pt current = {i + at_dest_x, j + at_dest_y};
bfb_peek(dest, ¤t);
transfer_fn(dest, src, src_x, src_y, ¤t, refcon);
}
}
}