<commit_before>//
//  UIImage+OEXHelpers.swift
//  edX
//
//  Created by <NAME> on 10/14/15.
//  Copyright © 2015 edX. All rights reserved.
//

import Foundation

extension UIImage {
    func imageCropped(toRect rect: CGRect) -> UIImage {
        guard let cgImage = self.cgImage else { return self }
        let imageRef = cgImage.cropping(to: rect)
        let cropped = UIImage(cgImage: imageRef!)
        return cropped
    }
    
    func resizedTo(size: CGSize) -> UIImage {
        UIGraphicsBeginImageContext(size)
        self.draw(in: CGRect(origin: CGPoint.zero, size: size))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage ?? self
    }
    
    func rotateUp() -> UIImage {
        guard imageOrientation != .up else { return self }
        
        var transform:CGAffineTransform = .identity
        switch imageOrientation {
        case .down, .downMirrored:
            transform = transform.translatedBy(x: size.width, y: size.height)
            transform = transform.rotated(by: CGFloat(Double.pi))
        case .left, .leftMirrored:
            transform = transform.translatedBy(x: size.width, y: 0)
            transform = transform.rotated(by: CGFloat(Double.pi/2))
        case .right, .rightMirrored:
            transform = transform.translatedBy(x: 0, y: size.height)
            transform = transform.rotated(by: CGFloat(-Double.pi/2))
        case .upMirrored:
            transform = transform.translatedBy(x: size.width, y: 0)
            transform = transform.scaledBy(x: -1, y: 1)
        default:
            transform = .identity
        }
        

        //Apply the transfrom to a graphics context and redraw the image
        UIGraphicsBeginImageContext(size)
        
        guard let context = UIGraphicsGetCurrentContext() else { return self }
        
        draw(in: CGRect(origin: CGPoint.zero, size: size))
        context.concatenate(transform)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image ?? self
    }
    
    func image(with color: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        if let cgImage = cgImage {
            context?.clip(to: rect, mask: cgImage)
        }
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
        
        if let cgImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage {
            UIGraphicsEndImageContext()
            return UIImage(cgImage: cgImage, scale: 1, orientation: .downMirrored)
        } else {
            return self
        }
    }
}<commit_msg>fix: sharpen blurry icons across the app (#1488)<commit_after>//
//  UIImage+OEXHelpers.swift
//  edX
//
//  Created by <NAME> on 10/14/15.
//  Copyright © 2015 edX. All rights reserved.
//

import Foundation

extension UIImage {
    func imageCropped(toRect rect: CGRect) -> UIImage {
        guard let cgImage = self.cgImage else { return self }
        let imageRef = cgImage.cropping(to: rect)
        let cropped = UIImage(cgImage: imageRef!)
        return cropped
    }
    
    func resizedTo(size: CGSize) -> UIImage {
        UIGraphicsBeginImageContext(size)
        self.draw(in: CGRect(origin: CGPoint.zero, size: size))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage ?? self
    }
    
    func rotateUp() -> UIImage {
        guard imageOrientation != .up else { return self }
        
        var transform:CGAffineTransform = .identity
        switch imageOrientation {
        case .down, .downMirrored:
            transform = transform.translatedBy(x: size.width, y: size.height)
            transform = transform.rotated(by: CGFloat(Double.pi))
        case .left, .leftMirrored:
            transform = transform.translatedBy(x: size.width, y: 0)
            transform = transform.rotated(by: CGFloat(Double.pi/2))
        case .right, .rightMirrored:
            transform = transform.translatedBy(x: 0, y: size.height)
            transform = transform.rotated(by: CGFloat(-Double.pi/2))
        case .upMirrored:
            transform = transform.translatedBy(x: size.width, y: 0)
            transform = transform.scaledBy(x: -1, y: 1)
        default:
            transform = .identity
        }
        

        //Apply the transfrom to a graphics context and redraw the image
        UIGraphicsBeginImageContext(size)
        
        guard let context = UIGraphicsGetCurrentContext() else { return self }
        
        draw(in: CGRect(origin: CGPoint.zero, size: size))
        context.concatenate(transform)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image ?? self
    }
    
    func image(with color: UIColor) -> UIImage {
        let image = withRenderingMode(.alwaysTemplate)
        let imageView = UIImageView(image: image)
        imageView.tintColor = color
        
        var tintedImage: UIImage?
        UIGraphicsBeginImageContextWithOptions(image.size, false, 0.0)
        if let context = UIGraphicsGetCurrentContext() {
            imageView.layer.render(in: context)
            tintedImage = UIGraphicsGetImageFromCurrentImageContext()
        }
        UIGraphicsEndImageContext()
        
        if let tintedImage = tintedImage {
            return tintedImage
        } else {
            return self
        }
    }
}
{'fraction_non_alphanumeric': 0.09284951974386339, 'fraction_numerical': 0.007648523657061544, 'mean_word_length': 2.213142857142857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '794281', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1612, 'n_words': 393}