<commit_before>// Request+AlamofireImage.swift
//
// Copyright (c) 2015 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Alamofire
import Foundation

#if os(iOS)
import UIKit
#elseif os(watchOS)
import UIKit
import WatchKit
#elseif os(OSX)
import Cocoa
#endif

extension Request {
    /// The completion handler closure used when an image response serializer completes.
    public typealias CompletionHandler = (NSURLRequest?, NSHTTPURLResponse?, Result<Image>) -> Void

    // MARK: - iOS and watchOS

#if os(iOS) || os(watchOS)

    /**
        Creates a response serializer that returns an image initialized from the response data using the specified
        image options.

        - parameter imageScale:           The scale factor used when interpreting the image data to construct 
                                          `responseImage`. Specifying a scale factor of 1.0 results in an image whose 
                                          size matches the pixel-based dimensions of the image. Applying a different 
                                          scale factor changes the size of the image as reported by the size property.
                                          `Screen.scale` by default.
        - parameter inflateResponseImage: Whether to automatically inflate response image data for compressed formats 
                                          (such as PNG or JPEG). Enabling this can significantly improve drawing 
                                          performance as it allows a bitmap representation to be constructed in the 
                                          background rather than on the main thread. `true` by default.

        - returns: An image response serializer.
    */
    public class func imageResponseSerializer(
        imageScale imageScale: CGFloat = Request.imageScale,
        inflateResponseImage: Bool = true)
        -> GenericResponseSerializer<UIImage>
    {
        return GenericResponseSerializer { request, response, data in
            guard let validData = data where validData.length > 0 else {
                return .Failure(data, Request.imageDataError())
            }

            guard Request.validateResponse(response) else {
                return .Failure(data, Request.contentTypeValidationError())
            }

            do {
                var image = try Request.imageFromResponseData(validData, imageScale: imageScale)

                if inflateResponseImage, let inflatedImage = image.af_inflatedImage() {
                    image = inflatedImage
                }

                return .Success(image)
            } catch {
                return .Failure(data, error)
            }
        }
    }

    /**
        Adds a handler to be called once the request has finished.

        - parameter imageScale:           The scale factor used when interpreting the image data to construct 
                                          `responseImage`. Specifying a scale factor of 1.0 results in an image whose 
                                          size matches the pixel-based dimensions of the image. Applying a different 
                                          scale factor changes the size of the image as reported by the size property.
                                          This is set to the value of scale of the main screen by default, which 
                                          automatically scales images for retina displays, for instance. 
                                          `Screen.scale` by default.
        - parameter inflateResponseImage: Whether to automatically inflate response image data for compressed formats 
                                          (such as PNG or JPEG). Enabling this can significantly improve drawing 
                                          performance as it allows a bitmap representation to be constructed in the 
                                          background rather than on the main thread. `true` by default.
        - parameter completionHandler:    A closure to be executed once the request has finished. The closure takes 4
                                          arguments: the URL request, the URL response, if one was received, the image, 
                                          if one could be created from the URL response and data, and any error produced 
                                          while creating the image.

        - returns: The request.
    */
    public func responseImage(
        imageScale: CGFloat = Request.imageScale,
        inflateResponseImage: Bool = true,
        completionHandler: CompletionHandler)
        -> Self
    {
        return response(
            responseSerializer: Request.imageResponseSerializer(
                imageScale: imageScale,
                inflateResponseImage: inflateResponseImage
            ),
            completionHandler: completionHandler
        )
    }

    private class func imageFromResponseData(data: NSData, imageScale: CGFloat) throws -> UIImage {
        if let image = UIImage.af_threadSafeImageWithData(data, scale: imageScale) {
            return image
        }

        throw imageDataError()
    }

