Merge branch 'png-strip-chunks'

This commit is contained in:
Jaex 2018-11-19 18:35:28 +03:00
commit 4dc53423ca
2 changed files with 69 additions and 0 deletions

View file

@ -214,6 +214,9 @@ public int HotkeyRepeatLimit
[Category("Image"), DefaultValue(true), Description("If JPEG exif contains orientation data then rotate image accordingly.")]
public bool RotateImageByExifOrientationData { get; set; }
[Category("Image"), DefaultValue(false), Description("Strip color space information chunks from PNG image.")]
public bool PNGStripColorSpaceInformation { get; set; }
[Category("Upload"), DefaultValue(false), Description("Can be used to disable uploading application wide.")]
public bool DisableUpload { get; set; }

View file

@ -309,6 +309,14 @@ public static MemoryStream SaveImageAsStream(Image img, EImageFormat imageFormat
{
case EImageFormat.PNG:
SaveImageAsPNGStream(img, stream, pngBitDepth);
if (Program.Settings.PNGStripColorSpaceInformation)
{
using (stream)
{
return PNGStripColorSpaceInformation(stream);
}
}
break;
case EImageFormat.JPEG:
SaveImageAsJPEGStream(img, stream, jpegQuality);
@ -367,6 +375,64 @@ private static void SaveImageAsPNGStream(Image img, Stream stream, PNGBitDepth b
img.Save(stream, ImageFormat.Png);
}
private static MemoryStream PNGStripChunks(MemoryStream stream, params string[] chunks)
{
MemoryStream output = new MemoryStream();
stream.Seek(0, SeekOrigin.Begin);
byte[] signature = new byte[8];
stream.Read(signature, 0, 8);
output.Write(signature, 0, 8);
while (true)
{
byte[] lenBytes = new byte[4];
if (stream.Read(lenBytes, 0, 4) != 4)
{
break;
}
if (BitConverter.IsLittleEndian)
{
Array.Reverse(lenBytes);
}
int len = BitConverter.ToInt32(lenBytes, 0);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(lenBytes);
}
byte[] type = new byte[4];
stream.Read(type, 0, 4);
byte[] data = new byte[len + 4];
stream.Read(data, 0, data.Length);
string strType = Encoding.ASCII.GetString(type);
if (!chunks.Contains(strType))
{
output.Write(lenBytes, 0, lenBytes.Length);
output.Write(type, 0, type.Length);
output.Write(data, 0, data.Length);
}
}
return output;
}
private static MemoryStream PNGStripColorSpaceInformation(MemoryStream stream)
{
// http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html
// 4.2.2.1. gAMA Image gamma
// 4.2.2.2. cHRM Primary chromaticities
// 4.2.2.3. sRGB Standard RGB color space
// 4.2.2.4. iCCP Embedded ICC profile
return PNGStripChunks(stream, "gAMA", "cHRM", "sRGB", "iCCP");
}
private static void SaveImageAsJPEGStream(Image img, Stream stream, int jpegQuality)
{
try