From 226642aa048a36c518cb2b48fa5445cb1fb34615 Mon Sep 17 00:00:00 2001 From: Benito Palacios Sanchez Date: Sat, 14 Oct 2023 10:11:44 +0200 Subject: [PATCH] :sparkles: Converter from TIFF to plain image --- src/Texim/Formats/Tiff2PlainFullImage.cs | 52 ++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/Texim/Formats/Tiff2PlainFullImage.cs diff --git a/src/Texim/Formats/Tiff2PlainFullImage.cs b/src/Texim/Formats/Tiff2PlainFullImage.cs new file mode 100644 index 0000000..79fc330 --- /dev/null +++ b/src/Texim/Formats/Tiff2PlainFullImage.cs @@ -0,0 +1,52 @@ +namespace Texim.Formats; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Texim.Colors; +using Texim.Images; +using Texim.Pixels; +using Yarhl.FileFormat; + +public class Tiff2PlainFullImage : IConverter +{ + public FullImage Convert(TiffImage source) + { + ArgumentNullException.ThrowIfNull(source); + + var image = new FullImage(source.CanvasWidth, source.CanvasHeight); + foreach (TiffPage page in source.Pages.Reverse()) { + DrawLayer(page, image); + } + + return image; + } + + private static void DrawLayer(TiffPage page, FullImage image) + { + for (int x = 0; x < page.Width; x++) { + for (int y = 0; y < page.Height; y++) { + Rgb pixel = GetColor(page, x, y); + if (pixel.Alpha == 0) { + continue; // don't overwrite other layers. + } + + int imageIdx = ((page.Y + y) * image.Width) + page.X + x; + image.Pixels[imageIdx] = pixel; + } + } + } + + private static Rgb GetColor(TiffPage page, int x, int y) + { + int idx = (y * page.Width) + x; + if (page.IsIndexed) { + IndexedPixel pixel = page.IndexedPixels[idx]; + return new Rgb(page.ColorMap[pixel.Index], pixel.Alpha); + } + + return page.RgbPixels[idx]; + } +}