Skip to content

Commit

Permalink
✨ Converter from TIFF to plain image
Browse files Browse the repository at this point in the history
  • Loading branch information
pleonex committed Oct 14, 2023
1 parent da3ae17 commit 226642a
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Texim/Formats/Tiff2PlainFullImage.cs
Original file line number Diff line number Diff line change
@@ -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<TiffImage, FullImage>
{
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];
}
}

0 comments on commit 226642a

Please sign in to comment.