Skip to content

Commit

Permalink
imagelib: Implement saveImage function
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Aug 9, 2023
1 parent f838bed commit 602382a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions imagelib/src/main/kotlin/pikt/error/ImageValueType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ package pikt.error
object ImageValueType {

const val IMAGE = "image"
const val WRITABLE_IMAGE = "writable image"
}
27 changes: 24 additions & 3 deletions imagelib/src/main/kotlin/pikt/imagelib/ImageFunctions.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -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]
*/
Expand Down Expand Up @@ -73,4 +94,4 @@ fun imageHeight(image: Any): Int {
}

return image.height
}
}
1 change: 1 addition & 0 deletions imagelib/src/main/resources/colors.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
newImage=B41EFF
saveImage=681EFF
imageWidth=
imageHeight=

0 comments on commit 602382a

Please sign in to comment.