diff options
| author | Fuwn <[email protected]> | 2024-05-22 02:24:48 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-05-22 02:25:37 +0000 |
| commit | e828ac949174b7a15e7370514f5471a249465ef9 (patch) | |
| tree | ac363c7a675a5958414c532d7543e7e9a6629f05 | |
| parent | refactor(image): annotate image format case headers (diff) | |
| download | mayu-0.1.4.tar.xz mayu-0.1.4.zip | |
refactor(image): use purely bit array syntaxv0.1.4
| -rw-r--r-- | gleam.toml | 2 | ||||
| -rw-r--r-- | src/image.gleam | 27 |
2 files changed, 9 insertions, 20 deletions
@@ -2,7 +2,7 @@ # https://gleam.run/writing-gleam/gleam-toml/. name = "mayu" -version = "0.1.1" +version = "0.1.4" gleam = ">= 1.1.0" description = "Moe-Counter Compatible Website Hit Counter" licenses = ["GPL-3.0-only"] diff --git a/src/image.gleam b/src/image.gleam index 39f783c..7fafa0a 100644 --- a/src/image.gleam +++ b/src/image.gleam @@ -1,42 +1,31 @@ -import gleam/int - pub type ImageInformation { ImageInformation(width: Int, height: Int, extension: String) } pub fn get_image_information(image) { case image { - <<0x89, "PNG\r\n":utf8, 0x1A, 0x0A, _:bits>> -> parse_png_chunks(image, 8) + <<0x89, "PNG\r\n":utf8, 0x1A, "\n":utf8, _rest:bits>> -> + parse_png_chunks(image, 8) << "GIF":utf8, - _:16, - _:unsigned, - width_0:8, - width_1:8, - height_0:8, - height_1:8, + _version:unsigned-24, + width:little-16, + height:little-16, _rest:bits, - >> -> - Ok(ImageInformation( - int.bitwise_or(width_0, int.bitwise_shift_left(width_1, 8)), - int.bitwise_or(height_0, int.bitwise_shift_left(height_1, 8)), - "gif", - )) + >> -> Ok(ImageInformation(width, height, "gif")) _ -> Error("Unsupported image format") } } fn parse_png_chunks(image, offset) { - let offset_bits = offset * 8 - case image { << - _:size(offset_bits), + _:unit(8)-size(offset), _length:32, "IHDR":utf8, width:32, height:32, - _:bits, + _rest:bits, >> -> Ok(ImageInformation(width, height, "png")) <<_:size(offset), length:32, _:4, _:bits>> -> parse_png_chunks(image, offset + length + 12) |