-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form1.cs
305 lines (256 loc) · 10.3 KB
/
Form1.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using mshtml;
namespace WebtoonBodge
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
RB_isPNG.Checked = true;
RB_isJPG.Checked = false;
dm.createLocalDir(dm.outputFolderName);
}
private void BTN_refresh_Click(object sender, EventArgs e)
{
LBX_images.Items.Clear();
foreach (String entry in dm.ProcessDirectory(Directory.GetCurrentDirectory()))
{
LBX_images.Items.Add(entry);
}
Console.WriteLine(dm.extension);
}
private void BTN_init_Click(object sender, EventArgs e)
{
Initialise(Convert.ToInt32(TB_maxHeight.Text), dm.extension);
BTN_init.Enabled = false;
MessageBox.Show("Operation est fini");
}
private void RB_isJPG_CheckedChanged(object sender, EventArgs e)
{
if (RB_isJPG.Checked)
{
dm.extension = ".jpg";
return;
}
}
private void RB_isPNG_CheckedChanged(object sender, EventArgs e)
{
if (RB_isPNG.Checked)
{
dm.extension = ".png";
return;
}
}
private void BTN_UpArrow_Click(object sender, EventArgs e)
{
MoveItem(-1);
}
public void MoveItem(int direction)
{
// Checking selected item
if (LBX_images.SelectedItem == null || LBX_images.SelectedIndex < 0)
return; // No selected item - nothing to do
// Calculate new index using move direction
int newIndex = LBX_images.SelectedIndex + direction;
// Checking bounds of the range
if (newIndex < 0 || newIndex >= LBX_images.Items.Count)
return; // Index out of range - nothing to do
object selected = LBX_images.SelectedItem;
// Removing removable element
LBX_images.Items.Remove(selected);
// Insert it in new position
LBX_images.Items.Insert(newIndex, selected);
// Restore selection
LBX_images.SetSelected(newIndex, true);
}
private void BTN_DownArrow_Click(object sender, EventArgs e)
{
MoveItem(1);
}
readonly DirectoryManager dm = new DirectoryManager();
//total images in folder
private int TOTAL_IMAGES = 0;
//total collected image height
private int CANVAS_HEIGHT = 0;
//path of where files are stored
private readonly string PATH_LOC = System.IO.Directory.GetCurrentDirectory();
public void Initialise(int max_height, string ext = ".png")
{
dm.extension = ext;
//if no output folder, create one
dm.createLocalDir(dm.outputFolderName);
//KABAMAMAMAMAMMABAM
if (dm.validateFolder(dm.outputFolderName))
{
FixHeight(MergeAll(PATH_LOC), max_height);
}
}
private void FixHeight(Bitmap srcImg, int maxHeight)
{
int page = 0;
Console.WriteLine("To do: {0}", srcImg.Height);
//current spot to draw from
int currentHeight = 0;
int pageNumber = 0;
//currently remaining is entire height of src image
int remainingHeight = srcImg.Height - 1;
//if entire image already fits max
if (remainingHeight <= maxHeight)
{
srcImg.Save(dm.outputFolderName + "/" + dm.outputPageName + "00" + dm.extension);
srcImg.Dispose(); // GDI+ Encountered a generic error
return;
}
//image properties, //image height is variable
int canvas_width = srcImg.Width;
while (remainingHeight > 0)
{
if (remainingHeight <= maxHeight)
{
maxHeight = remainingHeight;
}
//current bottom line
int cutHeight = currentHeight + maxHeight;
Console.WriteLine("Cutting page {0}, current{1}, cutting_from{2}", page, currentHeight, cutHeight);
if (maxHeight != remainingHeight)
{
//while it's a panel
while (DoesLineContainDifferentColours(srcImg, cutHeight))
{
cutHeight--;
if (cutHeight == currentHeight + (maxHeight / 2))
{
cutHeight = currentHeight + maxHeight;
for (int i = cutHeight; i < srcImg.Height - 1 && DoesLineContainDifferentColours(srcImg, cutHeight); i++)
{
if ((cutHeight) == srcImg.Height)
{
break;
}
cutHeight = i;
}
break;
}
}
}
//our dimensions
Bitmap img = new Bitmap(canvas_width, cutHeight - currentHeight);
//draw onto canvas
using (Graphics drawTool = Graphics.FromImage(img))
{
//make sure it HQ
drawTool.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//current spot to draw from
Console.WriteLine("creating image");
drawTool.DrawImage(srcImg, new Rectangle(0, 0, img.Width, img.Height), new Rectangle(0, currentHeight, img.Width, img.Height), GraphicsUnit.Pixel);
Console.WriteLine("image created");
currentHeight += img.Height;
remainingHeight -= img.Height;
for (int i = page; i<entriesH.Length; i++)
{
if (entriesH[page] > currentHeight)
{
break;
}
page++;
}
drawTool.Flush();
if (dm.extension.ToLower().Equals(".png"))
{
img.Save(dm.outputFolderName + "/" + Path.GetFileName(LBX_images.Items[page].ToString()) + (pageNumber++).ToString("00") + dm.extension, System.Drawing.Imaging.ImageFormat.Png);
}
else if (dm.extension.ToLower().Equals(".jpg") || dm.extension.ToLower().Equals(".jpeg"))
{
img.Save(dm.outputFolderName + "/" + Path.GetFileName(LBX_images.Items[page].ToString()) + (pageNumber++).ToString("00") + dm.extension, System.Drawing.Imaging.ImageFormat.Jpeg);
}
img.Dispose();
drawTool.Dispose();
}
}
}
public int[] entriesH;
//merge all .png/.jpeg files in a directory into a super image
//returns the super image
private Bitmap MergeAll(string dir)
{
//create a new set of images, where n of set is number of .png in dir
TOTAL_IMAGES = dm.ProcessDirectory(dir).Length;
Image[] imgs = new Image[TOTAL_IMAGES];
entriesH = new int[LBX_images.Items.Count];
//store each .png into each image
{
int i = 0; //index
foreach (object fileName in LBX_images.Items) //debug
{
Console.WriteLine("merging {0}", fileName);
imgs[i] = Image.FromFile(fileName.ToString());
entriesH[i] = imgs[i].Height;
//get the total height of all images
CANVAS_HEIGHT = imgs[i].Height + CANVAS_HEIGHT;
i++; //next img
}
}
if (entriesH.Length != 0)
{
for (int i = 0; i < entriesH.Length - 1; i++)
{
entriesH[i + 1] = entriesH[i] + entriesH[i + 1];
}
}
//declare master canvas
int canvas_width = imgs[0].Width;
int canvas_height = CANVAS_HEIGHT;
//our dimensions
Bitmap canvas = new Bitmap(canvas_width, canvas_height);
//draw onto
Graphics drawTool = Graphics.FromImage(canvas);
//make sure it HQ
drawTool.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//current spot to draw from
int current_height = 0;
for (int i = 0; i < TOTAL_IMAGES; i++)
{
drawTool.DrawImage(imgs[i], new Rectangle(0, current_height, imgs[i].Width, imgs[i].Height), new Rectangle(0, 0, imgs[i].Width, imgs[i].Height), GraphicsUnit.Pixel);
current_height += imgs[i].Height;
}
drawTool.Flush();
drawTool.Save();
//dispose all images used
foreach (Image img in imgs)
{
img.Dispose();
}
return canvas;
}
private bool DoesLineContainDifferentColours(Bitmap bmp, int lineNumber)
{
int threshold = 25;
Color lineColor = bmp.GetPixel(0, lineNumber);
for (int x = 1; x < bmp.Width; x++)
{
Color currentPixel = bmp.GetPixel(x, lineNumber);
if (Math.Abs(lineColor.R - currentPixel.R) > threshold ||
Math.Abs(lineColor.G - currentPixel.G) > threshold ||
Math.Abs(lineColor.B - currentPixel.B) > threshold)
{
return true;
}
}
return false;
}
}
}