// File: shirat74[P]resample/resample.cc
// Function: main
int
main (int argc, char *argv[])
{
  extern int   optind;
  extern char *optarg;
  int32_t      xsize = 0, ysize = 0, dpi = 0;
  std::string  filter;
  std::string  dstfile, srcfile;
  bool         keep_aspect = false;
  int          error = 0;

  // process command line options.
  {
    int  c;
    while ((c = getopt(argc, argv, "r:x:y:af:V")) != EOF) {
      switch(c) {
      case 'a': keep_aspect = true;   break;
      case 'r': dpi   = atoi(optarg); break;
      case 'x': xsize = atoi(optarg); break;
      case 'y': ysize = atoi(optarg); break;
      case 'f':
        switch(*optarg) {
        case 'b': filter = "Box"      ; break;
        case 'l': filter = "Bilinear" ; break;
        case 'B': filter = "B-Spline" ; break;
        case 'c': filter = "Bicubic"  ; break;
        case 'L': filter = "Lanczos3" ; break;
        case 'm': filter = "Mitchell" ; break;
        default: usage();
        }
        break;
      case '?': usage();
      default:  usage();
      }
    }
  }
  if((argc - optind) != 2)
    usage();
  srcfile = argv[optind];
  dstfile = argv[optind + 1];

  PNGImage src(srcfile);
  if (!src.valid()) {
    std::cerr << "Loading PNG image \"" << srcfile << "\" failed." << std::endl;
    exit(2);
  }
  if (xsize > 0 && ysize > 0) {
    if (keep_aspect)
      std::cerr << "Ignoring -a option." << std::endl;
  } else if (keep_aspect) {
    if (xsize == 0)
      xsize = ysize * src.getWidth()  / src.getHeight();
    if (ysize == 0)
      ysize = xsize * src.getHeight() / src.getWidth();
  }
  if (xsize <= 0)
    xsize = src.getWidth();
  if (ysize <= 0)
    ysize = src.getHeight();

  Resampler resampler(filter);
  Image ras = resampler.resampleImage(src, xsize, ysize);
  PNGImage dst(ras.getWidth(), ras.getHeight(),
               ras.getNComps(), ras.getBPC(), ras.getPixelBytes());
  if (dpi > 0)
    dst.setResolution(dpi, dpi);
  else { // Copy resolution setting from source image.
    dst.setResolution(src.getResolutionX(), src.getResolutionY());
  }
  // Copy colorspace related information
  // There are currently no easy way to copy it.
  {
    enum png_colorspace_type_e type = src.getColorSpaceType();
    dst.setColorSpaceType(type);
    switch (type) {
    case png_colorspace_iccp:
      dst.setICCProfileName(src.getICCProfileName());
      dst.setICCProfile(src.getICCProfile());
      break;
    case png_colorspace_srgb:
      dst.setsRGBIntent(src.getsRGBIntent());
      break;
    case png_colorspace_calibrated:
      {
        std::vector<float> c = src.getChromaticity();
        dst.setChromaticity(c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]);
        dst.setGamma(src.getGamma());
      }
      break;
    case png_colorspace_gamma_only:
      dst.setGamma(src.getGamma());
      break;
    case png_colorspace_device:
      // do nothing
      break;
    }
  }
  error = dst.save(dstfile);
  if (error) {
    std::cerr << "Could not save destination image: " << dstfile << std::endl;
    exit(2);
  }

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x390, %rsp            # imm = 0x390
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3430
leaq -0x60(%rbp), %rdi
callq 0x3430
leaq -0x80(%rbp), %rdi
callq 0x3430
movb $0x0, -0x81(%rbp)
movl $0x0, -0x88(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x33e8(%rip), %rdx      # 0xd540
callq 0x30c0
movl %eax, -0x8c(%rbp)
cmpl $-0x1, %eax
je 0xa2dc
movl -0x8c(%rbp), %eax
addl $-0x3f, %eax
movl %eax, %ecx
movq %rcx, -0x2d0(%rbp)
subl $0x3a, %eax
ja 0xa2ce
movq -0x2d0(%rbp), %rax
leaq 0x306f(%rip), %rcx      # 0xd204
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, -0x81(%rbp)
jmp 0xa2d7
movq 0x8e0f(%rip), %rax      # 0x12fc0
movq (%rax), %rdi
callq 0x3480
movl %eax, -0x1c(%rbp)
jmp 0xa2d7
movq 0x8df8(%rip), %rax      # 0x12fc0
movq (%rax), %rdi
callq 0x3480
movl %eax, -0x14(%rbp)
jmp 0xa2d7
movq 0x8de1(%rip), %rax      # 0x12fc0
movq (%rax), %rdi
callq 0x3480
movl %eax, -0x18(%rbp)
jmp 0xa2d7
movq 0x8dca(%rip), %rax      # 0x12fc0
movq (%rax), %rax
movsbl (%rax), %eax
addl $-0x42, %eax
movl %eax, %ecx
movq %rcx, -0x2d8(%rbp)
subl $0x2b, %eax
ja 0xa2ba
movq -0x2d8(%rbp), %rax
leaq 0x30d1(%rip), %rcx      # 0xd2f0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x331c(%rip), %rsi      # 0xd54b
leaq -0x40(%rbp), %rdi
callq 0x3420
jmp 0xa23a
jmp 0xa2c3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xabac
leaq 0x32f2(%rip), %rsi      # 0xd54f
leaq -0x40(%rbp), %rdi
callq 0x3420
jmp 0xa268
jmp 0xa2c3
leaq 0x32e7(%rip), %rsi      # 0xd558
leaq -0x40(%rbp), %rdi
callq 0x3420
jmp 0xa27c
jmp 0xa2c3
leaq 0x2f03(%rip), %rsi      # 0xd188
leaq -0x40(%rbp), %rdi
callq 0x3420
jmp 0xa290
jmp 0xa2c3
leaq 0x32c8(%rip), %rsi      # 0xd561
leaq -0x40(%rbp), %rdi
callq 0x3420
jmp 0xa2a4
jmp 0xa2c3
leaq 0x32bd(%rip), %rsi      # 0xd56a
leaq -0x40(%rbp), %rdi
callq 0x3420
jmp 0xa2b8
jmp 0xa2c3
callq 0xa0c0
jmp 0xa2c1
jmp 0xa2c3
jmp 0xa2d7
callq 0xa0c0
jmp 0xa2cc
jmp 0xa2ce
callq 0xa0c0
jmp 0xa2d5
jmp 0xa2d7
jmp 0xa14a
movl -0x8(%rbp), %eax
movq 0x8cea(%rip), %rcx      # 0x12fd0
subl (%rcx), %eax
cmpl $0x2, %eax
je 0xa2f6
callq 0xa0c0
jmp 0xa2f4
jmp 0xa2f6
movq -0x10(%rbp), %rax
movq 0x8ccf(%rip), %rcx      # 0x12fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rsi
leaq -0x80(%rbp), %rdi
callq 0x3420
jmp 0xa313
movq -0x10(%rbp), %rax
movq 0x8cb2(%rip), %rcx      # 0x12fd0
movslq (%rcx), %rcx
movq 0x8(%rax,%rcx,8), %rsi
leaq -0x60(%rbp), %rdi
callq 0x3420
jmp 0xa331
leaq -0x158(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x3160
jmp 0xa343
leaq -0x138(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x43c0
jmp 0xa358
leaq -0x158(%rbp), %rdi
callq 0x3210
leaq -0x138(%rbp), %rdi
callq 0xabe0
movb %al, -0x2d9(%rbp)
jmp 0xa378
movb -0x2d9(%rbp), %al
testb $0x1, %al
jne 0xa432
jmp 0xa388
movq 0x8c61(%rip), %rdi      # 0x12ff0
leaq 0x31dd(%rip), %rsi      # 0xd573
callq 0x3310
movq %rax, -0x2e8(%rbp)
jmp 0xa3a4
movq -0x2e8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x32f0
movq %rax, -0x2f0(%rbp)
jmp 0xa3bd
movq -0x2f0(%rbp), %rdi
leaq 0x31bc(%rip), %rsi      # 0xd587
callq 0x3310
movq %rax, -0x2f8(%rbp)
jmp 0xa3d9
movq -0x2f8(%rbp), %rdi
movq 0x8bd1(%rip), %rsi      # 0x12fb8
callq 0x3360
jmp 0xa3ee
movl $0x2, %edi
callq 0x33f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x3210
jmp 0xabac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xaba0
cmpl $0x0, -0x14(%rbp)
jle 0xa47f
cmpl $0x0, -0x18(%rbp)
jle 0xa47f
testb $0x1, -0x81(%rbp)
je 0xa47a
movq 0x8ba2(%rip), %rdi      # 0x12ff0
leaq 0x313c(%rip), %rsi      # 0xd591
callq 0x3310
movq %rax, -0x300(%rbp)
jmp 0xa463
movq -0x300(%rbp), %rdi
movq 0x8b47(%rip), %rsi      # 0x12fb8
callq 0x3360
jmp 0xa478
jmp 0xa47a
jmp 0xa54c
testb $0x1, -0x81(%rbp)
je 0xa54a
cmpl $0x0, -0x14(%rbp)
jne 0xa4ea
movl -0x18(%rbp), %eax
movl %eax, -0x308(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x56b0
movl %eax, -0x304(%rbp)
jmp 0xa4af
movl -0x304(%rbp), %ecx
movl -0x308(%rbp), %eax
imull %ecx, %eax
movl %eax, -0x310(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x56c0
movl %eax, -0x30c(%rbp)
jmp 0xa4d8
movl -0x30c(%rbp), %ecx
movl -0x310(%rbp), %eax
cltd
idivl %ecx
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0xa548
movl -0x14(%rbp), %eax
movl %eax, -0x318(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x56c0
movl %eax, -0x314(%rbp)
jmp 0xa50d
movl -0x314(%rbp), %ecx
movl -0x318(%rbp), %eax
imull %ecx, %eax
movl %eax, -0x320(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x56b0
movl %eax, -0x31c(%rbp)
jmp 0xa536
movl -0x31c(%rbp), %ecx
movl -0x320(%rbp), %eax
cltd
idivl %ecx
movl %eax, -0x18(%rbp)
jmp 0xa54a
jmp 0xa54c
cmpl $0x0, -0x14(%rbp)
jg 0xa56f
leaq -0x138(%rbp), %rdi
callq 0x56b0
movl %eax, -0x324(%rbp)
jmp 0xa566
movl -0x324(%rbp), %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x18(%rbp)
jg 0xa592
leaq -0x138(%rbp), %rdi
callq 0x56c0
movl %eax, -0x328(%rbp)
jmp 0xa589
movl -0x328(%rbp), %eax
movl %eax, -0x18(%rbp)
leaq -0x180(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x6b50
jmp 0xa5a4
cvtsi2ssl -0x14(%rbp), %xmm0
cvtsi2ssl -0x18(%rbp), %xmm1
leaq -0x198(%rbp), %rdi
leaq -0x180(%rbp), %rsi
leaq -0x138(%rbp), %rdx
callq 0x7730
jmp 0xa5ca
leaq -0x198(%rbp), %rdi
callq 0x56b0
movl %eax, -0x32c(%rbp)
jmp 0xa5de
leaq -0x198(%rbp), %rdi
callq 0x56c0
movl %eax, -0x330(%rbp)
jmp 0xa5f2
leaq -0x198(%rbp), %rdi
callq 0x5690
movb %al, -0x331(%rbp)
jmp 0xa606
leaq -0x198(%rbp), %rdi
callq 0x56e0
movb %al, -0x332(%rbp)
jmp 0xa61a
leaq -0x250(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x4020
jmp 0xa62f
movl -0x330(%rbp), %edx
movl -0x32c(%rbp), %esi
movb -0x332(%rbp), %al
movb -0x331(%rbp), %cl
movsbl %cl, %ecx
movsbl %al, %r8d
leaq -0x230(%rbp), %rdi
leaq -0x250(%rbp), %r9
callq 0xac00
jmp 0xa663
leaq -0x250(%rbp), %rdi
callq 0x3210
cmpl $0x0, -0x1c(%rbp)
jle 0xa6fc
cvtsi2ssl -0x1c(%rbp), %xmm1
leaq -0x230(%rbp), %rdi
movaps %xmm1, %xmm0
callq 0xac90
jmp 0xa68f
jmp 0xa748
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xab94
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xab88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x3210
jmp 0xab88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xab7c
leaq -0x138(%rbp), %rdi
callq 0xacd0
movss %xmm0, -0x338(%rbp)
jmp 0xa712
leaq -0x138(%rbp), %rdi
callq 0xacf0
movss %xmm0, -0x33c(%rbp)
jmp 0xa728
movss -0x33c(%rbp), %xmm1
movss -0x338(%rbp), %xmm0
leaq -0x230(%rbp), %rdi
callq 0xac90
jmp 0xa746
jmp 0xa748
leaq -0x138(%rbp), %rdi
callq 0xad10
movl %eax, -0x340(%rbp)
jmp 0xa75c
movl -0x340(%rbp), %eax
movl %eax, -0x254(%rbp)
movl -0x254(%rbp), %esi
leaq -0x230(%rbp), %rdi
callq 0xad30
jmp 0xa77c
movl -0x254(%rbp), %eax
movq %rax, -0x348(%rbp)
subq $0x4, %rax
ja 0xaa5c
movq -0x348(%rbp), %rax
leaq 0x2a4f(%rip), %rcx      # 0xd1f0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x278(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0xad80
jmp 0xa7bf
leaq -0x230(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0xad50
jmp 0xa7d4
leaq -0x278(%rbp), %rdi
callq 0x3210
leaq -0x290(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0xadf0
jmp 0xa7f5
leaq -0x230(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0xadc0
jmp 0xa80a
leaq -0x290(%rbp), %rdi
callq 0x5870
jmp 0xaa5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x278(%rbp), %rdi
callq 0x3210
jmp 0xab7c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x5870
jmp 0xab7c
leaq -0x138(%rbp), %rdi
callq 0xae50
movl %eax, -0x34c(%rbp)
jmp 0xa875
movl -0x34c(%rbp), %esi
leaq -0x230(%rbp), %rdi
callq 0xae30
jmp 0xa889
jmp 0xaa5c
leaq -0x2a8(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0xae70
jmp 0xa8a3
xorl %eax, %eax
movl %eax, %esi
leaq -0x2a8(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0xb0d0
movq -0x370(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x368(%rbp)
movl $0x1, %esi
callq 0xb0d0
movq -0x370(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x364(%rbp)
movl $0x2, %esi
callq 0xb0d0
movq -0x370(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x360(%rbp)
movl $0x3, %esi
callq 0xb0d0
movq -0x370(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x35c(%rbp)
movl $0x4, %esi
callq 0xb0d0
movq -0x370(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x358(%rbp)
movl $0x5, %esi
callq 0xb0d0
movq -0x370(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x354(%rbp)
movl $0x6, %esi
callq 0xb0d0
movq -0x370(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0x350(%rbp)
movl $0x7, %esi
callq 0xb0d0
movss -0x368(%rbp), %xmm0
movss -0x364(%rbp), %xmm1
movss -0x360(%rbp), %xmm2
movss -0x35c(%rbp), %xmm3
movss -0x358(%rbp), %xmm4
movss -0x354(%rbp), %xmm5
movss -0x350(%rbp), %xmm6
movss (%rax), %xmm7
leaq -0x230(%rbp), %rdi
callq 0xb040
jmp 0xa9cf
leaq -0x138(%rbp), %rdi
callq 0xb120
movss %xmm0, -0x374(%rbp)
jmp 0xa9e5
movss -0x374(%rbp), %xmm0
leaq -0x230(%rbp), %rdi
callq 0xb0f0
jmp 0xa9fb
leaq -0x2a8(%rbp), %rdi
callq 0xb140
jmp 0xaa5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0xb140
jmp 0xab7c
leaq -0x138(%rbp), %rdi
callq 0xb120
movss %xmm0, -0x378(%rbp)
jmp 0xaa42
movss -0x378(%rbp), %xmm0
leaq -0x230(%rbp), %rdi
callq 0xb0f0
jmp 0xaa58
jmp 0xaa5c
jmp 0xaa5c
leaq -0x2c8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3160
jmp 0xaa6e
leaq -0x230(%rbp), %rdi
leaq -0x2c8(%rbp), %rsi
callq 0x4cc0
movl %eax, -0x37c(%rbp)
jmp 0xaa89
movl -0x37c(%rbp), %eax
movl %eax, -0x88(%rbp)
leaq -0x2c8(%rbp), %rdi
callq 0x3210
cmpl $0x0, -0x88(%rbp)
je 0xab1e
movq 0x853f(%rip), %rdi      # 0x12ff0
leaq 0x2aed(%rip), %rsi      # 0xd5a5
callq 0x3310
movq %rax, -0x388(%rbp)
jmp 0xaac6
movq -0x388(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x32f0
movq %rax, -0x390(%rbp)
jmp 0xaadf
movq -0x390(%rbp), %rdi
movq 0x84cb(%rip), %rsi      # 0x12fb8
callq 0x3360
jmp 0xaaf4
movl $0x2, %edi
callq 0x33f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x2c8(%rbp), %rdi
callq 0x3210
jmp 0xab7c
movl $0x0, -0x4(%rbp)
leaq -0x230(%rbp), %rdi
callq 0xb1a0
leaq -0x198(%rbp), %rdi
callq 0x3df0
leaq -0x180(%rbp), %rdi
callq 0x6c40
leaq -0x138(%rbp), %rdi
callq 0xb1a0
leaq -0x80(%rbp), %rdi
callq 0x3210
leaq -0x60(%rbp), %rdi
callq 0x3210
leaq -0x40(%rbp), %rdi
callq 0x3210
movl -0x4(%rbp), %eax
addq $0x390, %rsp            # imm = 0x390
popq %rbp
retq
leaq -0x230(%rbp), %rdi
callq 0xb1a0
leaq -0x198(%rbp), %rdi
callq 0x3df0
leaq -0x180(%rbp), %rdi
callq 0x6c40
leaq -0x138(%rbp), %rdi
callq 0xb1a0
leaq -0x80(%rbp), %rdi
callq 0x3210
leaq -0x60(%rbp), %rdi
callq 0x3210
leaq -0x40(%rbp), %rdi
callq 0x3210
movq -0x98(%rbp), %rdi
callq 0x34f0
nopw %cs:(%rax,%rax)
nopl (%rax)