2007-10-07 22:21:47 +00:00
|
|
|
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Text;
|
|
|
|
using System.IO;
|
|
|
|
using CodeImp.DoomBuilder.Map;
|
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
|
|
|
using System.Drawing;
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
2007-10-08 14:29:31 +00:00
|
|
|
using System.Drawing.Imaging;
|
2007-10-07 22:21:47 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.IO
|
|
|
|
{
|
2008-01-02 21:49:43 +00:00
|
|
|
internal unsafe class DoomPictureReader : IImageReader
|
2007-10-07 22:21:47 +00:00
|
|
|
{
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// Palette to use
|
|
|
|
private Playpal palette;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public DoomPictureReader(Playpal palette)
|
|
|
|
{
|
|
|
|
// Initialize
|
|
|
|
this.palette = palette;
|
2007-10-26 13:17:20 +00:00
|
|
|
|
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
2007-10-07 22:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
2007-10-26 13:17:20 +00:00
|
|
|
// This validates the data as doom picture
|
|
|
|
public bool Validate(Stream stream)
|
|
|
|
{
|
|
|
|
BinaryReader reader = new BinaryReader(stream);
|
|
|
|
int width, height;
|
|
|
|
int dataoffset;
|
|
|
|
int datalength;
|
|
|
|
int columnaddr;
|
|
|
|
|
|
|
|
// Initialize
|
|
|
|
dataoffset = (int)stream.Position;
|
|
|
|
datalength = (int)stream.Length - (int)stream.Position;
|
|
|
|
|
|
|
|
// Need at least 4 bytes
|
|
|
|
if(datalength < 4) return false;
|
|
|
|
|
|
|
|
// Read size and offset
|
|
|
|
width = reader.ReadInt16();
|
|
|
|
height = reader.ReadInt16();
|
|
|
|
reader.ReadInt16();
|
|
|
|
reader.ReadInt16();
|
|
|
|
|
|
|
|
// Valid width and height?
|
|
|
|
if((width <= 0) || (height <= 0)) return false;
|
|
|
|
|
|
|
|
// Go for all columns
|
|
|
|
for(int x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
// Get column address
|
|
|
|
columnaddr = reader.ReadInt32();
|
|
|
|
|
|
|
|
// Check if address is outside valid range
|
|
|
|
if((columnaddr < (8 + width * 4)) || (columnaddr >= datalength)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-10-07 22:21:47 +00:00
|
|
|
// This creates a Bitmap from the given data
|
2007-10-08 14:29:31 +00:00
|
|
|
// Returns null on failure
|
2007-10-07 22:21:47 +00:00
|
|
|
public Bitmap ReadAsBitmap(Stream stream)
|
|
|
|
{
|
2007-10-08 14:29:31 +00:00
|
|
|
BitmapData bitmapdata;
|
2007-10-27 13:59:24 +00:00
|
|
|
PixelColorBlock pixeldata;
|
2007-10-08 14:29:31 +00:00
|
|
|
PixelColor* targetdata;
|
|
|
|
int width, height, x, y;
|
|
|
|
Bitmap bmp;
|
|
|
|
|
|
|
|
// Read pixel data
|
|
|
|
pixeldata = ReadAsPixelData(stream, out width, out height, out x, out y);
|
|
|
|
if(pixeldata != null)
|
|
|
|
{
|
|
|
|
// Create bitmap and lock pixels
|
|
|
|
bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
|
|
|
|
bitmapdata = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
|
|
|
targetdata = (PixelColor*)bitmapdata.Scan0.ToPointer();
|
|
|
|
|
|
|
|
// Copy the pixels
|
2007-10-27 13:59:24 +00:00
|
|
|
General.CopyMemory((void*)targetdata, (void*)pixeldata.Pointer, new UIntPtr((uint)(width * height * sizeof(PixelColor))));
|
2007-10-08 14:29:31 +00:00
|
|
|
|
|
|
|
// Done
|
|
|
|
bmp.UnlockBits(bitmapdata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Failed loading picture
|
2007-10-26 13:17:20 +00:00
|
|
|
bmp = null;
|
2007-10-08 14:29:31 +00:00
|
|
|
}
|
2007-10-26 13:17:20 +00:00
|
|
|
|
|
|
|
// Return result
|
|
|
|
return bmp;
|
2007-10-07 22:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This draws the picture to the given pixel color data
|
2007-10-08 14:29:31 +00:00
|
|
|
// Throws exception on failure
|
2007-10-07 22:21:47 +00:00
|
|
|
public void DrawToPixelData(Stream stream, PixelColor* target, int targetwidth, int targetheight, int x, int y)
|
|
|
|
{
|
2007-10-27 13:59:24 +00:00
|
|
|
PixelColorBlock pixeldata;
|
2007-10-08 14:29:31 +00:00
|
|
|
int width, height, ox, oy, tx, ty;
|
|
|
|
|
|
|
|
// Read pixel data
|
|
|
|
pixeldata = ReadAsPixelData(stream, out width, out height, out ox, out oy);
|
|
|
|
if(pixeldata != null)
|
|
|
|
{
|
|
|
|
// Go for all source pixels
|
|
|
|
// We don't care about the original image offset, so reuse ox/oy
|
|
|
|
for(ox = 0; ox < width; ox++)
|
|
|
|
{
|
|
|
|
for(oy = 0; oy < height; oy++)
|
|
|
|
{
|
|
|
|
// Copy this pixel?
|
2007-10-27 13:59:24 +00:00
|
|
|
if(pixeldata.Pointer[oy * width + ox].a > 0.5f)
|
2007-10-08 14:29:31 +00:00
|
|
|
{
|
|
|
|
// Calculate target pixel and copy when within bounds
|
|
|
|
tx = x + ox;
|
|
|
|
ty = y + oy;
|
|
|
|
if((tx >= 0) && (tx < targetwidth) && (ty >= 0) && (ty < targetheight))
|
2007-10-27 13:59:24 +00:00
|
|
|
target[ty * targetwidth + tx] = pixeldata.Pointer[oy * width + ox];
|
2007-10-08 14:29:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 22:21:47 +00:00
|
|
|
}
|
2007-10-08 14:29:31 +00:00
|
|
|
|
2007-10-07 22:21:47 +00:00
|
|
|
// This creates pixel color data from the given data
|
2007-10-08 14:29:31 +00:00
|
|
|
// Returns null on failure
|
2007-10-27 13:59:24 +00:00
|
|
|
private PixelColorBlock ReadAsPixelData(Stream stream, out int width, out int height, out int offsetx, out int offsety)
|
2007-10-07 22:21:47 +00:00
|
|
|
{
|
|
|
|
BinaryReader reader = new BinaryReader(stream);
|
2007-10-27 13:59:24 +00:00
|
|
|
PixelColorBlock pixeldata = null;
|
2007-10-07 22:21:47 +00:00
|
|
|
int y, count, p;
|
2007-10-08 14:29:31 +00:00
|
|
|
int[] columns;
|
|
|
|
int dataoffset;
|
|
|
|
|
2007-10-07 22:21:47 +00:00
|
|
|
// Initialize
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
offsetx = 0;
|
|
|
|
offsety = 0;
|
2007-10-08 14:29:31 +00:00
|
|
|
dataoffset = (int)stream.Position;
|
|
|
|
|
2007-10-07 22:21:47 +00:00
|
|
|
// Need at least 4 bytes
|
|
|
|
if((stream.Length - stream.Position) < 4) return null;
|
|
|
|
|
2007-10-08 14:29:31 +00:00
|
|
|
#if !DEBUG
|
2007-10-07 22:21:47 +00:00
|
|
|
try
|
|
|
|
{
|
2007-10-08 14:29:31 +00:00
|
|
|
#endif
|
2007-10-26 13:17:20 +00:00
|
|
|
|
|
|
|
// Read size and offset
|
|
|
|
width = reader.ReadInt16();
|
|
|
|
height = reader.ReadInt16();
|
|
|
|
offsetx = reader.ReadInt16();
|
|
|
|
offsety = reader.ReadInt16();
|
|
|
|
|
|
|
|
// Valid width and height?
|
|
|
|
if((width <= 0) || (height <= 0)) return null;
|
|
|
|
|
|
|
|
// Read the column addresses
|
|
|
|
columns = new int[width];
|
|
|
|
for(int x = 0; x < width; x++) columns[x] = reader.ReadInt32();
|
|
|
|
|
|
|
|
// Allocate memory
|
2007-10-27 13:59:24 +00:00
|
|
|
pixeldata = new PixelColorBlock(width, height);
|
|
|
|
pixeldata.Clear();
|
2007-10-26 13:17:20 +00:00
|
|
|
|
|
|
|
// Go for all columns
|
|
|
|
for(int x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
// Seek to column start
|
|
|
|
stream.Seek(dataoffset + columns[x], SeekOrigin.Begin);
|
2007-10-07 22:21:47 +00:00
|
|
|
|
2007-10-26 13:17:20 +00:00
|
|
|
// Read first post start
|
|
|
|
y = reader.ReadByte();
|
2007-10-07 22:21:47 +00:00
|
|
|
|
2007-10-26 13:17:20 +00:00
|
|
|
// Continue while not end of column reached
|
|
|
|
while(y < 255)
|
|
|
|
{
|
|
|
|
// Read number of pixels in post
|
|
|
|
count = reader.ReadByte();
|
2007-10-07 22:21:47 +00:00
|
|
|
|
2007-10-26 13:17:20 +00:00
|
|
|
// Skip unused pixel
|
|
|
|
stream.Seek(1, SeekOrigin.Current);
|
2007-10-07 22:21:47 +00:00
|
|
|
|
2007-10-26 13:17:20 +00:00
|
|
|
// Draw post
|
|
|
|
for(int yo = 0; yo < count; yo++)
|
|
|
|
{
|
|
|
|
// Read pixel color index
|
|
|
|
p = reader.ReadByte();
|
2007-10-07 22:21:47 +00:00
|
|
|
|
2007-10-26 13:17:20 +00:00
|
|
|
// Draw pixel
|
2007-10-27 13:59:24 +00:00
|
|
|
pixeldata.Pointer[(y + yo) * width + x] = palette[p];
|
2007-10-07 22:21:47 +00:00
|
|
|
}
|
2007-10-26 13:17:20 +00:00
|
|
|
|
|
|
|
// Skip unused pixel
|
|
|
|
stream.Seek(1, SeekOrigin.Current);
|
|
|
|
|
|
|
|
// Read next post start
|
|
|
|
y = reader.ReadByte();
|
2007-10-07 22:21:47 +00:00
|
|
|
}
|
2007-10-26 13:17:20 +00:00
|
|
|
}
|
2007-10-07 22:21:47 +00:00
|
|
|
|
2007-10-26 13:17:20 +00:00
|
|
|
// Return pointer
|
|
|
|
return pixeldata;
|
|
|
|
|
2007-10-08 14:29:31 +00:00
|
|
|
#if !DEBUG
|
2007-10-07 22:21:47 +00:00
|
|
|
}
|
|
|
|
catch(Exception)
|
|
|
|
{
|
|
|
|
// Return nothing
|
|
|
|
return null;
|
|
|
|
}
|
2007-10-08 14:29:31 +00:00
|
|
|
#endif
|
2007-10-07 22:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|