forked from mono/taglib-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStripImageData.cs
117 lines (87 loc) · 2.56 KB
/
StripImageData.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
using System;
using TagLib;
public class StripImageData
{
private static byte[] image_data = new byte[] {
0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00,
0x02, 0x11, 0x03, 0x11, 0x00, 0x3F, 0x00,
0x8C, 0x80, 0x07, 0xFF, 0xD9
};
public static void Main (string [] args)
{
if (args.Length != 1) {
Console.Out.WriteLine ("usage: mono StripImageData.exe [jpegfile]");
return;
}
ImageFile file = new ImageFile (args [0]);
file.Mode = File.AccessMode.Write;
long greatest_segment_position = 0;
long greatest_segment_length = 0;
// collect data segments
while (true) {
long sos = file.Find (new byte [] {0xFF, 0xDA}, file.Tell);
if (sos == -1)
break;
file.Seek (sos);
long segment_length = SkipDataSegment (file);
if (segment_length > greatest_segment_length) {
greatest_segment_length = segment_length;
greatest_segment_position = sos;
}
}
if (greatest_segment_length == 0)
{
Console.Out.WriteLine ("doesn't look like an jpeg file");
return;
}
System.Console.WriteLine ("Stripping data segment at {0}", greatest_segment_position);
file.RemoveBlock (greatest_segment_position, greatest_segment_length);
file.Seek (greatest_segment_position);
file.WriteBlock (image_data);
file.Mode = File.AccessMode.Closed;
}
private static long SkipDataSegment (ImageFile file)
{
long position = file.Tell;
// skip sos maker
if (file.ReadBlock (2).ToUInt () != 0xFFDA)
throw new Exception (String.Format ("Not a data segment at position: {0}", position));
while (true) {
if (0xFF == (byte) file.ReadBlock (1)[0]) {
byte maker = (byte) file.ReadBlock (1)[0];
if (maker != 0x00 && (maker <= 0xD0 || maker >= 0xD7))
break;
}
}
long length = file.Tell - position - 2;
System.Console.WriteLine ("Data segment of length {0} found at {1}", length, position);
return length;
}
private class ImageFile : File {
// Hacky implementation to make use of some methods defined in TagLib.File
public ImageFile (string path)
: base (new File.LocalFileAbstraction (path)) {}
public override Tag GetTag (TagLib.TagTypes type, bool create)
{
throw new System.NotImplementedException ();
}
public override Properties Properties {
get {
throw new System.NotImplementedException ();
}
}
public override void RemoveTags (TagLib.TagTypes types)
{
throw new System.NotImplementedException ();
}
public override void Save ()
{
throw new System.NotImplementedException ();
}
public override Tag Tag {
get {
throw new System.NotImplementedException ();
}
}
}
}