more binaryout
[henge/apc.git] / src / binaryout.c
index 39ece40..f8ce569 100644 (file)
@@ -25,7 +25,7 @@
 
 /* Public */
 void ir_binout_init(struct ir_class_t*);
-
 /* Memory Allocation */
 #define  struct_alloc(_T) ((struct _T*) stack_alloc(&datapages, sizeof(struct _T)))
 static
@@ -81,7 +81,7 @@ struct bin_frame_header_t {
 };
 
 struct bin_pixel_t {
-  int x, y, z, num; //the x matching pixel 
+  int x, y, z;
   uint32_t ref;
   int attach_idx;
 };
@@ -121,13 +121,22 @@ struct bin_processed_links_t
   struct bin_linklist_t* dlink_list;
   int                    dlink_len;
 };
+struct bin_pixel_ht_entry_t
+{ uint16_t key;
+  uint16_t value;
+};
+
+struct bin_pixel_ht_t
+{ struct bin_pixel_ht_t* next;
+  struct bin_pixel_ht_entry_t[] hash_entries;
+};
 
 struct bin_attachment_list_t **attachment_stack, **asp; //attachment_stack, attachment_stack_pointer
 FILE* binaryout;
 
 
 #define  NAMEHASH(name, domain) (XXH32(name, u8_strlen(name), 0XCEED ) & domain) 
-
+#define  REFHASH(ref, domain) (XXH32(&ref, sizeof(uint32_t), 0xCEED) & domain)
 static inline
 int bin_set_varcount
 ( ir_set set )
@@ -785,6 +794,23 @@ bin_process_framebox
   
   return framebox_start;
 }
+static inline
+struct bin_pixel_ht_t* bin_pixel_ht_alloc
+()
+{ struct bin_pixel_ht_t* htp;
+  if(!(htp = (struct bin_pixel_ht_t*) malloc(sizeof(bin_pixel_ht_t) + )))
+     eprintf("error mallocing pixel_ht\n");
+
+       return htp;
+}
+int bin_insert_pixel_ht_entry
+( struct bin_pixel_ht_t* ht,
+  struct bin_pixel_ht_entry_t* ht_entry
+)
+{ 
+
+}
+
 void
 bin_process_frameboxes
 ( ir_set set,
@@ -792,12 +818,26 @@ bin_process_frameboxes
   struct bin_pixel_node_t* default_pixel_list
 )
 { struct bin_ht_entry_t ht_entry;
+  struct bin_pixel_ht_t* ht;
+  struct bin_pixel_node_t* pixeliter;
   ir_setdata fiter;
+
+  /* create the default ht */
+  ht = bin_ht_alloc();
+  for(pixeliter = default_pixel_list; pixeliter != NULL; pixeliter = pixeliter->next)
+    { ht_entry.val = 1;
+      ht_entry.key = pixel_iter->data.ref;
+      bin_insert_pixel_ht_entry(&ht, ;
+    }
+  
+
+
   
   /* Insert variants into hash table to overwrite olink insertions*/
   for ( fiter = ir_set_framebox(set); fiter != NULL; fiter = ir_setdata_nextsib(fiter))
     { fseek(binaryout, 0, SEEK_END);
-      ht_entry.key = NAMEHASH(ir_setdata_name(fiter), ht->entries);  
+      ht_entry.key = NAMEHASH(ir_setdata_name(fiter), ht->entries);
+        /* create the copy, pass the copy */
       ht_entry.value = bin_process_framebox(set, fiter, default_pixel_list);
       bin_insert_var_ht_entry(ht->start, ht->entries * sizeof(ht_entry), &ht_entry, 1);     
     }
@@ -889,7 +929,7 @@ bin_process_facing
 
 /* pixel_list == ops, output up to fwidth amount of them */
 void
-bin_output_pixel_list(struct bin_pixel_node_t* map_pixel_list)
+bin_output_pixel_list(struct bin_pixel_node_t* map_pixel_list);
 
 /* TODO: Please rename all the functions jhc*/
 static inline
@@ -923,29 +963,65 @@ int bin_set_map_pixel_list_attach_idxs
        defaultiter = defaultiter->next;
     }
 }
+int bin_ref_in_pixel_list
+( struct bin_pixel_node_t* pixel_list,
+  uint32_t ref
+)
+{ struct bin_pixel_node_t* iter;
+  for(iter = pixel_list; iter != NULL; iter = iter->next)
+    { if(ref == iter.data.ref)
+       return 1;
+    }
+  return 0;
+}
+
+struct bin_pixel_ht_t*
+#define PIXEL_HT_SIZE() (sizeof(bin_pixel_ht_entry_t) * SYS_PAGESIZE + sizeof(bin_pixel_ht_t*))
+bin_pixel_ht_alloc
+( void )
+{ struct bin_pixel_ht_t* ht;
 
-/* map_pixel_list cannot have more pixels. for all of its pixels,
-   the refs must be represented in default pixel list. 0 if invalid, 1 if valid */
+  if(!(ht = (struct bin_pixel_ht_t*) malloc( PIXEL_HT_SIZE())))
+   eprintf("Memory allocation error in bin_pixel_ht_alloc\n");
+  memset(ht, 0, PIXEL_HT_SIZE());
+
+  return ht;
+}
+
+void
+bin_insert_pixel_ht_entry
+( struct bin_pixel_ht_t** ht,
+  struct bin_pixel_ht_entry_t* ht_entry
+)
+{ if(*ht == NULL)
+    *ht = bin_pixel_ht_alloc();
+    
+}
+
+/* Determines if the multiset map_pixel is a subset of the multiset default pixel list.
+   0 if invalid, 1 if valid */
 static inline
 int bin_valid_map_pixel_list
