-
Notifications
You must be signed in to change notification settings - Fork 23
/
remng.cc
1283 lines (1085 loc) · 28.7 KB
/
remng.cc
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the Advance project.
*
* Copyright (C) 2002, 2003, 2004, 2005 Andrea Mazzoleni
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "portable.h"
#include "pngex.h"
#include "mngex.h"
#include "except.h"
#include "file.h"
#include "compress.h"
#include "siglock.h"
#include "scroll.h"
#include "lib/endianrw.h"
#include "lib/mng.h"
#include <zlib.h>
#include <iostream>
#include <iomanip>
using namespace std;
int opt_dx;
int opt_dy;
int opt_limit;
bool opt_reduce;
bool opt_expand;
bool opt_noalpha;
shrink_t opt_level;
bool opt_quiet;
bool opt_verbose;
bool opt_scroll;
adv_mng_type opt_type;
bool opt_force;
bool opt_crc;
void clear_line()
{
cout << " \r";
}
adv_scroll_info* analyze_f_mng(adv_fz* f)
{
adv_mng* mng;
unsigned counter;
adv_scroll* scroll;
int dx = 0;
int dy = 0;
mng = adv_mng_init(f);
if (!mng) {
throw error() << "Error in the mng stream";
}
scroll = scroll_init(opt_dx, opt_dy, opt_limit);
counter = 0;
try {
while (1) {
unsigned pix_width;
unsigned pix_height;
unsigned char* pix_ptr;
unsigned pix_pixel;
unsigned pix_scanline;
unsigned char* dat_ptr_ext;
unsigned dat_size;
unsigned char* pal_ptr_ext;
unsigned pal_size;
unsigned tick;
int r;
r = adv_mng_read(mng, &pix_width, &pix_height, &pix_pixel, &dat_ptr_ext, &dat_size, &pix_ptr, &pix_scanline, &pal_ptr_ext, &pal_size, &tick, f);
if (r < 0) {
throw_png_error();
}
if (r > 0)
break;
data_ptr dat_ptr(dat_ptr_ext);
data_ptr pal_ptr(pal_ptr_ext);
scroll_analyze(scroll, pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline);
++counter;
if (opt_verbose) {
int x, y;
scroll_last_get(scroll, &x, &y);
if (dx < abs(x))
dx = abs(x);
if (dy < abs(y))
dy = abs(y);
cout << "Scroll frame " << counter << ", range " << dx << "x" << dy << " \r";
cout.flush();
}
}
} catch (...) {
adv_mng_done(mng);
scroll_done(scroll);
if (opt_verbose) {
cout << endl;
}
throw;
}
adv_mng_done(mng);
if (opt_verbose) {
clear_line();
}
adv_scroll_info* info = scroll_info_init(scroll);
scroll_done(scroll);
return info;
}
adv_scroll_info* analyze_mng(const string& path)
{
adv_fz* f;
adv_scroll_info* info;
f = fzopen(path.c_str(), "rb");
if (!f) {
throw error() << "Failed open for reading " << path;
}
try {
info = analyze_f_mng(f);
} catch (...) {
fzclose(f);
throw;
}
fzclose(f);
return info;
}
adv_scroll_info* analyze_png(int argc, char* argv[])
{
unsigned counter;
adv_scroll* scroll;
int dx = 0;
int dy = 0;
scroll = scroll_init(opt_dx, opt_dy, opt_limit);
counter = 0;
try {
for(int i=0;i<argc;++i) {
adv_fz* f_in;
string path_src = argv[i];
f_in = fzopen(path_src.c_str(), "rb");
if (!f_in) {
throw error() << "Failed open for reading " << path_src;
}
try {
unsigned char* dat_ptr_ext;
unsigned dat_size;
unsigned pix_pixel;
unsigned pix_width;
unsigned pix_height;
unsigned char* pal_ptr_ext;
unsigned pal_size;
unsigned char* pix_ptr;
unsigned pix_scanline;
if (adv_png_read(
&pix_width, &pix_height, &pix_pixel,
&dat_ptr_ext, &dat_size,
&pix_ptr, &pix_scanline,
&pal_ptr_ext, &pal_size,
f_in
) != 0) {
throw_png_error();
}
data_ptr dat_ptr(dat_ptr_ext);
data_ptr pal_ptr(pal_ptr_ext);
scroll_analyze(scroll, pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline);
++counter;
if (opt_verbose) {
int x, y;
scroll_last_get(scroll, &x, &y);
if (dx < abs(x))
dx = abs(x);
if (dy < abs(y))
dy = abs(y);
cout << "Scroll frame " << counter << ", range " << dx << "x" << dy << " \r";
cout.flush();
}
fzclose(f_in);
} catch (...) {
fzclose(f_in);
throw;
}
}
} catch (...) {
scroll_done(scroll);
if (opt_verbose) {
cout << endl;
}
throw;
}
if (opt_verbose) {
clear_line();
}
adv_scroll_info* info = scroll_info_init(scroll);
scroll_done(scroll);
return info;
}
bool is_reducible_image(unsigned img_width, unsigned img_height, unsigned img_pixel, unsigned char* img_ptr, unsigned img_scanline)
{
unsigned char col_ptr[256*3];
unsigned col_count;
unsigned i, j, k;
// if an alpha channel is present th eimage cannot be palettized
if (img_pixel != 3 && !opt_noalpha)
return false;
col_count = 0;
for(i=0;i<img_height;++i) {
unsigned char* p0 = img_ptr + i * img_scanline;
for(j=0;j<img_width;++j) {
for(k=0;k<col_count;++k) {
if (col_ptr[k*3] == p0[0] && col_ptr[k*3+1] == p0[1] && col_ptr[k*3+2] == p0[2])
break;
}
if (k == col_count) {
if (col_count == 256)
return false; /* too many colors */
col_ptr[col_count*3] = p0[0];
col_ptr[col_count*3+1] = p0[1];
col_ptr[col_count*3+2] = p0[2];
++col_count;
}
p0 += img_pixel;
}
}
return true;
}
bool is_reducible_mng(const string& path)
{
bool reducible;
adv_fz* f;
f = fzopen(path.c_str(), "rb");
if (!f) {
throw error() << "Failed open for reading " << path;
}
try {
reducible = true;
adv_mng* mng;
mng = adv_mng_init(f);
if (!mng) {
throw error() << "Error in the mng stream";
}
if (opt_verbose) {
cout << "Checking if the image is reducible\r";
cout.flush();
}
try {
while (reducible) {
unsigned pix_width;
unsigned pix_height;
unsigned char* pix_ptr;
unsigned pix_pixel;
unsigned pix_scanline;
unsigned char* dat_ptr_ext;
unsigned dat_size;
unsigned char* pal_ptr_ext;
unsigned pal_size;
unsigned tick;
int r;
r = adv_mng_read(mng, &pix_width, &pix_height, &pix_pixel, &dat_ptr_ext, &dat_size, &pix_ptr, &pix_scanline, &pal_ptr_ext, &pal_size, &tick, f);
if (r < 0) {
throw_png_error();
}
if (r > 0)
break;
data_ptr dat_ptr(dat_ptr_ext);
data_ptr pal_ptr(pal_ptr_ext);
if (!is_reducible_image(pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline))
reducible = false;
}
} catch (...) {
adv_mng_done(mng);
if (opt_verbose) {
cout << endl;
}
throw;
}
adv_mng_done(mng);
if (opt_verbose) {
clear_line();
}
} catch (...) {
fzclose(f);
throw;
}
fzclose(f);
return reducible;
}
bool is_reducible_png(int argc, char* argv[])
{
bool reducible;
reducible = true;
for(int i=1;i<argc && reducible;++i) {
adv_fz* f_in;
string path_src = argv[i];
f_in = fzopen(path_src.c_str(), "rb");
if (!f_in) {
throw error() << "Failed open for reading " << path_src;
}
try {
unsigned char* dat_ptr_ext;
unsigned dat_size;
unsigned pix_pixel;
unsigned pix_width;
unsigned pix_height;
unsigned char* pal_ptr_ext;
unsigned pal_size;
unsigned char* pix_ptr;
unsigned pix_scanline;
if (adv_png_read(
&pix_width, &pix_height, &pix_pixel,
&dat_ptr_ext, &dat_size,
&pix_ptr, &pix_scanline,
&pal_ptr_ext, &pal_size,
f_in
) != 0) {
throw_png_error();
}
data_ptr dat_ptr(dat_ptr_ext);
data_ptr pal_ptr(pal_ptr_ext);
if (!is_reducible_image(pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline))
reducible = false;
fzclose(f_in);
} catch (...) {
fzclose(f_in);
throw;
}
}
return reducible;
}
void convert_header(adv_mng_write* mng, adv_fz* f, unsigned* fc, unsigned frame_width, unsigned frame_height, unsigned frame_frequency, adv_scroll_info* info, bool alpha)
{
if (info) {
mng_write_header(mng, f, fc, frame_width, frame_height, frame_frequency, info->x, info->y, info->width, info->height, alpha);
} else {
mng_write_header(mng, f, fc, frame_width, frame_height, frame_frequency, 0, 0, 0, 0, alpha);
}
}
void convert_image(adv_mng_write* mng, adv_fz* f_out, unsigned* fc, unsigned pix_width, unsigned pix_height, unsigned pix_pixel, unsigned char* pix_ptr, unsigned pix_scanline, unsigned char* pal_ptr, unsigned pal_size, adv_scroll_coord* scc)
{
if (opt_noalpha && pix_pixel == 4) {
/* convert to 3 bytes per pixel */
unsigned dst_pixel = 3;
unsigned dst_scanline = 3 * pix_width;
data_ptr dst_ptr;
dst_ptr = data_alloc(dst_scanline * pix_height);
unsigned i, j;
for(i=0;i<pix_height;++i) {
const unsigned char* p0 = pix_ptr + i * pix_scanline;
unsigned char* p1 = dst_ptr + i * dst_scanline;
for(j=0;j<pix_width;++j) {
p1[0] = p0[0];
p1[1] = p0[1];
p1[2] = p0[2];
p0 += 4;
p1 += 3;
}
}
convert_image(mng, f_out, fc, pix_width, pix_height, dst_pixel, dst_ptr, dst_scanline, 0, 0, scc);
} else {
if (scc) {
mng_write_image(mng, f_out, fc, pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline, pal_ptr, pal_size, scc->x, scc->y);
} else {
mng_write_image(mng, f_out, fc, pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline, pal_ptr, pal_size, 0, 0);
}
}
}
void convert_f_mng(adv_fz* f_in, adv_fz* f_out, unsigned* filec, unsigned* framec, adv_scroll_info* info, bool reduce, bool expand)
{
unsigned counter;
adv_mng* mng;
adv_mng_write* mng_write;
bool first = true;
mng = adv_mng_init(f_in);
if (!mng) {
throw error() << "Error in the mng stream";
}
mng_write = mng_write_init(opt_type, opt_level, reduce, expand);
if (!mng_write) {
throw error() << "Error in the mng stream";
}
*filec = 0;
counter = 0;
try {
while (1) {
unsigned pix_width;
unsigned pix_height;
unsigned char* pix_ptr;
unsigned pix_pixel;
unsigned pix_scanline;
unsigned char* dat_ptr_ext;
unsigned dat_size;
unsigned char* pal_ptr_ext;
unsigned pal_size;
unsigned tick;
int r;
r = adv_mng_read(mng, &pix_width, &pix_height, &pix_pixel, &dat_ptr_ext, &dat_size, &pix_ptr, &pix_scanline, &pal_ptr_ext, &pal_size, &tick, f_in);
if (r < 0) {
throw_png_error();
}
if (r > 0)
break;
data_ptr dat_ptr(dat_ptr_ext);
data_ptr pal_ptr(pal_ptr_ext);
if (first) {
unsigned frequency = adv_mng_frequency_get(mng);
if (opt_type == mng_vlc && tick!=1) {
// adjust the frequency
frequency = (frequency + tick / 2) / tick;
if (frequency == 0)
frequency = 1;
}
convert_header(mng_write, f_out, filec, adv_mng_width_get(mng), adv_mng_height_get(mng), frequency, info, pix_pixel == 4 && !opt_noalpha);
first = false;
}
if (opt_type != mng_vlc)
mng_write_frame(mng_write, f_out, filec, tick);
if (info) {
if (counter >= info->mac) {
throw error() << "Internal error";
}
convert_image(mng_write, f_out, filec, pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline, pal_ptr, pal_size, &info->map[counter]);
} else {
convert_image(mng_write, f_out, filec, pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline, pal_ptr, pal_size, 0);
}
++counter;
if (opt_verbose) {
cout << "Compressing ";
if (reduce) cout << "and reducing ";
if (expand) cout << "and expanding ";
cout << "frame " << counter << ", size " << *filec << " \r";
cout.flush();
}
}
} catch (...) {
adv_mng_done(mng);
mng_write_done(mng_write);
if (opt_verbose) {
cout << endl;
}
throw;
}
mng_write_footer(mng_write, f_out, filec);
adv_mng_done(mng);
mng_write_done(mng_write);
if (opt_verbose) {
clear_line();
}
*framec = counter;
}
void convert_mng(const string& path_src, const string& path_dst)
{
adv_scroll_info* info;
bool reduce;
bool expand;
if (opt_scroll && opt_type == mng_vlc) {
throw error() << "The --scroll and --vlc options are incompatible";
}
if (opt_scroll && opt_type == mng_lc) {
throw error() << "The --scroll and --lc options are incompatible";
}
if (opt_scroll) {
info = analyze_mng(path_src);
} else {
info = 0;
}
if (opt_reduce) {
reduce = is_reducible_mng(path_src);
} else {
reduce = false;
}
if (opt_expand) {
expand = true;
} else {
expand = false;
}
adv_fz* f_in;
adv_fz* f_out;
unsigned filec;
unsigned framec;
f_in = fzopen(path_src.c_str(), "rb");
if (!f_in) {
throw error() << "Failed open for reading " << path_src;
}
f_out = fzopen(path_dst.c_str(), "wb");
if (!f_out) {
fzclose(f_in);
throw error() << "Failed open for writing " << path_dst;
}
try {
convert_f_mng(f_in, f_out, &filec, &framec, info, reduce, expand);
} catch (...) {
fzclose(f_in);
fzclose(f_out);
remove(path_dst.c_str());
throw;
}
fzclose(f_in);
fzclose(f_out);
if (info)
scroll_info_done(info);
}
void convert_mng_inplace(const string& path)
{
// temp name of the saved file
string path_dst = file_temp(path);
try {
convert_mng(path, path_dst);
} catch (...) {
remove(path_dst.c_str());
throw;
}
unsigned dst_size = file_size(path_dst);
if (!opt_force && file_size(path) < dst_size) {
// delete the new file
remove(path_dst.c_str());
throw error_unsupported() << "Bigger " << dst_size;
} else {
// prevent external signal
sig_auto_lock sal;
// delete the old file
if (remove(path.c_str()) != 0) {
remove(path_dst.c_str());
throw error() << "Failed delete of " << path;
}
// rename the new version with the correct name
if (::rename(path_dst.c_str(), path.c_str()) != 0) {
throw error() << "Failed rename of " << path_dst << " to " << path;
}
}
}
void mng_print(const string& path)
{
unsigned type;
unsigned size;
adv_fz* f_in;
f_in = fzopen(path.c_str(), "rb");
if (!f_in) {
throw error() << "Failed open for reading " << path;
}
try {
if (adv_mng_read_signature(f_in) != 0) {
throw_png_error();
}
do {
unsigned char* data_ext;
if (adv_png_read_chunk(f_in, &data_ext, &size, &type) != 0) {
throw_png_error();
}
data_ptr data(data_ext);
if (opt_crc) {
cout << hex << setw(8) << setfill('0') << crc32(0, data, size);
cout << " ";
cout << dec << setw(0) << setfill(' ') << size;
cout << "\n";
} else {
png_print_chunk(type, data, size);
}
} while (type != ADV_MNG_CN_MEND);
} catch (...) {
fzclose(f_in);
throw;
}
fzclose(f_in);
}
void extract(const string& path_src)
{
adv_fz* f_in;
adv_mng* mng;
adv_fz* f_out;
unsigned counter;
string base;
unsigned first_tick;
base = file_basename(path_src);
f_in = fzopen(path_src.c_str(), "rb");
if (!f_in) {
throw error() << "Failed open for reading " << path_src;
}
mng = adv_mng_init(f_in);
if (!mng) {
throw error() << "Error in the mng stream";
}
counter = 0;
first_tick = 1;
while (1) {
unsigned pix_width;
unsigned pix_height;
unsigned char* pix_ptr;
unsigned pix_pixel;
unsigned pix_scanline;
unsigned char* dat_ptr_ext;
unsigned dat_size;
unsigned char* pal_ptr_ext;
unsigned pal_size;
unsigned tick;
unsigned char* dst_ptr;
unsigned dst_pixel;
unsigned dst_scanline;
int r;
r = adv_mng_read(mng, &pix_width, &pix_height, &pix_pixel, &dat_ptr_ext, &dat_size, &pix_ptr, &pix_scanline, &pal_ptr_ext, &pal_size, &tick, f_in);
if (r < 0) {
throw_png_error();
}
if (r > 0)
break;
data_ptr dat_ptr(dat_ptr_ext);
data_ptr pal_ptr(pal_ptr_ext);
if (counter == 0) {
first_tick = tick;
if (!first_tick)
first_tick = 1;
}
ostringstream path_dst;
path_dst << base << "-";
// not optimal code for g++ 2.95.3
path_dst.setf(ios::right, ios::adjustfield);
path_dst << setw(8) << setfill('0') << counter;
path_dst << ".png";
if (!opt_quiet) {
cout << path_dst.str() << endl;
}
f_out = fzopen(path_dst.str().c_str(), "wb");
if (!f_out) {
fzclose(f_in);
throw error() << "Failed open for writing " << path_dst.str();
}
++counter;
if (!opt_noalpha) {
// convert to 4 byte RGBA format.
// mencoder 0.90 has problems with 3 byte RGB format.
png_convert_4(pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline, pal_ptr, pal_size, &dst_ptr, &dst_pixel, &dst_scanline);
png_write(f_out, pix_width, pix_height, dst_pixel, dst_ptr, dst_scanline, 0, 0, 0, 0, opt_level);
free(dst_ptr);
} else {
png_write(f_out, pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline, pal_ptr, pal_size, 0, 0, opt_level);
}
fzclose(f_out);
}
cout << adv_mng_frequency_get(mng) / (double)first_tick << endl;
if (opt_verbose) {
cout << endl;
cout << "Example mencoder call:" << endl;
cout << "mencoder " << base << "-\\*.png -mf on:w=" << adv_mng_width_get(mng) << ":h=" << adv_mng_height_get(mng) << ":fps=" << adv_mng_frequency_get(mng) / first_tick << ":type=png -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1000:vhq -o " << base << ".avi" << endl;
}
adv_mng_done(mng);
fzclose(f_in);
}
void add_all(int argc, char* argv[], unsigned frequency)
{
unsigned counter;
unsigned filec;
adv_fz* f_out;
string path_dst;
adv_scroll_info* info;
adv_mng_write* mng_write;
bool reduce;
bool expand;
if (argc < 2) {
throw error() << "Missing arguments";
}
if (opt_scroll && opt_type == mng_vlc) {
throw error() << "The --scroll and --vlc options are incompatible";
}
if (opt_scroll && opt_type == mng_lc) {
throw error() << "The --scroll and --lc options are incompatible";
}
if (opt_scroll) {
info = analyze_png(argc - 1, argv + 1);
} else {
info = 0;
}
if (opt_reduce) {
reduce = is_reducible_png(argc - 1, argv + 1);
} else {
reduce = false;
}
if (opt_expand) {
expand = true;
} else {
expand = false;
}
path_dst = argv[0];
f_out = fzopen(path_dst.c_str(), "wb");
if (!f_out) {
throw error() << "Failed open for writing " << path_dst;
}
mng_write = mng_write_init(opt_type, opt_level, reduce, expand);
if (!mng_write) {
throw error() << "Error in the mng stream";
}
filec = 0;
counter = 0;
try {
for(int i=1;i<argc;++i) {
adv_fz* f_in;
string path_src = argv[i];
f_in = fzopen(path_src.c_str(), "rb");
if (!f_in) {
throw error() << "Failed open for reading " << path_src;
}
try {
unsigned char* dat_ptr_ext;
unsigned dat_size;
unsigned pix_pixel;
unsigned pix_width;
unsigned pix_height;
unsigned char* pal_ptr_ext;
unsigned pal_size;
unsigned char* pix_ptr;
unsigned pix_scanline;
if (adv_png_read(
&pix_width, &pix_height, &pix_pixel,
&dat_ptr_ext, &dat_size,
&pix_ptr, &pix_scanline,
&pal_ptr_ext, &pal_size,
f_in
) != 0) {
throw_png_error();
}
data_ptr dat_ptr(dat_ptr_ext);
data_ptr pal_ptr(pal_ptr_ext);
if (!mng_write_has_header(mng_write)) {
convert_header(mng_write, f_out, &filec, pix_width, pix_height, frequency, info, pix_pixel == 4 && !opt_noalpha);
}
if (opt_type != mng_vlc)
mng_write_frame(mng_write, f_out, &filec, 1);
if (info) {
if (counter >= info->mac) {
throw error() << "Internal error";
}
convert_image(mng_write, f_out, &filec, pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline, pal_ptr, pal_size, &info->map[counter]);
} else {
convert_image(mng_write, f_out, &filec, pix_width, pix_height, pix_pixel, pix_ptr, pix_scanline, pal_ptr, pal_size, 0);
}
fzclose(f_in);
++counter;
if (opt_verbose) {
cout << "Compressing ";
if (reduce) cout << "and reducing ";
if (expand) cout << "and expanding ";
cout << "frame " << counter << ", size " << filec << " \r";
cout.flush();
}
} catch (...) {
fzclose(f_in);
throw;
}
}
} catch (...) {
if (opt_verbose) {
cout << endl;
}
fzclose(f_out);
remove(path_dst.c_str());
if (info)
scroll_info_done(info);
throw;
}
mng_write_footer(mng_write, f_out, &filec);
mng_write_done(mng_write);
fzclose(f_out);
if (info)
scroll_info_done(info);
if (opt_verbose) {
clear_line();
}
}
void remng_single(const string& file, unsigned long long& total_0, unsigned long long& total_1)
{
unsigned size_0;
unsigned size_1;
string desc;
if (!file_exists(file)) {
throw error() << "File " << file << " doesn't exist";
}
try {
size_0 = file_size(file);
try {
convert_mng_inplace(file);
} catch (error_unsupported& e) {
desc = e.desc_get();
}
size_1 = file_size(file);
} catch (error& e) {
throw e << " on " << file;
}
if (!opt_quiet) {
cout << setw(12) << size_0 << setw(12) << size_1 << " ";
if (size_0) {
unsigned perc = size_1 * 100LL / size_0;
cout << setw(3) << perc;
} else {
cout << " 0";
}
cout << "% " << file;
if (desc.length())
cout << " (" << desc << ")";
cout << endl;
}
total_0 += size_0;
total_1 += size_1;
}
void remng_all(int argc, char* argv[])
{
unsigned long long total_0 = 0;
unsigned long long total_1 = 0;
for(int i=0;i<argc;++i)
remng_single(argv[i], total_0, total_1);
if (!opt_quiet) {
cout << setw(12) << total_0 << setw(12) << total_1 << " ";
if (total_0) {
unsigned perc = total_1 * 100LL / total_0;
cout << setw(3) << perc;
} else {
cout << " 0";
}
cout << "%" << endl;
}
}
void list_all(int argc, char* argv[])
{