Created
June 5, 2015 20:12
-
-
Save bencz/45b47364a0c91c2fd68e to your computer and use it in GitHub Desktop.
bmp generator ( test )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace bmpGenerator | |
{ | |
public class ColorX | |
{ | |
public byte R { get; set; } | |
public byte G { get; set; } | |
public byte B { get; set; } | |
public byte A { get; set; } | |
public ColorX() | |
{ | |
R = 255; | |
G = 255; | |
B = 255; | |
A = 255; | |
} | |
public ColorX(byte r, byte g, byte b, byte a = 255) | |
{ | |
R = r; | |
G = g; | |
B = b; | |
A = a; | |
} | |
} | |
public class PointX : ColorX | |
{ | |
public int X { get; set; } | |
public int Y { get; set; } | |
public PointX() | |
{ | |
} | |
public PointX(int x, int y) | |
{ | |
X = x; | |
Y = y; | |
} | |
public PointX(int x, int y, ColorX color) | |
{ | |
X = x; | |
Y = y; | |
R = color.R; | |
G = color.G; | |
B = color.B; | |
A = color.A; | |
} | |
public PointX(int x, int y, byte r, byte g, byte b, byte a = 255) | |
: base(r, g, b, a) | |
{ | |
X = x; | |
Y = y; | |
} | |
} | |
public class DrawingX | |
{ | |
private List<PointX> points; | |
private int Width { get; set; } | |
private int Height { get; set; } | |
public DrawingX(int width, int height) | |
{ | |
points = new List<PointX>(width * height); | |
for (int i = 0; i < width * height; i++) | |
points.Add(new PointX()); | |
Width = width; | |
Height = height; | |
} | |
public int SetPixel(int x, int y) | |
{ | |
var pos = (x - Width) + ((y - Height) * Width) * -1; | |
pos = pos < 0 ? pos * -1 : pos; | |
points[pos] = new PointX(x, y); | |
return pos; | |
} | |
public void SetPixel(int x, int y, ColorX color) | |
{ | |
var pos = SetPixel(x, y); | |
points[pos].R = color.R; | |
points[pos].G = color.G; | |
points[pos].B = color.B; | |
} | |
private void XorSwap(ref int x1, ref int x2) | |
{ | |
x1 ^= x2; | |
x2 ^= x1; | |
x1 ^= x2; | |
} | |
public void DrawLine(int x0, int y0, int x1, int y1, ColorX color) | |
{ | |
var p1 = new PointX(x0, y0); | |
var p2 = new PointX(x1, y1); | |
DrawLine(p1, p2, color); | |
} | |
public void DrawLine(PointX p1, PointX p2, ColorX color) | |
{ | |
int x0 = p1.X; | |
int x1 = p2.X; | |
int y0 = p1.Y; | |
int y1 = p2.Y; | |
int xDelta = (x1 - x0); | |
int yDelta = (y1 - y0); | |
if (xDelta == 0) | |
{ | |
if (y0 > y1) | |
XorSwap(ref y0, ref y1); | |
for (int y = y0; y <= y1; y++) | |
SetPixel(x0, y, color); | |
} | |
else if (yDelta == 0) | |
{ | |
if (x0 > x1) | |
XorSwap(ref x0, ref x1); | |
for (int x = x0; x <= x1; x++) | |
SetPixel(x, y0, color); | |
} | |
else | |
{ | |
SetPixel(x0, y0, color); | |
int xStep = (xDelta < 0 ? -1 : 1); | |
int yStep = (yDelta < 0 ? -1 : 1); | |
xDelta = Math.Abs(xDelta) / 2; | |
yDelta = Math.Abs(yDelta) / 2; | |
if (xDelta >= yDelta) | |
{ | |
int error = yDelta - 2 * xDelta; | |
while (x0 != x1) | |
{ | |
if (error >= 0 && (error == 1 || xStep > 0)) | |
{ | |
error -= xDelta; | |
y0 += yStep; | |
} | |
error += yDelta; | |
x0 += xStep; | |
SetPixel(x0, y0, color); | |
} | |
} | |
else | |
{ | |
int error = xDelta - 2 * yDelta; | |
while (y0 != y1) | |
{ | |
if (error >= 0 && (error == 1 || yStep > 0)) | |
{ | |
error -= yDelta; | |
x0 += xStep; | |
} | |
error += xDelta; | |
y0 += yStep; | |
SetPixel(x0, y0, color); | |
} | |
} | |
} | |
} | |
public void DrawRectangle(PointX point, int width, int height, ColorX color) | |
{ | |
int x = point.X; | |
int y = point.Y; | |
DrawLine(x, y, x + width, y, color); | |
DrawLine(x + width, y, x + width, y + height, color); | |
DrawLine(x + width, y + height, x, y + height, color); | |
DrawLine(x, y + height, x, y, color); | |
} | |
public void FillRectangle(PointX point, int width, int height, ColorX color) | |
{ | |
for (int i = 0; i < width; i++) | |
DrawLine(point.X + i, point.Y, point.X + i, point.Y + height, color); | |
} | |
public void SaveFile(string fileName) | |
{ | |
BinaryWriter bw = new BinaryWriter(File.OpenWrite(fileName)); | |
bw.Write('B'); | |
bw.Write('M'); | |
bw.Write(Width * Height * 3 + 14 + 40); | |
bw.Write((short)0); | |
bw.Write((short)0); | |
bw.Write(14 + 40); | |
bw.Write(40); | |
bw.Write(Width); | |
bw.Write(Height); | |
bw.Write((short)1); | |
bw.Write((short)24); | |
for (int i = 0; i < 6; i++) | |
bw.Write((int)0); | |
for (int i = 0; i < points.Count; i++) | |
{ | |
var x = points[i]; | |
bw.Write(x.B); | |
bw.Write(x.G); | |
bw.Write(x.R); | |
} | |
bw.Close(); | |
} | |
} | |
class Program | |
{ | |
static void TestRetangles(DrawingX drw) | |
{ | |
var p1 = new PointX(0, 0); | |
var cyan = new ColorX(0, 255, 255); | |
drw.DrawRectangle(p1, 199, 399, cyan); | |
var yellow = new ColorX(255, 255, 0); | |
drw.FillRectangle(new PointX(20, 20), 40, 60, yellow); | |
var black = new ColorX(0, 0, 0); | |
drw.DrawRectangle(new PointX(20, 20), 40, 60, black); | |
int xStep = 4; | |
int yStep = 4; | |
int nRectangles = 10; | |
byte dRed = 0; | |
byte dGreen = 255; | |
byte dBlue = 0; | |
int stepValue = (255 / (nRectangles - 1)); | |
int dRedStep = 0; | |
int dGreenStep = -stepValue; | |
int dBlueStep = stepValue; | |
for (int i = 0; i < nRectangles; i++) | |
{ | |
var theColor = new ColorX(dRed, dGreen,dBlue); | |
var point = new PointX(70 + xStep * i, 50 - yStep * i); | |
drw.DrawRectangle(point, 80, 60, theColor); | |
dRed += (byte)dRedStep; | |
dGreen += (byte)dGreenStep; | |
dBlue += (byte)dBlueStep; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
DrawingX drw = new DrawingX(200, 400); | |
TestRetangles(drw); | |
drw.SaveFile("out.bmp"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment