// File: 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
subq $0x180, %rsp            # imm = 0x180
cmpq $0x0, 0x1c1f2d(%rip)    # 0x1d00a0
jne 0xe181
movl $0x1, -0x4(%rbp)
jmp 0xe9b0
leaq -0x120(%rbp), %rdi
leaq 0x1c1ce1(%rip), %rsi    # 0x1cfe70
callq 0x9350
cmpb $0x0, 0x28ff83(%rip)    # 0x29e11e
je 0xe1b2
leaq -0x120(%rbp), %rdi
leaq 0x1617a0(%rip), %rsi    # 0x16f94b
callq 0x96a0
jmp 0xe1c5
leaq -0x120(%rbp), %rdi
leaq 0x161795(%rip), %rsi    # 0x16f955
callq 0x96a0
leaq -0x120(%rbp), %rdi
leaq 0x175e3a(%rip), %rsi    # 0x18400d
callq 0x9b20
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0xe204
leaq -0x120(%rbp), %rsi
leaq 0x161770(%rip), %rdi    # 0x16f961
movb $0x0, %al
callq 0x9030
movl $0x1, -0x4(%rbp)
jmp 0xe9b0
movl $0x0, -0x14(%rbp)
movl $0x0, -0x138(%rbp)
movl $0x0, -0x148(%rbp)
movl $0x0, -0x144(%rbp)
leaq -0x120(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0x102, %esi            # imm = 0x102
callq 0x95e0
cmpq $0x0, %rax
je 0xe971
movl $0x0, -0x124(%rbp)
movl -0x124(%rbp), %eax
movl %eax, -0x16c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x9120
movq %rax, %rcx
movl -0x16c(%rbp), %eax
cmpl %ecx, %eax
jge 0xe2b4
movslq -0x124(%rbp), %rax
movsbl -0x120(%rbp,%rax), %eax
cmpl $0x20, %eax
je 0xe29f
movslq -0x124(%rbp), %rax
movsbl -0x120(%rbp,%rax), %eax
cmpl $0x9, %eax
jne 0xe2a1
jmp 0xe2a3
jmp 0xe2b4
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0xe252
movslq -0x124(%rbp), %rax
movsbl -0x120(%rbp,%rax), %eax
cmpl $0x22, %eax
je 0xe2cd
jmp 0xe229
movslq -0x124(%rbp), %rax
movsbl -0x120(%rbp,%rax), %eax
cmpl $0x0, %eax
jne 0xe2e6
jmp 0xe229
movslq -0x124(%rbp), %rax
movsbl -0x120(%rbp,%rax), %eax
cmpl $0xd, %eax
jne 0xe2ff
jmp 0xe229
movslq -0x124(%rbp), %rax
movsbl -0x120(%rbp,%rax), %eax
cmpl $0xa, %eax
jne 0xe318
jmp 0xe229
cmpl $0x0, -0x14(%rbp)
jne 0xe431
movl -0x124(%rbp), %eax
addl $0x1, %eax
cltq
leaq -0x120(%rbp), %rdi
addq %rax, %rdi
leaq 0x161647(%rip), %rsi    # 0x16f985
leaq -0x12c(%rbp), %rdx
leaq -0x130(%rbp), %rcx
leaq -0x134(%rbp), %r8
leaq -0x13c(%rbp), %r9
movb $0x0, %al
callq 0x9370
movl -0x12c(%rbp), %esi
movl -0x130(%rbp), %edx
movl -0x134(%rbp), %ecx
movl -0x13c(%rbp), %r8d
leaq 0x161610(%rip), %rdi    # 0x16f991
movb $0x0, %al
callq 0x9030
cmpl $0x0, -0x134(%rbp)
jle 0xe3b5
cmpl $0x0, -0x12c(%rbp)
jle 0xe3b5
cmpl $0x0, -0x130(%rbp)
jle 0xe3b5
cmpl $0x0, -0x13c(%rbp)
jle 0xe3b5
cmpl $0x4, -0x13c(%rbp)
jl 0xe3de
movl -0x134(%rbp), %esi
leaq 0x161601(%rip), %rdi    # 0x16f9c3
movb $0x0, %al
callq 0x9030
movq -0x10(%rbp), %rdi
callq 0x9380
movl $0x1, -0x4(%rbp)
jmp 0xe9b0
movslq -0x134(%rbp), %rdi
movl $0x14, %esi
callq 0x9230
movq %rax, -0x168(%rbp)
cmpq $0x0, -0x168(%rbp)
jne 0xe423
leaq 0x1615d4(%rip), %rdi    # 0x16f9db
movb $0x0, %al
callq 0x9030
movq -0x10(%rbp), %rdi
callq 0x9380
movl $0x8, -0x4(%rbp)
jmp 0xe9b0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xe96c
cmpl $0x1, -0x14(%rbp)
jne 0xe752
movl -0x138(%rbp), %eax
movl -0x134(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jl 0xe4b9
movl $0x2, -0x14(%rbp)
movq 0x1c1c44(%rip), %rdi    # 0x1d00a0
movq 0x1c1c45(%rip), %rsi    # 0x1d00a8
movl -0x12c(%rbp), %edx
movl -0x130(%rbp), %ecx
movl 0x390cee(%rip), %r8d    # 0x39f164
callq 0x90b0
movq %rax, 0x1c1c36(%rip)    # 0x1d00b8
cmpq $0x0, 0x1c1c2e(%rip)    # 0x1d00b8
jne 0xe4b7
cmpq $0x0, -0x168(%rbp)
je 0xe4a2
movq -0x168(%rbp), %rdi
callq 0x9450
movq -0x10(%rbp), %rdi
callq 0x9380
movl $0x4, -0x4(%rbp)
jmp 0xe9b0
jmp 0xe4b9
movq -0x168(%rbp), %rax
movslq -0x138(%rbp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x0, -0x128(%rbp)
movl -0x128(%rbp), %eax
cmpl -0x13c(%rbp), %eax
jge 0xe5ce
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
movslq -0x124(%rbp), %rax
movb -0x120(%rbp,%rax), %dl
movq -0x168(%rbp), %rax
movslq -0x138(%rbp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movslq -0x128(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x168(%rbp), %rax
movslq -0x138(%rbp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movl -0x128(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
cmpl $0x4, -0x128(%rbp)
jle 0xe5b8
leaq 0x16149d(%rip), %rdi    # 0x16f9fb
movb $0x0, %al
callq 0x9030
cmpq $0x0, -0x168(%rbp)
je 0xe57b
movq -0x168(%rbp), %rdi
callq 0x9450
cmpq $0x0, 0x1c1b35(%rip)    # 0x1d00b8
je 0xe598
movq 0x1c1b14(%rip), %rdi    # 0x1d00a0
movq 0x1c1b25(%rip), %rsi    # 0x1d00b8
callq 0x96c0
movq $0x0, 0x1c1b15(%rip) # 0x1d00b8
movq -0x10(%rbp), %rdi
callq 0x9380
movl $0x2, -0x4(%rbp)
jmp 0xe9b0
jmp 0xe5ba
movl -0x128(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x128(%rbp)
jmp 0xe4db
movq -0x168(%rbp), %rax
movslq -0x138(%rbp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0xe64c
leaq 0x16141e(%rip), %rdi    # 0x16fa10
movb $0x0, %al
callq 0x9030
cmpq $0x0, -0x168(%rbp)
je 0xe60f
movq -0x168(%rbp), %rdi
callq 0x9450
cmpq $0x0, 0x1c1aa1(%rip)    # 0x1d00b8
je 0xe62c
movq 0x1c1a80(%rip), %rdi    # 0x1d00a0
movq 0x1c1a91(%rip), %rsi    # 0x1d00b8
callq 0x96c0
movq $0x0, 0x1c1a81(%rip) # 0x1d00b8
movq -0x10(%rbp), %rdi
callq 0x9380
movl $0x3, -0x4(%rbp)
jmp 0xe9b0
movl $0x0, -0x140(%rbp)
movl -0x124(%rbp), %eax
movl %eax, -0x170(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x9120
movq %rax, %rcx
movl -0x170(%rbp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xe73e
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
movslq -0x124(%rbp), %rax
movsbl -0x120(%rbp,%rax), %eax
cmpl $0x23, %eax
je 0xe6a7
jmp 0xe656
movl -0x124(%rbp), %eax
addl $0x1, %eax
cltq
leaq -0x120(%rbp), %rdi
addq %rax, %rdi
callq 0x98d0
movl %eax, -0x140(%rbp)
movl -0x140(%rbp), %ecx
shrl $0x10, %ecx
andl $0xff, %ecx
movq -0x168(%rbp), %rax
movslq -0x138(%rbp), %rdx
imulq $0x14, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x140(%rbp), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
movq -0x168(%rbp), %rax
movslq -0x138(%rbp), %rdx
imulq $0x14, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movl -0x140(%rbp), %ecx
andl $0xff, %ecx
movq -0x168(%rbp), %rax
movslq -0x138(%rbp), %rdx
imulq $0x14, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
jmp 0xe656
movl -0x138(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x138(%rbp)
jmp 0xe96a
cmpl $0x2, -0x14(%rbp)
jne 0xe968
movl $0x0, -0x144(%rbp)
movl -0x144(%rbp), %eax
cmpl -0x12c(%rbp), %eax
jge 0xe94d
movb $0x0, -0x159(%rbp)
movl $0x0, -0x128(%rbp)
movl -0x128(%rbp), %eax
cmpl -0x13c(%rbp), %eax
jge 0xe7e1
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
cltq
movb -0x120(%rbp,%rax), %cl
movslq -0x128(%rbp), %rax
movb %cl, -0x159(%rbp,%rax)
movl -0x128(%rbp), %eax
addl $0x1, %eax
cltq
movb $0x0, -0x159(%rbp,%rax)
movl -0x128(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x128(%rbp)
jmp 0xe789
movl $0x0, -0x154(%rbp)
movl $0x0, -0x150(%rbp)
movl $0x0, -0x14c(%rbp)
movl $0x0, -0x128(%rbp)
movl -0x128(%rbp), %eax
cmpl -0x134(%rbp), %eax
jge 0xe8b3
leaq -0x159(%rbp), %rdi
movq -0x168(%rbp), %rsi
movslq -0x128(%rbp), %rax
imulq $0x14, %rax, %rax
addq %rax, %rsi
callq 0x9510
cmpl $0x0, %eax
jne 0xe89d
movq -0x168(%rbp), %rax
movslq -0x128(%rbp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x14c(%rbp)
movq -0x168(%rbp), %rax
movslq -0x128(%rbp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x150(%rbp)
movq -0x168(%rbp), %rax
movslq -0x128(%rbp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0x154(%rbp)
jmp 0xe8c8
jmp 0xe89f
movl -0x128(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x128(%rbp)
jmp 0xe809
leaq -0x159(%rbp), %rsi
leaq 0x161163(%rip), %rdi    # 0x16fa24
movb $0x0, %al
callq 0x9030
movq 0x1c17d1(%rip), %rax    # 0x1d00a0
movq %rax, -0x180(%rbp)
movq 0x1c17e3(%rip), %rax    # 0x1d00c0
movq %rax, -0x178(%rbp)
movl -0x14c(%rbp), %edi
movl -0x150(%rbp), %esi
movl -0x154(%rbp), %edx
callq 0xe9c0
movq -0x180(%rbp), %rdi
movq -0x178(%rbp), %rsi
movl %eax, %eax
movl %eax, %edx
callq 0x90d0
movq 0x1c1787(%rip), %rdi    # 0x1d00a0
movq 0x1c1798(%rip), %rsi    # 0x1d00b8
movq 0x1c1799(%rip), %rdx    # 0x1d00c0
movl -0x144(%rbp), %ecx
movl -0x148(%rbp), %r8d
callq 0x92b0
movl -0x144(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x144(%rbp)
jmp 0xe766
movl -0x148(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x148(%rbp)
cmpl -0x130(%rbp), %eax
jle 0xe966
jmp 0xe971
jmp 0xe968
jmp 0xe96a
jmp 0xe96c
jmp 0xe229
leaq 0x1610d9(%rip), %rdi    # 0x16fa51
movb $0x0, %al
callq 0x9030
cmpq $0x0, -0x168(%rbp)
je 0xe995
movq -0x168(%rbp), %rdi
callq 0x9450
movq $0x0, -0x168(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9380
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopl (%rax)