Browse Source

store scale as vector + remove unneeded bit or operations

`var t = blah | 0;` in js is for trimming floats into ints c# doesn't need it
af/merge-core
Scott Williams 9 years ago
parent
commit
83412932d5
  1. 11
      src/ImageSharp/Formats/Jpeg/Port/Components/Component.cs
  2. 11
      src/ImageSharp/Formats/Jpeg/Port/Components/JpegPixelArea.cs
  3. 20
      src/ImageSharp/Formats/Jpeg/Port/Components/ScanDecoder.cs
  4. 5
      src/ImageSharp/Formats/Jpeg/Port/JpegDecoderCore.cs

11
src/ImageSharp/Formats/Jpeg/Port/Components/Component.cs

@ -6,7 +6,7 @@
namespace ImageSharp.Formats.Jpeg.Port.Components namespace ImageSharp.Formats.Jpeg.Port.Components
{ {
using System; using System;
using System.Numerics;
using ImageSharp.Memory; using ImageSharp.Memory;
/// <summary> /// <summary>
@ -20,14 +20,9 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
public Buffer<short> Output; public Buffer<short> Output;
/// <summary> /// <summary>
/// Gets or sets the horizontal scaling factor /// Gets or sets the scaling factors
/// </summary>
public float ScaleX;
/// <summary>
/// Gets or sets the vertical scaling factor
/// </summary> /// </summary>
public float ScaleY; public Vector2 Scale;
/// <summary> /// <summary>
/// Gets or sets the number of blocks per line /// Gets or sets the number of blocks per line

11
src/ImageSharp/Formats/Jpeg/Port/Components/JpegPixelArea.cs

@ -7,6 +7,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
{ {
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using ImageSharp.Memory; using ImageSharp.Memory;
@ -68,9 +69,8 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
this.Height = height; this.Height = height;
int numberOfComponents = this.NumberOfComponents; int numberOfComponents = this.NumberOfComponents;
this.rowStride = width * numberOfComponents; this.rowStride = width * numberOfComponents;
var scale = new Vector2(this.imageWidth / (float)width, this.imageHeight / (float)height);
float scaleX = this.imageWidth / (float)width;
float scaleY = this.imageHeight / (float)height;
this.componentData = new Buffer<byte>(width * height * numberOfComponents); this.componentData = new Buffer<byte>(width * height * numberOfComponents);
Span<byte> componentDataSpan = this.componentData; Span<byte> componentDataSpan = this.componentData;
const uint Mask3Lsb = 0xFFFFFFF8; // Used to clear the 3 LSBs const uint Mask3Lsb = 0xFFFFFFF8; // Used to clear the 3 LSBs
@ -81,8 +81,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
for (int i = 0; i < numberOfComponents; i++) for (int i = 0; i < numberOfComponents; i++)
{ {
ref Component component = ref components.Components[i]; ref Component component = ref components.Components[i];
float componentScaleX = component.ScaleX * scaleX; Vector2 componentScale = component.Scale * scale;
float componentScaleY = component.ScaleY * scaleY;
int offset = i; int offset = i;
Span<short> output = component.Output; Span<short> output = component.Output;
int blocksPerScanline = (component.BlocksPerLine + 1) << 3; int blocksPerScanline = (component.BlocksPerLine + 1) << 3;
@ -91,14 +90,14 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
int j; int j;
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
j = 0 | (int)(x * componentScaleX); j = (int)(x * componentScale.X);
xScaleBlockOffsetSpan[x] = (int)((j & Mask3Lsb) << 3) | (j & 7); xScaleBlockOffsetSpan[x] = (int)((j & Mask3Lsb) << 3) | (j & 7);
} }
// Linearize the blocks of the component // Linearize the blocks of the component
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
j = 0 | (int)(y * componentScaleY); j = (int)(y * componentScale.Y);
int index = blocksPerScanline * (int)(j & Mask3Lsb) | ((j & 7) << 3); int index = blocksPerScanline * (int)(j & Mask3Lsb) | ((j & 7) << 3);
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {

20
src/ImageSharp/Formats/Jpeg/Port/Components/ScanDecoder.cs

@ -478,7 +478,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DecodeBlockBaseline(ref HuffmanTable dcHuffmanTable, ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcu, Stream stream) private void DecodeBlockBaseline(ref HuffmanTable dcHuffmanTable, ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcu, Stream stream)
{ {
int blockRow = (mcu / component.BlocksPerLine) | 0; int blockRow = mcu / component.BlocksPerLine;
int blockCol = mcu % component.BlocksPerLine; int blockCol = mcu % component.BlocksPerLine;
int offset = GetBlockBufferOffset(component, blockRow, blockCol); int offset = GetBlockBufferOffset(component, blockRow, blockCol);
this.DecodeBaseline(ref component, offset, ref dcHuffmanTable, ref acHuffmanTable, stream); this.DecodeBaseline(ref component, offset, ref dcHuffmanTable, ref acHuffmanTable, stream);
@ -487,7 +487,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DecodeMcuBaseline(ref HuffmanTable dcHuffmanTable, ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcusPerLine, int mcu, int row, int col, Stream stream) private void DecodeMcuBaseline(ref HuffmanTable dcHuffmanTable, ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcusPerLine, int mcu, int row, int col, Stream stream)
{ {
int mcuRow = (mcu / mcusPerLine) | 0; int mcuRow = mcu / mcusPerLine;
int mcuCol = mcu % mcusPerLine; int mcuCol = mcu % mcusPerLine;
int blockRow = (mcuRow * component.VerticalFactor) + row; int blockRow = (mcuRow * component.VerticalFactor) + row;
int blockCol = (mcuCol * component.HorizontalFactor) + col; int blockCol = (mcuCol * component.HorizontalFactor) + col;
@ -498,7 +498,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DecodeBlockDCFirst(ref HuffmanTable dcHuffmanTable, ref FrameComponent component, int mcu, Stream stream) private void DecodeBlockDCFirst(ref HuffmanTable dcHuffmanTable, ref FrameComponent component, int mcu, Stream stream)
{ {
int blockRow = (mcu / component.BlocksPerLine) | 0; int blockRow = mcu / component.BlocksPerLine;
int blockCol = mcu % component.BlocksPerLine; int blockCol = mcu % component.BlocksPerLine;
int offset = GetBlockBufferOffset(component, blockRow, blockCol); int offset = GetBlockBufferOffset(component, blockRow, blockCol);
this.DecodeDCFirst(ref component, offset, ref dcHuffmanTable, stream); this.DecodeDCFirst(ref component, offset, ref dcHuffmanTable, stream);
@ -507,7 +507,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DecodeMcuDCFirst(ref HuffmanTable dcHuffmanTable, ref FrameComponent component, int mcusPerLine, int mcu, int row, int col, Stream stream) private void DecodeMcuDCFirst(ref HuffmanTable dcHuffmanTable, ref FrameComponent component, int mcusPerLine, int mcu, int row, int col, Stream stream)
{ {
int mcuRow = (mcu / mcusPerLine) | 0; int mcuRow = mcu / mcusPerLine;
int mcuCol = mcu % mcusPerLine; int mcuCol = mcu % mcusPerLine;
int blockRow = (mcuRow * component.VerticalFactor) + row; int blockRow = (mcuRow * component.VerticalFactor) + row;
int blockCol = (mcuCol * component.HorizontalFactor) + col; int blockCol = (mcuCol * component.HorizontalFactor) + col;
@ -518,7 +518,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DecodeBlockDCSuccessive(ref FrameComponent component, int mcu, Stream stream) private void DecodeBlockDCSuccessive(ref FrameComponent component, int mcu, Stream stream)
{ {
int blockRow = (mcu / component.BlocksPerLine) | 0; int blockRow = mcu / component.BlocksPerLine;
int blockCol = mcu % component.BlocksPerLine; int blockCol = mcu % component.BlocksPerLine;
int offset = GetBlockBufferOffset(component, blockRow, blockCol); int offset = GetBlockBufferOffset(component, blockRow, blockCol);
this.DecodeDCSuccessive(ref component, offset, stream); this.DecodeDCSuccessive(ref component, offset, stream);
@ -527,7 +527,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DecodeMcuDCSuccessive(ref FrameComponent component, int mcusPerLine, int mcu, int row, int col, Stream stream) private void DecodeMcuDCSuccessive(ref FrameComponent component, int mcusPerLine, int mcu, int row, int col, Stream stream)
{ {
int mcuRow = (mcu / mcusPerLine) | 0; int mcuRow = mcu / mcusPerLine;
int mcuCol = mcu % mcusPerLine; int mcuCol = mcu % mcusPerLine;
int blockRow = (mcuRow * component.VerticalFactor) + row; int blockRow = (mcuRow * component.VerticalFactor) + row;
int blockCol = (mcuCol * component.HorizontalFactor) + col; int blockCol = (mcuCol * component.HorizontalFactor) + col;
@ -538,7 +538,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DecodeBlockACFirst(ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcu, Stream stream) private void DecodeBlockACFirst(ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcu, Stream stream)
{ {
int blockRow = (mcu / component.BlocksPerLine) | 0; int blockRow = mcu / component.BlocksPerLine;
int blockCol = mcu % component.BlocksPerLine; int blockCol = mcu % component.BlocksPerLine;
int offset = GetBlockBufferOffset(component, blockRow, blockCol); int offset = GetBlockBufferOffset(component, blockRow, blockCol);
this.DecodeACFirst(ref component, offset, ref acHuffmanTable, stream); this.DecodeACFirst(ref component, offset, ref acHuffmanTable, stream);
@ -547,7 +547,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DecodeMcuACFirst(ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcusPerLine, int mcu, int row, int col, Stream stream) private void DecodeMcuACFirst(ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcusPerLine, int mcu, int row, int col, Stream stream)
{ {
int mcuRow = (mcu / mcusPerLine) | 0; int mcuRow = mcu / mcusPerLine;
int mcuCol = mcu % mcusPerLine; int mcuCol = mcu % mcusPerLine;
int blockRow = (mcuRow * component.VerticalFactor) + row; int blockRow = (mcuRow * component.VerticalFactor) + row;
int blockCol = (mcuCol * component.HorizontalFactor) + col; int blockCol = (mcuCol * component.HorizontalFactor) + col;
@ -558,7 +558,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DecodeBlockACSuccessive(ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcu, Stream stream) private void DecodeBlockACSuccessive(ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcu, Stream stream)
{ {
int blockRow = (mcu / component.BlocksPerLine) | 0; int blockRow = mcu / component.BlocksPerLine;
int blockCol = mcu % component.BlocksPerLine; int blockCol = mcu % component.BlocksPerLine;
int offset = GetBlockBufferOffset(component, blockRow, blockCol); int offset = GetBlockBufferOffset(component, blockRow, blockCol);
this.DecodeACSuccessive(ref component, offset, ref acHuffmanTable, stream); this.DecodeACSuccessive(ref component, offset, ref acHuffmanTable, stream);
@ -567,7 +567,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DecodeMcuACSuccessive(ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcusPerLine, int mcu, int row, int col, Stream stream) private void DecodeMcuACSuccessive(ref HuffmanTable acHuffmanTable, ref FrameComponent component, int mcusPerLine, int mcu, int row, int col, Stream stream)
{ {
int mcuRow = (mcu / mcusPerLine) | 0; int mcuRow = mcu / mcusPerLine;
int mcuCol = mcu % mcusPerLine; int mcuCol = mcu % mcusPerLine;
int blockRow = (mcuRow * component.VerticalFactor) + row; int blockRow = (mcuRow * component.VerticalFactor) + row;
int blockCol = (mcuCol * component.HorizontalFactor) + col; int blockCol = (mcuCol * component.HorizontalFactor) + col;

5
src/ImageSharp/Formats/Jpeg/Port/JpegDecoderCore.cs

@ -277,8 +277,9 @@ namespace ImageSharp.Formats.Jpeg.Port
ref var frameComponent = ref this.frame.Components[i]; ref var frameComponent = ref this.frame.Components[i];
var component = new Component var component = new Component
{ {
ScaleX = frameComponent.HorizontalFactor / (float)this.frame.MaxHorizontalFactor, Scale = new System.Numerics.Vector2(
ScaleY = frameComponent.VerticalFactor / (float)this.frame.MaxVerticalFactor, frameComponent.HorizontalFactor / (float)this.frame.MaxHorizontalFactor,
frameComponent.VerticalFactor / (float)this.frame.MaxVerticalFactor),
BlocksPerLine = frameComponent.BlocksPerLine, BlocksPerLine = frameComponent.BlocksPerLine,
BlocksPerColumn = frameComponent.BlocksPerColumn BlocksPerColumn = frameComponent.BlocksPerColumn
}; };

Loading…
Cancel
Save