// File: lvandeve[P]lodepng/pngdetail.cpp
// Function: showColorStats(Data&, Options const&)
void showColorStats(Data& data, const Options& options) {
  data.loadInspect();
  if(!data.is_png) return;
  std::cout << (options.use_hex ? std::hex: std::dec);
  std::vector<unsigned char>& image = data.pixels;
  unsigned& w = data.w;
  unsigned& h = data.h;

  data.loadPixels();
  if(data.error) return;
  // TODO: move to show color stats function
  if(options.verbose) std::cout << "Num pixels: " << w * h << std::endl;
  size_t rc, gc, bc, ac;
  std::cout << "Num unique colors: " << countColors(image, w, h, &rc, &gc, &bc, &ac);
  std::cout << " (r: " << rc << ", g: " << gc << ", b: " << bc << ", a: " << ac << ")";
  std::cout << std::endl;
  if(w > 0 && h > 0) {
    double avg[4] = {0, 0, 0, 0};
    double min[4] = {999999, 999999, 999999, 999999};
    double max[4] = {0, 0, 0, 0};
    for(unsigned y = 0; y < h; y++) {
      for(unsigned x = 0; x < w; x++) {
        for(int c = 0; c < 4; c++) {
          double v = 256 * image[y * 8 * w + x * 8 + c * 2] + image[y * 8 * w + x * 8 + c * 2 + 1];
          avg[c] += v;
          min[c] = std::min(min[c], v);
          max[c] = std::max(max[c], v);
        }
      }
    }
    for(int c = 0; c < 4; c++) {
      avg[c] /= (w * h * 257.0);
      min[c] /= 257.0;
      max[c] /= 257.0;
    }
    if(options.verbose) std::cout << "Ranges shown as 0.0-255.0, even for 16-bit data:" << std::endl;
    std::cout << "Average color: " << avg[0] << ", " << avg[1] << ", " << avg[2] << ", " << avg[3] << std::endl;
    std::cout << "Color ranges: " << min[0] << "-" << max[0] << ", " << min[1] << "-" << max[1] << ", " << min[2] << "-" << max[2] << ", " << min[3] << "-" << max[3] << std::endl;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0x317c0
movq -0x8(%rbp), %rax
testb $0x1, 0x2dd(%rax)
jne 0x2f32e
jmp 0x2f93d
movq -0x10(%rbp), %rax
testb $0x1, 0x24(%rax)
je 0x2f348
leaq 0x2911(%rip), %rax      # 0x31c50
movq %rax, -0xd8(%rbp)
jmp 0x2f358
leaq 0x2931(%rip), %rax      # 0x31c80
movq %rax, -0xd8(%rbp)
jmp 0x2f358
movq -0xd8(%rbp), %rsi
movq 0x17c6a(%rip), %rdi     # 0x46fd0
callq 0x20b0
movq -0x8(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
addq $0x54, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
callq 0x326c0
movq -0x8(%rbp), %rax
cmpl $0x0, 0x2d8(%rax)
je 0x2f3aa
jmp 0x2f93d
movq -0x10(%rbp), %rax
testb $0x1, (%rax)
je 0x2f3ea
movq 0x17c16(%rip), %rdi     # 0x46fd0
leaq 0xbb01(%rip), %rsi      # 0x3aec2
callq 0x2250
movq %rax, %rdi
movq -0x20(%rbp), %rax
movl (%rax), %esi
movq -0x28(%rbp), %rax
imull (%rax), %esi
callq 0x2340
movq %rax, %rdi
movq 0x17bdb(%rip), %rsi     # 0x46fc0
callq 0x22a0
movq 0x17bdf(%rip), %rdi     # 0x46fd0
leaq 0xbad7(%rip), %rsi      # 0x3aecf
callq 0x2250
movq %rax, -0xe0(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %esi
movq -0x28(%rbp), %rax
movl (%rax), %edx
leaq -0x30(%rbp), %rcx
leaq -0x38(%rbp), %r8
leaq -0x40(%rbp), %r9
leaq -0x48(%rbp), %rax
movq %rax, (%rsp)
callq 0x2b360
movq -0xe0(%rbp), %rdi
movq %rax, %rsi
callq 0x2080
movq 0x17b8d(%rip), %rdi     # 0x46fd0
leaq 0xba99(%rip), %rsi      # 0x3aee3
callq 0x2250
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x2080
movq %rax, %rdi
leaq 0xb891(%rip), %rsi      # 0x3acf6
callq 0x2250
movq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x2080
movq %rax, %rdi
leaq 0xb87c(%rip), %rsi      # 0x3acfc
callq 0x2250
movq %rax, %rdi
movq -0x40(%rbp), %rsi
callq 0x2080
movq %rax, %rdi
leaq 0xba4e(%rip), %rsi      # 0x3aee9
callq 0x2250
movq %rax, %rdi
movq -0x48(%rbp), %rsi
callq 0x2080
movq %rax, %rdi
leaq 0xa67a(%rip), %rsi      # 0x39b30
callq 0x2250
movq 0x17b0e(%rip), %rdi     # 0x46fd0
movq 0x17af7(%rip), %rsi     # 0x46fc0
callq 0x22a0
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
jbe 0x2f93d
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
jbe 0x2f93d
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2150
movq 0xbd61(%rip), %rax      # 0x3b260
movq %rax, -0x90(%rbp)
movq 0xbd5b(%rip), %rax      # 0x3b268
movq %rax, -0x88(%rbp)
movq 0xbd55(%rip), %rax      # 0x3b270
movq %rax, -0x80(%rbp)
movq 0xbd52(%rip), %rax      # 0x3b278
movq %rax, -0x78(%rbp)
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2150
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x2f6de
movl $0x0, -0xb8(%rbp)
movl -0xb8(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x2f6c8
movl $0x0, -0xbc(%rbp)
cmpl $0x4, -0xbc(%rbp)
jge 0x2f6b2
movq -0x18(%rbp), %rdi
movl -0xb4(%rbp), %eax
shll $0x3, %eax
movq -0x20(%rbp), %rcx
imull (%rcx), %eax
movl -0xb8(%rbp), %ecx
shll $0x3, %ecx
addl %ecx, %eax
movl -0xbc(%rbp), %ecx
shll %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x19560
movzbl (%rax), %eax
shll $0x8, %eax
movl %eax, -0xe4(%rbp)
movq -0x18(%rbp), %rdi
movl -0xb4(%rbp), %eax
shll $0x3, %eax
movq -0x20(%rbp), %rcx
imull (%rcx), %eax
movl -0xb8(%rbp), %ecx
shll $0x3, %ecx
addl %ecx, %eax
movl -0xbc(%rbp), %ecx
shll %ecx
addl %ecx, %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x19560
movq %rax, %rcx
movl -0xe4(%rbp), %eax
movzbl (%rcx), %ecx
addl %ecx, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0xc8(%rbp), %xmm0
movslq -0xbc(%rbp), %rax
addsd -0x70(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x70(%rbp,%rax,8)
movslq -0xbc(%rbp), %rax
leaq -0x90(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
leaq -0xc8(%rbp), %rsi
callq 0x32850
movsd (%rax), %xmm0
movslq -0xbc(%rbp), %rax
movsd %xmm0, -0x90(%rbp,%rax,8)
movslq -0xbc(%rbp), %rax
leaq -0xb0(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
leaq -0xc8(%rbp), %rsi
callq 0x32890
movsd (%rax), %xmm0
movslq -0xbc(%rbp), %rax
movsd %xmm0, -0xb0(%rbp,%rax,8)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x2f57f
jmp 0x2f6b4
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x2f563
jmp 0x2f6ca
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x2f547
movl $0x0, -0xcc(%rbp)
cmpl $0x4, -0xcc(%rbp)
jge 0x2f78a
movq -0x20(%rbp), %rax
movl (%rax), %eax
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
imull %ecx, %eax
cvtsi2sd %rax, %xmm1
movsd 0xa69f(%rip), %xmm0     # 0x39db0
mulsd %xmm0, %xmm1
movslq -0xcc(%rbp), %rax
movsd -0x70(%rbp,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp,%rax,8)
movslq -0xcc(%rbp), %rax
movsd -0x90(%rbp,%rax,8), %xmm0
movsd 0xa66c(%rip), %xmm1     # 0x39db0
divsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp,%rax,8)
movslq -0xcc(%rbp), %rax
movsd -0xb0(%rbp,%rax,8), %xmm0
movsd 0xa647(%rip), %xmm1     # 0x39db0
divsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp,%rax,8)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x2f6e8
movq -0x10(%rbp), %rax
testb $0x1, (%rax)
je 0x2f7b5
movq 0x17836(%rip), %rdi     # 0x46fd0
leaq 0xb74e(%rip), %rsi      # 0x3aeef
callq 0x2250
movq %rax, %rdi
movq 0x17810(%rip), %rsi     # 0x46fc0
callq 0x22a0
movq 0x17814(%rip), %rdi     # 0x46fd0
leaq 0xb75d(%rip), %rsi      # 0x3af20
callq 0x2250
movq %rax, %rdi
movsd -0x70(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
leaq 0xb19f(%rip), %rsi      # 0x3a97e
callq 0x2250
movq %rax, %rdi
movsd -0x68(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
leaq 0xb183(%rip), %rsi      # 0x3a97e
callq 0x2250
movq %rax, %rdi
movsd -0x60(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
leaq 0xb167(%rip), %rsi      # 0x3a97e
callq 0x2250
movq %rax, %rdi
movsd -0x58(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
movq 0x1778d(%rip), %rsi     # 0x46fc0
callq 0x22a0
movq 0x17791(%rip), %rdi     # 0x46fd0
leaq 0xb6ea(%rip), %rsi      # 0x3af30
callq 0x2250
movq %rax, %rdi
movsd -0x90(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
leaq 0xb6da(%rip), %rsi      # 0x3af3f
callq 0x2250
movq %rax, %rdi
movsd -0xb0(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
leaq 0xb0fa(%rip), %rsi      # 0x3a97e
callq 0x2250
movq %rax, %rdi
movsd -0x88(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
leaq 0xb69c(%rip), %rsi      # 0x3af3f
callq 0x2250
movq %rax, %rdi
movsd -0xa8(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
leaq 0xb0bc(%rip), %rsi      # 0x3a97e
callq 0x2250
movq %rax, %rdi
movsd -0x80(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
leaq 0xb661(%rip), %rsi      # 0x3af3f
callq 0x2250
movq %rax, %rdi
movsd -0xa0(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
leaq 0xb081(%rip), %rsi      # 0x3a97e
callq 0x2250
movq %rax, %rdi
movsd -0x78(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
leaq 0xb626(%rip), %rsi      # 0x3af3f
callq 0x2250
movq %rax, %rdi
movsd -0x98(%rbp), %xmm0
callq 0x2450
movq %rax, %rdi
movq 0x17688(%rip), %rsi     # 0x46fc0
callq 0x22a0
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)