// File: jpbalarini[P]otsus/./framework/CImage.cpp
// Function: CImage::load_LUM(char*)
void CImage::load_LUM(char *filename) {
  unsigned char lumWidthArray[4];
  unsigned char lumHeightArray[4];
  size_t dummy;

  FILE *infile = fopen(filename, "rb");
  if (!infile) {
    PRINT_ERROR("CImage: error opening LUM file %s\n!", filename);
    exit(-1);
  }

  int num_channels = 1;
  this->bits = 16;

  PRINT_VERBOSE("CImage load RGB (%dx%d), %d channels, in I_%d\n",
  this->Nx, this->Ny, num_channels, this->id);

  dummy = fread(&lumWidthArray, sizeof(char), 4, infile);
  this->Nx = lumWidthArray[0] + 256*lumWidthArray[1] +
              65536*lumWidthArray[2] + 16777216*lumWidthArray[3];
  dummy += fread(&lumHeightArray, sizeof(char), 4, infile);
  dummy = dummy; // To prevent the warning
  this->Ny = lumHeightArray[0] + 256*lumHeightArray[1] +
           65536*lumHeightArray[2] + 16777216*lumHeightArray[3];

  // Identify file format
  char lumFormat[5];
  dummy = fread(lumFormat, 1, 5, infile);
  if (strcmp(lumFormat, "12LI") != 0 &&
      strcmp(lumFormat, "16LI") != 0 &&
      strcmp(lumFormat, "16LU") != 0) {
    PRINT_ERROR("Unknown LUM format (not 12LI, 16LI, 16LU): %s\n", lumFormat);
    exit(-1);
  }
  
  // Skip offset
  fseek(infile, -2 * this->Nx * this->Ny, SEEK_END);
  
  // Create data channels
  create_channels(num_channels, Nx, Ny);

  // Read data and put it in the channels
  for (int ch = 0; ch < num_channels; ch++) {    
    float *m = this->get_channel(ch);

    unsigned char luminanceArray[2];

    int ptr = 0;
    for (int y = 0; y < this->Ny; y++)
      for (int x = 0; x < this->Nx; x++) {
        dummy = fread(&luminanceArray, 1, 2, infile);
        m[ptr++] = luminanceArray[0] + 256*luminanceArray[1];
      }
  }

  fclose(infile);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x5c7f(%rip), %rsi      # 0xd152
callq 0x33c0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x752d
movq 0xbace(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x5bd5(%rip), %rsi      # 0xd0c9
movl $0xda, %edx
leaq 0x5dca(%rip), %rcx      # 0xd2ca
movb $0x0, %al
callq 0x3470
movq 0xbaaa(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x5ee3(%rip), %rsi      # 0xd3ff
movb $0x0, %al
callq 0x3470
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x33e0
movq -0x58(%rbp), %rax
movl $0x1, -0x2c(%rbp)
movl $0x10, 0x8(%rax)
leaq 0xbde3(%rip), %rax      # 0x13329
testb $0x1, (%rax)
je 0x7569
movq -0x58(%rbp), %rax
movl (%rax), %esi
movl 0x4(%rax), %edx
movl -0x2c(%rbp), %ecx
movl 0xc(%rax), %r8d
leaq 0x5e6e(%rip), %rdi      # 0xd3d0
movb $0x0, %al
callq 0x3070
movq -0x28(%rbp), %rcx
leaq -0x14(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x34c0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x20(%rbp)
movzbl -0x14(%rbp), %ecx
movzbl -0x13(%rbp), %edx
shll $0x8, %edx
addl %edx, %ecx
movzbl -0x12(%rbp), %edx
shll $0x10, %edx
addl %edx, %ecx
movzbl -0x11(%rbp), %edx
shll $0x18, %edx
addl %edx, %ecx
movl %ecx, (%rax)
movq -0x28(%rbp), %rcx
leaq -0x18(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x34c0
movq %rax, %rcx
movq -0x58(%rbp), %rax
addq -0x20(%rbp), %rcx
movq %rcx, -0x20(%rbp)
movq -0x20(%rbp), %rcx
movq %rcx, -0x20(%rbp)
movzbl -0x18(%rbp), %ecx
movzbl -0x17(%rbp), %edx
shll $0x8, %edx
addl %edx, %ecx
movzbl -0x16(%rbp), %edx
shll $0x10, %edx
addl %edx, %ecx
movzbl -0x15(%rbp), %edx
shll $0x18, %edx
addl %edx, %ecx
movl %ecx, 0x4(%rax)
leaq -0x31(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1, %esi
movl $0x5, %edx
callq 0x34c0
movq %rax, -0x20(%rbp)
leaq -0x31(%rbp), %rdi
leaq 0x5e01(%rip), %rsi      # 0xd423
callq 0x3460
cmpl $0x0, %eax
je 0x76a0
leaq -0x31(%rbp), %rdi
leaq 0x5df1(%rip), %rsi      # 0xd428
callq 0x3460
cmpl $0x0, %eax
je 0x76a0
leaq -0x31(%rbp), %rdi
leaq 0x5de1(%rip), %rsi      # 0xd42d
callq 0x3460
cmpl $0x0, %eax
je 0x76a0
movq 0xb95b(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x5a62(%rip), %rsi      # 0xd0c9
movl $0xf2, %edx
leaq 0x5c57(%rip), %rcx      # 0xd2ca
movb $0x0, %al
callq 0x3470
movq 0xb937(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq -0x31(%rbp), %rdx
leaq 0x5da3(%rip), %rsi      # 0xd432
movb $0x0, %al
callq 0x3470
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x33e0
movq -0x58(%rbp), %rcx
movq -0x28(%rbp), %rdi
imull $0xfffffffe, (%rcx), %eax # imm = 0xFFFFFFFE
imull 0x4(%rcx), %eax
movslq %eax, %rsi
movl $0x2, %edx
callq 0x3390
movq -0x58(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl (%rdi), %edx
movl 0x4(%rdi), %ecx
callq 0x6740
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x778a
movq -0x58(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x81e0
movq %rax, -0x40(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x58(%rbp), %rcx
movl -0x4c(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0x777a
movl $0x0, -0x50(%rbp)
movq -0x58(%rbp), %rcx
movl -0x50(%rbp), %eax
cmpl (%rcx), %eax
jge 0x776d
movq -0x28(%rbp), %rcx
leaq -0x42(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x34c0
movq %rax, -0x20(%rbp)
movzbl -0x42(%rbp), %eax
movzbl -0x41(%rbp), %ecx
shll $0x8, %ecx
addl %ecx, %eax
cvtsi2ss %eax, %xmm0
movq -0x40(%rbp), %rax
movl -0x48(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x48(%rbp)
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x7714
jmp 0x776f
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x7701
jmp 0x777c
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x76d7
movq -0x28(%rbp), %rdi
callq 0x32e0
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)