Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

ImageProcessor/Filters

Renars_Cernis Nov 22nd, 2018 95 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. Programming example Image_Filters
  2.  
  3. @@ -0,0 +1,218 @@
  4. //: Playground - noun: a place where people can play
  5.  
  6. import UIKit
  7.  
  8. let image = UIImage(named: "sample.png")!
  9.  
  10. // Process the image!
  11.  
  12. var myRGBA = RGBAImage(image: image)!
  13.  
  14. let x = 10
  15. let y = 10
  16.  
  17. let index = y * myRGBA.width + x
  18.  
  19. var pixel = myRGBA.pixels[index]
  20.  
  21. pixel.red
  22. pixel.blue
  23. pixel.green
  24.  
  25. pixel.red = 255
  26. pixel.blue = 0
  27. pixel.green = 0
  28.  
  29. myRGBA.pixels[index] = pixel
  30.  
  31. //let newImage = myRGBA.toUIImage()
  32.  
  33. var totalRed = 0
  34. var totalBlue = 0
  35. var totalGreen = 0
  36.  
  37. /*
  38. let count = myRGBA.width * myRGBA.height
  39. let avgRed = totalRed/count
  40. let avgGreen = totalGreen/count
  41. let avgBlue = totalBlue/count
  42. */
  43.  
  44. /*
  45.  for y in 0..<myRGBA.height {
  46.     for x in 0..<myRGBA.width {
  47.         let index = y * myRGBA.width + x
  48.         var pixel = myRGBA.pixels[index]
  49.         let colorDiff = Int(pixel.red) - avgRed
  50.         if (colorDiff>0)
  51.         {
  52.         pixel.red = UInt8( max(0,min(255,avgRed+colorDiff/effect)))
  53.         myRGBA.pixels[index] = pixel
  54.         }
  55.     }
  56.  }
  57.  */
  58. let avgRed = 119
  59. let avgGreen = 98
  60. let avgBlue = 83
  61. ////////
  62.  
  63. class SomeFilters {
  64.     var image: RGBAImage
  65.    
  66.     init(image: UIImage) {
  67.         self.image = RGBAImage(image: image)!
  68.     }
  69.    
  70.     // Green color Filter
  71.     func Green(value: UInt8 = 0) {
  72.         for a in 0..<self.image.width {
  73.             for b in 0..<self.image.width {
  74.                 let index = a * self.image.width + b
  75.                 var pixel = self.image.pixels[index]
  76.                 let redDiff = Int(pixel.red) - avgRed
  77.                 if(redDiff>0) {
  78.                     pixel.red = UInt8(max(0, min(255, avgRed+redDiff/3)))
  79.                 }
  80.                 self.image.pixels[index]=pixel
  81.             }
  82.         }
  83.     }
  84.    
  85.     // Red color Filter
  86.     func Red(value: UInt8 = 0) {
  87.         for a in 0..<self.image.width {
  88.             for b in 0..<self.image.width {
  89.                 let index = a * self.image.width + b
  90.                 var pixel = self.image.pixels[index]
  91.                 let greenDiff = Int(pixel.green) - avgGreen
  92.                 if(greenDiff>0) {
  93.                     pixel.green = UInt8(max(0, min(255, avgGreen+greenDiff/5)))
  94.                 }
  95.                 self.image.pixels[index]=pixel
  96.             }
  97.         }
  98.     }
  99.    
  100.     // Blue color Filter
  101.     func Blue(value: UInt8 = 0) {
  102.         for a in 0..<self.image.width {
  103.             for b in 0..<self.image.width {
  104.                 let index = a * self.image.width + b
  105.                 var pixel = self.image.pixels[index]
  106.                 let blueDiff = Int(pixel.blue) - avgBlue
  107.                 if(blueDiff<0) {
  108.                     pixel.blue = UInt8(max(0, min(255, avgBlue-blueDiff*3)))
  109.                 }
  110.                 self.image.pixels[index]=pixel
  111.             }
  112.         }
  113.     }
  114.    
  115.     // Yellow color Filter
  116.     func Yellow(value: UInt8 = 0) {
  117.         for a in 0..<self.image.width {
  118.             for b in 0..<self.image.width {
  119.                 let index = a * self.image.width + b
  120.                 var pixel = self.image.pixels[index]
  121.                 let blueDiff = Int(pixel.blue) - avgBlue
  122.                 if(blueDiff>0) {
  123.                     pixel.blue = UInt8(max(0, min(255, avgBlue-blueDiff*3)))
  124.                 }
  125.                 self.image.pixels[index]=pixel
  126.             }
  127.         }
  128.     }
  129.    
  130.     //Brightness Filter
  131.     func DecreaseBrightness(value: UInt8=0) {
  132.         for a in 0..<self.image.height {
  133.             for b in 0..<self.image.width {
  134.                 let index = a*self.image.width + b
  135.                 var pixel = self.image.pixels[index]
  136.                
  137.               // Change numbers to decrease image brightness
  138.                 pixel.red /= 5
  139.                 pixel.green /= 5
  140.                 pixel.blue /= 5
  141.                
  142.             }
  143.         }
  144.     }
  145.    
  146.    
  147.     func process(filters: [String]) {
  148.         for filter in filters {
  149.             switch filter {
  150.             case "Red":
  151.                 self.Red()
  152.             case "Green":
  153.                 self.Green()
  154.             case "Blue":
  155.                 self.Blue()
  156.             case "Yellow"://///
  157.                 self.Yellow()/////
  158.             case "DecreaseBrightness":
  159.                 self.DecreaseBrightness()
  160.             default:
  161.                 print("Wrong filter; The filter doesn't exist")
  162.             }
  163.         }
  164.     }
  165.    
  166.     func toUIImage() -> UIImage {
  167.         return self.image.toUIImage()!
  168.     }
  169.    
  170. }
  171.  
  172. let imageProcessing = SomeFilters(image: image)
  173. let filters = ["Yellow"]
  174. imageProcessing.process(filters)
  175. let newImage = imageProcessing.toUIImage()
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. /*
  183. public class SomeFilters {
  184.    
  185. }
  186. for y in 0..<myRGBA.height {
  187.     for x in 0..<myRGBA.width {
  188.         let index = y * myRGBA.width + x
  189.         var pixel = myRGBA.pixels[index]
  190.         let colorDiff = Int(pixel.red) - avgRed
  191.         if (colorDiff>0)
  192.         {
  193.             pixel.red = UInt8( max(0,min(255,avgRed+colorDiff/effect)))
  194.             myRGBA.pixels[index] = pixel
  195.         }
  196.     }
  197. }
  198.  
  199. let newImage2 = myRGBA.toUIImage()
  200. image
  201.  
  202.  
  203. var effect1 = 10
  204.  
  205. for y in 0..<myRGBA.height {
  206.     for x in 0..<myRGBA.width {
  207.         let index = y * myRGBA.width + x
  208.         var pixel = myRGBA.pixels[index]
  209.         let colorDiff = Int(pixel.green) - avgGreen
  210.         if (colorDiff>0)
  211.         {
  212.             pixel.green = UInt8( max(0,min(100,avgGreen+colorDiff*effect1)))
  213.             myRGBA.pixels[index] = pixel
  214.         }
  215.     }
  216. }
  217.  
  218. let newImage3 = myRGBA.toUIImage()
  219. image
  220.  
  221. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top