From 602382a939f7359df07d01956265d16aa9dde60d Mon Sep 17 00:00:00 2001 From: Giorgio Garofalo Date: Wed, 9 Aug 2023 16:09:30 +0200 Subject: [PATCH] imagelib: Implement saveImage function --- .../main/kotlin/pikt/error/ImageValueType.kt | 1 + .../kotlin/pikt/imagelib/ImageFunctions.kt | 27 ++++++++++++++++--- imagelib/src/main/resources/colors.properties | 1 + 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/imagelib/src/main/kotlin/pikt/error/ImageValueType.kt b/imagelib/src/main/kotlin/pikt/error/ImageValueType.kt index e38d936..6b5c3be 100644 --- a/imagelib/src/main/kotlin/pikt/error/ImageValueType.kt +++ b/imagelib/src/main/kotlin/pikt/error/ImageValueType.kt @@ -6,4 +6,5 @@ package pikt.error object ImageValueType { const val IMAGE = "image" + const val WRITABLE_IMAGE = "writable image" } \ No newline at end of file diff --git a/imagelib/src/main/kotlin/pikt/imagelib/ImageFunctions.kt b/imagelib/src/main/kotlin/pikt/imagelib/ImageFunctions.kt index 959ee28..e802ea4 100644 --- a/imagelib/src/main/kotlin/pikt/imagelib/ImageFunctions.kt +++ b/imagelib/src/main/kotlin/pikt/imagelib/ImageFunctions.kt @@ -1,6 +1,8 @@ package pikt.imagelib import pikt.error.ImageValueType.IMAGE +import pikt.error.ImageValueType.WRITABLE_IMAGE +import pikt.error.PiktIOException import pikt.error.PiktWrongArgumentTypeException import pikt.error.ValueType.NUMBER import pikt.stdlib.newFile @@ -13,7 +15,7 @@ import java.io.File * Instantiates a new writable [Image]. * @param width image width as [Int] * @param height image height as [Int] - * @return a new readable and writable image + * @return a new readable and writable blank image */ fun newImage(width: Any, height: Any): WritableImage { if (width !is Int) { @@ -38,11 +40,30 @@ fun newImage(width: Any, height: Any): WritableImage { /** * Instantiates a writable [Image] from an image on disk. - * @param pathOrFile either a [File] or a [String] path + * @param pathOrFile either a [File] or a [String] path to load the image from * @return the loaded image + * @throws PiktIOException if the image could not be read */ fun newImage(pathOrFile: Any): WritableImage = AwtImage.fromFile(newFile(pathOrFile, requireExistance = true)) +/** + * Saves an image to file. + * @param pathOrFile either a [File] or a [String] path to save the image to + * @throws PiktIOException if the image could not be saved + */ +fun saveImage(image: Any, pathOrFile: Any) { + if (image !is WritableImage) { + throw PiktWrongArgumentTypeException( + parameterName = "image", + argumentValue = image, + expectedType = WRITABLE_IMAGE, + reference = object {} + ) + } + + image.save(newFile(pathOrFile)) +} + /** * @return width of [image] */ @@ -73,4 +94,4 @@ fun imageHeight(image: Any): Int { } return image.height -} \ No newline at end of file +} diff --git a/imagelib/src/main/resources/colors.properties b/imagelib/src/main/resources/colors.properties index 5ecd0d3..4a7c7ad 100644 --- a/imagelib/src/main/resources/colors.properties +++ b/imagelib/src/main/resources/colors.properties @@ -1,3 +1,4 @@ newImage=B41EFF +saveImage=681EFF imageWidth= imageHeight= \ No newline at end of file