Browse Source

moved extending the ExifProfile to the jpeg decoder

pull/616/head
popow 8 years ago
parent
commit
efd0288f1d
  1. 38
      src/ImageSharp/Formats/Jpeg/GolangPort/GolangJpegDecoderCore.cs
  2. 38
      src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs
  3. 12
      src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs

38
src/ImageSharp/Formats/Jpeg/GolangPort/GolangJpegDecoderCore.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@ -68,6 +69,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
/// </summary>
private bool isExif;
/// <summary>
/// Contains exif data
/// </summary>
private byte[] exifData;
/// <summary>
/// Whether the image has an Adobe marker.
/// It's faster to check this than to use the equality operator on the struct
@ -413,6 +419,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
}
}
this.InitExifProfile();
this.InitDerivedMetaDataProperties();
}
@ -425,6 +432,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
&& mcuCounter < this.TotalMCUCount;
}
/// <summary>
/// Initializes the exif profile.
/// </summary>
private void InitExifProfile()
{
if (this.isExif)
{
this.MetaData.ExifProfile = new ExifProfile(this.exifData);
}
}
/// <summary>
/// Assigns derived metadata properties to <see cref="MetaData"/>, eg. horizontal and vertical resolution if it has a JFIF header.
/// </summary>
@ -500,19 +518,31 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
if (ProfileResolver.IsProfile(profile, ProfileResolver.ExifMarker))
{
this.isExif = true;
if (this.MetaData.ExifProfile == null)
if (this.exifData == null)
{
// the first 6 bytes (Exif00) will be skipped, because this is Jpeg specific
this.MetaData.ExifProfile = new ExifProfile(profile.Skip(6).ToArray());
this.exifData = profile.Skip(6).ToArray();
}
else
{
// if the exif information exceeds 64K, it will be split over multiple APP1 marker
this.MetaData.ExifProfile.Extend(profile.Skip(6).ToArray());
// if the exif information exceeds 64K, it will be split over multiple APP1 markers
this.ExtendExif(profile.Skip(6).ToArray());
}
}
}
/// <summary>
/// Extends the exif profile with additional data.
/// </summary>
/// <param name="bytes">The array containing addition profile data.</param>
private void ExtendExif(byte[] bytes)
{
int currentLength = this.exifData.Length;
Array.Resize(ref this.exifData, currentLength + bytes.Length);
Buffer.BlockCopy(bytes, 0, this.exifData, currentLength, bytes.Length);
}
/// <summary>
/// Processes the App2 marker retrieving any stored ICC profile information
/// </summary>

38
src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs

@ -74,6 +74,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
/// </summary>
private bool isExif;
/// <summary>
/// Contains exif data
/// </summary>
private byte[] exifData;
/// <summary>
/// Contains information about the JFIF marker
/// </summary>
@ -202,6 +207,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
where TPixel : struct, IPixel<TPixel>
{
this.ParseStream(stream);
this.InitExifProfile();
this.InitDerivedMetaDataProperties();
return this.PostProcessIntoImage<TPixel>();
}
@ -213,6 +219,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
public IImageInfo Identify(Stream stream)
{
this.ParseStream(stream, true);
this.InitExifProfile();
this.InitDerivedMetaDataProperties();
return new ImageInfo(new PixelTypeInfo(this.BitsPerPixel), this.ImageWidth, this.ImageHeight, this.MetaData);
}
@ -404,6 +411,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
throw new ImageFormatException($"Unsupported color mode. Max components 4; found {this.ComponentCount}");
}
/// <summary>
/// Initializes the exif profile.
/// </summary>
private void InitExifProfile()
{
if (this.isExif)
{
this.MetaData.ExifProfile = new ExifProfile(this.exifData);
}
}
/// <summary>
/// Assigns derived metadata properties to <see cref="MetaData"/>, eg. horizontal and vertical resolution if it has a JFIF header.
/// </summary>
@ -481,19 +499,31 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
if (ProfileResolver.IsProfile(profile, ProfileResolver.ExifMarker))
{
this.isExif = true;
if (this.MetaData.ExifProfile == null)
if (this.exifData == null)
{
// the first 6 bytes (Exif00) will be skipped, because this is Jpeg specific
this.MetaData.ExifProfile = new ExifProfile(profile.Skip(6).ToArray());
this.exifData = profile.Skip(6).ToArray();
}
else
{
// if the exif information exceeds 64K, it will be split over multiple APP1 marker
this.MetaData.ExifProfile.Extend(profile.Skip(6).ToArray());
// if the exif information exceeds 64K, it will be split over multiple APP1 markers
this.ExtendExif(profile.Skip(6).ToArray());
}
}
}
/// <summary>
/// Extends the exif profile with additional data.
/// </summary>
/// <param name="bytes">The array containing addition profile data.</param>
private void ExtendExif(byte[] bytes)
{
int currentLength = this.exifData.Length;
Array.Resize(ref this.exifData, currentLength + bytes.Length);
Buffer.BlockCopy(bytes, 0, this.exifData, currentLength, bytes.Length);
}
/// <summary>
/// Processes the App2 marker retrieving any stored ICC profile information
/// </summary>

12
src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs

@ -229,18 +229,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
this.values.Add(newExifValue);
}
/// <summary>
/// Extends the profile with additional data.
/// </summary>
/// <param name="bytes">The array containing addition profile data.</param>
public void Extend(byte[] bytes)
{
int currentLength = this.data.Length;
Array.Resize(ref this.data, currentLength + bytes.Length);
Buffer.BlockCopy(bytes, 0, this.data, currentLength, bytes.Length);
}
/// <summary>
/// Converts this instance to a byte array.
/// </summary>

Loading…
Cancel
Save