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
pull/298/head
Scott Williams 9 years ago
parent
commit
290646afcb
  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
{
using System;
using System.Numerics;
using ImageSharp.Memory;
/// <summary>
@ -20,14 +20,9 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
public Buffer<short> Output;
/// <summary>
/// Gets or sets the horizontal scaling factor
/// </summary>
public float ScaleX;
/// <summary>
/// Gets or sets the vertical scaling factor
/// Gets or sets the scaling factors
/// </summary>
public float ScaleY;
public Vector2 Scale;
/// <summary>
/// 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.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using ImageSharp.Memory;
@ -68,9 +69,8 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
this.Height = height;
int numberOfComponents = this.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);
Span<byte> componentDataSpan = this.componentData;
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++)
{
ref Component component = ref components.Components[i];
float componentScaleX = component.ScaleX * scaleX;
float componentScaleY = component.ScaleY * scaleY;
Vector2 componentScale = component.Scale * scale;
int offset = i;
Span<short> output = component.Output;
int blocksPerScanline = (component.BlocksPerLine + 1) << 3;
@ -91,14 +90,14 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
int j;
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);
}
// Linearize the blocks of the component
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);
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)]
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 offset = GetBlockBufferOffset(component, blockRow, blockCol);
this.DecodeBaseline(ref component, offset, ref dcHuffmanTable, ref acHuffmanTable, stream);
@ -487,7 +487,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[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)
{
int mcuRow = (mcu / mcusPerLine) | 0;
int mcuRow = mcu / mcusPerLine;
int mcuCol = mcu % mcusPerLine;
int blockRow = (mcuRow * component.VerticalFactor) + row;
int blockCol = (mcuCol * component.HorizontalFactor) + col;
@ -498,7 +498,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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 offset = GetBlockBufferOffset(component, blockRow, blockCol);
this.DecodeDCFirst(ref component, offset, ref dcHuffmanTable, stream);
@ -507,7 +507,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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 blockRow = (mcuRow * component.VerticalFactor) + row;
int blockCol = (mcuCol * component.HorizontalFactor) + col;
@ -518,7 +518,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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 offset = GetBlockBufferOffset(component, blockRow, blockCol);
this.DecodeDCSuccessive(ref component, offset, stream);
@ -527,7 +527,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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 blockRow = (mcuRow * component.VerticalFactor) + row;
int blockCol = (mcuCol * component.HorizontalFactor) + col;
@ -538,7 +538,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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 offset = GetBlockBufferOffset(component, blockRow, blockCol);
this.DecodeACFirst(ref component, offset, ref acHuffmanTable, stream);
@ -547,7 +547,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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 blockRow = (mcuRow * component.VerticalFactor) + row;
int blockCol = (mcuCol * component.HorizontalFactor) + col;
@ -558,7 +558,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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 offset = GetBlockBufferOffset(component, blockRow, blockCol);
this.DecodeACSuccessive(ref component, offset, ref acHuffmanTable, stream);
@ -567,7 +567,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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 blockRow = (mcuRow * component.VerticalFactor) + row;
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];
var component = new Component
{
ScaleX = frameComponent.HorizontalFactor / (float)this.frame.MaxHorizontalFactor,
ScaleY = frameComponent.VerticalFactor / (float)this.frame.MaxVerticalFactor,
Scale = new System.Numerics.Vector2(
frameComponent.HorizontalFactor / (float)this.frame.MaxHorizontalFactor,
frameComponent.VerticalFactor / (float)this.frame.MaxVerticalFactor),
BlocksPerLine = frameComponent.BlocksPerLine,
BlocksPerColumn = frameComponent.BlocksPerColumn
};

Loading…
Cancel
Save