MCPcopy Create free account
hub / github.com/ValveSoftware/openvr / HuffmanTree_make2DTree

Function HuffmanTree_make2DTree

samples/shared/lodepng.cpp:540–597  ·  view source on GitHub ↗

the tree representation used by the decoder. return value is error*/

Source from the content-addressed store, hash-verified

538
539/*the tree representation used by the decoder. return value is error*/
540static unsigned HuffmanTree_make2DTree(HuffmanTree* tree)
541{
542 unsigned nodefilled = 0; /*up to which node it is filled*/
543 unsigned treepos = 0; /*position in the tree (1 of the numcodes columns)*/
544 unsigned n, i;
545
546 tree->tree2d = (unsigned*)lodepng_malloc(tree->numcodes * 2 * sizeof(unsigned));
547 if(!tree->tree2d) return 83; /*alloc fail*/
548
549 /*
550 convert tree1d[] to tree2d[][]. In the 2D array, a value of 32767 means
551 uninited, a value >= numcodes is an address to another bit, a value < numcodes
552 is a code. The 2 rows are the 2 possible bit values (0 or 1), there are as
553 many columns as codes - 1.
554 A good huffmann tree has N * 2 - 1 nodes, of which N - 1 are internal nodes.
555 Here, the internal nodes are stored (what their 0 and 1 option point to).
556 There is only memory for such good tree currently, if there are more nodes
557 (due to too long length codes), error 55 will happen
558 */
559 for(n = 0; n < tree->numcodes * 2; n++)
560 {
561 tree->tree2d[n] = 32767; /*32767 here means the tree2d isn't filled there yet*/
562 }
563
564 for(n = 0; n < tree->numcodes; n++) /*the codes*/
565 {
566 for(i = 0; i < tree->lengths[n]; i++) /*the bits for this code*/
567 {
568 unsigned char bit = (unsigned char)((tree->tree1d[n] >> (tree->lengths[n] - i - 1)) & 1);
569 if(treepos > tree->numcodes - 2) return 55; /*oversubscribed, see comment in lodepng_error_text*/
570 if(tree->tree2d[2 * treepos + bit] == 32767) /*not yet filled in*/
571 {
572 if(i + 1 == tree->lengths[n]) /*last bit*/
573 {
574 tree->tree2d[2 * treepos + bit] = n; /*put the current code in it*/
575 treepos = 0;
576 }
577 else
578 {
579 /*put address of the next step in here, first that address has to be found of course
580 (it's just nodefilled + 1)...*/
581 nodefilled++;
582 /*addresses encoded with numcodes added to it*/
583 tree->tree2d[2 * treepos + bit] = nodefilled + tree->numcodes;
584 treepos = nodefilled;
585 }
586 }
587 else treepos = tree->tree2d[2 * treepos + bit] - tree->numcodes;
588 }
589 }
590
591 for(n = 0; n < tree->numcodes * 2; n++)
592 {
593 if(tree->tree2d[n] == 32767) tree->tree2d[n] = 0; /*remove possible remaining 32767's*/
594 }
595
596 return 0;
597}

Callers 1

Calls 1

lodepng_mallocFunction · 0.85

Tested by

no test coverage detected