-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmapImage.cs
65 lines (56 loc) · 2.14 KB
/
BitmapImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static BitMapper.ByteStuff;
namespace BitMapper
{
public class BitmapImage
{
public int PixelWidth { get; }
public int PixelHeight { get; }
private BitmapHeader _header;
private List<byte> _headerData;
private readonly Pixel[,] _imageData;
public BitmapImage(int pixelWidth, int pixelHeight)
{
PixelWidth = pixelWidth;
PixelHeight = pixelHeight;
InitHeader();
_imageData = new Pixel[PixelHeight, PixelWidth];
}
private void InitHeader()
{
_header = new BitmapHeader {reserved1 = 0, reserved2 = 0};
var infoHeader = new InfoHeader();
_header.type = Encoding.ASCII.GetBytes("BM");
_header.offset = GetSize(_header) + GetSize(infoHeader);
infoHeader.size = 40;
infoHeader.bits = 24;
infoHeader.compression = 0;
infoHeader.height = PixelHeight;
infoHeader.planes = 1;
infoHeader.width = PixelWidth;
infoHeader.imagesize = SizeOf<Pixel>() * (uint) infoHeader.height * (uint) infoHeader.width;
infoHeader.xresolution = 2835;
infoHeader.yresolution = 2835;
infoHeader.ncolours = 0;
_header.size = GetSize(_header) + SizeOf<InfoHeader>() + (SizeOf<Pixel>() * (uint) PixelWidth * (uint) PixelHeight);
_headerData = GetBytes(_header).ToList();
_headerData.AddRange(GetBytes(infoHeader));
}
public void Draw(Action<Pixel[,]> drawAction) => drawAction(_imageData);
public byte[] ToBytes()
{
var finalImage = new List<byte>();
finalImage.AddRange(_headerData);
for (var j = PixelHeight -1; j >=0 ; j--)
// for (var j = 0; j < PixelHeight; j++)
for (var i = 0; i < PixelWidth; i++)
{
finalImage.AddRange(GetBytes(_imageData[j, i]));
}
return finalImage.ToArray();
}
}
}