summaryrefslogtreecommitdiff
path: root/BitmapChar.cs
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2005-05-14 21:59:59 +0000
committerIan C <ianc@noddybox.co.uk>2005-05-14 21:59:59 +0000
commit5fb32592ed08ca0f45e3bf1dce77c637441f716e (patch)
tree1da9f0cc9220c7686f427cf04156ec2e7c2bef1e /BitmapChar.cs
parent10ea05f56ef0e70f3d74b2ea9102cc4537ec8e05 (diff)
Initial Checkin
Diffstat (limited to 'BitmapChar.cs')
-rw-r--r--BitmapChar.cs257
1 files changed, 257 insertions, 0 deletions
diff --git a/BitmapChar.cs b/BitmapChar.cs
new file mode 100644
index 0000000..910a4b2
--- /dev/null
+++ b/BitmapChar.cs
@@ -0,0 +1,257 @@
+// BitmapFontEd
+// Copyright (C) 2005 Ian Cowburn
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// $Id$
+//
+using System;
+using System.Drawing;
+using System.IO;
+
+namespace BitmapFontEd
+{
+ /// <summary>
+ /// Description of BitmapChar.
+ /// </summary>
+ public class BitmapChar
+ {
+ public BitmapChar(uint width, uint height)
+ {
+ m_width=width;
+ m_height=height;
+ m_data=new Color[m_width,m_height];
+ }
+
+ public BitmapChar() : this(8,8)
+ {
+ }
+
+ public BitmapChar(BitmapChar old)
+ {
+ m_width=old.m_width;
+ m_height=old.m_height;
+ m_data=new Color[m_width,m_height];
+
+ for(int x=0;x<m_width;x++)
+ for(int y=0;y<m_height;y++)
+ m_data[x,y]=old.m_data[x,y];
+ }
+
+ public uint Width
+ {
+ get {return m_width;}
+ }
+
+ public uint Height
+ {
+ get {return m_height;}
+ }
+
+ public void Resize(uint width, uint height)
+ {
+ Color[,] data=new Color[width,height];
+ uint mx=Math.Min(width,m_width);
+ uint my=Math.Min(height,m_height);
+
+ for(int x=0;x<mx;x++)
+ for(int y=0;y<my;y++)
+ data[x,y]=m_data[x,y];
+
+ m_width=width;
+ m_height=height;
+ m_data=data;
+ }
+
+ public Color this [uint x, uint y]
+ {
+ set {m_data[x,y]=value;}
+ get {return m_data[x,y];}
+ }
+
+ public void Clear(Color c)
+ {
+ for(int y=0;y<m_height;y++)
+ for(int x=0;x<m_width;x++)
+ m_data[x,y]=c;
+ }
+
+ public void MirrorHorizontal()
+ {
+ Color[,] d=new Color[m_width,m_height];
+
+ for(int y=0;y<m_height;y++)
+ for(int x=0;x<m_width;x++)
+ d[x,y]=m_data[m_width-x-1,y];
+
+ m_data=d;
+ }
+
+ public void MirrorVertical()
+ {
+ Color[,] d=new Color[m_width,m_height];
+
+ for(int y=0;y<m_height;y++)
+ for(int x=0;x<m_width;x++)
+ d[x,y]=m_data[x,m_height-y-1];
+
+ m_data=d;
+ }
+
+ public void Scroll(int dx, int dy)
+ {
+ Color[,] d=new Color[m_width,m_height];
+
+ for(int y=0;y<m_height;y++)
+ for(int x=0;x<m_width;x++)
+ d[Mod(x+dx,m_width),Mod(y+dy,m_height)]=m_data[x,y];
+
+ m_data=d;
+ }
+
+ public void RotateRight()
+ {
+ Color[,] d=new Color[m_width,m_height];
+ uint min=Math.Min(m_width,m_height);
+
+ for(int y=0;y<m_height;y++)
+ for(int x=0;x<m_width;x++)
+ {
+ long nx=min-y-1;
+ long ny=x;
+
+ if (nx>=0 && nx<m_width && ny>=0 && ny<m_height)
+ {
+ d[nx,ny]=m_data[x,y];
+ }
+ }
+
+ m_data=d;
+ }
+
+ public void RotateLeft()
+ {
+ Color[,] d=new Color[m_width,m_height];
+ uint min=Math.Min(m_width,m_height);
+
+ for(int y=0;y<m_height;y++)
+ for(int x=0;x<m_width;x++)
+ {
+ long nx=y;
+ long ny=min-x-1;
+
+ if (nx>=0 && nx<m_width && ny>=0 && ny<m_height)
+ {
+ d[nx,ny]=m_data[x,y];
+ }
+ }
+
+ m_data=d;
+ }
+
+ public void Output(Stream stream)
+ {
+ WriteUint(stream,m_width);
+ WriteUint(stream,m_height);
+
+ for(uint y=0;y<m_height;y++)
+ {
+ for(uint x=0;x<m_width;x++)
+ {
+ WriteInt(stream,m_data[x,y].ToArgb());
+ }
+ }
+ }
+
+ public static BitmapChar Input(Stream stream)
+ {
+ uint width=ReadUint(stream);
+ uint height=ReadUint(stream);
+
+ BitmapChar s=new BitmapChar(width,height);
+
+ for(uint y=0;y<height;y++)
+ {
+ for(uint x=0;x<width;x++)
+ {
+ s[x,y]=Color.FromArgb(ReadInt(stream));
+ }
+ }
+
+ return s;
+ }
+
+ // ------------------------------------------------
+ // PRIVATE
+ //
+ private uint m_width;
+ private uint m_height;
+ private Color[,] m_data;
+
+ private int Mod(int v, uint max)
+ {
+ while(v<0)
+ v+=(int)max;
+
+ v=v%(int)max;
+
+ return v;
+ }
+
+ private static uint ReadUint(Stream s)
+ {
+ uint l=0;
+
+ for(int f=0;f<4;f++)
+ {
+ int b=s.ReadByte();
+ l|=(uint)(b<<(f*8));
+ }
+
+ return l;
+ }
+
+ private static int ReadInt(Stream s)
+ {
+ int l=0;
+
+ for(int f=0;f<4;f++)
+ {
+ int b=s.ReadByte();
+ l|=b<<(f*8);
+ }
+
+ return l;
+ }
+
+ private static void WriteUint(Stream s, uint l)
+ {
+ for(uint f=0;f<4;f++)
+ {
+ s.WriteByte((byte)(l&0xff));
+ l=l>>8;
+ }
+ }
+
+ private static void WriteInt(Stream s, int l)
+ {
+ for(uint f=0;f<4;f++)
+ {
+ s.WriteByte((byte)(l&0xff));
+ l=l>>8;
+ }
+ }
+ }
+}