Skip to content

Commit 21262c3

Browse files
author
David Motsonashvili
committed
add tests, and JvmStatic and JvmField annotations
1 parent a48f7ac commit 21262c3

File tree

7 files changed

+98
-14
lines changed

7 files changed

+98
-14
lines changed

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/ImagenControlType.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ package com.google.firebase.ai.type
1818
/** Represents a control type for controlled Imagen generation/editing */
1919
public class ImagenControlType internal constructor(internal val value: String) {
2020
public companion object {
21+
2122
/** Use edge detection to ensure the new image follow the same outlines */
22-
public val CANNY: ImagenControlType = ImagenControlType("CONTROL_TYPE_CANNY")
23+
@JvmField public val CANNY: ImagenControlType = ImagenControlType("CONTROL_TYPE_CANNY")
24+
2325
/** Use enhanced edge detection to ensure the new image follow similar outlines */
24-
public val SCRIBBLE: ImagenControlType = ImagenControlType("CONTROL_TYPE_SCRIBBLE")
26+
@JvmField public val SCRIBBLE: ImagenControlType = ImagenControlType("CONTROL_TYPE_SCRIBBLE")
27+
2528
/** Use face mesh control to ensure that the new image has the same facial expressions */
26-
public val FACE_MESH: ImagenControlType = ImagenControlType("CONTROL_TYPE_FACE_MESH")
29+
@JvmField public val FACE_MESH: ImagenControlType = ImagenControlType("CONTROL_TYPE_FACE_MESH")
30+
2731
/**
2832
* Use color superpixels to ensure that the new image is similar in shape and color to the
2933
* original
3034
*/
35+
@JvmField
3136
public val COLOR_SUPERPIXEL: ImagenControlType =
3237
ImagenControlType("CONTROL_TYPE_COLOR_SUPERPIXEL")
3338
}

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/ImagenEditMode.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ public class ImagenEditMode private constructor(internal val value: String) {
2020

2121
public companion object {
2222
/** Inserts a new element into an image */
23+
@JvmField
2324
public val INPAINT_INSERTION: ImagenEditMode = ImagenEditMode("EDIT_MODE_INPAINT_INSERTION")
2425
/** Removes an element from an image */
26+
@JvmField
2527
public val INPAINT_REMOVAL: ImagenEditMode = ImagenEditMode("EDIT_MODE_INPAINT_REMOVAL")
2628
/** Extend the borders of an image outwards */
27-
public val OUTPAINT: ImagenEditMode = ImagenEditMode("EDIT_MODE_OUTPAINT")
29+
@JvmField public val OUTPAINT: ImagenEditMode = ImagenEditMode("EDIT_MODE_OUTPAINT")
2830
}
2931
}

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/ImagenImagePlacement.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,36 @@ private constructor(public val x: Int? = null, public val y: Int? = null) {
7373
* @param x the x coordinate of the top left corner of the original image
7474
* @param y the y coordinate of the top left corner of the original image
7575
*/
76+
@JvmStatic
7677
public fun fromCoordinate(x: Int, y: Int): ImagenImagePlacement {
7778
return ImagenImagePlacement(x, y)
7879
}
7980

8081
/** Center the image horizontally and vertically within the larger image */
81-
public val CENTER: ImagenImagePlacement = ImagenImagePlacement()
82+
@JvmField public val CENTER: ImagenImagePlacement = ImagenImagePlacement()
83+
8284
/** Center the image horizontally and aligned with the top edge of the larger image */
83-
public val TOP_CENTER: ImagenImagePlacement = ImagenImagePlacement()
85+
@JvmField public val TOP_CENTER: ImagenImagePlacement = ImagenImagePlacement()
86+
8487
/** Center the image horizontally and aligned with the bottom edge of the larger image */
85-
public val BOTTOM_CENTER: ImagenImagePlacement = ImagenImagePlacement()
88+
@JvmField public val BOTTOM_CENTER: ImagenImagePlacement = ImagenImagePlacement()
89+
8690
/** Center the image vertically and aligned with the left edge of the larger image */
87-
public val LEFT_CENTER: ImagenImagePlacement = ImagenImagePlacement()
91+
@JvmField public val LEFT_CENTER: ImagenImagePlacement = ImagenImagePlacement()
92+
8893
/** Center the image vertically and aligned with the right edge of the larger image */
89-
public val RIGHT_CENTER: ImagenImagePlacement = ImagenImagePlacement()
94+
@JvmField public val RIGHT_CENTER: ImagenImagePlacement = ImagenImagePlacement()
95+
9096
/** Align the image with the top left corner of the larger image */
91-
public val TOP_LEFT: ImagenImagePlacement = ImagenImagePlacement(0, 0)
97+
@JvmField public val TOP_LEFT: ImagenImagePlacement = ImagenImagePlacement(0, 0)
98+
9299
/** Align the image with the top right corner of the larger image */
93-
public val TOP_RIGHT: ImagenImagePlacement = ImagenImagePlacement()
100+
@JvmField public val TOP_RIGHT: ImagenImagePlacement = ImagenImagePlacement()
101+
94102
/** Align the image with the bottom left corner of the larger image */
95-
public val BOTTOM_LEFT: ImagenImagePlacement = ImagenImagePlacement()
103+
@JvmField public val BOTTOM_LEFT: ImagenImagePlacement = ImagenImagePlacement()
104+
96105
/** Align the image with the bottom right corner of the larger image */
97-
public val BOTTOM_RIGHT: ImagenImagePlacement = ImagenImagePlacement()
106+
@JvmField public val BOTTOM_RIGHT: ImagenImagePlacement = ImagenImagePlacement()
98107
}
99108
}

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/ImagenReferenceImage.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal constructor(
7474

7575
/**
7676
* Represents a reference image (provided or generated) to bound the created image via ControlNet
77-
* @param image the image provided, required if enableComputation is false
77+
* @param image the image provided, required if [enableComputation] is false
7878
* @param type the type of ControlNet reference image
7979
* @param referenceId the reference ID for this image, to be referenced in the prompt
8080
* @param enableComputation requests that the reference image be generated serverside instead of
@@ -125,6 +125,8 @@ internal constructor(maskConfig: ImagenMaskConfig, image: ImagenInlineImage? = n
125125
* original image.
126126
* @param newPosition the placement of the original image within the new outpainted image.
127127
*/
128+
@JvmOverloads
129+
@JvmStatic
128130
public fun generateMaskAndPadForOutpainting(
129131
image: ImagenInlineImage,
130132
newDimensions: Dimensions,

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/ImagenSubjectReferenceType.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ package com.google.firebase.ai.type
1919
public class ImagenSubjectReferenceType private constructor(internal val value: String) {
2020

2121
public companion object {
22+
2223
/** Marks the reference type as being of a person */
24+
@JvmField
2325
public val PERSON: ImagenSubjectReferenceType =
2426
ImagenSubjectReferenceType("SUBJECT_TYPE_PERSON")
27+
2528
/** Marks the reference type as being of a animal */
29+
@JvmField
2630
public val ANIMAL: ImagenSubjectReferenceType =
2731
ImagenSubjectReferenceType("SUBJECT_TYPE_ANIMAL")
32+
2833
/** Marks the reference type as being of a product */
34+
@JvmField
2935
public val PRODUCT: ImagenSubjectReferenceType =
3036
ImagenSubjectReferenceType("SUBJECT_TYPE_PRODUCT")
3137
}

firebase-ai/src/test/java/com/google/firebase/ai/SerializationTests.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ import com.google.firebase.ai.common.util.descriptorToJson
2020
import com.google.firebase.ai.type.Candidate
2121
import com.google.firebase.ai.type.CountTokensResponse
2222
import com.google.firebase.ai.type.GenerateContentResponse
23+
import com.google.firebase.ai.type.ImagenReferenceImage
2324
import com.google.firebase.ai.type.ModalityTokenCount
25+
import com.google.firebase.ai.type.PublicPreviewAPI
2426
import com.google.firebase.ai.type.Schema
2527
import io.kotest.assertions.json.shouldEqualJson
2628
import org.junit.Test
2729

30+
@OptIn(PublicPreviewAPI::class)
2831
internal class SerializationTests {
2932
@Test
3033
fun `test countTokensResponse serialization as Json`() {
@@ -233,4 +236,41 @@ internal class SerializationTests {
233236
val actualJson = descriptorToJson(Schema.Internal.serializer().descriptor)
234237
expectedJsonAsString shouldEqualJson actualJson.toString()
235238
}
239+
240+
@Test
241+
fun `test ReferenceImage serialization as Json`() {
242+
val expectedJsonAsString =
243+
"""
244+
{
245+
"id": "ImagenReferenceImage",
246+
"type": "object",
247+
"properties": {
248+
"referenceType": {
249+
"type": "string"
250+
},
251+
"referenceImage": {
252+
"${'$'}ref": "ImagenInlineImage"
253+
},
254+
"referenceId": {
255+
"type": "integer"
256+
},
257+
"subjectImageConfig": {
258+
"${'$'}ref": "ImagenSubjectConfig"
259+
},
260+
"maskImageConfig": {
261+
"${'$'}ref": "ImagenMaskConfig"
262+
},
263+
"styleImageConfig": {
264+
"${'$'}ref": "ImagenStyleConfig"
265+
},
266+
"controlConfig": {
267+
"${'$'}ref": "ImagenControlConfig"
268+
}
269+
}
270+
}
271+
"""
272+
.trimIndent()
273+
val actualJson = descriptorToJson(ImagenReferenceImage.Internal.serializer().descriptor)
274+
expectedJsonAsString shouldEqualJson actualJson.toString()
275+
}
236276
}

firebase-ai/src/testUtil/java/com/google/firebase/ai/JavaCompileTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import com.google.common.util.concurrent.ListenableFuture;
2222
import com.google.firebase.ai.FirebaseAI;
2323
import com.google.firebase.ai.GenerativeModel;
24+
import com.google.firebase.ai.ImagenModel;
2425
import com.google.firebase.ai.LiveGenerativeModel;
2526
import com.google.firebase.ai.java.ChatFutures;
2627
import com.google.firebase.ai.java.GenerativeModelFutures;
28+
import com.google.firebase.ai.java.ImagenModelFutures;
2729
import com.google.firebase.ai.java.LiveModelFutures;
2830
import com.google.firebase.ai.java.LiveSessionFutures;
2931
import com.google.firebase.ai.type.BlockReason;
@@ -33,6 +35,7 @@
3335
import com.google.firebase.ai.type.Content;
3436
import com.google.firebase.ai.type.ContentModality;
3537
import com.google.firebase.ai.type.CountTokensResponse;
38+
import com.google.firebase.ai.type.Dimensions;
3639
import com.google.firebase.ai.type.FileDataPart;
3740
import com.google.firebase.ai.type.FinishReason;
3841
import com.google.firebase.ai.type.FunctionCallPart;
@@ -43,6 +46,11 @@
4346
import com.google.firebase.ai.type.HarmProbability;
4447
import com.google.firebase.ai.type.HarmSeverity;
4548
import com.google.firebase.ai.type.ImagePart;
49+
import com.google.firebase.ai.type.ImagenBackgroundMask;
50+
import com.google.firebase.ai.type.ImagenEditMode;
51+
import com.google.firebase.ai.type.ImagenEditingConfig;
52+
import com.google.firebase.ai.type.ImagenInlineImage;
53+
import com.google.firebase.ai.type.ImagenMaskReference;
4654
import com.google.firebase.ai.type.InlineDataPart;
4755
import com.google.firebase.ai.type.LiveGenerationConfig;
4856
import com.google.firebase.ai.type.LiveServerContent;
@@ -65,6 +73,7 @@
6573
import com.google.firebase.concurrent.FirebaseExecutors;
6674
import java.util.ArrayList;
6775
import java.util.Calendar;
76+
import java.util.Collections;
6877
import java.util.List;
6978
import java.util.Map;
7079
import java.util.concurrent.Executor;
@@ -141,6 +150,17 @@ private LiveGenerationConfig getLiveConfig() {
141150
.build();
142151
}
143152

153+
private void testImagen() {
154+
ImagenModel modelSuspend = FirebaseAI.getInstance().imagenModel("");
155+
ImagenModelFutures model = ImagenModelFutures.from(modelSuspend);
156+
model.editImage(
157+
Collections.singletonList(new ImagenBackgroundMask()),
158+
"",
159+
new ImagenEditingConfig(ImagenEditMode.OUTPAINT, 25));
160+
ImagenMaskReference.generateMaskAndPadForOutpainting(
161+
new ImagenInlineImage(new byte[0], ""), new Dimensions(0, 0));
162+
}
163+
144164
private void testFutures(GenerativeModelFutures futures) throws Exception {
145165
Content content =
146166
new Content.Builder()

0 commit comments

Comments
 (0)