<commit_before>//
//  PhotoEditingViewController.swift
//  Extension
//
//  Created by <NAME> on 10/5/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Metal
import Photos
import PhotosUI
import UIKit

class PhotoEditingViewController: ViewController, PHContentEditingController {

  var input: PHContentEditingInput?

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    scrollView.contentInset.top = topLayoutGuide.length
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  // MARK: - PHContentEditingController

  func canHandleAdjustmentData(adjustmentData: PHAdjustmentData?) -> Bool {
    // Inspect the adjustmentData to determine whether your extension can work with past edits.
    // (Typically, you use its formatIdentifier and formatVersion properties to do this.)
    return false
  }

  func startContentEditingWithInput(contentEditingInput: PHContentEditingInput?, placeholderImage: UIImage) {
    // Present content for editing, and keep the contentEditingInput for use when closing the edit session.
    // If you returned true from canHandleAdjustmentData:, contentEditingInput has the original image and adjustment data.
    // If you returned false, the contentEditingInput has past edits "baked in".
    input = contentEditingInput

    guard let image = input?.displaySizeImage else {
      return
    }

    displayImage = image

    switch selectedImageProcessingFilter {
    case .Metal:
      applyFiltersAndDisplay(image: image, filterClass: GoldenSpiralMetalFilter.self, viewClass: MetalImageView.self)
    case .CoreGraphics:
      applyFiltersAndDisplay(image: image, filterClass: GoldenSpiralCGFilter.self, viewClass: UIImageView.self)
    }
  }

  func finishContentEditingWithCompletionHandler(completionHandler: ((PHContentEditingOutput!) -> Void)!) {
    // Update UI to reflect that editing has finished and output is being rendered.

    dispatch_async(dispatch_get_global_queue(CLong(DISPATCH_QUEUE_PRIORITY_DEFAULT), 0)) { [weak self] in
      guard let input = self?.input else {
        completionHandler?(nil)
        return
      }

      let output = PHContentEditingOutput(contentEditingInput: input)

      guard let path = self?.input?.fullSizeImageURL?.path else {
        completionHandler?(output)
        return
      }

      guard let sourceImage = UIImage(contentsOfFile: path) else {
        completionHandler?(output)
        return
      }

      guard let selectedImageProcessingFilter = self?.selectedImageProcessingFilter else {
        completionHandler?(output)
        return
      }

      var outputImage: UIImage!

      switch selectedImageProcessingFilter {
      case .Metal:
        let filter = GoldenSpiralMetalFilter()
        filter.inputImage = sourceImage
        if filter.canProcessImage {
          outputImage = filter.outputImage?.imageByConvertingFromCIImage(device: filter.device, context: filter.context)
        }
        else {
          dispatch_async(dispatch_get_main_queue()) {
            self?.showCannotBeProcessedByMetalAlert()
          }
        }
      case .CoreGraphics:
        let filter = GoldenSpiralCGFilter()
        filter.inputImage = sourceImage
        outputImage = filter.outputImage
      }

      guard outputImage != nil else {
        completionHandler?(output)
        return<commit_msg>Log execution time<commit_after>//
//  PhotoEditingViewController.swift
//  Extension
//
//  Created by <NAME> on 10/5/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Metal
import Photos
import PhotosUI
import UIKit

class PhotoEditingViewController: ViewController, PHContentEditingController {

  var input: PHContentEditingInput?

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    scrollView.contentInset.top = topLayoutGuide.length
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  // MARK: - PHContentEditingController

  func canHandleAdjustmentData(adjustmentData: PHAdjustmentData?) -> Bool {
    // Inspect the adjustmentData to determine whether your extension can work with past edits.
    // (Typically, you use its formatIdentifier and formatVersion properties to do this.)
    return false
  }

  func startContentEditingWithInput(contentEditingInput: PHContentEditingInput?, placeholderImage: UIImage) {
    // Present content for editing, and keep the contentEditingInput for use when closing the edit session.
    // If you returned true from canHandleAdjustmentData:, contentEditingInput has the original image and adjustment data.
    // If you returned false, the contentEditingInput has past edits "baked in".
    input = contentEditingInput

    guard let image = input?.displaySizeImage else {
      return
    }

    displayImage = image

    switch selectedImageProcessingFilter {
    case .Metal:
      applyFiltersAndDisplay(image: image, filterClass: GoldenSpiralMetalFilter.self, viewClass: MetalImageView.self)
    case .CoreGraphics:
      applyFiltersAndDisplay(image: image, filterClass: GoldenSpiralCGFilter.self, viewClass: UIImageView.self)
    }
  }

  func finishContentEditingWithCompletionHandler(completionHandler: ((PHContentEditingOutput!) -> Void)!) {
    // Update UI to reflect that editing has finished and output is being rendered.

    dispatch_async(dispatch_get_global_queue(CLong(DISPATCH_QUEUE_PRIORITY_DEFAULT), 0)) { [weak self] in
      guard let input = self?.input else {
        completionHandler?(nil)
        return
      }

      let output = PHContentEditingOutput(contentEditingInput: input)

      guard let path = self?.input?.fullSizeImageURL?.path else {
        completionHandler?(output)
        return
      }

      guard let sourceImage = UIImage(contentsOfFile: path) else {
        completionHandler?(output)
        return
      }

      guard let selectedImageProcessingFilter = self?.selectedImageProcessingFilter else {
        completionHandler?(output)
        return
      }

      var outputImage: UIImage!

      let duration = Timer.run {
        switch selectedImageProcessingFilter {
        case .Metal:
          let filter = GoldenSpiralMetalFilter()
          filter.inputImage = sourceImage.imageByFixingOrientation()
          if filter.canProcessImage {
            outputImage = filter.outputImage?.imageByConvertingFromCIImage(device: filter.device, context: filter.context)
          }
          else {
            dispatch_async(dispatch_get_main_queue()) {
              self?.showCannotBeProcessedByMetalAlert()
            }
          }
        case .CoreGraphics:
          let filter = GoldenSpiralCGFilter()
          filter.inputImage = sourceImage.imageByFixingOrientation()
          outputImage = filter.outputImage
        }
      }

      if let duration = duration {
        let durationString = self?.timerFormatter.stringFromTimer(duration)
        Logger.log("Filter executed in \(durationString)")
      }

      guard outputImage != nil else {
        completionHandler?(output)
        return
{'fraction_non_alphanumeric': 0.07313738892686261, 'fraction_numerical': 0.002734107997265892, 'mean_word_length': 3.1568181818181817, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '7475286', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1888, 'n_words': 513}