<commit_before>//! \file       ImageMGD.cs
//! \date       Thu Nov 24 14:37:18 2016
//! \brief      NSystem image format.
//
// Copyright (C) 2016 by morkt
//
// 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.
//

using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace GameRes.Formats.NSystem
{
    internal class MgdMetaData : ImageMetaData
    {
        public int  DataOffset;
        public int  UnpackedSize;
        public int  Mode;
    }

    [Export(typeof(ImageFormat))]
    public class MgdFormat : ImageFormat
    {
        public override string         Tag { get { return "MGD/NSystem"; } }
        public override string Description { get { return "NSystem image format"; } }
        public override uint     Signature { get { return 0x2044474D; } } // 'MGD '

        public override ImageMetaData ReadMetaData (IBinaryStream file)
        {
            var header = file.ReadHeader (0x1A);
            int header_size = header.ToUInt16 (4);
            uint width      = header.ToUInt16 (0xC);
            uint height     = header.ToUInt16 (0xE);
            int unpacked_size = header.ToInt32 (0x10);
            int mode        = header.ToInt32 (0x18);
            if (mode < 0 || mode > 2)
                return null;
            return new MgdMetaData
            {
                Width = width,
                Height = height,
                BPP = 32,
                DataOffset = header_size,
                UnpackedSize = unpacked_size,
                Mode = mode,
            };
        }

        public override ImageData Read (IBinaryStream file, ImageMetaData info)
        {
            var meta = (MgdMetaData)info;
            file.Position = meta.DataOffset;
            int data_size = file.ReadInt32();
            switch (meta.Mode)
            {
            case 0:
                var pixels = file.ReadBytes (data_size);
                return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);

            case 1:
                {
                    var decoder = new MgdDecoder (file, meta, data_size);
                    decoder.Unpack();
                    return ImageData.Create (info, PixelFormats.Bgra32, null, decoder.Data);
                }

            case 2:
                using (var png = new StreamRegion (file.AsStream, file.Position, data_size, true))
                {
                    var decoder = new PngBitmapDecoder (png, 
                        BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                    var frame = decoder.Frames[0];
                    frame.Freeze();
                    return new ImageData (frame, info);
                }

            default:
                throw new InvalidFormatException();
            }
        }

        public override void Write (Stream file, ImageData image)
        {
            throw new System.NotImplementedException ("MgdFormat.Write not implemented");
        }
    }

    internal class MgdDecoder
    {
        IBinaryStream   m_input;
        MgdMetaData     m_info;
        byte[]          m_output;

        public byte[] Data { get { return m_output; } }

        public MgdDecoder (IBinaryStream input, MgdMetaData info, int packed_size)
        {
            m_input = input;
            m_info = info;
            m_output = new byte[m_info.UnpackedSize];
        }

        public void Unpack ()
        {
            int alpha_size = m_input.ReadInt32();
            UnpackAlpha (alpha_size);
            int rgb_size = m_input.ReadInt32();
            UnpackColor (rgb_size);
        }

        void UnpackAlpha (int length)
        {
            int dst = 3;
            while (length > 0)
            {
                int count = m_input.ReadInt16();
                length -= 2;
                if (count < 0)
                {
                    count = (count & 0x7FFF) + 1;
                    byte a = (byte)~m_input.ReadUInt8();
                    length--;
                    for (int i = 0; i < count; ++i)
                    {
                        m_output[dst] = a;
                        dst += 4;
                    }
                }
                else
                {
                    for (int i = 0; i < count; ++i)
                    {
                        m_output[dst] = (byte)~m_input.ReadUInt8();
                        dst += 4;
                    }
                    length -= count;
                }

            }
        }

        void UnpackColor (int length)
        {
            int dst = 0;
            while (length > 0)
            {
                int count = m_input.ReadUInt8();
                length--;
                switch (count & 0xC0)
                {
                case 0x80:
                    count &= 0x3F;
                    int b = m_output[dst-4];<commit_msg>(MGD): apparently alpha channel is not inverted.<commit_after>//! \file       ImageMGD.cs
//! \date       Thu Nov 24 14:37:18 2016
//! \brief      NSystem image format.
//
// Copyright (C) 2016 by morkt
//
// 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.
//

using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace GameRes.Formats.NSystem
{
    internal class MgdMetaData : ImageMetaData
    {
        public int  DataOffset;
        public int  UnpackedSize;
        public int  Mode;
    }

    [Export(typeof(ImageFormat))]
    public class MgdFormat : ImageFormat
    {
        public override string         Tag { get { return "MGD/NSystem"; } }
        public override string Description { get { return "NSystem image format"; } }
        public override uint     Signature { get { return 0x2044474D; } } // 'MGD '

        public override ImageMetaData ReadMetaData (IBinaryStream file)
        {
            var header = file.ReadHeader (0x1A);
            int header_size = header.ToUInt16 (4);
            uint width      = header.ToUInt16 (0xC);
            uint height     = header.ToUInt16 (0xE);
            int unpacked_size = header.ToInt32 (0x10);
            int mode        = header.ToInt32 (0x18);
            if (mode < 0 || mode > 2)
                return null;
            return new MgdMetaData
            {
                Width = width,
                Height = height,
                BPP = 32,
                DataOffset = header_size,
                UnpackedSize = unpacked_size,
                Mode = mode,
            };
        }

        public override ImageData Read (IBinaryStream file, ImageMetaData info)
        {
            var meta = (MgdMetaData)info;
            file.Position = meta.DataOffset;
            int data_size = file.ReadInt32();
            switch (meta.Mode)
            {
            case 0:
                var pixels = file.ReadBytes (data_size);
                return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);

            case 1:
                {
                    var decoder = new MgdDecoder (file, meta, data_size);
                    decoder.Unpack();
                    return ImageData.Create (info, PixelFormats.Bgra32, null, decoder.Data);
                }

            case 2:
                using (var png = new StreamRegion (file.AsStream, file.Position, data_size, true))
                {
                    var decoder = new PngBitmapDecoder (png, 
                        BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                    var frame = decoder.Frames[0];
                    frame.Freeze();
                    return new ImageData (frame, info);
                }

            default:
                throw new InvalidFormatException();
            }
        }

        public override void Write (Stream file, ImageData image)
        {
            throw new System.NotImplementedException ("MgdFormat.Write not implemented");
        }
    }

    internal class MgdDecoder
    {
        IBinaryStream   m_input;
        MgdMetaData     m_info;
        byte[]          m_output;

        public byte[] Data { get { return m_output; } }

        public MgdDecoder (IBinaryStream input, MgdMetaData info, int packed_size)
        {
            m_input = input;
            m_info = info;
            m_output = new byte[m_info.UnpackedSize];
        }

        public void Unpack ()
        {
            int alpha_size = m_input.ReadInt32();
            UnpackAlpha (alpha_size);
            int rgb_size = m_input.ReadInt32();
            UnpackColor (rgb_size);
        }

        void UnpackAlpha (int length)
        {
            int dst = 3;
            while (length > 0)
            {
                int count = m_input.ReadInt16();
                length -= 2;
                if (count < 0)
                {
                    count = (count & 0x7FFF) + 1;
                    byte a = m_input.ReadUInt8();
                    length--;
                    for (int i = 0; i < count; ++i)
                    {
                        m_output[dst] = a;
                        dst += 4;
                    }
                }
                else
                {
                    for (int i = 0; i < count; ++i)
                    {
                        m_output[dst] = m_input.ReadUInt8();
                        dst += 4;
                    }
                    length -= count;
                }

            }
        }

        void UnpackColor (int length)
        {
            int dst = 0;
            while (length > 0)
            {
                int count = m_input.ReadUInt8();
                length--;
                switch (count & 0xC0)
                {
                case 0x80:
                    count &= 0x3F;
                    int b = m_output[dst-4];
{'fraction_non_alphanumeric': 0.08060580563735802, 'fraction_numerical': 0.014976861590239797, 'mean_word_length': 1.4099756690997567, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '945256', 'n_tokens_mistral': 3240, 'n_tokens_neox': 2940, 'n_words': 995}