2014-07-21 23:26:28 +04:00
|
|
|
//! \file ImageBMP.cs
|
|
|
|
//! \date Wed Jul 16 18:06:47 2014
|
|
|
|
//! \brief BMP image implementation.
|
|
|
|
//
|
2016-04-25 18:02:01 +04:00
|
|
|
// Copyright (C) 2014-2016 by morkt
|
2014-07-28 00:50:18 +04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2014-07-21 23:26:28 +04:00
|
|
|
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
2018-01-12 19:56:25 +04:00
|
|
|
using System.Collections.Generic;
|
2014-07-21 23:26:28 +04:00
|
|
|
using System.ComponentModel.Composition;
|
2018-01-12 19:56:25 +04:00
|
|
|
using System.Windows.Media;
|
2014-07-21 23:26:28 +04:00
|
|
|
using System.Windows.Media.Imaging;
|
2018-01-12 19:56:25 +04:00
|
|
|
using GameRes.Strings;
|
2014-07-21 23:26:28 +04:00
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes
|
|
|
|
{
|
2016-04-25 18:02:01 +04:00
|
|
|
public class BmpMetaData : ImageMetaData
|
|
|
|
{
|
|
|
|
public uint ImageLength;
|
2017-11-12 10:02:21 +04:00
|
|
|
public uint ImageOffset;
|
2016-04-25 18:02:01 +04:00
|
|
|
}
|
|
|
|
|
2016-08-02 16:38:42 +04:00
|
|
|
public interface IBmpExtension
|
|
|
|
{
|
2016-10-15 09:34:46 +04:00
|
|
|
ImageData Read (IBinaryStream file, BmpMetaData info);
|
2016-08-02 16:38:42 +04:00
|
|
|
}
|
|
|
|
|
2014-07-21 23:26:28 +04:00
|
|
|
[Export(typeof(ImageFormat))]
|
2023-10-27 04:13:30 +04:00
|
|
|
[ExportMetadata("Priority", 10)]
|
2018-01-08 19:55:28 +04:00
|
|
|
public sealed class BmpFormat : ImageFormat
|
2014-07-21 23:26:28 +04:00
|
|
|
{
|
2016-08-02 16:38:42 +04:00
|
|
|
public override string Tag { get { return "BMP"; } }
|
2014-07-21 23:26:28 +04:00
|
|
|
public override string Description { get { return "Windows device independent bitmap"; } }
|
2016-08-02 16:38:42 +04:00
|
|
|
public override uint Signature { get { return 0; } }
|
2016-10-11 00:05:22 +04:00
|
|
|
public override bool CanWrite { get { return true; } }
|
2016-08-02 16:38:42 +04:00
|
|
|
|
2018-01-08 19:55:28 +04:00
|
|
|
public BmpFormat ()
|
|
|
|
{
|
|
|
|
Settings = new[] { EnableExtensions };
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:38:42 +04:00
|
|
|
#pragma warning disable 649
|
|
|
|
[ImportMany(typeof(IBmpExtension))]
|
|
|
|
private IEnumerable<IBmpExtension> m_extensions;
|
|
|
|
#pragma warning restore 649
|
2014-07-21 23:26:28 +04:00
|
|
|
|
2018-01-08 19:55:28 +04:00
|
|
|
LocalResourceSetting EnableExtensions = new LocalResourceSetting {
|
|
|
|
Name = "BMPEnableExtensions",
|
2018-01-12 19:56:25 +04:00
|
|
|
Text = garStrings.BMPExtensionsText,
|
|
|
|
Description = garStrings.BMPExtensionsDesc,
|
2018-01-08 19:55:28 +04:00
|
|
|
};
|
2016-09-07 20:25:26 +04:00
|
|
|
|
2016-10-15 09:34:46 +04:00
|
|
|
public override ImageData Read (IBinaryStream file, ImageMetaData info)
|
2014-07-21 23:26:28 +04:00
|
|
|
{
|
2016-04-25 18:02:01 +04:00
|
|
|
var bmp_info = info as BmpMetaData;
|
2018-01-08 19:55:28 +04:00
|
|
|
if (bmp_info != null && EnableExtensions.Get<bool>() && file.AsStream.CanSeek)
|
2016-04-25 18:02:01 +04:00
|
|
|
{
|
2016-08-02 16:38:42 +04:00
|
|
|
foreach (var ext in m_extensions)
|
2016-04-25 18:02:01 +04:00
|
|
|
{
|
2016-08-02 16:38:42 +04:00
|
|
|
try
|
2016-07-26 07:05:02 +04:00
|
|
|
{
|
2016-08-02 16:38:42 +04:00
|
|
|
var image = ext.Read (file, bmp_info);
|
|
|
|
if (null != image)
|
|
|
|
return image;
|
2016-07-26 07:05:02 +04:00
|
|
|
}
|
2016-08-02 16:38:42 +04:00
|
|
|
catch (System.Exception X)
|
2016-07-26 07:05:02 +04:00
|
|
|
{
|
2016-08-02 16:38:42 +04:00
|
|
|
System.Diagnostics.Trace.WriteLine (X.Message, ext.ToString());
|
2016-07-26 07:05:02 +04:00
|
|
|
}
|
2016-10-15 09:34:46 +04:00
|
|
|
file.Position = 0;
|
2016-04-25 18:02:01 +04:00
|
|
|
}
|
|
|
|
}
|
2016-10-15 09:34:46 +04:00
|
|
|
var decoder = new BmpBitmapDecoder (file.AsStream,
|
2014-07-21 23:26:28 +04:00
|
|
|
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();
|
2016-06-18 11:02:33 +04:00
|
|
|
encoder.Frames.Add (BitmapFrame.Create (image.Bitmap, null, null, null));
|
2014-07-21 23:26:28 +04:00
|
|
|
encoder.Save (file);
|
|
|
|
}
|
|
|
|
|
2016-10-15 09:34:46 +04:00
|
|
|
public override ImageMetaData ReadMetaData (IBinaryStream file)
|
2014-07-21 23:26:28 +04:00
|
|
|
{
|
2016-10-15 09:34:46 +04:00
|
|
|
int c1 = file.ReadByte();
|
|
|
|
int c2 = file.ReadByte();
|
2016-08-18 14:59:17 +04:00
|
|
|
if ('B' != c1 || 'M' != c2)
|
2014-07-21 23:26:28 +04:00
|
|
|
return null;
|
2016-10-15 09:34:46 +04:00
|
|
|
uint size = file.ReadUInt32();
|
2017-11-12 10:02:21 +04:00
|
|
|
file.ReadUInt32();
|
|
|
|
uint image_offset = file.ReadUInt32();
|
2017-03-29 08:51:53 +04:00
|
|
|
uint header_size = file.ReadUInt32();
|
|
|
|
if (size < 14+header_size)
|
2014-07-21 23:26:28 +04:00
|
|
|
{
|
2016-10-15 09:34:46 +04:00
|
|
|
// some otherwise valid bitmaps have size field set to zero
|
2018-12-19 03:26:02 +04:00
|
|
|
if (size != 0 && size != 0xE || !file.AsStream.CanSeek)
|
2014-07-21 23:26:28 +04:00
|
|
|
return null;
|
2016-10-15 09:34:46 +04:00
|
|
|
size = (uint)file.Length;
|
2014-07-21 23:26:28 +04:00
|
|
|
}
|
2018-01-04 20:41:26 +04:00
|
|
|
else if (file.AsStream.CanSeek)
|
|
|
|
{
|
|
|
|
if (size > file.Length)
|
|
|
|
size = (uint)file.Length;
|
|
|
|
}
|
2017-03-29 08:51:53 +04:00
|
|
|
uint width, height;
|
|
|
|
if (0xC == header_size)
|
|
|
|
{
|
|
|
|
width = file.ReadUInt16();
|
|
|
|
height = file.ReadUInt16();
|
|
|
|
}
|
|
|
|
else if (header_size < 40 || size-14 < header_size)
|
|
|
|
{
|
2016-10-15 09:34:46 +04:00
|
|
|
return null;
|
2017-03-29 08:51:53 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
width = file.ReadUInt32();
|
|
|
|
height = file.ReadUInt32();
|
|
|
|
}
|
2016-10-15 09:34:46 +04:00
|
|
|
file.ReadInt16();
|
|
|
|
int bpp = file.ReadInt16();
|
|
|
|
return new BmpMetaData {
|
|
|
|
Width = width,
|
|
|
|
Height = height,
|
|
|
|
OffsetX = 0,
|
|
|
|
OffsetY = 0,
|
|
|
|
BPP = bpp,
|
|
|
|
ImageLength = size,
|
2017-11-12 10:02:21 +04:00
|
|
|
ImageOffset = image_offset,
|
2016-10-15 09:34:46 +04:00
|
|
|
};
|
2014-07-21 23:26:28 +04:00
|
|
|
}
|
|
|
|
}
|
2016-08-02 16:38:42 +04:00
|
|
|
|
|
|
|
[Export(typeof(IBmpExtension))]
|
|
|
|
public class BitmapWithAlpha : IBmpExtension
|
|
|
|
{
|
2016-10-15 09:34:46 +04:00
|
|
|
public ImageData Read (IBinaryStream file, BmpMetaData info)
|
2016-08-02 16:38:42 +04:00
|
|
|
{
|
2016-10-15 09:34:46 +04:00
|
|
|
if (file.AsStream.CanSeek)
|
2016-08-02 16:38:42 +04:00
|
|
|
{
|
|
|
|
var width_x_height = info.Width * info.Height;
|
2017-11-12 10:02:21 +04:00
|
|
|
uint bmp_length = width_x_height * (uint)info.BPP/8 + info.ImageOffset;
|
2016-08-02 16:38:42 +04:00
|
|
|
if (bmp_length == info.ImageLength || bmp_length+2 == info.ImageLength)
|
|
|
|
{
|
|
|
|
if (0x20 == info.BPP)
|
|
|
|
{
|
|
|
|
return ReadBitmapBGRA (file, info);
|
|
|
|
}
|
2018-02-02 03:35:54 +04:00
|
|
|
else if (0x18 == info.BPP)
|
2016-08-02 16:38:42 +04:00
|
|
|
{
|
2018-02-02 03:35:54 +04:00
|
|
|
uint length_with_alpha = info.ImageLength + width_x_height;
|
|
|
|
if (length_with_alpha == file.Length || length_with_alpha + info.Width == file.Length)
|
|
|
|
return ReadBitmapWithAlpha (file, info);
|
2016-08-02 16:38:42 +04:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 19:45:19 +04:00
|
|
|
else if (0x20 == info.BPP && (info.ImageLength - (width_x_height * 3 + info.ImageOffset)) <= 2)
|
|
|
|
{
|
|
|
|
return ReadBitmapBGRA (file, info);
|
|
|
|
}
|
2016-08-02 16:38:42 +04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-15 09:34:46 +04:00
|
|
|
private ImageData ReadBitmapWithAlpha (IBinaryStream file, BmpMetaData info)
|
2016-08-02 16:38:42 +04:00
|
|
|
{
|
|
|
|
file.Position = info.ImageLength;
|
|
|
|
var alpha = new byte[info.Width*info.Height];
|
|
|
|
if (alpha.Length != file.Read (alpha, 0, alpha.Length))
|
|
|
|
throw new EndOfStreamException();
|
|
|
|
|
2017-11-12 10:02:21 +04:00
|
|
|
file.Position = info.ImageOffset;
|
2016-08-02 16:38:42 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-10-15 09:34:46 +04:00
|
|
|
private ImageData ReadBitmapBGRA (IBinaryStream file, BmpMetaData info)
|
2016-08-02 16:38:42 +04:00
|
|
|
{
|
2017-11-12 10:02:21 +04:00
|
|
|
file.Position = info.ImageOffset;
|
2016-08-02 16:38:42 +04:00
|
|
|
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;
|
2023-09-07 12:32:52 +04:00
|
|
|
/* // sometimes alpha channel is inverted
|
|
|
|
for (int x = 3; x < stride; x += 4)
|
|
|
|
{
|
|
|
|
pixels[dst+x] ^= 0xFF;
|
|
|
|
has_alpha = has_alpha || pixels[dst+x] != 0;
|
|
|
|
}
|
|
|
|
*/
|
2016-08-02 16:38:42 +04:00
|
|
|
}
|
|
|
|
PixelFormat format = has_alpha ? PixelFormats.Bgra32 : PixelFormats.Bgr32;
|
|
|
|
return ImageData.Create (info, format, null, pixels, stride);
|
|
|
|
}
|
|
|
|
}
|
2014-07-21 23:26:28 +04:00
|
|
|
}
|