<commit_before>//
//  ImageExtensions.swift
//  Ello
//
//  Created by <NAME> on 12/9/14.
//  Copyright (c) 2014 Ello. All rights reserved.
//

import UIKit

public extension UIImage {

    class func imageWithColor(color:UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0);
        color.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

    class func imageWithHex(hex: Int) -> UIImage {
        return imageWithColor(UIColor(hex: hex))
    }

    func squareImage() -> UIImage? {
        var originalWidth  = self.size.width
        var originalHeight = self.size.height

        var edge: CGFloat
        if originalWidth > originalHeight {
            edge = originalHeight
        } else {
            edge = originalWidth
        }

        var posX = (originalWidth  - edge) / 2.0
        var posY = (originalHeight - edge) / 2.0

        var cropSquare = CGRectMake(posX, posY, edge, edge)

        var imageRef = CGImageCreateWithImageInRect(self.CGImage, cropSquare);
        return UIImage(CGImage: imageRef, scale: UIScreen.mainScreen().scale, orientation: self.imageOrientation)
    }

    func resizeToSize(targetSize: CGSize) -> UIImage {
        let newSize = self.size.scaledSize(targetSize)

        // This is the rect that we've calculated out and this is what is actually used below
        let rect = CGRectMake(0, 0, newSize.width, newSize.height)

        // Actually do the resizing to the rect using the ImageContext stuff
        UIGraphicsBeginImageContextWithOptions(newSize, false, self.scale)
        self.drawInRect(rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
    }

    func roundCorners() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let rect = CGRectMake(0.0, 0.0, self.size.width, self.size.height)
        UIBezierPath(roundedRect: rect, cornerRadius: size.width / 2.0).addClip()
        self.drawInRect(rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }

    func copyWithCorrectOrientationAndSize(completion:(image: UIImage) -> Void) {
        inBackground {
            let sourceImage: UIImage
            if self.imageOrientation == .Up {
                sourceImage = self
            }
            else {
                UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
                self.drawInRect(CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
                sourceImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
            }

            let maxSize = CGSize(width: 1200.0, height: 3600.0)
            let resizedImage = sourceImage.resizeToSize(maxSize)
            inForeground {
                completion(image: resizedImage)
            }
        }
    }<commit_msg>take 'scale' into account (by discarding it - all uploaded images are 1x)<commit_after>//
//  ImageExtensions.swift
//  Ello
//
//  Created by <NAME> on 12/9/14.
//  Copyright (c) 2014 Ello. All rights reserved.
//

import UIKit

public extension UIImage {

    class func imageWithColor(color:UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0);
        color.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

    class func imageWithHex(hex: Int) -> UIImage {
        return imageWithColor(UIColor(hex: hex))
    }

    func squareImage() -> UIImage? {
        var originalWidth  = self.size.width
        var originalHeight = self.size.height

        var edge: CGFloat
        if originalWidth > originalHeight {
            edge = originalHeight
        } else {
            edge = originalWidth
        }

        var posX = (originalWidth  - edge) / 2.0
        var posY = (originalHeight - edge) / 2.0

        var cropSquare = CGRectMake(posX, posY, edge, edge)

        var imageRef = CGImageCreateWithImageInRect(self.CGImage, cropSquare);
        return UIImage(CGImage: imageRef, scale: UIScreen.mainScreen().scale, orientation: self.imageOrientation)
    }

    func resizeToSize(targetSize: CGSize) -> UIImage {
        let newSize = self.size.scaledSize(targetSize)

        // This is the rect that we've calculated out and this is what is actually used below
        let rect = CGRectMake(0, 0, newSize.width, newSize.height)

        // Actually do the resizing to the rect using the ImageContext stuff
        UIGraphicsBeginImageContextWithOptions(newSize, false, self.scale)
        self.drawInRect(rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
    }

    func roundCorners() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let rect = CGRectMake(0.0, 0.0, self.size.width, self.size.height)
        UIBezierPath(roundedRect: rect, cornerRadius: size.width / 2.0).addClip()
        self.drawInRect(rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }

    func copyWithCorrectOrientationAndSize(completion:(image: UIImage) -> Void) {
        inBackground {
            let sourceImage: UIImage
            if self.imageOrientation == .Up && self.scale == 1.0 {
                sourceImage = self
            }
            else {
                let newSize = CGSize(width: self.size.width * self.scale, height: self.size.height * self.scale)
                UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
                self.drawInRect(CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
                sourceImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
            }

            let maxSize = CGSize(width: 1200.0, height: 3600.0)
            let resizedImage = sourceImage.resizeToSize(maxSize)
            inForeground {
                completion(image: resizedImage)
            }
        }
    }
{'fraction_non_alphanumeric': 0.08899153194765204, 'fraction_numerical': 0.012471131639722863, 'mean_word_length': 2.3347022587268995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, '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': '1774405', 'n_tokens_mistral': 1979, 'n_tokens_neox': 1838, 'n_words': 449}