// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x307b(%rip), %rsi      # 0x8110
movq %r15, %rdi
callq 0x3320
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x523f
movl $0x10, 0x8(%rbx)
leaq 0x7210(%rip), %rax      # 0xc2c9
cmpb $0x1, (%rax)
jne 0x50d8
movl (%rbx), %esi
movl 0x4(%rbx), %edx
movl 0xc(%rbx), %r8d
leaq 0x3300(%rip), %rdi      # 0x83ce
pushq $0x1
popq %rcx
xorl %eax, %eax
callq 0x3070
leaq 0x34(%rsp), %r15
pushq $0x1
popq %r12
pushq $0x4
popq %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq 0x10(%rsp), %r14
movq %r14, %rcx
callq 0x3410
movl (%r15), %eax
movl %eax, (%rbx)
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x3410
movl (%r15), %eax
movl %eax, 0x4(%rbx)
leaq 0xb(%rsp), %r15
pushq $0x5
popq %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x3410
leaq 0x32e4(%rip), %rsi      # 0x8421
movq %r15, %rdi
movq %r13, %rdx
callq 0x32b0
testl %eax, %eax
je 0x5180
leaq 0x32d3(%rip), %rsi      # 0x8426
leaq 0xb(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0x32b0
testl %eax, %eax
je 0x5180
leaq 0x32c0(%rip), %rsi      # 0x842b
leaq 0xb(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0x32b0
testl %eax, %eax
jne 0x5272
movslq (%rbx), %rax
movslq 0x4(%rbx), %rsi
imulq %rax, %rsi
addq %rsi, %rsi
negq %rsi
pushq $0x2
popq %rdx
movq 0x10(%rsp), %rdi
callq 0x32f0
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x4724
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x598a
movq %rax, 0x20(%rsp)
leaq 0x8(%rsp), %r12
pushq $0x1
popq %r13
pushq $0x2
popq %rbp
xorl %eax, %eax
cmpl 0x4(%rbx), %eax
jge 0x5227
movl %eax, 0x1c(%rsp)
movslq %r14d, %r14
movq %r14, 0x28(%rsp)
movq 0x20(%rsp), %rax
leaq (%rax,%r14,4), %r14
xorl %r15d, %r15d
cmpl (%rbx), %r15d
jge 0x5217
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
callq 0x3410
movzwl 0x8(%rsp), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, (%r14,%r15,4)
incq %r15
jmp 0x51eb
movl 0x1c(%rsp), %eax
incl %eax
movq 0x28(%rsp), %r14
addq %r15, %r14
jmp 0x51ce
movq 0x10(%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3270
movq 0x6d7a(%rip), %rbx      # 0xbfc0
movq (%rbx), %rdi
leaq 0x2e37(%rip), %rsi      # 0x8087
leaq 0x3071(%rip), %rcx      # 0x82c8
movl $0xda, %edx
xorl %eax, %eax
callq 0x33c0
movq (%rbx), %rdi
leaq 0x3190(%rip), %rsi      # 0x83fd
movq %r15, %rdx
jmp 0x52a5
movq 0x6d47(%rip), %rbx      # 0xbfc0
movq (%rbx), %rdi
leaq 0x2e04(%rip), %rsi      # 0x8087
leaq 0x303e(%rip), %rcx      # 0x82c8
movl $0xf2, %edx
xorl %eax, %eax
callq 0x33c0
movq (%rbx), %rdi
leaq 0x3190(%rip), %rsi      # 0x8430
leaq 0xb(%rsp), %rdx
xorl %eax, %eax
callq 0x33c0
pushq $-0x1
popq %rdi
callq 0x3340