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