-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Png working, needs some refinement and additional work for correctnes…
…s in all color profiles.
- Loading branch information
1 parent
a2f5561
commit f38817e
Showing
124 changed files
with
577 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Media.Common; | ||
|
||
namespace Codec.Png; | ||
|
||
public class Chunk : MemorySegment | ||
{ | ||
public Chunk(byte[] array, int offset) | ||
: base(array, offset) | ||
{ | ||
} | ||
|
||
public Chunk(string chunkType, int chunkSize) | ||
: base(new MemorySegment(ChunkHeader.ChunkHeaderLength + Binary.BytesPerInteger + chunkSize)) | ||
{ | ||
ChunkType = chunkType; | ||
ChunkSize = chunkSize; | ||
} | ||
|
||
public ChunkHeader Header => new ChunkHeader(Array, Offset); | ||
|
||
public int ChunkSize | ||
{ | ||
get { return (int)Header.Length; } | ||
set { Header.Length = (uint)value; } | ||
} | ||
|
||
public string ChunkType | ||
{ | ||
get { return Header.Name; } | ||
set { Header.Name = value; } | ||
} | ||
|
||
public MemorySegment Data => new MemorySegment(Array, Offset + ChunkHeader.ChunkHeaderLength, (int)Header.Length); | ||
|
||
public int Crc | ||
{ | ||
get { return Binary.Read32(Array, Offset + ChunkHeader.ChunkHeaderLength + ChunkSize, Binary.IsBigEndian); } | ||
set { Binary.Write32(Array, Offset + ChunkHeader.ChunkHeaderLength + ChunkSize, Binary.IsBigEndian, value); } | ||
} | ||
|
||
public MemorySegment CrcData => new(Array, Offset + ChunkHeader.ChunkHeaderLength + ChunkSize, Binary.BytesPerInteger); | ||
|
||
internal static Chunk ReadChunk(Stream inputStream) | ||
{ | ||
ChunkHeader header = new ChunkHeader(); | ||
if (ChunkHeader.ChunkHeaderLength != inputStream.Read(header.Array, header.Offset, ChunkHeader.ChunkHeaderLength)) | ||
throw new InvalidDataException("Not enough bytes for chunk length."); | ||
var chunk = new Chunk(header.Name, (int)header.Length); | ||
if (header.Length != inputStream.Read(chunk.Data.Array, chunk.Data.Offset, (int)header.Length)) | ||
throw new InvalidDataException("Not enough bytes for chunk data."); | ||
if (Binary.BytesPerInteger != inputStream.Read(chunk.CrcData.Array, chunk.CrcData.Offset, chunk.CrcData.Count)) | ||
throw new InvalidDataException("Not enough bytes for CrcData."); | ||
return chunk; | ||
} | ||
} |
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,55 @@ | ||
using Media.Common; | ||
using System.Text; | ||
|
||
namespace Codec.Png; | ||
|
||
public class ChunkHeader : MemorySegment | ||
{ | ||
public const int ChunkHeaderLength = 8; | ||
|
||
public ChunkHeader() | ||
: this(new byte[ChunkHeaderLength], 0) | ||
{ | ||
} | ||
|
||
public ChunkHeader(byte[] array, int offset) | ||
: base(array, offset, ChunkHeaderLength) | ||
{ | ||
} | ||
|
||
public uint Length | ||
{ | ||
get => Binary.ReadU32(Array, Offset, Binary.IsLittleEndian); | ||
set => Binary.Write32(Array, Offset, Binary.IsLittleEndian, value); | ||
} | ||
|
||
public uint Type | ||
{ | ||
get => Binary.ReadU32(Array, Offset + Binary.BytesPerInteger, Binary.IsLittleEndian); | ||
set => Binary.Write32(Array, Offset + Binary.BytesPerInteger, Binary.IsLittleEndian, value); | ||
} | ||
|
||
public string Name | ||
{ | ||
get { return Encoding.ASCII.GetString(Array, Offset + Binary.BytesPerInteger, Binary.BytesPerInteger); } | ||
set { Encoding.ASCII.GetBytes(value, 0, Binary.BytesPerInteger, Array, Offset + Binary.BytesPerInteger); } | ||
} | ||
|
||
/// <summary> | ||
/// Whether the chunk is critical (must be read by all readers) or ancillary (may be ignored). | ||
/// </summary> | ||
public bool IsCritical => char.IsUpper(Name[0]); | ||
|
||
/// <summary> | ||
/// A public chunk is one that is defined in the International Standard or is registered in the list of public chunk types maintained by the Registration Authority. | ||
/// Applications can also define private (unregistered) chunk types for their own purposes. | ||
/// </summary> | ||
public bool IsPublic => char.IsUpper(Name[1]); | ||
|
||
public bool IsPrivate => char.IsUpper(Name[2]); | ||
|
||
/// <summary> | ||
/// Whether the (if unrecognized) chunk is safe to copy. | ||
/// </summary> | ||
public bool IsSafeToCopy => char.IsUpper(Name[3]); | ||
} |
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
Oops, something went wrong.