defmodule Imagineer.Image.PNG.PngSuite.Basic.GrayscaleTest do
  use ExUnit.Case, async: true

  @test_path "test/support/images/pngsuite/basic/"
  @tmp_path "./tmp/"

  @actual_pixels [
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      0,
      0,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      0,
      0,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      0,
      0,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      1,
      1,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      0,
      0,
      0,
      0,
      1,
      1,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      0,
      0,
      0,
      0,
      1,
      1,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      0,
      0,
      0,
      0,
      1,
      1,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      0,
      0,
      0,
      0,
      1,
      1,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    [
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ]
  ]

  test "single bit grayscale" do
    {:ok, image} = Imagineer.load(@test_path <> "basn0g01.png")

    assert image.height == 32
    assert image.width == 32

    assert image.color_format == :grayscale
    assert image.compression == :zlib
    assert image.color_type == 0
    assert image.interlace_method == 0
    assert image.gamma == 1.0
    assert image.bit_depth == 1
    assert image.mask == nil
    assert image.format == :png
    assert image.mime_type == "image/png"
    assert image.palette == []

    assert_pixels_match(extract_single_channel_pixels(image), @actual_pixels)

    :ok = Imagineer.write(image, @tmp_path <> "basn0g01_test.png")
    {:ok, image} = Imagineer.load(@tmp_path <> "basn0g01_test.png")

    assert image.height == 32
    assert image.width == 32

    assert image.color_format == :grayscale
    assert image.compression == :zlib
    assert image.color_type == 0
    assert image.interlace_method == 0
    assert image.gamma == 1.0
    assert image.bit_depth == 1
    assert image.mask == nil
    assert image.format == :png
    assert image.mime_type == "image/png"
    assert image.palette == []

    assert_pixels_match(extract_single_channel_pixels(image), @actual_pixels)
  end

  @actual_pixels [
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3
    ],
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3
    ],
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3
    ],
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1
    ],
    [
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2
    ],
    [
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2
    ],
    [
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2
    ],
    [
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2
    ],
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3
    ],
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3
    ],
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3
    ],
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1
    ],
    [
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2
    ],
    [
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2
    ],
    [
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2
    ],
    [
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2
    ]
  ]

  test "two bit grayscale" do
    {:ok, image} = Imagineer.load(@test_path <> "basn0g02.png")

    assert image.height == 32
    assert image.width == 32

    assert image.color_format == :grayscale
    assert image.compression == :zlib
    assert image.color_type == 0
    assert image.interlace_method == 0
    assert image.gamma == 1.0
    assert image.bit_depth == 2
    assert image.mask == nil
    assert image.format == :png
    assert image.mime_type == "image/png"
    assert image.palette == []

    assert_pixels_match(extract_single_channel_pixels(image), @actual_pixels)

    :ok = Imagineer.write(image, @tmp_path <> "basn0g02_test.png")
    {:ok, image} = Imagineer.load(@tmp_path <> "basn0g02_test.png")

    assert image.height == 32
    assert image.width == 32

    assert image.color_format == :grayscale
    assert image.compression == :zlib
    assert image.color_type == 0
    assert image.interlace_method == 0
    assert image.gamma == 1.0
    assert image.bit_depth == 2
    assert image.mask == nil
    assert image.format == :png
    assert image.mime_type == "image/png"
    assert image.palette == []

    assert_pixels_match(extract_single_channel_pixels(image), @actual_pixels)
  end

  @actual_pixels [
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7
    ],
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7
    ],
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7
    ],
    [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8
    ],
    [
      1,
      1,
      1,
      1,
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9
    ],
    [
      2,
      2,
      2,
      2,
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9
    ],
    [
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10
    ],
    [
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10
    ],
    [
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10
    ],
    [
      3,
      3,
      3,
      3,
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10
    ],
    [
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11
    ],
    [
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11
    ],
    [
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11
    ],
    [
      4,
      4,
      4,
      4,
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11
    ],
    [
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12
    ],
    [
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12
    ],
    [
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12
    ],
    [
      5,
      5,
      5,
      5,
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12
    ],
    [
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12,
      13,
      13,
      13,
      13
    ],
    [
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12,
      13,
      13,
      13,
      13
    ],
    [
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12,
      13,
      13,
      13,
      13
    ],
    [
      6,
      6,
      6,
      6,
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12,
      13,
      13,
      13,
      13
    ],
    [
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12,
      13,
      13,
      13,
      13,
      14,
      14,
      14,
      14
    ],
    [
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12,
      13,
      13,
      13,
      13,
      14,
      14,
      14,
      14
    ],
    [
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12,
      13,
      13,
      13,
      13,
      14,
      14,
      14,
      14
    ],
    [
      7,
      7,
      7,
      7,
      8,
      8,
      8,
      8,
      9,
      9,
      9,
      9,
      10,
      10,
      10,
      10,
      11,
      11,
      11,
      11,
      12,
      12,
      12,
      12,
      13,
      13,
      13,
      13,
      14,
      14,
      14,
      14
    ]
  ]

  test "four bit grayscale" do
    {:ok, image} = Imagineer.load(@test_path <> "basn0g04.png")

    assert image.height == 32
    assert image.width == 32

    assert image.color_format == :grayscale
    assert image.compression == :zlib
    assert image.color_type == 0
    assert image.interlace_method == 0
    assert image.gamma == 1.0
    assert image.bit_depth == 4
    assert image.mask == nil
    assert image.format == :png
    assert image.mime_type == "image/png"
    assert image.palette == []

    assert_pixels_match(extract_single_channel_pixels(image), @actual_pixels)

    :ok = Imagineer.write(image, @tmp_path <> "basn0g04_test.png")
    {:ok, image} = Imagineer.load(@tmp_path <> "basn0g04_test.png")

    assert image.height == 32
    assert image.width == 32

    assert image.color_format == :grayscale
    assert image.compression == :zlib
    assert image.color_type == 0
    assert image.interlace_method == 0
    assert image.gamma == 1.0
    assert image.bit_depth == 4
    assert image.mask == nil
    assert image.format == :png
    assert image.mime_type == "image/png"
    assert image.palette == []

    assert_pixels_match(extract_single_channel_pixels(image), @actual_pixels)
  end

  @actual_pixels [
    [
      {0},
      {2304},
      {4608},
      {6912},
      {9216},
      {11520},
      {13824},
      {16128},
      {18432},
      {20736},
      {23040},
      {25344},
      {27648},
      {29952},
      {32256},
      {34560},
      {36864},
      {39168},
      {41472},
      {43776},
      {46080},
      {48384},
      {50688},
      {52992},
      {55296},
      {57600},
      {59904},
      {62208},
      {64512},
      {61695},
      {54783},
      {47871}
    ],
    [
      {512},
      {2816},
      {5120},
      {7424},
      {9728},
      {12032},
      {14336},
      {16640},
      {18944},
      {21248},
      {23552},
      {25856},
      {28160},
      {30464},
      {32768},
      {35072},
      {37376},
      {39680},
      {41984},
      {44288},
      {46592},
      {48896},
      {51200},
      {53504},
      {55808},
      {58112},
      {60416},
      {62720},
      {65024},
      {60159},
      {53247},
      {46335}
    ],
    [
      {1024},
      {3328},
      {5632},
      {7936},
      {10240},
      {12544},
      {14848},
      {17152},
      {19456},
      {21760},
      {24064},
      {26368},
      {28672},
      {30976},
      {33280},
      {35584},
      {37888},
      {40192},
      {42496},
      {44800},
      {47104},
      {49408},
      {51712},
      {54016},
      {56320},
      {58624},
      {60928},
      {63232},
      {65535},
      {58623},
      {51711},
      {44799}
    ],
    [
      {1536},
      {3840},
      {6144},
      {8448},
      {10752},
      {13056},
      {15360},
      {17664},
      {19968},
      {22272},
      {24576},
      {26880},
      {29184},
      {31488},
      {33792},
      {36096},
      {38400},
      {40704},
      {43008},
      {45312},
      {47616},
      {49920},
      {52224},
      {54528},
      {56832},
      {59136},
      {61440},
      {63744},
      {63999},
      {57087},
      {50175},
      {43263}
    ],
    [
      {2048},
      {4352},
      {6656},
      {8960},
      {11264},
      {13568},
      {15872},
      {18176},
      {20480},
      {22784},
      {25088},
      {27392},
      {29696},
      {32000},
      {34304},
      {36608},
      {38912},
      {41216},
      {43520},
      {45824},
      {48128},
      {50432},
      {52736},
      {55040},
      {57344},
      {59648},
      {61952},
      {64256},
      {62463},
      {55551},
      {48639},
      {41727}
    ],
    [
      {2560},
      {4864},
      {7168},
      {9472},
      {11776},
      {14080},
      {16384},
      {18688},
      {20992},
      {23296},
      {25600},
      {27904},
      {30208},
      {32512},
      {34816},
      {37120},
      {39424},
      {41728},
      {44032},
      {46336},
      {48640},
      {50944},
      {53248},
      {55552},
      {57856},
      {60160},
      {62464},
      {64768},
      {60927},
      {54015},
      {47103},
      {40191}
    ],
    [
      {3072},
      {5376},
      {7680},
      {9984},
      {12288},
      {14592},
      {16896},
      {19200},
      {21504},
      {23808},
      {26112},
      {28416},
      {30720},
      {33024},
      {35328},
      {37632},
      {39936},
      {42240},
      {44544},
      {46848},
      {49152},
      {51456},
      {53760},
      {56064},
      {58368},
      {60672},
      {62976},
      {65280},
      {59391},
      {52479},
      {45567},
      {38655}
    ],
    [
      {3584},
      {5888},
      {8192},
      {10496},
      {12800},
      {15104},
      {17408},
      {19712},
      {22016},
      {24320},
      {26624},
      {28928},
      {31232},
      {33536},
      {35840},
      {38144},
      {40448},
      {42752},
      {45056},
      {47360},
      {49664},
      {51968},
      {54272},
      {56576},
      {58880},
      {61184},
      {63488},
      {64767},
      {57855},
      {50943},
      {44031},
      {37119}
    ],
    [
      {4096},
      {6400},
      {8704},
      {11008},
      {13312},
      {15616},
      {17920},
      {20224},
      {22528},
      {24832},
      {27136},
      {29440},
      {31744},
      {34048},
      {36352},
      {38656},
      {40960},
      {43264},
      {45568},
      {47872},
      {50176},
      {52480},
      {54784},
      {57088},
      {59392},
      {61696},
      {64000},
      {63231},
      {56319},
      {49407},
      {42495},
      {35583}
    ],
    [
      {4608},
      {6912},
      {9216},
      {11520},
      {13824},
      {16128},
      {18432},
      {20736},
      {23040},
      {25344},
      {27648},
      {29952},
      {32256},
      {34560},
      {36864},
      {39168},
      {41472},
      {43776},
      {46080},
      {48384},
      {50688},
      {52992},
      {55296},
      {57600},
      {59904},
      {62208},
      {64512},
      {61695},
      {54783},
      {47871},
      {40959},
      {34047}
    ],
    [
      {5120},
      {7424},
      {9728},
      {12032},
      {14336},
      {16640},
      {18944},
      {21248},
      {23552},
      {25856},
      {28160},
      {30464},
      {32768},
      {35072},
      {37376},
      {39680},
      {41984},
      {44288},
      {46592},
      {48896},
      {51200},
      {53504},
      {55808},
      {58112},
      {60416},
      {62720},
      {65024},
      {60159},
      {53247},
      {46335},
      {39423},
      {32511}
    ],
    [
      {5632},
      {7936},
      {10240},
      {12544},
      {14848},
      {17152},
      {19456},
      {21760},
      {24064},
      {26368},
      {28672},
      {30976},
      {33280},
      {35584},
      {37888},
      {40192},
      {42496},
      {44800},
      {47104},
      {49408},
      {51712},
      {54016},
      {56320},
      {58624},
      {60928},
      {63232},
      {65535},
      {58623},
      {51711},
      {44799},
      {37887},
      {30975}
    ],
    [
      {6144},
      {8448},
      {10752},
      {13056},
      {15360},
      {17664},
      {19968},
      {22272},
      {24576},
      {26880},
      {29184},
      {31488},
      {33792},
      {36096},
      {38400},
      {40704},
      {43008},
      {45312},
      {47616},
      {49920},
      {52224},
      {54528},
      {56832},
      {59136},
      {61440},
      {63744},
      {63999},
      {57087},
      {50175},
      {43263},
      {36351},
      {29439}
    ],
    [
      {6656},
      {8960},
      {11264},
      {13568},
      {15872},
      {18176},
      {20480},
      {22784},
      {25088},
      {27392},
      {29696},
      {32000},
      {34304},
      {36608},
      {38912},
      {41216},
      {43520},
      {45824},
      {48128},
      {50432},
      {52736},
      {55040},
      {57344},
      {59648},
      {61952},
      {64256},
      {62463},
      {55551},
      {48639},
      {41727},
      {34815},
      {27903}
    ],
    [
      {7168},
      {9472},
      {11776},
      {14080},
      {16384},
      {18688},
      {20992},
      {23296},
      {25600},
      {27904},
      {30208},
      {32512},
      {34816},
      {37120},
      {39424},
      {41728},
      {44032},
      {46336},
      {48640},
      {50944},
      {53248},
      {55552},
      {57856},
      {60160},
      {62464},
      {64768},
      {60927},
      {54015},
      {47103},
      {40191},
      {33279},
      {26367}
    ],
    [
      {7680},
      {9984},
      {12288},
      {14592},
      {16896},
      {19200},
      {21504},
      {23808},
      {26112},
      {28416},
      {30720},
      {33024},
      {35328},
      {37632},
      {39936},
      {42240},
      {44544},
      {46848},
      {49152},
      {51456},
      {53760},
      {56064},
      {58368},
      {60672},
      {62976},
      {65280},
      {59391},
      {52479},
      {45567},
      {38655},
      {31743},
      {24831}
    ],
    [
      {8192},
      {10496},
      {12800},
      {15104},
      {17408},
      {19712},
      {22016},
      {24320},
      {26624},
      {28928},
      {31232},
      {33536},
      {35840},
      {38144},
      {40448},
      {42752},
      {45056},
      {47360},
      {49664},
      {51968},
      {54272},
      {56576},
      {58880},
      {61184},
      {63488},
      {64767},
      {57855},
      {50943},
      {44031},
      {37119},
      {30207},
      {23295}
    ],
    [
      {8704},
      {11008},
      {13312},
      {15616},
      {17920},
      {20224},
      {22528},
      {24832},
      {27136},
      {29440},
      {31744},
      {34048},
      {36352},
      {38656},
      {40960},
      {43264},
      {45568},
      {47872},
      {50176},
      {52480},
      {54784},
      {57088},
      {59392},
      {61696},
      {64000},
      {63231},
      {56319},
      {49407},
      {42495},
      {35583},
      {28671},
      {21759}
    ],
    [
      {9216},
      {11520},
      {13824},
      {16128},
      {18432},
      {20736},
      {23040},
      {25344},
      {27648},
      {29952},
      {32256},
      {34560},
      {36864},
      {39168},
      {41472},
      {43776},
      {46080},
      {48384},
      {50688},
      {52992},
      {55296},
      {57600},
      {59904},
      {62208},
      {64512},
      {61695},
      {54783},
      {47871},
      {40959},
      {34047},
      {27135},
      {20223}
    ],
    [
      {9728},
      {12032},
      {14336},
      {16640},
      {18944},
      {21248},
      {23552},
      {25856},
      {28160},
      {30464},
      {32768},
      {35072},
      {37376},
      {39680},
      {41984},
      {44288},
      {46592},
      {48896},
      {51200},
      {53504},
      {55808},
      {58112},
      {60416},
      {62720},
      {65024},
      {60159},
      {53247},
      {46335},
      {39423},
      {32511},
      {25599},
      {18687}
    ],
    [
      {10240},
      {12544},
      {14848},
      {17152},
      {19456},
      {21760},
      {24064},
      {26368},
      {28672},
      {30976},
      {33280},
      {35584},
      {37888},
      {40192},
      {42496},
      {44800},
      {47104},
      {49408},
      {51712},
      {54016},
      {56320},
      {58624},
      {60928},
      {63232},
      {65535},
      {58623},
      {51711},
      {44799},
      {37887},
      {30975},
      {24063},
      {17151}
    ],
    [
      {10752},
      {13056},
      {15360},
      {17664},
      {19968},
      {22272},
      {24576},
      {26880},
      {29184},
      {31488},
      {33792},
      {36096},
      {38400},
      {40704},
      {43008},
      {45312},
      {47616},
      {49920},
      {52224},
      {54528},
      {56832},
      {59136},
      {61440},
      {63744},
      {63999},
      {57087},
      {50175},
      {43263},
      {36351},
      {29439},
      {22527},
      {15615}
    ],
    [
      {11264},
      {13568},
      {15872},
      {18176},
      {20480},
      {22784},
      {25088},
      {27392},
      {29696},
      {32000},
      {34304},
      {36608},
      {38912},
      {41216},
      {43520},
      {45824},
      {48128},
      {50432},
      {52736},
      {55040},
      {57344},
      {59648},
      {61952},
      {64256},
      {62463},
      {55551},
      {48639},
      {41727},
      {34815},
      {27903},
      {20991},
      {14079}
    ],
    [
      {11776},
      {14080},
      {16384},
      {18688},
      {20992},
      {23296},
      {25600},
      {27904},
      {30208},
      {32512},
      {34816},
      {37120},
      {39424},
      {41728},
      {44032},
      {46336},
      {48640},
      {50944},
      {53248},
      {55552},
      {57856},
      {60160},
      {62464},
      {64768},
      {60927},
      {54015},
      {47103},
      {40191},
      {33279},
      {26367},
      {19455},
      {12543}
    ],
    [
      {12288},
      {14592},
      {16896},
      {19200},
      {21504},
      {23808},
      {26112},
      {28416},
      {30720},
      {33024},
      {35328},
      {37632},
      {39936},
      {42240},
      {44544},
      {46848},
      {49152},
      {51456},
      {53760},
      {56064},
      {58368},
      {60672},
      {62976},
      {65280},
      {59391},
      {52479},
      {45567},
      {38655},
      {31743},
      {24831},
      {17919},
      {11007}
    ],
    [
      {12800},
      {15104},
      {17408},
      {19712},
      {22016},
      {24320},
      {26624},
      {28928},
      {31232},
      {33536},
      {35840},
      {38144},
      {40448},
      {42752},
      {45056},
      {47360},
      {49664},
      {51968},
      {54272},
      {56576},
      {58880},
      {61184},
      {63488},
      {64767},
      {57855},
      {50943},
      {44031},
      {37119},
      {30207},
      {23295},
      {16383},
      {9471}
    ],
    [
      {13312},
      {15616},
      {17920},
      {20224},
      {22528},
      {24832},
      {27136},
      {29440},
      {31744},
      {34048},
      {36352},
      {38656},
      {40960},
      {43264},
      {45568},
      {47872},
      {50176},
      {52480},
      {54784},
      {57088},
      {59392},
      {61696},
      {64000},
      {63231},
      {56319},
      {49407},
      {42495},
      {35583},
      {28671},
      {21759},
      {14847},
      {7935}
    ],
    [
      {13824},
      {16128},
      {18432},
      {20736},
      {23040},
      {25344},
      {27648},
      {29952},
      {32256},
      {34560},
      {36864},
      {39168},
      {41472},
      {43776},
      {46080},
      {48384},
      {50688},
      {52992},
      {55296},
      {57600},
      {59904},
      {62208},
      {64512},
      {61695},
      {54783},
      {47871},
      {40959},
      {34047},
      {27135},
      {20223},
      {13311},
      {6399}
    ],
    [
      {14336},
      {16640},
      {18944},
      {21248},
      {23552},
      {25856},
      {28160},
      {30464},
      {32768},
      {35072},
      {37376},
      {39680},
      {41984},
      {44288},
      {46592},
      {48896},
      {51200},
      {53504},
      {55808},
      {58112},
      {60416},
      {62720},
      {65024},
      {60159},
      {53247},
      {46335},
      {39423},
      {32511},
      {25599},
      {18687},
      {11775},
      {4863}
    ],
    [
      {14848},
      {17152},
      {19456},
      {21760},
      {24064},
      {26368},
      {28672},
      {30976},
      {33280},
      {35584},
      {37888},
      {40192},
      {42496},
      {44800},
      {47104},
      {49408},
      {51712},
      {54016},
      {56320},
      {58624},
      {60928},
      {63232},
      {65535},
      {58623},
      {51711},
      {44799},
      {37887},
      {30975},
      {24063},
      {17151},
      {10239},
      {3327}
    ],
    [
      {15360},
      {17664},
      {19968},
      {22272},
      {24576},
      {26880},
      {29184},
      {31488},
      {33792},
      {36096},
      {38400},
      {40704},
      {43008},
      {45312},
      {47616},
      {49920},
      {52224},
      {54528},
      {56832},
      {59136},
      {61440},
      {63744},
      {63999},
      {57087},
      {50175},
      {43263},
      {36351},
      {29439},
      {22527},
      {15615},
      {8703},
      {1791}
    ],
    [
      {15872},
      {18176},
      {20480},
      {22784},
      {25088},
      {27392},
      {29696},
      {32000},
      {34304},
      {36608},
      {38912},
      {41216},
      {43520},
      {45824},
      {48128},
      {50432},
      {52736},
      {55040},
      {57344},
      {59648},
      {61952},
      {64256},
      {62463},
      {55551},
      {48639},
      {41727},
      {34815},
      {27903},
      {20991},
      {14079},
      {7167},
      {255}
    ]
  ]
  test "sixteen bit grayscale" do
    {:ok, image} = Imagineer.load(@test_path <> "basn0g16.png")

    assert image.height == 32
    assert image.width == 32
    assert image.color_format == :grayscale
    assert image.compression == :zlib
    assert image.color_type == 0
    assert image.interlace_method == 0
    assert image.gamma == 1.0
    assert image.bit_depth == 16
    assert image.mask == nil
    assert image.format == :png
    assert image.mime_type == "image/png"
    assert image.palette == []

    assert_pixels_match(image.pixels, @actual_pixels)

    :ok = Imagineer.write(image, @tmp_path <> "basn0g16_test.png")
    {:ok, image} = Imagineer.load(@tmp_path <> "basn0g16_test.png")

    assert image.height == 32
    assert image.width == 32

    assert image.color_format == :grayscale
    assert image.compression == :zlib
    assert image.color_type == 0
    assert image.interlace_method == 0
    assert image.gamma == 1.0
    assert image.bit_depth == 16
    assert image.mask == nil
    assert image.format == :png
    assert image.mime_type == "image/png"
    assert image.palette == []

    assert_pixels_match(image.pixels, @actual_pixels)
  end

  defp assert_pixels_match(parsed_pixels, actual_pixels) do
    Enum.zip(parsed_pixels, actual_pixels)
    |> Enum.with_index()
    |> Enum.each(fn
      {{parsed, actual}, index} ->
        assert(actual == parsed, """
          row #{index + 1} is fucked
          \texpected: #{inspect(actual)}
          \tgot: #{inspect(parsed)}
        """)
    end)
  end

  defp extract_single_channel_pixels(image) do
    Enum.map(image.pixels, fn row ->
      Enum.map(row, &elem(&1, 0))
    end)
  end
end
