Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ------------------------------------ */
- /* -------------- GFX.cs -------------- */
- /* ------------------------------------ */
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- namespace ModdingUtils {
- namespace GFX {
- public class Palette256Colours {
- Color [] colours = new Color [256];
- /// <summary>
- ///
- /// </summary>
- /// <param name="newColours">The array of colours to set the palette to</param>
- public Palette256Colours (Color [] newColours) {
- for (int i = 0; i < 256; i++) {
- if (i > newColours.Length - 1)
- colours [i] = Color.Black;
- else
- colours [i] = Color.FromArgb (255, newColours [i].R, newColours [i].G, newColours [i].B);
- }
- }
- public Color this [int index] {
- get {
- return colours [index];
- }
- set {
- colours [index] = value;
- }
- }
- }
- public class IMG256Colours {
- private int _width;
- private int _height;
- private Palette256Colours _palette;
- private byte [] _colours;
- /// <summary>
- /// Gets the image's width
- /// </summary>
- public int width {
- get {
- return _width;
- }
- }
- /// <summary>
- /// Gets the image's height
- /// </summary>
- public int height {
- get {
- return _height;
- }
- }
- /// <summary>
- /// Gets or sets the image's palette
- /// </summary>
- public Palette256Colours palette {
- get {
- return _palette;
- } set {
- _palette = value;
- }
- }
- public IMG256Colours (int newWidth, int newHeight, Palette256Colours newPalette) {
- _width = newWidth;
- _height = newHeight;
- _palette = newPalette;
- _colours = new byte [newWidth * newHeight];
- }
- ~IMG256Colours () {
- _width = 0;
- _height = 0;
- }
- public byte this [int index] {
- get {
- return _colours [index];
- } set {
- _colours [index] = value;
- }
- }
- public Bitmap ToBitmap () {
- Bitmap bitmap = new Bitmap (_width, _height);
- for (int i = 0; i < _width * _height; i++)
- bitmap.SetPixel (i % _width, i / _width, _palette [_colours [i]]);
- return bitmap;
- }
- }
- }
- }
- /* ------------------------------------ */
- /* ------------- Doom.cs -------------- */
- /* ------------------------------------ */
- using ModdingUtils.GFX;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ModdingUtils {
- /// <summary>
- /// Doom related utils
- /// </summary>
- namespace Doom {
- /// <summary>
- /// Image related
- /// </summary>
- namespace D_Imaging {
- public static class DoomGFX {
- public const byte doomGFXHeaderSize = 4 * 2;
- /// <summary>
- /// Creates pixel data from the specified memoryStream
- /// </summary>
- /// <param name="stream">The stream to read the image from</param>
- /// <returns></returns>
- public IMG256Colours DoomGFXToPixelData (MemoryStream stream) {
- ushort width, height;
- short left, top;
- IMG256Colours image;
- //List<Pixel> pixels = new List<Pixel> ();
- byte [] header = new byte [doomGFXHeaderSize];
- byte [] buffer = new byte [5 * 1024];
- stream.Read (header, 0, doomGFXHeaderSize);
- width = BitConverter.ToUInt16 (header, 0 * 2);
- height = BitConverter.ToUInt16 (header, 1 * 2);
- left = BitConverter.ToInt16 (header, 2 * 2);
- top = BitConverter.ToInt16 (header, 3 * 2);
- if (width <= 0 || height <= 0)
- return null;
- image = new IMG256Colours (width, height, new Palette256Colours (new Color [1]));
- uint [] columns = new uint [width];
- for (int x = 0; x < width; x++) {
- stream.Read (buffer, 0, 4);
- columns [x] = BitConverter.ToUInt32 (buffer, 0);
- }
- byte topDelta = 0;
- short prevDelta = -1;
- byte pixelCount = 0;
- for (int x = 0; x < width; x++) {
- stream.Seek (columns [x], SeekOrigin.Begin); // Go to the start of the column
- topDelta = (byte) stream.ReadByte ();
- prevDelta = topDelta;
- while (topDelta < 255) {
- pixelCount = (byte) stream.ReadByte (); // Read the pixelCount
- stream.Seek (1, SeekOrigin.Current); // Skip unused byte
- for (int y = 0; y < pixelCount; y++) {
- //pixels.Add (new Pixel ((short) x, (short) (prevDelta + y), (byte) stream.ReadByte ()));
- }
- stream.Seek (1, SeekOrigin.Current); // Skip unused byte
- topDelta = (byte) stream.ReadByte ();
- if (topDelta < prevDelta || (height > 255 && topDelta == prevDelta)) // Tall patches support
- prevDelta += topDelta;
- else
- prevDelta = topDelta;
- }
- }
- stream.Close ();
- return image;
- }
- }
- }
- /// <summary>
- /// WAD Lump
- /// </summary>
- public class Lump {
- public Lump (string newName, int newOffset, int newSize) {
- name = newName;
- offset = newOffset;
- size = newSize;
- }
- /// <summary>
- /// The lump's name
- /// </summary>
- public string name;
- /// <summary>
- /// The lump's offset
- /// </summary>
- public int offset;
- /// <summary>
- /// The lump's size
- /// </summary>
- public int size;
- }
- public static enum WADType {
- /// <summary>
- /// File is invalid or does not exist.
- /// </summary>
- InvalidFile,
- /// <summary>
- /// File is not a WAD.
- /// </summary>
- NotWAD,
- /// <summary>
- /// File is a normal WAD. (little-endian)
- /// </summary>
- PCWAD,
- /// <summary>
- /// File is a Jaguar WAD. (big-endian)
- /// </summary>
- JagWAD
- }
- /// <summary>
- /// WAD
- /// </summary>
- public class WAD {
- private WADType _type;
- private Stream _stream;
- public WAD (Stream stream, WADType type) {
- _stream = stream;
- _type = type;
- }
- public const byte headerSize = 12;
- /// <summary>
- /// The lumps in the WAD
- /// </summary>
- private List <Lump> lumps = new List <Lump> ();
- public static WADType IsWAD (string filename) {
- if (!File.Exists (filename))
- return WADType.InvalidFile;
- using (FileStream stream = new FileStream (filename, FileMode.Open)) {
- string identification;
- int numLumps, dirOffset;
- byte [] header = new byte [headerSize];
- stream.Read (header, 0, headerSize);
- identification = Encoding.ASCII.GetString (header, 0 * 4, 4);
- if (!(identification.Equals ("IWAD", StringComparison.CurrentCultureIgnoreCase) ||
- identification.Equals ("PWAD", StringComparison.CurrentCultureIgnoreCase)))
- return WADType.NotWAD;
- }
- return WADType.PCWAD;
- }
- /// <summary>
- /// Reads a lump from the WAD into a MemoryStream
- /// </summary>
- /// <param name="lump">The lump to read</param>
- /// <returns>Returns a MemoryStream containing the specified lump</returns>
- public MemoryStream ReadLumpToMemory (Lump lump) {
- using (MemoryStream outputStream = new MemoryStream ()) {
- using (_stream) {
- string identification;
- int numLumps, dirOffset;
- byte [] header = new byte [headerSize];
- byte [] buffer = new byte [5 * 1024];
- _stream.Read (header, 0, headerSize);
- identification = Encoding.ASCII.GetString (header, 0 * 4, 4);
- numLumps = BitConverter.ToInt32 (header, 1 * 4);
- dirOffset = BitConverter.ToInt32 (header, 2 * 4);
- _stream.Seek (dirOffset, SeekOrigin.Begin);
- }
- return outputStream;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement