Skip to content

Conversation

@timvw74
Copy link
Contributor

@timvw74 timvw74 commented Sep 24, 2025

This PR completes AVIF support by adding the ability to load AVIF images, It uses the avifdec tool to load and extract data from the image file.

@Spacefish Spacefish requested a review from Copilot September 24, 2025 22:57
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds AVIF image format decoding capabilities to complement existing encoding support by implementing the AVIFDecoder class that uses the external avifdec tool to decode AVIF files into standard image formats.

  • Implements AVIF image decoding using the external avifdec tool
  • Adds AVIF format detection and metadata parsing capabilities
  • Provides comprehensive test coverage for the decoder functionality

Reviewed Changes

Copilot reviewed 8 out of 11 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
test/AVIFDecoderTests.cs Test suite covering AVIF file identification, decoding, and format detection
src/Native.cs Adds reference to avifdec executable alongside existing avifenc
src/AVIFInfo.cs Data model for storing parsed AVIF metadata information
src/AVIFImageFormatDetector.cs Format detector for identifying AVIF files by header signature
src/AVIFFormat.cs Updates format class to include decoder, encoder, and singleton instance
src/AVIFEncoder.cs Updates to use renamed CAVIFENC constant
src/AVIFDecoder.cs Core decoder implementation using external avifdec tool
src/AVIFConfigurationModule.cs Configuration module registering decoder and format detector

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

[Theory]
[InlineData(AVIFTestFile, true)]
[InlineData(JPGTestFile, false)]
public void CanIdentifyAVIFFiles(string file, bool vaildAVIF)
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

Parameter name has a typo: 'vaildAVIF' should be 'validAVIF'.

Copilot uses AI. Check for mistakes.
}



Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

Remove extra blank lines to maintain consistent code formatting.

Suggested change

Copilot uses AI. Check for mistakes.

public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat format)
{
bool isAVIF = header.Length <= HeaderSize && IsAvif(header);
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

The condition should be >= not <=. A header with length less than HeaderSize (12) cannot contain a valid AVIF signature that requires checking bytes at positions 4-11.

Suggested change
bool isAVIF = header.Length <= HeaderSize && IsAvif(header);
bool isAVIF = header.Length >= HeaderSize && IsAvif(header);

Copilot uses AI. Check for mistakes.
Comment on lines +173 to +174
avifInfo.Width = int.Parse(resolutionParts[0].Trim());
avifInfo.Height = int.Parse(resolutionParts[1].Trim());
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

Using int.Parse without validation can throw exceptions on malformed input. Consider using int.TryParse and handle parsing failures gracefully.

Copilot uses AI. Check for mistakes.
}
break;
case "Bit Depth":
avifInfo.BitDepth = int.Parse(value);
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

Multiple uses of int.Parse without validation can throw exceptions on malformed input. Consider using int.TryParse and handle parsing failures gracefully for all integer parsing operations.

Copilot uses AI. Check for mistakes.
avifInfo.Format = value;
break;
case "Chroma Sam. Pos":
avifInfo.ChromaSamPos = int.Parse(value);
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

Multiple uses of int.Parse without validation can throw exceptions on malformed input. Consider using int.TryParse and handle parsing failures gracefully for all integer parsing operations.

Copilot uses AI. Check for mistakes.
avifInfo.Range = value;
break;
case "Color Primaries":
avifInfo.ColorPrimaries = int.Parse(value);
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

Multiple uses of int.Parse without validation can throw exceptions on malformed input. Consider using int.TryParse and handle parsing failures gracefully for all integer parsing operations.

Copilot uses AI. Check for mistakes.
avifInfo.ColorPrimaries = int.Parse(value);
break;
case "Transfer Char.":
avifInfo.TransferChar = int.Parse(value);
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

Multiple uses of int.Parse without validation can throw exceptions on malformed input. Consider using int.TryParse and handle parsing failures gracefully for all integer parsing operations.

Copilot uses AI. Check for mistakes.
avifInfo.TransferChar = int.Parse(value);
break;
case "Matrix Coeffs.":
avifInfo.MatrixCoeffs = int.Parse(value);
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

Multiple uses of int.Parse without validation can throw exceptions on malformed input. Consider using int.TryParse and handle parsing failures gracefully for all integer parsing operations.

Copilot uses AI. Check for mistakes.
Comment on lines +80 to +81
File.Delete(inputFilePath);
File.Delete(outputFilePath);
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

File deletion operations can throw exceptions if files are in use or access is denied. Wrap these calls in try-catch blocks to prevent cleanup failures from propagating.

Suggested change
File.Delete(inputFilePath);
File.Delete(outputFilePath);
try
{
File.Delete(inputFilePath);
}
catch
{
// Suppress any exceptions during cleanup
}
try
{
File.Delete(outputFilePath);
}
catch
{
// Suppress any exceptions during cleanup
}

Copilot uses AI. Check for mistakes.
@Spacefish
Copy link
Owner

LGTM

@Spacefish Spacefish merged commit 69bf150 into Spacefish:master Sep 24, 2025
1 check failed
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.

2 participants