// File: /workspace/github_cpp_makefile_star2_O3/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, 0x164503(%rip)    # 0x170ec0
je 0xcdea
leaq 0x1642c6(%rip), %rsi    # 0x170c90
leaq -0x180(%rbp), %rbx
movq %rbx, %rdi
callq 0x9340
movb 0x23255e(%rip), %r14b   # 0x23ef3e
movq %rbx, %rdi
callq 0x9120
addq %rbp, %rax
addq $-0x180, %rax           # imm = 0xFE80
testb %r14b, %r14b
je 0xca0b
movabsq $0x70782e726f78756c, %rcx # imm = 0x70782E726F78756C
movq %rcx, (%rax)
movw $0x6d, 0x8(%rax)
jmp 0xca1f
movabsq $0x2e72656874616568, %rcx # imm = 0x2E72656874616568
movq %rcx, (%rax)
movl $0x6d7078, 0x8(%rax)    # imm = 0x6D7078
leaq 0x114367(%rip), %rsi    # 0x120d8d
leaq -0x180(%rbp), %r15
movq %r15, %rdi
callq 0x9a47
testq %rax, %rax
je 0xcdd5
movq %rax, %rbx
leaq -0x17f(%rbp), %r14
movl $0x1, %eax
movq %rax, -0x68(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x60(%rbp)
movq %r15, %rdi
movl $0x102, %esi            # imm = 0x102
movq %rbx, %rdx
callq 0x9600
testq %rax, %rax
je 0xcdae
movq %r15, %rdi
callq 0x9120
movq %rax, %r12
testl %r12d, %r12d
jle 0xcab6
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movzbl -0x180(%rbp,%rcx), %edx
cmpl $0x20, %edx
je 0xcaac
cmpl $0x9, %edx
jne 0xcabb
incq %rcx
cmpq %rcx, %rax
jne 0xca9a
jmp 0xcabe
xorl %r12d, %r12d
jmp 0xcabe
movl %ecx, %r12d
movl %r12d, %r13d
cmpb $0x22, -0x180(%rbp,%r13)
jne 0xca67
movl -0x2c(%rbp), %eax
testl %eax, %eax
jne 0xcb6d
leaq (%r15,%r13), %rdi
incq %rdi
leaq 0xfd02f(%rip), %rsi     # 0x109b14
leaq -0x30(%rbp), %rdx
leaq -0x3c(%rbp), %rcx
leaq -0x38(%rbp), %r8
leaq -0x34(%rbp), %r9
xorl %eax, %eax
callq 0x9360
movl -0x30(%rbp), %esi
movl -0x3c(%rbp), %edx
movl -0x38(%rbp), %ecx
movl -0x34(%rbp), %r8d
leaq 0xfd010(%rip), %rdi     # 0x109b20
xorl %eax, %eax
callq 0x9030
movslq -0x38(%rbp), %rax
testq %rax, %rax
jle 0xcdff
cmpl $0x0, -0x30(%rbp)
jle 0xcdff
cmpl $0x0, -0x3c(%rbp)
jle 0xcdff
movl -0x34(%rbp), %ecx
addl $-0x4, %ecx
cmpl $-0x4, %ecx
jbe 0xcdff
movl $0x14, %esi
movq %rax, %rdi
callq 0x9230
movl $0x1, -0x2c(%rbp)
movq %rax, -0x50(%rbp)
testq %rax, %rax
jne 0xca67
jmp 0xce1f
cmpl $0x1, %eax
jne 0xcc0e
movl -0x38(%rbp), %eax
decl %eax
movl $0x1, %edx
cmpl %eax, -0x40(%rbp)
jl 0xcbba
movq 0x164334(%rip), %rdi    # 0x170ec0
movq 0x164335(%rip), %rsi    # 0x170ec8
movl -0x30(%rbp), %edx
movl -0x3c(%rbp), %ecx
movl 0x3333e4(%rip), %r8d    # 0x33ff84
callq 0x90b0
movq %rax, 0x16432c(%rip)    # 0x170ed8
movl $0x2, %edx
testq %rax, %rax
je 0xce86
movl -0x40(%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, -0x70(%rbp)
movq -0x68(%rbp), %rax
addq %rcx, %rax
addq %r14, %r13
xorl %r14d, %r14d
movslq -0x34(%rbp), %rcx
cmpq %rcx, %r14
jge 0xcd17
movb (%r13,%r14), %cl
movb %cl, -0x1(%rax,%r14)
movb $0x0, (%rax,%r14)
incq %r14
cmpq $0x6, %r14
jne 0xcbe4
jmp 0xce3b
cmpl $0x0, -0x30(%rbp)
jle 0xccf7
xorl %r13d, %r13d
movb $0x0, -0x45(%rbp)
movslq -0x34(%rbp), %rax
testq %rax, %rax
movl %r13d, -0x2c(%rbp)
jle 0xcc4d
movslq %r12d, %rcx
addq %r14, %rcx
xorl %edx, %edx
movb (%rcx,%rdx), %sil
movb %sil, -0x45(%rbp,%rdx)
movb $0x0, -0x44(%rbp,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0xcc34
addl %edx, %r12d
movslq -0x38(%rbp), %r14
movq -0x50(%rbp), %r13
testq %r14, %r14
jle 0xcc73
leaq -0x45(%rbp), %rdi
movq %r13, %rsi
callq 0x9520
testl %eax, %eax
je 0xcc8d
addq $0x14, %r13
decq %r14
jne 0xcc5a
leaq 0xfcef0(%rip), %rdi     # 0x109b6a
leaq -0x45(%rbp), %rsi
xorl %eax, %eax
callq 0x9030
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
jmp 0xcc99
movl 0x8(%r13), %edi
movl 0xc(%r13), %esi
movl 0x10(%r13), %edx
movl -0x2c(%rbp), %r13d
movq 0x16421c(%rip), %rax    # 0x170ec0
movq %rax, -0x58(%rbp)
movq 0x164231(%rip), %r14    # 0x170ee0
callq 0xcef2
movl %eax, %edx
movq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0x90d0
movq 0x1641f7(%rip), %rdi    # 0x170ec0
movq 0x164208(%rip), %rsi    # 0x170ed8
movq 0x164209(%rip), %rdx    # 0x170ee0
movl %r13d, %ecx
movq -0x60(%rbp), %r8
callq 0x92a0
incl %r13d
cmpl -0x30(%rbp), %r13d
leaq -0x17f(%rbp), %r14
jl 0xcc1b
movq -0x60(%rbp), %rcx
leal 0x1(%rcx), %eax
movl $0x2, -0x2c(%rbp)
cmpl -0x3c(%rbp), %ecx
movq %rax, -0x60(%rbp)
jl 0xca67
jmp 0xcdae
movq -0x58(%rbp), %rax
cmpb $0x0, (%rax)
je 0xcea7
movl %edx, -0x2c(%rbp)
addl %r14d, %r12d
movq %r15, %rdi
callq 0x9120
decl %eax
cmpl %r12d, %eax
leaq -0x17e(%rbp), %r12
movl $0xff, %r13d
movq -0x70(%rbp), %rax
jle 0xcd9a
addq %r14, %rax
cmpb $0x23, -0x17f(%rbp,%rax)
movq %rax, %r14
jne 0xcd83
leaq (%r12,%r14), %rdi
callq 0x98c6
movl %eax, %ecx
shrl $0x10, %ecx
andl %r13d, %ecx
movq -0x58(%rbp), %rdx
movl %ecx, 0x8(%rdx)
movl %eax, %ecx
shrl $0x8, %ecx
andl %r13d, %ecx
movl %ecx, 0xc(%rdx)
andl %r13d, %eax
movl %eax, 0x10(%rdx)
incq %r14
movq %r15, %rdi
callq 0x9120
decl %eax
cltq
cmpq %rax, %r14
movq %r14, %rax
jl 0xcd4d
incl -0x40(%rbp)
addq $0x14, -0x68(%rbp)
leaq -0x17f(%rbp), %r14
jmp 0xca67
leaq 0xfe9e6(%rip), %rdi     # 0x10b79b
callq 0x95d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0xcdc8
callq 0x9440
movq %rbx, %rdi
callq 0x9370
xorl %r12d, %r12d
jmp 0xcdea
leaq 0xfcd14(%rip), %rdi     # 0x109af0
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 0xfcd4c(%rip), %rdi     # 0x109b52
movl %eax, %esi
xorl %eax, %eax
callq 0x9030
movq %rbx, %rdi
callq 0x9370
movl $0x1, %r12d
jmp 0xcdea
leaq 0xfe9b0(%rip), %rdi     # 0x10b7d6
callq 0x95d0
movq %rbx, %rdi
callq 0x9370
movl $0x8, %r12d
jmp 0xcdea
leaq 0xfe980(%rip), %rdi     # 0x10b7c2
callq 0x95d0
movq -0x50(%rbp), %rdi
callq 0x9440
movq 0x164081(%rip), %rsi    # 0x170ed8
testq %rsi, %rsi
je 0xce68
movq 0x16405d(%rip), %rdi    # 0x170ec0
callq 0x96e0
movq $0x0, 0x164065(%rip) # 0x170ed8
movq %rbx, %rdi
callq 0x9370
movl $0x2, %r12d
jmp 0xcdea
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0xce94
callq 0x9440
movq %rbx, %rdi
callq 0x9370
movl $0x4, %r12d
jmp 0xcdea
leaq 0xfe901(%rip), %rdi     # 0x10b7af
callq 0x95d0
movq -0x50(%rbp), %rdi
callq 0x9440
movq 0x164015(%rip), %rsi    # 0x170ed8
testq %rsi, %rsi
je 0xced4
movq 0x163ff1(%rip), %rdi    # 0x170ec0
callq 0x96e0
movq $0x0, 0x163ff9(%rip) # 0x170ed8
movq %rbx, %rdi
callq 0x9370
movl $0x3, %r12d
jmp 0xcdea