|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 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 | + * http://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 | + |
| 17 | +using System.Collections.Generic; |
| 18 | + |
| 19 | +namespace Firebase.AI { |
| 20 | + /// <summary> |
| 21 | + /// An aspect ratio for images generated by Imagen. |
| 22 | + /// |
| 23 | + /// To specify an aspect ratio for generated images, set `AspectRatio` in |
| 24 | + /// your `ImagenGenerationConfig`. See the [Cloud |
| 25 | + /// documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images#aspect-ratio) |
| 26 | + /// for more details and examples of the supported aspect ratios. |
| 27 | + /// </summary> |
| 28 | + public enum ImagenAspectRatio { |
| 29 | + /// <summary> |
| 30 | + /// Square (1:1) aspect ratio. |
| 31 | + /// |
| 32 | + /// Common uses for this aspect ratio include social media posts. |
| 33 | + /// </summary> |
| 34 | + Square1x1, |
| 35 | + /// <summary> |
| 36 | + /// Portrait widescreen (9:16) aspect ratio. |
| 37 | + /// |
| 38 | + /// This is the `Landscape16x9` aspect ratio rotated 90 degrees. This a relatively new aspect |
| 39 | + /// ratio that has been popularized by short form video apps (for example, YouTube shorts). Use |
| 40 | + /// this for tall objects with strong vertical orientations such as buildings, trees, waterfalls, |
| 41 | + /// or other similar objects. |
| 42 | + /// </summary> |
| 43 | + Portrait9x16, |
| 44 | + /// <summary> |
| 45 | + /// Widescreen (16:9) aspect ratio. |
| 46 | + /// |
| 47 | + /// This ratio has replaced `Landscape4x3` as the most common aspect ratio for TVs, monitors, |
| 48 | + /// and mobile phone screens (landscape). Use this aspect ratio when you want to capture more of |
| 49 | + /// the background (for example, scenic landscapes). |
| 50 | + /// </summary> |
| 51 | + Landscape16x9, |
| 52 | + /// <summary> |
| 53 | + /// Portrait full screen (3:4) aspect ratio. |
| 54 | + /// |
| 55 | + /// This is the `Landscape4x3` aspect ratio rotated 90 degrees. This lets to capture more of |
| 56 | + /// the scene vertically compared to the `Square1x1` aspect ratio. |
| 57 | + /// </summary> |
| 58 | + Portrait3x4, |
| 59 | + /// <summary> |
| 60 | + /// Fullscreen (4:3) aspect ratio. |
| 61 | + /// |
| 62 | + /// This aspect ratio is commonly used in media or film. It is also the dimensions of most old |
| 63 | + /// (non-widescreen) TVs and medium format cameras. It captures more of the scene horizontally |
| 64 | + /// (compared to `Square1x1`), making it a preferred aspect ratio for photography. |
| 65 | + /// </summary> |
| 66 | + Landscape4x3 |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// An image format for images generated by Imagen. |
| 71 | + /// |
| 72 | + /// To specify an image format for generated images, set `ImageFormat` in |
| 73 | + /// your `ImagenGenerationConfig`. See the [Cloud |
| 74 | + /// documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/imagen-api#output-options) |
| 75 | + /// for more details. |
| 76 | + /// </summary> |
| 77 | + public readonly struct ImagenImageFormat { |
| 78 | +#if !DOXYGEN |
| 79 | + public string MimeType { get; } |
| 80 | + public int? CompressionQuality { get; } |
| 81 | +#endif |
| 82 | + |
| 83 | + private ImagenImageFormat(string mimeType, int? compressionQuality = null) { |
| 84 | + MimeType = mimeType; |
| 85 | + CompressionQuality = compressionQuality; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// PNG image format. |
| 90 | + /// |
| 91 | + /// Portable Network Graphic (PNG) is a lossless image format, meaning no image data is lost |
| 92 | + /// during compression. Images in PNG format are *typically* larger than JPEG images, though this |
| 93 | + /// depends on the image content and JPEG compression quality. |
| 94 | + /// </summary> |
| 95 | + public static ImagenImageFormat Png() { |
| 96 | + return new ImagenImageFormat("image/png"); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// JPEG image format. |
| 101 | + /// |
| 102 | + /// Joint Photographic Experts Group (JPEG) is a lossy compression format, meaning some image data |
| 103 | + /// is discarded during compression. Images in JPEG format are *typically* larger than PNG images, |
| 104 | + /// though this depends on the image content and JPEG compression quality. |
| 105 | + /// </summary> |
| 106 | + /// <param name="compressionQuality">The JPEG quality setting from 0 to 100, where `0` is highest level of |
| 107 | + /// compression (lowest image quality, smallest file size) and `100` is the lowest level of |
| 108 | + /// compression (highest image quality, largest file size); defaults to `75`.</param> |
| 109 | + public static ImagenImageFormat Jpeg(int? compressionQuality = null) { |
| 110 | + return new ImagenImageFormat("image/jpeg", compressionQuality); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Intended for internal use only. |
| 115 | + /// This method is used for serializing the object to JSON for the API request. |
| 116 | + /// </summary> |
| 117 | + internal Dictionary<string, object> ToJson() { |
| 118 | + Dictionary<string, object> jsonDict = new() { |
| 119 | + ["mimeType"] = MimeType |
| 120 | + }; |
| 121 | + if (CompressionQuality != null) { |
| 122 | + jsonDict["compressionQuality"] = CompressionQuality.Value; |
| 123 | + } |
| 124 | + return jsonDict; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Configuration options for generating images with Imagen. |
| 130 | + /// |
| 131 | + /// See [Parameters for Imagen |
| 132 | + /// models](https://firebase.google.com/docs/vertex-ai/model-parameters?platform=unity#imagen) to |
| 133 | + /// learn about parameters available for use with Imagen models, including how to configure them. |
| 134 | + /// </summary> |
| 135 | + public readonly struct ImagenGenerationConfig { |
| 136 | +#if !DOXYGEN |
| 137 | + public string NegativePrompt { get; } |
| 138 | + public int? NumberOfImages { get; } |
| 139 | + public ImagenAspectRatio? AspectRatio { get; } |
| 140 | + public ImagenImageFormat? ImageFormat { get; } |
| 141 | + public bool? AddWatermark { get; } |
| 142 | +#endif |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Initializes configuration options for generating images with Imagen. |
| 146 | + /// </summary> |
| 147 | + /// <param name="negativePrompt">Specifies elements to exclude from the generated image; |
| 148 | + /// disabled if not specified.</param> |
| 149 | + /// <param name="numberOfImages">The number of image samples to generate; |
| 150 | + /// defaults to 1 if not specified.</param> |
| 151 | + /// <param name="aspectRatio">The aspect ratio of generated images; |
| 152 | + /// defaults to to square, 1:1.</param> |
| 153 | + /// <param name="imageFormat">The image format of generated images; |
| 154 | + /// defaults to PNG.</param> |
| 155 | + /// <param name="addWatermark">Whether to add an invisible watermark to generated images; |
| 156 | + /// the default value depends on the model.</param> |
| 157 | + public ImagenGenerationConfig( |
| 158 | + string negativePrompt = null, |
| 159 | + int? numberOfImages = null, |
| 160 | + ImagenAspectRatio? aspectRatio = null, |
| 161 | + ImagenImageFormat? imageFormat = null, |
| 162 | + bool? addWatermark = null) { |
| 163 | + NegativePrompt = negativePrompt; |
| 164 | + NumberOfImages = numberOfImages; |
| 165 | + AspectRatio = aspectRatio; |
| 166 | + ImageFormat = imageFormat; |
| 167 | + AddWatermark = addWatermark; |
| 168 | + } |
| 169 | + |
| 170 | + private static string ConvertAspectRatio(ImagenAspectRatio aspectRatio) { |
| 171 | + return aspectRatio switch { |
| 172 | + ImagenAspectRatio.Square1x1 => "1:1", |
| 173 | + ImagenAspectRatio.Portrait9x16 => "9:16", |
| 174 | + ImagenAspectRatio.Landscape16x9 => "16:9", |
| 175 | + ImagenAspectRatio.Portrait3x4 => "3:4", |
| 176 | + ImagenAspectRatio.Landscape4x3 => "4:3", |
| 177 | + _ => aspectRatio.ToString(), // Fallback |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + /// <summary> |
| 182 | + /// Intended for internal use only. |
| 183 | + /// This method is used for serializing the object to JSON for the API request. |
| 184 | + /// </summary> |
| 185 | + internal Dictionary<string, object> ToJson() { |
| 186 | + Dictionary<string, object> jsonDict = new() { |
| 187 | + ["sampleCount"] = NumberOfImages ?? 1 |
| 188 | + }; |
| 189 | + if (!string.IsNullOrEmpty(NegativePrompt)) { |
| 190 | + jsonDict["negativePrompt"] = NegativePrompt; |
| 191 | + } |
| 192 | + if (AspectRatio != null) { |
| 193 | + jsonDict["aspectRatio"] = ConvertAspectRatio(AspectRatio.Value); |
| 194 | + } |
| 195 | + if (ImageFormat != null) { |
| 196 | + jsonDict["outputOptions"] = ImageFormat?.ToJson(); |
| 197 | + } |
| 198 | + if (AddWatermark != null) { |
| 199 | + jsonDict["addWatermark"] = AddWatermark; |
| 200 | + } |
| 201 | + |
| 202 | + return jsonDict; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | +} |
0 commit comments