-( struct bin_pixel_node_t* default_pixel_list,
+( struct bin_pixel_ht_t* default_ht,
   struct bin_pixel_node_t* map_pixel_list
 )
-{ struct bin_pixel_node_t* mapiter, *defaultiter;
+{ struct bin_pixel_node_t* mapiter, *defaultiter, *tmpdefault, tmpmap;
+  int i;
   defaultiter = default_pixel_list;
-  /* check length of each to make sure default < max */
-  /* for each pixel node in default and map */
-  /* TODO: Implement:: basically just checkking if map_pixel_list is subset of default_pixel_list
-     which means is the intersection of default_pl and map_pl == default_pl */
-  
+  mapiter = map_pixel_list;
 
+  while(mapiter != NULL)
+    { /* hash the ref*/
+      /* compare against the default ht */
+      /* decrement the value of the found ht_entry */
+      /* if(value == 0) */
+      /*  return 0 */
 
-  if(!mapiter && defaultiter) //defaultiter is longer so error!
-    return 0;
 
+    }
   
-  
-  
+  return 1;
     
 }
 static inline 
@@ -963,7 +1039,7 @@ int bin_process_map_pixel_list
 }
 
 void
-bin_assign_pixel_idxs
+ bin_assign_pixel_idxs
 ( struct bin_pixel_node_t* pixel_list )
 { 
 }
@@ -972,111 +1048,65 @@ bin_assign_pixel_idxs
 /* number the pixels as you insert them */
 struct bin_pixel_node_t*
 bin_insert_node_into_list
-( struct bin_pixel_node_t* pixel_list_root,
+( struct bin_pixel_node_t** pixel_list_root,
   struct bin_pixel_node_t* pixel_node
 )
-{ struct bin_pixel_node_t* head_node, * prev_node;
+{ struct bin_pixel_node_t** head_node;
 
+  head_node = pixel_list_root;
 
-  if(pixel_list_root == NULL)
-    { pixel_list_root = pixel_node;
-    }
-  
-  prev_node = head_node = pixel_list_root;  
-  while(head_node != NULL)
-    { if(pixel_node->data.z > head_node->data.z)
-       { if(head_node->next)
-           { prev_node = head_node;
-             head_node = head_node->next;
-           }
-         else
-           { head_node->next = pixel_node;
-             break;
-           }
-       }
-      else if (pixel_node->data.z < head_node->data.z)
-       { pixel_node->num++;
-         prev_node->next = pixel_node;
-         pixel_node->next = head_node;
-         break;
-       }
-      else // pixel_node->data.z == head_node->data.z
-       { pixel_node->num = head_node->num + 1;
-         prev_node->next = pixel_node;
-         pixel_node->next = head_node;
-       }
-    }
+  while(*head_node != NULL && head_node->data.ref < pixel_node->data.ref)
+    head_node = &((*head_node)->next);
+
+  pixel_node->next = *head_node;
+  *head_node = pixel_node;
 
   return pixel_list_root;
 
 
 }
 
-/* Returns the non null pixels of a single map */
-/* TODO: Finish this */
-struct bin_pixel_node_t* 
+/* Returns the non null pixels of a single mapframe (unaligned or not) 
+   given any  height and width */
+struct bin_pixel_node_t*
 bin_mapframe_to_pixel_list
 ( struct bin_img_info_t* img_info,
   int init_height,
   int init_width,
   RBGA_t* data
 )
-{ int x, y, fheight, fwidth;
+{ int j, i, fheight, fwidth, fhsize, fwsize;
+  RGBA_t* p;
   struct bin_pixel_node_t* pixel_list,* pixel_node;
 
   pixel_list = NULL;
 
   /* if frame clips, process unclippign frames */
-  if( img_info->unaligned_width )
-    { if(img_info->height < img_info->fheight)
-       fheight = img_info->height;
-      else
-       fheight = img_info->fheight;
-     }
-  else
-    fheight = img_info->fheight;
-  if (img_info->unaligned_height )
-    { if(img_info->width < img_info->fwidth)
-       fwidth = img_info->width;
-      else
-       fwidth = img_info->fwidth;
-    }
-  else
-    fwidth = img_info->fwidth;
+  
+  fwsize = img_info->unaligned_width ? img_info->unaligned_width : img_info->fwidth;
+  fhsize = img_info->unaligned_height ? img_info->unaligned_height : img_info->fwidth;
+
+
+  fwidth = img_info->fwidth;
+  fheight = img_info->fheight;
     
   
   /* Process the map*/
-  for (y = 0; y < fheight ; y++)
-    { for ( x = 0; x < fwidth; x++ )
-      { if (*data)
+  for (i = init_height; i < fhsize; i++)
+    { for ( j = init_width; j < fwsize; j++ )
+       { if (p = data[i*img_info->width+j])
          { pixel_node = struct_alloc(bin_pixel_node_t);
            /* get ref from 4 bytes of data */  
-           pixel_node->data.ref =  (*data) >> 8;
+           pixel_node->data.ref =  (*p) >> 8;
            /* bitshift by ? to get Z */
-           pixel_node->data.z = (*data & 0xFF);
+           pixel_node->data.z = (*p & 0xFF);
            /* set x and y */
-           pixel_node->data.x = x + init_width;
-           pixel_node->data.y = y + init_width;
-           pixel_list = bin_insert_node_into_list(pixel_list, pixel_node);
+           pixel_node->data.x = j;
+           pixel_node->data.y = i;
+           pixel_node->data.num = 0; 
+           pixel_list = bin_insert_node_into_list(&pixel_list, pixel_node);
          }
-       data++;
       }
-      data += img_info->width - img_info->fwidth; //stride TODO: isnt this null at the last iteration?
-    }
-  //TODO: fix these two for loops why dontcha
-  for ( x = 0; x < fwidth; x++ )
-    { if (*data)
-       { pixel_node = struct_alloc(bin_pixel_node_t);
-         /* get ref from 4 bytes of data */  
-         pixel_node->data.ref =  (*data) >> 8;
-         /* bitshift by ? to get Z */
-         pixel_node->data.z = (*data & 0xFF);
-         /* set x and y */
-         pixel_node->data.x = x + init_width;
-         pixel_node->data.y = y + init_width;
-         pixel_list = bin_insert_node_into_list(pixel_list, pixel_node);
-       }
-      data++;
     }
   
   return pixel_list;
@@ -1095,7 +1125,7 @@ int bin_pixel_list_len
     }    
   return count;
 }
-
+/* TODO: what are the qualifications for the default pixel list? len and __? */
 struct bin_pixel_node_t* 
 bin_cmp_default_pixel_lists
 ( struct bin_pixel_node_t* pl1,