diff options
| author | Fuwn <[email protected]> | 2024-05-14 15:27:00 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-05-14 15:27:00 -0700 |
| commit | 5fd1d1b3d215ef5244476cc260fec9c6aecd994f (patch) | |
| tree | c4687a4e4e59941aafd28ef8dde6af0d6d376c78 /src | |
| parent | e64b39d88be011009485a0e0faa2bbf323dc7714 (diff) | |
| download | mayu-5fd1d1b3d215ef5244476cc260fec9c6aecd994f.tar.xz mayu-5fd1d1b3d215ef5244476cc260fec9c6aecd994f.zip | |
refactor(image): deduce extension from informationv0.1.3
Diffstat (limited to 'src')
| -rw-r--r-- | src/image.gleam | 11 | ||||
| -rw-r--r-- | src/svg.gleam | 34 |
2 files changed, 20 insertions, 25 deletions
diff --git a/src/image.gleam b/src/image.gleam index 6672b46..c1ba7cf 100644 --- a/src/image.gleam +++ b/src/image.gleam @@ -1,10 +1,10 @@ import gleam/int -pub type ImageDimensions { - ImageDimensions(width: Int, height: Int) +pub type ImageInformation { + ImageInformation(width: Int, height: Int, extension: String) } -pub fn get_image_dimensions(image) { +pub fn get_image_information(image) { case image { <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, _:bits>> -> parse_png_chunks(image, 8) @@ -20,7 +20,7 @@ pub fn get_image_dimensions(image) { height_1:8, _rest:bits, >> -> - Ok(ImageDimensions( + Ok(ImageInformation( width_0 |> int.bitwise_or( width_1 @@ -31,6 +31,7 @@ pub fn get_image_dimensions(image) { height_1 |> int.bitwise_shift_left(8), ), + "gif", )) _ -> Error("Invalid PNG signature") } @@ -47,7 +48,7 @@ fn parse_png_chunks(image, offset) { width:32, height:32, _:bits, - >> -> Ok(ImageDimensions(width, height)) + >> -> Ok(ImageInformation(width, height, "png")) <<_:size(offset), length:32, _:4, _:bits>> -> parse_png_chunks(image, offset + length + 12) _ -> Error("Invalid PNG chunk") diff --git a/src/svg.gleam b/src/svg.gleam index 81494a1..3e0340d 100644 --- a/src/svg.gleam +++ b/src/svg.gleam @@ -9,13 +9,13 @@ type XmlImages { XmlImages(xml: String, width: Int, height: Int) } -fn image(data, dimensions: image.ImageDimensions, width, extension) { +fn image(data, image: image.ImageInformation, width) { "<image - height=\"" <> int.to_string(dimensions.height) <> "\" - width=\"" <> int.to_string(dimensions.width) <> "\" + height=\"" <> int.to_string(image.height) <> "\" + width=\"" <> int.to_string(image.width) <> "\" x=\"" <> int.to_string(width) <> "\" y=\"0\" - xlink:href=\"data:image/" <> extension <> ";base64," <> bit_array.base64_encode( + xlink:href=\"data:image/" <> image.extension <> ";base64," <> bit_array.base64_encode( data, False, ) <> "\"/>" @@ -24,12 +24,7 @@ fn image(data, dimensions: image.ImageDimensions, width, extension) { fn images(theme, digits, width, height, svgs) { case digits { [] -> XmlImages(string_builder.to_string(svgs), width, height) - [digit, ..rest] -> { - let extension = case theme { - "asoul" | "gelbooru" | "moebooru" | "rule34" | "urushi" -> "gif" - _ -> "png" - } - + [digit, ..rest] -> case simplifile.read_bits( from: "./themes/" @@ -37,28 +32,27 @@ fn images(theme, digits, width, height, svgs) { <> "/" <> int.to_string(digit) <> "." - <> extension, + <> case theme { + "gelbooru-h" | "moebooru-h" -> "png" + _ -> "gif" + }, ) { Ok(data) -> { - case image.get_image_dimensions(data) { - Ok(dimensions) -> + case image.get_image_information(data) { + Ok(information) -> images( theme, rest, - width + dimensions.width, - int.max(height, dimensions.height), - string_builder.append( - svgs, - image(data, dimensions, width, extension), - ), + width + information.width, + int.max(height, information.height), + string_builder.append(svgs, image(data, information, width)), ) Error(_) -> XmlImages(string_builder.to_string(svgs), width, height) } } Error(_) -> XmlImages(string_builder.to_string(svgs), width, height) } - } } } |