    private class var imageScale: CGFloat {
        #if os(iOS)
            return UIScreen.mainScreen().scale
        #elseif os(watchOS)<commit_msg>Switched back to the default UIImage initializer.<commit_after>// Request+AlamofireImage.swift
//
// Copyright (c) 2015 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Alamofire
import Foundation

#if os(iOS)
import UIKit
#elseif os(watchOS)
import UIKit
import WatchKit
#elseif os(OSX)
import Cocoa
#endif

extension Request {
    /// The completion handler closure used when an image response serializer completes.
    public typealias CompletionHandler = (NSURLRequest?, NSHTTPURLResponse?, Result<Image>) -> Void

    // MARK: - iOS and watchOS

#if os(iOS) || os(watchOS)

    /**
        Creates a response serializer that returns an image initialized from the response data using the specified
        image options.

        - parameter imageScale:           The scale factor used when interpreting the image data to construct 
                                          `responseImage`. Specifying a scale factor of 1.0 results in an image whose 
                                          size matches the pixel-based dimensions of the image. Applying a different 
                                          scale factor changes the size of the image as reported by the size property.
                                          `Screen.scale` by default.
        - parameter inflateResponseImage: Whether to automatically inflate response image data for compressed formats 
                                          (such as PNG or JPEG). Enabling this can significantly improve drawing 
                                          performance as it allows a bitmap representation to be constructed in the 
                                          background rather than on the main thread. `true` by default.

        - returns: An image response serializer.
    */
    public class func imageResponseSerializer(
        imageScale imageScale: CGFloat = Request.imageScale,
        inflateResponseImage: Bool = true)
        -> GenericResponseSerializer<UIImage>
    {
        return GenericResponseSerializer { request, response, data in
            guard let validData = data where validData.length > 0 else {
                return .Failure(data, Request.imageDataError())
            }

            guard Request.validateResponse(response) else {
                return .Failure(data, Request.contentTypeValidationError())
            }

            do {
                var image = try Request.imageFromResponseData(validData, imageScale: imageScale)

                if inflateResponseImage, let inflatedImage = image.af_inflatedImage() {
                    image = inflatedImage
                }

                return .Success(image)
            } catch {
                return .Failure(data, error)
            }
        }
    }

    /**
        Adds a handler to be called once the request has finished.

        - parameter imageScale:           The scale factor used when interpreting the image data to construct 
                                          `responseImage`. Specifying a scale factor of 1.0 results in an image whose 
                                          size matches the pixel-based dimensions of the image. Applying a different 
                                          scale factor changes the size of the image as reported by the size property.
                                          This is set to the value of scale of the main screen by default, which 
                                          automatically scales images for retina displays, for instance. 
                                          `Screen.scale` by default.
        - parameter inflateResponseImage: Whether to automatically inflate response image data for compressed formats 
                                          (such as PNG or JPEG). Enabling this can significantly improve drawing 
                                          performance as it allows a bitmap representation to be constructed in the 
                                          background rather than on the main thread. `true` by default.
        - parameter completionHandler:    A closure to be executed once the request has finished. The closure takes 4
                                          arguments: the URL request, the URL response, if one was received, the image, 
                                          if one could be created from the URL response and data, and any error produced 
                                          while creating the image.

        - returns: The request.
    */
    public func responseImage(
        imageScale: CGFloat = Request.imageScale,
        inflateResponseImage: Bool = true,
        completionHandler: CompletionHandler)
        -> Self
    {
        return response(
            responseSerializer: Request.imageResponseSerializer(
                imageScale: imageScale,
                inflateResponseImage: inflateResponseImage
            ),
            completionHandler: completionHandler
        )
    }

    private class func imageFromResponseData(data: NSData, imageScale: CGFloat) throws -> UIImage {
        if let image = UIImage(data: data, scale: imageScale) {
            return image
        }

        throw imageDataError()
    }

    private class var imageScale: CGFloat {
        #if os(iOS)
            return UIScreen.mainScreen().scale
        #elseif os(watchOS)
{'fraction_non_alphanumeric': 0.05051763057523157, 'fraction_numerical': 0.0015567836849069822, 'mean_word_length': 1.8475177304964538, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 76, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1193330', 'n_tokens_mistral': 2722, 'n_tokens_neox': 2554, 'n_words': 1262}