> ## Documentation Index
> Fetch the complete documentation index at: https://docs.runanywhere.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Image Generation

> Diffusion image generation and inpainting

`RunAnywhere.images` runs diffusion models on-device. The backend is CoreML, so this works on Apple
platforms only; on Android the call throws because no diffusion engine is registered.

```dart theme={null}
final result = await RunAnywhere.images.generate('a red bicycle in the rain');

final image = result.images.first;
print('${image.width}x${image.height}');
// image.bytes is encoded image data
```

`ImageResult` carries `images` (a `List<ImageData>` in batch order), `seed` (the seed the sampler
actually used), and `steps`.

`ImageData` carries `bytes`, `width`, `height`, and `mediaType`, which is non-null when the backend
reported one.

## ImageOptions

```dart theme={null}
final result = await RunAnywhere.images.generate(
  'a red bicycle',
  options: ImageOptions(
    negativePrompt: 'blurry, low quality',
    width: 512,
    height: 512,
    steps: 20,
    guidanceScale: 7.5,
    seed: 42,
  ),
);
```

| Field            | Type        | Default              | Notes                                     |
| ---------------- | ----------- | -------------------- | ----------------------------------------- |
| `negativePrompt` | `String?`   | `null`               | Concepts to steer away from               |
| `width`          | `int?`      | `null`               | Null uses the model's native size         |
| `height`         | `int?`      | `null`               | Null uses the model's native size         |
| `steps`          | `int?`      | backend default      | Denoising steps, up to 50                 |
| `guidanceScale`  | `double?`   | backend default      | Classifier-free guidance, up to 20.0      |
| `seed`           | `int?`      | `null`               | Deterministic sampling                    |
| `mode`           | `ImageMode` | `ImageMode.generate` | Generate from scratch, or inpaint         |
| `reportPartials` | `bool`      | `false`              | Emit preview images from `generateStream` |

## Progress

`generateStream` reports each denoising step, and previews too when `reportPartials` is on.

```dart theme={null}
await for (final event in RunAnywhere.images.generateStream(
  'a red bicycle',
  options: ImageOptions(steps: 20, reportPartials: true),
)) {
  switch (event) {
    case ImageStarted():
      setState(() => _status = 'Sampling');
    case ImageProgress(:final step, :final totalSteps, :final partialImage):
      setState(() {
        _fraction = totalSteps > 0 ? step / totalSteps : 0;
        if (partialImage != null) _preview = partialImage;
      });
    case ImageCompleted(:final result):
      setState(() => _image = result.images.first.bytes);
  }
}
```

`ImageEvent` is sealed. Failures arrive as an `SDKException` thrown into the consumer.

## Inpainting

Inpainting is a mode on the options, not a separate verb. Supply the base image and a mask marking the
region to repaint.

```dart theme={null}
final result = await RunAnywhere.images.generate(
  'a golden retriever sitting on the bench',
  options: ImageOptions(
    mode: ImageMode.inpaint(
      ImageInput.rawRgb(baseRgb, 512, 512),
      ImageInput.rawRgb(maskRgb, 512, 512),
    ),
  ),
);
```

## Models

Diffusion models register under `MODEL_CATEGORY_IMAGE_GENERATION` with the CoreML framework, so gate
the registration to Apple platforms.

```dart theme={null}
import 'dart:io';

if (Platform.isIOS || Platform.isMacOS) {
  await RunAnywhere.models.register(
    ModelRegistration.url(
      id: 'stable-diffusion-v1-5-coreml',
      name: 'Stable Diffusion 1.5 (CoreML)',
      url: 'https://huggingface.co/apple/coreml-stable-diffusion-v1-5-palettized',
      framework: InferenceFramework.INFERENCE_FRAMEWORK_COREML,
      category: ModelCategory.MODEL_CATEGORY_IMAGE_GENERATION,
      memoryRequirementBytes: 1200000000,
    ),
  );
}
```

<Warning>
  Diffusion is memory hungry. A 1.5-class model needs well over a gigabyte resident, so unload other
  models first with `RunAnywhere.models.unload()`.
</Warning>

## See also

<CardGroup cols={2}>
  <Card title="Vision language" icon="image" href="/flutter/vlm">
    Reading images instead of making them
  </Card>

  <Card title="Models" icon="box" href="/flutter/models">
    Registration and memory
  </Card>
</CardGroup>
