Loading...
Searching...
No Matches
BitmapRegionDecoderExample.swift
Go to the documentation of this file.
1import Foundation
2
3/**
4 * BitmapRegionDecoder usage example for Swift
5 * Demonstrates working with bitmap region decoding, image processing, and rectangle operations
6 */
7class BitmapRegionDecoderExample: NSObject {
8 private var decoder: BitmapRegionDecoder?
9 private var imageData: Data?
10
11 override init() {
12 super.init()
13 loadSampleImageData()
14 }
15
16 /**
17 * Load sample image data for demonstration
18 */
19 private func loadSampleImageData() {
20 // Simulate loading image data (in real app, this would be from file or network)
21 let pngHeader: [UInt8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] // PNG header
22 imageData = Data(pngHeader)
23 print("Sample image data loaded (\‍(imageData!.count) bytes)")
24 }
25
26 /**
27 * Demonstrate BitmapRegionDecoder methods
28 */
29 func demonstrateBitmapRegionDecoderMethods() {
30 guard let imageData = imageData else {
31 print("No image data available")
32 return
33 }
34
35 // [swift_BitmapRegionDecoder_newInstance]
36 // Create new instance of BitmapRegionDecoder
37 decoder = BitmapRegionDecoder.newInstance(data: imageData)
38 print("Created BitmapRegionDecoder instance")
39 // [swift_BitmapRegionDecoder_newInstance]
40
41 // [swift_BitmapRegionDecoder_newInstanceFromImage]
42 // When you already have an Image (e.g. from SDK), create decoder without passing raw bytes again
43 let imageForDecoder: Image? = nil // set from your pipeline when available
44 let decoderFromImage = BitmapRegionDecoder.newInstanceFromImage(data: imageForDecoder)
45 print("Decoder from Image: \‍(decoderFromImage != nil)")
46 // [swift_BitmapRegionDecoder_newInstanceFromImage]
47
48 // [swift_BitmapRegionDecoder_getWidth]
49 let sourceWidth = decoder!.width
50 print("Source image width: \‍(sourceWidth)")
51 // [swift_BitmapRegionDecoder_getWidth]
52
53 // [swift_BitmapRegionDecoder_getHeight]
54 let sourceHeight = decoder!.height
55 print("Source image height: \‍(sourceHeight)")
56 // [swift_BitmapRegionDecoder_getHeight]
57
58 // Demonstrate rectangle creation and usage
59 demonstrateRectangleUsage()
60
61 // Demonstrate region decoding
62 demonstrateRegionDecoding()
63
64 // Demonstrate different sample sizes
65 demonstrateSampleSizes()
66
67 // Demonstrate multiple regions
68 demonstrateMultipleRegions()
69 }
70
71 /**
72 * Demonstrate Rectangle usage
73 */
74 func demonstrateRectangleUsage() {
75 // [swift_Rectangle_constructor]
76 // Create rectangle with x, y, width, height
77 let rect1 = Rectangle(x: 10, y: 20, width: 100, height: 150)
78 print("Created rectangle: x=\‍(rect1.x), y=\‍(rect1.y), width=\‍(rect1.width), height=\‍(rect1.height)")
79 // [swift_Rectangle_constructor]
80
81 // [swift_Rectangle_getX]
82 // Get X coordinate
83 let x = rect1.x
84 print("Rectangle X coordinate: \‍(x)")
85 // [swift_Rectangle_getX]
86
87 // [swift_Rectangle_getY]
88 // Get Y coordinate
89 let y = rect1.y
90 print("Rectangle Y coordinate: \‍(y)")
91 // [swift_Rectangle_getY]
92
93 // [swift_Rectangle_getWidth]
94 // Get width
95 let width = rect1.width
96 print("Rectangle width: \‍(width)")
97 // [swift_Rectangle_getWidth]
98
99 // [swift_Rectangle_getHeight]
100 // Get height
101 let height = rect1.height
102 print("Rectangle height: \‍(height)")
103 // [swift_Rectangle_getHeight]
104
105 // Create different rectangles for different use cases
106 let fullImage = Rectangle(x: 0, y: 0, width: 1024, height: 768)
107 let topLeft = Rectangle(x: 0, y: 0, width: 512, height: 384)
108 let center = Rectangle(x: 256, y: 192, width: 512, height: 384)
109 let bottomRight = Rectangle(x: 512, y: 384, width: 512, height: 384)
110
111 print("Created different rectangles for different regions")
112 }
113
114 /**
115 * Demonstrate region decoding
116 */
117 func demonstrateRegionDecoding() {
118 guard let decoder = decoder else {
119 print("Decoder not initialized")
120 return
121 }
122
123 // Create a sample rectangle
124 let sampleRect = Rectangle(x: 50, y: 50, width: 200, height: 200)
125
126 // [swift_BitmapRegionDecoder_decodeRegion]
127 // Decode region with sample size 1 (full resolution)
128 let decodedImage = decoder.decodeRegion(rect: sampleRect, sampleSize: 1)
129 print("Decoded region: \‍(sampleRect.width)x\‍(sampleRect.height) at sample size 1")
130 // [swift_BitmapRegionDecoder_decodeRegion]
131
132 // Demonstrate the decoded image
133 demonstrateDecodedImage(decodedImage, description: "Sample Region")
134 }
135
136 /**
137 * Demonstrate different sample sizes
138 */
139 func demonstrateSampleSizes() {
140 guard let decoder = decoder else { return }
141
142 let testRect = Rectangle(x: 0, y: 0, width: 400, height: 300)
143
144 // Decode with different sample sizes
145 let sampleSizes: [Float] = [1.0, 2.0, 4.0, 8.0]
146
147 for sampleSize in sampleSizes {
148 // [swift_BitmapRegionDecoder_decodeRegion_sampleSize]
149 // Decode region with specific sample size
150 let decodedImage = decoder.decodeRegion(rect: testRect, sampleSize: sampleSize)
151 print("Decoded region with sample size \‍(sampleSize): \‍(Int(testRect.width / sampleSize))x\‍(Int(testRect.height / sampleSize))")
152 // [swift_BitmapRegionDecoder_decodeRegion_sampleSize]
153
154 demonstrateDecodedImage(decodedImage, description: "Sample Size \‍(sampleSize)")
155 }
156 }
157
158 /**
159 * Demonstrate multiple regions
160 */
161 func demonstrateMultipleRegions() {
162 guard let decoder = decoder else { return }
163
164 // Define multiple regions to decode
165 let regions = [
166 Rectangle(x: 0, y: 0, width: 256, height: 256), // Top-left quadrant
167 Rectangle(x: 256, y: 0, width: 256, height: 256), // Top-right quadrant
168 Rectangle(x: 0, y: 256, width: 256, height: 256), // Bottom-left quadrant
169 Rectangle(x: 256, y: 256, width: 256, height: 256) // Bottom-right quadrant
170 ]
171
172 print("=== Decoding Multiple Regions ===")
173 for (index, region) in regions.enumerated() {
174 // [swift_BitmapRegionDecoder_decodeRegion_multiple]
175 // Decode multiple regions
176 let decodedImage = decoder.decodeRegion(rect: region, sampleSize: 1)
177 print("Region \‍(index + 1): \‍(region.width)x\‍(region.height) at (\‍(region.x), \‍(region.y))")
178 // [swift_BitmapRegionDecoder_decodeRegion_multiple]
179
180 demonstrateDecodedImage(decodedImage, description: "Region \‍(index + 1)")
181 }
182 }
183
184 /**
185 * Demonstrate decoded image usage
186 */
187 func demonstrateDecodedImage(_ image: Any, description: String) {
188 print("--- \‍(description) ---")
189 // Note: In real implementation, you would access image properties here
190 print("Image decoded successfully")
191 print("---")
192 }
193
194 /**
195 * Demonstrate advanced bitmap region decoder features
196 */
197 func demonstrateAdvancedFeatures() {
198 print("=== Advanced BitmapRegionDecoder Features ===")
199
200 guard let imageData = imageData else { return }
201
202 // Create multiple decoders for different images
203 let imageDataList = [
204 imageData,
205 Data([0xFF, 0xD8, 0xFF, 0xE0]), // JPEG header
206 Data([0x47, 0x49, 0x46, 0x38]) // GIF header
207 ]
208
209 for (index, data) in imageDataList.enumerated() {
210 do {
211 // [swift_BitmapRegionDecoder_newInstance_advanced]
212 // Create decoder for different image types
213 let decoder = BitmapRegionDecoder.newInstance(data: data)
214 print("Created decoder for image type \‍(index + 1)")
215 // [swift_BitmapRegionDecoder_newInstance_advanced]
216
217 // Test decoding with different regions
218 let testRect = Rectangle(x: 0, y: 0, width: 100, height: 100)
219 let decodedImage = decoder.decodeRegion(rect: testRect, sampleSize: 1)
220 print("Successfully decoded region from image type \‍(index + 1)")
221
222 } catch {
223 print("Failed to decode image type \‍(index + 1): \‍(error)")
224 }
225 }
226 }
227
228 /**
229 * Demonstrate error handling
230 */
231 func demonstrateErrorHandling() {
232 print("=== Error Handling ===")
233
234 // Test with invalid image data
235 let invalidData = Data([0x00, 0x01, 0x02, 0x03])
236
237 do {
238 // [swift_BitmapRegionDecoder_newInstance_error]
239 // Create decoder with invalid data
240 let decoder = BitmapRegionDecoder.newInstance(data: invalidData)
241 print("Created decoder with invalid data")
242 // [swift_BitmapRegionDecoder_newInstance_error]
243
244 // Try to decode region
245 let rect = Rectangle(x: 0, y: 0, width: 50, height: 50)
246 let image = decoder.decodeRegion(rect: rect, sampleSize: 1)
247 print("Successfully decoded region from invalid data")
248
249 } catch {
250 print("Expected error when creating decoder with invalid data: \‍(error)")
251 }
252
253 // Test with invalid rectangle
254 if let decoder = decoder {
255 do {
256 let invalidRect = Rectangle(x: -10, y: -10, width: 100, height: 100)
257 let image = decoder.decodeRegion(rect: invalidRect, sampleSize: 1)
258 print("Successfully decoded region with negative coordinates")
259
260 } catch {
261 print("Expected error with invalid rectangle: \‍(error)")
262 }
263 }
264 }
265
266 /**
267 * Demonstrate performance optimization
268 */
269 func demonstratePerformanceOptimization() {
270 print("=== Performance Optimization ===")
271
272 guard let decoder = decoder else { return }
273
274 // Test different sample sizes for performance
275 let largeRect = Rectangle(x: 0, y: 0, width: 800, height: 600)
276 let sampleSizes: [Float] = [1.0, 2.0, 4.0, 8.0, 16.0]
277
278 for sampleSize in sampleSizes {
279 let startTime = Date()
280
281 let image = decoder.decodeRegion(rect: largeRect, sampleSize: sampleSize)
282
283 let elapsedTime = Date().timeIntervalSince(startTime) * 1000
284 print("Sample size \‍(sampleSize): \‍(Int(elapsedTime))ms")
285 }
286 }
287
288 /**
289 * Demonstrate rectangle manipulation
290 */
291 func demonstrateRectangleManipulation() {
292 print("=== Rectangle Manipulation ===")
293
294 // Create base rectangle
295 let baseRect = Rectangle(x: 100, y: 100, width: 200, height: 150)
296 print("Base rectangle: \‍(baseRect.x), \‍(baseRect.y), \‍(baseRect.width)x\‍(baseRect.height)")
297
298 // Create different variations
299 let variations = [
300 Rectangle(x: baseRect.x, y: baseRect.y, width: baseRect.width / 2, height: baseRect.height), // Half width
301 Rectangle(x: baseRect.x, y: baseRect.y, width: baseRect.width, height: baseRect.height / 2), // Half height
302 Rectangle(x: baseRect.x + 50, y: baseRect.y + 25, width: baseRect.width - 100, height: baseRect.height - 50), // Centered smaller
303 Rectangle(x: 0, y: 0, width: baseRect.width, height: baseRect.height) // Top-left aligned
304 ]
305
306 for (index, rect) in variations.enumerated() {
307 print("Variation \‍(index + 1): \‍(rect.x), \‍(rect.y), \‍(rect.width)x\‍(rect.height)")
308 }
309 }
310
311 /**
312 * Main demonstration method
313 */
314 func runExample() {
315 print("=== BitmapRegionDecoder Example ===")
316
317 demonstrateBitmapRegionDecoderMethods()
318 demonstrateRectangleManipulation()
319 demonstrateAdvancedFeatures()
320 demonstrateErrorHandling()
321 demonstratePerformanceOptimization()
322
323 // Wait a bit for processing
324 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
325 print("=== Example completed ===")
326 }
327 }
328}
329
330/**
331 * Function to run the example
332 */
333func main() {
334 let example = BitmapRegionDecoderExample()
335 example.runExample()
336
337 // Keep the app running for a while to see the output
338 RunLoop.main.run(until: Date().addingTimeInterval(10.0))
339}
340
341// Run the example
342main()