# Codex Brief: GIF Cleanup Studio for iPhone

Build a native iPhone version of GIF Cleanup Studio in SwiftUI. Do not wrap the Electron app.

## Source Files To Study

- `outputs/gif-cleanup-studio/src/App.tsx` for product workflow and screen structure.
- `outputs/gif-cleanup-studio/src/sprite.ts` for sprite-sheet parsing, alignment, color snapping, resizing, starter frame creation.
- `outputs/gif-cleanup-studio/src/gif.ts` for GIF export behavior.
- `outputs/gif-cleanup-studio/src/midi.ts` for MIDI Fighter pad ordering and frame mapping.
- `outputs/gif-cleanup-studio/src/classicHeroSprites.ts` for the CC0 starter cycle.

## Target

- iOS 17+ SwiftUI app, iPhone first, usable on iPad by layout scaling.
- Import sprite sheets from Photos and Files.
- Parse sheets into up to 64 frames with selectable columns, rows/auto rows, background tolerance, color snap tolerance, bottom/center/cell alignment, frame size presets `24/48/64/124/128`.
- Edit one frame at a time with a zoomed pixel canvas, pencil, eraser, eyedropper, grid toggle, and color picker.
- Show the MIDI Fighter model as 4 banks x 16 pads. On iOS, implement CoreMIDI input for real hardware where available; keep touch selection fully usable when hardware is absent.
- Export animated GIF at `8/16/24/30/60` FPS.
- Include a ChatGPT prompt drawer equivalent that reflects the chosen frame size, frame count, FPS, style notes, and learned starter cycle metadata.

## Implementation Requirements

- Model `PixelFrame` as width, height, RGBA `[UInt8]`, sourceIndex, id, name.
- Implement `SpriteSheetParser` with the same algorithm as `sprite.ts`: sample background from corners/edges, copy grid cells, compute non-background bounding boxes, align into a consistent target canvas, then normalize colors by nearest cluster within tolerance.
- Use PhotosUI and `UTType.image` for imports.
- Use CoreGraphics/ImageIO to read source pixels.
- Use `CGImageDestination` with `kUTTypeGIF` for export.
- Use CoreMIDI for MIDI devices.
- Map MIDI Fighter input to frame indices with the same four orientation modes from `midi.ts`: `bottom-left`, `top-left`, `top-right`, `bottom-right`.
- Keep rendering pixel-perfect: no smoothing when drawing frames, stable square pixel cells, no blurry scaling.

## Suggested File Layout

- `GIFCleanupStudioApp.swift`
- `Models/PixelFrame.swift`
- `Models/MidiBinding.swift`
- `Sprite/SpriteSheetParser.swift`
- `Sprite/FrameRenderer.swift`
- `Sprite/ClassicHeroStarter.swift`
- `Export/GifExporter.swift`
- `MIDI/MidiFighterController.swift`
- `Views/ContentView.swift`
- `Views/FrameBankGrid.swift`
- `Views/PixelEditorView.swift`
- `Views/PromptDrawerView.swift`
- `Tests/SpriteSheetParserTests.swift`
- `Tests/MidiMappingTests.swift`
- `Tests/GifExporterTests.swift`

## Checks Before Calling It Done

```bash
xcodebuild -scheme GIFCleanupStudio -destination 'generic/platform=iOS Simulator' build
xcodebuild -scheme GIFCleanupStudio -destination 'generic/platform=iOS Simulator' test
```

- Unit test that a `4x4` sheet produces 16 frames.
- Unit test that bottom alignment keeps all non-empty frames on the same baseline.
- Unit test that MIDI pad orientation maps frame 1 and frame 16 correctly for all four modes.
- Manual device note: CoreMIDI hardware selection must be tested on a real iPhone/iPad with the MIDI Fighter connected through a supported USB/MIDI path.
