-
Notifications
You must be signed in to change notification settings - Fork 11
/
PhotonFile.py
1511 lines (1273 loc) · 66.9 KB
/
PhotonFile.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
"""
Loads/Save .photon files (from the Anycubic Photon Slicer) in memory and allows editing of settings and bitmaps.
"""
__version__ = "alpha"
__author__ = "Nard Janssens, Vinicius Silva, Robert Gowans, Ivan Antalec, Leonardo Marques - See Github PhotonFileUtils"
import os
import copy
import math
import struct
from math import *
#import pygame
#from pygame.locals import *
import concurrent
import time
try:
import numpy
numpyAvailable = True
#print("Numpy library available.")
except ImportError:
numpyAvailable = False
#print ("Numpy library not found.")
########################################################################################################################
## Convert byte string to hex string
########################################################################################################################
def hexStr(bytes):
if isinstance(bytes, bytearray):
return ' '.join(format(h, '02X') for h in bytes)
if isinstance(bytes, int):
return format(bytes, '02X')
return ("No Byte (string)")
########################################################################################################################
## Class PhotonFile
########################################################################################################################
class PhotonFile:
isDrawing = False # Navigation can call upon retrieving bitmaps frequently. This var prevents multiple almost parallel loads
nrLayersString = "# Layers" #String is used in multiple locations and thus can be edited here
# Data type constants
tpByte = 0
tpChar = 1
tpInt = 2
tpFloat = 3
# Clipboard Vars to copy/cut and paste layer settinngs/imagedata
clipboardDef = None
clipboardData = None
# This is the data structure of photon file. For each variable we need to know
# Title string to display user, nr bytes to read/write, type of data stored, editable
# Each file consists of
# - General info ( pfStruct_Header, Header)
# - Two previews which contain meta-info an raw image data ( pfStruct_Previews, Previews)
# - For each layer meta-info ( pfStruct_LayerDefs, LayerDefs)
# - For each layer raw image data ( pfStruct_LayerData, LayerData)
pfStruct_Header = [
("Header", 8, tpByte, False, ""),
("Bed X (mm)", 4, tpFloat, True, "Short side of the print bed."),
("Bed Y (mm)", 4, tpFloat, True, "Long side of the print bed."),
("Bed Z (mm)", 4, tpFloat, True, "Maximum height the printer can print."),
("padding0", 3 * 4, tpByte, False, ""), # 3 ints
("Layer height (mm)", 4, tpFloat, True, "Default layer height."),
("Exp. time (s)", 4, tpFloat, True, "Default exposure time."),
("Exp. bottom (s)", 4, tpFloat, True, "Exposure time for bottom layers."),
("Off time (s)", 4, tpFloat, True, "Time UV is turned of between layers. \n Minimum is 6.5 sec, the time to rise the \n build plate and dip back in the resin."),
("# Bottom Layers", 4, tpInt, True, "Number of bottom layers.\n (These have different exposure time.)"),
("Resolution X", 4, tpInt, True, "X-Resolution of the screen through \n which the layer image is projected."),
("Resolution Y", 4, tpInt, True, "Y-Resolution of the screen through \n which the layer image is projected." ),
("Preview 0 (addr)", 4, tpInt, False, "Address where the metadata \n of the High Res preview image can be found."), # start of preview 0
("Layer Defs (addr)", 4, tpInt, False, "Address where the metadata \n for the layer images can be found."), # start of layerDefs
(nrLayersString, 4, tpInt, False, "Number of layers this file has."),
("Preview 1 (addr)", 4, tpInt, False, "Address where the metadata of the \n Low Res preview image can be found."), # start of preview 1
("unknown6", 4, tpInt, False, ""),
("Proj.type-Cast/Mirror", 4, tpInt, False, "LightCuring/Projection type:\n 1=LCD_X_MIRROR \n 0=CAST"), #LightCuring/Projection type // (1=LCD_X_MIRROR, 0=CAST)
("padding1", 6 * 4, tpByte, False, "") # 6 ints
]
pfStruct_Previews = [
("Resolution X", 4, tpInt, False, "X-Resolution of preview pictures."),
("Resolution Y", 4, tpInt, False, "Y-Resolution of preview pictures."),
("Image Address", 4, tpInt, False, "Address where the raw \n image can be found."), # start of rawData0
("Data Length", 4, tpInt, False, "Size (in bytes) of the \n raw image."), # size of rawData0
("padding", 4 * 4, tpByte, False, ""), # 4 ints
("Image Data", -1, tpByte, False, "The raw image."),
]
# The exposure time and off times are ignored by Photon printer, layerheight not and is cumulative
pfStruct_LayerDef = [
("Layer height (mm)", 4, tpFloat, True, "Height at which this layer \n should be printed."),
("Exp. time (s)", 4, tpFloat, False, "Exposure time for this layer.\n (Based on General Info.)"),
("Off time (s)", 4, tpFloat, False, "Off time for this layer.\n (Based on General Info.)"),
("Image Address", 4, tpInt, False, "Address where the raw image \n can be found."),#dataStartPos -> Image Address
("Data Length", 4, tpInt, False, "Size (in bytes) of the raw image."), #size of rawData+lastByte(1)
("padding", 4 * 4, tpByte, False, "") # 4 ints
]
# pfStruct_LayerData =
# rawData - rle encoded bytes except last one
# lastByte - last byte of encoded bitmap data
Header = {}
Previews = [{},{}]
LayerDefs = []
LayerData = []
History=[]
HistoryMaxDepth = 10
########################################################################################################################
## Methods to convert bytes (strings) to python variables and back again
########################################################################################################################
@staticmethod
def bytes_to_int(bytes):
""" Converts list or array of bytes to an int. """
result = 0
for b in reversed(bytes):
result = result * 256 + int(b)
return result
@staticmethod
def bytes_to_float(inbytes):
""" Converts list or array of bytes to an float. """
bits = PhotonFile.bytes_to_int(inbytes)
mantissa = ((bits & 8388607) / 8388608.0)
exponent = (bits >> 23) & 255
sign = 1.0 if bits >> 31 == 0 else -1.0
if exponent != 0:
mantissa += 1.0
elif mantissa == 0.0:
return sign * 0.0
return sign * pow(2.0, exponent - 127) * mantissa
@staticmethod
def bytes_to_hex(bytes):
""" Converts list or array of bytes to an hex. """
return ' '.join(format(h, '02X') for h in bytes)
@staticmethod
def hex_to_bytes(hexStr):
""" Converts hex to array of bytes. """
return bytearray.fromhex(hexStr)
@staticmethod
def int_to_bytes(intVal):
""" Converts POSITIVE int to bytes. """
return intVal.to_bytes(4, byteorder='little')
@staticmethod
def float_to_bytes(floatVal):
""" Converts POSITIVE floats to bytes.
Based heavily upon http: //www.simplymodbus.ca/ieeefloats.xls
"""
# Error when floatVal=0.5
return struct.pack('f',floatVal)
if floatVal == 0: return (0).to_bytes(4, byteorder='big')
sign = -1 if floatVal < 0 else 1
firstBit = 0 if sign == 1 else 1
exponent = -127 if abs(floatVal) < 1.1754943E-38 else floor(log(abs(floatVal), 10) / log(2, 10))
exponent127 = exponent + 127
mantissa = floatVal / pow(2, exponent) / sign
substract = mantissa - 1
multiply = round(substract * 8388608)
div256_1 = multiply / 256
divint_1 = int(div256_1)
rem_1 = int((div256_1 - divint_1) * 256)
div256_2 = divint_1 / 256
divint_2 = int(div256_2)
rem_2 = int((div256_2 - divint_2) * 256)
bin1 = (exponent127 & 0b11111110) >> 1 | firstBit << 7
bin2 = (exponent127 & 0b00000001) << 7 | divint_2
bin3 = rem_2
bin4 = rem_1
# print ("ALT: ",bin(bin1_new), bin(bin2_new),bin(bin3_new),bin(bin4_new))
bin1234 = bin1 | bin2 << 8 | bin3 << 16 | bin4 << 24
return bin1234.to_bytes(4, byteorder='big')
@staticmethod
def convBytes(bytes, bType):
""" Converts all photonfile types to bytes. """
nr = None
if bType == PhotonFile.tpInt:
nr = PhotonFile.bytes_to_int(bytes)
if bType == PhotonFile.tpFloat:
nr = PhotonFile.bytes_to_float(bytes)
if bType == PhotonFile.tpByte:
nr = PhotonFile.bytes_to_hex(bytes)
return nr
########################################################################################################################
## History methods
########################################################################################################################
def realDeepCopy(self,dictionary):
return #probable not needed
""" Makes a real copy of a dictionary consisting of bytes strings
"""
hC = copy.deepcopy(self.Header)
for key,byteString in dictionary.items():
dictionary[key]=(byteString+b'\x00')[:-1] # Force to make a real copy
def saveToHistory(self, action, layerNr):
""" Makes a copy of current /Layer Data to memory
Since all are bytearrays no Copy.Deepcopy is needed.
"""
# Copy LayerDefs and LayerData
layerDef=copy.deepcopy(self.LayerDefs[layerNr])
layerData=copy.deepcopy(self.LayerData[layerNr])
self.realDeepCopy(layerDef)
self.realDeepCopy(layerData)
# Append to history stack/array
newH = {"Action":action,"LayerNr":layerNr,"LayerDef":layerDef,"LayerData":layerData}
print("Stored:",newH,id(layerDef),id(layerData))
self.History.append(newH)
if len(self.History)>self.HistoryMaxDepth:
self.History.remove(self.History[0])
def loadFromHistory(self):
""" Load a copy of current Header/Preview/Layer Data to memory
We copy by reference and remove item from history stack.
"""
if len(self.History)==0:
raise Exception("You have reached the maximum depth to undo.")
# Find last item added to History
idxLastAdded=len(self.History)-1
lastItemAdded=self.History[idxLastAdded]
action=lastItemAdded["Action"]
layerNr =lastItemAdded["LayerNr"]
layerDef = lastItemAdded["LayerDef"]
layerData = lastItemAdded["LayerData"]
print("Found:", self.History[idxLastAdded])
# Reverse the actions
if action=="insert":
self.deleteLayer(layerNr, saveToHistory=False)
elif action=="delete":
self.clipboardDef=layerDef
self.clipboardData=layerData
self.insertLayerBefore(layerNr,fromClipboard=True, saveToHistory=False)
elif action=="replace":
self.clipboardDef=layerDef
self.clipboardData=layerData
self.deleteLayer(layerNr)
self.insertLayerBefore(layerNr,fromClipboard=True, saveToHistory=False)
# Remove this item
self.History.remove(lastItemAdded)
#Make alias for loadFromHistory
undo = loadFromHistory
########################################################################################################################
## Class methods
########################################################################################################################
def __init__(self, photonfilename):
""" Just stores photon filename. """
self.filename = photonfilename
def nrLayers(self):
""" Returns 4 bytes for number of layers as int. """
return PhotonFile.bytes_to_int(self.Header[self.nrLayersString])
def readFile(self):
""" Reads the photofile from disk to memory. """
with open(self.filename, "rb") as binary_file:
# Start at beginning
binary_file.seek(0)
# Read HEADER / General settings
for bTitle, bNr, bType, bEditable,bHint in self.pfStruct_Header:
self.Header[bTitle] = binary_file.read(bNr)
# Read PREVIEWS settings and raw image data
for previewNr in (0,1):
for bTitle, bNr, bType, bEditable, bHint in self.pfStruct_Previews:
# if rawData0 or rawData1 the number bytes to read is given bij dataSize0 and dataSize1
if bTitle == "Image Data": bNr = dataSize
self.Previews[previewNr][bTitle] = binary_file.read(bNr)
if bTitle == "Data Length": dataSize = PhotonFile.bytes_to_int(self.Previews[previewNr][bTitle])
# Read LAYERDEFS settings
nLayers = PhotonFile.bytes_to_int(self.Header[self.nrLayersString])
self.LayerDefs = [dict() for x in range(nLayers)]
# print("nLayers:", nLayers)
# print(" hex:", ' '.join(format(x, '02X') for x in self.Header[self.nrLayersString]))
# print(" dec:", nLayers)
# print("Reading layer meta-info")
for lNr in range(0, nLayers):
# print(" layer: ", lNr)
for bTitle, bNr, bType, bEditable, bHint in self.pfStruct_LayerDef:
self.LayerDefs[lNr][bTitle] = binary_file.read(bNr)
# Read LAYERRAWDATA image data
# print("Reading layer image-info")
self.LayerData = [dict() for x in range(nLayers)]
for lNr in range(0, nLayers):
rawDataSize = PhotonFile.bytes_to_int(self.LayerDefs[lNr]["Data Length"])
# print(" layer: ", lNr, " size: ",rawDataSize)
self.LayerData[lNr]["Raw"] = binary_file.read(rawDataSize - 1) # b'}}}}}}}}}}
# -1 because we don count byte for endOfLayer
self.LayerData[lNr]["EndOfLayer"] = binary_file.read(1)
# print (' '.join(format(x, '02X') for x in header))
# Clear History for this new file
self.History = []
def writeFile(self, newfilename=None):
""" Writes the photofile from memory to disk. """
# Check if other filename is given to save to, otherwise use filename used to load file.
if newfilename == None: newfilename = self.filename
with open(newfilename, "wb") as binary_file:
# Start at beginning
binary_file.seek(0)
# Write HEADER / General settings
for bTitle, bNr, bType, bEditable,bHint in self.pfStruct_Header:
binary_file.write(self.Header[bTitle])
# Write PREVIEWS settings and raw image data
for previewNr in (0, 1):
for bTitle, bNr, bType, bEditable, bHint in self.pfStruct_Previews:
#print ("Save: ",bTitle)
binary_file.write(self.Previews[previewNr][bTitle])
# Read LAYERDEFS settings
nLayers = PhotonFile.bytes_to_int(self.Header[self.nrLayersString])
for lNr in range(0, nLayers):
#print(" layer: ", lNr)
#print(" def: ", self.LayerDefs[lNr])
for bTitle, bNr, bType, bEditable, bHint in self.pfStruct_LayerDef:
binary_file.write(self.LayerDefs[lNr][bTitle])
# Read LAYERRAWDATA image data
# print("Reading layer image-info")
for lNr in range(0, nLayers):
binary_file.write(self.LayerData[lNr]["Raw"])
binary_file.write(self.LayerData[lNr]["EndOfLayer"])
########################################################################################################################
## Encoding
########################################################################################################################
def encodedBitmap_Bytes_withnumpy(image):
""" Converts image (filename/pygame.surface/numpy.array2d) to RLE encoded byte string.
Uses Numpy library - Fast
Based on https://gist.github.com/itdaniher/3f57be9f95fce8daaa5a56e44dd13de5
Encoding scheme:
Highest bit of each byte is color (black or white)
Lowest 7 bits of each byte is repetition of that color, with max of 125 / 0x7D
"""
imgarr=None
# Check if we got a string and if so load image
#if isinstance(image, str): # received name of file to load
# imgsurf = pygame.image.load(image)
# (width, height) = imgsurf.get_size()
#elif isinstance(image,pygame.Surface):
# imgsurf = image # reveived pygame.surface
# (width, height) = imgsurf.get_size()
#el
if isinstance(image, (numpy.ndarray, numpy.generic)): # received opencv / numpy image array
#print ("shape",image.shape)
(width, height)=image.shape
imgarr=image
else:
raise Exception("Only image filename, pygame.Surface or numpy.array (2D) are excepted")
# Check if size is correct size (1440 x 2560)
if not (width, height) == (1440, 2560):
raise Exception("Your image dimensions are off and should be 1440x2560")
#t0=time.time()
# Convert image data to Numpy 1-dimensional array
#if not isinstance(image, (numpy.ndarray, numpy.generic)): # Not needed if we got an numpy array as input
# imgarr = pygame.surfarray.array2d(imgsurf)
#t1 = time.time()
# Rotate,flip image and flatten array
imgarr = numpy.rot90(imgarr,axes=(1,0))
imgarr = numpy.fliplr(imgarr) # reverse/mirror array
x = numpy.asarray(imgarr).flatten(0)
#t2 = time.time()
#print ("rotate/flip/flatten:",(t2-t1))
# Encoding magic
where = numpy.flatnonzero
x = numpy.asarray(x)
n = len(x)
if n == 0:
return numpy.array([], dtype=numpy.int)
starts = numpy.r_[0, where(~numpy.isclose(x[1:], x[:-1], equal_nan=True)) + 1]
lengths = numpy.diff(numpy.r_[starts, n])
values = x[starts]
#ret=np.dstack((lengths, values))[0]
#t3 = time.time()
#print ("encoding magic:",(t3-t2))
# Reduce repetitions of color to max 0x7D/125 and store in bytearray
rleData = bytearray()
for (nr, col) in zip(lengths,values):
#color = (abs(col)>1) # slow
color=1 if col else 0 # fast
while nr > 0x7D:
encValue = (color << 7) | 0x7D
rleData.append(encValue)
nr = nr - 0x7D
encValue = (color << 7) | nr
rleData.append(encValue)
#t4 = time.time()
#print ("max rep 0x7D:",(t4-t3))
#quit()
# Needed is an byte string, so convert
return bytes(rleData)
def encodedBitmap_Bytes_nonumpy(surfOrFile):
""" Converts image data from file on disk to RLE encoded byte string.
Processes pixels one at a time (pygame.get_at) - Slow
Encoding scheme:
Highest bit of each byte is color (black or white)
Lowest 7 bits of each byte is repetition of that color, with max of 125 / 0x7D
"""
# Check if we got a string and if so load image
if isinstance(surfOrFile, str):
imgsurf = pygame.image.load(surfOrFile)
else:
imgsurf = surfOrFile
# Check if size is correct size (1440 x 2560)
#bitDepth = imgsurf.get_bitsize()
#bytePerPixel = imgsurf.get_bytesize()
(width, height) = imgsurf.get_size()
if not (width, height) == (1440,2560):
raise Exception("Your image dimensions are off and should be 1440x2560")
# Count number of pixels with same color up until 0x7D/125 repetitions
rleData = bytearray()
color = 0
black = 0
white = 1
nrOfColor = 0
prevColor=None
for y in range(height):
for x in range(width):
# print (imgsurf.get_at((x, y)))
(r, g, b, a) = imgsurf.get_at((x, y))
if ((r + g + b) // 3) < 128:
color = black
else:
color = white
if prevColor == None: prevColor = color
isLastPixel = (x == (width - 1) and y == (height - 1))
if color == prevColor and nrOfColor < 0x7D and not isLastPixel:
nrOfColor = nrOfColor + 1
else:
#print (color,nrOfColor,nrOfColor<<1)
encValue = (prevColor << 7) | nrOfColor # push color (B/W) to highest bit and repetitions to lowest 7 bits.
rleData.append(encValue)
prevColor = color
nrOfColor = 1
return bytes(rleData)
def encodedBitmap_Bytes(surfOrFile):
""" Depening on availability of Numpy, calls upon correct Encoding method."""
if numpyAvailable:
return PhotonFile.encodedBitmap_Bytes_withnumpy(surfOrFile)
else:
return PhotonFile.encodedBitmap_Bytes_nonumpy(surfOrFile)
def encodedPreviewBitmap_Bytes_nonumpy(filename,checkSizeForNr=0):
""" Converts image data from file on disk to RLE encoded byte string.
Processes pixels one at a time (pygame.get_at) - Slow
Encoding scheme:
The color (R,G,B) of a pixel spans 2 bytes (little endian) and each color component is 5 bits: RRRRR GGG GG X BBBBB
If the X bit is set, then the next 2 bytes (little endian) masked with 0xFFF represents how many more times to repeat that pixel.
"""
imgsurf = pygame.image.load(filename)
# bitDepth = imgsurf.get_bitsize()
# bytePerPixel = imgsurf.get_bytesize()
(width, height) = imgsurf.get_size()
print ("Size:", width, height)
#Preview images tend to have different sizes. Check on size is thus not possible.
#if checkSizeForNr==0 and not (width, height) == (360,186):
# raise Exception("Your image dimensions are off and should be 360x186 for the 1st preview.")
#if checkSizeForNr==1 and not (width, height) == (198,101):
# raise Exception("Your image dimensions are off and should be 198x101 for the 1st preview.")
# Count number of pixels with same color up until 0x7D/125 repetitions
rleData = bytearray()
color = 0
black = 0
white = 1
nrOfColor = 0
prevColor = None
for y in range(height):
for x in range(width):
#print (x,y)
# print (imgsurf.get_at((x, y)))
color = imgsurf.get_at((x, y)) # (r, g, b, a)
if prevColor == None: prevColor = color
isLastPixel = (x == (width - 1) and y == (height - 1))
if color == prevColor and nrOfColor < 0x0FFF and not isLastPixel:
nrOfColor = nrOfColor + 1
else:
# print (color,nrOfColor,nrOfColor<<1)
R=prevColor[0]
G=prevColor[1]
B=prevColor[2]
if nrOfColor>1:
X=1
else:
X=0
# build 2 or 4 bytes (depending on X
# The color (R,G,B) of a pixel spans 2 bytes (little endian) and
# each color component is 5 bits: RRRRR GGG GG X BBBBB
R = round(R / 255 * 31)
G = round(G / 255 * 31)
B = round(B / 255 * 31)
encValue0=R<<3 | G>>2
encValue1=(((G & 0b00000011)<<6) | X<<5 | B)
if X==1:
nrOfColor=nrOfColor-1 # write one less than nr of pixels
encValue2=nrOfColor>>8
encValue3=nrOfColor & 0b000000011111111
#seems like nr bytes pixels have 0011 as start
encValue2=encValue2 | 0b00110000
# save bytes
rleData.append(encValue1)
rleData.append(encValue0)
if X==1:
rleData.append(encValue3)
rleData.append(encValue2)
# search next color
prevColor = color
nrOfColor = 1
#print ("len",len(rleData))
return (width,height,bytes(rleData))
########################################################################################################################
## Decoding
########################################################################################################################
def getBitmap_withnumpy(self, layerNr, forecolor=(128,255,128), backcolor=(0,0,0),scale=(0.25,0.25),retNumpyArray=False):
""" Decodes a RLE byte array from PhotonFile object to a pygame surface.
Based on: https://gist.github.com/itdaniher/3f57be9f95fce8daaa5a56e44dd13de5
Encoding scheme:
Highest bit of each byte is color (black or white)
Lowest 7 bits of each byte is repetition of that color, with max of 125 / 0x7D
"""
#tStart=pygame.time.get_ticks()
# Colors are stored reversed and we count on alpha bit (size of int does not matter for numpy speed)
isAlpha=False
if len(forecolor) == 4 or len(backcolor) == 4: isAlpha = True
if len(forecolor) == 3:forecolor = (255,forecolor[0], forecolor[1], forecolor[2])
elif len(forecolor) == 4:forecolor = (forecolor[3], forecolor[0], forecolor[1],forecolor[2])
if len(backcolor) == 3:backcolor = (255,backcolor[0], backcolor[1], backcolor[2])
elif len(backcolor) == 4:backcolor=(backcolor[3], backcolor[0], backcolor[1],backcolor[2])
# If no layers return
if self.nrLayers()==0:#could occur if loading new file
memory=pygame.Surface((int(1440 * scale[0]), int(2560 * scale[1])),24)
return memory
# Tell PhotonFile we are drawing so GUI can prevent too many calls on getBitmap
self.isDrawing = True
# Retrieve raw image data and add last byte to complete the byte array
bA = self.LayerData[layerNr]["Raw"]
# add endOfLayer Byte
bA = bA + self.LayerData[layerNr]["EndOfLayer"]
# Convert bytes to numpy 1 dimensional array
bN =numpy.fromstring(bA,dtype=numpy.uint8)
# Extract color value (highest bit) and nr of repetitions (lowest 7 bits)
valbin = bN >> 7 # only read 1st bit
nr = bN & ~(1 << 7) # turn highest bit of
# Replace 0's en 1's with correct colors
forecolor_int = (forecolor[0] << 24) + (forecolor[1] << 16) + (forecolor[2] << 8) + forecolor[3]
backcolor_int = (backcolor[0] << 24) + (backcolor[1] << 16) + (backcolor[2] << 8) + backcolor[3]
val = numpy.array([{0: backcolor_int, 1: forecolor_int}[x] for x in valbin])
# Make a 2d array like [ [3,0] [2,1], [nr_i,val_i]...] using the colorvalues (val) and repetitions(nr)
runs = numpy.column_stack((nr, val))
# Decoding magic
runs_t = numpy.transpose(runs)
lengths = runs_t[0].astype(int)
values = runs_t[1].astype(int)
starts = numpy.concatenate(([0], numpy.cumsum(lengths)[:-1]))
starts, lengths, values = map(numpy.asarray, (starts, lengths, values))
ends = starts + lengths
n = ends[-1]
x = numpy.full(n, 0)
for lo, hi, val in zip(starts, ends, values):
x[lo:hi] = val
# Make sure we have a bitmap of the correct size and if not pad with black pixels
if not len(x) == 3686400: print ("Warning: The file decoded with less bytes than needed. Will pad the file with zero bytes.")
while not len(x)==3686400:
x=numpy.append(x,(0,))
# Convert 1-dim array to matrix
rgb2d = x.reshape((2560,1440)) # data is stored in rows of 2560
if retNumpyArray:return rgb2d # if numpy array is returned, rotation not needed
rgb2d = numpy.rot90(rgb2d, axes=(1, 0)) # we need 1440x2560
rgb2d = numpy.fliplr(rgb2d) # however data is mirrored along x axis
#picture=pygame.surfarray.make_surface(rgb2d)# convert numpy array to pygame surface
#memory=pygame.transform.scale(picture, (int(1440*scale[0]), int(2560*scale[1]))) # rescale for display in window
if isAlpha:
temp = pygame.Surface((1440, 2560), depth=32,flags=pygame.SRCALPHA)
else:
temp = pygame.Surface((1440, 2560), depth=24)
pygame.surfarray.blit_array(temp,rgb2d)
memory=pygame.transform.scale(temp, (int(1440*scale[0]), int(2560*scale[1]))) # rescale for display in window
# Done drawing so next caller knows that next call can be made.
self.isDrawing = False
#tDelta = pygame.time.get_ticks()-tStart
#print ("elaps:",tDelta)
return memory
def getBitmap_nonumpy(self, layerNr, forecolor=(128,255,128), backcolor=(0,0,0),scale=(0.25,0.25)):
""" Decodes a RLE byte array from PhotonFile object to a pygame surface.
Based on: https://gist.github.com/itdaniher/3f57be9f95fce8daaa5a56e44dd13de5
Encoding scheme:
Highest bit of each byte is color (black or white)
Lowest 7 bits of each byte is repetition of that color, with max of 125 / 0x7D
"""
# Tell PhotonFile we are drawing so GUI can prevent too many calls on getBitmap
memory = pygame.Surface((int(1440 * scale[0]), int(2560 * scale[1])))
if self.nrLayers()==0: return memory #could occur if loading new file
self.isDrawing = True
# Retrieve raw image data and add last byte to complete the byte array
bA = self.LayerData[layerNr]["Raw"]
# add endOfLayer Byte
bA = bA + self.LayerData[layerNr]["EndOfLayer"]
# Decode bytes to colors and draw lines of that color on the pygame surface
x = 0
y = 0
for idx, b in enumerate(bA):
# From each byte retrieve color (highest bit) and number of pixels of that color (lowest 7 bits)
nr = b & ~(1 << 7) # turn highest bit of
val = b >> 7 # only read 1st bit
# The surface to draw on is smaller (scale) than the file (1440x2560 pixels)
x1 = int(x *scale[0])
y1 = int(y *scale[1])
x2 = int((x + nr) *scale[0])
y2 = y1
if val==0:
col= backcolor
else:
col=forecolor
# Bytes and repetions of pixels with same color can span muliple lines (y-values)
if x2 > int(1440 *scale[0]): x2 = int(1440 *scale[1])
pygame.draw.line(memory, col, (x1, y1), (x2, y2))
# debug nr2=nr-(x+nr-1440) if (x+nr)>=1440 else nr
# debug print("draw line: ", x, y, " - ", nr2)
x = x + nr
if x >= 1440:
nr = x - 1440
x = 0
y = y + 1
x1 = int(x *scale[0])
y1 = int(y *scale[1])
x2 = int((x + nr) *scale[0])
y2 = y1
pygame.draw.line(memory, col, (x1, y1), (x2, y2))
# debug print ("draw line: ",x,y," - ",nr)
x = x + nr
#print("Screen Drawn")
# debug print ("layer: ", layerNr)
# debug print ("lastByte:", self.LayerData[layerNr]["EndOfLayer"])
# Done drawing so next caller knows that next call can be made.
self.isDrawing = False
return memory
def getBitmap(self, layerNr, forecolor=(128, 255, 128), backcolor=(0, 0, 0), scale=(1, 1)):
""" Depending on availability of Numpy, calls upon correct Decoding method."""
if numpyAvailable:
return self.getBitmap_withnumpy(layerNr,forecolor,backcolor,scale)
else:
return self.getBitmap_nonumpy(layerNr,forecolor,backcolor,scale)
def volume(self,progressDialog=None):
nLayers=self.nrLayers()
nrPixels=0
#numpyAvailable=False
for layerNr in range(0,nLayers):
img=self.getBitmap(layerNr,forecolor=(255,255,255,255),backcolor=(0,0,0,0),scale=(1,1))
pixarray = pygame.surfarray.pixels2d(img)
pixelsInLayer=0
if numpyAvailable:
pixelsInLayer=((pixarray>0).sum())
else:
for row in pixarray:
for color in row:
if color>0:pixelsInLayer+=1
nrPixels=nrPixels+pixelsInLayer
# Check if user canceled
if not progressDialog==None:
progressDialog.setProgress(100*layerNr/nLayers)
progressDialog.setProgressLabel(str(layerNr)+"/"+str(nLayers))
progressDialog.handleEvents()
if progressDialog.cancel: return False
#pixel is 0.047mm x 0.047mm x layer height
print (nrPixels)
pixVolume=0.047*0.047*self.bytes_to_float(self.Header["Layer height (mm)"])
volume=pixVolume*nrPixels
return volume
def getPreviewBitmap(self, prevNr, scaleToWidth=1440/4):
""" Decodes a RLE byte array from PhotonFile object to a pygame surface.
Based on https://github.com/Reonarudo/pcb2photon/issues/2
Encoding scheme:
The color (R,G,B) of a pixel spans 2 bytes (little endian) and each color component is 5 bits: RRRRR GGG GG X BBBBB
If the X bit is set, then the next 2 bytes (little endian) masked with 0xFFF represents how many more times to repeat that pixel.
"""
# Tell PhotonFile we are drawing so GUI can prevent too many calls on getBitmap
self.isDrawing = True
# Retrieve resolution of preview image and set pygame surface to that size.
w = PhotonFile.bytes_to_int(self.Previews[prevNr]["Resolution X"])
h = PhotonFile.bytes_to_int(self.Previews[prevNr]["Resolution Y"])
s = PhotonFile.bytes_to_int(self.Previews[prevNr]["Data Length"])
if scaleToWidth==0:
scale=(1,1)
else:
scale = (scaleToWidth / w, scaleToWidth / w)
memory = pygame.Surface((int(w), int(h)))
if w == 0 or h == 0: return memory # if size is (0,0) we return empty surface
# Retrieve raw image data and add last byte to complete the byte array
bA = self.Previews[prevNr]["Image Data"]
# Decode bytes to colors and draw lines of that color on the pygame surface
idx = 0
pixelIdx = 0
while idx < len(bA):
# Combine 2 bytes Little Endian so we get RRRRR GGG GG X BBBBB (and advance read byte counter)
b12 = bA[idx + 1] << 8 | bA[idx + 0]
idx += 2
# Retrieve colr components and make pygame color tuple
#red = round(((b12 >> 11) & 0x1F) / 31 * 255)
red = round(((b12 >> 11) & 0x1F) << 3 )
#green = round(((b12 >> 6) & 0x1F) / 31 * 255)
green = round(((b12 >> 6) & 0x1F) << 3 )
#blue = round(((b12 >> 0) & 0x1F) / 31 * 255)
blue = round((b12 & 0x1F) << 3 )
col = (red, green, blue)
# If the X bit is set, then the next 2 bytes (little endian) masked with 0xFFF represents how many more times to repeat that pixel.
nr = 1
if b12 & 0x20:
nr12 = bA[idx + 1] << 8 | bA[idx + 0]
idx += 2
nr += nr12 & 0x0FFF
# Draw (nr) many pixels of the color
for i in range(0, nr, 1):
x = int((pixelIdx % w))
y = int((pixelIdx / w))
memory.set_at((x, y), col)
pixelIdx += 1
# Scale the surface to the wanted resolution
memory = pygame.transform.scale(memory, (int(w * scale[0]), int(h * scale[1])))
# Done drawing so next caller knows that next call can be made.
self.isDrawing = False
return memory
########################################################################################################################
## Layer (Image) Operations
########################################################################################################################
def layerHeight(self,layerNr):
""" Return height between two layers
"""
# We retrieve layer height from previous layer
if layerNr>0:
curLayerHeight = self.bytes_to_float(self.LayerDefs[layerNr]["Layer height (mm)"])
prevLayerHeight = self.bytes_to_float(self.LayerDefs[layerNr-1]["Layer height (mm)"])
else:
if self.nrLayers()>1:
curLayerHeight = self.bytes_to_float(self.LayerDefs[layerNr+1]["Layer height (mm)"])
prevLayerHeight=0
else:
curLayerHeight=self.bytes_to_float(self.Header["Layer height (mm)"])
prevLayerHeight = 0
return curLayerHeight-prevLayerHeight
#print ("Delta:", deltaHeight)
def deleteLayer(self, layerNr, saveToHistory=True):
""" Deletes layer and its image data in the PhotonFile object, but store in clipboard for paste. """
# Store all data to history
if saveToHistory: self.saveToHistory("delete",layerNr)
#deltaHeight=self.bytes_to_float(self.LayerDefs[layerNr]["Layer height (mm)"])
deltaHeight =self.layerHeight(layerNr)
print ("deltaHeight:",deltaHeight)
# Update start addresses of RawData of before deletion with size of one extra layerdef (36 bytes)
for rLayerNr in range(0,layerNr):
# Adjust image address for removal of image raw data and end byte
curAddr=self.bytes_to_int(self.LayerDefs[rLayerNr]["Image Address"])
newAddr=curAddr-36 # size of layerdef
self.LayerDefs[rLayerNr]["Image Address"]= self.int_to_bytes(newAddr)
# Update start addresses of RawData of after deletion with size of image and layerdef
deltaLength = self.bytes_to_int(self.LayerDefs[layerNr]["Data Length"]) + 36 # +1 for len(EndOfLayer)
nLayers=self.nrLayers()
for rLayerNr in range(layerNr+1,nLayers):
# Adjust image address for removal of image raw data and end byte
curAddr=self.bytes_to_int(self.LayerDefs[rLayerNr]["Image Address"])
newAddr=curAddr-deltaLength
#print ("layer, cur, new: ",rLayerNr,curAddr,newAddr)
self.LayerDefs[rLayerNr]["Image Address"]= self.int_to_bytes(newAddr)
# Adjust layer starting height for removal of layer
curHeight=self.bytes_to_float(self.LayerDefs[rLayerNr]["Layer height (mm)"])
newHeight=curHeight-deltaHeight
self.LayerDefs[rLayerNr]["Layer height (mm)"] =self.float_to_bytes(newHeight)
# Store deleted layer in clipboard
self.clipboardDef=self.LayerDefs[layerNr].copy()
self.clipboardData=self.LayerData[layerNr].copy()
# Delete layer settings and data and reduce number of layers in header
self.LayerDefs.remove(self.LayerDefs[layerNr])
self.LayerData.remove(self.LayerData[layerNr])
self.Header[self.nrLayersString]=self.int_to_bytes(self.nrLayers()-1)
def insertLayerBefore(self, layerNr, fromClipboard=False, saveToHistory=True):
""" Inserts layer copying data of the previous layer or the clipboard. """
if fromClipboard and self.clipboardDef==None: raise Exception("Clipboard is empty!")
# Store all data to history
if saveToHistory: self.saveToHistory("insert",layerNr)
# Check if layerNr in range, could occur on undo after deleting last layer
# print(layerNr, "/", self.nrLayers())
insertLast=False
if layerNr>self.nrLayers(): layerNr=self.nrLayers()
if layerNr == self.nrLayers():
layerNr=layerNr-1 # temporary reduce layerNr
insertLast=True
# Check deltaHeight
deltaHeight = self.layerHeight(layerNr)
# Make duplicate of layerDef and layerData if not pasting from clipboard
if fromClipboard == False:
self.clipboardDef=self.LayerDefs[layerNr].copy()
self.clipboardData=self.LayerData[layerNr].copy()
# Set layerheight correctly
if layerNr==0: # if first layer than the height should start at 0
self.clipboardDef["Layer height (mm)"] = self.float_to_bytes(0)
else: # start at layer height of layer at which we insert
curLayerHeight = self.bytes_to_float(self.LayerDefs[layerNr]["Layer height (mm)"])
self.clipboardDef["Layer height (mm)"]=self.float_to_bytes(curLayerHeight)
# Set start addresses of layer in clipboard, we add 1 layer(def) so add 36 bytes
lA=self.bytes_to_int(self.LayerDefs[layerNr]["Image Address"])+36
# if lastlayer we need to add last image length
if insertLast: lA=lA+self.bytes_to_int(self.LayerDefs[layerNr]["Data Length"])
self.clipboardDef["Image Address"]=self.int_to_bytes(lA)
# If we inserting last layer, we correct layerNr
if insertLast: layerNr = layerNr + 1 # fix temporary reduced layerNr
# Update start addresses of RawData of before insertion with size of one extra layerdef (36 bytes)
for rLayerNr in range(0,layerNr):
# Adjust image address for removal of image raw data and end byte
curAddr=self.bytes_to_int(self.LayerDefs[rLayerNr]["Image Address"])
newAddr=curAddr+36 # size of layerdef
self.LayerDefs[rLayerNr]["Image Address"]= self.int_to_bytes(newAddr)
# Update start addresses of RawData of after insertion with size of image and layerdef
# Calculate how much room we need in between. We insert an extra layerdef (36 bytes) and a extra image
deltaLayerImgAddress = self.bytes_to_int(self.clipboardDef["Data Length"]) + 36
nLayers=self.nrLayers()
# remove
for rLayerNr in range(layerNr,nLayers):
# Adjust image address for removal of image raw data and end byte
curAddr=self.bytes_to_int(self.LayerDefs[rLayerNr]["Image Address"])
newAddr=curAddr+deltaLayerImgAddress
self.LayerDefs[rLayerNr]["Image Address"]= self.int_to_bytes(newAddr)
# Adjust layer starting height for removal of layer
curHeight=self.bytes_to_float(self.LayerDefs[rLayerNr]["Layer height (mm)"])
newHeight=curHeight+deltaHeight
self.LayerDefs[rLayerNr]["Layer height (mm)"] =self.float_to_bytes(newHeight)
#print ("layer, cur, new: ",rLayerNr,curAddr,newAddr, "|", curHeight,newHeight ,">",self.bytes_to_float(self.LayerDefs[rLayerNr]["Layer height (mm)"]))
# Insert layer settings and data and reduce number of layers in header
self.LayerDefs.insert(layerNr, self.clipboardDef)
self.LayerData.insert(layerNr, self.clipboardData)
self.Header[self.nrLayersString]=self.int_to_bytes(self.nrLayers()+1)
# Make new copy so second paste will not reference this inserted objects
self.clipboardDef = self.LayerDefs[layerNr].copy()
self.clipboardData = self.LayerData[layerNr].copy()
def copyLayer(self,layerNr):
# Make duplicate of layerDef and layerData
self.clipboardDef=self.LayerDefs[layerNr].copy()
self.clipboardData=self.LayerData[layerNr].copy()
def replaceBitmap(self, layerNr,filePath, saveToHistory=True):
""" Replace image data in PhotonFile object with new (encoded data of) image on disk."""
print(" ", layerNr, "/", filePath)
# Store all data to history
if saveToHistory: self.saveToHistory("replace",layerNr)
# Get/encode raw data
rawData = PhotonFile.encodedBitmap_Bytes(filePath)
# Last byte is stored seperately
rawDataTrunc = rawData[:-1]
rawDataLastByte = rawData[-1:]
# Get change in image rawData size so we can correct starting addresses of higher layer images
oldLength=self.bytes_to_int(self.LayerDefs[layerNr]["Data Length"]) #"Data Length" = len(rawData)+len(EndOfLayer)
newLength=len(rawData)
deltaLength=newLength-oldLength