Skip to content
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

Add support for HEIF based images #4

Open
wants to merge 188 commits into
base: main
Choose a base branch
from
Open

Add support for HEIF based images #4

wants to merge 188 commits into from

Conversation

lizard-boy
Copy link

@lizard-boy lizard-boy commented Nov 1, 2024

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

This PR is an attempt to start with SixLabors#1320

Implemented decoding and encoding of images based on a HEIF (ISO/IEC 23008-12:2022) container. These include (amongst others): HEIC, HIF and AVIF.

Please note this PR does NOT add any new compression algorithm to ImageSharp. For now it will take a JPEG thumbnail as its pixel source.
Needless to say, much more work is needed after this PR to reach the goal. I'm try to help there also.

Please do let me know any comments, also on whether to support the HEIF file format in the first place.

Greptile Summary

This PR adds initial support for HEIF-based image formats (HEIC, HIF, AVIF) by implementing AV1 codec components and HEIF container handling.

  • Added AV1 codec implementation in src/ImageSharp/Formats/Heif/Av1/ including bitstream handling, frame buffers, and color format support
  • Added Git LFS configuration for .heic, .hif, and .avif file formats in .gitattributes
  • Integrated HEIF format support into Configuration.cs via HeifConfigurationModule
  • Added test images and infrastructure for HEIF format validation
  • Note: Currently uses JPEG thumbnails as pixel source rather than full HEIF/HEIC compression algorithms

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

30 file(s) reviewed, 62 comment(s)
Edit PR Review Bot Settings | Greptile

// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using SixLabors.ImageSharp.Formats.Heif.Av1.Transform;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Transform namespace is imported but not used


public void Reset() => this.BitPosition = 0;

public void Skip(int bitCount) => this.BitPosition += bitCount;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: no bounds checking on Skip() - could advance beyond buffer end

Comment on lines +38 to +41
int byteOffset = Av1Math.DivideBy8Floor(this.BitPosition);
byte shift = (byte)(7 - Av1Math.Modulus8(this.BitPosition));
this.BitPosition++;
return (uint)((this.data[byteOffset] >> shift) & 0x01);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: ReadBit() needs bounds check on byteOffset against data.Length


ulong value = 0;
length = 0;
for (int i = 0; i < 56; i += 7)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: magic number 56 should be defined as constant with descriptive name

DebugGuard.IsTrue(Av1Math.Modulus8(this.BitPosition) == 0, "Reading of Little Endian value only allowed on byte alignment");

uint t = 0;
for (int i = 0; i < 8 * n; i += 8)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: potential integer overflow if n > int.MaxValue/8


namespace SixLabors.ImageSharp.Formats.Heif.Av1.OpenBitstreamUnit;

internal class ObuFrameHeader
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: class should be sealed since it's not designed for inheritance


namespace SixLabors.ImageSharp.Formats.Heif.Av1.OpenBitstreamUnit;

internal class ObuFrameSize
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider making this class sealed since it's not designed for inheritance and is internal

Comment on lines +8 to +10
internal int FrameWidth { get; set; }

internal int FrameHeight { get; set; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Consider adding validation to ensure width/height are non-negative


internal int FrameHeight { get; set; }

internal int SuperResolutionDenominator { get; set; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: SuperResolutionDenominator should be greater than or equal to 9 per AV1 spec

Comment on lines +6 to +19
internal class ObuFrameSize
{
internal int FrameWidth { get; set; }

internal int FrameHeight { get; set; }

internal int SuperResolutionDenominator { get; set; }

internal int SuperResolutionUpscaledWidth { get; set; }

internal int RenderWidth { get; set; }

internal int RenderHeight { get; set; }
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: XML documentation missing for class and properties to explain their purpose and constraints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants