aboutsummaryrefslogtreecommitdiff
path: root/src/image.gleam
blob: 39f783c6221c79ca628b6292ac53a59d99044ced (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)
    <<
      "GIF":utf8,
      _:16,
      _:unsigned,
      width_0:8,
      width_1:8,
      height_0:8,
      height_1:8,
      _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",
      ))
    _ -> Error("Unsupported image format")
  }
}

fn parse_png_chunks(image, offset) {
  let offset_bits = offset * 8

  case image {
    <<
      _:size(offset_bits),
      _length:32,
      "IHDR":utf8,
      width:32,
      height:32,
      _:bits,
    >> -> Ok(ImageInformation(width, height, "png"))
    <<_:size(offset), length:32, _:4, _:bits>> ->
      parse_png_chunks(image, offset + length + 12)
    _ -> Error("Invalid PNG chunk")
  }
}