-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqomutil.c
143 lines (134 loc) · 4.22 KB
/
qomutil.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
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
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "unistd.h"
#include "string.h"
#include <sys/time.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define QOI_IMPLEMENTATION
#include "qoi.h"
#define QOM_IMPLEMENTATION
#include "qom.h"
// support for reading and writing png images for testing
gfx_canvas *canvas_frompng(const char *filename)
{
int sizex, sizey, n;
unsigned char *data = stbi_load(filename, &sizex, &sizey, &n, 0);
if(!data) {
fprintf(stderr, "gfx_canvas_frompng: error: problem reading %s\n", filename);
exit(1);
}
return gfx_canvas_new_withdata(sizex, sizey, data);
}
void canvas_topng(gfx_canvas *in, const char *filename)
{
stbi_write_png(filename, in->sizex, in->sizey, 4, in->data, 4*in->sizex);
}
// test program follows
void qom_trim(qom *qm_in, qom *qm_out, int frame0, int frame1)
{
int nframes = qom_getnframes(qm_in);
if(nframes == 0)
return;
if(frame0<0) frame0 = 0;
if(frame1<0) frame1 = 0;
if(frame0>=nframes) frame0 = nframes-1;
if(frame1>=nframes) frame0 = nframes-1;
if(frame1<frame0) {
int temp = frame1; int frame1 = frame0; int frame0 = temp;
}
for(int frameno = 0; frameno<nframes; frameno++) {
if((frameno>=frame0) && (frameno<=frame1)) {
double usec;
gfx_canvas *c = qom_getframe(qm_in, frameno, &usec);
qom_putframe(qm_out, c, usec);
gfx_canvas_free(c);
}
}
}
static int firsted = 0;
void qom_randseg(qom *qm_in, qom *qm_out, int nframes)
{
int shift = qom_getnframes(qm_in)-nframes;
if(!firsted) {
srandom(getpid());
firsted = 1;
}
int offset = random()%shift;
int frame0 = offset;
int frame1 = offset+nframes-1;
qom_trim(qm_in, qm_out, frame0, frame1);
}
#define DEFAULT_FRAMETIME ((1000*1000)/30.0)
int main(int argc, char **argv)
{
if(argc<3) {
fprintf(stderr, "\nusage: qomutil -toqom 00.png 01.png 02.png out.qom\n\n");
fprintf(stderr, "usage: qomutil -topng in.qom outfamily\n\n");
fprintf(stderr, "usage: qomutil -print in.qom\n\n");
fprintf(stderr, "usage: qomutil -trim in.qom out.qom startframe endframe\n\n");
fprintf(stderr, "usage: qomutil -benchmark in.qom\n\n");
exit(1);
}
if(strcmp(argv[1], "-toqom") == 0) {
int usec = 0;
qom *qm = qom_open(argv[argc-1], "w");
if(!qm)
exit(1);
for(int argp = 2; argp<argc-1; argp++) {
gfx_canvas *c = canvas_frompng(argv[argp]);
qom_putframe(qm, c, usec);
gfx_canvas_free(c);
usec += DEFAULT_FRAMETIME;
}
qom_close(qm);
} else if(strcmp(argv[1], "-topng") == 0) {
qom *qm = qom_open(argv[2], "r");
if(!qm)
exit(1);
for(int frameno = 0; frameno<qom_getnframes(qm); frameno++) {
char outfname[1024];
double usec;
gfx_canvas *c = qom_getframe(qm, frameno, &usec);
sprintf(outfname, "%s%04d.png", argv[3], frameno);
canvas_topng(c, outfname);
gfx_canvas_free(c);
}
qom_close(qm);
} else if(strcmp(argv[1], "-print") == 0) {
qom *qm = qom_open(argv[2], "r");
if(!qm)
exit(1);
qom_print(qm, "test");
qom_close(qm);
} else if(strcmp(argv[1], "-trim") == 0) {
qom *qm_in = qom_open(argv[1], "r");
if(!qm_in)
exit(1);
qom *qm_out = qom_open(argv[2], "w");
if(!qm_out)
exit(1);
qom_trim(qm_in, qm_out, atoi(argv[3]), atoi(argv[4]));
qom_close(qm_out);
qom_close(qm_in);
} else if(strcmp(argv[1], "-randseg") == 0) {
qom *qm_in = qom_open(argv[2], "r");
if(!qm_in)
exit(1);
qom *qm_out = qom_open(argv[3], "w");
if(!qm_out)
exit(1);
qom_randseg(qm_in, qm_out, atoi(argv[4]));
qom_close(qm_out);
qom_close(qm_in);
} else if(strcmp(argv[1], "-benchmark") == 0) {
qom_readbenchmark(argv[2]);
} else {
fprintf(stderr, "strange option [%s]\n", argv[1]);
exit(1);
}
return 0;
}