-
Notifications
You must be signed in to change notification settings - Fork 2
/
PPM.cs
144 lines (135 loc) · 5.08 KB
/
PPM.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
/*
* Vision.NET 2.1 Computer Vision Library
* Copyright (C) 2009 Matthew Johnson
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
using System.Text;
using System.IO;
namespace VisionNET
{
/// <summary>
/// Utility class with routines for reading the simple PPM image file format.
/// </summary>
public static class PPM
{
private static char _currentChar;
private static FileStream _stream;
private static void init(FileStream stream)
{
_stream = stream;
_currentChar = (char)_stream.ReadByte();
}
private static string readString()
{
StringBuilder sb = new StringBuilder();
while (!char.IsWhiteSpace(_currentChar))
{
sb.Append(_currentChar);
_currentChar = (char)_stream.ReadByte();
}
return sb.ToString();
}
private static int readInt()
{
return int.Parse(readString());
}
private static void clearWhitespace()
{
while (char.IsWhiteSpace(_currentChar))
_currentChar = (char)_stream.ReadByte();
}
/// <summary>
/// Returns an RGB image version of the PPM file stored at <paramref name="filename"/>.
/// </summary>
/// <param name="filename">A PPM image file</param>
/// <returns>An RGB image</returns>
public static RGBImage Read(string filename)
{
FileStream stream = File.OpenRead(filename);
init(stream);
if (readString() != "P6")
return null;
clearWhitespace();
int columns = readInt();
clearWhitespace();
int rows = readInt();
clearWhitespace();
int max = readInt();
try
{
if (max < 256)
return readColorByteImage(columns, rows, max, stream);
else return readColorShortImage(columns, rows, max, stream);
}
finally
{
stream.Close();
}
}
private static unsafe RGBImage readColorByteImage(int columns, int rows, int max, FileStream stream)
{
RGBImage image = new RGBImage(rows, columns);
double multiplier = 255.0 / max;
fixed(byte* src=image.RawArray){
byte* ptr = src;
byte[] scanline = new byte[columns*3];
for (int r = 0; r < rows; r++)
{
int count = 0;
while (count < scanline.Length)
count += stream.Read(scanline, count, scanline.Length - count);
for (int c = 0; c < columns; c++)
{
byte R = scanline[c * 3];
byte G = scanline[c * 3 + 1];
byte B = scanline[c * 3 + 2];
*ptr++ = (byte)(R * multiplier);
*ptr++ = (byte)(G * multiplier);
*ptr++ = (byte)(B * multiplier);
}
}
}
return image;
}
private static unsafe RGBImage readColorShortImage(int columns, int rows, int max, FileStream stream)
{
RGBImage image = new RGBImage(rows, columns);
double multiplier = 255.0 / max;
fixed(byte* src=image.RawArray){
byte* ptr = src;
byte[] scanline = new byte[columns * 6];
for (int r = 0; r < rows; r++)
{
int count = 0;
while (count < scanline.Length)
count += stream.Read(scanline, count, scanline.Length - count);
for (int c = 0; c < columns; c++)
{
int shortR = scanline[c * 6];
shortR = shortR << 8 + scanline[c * 6 + 1];
int shortG = scanline[c * 6 + 2];
shortG = shortG << 8 + scanline[c * 6 + 3];
int shortB = scanline[c * 6 + 4];
shortB = shortB << 8 + scanline[c * 6 + 5];
*ptr++ = (byte)(multiplier * shortR);
*ptr++ = (byte)(multiplier * shortG);
*ptr++ = (byte)(multiplier * shortB);
}
}
}
return image;
}
}
}