// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heather.cpp
// Function: read_pixmap_file()
int read_pixmap_file()
{
FILE *file;
int state;
char buf[MAX_PATH+1];
int i;
int j;
int cols,rows;
int num_colors;
int ndx;
int cpc;
u32 val;
int x,y;
int r,g,b;
char name[4+1];

struct COLOR {
   char name[4+1];
   int r,g,b;
} *colors;

   // read a .XPM pixmap icon file and create an X11 pixmap
   if(display == 0) return 1;

   strcpy(buf, heather_path);
   if(luxor) strcat(buf, "luxor.xpm");
   else      strcat(buf, "heather.xpm");
   file = topen(buf, "r");
   if(file == 0) {
printf("Could not open icon pixmap file:%s\n", buf);
      return 1;
   }
   state = 0;
   ndx = 0;
   x = y = 0;

   while(fgets(buf, sizeof(buf), file) != NULL) {
      for(i=0; i<(int)strlen(buf); i++) {  // skip leading whitespace
         if((buf[i] == ' ') || (buf[i] == '\t')) continue;
         break;
      }
      if(buf[i] != '"') continue;  // not a line what we are looking for
      if(buf[i] == 0) continue;    // blank line
      if(buf[i] == 0x0D) continue; // blank line
      if(buf[i] == 0x0A) continue; // blank line

//printf("line %d:%s\n", i, &buf[i]);

      if(state == 0) {  // get pixmap size info
         sscanf(&buf[i+1], "%d %d %d %d", &cols,&rows, &num_colors, &cpc);
printf("icon pixmap  cols:%d  rows:%d  colors:%d  cpc:%d\n", cols,rows, num_colors, cpc);
         if((num_colors <= 0) || (cols <= 0) || (rows <= 0) || (cpc <= 0) || (cpc >= 4)) {
printf("invalid color count:%d\n", num_colors);
            fclose(file);
            return 1;
         }

         colors = (struct COLOR *) (void *) calloc(num_colors, sizeof(struct COLOR));
         if(colors == 0) {
printf("could not allocate color table\n");
            fclose(file);
            return 8;
         }
         ++state;
      }
      else if(state == 1) { // get color definitions
          if(ndx >= (num_colors-1)) {  // end of color definitions
             state = 2;
             icon_map = XCreatePixmap(display,screen, cols,rows, wattr.depth); // winz
             if(icon_map == 0) {
                if(colors) free(colors);
                fclose(file);
                return 4;
             }
          }

          colors[ndx].name[0] = 0;
          for(j=0; j<cpc; j++) {  // get color name string
             ++i;
             colors[ndx].name[j] = buf[i];
             colors[ndx].name[j+1] = 0;
             if(j > 4) {
printf("color code too long\n");
                if(colors) free(colors);
                if(icon_map) XFreePixmap(display, icon_map);
                icon_map = 0;
                fclose(file);
                return 2;
             }
          }
          if(colors[ndx].name[0] == 0) {  // no color name
printf("invalid color name\n");
             if(colors) free(colors);
             if(icon_map) XFreePixmap(display, icon_map);
             icon_map = 0;
             fclose(file);
             return 3;
          }

          val = 0;
          while(i < (int)strlen(buf)-1) {  // find pixel value (hex only)
             ++i;
             if(buf[i] != '#') continue;

             val = atohex(&buf[i+1]);
             colors[ndx].r = (val >> 16) & 0xFF;
             colors[ndx].g = (val >> 8) & 0xFF;
             colors[ndx].b = val & 0xFF;
          }
//printf("%d:  (%s) %02X %02X %02X\n", ndx, colors[ndx].name, colors[ndx].r,colors[ndx].g,colors[ndx].b);
          ++ndx;
      }
      else if(state == 2) { // get pixmap pixels
//printf("cpc:%d  pixel row %d:%s\n", cpc, y, &buf[i+1]);
         for(x=0; x<cols; x++) {
            name[0] = 0;
            for(j=0; j<cpc; j++) {   // get pixel color code
               name[j] = buf[++i];
               name[j+1] = 0;
            }

            r = g = b = 0;
            for(j=0; j<num_colors; j++) {  // lookup pixel color RGB value
               if(!strcmp(name, colors[j].name)) {
                  r = colors[j].r;
                  g = colors[j].g;
                  b = colors[j].b;
                  goto found_color;
               }
            }
printf("Bad color code (%s) in pixmap! Using black.\n", name);

            found_color:
//printf("%d: %s(%02X,%02X,%02X) \n", x, name, r,g,b);
            XSetForeground(display,gc, RGB_NATIVE(r,g,b));
            XDrawPoint(display,icon_map, gc, x,y);

         }
//printf("\n");
         if(++y > rows) break;
      }
   }

printf("Icon pixmap created\n");
   if(colors) free(colors);
   colors = 0;

   fclose(file);
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
pushq $0x1
popq %r15
cmpq $0x0, 0x14e7c1(%rip)    # 0x15aec0
je 0xcad3
leaq 0x14e584(%rip), %rsi    # 0x15ac90
leaq -0x160(%rbp), %rbx
movq %rbx, %rdi
callq 0x9330
movb 0x21c81c(%rip), %r14b   # 0x228f3e
movq %rbx, %rdi
callq 0x9120
addq %rbp, %rax
addq $-0x160, %rax           # imm = 0xFEA0
testb %r14b, %r14b
je 0xc74d
movabsq $0x70782e726f78756c, %rcx # imm = 0x70782E726F78756C
movq %rcx, (%rax)
movw $0x6d, 0x8(%rax)
jmp 0xc761
movabsq $0x2e72656874616568, %rcx # imm = 0x2E72656874616568
movq %rcx, (%rax)
movl $0x6d7078, 0x8(%rax)    # imm = 0x6D7078
leaq 0xfe56d(%rip), %rsi     # 0x10acd5
leaq -0x160(%rbp), %r14
movq %r14, %rdi
callq 0x9a22
testq %rax, %rax
je 0xcabe
movq %rax, %rbx
movl $0x0, -0x3c(%rbp)
xorl %r15d, %r15d
movq $0x0, -0x58(%rbp)
movq %r14, %rdi
movl $0x102, %esi            # imm = 0x102
movq %rbx, %rdx
callq 0x9600
testq %rax, %rax
je 0xca9c
movq %r14, %rdi
callq 0x9120
movq %rax, %r12
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
xorl %r13d, %r13d
cmpq %r13, %r12
je 0xc7e5
movzbl -0x160(%rbp,%r13), %eax
cmpl $0x9, %eax
je 0xc7e0
cmpl $0x20, %eax
jne 0xc7ea
incq %r13
jmp 0xc7c8
movq %r12, %r13
jmp 0xc7ed
movl %r13d, %r12d
cmpb $0x22, -0x160(%rbp,%r13)
jne 0xc795
testl %r15d, %r15d
jne 0xc895
movl %r12d, %eax
leaq (%rax,%r14), %rdi
incq %rdi
leaq 0xe7352(%rip), %rsi     # 0xf3b64
leaq -0x38(%rbp), %rdx
leaq -0x34(%rbp), %rcx
leaq -0x30(%rbp), %r8
leaq -0x2c(%rbp), %r9
xorl %eax, %eax
callq 0x9350
movl -0x38(%rbp), %esi
movl -0x34(%rbp), %edx
movl -0x30(%rbp), %ecx
movl -0x2c(%rbp), %r8d
leaq 0xe7333(%rip), %rdi     # 0xf3b70
xorl %eax, %eax
callq 0x9030
movslq -0x30(%rbp), %rsi
testq %rsi, %rsi
jle 0xcae8
cmpl $0x0, -0x38(%rbp)
jle 0xcae8
cmpl $0x0, -0x34(%rbp)
jle 0xcae8
movl -0x2c(%rbp), %eax
addl $-0x4, %eax
cmpl $-0x4, %eax
jbe 0xcae8
movq %rsi, %rdi
pushq $0x14
popq %rsi
callq 0x9220
pushq $0x1
popq %r15
movq %rax, -0x50(%rbp)
testq %rax, %rax
jne 0xc795
jmp 0xcb02
cmpl $0x1, %r15d
jne 0xc926
movl -0x30(%rbp), %eax
decl %eax
pushq $0x1
popq %r15
cmpl %eax, -0x3c(%rbp)
jl 0xc8e1
movq 0x14e60c(%rip), %rdi    # 0x15aec0
movq 0x14e60d(%rip), %rsi    # 0x15aec8
movl -0x38(%rbp), %edx
movl -0x34(%rbp), %ecx
movl 0x31d6bc(%rip), %r8d    # 0x329f84
callq 0x90b0
movq %rax, 0x14e604(%rip)    # 0x15aed8
pushq $0x2
popq %r15
testq %rax, %rax
je 0xcb62
movl -0x3c(%rbp), %eax
imulq $0x14, %rax, %rax
movq -0x50(%rbp), %rdx
leaq (%rdx,%rax), %r12
movb $0x0, (%rdx,%rax)
xorl %eax, %eax
movslq -0x2c(%rbp), %rcx
cmpq %rcx, %rax
jge 0xca35
movb -0x15f(%rbp,%r13), %cl
incq %r13
movb %cl, (%r12,%rax)
movb $0x0, 0x1(%r12,%rax)
incq %rax
cmpq $0x6, %rax
jne 0xc8f6
jmp 0xcb1a
xorl %r15d, %r15d
cmpl -0x38(%rbp), %r15d
jge 0xca1b
movb $0x0, -0x41(%rbp)
movl -0x2c(%rbp), %eax
movslq %r12d, %rcx
testl %eax, %eax
movl $0x0, %edx
cmovlel %edx, %eax
leaq -0x15f(%rbp), %rdx
addq %rdx, %rcx
xorl %edx, %edx
movq -0x50(%rbp), %rdi
cmpq %rdx, %rax
je 0xc96f
movb (%rcx,%rdx), %sil
movb %sil, -0x41(%rbp,%rdx)
movb $0x0, -0x40(%rbp,%rdx)
incq %rdx
jmp 0xc957
movl %r15d, -0x5c(%rbp)
movl %r12d, %r12d
addq %rdx, %r12
movl -0x30(%rbp), %r15d
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
incq %r15
movq %rdi, %rsi
decq %r15
je 0xc9b6
leaq 0x14(%rsi), %r13
leaq -0x41(%rbp), %rdi
callq 0x9520
movq %r13, %rsi
testl %eax, %eax
jne 0xc98f
movl -0xc(%r13), %edi
movl -0x8(%r13), %esi
movl -0x4(%r13), %edx
jmp 0xc9ce
leaq 0xe71fd(%rip), %rdi     # 0xf3bba
leaq -0x41(%rbp), %rsi
xorl %eax, %eax
callq 0x9030
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
movq 0x14e4eb(%rip), %r13    # 0x15aec0
movq 0x14e504(%rip), %r15    # 0x15aee0
callq 0xcbb8
movl %eax, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x90d0
movq 0x14e4cb(%rip), %rdi    # 0x15aec0
movq 0x14e4dc(%rip), %rsi    # 0x15aed8
movq 0x14e4dd(%rip), %rdx    # 0x15aee0
movl -0x5c(%rbp), %r15d
movl %r15d, %ecx
movq -0x58(%rbp), %r8
callq 0x9290
incl %r15d
jmp 0xc929
movq -0x58(%rbp), %rcx
leal 0x1(%rcx), %eax
pushq $0x2
popq %r15
cmpl -0x34(%rbp), %ecx
movq %rax, -0x58(%rbp)
jl 0xc795
jmp 0xca9c
cmpb $0x0, (%r12)
jne 0xca83
jmp 0xcb77
cmpb $0x23, -0x15f(%rbp,%r13)
jne 0xca80
leaq -0x15e(%rbp), %rax
leaq (%rax,%r13), %rdi
callq 0x98b2
movl %eax, %ecx
shrl $0x10, %ecx
movl $0xff, %edx
andl %edx, %ecx
movl %ecx, 0x8(%r12)
movl %eax, %ecx
shrl $0x8, %ecx
andl %edx, %ecx
movl %ecx, 0xc(%r12)
andl %edx, %eax
movl %eax, 0x10(%r12)
incq %r13
movq %r14, %rdi
callq 0x9120
decl %eax
cltq
cmpq %rax, %r13
jl 0xca41
incl -0x3c(%rbp)
jmp 0xc795
leaq 0xe8d73(%rip), %rdi     # 0xf5816
callq 0x95d0
movq -0x50(%rbp), %rdi
callq 0x9440
movq %rbx, %rdi
callq 0x9360
xorl %r15d, %r15d
jmp 0xcad3
leaq 0xe707b(%rip), %rdi     # 0xf3b40
leaq -0x160(%rbp), %rsi
xorl %eax, %eax
callq 0x9030
movl %r15d, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe70b3(%rip), %rdi     # 0xf3ba2
xorl %eax, %eax
callq 0x9030
movq %rbx, %rdi
callq 0x9360
pushq $0x1
jmp 0xcb5b
leaq 0xe8d48(%rip), %rdi     # 0xf5851
callq 0x95d0
movq %rbx, %rdi
callq 0x9360
pushq $0x8
jmp 0xcb5b
movq %rdx, %r14
leaq 0xe8d19(%rip), %rdi     # 0xf583d
callq 0x95d0
movq %r14, %rdi
callq 0x9440
movq 0x14e3a0(%rip), %rsi    # 0x15aed8
testq %rsi, %rsi
je 0xcb49
movq 0x14e37c(%rip), %rdi    # 0x15aec0
callq 0x96e0
andq $0x0, 0x14e387(%rip)    # 0x15aed8
movq %rbx, %rdi
callq 0x9360
pushq $0x2
popq %r15
jmp 0xcad3
movq -0x50(%rbp), %rdi
callq 0x9440
movq %rbx, %rdi
callq 0x9360
pushq $0x4
jmp 0xcb5b
leaq 0xe8cac(%rip), %rdi     # 0xf582a
callq 0x95d0
movq -0x50(%rbp), %rdi
callq 0x9440
movq 0x14e345(%rip), %rsi    # 0x15aed8
testq %rsi, %rsi
je 0xcba4
movq 0x14e321(%rip), %rdi    # 0x15aec0
callq 0x96e0
andq $0x0, 0x14e32c(%rip)    # 0x15aed8
movq %rbx, %rdi
callq 0x9360
pushq $0x3
jmp 0xcb5b