forked from morkt/GARbro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageBMP.cs
219 lines (205 loc) · 8.04 KB
/
ImageBMP.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
//! \file ImageBMP.cs
//! \date Wed Jul 16 18:06:47 2014
//! \brief BMP image implementation.
//
// Copyright (C) 2014-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.IO;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Windows.Media.Imaging;
using GameRes.Utility;
using System.Windows.Media;
using System.Collections.Generic;
namespace GameRes
{
public class BmpMetaData : ImageMetaData
{
public uint ImageLength;
public uint HeaderLength;
}
public interface IBmpExtension
{
ImageData Read (IBinaryStream file, BmpMetaData info);
}
[Export(typeof(ImageFormat))]
public class BmpFormat : ImageFormat
{
public override string Tag { get { return "BMP"; } }
public override string Description { get { return "Windows device independent bitmap"; } }
public override uint Signature { get { return 0; } }
public override bool CanWrite { get { return true; } }
#pragma warning disable 649
[ImportMany(typeof(IBmpExtension))]
private IEnumerable<IBmpExtension> m_extensions;
#pragma warning restore 649
bool EnableExtensions = true;
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var bmp_info = info as BmpMetaData;
if (bmp_info != null && EnableExtensions && file.AsStream.CanSeek)
{
foreach (var ext in m_extensions)
{
try
{
var image = ext.Read (file, bmp_info);
if (null != image)
return image;
}
catch (System.Exception X)
{
System.Diagnostics.Trace.WriteLine (X.Message, ext.ToString());
}
file.Position = 0;
}
}
var decoder = new BmpBitmapDecoder (file.AsStream,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
BitmapSource frame = decoder.Frames.First();
frame.Freeze();
return new ImageData (frame, info);
}
public override void Write (Stream file, ImageData image)
{
var encoder = new BmpBitmapEncoder();
encoder.Frames.Add (BitmapFrame.Create (image.Bitmap, null, null, null));
encoder.Save (file);
}
void SkipBytes (IBinaryStream file, uint num)
{
if (file.AsStream.CanSeek)
file.Seek (num, SeekOrigin.Current);
else
{
for (int i = 0; i < num / 4; ++i)
file.ReadInt32();
for (int i = 0; i < num % 4; ++i)
file.ReadByte();
}
}
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
int c1 = file.ReadByte();
int c2 = file.ReadByte();
if ('B' != c1 || 'M' != c2)
return null;
uint size = file.ReadUInt32();
SkipBytes (file, 8);
uint header_size = file.ReadUInt32();
if (size < 14+header_size)
{
// some otherwise valid bitmaps have size field set to zero
if (size != 0 || !file.AsStream.CanSeek)
return null;
size = (uint)file.Length;
}
uint width, height;
if (0xC == header_size)
{
width = file.ReadUInt16();
height = file.ReadUInt16();
}
else if (header_size < 40 || size-14 < header_size)
{
return null;
}
else
{
width = file.ReadUInt32();
height = file.ReadUInt32();
}
file.ReadInt16();
int bpp = file.ReadInt16();
return new BmpMetaData {
Width = width,
Height = height,
OffsetX = 0,
OffsetY = 0,
BPP = bpp,
ImageLength = size,
HeaderLength = header_size + 14,
};
}
}
[Export(typeof(IBmpExtension))]
public class BitmapWithAlpha : IBmpExtension
{
public ImageData Read (IBinaryStream file, BmpMetaData info)
{
if (file.AsStream.CanSeek)
{
var width_x_height = info.Width * info.Height;
uint bmp_length = width_x_height * (uint)info.BPP/8 + info.HeaderLength;
if (bmp_length == info.ImageLength || bmp_length+2 == info.ImageLength)
{
if (0x20 == info.BPP)
{
return ReadBitmapBGRA (file, info);
}
else if (0x18 == info.BPP && (info.ImageLength + width_x_height) == file.Length)
{
return ReadBitmapWithAlpha (file, info);
}
}
}
return null;
}
private ImageData ReadBitmapWithAlpha (IBinaryStream file, BmpMetaData info)
{
file.Position = info.ImageLength;
var alpha = new byte[info.Width*info.Height];
if (alpha.Length != file.Read (alpha, 0, alpha.Length))
throw new EndOfStreamException();
file.Position = info.HeaderLength;
int dst_stride = (int)info.Width * 4;
var pixels = new byte[(int)info.Height * dst_stride];
int a_src = 0;
for (int y = (int)info.Height-1; y >= 0; --y)
{
int dst = dst_stride * y;
for (int x = 0; x < dst_stride; x += 4)
{
file.Read (pixels, dst+x, 3);
pixels[dst+x+3] = alpha[a_src++];
}
}
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels, dst_stride);
}
private ImageData ReadBitmapBGRA (IBinaryStream file, BmpMetaData info)
{
file.Position = info.HeaderLength;
int stride = (int)info.Width * 4;
var pixels = new byte[(int)info.Height * stride];
bool has_alpha = false;
for (int y = (int)info.Height-1; y >= 0; --y)
{
int dst = stride * y;
file.Read (pixels, dst, stride);
for (int x = 3; !has_alpha && x < stride; x += 4)
has_alpha = pixels[dst+x] != 0;
}
PixelFormat format = has_alpha ? PixelFormats.Bgra32 : PixelFormats.Bgr32;
return ImageData.Create (info, format, null, pixels, stride);
}
}
}