-
Notifications
You must be signed in to change notification settings - Fork 7
/
print.c
84 lines (68 loc) · 1.48 KB
/
print.c
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
#include <stdlib.h>
#include "lodepng.h"
#include "print.h"
#include "unix.h"
#define PAGE_WIDTH 1728
#define PAGE_HEIGHT 2200
#define PAPER_MAX (PAGE_HEIGHT * 20)
#define PAGE_MAX (PAGE_HEIGHT * 3)
extern void meatball(int width, int height, unsigned char *paper, unsigned char *image, unsigned char *page);
static unsigned char paper[PAGE_WIDTH * PAPER_MAX];
static unsigned char page[4 * PAGE_WIDTH * PAGE_MAX];
static unsigned char image[4 * 4 * PAGE_WIDTH * 4 * PAGE_MAX];
static int lines, position;
void print_start()
{
lines = 0;
}
void print_line()
{
if(lines >= PAPER_MAX)
return;
lines++;
// Clear the line
position = PAGE_WIDTH;
do {
--position;
print_dot(0);
--position;
} while(position > 0);
}
void print_dot(int bw)
{
if(lines >= PAPER_MAX)
return;
if(position >= PAGE_WIDTH)
return;
paper[(lines-1) * PAGE_WIDTH + position] = bw ? 0x00 : 0xFF;
position++;
}
static void make_page(void)
{
int n = lines;
if(n > PAGE_MAX)
n = PAGE_MAX;
meatball(PAGE_WIDTH, n, paper, image, page);
}
int print_finish(int offset, char *file, size_t n)
{
char time[100];
int error;
if(lines <= offset + 2) {
*file = 0;
return 0;
}
lines -= offset;
timestamp(time, sizeof time);
snprintf(file, n, "xgp-%s.png", time);
if(fork()) {
error = lines;
} else {
make_page();
error = lodepng_encode32_file(file, page, PAGE_WIDTH, lines);
exit(error == 0 ? 0 : 1);
}
memmove(paper, paper + PAGE_WIDTH*lines, PAGE_WIDTH*offset);
lines = offset;
return error;
}