-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicoio.lua
2067 lines (1804 loc) · 70.6 KB
/
icoio.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
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
--[[
Wikipedia
https://en.wikipedia.org/wiki/ICO_(file_format)
"The evolution of the ICO file format" by Raymond Chen
https://devblogs.microsoft.com/oldnewthing/20101018-00/?p=12513
https://devblogs.microsoft.com/oldnewthing/20101019-00/?p=12503
https://devblogs.microsoft.com/oldnewthing/20101021-00/?p=12483
https://devblogs.microsoft.com/oldnewthing/20101022-00/?p=12473
Ani format
https://www.informit.com/articles/article.aspx?p=1189080&seqNum=3
Pels per meter
https://stackoverflow.com/questions/17550545/bmp-image-header-bixpelspermeter
Gimp Implementation
https://github.com/GNOME/gimp/blob/master/plug-ins/file-ico/ico-load.c
https://github.com/GNOME/gimp/blob/master/plug-ins/file-ico/ico-export.c
]]
local importFileExts <const> = { "ani", "cur", "ico" }
local exportFileExts <const> = { "ani", "cur", "ico" }
local visualTargets <const> = { "CANVAS", "LAYER", "SELECTION", "SLICES" }
local frameTargets <const> = { "ACTIVE", "ALL", "TAG" }
local formats <const> = { "RGB24", "RGB32", "RGBA32" }
local defaults <const> = {
-- TODO: Support 8 bit indexed?
fps = 12,
visualTarget = "CANVAS",
frameTarget = "ALL",
xHotSpot = 0,
yHotSpot = 0,
format = "RGB24",
-- The size restrictions can be pushed to 512 x 512 for ico and cur,
-- but not for anis, which should stay at 256 x 256. The 256 limit
-- allows anis to be opened in Inkscape, but not Irfanview.
wLimitAni = 256,
hLimitAni = 256,
wLimitIcoCur = 512,
hLimitIcoCur = 512,
}
---@param x integer
---@return integer
---@nodiscard
local function nextPowerOf2(x)
if x ~= 0 then
local xSgn = 1
local xAbs = x
if x < 0 then
xAbs = -x
xSgn = -1
end
local p = 1
while p < xAbs do
p = p << 1
end
return p * xSgn
end
return 0
end
---@param fileData string
---@return Image[] images
---@return integer wMax
---@return integer hMax
---@return integer[] uniqueColors
---@return string[]|nil errMsg
---@nodiscard
local function readIcoCur(fileData)
---@type Image[]
local images <const> = {}
local wMax = -2147483648
local hMax = -2147483648
---@type integer[]
local uniqueColors <const> = {}
-- Cache methods used in loops.
local ceil <const> = math.ceil
local strbyte <const> = string.byte
local strfmt <const> = string.format
local strpack <const> = string.pack
local strsub <const> = string.sub
local strunpack <const> = string.unpack
local tconcat <const> = table.concat
local icoHeaderType <const> = strunpack("<I2", strsub(fileData, 3, 4))
local typeIsIco = icoHeaderType == 1
local typeIsCur = icoHeaderType == 2
if (not typeIsIco) and (not typeIsCur) then
return images, wMax, hMax, uniqueColors,
{ "Only icons and cursors are supported." }
end
local icoHeaderEntries <const> = strunpack("<I2", strsub(fileData, 5, 6))
if icoHeaderEntries <= 0 then
return images, wMax, hMax, uniqueColors,
{ "The file contained no icon image entries." }
end
---@type table<integer, integer>
local abgr32Dict <const> = {}
local dictCursor = 0
local colorModeRgb <const> = ColorMode.RGB
local colorSpaceNone <const> = ColorSpace { sRGB = false }
local cursor = 6
local h = 0
while h < icoHeaderEntries do
h = h + 1
-- One problem causing invalid Aseprite icos is that the data
-- size and offset don't match the content length. One way to tell
-- that a file is Aseprite generated is that the bmpSize and
-- dataSize will be equal. For GIMP icos, the bmpSize will be zero.
-- In case it's ever worth recalculating the sizes, they don't use
-- the const modifier.
local icoWidth,
icoHeight,
numColors,
reserved <const>,
xHotSpot <const>, -- or bit planes for icos
yHotSpot <const>, -- or bits per pixel for icos
dataSize,
dataOffset = strunpack(
"B B B B <I2 <I2 <I4 <I4",
strsub(fileData, cursor + 1, cursor + 16))
-- print(strfmt("Entry: %d", h))
-- print(strfmt("icoWidth: %d (0x%02x)", icoWidth, icoWidth))
-- print(strfmt("icoHeight: %d (0x%02x)", icoHeight, icoHeight))
-- print(strfmt("numColors: %d (0x%02x)", numColors, numColors))
-- print(strfmt("reserved: %d (0x%02x)", reserved, reserved))
-- if typeIsCur then
-- print(strfmt("xHotSpot: %d (0x%04x)", xHotSpot, xHotSpot))
-- print(strfmt("yHotSpot: %d (0x%04x)", yHotSpot, yHotSpot))
-- else
-- print(strfmt("icoPlanes: %d (0x%04x)", xHotSpot, xHotSpot))
-- print(strfmt("icoBpp: %d (0x%04x)", yHotSpot, yHotSpot))
-- end
-- print(strfmt("dataSize: %d (0x%08x)", dataSize, dataSize))
-- print(strfmt("dataOffset: %d (0x%08x)", dataOffset, dataOffset))
-- Unlike bmp format, seems like width and height are unsigned?
local bmpHeaderSize <const>,
bmpWidth <const>,
bmpHeight2 <const>,
bmpPlanes <const>,
bmpBpp <const>,
bmpCompress <const>,
bmpChunk <const>,
bmpXRes <const>,
bmpYRes <const>,
bmpUsed <const>,
bmpKey <const> = strunpack(
"<I4 <I4 <I4 <I2 <I2 <I4 <I4 <I4 <I4 <I4 <I4",
strsub(fileData, dataOffset + 1, dataOffset + 40))
if icoWidth == 0 then icoWidth = 256 end
if icoHeight == 0 then icoHeight = 256 end
if numColors == 0 then numColors = 256 end
-- print(strfmt("bmpHeaderSize: %d (0x%08x)", bmpHeaderSize, bmpHeaderSize))
-- print(strfmt("bmpWidth: %d (0x%08x)", bmpWidth, bmpWidth))
-- print(strfmt("bmpHeight2: %d (0x%08x)", bmpHeight2, bmpHeight2))
if bmpHeaderSize ~= 40 or reserved ~= 0 then
return images, wMax, hMax, uniqueColors, {
"Found a malformed header when parsing the file.",
"This importer does not support Aseprite made icos,",
"nor does it support icos with compressed pngs."
}
end
-- These checks are equal to zero, not less than or equal to, in case
-- negative bitmap dimensions are supported.
if bmpWidth == 0 or bmpHeight2 == 0 then
return images, wMax, hMax, uniqueColors, {
"Invalid bitmap image dimensions."
}
end
-- Calculate the height here in case you want to try to verify the
-- data size.
local bmpHeight <const> = bmpHeight2 // 2 --[[@as integer]]
if bmpWidth > wMax then wMax = bmpWidth end
if bmpHeight > hMax then hMax = bmpHeight end
-- print(strfmt("bmpPlanes: %d (0x%04x)", bmpPlanes, bmpPlanes))
-- print(strfmt("bmpBpp: %d (0x%04x)", bmpBpp, bmpBpp))
-- print(strfmt("bmpCompress: %d (0x%08x)", bmpCompress, bmpCompress))
-- print(strfmt("bmpChunk: %d (0x%08x)", bmpChunk, bmpChunk))
-- print(strfmt("bmpXRes: %d (0x%08x)", bmpXRes, bmpXRes))
-- print(strfmt("bmpYRes: %d (0x%08x)", bmpYRes, bmpYRes))
-- print(strfmt("bmpUsed: %d (0x%08x)", bmpUsed, bmpUsed))
-- print(strfmt("bmpKey: %d (0x%08x)", bmpKey, bmpKey))
-- Calculations for draw mask, with 1 bit per alpha.
local areaImage <const> = bmpWidth * bmpHeight
local dWordsPerRowMask <const> = ceil(bmpWidth / 32)
local lenColorMask = areaImage * 4
if bmpBpp == 24 then
lenColorMask = ceil((bmpWidth * bmpBpp) / 32) * bmpHeight * 4
elseif bmpBpp == 16 then
lenColorMask = ceil((bmpWidth * bmpBpp) / 32) * bmpHeight * 4
elseif bmpBpp == 8 then
lenColorMask = numColors * 4 + bmpWidth * bmpHeight
elseif bmpBpp == 4 then
lenColorMask = numColors * 4
+ ceil((bmpWidth * bmpBpp) / 32) * bmpHeight * 4
elseif bmpBpp == 1 then
lenColorMask = numColors * 4
+ ceil((bmpWidth * bmpBpp) / 32) * bmpHeight * 4
end
local lenFileData <const> = #fileData
local alphaMapOffset <const> = dataOffset + 40 + lenColorMask
-- local lenDWords <const> = dWordsPerRowMask * bmpHeight
-- local dataSizeCalc <const> = 40 + lenColorMask + lenDWords * 4
-- print(strfmt(
-- "dataSize: %d, dataSizeCalc: %d (%s)",
-- dataSize, dataSizeCalc,
-- dataSize == dataSizeCalc and "match" or "mismatch"))
-- print(strfmt(
-- "lenColorMask: %d, dWordsPerRowMask: %d",
-- lenColorMask, dWordsPerRowMask))
-- print(strfmt(
-- "lenDWords: %d, alphaMapOffset: %d",
-- lenDWords, alphaMapOffset))
---@type integer[]
local masks <const> = {}
local i = 0
while i < areaImage do
local x <const> = i % bmpWidth
local y <const> = i // bmpWidth
local xDWord <const> = x // 32
local xBit <const> = 31 - x % 32
local idxDWord <const> = 4 * (y * dWordsPerRowMask + xDWord)
local orig <const> = alphaMapOffset + 1 + idxDWord
local dest <const> = alphaMapOffset + 4 + idxDWord
if dest > lenFileData then break end
local dWord <const> = strunpack(">I4", strsub(fileData,
orig, dest))
local mask <const> = (dWord >> xBit) & 0x1
masks[1 + i] = mask
i = i + 1
end
while i < areaImage do
i = i + 1
masks[i] = 0
end
-- print(tconcat(masks, ", "))
---@type integer[]
local palAbgr32s <const> = {}
local numColors4 <const> = numColors * 4
if bmpBpp <= 8 and numColors > 0 then
local j = 0
while j < numColors do
local j4 <const> = j * 4
local b8 <const>, g8 <const>, r8 <const> = strbyte(
fileData, dataOffset + 41 + j4, dataOffset + 43 + j4)
-- print(strfmt(
-- "j: %d, r8: %03d, g8: %03d, b8: %03d, #%06X",
-- j, r8, g8, b8,
-- (r8 << 0x10 | g8 << 0x08 | b8)))
j = j + 1
local palAbgr32 <const> = 0xff000000 | b8 << 0x10 | g8 << 0x08 | r8
palAbgr32s[j] = palAbgr32
end
end
---@type string[]
local byteStrs <const> = {}
if bmpBpp == 1 then
local dWordsPerRow1 <const> = ceil(bmpWidth / 32)
local capacityPerRow1 <const> = 4 * dWordsPerRow1
-- print(string.format("dWordsPerRow1: %d, capacityPerRow1: %d",
-- dWordsPerRow1, capacityPerRow1))
local k = 0
while k < areaImage do
local a8, b8, g8, r8 = 0, 0, 0, 0
local x <const> = k % bmpWidth
local yFlipped <const> = k // bmpWidth
local mask <const> = masks[1 + k]
if mask == 0 then
a8 = 255
local xByte <const> = 4 * x // 32
local xBit <const> = 7 - x % 8
local idxByte <const> = strbyte(fileData,
dataOffset + 41 + numColors4
+ yFlipped * capacityPerRow1 + xByte)
local idxMap <const> = (idxByte >> xBit) & 0x1
-- print(string.format("idxMap: %d", idxMap))
local abgr32 <const> = palAbgr32s[1 + idxMap]
r8 = abgr32 & 0xff
g8 = (abgr32 >> 0x08) & 0xff
b8 = (abgr32 >> 0x10) & 0xff
end
local y <const> = bmpHeight - 1 - yFlipped
local idxAse <const> = y * bmpWidth + x
byteStrs[1 + idxAse] = strpack("B B B B", r8, g8, b8, a8)
local abgr32 <const> = a8 << 0x18 | b8 << 0x10 | g8 << 0x08 | r8
if not abgr32Dict[abgr32] then
dictCursor = dictCursor + 1
abgr32Dict[abgr32] = dictCursor
end
k = k + 1
end
elseif bmpBpp == 8 then
local dWordsPerRow8 <const> = ceil(bmpWidth / 4)
local capacityPerRow8 <const> = 4 * dWordsPerRow8
-- print(strfmt("dWordsPerRow8: %d, capacityPerRow8: %d",
-- dWordsPerRow8, capacityPerRow8))
local k = 0
while k < areaImage do
local a8, b8, g8, r8 = 0, 0, 0, 0
local x <const> = k % bmpWidth
local yFlipped <const> = k // bmpWidth
local mask <const> = masks[1 + k]
if mask == 0 then
a8 = 255
local idxMap <const> = strbyte(fileData,
dataOffset + 41 + numColors4
+ yFlipped * capacityPerRow8 + x)
-- print(string.format("idxMap: %d", idxMap))
local abgr32 <const> = palAbgr32s[1 + idxMap]
r8 = abgr32 & 0xff
g8 = (abgr32 >> 0x08) & 0xff
b8 = (abgr32 >> 0x10) & 0xff
end
-- print(string.format(
-- "mask: %d, r8: %03d, g8: %03d, b8: %03d, a8: %03d, #%06X",
-- mask, r8, g8, b8, a8,
-- (r8 << 0x10 | g8 << 0x08 | b8)))
local y <const> = bmpHeight - 1 - yFlipped
local idxAse <const> = y * bmpWidth + x
byteStrs[1 + idxAse] = strpack("B B B B", r8, g8, b8, a8)
local abgr32 <const> = a8 << 0x18 | b8 << 0x10 | g8 << 0x08 | r8
if not abgr32Dict[abgr32] then
dictCursor = dictCursor + 1
abgr32Dict[abgr32] = dictCursor
end
k = k + 1
end
elseif bmpBpp == 24 then
-- Wikipedia: "24 bit images are stored as B G R triples
-- but are not DWORD aligned."
local bmpWidth3 <const> = bmpWidth * 3
local dWordsPerRow24 <const> = ceil(bmpWidth3 / 4)
local capacityPerRow24 <const> = 4 * dWordsPerRow24
-- print(string.format("dWordsPerRow24: %d, capacityPerRow24: %d",
-- dWordsPerRow24, capacityPerRow24))
local k = 0
while k < areaImage do
local a8, b8, g8, r8 = 0, 0, 0, 0
local x <const> = k % bmpWidth
local yFlipped <const> = k // bmpWidth
local mask <const> = masks[1 + k]
if mask == 0 then
a8 = 255
local x3 <const> = x * 3
local offset <const> = dataOffset + 41 + yFlipped * capacityPerRow24
b8, g8, r8 = strbyte(fileData, offset + x3, offset + 2 + x3)
end
-- print(string.format(
-- "mask: %d, r8: %03d, g8: %03d, b8: %03d, #%06X",
-- mask, r8, g8, b8,
-- (r8 << 0x10 | g8 << 0x08 | b8)))
local y <const> = bmpHeight - 1 - yFlipped
local idxAse <const> = y * bmpWidth + x
byteStrs[1 + idxAse] = strpack("B B B B", r8, g8, b8, a8)
local abgr32 <const> = a8 << 0x18 | b8 << 0x10 | g8 << 0x08 | r8
if not abgr32Dict[abgr32] then
dictCursor = dictCursor + 1
abgr32Dict[abgr32] = dictCursor
end
k = k + 1
end
elseif bmpBpp == 32 then
-- There's an issue with RGB32 as opened in GIMP vs. as
-- set in Windows Control Panel - Hardware and Sound -
-- Devices and Printers - Mouse . Color must be black to
-- be transparent with XOR mask. However, there's no way
-- to distinguish between RGB32 and RGBA32, so alpha still
-- reads as 0 and image appears blank.
--
-- The alpha cannot just be set to 255 if it is zero within
-- the mask clause above, as improperly formatted RGBA32
-- icos will have zero alpha colors with non-zero RGB.
---@type integer[]
local abgr32Arr <const> = {}
local allZeroAlpha = true
local k = 0
while k < areaImage do
local a8, b8, g8, r8 = 0, 0, 0, 0
local x <const> = k % bmpWidth
local yFlipped <const> = k // bmpWidth
local mask <const> = masks[1 + k]
if mask == 0 then
local k4 <const> = k * 4
b8, g8, r8, a8 = strbyte(fileData,
dataOffset + 41 + k4,
dataOffset + 44 + k4)
allZeroAlpha = allZeroAlpha and a8 == 0
end
-- print(string.format(
-- "mask: %d, r8: %03d, g8: %03d, b8: %03d, a8: %03d, #%06X",
-- mask, r8, g8, b8, a8,
-- (r8 << 0x10 | g8 << 0x08 | b8)))
local y <const> = bmpHeight - 1 - yFlipped
local idxAse <const> = y * bmpWidth + x
local abgr32 <const> = a8 << 0x18 | b8 << 0x10 | g8 << 0x08 | r8
abgr32Arr[1 + idxAse] = abgr32
if not abgr32Dict[abgr32] then
dictCursor = dictCursor + 1
abgr32Dict[abgr32] = dictCursor
end
k = k + 1
end -- End pixels loop.
local alphaMask <const> = allZeroAlpha
and 0xff000000
or 0x00000000
local m = 0
while m < areaImage do
local x <const> = m % bmpWidth
local yFlipped <const> = m // bmpWidth
local y <const> = bmpHeight - 1 - yFlipped
local idxAse <const> = y * bmpWidth + x
local mask <const> = masks[1 + m]
byteStrs[1 + idxAse] = strpack("<I4", (mask == 0)
and (alphaMask | abgr32Arr[1 + idxAse])
or 0)
m = m + 1
end -- End zero alpha correction.
end
if #byteStrs <= 0 then
return images, wMax, hMax, uniqueColors,
{ "Found malformed data when parsing the file." }
end
local imageSpec <const> = ImageSpec {
width = bmpWidth,
height = bmpHeight,
colorMode = colorModeRgb,
transparentColor = 0
}
imageSpec.colorSpace = colorSpaceNone
local image <const> = Image(imageSpec)
image.bytes = tconcat(byteStrs)
images[h] = image
cursor = cursor + 16
-- print(string.format("cursor: %d\n", cursor))
end
-- Ensure that alpha mask is at zero.
abgr32Dict[0] = -1
for abgr32, _ in pairs(abgr32Dict) do
uniqueColors[#uniqueColors + 1] = abgr32
end
table.sort(uniqueColors, function(a, b)
return abgr32Dict[a] < abgr32Dict[b]
end)
return images, wMax, hMax, uniqueColors, nil
end
---@param fileData string
---@return Image[] images
---@return integer wMax
---@return integer hMax
---@return integer[] uniqueColors
---@return number[] durations
---@return string[]|nil errMsg
---@nodiscard
local function readAni(fileData)
---@type Image[]
local images <const> = {}
local wMax = -2147483648
local hMax = -2147483648
---@type integer[]
local uniqueColors <const> = {}
---@type number[]
local durations <const> = {}
---@type integer[]
local sequence <const> = {}
-- Cache methods used in loops.
local strsub <const> = string.sub
local strunpack <const> = string.unpack
local tconcat <const> = table.concat
local riffKey <const> = strsub(fileData, 1, 4)
if riffKey ~= "RIFF" then
return images, wMax, hMax, uniqueColors, durations,
{ "\"RIFF\" identifier not found." }
end
local riffDataSize <const> = strunpack("<I4", strsub(fileData, 5, 8))
-- print(string.format("riffDataSize: %d", riffDataSize))
local aconKey <const> = strsub(fileData, 9, 12)
if aconKey ~= "ACON" then
return images, wMax, hMax, uniqueColors, durations,
{ "\"ACON\" identifier not found." }
end
local rateChunkFound = false
local seqChunkFound = false
local jiffDefault = 1
local cursor = 12
local lenFileData <const> = #fileData
while cursor < lenFileData do
local cursorSkip = 4
local dWord <const> = strsub(fileData, cursor + 1, cursor + 4)
-- print(string.format("cursor: %04d, dWord: \"%s\" 0x%08x",
-- cursor, dWord, strunpack("<I4", dWord)))
if dWord == "anih" then
local chunkSize <const>,
dataSize <const>,
frameCount <const>,
seqCount <const>,
width <const>,
height <const>,
bpp <const>,
bPlanes <const>,
jiffDefTrial <const>,
flags <const> = strunpack(
"<I4 <I4 <I4 <I4 <I4 <I4 <I4 <I4 <I4 <I4",
strsub(fileData, cursor + 5, cursor + 44))
-- print(string.format("01 chunkSize: %d", chunkSize))
-- print(string.format("02 dataSize: %d", dataSize))
-- print(string.format("03 frameCount: %d", frameCount))
-- print(string.format("04 seqCount: %d", seqCount))
-- print(string.format("05 width: %d", width))
-- print(string.format("06 height: %d", height))
-- print(string.format("07 bpp: %d", bpp))
-- print(string.format("08 bPlanes: %d", bPlanes))
-- print(string.format("09 jiffDefault: %d", jiffDefTrial))
-- print(string.format("10 flags: %d", flags))
jiffDefault = jiffDefTrial --[[@as integer]]
-- Even if this code works correctly, it's not worth trusting these
-- flags. Instead, search for seq chunks and test for icos.
local usesIcoCurs <const> = (flags & 1) == 1
local hasSeqChunk <const> = (flags & 2) == 2
-- print(string.format("usesIcoCurs: %s", usesIcoCurs and "true" or "false"))
-- print(string.format("hasSeqChunk: %s", hasSeqChunk and "true" or "false"))
cursorSkip = chunkSize + 8
elseif dWord == "rate" then
rateChunkFound = true
local chunkSize <const> = strunpack("<I4", strsub(
fileData, cursor + 5, cursor + 8))
-- print(string.format("chunkSize: %d", chunkSize))
local rateCount <const> = chunkSize // 4
local i = 0
while i < rateCount do
local i4 <const> = i * 4
local jiffieStr <const> = strsub(fileData,
cursor + 9 + i4, cursor + 12 + i4)
local jiffieI4 <const> = strunpack("<I4", jiffieStr)
local duration <const> = jiffieI4 / 60.0
durations[1 + i] = duration
i = i + 1
end
-- print(tconcat(durations, ", "))
cursorSkip = chunkSize + 8
elseif dWord == "seq " then
seqChunkFound = true
local chunkSize <const> = strunpack("<I4", strsub(
fileData, cursor + 5, cursor + 8))
-- print(string.format("chunkSize: %d", chunkSize))
local seqCount <const> = chunkSize // 4
local i = 0
while i < seqCount do
local i4 <const> = i * 4
local seqStr <const> = strsub(fileData,
cursor + 9 + i4, cursor + 12 + i4)
local seq <const> = strunpack("<I4", seqStr)
sequence[1 + i] = seq
i = i + 1
end
-- print(tconcat(sequence, ", "))
cursorSkip = chunkSize + 8
elseif dWord == "LIST" then
local chunkSize <const> = strunpack("<I4", strsub(
fileData, cursor + 5, cursor + 8))
-- print(string.format("chunkSize: %d", chunkSize))
local subCursor = cursor + 8
while subCursor < chunkSize do
local subCursorSkip = 4
local subDWord <const> = strsub(
fileData, subCursor + 1, subCursor + 4)
-- print(string.format("subCursor: %d, subDWord: \"%s\" 0x%08x",
-- subCursor, subDWord, strunpack("<I4", subDWord)))
if subDWord == "IART" then
local subChunkSize <const> = strunpack("<I4", strsub(
fileData, subCursor + 5, subCursor + 8))
-- print(string.format("subChunkSize: %d", subChunkSize))
subCursorSkip = subChunkSize + 8
elseif subDWord == "INAM" then
local subChunkSize <const> = strunpack("<I4", strsub(
fileData, subCursor + 5, subCursor + 8))
-- print(string.format("subChunkSize: %d", subChunkSize))
subCursorSkip = subChunkSize + 8
elseif subDWord == "INFO" then
subCursorSkip = 4
elseif subDWord == "fram" then
subCursorSkip = 4
elseif subDWord == "icon" then
local subChunkSize <const> = strunpack("<I4", strsub(
fileData, subCursor + 5, subCursor + 8))
-- print(string.format("subChunkSize: %d", subChunkSize))
local icoDataChunk <const> = strsub(fileData,
subCursor + 9, subCursor + 8 + subChunkSize)
local subImages <const>,
subWMax <const>,
subHMax <const>,
subUniqueColors <const>,
subErrMsg <const> = readIcoCur(icoDataChunk)
if subErrMsg ~= nil then
return images, wMax, hMax, uniqueColors, durations,
subErrMsg
end
local lenSubImages <const> = #subImages
if lenSubImages > 0
and subWMax > 0
and subHMax > 0 then
if subWMax > wMax then wMax = subWMax end
if subHMax > hMax then hMax = subHMax end
local i = 0
while i < lenSubImages do
i = i + 1
images[#images + 1] = subImages[i]
end
local lenSubUniqueColors <const> = #subUniqueColors
local j = 0
while j < lenSubUniqueColors do
j = j + 1
uniqueColors[#uniqueColors + 1] = subUniqueColors[j]
end
end
subCursorSkip = subChunkSize + 8
end
subCursor = subCursor + subCursorSkip
end
cursorSkip = chunkSize + 8
else
cursorSkip = 4
end
cursor = cursor + cursorSkip
end
local seqImages = {}
if seqChunkFound then
local lenImages <const> = #images
local lenSeq <const> = #sequence
local j = 0
while j < lenSeq do
j = j + 1
local idx <const> = 1 + sequence[j]
if idx <= lenImages then
seqImages[j] = images[idx]
end
end
else
seqImages = images
end
local lenSeqImages <const> = #seqImages
local lenDurations <const> = #durations
if (not rateChunkFound) or (lenSeqImages ~= lenDurations) then
local durDefault <const> = jiffDefault / 60.0
local j = 0
while j < lenSeqImages do
j = j + 1
durations[j] = durDefault
end
end
return seqImages, wMax, hMax, uniqueColors, durations, nil
end
---@param chosenImages Image[]
---@param chosenPalettes Palette[]
---@param colorModeSprite ColorMode
---@param alphaIndexSprite integer
---@param hasBkg boolean
---@param extIsCur boolean
---@param xHotSpot number
---@param yHotSpot number
---@param format string
---@return string
---@nodiscard
local function writeIcoCur(
chosenImages,
chosenPalettes,
colorModeSprite,
alphaIndexSprite,
hasBkg,
extIsCur,
xHotSpot, yHotSpot,
format)
-- Cache methods.
local ceil <const> = math.ceil
local floor <const> = math.floor
local strbyte <const> = string.byte
local strchar <const> = string.char
local strpack <const> = string.pack
local tconcat <const> = table.concat
local lenChosenImages = #chosenImages
---@type string[]
local entryHeaders <const> = {}
---@type string[]
local imageEntries <const> = {}
-- The overall header is 6 bytes,
-- each ico entry is 16 bytes.
local icoOffset = 6 + lenChosenImages * 16
local fmtIsRgb24 <const> = format == "RGB24"
local fmtIsRgb32 <const> = format == "RGB32"
local maskThreshold = 0
local bpp = 32
if fmtIsRgb24 then
maskThreshold = 127
bpp = 24
elseif fmtIsRgb32 then
maskThreshold = 127
bpp = 32
end
local cmIsRgb <const> = colorModeSprite == ColorMode.RGB
local cmIsGry <const> = colorModeSprite == ColorMode.GRAY
local cmIsIdx <const> = colorModeSprite == ColorMode.INDEXED
local cmSkip = 0
if cmIsRgb then
cmSkip = 4
elseif cmIsGry then
cmSkip = 2
elseif cmIsIdx then
cmSkip = 1
end
local k = 0
while k < lenChosenImages do
k = k + 1
local image <const> = chosenImages[k]
local palette <const> = chosenPalettes[k]
local srcByteStr <const> = image.bytes
local specImage <const> = image.spec
local wImage <const> = specImage.width
local hImage <const> = specImage.height
local areaWrite <const> = wImage * hImage
local hn1 <const> = hImage - 1
-- Convert different sprite formats to uniform data.
-- In bitmap format, y axis is from bottom to top.
---@type integer[]
local abgr32s <const> = {}
if cmIsIdx then
local m = 0
while m < areaWrite do
local x <const> = m % wImage
local y <const> = hn1 - m // wImage
local n1 <const> = cmSkip * (y * wImage + x)
local idx <const> = strbyte(srcByteStr, 1 + n1, cmSkip + n1)
local r8, g8, b8, a8 = 0, 0, 0, 0
if hasBkg or idx ~= alphaIndexSprite then
local aseColor <const> = palette:getColor(idx)
a8 = aseColor.alpha
if a8 > maskThreshold then
r8 = aseColor.red
g8 = aseColor.green
b8 = aseColor.blue
end
end
m = m + 1
abgr32s[m] = a8 << 0x18 | b8 << 0x10 | g8 << 0x08 | r8
end
elseif cmIsGry then
local m = 0
while m < areaWrite do
local x <const> = m % wImage
local y <const> = hn1 - m // wImage
local n2 <const> = cmSkip * (y * wImage + x)
local v8, a8 <const> = strbyte(srcByteStr, 1 + n2, cmSkip + n2)
if a8 <= maskThreshold then v8 = 0 end
m = m + 1
abgr32s[m] = a8 << 0x18 | v8 << 0x10 | v8 << 0x08 | v8
end
else
-- Default to RGB.
local m = 0
while m < areaWrite do
local x <const> = m % wImage
local y <const> = hn1 - m // wImage
local n4 <const> = cmSkip * (y * wImage + x)
local r8, g8, b8, a8 <const> = strbyte(srcByteStr, 1 + n4, cmSkip + n4)
if a8 <= maskThreshold then r8, g8, b8 = 0, 0, 0 end
m = m + 1
abgr32s[m] = a8 << 0x18 | b8 << 0x10 | g8 << 0x08 | r8
end
end
-- Write transparency mask.
-- Wikipedia: "The mask has to align to a DWORD (32 bits) and
-- should be packed with 0s. A 0 pixel means 'the corresponding
-- pixel in the image will be drawn' and a 1 means 'ignore this
-- pixel'."
---@type integer[]
local dWords <const> = {}
local dWordsPerRow <const> = ceil(wImage / 32)
local lenDWords <const> = dWordsPerRow * hImage
local o = 0
while o < areaWrite do
local abgr32 <const> = abgr32s[1 + o]
local a8 <const> = abgr32 >> 0x18 & 0xff
local draw <const> = a8 <= maskThreshold and 1 or 0
local x <const> = o % wImage
local y <const> = o // wImage
local xDWord <const> = x // 32
local xBit <const> = 31 - x % 32
local idxDWord <const> = y * dWordsPerRow + xDWord
local dWord <const> = dWords[1 + idxDWord] or 0
dWords[1 + idxDWord] = dWord | (draw << xBit)
o = o + 1
end
---@type string[]
local maskBytes <const> = {}
local p = 0
while p < lenDWords do
p = p + 1
-- This uses the reverse byte order due to how mask words
-- were written above.
maskBytes[p] = strpack(">I4", dWords[p])
end
-- Write color data.
---@type string[]
local trgColorBytes <const> = {}
local bytesPerRow <const> = 4 * ceil((wImage * bpp) / 32)
local hbpr <const> = hImage * bytesPerRow
if fmtIsRgb24 then
local q = 0
while q < hbpr do
local xByte <const> = q % bytesPerRow
local x <const> = xByte // 3
local c8 = 0
if x < wImage then
local y <const> = q // bytesPerRow
local i <const> = y * wImage + x
local abgr32 <const> = abgr32s[1 + i]
local channel <const> = xByte % 3
if channel == 2 then
c8 = abgr32 & 0xff
elseif channel == 1 then
c8 = abgr32 >> 0x08 & 0xff
else
c8 = abgr32 >> 0x10 & 0xff
end
end
q = q + 1
trgColorBytes[q] = strchar(c8)
end
elseif fmtIsRgb32 then
-- If alpha is left as zero, then image editors like GIMP and
-- XnView MP will treat the pixels as transparent.
local q = 0
while q < areaWrite do
q = q + 1
local abgr32 <const> = abgr32s[q]
local a8 <const> = abgr32 >> 0x18 & 0xff
local b8 <const> = abgr32 >> 0x10 & 0xff
local g8 <const> = abgr32 >> 0x08 & 0xff
local r8 <const> = abgr32 & 0xff
local a1 <const> = a8 <= maskThreshold and 0 or 255
trgColorBytes[q] = strpack("B B B B", b8, g8, r8, a1)
end
else
-- Default to RGBA32.
local q = 0
while q < areaWrite do
q = q + 1
local abgr32 <const> = abgr32s[q]
local a8 <const> = abgr32 >> 0x18 & 0xff
local b8 <const> = abgr32 >> 0x10 & 0xff
local g8 <const> = abgr32 >> 0x08 & 0xff
local r8 <const> = abgr32 & 0xff
trgColorBytes[q] = strpack("B B B B", b8, g8, r8, a8)
end
end
-- Size 256 is written as 0.
local w8 <const> = wImage >= 256 and 0 or wImage
local h8 <const> = hImage >= 256 and 0 or hImage
-- Bitmap height is 2x, because the transparency mask is written
-- after the color mask.
local hImage2 <const> = hImage + hImage
local lenColorMask = areaWrite * 4
if fmtIsRgb24 then
lenColorMask = hbpr
elseif fmtIsRgb32 then