Skip to content

Commit eab55b5

Browse files
committed
feat: added interpolateIntermediateImages()
1 parent 87ca500 commit eab55b5

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

fxgl-core/src/main/kotlin/com/almasb/fxgl/texture/Images.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package com.almasb.fxgl.texture
88

9+
import com.almasb.fxgl.animation.AnimatedImage
910
import com.almasb.fxgl.core.concurrent.Async
1011
import com.almasb.fxgl.logging.Logger
1112
import javafx.scene.Group
@@ -611,3 +612,34 @@ fun writeToFile(image: Image, filePath: Path): Boolean {
611612
}
612613
}
613614

615+
/**
616+
* Using given [images] interpolates between them to produce intermediate images (inbetweening).
617+
* The given [images] list must have at least 2 images, otherwise a new list containing the originals is returned.
618+
*
619+
* @return list with intermediate images, including the original images.
620+
* The new list size is (images.size - 1) * [numFramesBetweenImages] + (images.size).
621+
*/
622+
fun interpolateIntermediateImages(images: List<Image>, numFramesBetweenImages: Int): List<Image> {
623+
if (images.size < 2)
624+
return ArrayList(images)
625+
626+
val result = arrayListOf<Image>()
627+
628+
images.zipWithNext().forEach { (img1, img2) ->
629+
val anim = AnimatedImage(img1, img2)
630+
631+
result += img1
632+
633+
repeat(numFramesBetweenImages) { i ->
634+
635+
// we add +1 since i == 0 will give us [img1], which we add manually, so ignore
636+
// and i+1 == numFramesBetweenImages will give us [img2],
637+
// which we also add manually during next cycle, so ignore by +1
638+
result += anim.getValue((i + 1) / (numFramesBetweenImages.toDouble() + 1))
639+
}
640+
}
641+
642+
result += images.last()
643+
644+
return result
645+
}

fxgl-core/src/test/kotlin/com/almasb/fxgl/texture/ImagesTest.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import javafx.geometry.HorizontalDirection
1010
import javafx.geometry.VerticalDirection
1111
import javafx.scene.paint.Color
1212
import org.hamcrest.MatcherAssert.assertThat
13+
import org.hamcrest.Matchers.*
1314
import org.junit.jupiter.api.Test
1415

15-
1616
/**
1717
*
1818
* @author Almas Baimagambetov ([email protected])
@@ -64,4 +64,17 @@ class ImagesTest {
6464

6565
assertThat("Diagonal flip is not correct", matchPixels(expected, Texture(result)))
6666
}
67+
68+
@Test
69+
fun `Interpolate intermediate images`() {
70+
val img1 = ColoredTexture(200, 200, Color.WHITE).image
71+
val img2 = ColoredTexture(200, 200, Color.RED).image
72+
val img3 = ColoredTexture(200, 200, Color.BLACK).image
73+
74+
val result = interpolateIntermediateImages(listOf(img1, img2, img3), 10)
75+
assertThat(result.size, `is`(10*2 + 3))
76+
assertThat(result[0], `is`(img1))
77+
assertThat(result[11], `is`(img2))
78+
assertThat(result[22], `is`(img3))
79+
}
6780
}

0 commit comments

Comments
 (0)