-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTileImageFileType.cs
More file actions
356 lines (293 loc) · 13 KB
/
TileImageFileType.cs
File metadata and controls
356 lines (293 loc) · 13 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
////////////////////////////////////////////////////////////////////////////////////
//
// This file is part of pdn-image-tiling-filetype, a FileType plugin for Paint.NET
// that splits an image into tiles and saves them into a zip file.
//
// Copyright (c) 2013, 2017, 2020, 2021 Nicholas Hayes
//
// This file is licensed under the MIT License.
// See LICENSE.txt for complete licensing and attribution information.
//
////////////////////////////////////////////////////////////////////////////////////
// Portions of this file has been adapted from:
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
// See src/Resources/Files/License.txt for full licensing and attribution //
// details. //
// . //
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Ionic.Zip;
using PaintDotNet;
using PaintDotNet.IndirectUI;
using PaintDotNet.PropertySystem;
namespace TileImageFileType
{
[PluginSupportInfo(typeof(PluginSupportInfo))]
public sealed class TileImageFileType : PropertyBasedFileType, IFileTypeFactory
{
private enum SavableBitDepths
{
Rgba32, // 2^24 colors, plus a full 8-bit alpha channel
Rgb24, // 2^24 colors
Rgb8, // 256 colors
Rgba8 // 255 colors + 1 transparent
}
internal static string StaticName
{
get
{
return "Image Tiles";
}
}
public TileImageFileType() : base(StaticName, FileTypeFlags.SupportsSaving | FileTypeFlags.SavesWithProgress, new string[] { ".zip" })
{
}
protected override bool IsReflexive(PropertyBasedSaveConfigToken token)
{
return true;
}
private enum PropertyNames
{
TileHeight,
TileWidth,
SquareTiles
}
public override PropertyCollection OnCreateSavePropertyCollection()
{
const int DefaultTileSize = 256;
const int MinTileSize = 8;
const int MaxTileSize = 2048;
List<Property> props = new List<Property> {
new Int32Property(PropertyNames.TileHeight, DefaultTileSize, MinTileSize, MaxTileSize),
new Int32Property(PropertyNames.TileWidth, DefaultTileSize, MinTileSize, MaxTileSize),
new BooleanProperty(PropertyNames.SquareTiles, true)
};
List<PropertyCollectionRule> rules = new List<PropertyCollectionRule> {
new LinkValuesBasedOnBooleanRule<int, Int32Property>(
new object[] { PropertyNames.TileHeight, PropertyNames.TileWidth },
PropertyNames.SquareTiles,
false)
};
return new PropertyCollection(props, rules);
}
public override ControlInfo OnCreateSaveConfigUI(PropertyCollection props)
{
ControlInfo info = CreateDefaultSaveConfigUI(props);
info.SetPropertyControlValue(PropertyNames.TileHeight, ControlInfoPropertyNames.DisplayName, "Tile height");
info.SetPropertyControlValue(PropertyNames.TileWidth, ControlInfoPropertyNames.DisplayName, "Tile width");
info.SetPropertyControlValue(PropertyNames.SquareTiles, ControlInfoPropertyNames.Description, "Square tiles");
info.SetPropertyControlValue(PropertyNames.SquareTiles, ControlInfoPropertyNames.DisplayName, string.Empty);
return info;
}
protected override Document OnLoad(Stream input)
{
throw new NotImplementedException();
}
private unsafe void SquishSurfaceTo24Bpp(Surface surface)
{
byte* dst = (byte*)surface.GetRowAddress(0);
int byteWidth = surface.Width * 3;
int stride24bpp = (byteWidth + 3) & ~3;
int delta = stride24bpp - byteWidth;
for (int y = 0; y < surface.Height; ++y)
{
ColorBgra* src = surface.GetRowAddress(y);
ColorBgra* srcEnd = src + surface.Width;
while (src < srcEnd)
{
dst[0] = src->B;
dst[1] = src->G;
dst[2] = src->R;
++src;
dst += 3;
}
dst += delta;
}
}
private unsafe Bitmap CreateAliased24BppBitmap(Surface surface)
{
int stride = surface.Width * 3;
int realStride = (stride + 3) & ~3;
return new Bitmap(surface.Width, surface.Height, realStride, PixelFormat.Format24bppRgb, new IntPtr(surface.Scan0.VoidStar));
}
private unsafe void Analyze(Surface scratchSurface, Rectangle roi, out bool allOpaque, out bool all0or255Alpha, out int uniqueColorCount)
{
allOpaque = true;
all0or255Alpha = true;
HashSet<uint> uniqueColors = new HashSet<uint>();
for (int y = roi.Top; y < roi.Bottom; ++y)
{
ColorBgra* srcPtr = scratchSurface.GetPointAddressUnchecked(roi.Left, y);
ColorBgra* endPtr = srcPtr + roi.Width;
while (srcPtr < endPtr)
{
if (srcPtr->A != 255)
{
allOpaque = false;
}
if (srcPtr->A > 0 && srcPtr->A < 255)
{
all0or255Alpha = false;
}
if ((srcPtr->A == 255 && uniqueColors.Count < 300) && !uniqueColors.Contains(srcPtr->Bgra))
{
uniqueColors.Add(srcPtr->Bgra);
}
++srcPtr;
}
}
uniqueColorCount = uniqueColors.Count;
}
private SavableBitDepths GetBitDepth(Surface scratchSurface, Rectangle bounds)
{
bool allOpaque;
bool all0or255Alpha;
int uniqueColorCount;
Analyze(scratchSurface, bounds, out allOpaque, out all0or255Alpha, out uniqueColorCount);
SavableBitDepths bitDepth = SavableBitDepths.Rgba32;
if (allOpaque)
{
bitDepth = SavableBitDepths.Rgb24;
if (uniqueColorCount <= 256)
{
bitDepth = SavableBitDepths.Rgb8;
}
}
else if (all0or255Alpha && uniqueColorCount < 256)
{
bitDepth = SavableBitDepths.Rgba8;
}
// if bit depth is 24 or 8, then we have to do away with the alpha channel
// for 8-bit, we must have pixels that have either 0 or 255 alpha
if (bitDepth == SavableBitDepths.Rgb8 ||
bitDepth == SavableBitDepths.Rgba8 ||
bitDepth == SavableBitDepths.Rgb24)
{
UserBlendOps.NormalBlendOp blendOp = new UserBlendOps.NormalBlendOp();
unsafe
{
for (int y = bounds.Top; y < bounds.Bottom; ++y)
{
ColorBgra* srcPtr = scratchSurface.GetPointAddressUnchecked(bounds.Left, y);
ColorBgra* endPtr = srcPtr + bounds.Width;
while (srcPtr < endPtr)
{
if (srcPtr->A < 128 && bitDepth == SavableBitDepths.Rgba8)
{
*srcPtr = ColorBgra.FromBgra(0, 0, 0, 0);
}
else
{
*srcPtr = blendOp.Apply(ColorBgra.White, *srcPtr);
}
srcPtr++;
}
}
}
}
return bitDepth;
}
private static ImageCodecInfo GetImageCodecInfo(ImageFormat format)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo icf in encoders)
{
if (icf.FormatID == format.Guid)
{
return icf;
}
}
return null;
}
protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback)
{
int width = input.Width;
int height = input.Height;
using (RenderArgs args = new RenderArgs(scratchSurface))
{
input.Render(args, true);
}
int tileHeight = token.GetProperty<Int32Property>(PropertyNames.TileHeight).Value;
int tileWidth = token.GetProperty<Int32Property>(PropertyNames.TileWidth).Value;
List<Rectangle> rects = new List<Rectangle>();
for (int y = 0; y < height; y += tileHeight)
{
for (int x = 0; x < width; x += tileWidth)
{
Rectangle bounds = new Rectangle(x, y, Math.Min(tileWidth, width - x), Math.Min(tileHeight, height - y));
rects.Add(bounds);
}
}
double progressPercentage = 0.0;
double progressDelta = (1.0 / rects.Count) * 100.0;
using (ZipOutputStream zipStream = new ZipOutputStream(output, true))
{
int count = rects.Count;
ImageCodecInfo codec = GetImageCodecInfo(ImageFormat.Png);
EncoderParameters encoderParams = new EncoderParameters(1);
for (int i = 0; i < count; i++)
{
Rectangle bounds = rects[i];
SavableBitDepths bitDepth = GetBitDepth(scratchSurface, bounds);
zipStream.PutNextEntry(string.Format("Image{0}.png", (i + 1)));
if (bitDepth == SavableBitDepths.Rgba32)
{
encoderParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 32);
using (Bitmap temp = scratchSurface.CreateAliasedBitmap(bounds, true))
{
temp.Save(zipStream, codec, encoderParams);
}
}
else if (bitDepth == SavableBitDepths.Rgb24)
{
encoderParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 24);
using (Surface surface = new Surface(bounds.Width, bounds.Height))
{
surface.CopySurface(scratchSurface, bounds); // Make a new surface so we do not clobber the stride of the scratchSurface.
SquishSurfaceTo24Bpp(surface);
using (Bitmap temp = CreateAliased24BppBitmap(surface))
{
temp.Save(zipStream, codec, encoderParams);
}
}
}
else if (bitDepth == SavableBitDepths.Rgb8)
{
encoderParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 8);
using (Surface surface = scratchSurface.CreateWindow(bounds))
{
using (Bitmap temp = Quantize(surface, 7, 256, false, null))
{
temp.Save(zipStream, codec, encoderParams);
}
}
}
else if (bitDepth == SavableBitDepths.Rgba8)
{
encoderParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 8);
using (Surface surface = scratchSurface.CreateWindow(bounds))
{
using (Bitmap temp = Quantize(surface, 7, 256, true, null))
{
temp.Save(zipStream, codec, encoderParams);
}
}
}
progressPercentage += progressDelta;
callback(this, new ProgressEventArgs(progressPercentage));
}
}
}
public FileType[] GetFileTypeInstances()
{
return new FileType[] { new TileImageFileType() };
}
}
}