IMImagemeter.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // IMImagemeter.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 22/03/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. class IMImagemeterCodable: NSObject {
  25. struct imagemeterAnnotation: Codable {
  26. struct coordinates: Codable {
  27. let x: CGFloat
  28. let y: CGFloat
  29. }
  30. struct color: Codable {
  31. let rgba: [Int]
  32. }
  33. struct end_pt: Codable {
  34. let end_pt: coordinates
  35. enum CodingKeys : String, CodingKey {
  36. case end_pt = "end-pt"
  37. }
  38. }
  39. struct style: Codable {
  40. let color: color?
  41. let line_width: CGFloat?
  42. let text_outline_width: CGFloat?
  43. let font_base_size: CGFloat?
  44. let font_magnification: CGFloat?
  45. enum CodingKeys : String, CodingKey {
  46. case color
  47. case line_width = "line-width"
  48. case text_outline_width = "text-outline-width"
  49. case font_base_size = "font-base-size"
  50. case font_magnification = "font-magnification"
  51. }
  52. }
  53. struct audio_recording: Codable {
  54. let recording_filename: String
  55. let recording_duration_msecs: Int
  56. enum CodingKeys : String, CodingKey {
  57. case recording_filename = "recording-filename"
  58. case recording_duration_msecs = "recording-duration-msecs"
  59. }
  60. }
  61. struct capture_timestamp: Codable {
  62. let year: Int
  63. let month: Int
  64. let day: Int
  65. let hour: Int
  66. let minutes: Int
  67. let seconds: Int
  68. }
  69. struct image: Codable {
  70. let title: String
  71. let filename: String
  72. let annotated_image_filename: String
  73. let rotation: Int
  74. enum CodingKeys : String, CodingKey {
  75. case title
  76. case filename
  77. case annotated_image_filename = "annotated-image-filename"
  78. case rotation
  79. }
  80. }
  81. struct export_image_cache: Codable {
  82. let width: CGFloat
  83. let height: CGFloat
  84. let file_format: String
  85. let with_hardware_antialiasing: Bool
  86. let with_watermark: Bool
  87. let with_image_title: Bool
  88. enum CodingKeys : String, CodingKey {
  89. case width
  90. case height
  91. case file_format = "file-format"
  92. case with_hardware_antialiasing = "with-hardware-antialiasing"
  93. case with_watermark = "with-watermark"
  94. case with_image_title = "with-image-title"
  95. }
  96. }
  97. struct elements: Codable {
  98. let id: Int
  99. let class_: String
  100. let center: coordinates
  101. let width: CGFloat?
  102. let arrows: [end_pt]?
  103. let text: String?
  104. let audio_recording: audio_recording?
  105. let show_border: Bool?
  106. let show_arrows: Bool?
  107. let fill_background: Bool?
  108. let style: style?
  109. enum CodingKeys : String, CodingKey {
  110. case id
  111. case class_ = "class"
  112. case center
  113. case width
  114. case arrows
  115. case text
  116. case audio_recording = "audio-recording"
  117. case show_border = "show-border"
  118. case show_arrows = "show-arrows"
  119. case fill_background = "fill-background"
  120. case style
  121. }
  122. }
  123. struct thumbnails: Codable {
  124. let filename: String
  125. let width: CGFloat
  126. let height: CGFloat
  127. }
  128. let is_example_image: Bool
  129. let version: Int
  130. let capture_timestamp: capture_timestamp
  131. let image: image
  132. let export_image_cache: [export_image_cache]
  133. let elements: [elements]?
  134. let id: String
  135. let thumbnails: [thumbnails]
  136. let last_modification: Int
  137. enum CodingKeys : String, CodingKey {
  138. case is_example_image = "is-example-image"
  139. case version
  140. case capture_timestamp = "capture-timestamp"
  141. case image
  142. case export_image_cache = "export-image-cache"
  143. case elements
  144. case id
  145. case thumbnails
  146. case last_modification = "last-modification"
  147. }
  148. }
  149. @objc static let sharedInstance: IMImagemeterCodable = {
  150. let instance = IMImagemeterCodable()
  151. return instance
  152. }()
  153. func decoderAnnotetion(_ annotation: Data) -> imagemeterAnnotation? {
  154. let jsonDecoder = JSONDecoder.init()
  155. do {
  156. let decode = try jsonDecoder.decode(imagemeterAnnotation.self, from: annotation)
  157. return decode
  158. } catch let error {
  159. print("Serious internal error in decoding metadata ("+error.localizedDescription+")")
  160. return nil
  161. }
  162. }
  163. func convertCoordinate(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) -> (x: CGFloat, y: CGFloat) {
  164. let factor = sqrt(width * height / (1024*768))
  165. let factorX = factor * x + width/2
  166. let factorY = factor * y + height/2
  167. return(factorX, factorY)
  168. }
  169. }