Advertisement
Chronos_Ouroboros

C# Modding Stuff

Jun 4th, 2015
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.55 KB | None | 0 0
  1. /* ------------------------------------ */
  2. /* -------------- GFX.cs -------------- */
  3. /* ------------------------------------ */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9.  
  10. namespace ModdingUtils {
  11.     namespace GFX {
  12.         public class Palette256Colours {
  13.             Color [] colours = new Color [256];
  14.  
  15.             /// <summary>
  16.             ///
  17.             /// </summary>
  18.             /// <param name="newColours">The array of colours to set the palette to</param>
  19.             public Palette256Colours (Color [] newColours) {
  20.                 for (int i = 0; i < 256; i++) {
  21.                     if (i > newColours.Length - 1)
  22.                         colours [i] = Color.Black;
  23.                     else
  24.                         colours [i] = Color.FromArgb (255, newColours [i].R, newColours [i].G, newColours [i].B);
  25.                 }
  26.             }
  27.  
  28.             public Color this [int index] {
  29.                 get {
  30.                     return colours [index];
  31.                 }
  32.                 set {
  33.                     colours [index] = value;
  34.                 }
  35.             }
  36.         }
  37.  
  38.         public class IMG256Colours {
  39.             private int _width;
  40.             private int _height;
  41.             private Palette256Colours _palette;
  42.  
  43.             private byte [] _colours;
  44.            
  45.  
  46.             /// <summary>
  47.             /// Gets the image's width
  48.             /// </summary>
  49.             public int width {
  50.                 get {
  51.                     return _width;
  52.                 }
  53.             }
  54.  
  55.             /// <summary>
  56.             /// Gets the image's height
  57.             /// </summary>
  58.             public int height {
  59.                 get {
  60.                     return _height;
  61.                 }
  62.             }
  63.  
  64.             /// <summary>
  65.             /// Gets or sets the image's palette
  66.             /// </summary>
  67.             public Palette256Colours palette {
  68.                 get {
  69.                     return _palette;
  70.                 } set {
  71.                     _palette = value;
  72.                 }
  73.             }
  74.  
  75.             public IMG256Colours (int newWidth, int newHeight, Palette256Colours newPalette) {
  76.                 _width = newWidth;
  77.                 _height = newHeight;
  78.                 _palette = newPalette;
  79.  
  80.                 _colours = new byte [newWidth * newHeight];
  81.             }
  82.  
  83.             ~IMG256Colours () {
  84.                 _width = 0;
  85.                 _height = 0;
  86.             }
  87.  
  88.             public byte this [int index] {
  89.                 get {
  90.                     return _colours [index];
  91.                 } set {
  92.                     _colours [index] = value;
  93.                 }
  94.             }
  95.  
  96.             public Bitmap ToBitmap () {
  97.                 Bitmap bitmap = new Bitmap (_width, _height);
  98.  
  99.                 for (int i = 0; i < _width * _height; i++)
  100.                     bitmap.SetPixel (i % _width, i / _width, _palette [_colours [i]]);
  101.  
  102.                     return bitmap;
  103.             }
  104.         }
  105.     }
  106. }
  107.  
  108. /* ------------------------------------ */
  109. /* ------------- Doom.cs -------------- */
  110. /* ------------------------------------ */
  111. using ModdingUtils.GFX;
  112. using System;
  113. using System.Collections.Generic;
  114. using System.Drawing;
  115. using System.Drawing.Imaging;
  116. using System.IO;
  117. using System.Linq;
  118. using System.Text;
  119. using System.Threading.Tasks;
  120.  
  121. namespace ModdingUtils {
  122.     /// <summary>
  123.     /// Doom related utils
  124.     /// </summary>
  125.     namespace Doom {
  126.         /// <summary>
  127.         /// Image related
  128.         /// </summary>
  129.         namespace D_Imaging {
  130.             public static class DoomGFX {
  131.                 public const byte doomGFXHeaderSize = 4 * 2;
  132.  
  133.                 /// <summary>
  134.                 /// Creates pixel data from the specified memoryStream
  135.                 /// </summary>
  136.                 /// <param name="stream">The stream to read the image from</param>
  137.                 /// <returns></returns>
  138.                 public IMG256Colours DoomGFXToPixelData (MemoryStream stream) {
  139.                     ushort width, height;
  140.                     short left, top;
  141.                     IMG256Colours image;
  142.                     //List<Pixel> pixels = new List<Pixel> ();
  143.                     byte [] header = new byte [doomGFXHeaderSize];
  144.                     byte [] buffer = new byte [5 * 1024];
  145.                     stream.Read (header, 0, doomGFXHeaderSize);
  146.                     width = BitConverter.ToUInt16 (header, 0 * 2);
  147.                     height = BitConverter.ToUInt16 (header, 1 * 2);
  148.                     left = BitConverter.ToInt16 (header, 2 * 2);
  149.                     top = BitConverter.ToInt16 (header, 3 * 2);
  150.  
  151.                     if (width <= 0 || height <= 0)
  152.                         return null;
  153.  
  154.                     image = new IMG256Colours (width, height, new Palette256Colours (new Color [1]));
  155.  
  156.                     uint [] columns = new uint [width];
  157.  
  158.                     for (int x = 0; x < width; x++) {
  159.                         stream.Read (buffer, 0, 4);
  160.                         columns [x] = BitConverter.ToUInt32 (buffer, 0);
  161.                     }
  162.  
  163.                     byte topDelta = 0;
  164.                     short prevDelta = -1;
  165.                     byte pixelCount = 0;
  166.                     for (int x = 0; x < width; x++) {
  167.                         stream.Seek (columns [x], SeekOrigin.Begin); // Go to the start of the column
  168.  
  169.                         topDelta = (byte) stream.ReadByte ();
  170.                         prevDelta = topDelta;
  171.  
  172.                         while (topDelta < 255) {
  173.                             pixelCount = (byte) stream.ReadByte (); // Read the pixelCount
  174.                             stream.Seek (1, SeekOrigin.Current); // Skip unused byte
  175.  
  176.                             for (int y = 0; y < pixelCount; y++) {
  177.                                 //pixels.Add (new Pixel ((short) x, (short) (prevDelta + y), (byte) stream.ReadByte ()));
  178.  
  179.                             }
  180.  
  181.                             stream.Seek (1, SeekOrigin.Current); // Skip unused byte
  182.                             topDelta = (byte) stream.ReadByte ();
  183.                             if (topDelta < prevDelta || (height > 255 && topDelta == prevDelta)) // Tall patches support
  184.                                 prevDelta += topDelta;
  185.                             else
  186.                                 prevDelta = topDelta;
  187.                         }
  188.                     }
  189.  
  190.                     stream.Close ();
  191.  
  192.                     return image;
  193.                 }
  194.             }
  195.         }
  196.  
  197.         /// <summary>
  198.         /// WAD Lump
  199.         /// </summary>
  200.         public class Lump {
  201.             public Lump (string newName, int newOffset, int newSize) {
  202.                 name = newName;
  203.                 offset = newOffset;
  204.                 size = newSize;
  205.             }
  206.  
  207.             /// <summary>
  208.             /// The lump's name
  209.             /// </summary>
  210.             public string name;
  211.  
  212.             /// <summary>
  213.             /// The lump's offset
  214.             /// </summary>
  215.             public int offset;
  216.  
  217.             /// <summary>
  218.             /// The lump's size
  219.             /// </summary>
  220.             public int size;
  221.         }
  222.  
  223.         public static enum WADType {
  224.             /// <summary>
  225.             /// File is invalid or does not exist.
  226.             /// </summary>
  227.             InvalidFile,
  228.             /// <summary>
  229.             /// File is not a WAD.
  230.             /// </summary>
  231.             NotWAD,
  232.             /// <summary>
  233.             /// File is a normal WAD. (little-endian)
  234.             /// </summary>
  235.             PCWAD,
  236.             /// <summary>
  237.             /// File is a Jaguar WAD. (big-endian)
  238.             /// </summary>
  239.             JagWAD
  240.         }
  241.  
  242.         /// <summary>
  243.         /// WAD
  244.         /// </summary>
  245.         public class WAD {
  246.             private WADType _type;
  247.             private Stream _stream;
  248.  
  249.             public WAD (Stream stream, WADType type) {
  250.                 _stream = stream;
  251.                 _type = type;
  252.             }
  253.  
  254.             public const byte headerSize = 12;
  255.  
  256.             /// <summary>
  257.             /// The lumps in the WAD
  258.             /// </summary>
  259.             private List <Lump> lumps = new List <Lump> ();
  260.  
  261.             public static WADType IsWAD (string filename) {
  262.                 if (!File.Exists (filename))
  263.                     return WADType.InvalidFile;
  264.  
  265.                 using (FileStream stream = new FileStream (filename, FileMode.Open)) {
  266.                     string identification;
  267.                     int numLumps, dirOffset;
  268.  
  269.                     byte [] header = new byte [headerSize];
  270.                     stream.Read (header, 0, headerSize);
  271.  
  272.                     identification = Encoding.ASCII.GetString (header, 0 * 4, 4);
  273.  
  274.                     if (!(identification.Equals ("IWAD", StringComparison.CurrentCultureIgnoreCase) ||
  275.                         identification.Equals ("PWAD", StringComparison.CurrentCultureIgnoreCase)))
  276.                         return WADType.NotWAD;
  277.  
  278.  
  279.                 }
  280.  
  281.                 return WADType.PCWAD;
  282.             }
  283.  
  284.             /// <summary>
  285.             /// Reads a lump from the WAD into a MemoryStream
  286.             /// </summary>
  287.             /// <param name="lump">The lump to read</param>
  288.             /// <returns>Returns a MemoryStream containing the specified lump</returns>
  289.             public MemoryStream ReadLumpToMemory (Lump lump) {
  290.                 using (MemoryStream outputStream = new MemoryStream ()) {
  291.                     using (_stream) {
  292.                         string identification;
  293.                         int numLumps, dirOffset;
  294.  
  295.                         byte [] header = new byte [headerSize];
  296.                         byte [] buffer = new byte [5 * 1024];
  297.                         _stream.Read (header, 0, headerSize);
  298.  
  299.                         identification = Encoding.ASCII.GetString (header, 0 * 4, 4);
  300.                         numLumps = BitConverter.ToInt32 (header, 1 * 4);
  301.                         dirOffset = BitConverter.ToInt32 (header, 2 * 4);
  302.  
  303.                         _stream.Seek (dirOffset, SeekOrigin.Begin);
  304.                     }
  305.  
  306.                     return outputStream;
  307.                 }
  308.             }
  309.         }
  310.  
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement