// File: tomfunkhouser[P]gaps/apps/pd2img/pd2img.cpp
// Function: WriteImages(gaps::R2PixelDatabase const&, char const*)
static int
WriteImages(const R2PixelDatabase& pd, const char *output_image_directory)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();
  int count = 0;
  if (print_verbose) {
    printf("Writing images to %s ...\n", output_image_directory);
    fflush(stdout);
  }
  
  // Write images
  for (int i = 0; i < pd.NKeys(); i++) {
    const char *key = pd.Key(i);

    // Get format
    int format = pd.GetFormat(key);
    if (format < 0) {
      RNFail("Unable to get format of %s\n", key);
      continue;
    }
    
    // Print debug message
    if (print_debug) printf("  %s\n", key);

    // Get pathname
    char pathname[1024];
    sprintf(pathname, "%s/%s", output_image_directory, key);

    // Create directory
    char mkdir_cmd[1040];
    sprintf(mkdir_cmd, "mkdir -p %s", pathname);
    char *endp = strrchr(mkdir_cmd, '/');
    if (endp) *endp = '\0';
    system(mkdir_cmd);

    // Write image/grid of appropriate format
    if (format == R2_PIXEL_DATABASE_3_8_PNG_FORMAT) {
      R2Image image;
      if (pd.FindImage(key, &image)) {
        image.Write(pathname);
        count++;
      }
    }
    else if (format == R2_PIXEL_DATABASE_1_16_PNG_FORMAT) {
      R2Grid grid;
      if (pd.FindGrid(key, &grid, FALSE)) {
        grid.WriteFile(pathname);
        count++;
      }
    }
    else {
      RNFail("Unrecognized format %d for %s\n", format, key);
    }
  }

  // Print message
  if (print_verbose) {
    printf("  Time = %.2f\n", start_time.Elapsed());
    printf("  # Images = %d\n", count);
    fflush(stdout);
  }

  // Return succcess
  return 1;
}

// Assembly:
subq $0x968, %rsp            # imm = 0x968
movq %rdi, 0x960(%rsp)
movq %rsi, 0x958(%rsp)
leaq 0x948(%rsp), %rdi
callq 0x3d760
leaq 0x948(%rsp), %rdi
callq 0x6c60
movl $0x0, 0x944(%rsp)
cmpl $0x0, 0xb8b55(%rip)     # 0xbf468
je 0x693a
movq 0x958(%rsp), %rsi
leaq 0x9973e(%rip), %rdi     # 0xa0062
movb $0x0, %al
callq 0x5060
movq 0xb867e(%rip), %rax     # 0xbefb0
movq (%rax), %rdi
callq 0x5460
movl $0x0, 0x940(%rsp)
movl 0x940(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x960(%rsp), %rdi
callq 0x6c80
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x6bf9
movq 0x960(%rsp), %rdi
movl 0x940(%rsp), %esi
callq 0x6c90
movq %rax, 0x938(%rsp)
movq 0x960(%rsp), %rdi
movq 0x938(%rsp), %rsi
movq (%rdi), %rax
callq *0x20(%rax)
movl %eax, 0x934(%rsp)
cmpl $0x0, 0x934(%rsp)
jge 0x69c9
movq 0x938(%rsp), %rsi
leaq 0x996bf(%rip), %rdi     # 0xa007c
movb $0x0, %al
callq 0x41f80
jmp 0x6be3
cmpl $0x0, 0xb8a9c(%rip)     # 0xbf46c
je 0x69e8
movq 0x938(%rsp), %rsi
leaq 0x996b7(%rip), %rdi     # 0xa0098
movb $0x0, %al
callq 0x5060
leaq 0x530(%rsp), %rdi
movq 0x958(%rsp), %rdx
movq 0x938(%rsp), %rcx
leaq 0x99697(%rip), %rsi     # 0xa009e
movb $0x0, %al
callq 0x50c0
leaq 0x120(%rsp), %rdi
leaq 0x530(%rsp), %rdx
leaq 0x9967f(%rip), %rsi     # 0xa00a4
movb $0x0, %al
callq 0x50c0
leaq 0x120(%rsp), %rdi
movl $0x2f, %esi
callq 0x5430
movq %rax, 0x118(%rsp)
cmpq $0x0, 0x118(%rsp)
je 0x6a5c
movq 0x118(%rsp), %rax
movb $0x0, (%rax)
leaq 0x120(%rsp), %rdi
callq 0x5320
cmpl $0x0, 0x934(%rsp)
jne 0x6b18
leaq 0x100(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x2b830
movq 0x10(%rsp), %rdx
movq 0x960(%rsp), %rdi
movq 0x938(%rsp), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movl %eax, 0x18(%rsp)
jmp 0x6aad
movl 0x18(%rsp), %eax
cmpl $0x0, %eax
je 0x6b06
leaq 0x100(%rsp), %rdi
leaq 0x530(%rsp), %rsi
callq 0x6cc0
jmp 0x6acd
movl 0x944(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x944(%rsp)
jmp 0x6b06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x2c280
jmp 0x6c4e
leaq 0x100(%rsp), %rdi
callq 0x2c280
jmp 0x6be1
cmpl $0x1, 0x934(%rsp)
jne 0x6bc2
leaq 0x20(%rsp), %rdi
movq %rdi, (%rsp)
xorl %edx, %edx
movl %edx, 0x8(%rsp)
movl %edx, %esi
callq 0x6d10
movq (%rsp), %rdx
movl 0x8(%rsp), %ecx
movq 0x960(%rsp), %rdi
movq 0x938(%rsp), %rsi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
movl %eax, 0xc(%rsp)
jmp 0x6b63
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
je 0x6bb6
leaq 0x20(%rsp), %rdi
leaq 0x530(%rsp), %rsi
callq 0x181d0
jmp 0x6b80
movl 0x944(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x944(%rsp)
jmp 0x6bb6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x81c0
jmp 0x6c4e
leaq 0x20(%rsp), %rdi
callq 0x81c0
jmp 0x6bdf
movl 0x934(%rsp), %esi
movq 0x938(%rsp), %rdx
leaq 0x994d8(%rip), %rdi     # 0xa00b0
movb $0x0, %al
callq 0x41f80
jmp 0x6be1
jmp 0x6be3
movl 0x940(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x940(%rsp)
jmp 0x6945
cmpl $0x0, 0xb8868(%rip)     # 0xbf468
je 0x6c41
leaq 0x948(%rsp), %rdi
callq 0x3d820
leaq 0x994b9(%rip), %rdi     # 0xa00cf
movb $0x1, %al
callq 0x5060
movl 0x944(%rsp), %esi
leaq 0x994b3(%rip), %rdi     # 0xa00de
movb $0x0, %al
callq 0x5060
movq 0xb8377(%rip), %rax     # 0xbefb0
movq (%rax), %rdi
callq 0x5460
movl $0x1, %eax
addq $0x968, %rsp            # imm = 0x968
retq
movq 0xf8(%rsp), %rdi
callq 0x5680
nopl (%rax,%rax)