forked from FLIF-hub/FLIF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flif.cpp
1406 lines (1281 loc) · 59.9 KB
/
flif.cpp
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
/* FLIF - Free Lossless Image Format
Copyright (C) 2010-2015 Jon Sneyers & Pieter Wuille
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 3 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, see <http://www.gnu.org/licenses/>.
*/
/*
Parts of this code are based on code from the FFMPEG project, in
particular parts:
- ffv1.c - Copyright (c) 2003 Michael Niedermayer <[email protected]>
- common.h - copyright (c) 2006 Michael Niedermayer <[email protected]>
- rangecoder.c - Copyright (c) 2004 Michael Niedermayer <[email protected]>
*/
#include <string>
#include <string.h>
#include "maniac/rac.h"
#include "maniac/compound.h"
#include "maniac/util.h"
#include "image/color_range.h"
#include "transform/factory.h"
#include "flif_config.h"
#include "getopt.h"
#include <stdarg.h>
static FILE *f; // the compressed file
static std::vector<ColorVal> grey; // a pixel with values in the middle of the bounds
static int64_t pixels_todo = 0;
static int64_t pixels_done = 0;
static int verbosity = 1;
typedef SimpleBitChance FLIFBitChancePass1;
// faster:
//typedef SimpleBitChance FLIFBitChancePass2;
//typedef SimpleBitChance FLIFBitChanceParities;
// better compression:
typedef MultiscaleBitChance<6,SimpleBitChance> FLIFBitChancePass2;
typedef MultiscaleBitChance<6,SimpleBitChance> FLIFBitChanceParities;
typedef MultiscaleBitChance<6,SimpleBitChance> FLIFBitChanceTree;
#define MAX_TRANSFORM 8
const std::vector<std::string> transforms = {"YIQ","BND","ACB","PLT","PLA","FRS","DUP","FRA","???"};
template<typename RAC> void static write_name(RAC& rac, std::string desc)
{
int nb=0;
while (nb <= MAX_TRANSFORM) {
if (transforms[nb] == desc) break;
nb++;
}
if (transforms[nb] != desc) { fprintf(stderr,"ERROR: Unknown transform description string!\n"); return;}
UniformSymbolCoder<RAC> coder(rac);
coder.write_int(0, MAX_TRANSFORM, nb);
}
template<typename RAC> std::string static read_name(RAC& rac)
{
UniformSymbolCoder<RAC> coder(rac);
int nb = coder.read_int(0, MAX_TRANSFORM);
return transforms[nb];
}
void v_printf(const int v, const char *format, ...) {
if (verbosity < v) return;
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
fflush(stdout);
va_end(args);
}
// planes:
// 0 Y channel (luminance)
// 1 I (chroma)
// 2 Q (chroma)
// 3 Alpha (transparency)
/******************************************/
/* scanlines encoding/decoding */
/******************************************/
const int NB_PROPERTIES_scanlines[] = {7,8,9,7};
const int NB_PROPERTIES_scanlinesA[] = {8,9,10,7};
void static initPropRanges_scanlines(Ranges &propRanges, const ColorRanges &ranges, int p)
{
propRanges.clear();
int min = ranges.min(p);
int max = ranges.max(p);
int mind = min - max, maxd = max - min;
if (p != 3) {
for (int pp = 0; pp < p; pp++) {
propRanges.push_back(std::make_pair(ranges.min(pp), ranges.max(pp))); // pixels on previous planes
}
if (ranges.numPlanes()>3) propRanges.push_back(std::make_pair(ranges.min(3), ranges.max(3))); // pixel on alpha plane
}
propRanges.push_back(std::make_pair(min,max)); // guess (median of 3)
propRanges.push_back(std::make_pair(0,3)); // which predictor was it
propRanges.push_back(std::make_pair(mind,maxd));
propRanges.push_back(std::make_pair(mind,maxd));
propRanges.push_back(std::make_pair(mind,maxd));
propRanges.push_back(std::make_pair(mind,maxd));
propRanges.push_back(std::make_pair(mind,maxd));
}
ColorVal predict_and_calcProps_scanlines(Properties &properties, const ColorRanges *ranges, const Image &image, const int p, const uint32_t r, const uint32_t c, ColorVal &min, ColorVal &max) {
ColorVal guess;
int which = 0;
int index=0;
if (p != 3) {
for (int pp = 0; pp < p; pp++) {
properties[index++] = image(pp,r,c);
}
if (image.numPlanes()>3) properties[index++] = image(3,r,c);
}
ColorVal left = (c>0 ? image(p,r,c-1) : grey[p]);;
ColorVal top = (r>0 ? image(p,r-1,c) : grey[p]);
ColorVal topleft = (r>0 && c>0 ? image(p,r-1,c-1) : grey[p]);
ColorVal gradientTL = left + top - topleft;
guess = median3(gradientTL, left, top);
ranges->snap(p,properties,min,max,guess);
if (guess == gradientTL) which = 0;
else if (guess == left) which = 1;
else if (guess == top) which = 2;
properties[index++] = guess;
properties[index++] = which;
if (c > 0 && r > 0) { properties[index++] = left - topleft; properties[index++] = topleft - top; }
else { properties[index++] = 0; properties[index++] = 0; }
if (c+1 < image.cols() && r > 0) properties[index++] = top - image(p,r-1,c+1); // top - topright
else properties[index++] = 0;
if (r > 1) properties[index++] = image(p,r-2,c)-top; // toptop - top
else properties[index++] = 0;
if (c > 1) properties[index++] = image(p,r,c-2)-left; // leftleft - left
else properties[index++] = 0;
return guess;
}
template<typename Coder> void encode_scanlines_inner(std::vector<Coder*> &coders, const Images &images, const ColorRanges *ranges)
{
ColorVal min,max;
long fs = ftell(f);
long pixels = images[0].cols()*images[0].rows()*images.size();
int nump = images[0].numPlanes();
int beginp = (nump>3 ? 3 : 0);
for (int p = beginp, i=0; i++ < nump; p = (p+1)%nump) {
Properties properties((nump>3?NB_PROPERTIES_scanlinesA[p]:NB_PROPERTIES_scanlines[p]));
v_printf(2,"\r%i%% done [%i/%i] ENC[%ux%u] ",(int)(100*pixels_done/pixels_todo),i,nump,images[0].cols(),images[0].rows());
pixels_done += images[0].cols()*images[0].rows();
if (ranges->min(p) >= ranges->max(p)) continue;
for (uint32_t r = 0; r < images[0].rows(); r++) {
for (int fr=0; fr< (int)images.size(); fr++) {
const Image& image = images[fr];
if (image.seen_before >= 0) continue;
uint32_t begin=image.col_begin[r], end=image.col_end[r];
for (uint32_t c = begin; c < end; c++) {
if (nump>3 && p<3 && image(3,r,c) <= 0) continue;
ColorVal guess = predict_and_calcProps_scanlines(properties,ranges,image,p,r,c,min,max);
ColorVal curr = image(p,r,c);
assert(p != 3 || curr >= -fr);
if (p==3 && min < -fr) min = -fr;
coders[p]->write_int(properties, min - guess, max - guess, curr - guess);
}
}
}
long nfs = ftell(f);
if (nfs-fs > 0) {
v_printf(3,"filesize : %li (+%li for %li pixels, %f bpp)", nfs, nfs-fs, pixels, 8.0*(nfs-fs)/pixels );
v_printf(4,"\n");
}
fs = nfs;
}
}
template<typename Rac, typename Coder> void encode_scanlines_pass(Rac &rac, const Images &images, const ColorRanges *ranges, std::vector<Tree> &forest, int repeats)
{
std::vector<Coder*> coders;
for (int p = 0; p < ranges->numPlanes(); p++) {
Ranges propRanges;
initPropRanges_scanlines(propRanges, *ranges, p);
coders.push_back(new Coder(rac, propRanges, forest[p]));
}
while(repeats-- > 0) {
encode_scanlines_inner(coders, images, ranges);
}
for (int p = 0; p < ranges->numPlanes(); p++) {
coders[p]->simplify();
}
for (int p = 0; p < ranges->numPlanes(); p++) {
#ifdef STATS
indent(0); v_printf(2,"Plane %i\n", p);
coders[p]->info(0+1);
#endif
delete coders[p];
}
}
void encode_scanlines_interpol_zero_alpha(Images &images, const ColorRanges *ranges)
{
ColorVal min,max;
int nump = images[0].numPlanes();
if (nump > 3)
for (Image& image : images)
for (int p = 0; p < 3; p++) {
Properties properties((nump>3?NB_PROPERTIES_scanlinesA[p]:NB_PROPERTIES_scanlines[p]));
if (ranges->min(p) >= ranges->max(p)) continue;
// v_printf(2,"[%i] interpol_zero_alpha ",p);
// fflush(stdout);
for (uint32_t r = 0; r < image.rows(); r++) {
for (uint32_t c = 0; c < image.cols(); c++) {
if (image(3,r,c) == 0) {
image.set(p,r,c, predict_and_calcProps_scanlines(properties,ranges,image,p,r,c,min,max));
}
}
}
}
// v_printf(2,"\n");
}
template<typename Coder> void decode_scanlines_inner(std::vector<Coder*> &coders, Images &images, const ColorRanges *ranges)
{
ColorVal min,max;
int nump = images[0].numPlanes();
int beginp = (nump>3 ? 3 : 0);
for (int p = beginp, i=0; i++ < nump; p = (p+1)%nump) {
Properties properties((nump>3?NB_PROPERTIES_scanlinesA[p]:NB_PROPERTIES_scanlines[p]));
v_printf(2,"\r%i%% done [%i/%i] DEC[%ux%u] ",(int)(100*pixels_done/pixels_todo),i,nump,images[0].cols(),images[0].rows());
v_printf(4,"\n");
pixels_done += images[0].cols()*images[0].rows();
if (ranges->min(p) >= ranges->max(p)) continue;
for (uint32_t r = 0; r < images[0].rows(); r++) {
for (int fr=0; fr< (int)images.size(); fr++) {
Image& image = images[fr];
uint32_t begin=image.col_begin[r], end=image.col_end[r];
if (image.seen_before >= 0) { for(uint32_t c=0; c<image.cols(); c++) image.set(p,r,c,images[image.seen_before](p,r,c)); continue; }
if (fr>0) {
for (uint32_t c = 0; c < begin; c++)
if (nump>3 && p<3 && image(3,r,c) == 0) image.set(p,r,c,predict_and_calcProps_scanlines(properties,ranges,image,p,r,c,min,max));
else {
int oldframe=fr-1; image.set(p,r,c,images[oldframe](p,r,c));
while(p == 3 && image(p,r,c) < 0) {oldframe += image(p,r,c); assert(oldframe>=0); image.set(p,r,c,images[oldframe](p,r,c));}
}
} else {
if (nump>3 && p<3) { begin=0; end=image.cols(); }
}
for (uint32_t c = begin; c < end; c++) {
ColorVal guess = predict_and_calcProps_scanlines(properties,ranges,image,p,r,c,min,max);
if (p==3 && min < -fr) min = -fr;
if (nump>3 && p<3 && image(3,r,c) <= 0) { if (image(3,r,c) == 0) image.set(p,r,c,guess); else image.set(p,r,c,images[fr+image(3,r,c)](p,r,c)); continue;}
ColorVal curr = coders[p]->read_int(properties, min - guess, max - guess) + guess;
image.set(p,r,c, curr);
}
if (fr>0) {
for (uint32_t c = end; c < image.cols(); c++)
if (nump>3 && p<3 && image(3,r,c) == 0) image.set(p,r,c,predict_and_calcProps_scanlines(properties,ranges,image,p,r,c,min,max));
else {
int oldframe=fr-1; image.set(p,r,c,images[oldframe](p,r,c));
while(p == 3 && image(p,r,c) < 0) {oldframe += image(p,r,c); assert(oldframe>=0); image.set(p,r,c,images[oldframe](p,r,c));}
}
}
}
}
}
}
template<typename Rac, typename Coder> void decode_scanlines_pass(Rac &rac, Images &images, const ColorRanges *ranges, std::vector<Tree> &forest)
{
std::vector<Coder*> coders;
for (int p = 0; p < images[0].numPlanes(); p++) {
Ranges propRanges;
initPropRanges_scanlines(propRanges, *ranges, p);
coders.push_back(new Coder(rac, propRanges, forest[p]));
}
decode_scanlines_inner(coders, images, ranges);
for (int p = 0; p < images[0].numPlanes(); p++) {
delete coders[p];
}
}
/******************************************/
/* FLIF2 encoding/decoding */
/******************************************/
const int NB_PROPERTIES[] = {8,7,8,8};
const int NB_PROPERTIESA[] = {9,8,9,8};
void static initPropRanges(Ranges &propRanges, const ColorRanges &ranges, int p)
{
propRanges.clear();
int min = ranges.min(p);
int max = ranges.max(p);
int mind = min - max, maxd = max - min;
if (p != 3) { // alpha channel first
for (int pp = 0; pp < p; pp++) {
propRanges.push_back(std::make_pair(ranges.min(pp), ranges.max(pp))); // pixels on previous planes
}
if (ranges.numPlanes()>3) propRanges.push_back(std::make_pair(ranges.min(3), ranges.max(3))); // pixel on alpha plane
}
propRanges.push_back(std::make_pair(mind,maxd)); // neighbor A - neighbor B (top-bottom or left-right)
propRanges.push_back(std::make_pair(min,max)); // guess (median of 3)
propRanges.push_back(std::make_pair(0,3)); // which predictor was it
propRanges.push_back(std::make_pair(mind,maxd));
propRanges.push_back(std::make_pair(mind,maxd));
propRanges.push_back(std::make_pair(mind,maxd));
if (p == 0 || p == 3) {
propRanges.push_back(std::make_pair(mind,maxd));
propRanges.push_back(std::make_pair(mind,maxd));
}
}
// Prediction used for interpolation. Does not have to be the same as the guess used for encoding/decoding.
inline ColorVal predict(const Image &image, int z, int p, uint32_t r, uint32_t c)
{
if (z%2 == 0) { // filling horizontal lines
ColorVal top = image(p,z,r-1,c);
ColorVal bottom = (r+1 < image.rows(z) ? image(p,z,r+1,c) : top); //grey[p]);
ColorVal avg = (top + bottom)/2;
return avg;
} else { // filling vertical lines
ColorVal left = image(p,z,r,c-1);
ColorVal right = (c+1 < image.cols(z) ? image(p,z,r,c+1) : left); //grey[p]);
ColorVal avg = (left + right)/2;
return avg;
}
}
// Actual prediction. Also sets properties. Property vector should already have the right size before calling this.
ColorVal predict_and_calcProps(Properties &properties, const ColorRanges *ranges, const Image &image, const int z, const int p, const uint32_t r, const uint32_t c, ColorVal &min, ColorVal &max) {
ColorVal guess;
int which = 0;
int index = 0;
if (p != 3) {
for (int pp = 0; pp < p; pp++) {
properties[index++] = image(pp,z,r,c);
}
if (image.numPlanes()>3) properties[index++] = image(3,z,r,c);
}
ColorVal left;
ColorVal top;
ColorVal topleft = (r>0 && c>0 ? image(p,z,r-1,c-1) : grey[p]);
ColorVal topright = (r>0 && c+1 < image.cols(z) ? image(p,z,r-1,c+1) : grey[p]);
ColorVal bottomleft = (r+1 < image.rows(z) && c>0 ? image(p,z,r+1,c-1) : grey[p]);
if (z%2 == 0) { // filling horizontal lines
left = (c>0 ? image(p,z,r,c-1) : grey[p]);
top = image(p,z,r-1,c);
ColorVal gradientTL = left + top - topleft;
ColorVal bottom = (r+1 < image.rows(z) ? image(p,z,r+1,c) : top); //grey[p]);
ColorVal gradientBL = left + bottom - bottomleft;
ColorVal avg = (top + bottom)/2;
guess = median3(gradientTL, gradientBL, avg);
ranges->snap(p,properties,min,max,guess);
if (guess == avg) which = 0;
else if (guess == gradientTL) which = 1;
else if (guess == gradientBL) which = 2;
properties[index++] = top-bottom;
} else { // filling vertical lines
left = image(p,z,r,c-1);
top = (r>0 ? image(p,z,r-1,c) : grey[p]);
ColorVal gradientTL = left + top - topleft;
ColorVal right = (c+1 < image.cols(z) ? image(p,z,r,c+1) : left); //grey[p]);
ColorVal gradientTR = right + top - topright;
ColorVal avg = (left + right )/2;
guess = median3(gradientTL, gradientTR, avg);
ranges->snap(p,properties,min,max,guess);
if (guess == avg) which = 0;
else if (guess == gradientTL) which = 1;
else if (guess == gradientTR) which = 2;
properties[index++] = left-right;
}
properties[index++]=guess;
properties[index++]=which;
if (c > 0 && r > 0) { properties[index++]=left - topleft; properties[index++]=topleft - top; }
else { properties[index++]=0; properties[index++]=0; }
if (c+1 < image.cols(z) && r > 0) properties[index++]=top - topright;
else properties[index++]=0;
if (p == 0 || p == 3) {
if (r > 1) properties[index++]=image(p,z,r-2,c)-top; // toptop - top
else properties[index++]=0;
if (c > 1) properties[index++]=image(p,z,r,c-2)-left; // leftleft - left
else properties[index++]=0;
}
return guess;
}
int plane_zoomlevels(const Image &image, const int beginZL, const int endZL) {
return image.numPlanes() * (beginZL - endZL + 1);
}
std::pair<int, int> plane_zoomlevel(const Image &image, const int beginZL, const int endZL, int i) {
assert(i >= 0);
assert(i < plane_zoomlevels(image, beginZL, endZL));
// simple order: interleave planes, zoom in
// int p = i % image.numPlanes();
// int zl = beginZL - (i / image.numPlanes());
// more advanced order: give priority to more important plane(s)
// assumption: plane 0 is Y, plane 1 is I, plane 2 is Q, plane 3 is perhaps alpha, next planes (not used at the moment) are not important
const int max_behind[] = {0, 2, 4, 0, 16, 18, 20, 22};
int np = image.numPlanes();
if (np>7) {
// too many planes, do something simple
int p = i % image.numPlanes();
int zl = beginZL - (i / image.numPlanes());
return std::pair<int, int>(p,zl);
}
std::vector<int> czl(np);
for (int &pzl : czl) pzl = beginZL+1;
int highest_priority_plane = 0;
if (np >= 4) highest_priority_plane = 3; // alpha first
int nextp = highest_priority_plane;
while (i >= 0) {
czl[nextp]--;
i--;
if (i<0) break;
nextp=highest_priority_plane;
for (int p=0; p<np; p++) {
if (czl[p] > czl[highest_priority_plane] + max_behind[p]) {
nextp = p; break;
}
}
// ensure that nextp is not at the most detailed zoomlevel yet
while (czl[nextp] <= endZL) nextp = (nextp+1)%np;
}
int p = nextp;
int zl = czl[p];
return std::pair<int, int>(p,zl);
}
template<typename Coder> void encode_FLIF2_inner(std::vector<Coder*> &coders, const Images &images, const ColorRanges *ranges, const int beginZL, const int endZL)
{
ColorVal min,max;
int nump = images[0].numPlanes();
long fs = ftell(f);
for (int i = 0; i < plane_zoomlevels(images[0], beginZL, endZL); i++) {
std::pair<int, int> pzl = plane_zoomlevel(images[0], beginZL, endZL, i);
int p = pzl.first;
int z = pzl.second;
if (endZL==0) {
v_printf(2,"\r%i%% done [%i/%i] ENC[%i,%ux%u] ",(int) (100*pixels_done/pixels_todo),i,plane_zoomlevels(images[0], beginZL, endZL)-1,p,images[0].cols(z),images[0].rows(z));
}
pixels_done += images[0].cols(z)*images[0].rows(z)/2;
if (ranges->min(p) >= ranges->max(p)) continue;
Properties properties((nump>3?NB_PROPERTIESA[p]:NB_PROPERTIES[p]));
if (z % 2 == 0) {
// horizontal: scan the odd rows, output pixel values
for (uint32_t r = 1; r < images[0].rows(z); r += 2) {
for (int fr=0; fr<(int)images.size(); fr++) {
const Image& image = images[fr];
if (image.seen_before >= 0) { continue; }
uint32_t begin=(image.col_begin[r*image.zoom_rowpixelsize(z)]/image.zoom_colpixelsize(z)),
end=(1+(image.col_end[r*image.zoom_rowpixelsize(z)]-1)/image.zoom_colpixelsize(z));
for (uint32_t c = begin; c < end; c++) {
if (nump>3 && p<3 && image(3,z,r,c) <= 0) continue;
ColorVal guess = predict_and_calcProps(properties,ranges,image,z,p,r,c,min,max);
ColorVal curr = image(p,z,r,c);
if (p==3 && min < -fr) min = -fr;
assert (curr <= max); assert (curr >= min);
coders[p]->write_int(properties, min - guess, max - guess, curr - guess);
}
}
}
} else {
// vertical: scan the odd columns
for (uint32_t r = 0; r < images[0].rows(z); r++) {
for (int fr=0; fr<(int)images.size(); fr++) {
const Image& image = images[fr];
if (image.seen_before >= 0) { continue; }
uint32_t begin=(image.col_begin[r*image.zoom_rowpixelsize(z)]/image.zoom_colpixelsize(z)),
end=(1+(image.col_end[r*image.zoom_rowpixelsize(z)]-1)/image.zoom_colpixelsize(z))|1;
if (begin>1 && ((begin&1) ==0)) begin--;
if (begin==0) begin=1;
for (uint32_t c = begin; c < end; c+=2) {
if (nump>3 && p<3 && image(3,z,r,c) <= 0) continue;
ColorVal guess = predict_and_calcProps(properties,ranges,image,z,p,r,c,min,max);
ColorVal curr = image(p,z,r,c);
if (p==3 && min < -fr) min = -fr;
assert (curr <= max); assert (curr >= min);
coders[p]->write_int(properties, min - guess, max - guess, curr - guess);
}
}
}
}
if (endZL==0 && ftell(f)>fs) {
v_printf(3," wrote %li bytes ", ftell(f));
v_printf(5,"\n");
fs = ftell(f);
}
}
}
template<typename Rac, typename Coder> void encode_FLIF2_pass(Rac &rac, const Images &images, const ColorRanges *ranges, std::vector<Tree> &forest, const int beginZL, const int endZL, int repeats)
{
std::vector<Coder*> coders;
for (int p = 0; p < ranges->numPlanes(); p++) {
Ranges propRanges;
initPropRanges(propRanges, *ranges, p);
coders.push_back(new Coder(rac, propRanges, forest[p]));
}
for (const Image& image : images)
if (beginZL == image.zooms()) {
// special case: very left top pixel must be written first to get it all started
SimpleSymbolCoder<FLIFBitChanceMeta, Rac, 24> metaCoder(rac);
for (int p = 0; p < image.numPlanes(); p++) {
ColorVal curr = image(p,0,0);
metaCoder.write_int(ranges->min(p), ranges->max(p), curr);
}
}
while(repeats-- > 0) {
encode_FLIF2_inner(coders, images, ranges, beginZL, endZL);
}
for (int p = 0; p < images[0].numPlanes(); p++) {
coders[p]->simplify();
}
for (int p = 0; p < images[0].numPlanes(); p++) {
#ifdef STATS
indent(0); v_printf(2,"Plane %i\n", p);
coders[p]->info(0+1);
#endif
delete coders[p];
}
}
void encode_FLIF2_interpol_zero_alpha(Images &images, const ColorRanges *ranges, const int beginZL, const int endZL)
{
for (Image& image : images)
for (int i = 0; i < plane_zoomlevels(image, beginZL, endZL); i++) {
std::pair<int, int> pzl = plane_zoomlevel(image, beginZL, endZL, i);
int p = pzl.first;
int z = pzl.second;
if (p == 3) continue;
// v_printf(2,"[%i] interpol_zero_alpha ",p);
// fflush(stdout);
if (z % 2 == 0) {
// horizontal: scan the odd rows
for (uint32_t r = 1; r < image.rows(z); r += 2) {
for (uint32_t c = 0; c < image.cols(z); c++) {
if (image(3,z,r,c) == 0) image.set(p,z,r,c, predict(image,z,p,r,c));
}
}
} else {
// vertical: scan the odd columns
for (uint32_t r = 0; r < image.rows(z); r++) {
for (uint32_t c = 1; c < image.cols(z); c += 2) {
if (image(3,z,r,c) == 0) image.set(p,z,r,c, predict(image,z,p,r,c));
}
}
}
}
// v_printf(2,"\n");
}
// interpolate rest of the image
// used when decoding lossy
void decode_FLIF2_inner_interpol(Images &images, const ColorRanges *ranges, const int I, const int beginZL, const int endZL, const uint32_t R, const int scale)
{
for (int i = I; i < plane_zoomlevels(images[0], beginZL, endZL); i++) {
std::pair<int, int> pzl = plane_zoomlevel(images[0], beginZL, endZL, i);
int p = pzl.first;
int z = pzl.second;
if ( 1<<(z/2) < scale) continue;
pixels_done += images[0].cols(z)*images[0].rows(z)/2;
v_printf(2,"\r%i%% done [%i/%i] INTERPOLATE[%i,%ux%u] ",(int)(100*pixels_done/pixels_todo),i,plane_zoomlevels(images[0], beginZL, endZL)-1,p,images[0].cols(z),images[0].rows(z));
v_printf(5,"\n");
if (z % 2 == 0) {
// horizontal: scan the odd rows
for (uint32_t r = (I==i?R:1); r < images[0].rows(z); r += 2) {
for (Image& image : images) {
if (image.palette == false) {
for (uint32_t c = 0; c < image.cols(z); c++) {
image.set(p,z,r,c, predict(image,z,p,r,c)); // normal method: use predict() for interpolation
}
} else {
for (uint32_t c = 0; c < image.cols(z); c++) {
image.set(p,z,r,c, image(p,z,r-1,c)); // paletted image: no interpolation
}
}
}
}
} else {
// vertical: scan the odd columns
for (uint32_t r = (I==i?R:0); r < images[0].rows(z); r++) {
for (Image& image : images) {
if (image.palette == false) {
for (uint32_t c = 1; c < image.cols(z); c += 2) {
image.set(p,z,r,c, predict(image,z,p,r,c));
}
} else {
for (uint32_t c = 1; c < image.cols(z); c += 2) {
image.set(p,z,r,c, image(p,z,r,c-1));
}
}
}
}
}
}
v_printf(2,"\n");
}
template<typename Coder> void decode_FLIF2_inner(std::vector<Coder*> &coders, Images &images, const ColorRanges *ranges, const int beginZL, const int endZL, int quality, int scale)
{
ColorVal min,max;
int nump = images[0].numPlanes();
// if (quality >= 0) {
// quality = plane_zoomlevels(image, beginZL, endZL) * quality / 100;
// }
// decode
for (int i = 0; i < plane_zoomlevels(images[0], beginZL, endZL); i++) {
std::pair<int, int> pzl = plane_zoomlevel(images[0], beginZL, endZL, i);
int p = pzl.first;
int z = pzl.second;
if ((100*pixels_done > quality*pixels_todo) || 1<<(z/2) < scale) {
decode_FLIF2_inner_interpol(images, ranges, i, beginZL, endZL, (z%2 == 0 ?1:0), scale);
return;
}
if (endZL == 0) v_printf(2,"\r%i%% done [%i/%i] DEC[%i,%ux%u] ",(int)(100*pixels_done/pixels_todo),i,plane_zoomlevels(images[0], beginZL, endZL)-1,p,images[0].cols(z),images[0].rows(z));
pixels_done += images[0].cols(z)*images[0].rows(z)/2;
if (ranges->min(p) >= ranges->max(p)) continue;
ColorVal curr;
Properties properties((nump>3?NB_PROPERTIESA[p]:NB_PROPERTIES[p]));
if (z % 2 == 0) {
for (uint32_t r = 1; r < images[0].rows(z); r += 2) {
#ifdef CHECK_FOR_BROKENFILES
if (feof(f)) {
v_printf(1,"Row %i: Unexpected file end. Interpolation from now on.\n",r);
decode_FLIF2_inner_interpol(images, ranges, i, beginZL, endZL, (r>1?r-2:r), scale);
return;
}
#endif
for (int fr=0; fr<(int)images.size(); fr++) {
Image& image = images[fr];
if (image.seen_before >= 0) { for (uint32_t c=0; c<image.cols(z); c++) image.set(p,z,r,c,images[image.seen_before](p,z,r,c)); continue; }
uint32_t begin=image.col_begin[r*image.zoom_rowpixelsize(z)]/image.zoom_colpixelsize(z), end=1+(image.col_end[r*image.zoom_rowpixelsize(z)]-1)/image.zoom_colpixelsize(z);
if (fr>0) {
for (uint32_t c = 0; c < begin; c++)
if (nump>3 && p<3 && image(3,z,r,c) == 0) image.set(p,z,r,c, predict(image,z,p,r,c));
else { int oldframe=fr-1; image.set(p,z,r,c,images[oldframe](p,z,r,c));
while(p == 3 && image(p,z,r,c) < 0) {oldframe += image(p,z,r,c); assert(oldframe>=0); image.set(p,z,r,c,images[oldframe](p,z,r,c));}}
for (uint32_t c = end; c < image.cols(z); c++)
if (nump>3 && p<3 && image(3,z,r,c) == 0) image.set(p,z,r,c, predict(image,z,p,r,c));
else { int oldframe=fr-1; image.set(p,z,r,c,images[oldframe](p,z,r,c));
while(p == 3 && image(p,z,r,c) < 0) {oldframe += image(p,z,r,c); assert(oldframe>=0); image.set(p,z,r,c,images[oldframe](p,z,r,c));}}
} else {
if (nump>3 && p<3) { begin=0; end=image.cols(z); }
}
for (uint32_t c = begin; c < end; c++) {
if (nump>3 && p<3 && image(3,z,r,c) <= 0) { if (image(3,z,r,c) == 0) image.set(p,z,r,c,predict(image,z,p,r,c)); else image.set(p,z,r,c,images[fr+image(3,z,r,c)](p,z,r,c)); continue;}
ColorVal guess = predict_and_calcProps(properties,ranges,image,z,p,r,c,min,max);
if (p==3 && min < -fr) min = -fr;
curr = coders[p]->read_int(properties, min - guess, max - guess) + guess;
image.set(p,z,r,c, curr);
}
}
}
} else {
for (uint32_t r = 0; r < images[0].rows(z); r++) {
#ifdef CHECK_FOR_BROKENFILES
if (feof(f)) {
v_printf(1,"Row %i: Unexpected file end. Interpolation from now on.\n", r);
decode_FLIF2_inner_interpol(images, ranges, i, beginZL, endZL, (r>0?r-1:r), scale);
return;
}
#endif
for (int fr=0; fr<(int)images.size(); fr++) {
Image& image = images[fr];
if (image.seen_before >= 0) { for (uint32_t c=1; c<image.cols(z); c+=2) image.set(p,z,r,c,images[image.seen_before](p,z,r,c)); continue; }
uint32_t begin=(image.col_begin[r*image.zoom_rowpixelsize(z)]/image.zoom_colpixelsize(z)),
end=(1+(image.col_end[r*image.zoom_rowpixelsize(z)]-1)/image.zoom_colpixelsize(z))|1;
if (begin>1 && ((begin&1) ==0)) begin--;
if (begin==0) begin=1;
if (fr>0) {
for (uint32_t c = 1; c < begin; c+=2)
if (nump>3 && p<3 && image(3,z,r,c) == 0) image.set(p,z,r,c, predict(image,z,p,r,c));
else { int oldframe=fr-1; image.set(p,z,r,c,images[oldframe](p,z,r,c));
while(p == 3 && image(p,z,r,c) < 0) {oldframe += image(p,z,r,c); assert(oldframe>=0); image.set(p,z,r,c,images[oldframe](p,z,r,c));}}
for (uint32_t c = end; c < image.cols(z); c+=2)
if (nump>3 && p<3 && image(3,z,r,c) == 0) image.set(p,z,r,c, predict(image,z,p,r,c));
else { int oldframe=fr-1; image.set(p,z,r,c,images[oldframe](p,z,r,c));
while(p == 3 && image(p,z,r,c) < 0) {oldframe += image(p,z,r,c); assert(oldframe>=0); image.set(p,z,r,c,images[oldframe](p,z,r,c));}}
} else {
if (nump>3 && p<3) { begin=1; end=image.cols(z); }
}
for (uint32_t c = begin; c < end; c+=2) {
if (nump>3 && p<3 && image(3,z,r,c) <= 0) { if (image(3,z,r,c) == 0) image.set(p,z,r,c,predict(image,z,p,r,c)); else image.set(p,z,r,c,images[fr+image(3,z,r,c)](p,z,r,c)); continue;}
ColorVal guess = predict_and_calcProps(properties,ranges,image,z,p,r,c,min,max);
if (p==3 && min < -fr) min = -fr;
curr = coders[p]->read_int(properties, min - guess, max - guess) + guess;
image.set(p,z,r,c, curr);
}
}
}
}
if (endZL==0) {
v_printf(3," read %li bytes ", ftell(f));
v_printf(5,"\n");
}
}
}
template<typename Rac, typename Coder> void decode_FLIF2_pass(Rac &rac, Images &images, const ColorRanges *ranges, std::vector<Tree> &forest, const int beginZL, const int endZL, int quality, int scale)
{
std::vector<Coder*> coders;
for (int p = 0; p < images[0].numPlanes(); p++) {
Ranges propRanges;
initPropRanges(propRanges, *ranges, p);
coders.push_back(new Coder(rac, propRanges, forest[p]));
}
for (Image& image : images)
if (beginZL == image.zooms()) {
// special case: very left top pixel must be read first to get it all started
SimpleSymbolCoder<FLIFBitChanceMeta, Rac, 24> metaCoder(rac);
for (int p = 0; p < image.numPlanes(); p++) {
image.set(p,0,0, metaCoder.read_int(ranges->min(p), ranges->max(p)));
}
}
decode_FLIF2_inner(coders, images, ranges, beginZL, endZL, quality, scale);
for (int p = 0; p < images[0].numPlanes(); p++) {
delete coders[p];
}
}
/******************************************/
/* General encoding/decoding */
/******************************************/
template<typename BitChance, typename Rac> void encode_tree(Rac &rac, const ColorRanges *ranges, const std::vector<Tree> &forest, const int encoding)
{
for (int p = 0; p < ranges->numPlanes(); p++) {
Ranges propRanges;
if (encoding==1) initPropRanges_scanlines(propRanges, *ranges, p);
else initPropRanges(propRanges, *ranges, p);
MetaPropertySymbolCoder<BitChance, Rac> metacoder(rac, propRanges);
// forest[p].print(stdout);
if (ranges->min(p)<ranges->max(p))
metacoder.write_tree(forest[p]);
}
}
template<typename BitChance, typename Rac> void decode_tree(Rac &rac, const ColorRanges *ranges, std::vector<Tree> &forest, const int encoding)
{
for (int p = 0; p < ranges->numPlanes(); p++) {
Ranges propRanges;
if (encoding==1) initPropRanges_scanlines(propRanges, *ranges, p);
else initPropRanges(propRanges, *ranges, p);
MetaPropertySymbolCoder<BitChance, Rac> metacoder(rac, propRanges);
if (ranges->min(p)<ranges->max(p))
metacoder.read_tree(forest[p]);
// forest[p].print(stdout);
}
}
bool encode(const char* filename, Images &images, std::vector<std::string> transDesc, int encoding, int learn_repeats, int acb, int frame_delay, int palette_size, int lookback) {
if (encoding < 1 || encoding > 2) { fprintf(stderr,"Unknown encoding: %i\n", encoding); return false;}
f = fopen(filename,"w");
fputs("FLIF",f);
int numPlanes = images[0].numPlanes();
int numFrames = images.size();
char c=' '+16*encoding+numPlanes;
if (numFrames>1) c += 32;
fputc(c,f);
if (numFrames>1) {
if (numFrames<255) fputc((char)numFrames,f);
else {
fprintf(stderr,"Too many frames!\n");
}
}
c='1';
for (int p = 0; p < numPlanes; p++) {if (images[0].max(p) != 255) c='2';}
if (c=='2') {for (int p = 0; p < numPlanes; p++) {if (images[0].max(p) != 65535) c='0';}}
fputc(c,f);
Image& image = images[0];
assert(image.cols() <= 0xFFFF);
fputc(image.cols() >> 8,f);
fputc(image.cols() & 0xFF,f);
assert(image.rows() <= 0xFFFF);
fputc(image.rows() >> 8,f);
fputc(image.rows() & 0xFF,f);
RacOut rac(f);
SimpleSymbolCoder<FLIFBitChanceMeta, RacOut, 24> metaCoder(rac);
v_printf(3,"Input: %ux%u, channels:", images[0].cols(), images[0].rows());
for (int p = 0; p < numPlanes; p++) {
assert(image.min(p) == 0);
if (c=='0') {
metaCoder.write_int(1, 16, ilog2(image.max(p)+1));
v_printf(3," [%i] %i bpp",p,ilog2(image.max(p)+1));
}
}
if (c=='1') v_printf(3," %i, depth: 8 bit",numPlanes);
if (c=='2') v_printf(3," %i, depth: 16 bit",numPlanes);
if (numFrames>1) v_printf(3,", frames: %i",numFrames);
v_printf(3,"\n");
if (numFrames>1) {
for (int i=0; i<numFrames; i++) {
metaCoder.write_int(0, 60000, frame_delay); // time in ms between frames
}
}
// metaCoder.write_int(1, 65536, image.cols());
// metaCoder.write_int(1, 65536, image.rows());
// v_printf(2,"Header: %li bytes.\n", ftell(f));
// v_printf(2,"Header: %li bytes.\n", ftell(f));
std::vector<const ColorRanges*> rangesList;
std::vector<Transform*> transforms;
rangesList.push_back(getRanges(image));
int tcount=0;
v_printf(4,"Transforms: ");
for (unsigned int i=0; i<transDesc.size(); i++) {
Transform *trans = create_transform(transDesc[i]);
if (transDesc[i] == "PLT" || transDesc[i] == "PLA") trans->configure(palette_size);
if (transDesc[i] == "FRA") trans->configure(lookback);
if (!trans->init(rangesList.back()) ||
(!trans->process(rangesList.back(), images)
&& !(acb==1 && transDesc[i] == "ACB" && printf(", forced_") && (tcount=0)==0))) {
//fprintf(stderr, "Transform '%s' failed\n", transDesc[i].c_str());
} else {
if (tcount++ > 0) v_printf(4,", ");
v_printf(4,"%s", transDesc[i].c_str());
fflush(stdout);
rac.write(true);
write_name(rac, transDesc[i]);
trans->save(rangesList.back(), rac);
fflush(stdout);
rangesList.push_back(trans->meta(images, rangesList.back()));
trans->data(images);
}
}
if (tcount==0) v_printf(4,"none\n"); else v_printf(4,"\n");
rac.write(false);
const ColorRanges* ranges = rangesList.back();
grey.clear();
for (int p = 0; p < ranges->numPlanes(); p++) grey.push_back((ranges->min(p)+ranges->max(p))/2);
for (int p = 0; p < ranges->numPlanes(); p++) {
v_printf(7,"Plane %i: %i..%i\n",p,ranges->min(p),ranges->max(p));
}
int mbits = 0;
for (int p = 0; p < ranges->numPlanes(); p++) {
if (ranges->max(p) > ranges->min(p)) {
int nBits = ilog2((ranges->max(p) - ranges->min(p))*2-1)+1;
if (nBits > mbits) mbits = nBits;
}
}
int bits = 10; // hardcoding things for 8 bit RGB (which means 9 bit IQ and 10 bit differences)
if (mbits >10) bits=18;
if (mbits > bits) { printf("OOPS: %i > %i\n",mbits,bits); return false;}
pixels_todo = image.rows()*image.cols()*ranges->numPlanes()*(learn_repeats+1);
// two passes
std::vector<Tree> forest(ranges->numPlanes(), Tree());
RacDummy dummy;
if (ranges->numPlanes() > 3) {
v_printf(4,"Replacing fully transparent pixels with predicted pixel values at the other planes\n");
switch(encoding) {
case 1: encode_scanlines_interpol_zero_alpha(images, ranges); break;
case 2: encode_FLIF2_interpol_zero_alpha(images, ranges, image.zooms(), 0); break;
}
}
// not computing checksum until after transformations and potential zero-alpha changes
uint32_t checksum = image.checksum();
long fs = ftell(f);
int roughZL = 0;
if (encoding == 2) {
roughZL = image.zooms() - NB_NOLEARN_ZOOMS-1;
if (roughZL < 0) roughZL = 0;
//v_printf(2,"Encoding rough data\n");
if (bits==10) encode_FLIF2_pass<RacOut, FinalPropertySymbolCoder<FLIFBitChancePass2, RacOut, 10> >(rac, images, ranges, forest, image.zooms(), roughZL+1, 1);
else encode_FLIF2_pass<RacOut, FinalPropertySymbolCoder<FLIFBitChancePass2, RacOut, 18> >(rac, images, ranges, forest, image.zooms(), roughZL+1, 1);
}
//v_printf(2,"Encoding data (pass 1)\n");
if (learn_repeats>1) v_printf(3,"Learning a MANIAC tree. Iterating %i times.\n",learn_repeats);
switch(encoding) {
case 1:
if (bits==10) encode_scanlines_pass<RacDummy, PropertySymbolCoder<FLIFBitChancePass1, RacDummy, 10> >(dummy, images, ranges, forest, learn_repeats);
else encode_scanlines_pass<RacDummy, PropertySymbolCoder<FLIFBitChancePass1, RacDummy, 18> >(dummy, images, ranges, forest, learn_repeats);
break;
case 2:
if (bits==10) encode_FLIF2_pass<RacDummy, PropertySymbolCoder<FLIFBitChancePass1, RacDummy, 10> >(dummy, images, ranges, forest, roughZL, 0, learn_repeats);
else encode_FLIF2_pass<RacDummy, PropertySymbolCoder<FLIFBitChancePass1, RacDummy, 18> >(dummy, images, ranges, forest, roughZL, 0, learn_repeats);
break;
}
v_printf(3,"\rHeader: %li bytes.", fs);
if (encoding==2) v_printf(3," Rough data: %li bytes.", ftell(f)-fs);
fflush(stdout);
//v_printf(2,"Encoding tree\n");
fs = ftell(f);
encode_tree<FLIFBitChanceTree, RacOut>(rac, ranges, forest, encoding);
v_printf(3," MANIAC tree: %li bytes.\n", ftell(f)-fs);
//v_printf(2,"Encoding data (pass 2)\n");
switch(encoding) {
case 1:
if (bits==10) encode_scanlines_pass<RacOut, FinalPropertySymbolCoder<FLIFBitChancePass2, RacOut, 10> >(rac, images, ranges, forest, 1);
else encode_scanlines_pass<RacOut, FinalPropertySymbolCoder<FLIFBitChancePass2, RacOut, 18> >(rac, images, ranges, forest, 1);
break;
case 2:
if (bits==10) encode_FLIF2_pass<RacOut, FinalPropertySymbolCoder<FLIFBitChancePass2, RacOut, 10> >(rac, images, ranges, forest, roughZL, 0, 1);
else encode_FLIF2_pass<RacOut, FinalPropertySymbolCoder<FLIFBitChancePass2, RacOut, 18> >(rac, images, ranges, forest, roughZL, 0, 1);
break;
}
if (numFrames==1)
v_printf(2,"\rEncoding done, %li bytes for %ux%u pixels (%.4fbpp) \n",ftell(f), images[0].cols(), images[0].rows(), 1.0*ftell(f)/images[0].rows()/images[0].cols());
else
v_printf(2,"\rEncoding done, %li bytes for %i frames of %ux%u pixels (%.4fbpp) \n",ftell(f), numFrames, images[0].cols(), images[0].rows(), 1.0*ftell(f)/numFrames/images[0].rows()/images[0].cols());
//v_printf(2,"Writing checksum: %X\n", checksum);
metaCoder.write_int(0, 0xFFFF, checksum / 0x10000);
metaCoder.write_int(0, 0xFFFF, checksum & 0xFFFF);
rac.flush();
fclose(f);
for (int i=transforms.size()-1; i>=0; i--) {
delete transforms[i];
}
transforms.clear();
for (unsigned int i=0; i<rangesList.size(); i++) {
delete rangesList[i];
}
rangesList.clear();
return true;
}
bool decode(const char* filename, Images &images, int quality, int scale)
{
if (scale != 1 && scale != 2 && scale != 4 && scale != 8 && scale != 16 && scale != 32 && scale != 64 && scale != 128) {
fprintf(stderr,"Invalid scale down factor: %i\n", scale);
return false;
}
f = fopen(filename,"r");
if (!f) { fprintf(stderr,"Could not open file: %s\n",filename); return false; }
char buff[5];
if (!fgets(buff,5,f)) { fprintf(stderr,"Could not read header from file: %s\n",filename); return false; }
if (strcmp(buff,"FLIF")) { fprintf(stderr,"Not a FLIF file: %s\n",filename); return false; }
int c = fgetc(f)-' ';
int numFrames=1;
if (c > 47) {
c -= 32;
numFrames = fgetc(f);