// File: /workspace/github_cpp_makefile_star2_O1/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 $0x158, %rsp            # imm = 0x158
movl $0x1, %r12d
cmpq $0x0, 0x164501(%rip)    # 0x170ec0
je 0xcdd8
leaq 0x1642c4(%rip), %rsi    # 0x170c90
leaq -0x180(%rbp), %rbx
movq %rbx, %rdi
callq 0x9340
movb 0x23255c(%rip), %r14b   # 0x23ef3e
movq %rbx, %rdi
callq 0x9120
addq %rbp, %rax
addq $-0x180, %rax           # imm = 0xFE80
testb %r14b, %r14b
je 0xca0d
movabsq $0x70782e726f78756c, %rcx # imm = 0x70782E726F78756C
movq %rcx, (%rax)
movw $0x6d, 0x8(%rax)
jmp 0xca21
movabsq $0x2e72656874616568, %rcx # imm = 0x2E72656874616568
movq %rcx, (%rax)
movl $0x6d7078, 0x8(%rax)    # imm = 0x6D7078
leaq 0x114285(%rip), %rsi    # 0x120cad
leaq -0x180(%rbp), %r15
movq %r15, %rdi
callq 0x9a4d
testq %rax, %rax
je 0xcdc3
movq %rax, %rbx
movl $0x1, %eax
movq %rax, -0x70(%rbp)
movl $0x0, -0x3c(%rbp)
xorl %r14d, %r14d
movq $0x0, -0x68(%rbp)
movq %r15, %rdi
movl $0x102, %esi            # imm = 0x102
movq %rbx, %rdx
callq 0x9600
testq %rax, %rax
je 0xcd9c
movq %r15, %rdi
callq 0x9120
movq %rax, %r12
testl %r12d, %r12d
jle 0xcaad
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movzbl -0x180(%rbp,%rcx), %edx
cmpl $0x20, %edx
je 0xcaa3
cmpl $0x9, %edx
jne 0xcab2
incq %rcx
cmpq %rcx, %rax
jne 0xca91
jmp 0xcab5
xorl %r12d, %r12d
jmp 0xcab5
movl %ecx, %r12d
movl %r12d, %r13d
cmpb $0x22, -0x180(%rbp,%r13)
jne 0xca5e
testl %r14d, %r14d
jne 0xcb61
leaq (%r15,%r13), %rdi
incq %rdi
leaq 0xfd02a(%rip), %rsi     # 0x109b04
leaq -0x2c(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0x34(%rbp), %r8
leaq -0x30(%rbp), %r9
xorl %eax, %eax
callq 0x9360
movl -0x2c(%rbp), %esi
movl -0x38(%rbp), %edx
movl -0x34(%rbp), %ecx
movl -0x30(%rbp), %r8d
leaq 0xfd00b(%rip), %rdi     # 0x109b10
xorl %eax, %eax
callq 0x9030
movslq -0x34(%rbp), %rax
testq %rax, %rax
jle 0xcded
cmpl $0x0, -0x2c(%rbp)
jle 0xcded
cmpl $0x0, -0x38(%rbp)
jle 0xcded
movl -0x30(%rbp), %ecx
addl $-0x4, %ecx
cmpl $-0x4, %ecx
jbe 0xcded
movl $0x14, %esi
movq %rax, %rdi
callq 0x9230
movl $0x1, %r14d
movq %rax, -0x50(%rbp)
testq %rax, %rax
jne 0xca5e
jmp 0xce0d
cmpl $0x1, %r14d
jne 0xcc0b
movl -0x34(%rbp), %eax
decl %eax
movl $0x1, %r14d
cmpl %eax, -0x3c(%rbp)
jl 0xcbb1
movq 0x16433e(%rip), %rdi    # 0x170ec0
movq 0x16433f(%rip), %rsi    # 0x170ec8
movl -0x2c(%rbp), %edx
movl -0x38(%rbp), %ecx
movl 0x3333ee(%rip), %r8d    # 0x33ff84
callq 0x90b0
movq %rax, 0x164336(%rip)    # 0x170ed8
movl $0x2, %r14d
testq %rax, %rax
je 0xce79
movl -0x3c(%rbp), %eax
leaq (%rax,%rax,4), %rax
movq -0x50(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0x58(%rbp)
movb $0x0, (%rax)
movslq %r12d, %rax
movq %rax, -0x60(%rbp)
movq -0x70(%rbp), %rax
addq %rcx, %rax
leaq -0x17f(%rbp), %rcx
addq %r13, %rcx
xorl %r12d, %r12d
movslq -0x30(%rbp), %rdx
cmpq %rdx, %r12
jge 0xcd13
movb (%rcx,%r12), %dl
movb %dl, -0x1(%rax,%r12)
movb $0x0, (%rax,%r12)
incq %r12
cmpq $0x6, %r12
jne 0xcbe2
jmp 0xce29
cmpl $0x0, -0x2c(%rbp)
jle 0xccf4
xorl %r14d, %r14d
movb $0x0, -0x41(%rbp)
movslq -0x30(%rbp), %rax
testq %rax, %rax
movl %r14d, -0x58(%rbp)
jle 0xcc51
movslq %r12d, %rcx
leaq -0x17f(%rbp), %rdx
addq %rdx, %rcx
xorl %edx, %edx
movb (%rcx,%rdx), %sil
movb %sil, -0x41(%rbp,%rdx)
movb $0x0, -0x40(%rbp,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0xcc38
addl %edx, %r12d
movslq -0x34(%rbp), %r14
movq -0x50(%rbp), %r13
testq %r14, %r14
jle 0xcc77
leaq -0x41(%rbp), %rdi
movq %r13, %rsi
callq 0x9520
testl %eax, %eax
je 0xcc91
addq $0x14, %r13
decq %r14
jne 0xcc5e
leaq 0xfcedc(%rip), %rdi     # 0x109b5a
leaq -0x41(%rbp), %rsi
xorl %eax, %eax
callq 0x9030
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
jmp 0xcc9d
movl 0x8(%r13), %edi
movl 0xc(%r13), %esi
movl 0x10(%r13), %edx
movl -0x58(%rbp), %r14d
movq 0x164218(%rip), %r13    # 0x170ec0
movq 0x164231(%rip), %rax    # 0x170ee0
movq %rax, -0x60(%rbp)
callq 0xceea
movl %eax, %edx
movq %r13, %rdi
movq -0x60(%rbp), %rsi
callq 0x90d0
movq 0x1641f3(%rip), %rdi    # 0x170ec0
movq 0x164204(%rip), %rsi    # 0x170ed8
movq 0x164205(%rip), %rdx    # 0x170ee0
movl %r14d, %ecx
movq -0x68(%rbp), %r8
callq 0x92a0
incl %r14d
cmpl -0x2c(%rbp), %r14d
jl 0xcc18
movq -0x68(%rbp), %rcx
leal 0x1(%rcx), %eax
movl $0x2, %r14d
cmpl -0x38(%rbp), %ecx
movq %rax, -0x68(%rbp)
jl 0xca5e
jmp 0xcd9c
movq -0x58(%rbp), %rax
cmpb $0x0, (%rax)
je 0xce9a
addl %r12d, %r13d
movq %r15, %rdi
callq 0x9120
decl %eax
cmpl %r13d, %eax
leaq -0x17e(%rbp), %r13
movq -0x60(%rbp), %rax
jle 0xcd8f
addq %r12, %rax
cmpb $0x23, -0x17f(%rbp,%rax)
movq %rax, %r12
jne 0xcd78
leaq (%r12,%r13), %rdi
callq 0x98c6
movl %eax, %ecx
shrl $0x10, %ecx
movl $0xff, %edx
andl %edx, %ecx
movq -0x58(%rbp), %rsi
movl %ecx, 0x8(%rsi)
movl %eax, %ecx
shrl $0x8, %ecx
andl %edx, %ecx
movl %ecx, 0xc(%rsi)
andl %edx, %eax
movl %eax, 0x10(%rsi)
incq %r12
movq %r15, %rdi
callq 0x9120
decl %eax
cltq
cmpq %rax, %r12
movq %r12, %rax
jl 0xcd40
incl -0x3c(%rbp)
addq $0x14, -0x70(%rbp)
jmp 0xca5e
leaq 0xfe9eb(%rip), %rdi     # 0x10b78e
callq 0x95d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0xcdb6
callq 0x9440
movq %rbx, %rdi
callq 0x9370
xorl %r12d, %r12d
jmp 0xcdd8
leaq 0xfcd16(%rip), %rdi     # 0x109ae0
leaq -0x180(%rbp), %rsi
xorl %eax, %eax
callq 0x9030
movl %r12d, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xfcd4e(%rip), %rdi     # 0x109b42
movl %eax, %esi
xorl %eax, %eax
callq 0x9030
movq %rbx, %rdi
callq 0x9370
movl $0x1, %r12d
jmp 0xcdd8
leaq 0xfe9b5(%rip), %rdi     # 0x10b7c9
callq 0x95d0
movq %rbx, %rdi
callq 0x9370
movl $0x8, %r12d
jmp 0xcdd8
leaq 0xfe985(%rip), %rdi     # 0x10b7b5
callq 0x95d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0xce43
callq 0x9440
movq 0x16408e(%rip), %rsi    # 0x170ed8
testq %rsi, %rsi
je 0xce5b
movq 0x16406a(%rip), %rdi    # 0x170ec0
callq 0x96e0
movq $0x0, 0x164072(%rip) # 0x170ed8
movq %rbx, %rdi
callq 0x9370
movl $0x2, %r12d
jmp 0xcdd8
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0xce87
callq 0x9440
movq %rbx, %rdi
callq 0x9370
movl $0x4, %r12d
jmp 0xcdd8
leaq 0xfe901(%rip), %rdi     # 0x10b7a2
callq 0x95d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0xceb4
callq 0x9440
movq 0x16401d(%rip), %rsi    # 0x170ed8
testq %rsi, %rsi
je 0xcecc
movq 0x163ff9(%rip), %rdi    # 0x170ec0
callq 0x96e0
movq $0x0, 0x164001(%rip) # 0x170ed8
movq %rbx, %rdi
callq 0x9370
movl $0x3, %r12d
jmp 0xcdd8