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 // Demonstrate rectangle creation and usage
42 demonstrateRectangleUsage()
44 // Demonstrate region decoding
45 demonstrateRegionDecoding()
47 // Demonstrate different sample sizes
48 demonstrateSampleSizes()
50 // Demonstrate multiple regions
51 demonstrateMultipleRegions()
55 * Demonstrate Rectangle usage
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]
64 // [swift_Rectangle_getX]
67 print("Rectangle X coordinate: \(x)")
68 // [swift_Rectangle_getX]
70 // [swift_Rectangle_getY]
73 print("Rectangle Y coordinate: \(y)")
74 // [swift_Rectangle_getY]
76 // [swift_Rectangle_getWidth]
78 let width = rect1.width
79 print("Rectangle width: \(width)")
80 // [swift_Rectangle_getWidth]
82 // [swift_Rectangle_getHeight]
84 let height = rect1.height
85 print("Rectangle height: \(height)")
86 // [swift_Rectangle_getHeight]
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)
94 print("Created different rectangles for different regions")
98 * Demonstrate region decoding
100 func demonstrateRegionDecoding() {
101 guard let decoder = decoder else {
102 print("Decoder not initialized")
106 // Create a sample rectangle
107 let sampleRect = Rectangle(x: 50, y: 50, width: 200, height: 200)
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]
115 // Demonstrate the decoded image
116 demonstrateDecodedImage(decodedImage, description: "Sample Region")
120 * Demonstrate different sample sizes
122 func demonstrateSampleSizes() {
123 guard let decoder = decoder else { return }
125 let testRect = Rectangle(x: 0, y: 0, width: 400, height: 300)
127 // Decode with different sample sizes
128 let sampleSizes: [Float] = [1.0, 2.0, 4.0, 8.0]
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]
137 demonstrateDecodedImage(decodedImage, description: "Sample Size \(sampleSize)")
142 * Demonstrate multiple regions
144 func demonstrateMultipleRegions() {
145 guard let decoder = decoder else { return }
147 // Define multiple regions to decode
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
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]
163 demonstrateDecodedImage(decodedImage, description: "Region \(index + 1)")
168 * Demonstrate decoded image usage
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")
178 * Demonstrate advanced bitmap region decoder features
180 func demonstrateAdvancedFeatures() {
181 print("=== Advanced BitmapRegionDecoder Features ===")
183 guard let imageData = imageData else { return }
185 // Create multiple decoders for different images
186 let imageDataList = [
188 Data([0xFF, 0xD8, 0xFF, 0xE0]), // JPEG header
189 Data([0x47, 0x49, 0x46, 0x38]) // GIF header
192 for (index, data) in imageDataList.enumerated() {
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]
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)")
206 print("Failed to decode image type \(index + 1): \(error)")
212 * Demonstrate error handling
214 func demonstrateErrorHandling() {
215 print("=== Error Handling ===")
217 // Test with invalid image data
218 let invalidData = Data([0x00, 0x01, 0x02, 0x03])
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]
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")
233 print("Expected error when creating decoder with invalid data: \(error)")
236 // Test with invalid rectangle
237 if let decoder = decoder {
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")
244 print("Expected error with invalid rectangle: \(error)")
250 * Demonstrate performance optimization
252 func demonstratePerformanceOptimization() {
253 print("=== Performance Optimization ===")
255 guard let decoder = decoder else { return }
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]
261 for sampleSize in sampleSizes {
262 let startTime = Date()
264 let image = decoder.decodeRegion(rect: largeRect, sampleSize: sampleSize)
266 let elapsedTime = Date().timeIntervalSince(startTime) * 1000
267 print("Sample size \(sampleSize): \(Int(elapsedTime))ms")
272 * Demonstrate rectangle manipulation
274 func demonstrateRectangleManipulation() {
275 print("=== Rectangle Manipulation ===")
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)")
281 // Create different 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
289 for (index, rect) in variations.enumerated() {
290 print("Variation \(index + 1): \(rect.x), \(rect.y), \(rect.width)x\(rect.height)")
295 * Main demonstration method
298 print("=== BitmapRegionDecoder Example ===")
300 demonstrateBitmapRegionDecoderMethods()
301 demonstrateRectangleManipulation()
302 demonstrateAdvancedFeatures()
303 demonstrateErrorHandling()
304 demonstratePerformanceOptimization()
306 // Wait a bit for processing
307 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
308 print("=== Example completed ===")
314 * Function to run the example
317 let example = BitmapRegionDecoderExample()
320 // Keep the app running for a while to see the output
321 RunLoop.main.run(until: Date().addingTimeInterval(10.0))