-
Notifications
You must be signed in to change notification settings - Fork 3
/
artal.lua
854 lines (705 loc) · 25.7 KB
/
artal.lua
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
--[[
The MIT License (MIT)
Copyright (c) 2016 Daniel Rasmussen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
local ffi = require("ffi")
local utf8 = require("utf8")
local artalFunction = {}
artalFunction.version = "1.2"
--[[~~~~~~~~~~~~~~~~~~ Version 1.1 change log ~~~~~~~~~~~~~~~~~~
-- Fixed error in the image loading routine that caused it to incorrectly
determine the size of image. Thus loading garbage data.
~~~~~~~~~~~~~~~~~~~~~~ Version 1.2 change log ~~~~~~~~~~~~~~~~~~
-- Thanks to s-ol for this pull request. https://github.com/s-ol
-- Added support for Unicode / longer names in layers.
-- Added a test suite
-- Hierarchy test
-- Unicode test
-- Folder unicode name test
--]]
local function hexToNumber(first,second,third,forth) -- first is the direct hex -- Lack 8 length support
local result = first
if forth ~= nil then
result = result + second * ( 0xff +1)
result = result + third * ( 0xffff +1)
result = result + forth * ( 0xffffff +1)
elseif third ~= nil then
result = result + second * ( 0xff +1)
result = result + third * ( 0xffff +1)
elseif second ~= nil then
result = result + second * ( 0xff +1)
end
return result
end
local function directFileReader(C_fileData)
local self = {}
self.count = 0
function self:inkString(name , stringLength , stepLength)
-- Step length needs to grab 2 bytes in some cases.
if stepLength == nil then stepLength = 1 end
assert(self[name] == nil , "Artal: Name already taken.")
local resultString = ""
for i = 1 , stringLength do
local first = C_fileData[self.count]
local second
local third
local forth
if stepLength == 2 then
second = C_fileData[self.count+1]
elseif stepLength == 4 then
second = C_fileData[self.count+1]
third = C_fileData[self.count+2]
forth = C_fileData[self.count+3]
end
local resultStringAdd = hexToNumber(first,second,third,forth)
self.count = self.count + stepLength
--print(resultStringAdd)
if resultStringAdd > 31 and resultStringAdd < 127 then
--resultString = resultString.." \""..tostring(resultStringAdd).." "
resultString = resultString..string.char(resultStringAdd)
end
end
if name ~= nil then
self[name] = resultString
end
--print(name..": ",self[name])
return resultString
end
function self:inkUnicodeString(name , stringLength)
-- Step length needs to grab 2 bytes in some cases.
assert(self[name] == nil , "Artal: Name already taken.")
self.count = self.count + 4
stringLength = (stringLength - 4) / 2
local result = {}
for i = 1 , stringLength do
local first = C_fileData[self.count]
local second = C_fileData[self.count+1]
local byte = hexToNumber(second, first)
self.count = self.count + 2
if byte ~= 0 then
table.insert(result, byte)
end
end
result = utf8.char(unpack(result))
if name ~= nil then
self[name] = result
end
return result
end
function self:ink(name , stepLength, div) -- Lack 8 length support
assert(stepLength == 1 or stepLength == 2 or stepLength == 4 ,"Artal: ink must be a power of 2. No higher than 4.")
local first = C_fileData[self.count]
local second
local third
local forth
if stepLength == 2 then
second = C_fileData[self.count+1]
first , second = second , first
elseif stepLength == 4 then
second = C_fileData[self.count+1]
third = C_fileData[self.count+2]
forth = C_fileData[self.count+3]
first , second , third , forth = forth, third , second , first
end
if div ~= "don't count" then self.count = self.count + stepLength end
local resultString = hexToNumber(first,second,third,forth)
if div == "int" and resultString >= 2^(stepLength*8-1) then
resultString = resultString - 2^(stepLength*8)
end
if name == nil then
return resultString
end
if name == tonumber(name) then
name = tonumber(name)
end
assert(self[name] == nil , "Artal: Name already taken.")
self[name] = resultString
--print(name..": ",self[name])
end
--[[
function self:ink__SLOWANDCLEAN(name , stepLength, div) -- Lack 8 length support, Should create a C-type int64_t
assert(stepLength == 1 or stepLength == 2 or stepLength == 4 ,"Artal: ink must be a power of 2. No higher than 4.")
local result = 0
for i = stepLength, 1, -1 do
result = result + C_fileData[self.count-1+i] * 2^((stepLength-i)*8)
end
if div ~= "don't count" then self.count = self.count + stepLength end
if div == "int" and result >= 128 then
assert(stepLength == 1, "Artal: Function :ink does not handle \"int\" longer than 1 byte.")
result = result - 256
end
if name == nil then
return result
end
if name == tonumber(name) then
name = tonumber(name)
end
assert(self[name] == nil , "Artal: Name already taken.")
self[name] = result
--print(name..": ",self[name])
end
--]]
return self
end
local function artalNewLayerImageData(layerLoadData,askIsImageLayer)
local commonAssertMsg = ", make sure to only pass in data formed by the newPSD() function."
assert(layerLoadData.fileData ~= nil, "Artal: newLayer layerLoadData.fileData is nil"..commonAssertMsg)
assert(layerLoadData.fileData:type() == "FileData", "Artal: newLayer layerLoadData.fileData is not FileData"..commonAssertMsg)
assert(layerLoadData ~= nil, "Artal: newLayer layerLoadData is nil"..commonAssertMsg)
local image = layerLoadData
local C_fileData = ffi.cast("uint8_t *", image.fileData:getPointer())
local artal = directFileReader(C_fileData)
assert(artal:inkString(nil,4) == "8BPS", "Artal: newLayer layerLoadData.fileData is not a .psd file"..commonAssertMsg)
artal.count = artal.count - 4
local clampX = 0
if image.left < 0 then
clampX = image.left
--print("Clamped at X:",clampX)
end
local clampY = 0
if image.top < 0 then
clampY = image.top
--print("Clamped at Y:",clampY)
end
local clampW
if image.right > image.globalWidth then
clampW = image.globalWidth - image.left
--print("Clamped at W:",clampW)
else
clampW = image.right - image.left
end
local clampH
if image.bottom > image.globalHeight then
clampH = image.globalHeight - image.top
--print("Clamped at H:",clampH)
else
clampH = image.bottom - image.top
end
local dataSize = clampW*clampH*4
-- Shouldn't ever be negative. But the docs doesn't spesify.
if dataSize < 0 then dataSize = 0 end
if askIsImageLayer then
if dataSize == 0 then
return false
else
return true -- early out
end
end
local C_data
local imageData
if dataSize ~= 0 then
imageData = love.image.newImageData(clampW + clampX, clampH + clampY)
C_data = ffi.cast("char *", imageData:getPointer())
end
local lineInfo = {}
--print(clampW,clampH)
--print(image.channelPointer[1])
--print(image.channelPointer[2])
--print(image.channelPointer[3])
--print(image.channelPointer[4])
--print(image.left, image.right)
for CC = 1 , #image.channelID do
artal.count = image.channelPointer[CC]
local channelCompression = artal:ink(nil,2)
local alphaOpacity = 1
local cPixelPos
if image.channelID[CC] == -1 then
cPixelPos = 3
alphaOpacity = image.opacity / 255
elseif image.channelID[CC] == 0 then
cPixelPos = 0
elseif image.channelID[CC] == 1 then
cPixelPos = 1
elseif image.channelID[CC] == 2 then
cPixelPos = 2
else
assert(false,"Artal: Unsupported channel id/mode.")
end
if channelCompression == 1 then
for LINE = 1 , image.totalHeight do
-- Bytes per line
lineInfo[LINE] = artal:ink(nil,2)
--print(CC,lineInfo[LINE] )
end
local yPixelPos = clampY
for LINE = 1, image.totalHeight do
--print(LINE)
local countEnd = artal.count + lineInfo[LINE]
--print(artal.count , countEnd,lineInfo[LINE])
--print("INTO")
--print("line",LINE,yPixelPos)
if yPixelPos >= 0 then
if yPixelPos < clampH + clampY then
local xPixelPos = clampX
while artal.count < countEnd do
local headByte = artal:ink(nil,1,"int")
--print("head",headByte)
if headByte >= 0 then
for i = 1, 1 + headByte do
-- Literal pixels
local pixelValue = artal:ink(nil,1) * alphaOpacity
if xPixelPos >= 0 and xPixelPos < clampX+clampW then
-- Here be valid pixels
local pos = cPixelPos + (xPixelPos*4) + (yPixelPos*(clampW+clampX)*4)
--print(CC,xPixelPos,yPixelPos,pos,pixelValue,(clampW+clampX))
C_data[pos] = pixelValue
end
xPixelPos = xPixelPos + 1
end
elseif headByte > -128 then
local pixelValue = artal:ink(nil,1) * alphaOpacity
for i = 1 , 1 - headByte do
-- Repeat pixels
if xPixelPos >= 0 and xPixelPos < clampX+clampW then
-- Here be valid pixels
local pos = cPixelPos + (xPixelPos*4) + (yPixelPos*(clampW+clampX)*4)
--print(CC,xPixelPos,yPixelPos,pos,pixelValue,(clampW+clampX))
C_data[pos] = pixelValue
end
xPixelPos = xPixelPos + 1
end
else
artal:ink(nil,1)
end
end
--assert(artal.count == countEnd,"Artal: Error reading layerdata.")
else
--print(yPixelPos)
break -- All pixels you want are now filled.
end
else
artal.count = countEnd
end
yPixelPos = yPixelPos + 1
end
elseif channelCompression == 0 then
--if(CC == 2) then
--artal.count = (yPixelPos*clampW*4)
for y = -clampY, clampH - 1 do
for x = -clampX, clampW - 1 do
--print(x,y)
local pixelPos = (y*(clampW)+x)*4+cPixelPos
local dataPos = y*(clampW)+x
C_data[pixelPos] = C_fileData[artal.count + dataPos] * alphaOpacity
--print(x, y, pixelPos, dataPos, C_fileData[artal.count + dataPos])
end
end
--end
else
assert(false, "Artal: Unsupported compression mode:"..channelCompression)
end
end
if #image.channelID == 3 then -- Set opacity.
for i = 0, dataSize,4 do
C_data[i+3] = image.opacity
end
end
if dataSize == 0 then
return nil
else
return imageData
end
end
local function defaultLoadImageFunction(artalLayer,layerLoadData,folderStack,layerNumber)
local layer = {}
layer.name = artalLayer.betterName
layer.blend = artalLayer.betterBlend
if artalLayer.betterCliping == 0 then
layer.clip = false
else
layer.clip = true
end
local imageData = artalNewLayerImageData(layerLoadData)
if imageData then
layer.image = love.graphics.newImage(imageData)
if layer.clip then
layer.image:setWrap("clampzero")
end
layer.type = "image"
layer.ox = 0
layer.oy = 0
if artalLayer.left > 0 then
layer.ox = -artalLayer.left
end
if artalLayer.top > 0 then
layer.oy = -artalLayer.top
end
else
layer.type = "empty"
end
return layer
end
local function defaultLoadImageInfoFunction(artalLayer,layerLoadData,folderStack,layerNumber)
local layer = {}
layer.name = artalLayer.betterName
layer.blend = artalLayer.betterBlend
if artalLayer.betterCliping == 0 then
layer.clip = false
else
layer.clip = true
end
local thereExistImageData = artalNewLayerImageData(layerLoadData,true)
if thereExistImageData then
layer.type = "image"
layer.ox = 0
layer.oy = 0
if artalLayer.left > 0 then
layer.ox = -artalLayer.left
end
if artalLayer.top > 0 then
layer.oy = -artalLayer.top
end
else
layer.type = "empty"
end
return layer
end
local function defaultLoadFolderFunction(artalLayer,layerLoadData)
local layer = {}
layer.name = artalLayer.betterName
layer.blend = artalLayer.betterBlend
if artalLayer.betterCliping == 0 then
layer.clip = false
else
layer.clip = true
end
if artalLayer.folder == 3 then
layer.type = "open"
elseif artalLayer.folder == 1 or artalLayer.folder == 2 then
layer.type = "close"
end
return layer
end
function artalFunction.newPSD(fileNameOrData, structureFlagOrNumber)
local GC_C_fileData
if type(fileNameOrData) == "string" then
assert(love.filesystem.getInfo(fileNameOrData, "file") ~= nil, "Artal: PSD file do not exist.")
GC_C_fileData = love.filesystem.newFileData(fileNameOrData)
elseif type(fileNameOrData) == "userdata" and fileNameOrData:type() == "FileData" then
GC_C_fileData = fileNameOrData
else
assert(false,"Artal: fileName is not a string or FileData.")
end
local C_fileData = ffi.cast("uint8_t *", GC_C_fileData:getPointer())
local artal = directFileReader(C_fileData)
artal.startPoint = {}
artal.warning = {}
-- Header
assert(artal:inkString(nil,4) == "8BPS" , "Artal: File must be a .psd.")
assert(artal:ink(nil,2) == 1 , "Artal: File must be a .psd")
artal.count = artal.count + 6 --Reserved space
artal:ink("channelCount",2)
artal:ink("height",4)
artal:ink("width",4)
artal:ink("depth",2)
assert(artal.depth == 8, "Artal: Image must be a 8 bit image")
artal:ink("colorMode",2)
assert(artal.colorMode == 3, "Artal: RGB is the only supported color mode")
--Color Mode
artal:ink("colorModeLength",4)
assert(artal.colorModeLength == 0 ,"Artal: Color Mode Length is not 0 as expected.")
--Image Resources Section
artal:ink("imageResourcesLength",4)
artal.startPoint.layerMask = artal.count + artal.imageResourcesLength
assert(artal:inkString(nil,4) == "8BIM" , "Artal: imageResources Signature is not correct.")
artal:ink("imageResourcesID",2)
-- Stub
--Layer and Mask
artal.count = artal.startPoint.layerMask
--Layer and mask information section
artal:ink("layerLength",4)
artal.startPoint.imageData = artal.count + artal.layerLength
--Layer info
artal:ink("layerTotalLength",4)
artal:ink("layerTotalCount",2,"int")-- Don't really know why this can be a negative number.
artal.layer = {}
--print(artal.layerTotalCount)
local opacityBakingTable = {}
for LC = 1 , math.abs(artal.layerTotalCount) do
artal.layer[LC] = directFileReader(C_fileData)
artal.layer[LC].count = artal.count
--print("artal.layer["..LC.."]")
--Layer records
artal.layer[LC]:ink("top",4,"int")
artal.layer[LC]:ink("left",4,"int")
artal.layer[LC]:ink("bottom",4,"int")
artal.layer[LC]:ink("right",4,"int")
artal.layer[LC]:ink("channelCount",2)
assert(artal.layer[LC].channelCount == 4 or artal.layer[LC].channelCount == 3,
"Artal: Only supports 3 or 4 channels." )
artal.layer[LC].channel = {}
for CC = 1 , artal.layer[LC].channelCount do
artal.layer[LC].channel[CC] = directFileReader(C_fileData)
artal.layer[LC].channel[CC].count = artal.layer[LC].count
artal.layer[LC].channel[CC]:ink("id",2,"int")
assert(
artal.layer[LC].channel[CC].id == 0 or
artal.layer[LC].channel[CC].id == 1 or
artal.layer[LC].channel[CC].id == 2 or
artal.layer[LC].channel[CC].id == -1,
"Artal: Unsupported channel ID: "..artal.layer[LC].channel[CC].id)
artal.layer[LC].channel[CC]:ink("length",4)
artal.layer[LC].count = artal.layer[LC].channel[CC].count
end
assert(artal.layer[LC]:inkString(nil,4) == "8BIM" , "Artal: Layer Records Signature is not correct.")
artal.layer[LC]:inkString("blend",4)
artal.layer[LC].blendShortName = string.gsub(artal.layer[LC].blend," ","")
artal.layer[LC]:ink("opacity",1)
artal.layer[LC].bakedOpacity = artal.layer[LC].opacity -- This is modified later if folders has opacity.
artal.layer[LC]:ink("clipping",1)
artal.layer[LC]:ink("flags",1) --Prehaps make a inkBits that expose the 8 bits in a hex
artal.layer[LC].count = artal.layer[LC].count + 1 -- Pad
artal.layer[LC]:ink("additionalLength",4)
--Layer mask / adjustment layer data
artal.layer[LC]:ink("maskAdjustment",4)
assert(artal.layer[LC].maskAdjustment == 0, "Artal: maskAdjustment is not 0 as expected.")
--Layer blending ranges data
artal.layer[LC]:ink("blendingRangeLength",4) -- Jumping over this
artal.layer[LC].count = artal.layer[LC].count + artal.layer[LC].blendingRangeLength
--Layer Name
artal.layer[LC]:ink("nameLength",1)
artal.layer[LC]:inkString("name",artal.layer[LC].nameLength)
-- This is modified later if the folder is "close" so folders open/close have the same name / blend / clip.
artal.layer[LC].betterName = artal.layer[LC].name
artal.layer[LC].betterBlend = artal.layer[LC].blendShortName
artal.layer[LC].betterCliping = artal.layer[LC].clipping
--Pascal String padding
if ((artal.layer[LC].nameLength+1)/4) ~= math.floor((artal.layer[LC].nameLength+1)/4) then
artal.layer[LC].count = artal.layer[LC].count + math.ceil((artal.layer[LC].nameLength+1)/4)*4 - ((artal.layer[LC].nameLength+1)/4)*4
end
local syncFolderNameFlag = false
-- Additional Layer Information
while artal.layer[LC]:inkString(nil , 4) == "8BIM" do
local key = artal.layer[LC]:inkString(nil,4)
local length = artal.layer[LC]:ink(nil,4)
--print("key: "..key.." , Length: "..length)
if key == "luni" then
artal.layer[LC].betterName = artal.layer[LC]:inkUnicodeString("luniName",length)
elseif key == "lnsr" then
artal.layer[LC]:inkString("layerID",4)
elseif key == "lyid" then
artal.layer[LC]:ink("lyid",4) -- Seems errorious
elseif key == "clbl" then
artal.layer[LC]:ink("blendClippedElements",1) -- true == 1 / false == 0
artal.layer[LC].count = artal.layer[LC].count + 3
elseif key == "infx" then
artal.layer[LC]:ink("blendInteriorElements",1) -- true == 1 / false == 0
artal.layer[LC].count = artal.layer[LC].count + 3
elseif key == "knko" then
artal.layer[LC]:ink("knockoutSetting",1) -- true == 1 / false == 0
artal.layer[LC].count = artal.layer[LC].count + 3
elseif key == "lspf" then
artal.layer[LC]:ink("protectionFlags",4)
elseif key == "lclr" then
artal.layer[LC]:ink("sheetColorSetting",4)
artal.layer[LC].count = artal.layer[LC].count + 4 -- Pad
elseif key == "shmd" then
artal.layer[LC].count = artal.layer[LC].count + length -- Undocumented
elseif key == "fxrp" then
artal.layer[LC]:ink("referencePoint1",4)
artal.layer[LC]:ink("referencePoint1big",4)
artal.layer[LC]:ink("referencePoint2",4)
artal.layer[LC]:ink("referencePoint2big",4)
elseif key == "lsct" then
artal.layer[LC]:ink("folder",4)
if artal.layer[LC].folder == 3 then
table.insert(opacityBakingTable,LC)
elseif artal.layer[LC].folder == 1 or artal.layer[LC].folder == 2 then
-- There is no guarantee that "luniName" has been set at this point
-- so I defer the renaming of folders until after all flags have been read.
syncFolderNameFlag = true
end
if length >= 12 then
assert(artal.layer[LC]:inkString(nil,4) == "8BIM" , "Artal: lsct signature is not correct.")
artal.layer[LC]:inkString("sectionDividerBlend",4)
end
if length >= 16 then
artal.layer[LC]:ink("sectionDividerType",4)
end
elseif key == "lyvr" then
artal.layer[LC]:ink("layerVersion",4)
elseif key == "lfx2" then
assert(artal.layer[LC]:ink(nil,4) == 0 , "Artal: objectivVersion is not 0 as expected.")
artal.layer[LC]:ink("descriptorVersion",4)
--Stub
artal.layer[LC].count = artal.layer[LC].count - 8 + length
elseif key == "lrFX" then
--Stub
artal.layer[LC].count = artal.layer[LC].count + length
else
artal.warning[key] = "Key: \""..key.."\" is not yet handled."
artal.layer[LC].count = artal.layer[LC].count + length
--assert(false,"Artal: Key: \""..key.."\" is not yet handled.")
end
end
if syncFolderNameFlag == true then
local startCounter = opacityBakingTable[#opacityBakingTable]
artal.layer[startCounter].betterName = artal.layer[LC].betterName
artal.layer[startCounter].betterBlend = artal.layer[LC].betterBlend
artal.layer[startCounter].betterCliping = artal.layer[LC].betterClipping
for LCback = startCounter, LC - 1 do
artal.layer[LCback].bakedOpacity =
artal.layer[LCback].bakedOpacity * (artal.layer[LC].opacity/255)
--print(artal.layer[LCback].name,artal.layer[LCback].bakedOpacity)
end
table.remove(opacityBakingTable)
end
-- Back up the last false while loop
artal.layer[LC].count = artal.layer[LC].count - 4
artal.count = artal.layer[LC].count
end
if math.abs(artal.layerTotalCount) > 0 then
local startPointer = artal.count
for LC = 1 , math.abs(artal.layerTotalCount) do
for CC = 1 , artal.layer[LC].channelCount do
artal.layer[LC].channel[CC].pointer = startPointer
startPointer = startPointer + artal.layer[LC].channel[CC].length
end
end
end
if structureFlagOrNumber == "composed" then
artal.count = artal.startPoint.imageData
artal.channel = {}
local imageData = love.image.newImageData(artal.width,artal.height)
local C_data = ffi.cast("uint8_t *", imageData:getPointer())
local cPixelPos
assert(artal.channelCount == 3 or artal.channelCount == 4, "Artal: Composed image only supports 3 or 4 channels of imagedata.")
artal:ink("compression",2)
if artal.compression == 1 then
for CC = 1, artal.channelCount do
artal.channel[CC] = directFileReader(C_fileData)
artal.channel[CC].count = artal.count
artal.channel[CC].lineInfo = {}
for LINE = 1 , artal.height do
-- Bytes per line
artal.channel[CC].lineInfo[LINE] = artal.channel[CC]:ink(nil,2)
--print(artal.channel[CC].lineInfo[LINE],LINE)
end
artal.count = artal.channel[CC].count
end
for CC = 1, artal.channelCount do
artal.channel[CC].count = artal.count
local cPixelPos = CC - 1
for LINE = 1, artal.height do
local countEnd = artal.channel[CC].count + artal.channel[CC].lineInfo[LINE]
local yPixelPos = LINE - 1
local xPixelPos = 0
while artal.channel[CC].count < countEnd do -- These two should end up equal
local headByte = artal.channel[CC]:ink(nil,1,"int")
--print("head",headByte)
if headByte >= 0 then
for i = 1, 1 + headByte do
-- Literal pixels
local pixelValue = artal.channel[CC]:ink(nil,1)
-- Here be valid pixels
C_data[cPixelPos + (xPixelPos*4) + (yPixelPos*artal.width*4)] = pixelValue
--print(CC,xPixelPos,yPixelPos,cPixelPos)
xPixelPos = xPixelPos + 1
end
elseif headByte > -128 then
local pixelValue = artal.channel[CC]:ink(nil,1)
for i = 1 , 1 - headByte do
-- Repeat pixels
-- Here be valid pixels
C_data[cPixelPos + (xPixelPos*4) + (yPixelPos*artal.width*4)] = pixelValue
--print(CC,xPixelPos,yPixelPos,cPixelPos)
xPixelPos = xPixelPos + 1
end
else
artal.channel[CC]:ink(nil,1)
end
end
--assert(artal.channel[CC].count == countEnd,"Artal: Error reading layerdata.")
end
artal.count = artal.channel[CC].count
end
elseif artal.compression == 0 then
for CC = 1, artal.channelCount do
cPixelPos = CC - 1
for y = 0, artal.height - 1 do
for x = 0, artal.width - 1 do
--print(x,y)
local pixelPos = (y*artal.width+x)*4+cPixelPos
local dataPos = y*artal.width+x+(cPixelPos*artal.height*artal.width)
C_data[pixelPos] = C_fileData[artal.count + dataPos]
--print(pixelPos, dataPos)
end
end
end
else
assert(false,"Artal: Unsupported compression mode for composed image.")
end
if artal.channelCount == 3 then -- Set opacity to full
for i = 0, artal.width*artal.height*4,4 do
C_data[i+3] = 255
end
end
--artal.image = love.graphics.newImage(imageData)
return imageData -- Early out for composed mode.
end
local function imageDataForLayer(globalData,layerData,fileData)
local layerLoadData = {}
layerLoadData.left = layerData.left
layerLoadData.right = layerData.right
layerLoadData.top = layerData.top
layerLoadData.bottom = layerData.bottom
layerLoadData.channelPointer = {}
layerLoadData.channelID = {}
layerLoadData.opacity = layerData.bakedOpacity
for CC = 1, layerData.channelCount do
layerLoadData.channelPointer[CC] = layerData.channel[CC].pointer
layerLoadData.channelID[CC] = layerData.channel[CC].id
end
layerLoadData.globalWidth = globalData.width
layerLoadData.globalHeight = globalData.height
layerLoadData.totalWidth = layerLoadData.right - layerLoadData.left
layerLoadData.totalHeight = layerLoadData.bottom - layerLoadData.top
layerLoadData.fileData = fileData
return layerLoadData
end
if structureFlagOrNumber == nil or structureFlagOrNumber == "info" then
local imageTable =
{
width = artal.width,
height = artal.height
}
for LC = 1 , math.abs(artal.layerTotalCount) do
local layerLoadData = imageDataForLayer(artal,artal.layer[LC],GC_C_fileData)
if artal.layer[LC].folder == 1 or artal.layer[LC].folder == 2 or artal.layer[LC].folder == 3 then
imageTable[LC] = defaultLoadFolderFunction(artal.layer[LC],layerLoadData)
else
if structureFlagOrNumber == "info" then
imageTable[LC] = defaultLoadImageInfoFunction(artal.layer[LC],layerLoadData)
else
imageTable[LC] = defaultLoadImageFunction(artal.layer[LC],layerLoadData)
end
end
end
return imageTable
elseif type(structureFlagOrNumber) == "number" then
local layerLoadData = imageDataForLayer(artal,artal.layer[structureFlagOrNumber],GC_C_fileData)
local imageData = artalNewLayerImageData(layerLoadData)
return imageData
end
end
return artalFunction
--[[
Adobe documentation on the PSD file format.
http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/
--]]
-- Assemble our bodies.