/// Copyright (c) 2021 Iiro Iivanainen, Harri Linna, Jere Pakkanen, Riikka Vilavaara
///
/// Permission is hereby granted, free of charge, to any person obtaining
/// a copy of this software and associated documentation files (the
/// "Software"), to deal in the Software without restriction, including
/// without limitation the rights to use, copy, modify, merge, publish,
/// distribute, sublicense, and/or sell copies of the Software, and to
/// permit persons to whom the Software is furnished to do so, subject to
/// the following conditions:
///
/// The above copyright notice and this permission notice shall be included
/// in all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
/// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
/// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
/// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
/// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
/// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///
using ImageMagick;
using System.Text.Json.Serialization;
namespace CoreLibrary
{
///
/// Slice class contains one slice in image form and also the filepath.
/// Responsible of updating the image when given a new depth and turning the image to
/// byte array.
///
public class Slice
{
[JsonIgnore]
public MagickImage Stack;
public RelativeFilepath Filepath { get; set; }
public string Template { get; set; }
internal double LevelMin = 0.0;
internal double LevelMax = 100.0;
///
/// Constructor for JSON serialization and deserialization. Note that the
/// parameter names must match the attribute names (non case sensitive)
///
///
///
[JsonConstructor]
public Slice(RelativeFilepath filepath, string template)
{
Filepath = filepath;
Template = template;
}
///
/// Default constructor of Slice.
///
public Slice()
{
// Empty.
}
///
/// Updates the slice if Map is set as image sequence.
///
/// Requested slice
public void UpdateImgSeq(int depth)
{
string temp = "";
if (Template != null) temp = Template;
string path = "";
if (Filepath != null) path = Filepath.GetPath();
MagickImage img = MapReader.ReadImgSeq(path, depth, temp);
Stack = img;
}
///
/// Updates the slice if Map is set as multiframe tiff.
///
/// Requested slice
internal void Update3DTiff(int depth)
{
MagickImage img = MapReader.Read3Dtif(Filepath.GetPath(), depth);
Stack = img;
}
///
/// Updates the slice if Map is set as Raw file type.
///
/// Requested slice
/// Image width
/// Image height
/// Byte depth
/// Little endian or not. True if little endian,
/// false if big endian.
internal void UpdateRaw(int depth, int width, int height, int byteDepth,
bool littleEndian)
{
MagickImage img = MapReader.ReadRaw(Filepath.GetPath(),
depth, width, height, byteDepth, littleEndian);
Stack = img;
}
///
/// Return the slice as a byte array. Contains image file.
///
/// Byte array of the slice image file.
public byte[] GetAsByteArray()
{
if (Stack != null)
{
var img = new MagickImage(Stack.ToByteArray());
img.Level(new Percentage(LevelMin), new Percentage(LevelMax));
return img.ToByteArray();
}
return null;
}
///
/// Setting file path for the slice.
///
/// File path.
public void SetFilepath(string path)
{
Filepath = new RelativeFilepath(path);
}
///
/// Get file path for the slice.
///
/// File path.
public string GetFilepath()
{
if (Filepath == null) return null;
return Filepath.GetPath();
}
internal void SetSliceRelation(string relativeTo)
{
Filepath.SetRelation(relativeTo);
}
///
/// Adjust Level of stack.
///
/// Black point in percentage
/// White point in percentage
internal void AdjustLevel(double min, double max)
{
LevelMin = min;
LevelMax = max;
}
}
}