This commit is contained in:
Paul Chote
2010-08-11 19:04:03 +12:00
parent 20553a112d
commit 96f9f33776
2 changed files with 93 additions and 95 deletions

View File

@@ -19,88 +19,88 @@ namespace OpenRA.FileFormats
{ {
public class VqaReader public class VqaReader
{ {
public readonly ushort Frames;
public readonly byte Framerate;
public readonly ushort Width;
public readonly ushort Height;
Stream stream; Stream stream;
int currentFrame;
ushort flags; ushort flags;
public readonly ushort numFrames;
public readonly byte framerate;
ushort numColors; ushort numColors;
public readonly ushort width;
public readonly ushort height;
ushort blockWidth; ushort blockWidth;
ushort blockHeight; ushort blockHeight;
byte cbParts; byte cbParts;
int2 blocks; int2 blocks;
UInt32[] frames; UInt32[] offsets;
int currentFrame;
// Stores a list of subpixels, referenced by the VPTZ chunk
byte[] cbf;
// cbf array is updated every 8 frames
List<byte> newcbfFormat80 = new List<byte>();
int cbpCount = 0;
Color[] palette; Color[] palette;
// Stores a list of subpixels, referenced by the VPTZ chunk
byte[] cbf;
byte[] cbp;
int cbChunk = 0;
int cbOffset = 0;
// Contains a listof palette indices for the current frame // Top half contains block info, bottom half contains references to cbf array
byte[] framedata; byte[] framedata;
public VqaReader( Stream stream ) public VqaReader( Stream stream )
{ {
this.stream = stream; this.stream = stream;
BinaryReader reader = new BinaryReader( stream ); BinaryReader reader = new BinaryReader( stream );
// Decode FORM chunk // Decode FORM chunk
if (new String(reader.ReadChars(4)) != "FORM") if (new String(reader.ReadChars(4)) != "FORM")
throw new InvalidDataException("Invalid vqa (invalid FORM section)"); throw new InvalidDataException("Invalid vqa (invalid FORM section)");
/*var length = */ reader.ReadUInt32(); /*var length = */ reader.ReadUInt32();
if (new String(reader.ReadChars(8)) != "WVQAVQHD") if (new String(reader.ReadChars(8)) != "WVQAVQHD")
throw new InvalidDataException("Invalid vqa (not WVQAVQHD)"); throw new InvalidDataException("Invalid vqa (not WVQAVQHD)");
/* var length = */reader.ReadUInt32(); /* var length = */reader.ReadUInt32();
var version = reader.ReadUInt16();
/*var version = */reader.ReadUInt16();
flags = reader.ReadUInt16(); flags = reader.ReadUInt16();
numFrames = reader.ReadUInt16(); Frames = reader.ReadUInt16();
width = reader.ReadUInt16(); Width = reader.ReadUInt16();
height = reader.ReadUInt16(); Height = reader.ReadUInt16();
blockWidth = reader.ReadByte(); blockWidth = reader.ReadByte();
blockHeight = reader.ReadByte(); blockHeight = reader.ReadByte();
framerate = reader.ReadByte(); Framerate = reader.ReadByte();
cbParts = reader.ReadByte(); cbParts = reader.ReadByte();
blocks = new int2(width / blockWidth, height / blockHeight); blocks = new int2(Width / blockWidth, Height / blockHeight);
numColors = reader.ReadUInt16(); numColors = reader.ReadUInt16();
/*var maxBlocks = */reader.ReadUInt16(); /*var maxBlocks = */reader.ReadUInt16();
/*var unknown1 = */reader.ReadUInt16(); /*var unknown1 = */reader.ReadUInt16();
/*var unknown2 = */reader.ReadUInt32(); /*var unknown2 = */reader.ReadUInt32();
cbf = new byte[width*height]; // Audio
/*var freq = */reader.ReadUInt16();
/*var channels = */reader.ReadByte();
/*var bits = */reader.ReadByte();
/*var unknown3 = */reader.ReadChars(14);
cbf = new byte[Width*Height];
cbp = new byte[Width*Height];
palette = new Color[numColors]; palette = new Color[numColors];
framedata = new byte[2*blocks.X*blocks.Y]; framedata = new byte[2*blocks.X*blocks.Y];
// Audio?
var freq = reader.ReadUInt16();
var channels = reader.ReadByte();
var bits = reader.ReadByte();
/*var unknown3 = */reader.ReadChars(14);
// Decode FINF chunk // Decode FINF chunk
if (new String(reader.ReadChars(4)) != "FINF") if (new String(reader.ReadChars(4)) != "FINF")
throw new InvalidDataException("Invalid vqa (invalid FINF section)"); throw new InvalidDataException("Invalid vqa (invalid FINF section)");
/*var length = */reader.ReadUInt16();
/*var offset = */reader.ReadUInt16();
/*var unknown4 = */reader.ReadUInt16(); /*var unknown4 = */reader.ReadUInt16();
// Frame offsets // Frame offsets
frames = new UInt32[numFrames]; offsets = new UInt32[Frames];
for (int i = 0; i < numFrames; i++) for (int i = 0; i < Frames; i++)
{ {
frames[i] = reader.ReadUInt32(); offsets[i] = reader.ReadUInt32();
if (frames[i] > 0x40000000) frames[i] -= 0x40000000; if (offsets[i] > 0x40000000) offsets[i] -= 0x40000000;
frames[i] <<= 1; offsets[i] <<= 1;
} }
// Load the first frame // Load the first frame
@@ -111,9 +111,9 @@ namespace OpenRA.FileFormats
public void AdvanceFrame() public void AdvanceFrame()
{ {
// Seek to the start of the frame // Seek to the start of the frame
stream.Seek(frames[currentFrame], SeekOrigin.Begin); stream.Seek(offsets[currentFrame], SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream); BinaryReader reader = new BinaryReader(stream);
var end = (currentFrame < numFrames - 1) ? frames[currentFrame+1] : stream.Length; var end = (currentFrame < Frames - 1) ? offsets[currentFrame+1] : stream.Length;
while(reader.BaseStream.Position < end) while(reader.BaseStream.Position < end)
{ {
@@ -136,37 +136,8 @@ namespace OpenRA.FileFormats
// Chunks are aligned on even bytes; advance by a byte if the next one is null // Chunks are aligned on even bytes; advance by a byte if the next one is null
if (reader.PeekChar() == 0) reader.ReadByte(); if (reader.PeekChar() == 0) reader.ReadByte();
} }
if (++currentFrame == numFrames) if (++currentFrame == Frames)
{ currentFrame = cbOffset = cbChunk = 0;
currentFrame = 0;
cbpCount = 0;
newcbfFormat80.Clear();
}
}
public void FrameData(ref Bitmap frame)
{
var bitmapData = frame.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
int* c = (int*)bitmapData.Scan0;
for (var y = 0; y < blocks.Y; y++)
for (var x = 0; x < blocks.X; x++)
{
var px = framedata[x + y*blocks.X];
var mod = framedata[x + (y + blocks.Y)*blocks.X];
for (var j = 0; j < blockHeight; j++)
for (var i = 0; i < blockWidth; i++)
{
var cbfi = (mod*256 + px)*8 + j*blockWidth + i;
byte color = (mod == 0x0f) ? px : cbf[cbfi];
*(c + ((y*blockHeight + j) * bitmapData.Stride >> 2) + x*blockWidth + i) = palette[color].ToArgb();
}
}
}
frame.UnlockBits(bitmapData);
} }
// VQA Frame // VQA Frame
@@ -181,7 +152,7 @@ namespace OpenRA.FileFormats
switch(type) switch(type)
{ {
// Full compressed frame-modifier // Full frame-modifier
case "CBFZ": case "CBFZ":
Format80.DecodeInto( reader.ReadBytes(subchunkLength), cbf ); Format80.DecodeInto( reader.ReadBytes(subchunkLength), cbf );
break; break;
@@ -189,31 +160,26 @@ namespace OpenRA.FileFormats
cbf = reader.ReadBytes(subchunkLength); cbf = reader.ReadBytes(subchunkLength);
break; break;
// Partial compressed frame-modifier // frame-modifier chunk
case "CBP0":
case "CBPZ": case "CBPZ":
// Partial buffer is full; dump and recreate // Partial buffer is full; dump and recreate
if (cbpCount == cbParts) if (cbChunk == cbParts)
{ {
Format80.DecodeInto( newcbfFormat80.ToArray(), cbf ); if (type == "CBP0")
cbpCount = 0; cbf = (byte[])cbp.Clone();
newcbfFormat80.Clear(); else
Format80.DecodeInto( cbp, cbf );
cbOffset = cbChunk = 0;
} }
var bytes = reader.ReadBytes(subchunkLength); var bytes = reader.ReadBytes(subchunkLength);
foreach (var b in bytes) newcbfFormat80.Add(b); bytes.CopyTo(cbp,cbOffset);
cbpCount++; cbOffset += subchunkLength;
break; cbChunk++;
case "CBP0":
// Partial buffer is full; dump and recreate
if (cbpCount == cbParts)
{
cbf = newcbfFormat80.ToArray();
cbpCount = 0;
newcbfFormat80.Clear();
}
var bytes2 = reader.ReadBytes(subchunkLength);
foreach (var b in bytes2) newcbfFormat80.Add(b);
cbpCount++;
break; break;
// Palette // Palette
case "CPL0": case "CPL0":
for (int i = 0; i < numColors; i++) for (int i = 0; i < numColors; i++)
@@ -236,6 +202,31 @@ namespace OpenRA.FileFormats
} }
} }
public void FrameData(ref Bitmap frame)
{
var bitmapData = frame.LockBits(new Rectangle(0, 0, Width, Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
int* c = (int*)bitmapData.Scan0;
for (var y = 0; y < blocks.Y; y++)
for (var x = 0; x < blocks.X; x++)
{
var px = framedata[x + y*blocks.X];
var mod = framedata[x + (y + blocks.Y)*blocks.X];
for (var j = 0; j < blockHeight; j++)
for (var i = 0; i < blockWidth; i++)
{
var cbfi = (mod*256 + px)*8 + j*blockWidth + i;
byte color = (mod == 0x0f) ? px : cbf[cbfi];
*(c + ((y*blockHeight + j) * bitmapData.Stride >> 2) + x*blockWidth + i) = palette[color].ToArgb();
}
}
}
frame.UnlockBits(bitmapData);
}
public byte ToColorByte(byte b) public byte ToColorByte(byte b)
{ {
return (byte)((b & 63) * 255 / 63); return (byte)((b & 63) * 255 / 63);

View File

@@ -29,17 +29,18 @@ namespace OpenRA.Widgets
public void LoadVideo(string filename) public void LoadVideo(string filename)
{ {
video = new VqaReader(FileSystem.Open(filename)); video = new VqaReader(FileSystem.Open(filename));
timestep = 1e3f/video.framerate; timestep = 1e3f/video.Framerate;
var size = OpenRA.Graphics.Util.NextPowerOf2(Math.Max(video.width, video.height)); var size = OpenRA.Graphics.Util.NextPowerOf2(Math.Max(video.Width, video.Height));
videoFrame = new Bitmap(size,size); videoFrame = new Bitmap(size,size);
video.FrameData(ref videoFrame); video.FrameData(ref videoFrame);
videoSprite = new Sprite(new Sheet(new Size(size,size)), new Rectangle( 0, 0, video.width, video.height ), TextureChannel.Alpha); videoSprite = new Sprite(new Sheet(new Size(size,size)), new Rectangle( 0, 0, video.Width, video.Height ), TextureChannel.Alpha);
videoSprite.sheet.Texture.SetData(videoFrame); videoSprite.sheet.Texture.SetData(videoFrame);
} }
int lastTime; int lastTime;
bool advanceNext = false;
public override void DrawInner(World world) public override void DrawInner(World world)
{ {
if (video == null) if (video == null)
@@ -48,10 +49,16 @@ namespace OpenRA.Widgets
int t = Environment.TickCount; int t = Environment.TickCount;
int dt = t - lastTime; int dt = t - lastTime;
if (advanceNext)
{
advanceNext = false;
video.AdvanceFrame();
}
if (dt > timestep) if (dt > timestep)
{ {
lastTime = t; lastTime = t;
video.AdvanceFrame(); advanceNext = true;
video.FrameData(ref videoFrame); video.FrameData(ref videoFrame);
videoSprite.sheet.Texture.SetData(videoFrame); videoSprite.sheet.Texture.SetData(videoFrame);
} }