enc_debug_tree.cc (2905B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #include "lib/jxl/modular/encoding/enc_debug_tree.h" 7 8 #include <cinttypes> // PRId64 9 #include <cstdlib> 10 #include <string> 11 12 #include "lib/jxl/base/printf_macros.h" 13 #include "lib/jxl/modular/encoding/context_predict.h" 14 #include "lib/jxl/modular/encoding/dec_ma.h" 15 #include "lib/jxl/modular/options.h" 16 17 namespace jxl { 18 19 namespace { 20 const char *PredictorName(Predictor p) { 21 switch (p) { 22 case Predictor::Zero: 23 return "Zero"; 24 case Predictor::Left: 25 return "Left"; 26 case Predictor::Top: 27 return "Top"; 28 case Predictor::Average0: 29 return "Avg0"; 30 case Predictor::Average1: 31 return "Avg1"; 32 case Predictor::Average2: 33 return "Avg2"; 34 case Predictor::Average3: 35 return "Avg3"; 36 case Predictor::Average4: 37 return "Avg4"; 38 case Predictor::Select: 39 return "Sel"; 40 case Predictor::Gradient: 41 return "Grd"; 42 case Predictor::Weighted: 43 return "Wgh"; 44 case Predictor::TopLeft: 45 return "TopL"; 46 case Predictor::TopRight: 47 return "TopR"; 48 case Predictor::LeftLeft: 49 return "LL"; 50 default: 51 return "INVALID"; 52 }; 53 } 54 55 std::string PropertyName(size_t i) { 56 static_assert(kNumNonrefProperties == 16, "Update this function"); 57 switch (i) { 58 case 0: 59 return "c"; 60 case 1: 61 return "g"; 62 case 2: 63 return "y"; 64 case 3: 65 return "x"; 66 case 4: 67 return "|N|"; 68 case 5: 69 return "|W|"; 70 case 6: 71 return "N"; 72 case 7: 73 return "W"; 74 case 8: 75 return "W-WW-NW+NWW"; 76 case 9: 77 return "W+N-NW"; 78 case 10: 79 return "W-NW"; 80 case 11: 81 return "NW-N"; 82 case 12: 83 return "N-NE"; 84 case 13: 85 return "N-NN"; 86 case 14: 87 return "W-WW"; 88 case 15: 89 return "WGH"; 90 default: 91 return "ch[" + ToString(15 - static_cast<int>(i)) + "]"; 92 } 93 } 94 95 } // namespace 96 97 void PrintTree(const Tree &tree, const std::string &path) { 98 FILE *f = fopen((path + ".dot").c_str(), "w"); 99 fprintf(f, "graph{\n"); 100 for (size_t cur = 0; cur < tree.size(); cur++) { 101 if (tree[cur].property < 0) { 102 fprintf(f, "n%05" PRIuS " [label=\"%s%+" PRId64 " (x%u)\"];\n", cur, 103 PredictorName(tree[cur].predictor), tree[cur].predictor_offset, 104 tree[cur].multiplier); 105 } else { 106 fprintf(f, "n%05" PRIuS " [label=\"%s>%d\"];\n", cur, 107 PropertyName(tree[cur].property).c_str(), tree[cur].splitval); 108 fprintf(f, "n%05" PRIuS " -- n%05d;\n", cur, tree[cur].lchild); 109 fprintf(f, "n%05" PRIuS " -- n%05d;\n", cur, tree[cur].rchild); 110 } 111 } 112 fprintf(f, "}\n"); 113 fclose(f); 114 #if JXL_ENABLE_DOT 115 system(("dot " + path + ".dot -T svg -o " + path + ".svg").c_str()); 116 #endif 117 } 118 119 } // namespace jxl