-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookCoverCreatorApp.cs
390 lines (343 loc) · 17.4 KB
/
BookCoverCreatorApp.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
#nullable disable
namespace BookCoverCreator
{
/// <summary>
/// Program
/// </summary>
public static class Program
{
private static double aspectRatioCover = 2.0 / 3.0; // 6x9 paperback
private static double aspectRatioSpine = 0.13189; // 1.0 / 7.582 6x9 paperback spine
private static float spineAlphaMultiplier = 2.0f; // increase to reduce fade effect on the spine
private static float spineAlphaPower = 1.0f; // reduce to decrease fade effect on the spine
private static int finalWidth = 4039;
private static int finalHeight = 2775;
private static int spineWidth = 366;
private static string inputFolder;
private static string outputFolder;
private static string backCoverFileName = "BackCover.png";
private static string spineFileName = "Spine.png";
private static string frontCoverFileName = "FrontCover.png";
private static int frontWidth;
private static int backWidth;
private static Rectangle finalRect;
private static Rectangle spineRect;
private static Rectangle backCoverRect;
private static Rectangle backCoverMirrorRectDest;
private static Rectangle backCoverMirrorRectSource;
private static Rectangle frontCoverRect;
private static Rectangle frontCoverMirrorRectDest;
private static Rectangle frontCoverMirrorSource;
private static string paramFileName;
/// <summary>
/// Main
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Please pass one argument, the file name containing the metadata to process.");
Console.WriteLine("This file must contain the following parameters, one per line:");
Console.WriteLine("InputFolder=value (the input folder containing the BackCover, Spine, and FrontCover files--default extension is .png).");
Console.WriteLine("OutputFolder=value (the output folder).");
Console.WriteLine("Width (i.e. 4039)");
Console.WriteLine("Height (i.e. 2775)");
Console.WriteLine("SpineWidth (i.e. 366)");
Console.WriteLine();
Console.WriteLine("The following parameters are optional:");
Console.WriteLine("BackCoverFile=value (the name of the back cover file in the input folder, default is BackCover.png).");
Console.WriteLine("SpineFile=value (the name of the spine file in the input folder, default is Spine.png).");
Console.WriteLine("FrontCoverFile=value (the name of the front cover file in the input folder, default is FrontCover.png).");
Console.WriteLine("SpineAlphaMultiplier=value (i.e. 2.0, higher values reduce fade effect).");
Console.WriteLine("SpineAlphaPower (i.e. 1.0, lower values reduce fade effect, set to 0 for no fade).");
Console.WriteLine();
Console.WriteLine("Enter parameters file name to process:");
paramFileName = Console.ReadLine();
if (string.IsNullOrWhiteSpace(paramFileName))
{
Console.WriteLine("No parameters file name entered. Exiting.");
return;
}
}
else
{
paramFileName = args[0];
}
var dict = ParseKeyValueFile(paramFileName);
AssignVariables(dict);
string backCoverFile = Path.Combine(inputFolder, backCoverFileName);
string spineFile = Path.Combine(inputFolder, spineFileName);
string frontCoverFile = Path.Combine(inputFolder, frontCoverFileName);
string backCoverFileResized = Path.Combine(outputFolder, "04-BackCover.png");
string backCoverFileMirrored = Path.Combine(outputFolder, "02-BackCoverMirrored.png");
string spineFileFaded = Path.Combine(outputFolder, "01-SpineBlend.png");
string frontCoverResized = Path.Combine(outputFolder, "05-FrontCover.png");
string frontCoverFileMirrored = Path.Combine(outputFolder, "03-FrontCoverMirrored.png");
Directory.CreateDirectory(outputFolder);
ProcessCover(backCoverFile, backCoverFileResized, backCoverFileMirrored, true);
ProcessCover(frontCoverFile, frontCoverResized, frontCoverFileMirrored, false);
ProcessSpine(spineFile, spineFileFaded);
Console.WriteLine("Image processing complete. You can now take the files from the output folder at {0} and put them into your template image as individual layers.");
Console.WriteLine("Top down order is: ");
Console.WriteLine("01-SpineBlend.png");
Console.WriteLine("02-BackCoverMirrored.png");
Console.WriteLine("03-FrontCoverMirrored.png");
Console.WriteLine("04-BackCover.png");
Console.WriteLine("05-FrontCover.png");
}
private static Dictionary<string, string> ParseKeyValueFile(string fileName)
{
// Dictionary to store the key-value pairs
Dictionary<string, string> keyValuePairs = new(StringComparer.OrdinalIgnoreCase);
// Read all lines from the file
string[] lines = File.ReadAllLines(fileName.Trim('"'));
// Iterate through each line
foreach (string line in lines)
{
// Skip empty lines or lines that do not contain '='
if (string.IsNullOrWhiteSpace(line) || !line.Contains('='))
{
continue;
}
// Split the line into key and value
string[] parts = line.Split('=', 2); // Limit the split to 2 parts
if (parts.Length == 2)
{
string key = parts[0].Trim();
string value = parts[1].Trim();
// Add the key-value pair to the dictionary
keyValuePairs[key] = value;
}
}
return keyValuePairs;
}
private static void AssignVariables(Dictionary<string, string> dict)
{
foreach (var kv in dict)
{
switch (kv.Key.ToLowerInvariant())
{
case "inputfolder":
inputFolder = kv.Value;
break;
case "outputfolder":
outputFolder = kv.Value;
break;
case "backcoverfile":
backCoverFileName = kv.Value;
break;
case "spinefile":
spineFileName = kv.Value;
break;
case "frontcoverfile":
frontCoverFileName = kv.Value;
break;
case "spinealphamultiplier":
spineAlphaMultiplier = float.Parse(kv.Value);
break;
case "spinealphapower":
spineAlphaPower = float.Parse(kv.Value);
break;
case "width":
finalWidth = int.Parse(kv.Value);
break;
case "height":
finalHeight = int.Parse(kv.Value);
break;
case "spinewidth":
spineWidth = int.Parse(kv.Value);
break;
}
}
if (!Path.IsPathRooted(inputFolder))
{
inputFolder = Path.Combine(Path.GetDirectoryName(paramFileName), inputFolder);
}
if (!Path.IsPathRooted(outputFolder))
{
outputFolder = Path.Combine(Path.GetDirectoryName(paramFileName), outputFolder);
}
aspectRatioSpine = (double)spineWidth / finalHeight;
aspectRatioCover = (double)((finalWidth - spineWidth) / 2.0) / finalHeight;
frontWidth = (finalWidth - spineWidth) / 2;
backWidth = finalWidth - frontWidth - spineWidth;
finalRect = new(0, 0, finalWidth, finalHeight);
spineRect = new(backWidth, 0, spineWidth, finalHeight);
backCoverRect = new(0, 0, backWidth, finalHeight);
backCoverMirrorRectDest = new(spineRect.X, 0, spineRect.Width / 2, spineRect.Height);
backCoverMirrorRectSource = new(0, 0, spineRect.Width / 2, spineRect.Height);
frontCoverRect = new(backWidth + spineWidth, 0, frontWidth, finalHeight);
frontCoverMirrorRectDest = new(spineRect.Left + (spineRect.Width / 2), 0, spineRect.Width / 2, spineRect.Height);
frontCoverMirrorSource = new(frontCoverRect.Width - (spineRect.Width / 2), 0, (spineRect.Width / 2), spineRect.Height);
}
private static void ProcessCover(string inputFilePath, string outputFileResized, string outputFileMirrored, bool isBackCover)
{
using var coverImage = Image.Load<Rgba32>(inputFilePath);
using var croppedImage = Crop(coverImage, aspectRatioCover);
// Create the final image
var finalImage = new Image<Rgba32>(finalRect.Width, finalRect.Height, Color.Transparent);
// Calculate positions to position the resized image
Rectangle coverRectDest = isBackCover ? backCoverRect : frontCoverRect;
Rectangle mirrorRectDest = isBackCover ? backCoverMirrorRectDest : frontCoverMirrorRectDest;
Rectangle mirrorRectSource = isBackCover ? backCoverMirrorRectSource : frontCoverMirrorSource;
// Draw the resized image onto the final image
using var croppedImageResized = croppedImage.Clone(ctx => ctx.Resize(new Size(coverRectDest.Width, coverRectDest.Height)));
finalImage.Mutate(ctx => ctx.DrawImage(croppedImageResized, new Point(coverRectDest.X, coverRectDest.Y), 1.0f));
// Save the final image
finalImage.Save(outputFileResized);
// Re-use final image for mirroring
finalImage.Dispose();
finalImage = new Image<Rgba32>(finalRect.Width, finalRect.Height, Color.Transparent);
// If we have space, extend the image instead of mirroring
float widthRatio = (float)croppedImageResized.Width / (float)croppedImage.Width;
// Convert from cropt resize measures to original image measures
int neededGap = (int)((float)mirrorRectSource.Width / widthRatio);
int actualGap = (coverImage.Width - croppedImage.Width) / 2;
if (neededGap <= actualGap)
{
// Extract the edge of the cover, using the right side for back and left side for front,
// then draw this onto the final image at the mirror position
// Create a crop rect that takes into account the original cover size and the final cover size
int croppedX = ((coverImage.Width - croppedImage.Width) / 2);
int x = isBackCover ? croppedImage.Width + croppedX : croppedX - neededGap;
int y = (croppedImage.Height - coverImage.Height) / 2;
int width = neededGap;
int height = croppedImage.Height;
var cropRect = new Rectangle(x, y, width, height);
using var edgeImage = coverImage.Clone(ctx =>
{
ctx.Crop(cropRect);
ctx.Resize(mirrorRectDest.Width, mirrorRectDest.Height);
});
finalImage.Mutate(ctx => ctx.DrawImage(edgeImage, new Point(mirrorRectDest.X, mirrorRectDest.Y), 1.0f));
}
else
{
// Create the mirrored image
using var mirroredImage = new Image<Rgba32>(coverRectDest.Width, coverRectDest.Height);
// Draw image and mirror it
mirroredImage.Mutate(ctx => ctx.DrawImage(croppedImageResized, new Point(0, 0), 1.0f));
mirroredImage.Mutate(ctx => ctx.Flip(FlipMode.Horizontal));
// Extract the mirror rect source from the mirrored image
using var mirroredImageSource = mirroredImage.Clone(ctx => ctx.Crop(mirrorRectSource));
// Draw the mirrored image onto the final image
finalImage.Mutate(ctx => ctx.DrawImage(mirroredImageSource, new Point(mirrorRectDest.X, mirrorRectDest.Y), 1.0f));
}
// Save the mirrored image
finalImage.Save(outputFileMirrored);
finalImage.Dispose();
}
private static void ProcessSpine(string inputFilePath, string outputFilePath)
{
using Image<Rgba32> image = Image.Load<Rgba32>(inputFilePath);
Crop(image, aspectRatioSpine);
// Remove black rows from top and bottom
image.Mutate(ctx => RemoveBlackRows(image));
// Resize image to spine dimensions
image.Mutate(ctx => ctx.Resize(new Size(spineRect.Width, spineRect.Height)));
// Apply gradient fade from each edge to the center
ApplyAlphaGradient(image);
// Create the final image
using Image<Rgba32> finalImage = new(finalRect.Width, finalRect.Height, Color.Transparent);
// Draw the resized and faded image onto the final image
finalImage.Mutate(ctx => ctx.DrawImage(image, new Point(spineRect.X, spineRect.Y), 1.0f));
// Save the final image as a PNG
finalImage.Save(outputFilePath);
}
private static Image<Rgba32> Crop(Image<Rgba32> image, double aspectRatio)
{
// Calculate the desired dimensions of the cropped image
var imageAspectRatio = (double)image.Width / (double)image.Height;
var cropWidth = aspectRatio > imageAspectRatio ? image.Width : (int)(image.Height * aspectRatio);
var cropHeight = aspectRatio < imageAspectRatio ? image.Height : (int)(image.Width / aspectRatio);
// Calculate the crop rectangle, centered in the image
var x = (image.Width - cropWidth) / 2;
var y = (image.Height - cropHeight) / 2;
// Crop the image
var croppedImage = image.Clone();
croppedImage.Mutate(ctx => ctx.Crop(new Rectangle(x, y, cropWidth, cropHeight)));
return croppedImage;
}
private static void RemoveBlackRows(Image<Rgba32> image)
{
if (image.Width < 16 && image.Height < 16)
{
return;
}
const byte threshold = 10;
// Remove black rows from the top
int topNonBlackRow = 0;
for (int y = 0; y < image.Height; y++)
{
bool isBlackRow = true;
for (int x = 0; x < image.Width; x++)
{
if (image[x, y].R > threshold || image[x, y].G > threshold || image[x, y].B > threshold)
{
isBlackRow = false;
break;
}
}
if (!isBlackRow)
{
topNonBlackRow = y;
break;
}
}
// Remove black rows from the bottom
int bottomNonBlackRow = image.Height - 1;
for (int y = image.Height - 1; y >= 0; y--)
{
bool isBlackRow = true;
for (int x = 0; x < image.Width; x++)
{
if (image[x, y].R > threshold || image[x, y].G > threshold || image[x, y].B > threshold)
{
isBlackRow = false;
break;
}
}
if (!isBlackRow)
{
bottomNonBlackRow = y;
break;
}
}
// Crop the image to remove black rows from top and bottom
int newHeight = bottomNonBlackRow - topNonBlackRow + 1;
if (newHeight > 0)
{
image.Mutate(ctx => ctx.Crop(new Rectangle(0, topNonBlackRow, image.Width, newHeight)));
}
}
private static void ApplyAlphaGradient(Image<Rgba32> image)
{
int width = image.Width;
int halfWidth = width / 2;
static float GetAlphaFactor(int x, int width, int halfWidth)
{
float distanceFromEdge = Math.Min(x, width - x - 1);
return (float)Math.Clamp(Math.Pow((distanceFromEdge / halfWidth) * spineAlphaMultiplier, spineAlphaPower), 0.0, 1.0);
}
image.Mutate(ctx =>
{
for (int y = 0; y < image.Height; y++)
{
for (int x = 0; x < width; x++)
{
float alphaFactor = GetAlphaFactor(x, width, halfWidth);
Rgba32 pixel = image[x, y];
pixel.A = (byte)(pixel.A * alphaFactor);
image[x, y] = pixel;
}
}
});
}
}
}
#nullable restore