Skip to content

Commit

Permalink
🐛 Fix exporting TIFF RGB images
Browse files Browse the repository at this point in the history
  • Loading branch information
pleonex committed Oct 1, 2023
1 parent bd7a571 commit f9c54b3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Texim/Formats/Sprite2Tiff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public TiffImage Convert(ISprite source)
page.ColorMap = fullPalette;
page.IsIndexed = true;
} else {
page.RgbPixels = layerIndexedImage.CreateFullImage(parameters.Palettes).Pixels;
page.RgbPixels = layerIndexedImage.CreateFullImage(parameters.Palettes, true).Pixels;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Texim/Formats/Tiff2Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private static void WriteIndexedPage(Tiff tiff, TiffPage page, int totalPages)
Assert(tiff.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.INCH));
Assert(tiff.SetField(TiffTag.PHOTOMETRIC, Photometric.PALETTE));
Assert(tiff.SetField(TiffTag.EXTRASAMPLES, 1, new short[] { (short)ExtraSample.UNASSALPHA }));
Assert(tiff.SetField(TiffTag.COMPRESSION, Compression.LZW));

Assert(tiff.SetField(TiffTag.XPOSITION, page.X / 100.0));
Assert(tiff.SetField(TiffTag.YPOSITION, page.Y / 100.0));
Expand Down Expand Up @@ -148,6 +149,7 @@ private static void WriteRgbPage(Tiff tiff, TiffPage page, int totalPages)
Assert(tiff.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.INCH));
Assert(tiff.SetField(TiffTag.EXTRASAMPLES, 1, new short[] { (short)ExtraSample.UNASSALPHA }));
Assert(tiff.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB));
Assert(tiff.SetField(TiffTag.COMPRESSION, Compression.LZW));

Assert(tiff.SetField(TiffTag.XPOSITION, page.X / 100.0));
Assert(tiff.SetField(TiffTag.YPOSITION, page.Y / 100.0));
Expand Down
7 changes: 4 additions & 3 deletions src/Texim/Images/Indexed2FullImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ public FullImage Convert(IIndexedImage source)
var image = new FullImage(source.Width, source.Height);
for (int i = 0; i < source.Pixels.Length; i++) {
IndexedPixel pixel = source.Pixels[i];
if (pixel.PaletteIndex > palettes.Palettes.Count) {
throw new FormatException("Missing palettes");
}

if (firstColorAsTransparent && pixel.Index == 0) {
// Color may not exists as it could be a
// generated transparent pixel with not associated palette.
image.Pixels[i] = new Rgb(0, 0, 0, 0);
} else if (pixel.PaletteIndex >= palettes.Palettes.Count) {
throw new FormatException($"Missing palette {pixel.PaletteIndex}. Count: {palettes.Palettes.Count}");
} else if (pixel.Index >= palettes.Palettes[pixel.PaletteIndex].Colors.Count) {
throw new FormatException($"Missing color {pixel.Index}. Count: {palettes.Palettes[pixel.PaletteIndex].Colors.Count}");
} else {
var color = palettes.Palettes[pixel.PaletteIndex].Colors[pixel.Index];
image.Pixels[i] = (pixel.Alpha == 255) ? color : new Rgb(color, pixel.Alpha);
Expand Down

0 comments on commit f9c54b3

Please sign in to comment.