-
Notifications
You must be signed in to change notification settings - Fork 6
/
flifcrush.py
executable file
·1668 lines (1315 loc) · 61.5 KB
/
flifcrush.py
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
#!/usr/bin/env python3
# flifcrush - tries to reduce FLIF files in size
# Copyright (C) 2015 - 2017 Matthias Krüger
# 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 1, 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
import sys
if (sys.version_info.major != 3): # no python 3
print("Python 3 or higher is required.")
sys.exit(1)
import subprocess
import os
from PIL import Image # https://github.com/python-pillow/Pillow
from collections import Counter
from collections import namedtuple
import argparse
from itertools import chain # combine ranges
import random # getrandomfilename
import string # getrandomfilename
__author__ = 'Matthias "matthiaskrgr" Krüger'
parser = argparse.ArgumentParser()
parser.add_argument("inpath", help="file or path (recursively) to be converted to flif", metavar='N', nargs='+', type=str)
parser.add_argument("-i", "--interlace", help="force interlacing (default: find out best)", action='store_true')
parser.add_argument("-n", "--nointerlace", help="force interlacing off (default: find out best)", action='store_true')
parser.add_argument("-d", "--debug", help="print output of all runs at end", action='store_true')
#parser.add_argument("-b", "--bruteforce", help="bruteforce compression values, takes AGES and might be outdated", action='store_true')
parser.add_argument("-c", "--compare", help="compare to default flif compression", action='store_true')
args = parser.parse_args()
COMPARE = (args.compare)
DEBUG = (args.debug)
INPATHS = args.inpath
interlace_flag="--no-interlace" # default: false
INTERLACE=False
INTERLACE_FORCE=False
# make these global to access them easily inside functions
global size_before_glob, size_after_glob, files_count_glob, size_flifdefault_glob
size_before_glob = 0 # size of all images we process
size_after_glob = 0 # size of all flifs we generated
files_count_glob = 0 # number of files
size_flifdefault_glob = 0 # size of all images converted with flif default parameters
# colors for stdout
txt_ul = TXT_UL = '\033[04m' # underline
txt_res = TXT_RES = '\033[0m' #reset
if args.interlace:
interlace_flag="--interlace"
INTERLACE=True
INTERLACE_FORCE=True # do we force true or false?
best_interl = True
if args.nointerlace:
interlace_flag="--no-interlace"
INTERLACE=False
INTERLACE_FORCE=True # do we force true or false?
best_interl = False
#BRUTEFORCE = (args.bruteforce)
global output_best
output_best="none"
global arr_index
global progress_array
arr_index = 0
#progress_array="|/-\\"
#progress_array=".o0OOo."
progress_array=" ▁▂▃▄▅▆▇█▇▆▅▄▃▁"
arrlen=len(progress_array)
# prints activity indicator (some kind of ascii 'animation')
def showActivity(func_arg, size_new):
global arr_index
arr_index+=1
if (arr_index == arrlen):
arr_index = 0
# diff_best = best_dict['size'] - size_new
print(progress_array[arr_index] + " " + str(count) + ": " + str(func_arg) + ", size: " + str(size_new) + " b ", end="\r",flush=True)
# save .flif file that had the best combination of parameters
def save_file():
global output_best
flif2flif = False # default, we need extra parameter if we convert .flif to -clif
# if the condition is false, we didn't manage to reduce size
if output_best != "none":
OUTFILE=".".join(INFILE.split(".")[:-1])+".flif" # split by ".", rm last elm, join by "." and add "flif" extension
if (OUTFILE == INFILE): # most likely flif fo flif crushing
flif2flif = True
OUTFILE=get_rand_filename()
with open(OUTFILE, "w+b") as f:
f.write(output_best)
f.close
size_flif=os.path.getsize(OUTFILE)
size_orig=os.path.getsize(INFILE)
if (flif2flif): # overwrite INFILE with OUTFILE
os.remove(INFILE)
os.rename(OUTFILE, INFILE) # rename outfile to infile
# print some numbers
global size_after_glob
size_after_glob += size_flif
size_diff = size_orig - best_dict['size']
print("\033[K", end="")
print("reduced from " + str(size_orig) + " b to "+ str(best_dict['size']) + " ( -"+ str(size_diff) + " b, "+ str((( best_dict['size'] - size_orig)/ size_orig )*100)[:6] + " %) " + str(count) + " flif calls.\n\n")
#print("reduced from {size_orig}b to {size_flif}b ({size_diff}b, {perc_change} %) via \n [{bestoptim}] and {cnt} flif calls.\n\n".format(size_orig = os.path.getsize(INFILE), size_flif=size_flif, size_diff=(size_flif - size_orig), perc_change=str(((size_flif-size_orig) / size_orig)*100)[:6], bestoptim=str("maniac repeats:" + str(best_dict['maniac_repeats']) + " maniac_threshold:" + str(best_dict['maniac_threshold']) + " maniac_min_size:" + str(best_dict['maniac_min_size'])+ " maniac_divisor:" + str(best_dict['maniac_divisor']) + " max_palette_size:" + str(best_dict['max_palette_size']) + " chance-cutoff:" + str(best_dict['chance_cutoff']) + " chance-alpha:" + str(best_dict['chance_alpha']) + " ACB:" + str(best_dict['ACB']) + " INTERLACE:" + str(best_dict['INT']) + " PLC:" + str(best_dict['PLC']) + " RGB:" + str(best_dict['RGB']) + " A:" + str(best_dict['A'])), cnt=str(count)), end="\r")
if (best_dict['size'] > size_orig):
print("WARNING: failed to reduce size")
else:
print("\033[K", end="")
print("WARNING: could not reduce size! (" + str(count) + " attempts)")
# generates a name for a file that does not exist in current directory, used for tmp files
def get_rand_filename():
# this prevents accidentally overwriting a preexisting file
filename =''.join(random.choice(string.ascii_uppercase) for i in range(9))
while (os.path.isfile(filename)): # if the name already exists, try again
filename =''.join(random.choice(string.ascii_uppercase) for i in range(9))
return filename
def pct_of_best(size_new):
# if best size was 100 and new file is 50, return 50 %
pct = str(((size_new - best_dict['size']) / best_dict['size'])*100)
pct = "-0.000" if ("e" in pct) else pct[:6] # due to too-early [:6], '8.509566454608271e-07' would become "8.509"
return pct
def crush_maniac_repeats(): # -N
# globals we modify
global best_dict
global count
global arr_index
global output_best
# locals
range_maniac_repeats = 20 # try 0 -20
max_attempts = 5 # give up after 5 unsuccesfull attempts
failed_attempts = 0
for maniac_repeats in range(0, range_maniac_repeats):
count += 1
raw_command = [
flif_binary,
flif_to_flif,
('--maniac-repeats=' + str(maniac_repeats)), # <-
('--maniac-threshold=' + str(best_dict['maniac_threshold'])),
('--maniac-divisor=' + str(best_dict['maniac_divisor'])),
('--maniac-min-size=' + str(best_dict['maniac_min_size'])),
('--chance-cutoff=' + str(best_dict['chance_cutoff'])),
('--chance-alpha=' + str(best_dict['chance_alpha'])),
('--max-palette-size=' + str(best_dict['max_palette_size'])),
('--guess=' + str(best_dict['guess'])),
('--invisible-guess=' + str(best_dict['invisible_guess'])),
best_dict['interlace'].flag,
best_dict['no_channel_compact'].flag,
best_dict['force_color_buckets'].flag,
best_dict['no_ycocg'].flag,
best_dict['keep_invisible_rgb'].flag,
best_dict['no_subtract_green'].flag,
INFILE,
interlace_flag,
'--overwrite',
'/dev/stdout'
] # = raw_command
sanitized_command = [x for x in raw_command if x ] # remove empty elements, if any
output = subprocess.Popen(sanitized_command, stdout=subprocess.PIPE).stdout.read()
size_new = sys.getsizeof(output)
showActivity("maniac repeats: " + str(maniac_repeats), size_new)
#if (DEBUG):
# debug_array.append([{'Nr':count, 'maniac_repeats':maniac_repeats, 'maniac_threshold':maniac_threshold, 'maniac_min_size':maniac_min_size, 'maniac_divisor':maniac_divisor, 'max_palette_size':max_palette_size, 'ACB':ACB, 'INT': INTERLACE, 'size': size_new}])
if ((best_dict['size'] > size_new)): # new file is smaller // count==1: make sure best_dict is filled with first values we obtain. this way we still continue crushing even if initial N-run does not reduce size smaller than size_orig
failed_attempts = 0 # reset break-counter
output_best = output
size_change = best_dict['size']-size_new
perc_change = pct_of_best(size_new)
print("\033[K", end="")
print(
str(count) +
" maniac [ " + TXT_UL + "repeat: " + str(maniac_repeats) + TXT_RES +
" threshold: " + str(best_dict['maniac_threshold']) +
" min_size: " + str(best_dict['maniac_min_size']) +
" divisor: " + str(best_dict['maniac_divisor']) + " ] " + # ] maniac
" chance:[ cutoff: " + str(best_dict['chance_cutoff']) +
" alpha: " + str(best_dict['chance_alpha']) + " ] " + # ] chance
" palette: " + str(best_dict['max_palette_size']) +
" itlc: " + str(best_dict['interlace'].bool) +
" guess: " + str(best_dict['guess']) +
" inv_guess: " + str(best_dict['invisible_guess']) +
" no_CC: " + str(best_dict['no_channel_compact'].bool) +
" no_subG: " + str(best_dict['no_subtract_green'].bool) +
" Cbuck: " + str(best_dict['force_color_buckets'].bool) +
" no_ycocg: " + str(best_dict['no_ycocg'].bool) +
" inv_rgb: " + str(best_dict['keep_invisible_rgb'].bool) +
" size " + str(size_new) + " b " +
"-" + str(size_change) + " b " +
perc_change + " %")
best_dict['size'] = size_new
best_dict['count'] = count
best_dict['maniac_repeats'] = maniac_repeats
arr_index = 0
else: # file is not smaller
failed_attempts += 1
if (failed_attempts >= max_attempts):
return; # break out of loop, we have wasted enough time here
def crush_maniac_threshold(): # -T
# globals
global best_dict
global count
global arr_index
global output_best
if (best_dict['maniac_repeats'] == 0): # nothing to do here
return
#locals
range_maniac_threshold = 40
max_attempts = 40
failed_attempts = 0
for maniac_threshold in range(1, range_maniac_threshold):
# skip maniac_threshold 1-4, it takes too much ram in extreme cases
if (maniac_threshold <= 4):
continue
count += 1
raw_command = [
flif_binary,
flif_to_flif,
('--maniac-repeats=' + str(best_dict['maniac_repeats'])),
('--maniac-threshold=' + str(maniac_threshold)), # <-
('--maniac-divisor=' + str(best_dict['maniac_divisor'])),
('--maniac-min-size=' + str(best_dict['maniac_min_size'])),
('--chance-cutoff=' + str(best_dict['chance_cutoff'])),
('--chance-alpha=' + str(best_dict['chance_alpha'])),
('--max-palette-size=' + str(best_dict['max_palette_size'])),
('--guess=' + str(best_dict['guess'])),
('--invisible-guess=' + str(best_dict['invisible_guess'])),
best_dict['interlace'].flag,
best_dict['no_channel_compact'].flag,
best_dict['force_color_buckets'].flag,
best_dict['no_ycocg'].flag,
best_dict['keep_invisible_rgb'].flag,
best_dict['no_subtract_green'].flag,
INFILE,
interlace_flag,
'--overwrite',
'/dev/stdout',
] # = raw_command
sanitized_command = [x for x in raw_command if x ] # remove empty elements, if any
output = subprocess.Popen(sanitized_command, stdout=subprocess.PIPE).stdout.read()
size_new = sys.getsizeof(output)
showActivity(("maniac threshold: " + str(maniac_threshold)), size_new)
#if (DEBUG):
# debug_array.append([{'Nr':count, 'maniac_repeats':best_dict['maniac_repeats'], 'maniac_threshold':maniac_threshold, 'maniac_min_size':maniac_min_size, 'maniac_divisor':str(best_dict['maniac_divisor']), 'max_palette_size': max_palette_size, 'ACB':ACB, 'INT': INTERLACE, 'size': size_new}])
if (best_dict['size'] > size_new): # new file is smaller
failed_attempts = 0 # reset break-counter
output_best = output
size_change = best_dict['size']-size_new
perc_change = pct_of_best(size_new)
print("\033[K", end="")
print(
str(count) +
" maniac [ repeat: " + str(best_dict['maniac_repeats']) +
" " + TXT_UL + "threshold: " + str(maniac_threshold) + TXT_RES +
" min_size: " + str(best_dict['maniac_min_size']) +
" divisor: " + str(best_dict['maniac_divisor']) + " ] " + # ] maniac
" chance:[ cutoff: " + str(best_dict['chance_cutoff']) +
" alpha: " + str(best_dict['chance_alpha']) + " ] " + # ] chance
" palette: " + str(best_dict['max_palette_size']) +
" itlc: " + str(best_dict['interlace'].bool) +
" guess: " + str(best_dict['guess']) +
" inv_guess: " + str(best_dict['invisible_guess']) +
" no_CC: " + str(best_dict['no_channel_compact'].bool) +
" no_subG: " + str(best_dict['no_subtract_green'].bool) +
" Cbuck: " + str(best_dict['force_color_buckets'].bool) +
" no_ycocg: " + str(best_dict['no_ycocg'].bool) +
" inv_rgb: " + str(best_dict['keep_invisible_rgb'].bool) +
" size " + str(size_new) + " b " +
"-" + str(size_change) + " b " +
perc_change + " %")
best_dict['size'] = size_new
best_dict['count'] = count
best_dict['maniac_threshold'] = maniac_threshold
arr_index = 0
else:
failed_attempts += 1
if (failed_attempts >= max_attempts):
return;
def crush_maniac_divisor(): # -D
# globals
global best_dict
global count
global arr_index
global output_best
if (best_dict['maniac_repeats'] == 0): # nothing to do here
return
#locals
range_maniac_divisor = 268435455
maniac_divisor = 1
maniac_divisor_step = 1
maniac_divisor_step_upped = 0 # if True; maniac_divisor_step == 10
failed_attempts = 0
max_attempts = 200
while (maniac_divisor < range_maniac_divisor):
count +=1
raw_command = [
flif_binary,
flif_to_flif,
('--maniac-repeats=' + str(best_dict['maniac_repeats'])),
('--maniac-threshold=' + str(best_dict['maniac_threshold'])),
('--maniac-divisor=' + str(maniac_divisor)), # <-
('--maniac-min-size=' + str(best_dict['maniac_min_size'])),
('--chance-cutoff=' + str(best_dict['chance_cutoff'])),
('--chance-alpha=' + str(best_dict['chance_alpha'])),
('--max-palette-size=' + str(best_dict['max_palette_size'])),
('--guess=' + str(best_dict['guess'])),
('--invisible-guess=' + str(best_dict['invisible_guess'])),
best_dict['interlace'].flag,
best_dict['no_channel_compact'].flag,
best_dict['force_color_buckets'].flag,
best_dict['no_ycocg'].flag,
best_dict['keep_invisible_rgb'].flag,
best_dict['no_subtract_green'].flag,
INFILE,
interlace_flag,
'--overwrite',
'/dev/stdout',
] # = raw_command
sanitized_command = [x for x in raw_command if x ] # remove empty elements, if any
output = subprocess.Popen(sanitized_command, stdout=subprocess.PIPE).stdout.read()
size_new = sys.getsizeof(output)
showActivity("maniac divisor: " + str(maniac_divisor), size_new)
#if (DEBUG):
# debug_array.append([{'Nr':count, 'maniac_repeats':str(best_dict['maniac_repeats']), 'maniac_threshold':str(best_dict['maniac_threshold']), 'maniac_min_size':maniac_min_size, 'maniac_divisor':str(best_dict['maniac_divisor']), 'max_palette_size': max_palette_size, 'ACB':ACB, 'INT': INTERLACE, 'size': size_new}])
if (best_dict['size'] > size_new): # new file is smaller
failed_attempts = 0 # reset break-counter
output_best = output
size_change = best_dict['size']-size_new
perc_change = pct_of_best(size_new)
print("\033[K", end="")
print(
str(count) +
" maniac [ repeat: " + str(best_dict['maniac_repeats']) +
" threshold: " + str(best_dict['maniac_threshold']) +
" min_size: " + str(best_dict['maniac_min_size']) +
" " + TXT_UL + "divisor: " + str(maniac_divisor) + TXT_RES + " ] " + # ] maniac <----
" chance:[ cutoff: " + str(best_dict['chance_cutoff']) +
" alpha: " + str(best_dict['chance_alpha']) + " ] " + # ] chance
" palette: " + str(best_dict['max_palette_size']) +
" itlc: " + str(best_dict['interlace'].bool) +
" guess: " + str(best_dict['guess']) +
" inv_guess: " + str(best_dict['invisible_guess']) +
" no_CC: " + str(best_dict['no_channel_compact'].bool) +
" no_subG: " + str(best_dict['no_subtract_green'].bool) +
" Cbuck: " + str(best_dict['force_color_buckets'].bool) +
" no_ycocg: " + str(best_dict['no_ycocg'].bool) +
" inv_rgb: " + str(best_dict['keep_invisible_rgb'].bool) +
" size " + str(size_new) + " b " +
"-" + str(size_change) + " b " +
perc_change + " %")
best_dict['size'] = size_new
best_dict['count'] = count
best_dict['maniac_divisor'] = maniac_divisor
arr_index=0
else:
failed_attempts += 1
if ((maniac_divisor >= 100) and (maniac_divisor_step_upped == 0)): # increase the loop stepping to speed things up
maniac_divisor_step = 10
maniac_divisor_step_upped = 1
if ((maniac_divisor >= 1000) and (maniac_divisor_step_upped == 1)):
maniac_divisor_step = 100
maniac_divisor_step_upped = 2
if ((maniac_divisor >= 5000) and (maniac_divisor_step_upped == 2)):
maniac_divisor_step = 1000
maniac_divisor_step_upped = 3
if ((maniac_divisor >= 13000) and (maniac_divisor_step_upped == 3)):
maniac_divisor_step = 10000
maniac_divisor_step_upped = 4
if (failed_attempts >= max_attempts):
if (maniac_divisor < 268435453): # try max maniac_divisor
maniac_divisor = 268435454
continue
break;
if (maniac_divisor >= range_maniac_divisor):
break
maniac_divisor += maniac_divisor_step
def crush_maniac_min_size(): # -M
# globals
global best_dict
global count
global arr_index
global output_best
if (best_dict['maniac_repeats'] == 0): # nothing to do here
return
#locals
range_maniac_min_size = 3000
max_attempts = 200
failed_attempts = 0
for maniac_min_size in range(0, range_maniac_min_size):
count +=1
#if (DEBUG):
# debug_array.append([{'Nr':count, 'maniac_repeats':str(best_dict['maniac_repeats']), 'maniac_threshold':str(best_dict['maniac_threshold']), 'maniac_min_size':maniac_min_size, 'maniac_divisor':str(best_dict['maniac_divisor']), 'max_palette_size': max_palette_size, 'ACB':ACB, 'INT': INTERLACE, 'size': size_new}])
raw_command = [
flif_binary,
flif_to_flif,
('--maniac-repeats=' + str(best_dict['maniac_repeats'])),
('--maniac-threshold=' + str(best_dict['maniac_threshold'])),
('--maniac-divisor=' + str(best_dict['maniac_divisor'])),
('--maniac-min-size=' + str(maniac_min_size)), # <-
('--chance-cutoff=' + str(best_dict['chance_cutoff'])),
('--chance-alpha=' + str(best_dict['chance_alpha'])),
('--max-palette-size=' + str(best_dict['max_palette_size'])),
('--guess=' + str(best_dict['guess'])),
('--invisible-guess=' + str(best_dict['invisible_guess'])),
best_dict['interlace'].flag,
best_dict['no_channel_compact'].flag,
best_dict['force_color_buckets'].flag,
best_dict['no_ycocg'].flag,
best_dict['keep_invisible_rgb'].flag,
best_dict['no_subtract_green'].flag,
INFILE,
interlace_flag,
'--overwrite',
'/dev/stdout',
] # = raw_command
sanitized_command = [x for x in raw_command if x ] # remove empty elements, if any
output = subprocess.Popen(sanitized_command, stdout=subprocess.PIPE).stdout.read()
size_new = sys.getsizeof(output)
showActivity("maniac min size: " + str(maniac_min_size), size_new)
#if (DEBUG):
# debug_array.append([{'Nr':count, 'maniac_repeats':str(best_dict['maniac_repeats']), 'maniac_threshold':str(best_dict['maniac_threshold']), 'maniac_min_size':maniac_min_size, 'maniac_divisor':str(best_dict['maniac_divisor']), 'max_palette_size': max_palette_size, 'ACB':ACB, 'INT': INTERLACE, 'size': size_new}])
if (best_dict['size'] > size_new): # new file is smaller
failed_attempts = 0 # reset break-counter
output_best = output
size_change = best_dict['size']-size_new
perc_change = pct_of_best(size_new)
print("\033[K", end="")
print(
str(count) +
" maniac [ repeat: " + str(best_dict['maniac_repeats']) +
" threshold: " + str(best_dict['maniac_threshold']) +
" " + TXT_UL + "min_size: " + str(maniac_min_size) + TXT_RES + # <----
" divisor: " + str(best_dict['maniac_divisor']) + " ] " + # ] maniac
" chance:[ cutoff: " + str(best_dict['chance_cutoff']) +
" alpha: " + str(best_dict['chance_alpha']) + " ] " + # ] chance
" palette: " + str(best_dict['max_palette_size']) +
" itlc: " + str(best_dict['interlace'].bool) +
" guess: " + str(best_dict['guess']) +
" inv_guess: " + str(best_dict['invisible_guess']) +
" no_CC: " + str(best_dict['no_channel_compact'].bool) +
" no_subG: " + str(best_dict['no_subtract_green'].bool) +
" Cbuck: " + str(best_dict['force_color_buckets'].bool) +
" no_ycocg: " + str(best_dict['no_ycocg'].bool) +
" inv_rgb: " + str(best_dict['keep_invisible_rgb'].bool) +
" size " + str(size_new) + " b " +
"-" + str(size_change) + " b " +
perc_change + " %")
best_dict['maniac_min_size'] = maniac_min_size
best_dict['size'] = size_new
best_dict['count'] = count
failed_attempts = 0
arr_index = 0
else:
failed_attempts += 1
if (failed_attempts >= max_attempts):
break;
def crush_chance_cutoff():
# globals
global best_dict
global count
global arr_index
global output_best
#locals
range_chance_cutoff = 128
failed_attempts = 0
max_attempts=200
for chance_cutoff in range(1, range_chance_cutoff):
count += 1
raw_command = [
flif_binary,
flif_to_flif,
('--maniac-repeats=' + str(best_dict['maniac_repeats'])),
('--maniac-threshold=' + str(best_dict['maniac_threshold'])),
('--maniac-divisor=' + str(best_dict['maniac_divisor'])),
('--maniac-min-size=' + str(best_dict['maniac_min_size'])),
('--chance-cutoff=' + str(chance_cutoff)), # <-
('--chance-alpha=' + str(best_dict['chance_alpha'])),
('--max-palette-size=' + str(best_dict['max_palette_size'])),
('--guess=' + str(best_dict['guess'])),
('--invisible-guess=' + str(best_dict['invisible_guess'])),
best_dict['interlace'].flag,
best_dict['no_channel_compact'].flag,
best_dict['force_color_buckets'].flag,
best_dict['no_ycocg'].flag,
best_dict['keep_invisible_rgb'].flag,
best_dict['no_subtract_green'].flag,
INFILE,
interlace_flag,
'--overwrite',
'/dev/stdout',
] # = raw_command
sanitized_command = [x for x in raw_command if x ] # remove empty elements, if any
output = subprocess.Popen(sanitized_command, stdout=subprocess.PIPE).stdout.read()
size_new = sys.getsizeof(output)
showActivity("chance cutoff: " + str(chance_cutoff), size_new)
#if (DEBUG):
# debug_array.append([{'Nr':count, 'maniac_repeats':str(best_dict['maniac_repeats']), 'maniac_threshold':str(best_dict['maniac_threshold']), 'maniac_min_size':maniac_min_size, 'maniac_divisor':str(best_dict['maniac_divisor']), 'max_palette_size':max_palette_size, 'ACB':ACB, 'INT': INTERLACE, 'size': size_new}])
if (best_dict['size'] > size_new): # new file is smaller
failed_attempts = 0 # reset break-counter
output_best = output
size_change = best_dict['size']-size_new
perc_change = pct_of_best(size_new)
print("\033[K", end="")
print(
str(count) +
" maniac [ repeat: " + str(best_dict['maniac_repeats']) +
" threshold: " + str(best_dict['maniac_threshold']) +
" min_size: " + str(best_dict['maniac_min_size']) +
" divisor: " + str(best_dict['maniac_divisor']) + " ] " + # ] maniac
" chance:[ "+ TXT_UL + "cutoff: " + str(chance_cutoff) + TXT_RES + # <----
" alpha: " + str(best_dict['chance_alpha']) + " ] " + # ] chance
" palette: " + str(best_dict['max_palette_size']) +
" itlc: " + str(best_dict['interlace'].bool) +
" guess: " + str(best_dict['guess']) +
" inv_guess: " + str(best_dict['invisible_guess']) +
" no_CC: " + str(best_dict['no_channel_compact'].bool) +
" no_subG: " + str(best_dict['no_subtract_green'].bool) +
" Cbuck: " + str(best_dict['force_color_buckets'].bool) +
" no_ycocg: " + str(best_dict['no_ycocg'].bool) +
" inv_rgb: " + str(best_dict['keep_invisible_rgb'].bool) +
" size " + str(size_new) + " b " +
"-" + str(size_change) + " b " +
perc_change + " %")
best_dict['chance_cutoff'] = chance_cutoff
best_dict['size'] = size_new
best_dict['count'] = count
failed_attempts = 0
arr_index = 0
else:
failed_attempts += 1
if (failed_attempts >= max_attempts):
break;
def crush_chance_alpha(): # -Z
# globals
global best_dict
global count
global arr_index
global output_best
#locals
range_chance_alpha = 128
failed_attempts = 0
max_attempts=200
for chance_alpha in range(2, range_chance_alpha):
count += 1
raw_command = [
flif_binary,
flif_to_flif,
('--maniac-repeats=' + str(best_dict['maniac_repeats'])),
('--maniac-threshold=' + str(best_dict['maniac_threshold'])),
('--maniac-divisor=' + str(best_dict['maniac_divisor'])),
('--maniac-min-size=' + str(best_dict['maniac_min_size'])),
('--chance-cutoff=' + str(best_dict['chance_cutoff'])),
('--chance-alpha=' + str(chance_alpha)), # <-
('--max-palette-size=' + str(best_dict['max_palette_size'])),
('--guess=' + str(best_dict['guess'])),
('--invisible-guess=' + str(best_dict['invisible_guess'])),
best_dict['interlace'].flag,
best_dict['no_channel_compact'].flag,
best_dict['force_color_buckets'].flag,
best_dict['no_ycocg'].flag,
best_dict['keep_invisible_rgb'].flag,
best_dict['no_subtract_green'].flag,
INFILE,
interlace_flag,
'--overwrite',
'/dev/stdout',
] # = raw_command
sanitized_command = [x for x in raw_command if x ] # remove empty elements, if any
output = subprocess.Popen(sanitized_command, stdout=subprocess.PIPE).stdout.read()
size_new = sys.getsizeof(output)
showActivity("chance alpha: " + str(chance_alpha), size_new)
#if (DEBUG):
# debug_array.append([{'Nr':count, 'maniac_repeats':str(best_dict['maniac_repeats']), 'maniac_threshold':str(best_dict['maniac_threshold']), 'maniac_min_size':maniac_min_size, 'maniac_divisor':str(best_dict['maniac_divisor']), 'max_palette_size':max_palette_size, 'ACB':ACB, 'INT': INTERLACE, 'size': size_new}])
if (best_dict['size'] > size_new): # new file is smaller
failed_attempts = 0 # reset break-counter
output_best = output
size_change = best_dict['size']-size_new
perc_change = pct_of_best(size_new)
print("\033[K", end="")
print(
str(count) +
" maniac [ repeat: " + str(best_dict['maniac_repeats']) +
" threshold: " + str(best_dict['maniac_threshold']) +
" min_size: " + str(best_dict['maniac_min_size']) +
" divisor: " + str(best_dict['maniac_divisor']) + " ] " + # ] maniac
" chance:[ cutoff: " + str(best_dict['chance_cutoff']) +
" "+ TXT_UL + "alpha: " + str(chance_alpha) + TXT_RES + " ] " + # ] chance # <---.
" palette: " + str(best_dict['max_palette_size']) +
" itlc: " + str(best_dict['interlace'].bool) +
" guess: " + str(best_dict['guess']) +
" inv_guess: " + str(best_dict['invisible_guess']) +
" no_CC: " + str(best_dict['no_channel_compact'].bool) +
" no_subG: " + str(best_dict['no_subtract_green'].bool) +
" Cbuck: " + str(best_dict['force_color_buckets'].bool) +
" no_ycocg: " + str(best_dict['no_ycocg'].bool) +
" inv_rgb: " + str(best_dict['keep_invisible_rgb'].bool) +
" size " + str(size_new) + " b " +
"-" + str(size_change) + " b " +
perc_change + " %")
best_dict['chance_alpha'] = chance_alpha
best_dict['size'] = size_new
best_dict['count'] = count
failed_attempts = 0
arr_index = 0
else:
failed_attempts += 1
if (failed_attempts >= max_attempts):
break;
def crush_palette():
# globals
global best_dict
global count
global arr_index
global output_best
#locals
failed_attempts = 0
max_attempts=200
range1 = range(-11, 11)
range2 = range(inf['colors']-5, inf['colors']+10)
range3 = range(-inf['colors'], 10-inf['colors']) # negative
max_palette_size_range = set(chain(range1, range2, range3))
for max_palette_size in max_palette_size_range:
if ((max_palette_size < -32000) or (max_palette_size > 32000)) : # apparently negative values are ok
continue
count +=1
raw_command = [
flif_binary,
flif_to_flif,
('--maniac-repeats=' + str(best_dict['maniac_repeats'])),
('--maniac-threshold=' + str(best_dict['maniac_threshold'])),
('--maniac-divisor=' + str(best_dict['maniac_divisor'])),
('--maniac-min-size=' + str(best_dict['maniac_min_size'])),
('--chance-cutoff=' + str(best_dict['chance_cutoff'])),
('--chance-alpha=' + str(best_dict['chance_alpha'])),
('--max-palette-size=' + str(max_palette_size)), #<-
('--guess=' + str(best_dict['guess'])),
('--invisible-guess=' + str(best_dict['invisible_guess'])),
best_dict['interlace'].flag,
best_dict['no_channel_compact'].flag,
best_dict['force_color_buckets'].flag,
best_dict['no_ycocg'].flag,
best_dict['keep_invisible_rgb'].flag,
best_dict['no_subtract_green'].flag,
INFILE,
interlace_flag,
'--overwrite',
'/dev/stdout',
] # = raw_command
sanitized_command = [x for x in raw_command if x ] # remove empty elements, if any
output = subprocess.Popen(sanitized_command, stdout=subprocess.PIPE).stdout.read()
size_new = sys.getsizeof(output)
showActivity(("max palette size: " + str(max_palette_size)), size_new)
#if (DEBUG):
# debug_array.append([{'Nr':count, 'maniac_repeats':str(best_dict['maniac_repeats']), 'maniac_threshold':str(best_dict['maniac_threshold']), 'maniac_min_size':str(best_dict['maniac_min_size']), 'maniac_divisor':str(best_dict['maniac_divisor']), 'max_palette_size':max_palette_size, 'ACB':ACB, 'INT': INTERLACE, 'size': size_new}])
# crappy fix for #15 ; don't compare against size_orig at beginning
smaller_than_global = (best_dict['size'] > size_new)
smaller_than_flif_internal = (best_dict['size_flif_internal'] > size_new)
# do the smaller_than_flif_internal check is needed because palette pass is as first pass
# so we have to ignore it when we passed the initial iteration through all passes
if (smaller_than_global) or ((it==0) and smaller_than_flif_internal): # new file is smaller
failed_attempts = 0 # reset break-counter
output_best = output
if (smaller_than_global):
size_change = best_dict['size']-size_new
perc_change = pct_of_best(size_new)
print("\033[K", end="")
print(
str(count) +
" maniac [ repeat: " + str(best_dict['maniac_repeats']) +
" threshold: " + str(best_dict['maniac_threshold']) +
" min_size: " + str(best_dict['maniac_min_size']) +
" divisor: " + str(best_dict['maniac_divisor']) + " ] " + # ] maniac
" chance:[ cutoff: " + str(best_dict['chance_cutoff']) +
" alpha: " + str(best_dict['chance_alpha']) + " ] " + # ] chance
" "+ TXT_UL + "palette: " + str(max_palette_size) + TXT_RES + # <---.
" itlc: " + str(best_dict['interlace'].bool) +
" guess: " + str(best_dict['guess']) +
" inv_guess: " + str(best_dict['invisible_guess']) +
" no_CC: " + str(best_dict['no_channel_compact'].bool) +
" no_subG: " + str(best_dict['no_subtract_green'].bool) +
" Cbuck: " + str(best_dict['force_color_buckets'].bool) +
" no_ycocg: " + str(best_dict['no_ycocg'].bool) +
" inv_rgb: " + str(best_dict['keep_invisible_rgb'].bool) +
" size " + str(size_new) + " b " +
"-" + str(size_change) + " b " +
perc_change + " %")
if (size_new < best_dict['size_flif_internal']):
best_dict['size_flif_internal'] = size_new
best_dict['max_palette_size'] = max_palette_size
best_dict['size'] = size_new
best_dict['count'] = count
failed_attempts = 0
arr_index = 0
else:
failed_attempts += 1
if (failed_attempts >= max_attempts):
break;
def crush_keep_invisible_rgb():
# globals
global best_dict
global count
global arr_index
global output_best
for keep_invisible_rgb in True, False:
count +=1
flagstr = ("--keep-invisible-rgb" if (keep_invisible_rgb) else "")
raw_command = [
flif_binary,
flif_to_flif,
('--maniac-repeats=' + str(best_dict['maniac_repeats'])),
('--maniac-threshold=' + str(best_dict['maniac_threshold'])),
('--maniac-divisor=' + str(best_dict['maniac_divisor'])),
('--maniac-min-size=' + str(best_dict['maniac_min_size'])),
('--chance-cutoff=' + str(best_dict['chance_cutoff'])),
('--chance-alpha=' + str(best_dict['chance_alpha'])),
('--max-palette-size=' + str(best_dict['max_palette_size'])),
('--guess=' + str(best_dict['guess'])),
('--invisible-guess=' + str(best_dict['invisible_guess'])),
best_dict['interlace'].flag,
best_dict['no_channel_compact'].flag,
best_dict['force_color_buckets'].flag,
best_dict['no_ycocg'].flag,
flagstr,
best_dict['no_subtract_green'].flag,
INFILE,
interlace_flag,
'--overwrite',
'/dev/stdout',
] # = raw_command
sanitized_command = [x for x in raw_command if x ] # remove empty elements, if any
output = subprocess.Popen(sanitized_command, stdout=subprocess.PIPE).stdout.read()
size_new = sys.getsizeof(output)
showActivity("keep invisibler rgb: " + str(keep_invisible_rgb), size_new)
#if (DEBUG):
# debug_array.append([{'Nr':count, 'maniac_repeats':str(best_dict['maniac_repeats']), 'maniac_threshold':str(best_dict['maniac_threshold']), 'maniac_min_size':str(best_dict['maniac_min_size']), 'maniac_divisor':str(best_dict['maniac_divisor']), 'max_palette_size':max_palette_size, 'ACB':ACB, 'INT': INTERLACE, 'size': size_new}])
if (best_dict['size'] > size_new): # new file is smaller
output_best = output
size_change = best_dict['size']-size_new
perc_change = pct_of_best(size_new)
best_dict['keep_invisible_rgb'] = best_dict['keep_invisible_rgb']._replace(flag=flagstr)
best_dict['keep_invisible_rgb'] = best_dict['keep_invisible_rgb']._replace(bool=keep_invisible_rgb)
print("\033[K", end="")
print(
str(count) +
" maniac [ repeat: " + str(best_dict['maniac_repeats']) +
" threshold: " + str(best_dict['maniac_threshold']) +
" min_size: " + str(best_dict['maniac_min_size']) +
" divisor: " + str(best_dict['maniac_divisor']) + " ] " + # ] maniac
" chance:[ cutoff: " + str(best_dict['chance_cutoff']) +
" alpha: " + str(best_dict['chance_alpha']) + " ] " + # ] chance
" palette: " + str(best_dict['max_palette_size']) +
" itlc: " + str(best_dict['interlace'].bool) +
" guess: " + str(best_dict['guess']) +
" inv_guess: " + str(best_dict['invisible_guess']) +
" no_CC: " + str(best_dict['no_channel_compact'].bool) +
" no_subG: " + str(best_dict['no_subtract_green'].bool) +
" Cbuck: " + str(best_dict['force_color_buckets'].bool) +
" no_ycocg: " + str(best_dict['no_ycocg'].bool) +
" " + TXT_UL + "inv_rgb: " + str(best_dict['keep_invisible_rgb'].bool) + TXT_RES + # < ------
" size " + str(size_new) + " b " +
"-" + str(size_change) + " b " +
perc_change + " %")
best_dict['size'] = size_new
best_dict['count'] = count
arr_index = 0
def crush_force_color_buckets():
# globals
global best_dict
global count
global arr_index
global output_best
for force_color_buckets in True, False:
count +=1
flagstr = ("--force-color-buckets" if (force_color_buckets) else "--no-color-buckets")
raw_command = [
flif_binary,
flif_to_flif,
('--maniac-repeats=' + str(best_dict['maniac_repeats'])),
('--maniac-threshold=' + str(best_dict['maniac_threshold'])),
('--maniac-divisor=' + str(best_dict['maniac_divisor'])),
('--maniac-min-size=' + str(best_dict['maniac_min_size'])),
('--chance-cutoff=' + str(best_dict['chance_cutoff'])),
('--chance-alpha=' + str(best_dict['chance_alpha'])),
('--max-palette-size=' + str(best_dict['max_palette_size'])),
('--guess=' + str(best_dict['guess'])),
('--invisible-guess=' + str(best_dict['invisible_guess'])),
best_dict['interlace'].flag,
best_dict['no_channel_compact'].flag,
best_dict['force_color_buckets'].flag,
flagstr,
best_dict['keep_invisible_rgb'].flag,
best_dict['no_subtract_green'].flag,
INFILE,
interlace_flag,
'--overwrite',