-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.swift
More file actions
243 lines (218 loc) · 8.08 KB
/
test.swift
File metadata and controls
243 lines (218 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// ImageDetail.swift
// ImageProcessing
//
// Created by RYDE on 3/18/20.
// Copyright © 2020 Facebook. All rights reserved.
//
import Foundation
import UIKit
class ImagePixelReader {
enum Pixel:Int {
case alpha = 3
}
let image:UIImage
private var data:CFData
private let pointer:UnsafePointer<UInt8>
private let scale:Int
init?(image:UIImage){
self.image = image
guard let cfdata = self.image.cgImage?.dataProvider?.data,
let pointer = CFDataGetBytePtr(cfdata) else {
return nil
}
self.scale = Int(image.scale)
self.data = cfdata
self.pointer = pointer
}
func alphaAt(x:Int,y:Int)->UInt8{
let pixelPosition = (Int(image.size.width) * y * scale + x) * 4 * scale
return pointer[pixelPosition + Pixel.alpha.rawValue]
}
}
@objc(ImageDetail)
class ImageDetail: NSObject {
struct ImageAttributes {
var startX:Int
var startY:Int
var endX:Int
var endY:Int
var startYArray: [Int]
init(
startX:Int,
startY:Int,
endX:Int,
endY:Int,
startYArray:[Int]
) {
self.startX = startX
self.startY = startY
self.endX = endX
self.endY = endY
self.startYArray = startYArray
}
}
@objc
var dimensionsArray:NSArray = [[Int]]() as NSArray
@objc
var cornerCoordinatesArray:NSArray = [[Int]]() as NSArray
var availableArray:Array = Array(repeating: Array(repeating: true, count: 325), count: 215)
var availableArrayFirstIteration:Array = Array(repeating: Array(repeating: true, count: 325), count: 215)
var imagesSizeArray:Array = [Int]()
var sortedImagesSizeArray:Array = [Int]()
var imagesWidthArray:Array = [Int]()
var imagesArray:Array = [[[Bool]]]()
var imagesCornerCoordinateArray:Array = [[Int]]()
@objc
func setImageURIs(_ url: NSString) {
//print(Date())
var imageDimensionsArray:Array = [[Int]]()
for i in 0 ..< 5{
let fullURL = url as String + String(i+1) + ".png"
guard let imageURL = URL(string: fullURL) else { return }
guard let imageData = try? Data(contentsOf: imageURL) else { return }
guard let imageUI = UIImage(data: imageData) else { return }
//getting all the pixels
if let reader = ImagePixelReader(image: imageUI) {
var imageAttributes = ImageAttributes(startX: -1,startY: Int(imageUI.size.height), endX: -1, endY: -1, startYArray: [])
//iterate over all pixels
for x in 0 ..< Int(imageUI.size.width){
var firstPixelInColumn = false
for y in 0 ..< Int(imageUI.size.height){
let isPixelTransparent = reader.alphaAt(x: x, y: y) == 0
if !isPixelTransparent {
if imageAttributes.startX == -1 {
imageAttributes.startX = x
}
imageAttributes.endX = x
if !firstPixelInColumn {
firstPixelInColumn = true
imageAttributes.startYArray.append(y)
}
if imageAttributes.endY <= y {
imageAttributes.endY = y
}
}
}
}//x loop ends
imageAttributes.startY = imageAttributes.startYArray.min()!
let maxWidth = imageAttributes.endX + 1 - imageAttributes.startX
let maxHeight = imageAttributes.endY + 1 - imageAttributes.startY
var imageArray = Array(repeating: Array(repeating: false, count: maxHeight), count: maxWidth)
for x in 0 ..< imageAttributes.endX {
for y in 0 ..< imageAttributes.endY {
let isPixelTransparent = reader.alphaAt(x: x, y: y) == 0
if !isPixelTransparent {
let indexX = x - imageAttributes.startX
let indexY = y - imageAttributes.startY
imageArray[indexX][indexY] = true
}
}
}
imagesArray.append(imageArray)
imageDimensionsArray.append([Int(imageAttributes.startX), Int(imageAttributes.endX + 1), Int(imageAttributes.startY), Int(imageAttributes.endY+1)])
imagesSizeArray.append((imageAttributes.endX + 1 - imageAttributes.startX) * (imageAttributes.endY + 1 - imageAttributes.startY))
imagesWidthArray.append(imageAttributes.endX + 1 - imageAttributes.startX)
}
}
sortedImagesSizeArray = imagesSizeArray.map { $0 }
//sortedImagesSizeArray = imagesSizeArray.map { $0 }.sorted { $0 > $1 }
dimensionsArray = imageDimensionsArray as NSArray
//select a corner randomly for positioning the largest image
let startRandomCorner = Int.random(in: 0 ..< 3) //includes 3
var startX = 0
var startY = 0
var indices = [0,0,0,0,0]
for i in 0..<5 {
indices[i] = imagesSizeArray.firstIndex(of: sortedImagesSizeArray[i])!
}
let imageWidth = imagesWidthArray[indices[0]]
let imageHeight = sortedImagesSizeArray[0] / imageWidth
if startRandomCorner % 2 == 1 {
startX = 215 - imageWidth
}
if startRandomCorner / 2 >= 1 {
startY = 325 - imageHeight
}
imagesCornerCoordinateArray.append([startX, startY])
for x in startX ..< startX + imageWidth{
for y in startY ..< startY + imageHeight{
let i = x - startX;
let j = y - startY;
if imagesArray[indices[0]][i][j] {
availableArray[x][y] = false
availableArrayFirstIteration[x][y] = false
}
}
}
generateRandomPosition(1)
var unsortedImagesCornerCoordinateArray = [[Int]]()
for i in [0,1,2,3,4] {
let inverseIndex = indices.firstIndex(of: i) ?? 0
unsortedImagesCornerCoordinateArray.append(imagesCornerCoordinateArray[inverseIndex] )
}
cornerCoordinatesArray = unsortedImagesCornerCoordinateArray as NSArray
//print(Date())
}
func generateRandomPosition(
_ depthNumber: Int) {
if (depthNumber < 5) {
guard let nextIndex = imagesSizeArray.firstIndex(of:sortedImagesSizeArray[depthNumber] ) else { return }
let nextImageWidth = imagesWidthArray[nextIndex]
let nextImageHeight = sortedImagesSizeArray[depthNumber] / nextImageWidth
var isIternationCountExceeded = false
var iterationCount = 0
L1: while true {
iterationCount += 1
let nextRandomNumberX = Int.random(in: 0 ..< (215 - nextImageWidth))
let nextRandomNumberY = Int.random(in: 0 ..< (325 - nextImageHeight))
for x in nextRandomNumberX ..< nextRandomNumberX + nextImageWidth{
for y in nextRandomNumberY ..< nextRandomNumberY + nextImageHeight{
let i = x - nextRandomNumberX;
let j = y - nextRandomNumberY;
if imagesArray[nextIndex][i][j] && !availableArray[x][y] {
if iterationCount <= 50 {
continue L1
} else { // bring state of all variables to the point where recursion was called for the first time
let corner = imagesCornerCoordinateArray[0].map { $0 }
imagesCornerCoordinateArray.removeAll()
imagesCornerCoordinateArray.append(corner)
availableArray = availableArrayFirstIteration.map { $0 }
isIternationCountExceeded = true
break L1
}
}
}
}
for x in nextRandomNumberX ..< nextRandomNumberX + nextImageWidth{
for y in nextRandomNumberY ..< nextRandomNumberY + nextImageHeight{
let i = x - nextRandomNumberX;
let j = y - nextRandomNumberY;
if imagesArray[nextIndex][i][j] {
availableArray[x][y] = false
}
}
}
imagesCornerCoordinateArray.append([nextRandomNumberX, nextRandomNumberY])
break L1
}
if isIternationCountExceeded {
generateRandomPosition(1)
} else {
generateRandomPosition(depthNumber + 1)
}
}
}
@objc
func getDimensions(_ callback: RCTResponseSenderBlock) {
callback([NSNull(), dimensionsArray])
}
@objc
func getCornerCoordinates(_ callback: RCTResponseSenderBlock) {
callback([NSNull(), cornerCoordinatesArray])
}
@objc
static func requiresMainQueueSetup() -> Bool {
return true
}
}