|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.android.ai.samples.imagenediting.data |
| 17 | + |
| 18 | +import android.graphics.Bitmap |
| 19 | +import com.google.firebase.Firebase |
| 20 | +import com.google.firebase.ai.ai |
| 21 | +import com.google.firebase.ai.type.GenerativeBackend |
| 22 | +import com.google.firebase.ai.type.ImagenAspectRatio |
| 23 | +import com.google.firebase.ai.type.ImagenEditMode |
| 24 | +import com.google.firebase.ai.type.ImagenEditingConfig |
| 25 | +import com.google.firebase.ai.type.ImagenGenerationConfig |
| 26 | +import com.google.firebase.ai.type.ImagenImageFormat |
| 27 | +import com.google.firebase.ai.type.ImagenRawImage |
| 28 | +import com.google.firebase.ai.type.ImagenRawMask |
| 29 | +import com.google.firebase.ai.type.PublicPreviewAPI |
| 30 | +import com.google.firebase.ai.type.toImagenInlineImage |
| 31 | +import javax.inject.Inject |
| 32 | +import javax.inject.Singleton |
| 33 | + |
| 34 | +/** |
| 35 | + * A data source that provides methods for interacting with the Firebase Imagen API |
| 36 | + * for various image generation and editing tasks. |
| 37 | + * |
| 38 | + * This class encapsulates the logic for initializing Imagen models and calling |
| 39 | + * their respective functions for image generation, inpainting, outpainting, and style transfer. |
| 40 | + * It leverages the Firebase AI SDK for seamless integration with Vertex AI backends. |
| 41 | + * |
| 42 | + * Note: This class uses `@OptIn(PublicPreviewAPI::class)` as Imagen features |
| 43 | + * are currently in public preview. |
| 44 | + */ |
| 45 | +@Singleton |
| 46 | +class ImagenEditingDataSource @Inject constructor() { |
| 47 | + private companion object { |
| 48 | + const val IMAGEN_MODEL_NAME = "imagen-4.0-ultra-generate-001" |
| 49 | + const val IMAGEN_EDITING_MODEL_NAME = "imagen-3.0-capability-001" |
| 50 | + const val DEFAULT_EDIT_STEPS = 50 |
| 51 | + const val DEFAULT_STYLE_STRENGTH = 1 |
| 52 | + } |
| 53 | + |
| 54 | + @OptIn(PublicPreviewAPI::class) |
| 55 | + private val imagenModel = |
| 56 | + Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel( |
| 57 | + IMAGEN_MODEL_NAME, |
| 58 | + generationConfig = ImagenGenerationConfig( |
| 59 | + numberOfImages = 1, |
| 60 | + aspectRatio = ImagenAspectRatio.SQUARE_1x1, |
| 61 | + imageFormat = ImagenImageFormat.jpeg(compressionQuality = 75), |
| 62 | + ), |
| 63 | + ) |
| 64 | + |
| 65 | + @OptIn(PublicPreviewAPI::class) |
| 66 | + private val editingModel = |
| 67 | + Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel( |
| 68 | + IMAGEN_EDITING_MODEL_NAME, |
| 69 | + generationConfig = ImagenGenerationConfig( |
| 70 | + numberOfImages = 1, |
| 71 | + aspectRatio = ImagenAspectRatio.SQUARE_1x1, |
| 72 | + imageFormat = ImagenImageFormat.jpeg(compressionQuality = 75), |
| 73 | + ), |
| 74 | + ) |
| 75 | + |
| 76 | + /** |
| 77 | + * Generates an image based on the provided prompt. |
| 78 | + * |
| 79 | + * This function uses the Imagen model to generate an image from a textual description. |
| 80 | + * It returns the generated image as a Bitmap. |
| 81 | + * |
| 82 | + * @param prompt The textual description to generate the image from. |
| 83 | + * @return The generated image as a [Bitmap]. |
| 84 | + * @throws Exception if the image generation fails. |
| 85 | + */ |
| 86 | + @OptIn(PublicPreviewAPI::class) |
| 87 | + suspend fun generateImage(prompt: String): Bitmap { |
| 88 | + val imageResponse = imagenModel.generateImages( |
| 89 | + prompt = prompt, |
| 90 | + ) |
| 91 | + val image = imageResponse.images.first() |
| 92 | + return image.asBitmap() |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Performs inpainting on a source image using a provided mask and prompt. |
| 97 | + * |
| 98 | + * This function utilizes the Imagen editing model to fill in the masked areas |
| 99 | + * of the source image based on the textual prompt. |
| 100 | + * |
| 101 | + * @param sourceImage The original image to be inpainted. |
| 102 | + * @param maskImage A bitmap representing the mask, where white areas indicate |
| 103 | + * regions to be inpainted and black areas indicate regions to be preserved. |
| 104 | + * @param prompt A textual description of what should be generated in the masked areas. |
| 105 | + * @param editSteps The number of editing steps to perform. Defaults to `DEFAULT_EDIT_STEPS`. |
| 106 | + * @return A [Bitmap] representing the inpainted image. |
| 107 | + */ |
| 108 | + @OptIn(PublicPreviewAPI::class) |
| 109 | + suspend fun inpaintImageWithMask(sourceImage: Bitmap, maskImage: Bitmap, prompt: String, editSteps: Int = DEFAULT_EDIT_STEPS): Bitmap { |
| 110 | + val imageResponse = editingModel.editImage( |
| 111 | + referenceImages = listOf( |
| 112 | + ImagenRawImage(sourceImage.toImagenInlineImage()), |
| 113 | + ImagenRawMask(maskImage.toImagenInlineImage()), |
| 114 | + ), |
| 115 | + prompt = prompt, |
| 116 | + config = ImagenEditingConfig( |
| 117 | + editMode = ImagenEditMode.INPAINT_INSERTION, |
| 118 | + editSteps = editSteps, |
| 119 | + ), |
| 120 | + ) |
| 121 | + return imageResponse.images.first().asBitmap() |
| 122 | + } |
| 123 | +} |
0 commit comments