Skip to content

ArgumentNullException thrown when resizing jpg image #2087

@saintarian

Description

@saintarian

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

2.1.0

Other ImageSharp packages and versions

None

Environment (Operating system, version and so on)

MacOS 12.1 on M1 chip

.NET Framework version

6.0

Description

Running the xunit test below produces the following exception:

[xUnit.net 00:00:00.93]     ImageSharpResizeTest.TestExifIssue [FAIL]
  Failed ImageSharpResizeTest.TestExifIssue [454 ms]
  Error Message:
   System.ArgumentNullException : Array cannot be null. (Parameter 'bytes')
  Stack Trace:
     at System.Text.UTF8Encoding.GetBytes(Char* chars, Int32 charCount, Byte* bytes, Int32 byteCount)
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifEncodedStringHelpers.Write(Encoding encoding, String value, Span`1 destination)
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifEncodedStringHelpers.Write(EncodedString encodedString, Span`1 destination)
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifWriter.WriteValue(IExifValue exifValue, Span`1 destination, Int32 offset)
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifWriter.WriteData(UInt32 startIndex, List`1 values, Span`1 destination, Int32 offset)
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifWriter.GetData()
   at SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifProfile.ToByteArray()
   at SixLabors.ImageSharp.Formats.Jpeg.JpegEncoderCore.WriteExifProfile(ExifProfile exifProfile)
   at SixLabors.ImageSharp.Formats.Jpeg.JpegEncoderCore.WriteProfiles(ImageMetadata metadata)
   at SixLabors.ImageSharp.Formats.Jpeg.JpegEncoderCore.Encode[TPixel](Image`1 image, Stream stream, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.ImageEncoderUtilities.<EncodeAsync>g__DoEncodeAsync|0_0[TPixel](Stream innerStream, <>c__DisplayClass0_0`1& )
--- End of stack trace from previous location ---
   at SixLabors.ImageSharp.Formats.ImageEncoderUtilities.EncodeAsync[TPixel](IImageEncoderInternals encoder, Image`1 image, Stream stream, CancellationToken cancellationToken)
   at ImageSharpResizeTest.TestExifIssue() 

Output of magick identify -verbose exif-issue.jpg:

Image:
  Filename: exif-issue.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 172x250+0+0
  Units: Undefined
  Colorspace: sRGB
  Type: TrueColor
  Base type: Undefined
  Endianness: Undefined
  Depth: 8-bit
  Channel depth:
    Red: 8-bit
    Green: 8-bit
    Blue: 8-bit
  Channel statistics:
    Pixels: 43000
    Red:
      min: 0  (0)
      max: 255 (1)
      mean: 54.5425 (0.213892)
      median: 0 (0)
      standard deviation: 81.4001 (0.319216)
      kurtosis: 0.276704
      skewness: 1.31375
      entropy: 0.619689
    Green:
      min: 0  (0)
      max: 255 (1)
      mean: 62.8643 (0.246526)
      median: 0 (0)
      standard deviation: 78.2441 (0.30684)
      kurtosis: -1.00418
      skewness: 0.744749
      entropy: 0.627227
    Blue:
      min: 0  (0)
      max: 255 (1)
      mean: 34.6892 (0.136036)
      median: 0 (0)
      standard deviation: 52.3856 (0.205434)
      kurtosis: 1.87884
      skewness: 1.60727
      entropy: 0.599646
  Image statistics:
    Overall:
      min: 0  (0)
      max: 255 (1)
      mean: 50.6986 (0.198818)
      median: 0 (0)
      standard deviation: 70.6766 (0.277163)
      kurtosis: 0.308442
      skewness: 1.26185
      entropy: 0.615521
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Matte color: grey74
  Background color: white
  Border color: srgb(223,223,223)
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 172x250+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 92
  Orientation: Undefined
  Profiles:
    Profile-exif: 120 bytes
  Properties:
    date:create: 2022-04-13T20:57:09+00:00
    date:modify: 2022-04-08T18:18:33+00:00
    exif:Artist: PAINT ME IN BLACK
    exif:ExifOffset: 76
    exif:ExifVersion: 48, 50, 50, 48
    exif:Software: Picasa
    exif:UserComment: 0, 0, 0, 0, 0, 0, 0, 0
    jpeg:colorspace: 2
    jpeg:sampling-factor: 1x1,1x1,1x1
    signature: 924b44c4ff72b6fcd611251a00cd407a416844a2d05e0c2a30cebd190167aaad
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 30715B
  Number pixels: 43000
  Pixels per second: 7885990P
  User time: 0.010u
  Elapsed time: 0:01.005
  Version: ImageMagick 7.1.0-19 Q16-HDRI x86_64 2021-12-22 https://imagemagick.org

Steps to Reproduce

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Processing;
using Xunit;

public class ImageSharpResizeTest
{
    [Fact]
    public async Task TestExifIssue()
    {
        string url = "https://nyckel-public.s3.us-west-2.amazonaws.com/exif-issue.jpg";

        using var client = new HttpClient();
        using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
        using var imageStream = await response.Content.ReadAsStreamAsync();
        using var resizedStream = new MemoryStream();
        using var image = await SixLabors.ImageSharp.Image.LoadAsync(imageStream);

        var resizeOptions = new ResizeOptions
        {
            Size = new SixLabors.ImageSharp.Size { Width = 1024, Height = 1024 },
            Mode = ResizeMode.Max
        };
        image.Mutate(x => x.Resize(resizeOptions));
        await image.SaveAsync(resizedStream, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder());

        var bytes = resizedStream.ToArray();
    }
}    

Images

https://nyckel-public.s3.us-west-2.amazonaws.com/exif-issue.jpg

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions