// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::ReadBMPFile(char const*)
int R2Image::
ReadBMPFile(const char *filename)
{
  // Open file
  FILE *fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open image file: %s", filename);
    return 0;
  }

  /* Read file header */
  BMPBITMAPFILEHEADER bmfh;
  bmfh.bfType = WordReadLE(fp);
  bmfh.bfSize = DWordReadLE(fp);
  bmfh.bfReserved1 = WordReadLE(fp);
  bmfh.bfReserved2 = WordReadLE(fp);
  bmfh.bfOffBits = DWordReadLE(fp);
  
  /* Check file header */
  // assert(bmfh.bfType == BMP_BF_TYPE);
  /* ignore bmfh.bfSize */
  /* ignore bmfh.bfReserved1 */
  /* ignore bmfh.bfReserved2 */
  // assert(bmfh.bfOffBits == BMP_BF_OFF_BITS);
  
  /* Read info header */
  BMPBITMAPINFOHEADER bmih;
  bmih.biSize = DWordReadLE(fp);
  bmih.biWidth = LongReadLE(fp);
  bmih.biHeight = LongReadLE(fp);
  bmih.biPlanes = WordReadLE(fp);
  bmih.biBitCount = WordReadLE(fp);
  bmih.biCompression = DWordReadLE(fp);
  bmih.biSizeImage = DWordReadLE(fp);
  bmih.biXPelsPerMeter = LongReadLE(fp);
  bmih.biYPelsPerMeter = LongReadLE(fp);
  bmih.biClrUsed = DWordReadLE(fp);
  bmih.biClrImportant = DWordReadLE(fp);

  // Check if unsupported file type
  // If so, just fill in with default
  if ((bmih.biSize != BMP_BI_SIZE) || (bmih.biPlanes > 1) ||
      (bmih.biBitCount != 24) || (bmih.biCompression != BI_RGB)) {
    RNFail("Unsupported BMP version\n");
    fclose(fp);
    return 0;
  }

  // Check info header 
  assert(bmih.biSize == BMP_BI_SIZE);
  assert(bmih.biWidth > 0);
  assert(bmih.biHeight > 0);
  assert(bmih.biPlanes == 1);
  assert(bmih.biCompression == BI_RGB);   /* RGB */
  int lineLength = bmih.biWidth * bmih.biBitCount / 8;
  if ((lineLength % 4) != 0) lineLength = (lineLength / 4 + 1) * 4;

  // Allocate buffer
  unsigned int buffer_size = lineLength * bmih.biHeight;
  unsigned char *buffer = new unsigned char [buffer_size];
  if (!buffer) {
    RNFail("Unable to allocate memory for BMP file");
    fclose(fp);
    return 0;
  }

  // Read buffer 
  fseek(fp, (long) bmfh.bfOffBits, SEEK_SET);
  if (fread(buffer, 1, buffer_size, fp) != buffer_size) {
    RNFail("Error while reading BMP file %s", filename);
    delete [] buffer;
    return 0;
  }

  // Assign width, height, and ncomponents
  width = bmih.biWidth;
  height = bmih.biHeight;
  ncomponents = (bmih.biBitCount <= 8) ? 1 : 3;
  rowsize = ncomponents * width;
  if ((rowsize % 4) != 0) rowsize = (rowsize / 4 + 1) * 4;

  // Allocate pixels
  int nbytes = rowsize * height;
  pixels = new unsigned char [nbytes];
  if (!pixels) {
    RNFail("Unable to allocate memory for BMP file");
    delete [] buffer;
    fclose(fp);
    return 0;
  }

  // Copy pixels from buffer
  for (int j = 0; j < height; j++) {
    unsigned char *p = &pixels[j * rowsize];
    unsigned char *b = &buffer[j * lineLength];
    for (int i = 0; i < width; i++) {
      if (bmih.biBitCount == 1) {
        // Binary (this is not right, should be index into color table)
        p[i] = (b[i/8]>>(7-(i%8)) & 0x1) ? 255 : 0;
      }
      else if (bmih.biBitCount == 8) {
        // 8-bit (this is not right, should be index into color table)
        p[i] = b[i];
      }
      else if (bmih.biBitCount == 24) {
        // RGB - swap blue and red in each pixel
        p[i*3+0] = b[i*3+2];
        p[i*3+1] = b[i*3+1];
        p[i*3+2] = b[i*3+0];
      }
    }
  }

  // Delete buffer
  delete [] buffer;

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x32800(%rip), %rsi     # 0x9355b
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x60f42
movq %rax, %rbx
movq %r15, 0x20(%rsp)
movq %rax, %rdi
callq 0x62574
movq %rbx, %rdi
callq 0x62595
movq %rbx, %rdi
callq 0x62574
movq %rbx, %rdi
callq 0x62574
movq %rbx, %rdi
callq 0x62595
movl %eax, 0x1c(%rsp)
movq %rbx, %rdi
callq 0x62595
movl %eax, 0x10(%rsp)
movq %rbx, %rdi
callq 0x625e3
movl %eax, %r15d
movq %rbx, %rdi
callq 0x625e3
movl %eax, %ebp
movq %rbx, %rdi
callq 0x62574
movl %eax, %r13d
movq %rbx, %rdi
callq 0x62574
movl %eax, %r12d
movq %rbx, %rdi
callq 0x62595
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
callq 0x62595
movq %rbx, %rdi
callq 0x625e3
movq %rbx, %rdi
callq 0x625e3
movq %rbx, %rdi
callq 0x62595
movq %rbx, %rdi
callq 0x62595
cmpl $0x28, 0x10(%rsp)
jne 0x60f58
cmpw $0x1, %r13w
ja 0x60f58
cmpw $0x18, %r12w
jne 0x60f58
cmpl $0x0, 0xc(%rsp)
jne 0x60f58
leal (%r15,%r15,2), %ecx
pushq $0x4
popq %rsi
movl %ecx, %eax
testb $0x3, %cl
je 0x60e51
movl %ecx, %eax
cltd
idivl %esi
leal 0x4(,%rax,4), %eax
movl %ecx, 0x18(%rsp)
movl %eax, 0xc(%rsp)
movl %eax, %r13d
imull %ebp, %r13d
movq %r13, %rdi
callq 0xe030
movq %rax, 0x10(%rsp)
movl 0x1c(%rsp), %esi
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe4f0
pushq $0x1
popq %rsi
movq 0x10(%rsp), %rdi
movq %r13, %rdx
movq %rbx, %rcx
callq 0xe6f0
cmpq %r13, %rax
jne 0x60f73
movq 0x10(%rsp), %r12
movl %r15d, (%r14)
movl %ebp, 0x4(%r14)
movl $0x3, 0x8(%r14)
movl 0x18(%rsp), %eax
testb $0x3, %al
je 0x60ec3
cltd
pushq $0x4
popq %rcx
idivl %ecx
leal 0x4(,%rax,4), %eax
movl %eax, 0xc(%r14)
movl %ebp, %ecx
imull %eax, %ecx
movslq %ecx, %rdi
callq 0xe030
movq %rax, 0x10(%r14)
movslq 0xc(%rsp), %rax
xorl %ecx, %ecx
pushq $0x2
popq %rdx
movq %r12, %rdi
movq %r12, %rsi
movslq %ebp, %r8
cmpq %r8, %rcx
jge 0x60f92
movslq 0xc(%r14), %r11
imulq %rcx, %r11
addq 0x10(%r14), %r11
movq %rdx, %r8
xorl %r9d, %r9d
movslq %r15d, %r10
cmpq %r10, %r9
jge 0x60f36
movb (%rsi,%r8), %r10b
movb %r10b, -0x2(%r11,%r8)
movb -0x1(%rsi,%r8), %r10b
movb %r10b, -0x1(%r11,%r8)
movb -0x2(%rsi,%r8), %r10b
movb %r10b, (%r11,%r8)
incq %r9
movl (%r14), %r15d
addq $0x3, %r8
jmp 0x60f06
incq %rcx
movl 0x4(%r14), %ebp
addq %rax, %rsi
jmp 0x60ee8
leaq 0x39c5c(%rip), %rdi     # 0x9aba5
xorl %r12d, %r12d
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
jmp 0x60fa3
leaq 0x39c64(%rip), %rdi     # 0x9abc3
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x681ca
movq %rbx, %rdi
callq 0xe440
jmp 0x60fa3
leaq 0x39c62(%rip), %rdi     # 0x9abdc
movq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x681ca
movq 0x10(%rsp), %rdi
callq 0xe5d0
jmp 0x60fa3
callq 0xe5d0
movq %rbx, %rdi
callq 0xe440
pushq $0x1
popq %r12
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop