// File: lvandeve[P]lodepng/lodepng_unittest.cpp
// Function: testBkgdChunk()
void testBkgdChunk() {
  std::cout << "testBkgdChunk" << std::endl;
  // color param order is: generated, raw, png ( == bKGD)
  // here generated means: what color values the pixels will get, so what auto_convert will make it choose
  testBkgdChunk(255, 0, 0, 255, 0, 0, LCT_RGBA, 8, LCT_RGBA, 8, LCT_RGBA, 8, true);
  testBkgdChunk(255, 0, 0, 255, 0, 0, LCT_RGBA, 8, LCT_RGB, 8, LCT_RGB, 8, true);
  testBkgdChunk(255, 0, 0, 255, 0, 0, LCT_RGB, 8, LCT_RGB, 8, LCT_RGB, 8, true);
  testBkgdChunk(255, 255, 255, 1, 1, 1, LCT_GREY, 1, LCT_RGB, 8, LCT_RGB, 8, true);
  testBkgdChunk(255, 255, 255, 3, 3, 3, LCT_GREY, 2, LCT_RGB, 8, LCT_RGB, 8, true);
  testBkgdChunk(255, 255, 255, 15, 15, 15, LCT_GREY, 4, LCT_RGB, 8, LCT_RGB, 8, true);
  testBkgdChunk(255, 255, 255, 255, 255, 255, LCT_GREY, 8, LCT_RGB, 8, LCT_RGB, 8, true);
  testBkgdChunk(255, 255, 255, 65535, 65535, 65535, LCT_GREY, 16, LCT_RGB, 16, LCT_RGB, 8, true);
  testBkgdChunk(123, 0, 0, 123, 0, 0, LCT_GREY, 1, LCT_RGB, 8, LCT_RGB, 8, true);
  testBkgdChunk(170, 170, 170, 2, 2, 2, LCT_GREY, 1, LCT_RGB, 8, LCT_RGB, 8, true); // 170 = value 2 in 2-bit

  // without auto_convert. Note that it will still convert if different colortype is given for raw and png, it's just
  // not automatic in that case.
  testBkgdChunk(255, 0, 0, 255, 0, 0, LCT_RGBA, 8, LCT_RGBA, 8, LCT_RGBA, 8, false);
  testBkgdChunk(60000, 0, 0, 60000, 0, 0, LCT_RGBA, 8, LCT_RGBA, 8, LCT_RGBA, 16, false);
  testBkgdChunk(128, 128, 128, 128, 128, 128, LCT_GREY, 8, LCT_RGBA, 8, LCT_GREY, 8, false);
 {
    LodePNGColorMode pal;
    lodepng_color_mode_init(&pal);
    for(int i = 0; i < 200; i++) lodepng_palette_add(&pal, i, i / 2, 0, 255);
    pal.colortype = LCT_PALETTE;
    pal.bitdepth = 8;
    unsigned w = 200;
    unsigned h = 200;
    std::vector<unsigned char> img(w * h);
    for(unsigned y = 0; y < h; y++)
    for(unsigned x = 0; x < w; x++) {
      img[y * w + x] = x;
    }

    testBkgdChunk(100, 0, 0, 100, 100, 100, img, w, h, pal, pal, true, false);
    testBkgdChunk(100, 0, 0, 100, 100, 100, img, w, h, pal, pal, false, false);
    testBkgdChunk(250, 0, 0, 250, 250, 250, img, w, h, pal, pal, true, true);

    std::vector<unsigned char> fourcolor(w * h);
    for(unsigned y = 0; y < h; y++)
    for(unsigned x = 0; x < w; x++) {
      fourcolor[y * w + x] = x & 3;
    }
    // palette index 4 expected for output bKGD: auto_convert should turn the 200-sized
    // palette in one of size 5, 4 values for the fourcolor image above, and then a 5th for
    // the bkgd index. The other two 4's actually shouldn't matter, it's not defined what
    // they should be though currently lodepng sets them also to the palette index...
    testBkgdChunk(100, 0, 0, 4, 4, 4, fourcolor, w, h, pal, pal, true, false);


    std::vector<unsigned char> mini(4);
    mini[0] = 1; mini[1] = 2; mini[2] = 3; mini[3] = 4;
    // here we expect RGB color from the output image, since the image is tiny so it chooses to not add PLTE
    testBkgdChunk(100, 0, 0, 100, 50, 0, mini, 2, 2, pal, pal, true, false);

    lodepng_color_mode_cleanup(&pal);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq 0x7e146(%rip), %rdi     # 0xf7fd8
leaq 0x3c432(%rip), %rsi     # 0xb62cb
callq 0x3230
movq %rax, %rdi
movq 0x7e118(%rip), %rsi     # 0xf7fc0
callq 0x3280
movl $0xff, %ecx
xorl %r9d, %r9d
movl %ecx, %edi
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %r8d
movl $0x6, (%rsp)
movl $0x8, 0x8(%rsp)
movl $0x6, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x6, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0xff, %ecx
xorl %r9d, %r9d
movl %ecx, %edi
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %r8d
movl $0x6, (%rsp)
movl $0x8, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x2, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0xff, %ecx
xorl %r9d, %r9d
movl %ecx, %edi
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %r8d
movl $0x2, (%rsp)
movl $0x8, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x2, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0xff, %edx
movl $0x1, %r9d
xorl %eax, %eax
movl %edx, %edi
movl %edx, %esi
movl %r9d, %ecx
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x1, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x2, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0xff, %edx
movl $0x3, %r9d
xorl %eax, %eax
movl %edx, %edi
movl %edx, %esi
movl %r9d, %ecx
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x2, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x2, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0xff, %edx
movl $0xf, %r9d
xorl %eax, %eax
movl %edx, %edi
movl %edx, %esi
movl %r9d, %ecx
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x4, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x2, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0xff, %r9d
xorl %eax, %eax
movl %r9d, %edi
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x8, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x2, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0xff, %edx
movl $0xffff, %r9d           # imm = 0xFFFF
xorl %eax, %eax
movl %edx, %edi
movl %edx, %esi
movl %r9d, %ecx
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x10, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x10, 0x18(%rsp)
movl $0x2, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0x7b, %ecx
xorl %r9d, %r9d
movl %ecx, %edi
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x1, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x2, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0xaa, %edx
movl $0x2, %r9d
xorl %eax, %eax
movl %edx, %edi
movl %edx, %esi
movl %r9d, %ecx
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x1, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x2, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0xff, %ecx
xorl %r9d, %r9d
movl %ecx, %edi
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %r8d
movl $0x6, (%rsp)
movl $0x8, 0x8(%rsp)
movl $0x6, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x6, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0xea60, %ecx           # imm = 0xEA60
xorl %r9d, %r9d
movl %ecx, %edi
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %r8d
movl $0x6, (%rsp)
movl $0x8, 0x8(%rsp)
movl $0x6, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x6, 0x20(%rsp)
movl $0x10, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
movl $0x80, %r9d
xorl %eax, %eax
movl %r9d, %edi
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x8, 0x8(%rsp)
movl $0x6, 0x10(%rsp)
movl $0x8, 0x18(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x8, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
callq 0x79830
leaq -0x28(%rbp), %rdi
callq 0x56a0
movl $0x0, -0x2c(%rbp)
cmpl $0xc8, -0x2c(%rbp)
jge 0x7a389
movl -0x2c(%rbp), %eax
movb %al, -0xb3(%rbp)
movl -0x2c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movb -0xb3(%rbp), %dl
leaq -0x28(%rbp), %rdi
xorl %ecx, %ecx
movl $0xff, %r8d
movzbl %dl, %esi
movzbl %al, %edx
callq 0x58b0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x7a344
movl $0x3, -0x28(%rbp)
movl $0x8, -0x24(%rbp)
movl $0xc8, -0x30(%rbp)
movl $0xc8, -0x34(%rbp)
movl -0x30(%rbp), %eax
movl -0x34(%rbp), %ecx
imull %ecx, %eax
movq %rax, -0xc8(%rbp)
movb $0x0, -0x51(%rbp)
leaq -0x52(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x1bfa0
movq -0xc8(%rbp), %rsi
movq -0xc0(%rbp), %rcx
leaq -0x50(%rbp), %rdi
leaq -0x51(%rbp), %rdx
callq 0xaab60
jmp 0x7a3e6
leaq -0x52(%rbp), %rdi
callq 0x1bff0
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x7a490
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jae 0x7a480
movl -0x6c(%rbp), %ecx
movb %cl, %al
movb %al, -0xd1(%rbp)
movl -0x68(%rbp), %eax
movl -0x30(%rbp), %edx
imull %edx, %eax
addl %ecx, %eax
movl %eax, %esi
leaq -0x50(%rbp), %rdi
callq 0x1a5f0
movq %rax, -0xd0(%rbp)
jmp 0x7a43b
movq -0xd0(%rbp), %rax
movb -0xd1(%rbp), %cl
movb %cl, (%rax)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x7a409
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x52(%rbp), %rdi
callq 0x1bff0
jmp 0x7a895
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x7a888
jmp 0x7a482
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x7a3f6
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %edx
movq %rsp, %rax
leaq -0x28(%rbp), %rsi
movq %rsi, 0x20(%rax)
movq %rsi, 0x18(%rax)
movl %edx, 0x10(%rax)
movl %ecx, 0x8(%rax)
leaq -0x50(%rbp), %rcx
movq %rcx, (%rax)
movl $0x0, 0x30(%rax)
movl $0x1, 0x28(%rax)
xorl %edx, %edx
movl $0x64, %r9d
movl %r9d, %edi
movl %edx, %esi
movl %r9d, %ecx
movl %r9d, %r8d
callq 0x76ad0
jmp 0x7a4da
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %edx
movq %rsp, %rax
leaq -0x28(%rbp), %rsi
movq %rsi, 0x20(%rax)
movq %rsi, 0x18(%rax)
movl %edx, 0x10(%rax)
movl %ecx, 0x8(%rax)
leaq -0x50(%rbp), %rcx
movq %rcx, (%rax)
movl $0x0, 0x30(%rax)
movl $0x0, 0x28(%rax)
xorl %edx, %edx
movl $0x64, %r9d
movl %r9d, %edi
movl %edx, %esi
movl %r9d, %ecx
movl %r9d, %r8d
callq 0x76ad0
jmp 0x7a524
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %edx
movq %rsp, %rax
leaq -0x28(%rbp), %rsi
movq %rsi, 0x20(%rax)
movq %rsi, 0x18(%rax)
movl %edx, 0x10(%rax)
movl %ecx, 0x8(%rax)
leaq -0x50(%rbp), %rcx
movq %rcx, (%rax)
movl $0x1, 0x30(%rax)
movl $0x1, 0x28(%rax)
xorl %edx, %edx
movl $0xfa, %r9d
movl %r9d, %edi
movl %edx, %esi
movl %r9d, %ecx
movl %r9d, %r8d
callq 0x76ad0
jmp 0x7a56e
movl -0x30(%rbp), %eax
movl -0x34(%rbp), %ecx
imull %ecx, %eax
movq %rax, -0xe8(%rbp)
movb $0x0, -0x89(%rbp)
leaq -0x8a(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x1bfa0
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rcx
leaq -0x88(%rbp), %rdi
leaq -0x89(%rbp), %rdx
callq 0xaab60
jmp 0x7a5bb
leaq -0x8a(%rbp), %rdi
callq 0x1bff0
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x7a692
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x30(%rbp), %eax
jae 0x7a67c
movl -0x94(%rbp), %ecx
movb %cl, %al
andb $0x3, %al
movb %al, -0xf1(%rbp)
movl -0x90(%rbp), %eax
movl -0x30(%rbp), %edx
imull %edx, %eax
addl %ecx, %eax
movl %eax, %esi
leaq -0x88(%rbp), %rdi
callq 0x1a5f0
movq %rax, -0xf0(%rbp)
jmp 0x7a62e
movq -0xf0(%rbp), %rax
movb -0xf1(%rbp), %cl
movb %cl, (%rax)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x7a5ea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x8a(%rbp), %rdi
callq 0x1bff0
jmp 0x7a888
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x7a878
jmp 0x7a67e
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x7a5d1
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %edx
movq %rsp, %rax
leaq -0x28(%rbp), %rsi
movq %rsi, 0x20(%rax)
movq %rsi, 0x18(%rax)
movl %edx, 0x10(%rax)
movl %ecx, 0x8(%rax)
leaq -0x88(%rbp), %rcx
movq %rcx, (%rax)
movl $0x0, 0x30(%rax)
movl $0x1, 0x28(%rax)
movl $0x64, %edi
xorl %edx, %edx
movl $0x4, %r9d
movl %edx, %esi
movl %r9d, %ecx
movl %r9d, %r8d
callq 0x76ad0
jmp 0x7a6e1
movb $0x0, -0xb1(%rbp)
leaq -0xb2(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x1bfa0
movq -0x100(%rbp), %rcx
leaq -0xb0(%rbp), %rdi
movl $0x4, %esi
leaq -0xb1(%rbp), %rdx
callq 0xaab60
jmp 0x7a71c
leaq -0xb2(%rbp), %rdi
callq 0x1bff0
xorl %eax, %eax
movl %eax, %esi
leaq -0xb0(%rbp), %rdi
callq 0x1a5f0
movq %rax, -0x108(%rbp)
jmp 0x7a741
movq -0x108(%rbp), %rax
movb $0x1, (%rax)
leaq -0xb0(%rbp), %rdi
movl $0x1, %esi
callq 0x1a5f0
movq %rax, -0x110(%rbp)
jmp 0x7a765
movq -0x110(%rbp), %rax
movb $0x2, (%rax)
leaq -0xb0(%rbp), %rdi
movl $0x2, %esi
callq 0x1a5f0
movq %rax, -0x118(%rbp)
jmp 0x7a789
movq -0x118(%rbp), %rax
movb $0x3, (%rax)
leaq -0xb0(%rbp), %rdi
movl $0x3, %esi
callq 0x1a5f0
movq %rax, -0x120(%rbp)
jmp 0x7a7ad
movq -0x120(%rbp), %rax
movb $0x4, (%rax)
movq %rsp, %rax
leaq -0x28(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq %rcx, 0x18(%rax)
leaq -0xb0(%rbp), %rcx
movq %rcx, (%rax)
movl $0x0, 0x30(%rax)
movl $0x1, 0x28(%rax)
movl $0x2, 0x10(%rax)
movl $0x2, 0x8(%rax)
movl $0x64, %ecx
movl $0x32, %r8d
xorl %r9d, %r9d
movl %ecx, %edi
movl %r9d, %esi
movl %r9d, %edx
callq 0x76ad0
jmp 0x7a809
leaq -0x28(%rbp), %rdi
callq 0x5710
jmp 0x7a814
leaq -0xb0(%rbp), %rdi
callq 0x1a4c0
jmp 0x7a822
leaq -0x88(%rbp), %rdi
callq 0x1a4c0
jmp 0x7a830
leaq -0x50(%rbp), %rdi
callq 0x1a4c0
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xb2(%rbp), %rdi
callq 0x1bff0
jmp 0x7a878
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x1a4c0
jmp 0x7a876
jmp 0x7a878
leaq -0x88(%rbp), %rdi
callq 0x1a4c0
jmp 0x7a886
jmp 0x7a888
leaq -0x50(%rbp), %rdi
callq 0x1a4c0
jmp 0x7a893
jmp 0x7a895
movq -0x60(%rbp), %rdi
callq 0x3430
movq %rax, %rdi
callq 0x1a490
nopw %cs:(%rax,%rax)