Skip to content

Commit

Permalink
Improve NovelTool memory usage.
Browse files Browse the repository at this point in the history
Added PageSizeAffectByZoom options: Determines whether the page number size is affected by the zoom feature (Default false).
Added HeadSizeAffectByZoom options: Determines whether the header size is affected by the zoom feature (Default false).
Added FooterSizeAffectByZoom options: Determines whether the footer size is affected by the zoom feature (Default false).

Fix the issue that the page number is invalid when GenerateView saves all files
  • Loading branch information
avan06 committed Jul 21, 2022
1 parent a25f3b5 commit 04aee6b
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 111 deletions.
1 change: 1 addition & 0 deletions NovelTool/BitmapTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ public enum FilterXY
public static Bitmap ConvolutionFilter(Bitmap sourceBitmap, double[,] filterMatrix, double factor = 1, int bias = 0, bool grayscale = false)
{
if (sourceBitmap == null) return null;

BitmapData sourceData = sourceBitmap.LockBits(new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height];
byte[] resultBuffer = new byte[sourceData.Stride * sourceData.Height];
Expand Down
62 changes: 38 additions & 24 deletions NovelTool/GenerateView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public partial class GenerateView : Form
private Color OutputForeColor;
private float ForeColorRate;

private PositionType HeadPositionType;
private PositionType FooterPositionType;
private PositionType PagePositionType;
private bool HeadSizeAffectByZoom;
private bool FooterSizeAffectByZoom;
private bool PageSizeAffectByZoom;

private float TextFontSize;
private FontFamily TextFontFamily;
private FontStyle FontTextStyle;
Expand Down Expand Up @@ -105,14 +112,14 @@ private void ToolStripSaveAll_Click(object sender, EventArgs e)
{
(string path, string extension, ImageCodecInfo imgEncoder, EncoderParameters encoderParameters, PixelFormat pixelFormat) = GetSaveInfo();

ParseOutput(-1, true);

for (int idx = 0; idx < OutputImgs.Count; idx++)
for (int outputIdx = 0; outputIdx < outputCount; outputIdx++)
{
ParseOutput(outputIdx);

string name = mainForm.Filter.name != null ?
string.Format(@"{0}_{1}", idx, mainForm.Filter.name) :
string.Format(@"{0}", idx);
Bitmap result = (Bitmap)OutputImgs[idx];
string.Format(@"{0}_{1}", outputIdx, mainForm.Filter.name) :
string.Format(@"{0}", outputIdx);
Bitmap result = (Bitmap)OutputImgs[0];
if (!(result.Tag is bool))
{ //Bitmap Not Illustration
if (OutputAdjustColorCheck) ImageTool.ChangeForeColor(result, OutputForeColor.ToArgb(), OutputBackColor.ToArgb(), ForeColorRate);
Expand All @@ -129,6 +136,7 @@ private void ToolStripSaveAll_Click(object sender, EventArgs e)
ImageTool.SaveImage(outputImage, path, name, extension, imgEncoder, encoderParameters, pixelFormat);
}
result.Dispose();
GC.Collect();
}
}

Expand Down Expand Up @@ -240,6 +248,13 @@ private GenerateView()
OutputForeColor = Color.FromKnownColor(Properties.Settings.Default.OutputForeColor.Value);
ForeColorRate = Properties.Settings.Default.ForeColorRate.Value;

HeadPositionType = Properties.Settings.Default.HeadPositionType.Value; //Head輸出位置
FooterPositionType = Properties.Settings.Default.FooterPositionType.Value; //Footer輸出位置
PagePositionType = Properties.Settings.Default.PagePositionType.Value; //Page輸出位置
HeadSizeAffectByZoom = Properties.Settings.Default.HeadSizeAffectByZoom.Value;
FooterSizeAffectByZoom = Properties.Settings.Default.FooterSizeAffectByZoom.Value;
PageSizeAffectByZoom = Properties.Settings.Default.PageSizeAffectByZoom.Value;

TextFontSize = Properties.Settings.Default.TextFontSize.Value;
TextFontFamily = Properties.Settings.Default.TextFontFamily.Value;
FontTextStyle = (Properties.Settings.Default.TextFontBold.Value ? FontStyle.Bold : 0) | (Properties.Settings.Default.TextFontItalic.Value ? FontStyle.Italic : 0);
Expand Down Expand Up @@ -416,6 +431,7 @@ private int SetOutputPage(int outputIdx)

ToolStripPageBox.SelectedItem = outputIdx;

GC.Collect();
return outputIdx;
}

