@@ -80,10 +80,11 @@ internal constructor(
80
80
* @param referenceId the reference ID for this image, to be referenced in the prompt
81
81
* @param enableComputation requests that the reference image be generated serverside instead of
82
82
* provided
83
- * @param superpixelRegionSize if type is COLOR_SUPERPIXEL and [enableComputation] is true, this
84
- * will control the size of each superpixel region in pixels for the generated referenced image
85
- * @param superpixelRuler if type is COLOR_SUPERPIXEL and [enableComputation] is true, this will
86
- * control the superpixel smoothness factor for the generated referenced image
83
+ * @param superpixelRegionSize if type is [ImagenControlType.COLOR_SUPERPIXEL] and
84
+ * [enableComputation] is true, this will control the size of each superpixel region in pixels for
85
+ * the generated referenced image
86
+ * @param superpixelRuler if type is [ImagenControlType.COLOR_SUPERPIXEL] and [enableComputation] is
87
+ * true, this will control the superpixel smoothness factor for the generated referenced image
87
88
*/
88
89
@PublicPreviewAPI
89
90
public class ImagenControlReference (
@@ -102,9 +103,8 @@ public class ImagenControlReference(
102
103
) {}
103
104
104
105
/* *
105
- * Represents a reference image for Imagen editing which will mask of a region to be edited. This
106
- * image (generated or provided) should contain only black and white pixels, with black representing
107
- * parts of the image which should not change.
106
+ * Represents a mask for Imagen editing. This image (generated or provided) should contain only
107
+ * black and white pixels, with black representing parts of the image which should not change.
108
108
*/
109
109
@PublicPreviewAPI
110
110
public abstract class ImagenMaskReference
@@ -113,17 +113,16 @@ internal constructor(maskConfig: ImagenMaskConfig, image: ImagenInlineImage? = n
113
113
114
114
public companion object {
115
115
/* *
116
- * Generates these two reference images in order:
116
+ * Generates two reference images of [ImagenRawImage] and [ImagenRawMask]. These images are
117
+ * generated in this order:
117
118
* * One [ImagenRawImage] containing the original image, padded out to the new dimensions with
118
119
* black pixels, with the original image placed at the given placement
119
120
* * One [ImagenRawMask] of the same dimensions containing white everywhere except at the
120
- * placement original image.
121
- *
122
- * This is the format expected by Imagen for outpainting requests.
121
+ * placement original image. This is the format expected by Imagen for outpainting requests.
123
122
*
124
123
* @param image the original image
125
- * @param newDimensions the new dimensions for outpainting. This *must* be more than the
126
- * original image.
124
+ * @param newDimensions the new dimensions for outpainting. These new dimensions *must* be more
125
+ * than the original image.
127
126
* @param newPosition the placement of the original image within the new outpainted image.
128
127
*/
129
128
@JvmOverloads
@@ -132,6 +131,29 @@ internal constructor(maskConfig: ImagenMaskConfig, image: ImagenInlineImage? = n
132
131
image : ImagenInlineImage ,
133
132
newDimensions : Dimensions ,
134
133
newPosition : ImagenImagePlacement = ImagenImagePlacement .CENTER ,
134
+ ): List <ImagenReferenceImage > =
135
+ generateMaskAndPadForOutpainting(image, newDimensions, newPosition, 0.01 )
136
+
137
+ /* *
138
+ * Generates two reference images of [ImagenRawImage] and [ImagenRawMask]. These images are
139
+ * generated in this order:
140
+ * * One [ImagenRawImage] containing the original image, padded out to the new dimensions with
141
+ * black pixels, with the original image placed at the given placement
142
+ * * One [ImagenRawMask] of the same dimensions containing white everywhere except at the
143
+ * placement original image. This is the format expected by Imagen for outpainting requests.
144
+ *
145
+ * @param image the original image
146
+ * @param newDimensions the new dimensions for outpainting. These new dimensions *must* be more
147
+ * than the original image.
148
+ * @param newPosition the placement of the original image within the new outpainted image.
149
+ * @param dilation the dilation for the outpainting mask. See: [ImagenRawMask].
150
+ */
151
+ @JvmStatic
152
+ public fun generateMaskAndPadForOutpainting (
153
+ image : ImagenInlineImage ,
154
+ newDimensions : Dimensions ,
155
+ newPosition : ImagenImagePlacement = ImagenImagePlacement .CENTER ,
156
+ dilation : Double = 0.01
135
157
): List <ImagenReferenceImage > {
136
158
val originalBitmap = image.asBitmap()
137
159
if (
@@ -181,7 +203,7 @@ internal constructor(maskConfig: ImagenMaskConfig, image: ImagenInlineImage? = n
181
203
newImageCanvas.drawBitmap(originalBitmap, null , normalizedImageRectangle, null )
182
204
return listOf (
183
205
ImagenRawImage (newImageBitmap.toImagenInlineImage()),
184
- ImagenRawMask (maskBitmap.toImagenInlineImage()),
206
+ ImagenRawMask (maskBitmap.toImagenInlineImage(), dilation ),
185
207
)
186
208
}
187
209
}
@@ -190,8 +212,8 @@ internal constructor(maskConfig: ImagenMaskConfig, image: ImagenInlineImage? = n
190
212
/* *
191
213
* A generated mask image which will auto-detect and mask out the background. The background will be
192
214
* white, and the foreground black
193
- * @param dilation the amount to dilate the mask, this can help smooth the borders of an edit and
194
- * make it seem more convincing. For example, 0.05 would dilate the mask 5%.
215
+ * @param dilation the amount to dilate the mask. This can help smooth the borders of an edit and
216
+ * make it seem more convincing. For example, ` 0.05` will dilate the mask 5%.
195
217
*/
196
218
@PublicPreviewAPI
197
219
public class ImagenBackgroundMask (dilation : Double? = null ) :
@@ -200,21 +222,20 @@ public class ImagenBackgroundMask(dilation: Double? = null) :
200
222
/* *
201
223
* A generated mask image which will auto-detect and mask out the foreground. The background will be
202
224
* black, and the foreground white
203
- * @param dilation the amount to dilate the mask, this can help smooth the borders of an edit and
204
- * make it seem more convincing. For example, 0.05 would dilate the mask 5%.
225
+ * @param dilation the amount to dilate the mask. This can help smooth the borders of an edit and
226
+ * make it seem more convincing. For example, ` 0.05` will dilate the mask 5%.
205
227
*/
206
228
@PublicPreviewAPI
207
229
public class ImagenForegroundMask (dilation : Double? = null ) :
208
230
ImagenMaskReference (maskConfig = ImagenMaskConfig (ImagenMaskMode .FOREGROUND , dilation)) {}
209
231
210
232
/* *
211
- * Represents a reference image for Imagen editing which will mask of a region to be edited. This
212
- * image should contain only black and white pixels, with black representing parts of the image
213
- * which should not change.
233
+ * Represents a mask for Imagen editing. This image should contain only black and white pixels, with
234
+ * black representing parts of the image which should not change.
214
235
*
215
236
* @param mask the mask image
216
- * @param dilation the amount to dilate the mask, this can help smooth the borders of an edit and
217
- * make it seem more convincing. For example, 0.05 would dilate the mask 5%.
237
+ * @param dilation the amount to dilate the mask. This can help smooth the borders of an edit and
238
+ * make it seem more convincing. For example, ` 0.05` will dilate the mask 5%.
218
239
*/
219
240
@PublicPreviewAPI
220
241
public class ImagenRawMask (mask : ImagenInlineImage , dilation : Double? = null ) :
@@ -226,11 +247,11 @@ public class ImagenRawMask(mask: ImagenInlineImage, dilation: Double? = null) :
226
247
/* *
227
248
* Represents a generated mask for Imagen editing which masks out certain objects using object
228
249
* detection.
229
- * @param classes the list of segmentation IDs for objects to detect and mask out. See
230
- * [here ](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/imagen-api-edit#segment-ids)
231
- * for a list of segmentation IDs
232
- * @param dilation the amount to dilate the mask, this can help smooth the borders of an edit and
233
- * make it seem more convincing. For example, 0.05 would dilate the mask 5%.
250
+ * @param classes the list of segmentation IDs for objects to detect and mask out. Find a
251
+ * [list of segmentation IDs ](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/imagen-api-edit#segment-ids)
252
+ * in the Vertex AI documentation.
253
+ * @param dilation the amount to dilate the mask. This can help smooth the borders of an edit and
254
+ * make it seem more convincing. For example, ` 0.05` will dilate the mask 5%.
234
255
*/
235
256
@PublicPreviewAPI
236
257
public class ImagenSemanticMask (classes : List <Int >, dilation : Double? = null ) :
0 commit comments