|
| 1 | +using K4os.Compression.LZ4.Encoders; |
| 2 | +using Pfim; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Drawing; |
| 6 | +using System.Drawing.Imaging; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Runtime.InteropServices; |
| 10 | +using System.Text; |
| 11 | + |
| 12 | +namespace edds2png |
| 13 | +{ |
| 14 | + internal class BulkConverter |
| 15 | + { |
| 16 | + private readonly List<string> _imagePaths; |
| 17 | + |
| 18 | + public BulkConverter() |
| 19 | + { |
| 20 | + _imagePaths = new List<string>(); |
| 21 | + } |
| 22 | + |
| 23 | + public void Add(string path) |
| 24 | + { |
| 25 | + if (string.IsNullOrEmpty(path)) |
| 26 | + { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (!path.EndsWith(".edds", StringComparison.OrdinalIgnoreCase)) |
| 31 | + { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + _imagePaths.Add(path); |
| 36 | + } |
| 37 | + |
| 38 | + public void Process() |
| 39 | + { |
| 40 | + List<Exception> conversionExceptions = new List<Exception>(); |
| 41 | + foreach (var imagePath in _imagePaths.AsEnumerable()) |
| 42 | + { |
| 43 | + try |
| 44 | + { |
| 45 | + string fullPath = Path.GetFullPath(imagePath); |
| 46 | + Convert(fullPath); |
| 47 | + |
| 48 | + Console.WriteLine($"{fullPath} -> {Path.ChangeExtension(fullPath, ".png")}"); |
| 49 | + } |
| 50 | + catch (Exception inner) |
| 51 | + { |
| 52 | + conversionExceptions.Add(inner); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (conversionExceptions.Count != 0) |
| 57 | + { |
| 58 | + throw new AggregateException(conversionExceptions); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public bool CanProcess() |
| 63 | + { |
| 64 | + return _imagePaths.Count != 0; |
| 65 | + } |
| 66 | + |
| 67 | + private static unsafe void Convert(string imagePath) |
| 68 | + { |
| 69 | + using (var stream = DecompressEDDS(imagePath)) |
| 70 | + { |
| 71 | + using (var image = Pfimage.FromStream(stream)) |
| 72 | + { |
| 73 | + PixelFormat format; |
| 74 | + switch (image.Format) |
| 75 | + { |
| 76 | + case Pfim.ImageFormat.Rgba32: |
| 77 | + { |
| 78 | + format = PixelFormat.Format32bppArgb; |
| 79 | + break; |
| 80 | + } |
| 81 | + default: |
| 82 | + { |
| 83 | + throw new NotImplementedException(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + var handle = GCHandle.Alloc(image.Data, GCHandleType.Pinned); |
| 89 | + try |
| 90 | + { |
| 91 | + var data = Marshal.UnsafeAddrOfPinnedArrayElement(image.Data, 0); |
| 92 | + var bitmap = new Bitmap(image.Width, image.Height, image.Stride, format, data); |
| 93 | + bitmap.Save(Path.ChangeExtension(imagePath, ".png"), System.Drawing.Imaging.ImageFormat.Png); |
| 94 | + } |
| 95 | + finally |
| 96 | + { |
| 97 | + handle.Free(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static MemoryStream DecompressEDDS(string imagePath) |
| 104 | + { |
| 105 | + List<int> copyBlocks = new List<int>(); |
| 106 | + List<int> lz4Blocks = new List<int>(); |
| 107 | + List<byte> decodedBlocks = new List<byte>(); |
| 108 | + |
| 109 | + void FindBlocks(BinaryReader reader) |
| 110 | + { |
| 111 | + while (true) |
| 112 | + { |
| 113 | + byte[] blocks = reader.ReadBytes(4); |
| 114 | + char[] dd = Encoding.UTF8.GetChars(blocks); |
| 115 | + |
| 116 | + string block = new string(dd); |
| 117 | + int size = reader.ReadInt32(); |
| 118 | + |
| 119 | + switch (block) |
| 120 | + { |
| 121 | + case "COPY": copyBlocks.Add(size); break; |
| 122 | + case "LZ4 ": lz4Blocks.Add(size); break; |
| 123 | + default: reader.BaseStream.Seek(-8, SeekOrigin.Current); return; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + using (var reader = new BinaryReader(File.Open(imagePath, FileMode.Open))) |
| 129 | + { |
| 130 | + byte[] dds_header = reader.ReadBytes(128); |
| 131 | + byte[] dds_header_dx10 = null; |
| 132 | + |
| 133 | + if (dds_header[84] == 'D' && dds_header[85] == 'X' && dds_header[86] == '1' && dds_header[87] == '0') |
| 134 | + { |
| 135 | + dds_header_dx10 = reader.ReadBytes(20); |
| 136 | + } |
| 137 | + |
| 138 | + FindBlocks(reader); |
| 139 | + |
| 140 | + foreach (int count in copyBlocks) |
| 141 | + { |
| 142 | + byte[] buff = reader.ReadBytes(count); |
| 143 | + decodedBlocks.InsertRange(0, buff); |
| 144 | + } |
| 145 | + |
| 146 | + foreach (int Length in lz4Blocks) |
| 147 | + { |
| 148 | + LZ4ChainDecoder lz4ChainDecoder = new LZ4ChainDecoder(65536, 0); |
| 149 | + uint size = reader.ReadUInt32(); |
| 150 | + byte[] target = new byte[size]; |
| 151 | + |
| 152 | + int num = 0; |
| 153 | + int count1 = 0; |
| 154 | + int idx = 0; |
| 155 | + for (; num < Length - 4; num += count1 + 4) |
| 156 | + { |
| 157 | + count1 = reader.ReadInt32() & int.MaxValue; |
| 158 | + byte[] numArray = reader.ReadBytes(count1); |
| 159 | + byte[] buffer = new byte[65536]; |
| 160 | + LZ4EncoderExtensions.DecodeAndDrain(lz4ChainDecoder, numArray, 0, count1, buffer, 0, 65536, out int count2); |
| 161 | + |
| 162 | + Array.Copy(buffer, 0, target, idx, count2); |
| 163 | + idx += count2; |
| 164 | + } |
| 165 | + |
| 166 | + decodedBlocks.InsertRange(0, target); |
| 167 | + } |
| 168 | + |
| 169 | + if (dds_header_dx10 != null) |
| 170 | + { |
| 171 | + decodedBlocks.InsertRange(0, dds_header_dx10); |
| 172 | + } |
| 173 | + |
| 174 | + decodedBlocks.InsertRange(0, dds_header); |
| 175 | + byte[] final = decodedBlocks.ToArray(); |
| 176 | + |
| 177 | + MemoryStream stream = new MemoryStream(); |
| 178 | + stream.Write(final, 0, final.Length); |
| 179 | + stream.Position = 0; |
| 180 | + |
| 181 | + return stream; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments