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