-
Notifications
You must be signed in to change notification settings - Fork 0
/
KraFileType.cs
991 lines (903 loc) · 56 KB
/
KraFileType.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using PaintDotNet;
using PaintDotNet.Collections;
using PaintDotNet.Imaging;
using PaintDotNet.IO;
namespace KritaFileType
{
public class KraFileType : FileType
{
private const int ThumbMaxSize = 256;
private readonly string MimeType = "YXBwbGljYXRpb24veC1rcml0YQ==";
private readonly string DefaultPixel = "AAAAAA==";
private readonly string ICC = "AAAjeGxjbXMCEAAAbW50clJHQiBYWVogB98ACwAKAAwAEgA4YWNzcCpuaXgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPbWAAEAAAAA0y1sY21zAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALZGVzYwAAAQgAAACwY3BydAAAAbgAAAESd3RwdAAAAswAAAAUY2hhZAAAAuAAAAAsclhZWgAAAwwAAAAUYlhZWgAAAyAAAAAUZ1hZWgAAAzQAAAAUclRSQwAAA0gAACAMZ1RSQwAAA0gAACAMYlRSQwAAA0gAACAMY2hybQAAI1QAAAAkZGVzYwAAAAAAAAAcc1JHQi1lbGxlLVYyLXNyZ2J0cmMuaWNjAAAAAAAAAAAAAAAdAHMAUgBHAEIALQBlAGwAbABlAC0AVgAyAC0AcwByAGcAYgB0AHIAYwAuAGkAYwBjAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB0ZXh0AAAAAENvcHlyaWdodCAyMDE1LCBFbGxlIFN0b25lICh3ZWJzaXRlOiBodHRwOi8vbmluZWRlZ3JlZXNiZWxvdy5jb20vOyBlbWFpbDogZWxsZXN0b25lQG5pbmVkZWdyZWVzYmVsb3cuY29tKS4gVGhpcyBJQ0MgcHJvZmlsZSBpcyBsaWNlb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OSa5Lzk3uUB5SPlReVn5Ynlq+XN5fDmEuY05lbmeeab5r3m3+cC5yTnRudp54vnrefQ5/LoFOg36Fnoe+ie6MDo4+kF6SjpSult6Y/psunU6ffqGeo86l7qgeqk6sbq6esL6y7rUetz65bruevc6/7sIexE7Gbsieys7M/s8u0U7TftWu197aDtw+3l7gjuK+5O7nHulO637tru/e8g70PvZu+J76zvz+/y8BXwOPBb8H7wofDF8OjxC/Eu8VHxdPGY8bvx3vIB8iTySPJr8o7ysfLV8vjzG/M/82LzhfOp88zz8PQT9Db0WvR99KH0xPTo9Qv1L/VS9Xb1mfW99eD2BPYn9kv2b/aS9rb22fb99yH3RPdo94z3sPfT9/f4G/g++GL4hviq+M748fkV+Tn5XfmB+aX5yfns+hD6NPpY+nz6oPrE+uj7DPsw+1T7ePuc+8D75PwI/Cz8UPx1/Jn8vfzh/QX9Kf1N/XL9lv26/d7+Av4n/kv+b/6U/rj+3P8A/yX/Sf9t/5L/tv/b//9jaHJtAAAAAAADAAAAAKPXAABUfAAATM0AAJmaAAAmZwAAD1w=";
public static readonly DateTime EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private struct LayerInfo
{
public int x;
public int y;
public LayerInfo(int x, int y)
{
this.x = x;
this.y = y;
}
}
public KraFileType()
: base("KritaRaster", new FileTypeOptions
{
SupportsLayers = true,
LoadExtensions = new string[] { ".kra" },
//SaveExtensions = new string[] { ".kra" }
})
{
}
private unsafe static Bitmap getBitmapFromKraLayer(int xofs, int yofs, Stream inStream, int baseWidth, int baseHeight, string fileName)
{
Bitmap result = null;
using (Bitmap bitmap = new Bitmap(baseWidth, baseHeight))
{
byte[] layerContent;
using (var memoryStream = new MemoryStream())
{
inStream.CopyTo(memoryStream);
layerContent = memoryStream.ToArray();
}
int currentIndex = 0;
int version = ParseHeaderElement(layerContent, "VERSION ", ref currentIndex);
int tileWidth = ParseHeaderElement(layerContent, "TILEWIDTH ", ref currentIndex);
int tileHeight = ParseHeaderElement(layerContent, "TILEHEIGHT ", ref currentIndex);
int pixelSize = ParseHeaderElement(layerContent, "PIXELSIZE ", ref currentIndex);
int decompressedLength = pixelSize * tileWidth * tileHeight;
int numberOfTiles = ParseHeaderElement(layerContent, "DATA ", ref currentIndex);
for (int i = 0; i < numberOfTiles; i++)
{
/* Now it is time to extract & decompress the data */
/* First the non-general element of the header needs to be extracted */
string headerString = GetHeaderElement(layerContent, ref currentIndex);
var match = System.Text.RegularExpressions.Regex.Match(headerString, "(-?\\d*),(-?\\d*),(\\w*),(\\d*)");
/* This header contains: */
/* 1. A number that defines the left position of the tile (CAN BE NEGATIVE!!!) */
/* 2. A number that defines the top position of the tile (CAN BE NEGATIVE!!!) */
/* 3. The string "LZF" which states the compression algorithm */
/* 4. A number that defines the number of bytes of the data that comes after this header. */
/* sm[0] is the full string match which we don't need */
/* The left position of the tile */
var left = int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
/* The top position of the tile */
var top = int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
/* We don't really care about match[3] since it is always 'LZF' */
/* The number of compressed bytes coming after this header */
var compressedLength = int.Parse(match.Groups[4].Value, CultureInfo.InvariantCulture);
/* Put all the data in a vector */
byte[] dataVector = layerContent.Skip(currentIndex).Take(compressedLength).ToArray();
/* Allocate memory for the output */
byte[] output = new byte[decompressedLength];
/* Now... the first byte of this dataVector is actually an indicator of compression */
/* As follows: */
/* 0 . No compression, the data is actually raw! */
/* 1 . The data was compressed using LZF */
bool isLZF = IsLZF(dataVector);
if (isLZF)
{
LzffDecompress(dataVector, (uint)compressedLength, ref output, (uint)decompressedLength);
}
else
{
output = dataVector.Skip(1).ToArray();
}
/* TODO: Krita might also normalize the colors in some way */
/* This needs to be check and if present, the colors need to be denormalized */
/* Data is saved in following format: */
/* - Firstly all the RED data */
/* - Secondly all the GREEN data */
/* - Thirdly all the BLUE data */
/* - Fourthly all the ALPHA data */
/* This is different from the required ImageMagick format which requires quartets of: */
/* R1, G1, B1, A1, R2, G2, ... etc */
/* We'll just sort this here!*/
/* TODO: Sometimes there won't be any alpha channel when it is RGB instead of RGBA. */
int tileArea = tileHeight * tileWidth;
for (int j = 0; j < tileArea; j++)
{
var w = j / (float)tileHeight;
var v = (int)MathF.Floor(w);
var u = (int)((w - v) * tileWidth);
var y = xofs + top + v;
var x = yofs + left + u;
// Paint.NET does not have anything outside these borders
if (x < 0 || y < 0 || x >= bitmap.Width || y >= bitmap.Height) continue;
var red = output[2 * tileArea + j]; //RED CHANNEL
var green = output[tileArea + j]; //GREEN CHANNEL
var blue = output[j]; //BLUE CHANNEL
var alpha = output[3 * tileArea + j]; //ALPHA CHANNEL
bitmap.SetPixel(x, y, Color.FromArgb(alpha, red, green, blue));
}
/* Q: Why are the RED and BLUE channels swapped? */
/* A: I have no clue... that's how it is saved in the tile! */
/* Add the compressedLength to the currentIndex so the next tile starts at the correct position */
currentIndex += compressedLength;
}
result = (Bitmap)bitmap.Clone();
}
return result;
}
private static Color GetPixel(Bitmap bitmap, int x, int y)
{
if (x < 0 || y < 0 || x >= bitmap.Width || y >= bitmap.Height) return Color.Transparent;
return bitmap.GetPixel(x, y);
}
/// <summary>
/// Extract a header element and match it with the <paramref name="elementName"/>.
/// </summary>
private static int ParseHeaderElement(byte[] layerContent, string elementName, ref int currentIndex)
{
int elementIntValue = -1;
/* First extract the header element */
string elementValue = GetHeaderElement(layerContent, ref currentIndex);
/* Try to match the elementValue string */
if (elementValue.Contains(elementName))
{
int pos = elementValue.IndexOf(elementName);
/* If found then erase it from string */
elementValue = elementValue.Remove(pos, elementName.Length);
elementIntValue = int.Parse(elementValue, CultureInfo.InvariantCulture);
}
else
{
throw new Exception($"WARNING: Missing header element in tile with name '{elementName}'\n");
}
return elementIntValue;
}
/// <summary>
/// Extract first byte
/// </summary>
private static bool IsLZF(byte[] bytes)
{
return bytes.First() == 1;
}
/// <summary>
/// Extract a header element starting from the <paramref name="currentIndex"/> until the next "0x0A".
/// </summary>
/// <returns>The header element</returns>
private static string GetHeaderElement(byte[] layerContent, ref int currentIndex)
{
int startIndex = currentIndex;
/* Just go through the vector until you encounter "0x0A" */
while (layerContent.ElementAt(currentIndex) != (char)0x0A)
{
currentIndex++;
}
int endIndex = currentIndex;
/* Extract this header element */
byte[] elementContent = layerContent.Skip(startIndex).Take(endIndex - startIndex).ToArray();
string elementValue = System.Text.Encoding.Default.GetString(elementContent);
/* Increment the currentIndex pointer so that we skip the "0x0A" character */
currentIndex++;
return elementValue;
}
/// <summary>
/// Decompresses the data using the Krita LZFF algorithm
/// </summary>
/// <param name="input">The data to decompress</param>
/// <param name="inputLength">Length of the data to decompress</param>
/// <param name="output">A buffer which will contain the decompressed data</param>
/// <param name="outputLength">The size of the decompressed archive in the output buffer</param>
/// <returns></returns>
public static bool LzffDecompress(byte[] input, uint inputLength, ref byte[] output, uint outputLength)
{
outputLength = outputLength + 1;
Array.Resize(ref output, (int)outputLength);
int i = 0;
int o = 0;
while (i < inputLength)
{
uint control = input[i++];
if (control < (1 << 5))
{
var length = (int)(control + 1);
if (o + length > outputLength)
{
return false;
}
Array.Copy(input, i, output, o, length);
i += length;
o += length;
}
else
{
var length = (int)(control >> 5);
var offset = (int)((control & 0x1F) << 8);
if (length == 7)
{
length += input[i++];
}
length += 2;
offset |= input[i++];
if (o + length > outputLength)
{
return false;
}
offset = o - 1 - offset;
if (offset < 0)
{
return false;
}
int block = Math.Min(length, o - offset);
Array.Copy(output, offset, output, o, block);
o += block;
offset += block;
length -= block;
while (length > 0)
{
output[o++] = output[offset++];
length--;
}
}
}
output = output.Skip(1).ToArray();
return true;
}
private struct Tile
{
public int left;
public int top;
public byte[] data;
}
private static void WriteLayer(Stream layerContent, Bitmap bitmap)
{
WriteHeaderElement(layerContent, "VERSION", 2);
int tileWidth = 64;
int tileHeight = 64;
int pixelSize = 4;
WriteHeaderElement(layerContent, "TILEWIDTH", tileWidth);
WriteHeaderElement(layerContent, "TILEHEIGHT", tileHeight);
WriteHeaderElement(layerContent, "PIXELSIZE", pixelSize);
List<Tile> tiles = new List<Tile>();
var w = ((int)MathF.Ceiling(bitmap.Width / (float)tileWidth));
var h = ((int)MathF.Ceiling(bitmap.Height / (float)tileHeight));
for (int x = 0; x < w; x++)
{
var left = x * tileWidth;
for (int y = 0; y < h; y++)
{
var top = y * tileHeight;
tiles.Add(new Tile
{
left = left,
top = top,
data = GetTileBytes(bitmap, left, top, tileWidth, tileHeight, pixelSize)
});
}
}
WriteHeaderElement(layerContent, "DATA", tiles.Count);
foreach (var tile in tiles)
{
byte[] output = new byte[0x20000];
var compressedSize = LzffCompress(tile.data, tile.data.Length, output, 0x20000);
WriteHeaderElement(layerContent, $"{tile.left},{tile.top},LZF,{compressedSize}");
layerContent.Write(output, 0, compressedSize);
}
}
private static byte[] GetTileBytes(Bitmap bitmap, int left, int top, int tileWidth, int tileHeight, int pixelSize)
{
int uncompressedLength = pixelSize * tileWidth * tileHeight;
byte[] output = new byte[uncompressedLength];
int tileArea = tileHeight * tileWidth;
for (int j = 0; j < tileArea; j++)
{
var w = j / (float)tileHeight;
var v = (int)MathF.Floor(w);
var u = (int)((w - v) * tileWidth);
var y = top + v;
var x = left + u;
Color color = GetPixel(bitmap, x, y);
output[2 * tileArea + j] = color.R; //RED CHANNEL
output[tileArea + j] = color.G; //GREEN CHANNEL
output[j] = color.B; //BLUE CHANNEL
output[3 * tileArea + j] = color.A; //ALPHA CHANNEL
}
return new byte[1] { 1 }.Concat(output).ToArray();
}
private static void WriteHeaderElement(Stream layerContent, string elementName, int elementValue) => WriteHeaderElement(layerContent, elementName + " " + elementValue);
private static void WriteHeaderElement(Stream layerContent, string element)
{
WriteString(layerContent, element, false);
layerContent.WriteByte(0x0A);
}
private static void WriteString(Stream stream, string value, bool includeNewLine = true)
{
stream.Write(Encoding.Default.GetBytes(value));
if (includeNewLine)
WriteLine(stream);
}
private static void WriteLine(Stream stream)
=> stream.Write(Encoding.Default.GetBytes("\r\n"));
/// <summary>
/// Hashtable, that can be allocated only once
/// </summary>
private static readonly long[] HashTable = new long[HSIZE];
private const uint HLOG = 14;
private const uint HSIZE = (1 << 14);
private const uint MAX_LIT = (1 << 5);
private const uint MAX_OFF = (1 << 13);
private const uint MAX_REF = ((1 << 8) + (1 << 3));
/// <summary>
/// Compresses the data using LibLZF algorithm
/// </summary>
/// <param name="input">Reference to the data to compress</param>
/// <param name="inputLength">Length of the data to compress</param>
/// <param name="output">Reference to a buffer which will contain the compressed data</param>
/// <param name="outputLength">Length of the compression buffer (should be bigger than the input buffer)</param>
/// <returns>The size of the compressed archive in the output buffer</returns>
public static int LzffCompress(byte[] input, int inputLength, byte[] output, int outputLength)
{
Array.Clear(HashTable, 0, (int)HSIZE);
long hslot;
uint iidx = 0;
uint oidx = 0;
long reference;
uint hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]); // FRST(in_data, iidx);
long off;
int lit = 0;
for (; ; )
{
if (iidx < inputLength - 2)
{
hval = (hval << 8) | input[iidx + 2];
hslot = ((hval ^ (hval << 5)) >> (int)(((3 * 8 - HLOG)) - hval * 5) & (HSIZE - 1));
reference = HashTable[hslot];
HashTable[hslot] = (long)iidx;
if ((off = iidx - reference - 1) < MAX_OFF
&& iidx + 4 < inputLength
&& reference > 0
&& input[reference + 0] == input[iidx + 0]
&& input[reference + 1] == input[iidx + 1]
&& input[reference + 2] == input[iidx + 2]
)
{
/* match found at *reference++ */
uint len = 2;
uint maxlen = (uint)inputLength - iidx - len;
maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
if (oidx + lit + 1 + 3 >= outputLength)
{
return 0;
}
do
{
len++;
}
while (len < maxlen && input[reference + len] == input[iidx + len]);
if (lit != 0)
{
output[oidx++] = (byte)(lit - 1);
lit = -lit;
do
{
output[oidx++] = input[iidx + lit];
}
while ((++lit) != 0);
}
len -= 2;
iidx++;
if (len < 7)
{
output[oidx++] = (byte)((off >> 8) + (len << 5));
}
else
{
output[oidx++] = (byte)((off >> 8) + (7 << 5));
output[oidx++] = (byte)(len - 7);
}
output[oidx++] = (byte)off;
iidx += len - 1;
hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]);
hval = (hval << 8) | input[iidx + 2];
HashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - HLOG)) - hval * 5) & (HSIZE - 1))] = iidx;
iidx++;
hval = (hval << 8) | input[iidx + 2];
HashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - HLOG)) - hval * 5) & (HSIZE - 1))] = iidx;
iidx++;
continue;
}
}
else if (iidx == inputLength)
{
break;
}
/* one more literal byte we must copy */
lit++;
iidx++;
if (lit == MAX_LIT)
{
if (oidx + 1 + MAX_LIT >= outputLength)
{
return 0;
}
output[oidx++] = (byte)(MAX_LIT - 1);
lit = -lit;
do
{
output[oidx++] = input[iidx + lit];
}
while ((++lit) != 0);
}
}
if (lit != 0)
{
if (oidx + lit + 1 >= outputLength)
{
return 0;
}
output[oidx++] = (byte)(lit - 1);
lit = -lit;
do
{
output[oidx++] = input[iidx + lit];
}
while ((++lit) != 0);
}
return (int)oidx;
}
public enum KritaNodeType
{
paintlayer,
vectorlayer,
grouplayer,
referenceimages
}
protected override Document OnLoad(Stream input)
{
Document result;
using (ZipArchive zipArchive = new ZipArchive(input, ZipArchiveMode.Read))
{
try
{
string text;
using (StreamReader streamReader = new StreamReader(zipArchive.GetEntry("mimetype").Open()))
{
text = streamReader.ReadToEnd();
}
if (!text.Equals("application/x-krita", StringComparison.Ordinal))
{
throw new FormatException("Incorrect mimetype: " + text);
}
}
catch (NullReferenceException)
{
throw new FormatException("No mimetype found in OpenRaster file");
}
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(zipArchive.GetEntry("maindoc.xml").Open());
XmlElement documentElement = (XmlElement)xmlDocument.DocumentElement.GetElementsByTagName("IMAGE")[0];
int width = int.Parse(documentElement.GetAttribute("width"), CultureInfo.InvariantCulture);
int height = int.Parse(documentElement.GetAttribute("height"), CultureInfo.InvariantCulture);
string name = documentElement.GetAttribute("name");
Document document = new Document(width, height)
{
DpuUnit = MeasurementUnit.Inch,
DpuX = double.Parse(KraFileType.getAttribute(documentElement, "x-res", "72"), CultureInfo.InvariantCulture),
DpuY = double.Parse(KraFileType.getAttribute(documentElement, "y-res", "72"), CultureInfo.InvariantCulture)
};
XmlNodeList elementsByTagName = ((XmlElement)xmlDocument.GetElementsByTagName("layers")[0]).GetElementsByTagName("layer");
if (elementsByTagName.Count == 0)
{
throw new FormatException("No layers found in KritaRaster file");
}
int reverseCount = elementsByTagName.Count - 1;
for (int i = reverseCount; i >= 0; i--)
{
XmlElement xmlElement = (XmlElement)elementsByTagName[i];
int layerNumber = reverseCount - i;
KritaNodeType nodeType;
if (!Enum.TryParse<KritaNodeType>(KraFileType.getAttribute(xmlElement, "nodetype", "paintlayer"), true, out nodeType))
{
nodeType = KritaNodeType.paintlayer;
}
if (nodeType == KritaNodeType.paintlayer)
{
string layerName = KraFileType.getAttribute(xmlElement, "name", string.Format("Layer {0}", layerNumber));
string filename = KraFileType.getAttribute(xmlElement, "filename", string.Format("layer{0}", layerNumber));
int xofs = int.Parse(KraFileType.getAttribute(xmlElement, "x", "0"), CultureInfo.InvariantCulture);
int yofs = int.Parse(KraFileType.getAttribute(xmlElement, "y", "0"), CultureInfo.InvariantCulture);
byte opacity = byte.Parse(KraFileType.getAttribute(xmlElement, "opacity", "255"), CultureInfo.InvariantCulture);
bool visible = KraFileType.getAttribute(xmlElement, "visible", "1") == "1";
using (Stream stream = zipArchive.GetEntry(name + "/layers/" + filename).Open())
{
using (Bitmap bitmapFromOraLayer = KraFileType.getBitmapFromKraLayer(xofs, yofs, stream, width, height, filename))
{
BitmapLayer bitmapLayer = null;
if (i == reverseCount)
{
bitmapLayer = Layer.CreateBackgroundLayer(width, height);
}
else
{
bitmapLayer = new BitmapLayer(width, height);
}
bitmapLayer.Name = layerName;
bitmapLayer.Opacity = opacity;
bitmapLayer.Visible = visible;
string compositeop = KraFileType.getAttribute(xmlElement, "compositeop", "normal");
try
{
bitmapLayer.BlendMode = (LayerBlendMode)Enum.Parse(typeof(LayerBlendMode), compositeop, true);
}
catch
{
switch (compositeop)
{
case "add":
bitmapLayer.BlendMode = LayerBlendMode.Additive;
break;
case "burn":
bitmapLayer.BlendMode = LayerBlendMode.ColorBurn;
break;
case "dodge":
bitmapLayer.BlendMode = LayerBlendMode.ColorDodge;
break;
default:
bitmapLayer.BlendMode = LayerBlendMode.Normal;
break;
}
}
bitmapLayer.Surface.CopyFromGdipBitmap(bitmapFromOraLayer, false);
document.Layers.Insert(layerNumber, bitmapLayer);
}
}
}
}
result = document;
}
return result;
}
protected unsafe override void OnSave(Document input, Stream output, SaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback)
{
if (input == null || output == null)
{
throw new ArgumentNullException("Null error");
}
using (ZipArchive zipArchive = new ZipArchive(output, ZipArchiveMode.Update, true))
{
LayerInfo[] array2 = new LayerInfo[input.Layers.Count];
for (int i = 0; i < input.Layers.Count; i++)
{
BitmapLayer bitmapLayer = (BitmapLayer)input.Layers[i];
Rectangle bounds = bitmapLayer.Surface.Bounds;
int width = bitmapLayer.Width;
int height = bitmapLayer.Height;
using (Stream layer = zipArchive.CreateEntry("root/layers/layer" + i.ToString(CultureInfo.InvariantCulture)).Open())
{
var aliasedBitmap = bitmapLayer.Surface.CreateAliasedBitmap(bounds, true);
WriteLayer(layer, aliasedBitmap);
}
using (Stream defaultPixel = zipArchive.CreateEntry("root/layers/layer" + i.ToString(CultureInfo.InvariantCulture) + ".defaultpixel").Open())
{
byte[] defaultPixelArray = Convert.FromBase64String(this.DefaultPixel);
defaultPixel.Write(defaultPixelArray, 0, defaultPixelArray.Length);
}
using (Stream icc = zipArchive.CreateEntry("root/layers/layer" + i.ToString(CultureInfo.InvariantCulture) + ".icc").Open())
{
byte[] iccArray = Convert.FromBase64String(this.ICC);
icc.Write(iccArray, 0, iccArray.Length);
}
}
using (Stream icc = zipArchive.CreateEntry("root/annotations/icc").Open())
{
byte[] iccArray = Convert.FromBase64String(this.ICC);
icc.Write(iccArray, 0, iccArray.Length);
}
using (Stream animation = zipArchive.CreateEntry("root/animation/index.xml").Open())
{
byte[] animationArray = getAnimationIndexData();
animation.Write(animationArray, 0, animationArray.Length);
}
byte[] array = Convert.FromBase64String(this.MimeType);
output.Write(array, 0, array.Length);
using (Stream stream4 = zipArchive.CreateEntry("maindoc.xml").Open())
{
double dpiX;
double dpiY;
switch (input.DpuUnit)
{
case MeasurementUnit.Pixel:
dpiX = Document.GetDefaultDpu(MeasurementUnit.Inch);
dpiY = Document.GetDefaultDpu(MeasurementUnit.Inch);
break;
case MeasurementUnit.Inch:
dpiX = input.DpuX;
dpiY = input.DpuY;
break;
case MeasurementUnit.Centimeter:
dpiX = Document.DotsPerCmToDotsPerInch(input.DpuX);
dpiY = Document.DotsPerCmToDotsPerInch(input.DpuY);
break;
default:
throw new InvalidEnumArgumentException("Invalid measurement unit.");
}
array = getLayerXmlData(input.Layers, array2, dpiX, dpiY);
stream4.Write(array, 0, array.Length);
}
using (Stream info = zipArchive.CreateEntry("documentinfo.xml").Open())
{
var infoArray = getInfoXmlData();
info.Write(infoArray, 0, infoArray.Length);
}
using (Stream mimetype = zipArchive.CreateEntry("mimetype").Open())
{
byte[] mimetypeArray = Convert.FromBase64String(this.MimeType);
mimetype.Write(mimetypeArray, 0, mimetypeArray.Length);
}
using (Surface surface = new Surface(input.Width, input.Height))
{
input.Flatten(surface);
using (MemoryStream memoryStream2 = new MemoryStream())
{
surface.CreateAliasedBitmap().Save(memoryStream2, ImageFormat.Png);
array = memoryStream2.ToArray();
}
using (Stream stream5 = zipArchive.CreateEntry("mergedimage.png").Open())
{
stream5.Write(array, 0, array.Length);
}
using (Surface surface2 = new Surface(getThumbDimensions(input.Width, input.Height)))
{
surface2.FitSurface(ResamplingAlgorithm.SuperSampling, surface);
using (MemoryStream memoryStream3 = new MemoryStream())
{
surface2.CreateAliasedBitmap().Save(memoryStream3, ImageFormat.Png);
array = memoryStream3.ToArray();
}
}
using (Stream stream6 = zipArchive.CreateEntry("preview.png").Open())
{
stream6.Write(array, 0, array.Length);
}
}
}
}
private static byte[] getLayerXmlData(LayerList layers, KraFileType.LayerInfo[] info, double dpiX, double dpiY)
{
byte[] result = null;
using (MemoryStream memoryStream = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = false,
ConformanceLevel = ConformanceLevel.Document,
CloseOutput = false
};
XmlWriter xmlWriter = XmlWriter.Create(memoryStream, settings);
xmlWriter.WriteStartDocument();
Xmlns(xmlWriter, "DOC");
xmlWriter.WriteAttributeString("width", layers.GetAt(0).Width.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteAttributeString("syntaxVersion", "2.0");
xmlWriter.WriteAttributeString("kritaVersion", "5.0.2");
xmlWriter.WriteAttributeString("editor", "Krita");
xmlWriter.WriteStartElement("IMAGE");
xmlWriter.WriteAttributeString("width", layers.GetAt(0).Width.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteAttributeString("height", layers.GetAt(0).Height.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteAttributeString("version", "0.0.3");
xmlWriter.WriteAttributeString("x-res", dpiX.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteAttributeString("y-res", dpiY.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteAttributeString("name", "root");
xmlWriter.WriteAttributeString("mime", "application/x-kra");
xmlWriter.WriteAttributeString("colorspacename", "RGBA");
xmlWriter.WriteAttributeString("profile", "sRGB-elle-V2-srgbtrc.icc");
xmlWriter.WriteStartElement("layers");
for (int i = layers.Count - 1; i >= 0; i--)
{
BitmapLayer bitmapLayer = (BitmapLayer)layers[i];
xmlWriter.WriteStartElement("layer");
xmlWriter.WriteAttributeString("nodetype", "paintlayer");
xmlWriter.WriteAttributeString("name", bitmapLayer.Name);
xmlWriter.WriteAttributeString("uuid", "{" + Guid.NewGuid().ToString().ToLower() + "}");
xmlWriter.WriteAttributeString("opacity", bitmapLayer.Opacity.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteAttributeString("filename", "layer" + i.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteAttributeString("visible", bitmapLayer.Visible ? "1" : "0");
xmlWriter.WriteAttributeString("x", info[i].x.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteAttributeString("y", info[i].y.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteAttributeString("colorspacename", "RGBA");
xmlWriter.WriteAttributeString("channelflags", "");
xmlWriter.WriteAttributeString("collapsed", "0");
xmlWriter.WriteAttributeString("locked", "0");
xmlWriter.WriteAttributeString("intimeline", "1");
xmlWriter.WriteAttributeString("onionskin", "0");
xmlWriter.WriteAttributeString("colorlabel", "0");
xmlWriter.WriteAttributeString("channellockflags", "1111");
if (bitmapLayer.IsBackground)
{
xmlWriter.WriteAttributeString("selected", "true");
}
try
{
var blendMode = bitmapLayer.BlendMode;
var compositeOP = blendMode.ToString().ToLower();
if (blendMode == LayerBlendMode.Additive) compositeOP = "add";
else if(blendMode == LayerBlendMode.ColorDodge) compositeOP = "dodge";
else if (blendMode == LayerBlendMode.ColorBurn) compositeOP = "burn";
else if (blendMode == LayerBlendMode.Difference) compositeOP = "normal";
xmlWriter.WriteAttributeString("compositeop", compositeOP);
}
catch
{
xmlWriter.WriteAttributeString("compositeop", "normal");
}
xmlWriter.WriteEndElement();
}
xmlWriter.WriteEndElement();
AttributedElement(xmlWriter, "ProjectionBackgroundColor", "ColorData", "AAAAAA==");
AttributedElement(xmlWriter, "GlobalAssistantsColor", "SimpleColorData", "176,176,176,255");
EmptyElement(xmlWriter, "Palettes");
EmptyElement(xmlWriter, "resources");
xmlWriter.WriteStartElement("animation");
AnimationElement(xmlWriter, false);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
result = memoryStream.ToArray();
}
return result;
}
private static byte[] getAnimationIndexData()
{
byte[] result = null;
using (MemoryStream memoryStream = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = false,
ConformanceLevel = ConformanceLevel.Document,
CloseOutput = false
};
XmlWriter xmlWriter = XmlWriter.Create(memoryStream, settings);
xmlWriter.WriteStartDocument();
Xmlns(xmlWriter, "animation-metadata");
AnimationElement(xmlWriter, true);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
result = memoryStream.ToArray();
}
return result;
}
private static void AnimationElement(XmlWriter xmlWriter, bool index)
{
xmlWriter.WriteStartElement("framerate");
xmlWriter.WriteAttributeString("type", "value");
xmlWriter.WriteAttributeString("value", "24");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("range");
xmlWriter.WriteAttributeString("type", "timerange");
xmlWriter.WriteAttributeString("from", "0");
xmlWriter.WriteAttributeString("to", "100");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("currentTime");
xmlWriter.WriteAttributeString("type", "value");
xmlWriter.WriteAttributeString("value", "0");
xmlWriter.WriteEndElement();
if (index)
{
xmlWriter.WriteStartElement("export-settings");
xmlWriter.WriteStartElement("sequenceFilePath");
xmlWriter.WriteAttributeString("type", "value");
xmlWriter.WriteAttributeString("value", "");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("sequenceBaseName");
xmlWriter.WriteAttributeString("type", "value");
xmlWriter.WriteAttributeString("value", "");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("sequenceInitialFrameNumber");
xmlWriter.WriteAttributeString("type", "value");
xmlWriter.WriteAttributeString("value", "-1");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
}
}
private static void Xmlns(XmlWriter xmlWriter, string name, string link = "http://www.calligra.org/DTD/krita")
{
xmlWriter.WriteStartElement(name, link);
xmlWriter.WriteStartAttribute("xmlns", null, null);
xmlWriter.WriteRaw(link);
xmlWriter.WriteEndAttribute();
}
private static byte[] getInfoXmlData()
{
byte[] result = null;
using (MemoryStream memoryStream = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = false,
ConformanceLevel = ConformanceLevel.Document,
CloseOutput = false
};
XmlWriter xmlWriter = XmlWriter.Create(memoryStream, settings);
xmlWriter.WriteStartDocument();
Xmlns(xmlWriter, "document-info", "http://www.calligra.org/DTD/document-info");
xmlWriter.WriteStartElement("about");
ValuedElement(xmlWriter, "title", "root");
EmptyElement(xmlWriter, "description");
EmptyElement(xmlWriter, "subject");
ValuedElement(xmlWriter, "abstract", "<![CDATA[]]>");
EmptyElement(xmlWriter, "keyword");
ValuedElement(xmlWriter, "initial-creator", "Unknown");
ValuedElement(xmlWriter, "editing-cycles", "1");
ValuedElement(xmlWriter, "editing-time", "1");
ValuedElement(xmlWriter, "date", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss"));
ValuedElement(xmlWriter, "creation-date", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss"));
EmptyElement(xmlWriter, "language");
EmptyElement(xmlWriter, "license");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("author");
EmptyElement(xmlWriter, "full-name");
EmptyElement(xmlWriter, "creator-first-name");
EmptyElement(xmlWriter, "creator-last-name");
EmptyElement(xmlWriter, "initial");
EmptyElement(xmlWriter, "author-title");
EmptyElement(xmlWriter, "position");
EmptyElement(xmlWriter, "company");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
result = memoryStream.ToArray();
}
return result;
}
private static void ValuedElement(XmlWriter xmlWriter, string name, string value)
{
xmlWriter.WriteStartElement(name);
xmlWriter.WriteValue(value);
xmlWriter.WriteEndElement();
}
private static void AttributedElement(XmlWriter xmlWriter, string name, string attribute, string value)
{
xmlWriter.WriteStartElement(name);
xmlWriter.WriteAttributeString(attribute, value);
xmlWriter.WriteEndElement();
}
private static void EmptyElement(XmlWriter xmlWriter, string name)
{
xmlWriter.WriteStartElement(name);
xmlWriter.WriteEndElement();
}
private static Size getThumbDimensions(int width, int height)
{
if (width <= ThumbMaxSize && height <= ThumbMaxSize)
{
return new Size(width, height);
}
if (width <= height)
{
return new Size((int)((double)width / (double)height * ThumbMaxSize), ThumbMaxSize);
}
return new Size(ThumbMaxSize, (int)((double)height / (double)width * ThumbMaxSize));
}
private static string getAttribute(XmlElement element, string attribute, string defValue)
{
string attribute2 = element.GetAttribute(attribute);
if (!string.IsNullOrEmpty(attribute2))
{
return attribute2;
}
return defValue;
}
}
}