Skip to content

Commit

Permalink
Add data provider for Pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Nov 16, 2024
1 parent 8e77afd commit bbf4605
Show file tree
Hide file tree
Showing 204 changed files with 204 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ class FakerService {
* has declared a provider that matches the [simpleClassName] parameter.
*/
private fun getProviderData(primary: YamlCategory, secondary: Category? = null): YamlCategoryData {
return dictionary[primary]
return dictionary[primary]
?: secondary?.let { load(primary, secondary)[primary] }
?: load(primary)[primary]
?: throw NoSuchElementException("Category $primary not found in $this")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
---

== `Faker().uiFaces`

.Available Functions
[%collapsible]
====
[source,kotlin]
----
Faker().uiFaces.avatar()
----
====
53 changes: 53 additions & 0 deletions faker/pictures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## `PicturesFaker`

[![Maven Central](https://img.shields.io/maven-central/v/io.github.serpro69/kotlin-faker-pictures?style=for-the-badge)](https://search.maven.org/artifact/io.github.serpro69/kotlin-faker-pictures)
[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/io.github.serpro69/kotlin-faker-pictures?label=snapshot-version&server=https%3A%2F%2Foss.sonatype.org&style=for-the-badge&color=yellow)](#downloading)

Provides access to fake data generators within the Pictures domain.

## Usage

Documentation for kotlin-faker is available at [serpro69.github.io/kotlin-faker/](https://serpro69.github.io/kotlin-faker/).

### Downloading

Latest releases are always available on maven central.

**With gradle**

```groovy
dependencies {
implementation 'io.github.serpro69:kotlin-faker:$version'
implementation 'io.github.serpro69:kotlin-faker-pictures:$version'
}
```

**With maven**

```xml
<dependencies>
<dependency>
<groupId>io.github.serpro69</groupId>
<artifactId>kotlin-faker</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>io.github.serpro69</groupId>
<artifactId>kotlin-faker-pictures</artifactId>
<version>${version}</version>
</dependency>
</dependencies>
```

_NB! An additional fake data provider like 'pictures' requires the main `kotlin-faker` dependency to be on the classpath._

### Generating data

```kotlin
// NB! the package you import if using multiple fakers
import io.github.serpro69.kfaker.pictures.faker

val faker = faker { }

faker.uiFaces.avatar()
```
3 changes: 3 additions & 0 deletions faker/pictures/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
`faker-provider-conventions`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.serpro69.kfaker.books

import io.github.serpro69.kfaker.test.helper.`every public function in each provider is invoked without exceptions`
import io.github.serpro69.kfaker.test.helper.`faker instance is initialized with custom locale`
import io.kotest.core.spec.style.DescribeSpec

class BooksFakerIT : DescribeSpec({
`every public function in each provider is invoked without exceptions`(BooksFaker())

`faker instance is initialized with custom locale` { faker { } }
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.github.serpro69.kfaker.pictures

import io.github.serpro69.kfaker.AbstractFaker
import io.github.serpro69.kfaker.FakerConfig
import io.github.serpro69.kfaker.FakerDsl
import io.github.serpro69.kfaker.fakerConfig
import io.github.serpro69.kfaker.pictures.provider.UiFaces
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import javax.imageio.ImageIO

/**
* Typealias for the [PicturesFaker]
*/
typealias Faker = PicturesFaker

/**
* Provides access to fake data generators within the Pictures domain.
*
* Each category (generator) from this [PicturesFaker] is represented by a property
* that (usually) has the same name as the `.yml` dictionary file.
*
* @property unique global provider for generation of unique values.
*/
@Suppress("unused")
class PicturesFaker @JvmOverloads constructor(config: FakerConfig = fakerConfig { }) : AbstractFaker(config) {

val uiFaces: UiFaces by lazy { UiFaces(randomService) }

@FakerDsl
/**
* DSL builder for creating instances of [Faker]
*/
class Builder internal constructor() : AbstractFaker.Builder<Faker>() {

/**
* Builds an instance of [Faker] with this [config].
*/
override fun build(): Faker = Faker(config)
}
}

/**
* Applies the [block] function to [PicturesFaker.Builder]
* and returns as an instance of [PicturesFaker] from that builder.
*/
fun faker(block: PicturesFaker.Builder.() -> Unit): PicturesFaker = PicturesFaker.Builder().apply(block).build()

/**
* Returns the [input] as 32bit RGBA [ByteArray]
*/
internal fun convertTo32BitRGBA(input: ByteArray): ByteArray {
// read the input ByteArray into a BufferedImage
val inputImage = ImageIO.read(ByteArrayInputStream(input))
?: throw IllegalArgumentException("Invalid image data")
// create a new BufferedImage in 32-bit RGBA format
val rgbaImage = BufferedImage(inputImage.width, inputImage.height, BufferedImage.TYPE_INT_ARGB)
// draw the input image onto the RGBA BufferedImage
val graphics = rgbaImage.createGraphics()
graphics.drawImage(inputImage, 0, 0, null)
graphics.dispose()
// extract raw RGBA data from the BufferedImage
val width = rgbaImage.width
val height = rgbaImage.height
val pixelData = IntArray(width * height)
rgbaImage.getRGB(0, 0, width, height, pixelData, 0, width)
// convert the IntArray to a ByteArray
val byteArrayOutput = ByteArrayOutputStream()
for (pixel in pixelData) {
byteArrayOutput.write((pixel shr 16) and 0xFF) // Red
byteArrayOutput.write((pixel shr 8) and 0xFF) // Green
byteArrayOutput.write(pixel and 0xFF) // Blue
byteArrayOutput.write((pixel shr 24) and 0xFF) // Alpha
}
return byteArrayOutput.toByteArray()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@file:Suppress("unused")

package io.github.serpro69.kfaker.pictures.provider

import io.github.serpro69.kfaker.RandomService
import io.github.serpro69.kfaker.pictures.convertTo32BitRGBA
import io.github.serpro69.kfaker.provider.FakeDataProvider

/**
* [FakeDataProvider] implementation for AI-generated [UiFaces](https://uifaces.co/#browse-avatars) avatars.
*/
class UiFaces internal constructor(
private val randomService: RandomService
) : FakeDataProvider {
fun avatar(type: UiFacesAvatarType? = null): ByteArray {
val t = type ?: randomService.nextEnum()
val n = randomService.nextInt(1, t.size)
val instr = requireNotNull(javaClass.classLoader.getResourceAsStream("uifaces/$t/$n.jpg")) {
"UiFaces avatar $t/$n.jpg does not exist"
}
return convertTo32BitRGBA(instr.readBytes())
}
}

enum class UiFacesAvatarType(val size: Int) {
ABSTRACT(51),
ALIEN(18),
CARTOON(32),
HUMAN(78),
ROBOT(15),
;

override fun toString(): String = name.lowercase()
}
11 changes: 11 additions & 0 deletions faker/pictures/src/main/resources/uifaces/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This repo contains free AI-generated avatars from [uifaces](https://uifaces.co/#browse-avatars).

These images were downloaded from the above URI and are provided as is without any warranty.

As of 2024-10-15, the AI-generated avatars on UI Faces are distributed under the following terms:

> ✨ AI-generated images
>
> Images generated by OpenAI's DALL-E are offered under a license that allows for both internal and commercial use. This means you can freely utilize these AI-generated photos in a variety of settings, from personal projects to commercial applications, without concerns about licensing restrictions.
See the [uifaces_licenses](./uifaces_licenses.png) and the [licenses](https://uifaces.co/licenses) page for more details.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ val fakers =
"misc",
"movies",
"music",
"pictures",
"sports",
"tech",
"travel",
Expand Down

0 comments on commit bbf4605

Please sign in to comment.