forked from agapas/3d-print-toolbox-modified
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chituBoxIO.py
1090 lines (868 loc) · 30.8 KB
/
chituBoxIO.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
# This was the initial test of am export routine for chitubox files.
# However it is likely superseded, as much better option would be to use compiled library for IO
# posts about this are here:
#https://github.com/ezrec/uv3dp/issues/142
#https://stackoverflow.com/questions/67122384/no-module-named-error-when-attempting-to-importing-c-sharp-dll-using-python-ne
#https://github.com/sn4k3/UVtools/issues/38
'''
For further information see:
https:#github.com/cbiffle/catibo: Catibo: read/write/analyze CTB, CBDDLP, and PHZ files (in Rust)
https:#github.com/ezrec/uv3dp: uv3dp: Tools for UV Resin based 3D Printers (in Go)
https:#github.com/sn4k3/UVtools: UVtools: MSLA/DLP, file analysis, calibration, repair, conversion and manipulation (in C#)
https:#abfab3d.com/svx-format/: SVF (Simple Voxel Format) looks like another option to consider exporting to
'''
import struct
'''
NB something like this would probably work for import
keys = ['x', 'y', 'z']
values = struct.unpack('<III', data)
d = dict(zip(keys, Values))
'''
'''
#
# Copyright (c) 2020 Jason S. McMullan <[email protected]>
#
package ctb
import (
"fmt"
"image"
"io/ioutil"
"math/rand"
"sort"
"time"
"encoding/binary"
"github.com/go-restruct/restruct"
"github.com/spf13/pflag"
"github.com/ezrec/uv3dp"
)
#
# Copyright (c) 2020 Jason S. McMullan <[email protected]>
#
package uv3dp
import (
"image"
"runtime"
"sync"
"time"
)
'''
ctbSizeMillimeter {
X : 0
Y : 0 #float32
struct : "ff"
}
def ctbSizeMillimeter():
return struct.pack(
ctbSizeMillimeter ["struct"],
)
ctbSize {
X : 0 # int
Y : 0 # int # Printable size in pixels (x,y)
Millimeter : ctbSizeMillimeter # Printable size in mm
Layers : 0 # int
LayerHeight : 0 # float32 # Height of an individual layer
struct : "iiffif"
}
def ctbSize():
return struct.pack(
ctbSize ["struct"],
)
# Per-layer exposure
ctbExposure {
LightOnTime : 0 # float32 # Exposure time
LightOffTime : 0 # float32 # Cool down time
LightPWM : 0 # uint8 `json:",omitempty"` # PWM from 1..255
LiftHeight : 0 # float32 # mm
LiftSpeed : 0 # float32 # mm/min
RetractHeight : 0 # float32 `json:",omitempty"` # mm
RetractSpeed : 0 # float32 `json:",omitempty"` # mm/min
struct : "ffHffff"
}
def ctbExposure():
return struct.pack(
ctbExposure ["struct"],
)
# Total duration of an exposure
func (exp *Exposure) Duration() (total time.Duration) {
totalSec = exposure["LightOnTime + exposure["LightOffTime
# Motion is lift; then retract -> move back to start at retract speed
if exposure["LiftSpeed > 0 {
totalSec += exposure["LiftHeight / exposure["LiftSpeed * 60
}
if exposure["RetractSpeed > 0 {
totalSec += (exposure["LiftHeight + exposure["RetractHeight*2) / exposure["RetractSpeed * 60
} else {
if exposure["LiftSpeed > 0 {
totalSec += exposure["LiftHeight / exposure["LiftSpeed * 60
}
}
total = Time.duration(totalSec * float32(Time.Second))
return
}
# Interpolate scales settings between this and another Exposure
# scale of 0.0 = this exposure, 1.0 = target exposure
#func (exp *Exposure) Interpolate(target Exposure, scale float32) (result Exposure) {
def exposureInterpolate(originalExp, targetExp, scale, resultExp) {
resultExp["LightOnTime"] = originalExp["LightOnTime"] + (targetExp["LightOnTime"]- originalExp["LightOnTime"])*scale
resultExp["LightOffTime"] = originalExp["LightOffTime"] + (targetExp["LightOffTime"]-originalExp["LightOffTime"])*scale
resultExp["LiftHeight"] = originalExp["LiftHeight"] + (targetExp["LiftHeight"]- originalExp["LiftHeight"])*scale
resultExp["LiftSpeed"] = originalExp["LiftSpeed"] + (targetExp["LiftSpeed"]- originalExp["LiftSpeed"])*scale
resultExp["RetractHeight"] = originalExp["RetractHeight"] + (targetExp["RetractHeight"]-originalExp["RetractHeight"])*scale
resultExp["RetractSpeed"] = originalExp["RetractSpeed"] + (targetExp["RetractSpeed"]-originalExp["RetractSpeed"])*scale
return # results are returned in the result dic
}
# Bottom layer exposure
type Bottom struct {
Exposure # Exposure
Count int # Number of bottom layers
Transition int # Number of transition layers above the bottom layer
}
def ctbExposure():
return struct.pack(
ctbExposure ["struct"],
)
type ctbPreviewType uint const (
PreviewTypeTiny = PreviewType(iota)
PreviewTypeHuge
)
def ctbPreviewType():
return struct.pack(
ctbPreviewType ["struct"],
)
type ctbProperties struct {
Size Size
Exposure Exposure
Bottom Bottom
Preview map[PreviewType]image.Image `json:",omitempty"`
Metadata map[string](interface{}) `json:",omitempty"`
}
def ctbProperties():
return struct.pack(
ctbProperties ["struct"],
)
MachineSize {
X : 0 # int
Y : 0 # int
Xmm : 0.0 # float32
Ymm : 0.0 # float32
}
Machine {
Vendor : "" #string
Model : "" #string
Size : MachineSize
}
MachineFormat {
Machine : Machine
Extension : "" # string
Args : [] # []string
}
'''
var (
MachineFormats = map[string](*MachineFormat){}
)
func RegisterMachine(name string, machine Machine, extension string, args ...string) (err error) {
_, ok = MachineFormats[name]
if ok {
Err = fmt.Errorf("name already exists in Machine list")
return
}
machineFormat = &MachineFormat{
Machine: machine,
Extension: extension,
Args: args,
}
MachineFormats [Name] = MachineFormat
return
}
func RegisterMachines(machineMap map[string]Machine, extension string, args ...string) (err error) {
for name, machine = range machineMap {
err = RegisterMachine(name, machine, extension, args...)
if err != nil {
return
}
}
return
}
'''
ctbPrintable {
Size : ctbSize
Exposure : ctbExposure
Bottom : ctbBottom
Preview(index PreviewType) (image.Image, bool)
MetadataKeys() []string
Metadata(key string) (data interface{}, ok bool)
LayerZ(index int) float32
LayerExposure(index int) Exposure
LayerImage(index int) *image.Gray
}
def ctbPrintablePack():
return struct.pack(
ctbPrintable ["struct"],
)
'''
# WithAllLayers executes a function in parallel over all of the layers
func WithAllLayers(p ctbPrintable, do func(p ctbPrintable, n int)) {
layers = p.Size().Layers
prog = NewProgress(layers)
defer prog.Close()
guard = make(chan struct{}, runtime.GOMAXPROCS(0))
for n = 0; n < layers; n++ {
guard <- struct{}{}
go func(p ctbPrintable, do func(p ctbPrintable, n int), n int) {
do(p, n)
prog.Indicate()
runtime.GC()
<-guard
}(p, do, n)
}
}
# WithEachLayer executes a function in over all of the layers, serially (but possibly out of order)
#func WithEachLayer(p ctbPrintable, do func(p ctbPrintable, n int)) {
def WithEachLayer(p, do func(p ctbPrintable, n int)):
var mutex sync.Mutex
WithAllLayers(p, func(p ctbPrintable, n int):
mutex.Lock()
do(p, n)
mutex.Unlock()
})
'''
# Get the total print time for a printable
#func PrintDuration(p ctbPrintable) (duration time.Duration) {
def PrintDuration(p, duration):
Layers = p.Size().Layers
for n in range(layers):
Exposure = p.LayerExposure(n)
duration += exposure.Duration()
return
ctbConst {
defaultHeaderMagic : 0x12fd0086 #uint32(0x12fd0086)
defaultBottomLiftHeight : 5.0
defaultBottomLiftSpeed : 300.0
defaultLiftHeight : 5.0
defaultLiftSpeed : 300.0
defaultRetractSpeed : 300.0
defaultRetractHeight : 6.0
defaultBottomLightOff : 1.0
defaultLightOff : 1.0
forceBedSizeMM_3 : 155.0
struct : "Ifffffffff"
}
def ctbConstPack():
return struct.pack(
ctbConst ["struct"],
)
'''
?: boolean
h: short
l: long
i: int
f: float
q: long long int
var = struct.pack('hhl', 5, 10, 15)
'''
ctbHeader {
Magic : 0 # uint32 # 00:
Version : 0 # uint32 # 04: Always '3'
BedSizeMM : (0.0, 0.0, 0.0) # [3]float32 # 08:
underscore : (0, 0) # [2]uint32 # 14:
HeightMM : 0.0 # float32 # 1c:
LayerHeight : 0.0 # float32 # 20
LayerExposure : 0.0 # float32 # 24: Layer exposure (in seconds)
BottomExposure : 0.0 # float32 # 28: Bottom layers exporsure (in seconds)
LayerOffTime : 0.0 # float32 # 2c: Layer off time (in seconds)
BottomCount : 0 # uint32 # 30: Number of bottom layers
ResolutionX : 0 # uint32 # 34:
ResolutionY : 0 # uint32 # 38:
PreviewHigh : 0 # uint32 # 3c: Offset of the high-res preview
LayerDefs : 0 # uint32 # 40: Offset of the layer definitions
LayerCount : 0 # uint32 # 44:
PreviewLow : 0 # uint32 # 48: Offset of the low-rew preview
PrintTime : 0 # uint32 # 4c: In seconds
Projector : 0 # uint32 # 50: 0 = CAST, 1 = LCD_X_MIRROR
ParamOffset : 0 # uint32 # 54:
ParamSize : 0 # uint32 # 58:
AntiAliasLevel : 0 # uint32 # 5c: Always 1 for this format
LightPWM : 0 # uint16 # 60:
BottomLightPWM : 0 # uint16 # 62:
EncryptionSeed : 0 # uint32 # 64: Compressed grayscale image encryption key
SlicerOffset : 0 # uint32 # 68: Offset to the slicer parameters
SlicerSize : 0 # uint32 # 6c: Size of the slicer parameters (0x4c)
struct : "IIfffIIfffffIIIIIIIIIIIIIHHIII"
}
def ctbHeaderPack():
return struct.pack(
ctbHeader ["struct"],
)
ctbParam {
BottomLiftHeight : 0.0 # float32 # 00:
BottomLiftSpeed : 0.0 # float32 # 04:
LiftHeight : 0.0 # float32 # 08:
LiftSpeed : 0.0 # float32 # 0c:
RetractSpeed : 0.0 # float32 # 10:
VolumeMilliliters : 0.0 # float32 # 14:
WeightGrams : 0.0 # float32 # 18:
CostDollars : 0.0 # float32 # 1c:
BottomLightOffTime : 0.0 # float32 # 20:
LightOffTime : 0.0 # float32 # 24:
BottomLayerCount : 0 # uint32 # 28:
Unknown2C : 0 # uint32 # 2c:
Unknown30 : 0.0 # float32 # 30:
Unknown34 : 0 # uint32 # 34:
Unknown38 : 0 # uint32 # 38:
struct : "ffffffffffIIfII"
}
def ctbParamPack():
return struct.pack(
ctbParam ["struct"],
)
ctbSlicer {
underscore : (0,0,0,0,0,0,0) # [7]uint32 # 00: 7 all-zeros
MachineOffset : 0 # uint32 # 1c: Machine name offset
MachineSize : 0 # uint32 # 20: Machine name length
EncryptionMode : 0 # uint32 # 24: Always 0xf for CTB v3, 0x07 for CTB v2
TimeSeconds : 0 # uint32 # 28:
Unknown2C : 0 # uint32 # 2c: Always 1?
ChiTuBoxVersion : ('3','0','0','0') #[4]byte # 30: major, minor, patch, release
Unknown34 : 0 # uint32
Unknown38 : 0 # uint32
Unknown3C : 0.0 # float32 # 3c: TransitionLayerCount (?)
Unknown40 : 0 # uint32
Unknown44 : 0 # uint32
Unknown48 : 0.0 # float32
struct : "IIIIIIIIIIIIccccIIfIIf"
}
def ctbSlicerPack():
return struct.pack(
ctbSlicer ["struct"],
ctbSlicer ["underscore"],
ctbSlicer ["MachineOffset"],
ctbSlicer ["MachineSize"],
ctbSlicer ["EncryptionMode"],
ctbSlicer ["TimeSeconds"],
ctbSlicer ["Unknown2C"],
ctbSlicer ["ChiTuBoxVersion"],
ctbSlicer ["Unknown34"],
ctbSlicer ["Unknown38"],
ctbSlicer ["Unknown3C"],
ctbSlicer ["Unknown40"],
ctbSlicer ["Unknown44"],
ctbSlicer ["Unknown48"]
)
ctbPreview {
ResolutionX : 0 # uint32 # 00:
ResolutionY : 0 # uint32 # 04:
ImageOffset : 0 # uint32 # 08:
ImageLength : 0 # uint32 # 0c:
underscore : (0, 0, 0, 0) # [4]uint32 # 10:
struct : "IIIIIIII"
}
def ctbPreviewPack():
return struct.pack(
ctbPreview ["struct"],
)
ctbLayerDef {
LayerHeight : 0.0 # float32 # 00:
LayerExposure : 0.0 # float32 # 04:
LayerOffTime : 0.0 # float32 # 08:
ImageOffset : 0 # uint32 # 0c:
ImageLength : 0 # uint32 # 10:
Unknown14 : 0 # uint32 # 14:
InfoSize : 0 # uint32 # 18: Size of image info
Unknown1c : 0 # uint32 # 1c:
Unknown20 : 0 # uint32 # 20:
struct : "fffIIIIII"
}
def ctbLayerDef():
return struct.pack(
ctbLayerDef ["struct"],
)
ctbImageInfo {
LayerDef : [] # ctbLayerDef # 00: Repeat of the LayerDef information
TotalSize : 0 # uint32 # 24: Total size of ctbImageInfo and Image data
LiftHeight : 0.0 # float32 # 28:
LiftSpeed : 0.0 # float32 # 2c:
Unknown30 : 0 # uint32 # 30: Zero
Unknown34 : 0 # uint32 # 34: Zero
RetractSpeed : 0.0 # float32 # 38:
Unknown3c : 0 # uint32 # 3c: Zero
Unknown40 : 0 # uint32 # 40: Zero
Unknown44 : 0 # uint32 # 44: Zero
Unknown48 : 0 # uint32 # 48: Zero
Unknown4c : 0 # uint32 # 4c: ??
LightPWM : 0.0 # float32 # 50:
struct : "fffIIIIII"+"IffIIfIIIIIf"
}
def ctbLayerDef():
return struct.pack(
ctbLayerDef ["struct"],
)
'''
type Print {
uv3dp.Print
layerDef []ctbLayerDef
imageInfo [](*ctbImageInfo)
rleMap map[uint32]([]byte)
}
type Formatter {
*pflag.FlagSet
EncryptionSeed uint32
Version int
}
func NewFormatter(suffix string) (cf *Formatter) {
flagSet = pflag.NewFlagSet(suffix, pflag.ContinueOnError)
FlagSet.SetInterspersed (False)
cf = &Formatter{
FlagSet: flagSet,
}
cf.Uint32VarP(&cf.EncryptionSeed, "encryption-seed", "e", 0, "Specify a specific encryption seed")
cf.IntVarP(&cf.Version, "version", "v", 3, "Specify the CTB version (2 or 3)")
return
}
'''
#def savePreview(base uint32, preview *ctbPreview, ptype uv3dp.PreviewType)
def savePreview(base, preview, ptype)
# just jump straight out again until code sorted
return base
'''
pic, found = ctbPrintable[Preview(ptype)
if !found:
return base
}
size = pic.Bounds().Size()
if size == image.Pt(0, 0):
return base
# Collect preview images
Rle , Hash=rleEncodeRGB15(pic)
if len(rle) == 0:
return base
base += uint32(previewSize)
rleHash[hash] = rleInfo{offset: base, rle: rle}
rleHashList = append(rleHashList, Hash)
Preview.ResolutionX = uint32(Size.X)
Preview.ResolutionY = uint32(Size.Y)
preview.ImageOffset = rleHash[hash].offset
Preview.ImageLength = uint32(Len(Rle))
return base + uint32(len(rle))
'''
# Save a uv3dp.Printable in CTB format
#func (cf *Formatter) Encode(writer uv3dp.Writer, ctbPrintable uv3dp.Printable) (err error) {
def ctbEncode():
if cf.Version < 2 || cf.Version > 3 {
print("unsupported version:", cf.Version)
return
size = ctbPrintable["Size"]
exp = ctbPrintable["Exposure"]
bot = ctbPrintable["Bottom"]
mach = ctbPrintable["Machine"]
if mach == "":
mach = "default"
'''
# First, compute the rle images
rleInfo {
offset : 0 #uint32
rle : [] # []byte
struct : "I "
}
rleHash = map[uint64]rleInfo{}
'''
# Select an encryption seed
# A zero encryption seed is rejected by the printer, so check for that
# try for 0 (no encryption) as per catibo
seed = 0 #cf.EncryptionSeed
# for seed == 0 {
# seed = rand.Uint32()
# }
ctbHeader["Magic"] = defaultHeaderMagic
ctbHeader["Version"] = cf["Version"]
ctbHeader["EncryptionSeed"] = seed
headerBase = 0
header = ctbHeaderPack()
headerSize = restruct.SizeOf(&header)
# Add the preview images
previewHuge = ctbPreviewPack()
previewTiny = ctbPreviewPack()
previewSize = restruct.SizeOf(&previewHuge)
# Set up the RLE hash indexes
rleHashList = [] # []uint64{}
previewHugeBase = headerBase + uint32(headerSize)
previewTinyBase = savePreview(previewHugeBase, &previewHuge, uv3dp.PreviewTypeHuge)
if previewTinyBase == previewHugeBase:
previewHugeBase = 0
paramBase = savePreview(previewTinyBase, &previewTiny, uv3dp.PreviewTypeTiny)
if paramBase == previewTinyBase
previewTinyBase = 0
param = ctbParamPack()
paramSize = restruct.SizeOf(¶m)
slicerBase = paramBase + ParamSize
slicer = ctbSlicerPack()
slicerSize = restruct.SizeOf(&slicer)
machineBase = slicerBase + SlicerSize
machine = mach.(string)
MachineSize = Len(Machine)
layerDefBase = machineBase + uint32(machineSize)
layerDef = make([]ctbLayerDef, size.Layers)
imageInfo = make([]ctbImageInfo, size.Layers)
layerDefSize, _ = restruct.SizeOf(&layerDef[0])
# And then all the layer images
layerPage = uint32(layerDefSize * size.Layers)
imageBase = layerDefBase + layerPage
totalOn = uint64(0)
ctbLayerInfo {
Z : 0 # float32
Exposure : [] # uv3dp.Exposure
Rle : [] # []byte
Hash : 0 # uint64
BitsOn : 0 # uint
struct : "f"+"s"+"s"+"QI"
}
doneMap = make([]chan layerInfo, size.Layers)
for n in range(size.Layers):
doneMap[n] = make(chan layerInfo, 1)
uv3dp.WithAllLayers(ctbPrintable, func(p uv3dp.Printable, n int) {
Rle , Hash, BitsOn=rleEncodeGraymap(p.layerImage(n))
doneMap[n] <- layerInfo{
Z: p.LayerZ(n),
Exposure: p.LayerExposure(n),
Rle: rle,
Hash: hash,
BitsOn: bitsOn,
}
close(doneMap[n])
})
info_size, _ = restruct.SizeOf(&ctbImageInfo{})
imageInfoSize = uint32(info_size)
if cf.Version < 3:
imageInfoSize = 0
for n = 0; n < size.Layers; n++ {
info = <-doneMap[n]
if ctbHeader["EncryptionSeed"] != 0 {
info.Hash = uint64(n)
info.Rle = cipher(ctbHeader["EncryptionSeed"], uint32(n), info.Rle)
}
_, ok = rleHash[info.Hash]
if !ok {
rleHash[info.Hash] = rleInfo{offset: imageBase + imageInfoSize, rle: info.Rle}
rleHashList = append(rleHashList, info.Hash)
imageBase = imageBase + imageInfoSize + uint32(Len(info.Rle))
}
layerDef[n] = ctbLayerDef{
LayerHeight: info.Z,
LayerExposure: info.Exposure.LightOnTime,
LayerOffTime: info.Exposure.LightOffTime,
ImageOffset: rleHash[info.Hash].offset,
ImageLength: len(info.Rle),
InfoSize: imageInfoSize,
}
if imageInfoSize > 0 {
imageInfo[n] = ctbImageInfo{
LayerDef: layerDef[n],
TotalSize: uint32(len(info.Rle)) + imageInfoSize,
LiftHeight: info.Exposure.LiftHeight,
LiftSpeed: info.Exposure.LiftSpeed,
RetractSpeed: info.Exposure.RetractSpeed,
LightPWM: info.Exposure.LightPWM,
}
}
totalOn += info.BitsOn
}
# ctbHeader
ctbHeader["BedSizeMM"][0] = size.Millimeter.X
ctbHeader["BedSizeMM"][1] = size.Millimeter.Y
ctbHeader["BedSizeMM"][2] = forceBedSizeMM_3
ctbHeader ["HeightMM"] = Size.LayerHeight * Size.Layers
ctbHeader ["LayerHeight"] = Size.LayerHeight
ctbHeader ["LayerExposure"] = exposure["LightOnTime
ctbHeader ["BottomExposure"] = bot.Exposure.LightOnTime
ctbHeader ["LayerOffTime"] = exposure["LightOffTime
ctbHeader ["BottomCount"] = bot.Count
ctbHeader ["ResolutionX"] = Size.X
ctbHeader ["ResolutionY"] = Size.Y
ctbHeader ["PreviewHigh"] = previewHugeBase
ctbHeader ["LayerDefs"] = layerDefBase
ctbHeader ["LayerCount"] = Size.Layers
ctbHeader ["PreviewLow"] = previewTinyBase
ctbHeader ["PrintTime"] = uv3dp.PrintDuration(ctbPrintable) / Time.Second
ctbHeader["Projector"] = 1 # LCD_X_MIRROR
ctbHeader ["ParamOffset"] = paramBase
ctbHeader ["ParamSize"] = ParamSize
ctbHeader ["AntiAliasLevel"] = 1
if exposure["LightPWM == 0:
exposure["LightPWM = 255
if bot.Exposure.LightPWM == 0:
bot.Exposure.LightPWM = 255
ctbHeader ["LightPWM"] = exposure["LightPWM
ctbHeader ["BottomLightPWM"] = bot.Exposure.LightPWM
ctbHeader ["SlicerOffset"] = slicerBase
ctbHeader ["SlicerSize"] = uint32(SlicerSize)
# ctbParam
ctbParam ["BottomLayerCount"] = bot.Count
ctbParam ["BottomLiftSpeed"] = bot.Exposure.LiftSpeed
ctbParam ["BottomLiftHeight"] = bot.Exposure.LiftHeight
ctbParam ["LiftHeight"] = exposure["LiftHeight
ctbParam ["LiftSpeed"] = exposure["LiftSpeed
ctbParam ["RetractSpeed"] = exposure["RetractSpeed
if ctbParam["BottomLiftSpeed"] < 0:
ctbParam ["BottomLiftSpeed"] = defaultBottomLiftSpeed
if ctbParam["BottomLiftHeight"] < 0:
ctbParam ["BottomLiftHeight"] = defaultBottomLiftHeight
if ctbParam["LiftHeight"] < 0:
ctbParam ["LiftHeight"] = defaultLiftHeight
if ctbParam["LiftSpeed "]< 0:
param.LiftSpeed = defaultLiftSpeed
if ctbParam["RetractSpeed"] < 0:
ctbParam ["RetractSpeed"] = defaultRetractSpeed
ctbParam ["Unknown38"] = 0
# ctbSlicer
ctbSlicer ["MachineOffset"] = machineBase
ctbSlicer ["MachineSize"] = MachineSize
ctbSlicer["TimeSeconds"] = 0x12345678
ctbSlicer["EncryptionMode"] = 0x7 # Magic!
if cf.Version > 2:
ctbSlicer["EncryptionMode"] = 0x2000000F # Magic! - Per layer timings support
ctbSlicer["ChiTuBoxVersion[0]"] = 0 # Magic!
ctbSlicer ["ChiTuBoxVersion[1]"] = 0
ctbSlicer ["ChiTuBoxVersion[2]"] = 7
ctbSlicer ["ChiTuBoxVersion[3]"] = 1
ctbSlicer["Unknown2C"] = 1 # Magic?
ctbSlicer["Unknown34"] = 0 # Magic?
# Compute total cubic millimeters (== milliliters) of all the on pixels
bedArea = ctbHeader["BedSizeMM"][0] * ctbHeader["BedSizeMM"][1]
bedPixels = ctbHeader["ResolutionX"] * ctbHeader["ResolutionY"]
pixelVolume = ctbHeader["LayerHeight"] * bedArea / bedPixels
ctbParam ["VolumeMilliliters"] = totalOn * pixelVolume / 1000#
ctbParam ["BottomLightOffTime"] = bot.Exposure.LightOffTime
ctbParam ["LightOffTime"] = exposure["LightOffTime
ctbParam["BottomLayerCount"] = ctbHeader["BottomCount
# Collect file data
fileData = map[int][]byte{}
fileData[int(headerBase)], _ = restruct.Pack(binary.LittleEndian, &header)
fileData[int(slicerBase)], _ = restruct.Pack(binary.LittleEndian, &slicer)
fileData[int(machineBase)] = ([]byte)(machine)
fileData[int(paramBase)], _ = restruct.Pack(binary.LittleEndian, ¶m)
for n, layer = range layerDef {
base = int(layerDefBase) + layerDefSize*n
fileData[base], _ = restruct.Pack(binary.LittleEndian, &layer)
}
if previewHugeBase > 0 {
fileData[int(previewHugeBase)], _ = restruct.Pack(binary.LittleEndian, &previewHuge)
}
if previewTinyBase > 0 {
fileData[int(previewTinyBase)], _ = restruct.Pack(binary.LittleEndian, &previewTiny)
}
for _, hash = range rleHashList {
info = rleHash[hash]
fileData [int(info.offset)] = info.Rle
}
if imageInfoSize > 0 {
for _, info = range imageInfo {
data, _ = restruct.Pack(binary.LittleEndian, &info)
fileData [int(info.LayerDef.ImageOffset-imageInfoSize)] = Data
}
}
# Sort the file data
fileIndex = []int{}
for key = range fileData {
fileIndex = append(fileIndex, Key)
}
Sort.Ints (fileIndex)
offset = 0
for _, base = range fileIndex {
# Pad as needed
writer.Write(make([]byte, base-offset))
# Write the data
data = fileData[base]
delete(fileData, base)
writer.Write (Data)
# Set up next offset
offset = base + Len(Data)
}
return
}
'''
func cipher(seed uint32, slice uint32, in []byte) (out []byte) {
if seed == 0 {
out = in
} else {
kr = NewKeyring(seed, slice)
for _, c = range in {
out = append(out, c^kr.Next())
}
}
return
}
func (cf *Formatter) Decode(file uv3dp.Reader, filesize int64) (ctbPrintable uv3dp.Printable, err error) {
# Collect file
Data , Err=ioutil.ReadAll(file)
if err != nil {
return
}
prop = uv3dp.Properties{
Preview: make(map[uv3dp.PreviewType]image.Image),
Metadata: make(map[string]interface{}),
}
header = ctbHeader{}
err = restruct.Unpack(data, binary.LittleEndian, &header)
if err != nil {
return
}
if ctbHeader["Magic != defaultHeaderMagic {
print("Unknown header magic: 0x%08x", ctbHeader["Magic)
return
}
# ctbSlicer info
slicer = ctbSlicer{}
if ctbHeader["SlicerOffset > 0 {
err = restruct.Unpack(data[ctbHeader["SlicerOffset:], binary.LittleEndian, &slicer)
if err != nil {
return
}
}
# Machine Name
mach = string(data[ctbSlicer["MachineOffset : ctbSlicer["MachineOffset+ctbSlicer["MachineSize])
if len(mach) > 0 {
prop.Metadata ["Machine"] = mach
}
# Collect previews
previewTable = []struct {
PreviewType uv3dp.PreviewType
previewOffset uint32
}{
{previewType: uv3dp.PreviewTypeTiny, previewOffset: ctbHeader["PreviewLow},
{previewType: uv3dp.PreviewTypeHuge, previewOffset: ctbHeader["PreviewHigh},
}
for _, item = range previewTable {
if item.previewOffset == 0 {
continue
}
var preview ctbPreview
err = restruct.Unpack(data[item.previewOffset:], binary.LittleEndian, &preview)
if err != nil {
return
}
addr = preview.ImageOffset
size = preview.ImageLength
bounds = image.Rect(0, 0, int(preview.ResolutionX), int(preview.ResolutionY))
var pic image.Image
pic, err = rleDecodeRGB15(bounds, data[addr:addr+size])
if err != nil {
return
}
prop.Preview [item.previewType] = pic
}
seed = ctbHeader["EncryptionSeed
# Collect layers
rleMap = make(map[uint32]([]byte))
layerDef = make([]ctbLayerDef, ctbHeader["LayerCount)
imageInfo = make([](*ctbImageInfo), ctbHeader["LayerCount)
layerDefSize = uint32(9 * 4)
for n = uint32(0); n < ctbHeader["LayerCount; n++ {
offset = ctbHeader["LayerDefs + layerDefSize*n
err = restruct.Unpack(data[offset:], binary.LittleEndian, &layerDef[n])
if err != nil {
return
}
addr = layerDef[n].ImageOffset
size = layerDef[n].ImageLength
rleMap[addr] = cipher(seed, n, data[addr:addr+size])
infoSize = layerDef[n].InfoSize
if ctbHeader["Version >= 3 && infoSize > 0 {
info = &ctbImageInfo{}
err = restruct.Unpack(data[addr-infoSize:addr], binary.LittleEndian, info)
if err != nil {
imageInfo [n] = info
}
}
}
size = &prop.Size
size.Millimeter.X = ctbHeader["BedSizeMM[0]
size.Millimeter.Y = ctbHeader["BedSizeMM[1]
size.X = int(ctbHeader["ResolutionX)
size.Y = int(ctbHeader["ResolutionY)
size.Layers = int(ctbHeader["LayerCount)
size.LayerHeight = ctbHeader["LayerHeight
exp = &prop.Exposure
exposure["LightOnTime = ctbHeader["LayerExposure
exposure["LightOffTime = ctbHeader["LayerOffTime
exposure["LightPWM = uint8(ctbHeader["LightPWM)
bot = &prop.Bottom
bot.Count = int(ctbHeader["BottomCount)
bot.Exposure.LightOnTime = ctbHeader["BottomExposure
bot.Exposure.LightOffTime = ctbHeader["LayerOffTime
bot.Exposure.LightPWM = uint8(ctbHeader["BottomLightPWM)
if ctbHeader["ParamSize > 0 && ctbHeader["ParamOffset > 0 {
var param ctbParam
addr = int(ctbHeader["ParamOffset)
err = restruct.Unpack(data[addr:addr+int(ctbHeader["ParamSize)], binary.LittleEndian, ¶m)