-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvertron.py
executable file
·2864 lines (2305 loc) · 72.9 KB
/
convertron.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
"""
Convertron3000 Commodore 64 graphics converter
Copyright (C) 2024 fieserWolF / Abyss-Connection
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/>.
For futher questions, please contact me at
http://csdb.dk/scener/?id=3623
or
wolf@abyss-connection.de
For Python3, The Python Imaging Library (PIL), Numpy, Tcl/Tk and other used source licenses see file "LICENSE_OTHERS".
"""
import os
import sys
import hitherdither
import struct
from PIL import ImageTk, ImageEnhance, ImageFilter, ImageDraw
import PIL.Image as PilImage #we need another name, as it collides with tkinter.Image otherwise
from tkinter import *
from tkinter.filedialog import askopenfilename, asksaveasfilename
import json
#global constants
def _global_constants():
return None
#BGCOLOR="#ff0000"
BGCOLOR="#d9d9d9"
PROGNAME = 'CONVERTRON3000';
C64_CHAR_HEIGHT=25 #200/8
C64_CHAR_WIDTH=40 #320/8
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
RES_VERSION = resource_path('resources/version.txt')
RES_GFX_ICON = resource_path('resources/icon.png')
RES_GFX_ABOUT = resource_path('resources/about.png')
RES_DOC_ABOUT = resource_path('resources/about.txt')
VERSION = open(RES_VERSION, encoding="utf_8").read().rstrip()
_padx = 2
_pady = 2
_bd = 4
KOALA_WIDTH = 160
KOALA_HEIGHT = 200
HIRES_WIDTH = 320
HIRES_HEIGHT = 200
PALETTEDATA_PEPTO = (
0, 0, 0, # 0 black
255, 255, 255, # 1 white
104, 55, 43, # 2 red
112, 164, 178, # 3 cyan
111, 61, 134, # 4 purple
88, 141, 67, # 5 green
53, 40, 121, # 6 blue
184, 199, 111, # 7 yellow
111, 79, 37, # 8 orange
67, 57, 0, # 9 brown
154, 103, 89, # a pink
68, 68, 68, # b dark gray
108, 108, 108, # c gray
154, 210, 132, # d light green
108, 94, 181, # e light blue
149, 149, 149, # f light gray
)
PALETTEDATA_VIEW64 = (
0, 0, 0, # 0 black
250, 250, 250, # 1 white
127, 39, 52, # 2 red
111, 192, 180, # 3 cyan
135, 60, 149, # 4 purple
86, 156, 73, # 5 green
62, 62, 142, # 6 blue
187, 187, 106, # 7 yellow
134, 61, 39, # 8 orange
85, 44, 0, # 9 brown
174, 86, 99, # a pink
78, 78, 78, # b dark gray
117, 117, 117, # c gray
148, 218, 135, # d light green
117, 117, 197, # e light blue
156, 156, 156 # f light gray
)
PALETTEDATA_VICE = (
0, 0, 0, # 0 black
255, 255, 255, # 1 white
146, 74, 64, # 2 red
132, 197, 204, # 3 cyan
147, 81, 182, # 4 purple
114, 177, 75, # 5 green
72, 58, 170, # 6 blue
213, 223, 124, # 7 yellow
153, 105, 45, # 8 orange
103, 82, 0, # 9 brown
193, 129, 120, # a pink
92, 92, 92, # b dark gray
151, 151, 151, # c gray
179, 236, 145, # d light green
135, 139, 221, # e light blue
200, 200, 200 # f light gray
)
PALETTEDATA_COLODORE = (
0, 0, 0, # 0 black
255, 255, 255, # 1 white
129, 51, 56, # 2 red
117, 206, 200, # 3 cyan
142, 60, 151, # 4 purple
86, 172, 77, # 5 green
46, 44, 155, # 6 blue
237, 241, 113, # 7 yellow
142, 80, 41, # 8 orange
85, 56, 0, # 9 brown
196, 108, 113, # a pink
74, 74, 74, # b dark gray
123, 123, 123, # c gray
169, 255, 159, # d light green
112, 109, 235, # e light blue
178, 178, 178 # f light gray
)
#gradients from project one:
GRADIENT_PURPLE_COLORS = 6
GRADIENT_PURPLE_SCEME = (
0x00,
0x06,
0x04,
0x0a,
0x07,
0x01
)
GRADIENT_BROWN_COLORS = 7
GRADIENT_BROWN_SCEME = (
0x00,
0x09,
0x02,
0x08,
0x0a,
0x07,
0x01
)
GRADIENT_GRAY_COLORS = 5
GRADIENT_GRAY_SCEME = (
0x00,
0x0b,
0x0c,
0x0f,
0x01
)
GRADIENT_GREEN_COLORS = 6
GRADIENT_GREEN_SCEME = (
0x00,
0x09,
0x05,
0x03,
0x0d,
0x01
)
GRADIENT_BLUE_COLORS = 6
GRADIENT_BLUE_SCEME = (
0x00,
0x0b,
0x0e,
0x03,
0x0d,
0x01
)
GRADIENT_GREEN2_COLORS = 6
GRADIENT_GREEN2_SCEME = (
0x00,
0x09,
0x05,
0x03,
0x0d,
0x0f
)
"""
you need 15 values for each color
apart from the color to be replaced, each of the 16 c64 colors has to be in each table
colors near to the original come first, then the worse alternatives, in the end the worst alternative color
"""
REPLACEMENT_TABLE = (
( 11, 6, 2, 5, 9, 12, 8, 4, 10, 7, 14, 13, 3, 15, 1),#00 black
( 15, 3, 13, 14, 7, 10, 7, 8, 12, 9, 5, 2, 6, 11, 0),#01 white
( 8, 9, 10, 0, 4, 11, 6, 5, 12, 7, 14, 13, 3, 15, 1),#02 red
( 14, 13, 15, 10, 12, 7, 1, 6, 5, 8, 2, 9, 4, 11, 0),#03 cyan
( 6, 8, 10, 2, 9, 11, 12, 13, 14, 7, 5, 0, 3, 15, 1),#04 purple
( 13, 3, 11, 12, 6, 2, 9, 0, 4, 8, 10, 14, 7, 15, 1),#05 green
( 14, 4, 0, 2, 9, 5, 3, 11, 12, 8, 10, 7, 13, 15, 1),#06 blue
( 1, 13, 3, 15, 14, 10, 8, 12, 11, 4, 5, 6, 2, 9, 0),#07 yellow
( 2, 9, 10, 7, 15, 13, 14, 3, 1, 4, 5, 6, 11, 12, 0),#08 light brown
( 2, 8, 10, 11, 0, 6, 5, 12, 4, 7, 14, 13, 3, 15, 1),#09 brown
( 8, 7, 2, 9, 15, 12, 11, 6, 5, 4, 0, 10, 14, 15, 1),#10 light-red
( 12, 0, 15, 2, 9, 8, 6, 5, 4, 10, 7, 14, 13, 3, 1),#11 dark-gray
( 11, 15, 0, 2, 9, 8, 6, 5, 4, 10, 7, 14, 13, 3, 1),#12 gray
( 3, 5, 7, 15, 14, 10, 12, 11, 8, 1, 4, 9, 2, 6, 0),#13 light-green
( 3, 6, 1, 13, 15, 10, 8, 4, 7, 12, 5, 2, 11, 9, 0),#14 light-blue
( 1, 12, 11, 7, 13, 14, 4, 10, 8, 9, 5, 2, 6, 3, 0) #15 light-gray
)
CURSOR_HAND = 'hand2'
#CONFIG_FILENAME = "convertron3000.ini" #"c:\\convertron3000.ini"
"""
ERROR_DIFFUSION = (
'Floyd-Steinberg',
'Jarvis-Judice-Ninke',
'Stucki',
'Burkes',
'Sierra3',
'Sierra2',
'Sierra-2-4A',
'Stevenson-Arce',
'Atkinson'
)
"""
#global variables
def _global_variables():
return None
root = Tk()
user_filename_open = "none"
user_filename_save = "none"
user_start_address = StringVar()
user_start_address_checkbutton = IntVar()
user_sharpness = IntVar()
user_treshold = IntVar()
user_color_saturation = IntVar()
user_brightness = IntVar()
user_contrast = IntVar()
user_modes = StringVar()
user_outputformat = StringVar()
user_palette = StringVar()
user_filename_open_textvariable = StringVar()
convertbutton_text = StringVar()
user_effects_blur = IntVar()
user_effects_detail = IntVar()
user_effects_enhance = IntVar()
user_effects_enhance_more = IntVar()
user_effects_smooth = IntVar()
user_effects_smooth_more = IntVar()
user_effects_sharpen = IntVar()
user_effects_showClashes = IntVar()
user_gradient_sceme = StringVar()
user_dithering = StringVar()
user_backgroundcolor = IntVar()
user_backgroundcolor.set(99)
#defaults
user_outputformat.set("koala")
user_modes.set("colors")
user_palette.set("pepto")
user_gradient_sceme.set("purple")
user_filename_open_textvariable.set("none")
convertbutton_text.set("convert\nAlt+C")
user_dithering.set("none")
#user_dithering.set("bayer")
textbox = Text()
label_original_image = Label()
label_preview_image = Label()
label_koala_image = Label()
image_original = PilImage.new("RGB", (320, 200), "black")
image_preview = PilImage.new("RGB", (320, 200), "black")
image_koala = PilImage.new("RGBA", (320, 200), "black")
image_preview_convert = PilImage.new("RGB", (160, 200), "black")
koala_bitmap=[None]*8000
koala_col12=[None]*1000
koala_col3=[None]*1000
koala_bg=0
koala_colorindex_data = [0] * KOALA_WIDTH*KOALA_HEIGHT
hires_colorindex_data = [0] * HIRES_WIDTH*HIRES_HEIGHT
#initialize empty 320x200 data
image_result_koala = PilImage.new("P", (KOALA_WIDTH, KOALA_HEIGHT))
image_result_hires = PilImage.new("P", (HIRES_WIDTH, HIRES_HEIGHT))
scale_modifier_list=[]
scale_modifier_list_default=[]
user_custom_gradient_sceme = [0] * 16
user_custom_gradient_sceme_size = 0
color_clash_chars_xy = []
#hitherdither
def convert_to_hitherdither_palette (
palette_rgb
) :
pal = []
cnt = 0
for value in palette_rgb :
if (cnt == 0 ) : r = value
if (cnt == 1 ) : g = value
if (cnt == 2 ) :
b = value
rgb = (r * 256 * 256) + (g * 256) + b
pal.append(rgb)
cnt = -1
cnt += 1
return pal
hitherdither_palette = hitherdither.palette.Palette(convert_to_hitherdither_palette(PALETTEDATA_PEPTO)) #do to
hitherdither_tres_value=256/8 #play around with tresholds
hitherdither_tresholds = [hitherdither_tres_value, hitherdither_tres_value, hitherdither_tres_value]
hitherdither_order=8 #2,4,8,16,32,64,128
#https://docs.python.org/2.7/library/configparser.html#examples
def config_read(
filename
) :
global user_custom_gradient_sceme
global user_custom_gradient_sceme_size
with open(filename, "r") as f:
config = json.load(f)
user_custom_gradient_sceme_size = int(config['size'])
for a in range(0,len(user_custom_gradient_sceme)) :
user_custom_gradient_sceme[a] = int(config['color'+str(a)])
def gen_matrix( e ):
#https://github.com/justmao945/lab/tree/master/halftoning/ordered-dithering
''' Generating new matrix.
@param e The width and height of the matrix is 2^e.
@return New 2x2 to 2^e x 2^e matrix list.
'''
if e < 1: return None
m_list = [ [[1,2],[3,0]] ]
_b = m_list[0]
for n in range(1, e):
m = m_list[ n - 1 ]
m_list.append( [
[4*i+_b[0][0] for i in m[0]] + [4*i+_b[0][1] for i in m[0]],
[4*i+_b[0][0] for i in m[1]] + [4*i+_b[0][1] for i in m[1]],
[4*i+_b[1][0] for i in m[0]] + [4*i+_b[1][1] for i in m[0]],
[4*i+_b[1][0] for i in m[1]] + [4*i+_b[1][1] for i in m[1]],
] )
return m_list
"""
def ordered_dithering( pixel, size, matrix ):
#https://github.com/justmao945/lab/tree/master/halftoning/ordered-dithering
#Dithering on a single channel.
#@param pixel PIL PixelAccess object.
#@param size A tuple to represent the size of pixel.
#@param matrix Must be NxN, and N == 2^e where e>=1
X, Y = size
N = len(matrix)
T = [[255*(matrix[x][y]+0.5)/N/N for x in range(N)] for y in range(N)]
for y in range(0, Y):
for x in range(0, X):
# pixel[x,y] = 255 if pixel[x,y] > T[x%N][y%N] else 0
if pixel[x,y] > T[x%N][y%N] :
pixel[x,y] = 255
else :
pixel[x,y] = 0
"""
def image_quantize_c64_colors(image):
pal_image= PilImage.new("P", (1,1))
switcher_palette = {
'pepto': PALETTEDATA_PEPTO,
'view64': PALETTEDATA_VIEW64,
'vice': PALETTEDATA_VICE,
'colodore': PALETTEDATA_COLODORE,
}
my_palettedata = switcher_palette.get(user_palette.get(), PALETTEDATA_PEPTO)
hitherdither_tresholds = [256/user_treshold.get(), 256/user_treshold.get(), 256/user_treshold.get()]
#https://github.com/justmao945/lab/tree/master/halftoning/ordered-dithering
if (user_dithering.get() == 'bayer') :
hitherdither_palette = hitherdither.palette.Palette(convert_to_hitherdither_palette(my_palettedata))
image = hitherdither.ordered.bayer.bayer_dithering(image, hitherdither_palette, hitherdither_tresholds, order=hitherdither_order)
if (user_dithering.get() == 'yliluomas1') :
hitherdither_palette = hitherdither.palette.Palette(convert_to_hitherdither_palette(my_palettedata))
image = hitherdither.ordered.yliluoma.yliluomas_1_ordered_dithering(image, hitherdither_palette, order=hitherdither_order)
if (user_dithering.get() == 'line') :
hitherdither_palette = hitherdither.palette.Palette(convert_to_hitherdither_palette(my_palettedata))
my_width, my_height = image.size
image = image.resize((my_width*2,my_height), resample=PilImage.NEAREST)
image = hitherdither.ordered.bayer.bayer_dithering(image, hitherdither_palette, hitherdither_tresholds, order=hitherdither_order)
image = image.resize((my_width,my_height), resample=PilImage.NEAREST)
if (user_dithering.get() == 'dots') :
hitherdither_palette = hitherdither.palette.Palette(convert_to_hitherdither_palette(my_palettedata))
image = hitherdither.ordered.cluster.cluster_dot_dithering(image, hitherdither_palette, hitherdither_tresholds, order=hitherdither_order)
if (user_dithering.get() == 'floyd-steinberg') :
pal_image.putpalette(
(my_palettedata)
+(0,0,0)*(256-16)
)
quantisized_image = image.im.convert("P",1,pal_image.im)
image = image._new(quantisized_image)
if (user_dithering.get() == 'none') :
pal_image.putpalette(
(my_palettedata)
+(0,0,0)*(256-16)
)
quantisized_image = image.im.convert("P",0,pal_image.im)
image = image._new(quantisized_image)
return image
def image_quantize_paletted_brightness(image):
switcher_gradient_sceme = {
'purple': GRADIENT_PURPLE_SCEME,
'brown': GRADIENT_BROWN_SCEME,
'gray': GRADIENT_GRAY_SCEME,
'green': GRADIENT_GREEN_SCEME,
'blue': GRADIENT_BLUE_SCEME,
'green2': GRADIENT_GREEN2_SCEME,
'custom': user_custom_gradient_sceme,
}
gradient_sceme = switcher_gradient_sceme.get(user_gradient_sceme.get(), GRADIENT_PURPLE_SCEME)
switcher_gradient_colors = {
'purple': GRADIENT_PURPLE_COLORS,
'brown': GRADIENT_BROWN_COLORS,
'gray': GRADIENT_GRAY_COLORS,
'green': GRADIENT_GREEN_COLORS,
'blue': GRADIENT_BLUE_COLORS,
'green2': GRADIENT_GREEN2_COLORS,
'custom': user_custom_gradient_sceme_size,
}
gradient_colors = switcher_gradient_colors.get(user_gradient_sceme.get(), GRADIENT_PURPLE_COLORS)
#prepare grayscale palette
my_palettedata = []
for a in range (0,gradient_colors) :
for rgb in range (0,3) :
my_palettedata.append(
int(round(
(255/(gradient_colors-1))*a
))
)
#fill the rest of the palette with 0
for a in range (gradient_colors*3,256*3) :
my_palettedata.append(0)
hitherdither_tresholds = [256/user_treshold.get(), 256/user_treshold.get(), 256/user_treshold.get()]
#quantisize image to grayscale with given grayscale palette holding (gradient_colors) number of colors
if (user_dithering.get() == 'bayer') :
hitherdither_palette = hitherdither.palette.Palette(convert_to_hitherdither_palette(my_palettedata))
image = hitherdither.ordered.bayer.bayer_dithering(image, hitherdither_palette, hitherdither_tresholds, order=hitherdither_order)
if (user_dithering.get() == 'yliluomas1') :
hitherdither_palette = hitherdither.palette.Palette(convert_to_hitherdither_palette(my_palettedata))
image = hitherdither.ordered.yliluoma.yliluomas_1_ordered_dithering(image, hitherdither_palette, order=hitherdither_order)
if (user_dithering.get() == 'line') :
hitherdither_palette = hitherdither.palette.Palette(convert_to_hitherdither_palette(my_palettedata))
my_width, my_height = image.size
image = image.resize((my_width*2,my_height), resample=PilImage.NEAREST)
image = hitherdither.ordered.bayer.bayer_dithering(image, hitherdither_palette, hitherdither_tresholds, order=hitherdither_order)
image = image.resize((my_width,my_height), resample=PilImage.NEAREST)
if (user_dithering.get() == 'dots') :
hitherdither_palette = hitherdither.palette.Palette(convert_to_hitherdither_palette(my_palettedata))
image = hitherdither.ordered.cluster.cluster_dot_dithering(image, hitherdither_palette, hitherdither_tresholds, order=hitherdither_order)
if (user_dithering.get() == 'floyd-steinberg') :
pal_image= PilImage.new("P", (1,1))
pal_image.putpalette(my_palettedata)
quantisized_image = image.im.convert("P",1,pal_image.im)
image = image._new(quantisized_image)
if (user_dithering.get() == 'none') :
pal_image= PilImage.new("P", (1,1))
pal_image.putpalette(my_palettedata)
quantisized_image = image.im.convert("P",0,pal_image.im)
image = image._new(quantisized_image)
#make new palette with gradient sceme
rgb_palettedata = []
for a in range (0,gradient_colors):
rgb_palettedata.extend(koala_colorindex_to_rgb(gradient_sceme[a]))
#fill the rest of the palette
for a in range (gradient_colors*3,256*3) :
rgb_palettedata.append(0)
#apply new color gradient to grayscale image
image.putpalette(rgb_palettedata)
return image
def koala_index_to_colorindex(
index, #0..3
x,
y
) :
location = (y*C64_CHAR_WIDTH)+x
switcher = {
0 : koala_bg, #=koala_bg; // pixel not set = $d021 colour
1 : koala_col12[location] >> 4, #=koala_col12[(y*C64_CHAR_WIDTH)+x] SHR 4;
2 : koala_col12[location] & 0b00001111, #=koala_col12[(y*C64_CHAR_WIDTH)+x] and %00001111;
3 : koala_col3[location] & 0b00001111 #=koala_col3[(y*C64_CHAR_WIDTH)+x] and %00001111;
}
return switcher.get(index,0)
def hires_index_to_colorindex(
index, #0..1
x,
y
) :
location = (y*C64_CHAR_WIDTH)+x
switcher = {
0 : koala_col12[location] & 0b00001111, #=koala_col12[(y*C64_CHAR_WIDTH)+x] and %00001111;
1 : koala_col12[location] >> 4, #=koala_col12[(y*C64_CHAR_WIDTH)+x] SHR 4;
}
return switcher.get(index,0)
def koala_colorindex_to_rgb(
index
):
switcher_palette = {
'pepto': PALETTEDATA_PEPTO,
'view64': PALETTEDATA_VIEW64,
'vice': PALETTEDATA_VICE,
'colodore': PALETTEDATA_COLODORE,
}
my_palette = switcher_palette.get(user_palette.get(), PALETTEDATA_PEPTO)
return_palette = []
return_palette.append(my_palette[(index*3)+0])
return_palette.append(my_palette[(index*3)+1])
return_palette.append(my_palette[(index*3)+2])
return return_palette
def koala_to_image(
):
global koala_colorindex_data
SHR_PRE = [
6,
4,
2,
0
]
for y in range(0, C64_CHAR_HEIGHT):
for x in range(0, C64_CHAR_WIDTH):
pos = ((y*C64_CHAR_WIDTH)+x)*8
this_block = koala_bitmap[ pos:pos+8] #this_block holds 8 bytes
# print(this_block)
for row in range(0, 8):
this_row = this_block[row]
for column in range(0, 4):
iy = y*8 +row
ix = x*4 +column
#normal data
koalaindex = (this_row >> SHR_PRE[column]) & 0b00000011 #result should be 0..3
koala_colorindex_data[iy*KOALA_WIDTH+ix] = koala_index_to_colorindex(koalaindex,x,y)
def hires_to_image(
):
global koala_colorindex_data
#constants
SHR_PRE = [
7,
6,
5,
4,
3,
2,
1,
0
]
for y in range(0, C64_CHAR_HEIGHT):
for x in range(0, C64_CHAR_WIDTH):
pos = ((y*C64_CHAR_WIDTH)+x)*8
this_block = koala_bitmap[ pos:pos+8] #this_block holds 8 bytes
for row in range(0, 8):
this_row = this_block[row]
for column in range(0, 8):
my_index = (this_row >> SHR_PRE[column]) & 0b00000001 #result should be 0..1
iy = y*8 +row
ix = x*8 +column
hires_colorindex_data[iy*HIRES_WIDTH+ix] = hires_index_to_colorindex(my_index,x,y)
def convert_to_koala_find_replace_color(
palette,#:array of palette_type_extra,
replace_this
):
# find the next better color of the 4 most used ones (color table)
found=False;
return_value=0
for a in range (0,15): #should be 0,16
for b in range (0,4):
if (
(found == False) &
(REPLACEMENT_TABLE[replace_this][a] == palette[b][0])
):
return_value = palette[b][0]
found = True
# print('solution found: %d -> %d' %(replace_this, palette[b][0]))
if (found == False) :
print( 'Error in the color replacement table: ')
print( '4 mostly used colors: ')
#for a in range (0,4) : print ("%d " % palette[a].color),
for a in range (0,4) : print ("%d " % palette[a][0]),
print
print ( 'Replacement table for color %d: '% replace_this)
print( REPLACEMENT_TABLE[replace_this] )
print
return_value = palette[1][0];
print('Dirty fix: Replacing %d with most used color %d.' % (replace_this,palette[1][0]));
return return_value
def convert_to_koala_replace_colors(
block,
color,
solution
):
# print('Replacing %d -> %d in ' % (color, solution))
for a in range (0,8) :
for b in range (0,4) :
if (block[a][b] == color):
# print('block[%d,%d] ' % (a,b)),
block[a][b] = solution
# print
def convert_to_koala_sort_palette(
palette
):
#normal bubble sort, sort colors: mostly used first, least used last
for a in range(0,16):
for b in range(0,16):
if (a==b) : continue
if (palette[a][1] > palette[b][1]):
palette[b], palette[a] = palette[a].copy(), palette[b].copy()
def convert_to_koala_find_best_background_color(
bmp_bitmap
) :
"""
sets background color ($d021) to the most used color in the original image
"""
#my_palette = numpy.zeros((16,2), dtype=numpy.uint8) #32 bytes
my_palette = [ [0] *2 for i in range(16) ] #32 bytes
#init
for y in range (0,16) : my_palette[y][0] = y #color
for y in range (0,16) : my_palette[y][1] = 0 #amount
for y in range (0,200) :
for x in range (0,160) :
my_palette[bmp_bitmap[y][x]][1] += 1 #amount
convert_to_koala_sort_palette(my_palette)
return my_palette[0][0] #color
def show_color_clashes_on_console(
my_data
) :
if ( len(my_data) > 0 ) :
n=1
print('Color clashes:')
for c in my_data :
print('%3d: '%(n),end='')
print('x:%2d, y:%2d, %d colors used, colors: ' %(c[0], c[1], c[2]), end='')
for r in c[3] :
print('%2d, '%r , end='')
print()
n+=1
print('---end---')
"""
unique_list = [list(x) for x in set(tuple(x) for x in color_clash_chars_xy)]
color_clash_chars_xy = unique_list
n=1
print('unique list (should not contain duplicates):')
for c in color_clash_chars_xy :
print('%d: '%(n),end='')
print(c)
n+=1
print('---end---')
"""
def convert_to_koala(
) :
"""
converts a palettes image mode "P" to a koala
also checks color clashes
* reads: image_preview_convert
* sets: koala_bitmap, koala_col12, koala_col3 and koala_bg
"""
global textbox
global koala_bitmap, koala_col12, koala_col3, koala_bg
global color_clash_chars_xy
textbox.delete('1.0', END) #clear textbox
block = [ [0] * 4 for i in range(8)] #32 bytes
bitmap = [[ [0] * 8 for i in range(40)] for i in range(25)] #8000 bytes
screen = [ [0] * 40 for i in range(25)] #1000 bytes
colram = [ [0] * 40 for i in range(25)] #1000 bytes
palette = [ [0] * 2 for i in range(16)] #32 bytes palette[x][0]=color palette / [x][1]=amount
user_koala_bg_color = user_backgroundcolor.get()
textbox.insert(END,"procedure \"convert_to_koala\": working...\n")
root.update()
#fill bmp_bitmap with image_preview 160x200 data
bmp_bitmap = [ [0] * 160 for i in range(200) ]
my_list = list(image_preview_convert.getdata()) #image is in "P" mode
for y in range(0,200) :
for x in range(0,160) :
bmp_bitmap[y][x] = my_list[(y*160)+x]
#converting to koala: begin...
color_clash_counter = 0
color_clash_chars_counter = 0
background_color = 0
#background color
if (user_koala_bg_color!=99) : #99 = automatic
background_color = user_koala_bg_color
else:
background_color = convert_to_koala_find_best_background_color(bmp_bitmap);
textbox.insert(END,"Background Color = %d\n" % background_color)
#main loop
color_clash_chars_xy = []
for y in range (0,25):
for x in range (0,40):
#fill block with values
for c in range (0,8):
for d in range (0,4):
block[c][d] = bmp_bitmap[y*8+c][x*4+d]
# count all colors in this block: make palette
# clear palette
for c in range (0,16):
palette[c][0]=c #palette[c].color
palette[c][1]=0 # clear amount palette[c].amount
# fill palette amount values
for c in range (0,8):
for d in range (0,4):
palette[block[c][d]][1] += 1 # increase the color-counter (palette.amount) for block[c][d]
palette[background_color][1] = 99 # palette.amount BACKGROUND_COLOR always has to be in the palette
convert_to_koala_sort_palette(palette)
# print("sorted:")
# print(palette)
used_colors_count = 0
used_colors_colors = []
for c in range (0,16):
if (palette[c][1] > 0): #palette[c].amount
used_colors_count += 1 #this color has already been used (amount > 0)
used_colors_colors.append(palette[c][0]) #store color number of used color
if (used_colors_count > 4) : #this character has more than 4 colors -> fix this color clash
color_clash_chars_counter += 1
my_coords = []
my_coords.append(x)
my_coords.append(y)
my_coords.append(used_colors_count)
my_coords.append(used_colors_colors)
color_clash_chars_xy.append(my_coords)
#print("x: %d, y: %d"%(x,y))
#print(palette)
#print("")
for c in range (4,16): #find a solution for fourth, fifth, sixth... color if it is used
if (palette[c][1]>0): #palette[c].amount
color_clash_counter += 1
solution = convert_to_koala_find_replace_color(palette, palette[c][0]) #palette.color
# now really replace the colors in block[c,d]
convert_to_koala_replace_colors(block, palette[c][0], solution) #palette.color
# store colors
screen[y][x] = palette[1][0] #palette.color
screen[y][x] = (screen[y][x] << 4) | palette[2][0] #palette.color
colram[y][x] = palette[3][0] #palette.color
# convert bitmap data
for c in range (0,8):
for d in range (0,4):
if (block[c][d] == background_color) :
block[c][d] = 0
continue # %00
if (block[c][d] == palette[1][0]) : #palette.color
block[c][d] = 1
continue # %01
if (block[c][d] == palette[2][0]) : #palette.color
block[c][d] = 2
continue # %10
if (block[c][d] == palette[3][0]) : #palette.color
block[c][d] = 3
continue; # %11
# we should never reach here
textbox.insert(END,'Convert error in char[%d,%d]:\n'%(y,x))
textbox.insert(END,'Color %d in block[%d,%d] not found!\n'% (block[c][d], c, d))
textbox.insert(END,'Block row %d, column %d (convert bitmap data)\n' % (c,d))
textbox.insert(END,"\n")
return None # halt(1);
# store bitmap data
for c in range (0,8) :
bitmap[y][x][c] = block[c][0]
bitmap[y][x][c] = (bitmap[y][x][c] << 2) | block[c][1]
bitmap[y][x][c] = (bitmap[y][x][c] << 2) | block[c][2]
bitmap[y][x][c] = (bitmap[y][x][c] << 2) | block[c][3]
textbox.insert(END,'Fixed %d color clashes in %d character blocks.\n'% (color_clash_counter, color_clash_chars_counter));
show_color_clashes_on_console(color_clash_chars_xy)
#convert to our format used in koala_to_image
for y in range (0,25) :
for x in range (0,40) :
for c in range (0,8) : koala_bitmap[((y*40)+x)*8 +c] = bitmap[y][x][c]
koala_col12[(y*40)+x] = screen[y][x]
koala_col3[(y*40)+x] = colram[y][x]
koala_bg = background_color
def convert_to_hires_find_replace_color(
palette,#:array of palette_type_extra,
replace_this
):
# find the next better color of the 4 most used ones (color table)
found=False;
return_value=0
for a in range (0,15): #should be 0,16
for b in range (0,2):
if (
(found == False) &
(REPLACEMENT_TABLE[replace_this][a] == palette[b][0])
):
return_value = palette[b][0]
found = True
#print('solution found: %d -> %d' %(replace_this, palette[b][0]))
if (found == False) :