-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng2apng.c
122 lines (97 loc) · 3.18 KB
/
png2apng.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
/*
Copyright (C) 2016 Matteo Nastasi <[email protected]>
This file is part of png2apng.
png2apng 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 3 of the License, or
(at your option) any later version.
Foobar 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <subpng.h>
int filename2msec(char *s);
int filename2msec(char *s)
{
char bf[32], *b, *e;
if ((e = strstr(s, "ms)")) == NULL) {
return 1000;
}
for (b = (e - 1) ; b >= s ; b--) {
if (*b == '(') {
memcpy(bf, b+1, e - b - 1);
bf[e - b - 1] = '\0';
break;
}
}
return atoi(bf);
}
int main(int argc, char *argv[])
{
char *foutname;
struct png_img *png_out, *png_in;
struct png_acTL *actl;
struct png_fcTL fctl_in, *fctl;
struct png_IEND *iend;
struct png_chunk *pc;
int i, idat_idx, seq = 0;
strcpy(fctl_in.type, "fcTL");
fctl_in.sequence_number = 0;
fctl_in.x_offset = 0;
fctl_in.y_offset = 0;
fctl_in.dispose_op = 1; // clean before apply new frame
fctl_in.blend_op = 0; // no blend, full replace
foutname = argv[1];
if (fopen(foutname, "rb")) {
fprintf(stderr, "File %s already exists.\n", argv[1]);
exit(1);
}
if ((png_out = png_img_create()) == NULL) {
exit(2);
}
for (i = 2 ; i < argc ; i++) {
if ((png_in = png_img_load(argv[i])) == FALSE)
exit(3);
if (i == 2) {
png_img_chunk_add_from_png(png_out, png_in, 0);
actl = png_acTL_create(argc - 2, 0);
if ((pc = png_acTL_dump(actl)) == NULL)
exit(4);
png_img_chunk_add(png_out, pc, actl);
}
fctl_in.sequence_number = seq++;
fctl_in.width = png_img_width(png_in);
fctl_in.height = png_img_height(png_in);
fctl_in.delay_num = filename2msec(argv[i]);
fctl_in.delay_den = 1000;
if ((fctl = png_fcTL_create(&fctl_in)) == NULL)
exit(7);
if ((pc = png_fcTL_dump(fctl)) == NULL)
exit(8);
png_img_chunk_add(png_out, pc, fctl);
idat_idx = png_img_chunk_by_type(png_in, "IDAT", 0);
pc = png_in->chunk[idat_idx];
if (i > 2) {
pc = png_fdAT_from_IDAT(pc, seq++);
png_img_chunk_add(png_out, pc, NULL);
}
else {
png_img_chunk_add(png_out, pc, NULL);
}
}
if ((iend = png_IEND_create()) == NULL)
exit (5);
if ((pc = png_IEND_dump(iend)) == NULL)
exit(6);
png_img_chunk_add(png_out, pc, iend);
png_img_print(png_out);
png_img_write(png_out, foutname);
printf("Finished\n");
exit(0);
}