-
-
Notifications
You must be signed in to change notification settings - Fork 893
Add support for OpenEXR image format #3096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 84 commits
9dda678
a27696a
d3993f7
aaf2143
799dafe
4cf95a5
7e7472d
652b385
70cfcc9
3279762
85501f3
69c8cd8
fdc7b61
fd0c245
88a06e8
aabe561
440408b
ea1aaa7
ce21f1a
a9620ee
ec8163a
28dcdf2
94cd5b0
d0d5e86
7748467
fa52ab7
83dba58
b78402d
faf87fb
eaea042
490d4a0
d121039
1b97d8c
e4c22d8
1abfe69
fc40209
a0eaefb
b79ba21
51f3ab2
3b7aa4d
137ebbb
3ff9d24
0d51514
614a7f6
5ee52ef
f212af8
f9df911
a3349ef
9e80da9
d409a3b
d5f4e23
7cc73d9
6a92b0c
355d1bc
c0e3d28
3e7ef68
d29fe89
9ead3eb
aa0af92
53230af
a741e84
8c0c854
c8c3656
b956634
a9f6dbc
7939cc9
a1152c5
65df7c2
0c3ca3d
e6a9980
802e275
fccd5b5
84182c5
0aeaaf7
1853289
2916204
05c89f7
d1623fe
787ebb2
3fbf0ef
0ec523f
e611b1e
24dbf63
2abbedc
fb32977
51463a3
b4cd27f
5926455
0ceeba4
29f558a
b15625e
3722997
15701e5
af14463
1e69aa4
ce545b7
d98ffac
00db2e9
0390442
758896e
6b486e3
f96d287
dd395e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Six Labors Split License. | ||
|
|
||
| using SixLabors.ImageSharp.Formats.Exr.Constants; | ||
| using SixLabors.ImageSharp.Memory; | ||
|
|
||
| namespace SixLabors.ImageSharp.Formats.Exr.Compression.Compressors; | ||
|
|
||
| internal class NoneExrCompressor : ExrBaseCompressor | ||
| { | ||
| public NoneExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow) | ||
| : base(output, allocator, bytesPerBlock, bytesPerRow) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override ExrCompression Method => ExrCompression.Zip; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, but is also not needed. I made this similar to how the TiffCompressors are defined, but currently the compression property is not used. I have removed it because of that. |
||
|
|
||
| /// <inheritdoc/> | ||
| public override uint CompressRowBlock(Span<byte> rows, int rowCount) | ||
| { | ||
| this.Output.Write(rows); | ||
| return (uint)rows.Length; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void Dispose(bool disposing) | ||
| { | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Six Labors Split License. | ||
|
|
||
| using SixLabors.ImageSharp.Compression.Zlib; | ||
| using SixLabors.ImageSharp.Formats.Exr.Constants; | ||
| using SixLabors.ImageSharp.Memory; | ||
|
|
||
| namespace SixLabors.ImageSharp.Formats.Exr.Compression.Compressors; | ||
|
|
||
| internal class ZipExrCompressor : ExrBaseCompressor | ||
| { | ||
| private readonly DeflateCompressionLevel compressionLevel; | ||
|
|
||
| private readonly MemoryStream memoryStream; | ||
|
|
||
| private readonly System.Buffers.IMemoryOwner<byte> buffer; | ||
|
|
||
| public ZipExrCompressor(Stream output, MemoryAllocator allocator, uint bytesPerBlock, uint bytesPerRow, DeflateCompressionLevel compressionLevel) | ||
| : base(output, allocator, bytesPerBlock, bytesPerRow) | ||
| { | ||
| this.compressionLevel = compressionLevel; | ||
| this.buffer = allocator.Allocate<byte>((int)bytesPerBlock); | ||
| this.memoryStream = new(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override ExrCompression Method => ExrCompression.Zip; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override uint CompressRowBlock(Span<byte> rows, int rowCount) | ||
| { | ||
| // Re-oder pixel values. | ||
| Span<byte> reordered = this.buffer.GetSpan()[..(int)(rowCount * this.BytesPerRow)]; | ||
| int n = reordered.Length; | ||
| int t1 = 0; | ||
| int t2 = (n + 1) >> 1; | ||
| for (int i = 0; i < n; i++) | ||
| { | ||
| bool isOdd = (i & 1) == 1; | ||
| reordered[isOdd ? t2++ : t1++] = rows[i]; | ||
| } | ||
|
|
||
| // Predictor. | ||
| Span<byte> predicted = reordered; | ||
| byte p = predicted[0]; | ||
| for (int i = 1; i < predicted.Length; i++) | ||
| { | ||
| int d = (predicted[i] - p + 128 + 256) & 255; | ||
| p = predicted[i]; | ||
| predicted[i] = (byte)d; | ||
| } | ||
|
|
||
| this.memoryStream.Seek(0, SeekOrigin.Begin); | ||
| using (ZlibDeflateStream stream = new(this.Allocator, this.memoryStream, this.compressionLevel)) | ||
| { | ||
| stream.Write(predicted); | ||
| stream.Flush(); | ||
| } | ||
|
|
||
| int size = (int)this.memoryStream.Position; | ||
| byte[] buffer = this.memoryStream.GetBuffer(); | ||
| this.Output.Write(buffer, 0, size); | ||
|
|
||
| // Reset memory stream for next pixel row. | ||
| this.memoryStream.Seek(0, SeekOrigin.Begin); | ||
| this.memoryStream.SetLength(0); | ||
|
|
||
| return (uint)size; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void Dispose(bool disposing) | ||
| { | ||
| this.buffer.Dispose(); | ||
| this.memoryStream?.Dispose(); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually truncation, not scaling, and introduces bias across the range. Here's a test demonstrating that vs a safer scaling operation like the one we have for
From16BitTo8Bit.The correct method would be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this, I already had a feeling that there might be an issue with this