#include "../stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../stb/stb_image_write.h"
+#include "../ston/ston.h"
/* Public */
void ir_binout_init(struct ir_class_t*);
-
+/* Private */
+static
+long file_ht_insert(long,int,int,int,uint32_t*,uint32_t,uint32_t);
/* Memory Allocation */
#define struct_alloc(_T) ((struct _T*) stack_alloc(&datapages, sizeof(struct _T)))
static
struct bin_ht_header_t {
long start;
int entries;
+ int size;
};
struct bin_ht_entry_t {
uint32_t key;
long value;
};
+struct bin_variant_ht_entry_t {
+ uint32_t key;
+ long vvalue;
+ long mvalue;
+};
struct bin_class_header_t {
struct bin_ht_header_t child_ht;
struct bin_ht_header_t rootset_ht;
};
struct bin_pixel_t {
- int x, y, z;
+ int x, y, z;
uint32_t ref;
int attach_idx;
};
struct bin_attachment_t** attachments;
long filepos;
};
+struct bin_linklist_t;
+struct bin_linklist_t
+{ struct bin_linklist_t* next;
+ linkdata linkdata;
+};
+
+struct bin_processed_links_t
+{ struct bin_linklist_t* vlink_list;
+ int vlink_len;
+ struct bin_linklist_t* mlink_list;
+ int mlink_len;
+ struct bin_linklist_t* olink_list; //keep track of olink cycles
+ int olink_len;
+ 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;
+};
+
+static
struct bin_attachment_list_t **attachment_stack, **asp; //attachment_stack, attachment_stack_pointer
+static
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 )
return count;
}
-/* Given a position and a size, checks if the bytes are null and returns
- the file position to where it started. 1 if not null, 0 if null*/
-/* TODO: Determine why fseeking file past end sets bytes to -1, and not 0 */
-static inline
-int bytes_null( int len, int pos )
-{ while(len--)
- { if(fgetc(binaryout) > 0)
- { fseek(binaryout, pos, SEEK_SET);
- return 1;
- }
- }
- fseek(binaryout, pos, SEEK_SET);
- return 0;
-}
/* Checks if the key at entrypos is the same as the parameter key. Returns
1 if so, 0 if not. */
static inline
}
-typedef RGBA_t uint32_t;
+typedef uint32_t RGBA_t;
long bin_traverse_class(ir_class);
/* Takes root class and begins processing */
asp = attachment_stack;
pagelist_init(datapages, (size_t) SYS_PAGESIZE);
pagelist_init(linkpages, (size_t) SYS_PAGESIZE);
+ pagelist_init(plinkpages, (size_t) SYS_PAGESIZE);
bin_traverse_class(root_class);
}
-#define ENTRY_OCCUPIED() (bytes_null(sizeof(ht_entry->key), entry_pos))
-#define WRITE_ENTRY() do { \
- if(fseek(binaryout, entry_pos, SEEK_SET) == -1) wprintf("fseek failed with %s", strerror(errno)); \
- fwrite(&ht_entry, sizeof ht_entry, 1, binaryout); \
+/* INSERT INTO HASH TABLE */
+/* Returns the key position where the hash table entry was inserted. */
+#define SEEK_TO(_FPOS) do { \
+ errno = 0; \
+ if (fseek(binaryout, _FPOS, SEEK_SET)) \
+ eprintf("Failed to seek to position %l: %s\n", _FPOS, strerror(errno)); \
} while (0)
-#define LOOP_ENTRY(_HTSTART) (entry_pos = _HTSTART)
-#define INC_ENTRY() do { \
- entry_pos += sizeof(ht_entry); \
- if(fseek(binaryout, entry_pos, SEEK_SET) == -1) eprintf("fseek failed with %s", strerror(errno)); \
+#define SEEK_REL(_FPOS) do { \
+ errno = 0; \
+ if (fseek(binaryout, _FPOS, SEEK_CUR)) \
+ eprintf("Failed to seek to position %l: %s\n", _FPOS, strerror(errno)); \
} while (0)
-#define HT_END(_HTEND) (entry_pos >= _HTEND) //just in case at last entry
-long
-/* TODO: Should overwrite be a default? */
-bin_insert_ht_entry
-( long ht_start,
- long ht_size,
- struct bin_ht_entry_t* ht_entry,
- int overwrite
+#define READ_DATA_AND_INCREMENT(_DATA,_SIZE) do { \
+ errno = 0; \
+ if (fread(_DATA, _SIZE, 1, binaryout) != 1) \
+ eprintf("Failed to read data at file position %l: %s\n", \
+ ftell(binaryout), \
+ strerror(errno)); \
+ } while (0)
+#define READ_DATA(_DATA,_SIZE) do { \
+ READ_DATA_AND_INCREMENT(_DATA,_SIZE); \
+ SEEK_REL(-_SIZE); \
+ } while (0)
+#define WRITE_DATA_AND_INCREMENT(_DATA,_SIZE) do { \
+ errno = 0; \
+ if (fwrite(_DATA, _SIZE, 1, binaryout) != 1) \
+ eprintf("Failed to write data to file at position %l: %s\n", \
+ ftell(binaryout), \
+ strerror(errno)); \
+ } while (0)
+#define WRITE_DATA(_DATA,_SIZE) do { \
+ WRITE_DATA_AND_INCREMENT(_DATA,_SIZE); \
+ SEEK_REL(-_SIZE); \
+ } while (0)
+
+/* Insert a value into an arbitrary hash table in-file */
+static
+long file_ht_insert
+( long ht_start,
+ int ht_row_values,
+ int ht_which_value,
+ int ht_rows,
+ uint32_t* overwrite,
+ uint32_t key,
+ uint32_t value
)
-{ long entry_pos, ht_end;
+{ uint32_t entry[ht_row_values];
+# define ENTRY_VAL(N) entry[N]
+# define ENTRY_KEY ENTRY_VAL(0)
+ uint8_t looped = 0;
+ size_t entry_row = key & (ht_rows - 1);
+ int i;
+ long writepos, startpos;
+ startpos = ftell(binaryout);
+ next_entry:
+ SEEK_TO(ht_start + (entry_row * sizeof(entry)));
+ READ_DATA(entry, sizeof(uint32_t) * ht_row_values);
+ for (i = 0; i < ht_row_values; i++)
+ if (entry[i] != 0)
+ goto populated;
+ write_position:
+ SEEK_REL(sizeof(uint32_t) * ht_which_value);
+ WRITE_DATA(entry + ht_which_value, sizeof(*entry));
+ writepos = ftell(binaryout);
+ SEEK_TO(startpos);
+ return writepos;
+ populated:
+ if (ENTRY_KEY == key)
+ { if (overwrite != NULL)
+ *overwrite = ENTRY_VAL(ht_which_value);
+ ENTRY_VAL(ht_which_value) = value;
+ goto write_position;
+ }
+ if (entry_row < ht_rows)
+ entry_row++;
+ else if (looped)
+ eprintf("cannot insert into filled hashtable\n");
+ else
+ { looped++;
+ entry_row = 0;
+ }
+ goto next_entry;
+}
- ht_end = ht_start + ht_size;
- entry_pos = ht_start + sizeof(ht_entry) * ht_entry->key;
- fseek(binaryout, entry_pos, SEEK_SET);
-
- if (!ENTRY_OCCUPIED())
- { uprintf("key not occupied\n");
- WRITE_ENTRY();
+static
+uint32_t* mem_ht_insert
+( void* ht_start,
+ int ht_row_values,
+ int ht_which_value,
+ int ht_rows,
+ uint32_t* overwrite,
+ uint32_t key,
+ uint32_t value
+)
+{ uint32_t* entry;
+ uint8_t looped = 0;
+ size_t entry_row = key & (ht_rows - 1);
+ int i;
+ next_entry:
+ entry = (uint32_t*)ht_start + (ht_row_values * entry_row);
+ for (i = 0; i < ht_row_values; i++)
+ if (entry[i] != 0)
+ goto populated;
+ write_position:
+ entry[ht_which_value] = value;
+ return &entry[ht_which_value];
+ populated:
+ if (ENTRY_KEY == key)
+ { if (overwrite != NULL)
+ *overwrite = ENTRY_VAL(ht_which_value);
+ goto write_position;
}
- while( ENTRY_OCCUPIED() )
- { if(overwrite)
- { if(bin_keys_identical(entry_pos, ht_entry->key))
- break;
- else
- { eprintf("error in hashtable insertion, keys are identical");
- }
- }
- if (HT_END(ht_end))
- LOOP_ENTRY(ht_start);
- else
- INC_ENTRY();
+ if (entry_row < ht_rows)
+ entry_row++;
+ else if (looped)
+ eprintf("cannot insert into filled hashtable\n");
+ else
+ { looped++;
+ entry_row = 0;
}
- WRITE_ENTRY();
+ goto next_entry;
+}
- return entry_pos;
-
+/** @see http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */
+static inline
+int bin_calculate_ht_entries
+( int entries )
+{ entries = (entries << 1) - 1;
+ entries |= entries >> 1;
+ entries |= entries >> 2;
+ entries |= entries >> 4;
+ entries |= entries >> 8;
+ entries |= entries >> 16;
+ return ++entries;
}
/* | class header |
long bin_traverse_set(ir_set);
long
-#define HT_INIT(_START, _SIZE, _INIT_ENTRIES) do { \
- _SIZE = _INIT_ENTRIES ? (_INIT_ENTRIES * (sizeof(struct bin_ht_entry_t) << 1)): 0; \
+#define DEF_HT_INIT(_START, _SIZE, _INIT_ENTRIES) do { \
+ _SIZE = _ENTRIES * sizeof(var_ht_entry); \
+ _START = ftell(binaryout); \
+ fseek(binaryout, _SIZE, SEEK_CUR); \
+} while (0)
+#define VAR_HT_INIT(_START, _SIZE, _ENTRIES) do { \
+ _SIZE = _ENTRIES * sizeof(var_ht_entry); \
_START = ftell(binaryout); \
fseek(binaryout, _SIZE, SEEK_CUR); \
} while (0)
{ ir_class citer;
ir_set siter;
struct bin_class_header_t class_header;
- struct bin_ht_entry_t ht_entry;
long class_start, classht_start, classht_size, rootsetht_start, rootsetht_size;
- int num_csibs, num_ssibs;
+ int num_class_entries, num_rootset_entries;
uint8_t* class_name;
+ class_header.namelen = u8_strlen(class_name);
+
+
class_start = ftell(binaryout);
class_name = ir_class_name(class);
- num_csibs = bin_class_sibcount(class);
- num_ssibs = bin_set_sibcount(ir_class_rootset(class));
+ num_class_entries = bin_calculate_ht_entries(bin_class_sibcount(class));
+ num_rootset_entries = bin_calculate_ht_entries(bin_set_sibcount(ir_class_rootset(class)));
/* alloc space (before hash tables) for class header */
- class_header.namelen = u8_strlen(class_name);
fseek(binaryout, class_start + sizeof(class_header) + class_header.namelen ,SEEK_SET);
- HT_INIT(classht_start, classht_size, num_csibs);
- HT_INIT(rootsetht_start, rootsetht_size, num_ssibs);
+ DEF_HT_INIT(classht_start, classht_size, num_class_entries);
+ DEF_HT_INIT(rootsetht_start, rootsetht_size, num_rootset_entries);
/* TODO: Figure out generic way to output headers */
/* populate class header */
- class_header.child_ht.entries = num_csibs;
+ class_header.child_ht.entries = num_class_entries;
class_header.child_ht.start = classht_start;
- class_header.rootset_ht.entries = num_ssibs;
+ class_header.child_ht.size = classht_size;
+ class_header.rootset_ht.entries = num_rootset_entries;
class_header.rootset_ht.start = rootsetht_start;
+ class_header.rootset_ht.size = rootsetht_size;
fseek(binaryout, class_start, SEEK_SET); //seek back to where we allocated header
fwrite(&class_header, sizeof(class_header), 1, binaryout);
fwrite(class_name, class_header.namelen, 1, binaryout);
/* Start populating root_set hash table */
for ( siter = ir_class_rootset(class); siter != NULL; siter = ir_set_nextsib(siter))
{ fseek(binaryout, 0, SEEK_END);
- ht_entry.key = NAMEHASH(ir_set_name(siter), num_ssibs << 1);
- ht_entry.value = bin_traverse_set(siter);
- bin_insert_ht_entry(rootsetht_start, rootsetht_size, &ht_entry, 0);
+ file_ht_insert(rootsetht_start, 2, 1,
+ rootsetht_size,
+ &old_value,
+ NAMEHASH(ir_set_name(siter), num_rootset_entries),
+ bin_traverse_set(siter));
}
/* Start populating class child hash table */
{ if(chdir((char*) class_name))
eprintf("CHDIR %U from %s\n",(char*) class_name,getcwd(NULL,255));
fseek(binaryout, 0, SEEK_END);
- ht_entry.key = NAMEHASH(ir_class_name(citer), num_csibs << 1);
- ht_entry.value = bin_traverse_class(citer);
- bin_insert_ht_entry(classht_start, classht_size, &ht_entry, 0);
+ file_ht_insert(classht_start, 2, 1,
+ classht_size,
+ &old_value,
+ NAMEHASH(ir_class_name(citer), num_class_entries),
+ bin_traverse_class(citer));
if (chdir(".."))
eprintf("CHDIR ..\n");
}
( ir_set set )
{ ir_set iter;
struct bin_set_header_t header;
- struct bin_ht_entry_t ht_entry;
- int num_child, setname_len;
+ struct bin_def_ht_entry_t ht_entry;
+ int num_entries, setname_len;
long childht_start, childht_size, set_start;
uint8_t* set_name;
header.sdat_start = bin_process_sdat(set);
/* Setup child hash table for current sets children */
- num_child = bin_set_sibcount(ir_set_nextchild(set));
- HT_INIT(childht_start, childht_size, num_child);
+ num_entries = bin_calculate_ht_entries(bin_set_sibcount(ir_set_nextchild(set)));
+
+ DEF_HT_INIT(childht_start, childht_size, num_child);
/* populate header, write to file */
- header.child_ht.entries = num_child;
+ header.child_ht.entries = num_entries;
header.child_ht.start = childht_start;
+ header.child_ht.size = childht_size;
fseek(binaryout, set_start, SEEK_SET);
fwrite(&header, sizeof(struct bin_set_header_t), 1, binaryout);
fwrite(set_name, setname_len, 1, binaryout);
for(iter = ir_set_nextchild(set); iter != NULL; iter = ir_set_nextsib(iter))
{ fseek(binaryout, 0, SEEK_END);
- ht_entry.key = NAMEHASH(ir_set_name(iter), num_child << 1);
+ ht_entry.key = NAMEHASH(ir_set_name(iter), num_entries);
ht_entry.value = bin_traverse_set(iter);
bin_insert_ht_entry(childht_start, childht_size, &ht_entry, 0);
}
*/
-void bin_insert_links(struct bin_processed_lists_t*, struct bin_ht_header_t*, long);
-struct bin_pixel_node_t* bin_find_default_pixel_list(ir_set);
-void bin_process_frameboxes(ir_set, struct bin_ht_header_t*, struct bin_pixel_node_t*);
-int bin_process_links(ir_set, ir_setdata);
-int bin_process_dlinks(struct bin_processed_lists_t*);
+static inline void bin_insert_links(struct bin_processed_links_t*, struct bin_ht_header_t*, long);
+static inline struct bin_pixel_node_t* bin_find_default_pixel_list(ir_set);
+static inline void bin_process_frameboxes(ir_set, struct bin_ht_header_t*, struct bin_pixel_node_t*);
+static inline struct bin_processed_links_t* bin_process_links(ir_set, struct bin_processed_links_t*);
+static inline void bin_process_dlinks(struct bin_processed_links_t*);
/* Init the variant hash table for the set, process the sets links and add them to link_stack
and variant hash table, and then output the actual framedata */
fseek(binaryout, sizeof(struct bin_setdata_header_t), SEEK_CUR);
/* set up root for processed_links */
- processed_links_root = stack_alloc(&plinkpages, bin_processed_links_t);
- processed_links_root_val->mlink_len = processed_links_root->vlink_len = 0;
- processed_links_root = bin_process_links(set, processed_links_root);
+ processed_links_root = stack_alloc(&plinkpages, sizeof(struct bin_processed_links_t));
+ processed_links_root->mlink_len = processed_links_root->vlink_len = processed_links_root->dlink_len = processed_links_root->olinks_len = 0;
+ processed_links_root = bin_process_links(set, processed_links_root);
num_links = processed_links_root->mlink_len + processed_links_root->vlink_len;
- num_entries = bin_set_varcount(set) + num_links;
+ num_entries = bin_calculate_ht_entries(bin_set_varcount(set) + num_links);
- HT_INIT(varht_start, varht_size, num_entries);
+ VAR_HT_INIT(varht_start, varht_size, num_entries);
/* Populate the sdat_header */
fseek(binaryout, 0, sdat_start);
- header.variant_ht.start = ht_header.start = varht_start;
- header.variant_ht.entries = ht_header.entries = num_entries;
+ header.variant_ht.start = varht_start;
+ header.variant_ht.entries = num_entries;
+ header.variant_ht.size = varht_size;
attachment_list.filepos = header.attach_pos = ftell(binaryout) + sizeof(varht_start) + sizeof(num_entries);
fwrite(&header, sizeof(header), 1, binaryout);
- fseek(binaryout, 0, SEEK_END);
+ fseek(binaryout, 0, SEEK_ENDhttps://en.wikipedia.org/wiki/Generic_programming);
/* Process dlinks */
bin_process_dlinks(processed_links_root);
-
/* Determine the default pixel list for all of the frameboxes */
default_pixel_list = bin_find_default_pixel_list(set);
/* Output each framebox, and insert it into the variant hash table */
/* TODO: Convert the default pixel list to an attachment_list and then push the */
/* sdats attachment_list onto the attachment_stack so it can be procesed */
+
- /* free plinkpages and datapages */
return sdat_start;
static inline
void bin_process_dlinks
( struct bin_processed_links_t* processed_links )
-{ struct bin_linklist_t* olink_iter;
- ir_set trg_set;
+{ struct bin_linklist_t* dlink_iter;
for( dlink_iter = processed_links->dlink_list; dlink_iter != NULL; dlink_iter = dlink_iter->next)
- { /* Construct its fully qualified name based on ?*/
+ { /* TODO: Construct its fully qualified name based on its linkdata*/
+
/* Output an int for its length, and then output the name */
bin_process_vlink
( linkdata vlink,
struct bin_processed_links_t* processed_links)
-{ struct bin_processed_links_t* plp;
+{ struct bin_linklist_t* llp;
struct bin_linklist_t* vlink_list_head;
linkdata new_vlink;
ir_setdata fiter;
vlink_list_head = bin_linklist_head(processed_links->vlink_list);
link_name = ir_setdata_name(vlink);
if (link_name)
- { plp = struct_alloc(bin_processed_links_t);
- plp->linkdata = vlink;
- vlink_list_head->next = plp;
+ { llp = stack_alloc(&plinkpages, sizeof(bin_linklist_t));
+ llp->linkdata = vlink;
+ vlink_list_head->next = llp;
processed_links->vlink_len++;
- PUSH_LINK(vlink);
}
else // linking a variant hash table
{ trg_set = ir_linkdata_set(vlink);
for (fiter = ir_set_framebox(trg_set); fiter != NULL; fiter = ir_setdata_nextsib(fiter))
- { plp = struct_alloc(bin_processed_links_t);
- new_vlink = struct_alloc(linkdata);
- ir_linkdata_assign_name(new_vlink,ir_setdata_name(fiter));
- ir_linkdata_assign_set(new_vlink,trg_set);
- ir_linkdata_assign_type(new_vlink,VLINK);
- plp->linkdata = vlink;
- vlink_list_head->next = plp;
- processed_links->vlink_len++;
- PUSH_LINK(vlink);
- }
+ { llp = stack_alloc(&plinkpages, sizeof(bin_linklist_t));
+ new_vlink = stack_alloc(&plinkpages, sizeof(linkdata));
+ ir_data_assign_path(new_vlink,ir_setdata_name(fiter));
+ ir_linkdata_assign_set(new_vlink,trg_set);
+ ir_linkdata_assign_type(new_vlink,VLINK);
+ llp->linkdata = vlink;
+ vlink_list_head->next = llp;
+ processed_links->vlink_len++;
+ }
}
- return processed_links;
}
/* Adds an mlink to the stack_alloc, to be processed later */
+/* TODO: redo */
void
bin_process_mlink
( linkdata mlink,
- bin_processed_links_t* processed_links
+ struct bin_processed_links_t* processed_links
)
{ uint8_t* mlink_name;
- struct bin_processed_links_t* mlink_list_head;
+ struct bin_linklist_t* llp;
+ struct bin_linklist_t* mlink_list_head;
+ linkdata new_mlink;
+ ir_setdata fiter;
+ ir_set trg_set;
+
- mlink_list_head = bin_listlist_head(processed_links->mlink_list);
+ mlink_list_head = bin_linklist_head(processed_links->mlink_list);
mlink_name = ir_setdata_name(mlink);
- plp = stack_alloc(&linkpages, bin_linklist_t);
- plp->header.filepos = 0; //TBD after resolving the childlist | TODO: attach_pos?
- if(mlink_name) plp->name = mlink_name;// TODO: What does a mlink with a name mean? specifying the framebox mapsheet to use?
- plp->trg_set = ir_linkdata_set(mlink);
- mlink_list_head->next = plp;
- processed_links->mlink_len++;
+
+ if(mlink_name)
+ { llp = stack_alloc(&plinkpages, sizeof(bin_linklist_t));
+ llp->linkdata = mlink;
+ mlink_list_head->next = llp;
+ processed_links->mlink_len++;
+ }
+ else
+ { trg_set = ir_linkdata_set(mlink);
+ for (fiter = ir_set_framebox(trg_set); fiter != NULL; fiter = ir_setdata_nextsib(fiter))
+ { //TODO: check here if illegal mlink(linking to a opsheet of a vdat not in src_set domain)?
+ llp = stack_alloc(&plinkpages, sizeof(bin_linklist_t));
+ new_mlink = stack_alloc(&plinkpages, sizeof(linkdata));
+ ir_data_assign_path(new_mlink,ir_setdata_name(fiter));//TODO: assign name
+ ir_linkdata_assign_set(new_vlink,trg_set);
+ ir_linkdata_assign_type(new_vlink,VLINK);
+ llp->linkdata = vlink;
+ vlink_list_head->next = llp;
+ processed_links->vlink_len++;
+ }
+ }
+
+
return processed_links;
}
-/* TODO: implement this */
/* Determine if olink is already part of the olink_list.
if it is, theres a cycle, return 1. Else return 0. */
static inline
ir_set olink_set;
olink_set = ir_linkdata_set(olink);
for( iter = processed_links->olink_list; iter != NULL; iter = iter->next)
- if(iter == olink_set)
+ if(ir_linkdata_set(iter->linkdata) == olink_set)
return 1;
return 0;
else its a dlink so just add it to the processed_links list*/
static inline
void bin_process_olink
-( linkdata olink,
- struct bin_processed_links_t* processed_links_root,
- ir_set trg_set
+( ir_set trg_set,
+ linkdata olink,
+ struct bin_processed_links_t* processed_links_root
)
{ struct bin_linklist_t* link_list_head;
- if(trg_set)) //add olink to list so we can check for cycles
- { bin_set_frameboxes_vlinks(trg_set, processed_links_root); //TODO: implement
- bin_set_attachmentlist_mlink(trg_set, processed_links_root); //TODO: implement
+ struct bin_linklist_t* new_link;
+
+ new_link = stack_alloc(&plinkpages, sizeof(bin_linklist_t));
+ if(trg_set) //add olink to list so we can check for cycles
+ { bin_set_frameboxes_vlinks(trg_set, processed_links_root); //TODO:
+ bin_set_attachmentlist_mlink(trg_set, processed_links_root); //TODO:
link_list_head = bin_linklist_head(processed_links_root->olink_list);
- link_list_head->next = struct_alloc(bin_linklist_t);
- link_list_head->next->linkdata = olink;
+ new_link->linkdata = olink;
+ link_list_head->next = new_link;
}
else // olink is actually a dynamic link
{ link_list_head = bin_linklist_head(processed_links_root->dlink_list);
- link_list_head->next = struct_alloc(bin_linklist_t);
- link_list_head->next->linkdata = olink;
+ new_link->linkdata = olink;
+ link_list_head->next = new_link;
}
}
-struct bin_linklist_t;
-struct bin_linklist_t
-{ struct bin_linklist_t* next;
- linkdata linkdata;
-};
-
-struct bin_processed_links_t
-{ struct bin_linklist_t* vlink_list;
- int vlink_len;
- struct bin_linklist_t* mlink_list;
- int mlink_len;
- struct bin_linklist_t* olink_list; //keep track of olink cycles
- int olink_len;
- struct bin_linklist_t* dlink_list;
- int dlink_len;
-};
/* Given a set, determine the number of links it has and process each link and
then add them to stack_alloc, where they will be popped off and further processed. */
struct bin_processed_links_t* bin_process_links
( ir_set src_set,
- struct bin_processed_links_t* processed_links_root;
+ struct bin_processed_links_t* processed_links_root
)
-{ struct bin_processed_links_t* returned_val;
- linkdata linkdata;
+{ linkdata linkdata;
ir_set trg_set;
- for(linkdata = ir_set_link(src_set); linkdata != NULL; linkdata = (linkdata) ir_setdata_nextsib((ir_setdata) linkdata))
+ for(linkdata = ir_set_link(src_set); linkdata != NULL; linkdata = ir_setdata_nextsib((ir_setdata) linkdata))
{ switch (ir_linkdata_type(linkdata)) {
case OLINK:
if (olink_cycle(linkdata, processed_links_root))
}
/* Insert both mlinks and vlinks into the link stack, after determining their src_pos. Vlinks
- have an additional requirement of being added into the variant hash table */
-#define PUSH_PLINK(_LINK) (*(struct **) stack_alloc(&linkpages, sizeof (_LINK)) = _LINK )
+ have an additional requirement of being added into the variant hash table */
+#define FRAME_POS() (entry_pos + sizeof(long))
+#define MAP_POS() (entry_pos + sizeof(long)*2)
+#define PUSH_PLINK(_LINK) (*(linkdata*) stack_alloc(&linkpages, sizeof (_LINK)) = _LINK )
void
bin_insert_links
( struct bin_processed_links_t* links,
long attach_pos
)
{ struct bin_plink_t* plp;
- struct bin_ht_entry_t ht_entry;
- struct link_list_t* vlinkiter, mlinkiter;
+ struct bin_var_ht_entry_t ht_entry;
+ struct bin_linklist_t* vlinkiter, *mlinkiter;
long entry_pos;
- /* Insert vlinks into hash table, put v/mlinks on link stack to be processed later */
- for ( vlinkiter = links->vlink_root; vlinkiter != NULL; vlinkiter = vlinkiter->next;)
- { ht_entry.key = NAMEHASH(ir_setdata_name(vlinkiter->linkdata), ht->entries << 1);
- ht_entry.value = 0;
- entry_pos = bin_insert_ht_entry(ht->start, ht->entries * sizeof(ht_entry), &ht_entry, 0);
- ir_setdata_assign_fpos(vlinkiter->linkdata, entry_pos);
+ /* Insert vlinks and mlinks into hash table, put v/mlinks on link stack to be processed later */
+ for ( vlinkiter = links->vlink_list; vlinkiter != NULL; vlinkiter = vlinkiter->next)
+ { ht_entry.key = NAMEHASH(ir_setdata_name(vlinkiter->linkdata), ht->entries);
+ ht_entry.fvalue = 0;
+ entry_pos = bin_insert_var_ht_entry(ht->start, ht->size, &ht_entry, 0);
+ ir_setdata_assign_fpos(vlinkiter->linkdata, FRAME_POS());
PUSH_PLINK(vlinkiter->linkdata);
}
- for ( mlinkiter = links->mlink_root; mlinkiter != NULL; mlinkiter = mlinkiter->next)
- { >trg_set = plp->trg_set;
- plp->src_pos = attach_pos;
- PUSH_PLINK(plp);
+ /* TODO: If name exists in src_set, overwrite. if it dont, print a warning */
+ for ( mlinkiter = links->mlink_list; mlinkiter != NULL; mlinkiter = mlinkiter->next)
+ { ht_entry.key = NAMEHASH(ir_setdata_name(mlinkiter->linkdata), ht->size);
+ ht_entry.mvalue = 0;
+ entrypos = bin_insert_var_ht_entry(ht->start, ht->size, &ht_entry, 0);
+ ir_setdata_assign_fpos(mlinkiter->linkdata, MAP_POS());
+ PUSH_PLINK(mlinkiter->linkdata);
}
- /* Process dlinks here */
+ /* free all the processed links */
+ pagelist_free(plinkpages);
}
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,
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 << 1);
+ 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_ht_entry(ht->start, ht->entries * sizeof(ht_entry), &ht_entry, 1);
+ bin_insert_var_ht_entry(ht->start, ht->entries * sizeof(ht_entry), &ht_entry, 1);
}
}
|-----------------------------| */
long
+//TODO: THIS SHOULD THE SET SPEC, NOT THE FRAMEBOX NAME
#define GENERATE_FILENAME(_N) ((char*) u8_strcat(_N, png_suffix))
bin_process_facing
( ir_setdata framebox,
fwrite(&header, sizeof(header), 1, binaryout);
fseek(binaryout, 0, SEEK_END);
+
+
/* Assuming that fheight = image height */
/* For each mapframe in mapsheet */
for ( x = 0; x < header.frames; x++)
- { map_pixel_list = bin_map_to_pixel_list(mapsheet_info, 0, x * mapsheet_info.fwidth, data);
+ { map_pixel_list = bin_mapframe_to_pixel_list(mapsheet_info, 0, x * mapsheet_info.fwidth, mapdata);
if(!bin_process_map_pixel_list(default_pixel_list, map_pixel_list))
eprintf("error processing map pixel list\n");
bin_output_pixel_list(map_pixel_list);
- data += mapsheet_info.fwidth;
+ mapdata = mapsheet_info.fwidth * x; //do we do this in mapframe to pixellist?
}
- /* Determine pixel_list */
-
- /* Output pixel_list */
+
return facing_start;
}
+/* pixel_list == ops, output up to fwidth amount of them */
+void
+bin_output_pixel_list(struct bin_pixel_node_t* map_pixel_list);
+
/* TODO: Please rename all the functions jhc*/
static inline
void bin_number_pixel_list
{ int num = 0;
struct bin_pixel_node_t* iter;
while (iter)
- { iter.data.attach_idx = num++;
+ { iter->data.attach_idx = num++;
iter = iter->next;
}
}
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;
+}
-/* 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 */
+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;
+
+ 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 */
- while(defaultiter)
- { for( mapiter = map_pixel_list; mapiter != NULL; mapiter = mapiter->next)
- {
- }
- }
+ 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
struct bin_pixel_node_t* map_pixel_list
)
{ /* Determine if pixel_list is valid */
-
+ if(!bin_valid_map_pixel_list(default_pixel_list, map_pixel_list))
+ return 0;
+
/* Determine attach_idx of each pixel, as compared to default pixel list */
+
}
void
-bin_assign_pixel_idxs
+ bin_assign_pixel_idxs
( struct bin_pixel_node_t* pixel_list )
{
}
/* 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->data.z == head_node->data.z)
- { prev_node->next = pixel_node;
- pixel_node->next = head_node;
- break;
- }
- }
+ 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
}
+
return pixel_list;
}
}
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,