forked from 0xC0000054/pdn-photoshop-brush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryReverseWriter.cs
163 lines (143 loc) · 4.13 KB
/
BinaryReverseWriter.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
/////////////////////////////////////////////////////////////////////////////////
//
// ABR FileType Plugin for Paint.NET
//
// This software is provided under the MIT License:
// Copyright (c) 2012-2017 Nicholas Hayes
//
// See LICENSE.txt for complete licensing and attribution information.
//
/////////////////////////////////////////////////////////////////////////////////
// Portions of this file has been adapted from:
/////////////////////////////////////////////////////////////////////////////////
//
// Photoshop PSD FileType Plugin for Paint.NET
// http://psdplugin.codeplex.com/
//
// This software is provided under the MIT License:
// Copyright (c) 2006-2007 Frank Blumenberg
// Copyright (c) 2010-2011 Tao Yue
//
// Portions of this file are provided under the BSD 3-clause License:
// Copyright (c) 2006, Jonas Beckeman
//
// See LICENSE.txt for complete licensing and attribution information.
//
/////////////////////////////////////////////////////////////////////////////////
using System.Drawing;
using System.IO;
using System.Text;
namespace AbrFileTypePlugin
{
internal sealed class BinaryReverseWriter : BinaryWriter
{
private bool leaveOpen;
public BinaryReverseWriter(Stream stream, bool leaveOpen) : base(stream)
{
this.leaveOpen = leaveOpen;
}
protected override void Dispose(bool disposing)
{
if (!this.leaveOpen)
{
base.Dispose(disposing);
}
}
public override void Write(char value)
{
unsafe
{
SwapBytes((byte*)&value, 2);
}
base.Write(value);
}
public override void Write(short value)
{
unsafe
{
SwapBytes((byte*)&value, 2);
}
base.Write(value);
}
public override void Write(int value)
{
unsafe
{
SwapBytes((byte*)&value, 4);
}
base.Write(value);
}
public override void Write(long value)
{
unsafe
{
SwapBytes((byte*)&value, 8);
}
base.Write(value);
}
public override void Write(ushort value)
{
unsafe
{
SwapBytes((byte*)&value, 2);
}
base.Write(value);
}
public override void Write(uint value)
{
unsafe
{
SwapBytes((byte*)&value, 4);
}
base.Write(value);
}
public override void Write(ulong value)
{
unsafe
{
SwapBytes((byte*)&value, 8);
}
base.Write(value);
}
public override void Write(double value)
{
unsafe
{
SwapBytes((byte*)&value, 8);
}
base.Write(value);
}
//////////////////////////////////////////////////////////////////
public void WriteInt16Rectangle(Rectangle rect)
{
Write((short)rect.Top);
Write((short)rect.Left);
Write((short)rect.Bottom);
Write((short)rect.Right);
}
public void WriteInt32Rectangle(Rectangle rect)
{
Write(rect.Top);
Write(rect.Left);
Write(rect.Bottom);
Write(rect.Right);
}
public void WriteUnicodeString(string value)
{
Write(checked(value.Length + 1));
Write(Encoding.BigEndianUnicode.GetBytes(value));
// The string is always null-terminated.
Write((ushort)0);
}
//////////////////////////////////////////////////////////////////
private static unsafe void SwapBytes(byte* ptr, int nLength)
{
for (long i = 0; i < nLength / 2; ++i)
{
byte t = *(ptr + i);
*(ptr + i) = *(ptr + nLength - i - 1);
*(ptr + nLength - i - 1) = t;
}
}
}
}