-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImageConverter.cs
196 lines (168 loc) · 6.22 KB
/
ImageConverter.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace png2bmp32
{
/// <summary>
/// Image converter class.
/// </summary>
class ImageConverter
{
public const float InchesPerMeter = 39.3700787f;
/// <summary>
/// …
/// </summary>
/// <param name="strInputPath">…</param>
internal static void Convert(string strInputPath)
{
string strOutputPath = Path.GetDirectoryName(strInputPath) + "\\" + Path.GetFileNameWithoutExtension(strInputPath) + ".bmp";
// Load source image data and check if it is a png
byte[] inputData = File.ReadAllBytes(strInputPath);
if (!ImageTools.IsPNG(inputData)) throw new Exception(Properties.Resource.strNoPNGData);
// Get a Bitmap object from the source image data
Image imgInput;
using (var inputDataStream = new MemoryStream(inputData))
{
imgInput = Bitmap.FromStream(inputDataStream);
}
Bitmap bmpInput = new Bitmap(imgInput);
using (var output = new MemoryStream())
{
// Write BMP file and info headers
WriteBMPHeaders(output, bmpInput);
if (imgInput.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
{
// Copy image data line by line:
Convert32bppSource(output, bmpInput);
}
else
{
// Copy image data pixel by pixel:
ConvertNon32bppSource(output, bmpInput);
}
// Write generated 32bit BMP data to file:
using (FileStream file = File.OpenWrite(strOutputPath)) output.WriteTo(file);
}
}
/// <summary>
/// …
/// </summary>
/// <param name="output">…</param>
/// <param name="bmpInput">…</param>
private static void ConvertNon32bppSource(MemoryStream output, Bitmap bmpInput)
{
int nWidth = bmpInput.Width;
int nHeight = bmpInput.Height;
for (int y = nHeight - 1; y >= 0; --y)
{
for (int x = 0; x < nWidth; ++x)
{
Color c = bmpInput.GetPixel(x, y);
output.WriteByte(c.B);
output.WriteByte(c.G);
output.WriteByte(c.R);
output.WriteByte(c.A);
}
}
}
/// <summary>
/// …
/// </summary>
/// <param name="output">…</param>
/// <param name="bmpInput">…</param>
private static void Convert32bppSource(MemoryStream output, Bitmap bmpInput)
{
BitmapData bmpDataInput = null;
int nWidth = bmpInput.Width;
int nHeight = bmpInput.Height;
try
{
bmpDataInput = bmpInput.LockBits(
new Rectangle(0, 0, nWidth, nHeight),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
int nStride = bmpDataInput.Stride;
byte[] line = new byte[Math.Abs(nStride)];
if (nStride > 0) // Bottom up image
{
IntPtr ptr = new IntPtr((long)bmpDataInput.Scan0 + (nHeight - 1) * nStride);
for (int i = 0; i < nHeight; ++i)
{
Marshal.Copy(ptr, line, 0, line.Length);
output.Write(line, 0, line.Length);
ptr = new IntPtr((long)ptr - nStride);
}
}
else // Top down image
{
IntPtr ptr = bmpDataInput.Scan0;
for (int i = 0; i < nHeight; ++i)
{
Marshal.Copy(ptr, line, 0, line.Length);
output.Write(line, 0, line.Length);
ptr = new IntPtr((long)ptr + nStride);
}
}
}
finally
{
if (bmpDataInput != null) bmpInput.UnlockBits(bmpDataInput);
}
}
/// <summary>
/// …
/// </summary>
/// <param name="output">…</param>
/// <param name="bmpInput">…</param>
private static void WriteBMPHeaders(MemoryStream output, Bitmap bmpInput)
{
int nWidth = bmpInput.Width;
int nHeight = bmpInput.Height;
BitmapFileHeader hdrFile = new BitmapFileHeader();
BitmapInfoHeader hdrBmpInf = new BitmapInfoHeader();
hdrFile.Init();
hdrBmpInf.Init();
// Get some values in advance:
UInt32 uImageSize = (UInt32)(nWidth * nHeight);
UInt32 uFileSize = (UInt32)(hdrFile.bfOffBits + uImageSize);
UInt32 uXPelsPerMeter = (UInt32)Math.Round(bmpInput.HorizontalResolution * InchesPerMeter, 0);
UInt32 uYPelsPerMeter = (UInt32)Math.Round(bmpInput.VerticalResolution * InchesPerMeter, 0);
// Set file header values:
hdrFile.bfSize = uFileSize;
// Set image header values:
hdrBmpInf.biWidth = nWidth;
hdrBmpInf.biHeight = nHeight;
hdrBmpInf.biPlanes = 1;
hdrBmpInf.biBitCount = 32;
hdrBmpInf.biCompression = 0;
hdrBmpInf.biSizeImage = uImageSize;
hdrBmpInf.biXPelsPerMeter = (int)uXPelsPerMeter;
hdrBmpInf.biYPelsPerMeter = (int)uYPelsPerMeter;
hdrBmpInf.biClrUsed = hdrBmpInf.biClrImportant = 0;
// Write headers:
byte[] dataHdrFile = StructureToByteArray(hdrFile);
byte[] dataHdrBmpInf = StructureToByteArray(hdrBmpInf);
output.Write(dataHdrFile, 0, dataHdrFile.Length);
output.Write(dataHdrBmpInf, 0, dataHdrBmpInf.Length);
}
/// <summary>
/// …
/// </summary>
/// <param name="structure">…</param>
/// <returns>…</returns>
internal static byte[] StructureToByteArray(object structure)
{
int nLen = Marshal.SizeOf(structure);
byte[] data = new byte[nLen];
IntPtr pData = Marshal.AllocHGlobal(nLen);
Marshal.StructureToPtr(structure, pData, true);
Marshal.Copy(pData, data, 0, nLen);
Marshal.FreeHGlobal(pData);
return data;
}
}
}