Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #!/usr/bin/swift
  2.  
  3.  
  4. import Cocoa
  5.  
  6.  
  7. class fileMG {
  8.  
  9. var dir: String
  10. var file: String
  11. var path: String
  12. var extn: String
  13. var name: String
  14.  
  15. init(p: String) {
  16. let ns: NSString = p as NSString
  17. path = p
  18. file = ns.lastPathComponent
  19. dir = ns.deletingLastPathComponent
  20. extn = ns.pathExtension
  21. name = (file as NSString).deletingPathExtension
  22. print("path : \(path)")
  23. print("file : \(file)")
  24. print("dir : \(dir)")
  25. print("extn : \(extn)")
  26. print("name : \(name)")
  27. }
  28. }
  29.  
  30.  
  31. class imgMG: fileMG {
  32.  
  33. func resizeImg(w: Int, h: Int) -> String {
  34.  
  35. if let img: NSImage = NSImage(contentsOfFile: path) {
  36.  
  37. let orgBmpRep: NSBitmapImageRep = NSBitmapImageRep(data:img.tiffRepresentation!)!
  38. let orgRef: CGImage = orgBmpRep.cgImage!
  39. let orgW: Int = Int(orgRef.width)
  40. let orgH: Int = Int(orgRef.height)
  41.  
  42. var resizeW: Int = 0, resizeH: Int = 0
  43. if (orgW < orgH) {
  44. resizeW = w
  45. resizeH = orgH * resizeW / orgW
  46. } else {
  47. resizeH = h
  48. resizeW = orgW * resizeH / orgH
  49. }
  50. let resizeSize: NSSize = NSMakeSize(CGFloat(resizeW), CGFloat(resizeH))
  51.  
  52. let bitsPerComponent: Int = 8
  53. let bytesPerRow: Int = 4 * resizeW
  54. let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
  55. let bitmapInfo: UInt32 = CGImageAlphaInfo.premultipliedLast.rawValue
  56.  
  57. let bitmapContext: CGContext = CGContext(data: nil, width: resizeW, height: resizeH, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: UInt32(bitmapInfo))!
  58.  
  59. // 元画像 (image) をビットマップに書き出します。
  60. let bitmapRect: CGRect = NSMakeRect(0.0, 0.0, resizeSize.width, resizeSize.height)
  61. bitmapContext.draw(orgRef, in: bitmapRect)
  62.  
  63. // ビットマップを NSImage に変換します。
  64. let newImageRef: CGImage = bitmapContext.makeImage()!
  65. let newImage: NSImage = NSImage(cgImage: newImageRef, size: resizeSize)
  66.  
  67. // 保存
  68. let newBmpRep: NSBitmapImageRep = NSBitmapImageRep(data:newImage.tiffRepresentation!)!
  69. let imageProps: [String: AnyObject] = [ NSImageCompressionFactor as String: 1.0 as AnyObject ]
  70. let newData: NSData = newBmpRep.representation(using: NSBitmapImageFileType.PNG, properties: imageProps)! as NSData
  71. let newPath: String = "\(dir)/\(name)_\(w).\(extn)"
  72. newData.write(toFile: newPath, atomically: true)
  73. return "finish!!\n\(newPath)"
  74. } else {
  75. return "not file : " + path
  76.  
  77. }
  78. }
  79. }
  80.  
  81.  
  82. ///
  83. /// 引数取得
  84. ///
  85. if (CommandLine.arguments.count <= 1) {
  86. print("画像ファイルを指定してください")
  87. exit(0)
  88. }
  89. var imgResize = imgMG(p: String(CommandLine.arguments[1]))
  90. ///
  91. /// 3種類の画像出力
  92. ///
  93. print(imgResize.resizeImg(w: 50, h: 50))
  94. print(imgResize.resizeImg(w: 100, h: 100))
  95. print(imgResize.resizeImg(w: 300, h: 300))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement