File size: 23,818 Bytes
ac73ca8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f5abfa
7fe60a7
 
ac73ca8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bd91a4
ac73ca8
 
4bd91a4
 
ac73ca8
 
 
4bd91a4
 
ac73ca8
6f5abfa
 
7fe60a7
56e5f1a
ac73ca8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56e5f1a
 
 
 
 
 
 
 
 
ac73ca8
 
 
 
 
 
 
7fe60a7
 
 
 
 
ac73ca8
 
 
 
 
 
 
7fe60a7
 
 
 
 
 
 
ac73ca8
7fe60a7
 
 
 
 
 
ac73ca8
7fe60a7
 
ac73ca8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b3187d
 
 
 
 
 
 
 
ac73ca8
 
 
 
 
 
 
 
 
 
 
 
7fe60a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac73ca8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bd91a4
 
 
 
 
 
ac73ca8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
"""
dsp_bridge.py - SPCW Digital Signal Processing Bridge
Unified pipeline: Source β†’ Bake β†’ Transmit β†’ Reconstruct β†’ Display

PARALLEL WAVE ARCHITECTURE:
- Each tile is a "Wave" with a designated endpoint
- Waves transmit in parallel via ThreadPoolExecutor
- Smaller waves = faster transmission (more parallelism)
- Every wave has fractal address = endpoint for reconstruction

This is the transmission backbone. No intermediate files.
Direct memory-to-memory wave transport.
"""

import time
import math
import numpy as np
import cv2
import struct
import threading
from queue import Queue
from typing import Optional, Callable, Tuple, List, Dict
from dataclasses import dataclass, field
from concurrent.futures import ThreadPoolExecutor, as_completed

from .logos_core import (
    calculate_heat_code,
    pack_atom,
    unpack_atom,
    prime_harmonizer,
    PAYLOAD_SIZE,
    ATOM_SIZE,
    META_SIZE,
)
from .network import SHARED_NETWORK
from .baker import BreadBaker
from .fractal_engine import LogosFractalEngine


# ============================================================
# ATOM STRUCTURE
# ============================================================

METADATA_SIZE = 8  # [img_w:2B][img_h:2B][tile_row:1B][tile_col:1B][grid_rows:1B][grid_cols:1B]

def encode_tile_metadata(width: int, height: int, tile_row: int, tile_col: int,
                         grid_rows: int, grid_cols: int) -> bytes:
    """Encode tile metadata into first 8 bytes of payload"""
    return struct.pack('>HHBBBB', width, height, tile_row, tile_col, grid_rows, grid_cols)

def decode_tile_metadata(payload: bytes) -> Tuple[int, int, int, int, int, int]:
    """Decode tile metadata from payload"""
    if len(payload) < 8:
        return (0, 0, 0, 0, 0, 0)
    return struct.unpack('>HHBBBB', payload[:8])


# ============================================================
# WAVE & TRANSMISSION STATS
# ============================================================

@dataclass
class WaveStats:
    """Stats for a single wave (tile)"""
    wave_id: int = 0
    tile_row: int = 0
    tile_col: int = 0
    atoms: int = 0
    bytes: int = 0
    tx_time_ms: float = 0.0
    rx_time_ms: float = 0.0
    endpoint: int = 0  # Heat code (fractal address)


@dataclass
class TransmissionStats:
    """Real-time transmission statistics"""
    atoms_sent: int = 0
    atoms_received: int = 0
    bytes_transmitted: int = 0
    elapsed_ms: float = 0.0
    throughput_mbps: float = 0.0
    ssim: float = 0.0
    tiles_complete: int = 0
    total_tiles: int = 0
    waves: List[WaveStats] = field(default_factory=list)
    parallel_waves: int = 0
    
    @property
    def progress(self) -> float:
        if self.total_tiles == 0:
            return 0.0
        return self.tiles_complete / self.total_tiles
    
    @property
    def avg_wave_time_ms(self) -> float:
        if not self.waves:
            return 0.0
        return sum(w.tx_time_ms for w in self.waves) / len(self.waves)


# ============================================================
# DSP BRIDGE - PARALLEL WAVE ARCHITECTURE
# ============================================================

class DSPBridge:
    """
    Digital Signal Processing Bridge for SPCW Transport.
    
    AUTOMATIC WAVE ARCHITECTURE:
    - Image divided into 512x512 CHUNKS
    - Each chunk subdivided into 8x8 = 64 WAVES
    - Total waves = (chunks_x * 8) Γ— (chunks_y * 8)
    - Example: 4096x4096 β†’ 8x8 chunks β†’ 64x64 = 4096 waves
    
    Pipeline:
    1. Ingest source β†’ 2. Auto-chunk β†’ 3. Parallel encode
    4. Parallel decode β†’ 5. Verify SSIM β†’ 6. Display
    """
    
    WINDOW_NAME = "SPCW Live Transport"
    CHUNK_SIZE = 512  # Each chunk is 512x512
    WAVES_PER_CHUNK = 8  # 8x8 = 64 waves per chunk
    
    def __init__(self, grid_size: Optional[int] = None, num_workers: int = 64,
                 viewport_size: Tuple[int, int] = (1280, 720)):
        """
        Initialize DSP Bridge.
        AES-256 Adaptive Grid Support.
        """
        self.num_workers = num_workers
        self.viewport_size = viewport_size
        self.forced_grid_size = grid_size
        self.grid_size = grid_size if grid_size else 8
        
        # Use Shared Network Instance (Optimization)
        self.network = SHARED_NETWORK
        self.baker = BreadBaker()
        
        # Wave buffers: wave_id -> list of atoms
        self.wave_buffers: Dict[int, List[bytes]] = {}
        
        # State
        self.source_image: Optional[np.ndarray] = None
        self.canvas: Optional[np.ndarray] = None
        self.canvas_width = 0
        self.canvas_height = 0
        self.tile_w = 0
        self.tile_h = 0
        
        # Stats
        self.stats = TransmissionStats()
        
        # Control
        self._stop_flag = False
        self._is_running = False
        
        # Callbacks
        self.on_stats_update: Optional[Callable[[TransmissionStats], None]] = None
    
    def _tile_to_quadtree_path(self, tile_row: int, tile_col: int) -> List[int]:
        """Convert tile position to quadtree path for heat code"""
        path = []
        r_start, r_end = 0, self.grid_size
        c_start, c_end = 0, self.grid_size
        
        for _ in range(16):
            if r_end - r_start <= 1 and c_end - c_start <= 1:
                break
            
            r_mid = (r_start + r_end) // 2
            c_mid = (c_start + c_end) // 2
            
            in_bottom = tile_row >= r_mid if r_mid < r_end else False
            in_right = tile_col >= c_mid if c_mid < c_end else False
            
            quadrant = (int(in_bottom) << 1) | int(in_right)
            path.append(quadrant)
            
            if in_bottom:
                r_start = r_mid
            else:
                r_end = r_mid
            if in_right:
                c_start = c_mid
            else:
                c_end = c_mid
        
        return path
    
    def _encode_wave(self, wave_id: int, tile_row: int, tile_col: int, 
                     tile_data: np.ndarray) -> Tuple[int, List[bytes], WaveStats]:
        """
        Encode a single wave (tile) into atoms.
        This is a PURE FUNCTION - can run in parallel.
        
        Returns: (wave_id, list of atoms, wave stats)
        """
        start_time = time.perf_counter()
        
        # Compute fractal endpoint (heat code) from tile position
        path = self._tile_to_quadtree_path(tile_row, tile_col)
        heat_code = calculate_heat_code(path)
        
        # Validate against instantiated Prime Network
        # "Discrete wave transmission" aligned with topology
        is_manifold_aligned = self.network.validate_wave(heat_code)
        
        # Select domain based on topological alignment
        # Manifold aligned -> medium (standard)
        # Off-manifold -> small (constrained/filtered)
        domain_key = "medium" if is_manifold_aligned else "small"
        
        # Build metadata
        meta = encode_tile_metadata(
            self.canvas_width, self.canvas_height,
            tile_row, tile_col,
            self.grid_size, self.grid_size
        )
        
        # Flatten tile pixels REMOVED - Using Fractal Compression
        
        # Encode with BreadBaker (Fractal Compression)
        # We pass empty prefix so atoms use LOCAL addressing (relative to tile)
        atom_defs = self.baker.bake(tile_data, prefix_path=[])
        
        # Calculate payload capacity
        PIXEL_DATA_SIZE = PAYLOAD_SIZE - META_SIZE - METADATA_SIZE
        
        # Encode atoms
        atoms = []
        chunk_idx = 0
        
        for atom_def in atom_defs:
            local_path = atom_def['path_bits']
            payload_data = atom_def['payload']
            
            # Calculate LOCAL heat code for the atom header
            atom_heat_code = calculate_heat_code(local_path)
            
            # Build payload: metadata + compressed data
            # Note: We append tile metadata to EVERY atom currently, which is overhead
            # but required for stateless decoding if packets drop.
            # Optimization: Only first atom needs it? 
            # But we stick to protocol: metadata first.
            payload = meta + payload_data
            
            # Pack atom
            atom = pack_atom(atom_heat_code, payload, domain_key=domain_key, gap_id=chunk_idx)
            atoms.append(atom)
            chunk_idx += 1
        
        elapsed = (time.perf_counter() - start_time) * 1000
        
        wave_stats = WaveStats(
            wave_id=wave_id,
            tile_row=tile_row,
            tile_col=tile_col,
            atoms=len(atoms),
            bytes=len(atoms) * ATOM_SIZE,
            tx_time_ms=elapsed,
            endpoint=heat_code
        )
        
        return wave_id, atoms, wave_stats
    
    def _decode_wave(self, wave_id: int, atoms: List[bytes]) -> Tuple[int, int, int, np.ndarray, WaveStats]:
        """
        Decode a wave from atoms back to tile pixels.
        This is a PURE FUNCTION - can run in parallel.
        
        VECTORIZED: Uses numpy reshape instead of per-pixel loop.
        
        Returns: (wave_id, tile_row, tile_col, tile_pixels, wave_stats)
        """
        start_time = time.perf_counter()
        
        # Unpack first atom to get metadata
        if not atoms:
            # Handle empty atoms - return blank tile
            elapsed = (time.perf_counter() - start_time) * 1000
            wave_stats = WaveStats(
                wave_id=wave_id, atoms=0, bytes=0, rx_time_ms=elapsed
            )
            return wave_id, 0, 0, np.zeros((1, 1, 3), dtype=np.uint8), wave_stats

        first = unpack_atom(atoms[0])
        heat_code, payload, _, _ = first
        img_w, img_h, tile_row, tile_col, grid_rows, grid_cols = decode_tile_metadata(payload)
        
        # Calculate tile dimensions
        tile_h = math.ceil(img_h / grid_rows)
        tile_w = math.ceil(img_w / grid_cols)
        y0 = tile_row * tile_h
        x0 = tile_col * tile_w
        actual_h = min(tile_h, img_h - y0)
        actual_w = min(tile_w, img_w - x0)
        
        # Initialize Fractal Engine for this tile (Local Scope)
        fractal_engine = LogosFractalEngine(min_bucket_size=1) # High res
        
        # Process atoms
        # Each atom contains local instruction for the quadrant
        for atom in atoms:
            hc, pl, _, _ = unpack_atom(atom)
            # Skip metadata bytes [0:8] to get Baker payload
            # Baker payload: [Control/Data...]
            baker_payload = pl[METADATA_SIZE:] 
            
            # Helper: hex string of heat code
            hex_str = f"{hc:08x}"
            fractal_engine.process_atom(hex_str, baker_payload)
            
        # Draw Tile Result
        tile = fractal_engine.draw_viewport((actual_w, actual_h))
        
        elapsed = (time.perf_counter() - start_time) * 1000
        
        wave_stats = WaveStats(
            wave_id=wave_id,
            tile_row=tile_row,
            tile_col=tile_col,
            atoms=len(atoms),
            bytes=len(atoms) * ATOM_SIZE,
            rx_time_ms=elapsed,
            endpoint=heat_code
        )
        
        return wave_id, tile_row, tile_col, tile, wave_stats
    
    def _parallel_encode(self) -> Dict[int, List[bytes]]:
        """
        Parallel wave encoding - all waves encode simultaneously.
        Returns: dict mapping wave_id -> list of atoms
        """
        h, w = self.source_image.shape[:2]
        self.canvas_width = w
        self.canvas_height = h
        self.tile_h = math.ceil(h / self.grid_size)
        self.tile_w = math.ceil(w / self.grid_size)
        total_waves = self.grid_size * self.grid_size
        self.stats.total_tiles = total_waves
        self.stats.parallel_waves = min(self.num_workers, total_waves)
        
        # Prepare wave tasks
        tasks = []
        wave_id = 0
        for tr in range(self.grid_size):
            for tc in range(self.grid_size):
                # Extract tile region
                y0 = tr * self.tile_h
                y1 = min(h, y0 + self.tile_h)
                x0 = tc * self.tile_w
                x1 = min(w, x0 + self.tile_w)
                
                tile = self.source_image[y0:y1, x0:x1, :].copy()
                tasks.append((wave_id, tr, tc, tile))
                wave_id += 1
        
        # Parallel encode all waves
        wave_atoms: Dict[int, List[bytes]] = {}
        
        with ThreadPoolExecutor(max_workers=self.num_workers) as executor:
            futures = {
                executor.submit(self._encode_wave, wid, tr, tc, tile): wid
                for wid, tr, tc, tile in tasks
            }
            
            for future in as_completed(futures):
                wid, atoms, wave_stat = future.result()
                wave_atoms[wid] = atoms
                self.stats.atoms_sent += len(atoms)
                self.stats.bytes_transmitted += len(atoms) * ATOM_SIZE
                self.stats.waves.append(wave_stat)
        
        return wave_atoms
    
    def _parallel_decode(self, wave_atoms: Dict[int, List[bytes]]):
        """
        Parallel wave decoding - all waves decode simultaneously.
        """
        # Initialize canvas
        self.canvas = np.zeros((self.canvas_height, self.canvas_width, 3), dtype=np.uint8)
        
        # Parallel decode all waves
        with ThreadPoolExecutor(max_workers=self.num_workers) as executor:
            futures = {
                executor.submit(self._decode_wave, wid, atoms): wid
                for wid, atoms in wave_atoms.items()
            }
            
            for future in as_completed(futures):
                wid, tile_row, tile_col, tile, wave_stat = future.result()
                
                # Place tile at designated endpoint
                y0 = tile_row * self.tile_h
                x0 = tile_col * self.tile_w
                h, w = tile.shape[:2]
                self.canvas[y0:y0+h, x0:x0+w] = tile
                
                self.stats.atoms_received += wave_stat.atoms
                self.stats.tiles_complete += 1
    
    def _calculate_ssim(self) -> float:
        """Calculate SSIM between source and reconstructed"""
        if self.source_image is None or self.canvas is None:
            return 0.0
        
        if self.source_image.shape != self.canvas.shape:
            return 0.0
        
        # Exact match check
        if np.array_equal(self.source_image, self.canvas):
            return 1.0
        
        # MSE-based approximation
        gray_src = cv2.cvtColor(self.source_image, cv2.COLOR_RGB2GRAY)
        gray_dst = cv2.cvtColor(self.canvas, cv2.COLOR_RGB2GRAY)
        
        mse = np.mean((gray_src.astype(float) - gray_dst.astype(float)) ** 2)
        if mse == 0:
            return 1.0
        
        psnr = 10 * np.log10(255.0 ** 2 / mse)
        return min(1.0, psnr / 60.0)
    
    def _calculate_grid(self, width: int, height: int) -> int:
        """
        Auto-calculate optimal grid size based on image dimensions.
        
        Strategy:
        - Divide image into 512x512 chunks
        - Each chunk gets 8x8 = 64 waves
        - Minimum 8x8 grid, scales up for larger images
        """
        chunks_x = max(1, math.ceil(width / self.CHUNK_SIZE))
        chunks_y = max(1, math.ceil(height / self.CHUNK_SIZE))
        
        # Grid = chunks Γ— waves_per_chunk_side
        grid_x = chunks_x * self.WAVES_PER_CHUNK
        grid_y = chunks_y * self.WAVES_PER_CHUNK
        
        # Use the larger dimension for square grid
        grid_size = max(grid_x, grid_y)
        
        # Cap at reasonable maximum for memory
        grid_size = min(grid_size, 256)
        
        return grid_size
    
    def transmit(self, source_path: str, show_window: bool = True) -> TransmissionStats:
        """
        Main transmission method using AUTO WAVE ARCHITECTURE.
        
        Grid size is automatically determined:
        - 512x512 chunks with 64 waves each
        - Scales with image size
        
        Args:
            source_path: Path to source image
            show_window: Show display window (clean, no stats overlay)
            
        Returns:
            TransmissionStats with final metrics
        """
        self._stop_flag = False
        self._is_running = True
        self.stats = TransmissionStats()  # Reset stats
        
        # Load source
        img = cv2.imread(source_path)
        if img is None:
            raise ValueError(f"Could not load: {source_path}")
        self.source_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        
        h, w = self.source_image.shape[:2]
        
        # AUTO-CALCULATE grid based on image size (unless forced)
        if self.forced_grid_size:
            self.grid_size = self.forced_grid_size
        else:
            self.grid_size = self._calculate_grid(w, h)
            
        total_waves = self.grid_size * self.grid_size
        
        chunks_x = math.ceil(w / self.CHUNK_SIZE)
        chunks_y = math.ceil(h / self.CHUNK_SIZE)
        
        print(f"[DSP] Source: {w}x{h}")
        print(f"[DSP] Chunks: {chunks_x}x{chunks_y} ({chunks_x * chunks_y} @ 512x512)")
        print(f"[DSP] Waves: {self.grid_size}x{self.grid_size} = {total_waves}")
        print(f"[DSP] Workers: {self.num_workers} parallel")
        
        start_time = time.perf_counter()
        
        # PHASE 1: Parallel encode all waves
        encode_start = time.perf_counter()
        wave_atoms = self._parallel_encode()
        encode_time = (time.perf_counter() - encode_start) * 1000
        
        # PHASE 2: Parallel decode all waves
        decode_start = time.perf_counter()
        self._parallel_decode(wave_atoms)
        decode_time = (time.perf_counter() - decode_start) * 1000
        
        # Calculate final stats
        elapsed = time.perf_counter() - start_time
        self.stats.elapsed_ms = elapsed * 1000
        self.stats.throughput_mbps = (self.stats.bytes_transmitted / (1024 * 1024)) / elapsed if elapsed > 0 else 0
        self.stats.ssim = self._calculate_ssim()
        
        print(f"[DSP] Encode: {encode_time:.1f}ms | Decode: {decode_time:.1f}ms")
        print(f"[DSP] Transmitted: {self.stats.atoms_sent} atoms ({total_waves} waves)")
        print(f"[DSP] Time: {self.stats.elapsed_ms:.1f}ms")
        print(f"[DSP] Throughput: {self.stats.throughput_mbps:.2f} MB/s")
        print(f"[DSP] Avg wave: {self.stats.avg_wave_time_ms:.2f}ms")
        print(f"[DSP] SSIM: {self.stats.ssim:.6f} {'(PERFECT)' if self.stats.ssim == 1.0 else ''}")
        
        self._is_running = False
        
        # Display
        if show_window and self.canvas is not None:
            self._show_window()
        
        return self.stats
    
    def _show_window(self):
        """Display clean result window - no stats overlay (stats go to launcher)"""
        try:
            cv2.namedWindow(self.WINDOW_NAME, cv2.WINDOW_NORMAL)
            cv2.resizeWindow(self.WINDOW_NAME, self.viewport_size[0], self.viewport_size[1])
        except Exception as e:
            print(f"[WARN] Could not create window: {e}")
            return
        
        print(f"\n[CONTROLS] S: Side-by-side | Q: Quit")
        
        show_comparison = False
        
        try:
            while not self._stop_flag:
                if show_comparison and self.source_image is not None:
                    # Side-by-side: Original | Reconstructed
                    h = max(self.source_image.shape[0], self.canvas.shape[0])
                    w = self.source_image.shape[1] + self.canvas.shape[1] + 4
                    frame = np.zeros((h, w, 3), dtype=np.uint8)
                    frame[:self.source_image.shape[0], :self.source_image.shape[1]] = self.source_image
                    frame[:self.canvas.shape[0], self.source_image.shape[1]+4:] = self.canvas
                    
                    # Scale to fit viewport
                    scale = min(self.viewport_size[0] / w, self.viewport_size[1] / h)
                    frame = cv2.resize(frame, (int(w * scale), int(h * scale)), 
                                       interpolation=cv2.INTER_NEAREST)
                else:
                    # Just reconstructed - CLEAN, no overlay
                    scale = min(self.viewport_size[0] / self.canvas_width,
                               self.viewport_size[1] / self.canvas_height)
                    frame = cv2.resize(self.canvas, 
                                       (int(self.canvas_width * scale), int(self.canvas_height * scale)),
                                       interpolation=cv2.INTER_NEAREST)
                
                # Convert and display - NO TEXT OVERLAY
                display = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
                cv2.imshow(self.WINDOW_NAME, display)
                key = cv2.waitKey(30)
                
                # Check if window was closed
                try:
                    if cv2.getWindowProperty(self.WINDOW_NAME, cv2.WND_PROP_VISIBLE) < 1:
                        break
                except:
                    break
                    
                if key == ord('q') or key == 27:
                    break
                elif key == ord('s'):
                    show_comparison = not show_comparison
        finally:
            try:
                cv2.destroyWindow(self.WINDOW_NAME)
                cv2.waitKey(1)
            except:
                pass
    
    def stop(self):
        """Stop transmission"""
        self._stop_flag = True
    
    def get_canvas(self) -> Optional[np.ndarray]:
        """Get reconstructed image"""
        return self.canvas
    
    def save_output(self, path: str):
        """Save reconstructed image"""
        if self.canvas is not None:
            cv2.imwrite(path, cv2.cvtColor(self.canvas, cv2.COLOR_RGB2BGR))
            print(f"[DSP] Saved: {path}")


# ============================================================
# CLI
# ============================================================

def main():
    import argparse
    
    parser = argparse.ArgumentParser(
        description="SPCW DSP Bridge - Unified Transmission Pipeline",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  python dsp_bridge.py image.png                  # Transmit and display
  python dsp_bridge.py image.png -o output.png   # Save reconstruction
  python dsp_bridge.py image.png --grid 16       # 16x16 tile grid

Controls: S: Side-by-side comparison | Q: Quit
"""
    )
    parser.add_argument("source", help="Source image path")
    parser.add_argument("-o", "--output", help="Save reconstructed image")
    parser.add_argument("--grid", type=int, default=8, help="Grid size (default: 8)")
    parser.add_argument("--workers", type=int, default=8, help="Parallel workers (default: 8)")
    parser.add_argument("--viewport", nargs=2, type=int, default=[1280, 720],
                       metavar=("W", "H"), help="Viewport size")
    parser.add_argument("--no-display", action="store_true", help="No display window")
    
    args = parser.parse_args()
    
    bridge = DSPBridge(
        grid_size=args.grid,
        num_workers=args.workers,
        viewport_size=tuple(args.viewport)
    )
    
    try:
        stats = bridge.transmit(args.source, show_window=not args.no_display)
        
        if args.output:
            bridge.save_output(args.output)
        
        # Exit code based on SSIM
        if stats.ssim < 1.0:
            print(f"[WARN] Lossy transmission: SSIM={stats.ssim:.6f}")
    
    except Exception as e:
        print(f"[ERROR] {e}")
        raise


if __name__ == "__main__":
    main()