4 * BitmapRegionDecoder usage example for Swift
5 * Demonstrates working with bitmap region decoding, image processing, and rectangle operations
7class BitmapRegionDecoderExample: NSObject {
8 private var decoder: BitmapRegionDecoder?
9 private var imageData: Data?
17 * Load sample image data for demonstration
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)")
27 * Demonstrate BitmapRegionDecoder methods
29 func demonstrateBitmapRegionDecoderMethods() {
30 guard let imageData = imageData else {
31 print("No image data available")
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]
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]
48 // [swift_BitmapRegionDecoder_getWidth]
49 let sourceWidth = decoder!.width
50 print("Source image width: \(sourceWidth)")
51 // [swift_BitmapRegionDecoder_getWidth]
53 // [swift_BitmapRegionDecoder_getHeight]
54 let sourceHeight = decoder!.height
55 print("Source image height: \(sourceHeight)")
56 // [swift_BitmapRegionDecoder_getHeight]
58 // Demonstrate rectangle creation and usage
59 demonstrateRectangleUsage()
61 // Demonstrate region decoding
62 demonstrateRegionDecoding()
64 // Demonstrate different sample sizes
65 demonstrateSampleSizes()
67 // Demonstrate multiple regions
68 demonstrateMultipleRegions()
72 * Demonstrate Rectangle usage
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]
81 // [swift_Rectangle_getX]
84 print("Rectangle X coordinate: \(x)")
85 // [swift_Rectangle_getX]
87 // [swift_Rectangle_getY]
90 print("Rectangle Y coordinate: \(y)")
91 // [swift_Rectangle_getY]
93 // [swift_Rectangle_getWidth]
95 let width = rect1.width
96 print("Rectangle width: \(width)")
97 // [swift_Rectangle_getWidth]
99 // [swift_Rectangle_getHeight]
101 let height = rect1.height
102 print("Rectangle height: \(height)")
103 // [swift_Rectangle_getHeight]
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)
111 print("Created different rectangles for different regions")
115 * Demonstrate region decoding
117 func demonstrateRegionDecoding() {
118 guard let decoder = decoder else {
119 print("Decoder not initialized")
123 // Create a sample rectangle
124 let sampleRect = Rectangle(x: 50, y: 50, width: 200, height: 200)
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]
132 // Demonstrate the decoded image
133 demonstrateDecodedImage(decodedImage, description: "Sample Region")
137 * Demonstrate different sample sizes
139 func demonstrateSampleSizes() {
140 guard let decoder = decoder else { return }
142 let testRect = Rectangle(x: 0, y: 0, width: 400, height: 300)
144 // Decode with different sample sizes
145 let sampleSizes: [Float] = [1.0, 2.0, 4.0, 8.0]
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]
154 demonstrateDecodedImage(decodedImage, description: "Sample Size \(sampleSize)")
159 * Demonstrate multiple regions
161 func demonstrateMultipleRegions() {
162 guard let decoder = decoder else { return }
164 // Define multiple regions to decode
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
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]
180 demonstrateDecodedImage(decodedImage, description: "Region \(index + 1)")
185 * Demonstrate decoded image usage
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")
195 * Demonstrate advanced bitmap region decoder features
197 func demonstrateAdvancedFeatures() {
198 print("=== Advanced BitmapRegionDecoder Features ===")
200 guard let imageData = imageData else { return }
202 // Create multiple decoders for different images
203 let imageDataList = [
205 Data([0xFF, 0xD8, 0xFF, 0xE0]), // JPEG header
206 Data([0x47, 0x49, 0x46, 0x38]) // GIF header
209 for (index, data) in imageDataList.enumerated() {
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]
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)")
223 print("Failed to decode image type \(index + 1): \(error)")
229 * Demonstrate error handling
231 func demonstrateErrorHandling() {
232 print("=== Error Handling ===")
234 // Test with invalid image data
235 let invalidData = Data([0x00, 0x01, 0x02, 0x03])
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]
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")
250 print("Expected error when creating decoder with invalid data: \(error)")
253 // Test with invalid rectangle
254 if let decoder = decoder {
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")
261 print("Expected error with invalid rectangle: \(error)")
267 * Demonstrate performance optimization
269 func demonstratePerformanceOptimization() {
270 print("=== Performance Optimization ===")
272 guard let decoder = decoder else { return }
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]
278 for sampleSize in sampleSizes {
279 let startTime = Date()
281 let image = decoder.decodeRegion(rect: largeRect, sampleSize: sampleSize)
283 let elapsedTime = Date().timeIntervalSince(startTime) * 1000
284 print("Sample size \(sampleSize): \(Int(elapsedTime))ms")
289 * Demonstrate rectangle manipulation
291 func demonstrateRectangleManipulation() {
292 print("=== Rectangle Manipulation ===")
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)")
298 // Create different 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
306 for (index, rect) in variations.enumerated() {
307 print("Variation \(index + 1): \(rect.x), \(rect.y), \(rect.width)x\(rect.height)")
312 * Main demonstration method
315 print("=== BitmapRegionDecoder Example ===")
317 demonstrateBitmapRegionDecoderMethods()
318 demonstrateRectangleManipulation()
319 demonstrateAdvancedFeatures()
320 demonstrateErrorHandling()
321 demonstratePerformanceOptimization()
323 // Wait a bit for processing
324 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
325 print("=== Example completed ===")
331 * Function to run the example
334 let example = BitmapRegionDecoderExample()
337 // Keep the app running for a while to see the output
338 RunLoop.main.run(until: Date().addingTimeInterval(10.0))