Skip to content

Commit

Permalink
Update ImageTools.cs
Browse files Browse the repository at this point in the history
Fix vertical combining of images so that the image widths may vary.
  • Loading branch information
towsey committed Feb 20, 2020
1 parent 53e820e commit dadf0bf
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/TowseyLibrary/ImageTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3707,44 +3707,53 @@ public static Image<T> CombineImagesVertically<T>(List<Image<T>> list, int maxWi
{
return CombineImagesVertically(maxWidth, list.ToArray());
}

public static Image<T> CombineImagesVertically<T>(params Image<T>[] images) where T : struct, IPixel<T>
{
return CombineImagesVertically<T>(maximumWidth: null, images);
}

/// <summary>
/// Stacks the passed images one on top of the other.
/// Assumes that all images have the same width.
/// </summary>
/// <param name="array"></param>
/// <param name="maximumWidth">The maximum width of the output images</param>
/// <returns></returns>
/// <param name="maximumWidth">The maximum width of the output images.</param>
/// <param name="array">An array of Image.</param>
/// <returns>A single image.</returns>
public static Image<T> CombineImagesVertically<T>(int? maximumWidth, Image<T>[] array) where T : struct, IPixel<T>
{
int width = maximumWidth ?? array[0].Width; // assume all images have the same width

int width = 0;
int compositeHeight = 0;
for (int i = 0; i < array.Length; i++)
{
if (null == array[i])
if (array[i] == null)
{
continue;
}

compositeHeight += array[i].Height;

if (array[i].Width > width)
{
width = array[i].Width;
}
}

// check width is not over the max
if (width > maximumWidth)
{
width = (int)maximumWidth;
}

var compositeBmp = new Image<T>(width, compositeHeight);
int yOffset = 0;

// TODO: Fix at some point. Using default configuration with parallelism there is some kind of batching bug that causes a crash
compositeBmp.Mutate(Drawing.NoParallelConfiguration, gr => {

//gr.Clear(Color.Black);
gr.Clear(Color.DarkGray);

for (int i = 0; i < array.Length; i++)
{
if (null == array[i])
if (array[i] == null)
{
continue;
}
Expand All @@ -3761,8 +3770,8 @@ public static Image<T> CombineImagesVertically<T>(int? maximumWidth, Image<T>[]
/// Stacks the passed images one on top of the other.
/// Assumes that all images have the same width.
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
/// <param name="list">A list of images.</param>
/// <returns>A single image.</returns>
public static Image<T> CombineImagesInLine<T>(List<Image<T>> list) where T : struct, IPixel<T>
{
return CombineImagesInLine(list.ToArray());
Expand All @@ -3772,8 +3781,8 @@ public static Image<T> CombineImagesInLine<T>(List<Image<T>> list) where T : str
/// Stacks the passed images one on top of the other.
/// Assumes that all images have the same width.
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
/// <param name="array">An array of images.</param>
/// <returns>A single image.</returns>
public static Image<T> CombineImagesInLine<T>(params Image<T>[] array) where T : struct, IPixel<T>
{
int height = array[0].Height; // assume all images have the same height
Expand Down

0 comments on commit dadf0bf

Please sign in to comment.