Browse Source

Make ComponentOrder agnostic

Many color formats don't know what RGBA means.
af/merge-core
James Jackson-South 9 years ago
parent
commit
1b33bd67c4
  1. 8
      src/ImageSharp/Colors/Color.cs
  2. 16
      src/ImageSharp/Colors/ComponentOrder.cs
  3. 6
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  4. 4
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  5. 72
      src/ImageSharp/Image/PixelAccessor.cs
  6. 8
      src/ImageSharp/Image/PixelRow.cs
  7. 8
      src/ImageSharp/PixelAccessor.cs

8
src/ImageSharp/Colors/Color.cs

@ -242,23 +242,23 @@ namespace ImageSharp
{
switch (componentOrder)
{
case ComponentOrder.BGR:
case ComponentOrder.ZYX:
bytes[startIndex] = this.B;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.R;
break;
case ComponentOrder.BGRA:
case ComponentOrder.ZYXW:
bytes[startIndex] = this.B;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.R;
bytes[startIndex + 3] = this.A;
break;
case ComponentOrder.RGB:
case ComponentOrder.XYZ:
bytes[startIndex] = this.R;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.B;
break;
case ComponentOrder.RGBA:
case ComponentOrder.XYZW:
bytes[startIndex] = this.R;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.B;

16
src/ImageSharp/Colors/ComponentOrder.cs

@ -11,23 +11,23 @@ namespace ImageSharp
public enum ComponentOrder
{
/// <summary>
/// Blue-> Green-> Red order.
/// Z-> Y-> X order. Equivalent to B-> G-> R in <see cref="Color"/>
/// </summary>
BGR,
ZYX,
/// <summary>
/// Blue-> Green-> Red-> Alpha order.
/// Z-> Y-> X-> W order. Equivalent to B-> G-> R-> A in <see cref="Color"/>
/// </summary>
BGRA,
ZYXW,
/// <summary>
/// Red-> Green-> Blue order.
/// X-> Y-> Z order. Equivalent to R-> G-> B in <see cref="Color"/>
/// </summary>
RGB,
XYZ,
/// <summary>
/// Red-> Green-> Blue-> Alpha order.
/// X-> Y-> Z-> W order. Equivalent to R-> G-> B-> A in <see cref="Color"/>
/// </summary>
RGBA,
XYZW,
}
}

6
src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

@ -281,7 +281,7 @@ namespace ImageSharp.Formats
const int ComponentCount = 2;
TColor color = default(TColor);
using (PixelRow<TColor, TPacked> row = new PixelRow<TColor, TPacked>(width, ComponentOrder.RGB))
using (PixelRow<TColor, TPacked> row = new PixelRow<TColor, TPacked>(width, ComponentOrder.XYZ))
{
for (int y = 0; y < height; y++)
{
@ -320,7 +320,7 @@ namespace ImageSharp.Formats
where TPacked : struct
{
int padding = CalculatePadding(width, 3);
using (PixelRow<TColor, TPacked> row = new PixelRow<TColor, TPacked>(width, ComponentOrder.BGR, padding))
using (PixelRow<TColor, TPacked> row = new PixelRow<TColor, TPacked>(width, ComponentOrder.ZYX, padding))
{
for (int y = 0; y < height; y++)
{
@ -346,7 +346,7 @@ namespace ImageSharp.Formats
where TPacked : struct
{
int padding = CalculatePadding(width, 4);
using (PixelRow<TColor, TPacked> row = new PixelRow<TColor, TPacked>(width, ComponentOrder.BGRA, padding))
using (PixelRow<TColor, TPacked> row = new PixelRow<TColor, TPacked>(width, ComponentOrder.ZYXW, padding))
{
for (int y = 0; y < height; y++)
{

4
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -155,7 +155,7 @@ namespace ImageSharp.Formats
where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct
{
using (PixelRow<TColor, TPacked> row = new PixelRow<TColor, TPacked>(pixels.Width, ComponentOrder.BGRA, this.padding))
using (PixelRow<TColor, TPacked> row = new PixelRow<TColor, TPacked>(pixels.Width, ComponentOrder.ZYXW, this.padding))
{
for (int y = pixels.Height - 1; y >= 0; y--)
{
@ -176,7 +176,7 @@ namespace ImageSharp.Formats
where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct
{
using (PixelRow<TColor, TPacked> row = new PixelRow<TColor, TPacked>(pixels.Width, ComponentOrder.BGR, this.padding))
using (PixelRow<TColor, TPacked> row = new PixelRow<TColor, TPacked>(pixels.Width, ComponentOrder.ZYX, this.padding))
{
for (int y = pixels.Height - 1; y >= 0; y--)
{

72
src/ImageSharp/Image/PixelAccessor.cs

@ -148,17 +148,17 @@ namespace ImageSharp
{
switch (row.ComponentOrder)
{
case ComponentOrder.BGR:
this.CopyFromBGR(row, targetY, Math.Min(row.Width, this.Width));
case ComponentOrder.ZYX:
this.CopyFromZYX(row, targetY, Math.Min(row.Width, this.Width));
break;
case ComponentOrder.BGRA:
this.CopyFromBGRA(row, targetY, Math.Min(row.Width, this.Width));
case ComponentOrder.ZYXW:
this.CopyFromZYXW(row, targetY, Math.Min(row.Width, this.Width));
break;
case ComponentOrder.RGB:
this.CopyFromRGB(row, targetY, Math.Min(row.Width, this.Width));
case ComponentOrder.XYZ:
this.CopyFromXYZ(row, targetY, Math.Min(row.Width, this.Width));
break;
case ComponentOrder.RGBA:
this.CopyFromRGBA(row, targetY, Math.Min(row.Width, this.Width));
case ComponentOrder.XYZW:
this.CopyFromXYZW(row, targetY, Math.Min(row.Width, this.Width));
break;
default:
throw new NotSupportedException();
@ -177,17 +177,17 @@ namespace ImageSharp
{
switch (row.ComponentOrder)
{
case ComponentOrder.BGR:
this.CopyToBGR(row, sourceY, Math.Min(row.Width, this.Width));
case ComponentOrder.ZYX:
this.CopyToZYX(row, sourceY, Math.Min(row.Width, this.Width));
break;
case ComponentOrder.BGRA:
this.CopyToBGRA(row, sourceY, Math.Min(row.Width, this.Width));
case ComponentOrder.ZYXW:
this.CopyToZYXW(row, sourceY, Math.Min(row.Width, this.Width));
break;
case ComponentOrder.RGB:
this.CopyToRGB(row, sourceY, Math.Min(row.Width, this.Width));
case ComponentOrder.XYZ:
this.CopyToXYZ(row, sourceY, Math.Min(row.Width, this.Width));
break;
case ComponentOrder.RGBA:
this.CopyToRGBA(row, sourceY, Math.Min(row.Width, this.Width));
case ComponentOrder.XYZW:
this.CopyToXYZW(row, sourceY, Math.Min(row.Width, this.Width));
break;
default:
throw new NotSupportedException();
@ -224,12 +224,12 @@ namespace ImageSharp
}
/// <summary>
/// Copies from a row in <see cref="ComponentOrder.BGR"/> format.
/// Copies from a row in <see cref="ComponentOrder.ZYX"/> format.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="targetY">The target row index.</param>
/// <param name="width">The width.</param>
protected virtual void CopyFromBGR(PixelRow<TColor, TPacked> row, int targetY, int width)
protected virtual void CopyFromZYX(PixelRow<TColor, TPacked> row, int targetY, int width)
{
byte* source = row.DataPointer;
byte* destination = this.GetRowPointer(targetY);
@ -248,12 +248,12 @@ namespace ImageSharp
}
/// <summary>
/// Copies from a row in <see cref="ComponentOrder.BGRA"/> format.
/// Copies from a row in <see cref="ComponentOrder.ZYXW"/> format.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="targetY">The target row index.</param>
/// <param name="width">The width.</param>
protected virtual void CopyFromBGRA(PixelRow<TColor, TPacked> row, int targetY, int width)
protected virtual void CopyFromZYXW(PixelRow<TColor, TPacked> row, int targetY, int width)
{
byte* source = row.DataPointer;
byte* destination = this.GetRowPointer(targetY);
@ -272,12 +272,12 @@ namespace ImageSharp
}
/// <summary>
/// Copies from a row in <see cref="ComponentOrder.RGB"/> format.
/// Copies from a row in <see cref="ComponentOrder.XYZ"/> format.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="targetY">The target row index.</param>
/// <param name="width">The width.</param>
protected virtual void CopyFromRGB(PixelRow<TColor, TPacked> row, int targetY, int width)
protected virtual void CopyFromXYZ(PixelRow<TColor, TPacked> row, int targetY, int width)
{
byte* source = row.DataPointer;
byte* destination = this.GetRowPointer(targetY);
@ -296,12 +296,12 @@ namespace ImageSharp
}
/// <summary>
/// Copies from a row in <see cref="ComponentOrder.RGBA"/> format.
/// Copies from a row in <see cref="ComponentOrder.XYZW"/> format.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="targetY">The target row index.</param>
/// <param name="width">The width.</param>
protected virtual void CopyFromRGBA(PixelRow<TColor, TPacked> row, int targetY, int width)
protected virtual void CopyFromXYZW(PixelRow<TColor, TPacked> row, int targetY, int width)
{
byte* source = row.DataPointer;
byte* destination = this.GetRowPointer(targetY);
@ -320,65 +320,65 @@ namespace ImageSharp
}
/// <summary>
/// Copies to a row in <see cref="ComponentOrder.RGBA"/> format.
/// Copies to a row in <see cref="ComponentOrder.ZYX"/> format.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="sourceY">The target row index.</param>
/// <param name="width">The width.</param>
protected virtual void CopyToBGR(PixelRow<TColor, TPacked> row, int sourceY, int width)
protected virtual void CopyToZYX(PixelRow<TColor, TPacked> row, int sourceY, int width)
{
int offset = 0;
for (int x = 0; x < width; x++)
{
this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.BGR);
this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.ZYX);
offset += 3;
}
}
/// <summary>
/// Copies to a row in <see cref="ComponentOrder.BGRA"/> format.
/// Copies to a row in <see cref="ComponentOrder.ZYXW"/> format.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="sourceY">The target row index.</param>
/// <param name="width">The width.</param>
protected virtual void CopyToBGRA(PixelRow<TColor, TPacked> row, int sourceY, int width)
protected virtual void CopyToZYXW(PixelRow<TColor, TPacked> row, int sourceY, int width)
{
int offset = 0;
for (int x = 0; x < width; x++)
{
this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.BGRA);
this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.ZYXW);
offset += 4;
}
}
/// <summary>
/// Copies to a row in <see cref="ComponentOrder.RGB"/> format.
/// Copies to a row in <see cref="ComponentOrder.XYZ"/> format.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="sourceY">The target row index.</param>
/// <param name="width">The width.</param>
protected virtual void CopyToRGB(PixelRow<TColor, TPacked> row, int sourceY, int width)
protected virtual void CopyToXYZ(PixelRow<TColor, TPacked> row, int sourceY, int width)
{
int offset = 0;
for (int x = 0; x < width; x++)
{
this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.RGB);
this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.XYZ);
offset += 3;
}
}
/// <summary>
/// Copies to a row in <see cref="ComponentOrder.RGBA"/> format.
/// Copies to a row in <see cref="ComponentOrder.XYZW"/> format.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="sourceY">The target row index.</param>
/// <param name="width">The width.</param>
protected virtual void CopyToRGBA(PixelRow<TColor, TPacked> row, int sourceY, int width)
protected virtual void CopyToXYZW(PixelRow<TColor, TPacked> row, int sourceY, int width)
{
int offset = 0;
for (int x = 0; x < width; x++)
{
this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.RGBA);
this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.XYZW);
offset += 4;
}
}

8
src/ImageSharp/Image/PixelRow.cs

@ -71,11 +71,11 @@ namespace ImageSharp
{
switch (componentOrder)
{
case ComponentOrder.BGR:
case ComponentOrder.RGB:
case ComponentOrder.ZYX:
case ComponentOrder.XYZ:
return 3;
case ComponentOrder.BGRA:
case ComponentOrder.RGBA:
case ComponentOrder.ZYXW:
case ComponentOrder.XYZW:
return 4;
}

8
src/ImageSharp/PixelAccessor.cs

@ -23,7 +23,7 @@ namespace ImageSharp
}
/// <inheritdoc />
protected override void CopyFromBGR(PixelRow<Color, uint> row, int targetY, int width)
protected override void CopyFromZYX(PixelRow<Color, uint> row, int targetY, int width)
{
byte* source = row.DataPointer;
byte* destination = this.GetRowPointer(targetY);
@ -38,7 +38,7 @@ namespace ImageSharp
}
/// <inheritdoc />
protected override void CopyFromBGRA(PixelRow<Color, uint> row, int targetY, int width)
protected override void CopyFromZYXW(PixelRow<Color, uint> row, int targetY, int width)
{
byte* source = row.DataPointer;
byte* destination = this.GetRowPointer(targetY);
@ -53,7 +53,7 @@ namespace ImageSharp
}
/// <inheritdoc />
protected override void CopyToBGR(PixelRow<Color, uint> row, int sourceY, int width)
protected override void CopyToZYX(PixelRow<Color, uint> row, int sourceY, int width)
{
byte* source = this.GetRowPointer(sourceY);
byte* destination = row.DataPointer;
@ -79,7 +79,7 @@ namespace ImageSharp
}
/// <inheritdoc />
protected override void CopyToBGRA(PixelRow<Color, uint> row, int sourceY, int width)
protected override void CopyToZYXW(PixelRow<Color, uint> row, int sourceY, int width)
{
byte* source = this.GetRowPointer(sourceY);
byte* destination = row.DataPointer;

Loading…
Cancel
Save