|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { View, Text, Platform, TouchableOpacity, StyleSheet } from 'react-native'; |
| 3 | +// The gesture-handler ScrollView yields to the brush pan over the photo, so |
| 4 | +// painting never scrolls the page and scrolling still works everywhere else. |
| 5 | +import { ScrollView } from 'react-native-gesture-handler'; |
| 6 | +import { useSafeAreaInsets } from 'react-native-safe-area-context'; |
| 7 | +import { commonStyles, theme } from '../../theme'; |
| 8 | +import { |
| 9 | + useImage, |
| 10 | + Skia, |
| 11 | + ColorType, |
| 12 | + AlphaType, |
| 13 | + type SkImage as SkiaImageType, |
| 14 | +} from '@shopify/react-native-skia'; |
| 15 | +import { useInpainter, models } from 'react-native-executorch'; |
| 16 | +import ScreenWrapper from '../../components/ScreenWrapper'; |
| 17 | +import { getImage, skImageToBuffer } from '../../utils'; |
| 18 | +import { ModelPicker, type ModelOption } from '../../components/ModelPicker'; |
| 19 | +import { ImageViewport } from '../../components/ImageViewport'; |
| 20 | +import { ModelStatus } from '../../components/ModelStatus'; |
| 21 | +import { LatencyIndicator } from '../../components/LatencyIndicator'; |
| 22 | +import { Button } from '../../components/Button'; |
| 23 | +import { MaskCanvas, rasterizeMask, type MaskStroke } from '../../components/MaskCanvas'; |
| 24 | + |
| 25 | +const MODEL_OPTIONS: ModelOption[] = [ |
| 26 | + { label: 'MI-GAN 512 (XNNPACK FP16)', value: models.inpainting.MIGAN.SIZE_512.XNNPACK_FP16 }, |
| 27 | + { |
| 28 | + label: 'MI-GAN 512 (CoreML FP16)', |
| 29 | + value: models.inpainting.MIGAN.SIZE_512.COREML_FP16, |
| 30 | + disabled: Platform.OS !== 'ios', |
| 31 | + }, |
| 32 | + { |
| 33 | + label: 'MI-GAN 512 (Vulkan FP16)', |
| 34 | + value: models.inpainting.MIGAN.SIZE_512.VULKAN_FP16, |
| 35 | + disabled: Platform.OS !== 'android', |
| 36 | + }, |
| 37 | + { label: 'MI-GAN 256 (XNNPACK FP16)', value: models.inpainting.MIGAN.SIZE_256.XNNPACK_FP16 }, |
| 38 | + { |
| 39 | + label: 'MI-GAN 256 (CoreML FP16)', |
| 40 | + value: models.inpainting.MIGAN.SIZE_256.COREML_FP16, |
| 41 | + disabled: Platform.OS !== 'ios', |
| 42 | + }, |
| 43 | + { |
| 44 | + label: 'MI-GAN 256 (Vulkan FP16)', |
| 45 | + value: models.inpainting.MIGAN.SIZE_256.VULKAN_FP16, |
| 46 | + disabled: Platform.OS !== 'android', |
| 47 | + }, |
| 48 | + { |
| 49 | + label: 'MI-GAN Faces 256 (XNNPACK FP16)', |
| 50 | + value: models.inpainting.MIGAN.FACES_256.XNNPACK_FP16, |
| 51 | + }, |
| 52 | + { |
| 53 | + label: 'MI-GAN Faces 256 (CoreML FP16)', |
| 54 | + value: models.inpainting.MIGAN.FACES_256.COREML_FP16, |
| 55 | + disabled: Platform.OS !== 'ios', |
| 56 | + }, |
| 57 | + { |
| 58 | + label: 'MI-GAN Faces 256 (Vulkan FP16)', |
| 59 | + value: models.inpainting.MIGAN.FACES_256.VULKAN_FP16, |
| 60 | + disabled: Platform.OS !== 'android', |
| 61 | + }, |
| 62 | +]; |
| 63 | + |
| 64 | +/** Brush diameters in image pixels. */ |
| 65 | +const BRUSH_SIZES = [16, 32, 64] as const; |
| 66 | + |
| 67 | +function InpaintingContent() { |
| 68 | + const insets = useSafeAreaInsets(); |
| 69 | + const [selectedModel, setSelectedModel] = useState<any>(MODEL_OPTIONS[0].value); |
| 70 | + const [imageUri, setImageUri] = useState<string | null>(null); |
| 71 | + const [strokes, setStrokes] = useState<readonly MaskStroke[]>([]); |
| 72 | + const [brushWidth, setBrushWidth] = useState<number>(BRUSH_SIZES[1]); |
| 73 | + const [isProcessing, setIsProcessing] = useState(false); |
| 74 | + const [resultImage, setResultImage] = useState<SkiaImageType | null>(null); |
| 75 | + const [showOriginal, setShowOriginal] = useState(false); |
| 76 | + const [latency, setLatency] = useState<number | null>(null); |
| 77 | + const [error, setError] = useState<string | null>(null); |
| 78 | + |
| 79 | + const skiaImage = useImage(imageUri, (err) => setError(err.message || String(err))); |
| 80 | + |
| 81 | + const { |
| 82 | + isReady, |
| 83 | + downloadProgress, |
| 84 | + error: loadError, |
| 85 | + inpaint, |
| 86 | + inpaintWorklet, |
| 87 | + } = useInpainter(selectedModel); |
| 88 | + |
| 89 | + const resetResult = () => { |
| 90 | + setResultImage(null); |
| 91 | + setShowOriginal(false); |
| 92 | + setLatency(null); |
| 93 | + }; |
| 94 | + |
| 95 | + const handlePickImage = async (useCamera: boolean) => { |
| 96 | + setError(null); |
| 97 | + try { |
| 98 | + const uri = await getImage(useCamera); |
| 99 | + if (uri) { |
| 100 | + setImageUri(uri); |
| 101 | + setStrokes([]); |
| 102 | + resetResult(); |
| 103 | + } |
| 104 | + } catch (e: any) { |
| 105 | + setError(e.message || String(e)); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + const runInpainting = async (sync: boolean) => { |
| 110 | + if (!skiaImage || !inpaint || !inpaintWorklet) return; |
| 111 | + if (!sync) setIsProcessing(true); |
| 112 | + setError(null); |
| 113 | + try { |
| 114 | + const image = skImageToBuffer(skiaImage); |
| 115 | + const mask = rasterizeMask(strokes, image.width, image.height); |
| 116 | + if (!mask) throw new Error('Could not rasterize the mask'); |
| 117 | + |
| 118 | + const start = Date.now(); |
| 119 | + const output = sync ? inpaintWorklet(image, mask) : await inpaint(image, mask); |
| 120 | + setLatency(Date.now() - start); |
| 121 | + |
| 122 | + const info = { |
| 123 | + width: output.width, |
| 124 | + height: output.height, |
| 125 | + colorType: ColorType.RGBA_8888, |
| 126 | + alphaType: AlphaType.Premul, |
| 127 | + }; |
| 128 | + const result = Skia.Image.MakeImage(info, Skia.Data.fromBytes(output.data), output.width * 4); |
| 129 | + if (!result) throw new Error('Failed to decode the inpainted image in Skia'); |
| 130 | + setResultImage(result); |
| 131 | + setShowOriginal(false); |
| 132 | + } catch (e: any) { |
| 133 | + setError(e.message || String(e)); |
| 134 | + } finally { |
| 135 | + if (!sync) setIsProcessing(false); |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | + const activeError = loadError ? String(loadError) : error; |
| 140 | + const showingResult = resultImage !== null && !showOriginal; |
| 141 | + const activeImage = showingResult ? resultImage : skiaImage; |
| 142 | + const canRun = !!skiaImage && isReady && !isProcessing && strokes.length > 0; |
| 143 | + |
| 144 | + return ( |
| 145 | + <ScrollView |
| 146 | + style={commonStyles.container} |
| 147 | + contentContainerStyle={[ |
| 148 | + commonStyles.contentContainer, |
| 149 | + { paddingBottom: insets.bottom + theme.spacing.large }, |
| 150 | + ]} |
| 151 | + > |
| 152 | + <Text style={commonStyles.description}> |
| 153 | + Pick a photo, paint over what should disappear, and run the model to fill it in. |
| 154 | + </Text> |
| 155 | + |
| 156 | + <ModelPicker |
| 157 | + label="Inpainting Model" |
| 158 | + options={MODEL_OPTIONS} |
| 159 | + selectedValue={selectedModel} |
| 160 | + onValueChange={(model) => { |
| 161 | + setSelectedModel(model); |
| 162 | + resetResult(); |
| 163 | + setError(null); |
| 164 | + }} |
| 165 | + /> |
| 166 | + |
| 167 | + <ModelStatus |
| 168 | + isReady={isReady} |
| 169 | + downloadProgress={downloadProgress} |
| 170 | + error={activeError} |
| 171 | + modelTypeLabel="inpainting model" |
| 172 | + /> |
| 173 | + |
| 174 | + <ImageViewport skiaImage={activeImage} onPressPlaceholder={() => handlePickImage(false)}> |
| 175 | + {skiaImage && !showingResult && ( |
| 176 | + <MaskCanvas |
| 177 | + image={skiaImage} |
| 178 | + strokes={strokes} |
| 179 | + onStrokesChange={setStrokes} |
| 180 | + brushWidth={brushWidth} |
| 181 | + enabled={!isProcessing} |
| 182 | + /> |
| 183 | + )} |
| 184 | + </ImageViewport> |
| 185 | + |
| 186 | + {skiaImage && !showingResult && ( |
| 187 | + <View style={styles.brushRow}> |
| 188 | + <Text style={styles.brushLabel}>Brush</Text> |
| 189 | + {BRUSH_SIZES.map((size) => ( |
| 190 | + <TouchableOpacity |
| 191 | + key={size} |
| 192 | + style={[styles.brushChip, brushWidth === size && styles.brushChipActive]} |
| 193 | + onPress={() => setBrushWidth(size)} |
| 194 | + > |
| 195 | + <Text style={[styles.brushText, brushWidth === size && styles.brushTextActive]}> |
| 196 | + {size}px |
| 197 | + </Text> |
| 198 | + </TouchableOpacity> |
| 199 | + ))} |
| 200 | + <TouchableOpacity |
| 201 | + style={styles.brushChip} |
| 202 | + onPress={() => { |
| 203 | + setStrokes([]); |
| 204 | + resetResult(); |
| 205 | + }} |
| 206 | + disabled={strokes.length === 0} |
| 207 | + > |
| 208 | + <Text style={styles.brushText}>Clear</Text> |
| 209 | + </TouchableOpacity> |
| 210 | + </View> |
| 211 | + )} |
| 212 | + |
| 213 | + <View style={commonStyles.buttonRow}> |
| 214 | + <Button title="Gallery" onPress={() => handlePickImage(false)} variant="secondary" /> |
| 215 | + <Button title="Camera" onPress={() => handlePickImage(true)} variant="secondary" /> |
| 216 | + </View> |
| 217 | + |
| 218 | + <View style={commonStyles.buttonRow}> |
| 219 | + <Button |
| 220 | + title="Run Async" |
| 221 | + onPress={() => runInpainting(false)} |
| 222 | + disabled={!canRun} |
| 223 | + loading={isProcessing} |
| 224 | + /> |
| 225 | + <Button |
| 226 | + title="Run Sync" |
| 227 | + onPress={() => runInpainting(true)} |
| 228 | + disabled={!canRun} |
| 229 | + variant="accent" |
| 230 | + /> |
| 231 | + </View> |
| 232 | + |
| 233 | + {resultImage && ( |
| 234 | + <View style={commonStyles.buttonRow}> |
| 235 | + <Button |
| 236 | + title={showOriginal ? 'Show Result' : 'Show Original'} |
| 237 | + onPress={() => setShowOriginal(!showOriginal)} |
| 238 | + variant="secondary" |
| 239 | + /> |
| 240 | + </View> |
| 241 | + )} |
| 242 | + |
| 243 | + <LatencyIndicator latency={latency} /> |
| 244 | + </ScrollView> |
| 245 | + ); |
| 246 | +} |
| 247 | + |
| 248 | +export default function InpaintingScreen() { |
| 249 | + return ( |
| 250 | + <ScreenWrapper> |
| 251 | + <InpaintingContent /> |
| 252 | + </ScreenWrapper> |
| 253 | + ); |
| 254 | +} |
| 255 | + |
| 256 | +const styles = StyleSheet.create({ |
| 257 | + brushRow: { |
| 258 | + flexDirection: 'row', |
| 259 | + alignItems: 'center', |
| 260 | + gap: 8, |
| 261 | + marginBottom: 20, |
| 262 | + }, |
| 263 | + brushLabel: { |
| 264 | + color: theme.colors.textMuted, |
| 265 | + marginRight: 4, |
| 266 | + }, |
| 267 | + brushChip: { |
| 268 | + borderWidth: 1, |
| 269 | + borderColor: theme.colors.border, |
| 270 | + borderRadius: 16, |
| 271 | + paddingVertical: 6, |
| 272 | + paddingHorizontal: 12, |
| 273 | + }, |
| 274 | + brushChipActive: { |
| 275 | + backgroundColor: theme.colors.primary, |
| 276 | + borderColor: theme.colors.primary, |
| 277 | + }, |
| 278 | + brushText: { |
| 279 | + color: theme.colors.textMuted, |
| 280 | + fontSize: 13, |
| 281 | + }, |
| 282 | + brushTextActive: { |
| 283 | + color: '#fff', |
| 284 | + }, |
| 285 | +}); |
0 commit comments