2009-04-19 18:07:22 +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.IO;
|
|
|
|
using System.Drawing;
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.IO
|
|
|
|
{
|
|
|
|
internal unsafe class DoomPictureReader : IImageReader
|
|
|
|
{
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// Palette to use
|
2014-10-12 21:59:16 +00:00
|
|
|
private readonly Playpal palette;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public DoomPictureReader(Playpal palette)
|
|
|
|
{
|
|
|
|
// Initialize
|
|
|
|
this.palette = palette;
|
|
|
|
|
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This validates the data as doom picture
|
|
|
|
public bool Validate(Stream stream)
|
|
|
|
{
|
|
|
|
BinaryReader reader = new BinaryReader(stream);
|
2015-12-27 21:54:50 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Initialize
|
2015-12-27 21:54:50 +00:00
|
|
|
int datalength = (int)stream.Length - (int)stream.Position;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Need at least 4 bytes
|
|
|
|
if(datalength < 4) return false;
|
|
|
|
|
|
|
|
// Read size and offset
|
2015-12-27 21:54:50 +00:00
|
|
|
int width = reader.ReadInt16();
|
|
|
|
int height = reader.ReadInt16();
|
2009-04-19 18:07:22 +00:00
|
|
|
reader.ReadInt16();
|
|
|
|
reader.ReadInt16();
|
|
|
|
|
|
|
|
// Valid width and height?
|
2013-12-03 13:12:12 +00:00
|
|
|
if(width < 1 || height < 1) return false;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Go for all columns
|
|
|
|
for(int x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
// Get column address
|
2015-12-27 21:54:50 +00:00
|
|
|
int columnaddr = reader.ReadInt32();
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Check if address is outside valid range
|
|
|
|
if((columnaddr < (8 + width * 4)) || (columnaddr >= datalength)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This creates a Bitmap from the given data
|
|
|
|
// Returns null on failure
|
|
|
|
public Bitmap ReadAsBitmap(Stream stream)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
return ReadAsBitmap(stream, out x, out y);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This creates a Bitmap from the given data
|
|
|
|
// Returns null on failure
|
|
|
|
public Bitmap ReadAsBitmap(Stream stream, out int offsetx, out int offsety)
|
|
|
|
{
|
|
|
|
int width, height;
|
|
|
|
Bitmap bmp;
|
|
|
|
|
|
|
|
// Read pixel data
|
2016-10-28 21:07:33 +00:00
|
|
|
PixelColor[] pixeldata = ReadAsPixelData(stream, out width, out height, out offsetx, out offsety);
|
2009-04-19 18:07:22 +00:00
|
|
|
if(pixeldata != null)
|
|
|
|
{
|
|
|
|
// Create bitmap and lock pixels
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
|
2015-12-27 21:54:50 +00:00
|
|
|
BitmapData bitmapdata = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
|
|
|
PixelColor* targetdata = (PixelColor*)bitmapdata.Scan0.ToPointer();
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2016-10-28 21:07:33 +00:00
|
|
|
//mxd. Copy the pixels
|
|
|
|
int size = pixeldata.Length - 1;
|
|
|
|
for(PixelColor* cp = targetdata + size; cp >= targetdata; cp--)
|
|
|
|
*cp = pixeldata[size--];
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Done
|
|
|
|
bmp.UnlockBits(bitmapdata);
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
// Unable to make bitmap
|
2009-05-10 16:02:08 +00:00
|
|
|
General.ErrorLogger.Add(ErrorType.Error, "Unable to make Doom picture data. " + e.GetType().Name + ": " + e.Message);
|
2009-04-19 18:07:22 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Failed loading picture
|
|
|
|
bmp = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return result
|
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This draws the picture to the given pixel color data
|
|
|
|
// Throws exception on failure
|
|
|
|
public void DrawToPixelData(Stream stream, PixelColor* target, int targetwidth, int targetheight, int x, int y)
|
|
|
|
{
|
2015-12-27 21:54:50 +00:00
|
|
|
int width, height, ox, oy;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Read pixel data
|
2016-10-28 21:07:33 +00:00
|
|
|
PixelColor[] pixeldata = ReadAsPixelData(stream, out width, out height, out ox, out oy);
|
2009-04-19 18:07:22 +00:00
|
|
|
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?
|
2016-10-28 21:07:33 +00:00
|
|
|
if(pixeldata[oy * width + ox].a > 0.5f)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Calculate target pixel and copy when within bounds
|
2015-12-27 21:54:50 +00:00
|
|
|
int tx = x + ox;
|
|
|
|
int ty = y + oy;
|
2009-04-19 18:07:22 +00:00
|
|
|
if((tx >= 0) && (tx < targetwidth) && (ty >= 0) && (ty < targetheight))
|
2016-10-28 21:07:33 +00:00
|
|
|
target[ty * targetwidth + tx] = pixeldata[oy * width + ox];
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 21:07:33 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidDataException("Failed to read pixeldata"); //mxd. Let's throw exception on failure
|
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This creates pixel color data from the given data
|
|
|
|
// Returns null on failure
|
2016-10-28 21:07:33 +00:00
|
|
|
private PixelColor[] ReadAsPixelData(Stream stream, out int width, out int height, out int offsetx, out int offsety)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
BinaryReader reader = new BinaryReader(stream);
|
2015-12-27 21:54:50 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Initialize
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
offsetx = 0;
|
|
|
|
offsety = 0;
|
2015-12-27 21:54:50 +00:00
|
|
|
int dataoffset = (int)stream.Position;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Need at least 4 bytes
|
|
|
|
if((stream.Length - stream.Position) < 4) return null;
|
|
|
|
|
|
|
|
#if !DEBUG
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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
|
2015-12-27 21:54:50 +00:00
|
|
|
int[] columns = new int[width];
|
2009-04-19 18:07:22 +00:00
|
|
|
for(int x = 0; x < width; x++) columns[x] = reader.ReadInt32();
|
|
|
|
|
|
|
|
// Allocate memory
|
2016-10-28 21:07:33 +00:00
|
|
|
PixelColor[] pixeldata = new PixelColor[width * height];
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Go for all columns
|
|
|
|
for(int x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
// Seek to column start
|
|
|
|
stream.Seek(dataoffset + columns[x], SeekOrigin.Begin);
|
|
|
|
|
|
|
|
// Read first post start
|
2015-12-27 21:54:50 +00:00
|
|
|
int y = reader.ReadByte();
|
|
|
|
int read_y = y;
|
2010-08-01 00:07:12 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Continue while not end of column reached
|
2010-08-01 00:07:12 +00:00
|
|
|
while(read_y < 255)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Read number of pixels in post
|
2015-12-27 21:54:50 +00:00
|
|
|
int count = reader.ReadByte();
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Skip unused pixel
|
|
|
|
stream.Seek(1, SeekOrigin.Current);
|
|
|
|
|
|
|
|
// Draw post
|
|
|
|
for(int yo = 0; yo < count; yo++)
|
|
|
|
{
|
|
|
|
// Read pixel color index
|
2015-12-27 21:54:50 +00:00
|
|
|
int p = reader.ReadByte();
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2016-10-28 21:07:33 +00:00
|
|
|
//mxd. Sanity check required...
|
|
|
|
int offset = (y + yo) * width + x;
|
|
|
|
if(offset > pixeldata.Length - 1) return null;
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Draw pixel
|
2016-10-28 21:07:33 +00:00
|
|
|
pixeldata[offset] = palette[p];
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
2015-12-27 21:54:50 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Skip unused pixel
|
|
|
|
stream.Seek(1, SeekOrigin.Current);
|
|
|
|
|
|
|
|
// Read next post start
|
2010-08-01 00:07:12 +00:00
|
|
|
read_y = reader.ReadByte();
|
2016-10-28 21:07:33 +00:00
|
|
|
if(read_y < y || (height > 256 && read_y == y)) y += read_y; else y = read_y; //mxd. Fix for tall patches higher than 508 pixels
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return pointer
|
|
|
|
return pixeldata;
|
|
|
|
|
|
|
|
#if !DEBUG
|
|
|
|
}
|
|
|
|
catch(Exception)
|
|
|
|
{
|
|
|
|
// Return nothing
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|