<commit_before>/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (<EMAIL>).
 * See LICENSE for details.
 */

package com.almasb.fxgl.texture

import com.almasb.fxgl.core.concurrent.Async
import javafx.geometry.HorizontalDirection
import javafx.scene.Group
import javafx.scene.Node
import javafx.scene.SnapshotParameters
import javafx.scene.effect.BlendMode
import javafx.scene.image.Image
import javafx.scene.image.WritableImage
import javafx.scene.paint.Color
import javafx.scene.shape.Rectangle
import java.util.concurrent.Callable
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min



private val image: Image by lazy {
    val group = Group()
    val size = 32.0

    val r1 = Rectangle(size, size, Color.BLACK)
    val r2 = Rectangle(size, size, Color.HOTPINK)
    val r3 = Rectangle(size, size, Color.PURPLE)
    val r4 = Rectangle(size, size, Color.BLACK)

    r2.translateX = size
    r3.translateY = size
    r4.translateX = size
    r4.translateY = size

    r1.stroke = Color.GRAY
    r2.stroke = Color.GRAY
    r3.stroke = Color.GRAY
    r4.stroke = Color.GRAY

    group.children.addAll(r1, r2, r3, r4)

    var result: Image? = null

    Async.startAsyncFX {
        result = group.snapshot(null, null)
    }.await()

    result!!
}

fun getDummyImage() = image

fun toImage(node: Node): Image = Async.startAsyncFX(Callable {
    val params = SnapshotParameters()
    params.fill = Color.TRANSPARENT
    node.snapshot(params, null)
}).await()

fun merge(images: List<Image>): Image {
    if (images.isEmpty())
        return getDummyImage()

    if (images.size == 1)
        return images.first()

    var texture = Texture(images.first())

    images.drop(1).forEach {
        texture = texture.superTexture(Texture(it), HorizontalDirection.RIGHT)
    }

    return texture.image
}

/**
 * Resize image, with preserveRatio set to false by default.
 * If preserveRatio is true, vertical scaling will be dependent on horizontal scaling and original image proportion will be reserved.
 */
fun resize(image : Image, targetWidth: Int, targetHeight: Int): Image {
    return resize(image, targetWidth, targetHeight, false)
}

/**
 * Resize image.
 * If preserveRatio is true, vertical scaling will be dependent on horizontal scaling and original image proportion will be reserved.
 */
fun resize(image : Image, targetWidth: Int, targetHeight: Int, preserveRatio: Boolean): Image {
    val width = image.width.toInt()<commit_msg>added a method to obtain pixels of an image<commit_after>/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (<EMAIL>).
 * See LICENSE for details.
 */

package com.almasb.fxgl.texture

import com.almasb.fxgl.core.concurrent.Async
import javafx.geometry.HorizontalDirection
import javafx.scene.Group
import javafx.scene.Node
import javafx.scene.SnapshotParameters
import javafx.scene.effect.BlendMode
import javafx.scene.image.Image
import javafx.scene.image.WritableImage
import javafx.scene.paint.Color
import javafx.scene.shape.Rectangle
import java.util.concurrent.Callable
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min



private val image: Image by lazy {
    val group = Group()
    val size = 32.0

    val r1 = Rectangle(size, size, Color.BLACK)
    val r2 = Rectangle(size, size, Color.HOTPINK)
    val r3 = Rectangle(size, size, Color.PURPLE)
    val r4 = Rectangle(size, size, Color.BLACK)

    r2.translateX = size
    r3.translateY = size
    r4.translateX = size
    r4.translateY = size

    r1.stroke = Color.GRAY
    r2.stroke = Color.GRAY
    r3.stroke = Color.GRAY
    r4.stroke = Color.GRAY

    group.children.addAll(r1, r2, r3, r4)

    var result: Image? = null

    Async.startAsyncFX {
        result = group.snapshot(null, null)
    }.await()

    result!!
}

fun getDummyImage() = image

fun toImage(node: Node): Image = Async.startAsyncFX(Callable {
    val params = SnapshotParameters()
    params.fill = Color.TRANSPARENT
    node.snapshot(params, null)
}).await()

fun toPixels(image: Image): List<Pixel> {
    val result = arrayListOf<Pixel>()
    image.map {
        result += it
        it
    }
    return result
}

fun merge(images: List<Image>): Image {
    if (images.isEmpty())
        return getDummyImage()

    if (images.size == 1)
        return images.first()

    var texture = Texture(images.first())

    images.drop(1).forEach {
        texture = texture.superTexture(Texture(it), HorizontalDirection.RIGHT)
    }

    return texture.image
}

/**
 * Resize image, with preserveRatio set to false by default.
 * If preserveRatio is true, vertical scaling will be dependent on horizontal scaling and original image proportion will be reserved.
 */
fun resize(image : Image, targetWidth: Int, targetHeight: Int): Image {
    return resize(image, targetWidth, targetHeight, false)
}

/**
 * Resize image.
 * If preserveRatio is true, vertical scaling will be dependent on horizontal scaling and original image proportion will be reserved.
 */
fun resize(image : Image, targetWidth: Int, targetHeight: Int, preserveRatio: Boolean): Image {
    val width = image.width.toInt()
{'fraction_non_alphanumeric': 0.10428377592556697, 'fraction_numerical': 0.008141112618724558, 'mean_word_length': 3.463667820069204, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3137923', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1590, 'n_words': 464}