SubtitleEdit/libse/XSub.cs

130 lines
4.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Drawing;
namespace Nikse.SubtitleEdit.Core
{
public class XSub
{
public TimeCode Start { get; set; }
public TimeCode End { get; set; }
public int Width { get; private set; }
public int Height { get; private set; }
2018-01-27 13:50:44 +01:00
private readonly byte[] _colorBuffer;
private readonly byte[] _rleBuffer;
public XSub(string timeCode, int width, int height, byte[] colors, byte[] rle)
{
Start = DecodeTimeCode(timeCode.Substring(0, 13));
End = DecodeTimeCode(timeCode.Substring(13, 12));
Width = width;
Height = height;
2018-01-27 13:50:44 +01:00
_colorBuffer = colors;
_rleBuffer = rle;
}
private static TimeCode DecodeTimeCode(string timeCode)
{
var parts = timeCode.Split(new[] { ':', ';', '.', ',', '-' }, StringSplitOptions.RemoveEmptyEntries);
return new TimeCode(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), int.Parse(parts[3]));
}
private static int GenerateBitmap(FastBitmap bmp, byte[] buf, List<Color> fourColors)
{
int w = bmp.Width;
int h = bmp.Height;
int nibbleOffset = 0;
2018-01-27 13:50:44 +01:00
var nibbleEnd = buf.Length * 2;
var x = 0;
var y = 0;
2018-01-27 13:50:44 +01:00
for (; ; )
{
2018-01-27 13:50:44 +01:00
if (nibbleOffset >= nibbleEnd)
2019-01-19 14:40:37 +01:00
{
return -1;
2019-01-19 14:40:37 +01:00
}
var v = GetNibble(buf, nibbleOffset++);
if (v < 0x4)
{
v = (v << 4) | GetNibble(buf, nibbleOffset++);
if (v < 0x10)
{
v = (v << 4) | GetNibble(buf, nibbleOffset++);
if (v < 0x040)
{
v = (v << 4) | GetNibble(buf, nibbleOffset++);
if (v < 4)
{
v |= (w - x) << 2;
}
}
}
}
var len = v >> 2;
2018-01-27 13:50:44 +01:00
if (len > w - x)
2019-01-19 14:40:37 +01:00
{
2018-01-27 13:50:44 +01:00
len = w - x;
2019-01-19 14:40:37 +01:00
}
var color = v & 0x03;
if (color > 0)
{
2018-01-27 13:50:44 +01:00
var c = fourColors[color];
bmp.SetPixel(x, y, c, len);
}
x += len;
if (x >= w)
{
y++;
if (y >= h)
2019-01-19 14:40:37 +01:00
{
break;
2019-01-19 14:40:37 +01:00
}
x = 0;
nibbleOffset += (nibbleOffset & 1);
}
}
return 0;
}
2018-01-27 13:50:44 +01:00
private static int GetNibble(byte[] buf, int nibbleOffset)
{
2018-01-27 13:50:44 +01:00
return (buf[nibbleOffset >> 1] >> ((1 - (nibbleOffset & 1)) << 2)) & 0xf;
}
public Bitmap GetImage(Color background, Color pattern, Color emphasis1, Color emphasis2)
{
var fourColors = new List<Color> { background, pattern, emphasis1, emphasis2 };
var bmp = new Bitmap(Width, Height);
if (fourColors[0] != Color.Transparent)
{
using (var gr = Graphics.FromImage(bmp))
{
gr.FillRectangle(new SolidBrush(fourColors[0]), new Rectangle(0, 0, bmp.Width, bmp.Height));
}
}
var fastBmp = new FastBitmap(bmp);
fastBmp.LockImage();
2018-01-27 13:50:44 +01:00
GenerateBitmap(fastBmp, _rleBuffer, fourColors);
fastBmp.UnlockImage();
return bmp;
}
private Color GetColor(int start)
{
2018-01-27 13:50:44 +01:00
return Color.FromArgb(_colorBuffer[start], _colorBuffer[start + 1], _colorBuffer[start + 2]);
}
public Bitmap GetImage()
{
return GetImage(Color.Transparent, GetColor(3), GetColor(6), GetColor(9));
}
}
}