mif2png(QQGame 专用 mif 格式转 png 格式)

  

  大学时代的作品《UMU 游戏之争上游》的副产品,mif2bmp 改进版,用 GdiPlus 来产生 png 格式图片。

  mif2png.exe 下载:http://umu.ys168.com/,Tools 目录里。或 http://download.csdn.net/detail/umu/3843545

  以前有文章分析 mif 格式,不过很早了懒得找,直接开源吧,先发头部结构体:

#pragma pack(1)
struct MifHeader
{
    DWORD version;
    DWORD width;
    DWORD height;
    DWORD type;
    DWORD frame_count;
};
#pragma pack()

 

以下代码是 C# 写的 Paint.NET 文件类型插件 MifFileType.cs

 

using System;
using System.Collections.Generic;
using System.Text;
using PaintDotNet;
using PaintDotNet.Data;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace MifFileType
{
    public class MifFileType : FileType
    {
        public MifFileType()
            : base("MIF Files", FileTypeFlags.SupportsLoading | FileTypeFlags.SupportsLayers, new String[] { ".mif" })
        {
        }

        protected override Document OnLoad(Stream input)
        {
            if (input.Length < 20)
            {
                MessageBox.Show("Invalid MIF File", "UMU Corporation - MifFileTypePlugIn", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Bitmap b = new Bitmap(800, 600);
                return Document.FromImage(b);
            }

            try
            {
                BinaryReader br = new BinaryReader(input);

                int MifVersion = br.ReadInt32();
                int FrameWidth = br.ReadInt32();
                int FrameHeight = br.ReadInt32();
                int MifType = br.ReadInt32();
                int FrameCount = br.ReadInt32();

                int ImageWidth = FrameWidth;
                int ImageHeight = FrameHeight * FrameCount;

                bool Valid = true;
                long Prefix;

                if (MifType == 3)
                {
                    Prefix = 20;
                }
                else if (MifType == 7)
                {
                    Prefix = 20 + 4 * FrameCount;
                }
                else
                {
                    MessageBox.Show("Invalid MIF File", "UMU Corporation - MifFileTypePlugIn", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    Bitmap b = new Bitmap(800, 600);
                    return Document.FromImage(b);
                }

                if (MifVersion == 0)
                {
                    if (Prefix + ImageWidth * ImageHeight * 3 != input.Length)
                    {
                        Valid = false;
                    }
                }
                else if (MifVersion == 1)
                {
                    if (Prefix + ImageWidth * ImageHeight * 3 > input.Length)
                    {
                        Valid = false;
                    }
                }

                if (!Valid)
                {
                    MessageBox.Show("Invalid MIF File", "UMU Corporation - MifFileTypePlugIn", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    Bitmap b = new Bitmap(800, 600);
                    return Document.FromImage(b);
                }

                Bitmap bmp = new Bitmap(ImageWidth, ImageHeight);

                for (int CurrentFrame = 0; CurrentFrame < FrameCount; ++CurrentFrame)
                {
                    if (MifType == 7)
                    {
                        input.Seek(4, SeekOrigin.Current);
                    }

                    UInt16[] rgb16 = new UInt16[FrameWidth * FrameHeight];

                    for (int i = 0; i < FrameHeight * FrameWidth; ++i)
                    {
                        rgb16[i] = br.ReadUInt16();
                    }

                    Byte[] a8 = new Byte[FrameWidth * FrameHeight];

                    for (int i = 0; i < FrameHeight * FrameWidth; ++i)
                    {
                        a8[i] = br.ReadByte();
                    }

                    for (int y = 0; y < FrameHeight; ++y)
                    {
                        for (int x = 0; x < FrameWidth; ++x)
                        {
                            int a = a8[x + y * FrameWidth];
                            int r = (rgb16[x + y * FrameWidth] & 0xF800) >> 8;
                            int g = (rgb16[x + y * FrameWidth] & 0x07E0) >> 3;
                            int b = (rgb16[x + y * FrameWidth] & 0x001F) << 3;

                            if (a == 32)
                            {
                                a = 255;
                            }
                            else if (a > 0)
                            {
                                a <<= 3;
                            }

                            bmp.SetPixel(x, FrameHeight * CurrentFrame + y, Color.FromArgb(a, r, g, b));
                        }
                    }
                }

                br.Close();
                return Document.FromImage(bmp);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "UMU Corporation - MifFileTypePlugIn", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                Bitmap bmp = new Bitmap(800, 600);
                //Document doc = Document.FromImage(bmp);
                //doc.Tag = "UMU Corporation - MifFileTypePlugIn";
                //return doc;
                return Document.FromImage(bmp);
            }
        }
    }

    public class MifFileTypeFactory : IFileTypeFactory
    {
        public FileType[] GetFileTypeInstances()
        {
            return new FileType[] { new MifFileType() };
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值