<commit_before>﻿using System;
using UIKit;
using Foundation;
using CoreGraphics;
using ImageIO;
using FFImageLoading.Helpers;
using FFImageLoading.Work;

namespace FFImageLoading.Extensions
{
	public static class NSDataExtensions
	{
		public enum RCTResizeMode: long
		{
			ScaleAspectFill = UIViewContentMode.ScaleAspectFill,
			ScaleAspectFit = UIViewContentMode.ScaleAspectFit,
			ScaleToFill = UIViewContentMode.ScaleToFill,
		}

		// Shamelessly copied from React-Native: https://github.com/facebook/react-native/blob/2cbc9127560c5f0f89ae5aa6ff863b1818f1c7c3/Libraries/Image/RCTImageUtils.m
		public static UIImage ToImage(this NSData data, CGSize destSize, nfloat destScale, RCTResizeMode resizeMode = RCTResizeMode.ScaleAspectFit, ImageInformation imageinformation = null, bool allowUpscale = false)
        {
			using (var sourceRef = CGImageSource.FromData(data))
			{
                var imageProperties = GetImageProperties(sourceRef);

                if (imageProperties == null)
                {
                    throw new BadImageFormatException("Image is null");
                }

				if (imageinformation != null)
				{
					if (imageProperties.PixelWidth.HasValue && imageProperties.PixelHeight.HasValue)
						imageinformation.SetOriginalSize(imageProperties.PixelWidth.Value, imageProperties.PixelHeight.Value);
				}

				var sourceSize = new CGSize((nfloat)imageProperties.PixelWidth, (nfloat)imageProperties.PixelHeight);

				if (destSize.IsEmpty)
				{
					destSize = sourceSize;
					if (destScale <= 0)
					{
						destScale = 1;
					}
				}
				else if (destScale <= 0)
				{
					destScale = ScaleHelper.Scale;
				}

				// Calculate target size
				CGSize targetSize = RCTTargetSize(sourceSize, 1, destSize, destScale, resizeMode, allowUpscale);
				CGSize targetPixelSize = RCTSizeInPixels(targetSize, destScale);
				int maxPixelSize = (int)Math.Max(
					Math.Min(sourceSize.Width, targetPixelSize.Width),
					Math.Min(sourceSize.Height, targetPixelSize.Height)
				);

				var options = new CGImageThumbnailOptions()
				{
						ShouldAllowFloat = true,
						CreateThumbnailWithTransform = true,
						CreateThumbnailFromImageAlways = true,
						MaxPixelSize = maxPixelSize,
						ShouldCache = false,
				};

                UIImage image = null;
<commit_msg>Fixes #687<commit_after>﻿using System;
using UIKit;
using Foundation;
using CoreGraphics;
using ImageIO;
using FFImageLoading.Helpers;
using FFImageLoading.Work;

namespace FFImageLoading.Extensions
{
	public static class NSDataExtensions
	{
		public enum RCTResizeMode: long
		{
			ScaleAspectFill = UIViewContentMode.ScaleAspectFill,
			ScaleAspectFit = UIViewContentMode.ScaleAspectFit,
			ScaleToFill = UIViewContentMode.ScaleToFill,
		}

		// Shamelessly copied from React-Native: https://github.com/facebook/react-native/blob/2cbc9127560c5f0f89ae5aa6ff863b1818f1c7c3/Libraries/Image/RCTImageUtils.m
		public static UIImage ToImage(this NSData data, CGSize destSize, nfloat destScale, RCTResizeMode resizeMode = RCTResizeMode.ScaleAspectFit, ImageInformation imageinformation = null, bool allowUpscale = false)
        {
			using (var sourceRef = CGImageSource.FromData(data))
			{
                var imageProperties = GetImageProperties(sourceRef);

                if (imageProperties == null)
                {
                    throw new BadImageFormatException("Image is null");
                }

				if (imageinformation != null)
				{
					if (imageProperties.PixelWidth.HasValue && imageProperties.PixelHeight.HasValue)
						imageinformation.SetOriginalSize(imageProperties.PixelWidth.Value, imageProperties.PixelHeight.Value);
				}

				var sourceSize = new CGSize((nfloat)imageProperties.PixelWidth, (nfloat)imageProperties.PixelHeight);

				if (destSize.IsEmpty)
				{
					destSize = sourceSize;
					if (destScale <= 0)
					{
						destScale = 1;
					}
				}
				else if (destScale <= 0)
				{
					destScale = ScaleHelper.Scale;
				}

				// Calculate target size
				CGSize targetSize = RCTTargetSize(sourceSize, 1, destSize, destScale, resizeMode, allowUpscale);
				CGSize targetPixelSize = RCTSizeInPixels(targetSize, destScale);
				int maxPixelSize = (int)Math.Max(
                    allowUpscale ? targetPixelSize.Width : Math.Min(sourceSize.Width, targetPixelSize.Width),
					allowUpscale ? targetPixelSize.Height : Math.Min(sourceSize.Height, targetPixelSize.Height)
				);

				var options = new CGImageThumbnailOptions()
				{
						ShouldAllowFloat = true,
						CreateThumbnailWithTransform = true,
						CreateThumbnailFromImageAlways = true,
						MaxPixelSize = maxPixelSize,
						ShouldCache = false,
				};

                UIImage image = null;

{'fraction_non_alphanumeric': 0.0827131618116096, 'fraction_numerical': 0.012545183925154156, 'mean_word_length': 3.3515263644773357, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '7148160', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1355, 'n_words': 277}