|
| 1 | +# Reference Images Implementation Guide |
| 2 | + |
| 3 | +## Current Status |
| 4 | + |
| 5 | +### `init_image` Implementation (Already Complete) |
| 6 | + |
| 7 | +The `init_image` feature is fully implemented across the stack: |
| 8 | + |
| 9 | +#### 1. **Frontend/API Layer** - [image_generator.h](include/image_generator.h#L34) |
| 10 | +```cpp |
| 11 | +struct ImageGenerationParams { |
| 12 | + std::string init_image_base64; // Base64 encoded image |
| 13 | + float strength = 0.75f; // Strength of the init image |
| 14 | +}; |
| 15 | +``` |
| 16 | +
|
| 17 | +#### 2. **Backend Processing** - [image_generator.cpp](src/image_generator.cpp#L390-L410) |
| 18 | +- **Lines 390-394**: Decode and process init_image |
| 19 | + - Checks if img2img mode is enabled and init_image_base64 is provided |
| 20 | + - Calls `createInitImage()` to decode and resize the image |
| 21 | + |
| 22 | +- **Lines 499-509**: `createInitImage()` function |
| 23 | + - Decodes base64 to image using `base64ToImage()` |
| 24 | + - Resizes image to match width/height parameters |
| 25 | + - Returns sd_image_t struct |
| 26 | +
|
| 27 | +- **Lines 449-451**: Cleanup |
| 28 | + - Frees the init_image memory after generation |
| 29 | +
|
| 30 | +#### 3. **Core Engine** - [stable-diffusion.h](stable-diffusion.cpp/stable-diffusion.h#L287-L306) |
| 31 | +```cpp |
| 32 | +typedef struct { |
| 33 | + sd_image_t init_image; // The initial image for img2img |
| 34 | + sd_image_t* ref_images; // Reference images (NOT YET EXPOSED TO FRONTEND) |
| 35 | + int ref_images_count; // Number of reference images |
| 36 | + bool auto_resize_ref_image; // Auto-resize setting |
| 37 | + // ... other fields |
| 38 | +} sd_img_gen_params_t; |
| 39 | +``` |
| 40 | + |
| 41 | +### `ref_images` Implementation (Backend Only) |
| 42 | + |
| 43 | +The `ref_images` feature **already exists in the backend** but is NOT exposed through the ImageGenerator layer: |
| 44 | + |
| 45 | +#### 1. **Core Engine Support** - [stable-diffusion.h](stable-diffusion.cpp/stable-diffusion.h#L289-L291) |
| 46 | +- `sd_image_t* ref_images` - array of reference images |
| 47 | +- `int ref_images_count` - count of images |
| 48 | +- `bool auto_resize_ref_image` - auto-resize flag |
| 49 | + |
| 50 | +#### 2. **Usage in Backend** - [stable-diffusion.cpp](stable-diffusion.cpp/stable-diffusion.cpp#L3730) |
| 51 | +- Line 3730: `if (sd_img_gen_params->init_image.data != nullptr || sd_img_gen_params->ref_images_count > 0)` |
| 52 | +- Lines 3662-3664: Reference images are pushed into a vector for processing |
| 53 | +- Line 3669: Empty image is used if no ref_images for certain models |
| 54 | +- Lines 1753-1764: [conditioner.hpp](stable-diffusion.cpp/conditioner.hpp#L1753-L1764) - Vision processing of ref_images for Qwen image editing |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## What's Missing |
| 59 | + |
| 60 | +To fully implement `ref_images` support, you need to add to the **frontend layer** ([image_generator.h](include/image_generator.h) and [image_generator.cpp](src/image_generator.cpp)): |
| 61 | + |
| 62 | +### Changes Needed: |
| 63 | + |
| 64 | +1. **Add to ImageGenerationParams struct** ([image_generator.h](include/image_generator.h#L17)) |
| 65 | + ```cpp |
| 66 | + struct ImageGenerationParams { |
| 67 | + // ... existing fields ... |
| 68 | + |
| 69 | + // Reference images for Qwen/vision-based models |
| 70 | + std::vector<std::string> ref_images_base64; // Base64 encoded reference images |
| 71 | + bool auto_resize_ref_image = true; |
| 72 | + }; |
| 73 | + ``` |
| 74 | +
|
| 75 | +2. **Add helper function in ImageGenerator class** ([image_generator.cpp](src/image_generator.cpp)) |
| 76 | + ```cpp |
| 77 | + // Similar to createInitImage() - create function to process ref_images |
| 78 | + // Decode each base64 string, resize, and return vector of sd_image_t |
| 79 | + ``` |
| 80 | + |
| 81 | +3. **Process ref_images in generateImage()** (around line 390-410) |
| 82 | + ```cpp |
| 83 | + // Allocate array for reference images |
| 84 | + // Loop through params.ref_images_base64 |
| 85 | + // Decode and resize each one |
| 86 | + // Set gen_params.ref_images and gen_params.ref_images_count |
| 87 | + ``` |
| 88 | + |
| 89 | +4. **Cleanup ref_images memory** (around line 449-451) |
| 90 | + ```cpp |
| 91 | + // Free each reference image after generation |
| 92 | + ``` |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## File Locations Summary |
| 97 | + |
| 98 | +| Component | File | Key Lines | |
| 99 | +|-----------|------|-----------| |
| 100 | +| API Definition | [include/image_generator.h](include/image_generator.h) | 17-45 (struct) | |
| 101 | +| Frontend Logic | [src/image_generator.cpp](src/image_generator.cpp) | 390-410, 499-509, 449-451 | |
| 102 | +| Backend Struct | [stable-diffusion.cpp/stable-diffusion.h](stable-diffusion.cpp/stable-diffusion.h) | 287-306 | |
| 103 | +| Backend Usage | [stable-diffusion.cpp/stable-diffusion.cpp](stable-diffusion.cpp/stable-diffusion.cpp) | 3730, 3662-3664, 3669 | |
| 104 | +| Vision Processing | [stable-diffusion.cpp/conditioner.hpp](stable-diffusion.cpp/conditioner.hpp) | 1753-1764 | |
0 commit comments