<commit_before>﻿//=================================================================================================
// Copyright 2013-2015 <NAME> <https://magick.codeplex.com/>
//
// Licensed under the ImageMagick License (the "License"); you may not use this file except in 
// compliance with the License. You may obtain a copy of the License at
//
//   http://www.imagemagick.org/script/license.php
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.
//=================================================================================================

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;

namespace ImageMagick.ImageOptimizers
{
	///=============================================================================================
	/// <summary>
	/// Class that can be used to optimize png files.
	/// </summary>
	public sealed class PngOptimizer : IImageOptimizer, ILosslessImageOptimizer
	{
		//===========================================================================================
		private static void CheckFormat(MagickImage image)
		{
			MagickFormat format = image.FormatInfo.Module;
			if (format != MagickFormat.Png)
				throw new MagickCorruptImageErrorException("Invalid image format: " + format.ToString());
		}
		//===========================================================================================
		private static bool IsAlphaOpaque(MagickImage image)
		{
			using (PixelCollection pixels = image.GetReadOnlyPixels())
			{
				int alphaIndex = pixels.GetIndex(PixelChannel.Alpha);
				for (int y = 0; y < image.Height; y++)
				{
					var values = pixels.GetValues(y);
					for (int x = 0; x < image.Width; x++)
					{
						if (values[(x * pixels.Channels) + alphaIndex] != Quantum.Max)
							return false;
					}
				}
			}

			return true;
		}
		//===========================================================================================
		private static void CheckTransparency(MagickImage image)
		{
			if (!image.HasAlpha)
				return;

			if (IsAlphaOpaque(image))
				image.HasAlpha = false;
		}
		//===========================================================================================
		private void DoLosslessCompress(FileInfo file)
		{
			using (MagickImage image = new MagickImage(file))
			{
				CheckFormat(image);

				image.Strip();
				image.SetDefine(MagickFormat.Png, "exclude-chunks", "all");
				image.SetDefine(MagickFormat.Png, "include-chunks", "tRNS,gAMA");
				CheckTransparency(image);

				Collection<FileInfo> tempFiles = new Collection<FileInfo>();

				try
				{
					FileInfo bestFile = null;

					foreach (int quality in GetQualityList())
					{
						FileInfo tempFile = new FileInfo(Path.GetTempFileName());
						tempFiles.Add(tempFile);
<commit_msg>Using IsOpaque in the PngOptimizer instead of checking the pixels with GetReadOnlyPixels.<commit_after>﻿//=================================================================================================
// Copyright 2013-2015 <NAME> <https://magick.codeplex.com/>
//
// Licensed under the ImageMagick License (the "License"); you may not use this file except in 
// compliance with the License. You may obtain a copy of the License at
//
//   http://www.imagemagick.org/script/license.php
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.
//=================================================================================================

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;

namespace ImageMagick.ImageOptimizers
{
	///=============================================================================================
	/// <summary>
	/// Class that can be used to optimize png files.
	/// </summary>
	public sealed class PngOptimizer : IImageOptimizer, ILosslessImageOptimizer
	{
		//===========================================================================================
		private static void CheckFormat(MagickImage image)
		{
			MagickFormat format = image.FormatInfo.Module;
			if (format != MagickFormat.Png)
				throw new MagickCorruptImageErrorException("Invalid image format: " + format.ToString());
		}
		//===========================================================================================
		private static void CheckTransparency(MagickImage image)
		{
			if (!image.HasAlpha)
				return;

			if (image.IsOpaque)
				image.HasAlpha = false;
		}
		//===========================================================================================
		private void DoLosslessCompress(FileInfo file)
		{
			using (MagickImage image = new MagickImage(file))
			{
				CheckFormat(image);

				image.Strip();
				image.SetDefine(MagickFormat.Png, "exclude-chunks", "all");
				image.SetDefine(MagickFormat.Png, "include-chunks", "tRNS,gAMA");
				CheckTransparency(image);

				Collection<FileInfo> tempFiles = new Collection<FileInfo>();

				try
				{
					FileInfo bestFile = null;

					foreach (int quality in GetQualityList())
					{
						FileInfo tempFile = new FileInfo(Path.GetTempFileName());
						tempFiles.Add(tempFile);

{'fraction_non_alphanumeric': 0.3013014797646639, 'fraction_numerical': 0.0032091281868425743, 'mean_word_length': 4.660948536831484, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6596679', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1330, 'n_words': 391}