Skip to content

Commit

Permalink
fix for non square images
Browse files Browse the repository at this point in the history
  • Loading branch information
cadon committed Jun 1, 2022
1 parent 759a84f commit ea4ed27
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
43 changes: 22 additions & 21 deletions ColorRegionMaskCreator/ImageMasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static void CreateFiles(string baseImageFilePath, string colorMaskFile,
using (Bitmap bmpColorMask = File.Exists(colorMaskFile) ? new Bitmap(colorMaskFile) : null)
{
BitmapData bmpDataJpg = bmpBackgroundJpg.LockBits(
new Rectangle(0, 0, bmpBackground.Width, bmpBackground.Height),
new Rectangle(0, 0, bmpBackgroundJpg.Width, bmpBackgroundJpg.Height),
ImageLockMode.ReadWrite, bmpBackgroundJpg.PixelFormat);
BitmapData bmpDataBackground = bmpBackground.LockBits(new Rectangle(0, 0, bmpBackground.Width, bmpBackground.Height),
ImageLockMode.ReadWrite, bmpBackground.PixelFormat);
Expand Down Expand Up @@ -236,25 +236,8 @@ byte ApplyLightnessCorrection(byte val, double factor)
var imageWidth = (int)(bmpBackground.Width * resizeFactor);
var imageHeight = (int)(bmpBackground.Height * resizeFactor);

SaveResizedBitmap(bmpBackground, filePathBaseImage);
SaveResizedBitmap(bmpColorMask, filePathMaskImage);

void SaveResizedBitmap(Bitmap bmp, string filePath)
{
if (bmp == null) return;

using (var bmpResized = new Bitmap(imageWidth, imageHeight, bmp.PixelFormat))
using (var g = Graphics.FromImage(bmpResized))
{
g.CompositingMode = CompositingMode.SourceCopy;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(bmp, 0, 0, imageWidth, imageWidth);
bmpResized.Save(filePath, ImageFormat.Png);
}
}
SaveResizedBitmap(bmpBackground, filePathBaseImage, imageWidth, imageHeight);
SaveResizedBitmap(bmpColorMask, filePathMaskImage, imageWidth, imageHeight);

if (createRegionImages && bmpColorMask != null)
{
Expand All @@ -279,13 +262,31 @@ void SaveResizedBitmap(Bitmap bmp, string filePath)
{
if (CreateColorRegionImages(colors, enabledColorRegions, filePathMaskImage, regionImage))
SaveResizedBitmap(regionImage,
Path.Combine(OutputRegionsFolderPath, Path.GetFileNameWithoutExtension(baseImageFilePath) + "_PaintRegion_" + i + ".png"));
Path.Combine(OutputRegionsFolderPath, Path.GetFileNameWithoutExtension(baseImageFilePath) + "_PaintRegion_" + i + ".png"),
imageWidth, imageHeight);
}
}
}
}
}

private static void SaveResizedBitmap(Bitmap bmp, string filePath, int imageWidth, int imageHeight)
{
if (bmp == null) return;

using (var bmpResized = new Bitmap(imageWidth, imageHeight, bmp.PixelFormat))
using (var g = Graphics.FromImage(bmpResized))
{
g.CompositingMode = CompositingMode.SourceCopy;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(bmp, 0, 0, imageWidth, imageHeight);
bmpResized.Save(filePath, ImageFormat.Png);
}
}

/// <summary>
/// Creates a transformation for lightness values to create a gaussian histogram.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion ColorRegionMaskCreator/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
4 changes: 2 additions & 2 deletions ColorRegionMaskCreator/config.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; max size of the output images. Smaller images are not enlarged
maxWidth = 600
maxHeight = 800
maxWidth = 800
maxHeight = 600

; Color of the region highlighting
colorR = 240
Expand Down

0 comments on commit ea4ed27

Please sign in to comment.