-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jpeg2000Filetype.cs
243 lines (205 loc) · 5.74 KB
/
Jpeg2000Filetype.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
////////////////////////////////////////////////////////////////////////
//
// This file is part of pdn-jpeg-2000, a FileType plugin for Paint.NET
// that loads and saves JPEG 2000 images.
//
// Copyright (c) 2012-2018 Nicholas Hayes
//
// This file is licensed under the MIT License.
// See LICENSE.txt for complete licensing and attribution information.
//
////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.IO;
using Jpeg2000Filetype.Properties;
using PaintDotNet;
using PaintDotNet.IndirectUI;
using PaintDotNet.PropertySystem;
namespace Jpeg2000Filetype
{
[PluginSupportInfo(typeof(PluginSupportInfo))]
public sealed class Jpeg2000Filetype : PropertyBasedFileType, IFileTypeFactory
{
internal static string StaticName
{
get
{
return "Jpeg 2000";
}
}
public Jpeg2000Filetype() :
base(StaticName, FileTypeFlags.SupportsLoading | FileTypeFlags.SupportsSaving, new string[] {".jp2", ".j2c", ".jpg2", ".jpf"})
{
}
public FileType[] GetFileTypeInstances()
{
return new FileType[] { new Jpeg2000Filetype() };
}
protected unsafe override Document OnLoad(Stream input)
{
FileIO.ImageData image;
FileIO.CodecError result = FileIO.DecodeFile(input, out image);
if (result == FileIO.CodecError.Ok)
{
Document doc = new Document(image.width, image.height);
BitmapLayer layer = new BitmapLayer(image.width, image.height) { IsBackground = true };
Surface surf = layer.Surface;
if (!image.hasAlpha)
{
new UnaryPixelOps.SetAlphaChannelTo255().Apply(surf, surf.Bounds);
}
byte* scan0 = (byte*)image.data.ToPointer();
if (image.channels >= 3)
{
int stride = image.width * image.channels;
for (int y = 0; y < image.height; y++)
{
ColorBgra* p = surf.GetRowAddressUnchecked(y);
byte* src = scan0 + (y * stride);
for (int x = 0; x < image.width; x++)
{
p->R = src[0]; // JP2 uses RGB pixel order.
p->G = src[1];
p->B = src[2];
if (image.hasAlpha)
{
p->A = src[3];
}
p++;
src += image.channels;
}
}
}
else
{
for (int y = 0; y < image.height; y++)
{
ColorBgra* p = surf.GetRowAddressUnchecked(y);
byte* src = scan0 + (y * image.width);
for (int x = 0; x < image.width; x++)
{
p->R = p->G = p->B = src[0]; // gray scale image
if (image.hasAlpha)
{
p->A = src[1];
}
p++;
src += image.channels;
}
}
}
if (image.dpcmX > 0.0 && image.dpcmY > 0.0)
{
doc.DpuUnit = MeasurementUnit.Centimeter;
doc.DpuX = image.dpcmX;
doc.DpuY = image.dpcmY;
}
FileIO.FreeImageData(ref image);
doc.Layers.Add(layer);
return doc;
}
else
{
string error = string.Empty;
switch (result)
{
case FileIO.CodecError.InitFailed:
error = Resources.InitFailed;
break;
case FileIO.CodecError.OutOfMemory:
throw new OutOfMemoryException();
case FileIO.CodecError.UnknownFormat:
error = Resources.UnknownFormat;
break;
case FileIO.CodecError.DecodeFailure:
error = Resources.DecodeFailure;
break;
case FileIO.CodecError.TooManyComponents:
error = Resources.TooManyComponets;
break;
case FileIO.CodecError.ProfileCreation:
error = Resources.ProfileCreation;
break;
case FileIO.CodecError.ProfileConversion:
error = Resources.ProfileConversion;
break;
}
throw new FormatException(error);
}
}
private enum PropertyNames
{
Quality
}
public override PropertyCollection OnCreateSavePropertyCollection()
{
List<Property> props = new List<Property> { new Int32Property(PropertyNames.Quality, 85, 0, 100)};
return new PropertyCollection(props);
}
public override ControlInfo OnCreateSaveConfigUI(PropertyCollection props)
{
ControlInfo info = CreateDefaultSaveConfigUI(props);
info.SetPropertyControlValue(PropertyNames.Quality, ControlInfoPropertyNames.DisplayName, "Quality");
return info;
}
private static unsafe int CountChannels(Surface scratchSurface)
{
int width = scratchSurface.Width;
int height = scratchSurface.Height;
for (int y = 0; y < height; ++y)
{
ColorBgra* srcPtr = scratchSurface.GetRowAddress(y);
ColorBgra* endPtr = srcPtr + width;
while (srcPtr < endPtr)
{
if (srcPtr->A < 255)
{
return 4;
}
if (!(srcPtr->B == srcPtr->G && srcPtr->G == srcPtr->R))
{
// The image is RGB.
return 3;
}
++srcPtr;
}
}
// The image is gray scale
return 1;
}
protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
{
int quality = (int)token.GetProperty(PropertyNames.Quality).Value;
FileIO.EncodeParams parameters = new FileIO.EncodeParams();
parameters.quality = quality;
switch (input.DpuUnit)
{
case MeasurementUnit.Centimeter:
parameters.dpcmX = input.DpuX;
parameters.dpcmY = input.DpuY;
break;
case MeasurementUnit.Inch:
parameters.dpcmX = Document.DotsPerInchToDotsPerCm(input.DpuX);
parameters.dpcmY = Document.DotsPerInchToDotsPerCm(input.DpuY);
break;
case MeasurementUnit.Pixel:
parameters.dpcmX = Document.DefaultDpcm;
parameters.dpcmY = Document.DefaultDpcm;
break;
}
using (RenderArgs ra = new RenderArgs(scratchSurface))
{
input.Render(ra, true);
}
FileIO.EncodeFile(
scratchSurface.Scan0.Pointer,
scratchSurface.Width,
scratchSurface.Height,
scratchSurface.Stride,
CountChannels(scratchSurface),
parameters,
output);
}
}
}