//
// Copyright (c) James South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageProcessor.Formats
{
///
/// Color reader for reading grayscale colors from a png file.
///
public sealed class GrayscaleReader : IColorReader
{
///
/// Whether t also read the alpha channel.
///
private readonly bool useAlpha;
///
/// The current row.
///
private int row;
///
/// Initializes a new instance of the class.
///
///
/// If set to true the color reader will also read the
/// alpha channel from the scanline.
///
public GrayscaleReader(bool useAlpha)
{
this.useAlpha = useAlpha;
}
///
public void ReadScanline(byte[] scanline, float[] pixels, PngHeader header)
{
int offset;
byte[] newScanline = scanline.ToArrayByBitsLength(header.BitDepth);
// We divide by 255 as we will store the colors in our floating point format.
// Stored in r-> g-> b-> a order.
if (this.useAlpha)
{
for (int x = 0; x < header.Width / 2; x++)
{
offset = ((this.row * header.Width) + x) * 4;
pixels[offset] = newScanline[x * 2] / 255f;
pixels[offset + 1] = newScanline[x * 2] / 255f;
pixels[offset + 2] = newScanline[x * 2] / 255f;
pixels[offset + 3] = newScanline[(x * 2) + 1] / 255f;
}
}
else
{
for (int x = 0; x < header.Width; x++)
{
offset = ((this.row * header.Width) + x) * 4;
pixels[offset] = newScanline[x] / 255f;
pixels[offset + 1] = newScanline[x] / 255f;
pixels[offset + 2] = newScanline[x] / 255f;
pixels[offset + 3] = 1;
}
}
this.row++;
}
}
}