Skip to content

Commit a03fc48

Browse files
committed
Updated
data separation mechanism and image conversion codes have been changed.
1 parent 16281c6 commit a03fc48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+226
-198
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

.vs/Portable Viewer/v17/.suo

3.5 KB
Binary file not shown.

Portable Viewer/Form1.Designer.cs

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Portable Viewer/Form1.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public Form1() {
1616
InitializeComponent();
1717
}
1818

19-
private void Form1_Load(object sender, EventArgs e) {
20-
progressbar.Visible = false;
19+
private void Form1_Load(object sender, EventArgs e) {
2120

2221
this.AllowDrop = true;
2322
this.DragEnter += new DragEventHandler(Form1_DragEnter);
@@ -76,23 +75,13 @@ private void Open(string path) {
7675
default:
7776
throw new Exception("Not supported file format.");
7877
}
79-
80-
progressbar.Visible = true;
81-
pm.Progress += (object sender, int progress) => {
82-
progressbar.BeginInvoke((MethodInvoker)delegate() {
83-
progressbar.Value = progress;
84-
});
85-
};
78+
8679

8780
Thread t = new Thread(() => {
88-
pm.Parse();
81+
pm.Load();
8982
picturebox.BeginInvoke((MethodInvoker)delegate() {
90-
picturebox.Image = pm.Image;
91-
progressbar.Visible = false;
92-
if(pm.comment != null)
93-
this.Text = pm.comment + " - " + appName;
94-
else
95-
this.Text = System.IO.Path.GetFileName(path) + " - " + appName;
83+
picturebox.Image = pm.Image;
84+
this.Text = System.IO.Path.GetFileName(path) + " - " + appName;
9685
});
9786
});
9887

Portable Viewer/PBM.cs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,43 @@
22
using System.Collections.Generic;
33
using System.Text;
44
using System.Drawing;
5+
using System.Drawing.Imaging;
56

67
namespace Portable_Viewer {
78
public class PBM : PM {
89

910
public PBM(string path) : base(path) { }
1011

11-
override public void Parse() {
12-
magic = ReadToEndOfLine();
12+
override public void Load() {
13+
magic = parser.ReadString();
1314
if(magic != "P1" && magic != "P4") throw new Exception("Not supported PBM format. Only P1 and P4 are supported.");
14-
if(buffer[cursor] == '#') comment = ReadToEndOfLine();
1515

16-
string[] size = ReadToEndOfLine().Split(' ');
17-
width = int.Parse(size[0]);
18-
height = int.Parse(size[1]);
19-
16+
width = parser.ReadInt();
17+
height = parser.ReadInt();
18+
2019
switch (magic) {
2120
case "P1":
22-
Parse_P1();
21+
values = parser.ReadBytesByInts(width * height );
2322
break;
2423
case "P4":
25-
Parse_P4();
24+
values = parser.ReadBytesByBits(width * height );
2625
break;
27-
}
28-
}
26+
}
27+
28+
Image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
2929

30-
void Parse_P1() {
31-
Image = new Bitmap(width, height);
32-
int value = 0;
33-
string[] values;
34-
for (int y = 0; y < height; y++) {
35-
values = ReadToEndOfLine().Split(' ');
36-
for (int x = 0; x < width; x++) {
37-
value = int.Parse(values[x]) == 0 ? 255 : 0;
38-
Image.SetPixel(x, y, Color.FromArgb(value, value, value));
39-
}
40-
OnProgressChanged((int)(100.0 * y / height));
30+
BitmapData bitmapData = Image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, Image.PixelFormat);
31+
IntPtr ptr = bitmapData.Scan0;
32+
byte value = 0;
33+
for (int i = 0; i < values.Length; i++) {
34+
value = values[i] == 0 ? (byte)255 : (byte)0;
35+
System.Runtime.InteropServices.Marshal.WriteByte(ptr, i * 3 + 0, value);
36+
System.Runtime.InteropServices.Marshal.WriteByte(ptr, i * 3 + 1, value);
37+
System.Runtime.InteropServices.Marshal.WriteByte(ptr, i * 3 + 2, value);
4138
}
42-
}
4339

44-
void Parse_P4() {
45-
Image = new Bitmap(width, height);
46-
int value = 0;
47-
for (int y = 0; y < height; y++) {
48-
for (int x = 0; x < width; x++) {
49-
value = (buffer[cursor] & (1 << (7 - x % 8))) == 0 ? 255 : 0;
50-
Image.SetPixel(x, y, Color.FromArgb(value, value, value));
51-
if(x % 8 == 7) cursor++;
52-
}
53-
OnProgressChanged((int)(100.0 * y / height));
54-
}
55-
}
40+
Image.UnlockBits(bitmapData);
41+
42+
}
5643
}
5744
}

Portable Viewer/PGM.cs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,46 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Drawing;
4+
using System.Drawing.Imaging;
45
using System.Text;
56

67
namespace Portable_Viewer {
78
public class PGM : PM {
89

910
public PGM(string path) : base(path) { }
10-
11-
override public void Parse() {
12-
magic = ReadToEndOfLine();
11+
12+
override public void Load() {
13+
magic = parser.ReadString();
1314
if(magic != "P2" && magic != "P5") throw new Exception("Not supported PGM format. Only P2 and P5 are supported.");
14-
if(buffer[cursor] == '#') comment = ReadToEndOfLine();
1515

16-
string[] size = ReadToEndOfLine().Split(' ');
17-
width = int.Parse(size[0]);
18-
height = int.Parse(size[1]);
16+
width = parser.ReadInt();
17+
height = parser.ReadInt();
18+
maxval = parser.ReadInt();
1919

20-
maxval = int.Parse(ReadToEndOfLine());
21-
2220
switch (magic) {
2321
case "P2":
24-
Parse_P2();
22+
values = parser.ReadBytesByInts(width * height);
2523
break;
2624
case "P5":
27-
Parse_P5();
25+
values = parser.ReadBytes(width * height);
2826
break;
2927
}
30-
}
28+
29+
if(maxval != 255) Normalize();
30+
31+
Image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
3132

32-
void Parse_P2() {
33-
Image = new Bitmap(width, height);
34-
int value = 0;
35-
string[] values;
36-
for (int y = 0; y < height; y++) {
37-
values = ReadToEndOfLine().Split(' ');
38-
for (int x = 0; x < width; x++) {
39-
value = (int)(int.Parse(values[x]) * 255.0 / maxval);
40-
Image.SetPixel(x, y, Color.FromArgb(value, value, value));
41-
}
42-
OnProgressChanged((int)(100.0 * y / height));
43-
}
44-
}
33+
BitmapData bitmapData = Image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, Image.PixelFormat);
34+
IntPtr ptr = bitmapData.Scan0;
4535

46-
void Parse_P5() {
47-
Image = new Bitmap(width, height);
48-
int value = 0;
49-
for (int y = 0; y < height; y++) {
50-
for (int x = 0; x < width; x++) {
51-
value = (int)(buffer[cursor++] * 255.0 / maxval);
52-
Image.SetPixel(x, y, Color.FromArgb(value, value, value));
53-
}
54-
OnProgressChanged((int)(100.0 * y / height));
36+
for (int i = 0; i < values.Length; i++) {
37+
System.Runtime.InteropServices.Marshal.WriteByte(ptr, i * 3, values[i]);
38+
System.Runtime.InteropServices.Marshal.WriteByte(ptr, i * 3 + 1, values[i]);
39+
System.Runtime.InteropServices.Marshal.WriteByte(ptr, i * 3 + 2, values[i]);
5540
}
56-
}
41+
42+
Image.UnlockBits(bitmapData);
43+
44+
}
5745
}
5846
}

Portable Viewer/PM.cs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,28 @@
66
namespace Portable_Viewer {
77
public class PM {
88
public byte[] buffer;
9-
public int cursor = 0;
9+
public Parser parser;
1010

11-
public string magic;
12-
public string comment;
11+
public string magic;
1312
public int width;
1413
public int height;
1514
public int maxval;
15+
public byte[] values;
1616

17-
public Bitmap Image;
18-
19-
public delegate void ProgressChangedEventHandler(object sender, int progress);
20-
public event ProgressChangedEventHandler Progress;
17+
public Bitmap Image;
2118

2219
public PM(string path) {
2320
buffer = System.IO.File.ReadAllBytes(path);
21+
parser = new Parser(buffer);
2422
}
23+
24+
public virtual void Load() { }
25+
2526

26-
public string ReadToEndOfLine() {
27-
int nextEOLPos = cursor;
28-
while (true) {
29-
if (buffer[nextEOLPos] == '\n') {
30-
int nextCursor = nextEOLPos + 1;
31-
if(buffer[nextEOLPos - 1] == '\r')
32-
nextEOLPos--;
33-
34-
string result = Encoding.ASCII.GetString(buffer, cursor, nextEOLPos - cursor);
35-
cursor = nextCursor;
36-
return result;
37-
}
38-
nextEOLPos++;
39-
}
40-
}
41-
42-
public virtual void Parse() { }
43-
44-
protected virtual void OnProgressChanged(int progress) {
45-
if (Progress != null) {
46-
Progress(this, progress);
27+
28+
public void Normalize(){
29+
for (int i = 0; i < values.Length; i++) {
30+
values[i] = (byte)(values[i] * 255.0 / maxval);
4731
}
4832
}
4933
}

Portable Viewer/PPM.cs

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,47 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Drawing;
4+
using System.Drawing.Imaging;
45
using System.Text;
56

67
namespace Portable_Viewer {
78
public class PPM : PM {
89

910
public PPM(string path) : base(path) { }
11+
12+
override public void Load() {
13+
magic = parser.ReadString();
14+
if(magic != "P3" && magic != "P6") throw new Exception("Not supported PPM format. Only P3 and P6 are supported.");
1015

11-
override public void Parse() {
12-
magic = ReadToEndOfLine();
13-
if (magic != "P3" && magic != "P6") throw new Exception("Not supported PPM format. Only P3 and P6 are supported.");
14-
if (buffer[cursor] == '#') comment = ReadToEndOfLine();
15-
16-
string[] size = ReadToEndOfLine().Split(' ');
17-
width = int.Parse(size[0]);
18-
height = int.Parse(size[1]);
19-
20-
maxval = int.Parse(ReadToEndOfLine());
16+
width = parser.ReadInt();
17+
height = parser.ReadInt();
18+
maxval = parser.ReadInt();
2119

2220
switch (magic) {
2321
case "P3":
24-
Parse_P3();
22+
values = parser.ReadBytesByInts(width * height * 3);
2523
break;
2624
case "P6":
27-
Parse_P6();
25+
values = parser.ReadBytes(width * height * 3);
2826
break;
2927
}
30-
}
28+
29+
if(maxval != 255) Normalize();
30+
31+
Image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
3132

32-
void Parse_P3() {
33-
Image = new Bitmap(width, height);
34-
int r = 0, g = 0, b = 0;
35-
string[] values;
36-
for (int y = 0; y < height; y++) {
37-
values = ReadToEndOfLine().Split(' ');
38-
for (int x = 0; x < width; x++) {
39-
r = (int)(int.Parse(values[x * 3]) * 255.0 / maxval);
40-
g = (int)(int.Parse(values[x * 3 + 1]) * 255.0 / maxval);
41-
b = (int)(int.Parse(values[x * 3 + 2]) * 255.0 / maxval);
42-
Image.SetPixel(x, y, Color.FromArgb(r, g, b));
43-
}
44-
OnProgressChanged((int)(100.0 * y / height));
45-
}
46-
}
33+
BitmapData bitmapData = Image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, Image.PixelFormat);
34+
IntPtr ptr = bitmapData.Scan0;
4735

48-
void Parse_P6() {
49-
Image = new Bitmap(width, height);
50-
int r = 0, g = 0, b = 0;
51-
for (int y = 0; y < height; y++) {
52-
for (int x = 0; x < width; x++) {
53-
r = (int)(buffer[cursor++] * 255.0 / maxval);
54-
g = (int)(buffer[cursor++] * 255.0 / maxval);
55-
b = (int)(buffer[cursor++] * 255.0 / maxval);
56-
Image.SetPixel(x, y, Color.FromArgb(r, g, b));
57-
}
58-
OnProgressChanged((int)(100.0 * y / height));
36+
for (int i = 0; i < values.Length / 3; i++) {
37+
System.Runtime.InteropServices.Marshal.WriteInt32(
38+
ptr, i * 4,
39+
(255 << 24) | (values[i * 3 + 0] << 16) | (values[i * 3 + 1] << 8) | (values[i * 3 + 2] << 0)
40+
);
5941
}
60-
}
42+
43+
Image.UnlockBits(bitmapData);
44+
45+
}
6146
}
6247
}

0 commit comments

Comments
 (0)