|
| 1 | +//! \file ImageAF.cs |
| 2 | +//! \date 2018 May 03 |
| 3 | +//! \brief TanukiSoft bitmap. |
| 4 | +// |
| 5 | +// Copyright (C) 2018 by morkt |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to |
| 9 | +// deal in the Software without restriction, including without limitation the |
| 10 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | +// sell copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | +// IN THE SOFTWARE. |
| 24 | +// |
| 25 | + |
| 26 | +using System.ComponentModel.Composition; |
| 27 | +using System.IO; |
| 28 | +using System.Windows.Media; |
| 29 | +using GameRes.Compression; |
| 30 | + |
| 31 | +namespace GameRes.Formats.Tanuki |
| 32 | +{ |
| 33 | + [Export(typeof(ImageFormat))] |
| 34 | + public class AmapFormat : ImageFormat |
| 35 | + { |
| 36 | + public override string Tag { get { return "AMAP"; } } |
| 37 | + public override string Description { get { return "TanukiSoft bitmap format"; } } |
| 38 | + public override uint Signature { get { return 0x50414D41; } } // 'AMAP' |
| 39 | + |
| 40 | + public AmapFormat () |
| 41 | + { |
| 42 | + Extensions = new[] { "af" }; |
| 43 | + } |
| 44 | + |
| 45 | + public override ImageMetaData ReadMetaData (IBinaryStream file) |
| 46 | + { |
| 47 | + var header = file.ReadHeader (0x14); |
| 48 | + return new ImageMetaData { |
| 49 | + Width = header.ToUInt16 (4), |
| 50 | + Height = header.ToUInt16 (6), |
| 51 | + BPP = 8, |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + public override ImageData Read (IBinaryStream file, ImageMetaData info) |
| 56 | + { |
| 57 | + file.Position = 0x10; |
| 58 | + var pixels = LzssUnpack (file); |
| 59 | + return ImageData.Create (info, PixelFormats.Gray8, null, pixels); |
| 60 | + } |
| 61 | + |
| 62 | + public override void Write (Stream file, ImageData image) |
| 63 | + { |
| 64 | + throw new System.NotImplementedException ("AmapFormat.Write not implemented"); |
| 65 | + } |
| 66 | + |
| 67 | + byte[] LzssUnpack (IBinaryStream input) |
| 68 | + { |
| 69 | + int unpacked_size = input.ReadInt32(); |
| 70 | + var output = new byte[unpacked_size]; |
| 71 | + var frame = new byte[0x1000]; |
| 72 | + int frame_pos = 0xFEE; |
| 73 | + int dst = 0; |
| 74 | + int ctl = 0; |
| 75 | + while (dst < unpacked_size) |
| 76 | + { |
| 77 | + ctl >>= 1; |
| 78 | + if (0 == (ctl & 0x100)) |
| 79 | + { |
| 80 | + ctl = input.ReadByte(); |
| 81 | + if (-1 == ctl) |
| 82 | + break; |
| 83 | + ctl |= 0xFF00; |
| 84 | + } |
| 85 | + if (0 != (ctl & 1)) |
| 86 | + { |
| 87 | + int b = input.ReadByte(); |
| 88 | + if (-1 == b) |
| 89 | + break; |
| 90 | + output[dst++] = frame[frame_pos++ & 0xFFF] = (byte)b; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + int lo = input.ReadByte(); |
| 95 | + if (-1 == lo) |
| 96 | + break; |
| 97 | + int hi = input.ReadByte(); |
| 98 | + if (-1 == hi) |
| 99 | + break; |
| 100 | + int offset = (hi & 0xF0) << 4 | lo; |
| 101 | + for (int count = 3 + (~hi & 0xF); count != 0; --count) |
| 102 | + { |
| 103 | + byte v = frame[offset++ & 0xFFF]; |
| 104 | + output[dst++] = frame[frame_pos++ & 0xFFF] = v; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return output; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments