forked from JimBobSquarePants/ImageProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract jpeg component classes [skip ci]
- Loading branch information
1 parent
638883c
commit 89550ed
Showing
11 changed files
with
880 additions
and
619 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// <copyright file="Bits.cs" company="James Jackson-South"> | ||
// Copyright (c) James Jackson-South and contributors. | ||
// Licensed under the Apache License, Version 2.0. | ||
// </copyright> | ||
|
||
namespace ImageProcessorCore.Formats | ||
{ | ||
/// <summary> | ||
/// Holds the unprocessed bits that have been taken from the byte-stream. | ||
/// The n least significant bits of a form the unread bits, to be read in MSB to | ||
/// LSB order. | ||
/// </summary> | ||
internal class Bits | ||
{ | ||
/// <summary> | ||
/// Gets or sets the accumulator. | ||
/// </summary> | ||
public uint Accumulator { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the mask. | ||
/// <![CDATA[mask==1<<(unreadbits-1) when unreadbits>0, with mask==0 when unreadbits==0.]]> | ||
/// </summary> | ||
public uint Mask { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the number of unread bits in the accumulator. | ||
/// </summary> | ||
public int UnreadBits { get; set; } | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// <copyright file="Bytes.cs" company="James Jackson-South"> | ||
// Copyright (c) James Jackson-South and contributors. | ||
// Licensed under the Apache License, Version 2.0. | ||
// </copyright> | ||
|
||
namespace ImageProcessorCore.Formats | ||
{ | ||
/// <summary> | ||
/// Bytes is a byte buffer, similar to a stream, except that it | ||
/// has to be able to unread more than 1 byte, due to byte stuffing. | ||
/// Byte stuffing is specified in section F.1.2.3. | ||
/// </summary> | ||
internal class Bytes | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Bytes"/> class. | ||
/// </summary> | ||
public Bytes() | ||
{ | ||
this.Buffer = new byte[4096]; | ||
this.I = 0; | ||
this.J = 0; | ||
this.UnreadableBytes = 0; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the buffer. | ||
/// buffer[i:j] are the buffered bytes read from the underlying | ||
/// stream that haven't yet been passed further on. | ||
/// </summary> | ||
public byte[] Buffer { get; set; } | ||
|
||
public int I { get; set; } | ||
|
||
public int J { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the unreadable bytes. The number of bytes to back up i after | ||
/// overshooting. It can be 0, 1 or 2. | ||
/// </summary> | ||
public int UnreadableBytes { get; set; } | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/ImageProcessorCore/Formats/Jpg/Components/Component.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// <copyright file="Component.cs" company="James Jackson-South"> | ||
// Copyright (c) James Jackson-South and contributors. | ||
// Licensed under the Apache License, Version 2.0. | ||
// </copyright> | ||
|
||
namespace ImageProcessorCore.Formats | ||
{ | ||
/// <summary> | ||
/// Represents a single color component | ||
/// </summary> | ||
internal class Component | ||
{ | ||
/// <summary> | ||
/// Gets or sets the horizontal sampling factor. | ||
/// </summary> | ||
public int HorizontalFactor { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the vertical sampling factor. | ||
/// </summary> | ||
public int VerticalFactor { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the identifier | ||
/// </summary> | ||
public byte Identifier { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the quantization table destination selector. | ||
/// </summary> | ||
public byte Selector { get; set; } | ||
} | ||
} |
File renamed without changes.
101 changes: 101 additions & 0 deletions
101
src/ImageProcessorCore/Formats/Jpg/Components/GrayImage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// <copyright file="GrayImage.cs" company="James Jackson-South"> | ||
// Copyright (c) James Jackson-South and contributors. | ||
// Licensed under the Apache License, Version 2.0. | ||
// </copyright> | ||
|
||
namespace ImageProcessorCore.Formats | ||
{ | ||
/// <summary> | ||
/// Represents a grayscale image | ||
/// </summary> | ||
internal class GrayImage | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="GrayImage"/> class. | ||
/// </summary> | ||
/// <param name="width">The width.</param> | ||
/// <param name="height">The height.</param> | ||
public GrayImage(int width, int height) | ||
{ | ||
this.Width = width; | ||
this.Height = height; | ||
this.Pixels = new byte[width * height]; | ||
this.Stride = width; | ||
this.Offset = 0; | ||
} | ||
|
||
/// <summary> | ||
/// Prevents a default instance of the <see cref="GrayImage"/> class from being created. | ||
/// </summary> | ||
private GrayImage() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the pixels. | ||
/// </summary> | ||
public byte[] Pixels { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the stride. | ||
/// </summary> | ||
public int Stride { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the horizontal position. | ||
/// </summary> | ||
public int X { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the vertical position. | ||
/// </summary> | ||
public int Y { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the width. | ||
/// </summary> | ||
public int Width { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the height. | ||
/// </summary> | ||
public int Height { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the offset | ||
/// </summary> | ||
public int Offset { get; set; } | ||
|
||
/// <summary> | ||
/// Gets an image made up of a subset of the originals pixels. | ||
/// </summary> | ||
/// <param name="x">The x-coordinate of the image.</param> | ||
/// <param name="y">The y-coordinate of the image.</param> | ||
/// <param name="width">The width.</param> | ||
/// <param name="height">The height.</param> | ||
/// <returns> | ||
/// The <see cref="GrayImage"/>. | ||
/// </returns> | ||
public GrayImage Subimage(int x, int y, int width, int height) | ||
{ | ||
return new GrayImage | ||
{ | ||
Width = width, | ||
Height = height, | ||
Pixels = this.Pixels, | ||
Stride = this.Stride, | ||
Offset = (y * this.Stride) + x | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the row offset at the given position | ||
/// </summary> | ||
/// <param name="y">The y-coordinate of the image.</param> | ||
/// <returns>The <see cref="int"/></returns> | ||
public int GetRowOffset(int y) | ||
{ | ||
return this.Offset + (y * this.Stride); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// <copyright file="Huffman.cs" company="James Jackson-South"> | ||
// Copyright (c) James Jackson-South and contributors. | ||
// Licensed under the Apache License, Version 2.0. | ||
// </copyright> | ||
|
||
namespace ImageProcessorCore.Formats | ||
{ | ||
/// <summary> | ||
/// Represents a Huffman tree | ||
/// </summary> | ||
internal class Huffman | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Huffman"/> class. | ||
/// </summary> | ||
/// <param name="lutSize">The log-2 size of the Huffman decoder's look-up table.</param> | ||
/// <param name="maxNCodes">The maximum (inclusive) number of codes in a Huffman tree.</param> | ||
/// <param name="maxCodeLength">The maximum (inclusive) number of bits in a Huffman code.</param> | ||
public Huffman(int lutSize, int maxNCodes, int maxCodeLength) | ||
{ | ||
this.Lut = new ushort[1 << lutSize]; | ||
this.Values = new byte[maxNCodes]; | ||
this.MinCodes = new int[maxCodeLength]; | ||
this.MaxCodes = new int[maxCodeLength]; | ||
this.Indices = new int[maxCodeLength]; | ||
this.Length = 0; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the number of codes in the tree. | ||
/// </summary> | ||
public int Length { get; set; } | ||
|
||
/// <summary> | ||
/// Gets the look-up table for the next LutSize bits in the bit-stream. | ||
/// The high 8 bits of the uint16 are the encoded value. The low 8 bits | ||
/// are 1 plus the code length, or 0 if the value is too large to fit in | ||
/// lutSize bits. | ||
/// </summary> | ||
public ushort[] Lut { get; } | ||
|
||
/// <summary> | ||
/// Gets the the decoded values, sorted by their encoding. | ||
/// </summary> | ||
public byte[] Values { get; } | ||
|
||
/// <summary> | ||
/// Gets the array of minimum codes. | ||
/// MinCodes[i] is the minimum code of length i, or -1 if there are no codes of that length. | ||
/// </summary> | ||
public int[] MinCodes { get; } | ||
|
||
/// <summary> | ||
/// Gets the array of maximum codes. | ||
/// MaxCodes[i] is the maximum code of length i, or -1 if there are no codes of that length. | ||
/// </summary> | ||
public int[] MaxCodes { get; } | ||
|
||
/// <summary> | ||
/// Gets the array of indices. Indices[i] is the index into Values of MinCodes[i]. | ||
/// </summary> | ||
public int[] Indices { get; } | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.