Expand Down Expand Up @@ -745,18 +761,15 @@ private void AddNowCreateNext(bool outputAll, int outputIdx,
{
(destImage, destPoint) = CreateDestImage(OutputWidth, OutputHeight, srcImage);

PositionType HeadPositionType = Properties.Settings.Default.HeadPositionType.Value; //Head輸出位置
PositionType FooterPositionType = Properties.Settings.Default.FooterPositionType.Value; //Footer輸出位置
PositionType PagePositionType = Properties.Settings.Default.PagePositionType.Value; //Page輸出位置

if (!isEpubAozora)
{
if (srcImage == null) srcImage = OpenImage(fullPath);

GenerateDrawTitle(HeadPositionType, columnHeadList, srcImage, ref destImage);
GenerateDrawTitle(FooterPositionType, columnFooterList, srcImage, ref destImage);
GenerateDrawTitle(HeadPositionType, HeadSizeAffectByZoom, columnHeadList, srcImage, ref destImage);
GenerateDrawTitle(FooterPositionType, FooterSizeAffectByZoom, columnFooterList, srcImage, ref destImage);
}
GenerateDrawText(PagePositionType, outputIdx.ToString(), srcImage, ref destImage);
var pageCnt = outputIdx != -1 ? outputIdx : outputImgs.Count;
GenerateDrawText(PagePositionType, pageCnt.ToString(), srcImage, ref destImage);
}
else (destImage, destPoint) = CreateDestImage(1, 1, null);
}
Expand All @@ -771,14 +784,15 @@ private void AddNowCreateNext(bool outputAll, int outputIdx,
/// <param name="srcImage">The source Bitmap currently read</param>
/// <param name="destImage">The currently generated Bitmap</param>
/// <param name="initSize">Margin size of print header or footer</param>
private void GenerateDrawTitle(PositionType PositionTypeBoxTitle,
private void GenerateDrawTitle(PositionType PositionTypeBoxTitle, bool SizeAffectByZoom,
List<(RectType RType, float X, float Y, float Width, float Height, List<(RectType RType, float X, float Y, float Width, float Height)> Entitys)> columnTitleList,
Bitmap srcImage, ref Bitmap destImage, int initSize = 10)
{
if (PositionTypeBoxTitle == PositionType.None || columnTitleList == null || columnTitleList.Count <= 0) return;

float offsetX = -1;
int modeWidth = (int)(mainForm.Modes.Width * mainForm.ZoomFactor);
float zoomFactor = SizeAffectByZoom ? mainForm.ZoomFactor : 1;
int modeWidth = (int)(mainForm.Modes.Width * zoomFactor);
Point destPointTitle = new Point(initSize, initSize);
if (PositionTypeBoxTitle == PositionType.TopRight) destPointTitle = new Point(OutputWidth - initSize, initSize);
else if (PositionTypeBoxTitle == PositionType.BottomRight) destPointTitle = new Point(OutputWidth - initSize, OutputHeight - initSize);
Expand All @@ -792,9 +806,9 @@ private void GenerateDrawTitle(PositionType PositionTypeBoxTitle,
for (int cIdx = columnTitleList.Count - 1; cIdx >= 0; cIdx--)
{
var columnTitle = columnTitleList[cIdx];
int newWidth = (int)(columnTitle.Width * mainForm.ZoomFactor);
int newHeight = (int)(columnTitle.Height * mainForm.ZoomFactor);
int blankWidth = offsetX != -1 ? (int)((offsetX - columnTitle.X - columnTitle.Width) * mainForm.ZoomFactor) : 0;
int newWidth = (int)(columnTitle.Width * zoomFactor);
int newHeight = (int)(columnTitle.Height * zoomFactor);
int blankWidth = offsetX != -1 ? (int)((offsetX - columnTitle.X - columnTitle.Width) * zoomFactor) : 0;
blankWidth = blankWidth > modeWidth ? modeWidth : blankWidth;
DrawTitle(columnTitle, newWidth, newHeight, -blankWidth, true, destPointTitle.Y == OutputHeight - initSize, srcImage, ref destImage, ref destPointTitle);
offsetX = columnTitle.X;
Expand All @@ -809,9 +823,9 @@ private void GenerateDrawTitle(PositionType PositionTypeBoxTitle,
for (int cIdx = 0; cIdx < columnTitleList.Count; cIdx++)
{
var columnTitle = columnTitleList[cIdx];
int newWidth = (int)(columnTitle.Width * mainForm.ZoomFactor);
int newHeight = (int)(columnTitle.Height * mainForm.ZoomFactor);
int blankWidth = offsetX != -1 ? (int)((columnTitle.X - offsetX) * mainForm.ZoomFactor) : 0;
int newWidth = (int)(columnTitle.Width * zoomFactor);
int newHeight = (int)(columnTitle.Height * zoomFactor);
int blankWidth = offsetX != -1 ? (int)((columnTitle.X - offsetX) * zoomFactor) : 0;
blankWidth = blankWidth > modeWidth ? modeWidth : blankWidth;
DrawTitle(columnTitle, newWidth, newHeight, blankWidth, false, destPointTitle.Y == OutputHeight - initSize, srcImage, ref destImage, ref destPointTitle);
offsetX = columnTitle.X + columnTitle.Width;
Expand Down Expand Up @@ -897,10 +911,10 @@ private void GenerateDrawText(PositionType PositionTypeBoxTitle, string text,
format.LineAlignment = StringAlignment.Far;
}

DrawText(text, format, ref destImage, ref destPoint);
DrawText(text, format, ref destImage, ref destPoint, PageSizeAffectByZoom);
}

private void DrawText(string text, StringFormat format, ref Bitmap destImage, ref Point destPoint)
private void DrawText(string text, StringFormat format, ref Bitmap destImage, ref Point destPoint, bool PageZoomAffect = false)
{
using (Graphics gr = Graphics.FromImage(destImage))
{
Expand All @@ -914,7 +928,7 @@ private void DrawText(string text, StringFormat format, ref Bitmap destImage, re
using (var foreBrush = new SolidBrush(OutputForeColor))
using (GraphicsPath textPath = new GraphicsPath())
{
textPath.AddString(text, TextFontFamily, (int)FontTextStyle, TextFontSize * mainForm.ZoomFactor, destPoint, format);
textPath.AddString(text, TextFontFamily, (int)FontTextStyle, TextFontSize * (PageZoomAffect ? mainForm.ZoomFactor : 1), destPoint, format);
gr.FillPath(foreBrush, textPath);
}
}
Expand Down
Loading

0 comments on commit 04aee6b

Please sign in to comment.