// File: /workspace/github_cpp_makefile_star2_O3/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 0x38c16(%rip), %rsi     # 0xa25ab
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x69bd7
movq %rax, %rbx
movq %r15, 0x20(%rsp)
movq %rax, %rdi
callq 0xe170
movq %rbx, %rdi
callq 0xe170
movq %rbx, %rdi
callq 0x6b3e1
movq %rbx, %rdi
callq 0xe170
movq %rbx, %rdi
callq 0xe170
movq %rbx, %rdi
callq 0xe170
movq %rbx, %rdi
callq 0xe170
movq %rbx, %rdi
callq 0x6b3e1
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
callq 0x6b3e1
movl %eax, 0x4(%rsp)
movq %rbx, %rdi
callq 0x6b42f
movl %eax, %r15d
movq %rbx, %rdi
callq 0x6b42f
movl %eax, %ebp
movq %rbx, %rdi
callq 0xe170
movl %eax, 0x1c(%rsp)
movq %rbx, %rdi
callq 0xe170
movl %eax, %r13d
movq %rbx, %rdi
callq 0xe170
movl %eax, 0x18(%rsp)
movq %rbx, %rdi
callq 0xe170
movl %eax, %r12d
movq %rbx, %rdi
callq 0x6b3e1
movl %eax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x6b3e1
movq %rbx, %rdi
callq 0x6b42f
movq %rbx, %rdi
callq 0x6b42f
movq %rbx, %rdi
callq 0x6b3e1
movq %rbx, %rdi
callq 0x6b3e1
cmpl $0x0, 0x8(%rsp)
jne 0x69bec
cmpl $0x28, 0x4(%rsp)
jne 0x69bec
shll $0x8, %r13d
orl 0x1c(%rsp), %r13d
andl $0xfffe, %r13d          # imm = 0xFFFE
jne 0x69bec
shll $0x8, %r12d
orl 0x18(%rsp), %r12d
movzwl %r12w, %eax
cmpl $0x18, %eax
jne 0x69bec
leal (%r15,%r15,2), %r13d
movl %r13d, %eax
testb $0x3, %r13b
je 0x69ad5
leal 0x3(%r13), %eax
testl %r13d, %r13d
cmovnsl %r13d, %eax
andl $-0x4, %eax
addl $0x4, %eax
movl %eax, 0x4(%rsp)
movl %eax, %r12d
imull %ebp, %r12d
movq %r12, %rdi
callq 0xe030
movq %rax, 0x8(%rsp)
movl 0x14(%rsp), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe4d0
movl $0x1, %esi
movq 0x8(%rsp), %rdi
movq %r12, %rdx
movq %rbx, %rcx
callq 0xe6c0
cmpq %r12, %rax
jne 0x69c06
movq 0x8(%rsp), %r12
movl %r15d, (%r14)
movl %ebp, 0x4(%r14)
movl $0x3, 0x8(%r14)
testb $0x3, %r13b
je 0x69b47
leal 0x3(%r13), %eax
testl %r13d, %r13d
cmovnsl %r13d, %eax
andl $-0x4, %eax
addl $0x4, %eax
movl %eax, %r13d
movl %r13d, 0xc(%r14)
imull %ebp, %r13d
movslq %r13d, %rdi
callq 0xe030
movq %rax, 0x10(%r14)
testl %ebp, %ebp
movq %r12, %rdi
jle 0x69bc3
movslq 0x4(%rsp), %rax
xorl %ecx, %ecx
movq %rdi, %rdx
testl %r15d, %r15d
jle 0x69bb5
movslq 0xc(%r14), %rsi
imulq %rcx, %rsi
addq 0x10(%r14), %rsi
movl $0x2, %r10d
xorl %r8d, %r8d
movb (%rdx,%r10), %r9b
movb %r9b, -0x2(%rsi,%r10)
movb -0x1(%rdx,%r10), %r9b
movb %r9b, -0x1(%rsi,%r10)
movb -0x2(%rdx,%r10), %r9b
movb %r9b, (%rsi,%r10)
incq %r8
movslq (%r14), %r15
addq $0x3, %r10
cmpq %r15, %r8
jl 0x69b86
movl 0x4(%r14), %ebp
incq %rcx
movslq %ebp, %rsi
addq %rax, %rdx
cmpq %rsi, %rcx
jl 0x69b6c
callq 0xe5b0
movq %rbx, %rdi
callq 0xe430
movl $0x1, %ebp
jmp 0x69c25
leaq 0x40047(%rip), %rdi     # 0xa9c25
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
jmp 0x69c25
leaq 0x40050(%rip), %rdi     # 0xa9c43
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x719d2
movq %rbx, %rdi
callq 0xe430
jmp 0x69c25
leaq 0x4004f(%rip), %rdi     # 0xa9c5c
movq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x719d2
movq 0x8(%rsp), %rdi
callq 0xe5b0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq