GARbro-mirror/ArcFormats/NekoSDK/ImageALP.cs

111 lines
4.5 KiB
C#
Raw Normal View History

2016-08-02 20:40:31 +08:00
//! \file ImageALP.cs
//! \date Tue Aug 02 15:35:56 2016
//! \brief NekoSDK BMP alpha-channel extension.
//
// 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;
2018-09-02 13:34:26 +08:00
using System.Windows.Media.Imaging;
2016-08-02 20:40:31 +08:00
namespace GameRes.Formats.NekoSDK
{
[Export(typeof(IBmpExtension))]
public class AlpBitmap : IBmpExtension
{
2016-10-16 13:22:53 +08:00
public ImageData Read (IBinaryStream file, BmpMetaData info)
2016-08-02 20:40:31 +08:00
{
2018-09-02 13:34:26 +08:00
if (!file.CanSeek)
2016-08-02 20:40:31 +08:00
return null;
var alp_name = Path.ChangeExtension (info.FileName, "alp");
if (alp_name.Equals (info.FileName, StringComparison.InvariantCultureIgnoreCase)
|| !VFS.FileExists (alp_name))
return null;
2018-09-02 13:34:26 +08:00
int alp_stride = ((int)info.Width + 3) & -4;
int alpha_size = alp_stride * (int)info.Height;
2016-08-02 20:40:31 +08:00
var alpha = new byte[alpha_size];
using (var alp = VFS.OpenStream (alp_name))
{
2018-09-02 13:34:26 +08:00
alpha_size = alp.Read (alpha, 0, alpha.Length);
if (alp.ReadByte() != -1)
2016-08-02 20:40:31 +08:00
return null;
2018-09-02 13:34:26 +08:00
if (alpha_size != alpha.Length)
{
if (alp_stride == (int)info.Width)
return null;
alp_stride = (int)info.Width;
if (alpha_size != alp_stride * (int)info.Height)
return null;
}
2016-08-02 20:40:31 +08:00
}
2018-09-02 13:34:26 +08:00
if (info.BPP != 24 && info.BPP != 32)
return ReadBmp (file, info, alpha, alp_stride);
file.Position = info.ImageOffset;
2016-08-02 20:40:31 +08:00
int dst_stride = (int)info.Width * 4;
int gap = -((int)info.Width * info.BPP / 8) & 3;
2016-08-02 20:40:31 +08:00
var pixels = new byte[(int)info.Height * dst_stride];
int src_pixel_size = info.BPP / 8;
int dst = (int)(info.Height-1) * dst_stride;
for (int y = (int)info.Height-1; y >= 0; --y)
{
2018-09-02 13:34:26 +08:00
int a_src = alp_stride * y;
2016-08-02 20:40:31 +08:00
for (int x = 0; x < dst_stride; x += 4)
{
file.Read (pixels, dst+x, src_pixel_size);
pixels[dst+x+3] = alpha[a_src++];
}
if (gap != 0)
file.Seek (gap, SeekOrigin.Current);
2016-08-02 20:40:31 +08:00
dst -= dst_stride;
}
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels, dst_stride);
}
2018-09-02 13:34:26 +08:00
public ImageData ReadBmp (IBinaryStream file, BmpMetaData info, byte[] alpha, int alp_stride)
{
var decoder = new BmpBitmapDecoder (file.AsStream,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
BitmapSource bitmap = decoder.Frames[0];
bitmap = new FormatConvertedBitmap (bitmap, PixelFormats.Bgr32, null, 0);
int dst_stride = (int)info.Width * 4;
var pixels = new byte[(int)info.Height * dst_stride];
bitmap.CopyPixels (pixels, dst_stride, 0);
int dst = 0;
int src = 0;
for (int y = (int)info.Height; y > 0; --y)
{
int a_src = src;
for (int x = 3; x < dst_stride; x += 4)
{
pixels[dst+x] = alpha[a_src++];
}
dst += dst_stride;
src += alp_stride;
}
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels, dst_stride);
}
2016-08-02 20:40:31 +08:00
}
}