Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / C#

Creating your own animation format

Rate me:
Please Sign up or sign in to vote.
4.96/5 (22 votes)
6 Feb 2013CPOL14 min read 36.8K   726   33  
Learn how to create your own animation format, capable of doing basic compression.
using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Pfz.Imaging
{
	/// <summary>
	/// This class represents a 24-bit Rgb (red, green and blue) color structure.
	/// </summary>
	[StructLayout(LayoutKind.Explicit)]
	public struct Rgb24:
		IEncodablePixelFormat<Rgb24>,
		ISimplifiablePixelFormat<Rgb24>
	{
		public Rgb24(int value)
		{
			_red = (byte)(value >> 16);
			_green = (byte)(value >> 8);
			_blue = (byte)value;
		}

		/// <summary>
		/// Creates a new Rgb24 struct from the given values.
		/// </summary>
		public Rgb24(byte red, byte green, byte blue)
		{
			_red = red;
			_green = green;
			_blue = blue;
		}
	
		[FieldOffset(0)]
		private byte _red;
		/// <summary>
		/// Represents the Red value of this structure.
		/// </summary>
		public byte Red
		{
			get
			{
				return _red;
			}
		}
		
		[FieldOffset(1)]
		private byte _green;
		/// <summary>
		/// Represents the Green value of this structure.
		/// </summary>
		public byte Green
		{
			get
			{
				return _green;
			}
		}
		
		[FieldOffset(2)]
		private byte _blue;
		/// <summary>
		/// Represents the Blue value of this structure.
		/// </summary>
		public byte Blue
		{
			get
			{
				return _blue;
			}
		}

		public int ToInt32()
		{
			return 255 << 24 | (_red << 16) | (_green << 8) | _blue;
		}
		
		/// <summary>
		/// Explicit cast.
		/// </summary>
		public static explicit operator Rgb24(int argb)
		{
			return new Rgb24(argb);
		}

		/// <summary>
		/// Explicit cast.
		/// </summary>
		public static explicit operator int(Rgb24 rgb24)
		{
			return rgb24.ToInt32();
		}

		/// <summary>
		/// Compares this Rgb24 object with another one.
		/// </summary>
		public bool Equals(Rgb24 other)
		{
			return _red == other._red && _green == other._green && _blue == other._blue;
		}

		/// <summary>
		/// Compares this Rgb24 object with another object.
		/// </summary>
		public override bool Equals(object obj)
		{
			if (obj is Rgb24)
				return Equals((Rgb24)obj);

			return false;
		}

		/// <summary>
		/// Gets the HashCode of this object.
		/// </summary>
		public override int GetHashCode()
		{
			return ToInt32();
		}

		/// <summary>
		/// Compares two Rgb24 for equality.
		/// </summary>
		public static bool operator == (Rgb24 a, Rgb24 b)
		{
			return a.Equals(b);
		}

		/// <summary>
		/// Compares two Rgb24 for inequality.
		/// </summary>
		public static bool operator != (Rgb24 a, Rgb24 b)
		{
			return !a.Equals(b);
		}

		public Rgb24 EncodeAdd(Rgb24 other)
		{
			int red = _red + other._red - 127;
			int green = _green + other._green - 127;
			int blue = _blue + other._blue - 127;

			return new Rgb24((byte)red, (byte)green, (byte)blue);
		}

		public Rgb24 EncodeSubtract(Rgb24 other)
		{
			int red = _red - other._red + 127;
			int green = _green - other._green + 127;
			int blue = _blue - other._blue + 127;

			return new Rgb24((byte)red, (byte)green, (byte)blue);
		}

		public int PixelSize
		{
			get
			{
				return 3;
			}
		}

		public void Write(byte[] buffer, int offset)
		{
			buffer[offset++] = _red;
			buffer[offset++] = _green;
			buffer[offset] = _blue;
		}

		public void Write(Stream stream)
		{
			stream.WriteByte(_red);
			stream.WriteByte(_green);
			stream.WriteByte(_blue);
		}

		public Rgb24 Read(byte[] buffer, int offset)
		{
			byte red = buffer[offset++];
			byte green = buffer[offset++];
			byte blue = buffer[offset];

			return new Rgb24(red, green, blue);
		}
		public Rgb24 Read(Stream stream)
		{
			byte red = _StreamHelper._ReadByteOrThrow(stream);
			byte green = _StreamHelper._ReadByteOrThrow(stream);
			byte blue = _StreamHelper._ReadByteOrThrow(stream);

			return new Rgb24(red, green, blue);
		}

		public Rgb24 Simplify(int factor)
		{
			if (factor < 1)
				throw new ArgumentException("factor must be at least 1");

			int red = _red / factor;
			int green = _green / factor;
			int blue = _blue / factor;

			red *= factor;
			green *= factor;
			blue *= factor;

			return new Rgb24((byte)red, (byte)green, (byte)blue);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Microsoft
United States United States
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.

At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.

Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com

Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).

Comments and Discussions