LLVMFuzzerTestOneInput:
  115|  6.85k|extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  116|  6.85k|  Jbig2Allocator allocator;
  117|  6.85k|  Jbig2Ctx *ctx = NULL;
  118|       |
  119|  6.85k|  used = 0;
  120|       |
  121|  6.85k|  allocator.alloc = jbig2_fuzzer_alloc;
  122|  6.85k|  allocator.free = jbig2_fuzzer_free;
  123|  6.85k|  allocator.realloc = jbig2_fuzzer_realloc;
  124|       |
  125|  6.85k|  ctx = jbig2_ctx_new(&allocator, (Jbig2Options) 0, NULL, NULL, NULL);
  ------------------
  |  |   84|  6.85k|#define jbig2_ctx_new(allocator, options, global_ctx, error_callback, error_callback_data) jbig2_ctx_new_imp((allocator), (options), (global_ctx), (error_callback), (error_callback_data), JBIG2_VERSION_MAJOR, JBIG2_VERSION_MINOR)
  |  |  ------------------
  |  |  |  |   28|  6.85k|#define JBIG2_VERSION_MAJOR (0)
  |  |  ------------------
  |  |               #define jbig2_ctx_new(allocator, options, global_ctx, error_callback, error_callback_data) jbig2_ctx_new_imp((allocator), (options), (global_ctx), (error_callback), (error_callback_data), JBIG2_VERSION_MAJOR, JBIG2_VERSION_MINOR)
  |  |  ------------------
  |  |  |  |   29|  6.85k|#define JBIG2_VERSION_MINOR (20)
  |  |  ------------------
  ------------------
  126|  6.85k|  if (jbig2_data_in(ctx, data, size) == 0)
  ------------------
  |  Branch (126:7): [True: 5.79k, False: 1.05k]
  ------------------
  127|  5.79k|  {
  128|  5.79k|    if (jbig2_complete_page(ctx) == 0)
  ------------------
  |  Branch (128:9): [True: 627, False: 5.16k]
  ------------------
  129|    627|    {
  130|    627|      Jbig2Image *image = jbig2_page_out(ctx);
  131|    627|      if (image != NULL)
  ------------------
  |  Branch (131:11): [True: 627, False: 0]
  ------------------
  132|    627|      {
  133|    627|        int sum = 0;
  134|   834M|        for (int i = 0; i < image->height * image->stride; i++)
  ------------------
  |  Branch (134:25): [True: 834M, False: 627]
  ------------------
  135|   834M|          sum += image->data[i];
  136|    627|        printf("sum of image data bytes: %d\n", sum);
  137|    627|      }
  138|    627|      jbig2_release_page(ctx, image);
  139|    627|    }
  140|  5.79k|  }
  141|  6.85k|  jbig2_ctx_free(ctx);
  142|       |
  143|  6.85k|  return 0;
  144|  6.85k|}
jbig2_fuzzer.cc:_ZL18jbig2_fuzzer_allocP15_Jbig2Allocatorm:
   45|  48.8M|{
   46|  48.8M|  char *ptr = NULL;
   47|       |
   48|  48.8M|  if (size == 0)
  ------------------
  |  Branch (48:7): [True: 5.75k, False: 48.8M]
  ------------------
   49|  5.75k|    return NULL;
   50|  48.8M|  if (size > SIZE_MAX - ALIGNMENT)
  ------------------
  |  |   26|  48.8M|#define ALIGNMENT ((size_t) 16)
  ------------------
  |  Branch (50:7): [True: 0, False: 48.8M]
  ------------------
   51|      0|    return NULL;
   52|  48.8M|  if (size + ALIGNMENT > MAX_ALLOCATION - used)
  ------------------
  |  |   26|  48.8M|#define ALIGNMENT ((size_t) 16)
  ------------------
                if (size + ALIGNMENT > MAX_ALLOCATION - used)
  ------------------
  |  |   30|  48.8M|#define MAX_ALLOCATION (32 * MBYTE)
  |  |  ------------------
  |  |  |  |   28|  48.8M|#define MBYTE (1024 * KBYTE)
  |  |  |  |  ------------------
  |  |  |  |  |  |   27|  48.8M|#define KBYTE ((size_t) 1024)
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  |  Branch (52:7): [True: 1.34k, False: 48.8M]
  ------------------
   53|  1.34k|    return jbig2_fuzzer_reached_limit(0, size + ALIGNMENT);
  ------------------
  |  |   26|  1.34k|#define ALIGNMENT ((size_t) 16)
  ------------------
   54|       |
   55|  48.8M|  ptr = (char *) malloc(size + ALIGNMENT);
  ------------------
  |  |   26|  48.8M|#define ALIGNMENT ((size_t) 16)
  ------------------
   56|  48.8M|  if (ptr == NULL)
  ------------------
  |  Branch (56:7): [True: 0, False: 48.8M]
  ------------------
   57|      0|    return NULL;
   58|       |
   59|  48.8M|  memcpy(ptr, &size, sizeof(size));
   60|  48.8M|  used += size + ALIGNMENT;
  ------------------
  |  |   26|  48.8M|#define ALIGNMENT ((size_t) 16)
  ------------------
   61|       |
   62|  48.8M|  return ptr + ALIGNMENT;
  ------------------
  |  |   26|  48.8M|#define ALIGNMENT ((size_t) 16)
  ------------------
   63|  48.8M|}
jbig2_fuzzer.cc:_ZL26jbig2_fuzzer_reached_limitmm:
   35|  1.41k|{
   36|  1.41k|  if (oldsize == 0)
  ------------------
  |  Branch (36:7): [True: 1.34k, False: 67]
  ------------------
   37|  1.34k|    fprintf(stderr, "limit: %zu Mbyte used: %zu Mbyte allocation: %zu: limit reached\n", MAX_ALLOCATION / MBYTE, used / MBYTE, size);
  ------------------
  |  |   30|  1.34k|#define MAX_ALLOCATION (32 * MBYTE)
  |  |  ------------------
  |  |  |  |   28|  1.34k|#define MBYTE (1024 * KBYTE)
  |  |  |  |  ------------------
  |  |  |  |  |  |   27|  1.34k|#define KBYTE ((size_t) 1024)
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
                  fprintf(stderr, "limit: %zu Mbyte used: %zu Mbyte allocation: %zu: limit reached\n", MAX_ALLOCATION / MBYTE, used / MBYTE, size);
  ------------------
  |  |   28|  1.34k|#define MBYTE (1024 * KBYTE)
  |  |  ------------------
  |  |  |  |   27|  1.34k|#define KBYTE ((size_t) 1024)
  |  |  ------------------
  ------------------
                  fprintf(stderr, "limit: %zu Mbyte used: %zu Mbyte allocation: %zu: limit reached\n", MAX_ALLOCATION / MBYTE, used / MBYTE, size);
  ------------------
  |  |   28|  1.34k|#define MBYTE (1024 * KBYTE)
  |  |  ------------------
  |  |  |  |   27|  1.34k|#define KBYTE ((size_t) 1024)
  |  |  ------------------
  ------------------
   38|     67|  else
   39|     67|    fprintf(stderr, "limit: %zu Mbyte used: %zu Mbyte reallocation: %zu -> %zu: limit reached\n", MAX_ALLOCATION / MBYTE, used / MBYTE, oldsize, size);
  ------------------
  |  |   30|     67|#define MAX_ALLOCATION (32 * MBYTE)
  |  |  ------------------
  |  |  |  |   28|     67|#define MBYTE (1024 * KBYTE)
  |  |  |  |  ------------------
  |  |  |  |  |  |   27|     67|#define KBYTE ((size_t) 1024)
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
                  fprintf(stderr, "limit: %zu Mbyte used: %zu Mbyte reallocation: %zu -> %zu: limit reached\n", MAX_ALLOCATION / MBYTE, used / MBYTE, oldsize, size);
  ------------------
  |  |   28|     67|#define MBYTE (1024 * KBYTE)
  |  |  ------------------
  |  |  |  |   27|     67|#define KBYTE ((size_t) 1024)
  |  |  ------------------
  ------------------
                  fprintf(stderr, "limit: %zu Mbyte used: %zu Mbyte reallocation: %zu -> %zu: limit reached\n", MAX_ALLOCATION / MBYTE, used / MBYTE, oldsize, size);
  ------------------
  |  |   28|     67|#define MBYTE (1024 * KBYTE)
  |  |  ------------------
  |  |  |  |   27|     67|#define KBYTE ((size_t) 1024)
  |  |  ------------------
  ------------------
   40|  1.41k|  fflush(0);
   41|       |  return NULL;
   42|  1.41k|}
jbig2_fuzzer.cc:_ZL17jbig2_fuzzer_freeP15_Jbig2AllocatorPv:
   66|  48.9M|{
   67|  48.9M|  size_t size;
   68|       |
   69|  48.9M|  if (ptr == NULL)
  ------------------
  |  Branch (69:7): [True: 94.2k, False: 48.8M]
  ------------------
   70|  94.2k|    return;
   71|  48.8M|  if (ptr < (void *) ALIGNMENT)
  ------------------
  |  |   26|  48.8M|#define ALIGNMENT ((size_t) 16)
  ------------------
  |  Branch (71:7): [True: 0, False: 48.8M]
  ------------------
   72|      0|    return;
   73|       |
   74|  48.8M|  ptr = (char *) ptr - ALIGNMENT;
  ------------------
  |  |   26|  48.8M|#define ALIGNMENT ((size_t) 16)
  ------------------
   75|  48.8M|  memcpy(&size, ptr, sizeof(size));
   76|       |
   77|  48.8M|  used -= size + ALIGNMENT;
  ------------------
  |  |   26|  48.8M|#define ALIGNMENT ((size_t) 16)
  ------------------
   78|  48.8M|  free(ptr);
   79|  48.8M|}
jbig2_fuzzer.cc:_ZL20jbig2_fuzzer_reallocP15_Jbig2AllocatorPvm:
   82|  8.13k|{
   83|  8.13k|  size_t oldsize;
   84|  8.13k|  char *ptr;
   85|       |
   86|  8.13k|  if (old == NULL)
  ------------------
  |  Branch (86:7): [True: 0, False: 8.13k]
  ------------------
   87|      0|    return jbig2_fuzzer_alloc(allocator, size);
   88|  8.13k|  if (old < (void *) ALIGNMENT)
  ------------------
  |  |   26|  8.13k|#define ALIGNMENT ((size_t) 16)
  ------------------
  |  Branch (88:7): [True: 0, False: 8.13k]
  ------------------
   89|      0|    return NULL;
   90|       |
   91|  8.13k|  if (size == 0) {
  ------------------
  |  Branch (91:7): [True: 0, False: 8.13k]
  ------------------
   92|      0|    jbig2_fuzzer_free(allocator, old);
   93|      0|    return NULL;
   94|      0|  }
   95|  8.13k|  if (size > SIZE_MAX - ALIGNMENT)
  ------------------
  |  |   26|  8.13k|#define ALIGNMENT ((size_t) 16)
  ------------------
  |  Branch (95:7): [True: 0, False: 8.13k]
  ------------------
   96|      0|    return NULL;
   97|       |
   98|  8.13k|  old = (char *) old - ALIGNMENT;
  ------------------
  |  |   26|  8.13k|#define ALIGNMENT ((size_t) 16)
  ------------------
   99|  8.13k|  memcpy(&oldsize, old, sizeof(oldsize));
  100|       |
  101|  8.13k|  if (size + ALIGNMENT > MAX_ALLOCATION - used + oldsize + ALIGNMENT)
  ------------------
  |  |   26|  8.13k|#define ALIGNMENT ((size_t) 16)
  ------------------
                if (size + ALIGNMENT > MAX_ALLOCATION - used + oldsize + ALIGNMENT)
  ------------------
  |  |   30|  8.13k|#define MAX_ALLOCATION (32 * MBYTE)
  |  |  ------------------
  |  |  |  |   28|  8.13k|#define MBYTE (1024 * KBYTE)
  |  |  |  |  ------------------
  |  |  |  |  |  |   27|  8.13k|#define KBYTE ((size_t) 1024)
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
                if (size + ALIGNMENT > MAX_ALLOCATION - used + oldsize + ALIGNMENT)
  ------------------
  |  |   26|  8.13k|#define ALIGNMENT ((size_t) 16)
  ------------------
  |  Branch (101:7): [True: 67, False: 8.06k]
  ------------------
  102|     67|    return jbig2_fuzzer_reached_limit(oldsize + ALIGNMENT, size + ALIGNMENT);
  ------------------
  |  |   26|     67|#define ALIGNMENT ((size_t) 16)
  ------------------
                  return jbig2_fuzzer_reached_limit(oldsize + ALIGNMENT, size + ALIGNMENT);
  ------------------
  |  |   26|     67|#define ALIGNMENT ((size_t) 16)
  ------------------
  103|       |
  104|  8.06k|  ptr = (char *) realloc(old, size + ALIGNMENT);
  ------------------
  |  |   26|  8.06k|#define ALIGNMENT ((size_t) 16)
  ------------------
  105|  8.06k|  if (ptr == NULL)
  ------------------
  |  Branch (105:7): [True: 0, False: 8.06k]
  ------------------
  106|      0|    return NULL;
  107|       |
  108|  8.06k|  used -= oldsize + ALIGNMENT;
  ------------------
  |  |   26|  8.06k|#define ALIGNMENT ((size_t) 16)
  ------------------
  109|  8.06k|  memcpy(ptr, &size, sizeof(size));
  110|  8.06k|  used += size + ALIGNMENT;
  ------------------
  |  |   26|  8.06k|#define ALIGNMENT ((size_t) 16)
  ------------------
  111|       |
  112|  8.06k|  return ptr + ALIGNMENT;
  ------------------
  |  |   26|  8.06k|#define ALIGNMENT ((size_t) 16)
  ------------------
  113|  8.06k|}

jbig2_alloc:
   66|  48.8M|{
   67|       |    /* Check for integer multiplication overflow when computing
   68|       |    the full size of the allocation. */
   69|  48.8M|    if (num > 0 && size > SIZE_MAX / num)
  ------------------
  |  Branch (69:9): [True: 48.8M, False: 5.75k]
  |  Branch (69:20): [True: 0, False: 48.8M]
  ------------------
   70|      0|        return NULL;
   71|  48.8M|    return allocator->alloc(allocator, size * num);
   72|  48.8M|}
jbig2_error:
   93|   171M|{
   94|   171M|    char buf[1024];
   95|   171M|    va_list ap;
   96|   171M|    int n;
   97|       |
   98|   171M|    va_start(ap, fmt);
   99|   171M|    n = vsnprintf(buf, sizeof(buf), fmt, ap);
  100|   171M|    va_end(ap);
  101|   171M|    if (n < 0 || n == sizeof(buf))
  ------------------
  |  Branch (101:9): [True: 0, False: 171M]
  |  Branch (101:18): [True: 0, False: 171M]
  ------------------
  102|      0|        ctx->error_callback(ctx->error_callback_data, "failed to generate error string", severity, segment_number);
  103|   171M|    else
  104|   171M|        ctx->error_callback(ctx->error_callback_data, buf, severity, segment_number);
  105|   171M|    return -1;
  106|   171M|}
jbig2_ctx_new_imp:
  110|  6.85k|{
  111|  6.85k|    Jbig2Ctx *result;
  112|       |
  113|  6.85k|    if (jbig2_version_major != JBIG2_VERSION_MAJOR || jbig2_version_minor != JBIG2_VERSION_MINOR) {
  ------------------
  |  |   28|  13.7k|#define JBIG2_VERSION_MAJOR (0)
  ------------------
                  if (jbig2_version_major != JBIG2_VERSION_MAJOR || jbig2_version_minor != JBIG2_VERSION_MINOR) {
  ------------------
  |  |   29|  6.85k|#define JBIG2_VERSION_MINOR (20)
  ------------------
  |  Branch (113:9): [True: 0, False: 6.85k]
  |  Branch (113:55): [True: 0, False: 6.85k]
  ------------------
  114|      0|        Jbig2Ctx fakectx;
  115|      0|        fakectx.error_callback = error_callback;
  116|      0|        fakectx.error_callback_data = error_callback_data;
  117|      0|        jbig2_error(&fakectx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "incompatible jbig2dec header (%d.%d) and library (%d.%d) versions",
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  118|      0|            jbig2_version_major, jbig2_version_minor, JBIG2_VERSION_MAJOR, JBIG2_VERSION_MINOR);
  ------------------
  |  |   28|      0|#define JBIG2_VERSION_MAJOR (0)
  ------------------
                          jbig2_version_major, jbig2_version_minor, JBIG2_VERSION_MAJOR, JBIG2_VERSION_MINOR);
  ------------------
  |  |   29|      0|#define JBIG2_VERSION_MINOR (20)
  ------------------
  119|      0|        return NULL;
  120|      0|    }
  121|       |
  122|  6.85k|    if (allocator == NULL)
  ------------------
  |  Branch (122:9): [True: 0, False: 6.85k]
  ------------------
  123|      0|        allocator = &jbig2_default_allocator;
  124|  6.85k|    if (error_callback == NULL)
  ------------------
  |  Branch (124:9): [True: 6.85k, False: 0]
  ------------------
  125|  6.85k|        error_callback = &jbig2_default_error;
  126|       |
  127|  6.85k|    result = (Jbig2Ctx *) jbig2_alloc(allocator, sizeof(Jbig2Ctx), 1);
  128|  6.85k|    if (result == NULL) {
  ------------------
  |  Branch (128:9): [True: 0, False: 6.85k]
  ------------------
  129|      0|        error_callback(error_callback_data, "failed to allocate initial context", JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER);
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  130|      0|        return NULL;
  131|      0|    }
  132|       |
  133|  6.85k|    result->allocator = allocator;
  134|  6.85k|    result->options = options;
  135|  6.85k|    result->global_ctx = (const Jbig2Ctx *)global_ctx;
  136|  6.85k|    result->error_callback = error_callback;
  137|  6.85k|    result->error_callback_data = error_callback_data;
  138|       |
  139|  6.85k|    result->state = (options & JBIG2_OPTIONS_EMBEDDED) ? JBIG2_FILE_HEADER_MAYBE : JBIG2_FILE_HEADER;
  ------------------
  |  Branch (139:21): [True: 0, False: 6.85k]
  ------------------
  140|       |
  141|  6.85k|    result->buf = NULL;
  142|       |
  143|  6.85k|    result->n_segments = 0;
  144|  6.85k|    result->n_segments_max = 16;
  145|  6.85k|    result->segments = jbig2_new(result, Jbig2Segment *, result->n_segments_max);
  ------------------
  |  |  141|  6.85k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  146|  6.85k|    if (result->segments == NULL) {
  ------------------
  |  Branch (146:9): [True: 0, False: 6.85k]
  ------------------
  147|      0|        error_callback(error_callback_data, "failed to allocate initial segments", JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER);
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  148|      0|        jbig2_free(allocator, result);
  149|      0|        return NULL;
  150|      0|    }
  151|  6.85k|    result->segment_index = 0;
  152|       |
  153|  6.85k|    result->current_page = 0;
  154|  6.85k|    result->max_page_index = 4;
  155|  6.85k|    result->pages = jbig2_new(result, Jbig2Page, result->max_page_index);
  ------------------
  |  |  141|  6.85k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  156|  6.85k|    if (result->pages == NULL) {
  ------------------
  |  Branch (156:9): [True: 0, False: 6.85k]
  ------------------
  157|      0|        error_callback(error_callback_data, "failed to allocated initial pages", JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER);
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  158|      0|        jbig2_free(allocator, result->segments);
  159|      0|        jbig2_free(allocator, result);
  160|      0|        return NULL;
  161|      0|    }
  162|  6.85k|    {
  163|  6.85k|        uint32_t index;
  164|       |
  165|  34.2k|        for (index = 0; index < result->max_page_index; index++) {
  ------------------
  |  Branch (165:25): [True: 27.4k, False: 6.85k]
  ------------------
  166|  27.4k|            result->pages[index].state = JBIG2_PAGE_FREE;
  167|  27.4k|            result->pages[index].number = 0;
  168|  27.4k|            result->pages[index].width = 0;
  169|  27.4k|            result->pages[index].height = 0xffffffff;
  170|  27.4k|            result->pages[index].x_resolution = 0;
  171|  27.4k|            result->pages[index].y_resolution = 0;
  172|  27.4k|            result->pages[index].stripe_size = 0;
  173|  27.4k|            result->pages[index].striped = 0;
  174|  27.4k|            result->pages[index].end_row = 0;
  175|  27.4k|            result->pages[index].flags = 0;
  176|  27.4k|            result->pages[index].image = NULL;
  177|  27.4k|        }
  178|  6.85k|    }
  179|       |
  180|  6.85k|    return result;
  181|  6.85k|}
jbig2_get_int16:
  192|  8.84k|{
  193|  8.84k|    return get_int16(bptr);
  ------------------
  |  |  186|  8.84k|    (((int)get_uint16(bptr) ^ 0x8000) - 0x8000)
  |  |  ------------------
  |  |  |  |  184|  8.84k|    (((bptr)[0] << 8) | (bptr)[1])
  |  |  ------------------
  ------------------
  194|  8.84k|}
jbig2_get_uint16:
  200|  20.9k|{
  201|  20.9k|    return get_uint16(bptr);
  ------------------
  |  |  184|  20.9k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
  202|  20.9k|}
jbig2_get_int32:
  208|  8.08k|{
  209|  8.08k|    return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  186|  8.08k|    (((int)get_uint16(bptr) ^ 0x8000) - 0x8000)
  |  |  ------------------
  |  |  |  |  184|  8.08k|    (((bptr)[0] << 8) | (bptr)[1])
  |  |  ------------------
  ------------------
                  return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  184|  8.08k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
  210|  8.08k|}
jbig2_get_uint32:
  216|   409k|{
  217|   409k|    return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  184|   409k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
                  return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  184|   409k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
  218|   409k|}
jbig2_data_in:
  250|  6.85k|{
  251|  6.85k|    if (ctx->buf == NULL) {
  ------------------
  |  Branch (251:9): [True: 6.85k, False: 0]
  ------------------
  252|  6.85k|        size_t buf_size = jbig2_find_buffer_size(size);
  253|  6.85k|        ctx->buf = jbig2_new(ctx, byte, buf_size);
  ------------------
  |  |  141|  6.85k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  254|  6.85k|        if (ctx->buf == NULL) {
  ------------------
  |  Branch (254:13): [True: 0, False: 6.85k]
  ------------------
  255|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate buffer when reading data");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  256|      0|        }
  257|  6.85k|        ctx->buf_size = buf_size;
  258|  6.85k|        ctx->buf_rd_ix = 0;
  259|  6.85k|        ctx->buf_wr_ix = 0;
  260|  6.85k|    } else if (size > ctx->buf_size - ctx->buf_wr_ix) {
  ------------------
  |  Branch (260:16): [True: 0, False: 0]
  ------------------
  261|      0|        size_t already = ctx->buf_wr_ix - ctx->buf_rd_ix;
  262|       |
  263|      0|        if (ctx->buf_rd_ix <= (ctx->buf_size >> 1) && size <= ctx->buf_size - already) {
  ------------------
  |  Branch (263:13): [True: 0, False: 0]
  |  Branch (263:55): [True: 0, False: 0]
  ------------------
  264|      0|            memmove(ctx->buf, ctx->buf + ctx->buf_rd_ix, already);
  265|      0|        } else {
  266|      0|            byte *buf;
  267|      0|            size_t buf_size;
  268|       |
  269|      0|            if (already > SIZE_MAX - size) {
  ------------------
  |  Branch (269:17): [True: 0, False: 0]
  ------------------
  270|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "read data causes buffer to grow too large");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  271|      0|            }
  272|       |
  273|      0|            buf_size = jbig2_find_buffer_size(size + already);
  274|       |
  275|      0|            buf = jbig2_new(ctx, byte, buf_size);
  ------------------
  |  |  141|      0|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  276|      0|            if (buf == NULL) {
  ------------------
  |  Branch (276:17): [True: 0, False: 0]
  ------------------
  277|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate bigger buffer when reading data");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  278|      0|            }
  279|      0|            memcpy(buf, ctx->buf + ctx->buf_rd_ix, already);
  280|      0|            jbig2_free(ctx->allocator, ctx->buf);
  281|      0|            ctx->buf = buf;
  282|      0|            ctx->buf_size = buf_size;
  283|      0|        }
  284|      0|        ctx->buf_wr_ix -= ctx->buf_rd_ix;
  285|      0|        ctx->buf_rd_ix = 0;
  286|      0|    }
  287|       |
  288|  6.85k|    memcpy(ctx->buf + ctx->buf_wr_ix, data, size);
  289|  6.85k|    ctx->buf_wr_ix += size;
  290|       |
  291|       |    /* data has now been added to buffer */
  292|       |
  293|   141k|    for (;;) {
  294|   141k|        const byte jbig2_id_string[8] = { 0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a };
  295|   141k|        Jbig2Segment *segment;
  296|   141k|        size_t header_size;
  297|   141k|        int code;
  298|       |
  299|   141k|        switch (ctx->state) {
  ------------------
  |  Branch (299:17): [True: 141k, False: 0]
  ------------------
  300|      0|        case JBIG2_FILE_HEADER_MAYBE:
  ------------------
  |  Branch (300:9): [True: 0, False: 141k]
  ------------------
  301|       |            /* Accept either a file header, or an embedded file. */
  302|      0|            if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9)
  ------------------
  |  Branch (302:17): [True: 0, False: 0]
  ------------------
  303|      0|                return 0;
  304|      0|            if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8) == 0)
  ------------------
  |  Branch (304:17): [True: 0, False: 0]
  ------------------
  305|      0|            {
  306|      0|                ctx->state = JBIG2_FILE_HEADER;
  307|      0|                (void)jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "JBIG2 file header ignored in supposedly embedded stream");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  308|      0|            }
  309|      0|            else
  310|      0|                ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  311|      0|            break;
  312|  6.85k|        case JBIG2_FILE_HEADER:
  ------------------
  |  Branch (312:9): [True: 6.85k, False: 134k]
  ------------------
  313|       |            /* D.4.1 */
  314|  6.85k|            if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9)
  ------------------
  |  Branch (314:17): [True: 6, False: 6.84k]
  ------------------
  315|      6|                return 0;
  316|  6.84k|            if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8))
  ------------------
  |  Branch (316:17): [True: 63, False: 6.78k]
  ------------------
  317|     63|                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "not a JBIG2 file header");
  ------------------
  |  |   68|     63|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  318|       |            /* D.4.2 */
  319|  6.78k|            ctx->file_header_flags = ctx->buf[ctx->buf_rd_ix + 8];
  320|       |            /* Check for T.88 amendment 2 */
  321|  6.78k|            if (ctx->file_header_flags & 0x04)
  ------------------
  |  Branch (321:17): [True: 1, False: 6.78k]
  ------------------
  322|      1|                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates use of 12 adaptive template pixels (NYI)");
  ------------------
  |  |   68|      1|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  323|       |            /* Check for T.88 amendment 3 */
  324|  6.78k|            if (ctx->file_header_flags & 0x08)
  ------------------
  |  Branch (324:17): [True: 1, False: 6.78k]
  ------------------
  325|      1|                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates use of colored region segments (NYI)");
  ------------------
  |  |   68|      1|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  326|  6.78k|            if (ctx->file_header_flags & 0xFC) {
  ------------------
  |  Branch (326:17): [True: 1.56k, False: 5.21k]
  ------------------
  327|  1.56k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "reserved bits (2-7) of file header flags are not zero (0x%02x)", ctx->file_header_flags);
  ------------------
  |  |   68|  1.56k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  328|  1.56k|            }
  329|       |            /* D.4.3 */
  330|  6.78k|            if (!(ctx->file_header_flags & 2)) {        /* number of pages is known */
  ------------------
  |  Branch (330:17): [True: 2.22k, False: 4.56k]
  ------------------
  331|  2.22k|                if (ctx->buf_wr_ix - ctx->buf_rd_ix < 13)
  ------------------
  |  Branch (331:21): [True: 7, False: 2.21k]
  ------------------
  332|      7|                    return 0;
  333|  2.21k|                ctx->n_pages = jbig2_get_uint32(ctx->buf + ctx->buf_rd_ix + 9);
  334|  2.21k|                ctx->buf_rd_ix += 13;
  335|  2.21k|                if (ctx->n_pages == 1)
  ------------------
  |  Branch (335:21): [True: 39, False: 2.17k]
  ------------------
  336|     39|                    jbig2_error(ctx, JBIG2_SEVERITY_INFO, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates a single page document");
  ------------------
  |  |   68|     39|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  337|  2.17k|                else
  338|  2.17k|                    jbig2_error(ctx, JBIG2_SEVERITY_INFO, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates a %d page document", ctx->n_pages);
  ------------------
  |  |   68|  2.17k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  339|  4.56k|            } else {            /* number of pages not known */
  340|  4.56k|                ctx->n_pages = 0;
  341|  4.56k|                ctx->buf_rd_ix += 9;
  342|  4.56k|            }
  343|       |            /* determine the file organization based on the flags - D.4.2 again */
  344|  6.77k|            if (ctx->file_header_flags & 1) {
  ------------------
  |  Branch (344:17): [True: 5.90k, False: 873]
  ------------------
  345|  5.90k|                ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  346|  5.90k|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates sequential organization");
  ------------------
  |  |   68|  5.90k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  347|  5.90k|            } else {
  348|    873|                ctx->state = JBIG2_FILE_RANDOM_HEADERS;
  349|    873|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates random-access organization");
  ------------------
  |  |   68|    873|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  350|    873|            }
  351|  6.77k|            break;
  352|  53.8k|        case JBIG2_FILE_SEQUENTIAL_HEADER:
  ------------------
  |  Branch (352:9): [True: 53.8k, False: 87.5k]
  ------------------
  353|  80.9k|        case JBIG2_FILE_RANDOM_HEADERS:
  ------------------
  |  Branch (353:9): [True: 27.1k, False: 114k]
  ------------------
  354|  80.9k|            segment = jbig2_parse_segment_header(ctx, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix, &header_size);
  355|  80.9k|            if (segment == NULL)
  ------------------
  |  Branch (355:17): [True: 1.27k, False: 79.6k]
  ------------------
  356|  1.27k|                return 0; /* need more data */
  357|  79.6k|            ctx->buf_rd_ix += header_size;
  358|       |
  359|  79.6k|            if (ctx->n_segments >= ctx->n_segments_max) {
  ------------------
  |  Branch (359:17): [True: 612, False: 79.0k]
  ------------------
  360|    612|                Jbig2Segment **segments;
  361|       |
  362|    612|                if (ctx->n_segments_max == UINT32_MAX) {
  ------------------
  |  Branch (362:21): [True: 0, False: 612]
  ------------------
  363|      0|                    ctx->state = JBIG2_FILE_EOF;
  364|      0|                    jbig2_free_segment(ctx, segment);
  365|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "too many segments in jbig2 image");
  366|      0|                }
  367|    612|                else if (ctx->n_segments_max > (UINT32_MAX >> 2)) {
  ------------------
  |  Branch (367:26): [True: 0, False: 612]
  ------------------
  368|      0|                    ctx->n_segments_max = UINT32_MAX;
  369|    612|                } else {
  370|    612|                    ctx->n_segments_max <<= 2;
  371|    612|                }
  372|       |
  373|    612|                segments = jbig2_renew(ctx, ctx->segments, Jbig2Segment *, ctx->n_segments_max);
  ------------------
  |  |  143|    612|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
  374|    612|                if (segments == NULL) {
  ------------------
  |  Branch (374:21): [True: 2, False: 610]
  ------------------
  375|      2|                    ctx->state = JBIG2_FILE_EOF;
  376|      2|                    jbig2_free_segment(ctx, segment);
  377|      2|                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate space for more segments");
  378|      2|                }
  379|    610|                ctx->segments = segments;
  380|    610|            }
  381|       |
  382|  79.6k|            ctx->segments[ctx->n_segments++] = segment;
  383|  79.6k|            if (ctx->state == JBIG2_FILE_RANDOM_HEADERS) {
  ------------------
  |  Branch (383:17): [True: 26.2k, False: 53.3k]
  ------------------
  384|  26.2k|                if ((segment->flags & 63) == 51)        /* end of file */
  ------------------
  |  Branch (384:21): [True: 39, False: 26.2k]
  ------------------
  385|     39|                    ctx->state = JBIG2_FILE_RANDOM_BODIES;
  386|  26.2k|            } else              /* JBIG2_FILE_SEQUENTIAL_HEADER */
  387|  53.3k|                ctx->state = JBIG2_FILE_SEQUENTIAL_BODY;
  388|  79.6k|            break;
  389|  53.3k|        case JBIG2_FILE_SEQUENTIAL_BODY:
  ------------------
  |  Branch (389:9): [True: 53.3k, False: 88.0k]
  ------------------
  390|  53.6k|        case JBIG2_FILE_RANDOM_BODIES:
  ------------------
  |  Branch (390:9): [True: 228, False: 141k]
  ------------------
  391|  53.6k|            segment = ctx->segments[ctx->segment_index];
  392|       |
  393|       |            /* immediate generic regions may have unknown size */
  394|  53.6k|            if (segment->data_length == 0xffffffff && (segment->flags & 63) == 38) {
  ------------------
  |  Branch (394:17): [True: 18.7k, False: 34.8k]
  |  Branch (394:55): [True: 15.3k, False: 3.46k]
  ------------------
  395|  15.3k|                byte *s, *e, *p;
  396|  15.3k|                int mmr;
  397|  15.3k|                byte mmr_marker[2] = { 0x00, 0x00 };
  398|  15.3k|                byte arith_marker[2] = { 0xff, 0xac };
  399|  15.3k|                byte *desired_marker;
  400|       |
  401|  15.3k|                s = p = ctx->buf + ctx->buf_rd_ix;
  402|  15.3k|                e = ctx->buf + ctx->buf_wr_ix;
  403|       |
  404|  15.3k|                if (e - p < 18)
  ------------------
  |  Branch (404:21): [True: 38, False: 15.2k]
  ------------------
  405|     38|                        return 0; /* need more data */
  406|       |
  407|  15.2k|                mmr = p[17] & 1;
  408|  15.2k|                p += 18;
  409|  15.2k|                desired_marker = mmr ? mmr_marker : arith_marker;
  ------------------
  |  Branch (409:34): [True: 748, False: 14.5k]
  ------------------
  410|       |
  411|       |                /* look for two byte marker */
  412|  15.2k|                if (e - p < 2)
  ------------------
  |  Branch (412:21): [True: 50, False: 15.2k]
  ------------------
  413|     50|                    return 0; /* need more data */
  414|       |
  415|  1.38M|                while (p[0] != desired_marker[0] || p[1] != desired_marker[1]) {
  ------------------
  |  Branch (415:24): [True: 1.19M, False: 191k]
  |  Branch (415:53): [True: 176k, False: 14.7k]
  ------------------
  416|  1.37M|                    p++;
  417|  1.37M|                    if (e - p < 2)
  ------------------
  |  Branch (417:25): [True: 485, False: 1.36M]
  ------------------
  418|    485|                        return 0; /* need more data */
  419|  1.37M|                }
  420|  14.7k|                p += 2;
  421|       |
  422|       |                /* the marker is followed by a four byte row count */
  423|  14.7k|                if (e - p < 4)
  ------------------
  |  Branch (423:21): [True: 25, False: 14.7k]
  ------------------
  424|     25|                        return 0; /* need more data */
  425|  14.7k|                segment->rows = jbig2_get_uint32(p);
  426|  14.7k|                p += 4;
  427|       |
  428|  14.7k|                segment->data_length = (size_t) (p - s);
  429|  14.7k|                jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "unknown length determined to be %lu", (long) segment->data_length);
  430|  14.7k|            }
  431|  38.3k|            else if (segment->data_length > ctx->buf_wr_ix - ctx->buf_rd_ix)
  ------------------
  |  Branch (431:22): [True: 3.91k, False: 34.4k]
  ------------------
  432|  3.91k|                    return 0; /* need more data */
  433|       |
  434|  49.1k|            code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
  435|  49.1k|            ctx->buf_rd_ix += segment->data_length;
  436|  49.1k|            ctx->segment_index++;
  437|  49.1k|            if (ctx->state == JBIG2_FILE_RANDOM_BODIES) {
  ------------------
  |  Branch (437:17): [True: 204, False: 48.9k]
  ------------------
  438|    204|                if (ctx->segment_index == ctx->n_segments)
  ------------------
  |  Branch (438:21): [True: 0, False: 204]
  ------------------
  439|      0|                    ctx->state = JBIG2_FILE_EOF;
  440|  48.9k|            } else {            /* JBIG2_FILE_SEQUENTIAL_BODY */
  441|  48.9k|                ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  442|  48.9k|            }
  443|  49.1k|            if (code < 0) {
  ------------------
  |  Branch (443:17): [True: 992, False: 48.1k]
  ------------------
  444|    992|                ctx->state = JBIG2_FILE_EOF;
  445|    992|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode; treating as end of file");
  446|    992|            }
  447|  48.1k|            break;
  448|  48.1k|        case JBIG2_FILE_EOF:
  ------------------
  |  Branch (448:9): [True: 0, False: 141k]
  ------------------
  449|      0|            if (ctx->buf_rd_ix == ctx->buf_wr_ix)
  ------------------
  |  Branch (449:17): [True: 0, False: 0]
  ------------------
  450|      0|                return 0;
  451|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "garbage beyond end of file");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  452|   141k|        }
  453|   141k|    }
  454|  6.85k|}
jbig2_ctx_free:
  458|  6.85k|{
  459|  6.85k|    Jbig2Allocator *ca;
  460|  6.85k|    uint32_t i;
  461|       |
  462|  6.85k|    if (ctx == NULL)
  ------------------
  |  Branch (462:9): [True: 0, False: 6.85k]
  ------------------
  463|      0|        return NULL;
  464|       |
  465|  6.85k|    ca = ctx->allocator;
  466|  6.85k|    jbig2_free(ca, ctx->buf);
  467|  6.85k|    if (ctx->segments != NULL) {
  ------------------
  |  Branch (467:9): [True: 6.85k, False: 0]
  ------------------
  468|  86.5k|        for (i = 0; i < ctx->n_segments; i++)
  ------------------
  |  Branch (468:21): [True: 79.6k, False: 6.85k]
  ------------------
  469|  79.6k|            jbig2_free_segment(ctx, ctx->segments[i]);
  470|  6.85k|        jbig2_free(ca, ctx->segments);
  471|  6.85k|    }
  472|       |
  473|  6.85k|    if (ctx->pages != NULL) {
  ------------------
  |  Branch (473:9): [True: 6.85k, False: 0]
  ------------------
  474|  21.4k|        for (i = 0; i <= ctx->current_page; i++)
  ------------------
  |  Branch (474:21): [True: 14.5k, False: 6.85k]
  ------------------
  475|  14.5k|            if (ctx->pages[i].image != NULL)
  ------------------
  |  Branch (475:17): [True: 8.74k, False: 5.84k]
  ------------------
  476|  8.74k|                jbig2_image_release(ctx, ctx->pages[i].image);
  477|  6.85k|        jbig2_free(ca, ctx->pages);
  478|  6.85k|    }
  479|       |
  480|  6.85k|    jbig2_free(ca, ctx);
  481|       |
  482|  6.85k|    return ca;
  483|  6.85k|}
jbig2_word_stream_buf_new:
  545|  27.9k|{
  546|  27.9k|    Jbig2WordStreamBuf *result = jbig2_new(ctx, Jbig2WordStreamBuf, 1);
  ------------------
  |  |  141|  27.9k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  547|       |
  548|  27.9k|    if (result == NULL) {
  ------------------
  |  Branch (548:9): [True: 6, False: 27.9k]
  ------------------
  549|      6|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate word stream");
  ------------------
  |  |   68|      6|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  550|      6|        return NULL;
  551|      6|    }
  552|       |
  553|  27.9k|    result->super.get_next_word = jbig2_word_stream_buf_get_next_word;
  554|  27.9k|    result->data = data;
  555|  27.9k|    result->size = size;
  556|       |
  557|  27.9k|    return &result->super;
  558|  27.9k|}
jbig2_word_stream_buf_free:
  562|  28.3k|{
  563|  28.3k|    jbig2_free(ctx->allocator, ws);
  564|  28.3k|}
jbig2_free:
  577|  48.9M|{
  578|  48.9M|    allocator->free(allocator, p);
  579|  48.9M|}
jbig2_realloc:
  583|  8.13k|{
  584|       |    /* check for integer multiplication overflow */
  585|  8.13k|    if (num > 0 && size >= SIZE_MAX / num)
  ------------------
  |  Branch (585:9): [True: 8.13k, False: 0]
  |  Branch (585:20): [True: 0, False: 8.13k]
  ------------------
  586|      0|        return NULL;
  587|  8.13k|    return allocator->realloc(allocator, p, size * num);
  588|  8.13k|}
jbig2.c:jbig2_default_error:
   78|   171M|{
   79|   171M|    (void) data;
   80|       |
   81|       |    /* report only fatal errors by default */
   82|   171M|    if (severity == JBIG2_SEVERITY_FATAL) {
  ------------------
  |  Branch (82:9): [True: 5.95k, False: 171M]
  ------------------
   83|  5.95k|        fprintf(stderr, "jbig2 decoder FATAL ERROR: %s", msg);
   84|  5.95k|        if (seg_idx != JBIG2_UNKNOWN_SEGMENT_NUMBER)
  ------------------
  |  |   68|  5.95k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  |  Branch (84:13): [True: 1.74k, False: 4.21k]
  ------------------
   85|  1.74k|            fprintf(stderr, " (segment 0x%02x)", seg_idx);
   86|  5.95k|        fprintf(stderr, "\n");
   87|       |        fflush(stderr);
   88|  5.95k|    }
   89|   171M|}
jbig2.c:jbig2_find_buffer_size:
  222|  6.85k|{
  223|  6.85k|    const size_t initial_buf_size = 1024;
  224|  6.85k|    size_t size = initial_buf_size;
  225|       |
  226|  6.85k|    if (desired == SIZE_MAX)
  ------------------
  |  Branch (226:9): [True: 0, False: 6.85k]
  ------------------
  227|      0|        return SIZE_MAX;
  228|       |
  229|  10.3k|    while (size < desired)
  ------------------
  |  Branch (229:12): [True: 3.53k, False: 6.85k]
  ------------------
  230|  3.53k|        size <<= 1;
  231|       |
  232|  6.85k|    return size;
  233|  6.85k|}
jbig2.c:jbig2_word_stream_buf_get_next_word:
  510|  6.14M|{
  511|  6.14M|    Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self;
  512|  6.14M|    uint32_t val = 0;
  513|  6.14M|    int ret = 0;
  514|       |
  515|  6.14M|    if (self == NULL || word == NULL) {
  ------------------
  |  Branch (515:9): [True: 0, False: 6.14M]
  |  Branch (515:25): [True: 0, False: 6.14M]
  ------------------
  516|      0|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read next word of stream because stream or output missing");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  517|      0|    }
  518|  6.14M|    if (offset >= z->size) {
  ------------------
  |  Branch (518:9): [True: 5.76M, False: 380k]
  ------------------
  519|  5.76M|        *word = 0;
  520|  5.76M|        return 0;
  521|  5.76M|    }
  522|       |
  523|   380k|    if (offset < z->size) {
  ------------------
  |  Branch (523:9): [True: 380k, False: 0]
  ------------------
  524|   380k|        val = (uint32_t) z->data[offset] << 24;
  525|   380k|        ret++;
  526|   380k|    }
  527|   380k|    if (offset + 1 < z->size) {
  ------------------
  |  Branch (527:9): [True: 374k, False: 6.34k]
  ------------------
  528|   374k|        val |= (uint32_t) z->data[offset + 1] << 16;
  529|   374k|        ret++;
  530|   374k|    }
  531|   380k|    if (offset + 2 < z->size) {
  ------------------
  |  Branch (531:9): [True: 370k, False: 10.3k]
  ------------------
  532|   370k|        val |= (uint32_t) z->data[offset + 2] << 8;
  533|   370k|        ret++;
  534|   370k|    }
  535|   380k|    if (offset + 3 < z->size) {
  ------------------
  |  Branch (535:9): [True: 367k, False: 13.1k]
  ------------------
  536|   367k|        val |= z->data[offset + 3];
  537|   367k|        ret++;
  538|   367k|    }
  539|   380k|    *word = val;
  540|   380k|    return ret;
  541|  6.14M|}

jbig2_arith_new:
  191|  27.9k|{
  192|  27.9k|    Jbig2ArithState *result;
  193|  27.9k|    int ret;
  194|       |
  195|  27.9k|    result = jbig2_new(ctx, Jbig2ArithState, 1);
  ------------------
  |  |  141|  27.9k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  196|  27.9k|    if (result == NULL) {
  ------------------
  |  Branch (196:9): [True: 1, False: 27.9k]
  ------------------
  197|      1|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate arithmetic coding state");
  ------------------
  |  |   68|      1|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  198|      1|        return NULL;
  199|      1|    }
  200|       |
  201|  27.9k|    result->err = 0;
  202|  27.9k|    result->ws = ws;
  203|  27.9k|    result->offset = 0;
  204|       |
  205|  27.9k|    ret = result->ws->get_next_word(ctx, result->ws, result->offset, &result->next_word);
  206|  27.9k|    if (ret < 0) {
  ------------------
  |  Branch (206:9): [True: 0, False: 27.9k]
  ------------------
  207|      0|        jbig2_free(ctx->allocator, result);
  208|      0|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to initialize underlying stream of arithmetic decoder");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  209|      0|        return NULL;
  210|      0|    }
  211|       |
  212|  27.9k|    result->next_word_bytes = (size_t) ret;
  213|  27.9k|    if (result->next_word_bytes == 0) {
  ------------------
  |  Branch (213:9): [True: 30, False: 27.8k]
  ------------------
  214|     30|        jbig2_free(ctx->allocator, result);
  215|     30|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read first byte from underlying stream when initializing arithmetic decoder");
  ------------------
  |  |   68|     30|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  216|     30|        return NULL;
  217|     30|    }
  218|       |
  219|  27.8k|    result->offset += result->next_word_bytes;
  220|       |
  221|       |    /* Figure F.1 */
  222|  27.8k|    result->C = (~(result->next_word >> 8)) & 0xFF0000;
  223|       |
  224|       |    /* Figure E.20 (2) */
  225|  27.8k|    if (jbig2_arith_bytein(ctx, result) < 0) {
  ------------------
  |  Branch (225:9): [True: 0, False: 27.8k]
  ------------------
  226|      0|        jbig2_free(ctx->allocator, result);
  227|      0|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read second byte from underlying stream when initializing arithmetic decoder");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  228|      0|        return NULL;
  229|      0|    }
  230|       |
  231|       |    /* Figure E.20 (3) */
  232|  27.8k|    result->C <<= 7;
  233|  27.8k|    result->CT -= 7;
  234|  27.8k|    result->A = 0x8000;
  235|       |
  236|  27.8k|    return result;
  237|  27.8k|}
jbig2_arith_decode:
  319|  14.1G|{
  320|  14.1G|    Jbig2ArithCx cx = *pcx;
  321|  14.1G|    const Jbig2ArithQe *pqe;
  322|  14.1G|    unsigned int index = cx & 0x7f;
  323|  14.1G|    bool D;
  ------------------
  |  |   45|  14.1G|#define bool int
  ------------------
  324|       |
  325|  14.1G|    if (index >= MAX_QE_ARRAY_SIZE) {
  ------------------
  |  |  239|  14.1G|#define MAX_QE_ARRAY_SIZE 47
  ------------------
  |  Branch (325:9): [True: 0, False: 14.1G]
  ------------------
  326|      0|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to determine probability estimate because index out of range");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  327|      0|    }
  328|       |
  329|  14.1G|    pqe = &jbig2_arith_Qe[index];
  330|       |
  331|       |    /* Figure F.2 */
  332|  14.1G|    as->A -= pqe->Qe;
  333|  14.1G|    if ((as->C >> 16) < as->A) {
  ------------------
  |  Branch (333:9): [True: 13.9G, False: 201M]
  ------------------
  334|  13.9G|        if ((as->A & 0x8000) == 0) {
  ------------------
  |  Branch (334:13): [True: 241M, False: 13.6G]
  ------------------
  335|       |            /* MPS_EXCHANGE, Figure E.16 */
  336|   241M|            if (as->A < pqe->Qe) {
  ------------------
  |  Branch (336:17): [True: 38.9M, False: 202M]
  ------------------
  337|  38.9M|                D = 1 - (cx >> 7);
  338|  38.9M|                *pcx ^= pqe->lps_xor;
  339|   202M|            } else {
  340|   202M|                D = cx >> 7;
  341|   202M|                *pcx ^= pqe->mps_xor;
  342|   202M|            }
  343|   241M|            if (jbig2_arith_renormd(ctx, as) < 0) {
  ------------------
  |  Branch (343:17): [True: 0, False: 241M]
  ------------------
  344|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to renormalize decoder");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  345|      0|            }
  346|       |
  347|   241M|            return D;
  348|  13.6G|        } else {
  349|  13.6G|            return cx >> 7;
  350|  13.6G|        }
  351|  13.9G|    } else {
  352|   201M|        as->C -= (as->A) << 16;
  353|       |        /* LPS_EXCHANGE, Figure E.17 */
  354|   201M|        if (as->A < pqe->Qe) {
  ------------------
  |  Branch (354:13): [True: 41.0M, False: 160M]
  ------------------
  355|  41.0M|            as->A = pqe->Qe;
  356|  41.0M|            D = cx >> 7;
  357|  41.0M|            *pcx ^= pqe->mps_xor;
  358|   160M|        } else {
  359|   160M|            as->A = pqe->Qe;
  360|   160M|            D = 1 - (cx >> 7);
  361|   160M|            *pcx ^= pqe->lps_xor;
  362|   160M|        }
  363|   201M|        if (jbig2_arith_renormd(ctx, as) < 0) {
  ------------------
  |  Branch (363:13): [True: 0, False: 201M]
  ------------------
  364|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to renormalize decoder");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  365|      0|        }
  366|       |
  367|   201M|        return D;
  368|   201M|    }
  369|  14.1G|}
jbig2_arith.c:jbig2_arith_bytein:
   59|  72.1M|{
   60|  72.1M|    byte B;
   61|       |
   62|       |    /* Treat both errors and reading beyond end of stream as an error. */
   63|  72.1M|    if (as->err != 0) {
  ------------------
  |  Branch (63:9): [True: 0, False: 72.1M]
  ------------------
   64|      0|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read from underlying stream during arithmetic decoding");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   65|      0|        return -1;
   66|      0|    }
   67|  72.1M|    if (as->next_word_bytes == 0) {
  ------------------
  |  Branch (67:9): [True: 0, False: 72.1M]
  ------------------
   68|      0|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read beyond end of underlying stream during arithmetic decoding");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   69|      0|        return -1;
   70|      0|    }
   71|       |
   72|       |    /* At this point there is at least one byte in as->next_word. */
   73|       |
   74|       |    /* This code confused me no end when I first read it, so a quick note
   75|       |     * to save others (and future me's) from being similarly confused.
   76|       |     * 'next_word' does indeed contain 'next_word_bytes' of valid data
   77|       |     * (always starting at the most significant byte). The confusing
   78|       |     * thing is that the first byte has always already been read.
   79|       |     * i.e. it serves only as an indication that the last byte we read
   80|       |     * was FF or not.
   81|       |     *
   82|       |     * The jbig2 bytestream uses FF bytes, followed by a byte > 0x8F as
   83|       |     * marker bytes. These never occur in normal streams of arithmetic
   84|       |     * encoding, so meeting one terminates the stream (with an infinite
   85|       |     * series of 1 bits).
   86|       |     *
   87|       |     * If we meet an FF byte, we return it as normal. We just 'remember'
   88|       |     * that fact for the next byte we read.
   89|       |     */
   90|       |
   91|       |    /* Figure F.3 */
   92|  72.1M|    B = (byte)((as->next_word >> 24) & 0xFF);
   93|  72.1M|    if (B == 0xFF) {
  ------------------
  |  Branch (93:9): [True: 71.4M, False: 722k]
  ------------------
   94|  71.4M|        byte B1;
   95|       |
   96|       |        /* next_word_bytes can only be == 1 here, but let's be defensive. */
   97|  71.4M|        if (as->next_word_bytes <= 1) {
  ------------------
  |  Branch (97:13): [True: 7.14k, False: 71.4M]
  ------------------
   98|  7.14k|            int ret = as->ws->get_next_word(ctx, as->ws, as->offset, &as->next_word);
   99|  7.14k|            if (ret < 0) {
  ------------------
  |  Branch (99:17): [True: 0, False: 7.14k]
  ------------------
  100|      0|                as->err = 1;
  101|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to check for marker code due to failure in underlying stream during arithmetic decoding");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  102|      0|            }
  103|  7.14k|            as->next_word_bytes = (size_t) ret;
  104|       |
  105|  7.14k|            if (as->next_word_bytes == 0) {
  ------------------
  |  Branch (105:17): [True: 364, False: 6.78k]
  ------------------
  106|    364|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read end of possible terminating marker code, assuming terminating marker code");
  ------------------
  |  |   68|    364|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  107|    364|                as->next_word = 0xFF900000;
  108|    364|                as->next_word_bytes = 2;
  109|    364|                as->C += 0xFF00;
  110|    364|                as->CT = 8;
  111|    364|                return 0;
  112|    364|            }
  113|       |
  114|  6.78k|            as->offset += as->next_word_bytes;
  115|       |
  116|  6.78k|            B1 = (byte)((as->next_word >> 24) & 0xFF);
  117|  6.78k|            if (B1 > 0x8F) {
  ------------------
  |  Branch (117:17): [True: 4.71k, False: 2.07k]
  ------------------
  118|       |#ifdef JBIG2_DEBUG_ARITH
  119|       |                fprintf(stderr, "read %02x (aa)\n", B);
  120|       |#endif
  121|  4.71k|                as->CT = 8;
  122|  4.71k|                as->next_word = 0xFF000000 | (as->next_word >> 8);
  123|  4.71k|                as->next_word_bytes = 2;
  124|  4.71k|                as->offset--;
  125|  4.71k|            } else {
  126|       |#ifdef JBIG2_DEBUG_ARITH
  127|       |                fprintf(stderr, "read %02x (a)\n", B);
  128|       |#endif
  129|  2.07k|                as->C += 0xFE00 - (B1 << 9);
  130|  2.07k|                as->CT = 7;
  131|  2.07k|            }
  132|  71.4M|        } else {
  133|  71.4M|            B1 = (byte)((as->next_word >> 16) & 0xFF);
  134|  71.4M|            if (B1 > 0x8F) {
  ------------------
  |  Branch (134:17): [True: 71.4M, False: 4.88k]
  ------------------
  135|       |#ifdef JBIG2_DEBUG_ARITH
  136|       |                fprintf(stderr, "read %02x (ba)\n", B);
  137|       |#endif
  138|  71.4M|                as->CT = 8;
  139|  71.4M|            } else {
  140|  4.88k|                as->next_word_bytes--;
  141|  4.88k|                as->next_word <<= 8;
  142|       |#ifdef JBIG2_DEBUG_ARITH
  143|       |                fprintf(stderr, "read %02x (b)\n", B);
  144|       |#endif
  145|       |
  146|  4.88k|                as->C += 0xFE00 - (B1 << 9);
  147|  4.88k|                as->CT = 7;
  148|  4.88k|            }
  149|  71.4M|        }
  150|  71.4M|    } else {
  151|       |#ifdef JBIG2_DEBUG_ARITH
  152|       |        fprintf(stderr, "read %02x\n", B);
  153|       |#endif
  154|   722k|        as->next_word <<= 8;
  155|   722k|        as->next_word_bytes--;
  156|       |
  157|   722k|        if (as->next_word_bytes == 0) {
  ------------------
  |  Branch (157:13): [True: 173k, False: 548k]
  ------------------
  158|   173k|            int ret = as->ws->get_next_word(ctx, as->ws, as->offset, &as->next_word);
  159|   173k|            if (ret < 0) {
  ------------------
  |  Branch (159:17): [True: 0, False: 173k]
  ------------------
  160|      0|                as->err = 1;
  161|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read from underlying stream during arithmetic decoding");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  162|      0|            }
  163|   173k|            as->next_word_bytes = (size_t) ret;
  164|       |
  165|   173k|            if (as->next_word_bytes == 0) {
  ------------------
  |  Branch (165:17): [True: 5.17k, False: 168k]
  ------------------
  166|  5.17k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to find terminating marker code before end of underlying stream, assuming terminating marker code");
  ------------------
  |  |   68|  5.17k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  167|  5.17k|                as->next_word = 0xFF900000;
  168|  5.17k|                as->next_word_bytes = 2;
  169|  5.17k|                as->C += 0xFF00;
  170|  5.17k|                as->CT = 8;
  171|  5.17k|                return 0;
  172|  5.17k|            }
  173|       |
  174|   168k|            as->offset += as->next_word_bytes;
  175|   168k|        }
  176|       |
  177|   717k|        B = (byte)((as->next_word >> 24) & 0xFF);
  178|   717k|        as->C += 0xFF00 - (B << 8);
  179|   717k|        as->CT = 8;
  180|   717k|    }
  181|       |
  182|  72.1M|    return 0;
  183|  72.1M|}
jbig2_arith.c:jbig2_arith_renormd:
  303|   442M|{
  304|       |    /* Figure E.18 */
  305|   576M|    do {
  306|   576M|        if (as->CT == 0 && jbig2_arith_bytein(ctx, as) < 0) {
  ------------------
  |  Branch (306:13): [True: 72.1M, False: 504M]
  |  Branch (306:28): [True: 0, False: 72.1M]
  ------------------
  307|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read byte from compressed data stream");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  308|      0|        }
  309|   576M|        as->A <<= 1;
  310|   576M|        as->C <<= 1;
  311|   576M|        as->CT--;
  312|   576M|    } while ((as->A & 0x8000) == 0);
  ------------------
  |  Branch (312:14): [True: 134M, False: 442M]
  ------------------
  313|       |
  314|   442M|    return 0;
  315|   442M|}

jbig2_arith_iaid_ctx_new:
   46|  5.98k|{
   47|  5.98k|    Jbig2ArithIaidCtx *result;
   48|  5.98k|    size_t ctx_size;
   49|       |
   50|  5.98k|    if (SBSYMCODELEN > 31 || sizeof(ctx_size) * 8 <= SBSYMCODELEN)
  ------------------
  |  Branch (50:9): [True: 9, False: 5.97k]
  |  Branch (50:30): [True: 0, False: 5.97k]
  ------------------
   51|      9|    {
   52|      9|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "requested IAID arithmetic coding state size too large");
  ------------------
  |  |   68|      9|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   53|      9|        return NULL;
   54|      9|    }
   55|       |
   56|  5.97k|    ctx_size = (size_t) 1U << SBSYMCODELEN;
   57|       |
   58|  5.97k|    result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1);
  ------------------
  |  |  141|  5.97k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   59|  5.97k|    if (result == NULL) {
  ------------------
  |  Branch (59:9): [True: 1, False: 5.97k]
  ------------------
   60|      1|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate IAID arithmetic coding state");
  ------------------
  |  |   68|      1|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   61|      1|        return NULL;
   62|      1|    }
   63|       |
   64|  5.97k|    result->SBSYMCODELEN = SBSYMCODELEN;
   65|  5.97k|    result->IAIDx = jbig2_new(ctx, Jbig2ArithCx, ctx_size);
  ------------------
  |  |  141|  5.97k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   66|  5.97k|    if (result->IAIDx == NULL)
  ------------------
  |  Branch (66:9): [True: 16, False: 5.95k]
  ------------------
   67|     16|    {
   68|     16|        jbig2_free(ctx->allocator, result);
   69|     16|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate symbol ID in IAID arithmetic coding state");
  ------------------
  |  |   68|     16|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   70|     16|        return NULL;
   71|     16|    }
   72|       |
   73|  5.95k|    memset(result->IAIDx, 0, ctx_size);
   74|  5.95k|    return result;
   75|  5.97k|}
jbig2_arith_iaid_decode:
   81|  9.97M|{
   82|  9.97M|    Jbig2ArithCx *IAIDx = actx->IAIDx;
   83|  9.97M|    uint8_t SBSYMCODELEN = actx->SBSYMCODELEN;
   84|       |    /* A.3 (1) */
   85|  9.97M|    uint32_t PREV = 1;
   86|  9.97M|    int D;
   87|  9.97M|    int i;
   88|       |
   89|       |    /* A.3 (2) */
   90|  30.2M|    for (i = 0; i < SBSYMCODELEN; i++) {
  ------------------
  |  Branch (90:17): [True: 20.2M, False: 9.97M]
  ------------------
   91|  20.2M|        D = jbig2_arith_decode(ctx, as, &IAIDx[PREV]);
   92|  20.2M|        if (D < 0)
  ------------------
  |  Branch (92:13): [True: 0, False: 20.2M]
  ------------------
   93|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAIDx code");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   94|       |#ifdef VERBOSE
   95|       |        fprintf(stderr, "IAID%x: D = %d\n", PREV, D);
   96|       |#endif
   97|  20.2M|        PREV = (PREV << 1) | (uint32_t)D;
   98|  20.2M|    }
   99|       |    /* A.3 (3) */
  100|  9.97M|    PREV -= (1U << SBSYMCODELEN);
  101|       |#ifdef VERBOSE
  102|       |    fprintf(stderr, "IAID result: %d\n", PREV);
  103|       |#endif
  104|  9.97M|    *p_result = (int)PREV;
  105|  9.97M|    return 0;
  106|  9.97M|}
jbig2_arith_iaid_ctx_free:
  110|  5.98k|{
  111|  5.98k|    if (iax != NULL) {
  ------------------
  |  Branch (111:9): [True: 5.95k, False: 30]
  ------------------
  112|  5.95k|        jbig2_free(ctx->allocator, iax->IAIDx);
  113|  5.95k|        jbig2_free(ctx->allocator, iax);
  114|  5.95k|    }
  115|  5.98k|}

jbig2_arith_int_ctx_new:
   41|  48.8k|{
   42|  48.8k|    Jbig2ArithIntCtx *result = jbig2_new(ctx, Jbig2ArithIntCtx, 1);
  ------------------
  |  |  141|  48.8k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   43|       |
   44|  48.8k|    if (result == NULL) {
  ------------------
  |  Branch (44:9): [True: 84, False: 48.7k]
  ------------------
   45|     84|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate arithmetic integer coding state");
  ------------------
  |  |   68|     84|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   46|     84|        return NULL;
   47|  48.7k|    } else {
   48|  48.7k|        memset(result->IAx, 0, sizeof(result->IAx));
   49|  48.7k|    }
   50|       |
   51|  48.7k|    return result;
   52|  48.8k|}
jbig2_arith_int_decode:
   58|  16.4M|{
   59|  16.4M|    Jbig2ArithCx *IAx = actx->IAx;
   60|  16.4M|    int PREV = 1;
   61|  16.4M|    int S;
   62|  16.4M|    int32_t V;
   63|  16.4M|    int bit;
   64|  16.4M|    int n_tail, offset;
   65|  16.4M|    int i;
   66|       |
   67|  16.4M|    S = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   68|  16.4M|    if (S < 0)
  ------------------
  |  Branch (68:9): [True: 0, False: 16.4M]
  ------------------
   69|      0|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx S");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   70|  16.4M|    PREV = (PREV << 1) | S;
   71|       |
   72|  16.4M|    bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   73|  16.4M|    if (bit < 0)
  ------------------
  |  Branch (73:9): [True: 0, False: 16.4M]
  ------------------
   74|      0|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 0");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   75|  16.4M|    PREV = (PREV << 1) | bit;
   76|  16.4M|    if (bit) {
  ------------------
  |  Branch (76:9): [True: 10.2M, False: 6.13M]
  ------------------
   77|  10.2M|        bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   78|  10.2M|        if (bit < 0)
  ------------------
  |  Branch (78:13): [True: 0, False: 10.2M]
  ------------------
   79|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 1");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   80|  10.2M|        PREV = (PREV << 1) | bit;
   81|       |
   82|  10.2M|        if (bit) {
  ------------------
  |  Branch (82:13): [True: 5.05M, False: 5.22M]
  ------------------
   83|  5.05M|            bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   84|  5.05M|            if (bit < 0)
  ------------------
  |  Branch (84:17): [True: 0, False: 5.05M]
  ------------------
   85|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 2");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   86|  5.05M|            PREV = (PREV << 1) | bit;
   87|       |
   88|  5.05M|            if (bit) {
  ------------------
  |  Branch (88:17): [True: 4.55M, False: 502k]
  ------------------
   89|  4.55M|                bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   90|  4.55M|                if (bit < 0)
  ------------------
  |  Branch (90:21): [True: 0, False: 4.55M]
  ------------------
   91|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 3");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   92|  4.55M|                PREV = (PREV << 1) | bit;
   93|       |
   94|  4.55M|                if (bit) {
  ------------------
  |  Branch (94:21): [True: 1.90M, False: 2.65M]
  ------------------
   95|  1.90M|                    bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   96|  1.90M|                    if (bit < 0)
  ------------------
  |  Branch (96:25): [True: 0, False: 1.90M]
  ------------------
   97|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 4");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   98|  1.90M|                    PREV = (PREV << 1) | bit;
   99|       |
  100|  1.90M|                    if (bit) {
  ------------------
  |  Branch (100:25): [True: 232k, False: 1.66M]
  ------------------
  101|   232k|                        n_tail = 32;
  102|   232k|                        offset = 4436;
  103|  1.66M|                    } else {
  104|  1.66M|                        n_tail = 12;
  105|  1.66M|                        offset = 340;
  106|  1.66M|                    }
  107|  2.65M|                } else {
  108|  2.65M|                    n_tail = 8;
  109|  2.65M|                    offset = 84;
  110|  2.65M|                }
  111|  4.55M|            } else {
  112|   502k|                n_tail = 6;
  113|   502k|                offset = 20;
  114|   502k|            }
  115|  5.22M|        } else {
  116|  5.22M|            n_tail = 4;
  117|  5.22M|            offset = 4;
  118|  5.22M|        }
  119|  10.2M|    } else {
  120|  6.13M|        n_tail = 2;
  121|  6.13M|        offset = 0;
  122|  6.13M|    }
  123|       |
  124|  16.4M|    V = 0;
  125|   101M|    for (i = 0; i < n_tail; i++) {
  ------------------
  |  Branch (125:17): [True: 84.8M, False: 16.4M]
  ------------------
  126|  84.8M|        bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
  127|  84.8M|        if (bit < 0)
  ------------------
  |  Branch (127:13): [True: 0, False: 84.8M]
  ------------------
  128|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx V bit %d", i);
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  129|  84.8M|        PREV = ((PREV << 1) & 511) | (PREV & 256) | bit;
  130|  84.8M|        V = (V << 1) | bit;
  131|  84.8M|    }
  132|       |
  133|       |    /* offset is always >=0, so underflow can't happen. */
  134|       |    /* avoid overflow by clamping 32 bit value. */
  135|  16.4M|    if (V > INT32_MAX - offset)
  ------------------
  |  Branch (135:9): [True: 251, False: 16.4M]
  ------------------
  136|    251|        V = INT32_MAX;
  137|  16.4M|    else
  138|  16.4M|        V += offset;
  139|  16.4M|    V = S ? -V : V;
  ------------------
  |  Branch (139:9): [True: 6.06M, False: 10.3M]
  ------------------
  140|  16.4M|    *p_result = V;
  141|  16.4M|    return S && V == 0 ? 1 : 0;
  ------------------
  |  Branch (141:12): [True: 6.06M, False: 10.3M]
  |  Branch (141:17): [True: 1.51M, False: 4.54M]
  ------------------
  142|  16.4M|}
jbig2_arith_int_ctx_free:
  146|  64.5k|{
  147|  64.5k|    jbig2_free(ctx->allocator, iax);
  148|  64.5k|}

jbig2_generic_stats_size:
  360|  17.5k|{
  361|  17.5k|    int stats_size = template == 0 ? 1 << 16 : template == 1 ? 1 << 13 : 1 << 10;
  ------------------
  |  Branch (361:22): [True: 8.88k, False: 8.69k]
  |  Branch (361:48): [True: 2.13k, False: 6.55k]
  ------------------
  362|       |
  363|  17.5k|    (void) ctx;
  364|       |
  365|  17.5k|    return stats_size;
  366|  17.5k|}
jbig2_decode_generic_region:
 1372|   181k|{
 1373|   181k|    const int8_t *gbat = params->gbat;
 1374|       |
 1375|   181k|    if (!params->MMR && params->TPGDON)
  ------------------
  |  Branch (1375:9): [True: 181k, False: 0]
  |  Branch (1375:25): [True: 16.5k, False: 165k]
  ------------------
 1376|  16.5k|        return jbig2_decode_generic_region_TPGDON(ctx, segment, params, as, image, GB_stats);
 1377|       |
 1378|   165k|    if (!params->MMR && params->GBTEMPLATE == 0) {
  ------------------
  |  Branch (1378:9): [True: 165k, False: 0]
  |  Branch (1378:25): [True: 21.6k, False: 143k]
  ------------------
 1379|  21.6k|        if (!params->USESKIP && gbat[0] == +3 && gbat[1] == -1 && gbat[2] == -3 && gbat[3] == -1 && gbat[4] == +2 && gbat[5] == -2 && gbat[6] == -2 && gbat[7] == -2)
  ------------------
  |  Branch (1379:13): [True: 21.1k, False: 460]
  |  Branch (1379:33): [True: 18.6k, False: 2.46k]
  |  Branch (1379:50): [True: 16.3k, False: 2.30k]
  |  Branch (1379:67): [True: 14.3k, False: 2.07k]
  |  Branch (1379:84): [True: 12.6k, False: 1.62k]
  |  Branch (1379:101): [True: 12.3k, False: 346]
  |  Branch (1379:118): [True: 10.4k, False: 1.89k]
  |  Branch (1379:135): [True: 7.51k, False: 2.92k]
  |  Branch (1379:152): [True: 7.20k, False: 313]
  ------------------
 1380|  7.20k|            return jbig2_decode_generic_template0(ctx, segment, params, as, image, GB_stats);
 1381|  14.4k|        else
 1382|  14.4k|            return jbig2_decode_generic_template0_unopt(ctx, segment, params, as, image, GB_stats);
 1383|   143k|    } else if (!params->MMR && params->GBTEMPLATE == 1) {
  ------------------
  |  Branch (1383:16): [True: 143k, False: 0]
  |  Branch (1383:32): [True: 11.4k, False: 132k]
  ------------------
 1384|  11.4k|        if (!params->USESKIP && gbat[0] == +3 && gbat[1] == -1)
  ------------------
  |  Branch (1384:13): [True: 10.5k, False: 885]
  |  Branch (1384:33): [True: 5.75k, False: 4.77k]
  |  Branch (1384:50): [True: 5.43k, False: 318]
  ------------------
 1385|  5.43k|            return jbig2_decode_generic_template1(ctx, segment, params, as, image, GB_stats);
 1386|  5.97k|        else
 1387|  5.97k|            return jbig2_decode_generic_template1_unopt(ctx, segment, params, as, image, GB_stats);
 1388|  11.4k|    }
 1389|   132k|    else if (!params->MMR && params->GBTEMPLATE == 2) {
  ------------------
  |  Branch (1389:14): [True: 132k, False: 0]
  |  Branch (1389:30): [True: 120k, False: 11.5k]
  ------------------
 1390|   120k|        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
  ------------------
  |  Branch (1390:13): [True: 119k, False: 1.04k]
  |  Branch (1390:33): [True: 83.9k, False: 35.7k]
  |  Branch (1390:49): [True: 81.9k, False: 2.02k]
  ------------------
 1391|  81.9k|            return jbig2_decode_generic_template2(ctx, segment, params, as, image, GB_stats);
 1392|  38.7k|        else
 1393|  38.7k|            return jbig2_decode_generic_template2_unopt(ctx, segment, params, as, image, GB_stats);
 1394|   120k|    } else if (!params->MMR && params->GBTEMPLATE == 3) {
  ------------------
  |  Branch (1394:16): [True: 11.5k, False: 0]
  |  Branch (1394:32): [True: 11.5k, False: 0]
  ------------------
 1395|  11.5k|        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
  ------------------
  |  Branch (1395:13): [True: 11.2k, False: 264]
  |  Branch (1395:33): [True: 5.24k, False: 6.05k]
  |  Branch (1395:49): [True: 4.73k, False: 505]
  ------------------
 1396|  4.73k|            return jbig2_decode_generic_template3(ctx, segment, params, as, image, GB_stats);
 1397|  6.82k|        else
 1398|  6.82k|            return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats);
 1399|  11.5k|    }
 1400|       |
 1401|      0|    {
 1402|      0|        int i;
 1403|       |
 1404|      0|        for (i = 0; i < 8; i++)
  ------------------
  |  Branch (1404:21): [True: 0, False: 0]
  ------------------
 1405|      0|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "gbat[%d] = %d", i, params->gbat[i]);
 1406|      0|    }
 1407|       |
 1408|      0|    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported generic region (MMR=%d, GBTEMPLATE=%d)", params->MMR, params->GBTEMPLATE);
 1409|   165k|}
jbig2_immediate_generic_region:
 1416|  21.1k|{
 1417|  21.1k|    Jbig2RegionSegmentInfo rsi;
 1418|  21.1k|    byte seg_flags;
 1419|  21.1k|    int8_t gbat[8];
 1420|  21.1k|    int offset;
 1421|  21.1k|    uint32_t gbat_bytes = 0;
 1422|  21.1k|    Jbig2GenericRegionParams params;
 1423|  21.1k|    int code = 0;
 1424|  21.1k|    Jbig2Image *image = NULL;
 1425|  21.1k|    Jbig2WordStream *ws = NULL;
 1426|  21.1k|    Jbig2ArithState *as = NULL;
 1427|  21.1k|    Jbig2ArithCx *GB_stats = NULL;
 1428|  21.1k|    uint32_t height;
 1429|  21.1k|    Jbig2Page *page = &ctx->pages[ctx->current_page];
 1430|       |
 1431|       |    /* 7.4.6 */
 1432|  21.1k|    if (segment->data_length < 18)
  ------------------
  |  Branch (1432:9): [True: 46, False: 21.0k]
  ------------------
 1433|     46|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
 1434|       |
 1435|  21.0k|    jbig2_get_region_segment_info(&rsi, segment_data);
 1436|  21.0k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "generic region: %u x %u @ (%u, %u), flags = %02x", rsi.width, rsi.height, rsi.x, rsi.y, rsi.flags);
 1437|       |
 1438|       |    /* 7.4.6.4 */
 1439|  21.0k|    height = rsi.height;
 1440|  21.0k|    if (segment->rows != UINT32_MAX) {
  ------------------
  |  Branch (1440:9): [True: 14.6k, False: 6.39k]
  ------------------
 1441|  14.6k|        if (segment->rows > rsi.height)
  ------------------
  |  Branch (1441:13): [True: 47, False: 14.6k]
  ------------------
 1442|     47|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment contains more rows than stated in header");
 1443|  14.6k|        height = segment->rows;
 1444|  14.6k|    }
 1445|       |
 1446|       |    /* 7.4.6.2 */
 1447|  21.0k|    seg_flags = segment_data[17];
 1448|  21.0k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "segment flags = %02x", seg_flags);
 1449|  21.0k|    if ((seg_flags & 1) && (seg_flags & 6))
  ------------------
  |  Branch (1449:9): [True: 1.00k, False: 20.0k]
  |  Branch (1449:28): [True: 443, False: 565]
  ------------------
 1450|    443|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "MMR is 1, but GBTEMPLATE is not 0");
 1451|       |
 1452|       |    /* 7.4.6.3 */
 1453|  21.0k|    if (!(seg_flags & 1)) {
  ------------------
  |  Branch (1453:9): [True: 20.0k, False: 1.00k]
  ------------------
 1454|  20.0k|        gbat_bytes = (seg_flags & 6) ? 2 : 8;
  ------------------
  |  Branch (1454:22): [True: 10.4k, False: 9.60k]
  ------------------
 1455|  20.0k|        if (18 + gbat_bytes > segment->data_length)
  ------------------
  |  Branch (1455:13): [True: 12, False: 20.0k]
  ------------------
 1456|     12|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
 1457|  20.0k|        memcpy(gbat, segment_data + 18, gbat_bytes);
 1458|  20.0k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "gbat: %d, %d", gbat[0], gbat[1]);
 1459|  20.0k|    }
 1460|       |
 1461|  21.0k|    offset = 18 + gbat_bytes;
 1462|       |
 1463|       |    /* Check for T.88 amendment 2 */
 1464|  21.0k|    if ((seg_flags >> 5) & 1)
  ------------------
  |  Branch (1464:9): [True: 32, False: 21.0k]
  ------------------
 1465|     32|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment uses 12 adaptive template pixels (NYI)");
 1466|       |
 1467|       |    /* Table 34 */
 1468|  21.0k|    params.MMR = seg_flags & 1;
 1469|  21.0k|    params.GBTEMPLATE = (seg_flags & 6) >> 1;
 1470|  21.0k|    params.TPGDON = (seg_flags & 8) >> 3;
 1471|  21.0k|    params.USESKIP = 0;
 1472|  21.0k|    memcpy(params.gbat, gbat, gbat_bytes);
 1473|       |
 1474|  21.0k|    if (page->height == 0xffffffff && page->striped && page->stripe_size > 0) {
  ------------------
  |  Branch (1474:9): [True: 13.7k, False: 7.20k]
  |  Branch (1474:39): [True: 12.4k, False: 1.33k]
  |  Branch (1474:56): [True: 12.4k, False: 0]
  ------------------
 1475|  12.4k|        if (rsi.y >= page->end_row + page->stripe_size) {
  ------------------
  |  Branch (1475:13): [True: 2.40k, False: 10.0k]
  ------------------
 1476|  2.40k|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "ignoring %u x %u region at (%u, %u) outside of stripe at row %u covering %u rows, on page of height %u", rsi.width, rsi.height, rsi.x, rsi.y, page->end_row, page->stripe_size, page->image->height);
 1477|  2.40k|            return 0;
 1478|  2.40k|        }
 1479|  10.0k|        if (height > page->end_row + page->stripe_size) {
  ------------------
  |  Branch (1479:13): [True: 8.26k, False: 1.80k]
  ------------------
 1480|  8.26k|            height = page->end_row + page->stripe_size;
 1481|  8.26k|        }
 1482|  10.0k|    } else {
 1483|  8.53k|        if (rsi.y >= page->height) {
  ------------------
  |  Branch (1483:13): [True: 755, False: 7.78k]
  ------------------
 1484|    755|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "ignoring %u x %u region at (%u, %u) outside of page of height %u", rsi.width, rsi.height, rsi.x, rsi.y, page->height);
 1485|    755|            return 0;
 1486|    755|        }
 1487|  7.78k|        if (height > page->height - rsi .y) {
  ------------------
  |  Branch (1487:13): [True: 6.44k, False: 1.33k]
  ------------------
 1488|  6.44k|            height = page->height - rsi.y;
 1489|  6.44k|        }
 1490|  7.78k|    }
 1491|  17.8k|    if (height == 0) {
  ------------------
  |  Branch (1491:9): [True: 485, False: 17.3k]
  ------------------
 1492|    485|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "nothing remains of region, ignoring");
 1493|    485|        return 0;
 1494|    485|    }
 1495|       |
 1496|  17.3k|    image = jbig2_image_new(ctx, rsi.width, height);
 1497|  17.3k|    if (image == NULL)
  ------------------
  |  Branch (1497:9): [True: 70, False: 17.2k]
  ------------------
 1498|     70|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate generic image");
 1499|  17.2k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, height);
 1500|       |
 1501|  17.2k|    if (params.MMR) {
  ------------------
  |  Branch (1501:9): [True: 443, False: 16.8k]
  ------------------
 1502|    443|        code = jbig2_decode_generic_mmr(ctx, segment, &params, segment_data + offset, segment->data_length - offset, image);
 1503|    443|        if (code < 0) {
  ------------------
  |  Branch (1503:13): [True: 26, False: 417]
  ------------------
 1504|     26|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode MMR-coded generic region");
 1505|     26|            goto cleanup;
 1506|     26|        }
 1507|  16.8k|    } else {
 1508|  16.8k|        int stats_size = jbig2_generic_stats_size(ctx, params.GBTEMPLATE);
 1509|       |
 1510|  16.8k|        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  16.8k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
 1511|  16.8k|        if (GB_stats == NULL) {
  ------------------
  |  Branch (1511:13): [True: 1, False: 16.8k]
  ------------------
 1512|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder states when handling immediate generic region");
 1513|      1|            goto cleanup;
 1514|      1|        }
 1515|  16.8k|        memset(GB_stats, 0, stats_size);
 1516|       |
 1517|  16.8k|        ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
 1518|  16.8k|        if (ws == NULL) {
  ------------------
  |  Branch (1518:13): [True: 1, False: 16.8k]
  ------------------
 1519|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocated word stream when handling immediate generic region");
 1520|      1|            goto cleanup;
 1521|      1|        }
 1522|  16.8k|        as = jbig2_arith_new(ctx, ws);
 1523|  16.8k|        if (as == NULL) {
  ------------------
  |  Branch (1523:13): [True: 6, False: 16.8k]
  ------------------
 1524|      6|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling immediate generic region");
 1525|      6|            goto cleanup;
 1526|      6|        }
 1527|  16.8k|        code = jbig2_decode_generic_region(ctx, segment, &params, as, image, GB_stats);
 1528|  16.8k|        if (code < 0) {
  ------------------
  |  Branch (1528:13): [True: 76, False: 16.7k]
  ------------------
 1529|     76|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode immediate generic region");
 1530|     76|            goto cleanup;
 1531|     76|        }
 1532|  16.8k|    }
 1533|       |
 1534|  17.1k|    code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
 1535|  17.1k|    if (code < 0)
  ------------------
  |  Branch (1535:9): [True: 733, False: 16.4k]
  ------------------
 1536|    733|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add result to page");
 1537|       |
 1538|  17.2k|cleanup:
 1539|  17.2k|    jbig2_free(ctx->allocator, as);
 1540|  17.2k|    jbig2_word_stream_buf_free(ctx, ws);
 1541|  17.2k|    jbig2_free(ctx->allocator, GB_stats);
 1542|  17.2k|    jbig2_image_release(ctx, image);
 1543|       |
 1544|  17.2k|    return code;
 1545|  17.1k|}
jbig2_generic.c:jbig2_decode_generic_region_TPGDON:
 1337|  16.5k|{
 1338|  16.5k|    switch (params->GBTEMPLATE) {
  ------------------
  |  Branch (1338:13): [True: 16.5k, False: 0]
  ------------------
 1339|  8.45k|    case 0:
  ------------------
  |  Branch (1339:5): [True: 8.45k, False: 8.07k]
  ------------------
 1340|  8.45k|        return jbig2_decode_generic_template0_TPGDON(ctx, segment, params, as, image, GB_stats);
 1341|  1.92k|    case 1:
  ------------------
  |  Branch (1341:5): [True: 1.92k, False: 14.6k]
  ------------------
 1342|  1.92k|        return jbig2_decode_generic_template1_TPGDON(ctx, segment, params, as, image, GB_stats);
 1343|  5.13k|    case 2:
  ------------------
  |  Branch (1343:5): [True: 5.13k, False: 11.3k]
  ------------------
 1344|  5.13k|        return jbig2_decode_generic_template2_TPGDON(ctx, segment, params, as, image, GB_stats);
 1345|  1.01k|    case 3:
  ------------------
  |  Branch (1345:5): [True: 1.01k, False: 15.5k]
  ------------------
 1346|  1.01k|        return jbig2_decode_generic_template3_TPGDON(ctx, segment, params, as, image, GB_stats);
 1347|  16.5k|    }
 1348|       |
 1349|      0|    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported GBTEMPLATE (%d)", params->GBTEMPLATE);
 1350|  16.5k|}
jbig2_generic.c:jbig2_decode_generic_template0_TPGDON:
  924|  8.45k|{
  925|  8.45k|    const int64_t GBW = image->width;
  926|  8.45k|    const int64_t GBH = image->height;
  927|  8.45k|    uint32_t CONTEXT;
  928|  8.45k|    int64_t x, y;
  929|  8.45k|    int LTP = 0;
  930|  8.45k|    int gmin, gmax;
  931|  8.45k|    int64_t left, right, top;
  932|       |
  933|  8.45k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
  ------------------
  |  |  440|  16.9k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.45k]
  |  |  |  Branch (440:20): [True: 4, False: 8.45k]
  |  |  |  Branch (440:31): [True: 0, False: 8.45k]
  |  |  |  Branch (440:46): [True: 6.07k, False: 2.37k]
  |  |  |  Branch (440:57): [True: 0, False: 6.07k]
  |  |  |  Branch (440:72): [True: 2.37k, False: 6.07k]
  |  |  |  Branch (440:84): [True: 7, False: 2.37k]
  |  |  ------------------
  ------------------
  934|  8.44k|        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
  ------------------
  |  |  440|  16.8k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.44k]
  |  |  |  Branch (440:20): [True: 5, False: 8.43k]
  |  |  |  Branch (440:31): [True: 0, False: 8.43k]
  |  |  |  Branch (440:46): [True: 7.44k, False: 992]
  |  |  |  Branch (440:57): [True: 0, False: 7.44k]
  |  |  |  Branch (440:72): [True: 992, False: 7.44k]
  |  |  |  Branch (440:84): [True: 4, False: 988]
  |  |  ------------------
  ------------------
  935|  8.43k|        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
  ------------------
  |  |  440|  16.8k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.43k]
  |  |  |  Branch (440:20): [True: 4, False: 8.43k]
  |  |  |  Branch (440:31): [True: 0, False: 8.43k]
  |  |  |  Branch (440:46): [True: 7.41k, False: 1.02k]
  |  |  |  Branch (440:57): [True: 0, False: 7.41k]
  |  |  |  Branch (440:72): [True: 1.02k, False: 7.41k]
  |  |  |  Branch (440:84): [True: 6, False: 1.01k]
  |  |  ------------------
  ------------------
  936|  8.42k|        pixel_outside_field(params->gbat[6], params->gbat[7]))
  ------------------
  |  |  440|  8.42k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.42k]
  |  |  |  Branch (440:20): [True: 3, False: 8.42k]
  |  |  |  Branch (440:31): [True: 0, False: 8.42k]
  |  |  |  Branch (440:46): [True: 7.04k, False: 1.37k]
  |  |  |  Branch (440:57): [True: 0, False: 7.04k]
  |  |  |  Branch (440:72): [True: 1.37k, False: 7.04k]
  |  |  |  Branch (440:84): [True: 9, False: 1.36k]
  |  |  ------------------
  ------------------
  937|     42|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  938|     42|                           "adaptive template pixel is out of field");
  939|       |
  940|       |    /* JBig2 has 'standard' values for gbat (see 6.2.5.4 of the spec).
  941|       |     * Have an optimised version for those locations. This greatly
  942|       |     * simplifies some of the fetches. It's almost like they thought
  943|       |     * it through. */
  944|  8.41k|    if (params->gbat[0] ==  3 && params->gbat[1] == -1 &&
  ------------------
  |  Branch (944:9): [True: 4.81k, False: 3.59k]
  |  Branch (944:34): [True: 4.76k, False: 49]
  ------------------
  945|  4.76k|        params->gbat[2] == -3 && params->gbat[3] == -1 &&
  ------------------
  |  Branch (945:9): [True: 3.92k, False: 843]
  |  Branch (945:34): [True: 3.25k, False: 674]
  ------------------
  946|  3.25k|        params->gbat[4] ==  2 && params->gbat[5] == -2 &&
  ------------------
  |  Branch (946:9): [True: 2.98k, False: 265]
  |  Branch (946:34): [True: 2.76k, False: 222]
  ------------------
  947|  2.76k|        params->gbat[6] == -2 && params->gbat[7] == -2)
  ------------------
  |  Branch (947:9): [True: 1.46k, False: 1.30k]
  |  Branch (947:34): [True: 1.34k, False: 112]
  ------------------
  948|  1.34k|    {
  949|  54.3M|        for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (949:21): [True: 54.3M, False: 1.34k]
  ------------------
  950|  54.3M|            int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
  951|  54.3M|            if (bit < 0)
  ------------------
  |  Branch (951:17): [True: 0, False: 54.3M]
  ------------------
  952|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
  953|  54.3M|            LTP ^= bit;
  954|  54.3M|            if (!LTP) {
  ------------------
  |  Branch (954:17): [True: 27.1M, False: 27.1M]
  ------------------
  955|  27.1M|                uint32_t out_byte = 0;
  956|  27.1M|                int out_bits_to_go_in_byte = 8;
  957|  27.1M|                uint8_t *d = &image->data[image->stride * y];
  958|  27.1M|                uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (958:35): [True: 27.1M, False: 851]
  ------------------
  959|  27.1M|                uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (959:35): [True: 27.1M, False: 2.04k]
  ------------------
  960|  27.1M|                uint32_t pd = 0;
  961|  27.1M|                uint32_t ppd = 0;
  962|  27.1M|                if (y > 0 && pline) {
  ------------------
  |  Branch (962:21): [True: 27.1M, False: 851]
  |  Branch (962:30): [True: 27.1M, False: 0]
  ------------------
  963|  27.1M|                    pd = (*pline++ << 8);
  964|  27.1M|                    if (GBW > 8)
  ------------------
  |  Branch (964:25): [True: 292k, False: 26.8M]
  ------------------
  965|   292k|                        pd |= *pline++;
  966|  27.1M|                    if (y > 1 && ppline) {
  ------------------
  |  Branch (966:25): [True: 27.1M, False: 1.18k]
  |  Branch (966:34): [True: 27.1M, False: 0]
  ------------------
  967|  27.1M|                        ppd = (*ppline++ << 8);
  968|  27.1M|                        if (GBW > 8)
  ------------------
  |  Branch (968:29): [True: 291k, False: 26.8M]
  ------------------
  969|   291k|                            ppd |= *ppline++;
  970|  27.1M|                    }
  971|  27.1M|                }
  972|   652M|                for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (972:29): [True: 625M, False: 27.1M]
  ------------------
  973|   625M|                    if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (973:25): [True: 0, False: 625M]
  |  Branch (973:44): [True: 0, False: 0]
  ------------------
  974|      0|                        bit = 0;
  975|   625M|                    } else {
  976|   625M|                        CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
  977|   625M|                        CONTEXT |= (pd>>8) & 0x7F0; /* Next 7 pixels */
  978|   625M|                        CONTEXT |= (ppd>>2) & 0xF800; /* Final 5 pixels */
  979|   625M|                        bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  980|   625M|                        if (bit < 0)
  ------------------
  |  Branch (980:29): [True: 0, False: 625M]
  ------------------
  981|      0|                            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
  982|   625M|                    }
  983|   625M|                    pd = pd<<1;
  984|   625M|                    ppd = ppd<<1;
  985|   625M|                    out_byte = (out_byte<<1) | bit;
  986|   625M|                    out_bits_to_go_in_byte--;
  987|   625M|                    if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (987:25): [True: 76.9M, False: 548M]
  ------------------
  988|  76.9M|                        out_bits_to_go_in_byte = 8;
  989|  76.9M|                        *d++ = (uint8_t)out_byte;
  990|  76.9M|                        if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (990:29): [True: 57.9M, False: 18.9M]
  |  Branch (990:42): [True: 23.7M, False: 34.1M]
  |  Branch (990:51): [True: 23.7M, False: 0]
  ------------------
  991|  23.7M|                            pd |= *pline++;
  992|  23.7M|                            if (y > 1 && ppline)
  ------------------
  |  Branch (992:33): [True: 21.0M, False: 2.70M]
  |  Branch (992:42): [True: 21.0M, False: 0]
  ------------------
  993|  21.0M|                                ppd |= *ppline++;
  994|  23.7M|                        }
  995|  76.9M|                    }
  996|   625M|                }
  997|  27.1M|                if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (997:21): [True: 8.51M, False: 18.6M]
  ------------------
  998|  8.51M|                    *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  999|  27.1M|            } else {
 1000|  27.1M|                copy_prev_row(image, y);
 1001|  27.1M|            }
 1002|  54.3M|        }
 1003|  1.34k|        return 0;
 1004|  1.34k|    }
 1005|       |
 1006|       |    /* We divide the width into 3 regions 0..left...right...GBW,
 1007|       |     * between left and right, we know that our accesses will never
 1008|       |     * step outside the image, enabling us to use faster accessors. */
 1009|  7.06k|    left = 4;
 1010|  7.06k|    right = 2;
 1011|  7.06k|    gmin = gmax = params->gbat[0];
 1012|  7.06k|    if (params->gbat[2] < gmin)
  ------------------
  |  Branch (1012:9): [True: 2.76k, False: 4.30k]
  ------------------
 1013|  2.76k|        gmin = params->gbat[2];
 1014|  7.06k|    if (gmax < params->gbat[2])
  ------------------
  |  Branch (1014:9): [True: 3.20k, False: 3.85k]
  ------------------
 1015|  3.20k|        gmax = params->gbat[2];
 1016|  7.06k|    if (params->gbat[4] < gmin)
  ------------------
  |  Branch (1016:9): [True: 1.00k, False: 6.05k]
  ------------------
 1017|  1.00k|        gmin = params->gbat[4];
 1018|  7.06k|    if (gmax < params->gbat[4])
  ------------------
  |  Branch (1018:9): [True: 897, False: 6.16k]
  ------------------
 1019|    897|        gmax = params->gbat[4];
 1020|  7.06k|    if (params->gbat[6] < gmin)
  ------------------
  |  Branch (1020:9): [True: 1.13k, False: 5.93k]
  ------------------
 1021|  1.13k|        gmin = params->gbat[6];
 1022|  7.06k|    if (gmax < params->gbat[6])
  ------------------
  |  Branch (1022:9): [True: 2.89k, False: 4.17k]
  ------------------
 1023|  2.89k|        gmax = params->gbat[6];
 1024|  7.06k|    if (left < -((int64_t) gmin))
  ------------------
  |  Branch (1024:9): [True: 4.28k, False: 2.77k]
  ------------------
 1025|  4.28k|        left = -((int64_t) gmin);
 1026|  7.06k|    if (right < gmax)
  ------------------
  |  Branch (1026:9): [True: 3.86k, False: 3.20k]
  ------------------
 1027|  3.86k|        right = gmax;
 1028|  7.06k|    if (right > GBW)
  ------------------
  |  Branch (1028:9): [True: 2.46k, False: 4.59k]
  ------------------
 1029|  2.46k|        right = GBW;
 1030|  7.06k|    right = GBW - right;
 1031|       |    /* So 0 <= x < left or right <= x < GBW needs bounds checking. */
 1032|       |
 1033|       |    /* Now we do the same for the height, but here there is no bottom
 1034|       |     * region, as we only ever look up for y. */
 1035|  7.06k|    top = 2;
 1036|  7.06k|    gmin = params->gbat[1];
 1037|  7.06k|    if (params->gbat[3] < gmin)
  ------------------
  |  Branch (1037:9): [True: 2.19k, False: 4.87k]
  ------------------
 1038|  2.19k|        gmin = params->gbat[3];
 1039|  7.06k|    if (params->gbat[5] < gmin)
  ------------------
  |  Branch (1039:9): [True: 3.87k, False: 3.18k]
  ------------------
 1040|  3.87k|        gmin = params->gbat[5];
 1041|  7.06k|    if (params->gbat[7] < gmin)
  ------------------
  |  Branch (1041:9): [True: 1.55k, False: 5.51k]
  ------------------
 1042|  1.55k|        gmin = params->gbat[7];
 1043|  7.06k|    if (top < -((int64_t) gmin))
  ------------------
  |  Branch (1043:9): [True: 4.41k, False: 2.64k]
  ------------------
 1044|  4.41k|        top = -((int64_t) gmin);
 1045|       |    /* So 0 <= y < top needs bounds checking. */
 1046|       |
 1047|   142M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1047:17): [True: 142M, False: 7.06k]
  ------------------
 1048|   142M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
 1049|   142M|        if (bit < 0)
  ------------------
  |  Branch (1049:13): [True: 0, False: 142M]
  ------------------
 1050|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
 1051|   142M|        LTP ^= bit;
 1052|   142M|        if (!LTP) {
  ------------------
  |  Branch (1052:13): [True: 93.5M, False: 48.6M]
  ------------------
 1053|  93.5M|            uint32_t out_byte = 0;
 1054|  93.5M|            int out_bits_to_go_in_byte = 8;
 1055|  93.5M|            uint8_t *d = &image->data[image->stride * y];
 1056|  93.5M|            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1056:31): [True: 93.5M, False: 3.79k]
  ------------------
 1057|  93.5M|            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (1057:31): [True: 93.5M, False: 9.06k]
  ------------------
 1058|  93.5M|            uint32_t pd = 0;
 1059|  93.5M|            uint32_t ppd = 0;
 1060|  93.5M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1060:17): [True: 93.5M, False: 3.79k]
  |  Branch (1060:26): [True: 93.5M, False: 0]
  ------------------
 1061|  93.5M|                pd = (*pline++ << 8);
 1062|  93.5M|                if (GBW > 8)
  ------------------
  |  Branch (1062:21): [True: 3.21M, False: 90.2M]
  ------------------
 1063|  3.21M|                    pd |= *pline++;
 1064|  93.5M|                if (y > 1 && ppline) {
  ------------------
  |  Branch (1064:21): [True: 93.5M, False: 5.27k]
  |  Branch (1064:30): [True: 93.5M, False: 0]
  ------------------
 1065|  93.5M|                    ppd = (*ppline++ << 8);
 1066|  93.5M|                    if (GBW > 8)
  ------------------
  |  Branch (1066:25): [True: 3.20M, False: 90.2M]
  ------------------
 1067|  3.20M|                        ppd |= *ppline++;
 1068|  93.5M|                }
 1069|  93.5M|            }
 1070|   964M|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1070:25): [True: 870M, False: 93.5M]
  ------------------
 1071|   870M|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1071:21): [True: 0, False: 870M]
  |  Branch (1071:40): [True: 0, False: 0]
  ------------------
 1072|      0|                    bit = 0;
 1073|   870M|                } else {
 1074|   870M|                    CONTEXT = out_byte & 0x000F; /* First 4 pixels */
 1075|   870M|                    CONTEXT |= (pd>>8) & 0x03E0; /* Skip one, next 5 pixels */
 1076|   870M|                    CONTEXT |= (ppd>>2) & 0x7000; /* Skip 2, next 3 pixels, skip one */
 1077|   870M|                    if (y >= top && x >= left && x < right)
  ------------------
  |  Branch (1077:25): [True: 685M, False: 185M]
  |  Branch (1077:37): [True: 468M, False: 216M]
  |  Branch (1077:50): [True: 435M, False: 32.9M]
  ------------------
 1078|   435M|                    {
 1079|   435M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[0], y + params->gbat[1]) << 4;
 1080|   435M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[2], y + params->gbat[3]) << 10;
 1081|   435M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[4], y + params->gbat[5]) << 11;
 1082|   435M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[6], y + params->gbat[7]) << 15;
 1083|   435M|                    }
 1084|   435M|                    else
 1085|   435M|                    {
 1086|   435M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
 1087|   435M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
 1088|   435M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
 1089|   435M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
 1090|   435M|                    }
 1091|   870M|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1092|   870M|                    if (bit < 0)
  ------------------
  |  Branch (1092:25): [True: 0, False: 870M]
  ------------------
 1093|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
 1094|   870M|                }
 1095|   870M|                pd = pd<<1;
 1096|   870M|                ppd = ppd<<1;
 1097|   870M|                out_byte = (out_byte<<1) | bit;
 1098|   870M|                out_bits_to_go_in_byte--;
 1099|   870M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1100|   870M|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1100:21): [True: 86.9M, False: 783M]
  ------------------
 1101|  86.9M|                    out_bits_to_go_in_byte = 8;
 1102|  86.9M|                    d++;
 1103|  86.9M|                    if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (1103:25): [True: 82.4M, False: 4.47M]
  |  Branch (1103:38): [True: 66.2M, False: 16.2M]
  |  Branch (1103:47): [True: 66.2M, False: 0]
  ------------------
 1104|  66.2M|                        pd |= *pline++;
 1105|  66.2M|                        if (y > 1 && ppline)
  ------------------
  |  Branch (1105:29): [True: 61.2M, False: 5.00M]
  |  Branch (1105:38): [True: 61.2M, False: 0]
  ------------------
 1106|  61.2M|                            ppd |= *ppline++;
 1107|  66.2M|                    }
 1108|  86.9M|                }
 1109|   870M|            }
 1110|  93.5M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1110:17): [True: 92.2M, False: 1.26M]
  ------------------
 1111|  92.2M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1112|  93.5M|        } else {
 1113|  48.6M|            copy_prev_row(image, y);
 1114|  48.6M|        }
 1115|   142M|    }
 1116|       |
 1117|  7.06k|    return 0;
 1118|  7.06k|}
jbig2_generic.c:copy_prev_row:
  908|   233M|{
  909|   233M|    if (!row) {
  ------------------
  |  Branch (909:9): [True: 5.57k, False: 233M]
  ------------------
  910|       |        /* no previous row */
  911|  5.57k|        memset(image->data, 0, image->stride);
  912|   233M|    } else {
  913|       |        /* duplicate data from the previous row */
  914|   233M|        uint8_t *src = image->data + (row - 1) * image->stride;
  915|       |
  916|   233M|        memcpy(src + image->stride, src, image->stride);
  917|   233M|    }
  918|   233M|}
jbig2_generic.c:jbig2_image_get_pixel_fast:
  348|  1.74G|{
  349|  1.74G|    size_t sx = (size_t) x;
  350|  1.74G|    size_t sy = (size_t) y;
  351|  1.74G|    size_t byte = (sx >> 3) + sy * image->stride;
  352|  1.74G|    int bit = 7 - ((int) (sx & 7));
  353|       |
  354|  1.74G|    return ((image->data[byte] >> bit) & 1);
  355|  1.74G|}
jbig2_generic.c:jbig2_decode_generic_template1_TPGDON:
 1124|  1.92k|{
 1125|  1.92k|    const int64_t GBW = image->width;
 1126|  1.92k|    const int64_t GBH = image->height;
 1127|  1.92k|    uint32_t CONTEXT;
 1128|  1.92k|    int64_t x, y;
 1129|  1.92k|    int LTP = 0;
 1130|       |
 1131|  1.92k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  1.92k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 1.92k]
  |  |  |  Branch (440:20): [True: 2, False: 1.92k]
  |  |  |  Branch (440:31): [True: 0, False: 1.92k]
  |  |  |  Branch (440:46): [True: 1.13k, False: 783]
  |  |  |  Branch (440:57): [True: 0, False: 1.13k]
  |  |  |  Branch (440:72): [True: 783, False: 1.13k]
  |  |  |  Branch (440:84): [True: 3, False: 780]
  |  |  ------------------
  ------------------
 1132|      5|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 1133|      5|                           "adaptive template pixel is out of field");
 1134|       |
 1135|  95.4M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1135:17): [True: 95.4M, False: 1.91k]
  ------------------
 1136|  95.4M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0795]);
 1137|  95.4M|        if (bit < 0)
  ------------------
  |  Branch (1137:13): [True: 0, False: 95.4M]
  ------------------
 1138|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON1");
 1139|  95.4M|        LTP ^= bit;
 1140|  95.4M|        if (!LTP) {
  ------------------
  |  Branch (1140:13): [True: 46.9M, False: 48.4M]
  ------------------
 1141|  46.9M|            uint32_t out_byte = 0;
 1142|  46.9M|            int out_bits_to_go_in_byte = 8;
 1143|  46.9M|            uint8_t *d = &image->data[image->stride * y];
 1144|  46.9M|            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1144:31): [True: 46.9M, False: 1.20k]
  ------------------
 1145|  46.9M|            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (1145:31): [True: 46.9M, False: 2.46k]
  ------------------
 1146|  46.9M|            uint32_t pd = 0;
 1147|  46.9M|            uint32_t ppd = 0;
 1148|  46.9M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1148:17): [True: 46.9M, False: 1.20k]
  |  Branch (1148:26): [True: 46.9M, False: 0]
  ------------------
 1149|  46.9M|                pd = (*pline++ << 8);
 1150|  46.9M|                if (GBW > 8)
  ------------------
  |  Branch (1150:21): [True: 3.24M, False: 43.7M]
  ------------------
 1151|  3.24M|                    pd |= *pline++;
 1152|  46.9M|                if (y > 1 && ppline) {
  ------------------
  |  Branch (1152:21): [True: 46.9M, False: 1.26k]
  |  Branch (1152:30): [True: 46.9M, False: 0]
  ------------------
 1153|  46.9M|                    ppd = (*ppline++ << 8);
 1154|  46.9M|                    if (GBW > 8)
  ------------------
  |  Branch (1154:25): [True: 3.24M, False: 43.7M]
  ------------------
 1155|  3.24M|                        ppd |= *ppline++;
 1156|  46.9M|                }
 1157|  46.9M|            }
 1158|  1.61G|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1158:25): [True: 1.56G, False: 46.9M]
  ------------------
 1159|  1.56G|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1159:21): [True: 0, False: 1.56G]
  |  Branch (1159:40): [True: 0, False: 0]
  ------------------
 1160|      0|                    bit = 0;
 1161|  1.56G|                } else {
 1162|  1.56G|                    CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
 1163|  1.56G|                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
 1164|  1.56G|                    CONTEXT |= (pd>>9) & 0x01F0; /* next 5 pixels */
 1165|  1.56G|                    CONTEXT |= (ppd>>4) & 0x1E00; /* next 4 pixels */
 1166|  1.56G|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1167|  1.56G|                    if (bit < 0)
  ------------------
  |  Branch (1167:25): [True: 0, False: 1.56G]
  ------------------
 1168|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON2");
 1169|  1.56G|                }
 1170|  1.56G|                pd = pd<<1;
 1171|  1.56G|                ppd = ppd<<1;
 1172|  1.56G|                out_byte = (out_byte<<1) | bit;
 1173|  1.56G|                out_bits_to_go_in_byte--;
 1174|  1.56G|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1175|  1.56G|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1175:21): [True: 175M, False: 1.39G]
  ------------------
 1176|   175M|                    out_bits_to_go_in_byte = 8;
 1177|   175M|                    d++;
 1178|   175M|                    if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (1178:25): [True: 169M, False: 5.55M]
  |  Branch (1178:38): [True: 150M, False: 19.7M]
  |  Branch (1178:47): [True: 150M, False: 0]
  ------------------
 1179|   150M|                        pd |= *pline++;
 1180|   150M|                        if (y > 1 && ppline)
  ------------------
  |  Branch (1180:29): [True: 103M, False: 46.6M]
  |  Branch (1180:38): [True: 103M, False: 0]
  ------------------
 1181|   103M|                            ppd |= *ppline++;
 1182|   150M|                    }
 1183|   175M|                }
 1184|  1.56G|            }
 1185|  46.9M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1185:17): [True: 44.6M, False: 2.31M]
  ------------------
 1186|  44.6M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1187|  48.4M|        } else {
 1188|  48.4M|            copy_prev_row(image, y);
 1189|  48.4M|        }
 1190|  95.4M|    }
 1191|       |
 1192|  1.91k|    return 0;
 1193|  1.91k|}
jbig2_generic.c:jbig2_decode_generic_template2_TPGDON:
 1199|  5.13k|{
 1200|  5.13k|    const int64_t GBW = image->width;
 1201|  5.13k|    const int64_t GBH = image->height;
 1202|  5.13k|    uint32_t CONTEXT;
 1203|  5.13k|    int64_t x, y;
 1204|  5.13k|    int LTP = 0;
 1205|       |
 1206|  5.13k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  5.13k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 5.13k]
  |  |  |  Branch (440:20): [True: 3, False: 5.13k]
  |  |  |  Branch (440:31): [True: 0, False: 5.13k]
  |  |  |  Branch (440:46): [True: 4.73k, False: 400]
  |  |  |  Branch (440:57): [True: 0, False: 4.73k]
  |  |  |  Branch (440:72): [True: 400, False: 4.73k]
  |  |  |  Branch (440:84): [True: 5, False: 395]
  |  |  ------------------
  ------------------
 1207|      8|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 1208|      8|                           "adaptive template pixel is out of field");
 1209|       |
 1210|  77.8M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1210:17): [True: 77.8M, False: 5.12k]
  ------------------
 1211|  77.8M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0xE5]);
 1212|  77.8M|        if (bit < 0)
  ------------------
  |  Branch (1212:13): [True: 0, False: 77.8M]
  ------------------
 1213|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON1");
 1214|  77.8M|        LTP ^= bit;
 1215|  77.8M|        if (!LTP) {
  ------------------
  |  Branch (1215:13): [True: 27.7M, False: 50.0M]
  ------------------
 1216|  27.7M|            uint32_t out_byte = 0;
 1217|  27.7M|            int out_bits_to_go_in_byte = 8;
 1218|  27.7M|            uint8_t *d = &image->data[image->stride * y];
 1219|  27.7M|            uint8_t *pline  =  y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1219:32): [True: 27.7M, False: 4.81k]
  ------------------
 1220|  27.7M|            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (1220:31): [True: 27.7M, False: 9.91k]
  ------------------
 1221|  27.7M|            uint32_t pd = 0;
 1222|  27.7M|            uint32_t ppd = 0;
 1223|  27.7M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1223:17): [True: 27.7M, False: 4.81k]
  |  Branch (1223:26): [True: 27.7M, False: 0]
  ------------------
 1224|  27.7M|                pd = (*pline++ << 8);
 1225|  27.7M|                if (GBW > 8)
  ------------------
  |  Branch (1225:21): [True: 612k, False: 27.1M]
  ------------------
 1226|   612k|                    pd |= *pline++;
 1227|  27.7M|                if (y > 1 && ppline) {
  ------------------
  |  Branch (1227:21): [True: 27.7M, False: 5.10k]
  |  Branch (1227:30): [True: 27.7M, False: 0]
  ------------------
 1228|  27.7M|                    ppd = (*ppline++ << 8);
 1229|  27.7M|                    if (GBW > 8)
  ------------------
  |  Branch (1229:25): [True: 611k, False: 27.1M]
  ------------------
 1230|   611k|                        ppd |= *ppline++;
 1231|  27.7M|                }
 1232|  27.7M|            }
 1233|   336M|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1233:25): [True: 309M, False: 27.7M]
  ------------------
 1234|   309M|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1234:21): [True: 0, False: 309M]
  |  Branch (1234:40): [True: 0, False: 0]
  ------------------
 1235|      0|                    bit = 0;
 1236|   309M|                } else {
 1237|   309M|                    CONTEXT  = out_byte & 0x003; /* First 2 pixels */
 1238|   309M|                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
 1239|   309M|                    CONTEXT |= (pd>>11) & 0x078; /* next 4 pixels */
 1240|   309M|                    CONTEXT |= (ppd>>7) & 0x380; /* next 3 pixels */
 1241|   309M|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1242|   309M|                    if (bit < 0)
  ------------------
  |  Branch (1242:25): [True: 0, False: 309M]
  ------------------
 1243|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON2");
 1244|   309M|                }
 1245|   309M|                pd = pd<<1;
 1246|   309M|                ppd = ppd<<1;
 1247|   309M|                out_byte = (out_byte<<1) | bit;
 1248|   309M|                out_bits_to_go_in_byte--;
 1249|   309M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1250|   309M|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1250:21): [True: 34.9M, False: 274M]
  ------------------
 1251|  34.9M|                    out_bits_to_go_in_byte = 8;
 1252|  34.9M|                    d++;
 1253|  34.9M|                    if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (1253:25): [True: 33.2M, False: 1.65M]
  |  Branch (1253:38): [True: 26.6M, False: 6.58M]
  |  Branch (1253:47): [True: 26.6M, False: 0]
  ------------------
 1254|  26.6M|                        pd |= *pline++;
 1255|  26.6M|                        if (y > 1 && ppline)
  ------------------
  |  Branch (1255:29): [True: 23.3M, False: 3.31M]
  |  Branch (1255:38): [True: 23.3M, False: 0]
  ------------------
 1256|  23.3M|                            ppd |= *ppline++;
 1257|  26.6M|                    }
 1258|  34.9M|                }
 1259|   309M|            }
 1260|  27.7M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1260:17): [True: 26.7M, False: 1.04M]
  ------------------
 1261|  26.7M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1262|  50.0M|        } else {
 1263|  50.0M|            copy_prev_row(image, y);
 1264|  50.0M|        }
 1265|  77.8M|    }
 1266|       |
 1267|  5.12k|    return 0;
 1268|  5.12k|}
jbig2_generic.c:jbig2_decode_generic_template3_TPGDON:
 1274|  1.01k|{
 1275|  1.01k|    const int64_t GBW = image->width;
 1276|  1.01k|    const int64_t GBH = image->height;
 1277|  1.01k|    uint32_t CONTEXT;
 1278|  1.01k|    int64_t x, y;
 1279|  1.01k|    int LTP = 0;
 1280|       |
 1281|  1.01k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  1.01k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 1.01k]
  |  |  |  Branch (440:20): [True: 2, False: 1.01k]
  |  |  |  Branch (440:31): [True: 0, False: 1.01k]
  |  |  |  Branch (440:46): [True: 626, False: 387]
  |  |  |  Branch (440:57): [True: 0, False: 626]
  |  |  |  Branch (440:72): [True: 387, False: 626]
  |  |  |  Branch (440:84): [True: 3, False: 384]
  |  |  ------------------
  ------------------
 1282|      5|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 1283|      5|                           "adaptive template pixel is out of field");
 1284|       |
 1285|   168M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1285:17): [True: 168M, False: 1.01k]
  ------------------
 1286|   168M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0195]);
 1287|   168M|        if (bit < 0)
  ------------------
  |  Branch (1287:13): [True: 0, False: 168M]
  ------------------
 1288|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON1");
 1289|   168M|        LTP ^= bit;
 1290|   168M|        if (!LTP) {
  ------------------
  |  Branch (1290:13): [True: 109M, False: 59.3M]
  ------------------
 1291|   109M|            uint32_t out_byte = 0;
 1292|   109M|            int out_bits_to_go_in_byte = 8;
 1293|   109M|            uint8_t *d = &image->data[image->stride * y];
 1294|   109M|            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1294:31): [True: 109M, False: 237]
  ------------------
 1295|   109M|            uint32_t pd = 0;
 1296|   109M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1296:17): [True: 109M, False: 237]
  |  Branch (1296:26): [True: 109M, False: 0]
  ------------------
 1297|   109M|                pd = (*pline++ << 8);
 1298|   109M|                if (GBW > 8)
  ------------------
  |  Branch (1298:21): [True: 494k, False: 108M]
  ------------------
 1299|   494k|                    pd |= *pline++;
 1300|   109M|            }
 1301|  1.01G|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1301:25): [True: 910M, False: 109M]
  ------------------
 1302|   910M|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1302:21): [True: 0, False: 910M]
  |  Branch (1302:40): [True: 0, False: 0]
  ------------------
 1303|      0|                    bit = 0;
 1304|   910M|                } else {
 1305|   910M|                    CONTEXT  = out_byte & 0x0F; /* First 4 pixels */
 1306|   910M|                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
 1307|   910M|                    CONTEXT |= (pd>>9) & 0x3E0; /* next 5 pixels */
 1308|   910M|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1309|   910M|                    if (bit < 0)
  ------------------
  |  Branch (1309:25): [True: 0, False: 910M]
  ------------------
 1310|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON2");
 1311|   910M|                }
 1312|   910M|                pd = pd<<1;
 1313|   910M|                out_byte = (out_byte<<1) | bit;
 1314|   910M|                out_bits_to_go_in_byte--;
 1315|   910M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1316|   910M|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1316:21): [True: 85.7M, False: 824M]
  ------------------
 1317|  85.7M|                    out_bits_to_go_in_byte = 8;
 1318|  85.7M|                    d++;
 1319|  85.7M|                    if (x+9 < GBW && y > 0 && pline)
  ------------------
  |  Branch (1319:25): [True: 84.7M, False: 981k]
  |  Branch (1319:38): [True: 63.4M, False: 21.3M]
  |  Branch (1319:47): [True: 63.4M, False: 0]
  ------------------
 1320|  63.4M|                        pd |= *pline++;
 1321|  85.7M|                }
 1322|   910M|            }
 1323|   109M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1323:17): [True: 108M, False: 486k]
  ------------------
 1324|   108M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1325|   109M|        } else {
 1326|  59.3M|            copy_prev_row(image, y);
 1327|  59.3M|        }
 1328|   168M|    }
 1329|       |
 1330|  1.01k|    return 0;
 1331|  1.01k|}
jbig2_generic.c:jbig2_decode_generic_template0:
  373|  7.20k|{
  374|  7.20k|    const int64_t GBW = image->width;
  375|  7.20k|    const int64_t GBH = image->height;
  376|  7.20k|    const uint32_t rowstride = image->stride;
  377|  7.20k|    int64_t x, y;
  378|  7.20k|    byte *line2 = NULL;
  379|  7.20k|    byte *line1 = NULL;
  380|  7.20k|    byte *gbreg_line = (byte *) image->data;
  381|       |
  382|  7.20k|    (void) ctx;
  383|  7.20k|    (void) params;
  384|       |
  385|       |#ifdef OUTPUT_PBM
  386|       |    printf("P4\n%u %u\n",
  387|       |        (uint32_t) GBW,
  388|       |        (uint32_t) GBH);
  389|       |#endif
  390|       |
  391|  7.20k|    if (GBW <= 0)
  ------------------
  |  Branch (391:9): [True: 0, False: 7.20k]
  ------------------
  392|      0|        return 0;
  393|       |
  394|   148M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (394:17): [True: 148M, False: 7.20k]
  ------------------
  395|   148M|        uint32_t CONTEXT;
  396|   148M|        uint32_t line_m1;
  397|   148M|        uint32_t line_m2;
  398|   148M|        int64_t padded_width = (GBW + 7) & -8;
  399|       |
  400|   148M|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (400:19): [True: 148M, False: 7.20k]
  ------------------
  401|   148M|        line_m2 = line2 ? line2[0] << 6 : 0;
  ------------------
  |  Branch (401:19): [True: 148M, False: 14.2k]
  ------------------
  402|   148M|        CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800);
  403|       |
  404|       |        /* 6.2.5.7 3d */
  405|   397M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (405:21): [True: 248M, False: 148M]
  ------------------
  406|   248M|            byte result = 0;
  407|   248M|            int64_t x_minor;
  408|   248M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (408:35): [True: 100M, False: 148M]
  ------------------
  409|       |
  410|   248M|            if (line1)
  ------------------
  |  Branch (410:17): [True: 218M, False: 29.7M]
  ------------------
  411|   218M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (411:45): [True: 70.5M, False: 148M]
  ------------------
  412|       |
  413|   248M|            if (line2)
  ------------------
  |  Branch (413:17): [True: 202M, False: 46.4M]
  ------------------
  414|   202M|                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 6 : 0);
  ------------------
  |  Branch (414:45): [True: 53.8M, False: 148M]
  ------------------
  415|       |
  416|       |            /* This is the speed-critical inner loop. */
  417|  1.56G|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (417:31): [True: 1.31G, False: 248M]
  ------------------
  418|  1.31G|                int bit;
  419|       |
  420|  1.31G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  421|  1.31G|                if (bit < 0)
  ------------------
  |  Branch (421:21): [True: 0, False: 1.31G]
  ------------------
  422|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 optimized");
  423|  1.31G|                result |= bit << (7 - x_minor);
  424|  1.31G|                CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800);
  425|  1.31G|            }
  426|   248M|            gbreg_line[x >> 3] = result;
  427|   248M|        }
  428|       |#ifdef OUTPUT_PBM
  429|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  430|       |#endif
  431|   148M|        line2 = line1;
  432|   148M|        line1 = gbreg_line;
  433|   148M|        gbreg_line += rowstride;
  434|   148M|    }
  435|       |
  436|  7.20k|    return 0;
  437|  7.20k|}
jbig2_generic.c:jbig2_decode_generic_template0_unopt:
  446|  14.4k|{
  447|  14.4k|    const int64_t GBW = image->width;
  448|  14.4k|    const int64_t GBH = image->height;
  449|  14.4k|    uint32_t CONTEXT;
  450|  14.4k|    int64_t x, y;
  451|  14.4k|    int bit;
  452|       |
  453|  14.4k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
  ------------------
  |  |  440|  28.8k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 14.4k]
  |  |  |  Branch (440:20): [True: 4, False: 14.3k]
  |  |  |  Branch (440:31): [True: 0, False: 14.3k]
  |  |  |  Branch (440:46): [True: 13.8k, False: 552]
  |  |  |  Branch (440:57): [True: 0, False: 13.8k]
  |  |  |  Branch (440:72): [True: 552, False: 13.8k]
  |  |  |  Branch (440:84): [True: 12, False: 540]
  |  |  ------------------
  ------------------
  454|  14.3k|        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
  ------------------
  |  |  440|  28.7k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 14.3k]
  |  |  |  Branch (440:20): [True: 4, False: 14.3k]
  |  |  |  Branch (440:31): [True: 0, False: 14.3k]
  |  |  |  Branch (440:46): [True: 13.8k, False: 497]
  |  |  |  Branch (440:57): [True: 0, False: 13.8k]
  |  |  |  Branch (440:72): [True: 497, False: 13.8k]
  |  |  |  Branch (440:84): [True: 3, False: 494]
  |  |  ------------------
  ------------------
  455|  14.3k|        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
  ------------------
  |  |  440|  28.7k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 14.3k]
  |  |  |  Branch (440:20): [True: 3, False: 14.3k]
  |  |  |  Branch (440:31): [True: 0, False: 14.3k]
  |  |  |  Branch (440:46): [True: 13.2k, False: 1.10k]
  |  |  |  Branch (440:57): [True: 0, False: 13.2k]
  |  |  |  Branch (440:72): [True: 1.10k, False: 13.2k]
  |  |  |  Branch (440:84): [True: 3, False: 1.10k]
  |  |  ------------------
  ------------------
  456|  14.3k|        pixel_outside_field(params->gbat[6], params->gbat[7]))
  ------------------
  |  |  440|  14.3k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 14.3k]
  |  |  |  Branch (440:20): [True: 4, False: 14.3k]
  |  |  |  Branch (440:31): [True: 0, False: 14.3k]
  |  |  |  Branch (440:46): [True: 12.3k, False: 2.06k]
  |  |  |  Branch (440:57): [True: 0, False: 12.3k]
  |  |  |  Branch (440:72): [True: 2.06k, False: 12.3k]
  |  |  |  Branch (440:84): [True: 1, False: 2.06k]
  |  |  ------------------
  ------------------
  457|     34|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  458|     34|                           "adaptive template pixel is out of field");
  459|       |
  460|   130M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (460:17): [True: 130M, False: 14.3k]
  ------------------
  461|   130M|        uint32_t out_byte = 0;
  462|   130M|        int out_bits_to_go_in_byte = 8;
  463|   130M|        uint8_t *d = &image->data[image->stride * y];
  464|   130M|        uint32_t pd = 0;
  465|   130M|        uint32_t ppd = 0;
  466|   130M|        uint8_t *pline = NULL;
  467|   130M|        uint8_t *ppline = NULL;
  468|   130M|        if (y >= 1)
  ------------------
  |  Branch (468:13): [True: 130M, False: 14.3k]
  ------------------
  469|   130M|        {
  470|   130M|            pline  = &image->data[image->stride * (y-1)];
  471|   130M|            pd = (*pline++ << 8);
  472|   130M|            if (GBW > 8)
  ------------------
  |  Branch (472:17): [True: 910k, False: 129M]
  ------------------
  473|   910k|                pd |= *pline++;
  474|   130M|        }
  475|   130M|        if (y >= 2) {
  ------------------
  |  Branch (475:13): [True: 130M, False: 25.5k]
  ------------------
  476|   130M|            ppline = &image->data[image->stride * (y-2)];
  477|   130M|            ppd = (*ppline++ << 8);
  478|   130M|            if (GBW > 8)
  ------------------
  |  Branch (478:17): [True: 899k, False: 129M]
  ------------------
  479|   899k|                ppd |= *ppline++;
  480|   130M|        }
  481|  1.10G|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (481:21): [True: 976M, False: 130M]
  ------------------
  482|   976M|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (482:17): [True: 247M, False: 728M]
  |  Branch (482:36): [True: 246M, False: 804k]
  ------------------
  483|   246M|                bit = 0;
  484|   729M|            } else {
  485|   729M|                CONTEXT  = out_byte & 0x000F; /* First 4 pixels */
  486|   729M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
  487|   729M|                CONTEXT |= (pd>>8) & 0x03E0; /* Next 5 pixels */
  488|   729M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
  489|   729M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
  490|   729M|                CONTEXT |= (ppd>>2) & 0x7000; /* Next 3 pixels */
  491|   729M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
  492|   729M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  493|   729M|                if (bit < 0)
  ------------------
  |  Branch (493:21): [True: 0, False: 729M]
  ------------------
  494|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 unoptimized");
  495|   729M|            }
  496|   976M|            pd = pd<<1;
  497|   976M|            ppd = ppd<<1;
  498|   976M|            out_byte = (out_byte<<1) | bit;
  499|   976M|            out_bits_to_go_in_byte--;
  500|   976M|            *d = out_byte<<out_bits_to_go_in_byte;
  501|   976M|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (501:17): [True: 89.5M, False: 886M]
  ------------------
  502|  89.5M|                out_bits_to_go_in_byte = 8;
  503|  89.5M|                d++;
  504|  89.5M|                if (x+9 < GBW && pline != NULL) {
  ------------------
  |  Branch (504:21): [True: 88.4M, False: 1.11M]
  |  Branch (504:34): [True: 46.0M, False: 42.4M]
  ------------------
  505|  46.0M|                    pd |= *pline++;
  506|  46.0M|                    if (ppline != NULL)
  ------------------
  |  Branch (506:25): [True: 36.4M, False: 9.59M]
  ------------------
  507|  36.4M|                        ppd |= *ppline++;
  508|  46.0M|                }
  509|  89.5M|            }
  510|   976M|        }
  511|   130M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (511:13): [True: 130M, False: 188k]
  ------------------
  512|   130M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  513|   130M|    }
  514|  14.3k|    return 0;
  515|  14.3k|}
jbig2_generic.c:jbig2_decode_generic_template1:
  589|  5.43k|{
  590|  5.43k|    const int64_t GBW = image->width;
  591|  5.43k|    const int64_t GBH = image->height;
  592|  5.43k|    const uint32_t rowstride = image->stride;
  593|  5.43k|    int64_t x, y;
  594|  5.43k|    byte *line2 = NULL;
  595|  5.43k|    byte *line1 = NULL;
  596|  5.43k|    byte *gbreg_line = (byte *) image->data;
  597|       |
  598|  5.43k|    (void) params;
  599|       |
  600|       |#ifdef OUTPUT_PBM
  601|       |    printf("P4\n%u %u\n",
  602|       |        (uint32_t) GBW,
  603|       |        (uint32_t) GBH);
  604|       |#endif
  605|       |
  606|  5.43k|    if (GBW <= 0)
  ------------------
  |  Branch (606:9): [True: 0, False: 5.43k]
  ------------------
  607|      0|        return 0;
  608|       |
  609|  65.1M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (609:17): [True: 65.1M, False: 5.43k]
  ------------------
  610|  65.1M|        uint32_t CONTEXT;
  611|  65.1M|        uint32_t line_m1;
  612|  65.1M|        uint32_t line_m2;
  613|  65.1M|        int64_t padded_width = (GBW + 7) & -8;
  614|       |
  615|  65.1M|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (615:19): [True: 65.1M, False: 5.43k]
  ------------------
  616|  65.1M|        line_m2 = line2 ? line2[0] << 5 : 0;
  ------------------
  |  Branch (616:19): [True: 65.1M, False: 10.5k]
  ------------------
  617|  65.1M|        CONTEXT = ((line_m1 >> 1) & 0x1f8) | ((line_m2 >> 1) & 0x1e00);
  618|       |
  619|       |        /* 6.2.5.7 3d */
  620|   249M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (620:21): [True: 184M, False: 65.1M]
  ------------------
  621|   184M|            byte result = 0;
  622|   184M|            int64_t x_minor;
  623|   184M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (623:35): [True: 118M, False: 65.1M]
  ------------------
  624|       |
  625|   184M|            if (line1)
  ------------------
  |  Branch (625:17): [True: 155M, False: 28.5M]
  ------------------
  626|   155M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (626:45): [True: 90.3M, False: 65.1M]
  ------------------
  627|       |
  628|   184M|            if (line2)
  ------------------
  |  Branch (628:17): [True: 143M, False: 40.4M]
  ------------------
  629|   143M|                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 5 : 0);
  ------------------
  |  Branch (629:45): [True: 78.5M, False: 65.1M]
  ------------------
  630|       |
  631|       |            /* This is the speed-critical inner loop. */
  632|  1.22G|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (632:31): [True: 1.03G, False: 184M]
  ------------------
  633|  1.03G|                int bit;
  634|       |
  635|  1.03G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  636|  1.03G|                if (bit < 0)
  ------------------
  |  Branch (636:21): [True: 0, False: 1.03G]
  ------------------
  637|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 optimized");
  638|  1.03G|                result |= bit << (7 - x_minor);
  639|  1.03G|                CONTEXT = ((CONTEXT & 0xefb) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x8) | ((line_m2 >> (8 - x_minor)) & 0x200);
  640|  1.03G|            }
  641|   184M|            gbreg_line[x >> 3] = result;
  642|   184M|        }
  643|       |#ifdef OUTPUT_PBM
  644|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  645|       |#endif
  646|  65.1M|        line2 = line1;
  647|  65.1M|        line1 = gbreg_line;
  648|  65.1M|        gbreg_line += rowstride;
  649|  65.1M|    }
  650|       |
  651|  5.43k|    return 0;
  652|  5.43k|}
jbig2_generic.c:jbig2_decode_generic_template1_unopt:
  521|  5.97k|{
  522|  5.97k|    const int64_t GBW = image->width;
  523|  5.97k|    const int64_t GBH = image->height;
  524|  5.97k|    uint32_t CONTEXT;
  525|  5.97k|    int64_t x, y;
  526|  5.97k|    int bit;
  527|       |
  528|  5.97k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  5.97k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 5.97k]
  |  |  |  Branch (440:20): [True: 3, False: 5.97k]
  |  |  |  Branch (440:31): [True: 0, False: 5.97k]
  |  |  |  Branch (440:46): [True: 4.94k, False: 1.02k]
  |  |  |  Branch (440:57): [True: 0, False: 4.94k]
  |  |  |  Branch (440:72): [True: 1.02k, False: 4.94k]
  |  |  |  Branch (440:84): [True: 5, False: 1.01k]
  |  |  ------------------
  ------------------
  529|      8|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  530|      8|                           "adaptive template pixel is out of field");
  531|       |
  532|   163M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (532:17): [True: 163M, False: 5.96k]
  ------------------
  533|   163M|        uint32_t out_byte = 0;
  534|   163M|        int out_bits_to_go_in_byte = 8;
  535|   163M|        uint8_t *d = &image->data[image->stride * y];
  536|   163M|        uint32_t pd = 0;
  537|   163M|        uint32_t ppd = 0;
  538|   163M|        uint8_t *pline = NULL;
  539|   163M|        uint8_t *ppline = NULL;
  540|   163M|        if (y >= 1) {
  ------------------
  |  Branch (540:13): [True: 163M, False: 5.96k]
  ------------------
  541|   163M|            pline  = &image->data[image->stride * (y-1)];
  542|   163M|            pd = (*pline++ << 8);
  543|   163M|            if (GBW > 8)
  ------------------
  |  Branch (543:17): [True: 794k, False: 163M]
  ------------------
  544|   794k|                pd |= *pline++;
  545|   163M|        }
  546|   163M|        if (y >= 2) {
  ------------------
  |  Branch (546:13): [True: 163M, False: 8.99k]
  ------------------
  547|   163M|            ppline = &image->data[image->stride * (y-2)];
  548|   163M|            ppd = (*ppline++ << 8);
  549|   163M|            if (GBW > 8)
  ------------------
  |  Branch (549:17): [True: 791k, False: 163M]
  ------------------
  550|   791k|                ppd |= *ppline++;
  551|   163M|        }
  552|  1.46G|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (552:21): [True: 1.30G, False: 163M]
  ------------------
  553|  1.30G|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (553:17): [True: 30.2M, False: 1.27G]
  |  Branch (553:36): [True: 30.2M, False: 16.9k]
  ------------------
  554|  30.2M|                bit = 0;
  555|  1.27G|            } else {
  556|  1.27G|                CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
  557|  1.27G|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
  558|  1.27G|                CONTEXT |= (pd>>9) & 0x01F0; /* Next 5 pixels */
  559|  1.27G|                CONTEXT |= (ppd>>4) & 0x1E00; /* Next 4 pixels */
  560|  1.27G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  561|  1.27G|                if (bit < 0)
  ------------------
  |  Branch (561:21): [True: 0, False: 1.27G]
  ------------------
  562|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 unoptimized");
  563|  1.27G|            }
  564|  1.30G|            pd = pd<<1;
  565|  1.30G|            ppd = ppd<<1;
  566|  1.30G|            out_byte = (out_byte<<1) | bit;
  567|  1.30G|            out_bits_to_go_in_byte--;
  568|  1.30G|            *d = out_byte<<out_bits_to_go_in_byte;
  569|  1.30G|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (569:17): [True: 139M, False: 1.16G]
  ------------------
  570|   139M|                out_bits_to_go_in_byte = 8;
  571|   139M|                d++;
  572|   139M|                if (x+9 < GBW && pline != NULL) {
  ------------------
  |  Branch (572:21): [True: 125M, False: 13.4M]
  |  Branch (572:34): [True: 53.3M, False: 72.5M]
  ------------------
  573|  53.3M|                    pd |= *pline++;
  574|  53.3M|                    if (ppline != NULL)
  ------------------
  |  Branch (574:25): [True: 43.5M, False: 9.76M]
  ------------------
  575|  43.5M|                        ppd |= *ppline++;
  576|  53.3M|                }
  577|   139M|            }
  578|  1.30G|        }
  579|   163M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (579:13): [True: 151M, False: 12.6M]
  ------------------
  580|   151M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  581|   163M|    }
  582|  5.96k|    return 0;
  583|  5.96k|}
jbig2_generic.c:jbig2_decode_generic_template2:
  725|  81.9k|{
  726|  81.9k|    const int64_t GBW = image->width;
  727|  81.9k|    const int64_t GBH = image->height;
  728|  81.9k|    const uint32_t rowstride = image->stride;
  729|  81.9k|    int64_t x, y;
  730|  81.9k|    byte *line2 = NULL;
  731|  81.9k|    byte *line1 = NULL;
  732|  81.9k|    byte *gbreg_line = (byte *) image->data;
  733|       |
  734|  81.9k|    (void) params;
  735|       |
  736|       |#ifdef OUTPUT_PBM
  737|       |    printf("P4\n%u %u\n",
  738|       |        (uint32_t) GBW,
  739|       |        (uint32_t) GBH);
  740|       |#endif
  741|       |
  742|  81.9k|    if (GBW <= 0)
  ------------------
  |  Branch (742:9): [True: 0, False: 81.9k]
  ------------------
  743|      0|        return 0;
  744|       |
  745|   132M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (745:17): [True: 131M, False: 81.9k]
  ------------------
  746|   131M|        uint32_t CONTEXT;
  747|   131M|        uint32_t line_m1;
  748|   131M|        uint32_t line_m2;
  749|   131M|        int64_t padded_width = (GBW + 7) & -8;
  750|       |
  751|   131M|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (751:19): [True: 131M, False: 81.9k]
  ------------------
  752|   131M|        line_m2 = line2 ? line2[0] << 4 : 0;
  ------------------
  |  Branch (752:19): [True: 131M, False: 94.0k]
  ------------------
  753|   131M|        CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380);
  754|       |
  755|       |        /* 6.2.5.7 3d */
  756|   368M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (756:21): [True: 236M, False: 131M]
  ------------------
  757|   236M|            byte result = 0;
  758|   236M|            int64_t x_minor;
  759|   236M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (759:35): [True: 104M, False: 131M]
  ------------------
  760|       |
  761|   236M|            if (line1)
  ------------------
  |  Branch (761:17): [True: 174M, False: 61.9M]
  ------------------
  762|   174M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (762:45): [True: 42.6M, False: 131M]
  ------------------
  763|       |
  764|   236M|            if (line2)
  ------------------
  |  Branch (764:17): [True: 144M, False: 91.8M]
  ------------------
  765|   144M|                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 4 : 0);
  ------------------
  |  Branch (765:45): [True: 12.7M, False: 131M]
  ------------------
  766|       |
  767|       |            /* This is the speed-critical inner loop. */
  768|  1.23G|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (768:31): [True: 1.00G, False: 236M]
  ------------------
  769|  1.00G|                int bit;
  770|       |
  771|  1.00G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  772|  1.00G|                if (bit < 0)
  ------------------
  |  Branch (772:21): [True: 0, False: 1.00G]
  ------------------
  773|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 optimized");
  774|  1.00G|                result |= bit << (7 - x_minor);
  775|  1.00G|                CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80);
  776|  1.00G|            }
  777|   236M|            gbreg_line[x >> 3] = result;
  778|   236M|        }
  779|       |#ifdef OUTPUT_PBM
  780|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  781|       |#endif
  782|   131M|        line2 = line1;
  783|   131M|        line1 = gbreg_line;
  784|   131M|        gbreg_line += rowstride;
  785|   131M|    }
  786|       |
  787|  81.9k|    return 0;
  788|  81.9k|}
jbig2_generic.c:jbig2_decode_generic_template2_unopt:
  658|  38.7k|{
  659|  38.7k|    const int64_t GBW = image->width;
  660|  38.7k|    const int64_t GBH = image->height;
  661|  38.7k|    uint32_t CONTEXT;
  662|  38.7k|    int64_t x, y;
  663|  38.7k|    int bit;
  664|       |
  665|  38.7k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  38.7k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 38.7k]
  |  |  |  Branch (440:20): [True: 2, False: 38.7k]
  |  |  |  Branch (440:31): [True: 0, False: 38.7k]
  |  |  |  Branch (440:46): [True: 37.6k, False: 1.10k]
  |  |  |  Branch (440:57): [True: 0, False: 37.6k]
  |  |  |  Branch (440:72): [True: 1.10k, False: 37.6k]
  |  |  |  Branch (440:84): [True: 5, False: 1.10k]
  |  |  ------------------
  ------------------
  666|      7|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  667|      7|                           "adaptive template pixel is out of field");
  668|       |
  669|   199M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (669:17): [True: 199M, False: 38.7k]
  ------------------
  670|   199M|        uint32_t out_byte = 0;
  671|   199M|        int out_bits_to_go_in_byte = 8;
  672|   199M|        uint8_t *d = &image->data[image->stride * y];
  673|   199M|        uint8_t *pline  = &image->data[image->stride * (y-1)];
  674|   199M|        uint8_t *ppline = &image->data[image->stride * (y-2)];
  675|   199M|        uint32_t pd = 0;
  676|   199M|        uint32_t ppd = 0;
  677|   199M|        if (y > 0) {
  ------------------
  |  Branch (677:13): [True: 199M, False: 38.7k]
  ------------------
  678|   199M|            pd = (*pline++ << 8);
  679|   199M|            if (GBW > 8)
  ------------------
  |  Branch (679:17): [True: 795k, False: 198M]
  ------------------
  680|   795k|                pd |= *pline++;
  681|   199M|            if (y > 1) {
  ------------------
  |  Branch (681:17): [True: 199M, False: 38.7k]
  ------------------
  682|   199M|                ppd = (*ppline++ << 8);
  683|   199M|                if (GBW > 8)
  ------------------
  |  Branch (683:21): [True: 757k, False: 198M]
  ------------------
  684|   757k|                    ppd |= *ppline++;
  685|   199M|            }
  686|   199M|        }
  687|  1.78G|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (687:21): [True: 1.58G, False: 199M]
  ------------------
  688|  1.58G|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (688:17): [True: 120M, False: 1.46G]
  |  Branch (688:36): [True: 120M, False: 49.7k]
  ------------------
  689|   120M|                bit = 0;
  690|  1.46G|            } else {
  691|  1.46G|                CONTEXT  = out_byte & 0x003; /* First 2 pixels */
  692|  1.46G|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
  693|  1.46G|                CONTEXT |= (pd>>11) & 0x078; /* Next 4 pixels */
  694|  1.46G|                CONTEXT |= (ppd>>7) & 0x380; /* Next 3 pixels */
  695|  1.46G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  696|  1.46G|                if (bit < 0)
  ------------------
  |  Branch (696:21): [True: 0, False: 1.46G]
  ------------------
  697|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 unoptimized");
  698|  1.46G|            }
  699|  1.58G|            pd = pd<<1;
  700|  1.58G|            ppd = ppd<<1;
  701|  1.58G|            out_byte = (out_byte<<1) | bit;
  702|  1.58G|            out_bits_to_go_in_byte--;
  703|  1.58G|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  704|  1.58G|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (704:17): [True: 154M, False: 1.42G]
  ------------------
  705|   154M|                out_bits_to_go_in_byte = 8;
  706|   154M|                d++;
  707|   154M|                if (x+9 < GBW && y > 0) {
  ------------------
  |  Branch (707:21): [True: 153M, False: 1.16M]
  |  Branch (707:34): [True: 79.4M, False: 74.1M]
  ------------------
  708|  79.4M|                    pd |= *pline++;
  709|  79.4M|                    if (y > 1)
  ------------------
  |  Branch (709:25): [True: 55.9M, False: 23.4M]
  ------------------
  710|  55.9M|                        ppd |= *ppline++;
  711|  79.4M|                }
  712|   154M|            }
  713|  1.58G|        }
  714|   199M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (714:13): [True: 199M, False: 326k]
  ------------------
  715|   199M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  716|   199M|    }
  717|       |
  718|  38.7k|    return 0;
  719|  38.7k|}
jbig2_generic.c:jbig2_decode_generic_template3:
  794|  4.73k|{
  795|  4.73k|    const int64_t GBW = image->width;
  796|  4.73k|    const int64_t GBH = image->height;
  797|  4.73k|    const uint32_t rowstride = image->stride;
  798|  4.73k|    byte *line1 = NULL;
  799|  4.73k|    byte *gbreg_line = (byte *) image->data;
  800|  4.73k|    int64_t x, y;
  801|       |
  802|  4.73k|    (void) params;
  803|       |
  804|       |#ifdef OUTPUT_PBM
  805|       |    printf("P4\n%u %u\n",
  806|       |        (uint32_t) GBW,
  807|       |        (uint32_t) GBH);
  808|       |#endif
  809|       |
  810|  4.73k|    if (GBW <= 0)
  ------------------
  |  Branch (810:9): [True: 0, False: 4.73k]
  ------------------
  811|      0|        return 0;
  812|       |
  813|  78.7k|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (813:17): [True: 74.0k, False: 4.73k]
  ------------------
  814|  74.0k|        uint32_t CONTEXT;
  815|  74.0k|        uint32_t line_m1;
  816|  74.0k|        int64_t padded_width = (GBW + 7) & -8;
  817|       |
  818|  74.0k|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (818:19): [True: 69.2k, False: 4.73k]
  ------------------
  819|  74.0k|        CONTEXT = (line_m1 >> 1) & 0x3f0;
  820|       |
  821|       |        /* 6.2.5.7 3d */
  822|   108M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (822:21): [True: 108M, False: 74.0k]
  ------------------
  823|   108M|            byte result = 0;
  824|   108M|            int64_t x_minor;
  825|   108M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (825:35): [True: 108M, False: 74.0k]
  ------------------
  826|       |
  827|   108M|            if (line1)
  ------------------
  |  Branch (827:17): [True: 80.6M, False: 27.9M]
  ------------------
  828|  80.6M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (828:45): [True: 80.5M, False: 69.2k]
  ------------------
  829|       |
  830|       |            /* This is the speed-critical inner loop. */
  831|   976M|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (831:31): [True: 868M, False: 108M]
  ------------------
  832|   868M|                int bit;
  833|       |
  834|   868M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  835|   868M|                if (bit < 0)
  ------------------
  |  Branch (835:21): [True: 0, False: 868M]
  ------------------
  836|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 optimized");
  837|   868M|                result |= bit << (7 - x_minor);
  838|   868M|                CONTEXT = ((CONTEXT & 0x1f7) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x10);
  839|   868M|            }
  840|   108M|            gbreg_line[x >> 3] = result;
  841|   108M|        }
  842|       |#ifdef OUTPUT_PBM
  843|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  844|       |#endif
  845|  74.0k|        line1 = gbreg_line;
  846|  74.0k|        gbreg_line += rowstride;
  847|  74.0k|    }
  848|       |
  849|  4.73k|    return 0;
  850|  4.73k|}
jbig2_generic.c:jbig2_decode_generic_template3_unopt:
  856|  6.82k|{
  857|  6.82k|    const int64_t GBW = image->width;
  858|  6.82k|    const int64_t GBH = image->height;
  859|  6.82k|    uint32_t CONTEXT;
  860|  6.82k|    int64_t x, y;
  861|  6.82k|    int bit;
  862|       |
  863|  6.82k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  6.82k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 6.82k]
  |  |  |  Branch (440:20): [True: 2, False: 6.82k]
  |  |  |  Branch (440:31): [True: 0, False: 6.82k]
  |  |  |  Branch (440:46): [True: 2.81k, False: 4.00k]
  |  |  |  Branch (440:57): [True: 0, False: 2.81k]
  |  |  |  Branch (440:72): [True: 4.00k, False: 2.81k]
  |  |  |  Branch (440:84): [True: 4, False: 4.00k]
  |  |  ------------------
  ------------------
  864|      6|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  865|      6|                           "adaptive template pixel is out of field");
  866|       |
  867|   106M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (867:17): [True: 106M, False: 6.81k]
  ------------------
  868|   106M|        uint32_t out_byte = 0;
  869|   106M|        int out_bits_to_go_in_byte = 8;
  870|   106M|        uint8_t *d = &image->data[image->stride * y];
  871|   106M|        uint8_t *pline  = &image->data[image->stride * (y-1)];
  872|   106M|        uint32_t pd = 0;
  873|   106M|        if (y > 0) {
  ------------------
  |  Branch (873:13): [True: 106M, False: 6.81k]
  ------------------
  874|   106M|            pd = (*pline++ << 8);
  875|   106M|            if (GBW > 8)
  ------------------
  |  Branch (875:17): [True: 1.03M, False: 105M]
  ------------------
  876|  1.03M|                pd |= *pline++;
  877|   106M|        }
  878|  1.04G|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (878:21): [True: 941M, False: 106M]
  ------------------
  879|   941M|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (879:17): [True: 38.9M, False: 902M]
  |  Branch (879:36): [True: 38.9M, False: 6.02k]
  ------------------
  880|  38.9M|                bit = 0;
  881|   902M|            } else {
  882|   902M|                CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
  883|   902M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
  884|   902M|                CONTEXT |= (pd>>9) & 0x3E0; /* Next 5 pixels */
  885|   902M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  886|   902M|                if (bit < 0)
  ------------------
  |  Branch (886:21): [True: 0, False: 902M]
  ------------------
  887|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 unoptimized");
  888|   902M|            }
  889|   941M|            pd = pd<<1;
  890|   941M|            out_byte = (out_byte<<1) | bit;
  891|   941M|            out_bits_to_go_in_byte--;
  892|   941M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  893|   941M|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (893:17): [True: 99.6M, False: 841M]
  ------------------
  894|  99.6M|                out_bits_to_go_in_byte = 8;
  895|  99.6M|                d++;
  896|  99.6M|                if (x+9 < GBW && y > 0)
  ------------------
  |  Branch (896:21): [True: 95.5M, False: 4.11M]
  |  Branch (896:34): [True: 40.2M, False: 55.3M]
  ------------------
  897|  40.2M|                    pd |= *pline++;
  898|  99.6M|            }
  899|   941M|        }
  900|   106M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (900:13): [True: 103M, False: 3.07M]
  ------------------
  901|   103M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  902|   106M|    }
  903|  6.81k|    return 0;
  904|  6.81k|}

jbig2_hd_release:
  109|  2.96k|{
  110|  2.96k|    int i;
  111|       |
  112|  2.96k|    if (dict == NULL)
  ------------------
  |  Branch (112:9): [True: 0, False: 2.96k]
  ------------------
  113|      0|        return;
  114|  2.96k|    if (dict->patterns != NULL)
  ------------------
  |  Branch (114:9): [True: 2.96k, False: 0]
  ------------------
  115|  10.7M|        for (i = 0; i < dict->n_patterns; i++)
  ------------------
  |  Branch (115:21): [True: 10.7M, False: 2.96k]
  ------------------
  116|  10.7M|            jbig2_image_release(ctx, dict->patterns[i]);
  117|  2.96k|    jbig2_free(ctx->allocator, dict->patterns);
  118|  2.96k|    jbig2_free(ctx->allocator, dict);
  119|  2.96k|}
jbig2_pattern_dictionary:
  200|  4.04k|{
  201|  4.04k|    Jbig2PatternDictParams params;
  202|  4.04k|    Jbig2ArithCx *GB_stats = NULL;
  203|  4.04k|    byte flags;
  204|  4.04k|    int offset = 0;
  205|       |
  206|       |    /* 7.4.4.1 - Data header */
  207|  4.04k|    if (segment->data_length < 7) {
  ------------------
  |  Branch (207:9): [True: 10, False: 4.03k]
  ------------------
  208|     10|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  209|     10|    }
  210|  4.03k|    flags = segment_data[0];
  211|  4.03k|    params.HDMMR = flags & 1;
  212|  4.03k|    params.HDTEMPLATE = (flags & 6) >> 1;
  213|  4.03k|    params.HDPW = segment_data[1];
  214|  4.03k|    params.HDPH = segment_data[2];
  215|  4.03k|    params.GRAYMAX = jbig2_get_uint32(segment_data + 3);
  216|  4.03k|    offset += 7;
  217|       |
  218|  4.03k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  219|  4.03k|                "pattern dictionary, flags=%02x, %d grays (%dx%d cell)", flags, params.GRAYMAX + 1, params.HDPW, params.HDPH);
  220|       |
  221|  4.03k|    if (params.HDMMR && params.HDTEMPLATE) {
  ------------------
  |  Branch (221:9): [True: 3.67k, False: 361]
  |  Branch (221:25): [True: 3.14k, False: 530]
  ------------------
  222|  3.14k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HDTEMPLATE is %d when HDMMR is %d, contrary to spec", params.HDTEMPLATE, params.HDMMR);
  223|  3.14k|    }
  224|  4.03k|    if (flags & 0xf8) {
  ------------------
  |  Branch (224:9): [True: 3.52k, False: 513]
  ------------------
  225|  3.52k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved flag bits non-zero");
  226|  3.52k|    }
  227|       |
  228|       |    /* 7.4.4.2 */
  229|  4.03k|    if (!params.HDMMR) {
  ------------------
  |  Branch (229:9): [True: 361, False: 3.67k]
  ------------------
  230|       |        /* allocate and zero arithmetic coding stats */
  231|    361|        int stats_size = jbig2_generic_stats_size(ctx, params.HDTEMPLATE);
  232|       |
  233|    361|        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|    361|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  234|    361|        if (GB_stats == NULL)
  ------------------
  |  Branch (234:13): [True: 1, False: 360]
  ------------------
  235|      1|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling pattern dictionary");
  236|    360|        memset(GB_stats, 0, stats_size);
  237|    360|    }
  238|       |
  239|  4.03k|    segment->result = jbig2_decode_pattern_dict(ctx, segment, &params, segment_data + offset, segment->data_length - offset, GB_stats);
  240|       |
  241|       |    /* todo: retain GB_stats? */
  242|  4.03k|    if (!params.HDMMR) {
  ------------------
  |  Branch (242:9): [True: 360, False: 3.67k]
  ------------------
  243|    360|        jbig2_free(ctx->allocator, GB_stats);
  244|    360|    }
  245|       |
  246|  4.03k|    return (segment->result != NULL) ? 0 : -1;
  ------------------
  |  Branch (246:12): [True: 2.96k, False: 1.07k]
  ------------------
  247|  4.03k|}
jbig2_halftone_region:
  560|  2.21k|{
  561|  2.21k|    int offset = 0;
  562|  2.21k|    Jbig2RegionSegmentInfo region_info;
  563|  2.21k|    Jbig2HalftoneRegionParams params;
  564|  2.21k|    Jbig2Image *image = NULL;
  565|  2.21k|    Jbig2ArithCx *GB_stats = NULL;
  566|  2.21k|    int code = 0;
  567|       |
  568|       |    /* 7.4.5.1 */
  569|  2.21k|    if (segment->data_length < 17)
  ------------------
  |  Branch (569:9): [True: 5, False: 2.20k]
  ------------------
  570|      5|        goto too_short;
  571|  2.20k|    jbig2_get_region_segment_info(&region_info, segment_data);
  572|  2.20k|    offset += 17;
  573|       |
  574|  2.20k|    if (segment->data_length < 18)
  ------------------
  |  Branch (574:9): [True: 1, False: 2.20k]
  ------------------
  575|      1|        goto too_short;
  576|       |
  577|       |    /* 7.4.5.1.1 Figure 42 */
  578|  2.20k|    params.flags = segment_data[offset];
  579|  2.20k|    params.HMMR = params.flags & 1;
  580|  2.20k|    params.HTEMPLATE = (params.flags & 6) >> 1;
  581|  2.20k|    params.HENABLESKIP = (params.flags & 8) >> 3;
  582|  2.20k|    params.HCOMBOP = (Jbig2ComposeOp)((params.flags & 0x70) >> 4);
  583|  2.20k|    params.HDEFPIXEL = (params.flags & 0x80) >> 7;
  584|  2.20k|    offset += 1;
  585|       |
  586|  2.20k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  587|  2.20k|                "halftone region: %u x %u @ (%u, %u), flags = %02x", region_info.width, region_info.height, region_info.x, region_info.y, params.flags);
  588|       |
  589|  2.20k|    if (params.HMMR && params.HTEMPLATE) {
  ------------------
  |  Branch (589:9): [True: 1.83k, False: 371]
  |  Branch (589:24): [True: 110, False: 1.72k]
  ------------------
  590|    110|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HTEMPLATE is %d when HMMR is %d, contrary to spec", params.HTEMPLATE, params.HMMR);
  591|    110|    }
  592|  2.20k|    if (params.HMMR && params.HENABLESKIP) {
  ------------------
  |  Branch (592:9): [True: 1.83k, False: 371]
  |  Branch (592:24): [True: 83, False: 1.75k]
  ------------------
  593|     83|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HENABLESKIP is %d when HMMR is %d, contrary to spec", params.HENABLESKIP, params.HMMR);
  594|     83|    }
  595|       |
  596|       |    /* 7.4.5.1.2 Figure 43 */
  597|  2.20k|    if (segment->data_length - offset < 16)
  ------------------
  |  Branch (597:9): [True: 8, False: 2.20k]
  ------------------
  598|      8|        goto too_short;
  599|  2.20k|    params.HGW = jbig2_get_uint32(segment_data + offset);
  600|  2.20k|    params.HGH = jbig2_get_uint32(segment_data + offset + 4);
  601|  2.20k|    params.HGX = jbig2_get_int32(segment_data + offset + 8);
  602|  2.20k|    params.HGY = jbig2_get_int32(segment_data + offset + 12);
  603|  2.20k|    offset += 16;
  604|       |
  605|       |    /* 7.4.5.1.3 Figure 44 */
  606|  2.20k|    if (segment->data_length - offset < 4)
  ------------------
  |  Branch (606:9): [True: 2, False: 2.19k]
  ------------------
  607|      2|        goto too_short;
  608|  2.19k|    params.HRX = jbig2_get_uint16(segment_data + offset);
  609|  2.19k|    params.HRY = jbig2_get_uint16(segment_data + offset + 2);
  610|  2.19k|    offset += 4;
  611|       |
  612|  2.19k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  613|  2.19k|                "grid %d x %d @ (%d.%d,%d.%d) vector (%d.%d,%d.%d)",
  614|  2.19k|                params.HGW, params.HGH,
  615|  2.19k|                params.HGX >> 8, params.HGX & 0xff,
  616|  2.19k|                params.HGY >> 8, params.HGY & 0xff,
  617|  2.19k|                params.HRX >> 8, params.HRX & 0xff,
  618|  2.19k|                params.HRY >> 8, params.HRY & 0xff);
  619|       |
  620|       |    /* 7.4.5.2 */
  621|  2.19k|    if (!params.HMMR) {
  ------------------
  |  Branch (621:9): [True: 367, False: 1.83k]
  ------------------
  622|       |        /* allocate and zero arithmetic coding stats */
  623|    367|        int stats_size = jbig2_generic_stats_size(ctx, params.HTEMPLATE);
  624|       |
  625|    367|        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|    367|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  626|    367|        if (GB_stats == NULL) {
  ------------------
  |  Branch (626:13): [True: 1, False: 366]
  ------------------
  627|      1|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder states in halftone region");
  628|      1|        }
  629|    366|        memset(GB_stats, 0, stats_size);
  630|    366|    }
  631|       |
  632|  2.19k|    image = jbig2_image_new(ctx, region_info.width, region_info.height);
  633|  2.19k|    if (image == NULL) {
  ------------------
  |  Branch (633:9): [True: 17, False: 2.18k]
  ------------------
  634|     17|        jbig2_free(ctx->allocator, GB_stats);
  635|     17|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate halftone image");
  636|     17|    }
  637|       |
  638|  2.18k|    code = jbig2_decode_halftone_region(ctx, segment, &params, segment_data + offset, segment->data_length - offset, image, GB_stats);
  639|  2.18k|    if (code < 0) {
  ------------------
  |  Branch (639:9): [True: 177, False: 2.00k]
  ------------------
  640|    177|        jbig2_image_release(ctx, image);
  641|    177|        jbig2_free(ctx->allocator, GB_stats);
  642|    177|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode halftone region");
  643|    177|    }
  644|       |
  645|       |    /* todo: retain GB_stats? */
  646|  2.00k|    if (!params.HMMR) {
  ------------------
  |  Branch (646:9): [True: 286, False: 1.71k]
  ------------------
  647|    286|        jbig2_free(ctx->allocator, GB_stats);
  648|    286|    }
  649|       |
  650|  2.00k|    code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, region_info.x, region_info.y, region_info.op);
  651|  2.00k|    if (code < 0) {
  ------------------
  |  Branch (651:9): [True: 141, False: 1.86k]
  ------------------
  652|    141|        jbig2_image_release(ctx, image);
  653|    141|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add halftone region to page");
  654|    141|    }
  655|       |
  656|  1.86k|    jbig2_image_release(ctx, image);
  657|       |
  658|  1.86k|    return code;
  659|       |
  660|     16|too_short:
  661|     16|    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  662|  2.00k|}
jbig2_halftone.c:jbig2_decode_pattern_dict:
  140|  4.03k|{
  141|  4.03k|    Jbig2PatternDict *hd = NULL;
  142|  4.03k|    Jbig2Image *image = NULL;
  143|  4.03k|    Jbig2GenericRegionParams rparams;
  144|  4.03k|    int code = 0;
  145|       |
  146|       |    /* allocate the collective image */
  147|  4.03k|    image = jbig2_image_new(ctx, params->HDPW * (params->GRAYMAX + 1), params->HDPH);
  148|  4.03k|    if (image == NULL) {
  ------------------
  |  Branch (148:9): [True: 48, False: 3.99k]
  ------------------
  149|     48|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate collective bitmap for halftone dictionary");
  150|     48|        return NULL;
  151|     48|    }
  152|       |
  153|       |    /* fill out the generic region decoder parameters */
  154|  3.99k|    rparams.MMR = params->HDMMR;
  155|  3.99k|    rparams.GBTEMPLATE = params->HDTEMPLATE;
  156|  3.99k|    rparams.TPGDON = 0;         /* not used if HDMMR = 1 */
  157|  3.99k|    rparams.USESKIP = 0;
  158|  3.99k|    rparams.gbat[0] = -(int8_t) params->HDPW;
  159|  3.99k|    rparams.gbat[1] = 0;
  160|  3.99k|    rparams.gbat[2] = -3;
  161|  3.99k|    rparams.gbat[3] = -1;
  162|  3.99k|    rparams.gbat[4] = 2;
  163|  3.99k|    rparams.gbat[5] = -2;
  164|  3.99k|    rparams.gbat[6] = -2;
  165|  3.99k|    rparams.gbat[7] = -2;
  166|       |
  167|  3.99k|    if (params->HDMMR) {
  ------------------
  |  Branch (167:9): [True: 3.64k, False: 347]
  ------------------
  168|  3.64k|        code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data, size, image);
  169|  3.64k|    } else {
  170|    347|        Jbig2WordStream *ws = jbig2_word_stream_buf_new(ctx, data, size);
  171|       |
  172|    347|        if (ws != NULL) {
  ------------------
  |  Branch (172:13): [True: 346, False: 1]
  ------------------
  173|    346|            Jbig2ArithState *as = jbig2_arith_new(ctx, ws);
  174|       |
  175|    346|            if (as != NULL) {
  ------------------
  |  Branch (175:17): [True: 341, False: 5]
  ------------------
  176|    341|                code = jbig2_decode_generic_region(ctx, segment, &rparams, as, image, GB_stats);
  177|    341|            } else {
  178|      5|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling halftone dictionary");
  179|      5|            }
  180|       |
  181|    346|            jbig2_free(ctx->allocator, as);
  182|    346|            jbig2_word_stream_buf_free(ctx, ws);
  183|    346|        } else {
  184|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate word stream when handling halftone dictionary");
  185|      1|        }
  186|    347|    }
  187|       |
  188|  3.99k|    if (code == 0)
  ------------------
  |  Branch (188:9): [True: 3.80k, False: 187]
  ------------------
  189|  3.80k|        hd = jbig2_hd_new(ctx, params, image);
  190|    187|    else
  191|    187|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode immediate generic region");
  192|  3.99k|    jbig2_image_release(ctx, image);
  193|       |
  194|  3.99k|    return hd;
  195|  4.03k|}
jbig2_halftone.c:jbig2_hd_new:
   44|  3.80k|{
   45|  3.80k|    Jbig2PatternDict *new;
   46|  3.80k|    const uint32_t N = params->GRAYMAX + 1;
   47|  3.80k|    const uint32_t HPW = params->HDPW;
   48|  3.80k|    const uint32_t HPH = params->HDPH;
   49|  3.80k|    int code;
   50|  3.80k|    uint32_t i, j;
   51|       |
   52|  3.80k|    if (N == 0) {
  ------------------
  |  Branch (52:9): [True: 0, False: 3.80k]
  ------------------
   53|       |        /* We've wrapped. */
   54|      0|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "params->GRAYMAX out of range");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   55|      0|        return NULL;
   56|      0|    }
   57|       |
   58|       |    /* allocate a new struct */
   59|  3.80k|    new = jbig2_new(ctx, Jbig2PatternDict, 1);
  ------------------
  |  |  141|  3.80k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   60|  3.80k|    if (new != NULL) {
  ------------------
  |  Branch (60:9): [True: 3.80k, False: 1]
  ------------------
   61|  3.80k|        new->patterns = jbig2_new(ctx, Jbig2Image *, N);
  ------------------
  |  |  141|  3.80k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   62|  3.80k|        if (new->patterns == NULL) {
  ------------------
  |  Branch (62:13): [True: 723, False: 3.07k]
  ------------------
   63|    723|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate pattern in collective bitmap dictionary");
  ------------------
  |  |   68|    723|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   64|    723|            jbig2_free(ctx->allocator, new);
   65|    723|            return NULL;
   66|    723|        }
   67|  3.07k|        new->n_patterns = N;
   68|  3.07k|        new->HPW = HPW;
   69|  3.07k|        new->HPH = HPH;
   70|       |
   71|       |        /* 6.7.5(4) - copy out the individual pattern images */
   72|  17.8M|        for (i = 0; i < N; i++) {
  ------------------
  |  Branch (72:21): [True: 17.8M, False: 2.96k]
  ------------------
   73|  17.8M|            new->patterns[i] = jbig2_image_new(ctx, HPW, HPH);
   74|  17.8M|            if (new->patterns[i] == NULL) {
  ------------------
  |  Branch (74:17): [True: 117, False: 17.8M]
  ------------------
   75|    117|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate pattern element image");
  ------------------
  |  |   68|    117|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   76|       |                /* new->patterns[i] above did not succeed, so releasing patterns 0..i-1 is enough */
   77|  7.12M|                for (j = 0; j < i; j++)
  ------------------
  |  Branch (77:29): [True: 7.12M, False: 117]
  ------------------
   78|  7.12M|                    jbig2_image_release(ctx, new->patterns[j]);
   79|    117|                jbig2_free(ctx->allocator, new->patterns);
   80|    117|                jbig2_free(ctx->allocator, new);
   81|    117|                return NULL;
   82|    117|            }
   83|       |            /* compose with the REPLACE operator; the source
   84|       |               will be clipped to the destination, selecting the
   85|       |               proper sub image */
   86|  17.8M|            code = jbig2_image_compose(ctx, new->patterns[i], image, -((int64_t) i) * HPW, 0, JBIG2_COMPOSE_REPLACE);
   87|  17.8M|            if (code < 0) {
  ------------------
  |  Branch (87:17): [True: 0, False: 17.8M]
  ------------------
   88|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to compose image into collective bitmap dictionary");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   89|       |                /* new->patterns[i] above succeeded, so release all patterns 0..i */
   90|      0|                for (j = 0; j <= i; j++)
  ------------------
  |  Branch (90:29): [True: 0, False: 0]
  ------------------
   91|      0|                    jbig2_image_release(ctx, new->patterns[j]);
   92|      0|                jbig2_free(ctx->allocator, new->patterns);
   93|      0|                jbig2_free(ctx->allocator, new);
   94|      0|                return NULL;
   95|      0|            }
   96|  17.8M|        }
   97|  3.07k|    } else {
   98|      1|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate collective bitmap dictionary");
  ------------------
  |  |   68|      1|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   99|      1|    }
  100|       |
  101|  2.96k|    return new;
  102|  3.80k|}
jbig2_halftone.c:jbig2_decode_halftone_region:
  464|  2.18k|{
  465|  2.18k|    uint32_t HBPP;
  466|  2.18k|    uint32_t HNUMPATS;
  467|  2.18k|    uint16_t **GI = NULL;
  468|  2.18k|    Jbig2Image *HSKIP = NULL;
  469|  2.18k|    Jbig2PatternDict *HPATS;
  470|  2.18k|    uint32_t i;
  471|  2.18k|    int64_t mg, ng;
  472|  2.18k|    uint16_t gray_val;
  473|  2.18k|    int code = 0;
  474|       |
  475|       |    /* We need the patterns used in this region, get them from the referred pattern dictionary */
  476|  2.18k|    HPATS = jbig2_decode_ht_region_get_hpats(ctx, segment);
  477|  2.18k|    if (!HPATS) {
  ------------------
  |  Branch (477:9): [True: 17, False: 2.16k]
  ------------------
  478|     17|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "no pattern dictionary found, skipping halftone image");
  479|     17|        goto cleanup;
  480|     17|    }
  481|       |
  482|       |    /* 6.6.5 point 1. Fill bitmap with HDEFPIXEL */
  483|  2.16k|    memset(image->data, params->HDEFPIXEL, image->stride * image->height);
  484|       |
  485|       |    /* 6.6.5 point 2. compute HSKIP according to 6.6.5.1 */
  486|  2.16k|    if (params->HENABLESKIP == 1) {
  ------------------
  |  Branch (486:9): [True: 363, False: 1.80k]
  ------------------
  487|    363|        HSKIP = jbig2_image_new(ctx, params->HGW, params->HGH);
  488|    363|        if (HSKIP == NULL)
  ------------------
  |  Branch (488:13): [True: 3, False: 360]
  ------------------
  489|      3|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate skip image");
  490|       |
  491|   211M|        for (mg = 0; mg < params->HGH; ++mg) {
  ------------------
  |  Branch (491:22): [True: 211M, False: 360]
  ------------------
  492|  1.69G|            for (ng = 0; ng < params->HGW; ++ng) {
  ------------------
  |  Branch (492:26): [True: 1.48G, False: 211M]
  ------------------
  493|  1.48G|                int64_t x = ((int64_t) params->HGX + mg * params->HRY + ng * params->HRX) >> 8;
  494|  1.48G|                int64_t y = ((int64_t) params->HGY + mg * params->HRX - ng * params->HRY) >> 8;
  495|       |
  496|  1.48G|                if (x + HPATS->HPW <= 0 || x >= image->width || y + HPATS->HPH <= 0 || y >= image->height) {
  ------------------
  |  Branch (496:21): [True: 63.9M, False: 1.41G]
  |  Branch (496:44): [True: 1.18G, False: 235M]
  |  Branch (496:65): [True: 124M, False: 111M]
  |  Branch (496:88): [True: 110M, False: 501k]
  ------------------
  497|  1.48G|                    jbig2_image_set_pixel(HSKIP, ng, mg, 1);
  498|  1.48G|                } else {
  499|   501k|                    jbig2_image_set_pixel(HSKIP, ng, mg, 0);
  500|   501k|                }
  501|  1.48G|            }
  502|   211M|        }
  503|    360|    }
  504|       |
  505|       |    /* 6.6.5 point 3. set HBPP to ceil(log2(HNUMPATS)): */
  506|  2.16k|    HNUMPATS = HPATS->n_patterns;
  507|  2.16k|    HBPP = 0;
  508|  17.3k|    while (HNUMPATS > (1U << ++HBPP));
  ------------------
  |  Branch (508:12): [True: 15.1k, False: 2.16k]
  ------------------
  509|  2.16k|    if (HBPP > 16) {
  ------------------
  |  Branch (509:9): [True: 3, False: 2.15k]
  ------------------
  510|      3|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "HBPP is larger than supported (%u)", HBPP);
  511|      3|        goto cleanup;
  512|      3|    }
  513|       |
  514|       |    /* 6.6.5 point 4. decode gray-scale image as mentioned in annex C */
  515|  2.15k|    GI = jbig2_decode_gray_scale_image(ctx, segment, data, size,
  516|  2.15k|                                       params->HMMR, params->HGW, params->HGH, HBPP, params->HENABLESKIP, HSKIP, params->HTEMPLATE, GB_stats);
  517|  2.15k|    if (!GI) {
  ------------------
  |  Branch (517:9): [True: 154, False: 2.00k]
  ------------------
  518|    154|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to acquire gray-scale image, skipping halftone image");
  519|    154|        goto cleanup;
  520|    154|    }
  521|       |
  522|       |    /* 6.6.5 point 5. place patterns with procedure mentioned in 6.6.5.2 */
  523|  12.5M|    for (mg = 0; mg < params->HGH; ++mg) {
  ------------------
  |  Branch (523:18): [True: 12.5M, False: 2.00k]
  ------------------
  524|   112M|        for (ng = 0; ng < params->HGW; ++ng) {
  ------------------
  |  Branch (524:22): [True: 99.7M, False: 12.5M]
  ------------------
  525|  99.7M|            int64_t x = ((int64_t) params->HGX + mg * params->HRY + ng * params->HRX) >> 8;
  526|  99.7M|            int64_t y = ((int64_t) params->HGY + mg * params->HRX - ng * params->HRY) >> 8;
  527|       |
  528|       |            /* prevent pattern index >= HNUMPATS */
  529|  99.7M|            gray_val = GI[ng][mg];
  530|  99.7M|            if (gray_val >= HNUMPATS) {
  ------------------
  |  Branch (530:17): [True: 8.40M, False: 91.3M]
  ------------------
  531|  8.40M|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "gray-scale index %d out of range, using largest index", gray_val);
  532|       |                /* use highest available pattern */
  533|  8.40M|                gray_val = HNUMPATS - 1;
  534|  8.40M|            }
  535|  99.7M|            code = jbig2_image_compose(ctx, image, HPATS->patterns[gray_val], x, y, params->HCOMBOP);
  536|  99.7M|            if (code < 0) {
  ------------------
  |  Branch (536:17): [True: 0, False: 99.7M]
  ------------------
  537|      0|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to compose pattern with gray-scale image");
  538|      0|                goto cleanup;
  539|      0|            }
  540|  99.7M|        }
  541|  12.5M|    }
  542|       |
  543|  2.17k|cleanup:
  544|  2.17k|    if (GI) {
  ------------------
  |  Branch (544:9): [True: 2.00k, False: 174]
  ------------------
  545|  5.42M|        for (i = 0; i < params->HGW; ++i) {
  ------------------
  |  Branch (545:21): [True: 5.41M, False: 2.00k]
  ------------------
  546|  5.41M|            jbig2_free(ctx->allocator, GI[i]);
  547|  5.41M|        }
  548|  2.00k|    }
  549|  2.17k|    jbig2_free(ctx->allocator, GI);
  550|  2.17k|    jbig2_image_release(ctx, HSKIP);
  551|       |
  552|  2.17k|    return code;
  553|  2.00k|}
jbig2_halftone.c:jbig2_decode_ht_region_get_hpats:
  425|  2.18k|{
  426|  2.18k|    int index = 0;
  427|  2.18k|    Jbig2PatternDict *pattern_dict = NULL;
  428|  2.18k|    Jbig2Segment *rsegment = NULL;
  429|       |
  430|       |    /* loop through all referred segments */
  431|  2.85k|    while (!pattern_dict && segment->referred_to_segment_count > index) {
  ------------------
  |  Branch (431:12): [True: 2.85k, False: 0]
  |  Branch (431:29): [True: 2.83k, False: 17]
  ------------------
  432|  2.83k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  433|  2.83k|        if (rsegment) {
  ------------------
  |  Branch (433:13): [True: 2.52k, False: 317]
  ------------------
  434|       |            /* segment type is pattern dictionary and result is not empty */
  435|  2.52k|            if ((rsegment->flags & 0x3f) == 16 && rsegment->result) {
  ------------------
  |  Branch (435:17): [True: 2.16k, False: 359]
  |  Branch (435:51): [True: 2.16k, False: 0]
  ------------------
  436|  2.16k|                pattern_dict = (Jbig2PatternDict *) rsegment->result;
  437|  2.16k|                return pattern_dict;
  438|  2.16k|            }
  439|  2.52k|        }
  440|    676|        index++;
  441|    676|    }
  442|     17|    return pattern_dict;
  443|  2.18k|}
jbig2_halftone.c:jbig2_decode_gray_scale_image:
  275|  2.15k|{
  276|  2.15k|    uint16_t **GSVALS = NULL;
  277|  2.15k|    size_t consumed_bytes = 0;
  278|  2.15k|    uint32_t i, j, stride;
  279|  2.15k|    int64_t x, y;
  280|  2.15k|    int code;
  281|  2.15k|    Jbig2Image **GSPLANES;
  282|  2.15k|    Jbig2GenericRegionParams rparams;
  283|  2.15k|    Jbig2WordStream *ws = NULL;
  284|  2.15k|    Jbig2ArithState *as = NULL;
  285|       |
  286|       |    /* allocate GSPLANES */
  287|  2.15k|    GSPLANES = jbig2_new(ctx, Jbig2Image *, GSBPP);
  ------------------
  |  |  141|  2.15k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  288|  2.15k|    if (GSPLANES == NULL) {
  ------------------
  |  Branch (288:9): [True: 1, False: 2.15k]
  ------------------
  289|      1|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate %d bytes for GSPLANES", GSBPP);
  290|      1|        return NULL;
  291|      1|    }
  292|       |
  293|  19.2k|    for (i = 0; i < GSBPP; ++i) {
  ------------------
  |  Branch (293:17): [True: 17.1k, False: 2.13k]
  ------------------
  294|  17.1k|        GSPLANES[i] = jbig2_image_new(ctx, GSW, GSH);
  295|  17.1k|        if (GSPLANES[i] == NULL) {
  ------------------
  |  Branch (295:13): [True: 22, False: 17.0k]
  ------------------
  296|     22|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate %dx%d image for GSPLANES", GSW, GSH);
  297|       |            /* free already allocated */
  298|     72|            for (j = i; j > 0;)
  ------------------
  |  Branch (298:25): [True: 50, False: 22]
  ------------------
  299|     50|                jbig2_image_release(ctx, GSPLANES[--j]);
  300|     22|            jbig2_free(ctx->allocator, GSPLANES);
  301|     22|            return NULL;
  302|     22|        }
  303|  17.1k|    }
  304|       |
  305|       |    /* C.5 step 1. Decode GSPLANES[GSBPP-1] */
  306|       |    /* fill generic region decoder parameters */
  307|  2.13k|    rparams.MMR = GSMMR;
  308|  2.13k|    rparams.GBTEMPLATE = GSTEMPLATE;
  309|  2.13k|    rparams.TPGDON = 0;
  310|  2.13k|    rparams.USESKIP = GSUSESKIP;
  311|  2.13k|    rparams.SKIP = GSKIP;
  312|  2.13k|    rparams.gbat[0] = (GSTEMPLATE <= 1 ? 3 : 2);
  ------------------
  |  Branch (312:24): [True: 1.91k, False: 215]
  ------------------
  313|  2.13k|    rparams.gbat[1] = -1;
  314|  2.13k|    rparams.gbat[2] = -3;
  315|  2.13k|    rparams.gbat[3] = -1;
  316|  2.13k|    rparams.gbat[4] = 2;
  317|  2.13k|    rparams.gbat[5] = -2;
  318|  2.13k|    rparams.gbat[6] = -2;
  319|  2.13k|    rparams.gbat[7] = -2;
  320|       |
  321|  2.13k|    if (GSMMR) {
  ------------------
  |  Branch (321:9): [True: 1.80k, False: 334]
  ------------------
  322|  1.80k|        code = jbig2_decode_halftone_mmr(ctx, &rparams, data, size, GSPLANES[GSBPP - 1], &consumed_bytes);
  323|  1.80k|    } else {
  324|    334|        ws = jbig2_word_stream_buf_new(ctx, data, size);
  325|    334|        if (ws == NULL) {
  ------------------
  |  Branch (325:13): [True: 1, False: 333]
  ------------------
  326|      1|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate word stream when decoding gray scale image");
  327|      1|            goto cleanup;
  328|      1|        }
  329|       |
  330|    333|        as = jbig2_arith_new(ctx, ws);
  331|    333|        if (as == NULL) {
  ------------------
  |  Branch (331:13): [True: 7, False: 326]
  ------------------
  332|      7|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when decoding gray scale image");
  333|      7|            goto cleanup;
  334|      7|        }
  335|       |
  336|    326|        code = jbig2_decode_generic_region(ctx, segment, &rparams, as, GSPLANES[GSBPP - 1], GB_stats);
  337|    326|    }
  338|  2.12k|    if (code < 0) {
  ------------------
  |  Branch (338:9): [True: 3, False: 2.12k]
  ------------------
  339|      3|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error decoding GSPLANES for halftone image");
  340|      3|        goto cleanup;
  341|      3|    }
  342|       |
  343|       |    /* C.5 step 2. Set j = GSBPP-2 */
  344|  2.12k|    j = GSBPP - 1;
  345|       |    /* C.5 step 3. decode loop */
  346|  16.7k|    while (j > 0) {
  ------------------
  |  Branch (346:12): [True: 14.6k, False: 2.06k]
  ------------------
  347|  14.6k|        j--;
  348|       |        /*  C.5 step 3. (a) */
  349|  14.6k|        if (GSMMR) {
  ------------------
  |  Branch (349:13): [True: 12.1k, False: 2.54k]
  ------------------
  350|  12.1k|            code = jbig2_decode_halftone_mmr(ctx, &rparams, data + consumed_bytes, size - consumed_bytes, GSPLANES[j], &consumed_bytes);
  351|  12.1k|        } else {
  352|  2.54k|            code = jbig2_decode_generic_region(ctx, segment, &rparams, as, GSPLANES[j], GB_stats);
  353|  2.54k|        }
  354|  14.6k|        if (code < 0) {
  ------------------
  |  Branch (354:13): [True: 61, False: 14.5k]
  ------------------
  355|     61|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode GSPLANES for halftone image");
  356|     61|            goto cleanup;
  357|     61|        }
  358|       |
  359|       |        /* C.5 step 3. (b):
  360|       |         * for each [x,y]
  361|       |         * GSPLANES[j][x][y] = GSPLANES[j+1][x][y] XOR GSPLANES[j][x][y] */
  362|  14.5k|        stride = GSPLANES[j]->stride;
  363|   300M|        for (i = 0; i < stride * GSH; ++i)
  ------------------
  |  Branch (363:21): [True: 300M, False: 14.5k]
  ------------------
  364|   300M|            GSPLANES[j]->data[i] ^= GSPLANES[j + 1]->data[i];
  365|       |
  366|       |        /*  C.5 step 3. (c) */
  367|  14.5k|    }
  368|       |
  369|       |    /* allocate GSVALS */
  370|  2.06k|    GSVALS = jbig2_new(ctx, uint16_t *, GSW);
  ------------------
  |  |  141|  2.06k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  371|  2.06k|    if (GSVALS == NULL) {
  ------------------
  |  Branch (371:9): [True: 13, False: 2.04k]
  ------------------
  372|     13|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSW);
  373|     13|        goto cleanup;
  374|     13|    }
  375|  11.6M|    for (i = 0; i < GSW; ++i) {
  ------------------
  |  Branch (375:17): [True: 11.6M, False: 2.00k]
  ------------------
  376|  11.6M|        GSVALS[i] = jbig2_new(ctx, uint16_t, GSH);
  ------------------
  |  |  141|  11.6M|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  377|  11.6M|        if (GSVALS[i] == NULL) {
  ------------------
  |  Branch (377:13): [True: 46, False: 11.6M]
  ------------------
  378|     46|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSH * GSW);
  379|       |            /* free already allocated */
  380|  6.18M|            for (j = i; j > 0;)
  ------------------
  |  Branch (380:25): [True: 6.18M, False: 46]
  ------------------
  381|  6.18M|                jbig2_free(ctx->allocator, GSVALS[--j]);
  382|     46|            jbig2_free(ctx->allocator, GSVALS);
  383|     46|            GSVALS = NULL;
  384|     46|            goto cleanup;
  385|     46|        }
  386|  11.6M|    }
  387|       |
  388|       |    /*  C.5 step 4.  */
  389|  5.42M|    for (x = 0; x < GSW; ++x) {
  ------------------
  |  Branch (389:17): [True: 5.41M, False: 2.00k]
  ------------------
  390|   105M|        for (y = 0; y < GSH; ++y) {
  ------------------
  |  Branch (390:21): [True: 99.7M, False: 5.41M]
  ------------------
  391|  99.7M|            GSVALS[x][y] = 0;
  392|       |
  393|   694M|            for (j = 0; j < GSBPP; ++j)
  ------------------
  |  Branch (393:25): [True: 594M, False: 99.7M]
  ------------------
  394|   594M|                GSVALS[x][y] += jbig2_image_get_pixel(GSPLANES[j], x, y) << j;
  395|  99.7M|        }
  396|  5.41M|    }
  397|       |
  398|  2.13k|cleanup:
  399|       |    /* free memory */
  400|  2.13k|    if (!GSMMR) {
  ------------------
  |  Branch (400:9): [True: 334, False: 1.80k]
  ------------------
  401|    334|        jbig2_free(ctx->allocator, as);
  402|    334|        jbig2_word_stream_buf_free(ctx, ws);
  403|    334|    }
  404|  19.1k|    for (i = 0; i < GSBPP; ++i)
  ------------------
  |  Branch (404:17): [True: 17.0k, False: 2.13k]
  ------------------
  405|  17.0k|        jbig2_image_release(ctx, GSPLANES[i]);
  406|       |
  407|  2.13k|    jbig2_free(ctx->allocator, GSPLANES);
  408|       |
  409|  2.13k|    return GSVALS;
  410|  2.00k|}

jbig2_huffman_new:
   69|  6.49k|{
   70|  6.49k|    Jbig2HuffmanState *result = NULL;
   71|  6.49k|    int code;
   72|       |
   73|  6.49k|    result = jbig2_new(ctx, Jbig2HuffmanState, 1);
  ------------------
  |  |  141|  6.49k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   74|       |
   75|  6.49k|    if (result != NULL) {
  ------------------
  |  Branch (75:9): [True: 6.49k, False: 2]
  ------------------
   76|  6.49k|        result->offset = 0;
   77|  6.49k|        result->offset_bits = 0;
   78|  6.49k|        result->offset_limit = 0;
   79|  6.49k|        result->ws = ws;
   80|  6.49k|        result->ctx = ctx;
   81|  6.49k|        code = huff_get_next_word(result, 0, &result->this_word);
  ------------------
  |  |   61|  6.49k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
   82|  6.49k|        if (code < 0) {
  ------------------
  |  Branch (82:13): [True: 0, False: 6.49k]
  ------------------
   83|      0|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read first huffman word");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   84|      0|            jbig2_huffman_free(ctx, result);
   85|      0|            return NULL;
   86|      0|        }
   87|  6.49k|        code = huff_get_next_word(result, 4, &result->next_word);
  ------------------
  |  |   61|  6.49k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
   88|  6.49k|        if (code < 0) {
  ------------------
  |  Branch (88:13): [True: 0, False: 6.49k]
  ------------------
   89|      0|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read second huffman word");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   90|      0|            jbig2_huffman_free(ctx, result);
   91|      0|            return NULL;
   92|      0|        }
   93|  6.49k|    } else {
   94|      2|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate new huffman coding state");
  ------------------
  |  |   68|      2|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   95|      2|        return NULL;
   96|      2|    }
   97|       |
   98|  6.49k|    return result;
   99|  6.49k|}
jbig2_huffman_free:
  106|  15.1k|{
  107|  15.1k|    jbig2_free(ctx->allocator, hs);
  108|  15.1k|}
jbig2_huffman_skip:
  182|  32.7k|{
  183|  32.7k|    uint32_t bits = hs->offset_bits & 7;
  184|  32.7k|    int code;
  185|       |
  186|  32.7k|    if (bits) {
  ------------------
  |  Branch (186:9): [True: 27.9k, False: 4.73k]
  ------------------
  187|  27.9k|        bits = 8 - bits;
  188|  27.9k|        hs->offset_bits += bits;
  189|  27.9k|        hs->this_word = (hs->this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
  190|  27.9k|    }
  191|       |
  192|  32.7k|    if (hs->offset_bits >= 32) {
  ------------------
  |  Branch (192:9): [True: 5.99k, False: 26.7k]
  ------------------
  193|  5.99k|        hs->this_word = hs->next_word;
  194|  5.99k|        hs->offset += 4;
  195|  5.99k|        code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
  ------------------
  |  |   61|  5.99k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  196|  5.99k|        if (code < 0) {
  ------------------
  |  Branch (196:13): [True: 0, False: 5.99k]
  ------------------
  197|      0|            return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read next huffman word when skipping");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  198|      0|        }
  199|  5.99k|        hs->offset_bits -= 32;
  200|  5.99k|        if (hs->offset_bits) {
  ------------------
  |  Branch (200:13): [True: 29, False: 5.96k]
  ------------------
  201|     29|            hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
  202|     29|        }
  203|  5.99k|    }
  204|  32.7k|    return 0;
  205|  32.7k|}
jbig2_huffman_advance:
  211|  29.3k|{
  212|  29.3k|    int code;
  213|  29.3k|    hs->offset += advance & ~3;
  214|  29.3k|    hs->offset_bits += ((uint32_t) (advance & 3)) << 3;
  215|  29.3k|    if (hs->offset_bits >= 32) {
  ------------------
  |  Branch (215:9): [True: 14.4k, False: 14.9k]
  ------------------
  216|  14.4k|        hs->offset += 4;
  217|  14.4k|        hs->offset_bits -= 32;
  218|  14.4k|    }
  219|  29.3k|    code = huff_get_next_word(hs, hs->offset, &hs->this_word);
  ------------------
  |  |   61|  29.3k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  220|  29.3k|    if (code < 0) {
  ------------------
  |  Branch (220:9): [True: 0, False: 29.3k]
  ------------------
  221|      0|        return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get first huffman word after advancing");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  222|      0|    }
  223|  29.3k|    code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
  ------------------
  |  |   61|  29.3k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  224|  29.3k|    if (code < 0) {
  ------------------
  |  Branch (224:9): [True: 0, False: 29.3k]
  ------------------
  225|      0|        return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get second huffman word after advancing");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  226|      0|    }
  227|  29.3k|    if (hs->offset_bits > 0)
  ------------------
  |  Branch (227:9): [True: 15.6k, False: 13.6k]
  ------------------
  228|  15.6k|        hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
  229|  29.3k|    return 0;
  230|  29.3k|}
jbig2_huffman_offset:
  237|  24.7k|{
  238|  24.7k|    return hs->offset + (hs->offset_bits >> 3);
  239|  24.7k|}
jbig2_huffman_get_bits:
  246|  11.8M|{
  247|  11.8M|    uint32_t this_word = hs->this_word;
  248|  11.8M|    int32_t result;
  249|  11.8M|    int code;
  250|       |
  251|  11.8M|    if (hs->offset_limit && hs->offset >= hs->offset_limit) {
  ------------------
  |  Branch (251:9): [True: 10.9M, False: 932k]
  |  Branch (251:29): [True: 0, False: 10.9M]
  ------------------
  252|      0|        *err = -1;
  253|      0|        return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "end of jbig2 buffer reached at offset "FMTZ, (FMTZ_CAST) hs->offset);
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
                      return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "end of jbig2 buffer reached at offset "FMTZ, (FMTZ_CAST) hs->offset);
  ------------------
  |  |   74|      0|#define FMTZ "%zu"
  ------------------
  254|      0|    }
  255|       |
  256|  11.8M|    result = this_word >> (32 - bits);
  257|  11.8M|    hs->offset_bits += bits;
  258|  11.8M|    if (hs->offset_bits >= 32) {
  ------------------
  |  Branch (258:9): [True: 144k, False: 11.7M]
  ------------------
  259|   144k|        hs->offset += 4;
  260|   144k|        hs->offset_bits -= 32;
  261|   144k|        hs->this_word = hs->next_word;
  262|   144k|        code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
  ------------------
  |  |   61|   144k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  263|   144k|        if (code < 0) {
  ------------------
  |  Branch (263:13): [True: 0, False: 144k]
  ------------------
  264|      0|            return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get next huffman word");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  265|      0|        }
  266|   144k|        if (hs->offset_bits) {
  ------------------
  |  Branch (266:13): [True: 76.9k, False: 67.1k]
  ------------------
  267|  76.9k|            hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
  268|  76.9k|        } else {
  269|  67.1k|            hs->this_word = (hs->this_word << hs->offset_bits);
  270|  67.1k|        }
  271|  11.7M|    } else {
  272|  11.7M|        hs->this_word = (this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
  273|  11.7M|    }
  274|       |
  275|  11.8M|    return result;
  276|  11.8M|}
jbig2_huffman_get:
  280|  69.4M|{
  281|  69.4M|    Jbig2HuffmanEntry *entry;
  282|  69.4M|    byte flags;
  283|  69.4M|    int offset_bits = hs->offset_bits;
  284|  69.4M|    uint32_t this_word = hs->this_word;
  285|  69.4M|    uint32_t next_word;
  286|  69.4M|    int RANGELEN;
  287|  69.4M|    int32_t result;
  288|       |
  289|  69.4M|    if (hs->offset_limit && hs->offset >= hs->offset_limit) {
  ------------------
  |  Branch (289:9): [True: 67.9M, False: 1.53M]
  |  Branch (289:29): [True: 169, False: 67.9M]
  ------------------
  290|    169|        if (oob)
  ------------------
  |  Branch (290:13): [True: 169, False: 0]
  ------------------
  291|    169|            *oob = -1;
  292|    169|        return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "end of Jbig2WordStream reached at offset "FMTZ, (FMTZ_CAST) hs->offset);
  ------------------
  |  |   68|    169|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
                      return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "end of Jbig2WordStream reached at offset "FMTZ, (FMTZ_CAST) hs->offset);
  ------------------
  |  |   74|    169|#define FMTZ "%zu"
  ------------------
  293|    169|    }
  294|       |
  295|  69.4M|    for (;;) {
  296|  69.4M|        int log_table_size = table->log_table_size;
  297|  69.4M|        int PREFLEN;
  298|  69.4M|        int code;
  299|       |
  300|       |        /* SumatraPDF: shifting by the size of the operand is undefined */
  301|  69.4M|        entry = &table->entries[log_table_size > 0 ? this_word >> (32 - log_table_size) : 0];
  ------------------
  |  Branch (301:33): [True: 69.4M, False: 598]
  ------------------
  302|  69.4M|        flags = entry->flags;
  303|  69.4M|        PREFLEN = entry->PREFLEN;
  304|  69.4M|        if (flags == (byte) -1 || PREFLEN == (byte) -1) {
  ------------------
  |  Branch (304:13): [True: 49, False: 69.4M]
  |  Branch (304:35): [True: 0, False: 69.4M]
  ------------------
  305|     49|            if (oob)
  ------------------
  |  Branch (305:17): [True: 49, False: 0]
  ------------------
  306|     49|                *oob = -1;
  307|     49|            return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "encountered unpopulated huffman table entry");
  ------------------
  |  |   68|     49|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  308|     49|        }
  309|       |
  310|  69.4M|        next_word = hs->next_word;
  311|  69.4M|        offset_bits += PREFLEN;
  312|  69.4M|        if (offset_bits >= 32) {
  ------------------
  |  Branch (312:13): [True: 5.71M, False: 63.7M]
  ------------------
  313|  5.71M|            this_word = next_word;
  314|  5.71M|            hs->offset += 4;
  315|  5.71M|            code = huff_get_next_word(hs, hs->offset + 4, &next_word);
  ------------------
  |  |   61|  5.71M|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  316|  5.71M|            if (code < 0) {
  ------------------
  |  Branch (316:17): [True: 0, False: 5.71M]
  ------------------
  317|      0|                return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get next huffman word");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  318|      0|            }
  319|  5.71M|            else if (code < 4)
  ------------------
  |  Branch (319:22): [True: 5.65M, False: 55.0k]
  ------------------
  320|  5.65M|            {
  321|  5.65M|                hs->offset_limit = hs->offset + 4 + code;
  322|  5.65M|            }
  323|  5.71M|            offset_bits -= 32;
  324|  5.71M|            hs->next_word = next_word;
  325|  5.71M|            PREFLEN = offset_bits;
  326|  5.71M|        }
  327|  69.4M|        if (PREFLEN)
  ------------------
  |  Branch (327:13): [True: 66.0M, False: 3.37M]
  ------------------
  328|  66.0M|            this_word = (this_word << PREFLEN) | (next_word >> (32 - offset_bits));
  329|  69.4M|        if (flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
  ------------------
  |  |   44|  69.4M|#define JBIG2_HUFFMAN_FLAGS_ISEXT 4
  ------------------
  |  Branch (329:13): [True: 0, False: 69.4M]
  ------------------
  330|      0|            table = entry->u.ext_table;
  331|      0|        } else
  332|  69.4M|            break;
  333|  69.4M|    }
  334|  69.4M|    result = entry->u.RANGELOW;
  335|  69.4M|    RANGELEN = entry->RANGELEN;
  336|  69.4M|    if (RANGELEN > 0) {
  ------------------
  |  Branch (336:9): [True: 6.67k, False: 69.4M]
  ------------------
  337|  6.67k|        int32_t HTOFFSET;
  338|  6.67k|        int code;
  339|       |
  340|  6.67k|        HTOFFSET = this_word >> (32 - RANGELEN);
  341|  6.67k|        if (flags & JBIG2_HUFFMAN_FLAGS_ISLOW)
  ------------------
  |  |   43|  6.67k|#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
  ------------------
  |  Branch (341:13): [True: 662, False: 6.01k]
  ------------------
  342|    662|            result -= HTOFFSET;
  343|  6.01k|        else
  344|  6.01k|            result += HTOFFSET;
  345|       |
  346|  6.67k|        offset_bits += RANGELEN;
  347|  6.67k|        if (offset_bits >= 32) {
  ------------------
  |  Branch (347:13): [True: 5.97k, False: 696]
  ------------------
  348|  5.97k|            this_word = next_word;
  349|  5.97k|            hs->offset += 4;
  350|  5.97k|            code = huff_get_next_word(hs, hs->offset + 4, &next_word);
  ------------------
  |  |   61|  5.97k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  351|  5.97k|            if (code < 0) {
  ------------------
  |  Branch (351:17): [True: 0, False: 5.97k]
  ------------------
  352|      0|                return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get next huffman word");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  353|      0|            }
  354|  5.97k|            else if (code < 4)
  ------------------
  |  Branch (354:22): [True: 2.92k, False: 3.05k]
  ------------------
  355|  2.92k|            {
  356|  2.92k|                hs->offset_limit = hs->offset + 4 + code;
  357|  2.92k|            }
  358|  5.97k|            offset_bits -= 32;
  359|  5.97k|            hs->next_word = next_word;
  360|  5.97k|            RANGELEN = offset_bits;
  361|  5.97k|        }
  362|  6.67k|        if (RANGELEN)
  ------------------
  |  Branch (362:13): [True: 6.03k, False: 636]
  ------------------
  363|  6.03k|            this_word = (this_word << RANGELEN) | (next_word >> (32 - offset_bits));
  364|  6.67k|    }
  365|       |
  366|  69.4M|    hs->this_word = this_word;
  367|  69.4M|    hs->offset_bits = offset_bits;
  368|       |
  369|  69.4M|    if (oob != NULL)
  ------------------
  |  Branch (369:9): [True: 69.4M, False: 0]
  ------------------
  370|  69.4M|        *oob = (flags & JBIG2_HUFFMAN_FLAGS_ISOOB);
  ------------------
  |  |   42|  69.4M|#define JBIG2_HUFFMAN_FLAGS_ISOOB 1
  ------------------
  371|       |
  372|  69.4M|    return result;
  373|  69.4M|}
jbig2_build_huffman_table:
  385|  35.7k|{
  386|  35.7k|    int *LENCOUNT;
  387|  35.7k|    int LENMAX = -1;
  388|  35.7k|    const int lencountcount = 256;
  389|  35.7k|    const Jbig2HuffmanLine *lines = params->lines;
  390|  35.7k|    int n_lines = params->n_lines;
  391|  35.7k|    int i, j;
  392|  35.7k|    uint32_t max_j;
  393|  35.7k|    int log_table_size = 0;
  394|  35.7k|    Jbig2HuffmanTable *result;
  395|  35.7k|    Jbig2HuffmanEntry *entries;
  396|  35.7k|    int CURLEN;
  397|  35.7k|    int firstcode = 0;
  398|  35.7k|    int CURCODE;
  399|  35.7k|    int CURTEMP;
  400|       |
  401|  35.7k|    LENCOUNT = jbig2_new(ctx, int, lencountcount);
  ------------------
  |  |  141|  35.7k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  402|       |
  403|  35.7k|    if (LENCOUNT == NULL) {
  ------------------
  |  Branch (403:9): [True: 4, False: 35.7k]
  ------------------
  404|      4|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate huffman histogram");
  ------------------
  |  |   68|      4|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  405|      4|        return NULL;
  406|      4|    }
  407|  35.7k|    memset(LENCOUNT, 0, sizeof(int) * lencountcount);
  408|       |
  409|       |    /* B.3, 1. */
  410|  17.0M|    for (i = 0; i < params->n_lines; i++) {
  ------------------
  |  Branch (410:17): [True: 16.9M, False: 35.7k]
  ------------------
  411|  16.9M|        int PREFLEN = lines[i].PREFLEN;
  412|  16.9M|        int lts;
  413|       |
  414|  16.9M|        if (PREFLEN > LENMAX) {
  ------------------
  |  Branch (414:13): [True: 105k, False: 16.8M]
  ------------------
  415|   361k|            for (j = LENMAX + 1; j < PREFLEN + 1; j++)
  ------------------
  |  Branch (415:34): [True: 255k, False: 105k]
  ------------------
  416|   255k|                LENCOUNT[j] = 0;
  417|   105k|            LENMAX = PREFLEN;
  418|   105k|        }
  419|  16.9M|        LENCOUNT[PREFLEN]++;
  420|       |
  421|  16.9M|        lts = PREFLEN + lines[i].RANGELEN;
  422|  16.9M|        if (lts > LOG_TABLE_SIZE_MAX)
  ------------------
  |  |  378|  16.9M|#define LOG_TABLE_SIZE_MAX 16
  ------------------
  |  Branch (422:13): [True: 3.39M, False: 13.5M]
  ------------------
  423|  3.39M|            lts = PREFLEN;
  424|  16.9M|        if (lts <= LOG_TABLE_SIZE_MAX && log_table_size < lts)
  ------------------
  |  |  378|  33.9M|#define LOG_TABLE_SIZE_MAX 16
  ------------------
  |  Branch (424:13): [True: 13.6M, False: 3.32M]
  |  Branch (424:42): [True: 97.7k, False: 13.5M]
  ------------------
  425|  97.7k|            log_table_size = lts;
  426|  16.9M|    }
  427|  35.7k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "constructing huffman table log size %d", log_table_size);
  ------------------
  |  |   68|  35.7k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  428|  35.7k|    max_j = 1 << log_table_size;
  429|       |
  430|  35.7k|    result = jbig2_new(ctx, Jbig2HuffmanTable, 1);
  ------------------
  |  |  141|  35.7k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  431|  35.7k|    if (result == NULL) {
  ------------------
  |  Branch (431:9): [True: 11, False: 35.7k]
  ------------------
  432|     11|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate result");
  ------------------
  |  |   68|     11|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  433|     11|        jbig2_free(ctx->allocator, LENCOUNT);
  434|     11|        return NULL;
  435|     11|    }
  436|  35.7k|    result->log_table_size = log_table_size;
  437|  35.7k|    entries = jbig2_new(ctx, Jbig2HuffmanEntry, max_j);
  ------------------
  |  |  141|  35.7k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  438|  35.7k|    if (entries == NULL) {
  ------------------
  |  Branch (438:9): [True: 18, False: 35.7k]
  ------------------
  439|     18|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate result entries");
  ------------------
  |  |   68|     18|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  440|     18|        jbig2_free(ctx->allocator, result);
  441|     18|        jbig2_free(ctx->allocator, LENCOUNT);
  442|     18|        return NULL;
  443|     18|    }
  444|       |    /* fill now to catch missing JBIG2Globals later */
  445|  35.7k|    memset(entries, 0xFF, sizeof(Jbig2HuffmanEntry) * max_j);
  446|  35.7k|    result->entries = entries;
  447|       |
  448|  35.7k|    LENCOUNT[0] = 0;
  449|       |
  450|   255k|    for (CURLEN = 1; CURLEN <= LENMAX; CURLEN++) {
  ------------------
  |  Branch (450:22): [True: 219k, False: 35.6k]
  ------------------
  451|   219k|        int shift = log_table_size - CURLEN;
  452|       |
  453|       |        /* B.3 3.(a) */
  454|   219k|        firstcode = (firstcode + LENCOUNT[CURLEN - 1]) << 1;
  455|   219k|        CURCODE = firstcode;
  456|       |        /* B.3 3.(b) */
  457|   152M|        for (CURTEMP = 0; CURTEMP < n_lines; CURTEMP++) {
  ------------------
  |  Branch (457:27): [True: 152M, False: 219k]
  ------------------
  458|   152M|            int PREFLEN = lines[CURTEMP].PREFLEN;
  459|       |
  460|   152M|            if (PREFLEN == CURLEN) {
  ------------------
  |  Branch (460:17): [True: 734k, False: 151M]
  ------------------
  461|   734k|                int RANGELEN = lines[CURTEMP].RANGELEN;
  462|   734k|                uint32_t start_j = CURCODE << shift;
  463|   734k|                uint32_t end_j = (CURCODE + 1) << shift;
  464|   734k|                uint32_t cur_j;
  465|   734k|                byte eflags = 0;
  466|       |
  467|   734k|                if (end_j > max_j) {
  ------------------
  |  Branch (467:21): [True: 106, False: 734k]
  ------------------
  468|    106|                    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ran off the end of the entries table! (%d >= %d)", end_j, max_j);
  ------------------
  |  |   68|    106|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  469|    106|                    jbig2_free(ctx->allocator, result->entries);
  470|    106|                    jbig2_free(ctx->allocator, result);
  471|    106|                    jbig2_free(ctx->allocator, LENCOUNT);
  472|    106|                    return NULL;
  473|    106|                }
  474|       |                /* todo: build extension tables */
  475|   734k|                if (params->HTOOB && CURTEMP == n_lines - 1)
  ------------------
  |  Branch (475:21): [True: 47.0k, False: 687k]
  |  Branch (475:38): [True: 4.60k, False: 42.4k]
  ------------------
  476|  4.60k|                    eflags |= JBIG2_HUFFMAN_FLAGS_ISOOB;
  ------------------
  |  |   42|  4.60k|#define JBIG2_HUFFMAN_FLAGS_ISOOB 1
  ------------------
  477|   734k|                if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2))
  ------------------
  |  Branch (477:21): [True: 14.5k, False: 719k]
  |  Branch (477:43): [True: 47.0k, False: 687k]
  ------------------
  478|  14.5k|                    eflags |= JBIG2_HUFFMAN_FLAGS_ISLOW;
  ------------------
  |  |   43|  14.5k|#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
  ------------------
  479|   734k|                if (PREFLEN + RANGELEN > LOG_TABLE_SIZE_MAX) {
  ------------------
  |  |  378|   734k|#define LOG_TABLE_SIZE_MAX 16
  ------------------
  |  Branch (479:21): [True: 50.7k, False: 683k]
  ------------------
  480|  5.43M|                    for (cur_j = start_j; cur_j < end_j; cur_j++) {
  ------------------
  |  Branch (480:43): [True: 5.38M, False: 50.7k]
  ------------------
  481|  5.38M|                        entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW;
  482|  5.38M|                        entries[cur_j].PREFLEN = PREFLEN;
  483|  5.38M|                        entries[cur_j].RANGELEN = RANGELEN;
  484|  5.38M|                        entries[cur_j].flags = eflags;
  485|  5.38M|                    }
  486|   683k|                } else {
  487|   265M|                    for (cur_j = start_j; cur_j < end_j; cur_j++) {
  ------------------
  |  Branch (487:43): [True: 264M, False: 683k]
  ------------------
  488|   264M|                        int32_t HTOFFSET = (cur_j >> (shift - RANGELEN)) & ((1 << RANGELEN) - 1);
  489|       |
  490|   264M|                        if (eflags & JBIG2_HUFFMAN_FLAGS_ISLOW)
  ------------------
  |  |   43|   264M|#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
  ------------------
  |  Branch (490:29): [True: 543k, False: 263M]
  ------------------
  491|   543k|                            entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW - HTOFFSET;
  492|   263M|                        else
  493|   263M|                            entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW + HTOFFSET;
  494|   264M|                        entries[cur_j].PREFLEN = PREFLEN + RANGELEN;
  495|   264M|                        entries[cur_j].RANGELEN = 0;
  496|   264M|                        entries[cur_j].flags = eflags;
  497|   264M|                    }
  498|   683k|                }
  499|   734k|                CURCODE++;
  500|   734k|            }
  501|   152M|        }
  502|   219k|    }
  503|       |
  504|  35.6k|    jbig2_free(ctx->allocator, LENCOUNT);
  505|       |
  506|  35.6k|    return result;
  507|  35.7k|}
jbig2_release_huffman_table:
  512|  48.8k|{
  513|  48.8k|    if (table != NULL) {
  ------------------
  |  Branch (513:9): [True: 35.6k, False: 13.2k]
  ------------------
  514|  35.6k|        jbig2_free(ctx->allocator, table->entries);
  515|  35.6k|        jbig2_free(ctx->allocator, table);
  516|  35.6k|    }
  517|  48.8k|}
jbig2_table:
  550|  1.85k|{
  551|  1.85k|    Jbig2HuffmanParams *params = NULL;
  552|  1.85k|    Jbig2HuffmanLine *line = NULL;
  553|       |
  554|  1.85k|    segment->result = NULL;
  555|  1.85k|    if (segment->data_length < 10)
  ------------------
  |  Branch (555:9): [True: 12, False: 1.84k]
  ------------------
  556|     12|        goto too_short;
  557|  1.84k|    if (segment->data_length > SIZE_MAX / 8)
  ------------------
  |  Branch (557:9): [True: 0, False: 1.84k]
  ------------------
  558|      0|    {
  559|      0|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment data too large for Huffman Table");
  560|      0|        goto error_exit;
  561|      0|    }
  562|       |
  563|  1.84k|    {
  564|       |        /* B.2 1) (B.2.1) Code table flags */
  565|  1.84k|        const int code_table_flags = segment_data[0];
  566|  1.84k|        const int HTOOB = code_table_flags & 0x01;      /* Bit 0: HTOOB */
  567|       |
  568|       |        /* Bits 1-3: Number of bits used in code table line prefix size fields */
  569|  1.84k|        const int HTPS = (code_table_flags >> 1 & 0x07) + 1;
  570|       |
  571|       |        /* Bits 4-6: Number of bits used in code table line range size fields */
  572|  1.84k|        const int HTRS = (code_table_flags >> 4 & 0x07) + 1;
  573|       |
  574|       |        /* B.2 2) (B.2.2) The lower bound of the first table line in the encoded table */
  575|  1.84k|        const int32_t HTLOW = jbig2_get_int32(segment_data + 1);
  576|       |
  577|       |        /* B.2 3) (B.2.3) One larger than the upper bound of
  578|       |           the last normal table line in the encoded table */
  579|  1.84k|        const int32_t HTHIGH = jbig2_get_int32(segment_data + 5);
  580|       |
  581|       |        /* estimated number of lines in this table, used for allocating memory for lines */
  582|  1.84k|        const size_t lines_max = (segment->data_length * 8 - HTPS * (HTOOB ? 3 : 2)) / (HTPS + HTRS) + (HTOOB ? 3 : 2);
  ------------------
  |  Branch (582:70): [True: 886, False: 957]
  |  Branch (582:105): [True: 886, False: 957]
  ------------------
  583|       |
  584|       |        /* points to a first table line data */
  585|  1.84k|        const byte *lines_data = segment_data + 9;
  586|  1.84k|        const size_t lines_data_bitlen = (segment->data_length - 9) * 8;        /* length in bit */
  587|       |
  588|       |        /* bit offset: controls bit reading */
  589|  1.84k|        size_t boffset = 0;
  590|       |
  591|       |        /* B.2 4) */
  592|  1.84k|        int32_t CURRANGELOW = HTLOW;
  593|  1.84k|        size_t NTEMP = 0;
  594|       |
  595|       |#ifdef JBIG2_DEBUG
  596|       |        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  597|       |                    "DECODING USER TABLE... Flags: %d, HTOOB: %d, HTPS: %d, HTRS: %d, HTLOW: %d, HTHIGH: %d",
  598|       |                    code_table_flags, HTOOB, HTPS, HTRS, HTLOW, HTHIGH);
  599|       |#endif
  600|       |
  601|  1.84k|        if (segment->data_length > SIZE_MAX / 8) {
  ------------------
  |  Branch (601:13): [True: 0, False: 1.84k]
  ------------------
  602|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Huffman Table size too large");
  603|      0|            goto error_exit;
  604|      0|        }
  605|  1.84k|        if (HTLOW >= HTHIGH) {
  ------------------
  |  Branch (605:13): [True: 38, False: 1.80k]
  ------------------
  606|     38|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid Huffman Table range");
  607|     38|            goto error_exit;
  608|     38|        }
  609|       |
  610|       |        /* allocate HuffmanParams & HuffmanLine */
  611|  1.80k|        params = jbig2_new(ctx, Jbig2HuffmanParams, 1);
  ------------------
  |  |  141|  1.80k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  612|  1.80k|        if (params == NULL) {
  ------------------
  |  Branch (612:13): [True: 1, False: 1.80k]
  ------------------
  613|      1|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate Huffman Table Parameter");
  614|      1|            goto error_exit;
  615|      1|        }
  616|  1.80k|        line = jbig2_new(ctx, Jbig2HuffmanLine, lines_max);
  ------------------
  |  |  141|  1.80k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  617|  1.80k|        if (line == NULL) {
  ------------------
  |  Branch (617:13): [True: 1, False: 1.80k]
  ------------------
  618|      1|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate huffman table lines");
  619|      1|            goto error_exit;
  620|      1|        }
  621|       |        /* B.2 5) */
  622|  7.18M|        while (CURRANGELOW < HTHIGH) {
  ------------------
  |  Branch (622:16): [True: 7.18M, False: 1.73k]
  ------------------
  623|       |            /* B.2 5) a) */
  624|  7.18M|            if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (624:17): [True: 22, False: 7.18M]
  ------------------
  625|     22|                goto too_short;
  626|  7.18M|            if (NTEMP >= lines_max) {
  ------------------
  |  Branch (626:17): [True: 0, False: 7.18M]
  ------------------
  627|      0|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  628|      0|                goto error_exit;
  629|      0|            }
  630|  7.18M|            line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  631|       |            /* B.2 5) b) */
  632|  7.18M|            if (boffset + HTRS >= lines_data_bitlen)
  ------------------
  |  Branch (632:17): [True: 47, False: 7.18M]
  ------------------
  633|     47|                goto too_short;
  634|  7.18M|            line[NTEMP].RANGELEN = jbig2_table_read_bits(lines_data, &boffset, HTRS);
  635|       |            /* B.2 5) c) */
  636|  7.18M|            line[NTEMP].RANGELOW = CURRANGELOW;
  637|  7.18M|            CURRANGELOW += (1 << line[NTEMP].RANGELEN);
  638|  7.18M|            NTEMP++;
  639|  7.18M|        }
  640|       |        /* B.2 6), B.2 7) lower range table line */
  641|  1.73k|        if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (641:13): [True: 4, False: 1.73k]
  ------------------
  642|      4|            goto too_short;
  643|  1.73k|        if (NTEMP >= lines_max) {
  ------------------
  |  Branch (643:13): [True: 0, False: 1.73k]
  ------------------
  644|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  645|      0|            goto error_exit;
  646|      0|        }
  647|  1.73k|        line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  648|  1.73k|        line[NTEMP].RANGELEN = 32;
  649|  1.73k|        line[NTEMP].RANGELOW = HTLOW - 1;
  650|  1.73k|        NTEMP++;
  651|       |        /* B.2 8), B.2 9) upper range table line */
  652|  1.73k|        if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (652:13): [True: 3, False: 1.72k]
  ------------------
  653|      3|            goto too_short;
  654|  1.72k|        if (NTEMP >= lines_max) {
  ------------------
  |  Branch (654:13): [True: 0, False: 1.72k]
  ------------------
  655|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  656|      0|            goto error_exit;
  657|      0|        }
  658|  1.72k|        line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  659|  1.72k|        line[NTEMP].RANGELEN = 32;
  660|  1.72k|        line[NTEMP].RANGELOW = HTHIGH;
  661|  1.72k|        NTEMP++;
  662|       |        /* B.2 10) */
  663|  1.72k|        if (HTOOB) {
  ------------------
  |  Branch (663:13): [True: 830, False: 897]
  ------------------
  664|       |            /* B.2 10) a), B.2 10) b) out-of-bound table line */
  665|    830|            if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (665:17): [True: 7, False: 823]
  ------------------
  666|      7|                goto too_short;
  667|    823|            if (NTEMP >= lines_max) {
  ------------------
  |  Branch (667:17): [True: 0, False: 823]
  ------------------
  668|      0|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  669|      0|                goto error_exit;
  670|      0|            }
  671|    823|            line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  672|    823|            line[NTEMP].RANGELEN = 0;
  673|    823|            line[NTEMP].RANGELOW = 0;
  674|    823|            NTEMP++;
  675|    823|        }
  676|  1.72k|        if (NTEMP != lines_max) {
  ------------------
  |  Branch (676:13): [True: 1.72k, False: 0]
  ------------------
  677|  1.72k|            Jbig2HuffmanLine *new_line = jbig2_renew(ctx, line,
  ------------------
  |  |  143|  1.72k|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
  678|  1.72k|                                                     Jbig2HuffmanLine, NTEMP);
  679|       |
  680|  1.72k|            if (new_line == NULL) {
  ------------------
  |  Branch (680:17): [True: 0, False: 1.72k]
  ------------------
  681|      0|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to reallocate huffman table lines");
  682|      0|                goto error_exit;
  683|      0|            }
  684|  1.72k|            line = new_line;
  685|  1.72k|        }
  686|  1.72k|        params->HTOOB = HTOOB;
  687|       |        /* Assuming int >= int32_t here. */
  688|  1.72k|        if (NTEMP > INT32_MAX) {
  ------------------
  |  Branch (688:13): [True: 0, False: 1.72k]
  ------------------
  689|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "implausible number of huffman table lines");
  690|      0|            goto error_exit;
  691|      0|        }
  692|  1.72k|        params->n_lines = (int)NTEMP;
  693|  1.72k|        params->lines = line;
  694|  1.72k|        segment->result = params;
  695|       |
  696|       |#ifdef JBIG2_DEBUG
  697|       |        {
  698|       |            size_t i;
  699|       |
  700|       |            for (i = 0; i < NTEMP; i++) {
  701|       |                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  702|       |                            "Line: "FMTZ", PREFLEN: %d, RANGELEN: %d, RANGELOW: %d",
  703|       |                            (FMTZ_CAST) i, params->lines[i].PREFLEN, params->lines[i].RANGELEN, params->lines[i].RANGELOW);
  704|       |            }
  705|       |        }
  706|       |#endif
  707|  1.72k|    }
  708|      0|    return 0;
  709|       |
  710|     95|too_short:
  711|     95|    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  712|    135|error_exit:
  713|    135|    jbig2_free(ctx->allocator, line);
  714|    135|    jbig2_free(ctx->allocator, params);
  715|    135|    return -1;
  716|     95|}
jbig2_table_free:
  721|  1.72k|{
  722|  1.72k|    if (params != NULL) {
  ------------------
  |  Branch (722:9): [True: 1.72k, False: 0]
  ------------------
  723|  1.72k|        jbig2_free(ctx->allocator, (void *)params->lines);
  724|  1.72k|        jbig2_free(ctx->allocator, params);
  725|  1.72k|    }
  726|  1.72k|}
jbig2_find_table:
  731|    516|{
  732|    516|    int i, table_index = 0;
  733|       |
  734|  1.39k|    for (i = 0; i < segment->referred_to_segment_count; i++) {
  ------------------
  |  Branch (734:17): [True: 1.34k, False: 51]
  ------------------
  735|  1.34k|        const Jbig2Segment *const rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[i]);
  736|       |
  737|  1.34k|        if (rsegment && (rsegment->flags & 63) == 53) {
  ------------------
  |  Branch (737:13): [True: 853, False: 492]
  |  Branch (737:25): [True: 601, False: 252]
  ------------------
  738|    601|            if (table_index == index)
  ------------------
  |  Branch (738:17): [True: 465, False: 136]
  ------------------
  739|    465|                return (const Jbig2HuffmanParams *)rsegment->result;
  740|    136|            ++table_index;
  741|    136|        }
  742|  1.34k|    }
  743|       |
  744|     51|    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table not found (%d)", index);
  745|       |    return NULL;
  746|    516|}
jbig2_huffman.c:jbig2_table_read_bits:
  524|  14.3M|{
  525|  14.3M|    uint32_t result = 0;
  526|  14.3M|    size_t byte_offset = *bitoffset / 8;
  527|  14.3M|    const int endbit = ((int) (*bitoffset & 7)) + bitlen;
  528|  14.3M|    const int n_proc_bytes = (endbit + 7) / 8;
  529|  14.3M|    const int rshift = n_proc_bytes * 8 - endbit;
  530|  14.3M|    int i;
  531|       |
  532|  29.0M|    for (i = n_proc_bytes - 1; i >= 0; i--) {
  ------------------
  |  Branch (532:32): [True: 14.7M, False: 14.3M]
  ------------------
  533|  14.7M|        uint32_t d = data[byte_offset++];
  534|  14.7M|        const int nshift = i * 8 - rshift;
  535|       |
  536|  14.7M|        if (nshift > 0)
  ------------------
  |  Branch (536:13): [True: 357k, False: 14.3M]
  ------------------
  537|   357k|            d <<= nshift;
  538|  14.3M|        else if (nshift < 0)
  ------------------
  |  Branch (538:18): [True: 12.5M, False: 1.80M]
  ------------------
  539|  12.5M|            d >>= -nshift;
  540|  14.7M|        result |= d;
  541|  14.7M|    }
  542|       |    result &= ~(UINT32_MAX << bitlen);
  543|  14.3M|    *bitoffset += bitlen;
  544|  14.3M|    return result;
  545|  14.3M|}

jbig2_image_new:
   36|  18.3M|{
   37|  18.3M|    Jbig2Image *image;
   38|  18.3M|    uint32_t stride;
   39|       |
   40|  18.3M|    if (width == 0 || height == 0) {
  ------------------
  |  Branch (40:9): [True: 94, False: 18.3M]
  |  Branch (40:23): [True: 12, False: 18.3M]
  ------------------
   41|    106|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to create zero sized image");
  ------------------
  |  |   68|    106|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   42|    106|        return NULL;
   43|    106|    }
   44|       |
   45|  18.3M|    image = jbig2_new(ctx, Jbig2Image, 1);
  ------------------
  |  |  141|  18.3M|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   46|  18.3M|    if (image == NULL) {
  ------------------
  |  Branch (46:9): [True: 41, False: 18.3M]
  ------------------
   47|     41|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate image");
  ------------------
  |  |   68|     41|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   48|     41|        return NULL;
   49|     41|    }
   50|       |
   51|  18.3M|    stride = ((width - 1) >> 3) + 1;    /* generate a byte-aligned stride */
   52|       |
   53|       |    /* check for integer multiplication overflow */
   54|  18.3M|    if (height > (INT32_MAX / stride)) {
  ------------------
  |  Branch (54:9): [True: 246, False: 18.3M]
  ------------------
   55|    246|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "integer multiplication overflow (stride=%u, height=%u)", stride, height);
  ------------------
  |  |   68|    246|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   56|    246|        jbig2_free(ctx->allocator, image);
   57|    246|        return NULL;
   58|    246|    }
   59|  18.3M|    image->data = jbig2_new(ctx, uint8_t, (size_t) height * stride);
  ------------------
  |  |  141|  18.3M|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   60|  18.3M|    if (image->data == NULL) {
  ------------------
  |  Branch (60:9): [True: 184, False: 18.3M]
  ------------------
   61|    184|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate image data buffer (stride=%u, height=%u)", stride, height);
  ------------------
  |  |   68|    184|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   62|    184|        jbig2_free(ctx->allocator, image);
   63|    184|        return NULL;
   64|    184|    }
   65|       |
   66|  18.3M|    image->width = width;
   67|  18.3M|    image->height = height;
   68|  18.3M|    image->stride = stride;
   69|  18.3M|    image->refcount = 1;
   70|       |
   71|  18.3M|    return image;
   72|  18.3M|}
jbig2_image_reference:
   77|  62.3M|{
   78|  62.3M|    (void) ctx;
   79|  62.3M|    if (image)
  ------------------
  |  Branch (79:9): [True: 15.2M, False: 47.1M]
  ------------------
   80|  15.2M|        image->refcount++;
   81|  62.3M|    return image;
   82|  62.3M|}
jbig2_image_release:
   87|   537M|{
   88|   537M|    if (image == NULL)
  ------------------
  |  Branch (88:9): [True: 503M, False: 33.6M]
  ------------------
   89|   503M|        return;
   90|  33.6M|    image->refcount--;
   91|  33.6M|    if (image->refcount == 0)
  ------------------
  |  Branch (91:9): [True: 18.3M, False: 15.2M]
  ------------------
   92|  18.3M|        jbig2_image_free(ctx, image);
   93|  33.6M|}
jbig2_image_free:
   98|  18.3M|{
   99|  18.3M|    if (image != NULL) {
  ------------------
  |  Branch (99:9): [True: 18.3M, False: 0]
  ------------------
  100|  18.3M|        jbig2_free(ctx->allocator, image->data);
  101|  18.3M|        jbig2_free(ctx->allocator, image);
  102|  18.3M|    }
  103|  18.3M|}
jbig2_image_resize:
  108|  5.64k|{
  109|  5.64k|    if (width == image->width) {
  ------------------
  |  Branch (109:9): [True: 5.64k, False: 0]
  ------------------
  110|  5.64k|        uint8_t *data;
  111|       |
  112|       |        /* check for integer multiplication overflow */
  113|  5.64k|        if (image->height > (INT32_MAX / image->stride)) {
  ------------------
  |  Branch (113:13): [True: 0, False: 5.64k]
  ------------------
  114|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "integer multiplication overflow during resize (stride=%u, height=%u)", image->stride, height);
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  115|      0|            return NULL;
  116|      0|        }
  117|       |        /* use the same stride, just change the length */
  118|  5.64k|        data = jbig2_renew(ctx, image->data, uint8_t, (size_t) height * image->stride);
  ------------------
  |  |  143|  5.64k|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
  119|  5.64k|        if (data == NULL) {
  ------------------
  |  Branch (119:13): [True: 64, False: 5.58k]
  ------------------
  120|     64|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to reallocate image");
  ------------------
  |  |   68|     64|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  121|     64|            return NULL;
  122|     64|        }
  123|  5.58k|        image->data = data;
  124|  5.58k|        if (height > image->height) {
  ------------------
  |  Branch (124:13): [True: 5.58k, False: 0]
  ------------------
  125|  5.58k|            const uint8_t fill = value ? 0xFF : 0x00;
  ------------------
  |  Branch (125:34): [True: 3.84k, False: 1.73k]
  ------------------
  126|  5.58k|            memset(image->data + (size_t) image->height * image->stride, fill, ((size_t) height - image->height) * image->stride);
  127|  5.58k|        }
  128|  5.58k|        image->height = height;
  129|       |
  130|  5.58k|    } else {
  131|      0|        Jbig2Image *newimage;
  132|      0|        int code;
  133|       |
  134|       |        /* Unoptimized implementation, but it works. */
  135|       |
  136|      0|        newimage = jbig2_image_new(ctx, width, height);
  137|      0|        if (newimage == NULL) {
  ------------------
  |  Branch (137:13): [True: 0, False: 0]
  ------------------
  138|      0|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate resized image");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  139|      0|            return NULL;
  140|      0|        }
  141|      0|        jbig2_image_clear(ctx, newimage, value);
  142|       |
  143|      0|        code = jbig2_image_compose(ctx, newimage, image, 0, 0, JBIG2_COMPOSE_REPLACE);
  144|      0|        if (code < 0) {
  ------------------
  |  Branch (144:13): [True: 0, False: 0]
  ------------------
  145|      0|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to compose image buffers when resizing");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  146|      0|            jbig2_image_release(ctx, newimage);
  147|      0|            return NULL;
  148|      0|        }
  149|       |
  150|       |        /* if refcount > 1 the original image, its pointer must
  151|       |        be kept, so simply replaces its innards, and throw away
  152|       |        the empty new image shell. */
  153|      0|        jbig2_free(ctx->allocator, image->data);
  154|      0|        image->width = newimage->width;
  155|      0|        image->height = newimage->height;
  156|      0|        image->stride = newimage->stride;
  157|      0|        image->data = newimage->data;
  158|      0|        jbig2_free(ctx->allocator, newimage);
  159|      0|    }
  160|       |
  161|  5.58k|    return image;
  162|  5.64k|}
jbig2_image_compose:
  336|   146M|{
  337|   146M|    uint32_t w, h;
  338|   146M|    uint32_t shift;
  339|   146M|    uint32_t leftbyte;
  340|   146M|    uint8_t *ss;
  341|   146M|    uint8_t *dd;
  342|   146M|    uint8_t leftmask, rightmask;
  343|   146M|    int early = x >= 0;
  344|   146M|    int late;
  345|   146M|    uint32_t bytewidth;
  346|   146M|    uint32_t syoffset = 0;
  347|       |
  348|   146M|    (void) ctx;
  349|       |
  350|   146M|    if (src == NULL)
  ------------------
  |  Branch (350:9): [True: 13.9M, False: 132M]
  ------------------
  351|  13.9M|        return 0;
  352|       |
  353|       |    /* Detect if src image has no overlap with the dst image.
  354|       |     * Because the widths/heights are of type uint32_t their theoretical
  355|       |     * maximum size is UINT32_MAX. Therefore this check also rejects any
  356|       |     * x/y values outside the range [-UINT32_MAX + 1, UINT32_MAX - 1].
  357|       |     * And after this if-statement x/y must necessarily fall within this
  358|       |     * closed range.
  359|       |     */
  360|   132M|    if (
  361|   132M|            (x <= -((int64_t) src->width)) || (x >= (int64_t) dst->width) ||
  ------------------
  |  Branch (361:13): [True: 22.1M, False: 110M]
  |  Branch (361:47): [True: 72.0M, False: 38.7M]
  ------------------
  362|  38.7M|            (y <= -((int64_t) src->height)) || (y >= (int64_t) dst->height))
  ------------------
  |  Branch (362:13): [True: 3.17M, False: 35.5M]
  |  Branch (362:48): [True: 16.7M, False: 18.8M]
  ------------------
  363|   114M|    {
  364|   114M|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "src image entirely outside dst image in compose_image");
  ------------------
  |  |   68|   114M|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  365|   114M|        return 0;
  366|   114M|    }
  367|       |
  368|       |    /* This code takes a src image and combines it onto dst at offset (x,y), with operation op. */
  369|       |
  370|       |    /* Data is packed msb first within a byte, so with bits numbered: 01234567.
  371|       |     * Second byte is: 89abcdef. So to combine into a run, we use:
  372|       |     *       (s[0]<<8) | s[1] == 0123456789abcdef.
  373|       |     * To read from src into dst at offset 3, we need to read:
  374|       |     *    read:      0123456789abcdef...
  375|       |     *    write:  0123456798abcdef...
  376|       |     * In general, to read from src and write into dst at offset x, we need to shift
  377|       |     * down by (x&7) bits to allow for bit alignment. So shift = x&7.
  378|       |     * So the 'central' part of our runs will see us doing:
  379|       |     *   *d++ op= ((s[0]<<8)|s[1])>>shift;
  380|       |     * with special cases on the left and right edges of the run to mask.
  381|       |     * With the left hand edge, we have to be careful not to 'underread' the start of
  382|       |     * the src image; this is what the early flag is about. Similarly we have to be
  383|       |     * careful not to read off the right hand edge; this is what the late flag is for.
  384|       |     */
  385|       |
  386|  18.8M|    w = src->width;
  387|  18.8M|    h = src->height;
  388|  18.8M|    shift = (uint32_t) (x & 7);
  389|  18.8M|    ss = src->data - early;
  390|       |
  391|       |    /* Since we know that x/y are now limited to [-UINT32_MAX + 1, UINT32_MAX - 1],
  392|       |     * we know that we can't accidentally negate an INT64_MIN. Moreover, we know
  393|       |     * that their negated values fit within an uint32_t, so casting to uint32_t is
  394|       |     * safe.
  395|       |     */
  396|       |
  397|       |    /* clip left/top */
  398|  18.8M|    if (x < 0) {
  ------------------
  |  Branch (398:9): [True: 18.0M, False: 778k]
  ------------------
  399|  18.0M|        uint32_t negx = (uint32_t) (-x);
  400|  18.0M|        w -= negx;
  401|  18.0M|        ss += (negx-1)>>3;
  402|  18.0M|        x = 0;
  403|  18.0M|    }
  404|  18.8M|    if (y < 0) {
  ------------------
  |  Branch (404:9): [True: 35.4k, False: 18.8M]
  ------------------
  405|  35.4k|        uint32_t negy = (uint32_t) (-y);
  406|  35.4k|        h -= negy;
  407|  35.4k|        syoffset = negy * src->stride;
  408|  35.4k|        y = 0;
  409|  35.4k|    }
  410|       |
  411|       |    /* clip right/bottom */
  412|  18.8M|    if ((uint32_t)x + w > dst->width)
  ------------------
  |  Branch (412:9): [True: 18.0M, False: 795k]
  ------------------
  413|  18.0M|        w = dst->width - ((uint32_t) x);
  414|  18.8M|    if ((uint32_t)y + h > dst->height)
  ------------------
  |  Branch (414:9): [True: 338k, False: 18.5M]
  ------------------
  415|   338k|        h = dst->height - ((uint32_t) y);
  416|       |#ifdef JBIG2_DEBUG
  417|       |    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "compositing %ux%u at (%u, %u) after clipping",
  418|       |        w, h, (uint32_t) x, (uint32_t) y);
  419|       |#endif
  420|       |
  421|       |    /* check for zero clipping region */
  422|  18.8M|    if ((w <= 0) || (h <= 0)) {
  ------------------
  |  Branch (422:9): [True: 0, False: 18.8M]
  |  Branch (422:21): [True: 0, False: 18.8M]
  ------------------
  423|       |#ifdef JBIG2_DEBUG
  424|       |        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "zero clipping region");
  425|       |#endif
  426|      0|        return 0;
  427|      0|    }
  428|       |
  429|  18.8M|    leftbyte = (uint32_t) x >> 3;
  430|  18.8M|    dd = dst->data + y * dst->stride + leftbyte;
  431|  18.8M|    bytewidth = (((uint32_t) x + w - 1) >> 3) - leftbyte + 1;
  432|  18.8M|    leftmask = 255>>(x&7);
  433|  18.8M|    rightmask = (((x+w)&7) == 0) ? 255 : ~(255>>((x+w)&7));
  ------------------
  |  Branch (433:17): [True: 1.96M, False: 16.8M]
  ------------------
  434|  18.8M|    if (bytewidth == 1)
  ------------------
  |  Branch (434:9): [True: 8.17M, False: 10.6M]
  ------------------
  435|  8.17M|        leftmask &= rightmask;
  436|  18.8M|    late = (ss + bytewidth >= src->data + ((src->width+7)>>3));
  437|  18.8M|    ss += syoffset;
  438|       |
  439|  18.8M|    switch(op)
  ------------------
  |  Branch (439:12): [True: 18.7M, False: 95.5k]
  ------------------
  440|  18.8M|    {
  441|   435k|    case JBIG2_COMPOSE_OR:
  ------------------
  |  Branch (441:5): [True: 435k, False: 18.4M]
  ------------------
  442|   435k|        jbig2_image_compose_opt_OR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  443|   435k|        break;
  444|  1.86k|    case JBIG2_COMPOSE_AND:
  ------------------
  |  Branch (444:5): [True: 1.86k, False: 18.8M]
  ------------------
  445|  1.86k|        jbig2_image_compose_opt_AND(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  446|  1.86k|        break;
  447|   239k|    case JBIG2_COMPOSE_XOR:
  ------------------
  |  Branch (447:5): [True: 239k, False: 18.6M]
  ------------------
  448|   239k|        jbig2_image_compose_opt_XOR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  449|   239k|        break;
  450|  2.16k|    case JBIG2_COMPOSE_XNOR:
  ------------------
  |  Branch (450:5): [True: 2.16k, False: 18.8M]
  ------------------
  451|  2.16k|        jbig2_image_compose_opt_XNOR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  452|  2.16k|        break;
  453|  18.0M|    case JBIG2_COMPOSE_REPLACE:
  ------------------
  |  Branch (453:5): [True: 18.0M, False: 773k]
  ------------------
  454|  18.0M|        jbig2_image_compose_opt_REPLACE(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  455|  18.0M|        break;
  456|  18.8M|    }
  457|       |
  458|  18.8M|    return 0;
  459|  18.8M|}
jbig2_image_clear:
  464|   114k|{
  465|   114k|    const uint8_t fill = value ? 0xFF : 0x00;
  ------------------
  |  Branch (465:26): [True: 4.86k, False: 109k]
  ------------------
  466|       |
  467|   114k|    (void) ctx;
  468|       |
  469|   114k|    memset(image->data, fill, image->stride * image->height);
  470|   114k|}
jbig2_image_get_pixel:
  478|  18.7G|{
  479|  18.7G|    const int64_t w = image->width;
  480|  18.7G|    const int64_t h = image->height;
  481|  18.7G|    size_t sx, sy;
  482|  18.7G|    size_t byte;
  483|  18.7G|    int bit;
  484|       |
  485|  18.7G|    if ((x < 0) || (x >= w))
  ------------------
  |  Branch (485:9): [True: 1.90G, False: 16.8G]
  |  Branch (485:20): [True: 3.13G, False: 13.7G]
  ------------------
  486|  5.03G|        return 0;
  487|  13.7G|    if ((y < 0) || (y >= h))
  ------------------
  |  Branch (487:9): [True: 3.71G, False: 9.99G]
  |  Branch (487:20): [True: 1.50G, False: 8.48G]
  ------------------
  488|  5.22G|        return 0;
  489|       |
  490|  8.48G|    sx = (size_t) x;
  491|  8.48G|    sy = (size_t) y;
  492|       |
  493|  8.48G|    byte = (sx >> 3) + sy * image->stride;
  494|  8.48G|    bit = 7 - ((int) (sx & 7));
  495|       |
  496|  8.48G|    return ((image->data[byte] >> bit) & 1);
  497|  13.7G|}
jbig2_image_set_pixel:
  502|  2.09G|{
  503|  2.09G|    const int64_t w = image->width;
  504|  2.09G|    const int64_t h = image->height;
  505|  2.09G|    uint8_t scratch, mask;
  506|  2.09G|    size_t sx, sy;
  507|  2.09G|    size_t byte;
  508|  2.09G|    int bit;
  509|       |
  510|  2.09G|    if ((x < 0) || (x >= w))
  ------------------
  |  Branch (510:9): [True: 0, False: 2.09G]
  |  Branch (510:20): [True: 0, False: 2.09G]
  ------------------
  511|      0|        return;
  512|  2.09G|    if ((y < 0) || (y >= h))
  ------------------
  |  Branch (512:9): [True: 0, False: 2.09G]
  |  Branch (512:20): [True: 0, False: 2.09G]
  ------------------
  513|      0|        return;
  514|       |
  515|  2.09G|    sx = (size_t) x;
  516|  2.09G|    sy = (size_t) y;
  517|       |
  518|  2.09G|    byte = (sx >> 3) + sy * image->stride;
  519|  2.09G|    bit = 7 - ((int) (sx & 7));
  520|  2.09G|    mask = (uint8_t) ((1 << bit) ^ 0xff);
  521|       |
  522|  2.09G|    scratch = image->data[byte] & mask;
  523|  2.09G|    image->data[byte] = scratch | (value << bit);
  524|  2.09G|}
jbig2_image.c:jbig2_image_compose_opt_OR:
  290|   435k|{
  291|   435k|    if (early || late)
  ------------------
  |  Branch (291:9): [True: 431k, False: 3.16k]
  |  Branch (291:18): [True: 1.31k, False: 1.84k]
  ------------------
  292|   433k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_OR);
  293|  1.84k|    else
  294|  1.84k|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_OR);
  295|   435k|}
jbig2_image.c:template_image_compose_opt:
  166|  18.7M|{
  167|  18.7M|    int i;
  168|  18.7M|    uint32_t j;
  169|  18.7M|    int bytewidth = (int)bytewidth_;
  170|       |
  171|  18.7M|    if (bytewidth == 1) {
  ------------------
  |  Branch (171:9): [True: 8.17M, False: 10.5M]
  ------------------
  172|   238M|        for (j = 0; j < h; j++) {
  ------------------
  |  Branch (172:21): [True: 230M, False: 8.17M]
  ------------------
  173|       |            /* Only 1 byte! */
  174|   230M|            uint8_t v = (((early ? 0 : ss[0]<<8) | (late ? 0 : ss[1]))>>shift);
  ------------------
  |  Branch (174:28): [True: 149M, False: 81.6M]
  |  Branch (174:53): [True: 151k, False: 230M]
  ------------------
  175|   230M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (175:17): [True: 131M, False: 98.9M]
  ------------------
  176|   131M|                *dd |= v & leftmask;
  177|  98.9M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (177:22): [True: 34.4k, False: 98.9M]
  ------------------
  178|  34.4k|                *dd &= (v & leftmask) | ~leftmask;
  179|  98.9M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (179:22): [True: 16.6M, False: 82.3M]
  ------------------
  180|  16.6M|                *dd ^= v & leftmask;
  181|  82.3M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (181:22): [True: 184k, False: 82.1M]
  ------------------
  182|   184k|                *dd ^= (~v) & leftmask;
  183|  82.1M|            else /* Replace */
  184|  82.1M|                *dd = (v & leftmask) | (*dd & ~leftmask);
  185|   230M|            dd += dstride;
  186|   230M|            ss += sstride;
  187|   230M|        }
  188|  8.17M|        return;
  189|  8.17M|    }
  190|  10.5M|    bytewidth -= 2;
  191|  10.5M|    if (shift == 0) {
  ------------------
  |  Branch (191:9): [True: 3.52M, False: 7.05M]
  ------------------
  192|  3.52M|        ss++;
  193|  55.7M|        for (j = 0; j < h; j++) {
  ------------------
  |  Branch (193:21): [True: 52.2M, False: 3.52M]
  ------------------
  194|       |            /* Left byte */
  195|  52.2M|            const uint8_t * JBIG2_RESTRICT s = ss;
  196|  52.2M|            uint8_t * JBIG2_RESTRICT d = dd;
  197|  52.2M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (197:17): [True: 513k, False: 51.7M]
  ------------------
  198|   513k|                *d++ |= *s++ & leftmask;
  199|  51.7M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (199:22): [True: 47.6k, False: 51.6M]
  ------------------
  200|  47.6k|                *d++ &= (*s++ & leftmask) | ~leftmask;
  201|  51.6M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (201:22): [True: 1.61k, False: 51.6M]
  ------------------
  202|  1.61k|                *d++ ^= *s++ & leftmask;
  203|  51.6M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (203:22): [True: 67.9k, False: 51.5M]
  ------------------
  204|  67.9k|                *d++ ^= (~*s++) & leftmask;
  205|  51.5M|            else /* Replace */
  206|  51.5M|                *d = (*s++ & leftmask) | (*d & ~leftmask), d++;
  207|       |            /* Central run */
  208|  2.19G|            for (i = bytewidth; i != 0; i--) {
  ------------------
  |  Branch (208:33): [True: 2.14G, False: 52.2M]
  ------------------
  209|  2.14G|                if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (209:21): [True: 1.74G, False: 397M]
  ------------------
  210|  1.74G|                    *d++ |= *s++;
  211|   397M|                else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (211:26): [True: 94.1k, False: 397M]
  ------------------
  212|  94.1k|                    *d++ &= *s++;
  213|   397M|                else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (213:26): [True: 91.2k, False: 397M]
  ------------------
  214|  91.2k|                    *d++ ^= *s++;
  215|   397M|                else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (215:26): [True: 536k, False: 396M]
  ------------------
  216|   536k|                    *d++ ^= ~*s++;
  217|   396M|                else /* Replace */
  218|   396M|                    *d++ = *s++;
  219|  2.14G|            }
  220|       |            /* Right byte */
  221|  52.2M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (221:17): [True: 513k, False: 51.7M]
  ------------------
  222|   513k|                *d |= *s & rightmask;
  223|  51.7M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (223:22): [True: 47.6k, False: 51.6M]
  ------------------
  224|  47.6k|                *d &= (*s & rightmask) | ~rightmask;
  225|  51.6M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (225:22): [True: 1.61k, False: 51.6M]
  ------------------
  226|  1.61k|                *d ^= *s & rightmask;
  227|  51.6M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (227:22): [True: 67.9k, False: 51.5M]
  ------------------
  228|  67.9k|                *d ^= (~*s) & rightmask;
  229|  51.5M|            else /* Replace */
  230|  51.5M|                *d = (*s & rightmask) | (*d & ~rightmask);
  231|  52.2M|            dd += dstride;
  232|  52.2M|            ss += sstride;
  233|  52.2M|        }
  234|  7.05M|    } else {
  235|   102M|        for (j = 0; j < h; j++) {
  ------------------
  |  Branch (235:21): [True: 95.0M, False: 7.05M]
  ------------------
  236|       |            /* Left byte */
  237|  95.0M|            const uint8_t * JBIG2_RESTRICT s = ss;
  238|  95.0M|            uint8_t * JBIG2_RESTRICT d = dd;
  239|  95.0M|            uint8_t s0, s1, v;
  240|  95.0M|            s0 = early ? 0 : *s;
  ------------------
  |  Branch (240:18): [True: 2.41M, False: 92.6M]
  ------------------
  241|  95.0M|            s++;
  242|  95.0M|            s1 = *s++;
  243|  95.0M|            v = ((s0<<8) | s1)>>shift;
  244|  95.0M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (244:17): [True: 2.03M, False: 92.9M]
  ------------------
  245|  2.03M|                *d++ |= v & leftmask;
  246|  92.9M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (246:22): [True: 116k, False: 92.8M]
  ------------------
  247|   116k|                *d++ &= (v & leftmask) | ~leftmask;
  248|  92.8M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (248:22): [True: 270k, False: 92.5M]
  ------------------
  249|   270k|                *d++ ^= v & leftmask;
  250|  92.5M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (250:22): [True: 36.4k, False: 92.5M]
  ------------------
  251|  36.4k|                *d++ ^= (~v) & leftmask;
  252|  92.5M|            else /* Replace */
  253|  92.5M|                *d = (v & leftmask) | (*d & ~leftmask), d++;
  254|       |            /* Central run */
  255|  1.61G|            for (i = bytewidth; i > 0; i--) {
  ------------------
  |  Branch (255:33): [True: 1.51G, False: 95.0M]
  ------------------
  256|  1.51G|                s0 = s1; s1 = *s++;
  257|  1.51G|                v = ((s0<<8) | s1)>>shift;
  258|  1.51G|                if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (258:21): [True: 683M, False: 835M]
  ------------------
  259|   683M|                    *d++ |= v;
  260|   835M|                else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (260:26): [True: 799k, False: 834M]
  ------------------
  261|   799k|                    *d++ &= v;
  262|   834M|                else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (262:26): [True: 794k, False: 834M]
  ------------------
  263|   794k|                    *d++ ^= v;
  264|   834M|                else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (264:26): [True: 4.06M, False: 830M]
  ------------------
  265|  4.06M|                    *d++ ^= ~v;
  266|   830M|                else /* Replace */
  267|   830M|                    *d++ = v;
  268|  1.51G|            }
  269|       |            /* Right byte */
  270|  95.0M|            s0 = s1; s1 = (late ? 0 : *s);
  ------------------
  |  Branch (270:28): [True: 1.49M, False: 93.5M]
  ------------------
  271|  95.0M|            v = (((s0<<8) | s1)>>shift);
  272|  95.0M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (272:17): [True: 2.03M, False: 92.9M]
  ------------------
  273|  2.03M|                *d |= v & rightmask;
  274|  92.9M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (274:22): [True: 116k, False: 92.8M]
  ------------------
  275|   116k|                *d &= (v & rightmask) | ~rightmask;
  276|  92.8M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (276:22): [True: 270k, False: 92.5M]
  ------------------
  277|   270k|                *d ^= v & rightmask;
  278|  92.5M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (278:22): [True: 36.4k, False: 92.5M]
  ------------------
  279|  36.4k|                *d ^= ~v & rightmask;
  280|  92.5M|            else /* Replace */
  281|  92.5M|                *d = (v & rightmask) | (*d & ~rightmask);
  282|  95.0M|            dd += dstride;
  283|  95.0M|            ss += sstride;
  284|  95.0M|        }
  285|  7.05M|    }
  286|  10.5M|}
jbig2_image.c:jbig2_image_compose_opt_AND:
  299|  1.86k|{
  300|  1.86k|    if (early || late)
  ------------------
  |  Branch (300:9): [True: 918, False: 950]
  |  Branch (300:18): [True: 318, False: 632]
  ------------------
  301|  1.23k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_AND);
  302|    632|    else
  303|    632|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_AND);
  304|  1.86k|}
jbig2_image.c:jbig2_image_compose_opt_XOR:
  308|   239k|{
  309|   239k|    if (early || late)
  ------------------
  |  Branch (309:9): [True: 236k, False: 2.98k]
  |  Branch (309:18): [True: 939, False: 2.04k]
  ------------------
  310|   237k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XOR);
  311|  2.04k|    else
  312|  2.04k|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XOR);
  313|   239k|}
jbig2_image.c:jbig2_image_compose_opt_XNOR:
  317|  2.16k|{
  318|  2.16k|    if (early || late)
  ------------------
  |  Branch (318:9): [True: 749, False: 1.41k]
  |  Branch (318:18): [True: 491, False: 923]
  ------------------
  319|  1.24k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XNOR);
  320|    923|    else
  321|    923|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XNOR);
  322|  2.16k|}
jbig2_image.c:jbig2_image_compose_opt_REPLACE:
  326|  18.0M|{
  327|  18.0M|    if (early || late)
  ------------------
  |  Branch (327:9): [True: 12.9k, False: 18.0M]
  |  Branch (327:18): [True: 10.9k, False: 18.0M]
  ------------------
  328|  23.8k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_REPLACE);
  329|  18.0M|    else
  330|  18.0M|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_REPLACE);
  331|  18.0M|}

jbig2_decode_generic_mmr:
 1207|  11.0k|{
 1208|  11.0k|    Jbig2MmrCtx mmr;
 1209|  11.0k|    const uint32_t rowstride = image->stride;
 1210|  11.0k|    byte *dst = image->data;
 1211|  11.0k|    byte *ref = NULL;
 1212|  11.0k|    uint32_t y;
 1213|  11.0k|    int code = 0;
 1214|  11.0k|    int eofb = 0;
 1215|       |
 1216|  11.0k|    (void) params;
 1217|       |
 1218|  11.0k|    jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
 1219|       |
 1220|   228M|    for (y = 0; !eofb && y < image->height; y++) {
  ------------------
  |  Branch (1220:17): [True: 228M, False: 1.99k]
  |  Branch (1220:26): [True: 228M, False: 8.88k]
  ------------------
 1221|   228M|        memset(dst, 0, rowstride);
 1222|   228M|        code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb);
 1223|   228M|        if (code < 0)
  ------------------
  |  Branch (1223:13): [True: 199, False: 228M]
  ------------------
 1224|    199|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode mmr line");
 1225|   228M|        ref = dst;
 1226|   228M|        dst += rowstride;
 1227|   228M|    }
 1228|       |
 1229|  10.8k|    if (eofb && y < image->height) {
  ------------------
  |  Branch (1229:9): [True: 1.99k, False: 8.88k]
  |  Branch (1229:17): [True: 1.90k, False: 89]
  ------------------
 1230|  1.90k|        memset(dst, 0, rowstride * (image->height - y));
 1231|  1.90k|    }
 1232|       |
 1233|  10.8k|    return code;
 1234|  11.0k|}
jbig2_decode_halftone_mmr:
 1252|  13.9k|{
 1253|  13.9k|    Jbig2MmrCtx mmr;
 1254|  13.9k|    const uint32_t rowstride = image->stride;
 1255|  13.9k|    byte *dst = image->data;
 1256|  13.9k|    byte *ref = NULL;
 1257|  13.9k|    uint32_t y;
 1258|  13.9k|    int code = 0;
 1259|  13.9k|    const uint32_t EOFB = 0x001001;
 1260|  13.9k|    int eofb = 0;
 1261|       |
 1262|  13.9k|    (void) params;
 1263|       |
 1264|  13.9k|    jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
 1265|       |
 1266|  58.5M|    for (y = 0; !eofb && y < image->height; y++) {
  ------------------
  |  Branch (1266:17): [True: 58.5M, False: 1.90k]
  |  Branch (1266:26): [True: 58.5M, False: 11.9k]
  ------------------
 1267|  58.5M|        memset(dst, 0, rowstride);
 1268|  58.5M|        code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb);
 1269|  58.5M|        if (code < 0)
  ------------------
  |  Branch (1269:13): [True: 64, False: 58.5M]
  ------------------
 1270|     64|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode halftone mmr line");
  ------------------
  |  |   68|     64|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1271|  58.5M|        ref = dst;
 1272|  58.5M|        dst += rowstride;
 1273|  58.5M|    }
 1274|       |
 1275|  13.8k|    if (eofb && y < image->height) {
  ------------------
  |  Branch (1275:9): [True: 1.90k, False: 11.9k]
  |  Branch (1275:17): [True: 1.66k, False: 246]
  ------------------
 1276|  1.66k|        memset(dst, 0, rowstride * (image->height - y));
 1277|  1.66k|    }
 1278|       |
 1279|       |    /* test for EOFB (see section 6.2.6) */
 1280|  13.8k|    if (mmr.word >> 8 == EOFB) {
  ------------------
  |  Branch (1280:9): [True: 1.34k, False: 12.5k]
  ------------------
 1281|  1.34k|        jbig2_decode_mmr_consume(&mmr, 24);
 1282|  1.34k|    }
 1283|       |
 1284|  13.8k|    *consumed_bytes += (mmr.consumed_bits + 7) / 8;
 1285|  13.8k|    return code;
 1286|  13.9k|}
jbig2_mmr.c:jbig2_decode_mmr_init:
   61|  24.9k|{
   62|  24.9k|    mmr->width = width;
   63|  24.9k|    mmr->height = height;
   64|  24.9k|    mmr->data = data;
   65|  24.9k|    mmr->size = size;
   66|  24.9k|    mmr->data_index = 0;
   67|  24.9k|    mmr->bit_index = 32;
   68|  24.9k|    mmr->word = 0;
   69|  24.9k|    mmr->consumed_bits = 0;
   70|       |
   71|   100k|    while (mmr->bit_index >= 8 && mmr->data_index < mmr->size) {
  ------------------
  |  Branch (71:12): [True: 86.1k, False: 14.8k]
  |  Branch (71:35): [True: 75.9k, False: 10.1k]
  ------------------
   72|  75.9k|        mmr->bit_index -= 8;
   73|  75.9k|        mmr->word |= (mmr->data[mmr->data_index] << mmr->bit_index);
   74|  75.9k|        mmr->data_index++;
   75|  75.9k|    }
   76|  24.9k|}
jbig2_mmr.c:jbig2_decode_mmr_line:
  985|   286M|{
  986|   286M|    uint32_t a0 = MINUS1;
  ------------------
  |  |   54|   286M|#define MINUS1 UINT32_MAX
  ------------------
  987|   286M|    uint32_t a1, a2, b1, b2;
  988|   286M|    int c = 0;                  /* 0 is white, black is 1 */
  989|       |
  990|   290M|    while (1) {
  ------------------
  |  Branch (990:12): [True: 290M, Folded]
  ------------------
  991|   290M|        uint32_t word = mmr->word;
  992|       |
  993|       |        /* printf ("%08x\n", word); */
  994|       |
  995|   290M|        if (a0 != MINUS1 && a0 >= mmr->width)
  ------------------
  |  |   54|   581M|#define MINUS1 UINT32_MAX
  ------------------
  |  Branch (995:13): [True: 3.83M, False: 286M]
  |  Branch (995:29): [True: 1.26M, False: 2.56M]
  ------------------
  996|  1.26M|            break;
  997|       |
  998|   289M|        if ((word >> (32 - 3)) == 1) {
  ------------------
  |  Branch (998:13): [True: 908k, False: 288M]
  ------------------
  999|   908k|            int white_run, black_run;
 1000|       |
 1001|   908k|            jbig2_decode_mmr_consume(mmr, 3);
 1002|       |
 1003|   908k|            if (a0 == MINUS1)
  ------------------
  |  |   54|   908k|#define MINUS1 UINT32_MAX
  ------------------
  |  Branch (1003:17): [True: 72.9k, False: 835k]
  ------------------
 1004|  72.9k|                a0 = 0;
 1005|       |
 1006|   908k|            if (c == 0) {
  ------------------
  |  Branch (1006:17): [True: 810k, False: 97.9k]
  ------------------
 1007|   810k|                white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8);
 1008|   810k|                if (white_run < 0)
  ------------------
  |  Branch (1008:21): [True: 46, False: 810k]
  ------------------
 1009|     46|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode white H run");
  ------------------
  |  |   68|     46|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1010|   810k|                black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7);
 1011|   810k|                if (black_run < 0)
  ------------------
  |  Branch (1011:21): [True: 155, False: 810k]
  ------------------
 1012|    155|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode black H run");
  ------------------
  |  |   68|    155|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1013|       |                /* printf ("H %d %d\n", white_run, black_run); */
 1014|   810k|                a1 = a0 + white_run;
 1015|   810k|                a2 = a1 + black_run;
 1016|   810k|                if (a1 > mmr->width)
  ------------------
  |  Branch (1016:21): [True: 44.7k, False: 765k]
  ------------------
 1017|  44.7k|                    a1 = mmr->width;
 1018|   810k|                if (a2 > mmr->width)
  ------------------
  |  Branch (1018:21): [True: 48.8k, False: 761k]
  ------------------
 1019|  48.8k|                    a2 = mmr->width;
 1020|   810k|                if (a2 < a1) {
  ------------------
  |  Branch (1020:21): [True: 0, False: 810k]
  ------------------
 1021|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative black H run");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1022|      0|                    a2 = a1;
 1023|      0|                }
 1024|   810k|                if (a1 < mmr->width)
  ------------------
  |  Branch (1024:21): [True: 764k, False: 45.1k]
  ------------------
 1025|   764k|                    jbig2_set_bits(dst, a1, a2);
 1026|   810k|                a0 = a2;
 1027|   810k|            } else {
 1028|  97.9k|                black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7);
 1029|  97.9k|                if (black_run < 0)
  ------------------
  |  Branch (1029:21): [True: 21, False: 97.9k]
  ------------------
 1030|     21|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode black H run");
  ------------------
  |  |   68|     21|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1031|  97.9k|                white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8);
 1032|  97.9k|                if (white_run < 0)
  ------------------
  |  Branch (1032:21): [True: 41, False: 97.8k]
  ------------------
 1033|     41|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode white H run");
  ------------------
  |  |   68|     41|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1034|       |                /* printf ("H %d %d\n", black_run, white_run); */
 1035|  97.8k|                a1 = a0 + black_run;
 1036|  97.8k|                a2 = a1 + white_run;
 1037|  97.8k|                if (a1 > mmr->width)
  ------------------
  |  Branch (1037:21): [True: 18.6k, False: 79.1k]
  ------------------
 1038|  18.6k|                    a1 = mmr->width;
 1039|  97.8k|                if (a2 > mmr->width)
  ------------------
  |  Branch (1039:21): [True: 35.3k, False: 62.5k]
  ------------------
 1040|  35.3k|                    a2 = mmr->width;
 1041|  97.8k|                if (a1 < a0) {
  ------------------
  |  Branch (1041:21): [True: 0, False: 97.8k]
  ------------------
 1042|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative white H run");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1043|      0|                    a1 = a0;
 1044|      0|                }
 1045|  97.8k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1045:21): [True: 97.8k, False: 0]
  ------------------
 1046|  97.8k|                    jbig2_set_bits(dst, a0, a1);
 1047|  97.8k|                a0 = a2;
 1048|  97.8k|            }
 1049|   908k|        }
 1050|       |
 1051|   288M|        else if ((word >> (32 - 4)) == 1) {
  ------------------
  |  Branch (1051:18): [True: 278k, False: 288M]
  ------------------
 1052|       |            /* printf ("P\n"); */
 1053|   278k|            jbig2_decode_mmr_consume(mmr, 4);
 1054|   278k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1055|   278k|            b2 = jbig2_find_changing_element(ref, b1, mmr->width);
 1056|   278k|            if (c) {
  ------------------
  |  Branch (1056:17): [True: 134k, False: 143k]
  ------------------
 1057|   134k|                if (b2 < a0) {
  ------------------
  |  Branch (1057:21): [True: 0, False: 134k]
  ------------------
 1058|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative P run");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1059|      0|                    b2 = a0;
 1060|      0|                }
 1061|   134k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1061:21): [True: 134k, False: 0]
  ------------------
 1062|   134k|                    jbig2_set_bits(dst, a0, b2);
 1063|   134k|            }
 1064|   278k|            a0 = b2;
 1065|   278k|        }
 1066|       |
 1067|   288M|        else if ((word >> (32 - 1)) == 1) {
  ------------------
  |  Branch (1067:18): [True: 1.32M, False: 286M]
  ------------------
 1068|       |            /* printf ("V(0)\n"); */
 1069|  1.32M|            jbig2_decode_mmr_consume(mmr, 1);
 1070|  1.32M|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1071|  1.32M|            if (c) {
  ------------------
  |  Branch (1071:17): [True: 486k, False: 839k]
  ------------------
 1072|   486k|                if (b1 < a0) {
  ------------------
  |  Branch (1072:21): [True: 0, False: 486k]
  ------------------
 1073|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative V(0) run");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1074|      0|                    b1 = a0;
 1075|      0|                }
 1076|   486k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1076:21): [True: 486k, False: 0]
  ------------------
 1077|   486k|                    jbig2_set_bits(dst, a0, b1);
 1078|   486k|            }
 1079|  1.32M|            a0 = b1;
 1080|  1.32M|            c = !c;
 1081|  1.32M|        }
 1082|       |
 1083|   286M|        else if ((word >> (32 - 3)) == 3) {
  ------------------
  |  Branch (1083:18): [True: 549k, False: 286M]
  ------------------
 1084|       |            /* printf ("VR(1)\n"); */
 1085|   549k|            jbig2_decode_mmr_consume(mmr, 3);
 1086|   549k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1087|   549k|            if (b1 + 1 <= mmr->width)
  ------------------
  |  Branch (1087:17): [True: 235k, False: 313k]
  ------------------
 1088|   235k|                b1 += 1;
 1089|   549k|            if (c) {
  ------------------
  |  Branch (1089:17): [True: 208k, False: 341k]
  ------------------
 1090|   208k|                if (b1 < a0) {
  ------------------
  |  Branch (1090:21): [True: 0, False: 208k]
  ------------------
 1091|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VR(1) run");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1092|      0|                    b1 = a0;
 1093|      0|                }
 1094|   208k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1094:21): [True: 208k, False: 0]
  ------------------
 1095|   208k|                    jbig2_set_bits(dst, a0, b1);
 1096|   208k|            }
 1097|   549k|            a0 = b1;
 1098|   549k|            c = !c;
 1099|   549k|        }
 1100|       |
 1101|   286M|        else if ((word >> (32 - 6)) == 3) {
  ------------------
  |  Branch (1101:18): [True: 120k, False: 286M]
  ------------------
 1102|       |            /* printf ("VR(2)\n"); */
 1103|   120k|            jbig2_decode_mmr_consume(mmr, 6);
 1104|   120k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1105|   120k|            if (b1 + 2 <= mmr->width)
  ------------------
  |  Branch (1105:17): [True: 35.6k, False: 84.4k]
  ------------------
 1106|  35.6k|                b1 += 2;
 1107|   120k|            if (c) {
  ------------------
  |  Branch (1107:17): [True: 69.8k, False: 50.3k]
  ------------------
 1108|  69.8k|                if (b1 < a0) {
  ------------------
  |  Branch (1108:21): [True: 0, False: 69.8k]
  ------------------
 1109|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VR(2) run");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1110|      0|                    b1 = a0;
 1111|      0|                }
 1112|  69.8k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1112:21): [True: 69.8k, False: 0]
  ------------------
 1113|  69.8k|                    jbig2_set_bits(dst, a0, b1);
 1114|  69.8k|            }
 1115|   120k|            a0 = b1;
 1116|   120k|            c = !c;
 1117|   120k|        }
 1118|       |
 1119|   286M|        else if ((word >> (32 - 7)) == 3) {
  ------------------
  |  Branch (1119:18): [True: 47.2k, False: 286M]
  ------------------
 1120|       |            /* printf ("VR(3)\n"); */
 1121|  47.2k|            jbig2_decode_mmr_consume(mmr, 7);
 1122|  47.2k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1123|  47.2k|            if (b1 + 3 <= mmr->width)
  ------------------
  |  Branch (1123:17): [True: 28.5k, False: 18.7k]
  ------------------
 1124|  28.5k|                b1 += 3;
 1125|  47.2k|            if (c) {
  ------------------
  |  Branch (1125:17): [True: 21.4k, False: 25.7k]
  ------------------
 1126|  21.4k|                if (b1 < a0) {
  ------------------
  |  Branch (1126:21): [True: 0, False: 21.4k]
  ------------------
 1127|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VR(3) run");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1128|      0|                    b1 = a0;
 1129|      0|                }
 1130|  21.4k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1130:21): [True: 21.4k, False: 0]
  ------------------
 1131|  21.4k|                    jbig2_set_bits(dst, a0, b1);
 1132|  21.4k|            }
 1133|  47.2k|            a0 = b1;
 1134|  47.2k|            c = !c;
 1135|  47.2k|        }
 1136|       |
 1137|   286M|        else if ((word >> (32 - 3)) == 2) {
  ------------------
  |  Branch (1137:18): [True: 383k, False: 285M]
  ------------------
 1138|       |            /* printf ("VL(1)\n"); */
 1139|   383k|            jbig2_decode_mmr_consume(mmr, 3);
 1140|   383k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1141|   383k|            if (b1 >= 1)
  ------------------
  |  Branch (1141:17): [True: 381k, False: 2.58k]
  ------------------
 1142|   381k|                b1 -= 1;
 1143|   383k|            if (c) {
  ------------------
  |  Branch (1143:17): [True: 118k, False: 265k]
  ------------------
 1144|   118k|                if (b1 < a0) {
  ------------------
  |  Branch (1144:21): [True: 0, False: 118k]
  ------------------
 1145|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(1) run");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1146|      0|                    b1 = a0;
 1147|      0|                }
 1148|   118k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1148:21): [True: 118k, False: 0]
  ------------------
 1149|   118k|                    jbig2_set_bits(dst, a0, b1);
 1150|   118k|            }
 1151|   383k|            a0 = b1;
 1152|   383k|            c = !c;
 1153|   383k|        }
 1154|       |
 1155|   285M|        else if ((word >> (32 - 6)) == 2) {
  ------------------
  |  Branch (1155:18): [True: 139k, False: 285M]
  ------------------
 1156|       |            /* printf ("VL(2)\n"); */
 1157|   139k|            jbig2_decode_mmr_consume(mmr, 6);
 1158|   139k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1159|   139k|            if (b1 >= 2)
  ------------------
  |  Branch (1159:17): [True: 138k, False: 1.36k]
  ------------------
 1160|   138k|                b1 -= 2;
 1161|   139k|            if (c) {
  ------------------
  |  Branch (1161:17): [True: 37.4k, False: 102k]
  ------------------
 1162|  37.4k|                if (b1 < a0) {
  ------------------
  |  Branch (1162:21): [True: 7.69k, False: 29.7k]
  ------------------
 1163|  7.69k|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(2) run");
  ------------------
  |  |   68|  7.69k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1164|  7.69k|                    b1 = a0;
 1165|  7.69k|                }
 1166|  37.4k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1166:21): [True: 37.4k, False: 0]
  ------------------
 1167|  37.4k|                    jbig2_set_bits(dst, a0, b1);
 1168|  37.4k|            }
 1169|   139k|            a0 = b1;
 1170|   139k|            c = !c;
 1171|   139k|        }
 1172|       |
 1173|   285M|        else if ((word >> (32 - 7)) == 2) {
  ------------------
  |  Branch (1173:18): [True: 82.6k, False: 285M]
  ------------------
 1174|       |            /* printf ("VL(3)\n"); */
 1175|  82.6k|            jbig2_decode_mmr_consume(mmr, 7);
 1176|  82.6k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1177|  82.6k|            if (b1 >= 3)
  ------------------
  |  Branch (1177:17): [True: 76.9k, False: 5.73k]
  ------------------
 1178|  76.9k|                b1 -= 3;
 1179|  82.6k|            if (c) {
  ------------------
  |  Branch (1179:17): [True: 30.6k, False: 52.0k]
  ------------------
 1180|  30.6k|                if (b1 < a0) {
  ------------------
  |  Branch (1180:21): [True: 8.53k, False: 22.0k]
  ------------------
 1181|  8.53k|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(3) run");
  ------------------
  |  |   68|  8.53k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1182|  8.53k|                    b1 = a0;
 1183|  8.53k|                }
 1184|  30.6k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1184:21): [True: 30.6k, False: 0]
  ------------------
 1185|  30.6k|                    jbig2_set_bits(dst, a0, b1);
 1186|  30.6k|            }
 1187|  82.6k|            a0 = b1;
 1188|  82.6k|            c = !c;
 1189|  82.6k|        }
 1190|       |
 1191|   285M|        else if ((word >> (32 - 24)) == 0x1001) {
  ------------------
  |  Branch (1191:18): [True: 3.90k, False: 285M]
  ------------------
 1192|       |            /* printf ("EOFB\n"); */
 1193|  3.90k|            jbig2_decode_mmr_consume(mmr, 24);
 1194|  3.90k|            *eofb = 1;
 1195|  3.90k|            break;
 1196|  3.90k|        }
 1197|       |
 1198|   285M|        else
 1199|   285M|            break;
 1200|   289M|    }
 1201|       |
 1202|   286M|    return 0;
 1203|   286M|}
jbig2_mmr.c:jbig2_decode_get_run:
  965|  1.81M|{
  966|  1.81M|    int result = 0;
  967|  1.81M|    int val;
  968|       |
  969|  5.47M|    do {
  970|  5.47M|        val = jbig2_decode_get_code(mmr, table, initial_bits);
  971|  5.47M|        if (val == ERROR)
  ------------------
  |  |   55|  5.47M|#define ERROR -1
  ------------------
  |  Branch (971:13): [True: 10, False: 5.47M]
  ------------------
  972|     10|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "invalid code detected in MMR-coded data");
  ------------------
  |  |   68|     10|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  973|  5.47M|        else if (val == UNCOMPRESSED)
  ------------------
  |  |   57|  5.47M|#define UNCOMPRESSED -3
  ------------------
  |  Branch (973:18): [True: 6, False: 5.47M]
  ------------------
  974|      6|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "uncompressed code in MMR-coded data");
  ------------------
  |  |   68|      6|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  975|  5.47M|        else if (val == ZEROES)
  ------------------
  |  |   56|  5.47M|#define ZEROES -2
  ------------------
  |  Branch (975:18): [True: 247, False: 5.47M]
  ------------------
  976|    247|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "zeroes code in MMR-coded data");
  ------------------
  |  |   68|    247|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  977|  5.47M|        result += val;
  978|  5.47M|    } while (val >= 64);
  ------------------
  |  Branch (978:14): [True: 3.65M, False: 1.81M]
  ------------------
  979|       |
  980|  1.81M|    return result;
  981|  1.81M|}
jbig2_mmr.c:jbig2_decode_get_code:
  944|  5.47M|{
  945|  5.47M|    uint32_t word = mmr->word;
  946|  5.47M|    int table_ix = word >> (32 - initial_bits);
  947|  5.47M|    int val = table[table_ix].val;
  948|  5.47M|    int n_bits = table[table_ix].n_bits;
  949|       |
  950|  5.47M|    if (n_bits > initial_bits) {
  ------------------
  |  Branch (950:9): [True: 16.7k, False: 5.45M]
  ------------------
  951|  16.7k|        int mask = (1 << (32 - initial_bits)) - 1;
  952|       |
  953|  16.7k|        table_ix = val + ((word & mask) >> (32 - n_bits));
  954|  16.7k|        val = table[table_ix].val;
  955|  16.7k|        n_bits = initial_bits + table[table_ix].n_bits;
  956|  16.7k|    }
  957|       |
  958|  5.47M|    jbig2_decode_mmr_consume(mmr, n_bits);
  959|       |
  960|  5.47M|    return val;
  961|  5.47M|}
jbig2_mmr.c:jbig2_set_bits:
  922|  1.96M|{
  923|  1.96M|    uint32_t a0, a1, b0, b1, a;
  924|       |
  925|  1.96M|    a0 = x0 >> 3;
  926|  1.96M|    a1 = x1 >> 3;
  927|       |
  928|  1.96M|    b0 = x0 & 7;
  929|  1.96M|    b1 = x1 & 7;
  930|       |
  931|  1.96M|    if (a0 == a1) {
  ------------------
  |  Branch (931:9): [True: 868k, False: 1.10M]
  ------------------
  932|   868k|        line[a0] |= lm[b0] & rm[b1];
  933|  1.10M|    } else {
  934|  1.10M|        line[a0] |= lm[b0];
  935|  2.26G|        for (a = a0 + 1; a < a1; a++)
  ------------------
  |  Branch (935:26): [True: 2.26G, False: 1.10M]
  ------------------
  936|  2.26G|            line[a] = 0xFF;
  937|  1.10M|        if (b1)
  ------------------
  |  Branch (937:13): [True: 807k, False: 293k]
  ------------------
  938|   807k|            line[a1] |= rm[b1];
  939|  1.10M|    }
  940|  1.96M|}
jbig2_mmr.c:jbig2_find_changing_element_of_color:
  908|  2.92M|{
  909|  2.92M|    if (line == NULL)
  ------------------
  |  Branch (909:9): [True: 17.4k, False: 2.90M]
  ------------------
  910|  17.4k|        return w;
  911|  2.90M|    x = jbig2_find_changing_element(line, x, w);
  912|  2.90M|    if (x < w && getbit(line, x) != color)
  ------------------
  |  |  745|  1.78M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  |  Branch (912:9): [True: 1.78M, False: 1.12M]
  |  Branch (912:18): [True: 356k, False: 1.42M]
  ------------------
  913|   356k|        x = jbig2_find_changing_element(line, x, w);
  914|  2.90M|    return x;
  915|  2.92M|}
jbig2_mmr.c:jbig2_find_changing_element:
  759|  3.54M|{
  760|  3.54M|    int a;
  761|  3.54M|    uint8_t     all8;
  762|  3.54M|    uint16_t    all16;
  763|  3.54M|    uint32_t    all32;
  764|       |
  765|  3.54M|    if (line == NULL)
  ------------------
  |  Branch (765:9): [True: 630, False: 3.54M]
  ------------------
  766|    630|        return w;
  767|       |
  768|  3.54M|    if (x == MINUS1) {
  ------------------
  |  |   54|  3.54M|#define MINUS1 UINT32_MAX
  ------------------
  |  Branch (768:9): [True: 1.18M, False: 2.35M]
  ------------------
  769|  1.18M|        a = 0;
  770|  1.18M|        x = 0;
  771|  2.35M|    } else if (x < w) {
  ------------------
  |  Branch (771:16): [True: 2.16M, False: 188k]
  ------------------
  772|  2.16M|        a = getbit(line, x);
  ------------------
  |  |  745|  2.16M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  773|  2.16M|        x++;
  774|  2.16M|    } else {
  775|   188k|        return x;
  776|   188k|    }
  777|       |
  778|       |    /* We will be looking for a uint8 or uint16 or uint32 that has at least one
  779|       |    bit different from <a>, so prepare some useful values for comparison. */
  780|  3.35M|    all8  = (a) ? 0xff : 0;
  ------------------
  |  Branch (780:13): [True: 1.20M, False: 2.14M]
  ------------------
  781|  3.35M|    all16 = (a) ? 0xffff : 0;
  ------------------
  |  Branch (781:13): [True: 1.20M, False: 2.14M]
  ------------------
  782|  3.35M|    all32 = (a) ? 0xffffffff : 0;
  ------------------
  |  Branch (782:13): [True: 1.20M, False: 2.14M]
  ------------------
  783|       |
  784|       |    /* Check individual bits up to next 8-bit boundary.
  785|       |
  786|       |    [Would it be worth looking at top 4 bits, then at 2 bits then at 1 bit,
  787|       |    instead of iterating over all 8 bits? */
  788|       |
  789|  3.35M|    if ( ((uint8_t*) line)[ x / 8] == all8) {
  ------------------
  |  Branch (789:10): [True: 1.40M, False: 1.94M]
  ------------------
  790|       |        /* Don't bother checking individual bits if the enclosing uint8 equals
  791|       |        all8 - just move to the next byte. */
  792|  1.40M|        x = x / 8 * 8 + 8;
  793|  1.40M|        if (x >= w) {
  ------------------
  |  Branch (793:13): [True: 139k, False: 1.27M]
  ------------------
  794|   139k|            x = w;
  795|   139k|            goto end;
  796|   139k|        }
  797|  1.94M|    } else {
  798|  4.46M|        for(;;) {
  799|  4.46M|            if (x == w) {
  ------------------
  |  Branch (799:17): [True: 424k, False: 4.04M]
  ------------------
  800|   424k|                goto end;
  801|   424k|            }
  802|  4.04M|            if (x % 8 == 0) {
  ------------------
  |  Branch (802:17): [True: 991k, False: 3.05M]
  ------------------
  803|   991k|                break;
  804|   991k|            }
  805|  3.05M|            if (getbit(line, x) != a) {
  ------------------
  |  |  745|  3.05M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  |  Branch (805:17): [True: 528k, False: 2.52M]
  ------------------
  806|   528k|                goto end;
  807|   528k|            }
  808|  2.52M|            x += 1;
  809|  2.52M|        }
  810|  1.94M|    }
  811|       |
  812|  3.35M|    assert(x % 8 == 0);
  ------------------
  |  Branch (812:5): [True: 0, False: 2.26M]
  |  Branch (812:5): [True: 2.26M, False: 0]
  ------------------
  813|       |    /* Check next uint8 if we are not on 16-bit boundary. */
  814|  2.26M|    if (x % 16) {
  ------------------
  |  Branch (814:9): [True: 1.44M, False: 813k]
  ------------------
  815|  1.44M|        if (w - x < 8) {
  ------------------
  |  Branch (815:13): [True: 7.14k, False: 1.44M]
  ------------------
  816|  7.14k|            goto check1;
  817|  7.14k|        }
  818|  1.44M|        if ( ((uint8_t*) line)[ x / 8] != all8) {
  ------------------
  |  Branch (818:14): [True: 192k, False: 1.24M]
  ------------------
  819|   192k|            goto check1;
  820|   192k|        }
  821|  1.24M|        x += 8; /* This will make x a multiple of 16. */
  822|  1.24M|    }
  823|       |
  824|  2.26M|    assert(x % 16 == 0);
  ------------------
  |  Branch (824:5): [True: 0, False: 2.06M]
  |  Branch (824:5): [True: 2.06M, False: 0]
  ------------------
  825|       |    /* Check next uint16 if we are not on 32-bit boundary. */
  826|  2.06M|    if (x % 32) {
  ------------------
  |  Branch (826:9): [True: 1.36M, False: 696k]
  ------------------
  827|  1.36M|        if (w - x < 16) {
  ------------------
  |  Branch (827:13): [True: 11.8k, False: 1.35M]
  ------------------
  828|  11.8k|            goto check8;
  829|  11.8k|        }
  830|  1.35M|        if ( getword16((line + (x / 8))) != all16) {
  ------------------
  |  |  754|  1.35M|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  ------------------
  |  Branch (830:14): [True: 129k, False: 1.22M]
  ------------------
  831|   129k|            goto check8_no_eof;
  832|   129k|        }
  833|  1.22M|        x += 16; /* This will make x a multiple of 32. */
  834|  1.22M|    }
  835|       |
  836|       |    /* We are now on a 32-bit boundary. Check uint32's until we reach last
  837|       |    sub-32-bit region. */
  838|  2.06M|    assert(x % 32 == 0);
  ------------------
  |  Branch (838:5): [True: 0, False: 1.92M]
  |  Branch (838:5): [True: 1.92M, False: 0]
  ------------------
  839|  1.33G|    for(;;) {
  840|  1.33G|        if (w - x < 32) {
  ------------------
  |  Branch (840:13): [True: 1.18M, False: 1.33G]
  ------------------
  841|       |            /* We could still look at the uint32 here - if it equals all32, we
  842|       |            know there is no match before <w> so could do {x = w; goto end;}.
  843|       |
  844|       |            But for now we simply fall into the epilogue checking, which will
  845|       |            look at the next uint16, then uint8, then last 8 bits. */
  846|  1.18M|            goto check16;
  847|  1.18M|        }
  848|  1.33G|        if ( getword32((line + (x / 8))) != all32) {
  ------------------
  |  |  755|  1.33G|#define getword32(b)  ((uint32_t)(getword16(b) | (getword16((b + 2)) << 16)))
  |  |  ------------------
  |  |  |  |  754|  1.33G|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  |  |  ------------------
  |  |               #define getword32(b)  ((uint32_t)(getword16(b) | (getword16((b + 2)) << 16)))
  |  |  ------------------
  |  |  |  |  754|  1.33G|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  |  |  ------------------
  ------------------
  |  Branch (848:14): [True: 735k, False: 1.33G]
  ------------------
  849|   735k|            goto check16_no_eof;
  850|   735k|        }
  851|  1.33G|        x += 32;
  852|  1.33G|    }
  853|       |
  854|       |    /* Check next uint16. */
  855|  1.18M|check16:
  856|  1.18M|    assert(x % 16 == 0);
  ------------------
  |  Branch (856:5): [True: 0, False: 1.18M]
  |  Branch (856:5): [True: 1.18M, False: 0]
  ------------------
  857|  1.18M|    if (w - x < 16) {
  ------------------
  |  Branch (857:9): [True: 1.13M, False: 50.6k]
  ------------------
  858|  1.13M|        goto check8;
  859|  1.13M|    }
  860|   785k|check16_no_eof:
  861|   785k|    assert(w - x >= 16);
  ------------------
  |  Branch (861:5): [True: 0, False: 785k]
  |  Branch (861:5): [True: 785k, False: 0]
  ------------------
  862|   785k|    if ( getword16((line + (x / 8))) != all16) {
  ------------------
  |  |  754|   785k|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  ------------------
  |  Branch (862:10): [True: 333k, False: 452k]
  ------------------
  863|   333k|        goto check8_no_eof;
  864|   333k|    }
  865|   452k|    x += 16;
  866|       |
  867|       |    /* Check next uint8. */
  868|  1.59M|check8:
  869|  1.59M|    assert(x % 8 == 0);
  ------------------
  |  Branch (869:5): [True: 0, False: 1.59M]
  |  Branch (869:5): [True: 1.59M, False: 0]
  ------------------
  870|  1.59M|    if (w - x < 8) {
  ------------------
  |  Branch (870:9): [True: 1.13M, False: 463k]
  ------------------
  871|  1.13M|        goto check1;
  872|  1.13M|    }
  873|   926k|check8_no_eof:
  874|   926k|    assert(w - x >= 8);
  ------------------
  |  Branch (874:5): [True: 0, False: 926k]
  |  Branch (874:5): [True: 926k, False: 0]
  ------------------
  875|   926k|    if ( ((uint8_t*) line)[x/8] != all8) {
  ------------------
  |  Branch (875:10): [True: 447k, False: 479k]
  ------------------
  876|   447k|        goto check1;
  877|   447k|    }
  878|   479k|    x += 8;
  879|       |
  880|       |    /* Check up to the next 8 bits. */
  881|  2.26M|check1:
  882|  2.26M|    assert(x % 8 == 0);
  ------------------
  |  Branch (882:5): [True: 0, False: 2.26M]
  |  Branch (882:5): [True: 2.26M, False: 0]
  ------------------
  883|  2.26M|    if ( ((uint8_t*) line)[ x / 8] == all8) {
  ------------------
  |  Branch (883:10): [True: 514k, False: 1.74M]
  ------------------
  884|   514k|        x = w;
  885|   514k|        goto end;
  886|   514k|    }
  887|  1.74M|    {
  888|  6.49M|        for(;;) {
  889|  6.49M|            if (x == w) {
  ------------------
  |  Branch (889:17): [True: 271k, False: 6.22M]
  ------------------
  890|   271k|                goto end;
  891|   271k|            }
  892|  6.22M|            if (getbit(line, x) != a) {
  ------------------
  |  |  745|  6.22M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  |  Branch (892:17): [True: 1.47M, False: 4.74M]
  ------------------
  893|  1.47M|                goto end;
  894|  1.47M|            }
  895|  4.74M|            x += 1;
  896|  4.74M|        }
  897|  1.74M|    }
  898|       |
  899|  3.35M|end:
  900|  3.35M|    return x;
  901|  1.74M|}
jbig2_mmr.c:jbig2_decode_mmr_consume:
   80|  9.31M|{
   81|  9.31M|    mmr->consumed_bits += n_bits;
   82|  9.31M|    if (mmr->consumed_bits > mmr->size * 8)
  ------------------
  |  Branch (82:9): [True: 2.58k, False: 9.31M]
  ------------------
   83|  2.58k|        mmr->consumed_bits = mmr->size * 8;
   84|       |
   85|  9.31M|    mmr->word <<= n_bits;
   86|  9.31M|    mmr->bit_index += n_bits;
   87|  14.9M|    while (mmr->bit_index >= 8 && mmr->data_index < mmr->size) {
  ------------------
  |  Branch (87:12): [True: 5.67M, False: 9.26M]
  |  Branch (87:35): [True: 5.62M, False: 47.3k]
  ------------------
   88|  5.62M|        mmr->bit_index -= 8;
   89|  5.62M|        mmr->word |= (mmr->data[mmr->data_index] << mmr->bit_index);
   90|  5.62M|        mmr->data_index++;
   91|  5.62M|    }
   92|  9.31M|}

jbig2_page_info:
   67|  8.86k|{
   68|  8.86k|    Jbig2Page *page, *pages;
   69|       |
   70|       |    /* a new page info segment implies the previous page is finished */
   71|  8.86k|    page = &(ctx->pages[ctx->current_page]);
   72|  8.86k|    if (page->number != 0 && (page->state == JBIG2_PAGE_NEW || page->state == JBIG2_PAGE_FREE)) {
  ------------------
  |  Branch (72:9): [True: 7.11k, False: 1.74k]
  |  Branch (72:31): [True: 7.02k, False: 98]
  |  Branch (72:64): [True: 0, False: 98]
  ------------------
   73|  7.02k|        page->state = JBIG2_PAGE_COMPLETE;
   74|  7.02k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unexpected page info segment, marking previous page finished");
   75|  7.02k|    }
   76|       |
   77|       |    /* find a free page */
   78|  8.86k|    {
   79|  8.86k|        uint32_t index, j;
   80|       |
   81|  8.86k|        index = ctx->current_page;
   82|  16.5k|        while (ctx->pages[index].state != JBIG2_PAGE_FREE) {
  ------------------
  |  Branch (82:16): [True: 7.73k, False: 8.86k]
  ------------------
   83|  7.73k|            index++;
   84|  7.73k|            if (index >= ctx->max_page_index) {
  ------------------
  |  Branch (84:17): [True: 159, False: 7.57k]
  ------------------
   85|       |                /* grow the list */
   86|       |
   87|    159|                if (ctx->max_page_index == UINT32_MAX) {
  ------------------
  |  Branch (87:21): [True: 0, False: 159]
  ------------------
   88|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "too many pages in jbig2 image");
   89|      0|                }
   90|    159|                else if (ctx->max_page_index > (UINT32_MAX >> 2)) {
  ------------------
  |  Branch (90:26): [True: 0, False: 159]
  ------------------
   91|      0|                    ctx->max_page_index = UINT32_MAX;
   92|    159|                } else {
   93|    159|                    ctx->max_page_index <<= 2;
   94|    159|                }
   95|       |
   96|    159|                pages = jbig2_renew(ctx, ctx->pages, Jbig2Page, ctx->max_page_index);
  ------------------
  |  |  143|    159|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
   97|    159|                if (pages == NULL) {
  ------------------
  |  Branch (97:21): [True: 1, False: 158]
  ------------------
   98|      1|                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to reallocate pages");
   99|      1|                }
  100|    158|                ctx->pages = pages;
  101|  13.3k|                for (j = index; j < ctx->max_page_index; j++) {
  ------------------
  |  Branch (101:33): [True: 13.1k, False: 158]
  ------------------
  102|  13.1k|                    ctx->pages[j].state = JBIG2_PAGE_FREE;
  103|  13.1k|                    ctx->pages[j].number = 0;
  104|  13.1k|                    ctx->pages[j].image = NULL;
  105|  13.1k|                }
  106|    158|            }
  107|  7.73k|        }
  108|  8.86k|        page = &(ctx->pages[index]);
  109|  8.86k|        ctx->current_page = index;
  110|  8.86k|        page->state = JBIG2_PAGE_NEW;
  111|  8.86k|        page->number = segment->page_association;
  112|  8.86k|    }
  113|       |
  114|       |    /* FIXME: would be nice if we tried to work around this */
  115|  8.86k|    if (segment->data_length < 19) {
  ------------------
  |  Branch (115:9): [True: 12, False: 8.85k]
  ------------------
  116|     12|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  117|     12|    }
  118|       |
  119|       |    /* 7.4.8.x */
  120|  8.85k|    page->width = jbig2_get_uint32(segment_data);
  121|  8.85k|    page->height = jbig2_get_uint32(segment_data + 4);
  122|       |
  123|  8.85k|    page->x_resolution = jbig2_get_uint32(segment_data + 8);
  124|  8.85k|    page->y_resolution = jbig2_get_uint32(segment_data + 12);
  125|  8.85k|    page->flags = segment_data[16];
  126|       |    /* Check for T.88 amendment 3 */
  127|  8.85k|    if (page->flags & 0x80)
  ------------------
  |  Branch (127:9): [True: 11, False: 8.84k]
  ------------------
  128|     11|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "page segment indicates use of color segments (NYI)");
  129|       |
  130|       |    /* 7.4.8.6 */
  131|  8.84k|    {
  132|  8.84k|        int16_t striping = jbig2_get_int16(segment_data + 17);
  133|       |
  134|  8.84k|        if (striping & 0x8000) {
  ------------------
  |  Branch (134:13): [True: 5.87k, False: 2.96k]
  ------------------
  135|  5.87k|            page->striped = TRUE;
  ------------------
  |  |   53|  5.87k|#define TRUE 1
  ------------------
  136|  5.87k|            page->stripe_size = striping & 0x7FFF;
  137|  5.87k|        } else {
  138|  2.96k|            page->striped = FALSE;
  ------------------
  |  |   56|  2.96k|#define FALSE 0
  ------------------
  139|  2.96k|            page->stripe_size = 0;      /* would page->height be better? */
  140|  2.96k|        }
  141|  8.84k|    }
  142|  8.84k|    if (page->height == 0xFFFFFFFF && page->striped == FALSE) {
  ------------------
  |  |   56|  6.03k|#define FALSE 0
  ------------------
  |  Branch (142:9): [True: 6.03k, False: 2.80k]
  |  Branch (142:39): [True: 288, False: 5.74k]
  ------------------
  143|    288|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "height is unspecified but page is not marked as striped, assuming striped with maximum strip size");
  144|    288|        page->striped = TRUE;
  ------------------
  |  |   53|    288|#define TRUE 1
  ------------------
  145|    288|        page->stripe_size = 0x7FFF;
  146|    288|    }
  147|  8.84k|    page->end_row = 0;
  148|       |
  149|  8.84k|    if (segment->data_length > 19) {
  ------------------
  |  Branch (149:9): [True: 7.10k, False: 1.73k]
  ------------------
  150|  7.10k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extra data in segment");
  151|  7.10k|    }
  152|       |
  153|  8.84k|    dump_page_info(ctx, segment, page);
  154|       |
  155|       |    /* allocate an appropriate page image buffer */
  156|       |    /* 7.4.8.2 */
  157|  8.84k|    if (page->height == 0xFFFFFFFF) {
  ------------------
  |  Branch (157:9): [True: 6.03k, False: 2.80k]
  ------------------
  158|  6.03k|        page->image = jbig2_image_new(ctx, page->width, page->stripe_size);
  159|  6.03k|    } else {
  160|  2.80k|        page->image = jbig2_image_new(ctx, page->width, page->height);
  161|  2.80k|    }
  162|  8.84k|    if (page->image == NULL) {
  ------------------
  |  Branch (162:9): [True: 99, False: 8.74k]
  ------------------
  163|     99|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate buffer for page image");
  164|  8.74k|    } else {
  165|       |        /* 8.2 (3) fill the page with the default pixel value */
  166|  8.74k|        jbig2_image_clear(ctx, page->image, (page->flags & 4));
  167|  8.74k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  168|  8.74k|                    "allocated %dx%d page image (%d bytes)", page->image->width, page->image->height, page->image->stride * page->image->height);
  169|  8.74k|    }
  170|       |
  171|  8.74k|    return 0;
  172|  8.84k|}
jbig2_end_of_stripe:
  179|    686|{
  180|    686|    Jbig2Page *page = &ctx->pages[ctx->current_page];
  181|    686|    uint32_t end_row;
  182|       |
  183|    686|    if (segment->data_length < 4)
  ------------------
  |  Branch (183:9): [True: 11, False: 675]
  ------------------
  184|     11|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  185|    675|    end_row = jbig2_get_uint32(segment_data);
  186|    675|    if (end_row < page->end_row) {
  ------------------
  |  Branch (186:9): [True: 245, False: 430]
  ------------------
  187|    245|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  188|    245|                    "end of stripe segment with non-positive end row advance (new end row %d vs current end row %d)", end_row, page->end_row);
  189|    430|    } else {
  190|    430|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of stripe: advancing end row from %u to %u", page->end_row, end_row);
  191|    430|    }
  192|       |
  193|    675|    page->end_row = end_row;
  194|       |
  195|    675|    return 0;
  196|    686|}
jbig2_complete_page:
  208|  7.19k|{
  209|  7.19k|    int code;
  210|       |
  211|       |    /* check for unfinished segments */
  212|  7.19k|    if (ctx->segment_index != ctx->n_segments) {
  ------------------
  |  Branch (212:9): [True: 6.70k, False: 483]
  ------------------
  213|  6.70k|        Jbig2Segment *segment = ctx->segments[ctx->segment_index];
  214|       |
  215|       |        /* Some versions of Xerox Workcentre generate PDF files
  216|       |           with the segment data length field of the last segment
  217|       |           set to -1. Try to cope with this here. */
  218|  6.70k|        if ((segment->data_length & 0xffffffff) == 0xffffffff) {
  ------------------
  |  Branch (218:13): [True: 4.79k, False: 1.91k]
  ------------------
  219|  4.79k|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "file has an invalid segment data length; trying to decode using the available data");
  220|  4.79k|            segment->data_length = ctx->buf_wr_ix - ctx->buf_rd_ix;
  221|  4.79k|            code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
  222|  4.79k|            ctx->buf_rd_ix += segment->data_length;
  223|  4.79k|            ctx->segment_index++;
  224|  4.79k|            if (code < 0) {
  ------------------
  |  Branch (224:17): [True: 4.07k, False: 714]
  ------------------
  225|  4.07k|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to parse segment");
  226|  4.07k|            }
  227|  4.79k|        }
  228|  6.70k|    }
  229|       |
  230|       |    /* ensure image exists before marking page as complete */
  231|  3.11k|    if (ctx->pages[ctx->current_page].image == NULL) {
  ------------------
  |  Branch (231:9): [True: 1.13k, False: 1.98k]
  ------------------
  232|  1.13k|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page has no image, cannot be completed");
  ------------------
  |  |   68|  1.13k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  233|  1.13k|    }
  234|       |
  235|  1.98k|    ctx->pages[ctx->current_page].state = JBIG2_PAGE_COMPLETE;
  236|  1.98k|    return 0;
  237|  3.11k|}
jbig2_end_of_page:
  244|  1.39k|{
  245|  1.39k|    uint32_t page_number = ctx->pages[ctx->current_page].number;
  246|  1.39k|    int code;
  247|       |
  248|  1.39k|    (void) segment_data;
  249|       |
  250|  1.39k|    if (segment->page_association != page_number) {
  ------------------
  |  Branch (250:9): [True: 639, False: 757]
  ------------------
  251|    639|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  252|    639|                    "end of page marker for page %d doesn't match current page number %d", segment->page_association, page_number);
  253|    639|    }
  254|       |
  255|  1.39k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of page %d", page_number);
  256|       |
  257|  1.39k|    code = jbig2_complete_page(ctx);
  258|  1.39k|    if (code < 0)
  ------------------
  |  Branch (258:9): [True: 43, False: 1.35k]
  ------------------
  259|     43|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to complete page");
  260|       |
  261|       |#ifdef OUTPUT_PBM
  262|       |    code = jbig2_image_write_pbm(ctx->pages[ctx->current_page].image, stdout);
  263|       |    if (code < 0)
  264|       |        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write page image");
  265|       |#endif
  266|       |
  267|  1.35k|    return 0;
  268|  1.39k|}
jbig2_page_add_result:
  278|  22.9k|{
  279|  22.9k|    int code;
  280|       |
  281|  22.9k|    if (x > INT32_MAX || y > INT32_MAX)
  ------------------
  |  Branch (281:9): [True: 335, False: 22.5k]
  |  Branch (281:26): [True: 306, False: 22.2k]
  ------------------
  282|    641|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unsupported image coordinates");
  ------------------
  |  |   68|    641|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  283|       |
  284|       |    /* ensure image exists first */
  285|  22.2k|    if (page->image == NULL)
  ------------------
  |  Branch (285:9): [True: 291, False: 21.9k]
  ------------------
  286|    291|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page info possibly missing, no image defined");
  ------------------
  |  |   68|    291|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  287|       |
  288|       |    /* grow the page to accommodate a new stripe if necessary */
  289|  21.9k|    if (page->striped && page->height == 0xFFFFFFFF) {
  ------------------
  |  Branch (289:9): [True: 12.2k, False: 9.74k]
  |  Branch (289:26): [True: 10.1k, False: 2.12k]
  ------------------
  290|  10.1k|        uint32_t new_height;
  291|       |
  292|  10.1k|        if (y > UINT32_MAX - image->height)
  ------------------
  |  Branch (292:13): [True: 0, False: 10.1k]
  ------------------
  293|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "adding image at coordinate would grow page out of bounds");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  294|  10.1k|        new_height = y + image->height;
  295|       |
  296|  10.1k|        if (page->image->height < new_height) {
  ------------------
  |  Branch (296:13): [True: 5.64k, False: 4.47k]
  ------------------
  297|  5.64k|            Jbig2Image *resized_image = NULL;
  298|       |
  299|  5.64k|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "growing page buffer to %u rows to accommodate new stripe", new_height);
  ------------------
  |  |   68|  5.64k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  300|  5.64k|            resized_image = jbig2_image_resize(ctx, page->image, page->image->width, new_height, page->flags & 4);
  301|  5.64k|            if (resized_image == NULL) {
  ------------------
  |  Branch (301:17): [True: 64, False: 5.58k]
  ------------------
  302|     64|                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unable to resize image to accommodate new stripe");
  ------------------
  |  |   68|     64|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  303|     64|            }
  304|  5.58k|            page->image = resized_image;
  305|  5.58k|        }
  306|  10.1k|    }
  307|       |
  308|  21.9k|    code = jbig2_image_compose(ctx, page->image, image, x, y, op);
  309|  21.9k|    if (code < 0)
  ------------------
  |  Branch (309:9): [True: 0, False: 21.9k]
  ------------------
  310|      0|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to compose image with page");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  311|       |
  312|  21.9k|    return 0;
  313|  21.9k|}
jbig2_page_out:
  329|    627|{
  330|    627|    uint32_t index;
  331|       |
  332|       |    /* search for a completed page */
  333|    728|    for (index = 0; index < ctx->max_page_index; index++) {
  ------------------
  |  Branch (333:21): [True: 728, False: 0]
  ------------------
  334|    728|        if (ctx->pages[index].state == JBIG2_PAGE_COMPLETE) {
  ------------------
  |  Branch (334:13): [True: 627, False: 101]
  ------------------
  335|    627|            Jbig2Image *img = ctx->pages[index].image;
  336|    627|            uint32_t page_number = ctx->pages[index].number;
  337|       |
  338|    627|            if (img == NULL) {
  ------------------
  |  Branch (338:17): [True: 0, False: 627]
  ------------------
  339|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d returned with no associated image", page_number);
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  340|      0|                continue;
  341|      0|            }
  342|       |
  343|    627|            ctx->pages[index].state = JBIG2_PAGE_RETURNED;
  344|    627|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d returned to the client", page_number);
  ------------------
  |  |   68|    627|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  345|    627|            return jbig2_image_reference(ctx, img);
  346|    627|        }
  347|    728|    }
  348|       |
  349|       |    /* no pages available */
  350|      0|    return NULL;
  351|    627|}
jbig2_release_page:
  358|    627|{
  359|    627|    uint32_t index;
  360|       |
  361|    627|    if (image == NULL)
  ------------------
  |  Branch (361:9): [True: 0, False: 627]
  ------------------
  362|      0|        return;
  363|       |
  364|       |    /* find the matching page struct and mark it released */
  365|    728|    for (index = 0; index < ctx->max_page_index; index++) {
  ------------------
  |  Branch (365:21): [True: 728, False: 0]
  ------------------
  366|    728|        if (ctx->pages[index].image == image) {
  ------------------
  |  Branch (366:13): [True: 627, False: 101]
  ------------------
  367|    627|            jbig2_image_release(ctx, image);
  368|    627|            ctx->pages[index].state = JBIG2_PAGE_RELEASED;
  369|    627|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d released by the client", ctx->pages[index].number);
  ------------------
  |  |   68|    627|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  370|    627|            return;
  371|    627|        }
  372|    728|    }
  373|       |
  374|       |    /* no matching pages */
  375|      0|    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to release unknown page");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  376|      0|}
jbig2_page.c:dump_page_info:
   44|  8.84k|{
   45|  8.84k|    if (page->x_resolution == 0) {
  ------------------
  |  Branch (45:9): [True: 1.12k, False: 7.71k]
  ------------------
   46|  1.12k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (unknown res)", page->number, page->width, page->height);
   47|  7.71k|    } else if (page->x_resolution == page->y_resolution) {
  ------------------
  |  Branch (47:16): [True: 160, False: 7.55k]
  ------------------
   48|    160|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (%d ppm)", page->number, page->width, page->height, page->x_resolution);
   49|  7.55k|    } else {
   50|  7.55k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
   51|  7.55k|                    "page %d image is %dx%d (%dx%d ppm)", page->number, page->width, page->height, page->x_resolution, page->y_resolution);
   52|  7.55k|    }
   53|  8.84k|    if (page->striped) {
  ------------------
  |  Branch (53:9): [True: 6.16k, False: 2.67k]
  ------------------
   54|  6.16k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "\tmaximum stripe size: %d", page->stripe_size);
   55|  6.16k|    }
   56|  8.84k|}

jbig2_decode_refinement_region:
  375|  97.1k|{
  376|  97.1k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  377|  97.1k|                "decoding generic refinement region with offset %d,%x, GRTEMPLATE=%d, TPGRON=%d",
  378|  97.1k|                params->GRREFERENCEDX, params->GRREFERENCEDY, params->GRTEMPLATE, params->TPGRON);
  379|       |
  380|  97.1k|    if (params->TPGRON)
  ------------------
  |  Branch (380:9): [True: 484, False: 96.6k]
  ------------------
  381|    484|        return jbig2_decode_refinement_TPGRON(ctx, params, as, image, GR_stats);
  382|       |
  383|  96.6k|    if (params->GRTEMPLATE)
  ------------------
  |  Branch (383:9): [True: 6.50k, False: 90.1k]
  ------------------
  384|  6.50k|        return jbig2_decode_refinement_template1_unopt(ctx, segment, params, as, image, GR_stats);
  385|  90.1k|    else
  386|  90.1k|        return jbig2_decode_refinement_template0_unopt(ctx, segment, params, as, image, GR_stats);
  387|  96.6k|}
jbig2_refinement_region:
  427|    858|{
  428|    858|    Jbig2RefinementRegionParams params;
  429|    858|    Jbig2RegionSegmentInfo rsi;
  430|    858|    int offset = 0;
  431|    858|    byte seg_flags;
  432|    858|    int code = 0;
  433|       |
  434|       |    /* 7.4.7 */
  435|    858|    if (segment->data_length < 18)
  ------------------
  |  Branch (435:9): [True: 8, False: 850]
  ------------------
  436|      8|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  437|       |
  438|    850|    jbig2_get_region_segment_info(&rsi, segment_data);
  439|    850|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "generic region: %u x %u @ (%u, %u), flags = %02x", rsi.width, rsi.height, rsi.x, rsi.y, rsi.flags);
  440|       |
  441|       |    /* 7.4.7.2 */
  442|    850|    seg_flags = segment_data[17];
  443|    850|    params.GRTEMPLATE = seg_flags & 0x01;
  444|    850|    params.TPGRON = seg_flags & 0x02 ? 1 : 0;
  ------------------
  |  Branch (444:21): [True: 505, False: 345]
  ------------------
  445|    850|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  446|    850|                "segment flags = %02x %s%s", seg_flags, params.GRTEMPLATE ? " GRTEMPLATE" : "", params.TPGRON ? " TPGRON" : "");
  ------------------
  |  Branch (446:57): [True: 334, False: 516]
  |  Branch (446:97): [True: 505, False: 345]
  ------------------
  447|    850|    if (seg_flags & 0xFC)
  ------------------
  |  Branch (447:9): [True: 455, False: 395]
  ------------------
  448|    455|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved segment flag bits are non-zero");
  449|    850|    offset += 18;
  450|       |
  451|       |    /* 7.4.7.3 */
  452|    850|    if (!params.GRTEMPLATE) {
  ------------------
  |  Branch (452:9): [True: 516, False: 334]
  ------------------
  453|    516|        if (segment->data_length < 22)
  ------------------
  |  Branch (453:13): [True: 3, False: 513]
  ------------------
  454|      3|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  455|    513|        params.grat[0] = segment_data[offset + 0];
  456|    513|        params.grat[1] = segment_data[offset + 1];
  457|    513|        params.grat[2] = segment_data[offset + 2];
  458|    513|        params.grat[3] = segment_data[offset + 3];
  459|    513|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  460|    513|                    "grat1: (%d, %d) grat2: (%d, %d)", params.grat[0], params.grat[1], params.grat[2], params.grat[3]);
  461|    513|        offset += 4;
  462|    513|    }
  463|       |
  464|       |    /* 7.4.7.4 - set up the reference image */
  465|    847|    if (segment->referred_to_segment_count) {
  ------------------
  |  Branch (465:9): [True: 210, False: 637]
  ------------------
  466|    210|        Jbig2Segment *ref;
  467|       |
  468|    210|        ref = jbig2_region_find_referred(ctx, segment);
  469|    210|        if (ref == NULL)
  ------------------
  |  Branch (469:13): [True: 27, False: 183]
  ------------------
  470|     27|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to find reference bitmap");
  471|    183|        if (ref->result == NULL)
  ------------------
  |  Branch (471:13): [True: 0, False: 183]
  ------------------
  472|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "reference bitmap has no decoded image");
  473|       |        /* the reference bitmap is the result of a previous
  474|       |           intermediate region segment; the reference selection
  475|       |           rules say to use the first one available, and not to
  476|       |           reuse any intermediate result, so we simply take another
  477|       |           reference to it and free the original to keep track of this. */
  478|    183|        params.GRREFERENCE = jbig2_image_reference(ctx, (Jbig2Image *) ref->result);
  479|    183|        jbig2_image_release(ctx, (Jbig2Image *) ref->result);
  480|    183|        ref->result = NULL;
  481|    183|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "found reference bitmap in segment %d", ref->number);
  482|    637|    } else {
  483|       |        /* the reference is just (a subset of) the page buffer */
  484|    637|        if (ctx->pages[ctx->current_page].image == NULL)
  ------------------
  |  Branch (484:13): [True: 4, False: 633]
  ------------------
  485|      4|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "reference page bitmap has no decoded image");
  486|    633|        params.GRREFERENCE = jbig2_image_reference(ctx, ctx->pages[ctx->current_page].image);
  487|       |        /* TODO: subset the image if appropriate */
  488|    633|    }
  489|       |
  490|       |    /* 7.4.7.5 */
  491|    816|    params.GRREFERENCEDX = 0;
  492|    816|    params.GRREFERENCEDY = 0;
  493|    816|    {
  494|    816|        Jbig2WordStream *ws = NULL;
  495|    816|        Jbig2ArithState *as = NULL;
  496|    816|        Jbig2ArithCx *GR_stats = NULL;
  497|    816|        int stats_size;
  498|    816|        Jbig2Image *image = NULL;
  499|       |
  500|    816|        image = jbig2_image_new(ctx, rsi.width, rsi.height);
  501|    816|        if (image == NULL) {
  ------------------
  |  Branch (501:13): [True: 8, False: 808]
  ------------------
  502|      8|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate refinement image");
  503|      8|            goto cleanup;
  504|      8|        }
  505|    808|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, rsi.height);
  506|       |
  507|    808|        stats_size = params.GRTEMPLATE ? 1 << 10 : 1 << 13;
  ------------------
  |  Branch (507:22): [True: 312, False: 496]
  ------------------
  508|    808|        GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|    808|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  509|    808|        if (GR_stats == NULL) {
  ------------------
  |  Branch (509:13): [True: 1, False: 807]
  ------------------
  510|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder state for generic refinement regions");
  511|      1|            goto cleanup;
  512|      1|        }
  513|    807|        memset(GR_stats, 0, stats_size);
  514|       |
  515|    807|        ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
  516|    807|        if (ws == NULL) {
  ------------------
  |  Branch (516:13): [True: 1, False: 806]
  ------------------
  517|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate word stream when handling refinement region");
  518|      1|            goto cleanup;
  519|      1|        }
  520|       |
  521|    806|        as = jbig2_arith_new(ctx, ws);
  522|    806|        if (as == NULL) {
  ------------------
  |  Branch (522:13): [True: 2, False: 804]
  ------------------
  523|      2|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling refinement region");
  524|      2|            goto cleanup;
  525|      2|        }
  526|       |
  527|    804|        code = jbig2_decode_refinement_region(ctx, segment, &params, as, image, GR_stats);
  528|    804|        if (code < 0) {
  ------------------
  |  Branch (528:13): [True: 9, False: 795]
  ------------------
  529|      9|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
  530|      9|            goto cleanup;
  531|      9|        }
  532|       |
  533|    795|        if ((segment->flags & 63) == 40) {
  ------------------
  |  Branch (533:13): [True: 205, False: 590]
  ------------------
  534|       |            /* intermediate region. save the result for later */
  535|    205|            segment->result = jbig2_image_reference(ctx, image);
  536|    590|        } else {
  537|       |            /* immediate region. composite onto the page */
  538|    590|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  539|    590|                        "composing %dx%d decoded refinement region onto page at (%d, %d)", rsi.width, rsi.height, rsi.x, rsi.y);
  540|    590|            code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
  541|    590|            if (code < 0) {
  ------------------
  |  Branch (541:17): [True: 108, False: 482]
  ------------------
  542|    108|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add refinement region to page");
  543|    108|                goto cleanup;
  544|    108|            }
  545|    590|        }
  546|       |
  547|    816|cleanup:
  548|    816|        jbig2_image_release(ctx, image);
  549|    816|        jbig2_image_release(ctx, params.GRREFERENCE);
  550|    816|        jbig2_free(ctx->allocator, as);
  551|    816|        jbig2_word_stream_buf_free(ctx, ws);
  552|    816|        jbig2_free(ctx->allocator, GR_stats);
  553|    816|    }
  554|       |
  555|      0|    return code;
  556|    795|}
jbig2_refinement.c:jbig2_decode_refinement_TPGRON:
  311|    484|{
  312|    484|    const int64_t GRW = image->width;
  313|    484|    const int64_t GRH = image->height;
  314|    484|    int64_t x, y;
  315|    484|    int iv, LTP = 0;
  316|    484|    uint32_t start_context = (params->GRTEMPLATE ? 0x40 : 0x100);
  ------------------
  |  Branch (316:31): [True: 153, False: 331]
  ------------------
  317|    484|    ContextBuilder mkctx = (params->GRTEMPLATE ? mkctx1 : mkctx0);
  ------------------
  |  Branch (317:29): [True: 153, False: 331]
  ------------------
  318|       |
  319|    484|    if (params->GRTEMPLATE == 0 &&
  ------------------
  |  Branch (319:9): [True: 331, False: 153]
  ------------------
  320|    331|        (pixel_outside_field(params->grat[0], params->grat[1]) ||
  ------------------
  |  |   47|    662|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (47:6): [True: 0, False: 331]
  |  |  |  Branch (47:20): [True: 2, False: 329]
  |  |  |  Branch (47:31): [True: 0, False: 329]
  |  |  |  Branch (47:46): [True: 148, False: 181]
  |  |  |  Branch (47:57): [True: 0, False: 148]
  |  |  |  Branch (47:72): [True: 181, False: 148]
  |  |  |  Branch (47:84): [True: 3, False: 178]
  |  |  ------------------
  ------------------
  321|    326|        refpixel_outside_field(params->grat[2], params->grat[3])))
  ------------------
  |  |   49|    326|    ((y) < -128 || (y) > 127 || (x) < -128 || (x) > 127)
  |  |  ------------------
  |  |  |  Branch (49:6): [True: 0, False: 326]
  |  |  |  Branch (49:20): [True: 0, False: 326]
  |  |  |  Branch (49:33): [True: 0, False: 326]
  |  |  |  Branch (49:47): [True: 0, False: 326]
  |  |  ------------------
  ------------------
  322|      5|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER,
  ------------------
  |  |   68|      5|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  323|      5|                           "adaptive template pixel is out of field");
  324|       |
  325|  22.1M|    for (y = 0; y < GRH; y++) {
  ------------------
  |  Branch (325:17): [True: 22.1M, False: 479]
  ------------------
  326|  22.1M|        int bit = jbig2_arith_decode(ctx, as, &GR_stats[start_context]);
  327|  22.1M|        if (bit < 0)
  ------------------
  |  Branch (327:13): [True: 0, False: 22.1M]
  ------------------
  328|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode arithmetic code when handling refinement TPGRON1");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  329|  22.1M|        LTP ^= bit;
  330|  22.1M|        if (!LTP) {
  ------------------
  |  Branch (330:13): [True: 12.9M, False: 9.15M]
  ------------------
  331|   191M|            for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (331:25): [True: 178M, False: 12.9M]
  ------------------
  332|   178M|                bit = jbig2_arith_decode(ctx, as, &GR_stats[mkctx(params, image, x, y)]);
  333|   178M|                if (bit < 0)
  ------------------
  |  Branch (333:21): [True: 0, False: 178M]
  ------------------
  334|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode arithmetic code when handling refinement TPGRON1");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  335|   178M|                jbig2_image_set_pixel(image, x, y, bit);
  336|   178M|            }
  337|  12.9M|        } else {
  338|   103M|            for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (338:25): [True: 94.6M, False: 9.15M]
  ------------------
  339|  94.6M|                iv = implicit_value(params, image, x, y);
  340|  94.6M|                if (iv < 0) {
  ------------------
  |  Branch (340:21): [True: 644k, False: 94.0M]
  ------------------
  341|   644k|                    int bit = jbig2_arith_decode(ctx, as, &GR_stats[mkctx(params, image, x, y)]);
  342|   644k|                    if (bit < 0)
  ------------------
  |  Branch (342:25): [True: 0, False: 644k]
  ------------------
  343|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode arithmetic code when handling refinement TPGRON1");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  344|   644k|                    jbig2_image_set_pixel(image, x, y, bit);
  345|   644k|                } else
  346|  94.0M|                    jbig2_image_set_pixel(image, x, y, iv);
  347|  94.6M|            }
  348|  9.15M|        }
  349|  22.1M|    }
  350|       |
  351|    479|    return 0;
  352|    479|}
jbig2_refinement.c:mkctx1:
  290|  86.8M|{
  291|  86.8M|    Jbig2Image *ref = params->GRREFERENCE;
  292|  86.8M|    const int32_t dx = params->GRREFERENCEDX;
  293|  86.8M|    const int32_t dy = params->GRREFERENCEDY;
  294|  86.8M|    uint32_t CONTEXT;
  295|       |
  296|  86.8M|    CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
  297|  86.8M|    CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  298|  86.8M|    CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  299|  86.8M|    CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
  300|  86.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  301|  86.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  302|  86.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
  303|  86.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
  304|  86.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
  305|  86.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
  306|  86.8M|    return CONTEXT;
  307|  86.8M|}
jbig2_refinement.c:mkctx0:
  266|  92.2M|{
  267|  92.2M|    Jbig2Image *ref = params->GRREFERENCE;
  268|  92.2M|    const int32_t dx = params->GRREFERENCEDX;
  269|  92.2M|    const int32_t dy = params->GRREFERENCEDY;
  270|  92.2M|    uint32_t CONTEXT;
  271|       |
  272|  92.2M|    CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
  273|  92.2M|    CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  274|  92.2M|    CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  275|  92.2M|    CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y + params->grat[1]) << 3;
  276|  92.2M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  277|  92.2M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  278|  92.2M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
  279|  92.2M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
  280|  92.2M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
  281|  92.2M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
  282|  92.2M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
  283|  92.2M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
  284|  92.2M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
  285|  92.2M|    return CONTEXT;
  286|  92.2M|}
jbig2_refinement.c:implicit_value:
  245|  94.6M|{
  246|  94.6M|    Jbig2Image *ref = params->GRREFERENCE;
  247|  94.6M|    const int64_t i = x - params->GRREFERENCEDX;
  248|  94.6M|    const int64_t j = y - params->GRREFERENCEDY;
  249|  94.6M|    int m = jbig2_image_get_pixel(ref, i, j);
  250|       |
  251|  94.6M|    (void) image;
  252|       |
  253|  94.6M|    return ((jbig2_image_get_pixel(ref, i - 1, j - 1) == m) &&
  ------------------
  |  Branch (253:13): [True: 94.2M, False: 448k]
  ------------------
  254|  94.2M|            (jbig2_image_get_pixel(ref, i, j - 1) == m) &&
  ------------------
  |  Branch (254:13): [True: 94.1M, False: 71.8k]
  ------------------
  255|  94.1M|            (jbig2_image_get_pixel(ref, i + 1, j - 1) == m) &&
  ------------------
  |  Branch (255:13): [True: 94.1M, False: 28.4k]
  ------------------
  256|  94.1M|            (jbig2_image_get_pixel(ref, i - 1, j) == m) &&
  ------------------
  |  Branch (256:13): [True: 94.0M, False: 20.5k]
  ------------------
  257|  94.0M|            (jbig2_image_get_pixel(ref, i + 1, j) == m) &&
  ------------------
  |  Branch (257:13): [True: 94.0M, False: 6.52k]
  ------------------
  258|  94.0M|            (jbig2_image_get_pixel(ref, i - 1, j + 1) == m) &&
  ------------------
  |  Branch (258:13): [True: 94.0M, False: 64.4k]
  ------------------
  259|  94.0M|            (jbig2_image_get_pixel(ref, i, j + 1) == m) &&
  ------------------
  |  Branch (259:13): [True: 94.0M, False: 1.97k]
  ------------------
  260|  94.0M|            (jbig2_image_get_pixel(ref, i + 1, j + 1) == m)
  ------------------
  |  Branch (260:13): [True: 94.0M, False: 1.75k]
  ------------------
  261|  94.6M|           )? m : -1;
  262|  94.6M|}
jbig2_refinement.c:jbig2_decode_refinement_template1_unopt:
  117|  6.50k|{
  118|  6.50k|    const int64_t GRW = image->width;
  119|  6.50k|    const int64_t GRH = image->height;
  120|  6.50k|    Jbig2Image *ref = params->GRREFERENCE;
  121|  6.50k|    const int32_t dx = params->GRREFERENCEDX;
  122|  6.50k|    const int32_t dy = params->GRREFERENCEDY;
  123|  6.50k|    uint32_t CONTEXT;
  124|  6.50k|    int64_t x, y;
  125|  6.50k|    int bit;
  126|       |
  127|  16.8M|    for (y = 0; y < GRH; y++) {
  ------------------
  |  Branch (127:17): [True: 16.8M, False: 6.50k]
  ------------------
  128|   247M|        for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (128:21): [True: 231M, False: 16.8M]
  ------------------
  129|   231M|            CONTEXT = 0;
  130|   231M|            CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
  131|   231M|            CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  132|   231M|            CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  133|   231M|            CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
  134|   231M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  135|   231M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  136|   231M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
  137|   231M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
  138|   231M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
  139|   231M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
  140|   231M|            bit = jbig2_arith_decode(ctx, as, &GR_stats[CONTEXT]);
  141|   231M|            if (bit < 0)
  ------------------
  |  Branch (141:17): [True: 0, False: 231M]
  ------------------
  142|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling refinement template0");
  143|   231M|            jbig2_image_set_pixel(image, x, y, bit);
  144|   231M|        }
  145|  16.8M|    }
  146|       |
  147|       |#ifdef JBIG2_DEBUG_DUMP
  148|       |    {
  149|       |        static unsigned int count = 0;
  150|       |        char name[32];
  151|       |        int code;
  152|       |
  153|       |        snprintf(name, 32, "refin-%d.pbm", count);
  154|       |        code = jbig2_image_write_pbm_file(ref, name);
  155|       |        if (code < 0)
  156|       |            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write refinement input");
  157|       |        snprintf(name, 32, "refout-%d.pbm", count);
  158|       |        code = jbig2_image_write_pbm_file(image, name);
  159|       |        if (code < 0)
  160|       |            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write refinement output");
  161|       |        count++;
  162|       |    }
  163|       |#endif
  164|       |
  165|  6.50k|    return 0;
  166|  6.50k|}
jbig2_refinement.c:jbig2_decode_refinement_template0_unopt:
   55|  90.1k|{
   56|  90.1k|    const int64_t GRW = image->width;
   57|  90.1k|    const int64_t GRH = image->height;
   58|  90.1k|    Jbig2Image *ref = params->GRREFERENCE;
   59|  90.1k|    const int32_t dx = params->GRREFERENCEDX;
   60|  90.1k|    const int32_t dy = params->GRREFERENCEDY;
   61|  90.1k|    uint32_t CONTEXT;
   62|  90.1k|    int64_t x, y;
   63|  90.1k|    int bit;
   64|       |
   65|  90.1k|    if (pixel_outside_field(params->grat[0], params->grat[1]) ||
  ------------------
  |  |   47|   180k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (47:6): [True: 0, False: 90.1k]
  |  |  |  Branch (47:20): [True: 4, False: 90.1k]
  |  |  |  Branch (47:31): [True: 0, False: 90.1k]
  |  |  |  Branch (47:46): [True: 23.3k, False: 66.8k]
  |  |  |  Branch (47:57): [True: 0, False: 23.3k]
  |  |  |  Branch (47:72): [True: 66.8k, False: 23.3k]
  |  |  |  Branch (47:84): [True: 8, False: 66.8k]
  |  |  ------------------
  ------------------
   66|  90.1k|        refpixel_outside_field(params->grat[2], params->grat[3]))
  ------------------
  |  |   49|  90.1k|    ((y) < -128 || (y) > 127 || (x) < -128 || (x) > 127)
  |  |  ------------------
  |  |  |  Branch (49:6): [True: 0, False: 90.1k]
  |  |  |  Branch (49:20): [True: 0, False: 90.1k]
  |  |  |  Branch (49:33): [True: 0, False: 90.1k]
  |  |  |  Branch (49:47): [True: 0, False: 90.1k]
  |  |  ------------------
  ------------------
   67|     12|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
   68|     12|                           "adaptive template pixel is out of field");
   69|       |
   70|  1.77M|    for (y = 0; y < GRH; y++) {
  ------------------
  |  Branch (70:17): [True: 1.68M, False: 90.1k]
  ------------------
   71|   109M|        for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (71:21): [True: 107M, False: 1.68M]
  ------------------
   72|   107M|            CONTEXT = 0;
   73|   107M|            CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
   74|   107M|            CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
   75|   107M|            CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
   76|   107M|            CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y + params->grat[1]) << 3;
   77|   107M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
   78|   107M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
   79|   107M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
   80|   107M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
   81|   107M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
   82|   107M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
   83|   107M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
   84|   107M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
   85|   107M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
   86|   107M|            bit = jbig2_arith_decode(ctx, as, &GR_stats[CONTEXT]);
   87|   107M|            if (bit < 0)
  ------------------
  |  Branch (87:17): [True: 0, False: 107M]
  ------------------
   88|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling refinement template0");
   89|   107M|            jbig2_image_set_pixel(image, x, y, bit);
   90|   107M|        }
   91|  1.68M|    }
   92|       |#ifdef JBIG2_DEBUG_DUMP
   93|       |    {
   94|       |        static unsigned int count = 0;
   95|       |        char name[32];
   96|       |        int code;
   97|       |
   98|       |        snprintf(name, 32, "refin-%d.pbm", count);
   99|       |        code = jbig2_image_write_pbm_file(ref, name);
  100|       |        if (code < 0)
  101|       |            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed write refinement input");
  102|       |        snprintf(name, 32, "refout-%d.pbm", count);
  103|       |        code = jbig2_image_write_pbm_file(image, name);
  104|       |        if (code < 0)
  105|       |            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed write refinement output");
  106|       |        count++;
  107|       |    }
  108|       |#endif
  109|       |
  110|  90.1k|    return 0;
  111|  90.1k|}
jbig2_refinement.c:jbig2_region_find_referred:
  395|    210|{
  396|    210|    const int nsegments = segment->referred_to_segment_count;
  397|    210|    Jbig2Segment *rsegment;
  398|    210|    int index;
  399|       |
  400|  4.15k|    for (index = 0; index < nsegments; index++) {
  ------------------
  |  Branch (400:21): [True: 4.12k, False: 27]
  ------------------
  401|  4.12k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  402|  4.12k|        if (rsegment == NULL) {
  ------------------
  |  Branch (402:13): [True: 2.74k, False: 1.37k]
  ------------------
  403|  2.74k|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to find referred to segment %d", segment->referred_to_segments[index]);
  404|  2.74k|            continue;
  405|  2.74k|        }
  406|  1.37k|        switch (rsegment->flags & 63) {
  407|    748|        case 4:                /* intermediate text region */
  ------------------
  |  Branch (407:9): [True: 748, False: 628]
  ------------------
  408|    753|        case 20:               /* intermediate halftone region */
  ------------------
  |  Branch (408:9): [True: 5, False: 1.37k]
  ------------------
  409|    753|        case 36:               /* intermediate generic region */
  ------------------
  |  Branch (409:9): [True: 0, False: 1.37k]
  ------------------
  410|    957|        case 40:               /* intermediate generic refinement region */
  ------------------
  |  Branch (410:9): [True: 204, False: 1.17k]
  ------------------
  411|    957|            if (rsegment->result)
  ------------------
  |  Branch (411:17): [True: 183, False: 774]
  ------------------
  412|    183|                return rsegment;
  413|    774|            break;
  414|    774|        default:               /* keep looking */
  ------------------
  |  Branch (414:9): [True: 419, False: 957]
  ------------------
  415|    419|            break;
  416|  1.37k|        }
  417|  1.37k|    }
  418|       |    /* no appropriate reference was found. */
  419|     27|    return NULL;
  420|    210|}

jbig2_parse_segment_header:
   44|  80.9k|{
   45|  80.9k|    Jbig2Segment *result;
   46|  80.9k|    uint8_t rtscarf;
   47|  80.9k|    uint32_t rtscarf_long;
   48|  80.9k|    uint32_t *referred_to_segments;
   49|  80.9k|    uint32_t referred_to_segment_count;
   50|  80.9k|    uint32_t referred_to_segment_size;
   51|  80.9k|    uint32_t pa_size;
   52|  80.9k|    uint32_t offset;
   53|       |
   54|       |    /* minimum possible size of a jbig2 segment header */
   55|  80.9k|    if (buf_size < 11)
  ------------------
  |  Branch (55:9): [True: 547, False: 80.4k]
  ------------------
   56|    547|        return NULL;
   57|       |
   58|  80.4k|    result = jbig2_new(ctx, Jbig2Segment, 1);
  ------------------
  |  |  141|  80.4k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   59|  80.4k|    if (result == NULL) {
  ------------------
  |  Branch (59:9): [True: 1, False: 80.4k]
  ------------------
   60|      1|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate segment");
  ------------------
  |  |   68|      1|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   61|      1|        return NULL;
   62|      1|    }
   63|       |
   64|       |    /* 7.2.2 */
   65|  80.4k|    result->number = jbig2_get_uint32(buf);
   66|  80.4k|    if (result->number == JBIG2_UNKNOWN_SEGMENT_NUMBER) {
  ------------------
  |  |   68|  80.4k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  |  Branch (66:9): [True: 6, False: 80.3k]
  ------------------
   67|      6|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "segment number too large");
  ------------------
  |  |   68|      6|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   68|      6|        jbig2_free(ctx->allocator, result);
   69|      6|        return NULL;
   70|      6|    }
   71|       |
   72|       |    /* 7.2.3 */
   73|  80.3k|    result->flags = buf[4];
   74|       |
   75|       |    /* 7.2.4 referred-to segments */
   76|  80.3k|    rtscarf = buf[5];
   77|  80.3k|    if ((rtscarf & 0xe0) == 0xe0) {
  ------------------
  |  Branch (77:9): [True: 955, False: 79.4k]
  ------------------
   78|    955|        rtscarf_long = jbig2_get_uint32(buf + 5);
   79|    955|        referred_to_segment_count = rtscarf_long & 0x1fffffff;
   80|    955|        offset = 5 + 4 + (referred_to_segment_count + 1) / 8;
   81|  79.4k|    } else {
   82|  79.4k|        referred_to_segment_count = (rtscarf >> 5);
   83|  79.4k|        offset = 5 + 1;
   84|  79.4k|    }
   85|  80.3k|    result->referred_to_segment_count = referred_to_segment_count;
   86|       |
   87|       |    /* we now have enough information to compute the full header length */
   88|  80.3k|    referred_to_segment_size = result->number <= 256 ? 1 : result->number <= 65536 ? 2 : 4;     /* 7.2.5 */
  ------------------
  |  Branch (88:32): [True: 26.9k, False: 53.4k]
  |  Branch (88:60): [True: 4.97k, False: 48.5k]
  ------------------
   89|  80.3k|    pa_size = result->flags & 0x40 ? 4 : 1;     /* 7.2.6 */
  ------------------
  |  Branch (89:15): [True: 6.21k, False: 74.1k]
  ------------------
   90|  80.3k|    if (offset + referred_to_segment_count * referred_to_segment_size + pa_size + 4 > buf_size) {
  ------------------
  |  Branch (90:9): [True: 717, False: 79.6k]
  ------------------
   91|    717|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "attempted to parse segment header with insufficient data, asking for more data");
   92|    717|        jbig2_free(ctx->allocator, result);
   93|    717|        return NULL;
   94|    717|    }
   95|       |
   96|       |    /* 7.2.5 */
   97|  79.6k|    if (referred_to_segment_count) {
  ------------------
  |  Branch (97:9): [True: 18.3k, False: 61.3k]
  ------------------
   98|  18.3k|        uint32_t i;
   99|       |
  100|  18.3k|        referred_to_segments = jbig2_new(ctx, uint32_t, referred_to_segment_count * referred_to_segment_size);
  ------------------
  |  |  141|  18.3k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  101|  18.3k|        if (referred_to_segments == NULL) {
  ------------------
  |  Branch (101:13): [True: 1, False: 18.3k]
  ------------------
  102|      1|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, result->number, "failed to allocate referred to segments");
  103|      1|            jbig2_free(ctx->allocator, result);
  104|      1|            return NULL;
  105|      1|        }
  106|       |
  107|   160k|        for (i = 0; i < referred_to_segment_count; i++) {
  ------------------
  |  Branch (107:21): [True: 141k, False: 18.3k]
  ------------------
  108|   141k|            referred_to_segments[i] =
  109|   141k|                (referred_to_segment_size == 1) ? buf[offset] :
  ------------------
  |  Branch (109:17): [True: 99.8k, False: 41.9k]
  ------------------
  110|   141k|                (referred_to_segment_size == 2) ? jbig2_get_uint16(buf + offset) : jbig2_get_uint32(buf + offset);
  ------------------
  |  Branch (110:17): [True: 6.50k, False: 35.4k]
  ------------------
  111|   141k|            offset += referred_to_segment_size;
  112|   141k|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "segment %d refers to segment %d", result->number, referred_to_segments[i]);
  113|   141k|        }
  114|  18.3k|        result->referred_to_segments = referred_to_segments;
  115|  61.3k|    } else {                    /* no referred-to segments */
  116|       |
  117|  61.3k|        result->referred_to_segments = NULL;
  118|  61.3k|    }
  119|       |
  120|       |    /* 7.2.6 */
  121|  79.6k|    if (pa_size == 4) {
  ------------------
  |  Branch (121:9): [True: 5.84k, False: 73.8k]
  ------------------
  122|  5.84k|        result->page_association = jbig2_get_uint32(buf + offset);
  123|  5.84k|        offset += 4;
  124|  73.8k|    } else {
  125|  73.8k|        result->page_association = buf[offset++];
  126|  73.8k|    }
  127|  79.6k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "segment %d is associated with page %d", result->number, result->page_association);
  128|       |
  129|       |    /* 7.2.7 */
  130|  79.6k|    result->rows = UINT32_MAX;
  131|  79.6k|    result->data_length = jbig2_get_uint32(buf + offset);
  132|  79.6k|    *p_header_size = offset + 4;
  133|       |
  134|       |    /* no body parsing results yet */
  135|  79.6k|    result->result = NULL;
  136|       |
  137|  79.6k|    return result;
  138|  79.6k|}
jbig2_free_segment:
  142|  79.6k|{
  143|  79.6k|    if (segment == NULL)
  ------------------
  |  Branch (143:9): [True: 0, False: 79.6k]
  ------------------
  144|      0|        return;
  145|       |
  146|  79.6k|    jbig2_free(ctx->allocator, segment->referred_to_segments);
  147|       |    /* todo: we need either some separate fields or
  148|       |       a more complex result object rather than this
  149|       |       brittle special casing */
  150|  79.6k|    switch (segment->flags & 63) {
  151|  16.4k|    case 0:                    /* symbol dictionary */
  ------------------
  |  Branch (151:5): [True: 16.4k, False: 63.2k]
  ------------------
  152|  16.4k|        if (segment->result != NULL)
  ------------------
  |  Branch (152:13): [True: 4.57k, False: 11.8k]
  ------------------
  153|  4.57k|            jbig2_sd_release(ctx, (Jbig2SymbolDict *) segment->result);
  154|  16.4k|        break;
  155|  4.16k|    case 4:                    /* intermediate text region */
  ------------------
  |  Branch (155:5): [True: 4.16k, False: 75.5k]
  ------------------
  156|  4.59k|    case 40:                   /* intermediate refinement region */
  ------------------
  |  Branch (156:5): [True: 426, False: 79.2k]
  ------------------
  157|  4.59k|        if (segment->result != NULL)
  ------------------
  |  Branch (157:13): [True: 168, False: 4.42k]
  ------------------
  158|    168|            jbig2_image_release(ctx, (Jbig2Image *) segment->result);
  159|  4.59k|        break;
  160|  4.53k|    case 16:                   /* pattern dictionary */
  ------------------
  |  Branch (160:5): [True: 4.53k, False: 75.1k]
  ------------------
  161|  4.53k|        if (segment->result != NULL)
  ------------------
  |  Branch (161:13): [True: 2.96k, False: 1.56k]
  ------------------
  162|  2.96k|            jbig2_hd_release(ctx, (Jbig2PatternDict *) segment->result);
  163|  4.53k|        break;
  164|  2.07k|    case 53:                   /* user-supplied huffman table */
  ------------------
  |  Branch (164:5): [True: 2.07k, False: 77.6k]
  ------------------
  165|  2.07k|        if (segment->result != NULL)
  ------------------
  |  Branch (165:13): [True: 1.72k, False: 356]
  ------------------
  166|  1.72k|            jbig2_table_free(ctx, (Jbig2HuffmanParams *) segment->result);
  167|  2.07k|        break;
  168|  52.0k|    default:
  ------------------
  |  Branch (168:5): [True: 52.0k, False: 27.6k]
  ------------------
  169|       |        /* anything else is probably an undefined pointer */
  170|  52.0k|        break;
  171|  79.6k|    }
  172|  79.6k|    jbig2_free(ctx->allocator, segment);
  173|  79.6k|}
jbig2_find_segment:
  178|   263k|{
  179|   263k|    int index, index_max = ctx->segment_index - 1;
  180|   263k|    const Jbig2Ctx *global_ctx = ctx->global_ctx;
  181|       |
  182|       |    /* FIXME: binary search would be better */
  183|  1.99M|    for (index = index_max; index >= 0; index--)
  ------------------
  |  Branch (183:29): [True: 1.84M, False: 146k]
  ------------------
  184|  1.84M|        if (ctx->segments[index]->number == number)
  ------------------
  |  Branch (184:13): [True: 116k, False: 1.73M]
  ------------------
  185|   116k|            return (ctx->segments[index]);
  186|       |
  187|   146k|    if (global_ctx)
  ------------------
  |  Branch (187:9): [True: 0, False: 146k]
  ------------------
  188|      0|        for (index = global_ctx->segment_index - 1; index >= 0; index--)
  ------------------
  |  Branch (188:53): [True: 0, False: 0]
  ------------------
  189|      0|            if (global_ctx->segments[index]->number == number)
  ------------------
  |  Branch (189:17): [True: 0, False: 0]
  ------------------
  190|      0|                return (global_ctx->segments[index]);
  191|       |
  192|       |    /* didn't find a match */
  193|   146k|    return NULL;
  194|   146k|}
jbig2_get_region_segment_info:
  199|  27.8k|{
  200|       |    /* 7.4.1 */
  201|  27.8k|    info->width = jbig2_get_uint32(segment_data);
  202|  27.8k|    info->height = jbig2_get_uint32(segment_data + 4);
  203|  27.8k|    info->x = jbig2_get_uint32(segment_data + 8);
  204|  27.8k|    info->y = jbig2_get_uint32(segment_data + 12);
  205|  27.8k|    info->flags = segment_data[16];
  206|  27.8k|    info->op = (Jbig2ComposeOp)(info->flags & 0x7);
  207|  27.8k|}
jbig2_parse_segment:
  337|  53.8k|{
  338|  53.8k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  339|  53.8k|                "segment %d, flags=%x, type=%d, data_length=%ld", segment->number, segment->flags, segment->flags & 63, (long) segment->data_length);
  340|  53.8k|    switch (segment->flags & 63) {
  341|  6.11k|    case 0:
  ------------------
  |  Branch (341:5): [True: 6.11k, False: 47.7k]
  ------------------
  342|  6.11k|        return jbig2_symbol_dictionary(ctx, segment, segment_data);
  343|    314|    case 4:                    /* intermediate text region */
  ------------------
  |  Branch (343:5): [True: 314, False: 53.5k]
  ------------------
  344|    522|    case 6:                    /* immediate text region */
  ------------------
  |  Branch (344:5): [True: 208, False: 53.6k]
  ------------------
  345|  3.69k|    case 7:                    /* immediate lossless text region */
  ------------------
  |  Branch (345:5): [True: 3.17k, False: 50.7k]
  ------------------
  346|  3.69k|        return jbig2_text_region(ctx, segment, segment_data);
  347|  4.04k|    case 16:
  ------------------
  |  Branch (347:5): [True: 4.04k, False: 49.8k]
  ------------------
  348|  4.04k|        return jbig2_pattern_dictionary(ctx, segment, segment_data);
  349|     41|    case 20:                   /* intermediate halftone region */
  ------------------
  |  Branch (349:5): [True: 41, False: 53.8k]
  ------------------
  350|     86|    case 22:                   /* immediate halftone region */
  ------------------
  |  Branch (350:5): [True: 45, False: 53.8k]
  ------------------
  351|  2.21k|    case 23:                   /* immediate lossless halftone region */
  ------------------
  |  Branch (351:5): [True: 2.12k, False: 51.7k]
  ------------------
  352|  2.21k|        return jbig2_halftone_region(ctx, segment, segment_data);
  353|      3|    case 36:
  ------------------
  |  Branch (353:5): [True: 3, False: 53.8k]
  ------------------
  354|      3|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unhandled segment type 'intermediate generic region' (NYI)");
  355|  15.3k|    case 38:                   /* immediate generic region */
  ------------------
  |  Branch (355:5): [True: 15.3k, False: 38.5k]
  ------------------
  356|  21.1k|    case 39:                   /* immediate lossless generic region */
  ------------------
  |  Branch (356:5): [True: 5.83k, False: 48.0k]
  ------------------
  357|  21.1k|        return jbig2_immediate_generic_region(ctx, segment, segment_data);
  358|    224|    case 40:                   /* intermediate generic refinement region */
  ------------------
  |  Branch (358:5): [True: 224, False: 53.6k]
  ------------------
  359|    762|    case 42:                   /* immediate generic refinement region */
  ------------------
  |  Branch (359:5): [True: 538, False: 53.3k]
  ------------------
  360|    858|    case 43:                   /* immediate lossless generic refinement region */
  ------------------
  |  Branch (360:5): [True: 96, False: 53.7k]
  ------------------
  361|    858|        return jbig2_refinement_region(ctx, segment, segment_data);
  362|  8.86k|    case 48:
  ------------------
  |  Branch (362:5): [True: 8.86k, False: 45.0k]
  ------------------
  363|  8.86k|        return jbig2_page_info(ctx, segment, segment_data);
  364|  1.39k|    case 49:
  ------------------
  |  Branch (364:5): [True: 1.39k, False: 52.4k]
  ------------------
  365|  1.39k|        return jbig2_end_of_page(ctx, segment, segment_data);
  366|    686|    case 50:
  ------------------
  |  Branch (366:5): [True: 686, False: 53.2k]
  ------------------
  367|    686|        return jbig2_end_of_stripe(ctx, segment, segment_data);
  368|    512|    case 51:
  ------------------
  |  Branch (368:5): [True: 512, False: 53.3k]
  ------------------
  369|    512|        ctx->state = JBIG2_FILE_EOF;
  370|    512|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of file");
  371|    512|        break;
  372|    139|    case 52:
  ------------------
  |  Branch (372:5): [True: 139, False: 53.7k]
  ------------------
  373|    139|        return jbig2_parse_profile_segment(ctx, segment, segment_data);
  374|  1.85k|    case 53:                   /* user-supplied huffman table */
  ------------------
  |  Branch (374:5): [True: 1.85k, False: 52.0k]
  ------------------
  375|  1.85k|        return jbig2_table(ctx, segment, segment_data);
  376|      4|    case 54:
  ------------------
  |  Branch (376:5): [True: 4, False: 53.8k]
  ------------------
  377|      4|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled segment type 'color palette' (NYI)");
  378|    587|    case 62:
  ------------------
  |  Branch (378:5): [True: 587, False: 53.3k]
  ------------------
  379|    587|        return jbig2_parse_extension_segment(ctx, segment, segment_data);
  380|  1.77k|    default:
  ------------------
  |  Branch (380:5): [True: 1.77k, False: 52.1k]
  ------------------
  381|  1.77k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unknown segment type %d", segment->flags & 63);
  382|  53.8k|    }
  383|  2.28k|    return 0;
  384|  53.8k|}
jbig2_segment.c:jbig2_parse_profile_segment:
  253|    139|{
  254|    139|    uint32_t profiles;
  255|    139|    uint32_t i;
  256|    139|    uint32_t profile;
  257|    139|    int index;
  258|    139|    const char *requirements;
  259|    139|    const char *generic_region;
  260|    139|    const char *refinement_region;
  261|    139|    const char *halftone_region;
  262|    139|    const char *numerical_data;
  263|       |
  264|    139|    if (segment->data_length < 4)
  ------------------
  |  Branch (264:9): [True: 5, False: 134]
  ------------------
  265|      5|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
  266|    134|    index = 0;
  267|       |
  268|    134|    profiles = jbig2_get_uint32(&segment_data[index]);
  269|    134|    index += 4;
  270|       |
  271|  18.2k|    for (i = 0; i < profiles; i++) {
  ------------------
  |  Branch (271:17): [True: 18.2k, False: 57]
  ------------------
  272|  18.2k|        if (segment->data_length - index < 4)
  ------------------
  |  Branch (272:13): [True: 77, False: 18.1k]
  ------------------
  273|     77|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short to store profile");
  274|       |
  275|  18.1k|        profile = jbig2_get_uint32(&segment_data[index]);
  276|  18.1k|        index += 4;
  277|       |
  278|  18.1k|        switch (profile) {
  279|    289|        case 0x00000001:
  ------------------
  |  Branch (279:9): [True: 289, False: 17.8k]
  ------------------
  280|    289|            requirements = "All JBIG2 capabilities";
  281|    289|            generic_region = "No restriction";
  282|    289|            refinement_region = "No restriction";
  283|    289|            halftone_region = "No restriction";
  284|    289|            numerical_data = "No restriction";
  285|    289|            break;
  286|    120|        case 0x00000002:
  ------------------
  |  Branch (286:9): [True: 120, False: 18.0k]
  ------------------
  287|    120|            requirements = "Maximum compression";
  288|    120|            generic_region = "Arithmetic only; any template used";
  289|    120|            refinement_region = "No restriction";
  290|    120|            halftone_region = "No restriction";
  291|    120|            numerical_data = "Arithmetic only";
  292|    120|            break;
  293|    238|        case 0x00000003:
  ------------------
  |  Branch (293:9): [True: 238, False: 17.8k]
  ------------------
  294|    238|            requirements = "Medium complexity and medium compression";
  295|    238|            generic_region = "Arithmetic only; only 10-pixel and 13-pixel templates";
  296|    238|            refinement_region = "10-pixel template only";
  297|    238|            halftone_region = "No skip mask used";
  298|    238|            numerical_data = "Arithmetic only";
  299|    238|            break;
  300|    164|        case 0x00000004:
  ------------------
  |  Branch (300:9): [True: 164, False: 17.9k]
  ------------------
  301|    164|            requirements = "Low complexity with progressive lossless capability";
  302|    164|            generic_region = "MMR only";
  303|    164|            refinement_region = "10-pixel template only";
  304|    164|            halftone_region = "No skip mask used";
  305|    164|            numerical_data = "Huffman only";
  306|    164|            break;
  307|     61|        case 0x00000005:
  ------------------
  |  Branch (307:9): [True: 61, False: 18.0k]
  ------------------
  308|     61|            requirements = "Low complexity";
  309|     61|            generic_region = "MMR only";
  310|     61|            refinement_region = "Not available";
  311|     61|            halftone_region = "No skip mask used";
  312|     61|            numerical_data = "Huffman only";
  313|     61|            break;
  314|  17.2k|        default:
  ------------------
  |  Branch (314:9): [True: 17.2k, False: 872]
  ------------------
  315|  17.2k|            requirements = "Unknown";
  316|  17.2k|            generic_region = "Unknown";
  317|  17.2k|            refinement_region = "Unknown";
  318|  17.2k|            halftone_region = "Unknown";
  319|  17.2k|            numerical_data = "Unknown";
  320|  17.2k|            break;
  321|  18.1k|        }
  322|       |
  323|  18.1k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "Supported profile: 0x%08x", profile);
  324|  18.1k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Requirements: %s", requirements);
  325|  18.1k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Generic region coding: %s", generic_region);
  326|  18.1k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Refinement region coding: %s", refinement_region);
  327|  18.1k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Halftone region coding: %s", halftone_region);
  328|  18.1k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Numerical data: %s", numerical_data);
  329|  18.1k|    }
  330|       |
  331|     57|    return 0;
  332|    134|}
jbig2_segment.c:jbig2_parse_extension_segment:
  212|    587|{
  213|    587|    uint32_t type;
  214|    587|    bool reserved;
  ------------------
  |  |   45|    587|#define bool int
  ------------------
  215|    587|    bool necessary;
  ------------------
  |  |   45|    587|#define bool int
  ------------------
  216|       |
  217|    587|    if (segment->data_length < 4)
  ------------------
  |  Branch (217:9): [True: 8, False: 579]
  ------------------
  218|      8|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  219|       |
  220|    579|    type = jbig2_get_uint32(segment_data);
  221|    579|    reserved = type & 0x20000000;
  222|       |    /* Not implemented since this bit
  223|       |    is only needed by encoders.
  224|       |    dependent = type & 0x40000000;
  225|       |    */
  226|    579|    necessary = type & 0x80000000;
  227|       |
  228|    579|    if (necessary && !reserved) {
  ------------------
  |  Branch (228:9): [True: 40, False: 539]
  |  Branch (228:22): [True: 7, False: 33]
  ------------------
  229|      7|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extension segment is marked 'necessary' but not 'reserved' contrary to spec");
  230|      7|    }
  231|       |
  232|    579|    switch (type) {
  233|    232|    case 0x20000000:
  ------------------
  |  Branch (233:5): [True: 232, False: 347]
  ------------------
  234|    232|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "ignoring ASCII comment");
  235|    232|        break;
  236|     67|    case 0x20000002:
  ------------------
  |  Branch (236:5): [True: 67, False: 512]
  ------------------
  237|     67|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "ignoring UCS-2 comment");
  238|     67|        break;
  239|    280|    default:
  ------------------
  |  Branch (239:5): [True: 280, False: 299]
  ------------------
  240|    280|        if (necessary) {
  ------------------
  |  Branch (240:13): [True: 40, False: 240]
  ------------------
  241|     40|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unhandled necessary extension segment type 0x%08x", type);
  242|    240|        } else {
  243|    240|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled non-necessary extension segment, skipping");
  244|    240|        }
  245|    579|    }
  246|       |
  247|    539|    return 0;
  248|    579|}

jbig2_sd_new:
  100|  17.5k|{
  101|  17.5k|    Jbig2SymbolDict *new_dict = NULL;
  102|       |
  103|  17.5k|    new_dict = jbig2_new(ctx, Jbig2SymbolDict, 1);
  ------------------
  |  |  141|  17.5k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  104|  17.5k|    if (new_dict != NULL) {
  ------------------
  |  Branch (104:9): [True: 17.5k, False: 1]
  ------------------
  105|  17.5k|        new_dict->glyphs = jbig2_new(ctx, Jbig2Image *, n_symbols);
  ------------------
  |  |  141|  17.5k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  106|  17.5k|        new_dict->n_symbols = n_symbols;
  107|  17.5k|    } else {
  108|      1|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate new empty symbol dictionary");
  ------------------
  |  |   68|      1|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  109|      1|        return NULL;
  110|      1|    }
  111|       |
  112|  17.5k|    if (new_dict->glyphs != NULL) {
  ------------------
  |  Branch (112:9): [True: 11.6k, False: 5.92k]
  ------------------
  113|  11.6k|        memset(new_dict->glyphs, 0, n_symbols * sizeof(Jbig2Image *));
  114|  11.6k|    } else if (new_dict->n_symbols > 0) {
  ------------------
  |  Branch (114:16): [True: 167, False: 5.75k]
  ------------------
  115|    167|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate glyphs for new empty symbol dictionary");
  ------------------
  |  |   68|    167|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  116|    167|        jbig2_free(ctx->allocator, new_dict);
  117|    167|        return NULL;
  118|    167|    }
  119|       |
  120|  17.3k|    return new_dict;
  121|  17.5k|}
jbig2_sd_release:
  126|  22.5k|{
  127|  22.5k|    uint32_t i;
  128|       |
  129|  22.5k|    if (dict == NULL)
  ------------------
  |  Branch (129:9): [True: 5.17k, False: 17.3k]
  ------------------
  130|  5.17k|        return;
  131|  17.3k|    if (dict->glyphs != NULL)
  ------------------
  |  Branch (131:9): [True: 11.6k, False: 5.75k]
  ------------------
  132|   490M|        for (i = 0; i < dict->n_symbols; i++)
  ------------------
  |  Branch (132:21): [True: 490M, False: 11.6k]
  ------------------
  133|   490M|            jbig2_image_release(ctx, dict->glyphs[i]);
  134|  17.3k|    jbig2_free(ctx->allocator, dict->glyphs);
  135|  17.3k|    jbig2_free(ctx->allocator, dict);
  136|  17.3k|}
jbig2_sd_count_referred:
  150|  13.5k|{
  151|  13.5k|    int index;
  152|  13.5k|    Jbig2Segment *rsegment;
  153|  13.5k|    uint32_t n_dicts = 0;
  154|       |
  155|   193k|    for (index = 0; index < segment->referred_to_segment_count; index++) {
  ------------------
  |  Branch (155:21): [True: 180k, False: 13.5k]
  ------------------
  156|   180k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  157|   180k|        if (rsegment && ((rsegment->flags & 63) == 0) &&
  ------------------
  |  Branch (157:13): [True: 77.5k, False: 102k]
  |  Branch (157:25): [True: 63.2k, False: 14.2k]
  ------------------
  158|  63.2k|            rsegment->result && (((Jbig2SymbolDict *) rsegment->result)->n_symbols > 0) && ((*((Jbig2SymbolDict *) rsegment->result)->glyphs) != NULL))
  ------------------
  |  Branch (158:13): [True: 63.2k, False: 0]
  |  Branch (158:33): [True: 58.1k, False: 5.16k]
  |  Branch (158:92): [True: 55.5k, False: 2.56k]
  ------------------
  159|  55.5k|            n_dicts++;
  160|   180k|    }
  161|       |
  162|  13.5k|    return (n_dicts);
  163|  13.5k|}
jbig2_sd_list_referred:
  168|  3.90k|{
  169|  3.90k|    int index;
  170|  3.90k|    Jbig2Segment *rsegment;
  171|  3.90k|    Jbig2SymbolDict **dicts;
  172|  3.90k|    uint32_t n_dicts = jbig2_sd_count_referred(ctx, segment);
  173|  3.90k|    uint32_t dindex = 0;
  174|       |
  175|  3.90k|    dicts = jbig2_new(ctx, Jbig2SymbolDict *, n_dicts);
  ------------------
  |  |  141|  3.90k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  176|  3.90k|    if (dicts == NULL) {
  ------------------
  |  Branch (176:9): [True: 2, False: 3.90k]
  ------------------
  177|      2|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate referred list of symbol dictionaries");
  178|      2|        return NULL;
  179|      2|    }
  180|       |
  181|  78.4k|    for (index = 0; index < segment->referred_to_segment_count; index++) {
  ------------------
  |  Branch (181:21): [True: 74.5k, False: 3.90k]
  ------------------
  182|  74.5k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  183|  74.5k|        if (rsegment && ((rsegment->flags & 63) == 0) && rsegment->result &&
  ------------------
  |  Branch (183:13): [True: 33.9k, False: 40.5k]
  |  Branch (183:25): [True: 27.4k, False: 6.56k]
  |  Branch (183:58): [True: 27.4k, False: 0]
  ------------------
  184|  27.4k|                (((Jbig2SymbolDict *) rsegment->result)->n_symbols > 0) && ((*((Jbig2SymbolDict *) rsegment->result)->glyphs) != NULL)) {
  ------------------
  |  Branch (184:17): [True: 25.3k, False: 2.04k]
  |  Branch (184:76): [True: 24.4k, False: 935]
  ------------------
  185|       |            /* add this referred to symbol dictionary */
  186|  24.4k|            dicts[dindex++] = (Jbig2SymbolDict *) rsegment->result;
  187|  24.4k|        }
  188|  74.5k|    }
  189|       |
  190|  3.90k|    if (dindex != n_dicts) {
  ------------------
  |  Branch (190:9): [True: 0, False: 3.90k]
  ------------------
  191|       |        /* should never happen */
  192|      0|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "counted %d symbol dictionaries but built a list with %d.", n_dicts, dindex);
  193|      0|        jbig2_free(ctx->allocator, dicts);
  194|      0|        return NULL;
  195|      0|    }
  196|       |
  197|  3.90k|    return (dicts);
  198|  3.90k|}
jbig2_sd_cat:
  204|  1.01k|{
  205|  1.01k|    uint32_t i, j, k, symbols;
  206|  1.01k|    Jbig2SymbolDict *new_dict = NULL;
  207|       |
  208|       |    /* count the imported symbols and allocate a new array */
  209|  1.01k|    symbols = 0;
  210|  17.0k|    for (i = 0; i < n_dicts; i++)
  ------------------
  |  Branch (210:17): [True: 16.0k, False: 1.01k]
  ------------------
  211|  16.0k|    {
  212|  16.0k|        if (dicts[i]->n_symbols > UINT32_MAX - symbols)
  ------------------
  |  Branch (212:13): [True: 0, False: 16.0k]
  ------------------
  213|      0|        {
  214|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "too many symbols in dicts to concat");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  215|      0|            return NULL;
  216|      0|        }
  217|  16.0k|        symbols += dicts[i]->n_symbols;
  218|  16.0k|    }
  219|       |
  220|       |    /* fill a new array with new references to glyph pointers */
  221|  1.01k|    new_dict = jbig2_sd_new(ctx, symbols);
  222|  1.01k|    if (new_dict != NULL) {
  ------------------
  |  Branch (222:9): [True: 1.00k, False: 7]
  ------------------
  223|  1.00k|        k = 0;
  224|  17.0k|        for (i = 0; i < n_dicts; i++)
  ------------------
  |  Branch (224:21): [True: 16.0k, False: 1.00k]
  ------------------
  225|  25.3M|            for (j = 0; j < dicts[i]->n_symbols; j++)
  ------------------
  |  Branch (225:25): [True: 25.3M, False: 16.0k]
  ------------------
  226|  25.3M|                new_dict->glyphs[k++] = jbig2_image_reference(ctx, dicts[i]->glyphs[j]);
  227|  1.00k|    } else {
  228|      7|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate new symbol dictionary");
  ------------------
  |  |   68|      7|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  229|      7|    }
  230|       |
  231|  1.01k|    return new_dict;
  232|  1.01k|}
jbig2_symbol_dictionary:
  872|  6.11k|{
  873|  6.11k|    Jbig2SymbolDictParams params;
  874|  6.11k|    uint16_t flags;
  875|  6.11k|    uint32_t sdat_bytes;
  876|  6.11k|    uint32_t offset;
  877|  6.11k|    Jbig2ArithCx *GB_stats = NULL;
  878|  6.11k|    Jbig2ArithCx *GR_stats = NULL;
  879|  6.11k|    int table_index = 0;
  880|  6.11k|    const Jbig2HuffmanParams *huffman_params;
  881|       |
  882|  6.11k|    params.SDHUFF = 0;
  883|       |
  884|  6.11k|    if (segment->data_length < 10)
  ------------------
  |  Branch (884:9): [True: 30, False: 6.08k]
  ------------------
  885|     30|        goto too_short;
  886|       |
  887|       |    /* 7.4.2.1.1 */
  888|  6.08k|    flags = jbig2_get_uint16(segment_data);
  889|       |
  890|       |    /* zero params to ease cleanup later */
  891|  6.08k|    memset(&params, 0, sizeof(Jbig2SymbolDictParams));
  892|       |
  893|  6.08k|    params.SDHUFF = flags & 1;
  894|  6.08k|    params.SDREFAGG = (flags >> 1) & 1;
  895|  6.08k|    params.SDTEMPLATE = (flags >> 10) & 3;
  896|  6.08k|    params.SDRTEMPLATE = (flags >> 12) & 1;
  897|       |
  898|  6.08k|    if (params.SDHUFF) {
  ------------------
  |  Branch (898:9): [True: 3.38k, False: 2.69k]
  ------------------
  899|  3.38k|        switch ((flags & 0x000c) >> 2) {
  900|  3.05k|        case 0:                /* Table B.4 */
  ------------------
  |  Branch (900:9): [True: 3.05k, False: 335]
  ------------------
  901|  3.05k|            params.SDHUFFDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_D);
  902|  3.05k|            break;
  903|    192|        case 1:                /* Table B.5 */
  ------------------
  |  Branch (903:9): [True: 192, False: 3.19k]
  ------------------
  904|    192|            params.SDHUFFDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_E);
  905|    192|            break;
  906|    142|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (906:9): [True: 142, False: 3.24k]
  ------------------
  907|    142|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  908|    142|            if (huffman_params == NULL) {
  ------------------
  |  Branch (908:17): [True: 12, False: 130]
  ------------------
  909|     12|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DH huffman table not found (%d)", table_index);
  910|     12|                goto cleanup;
  911|     12|            }
  912|    130|            params.SDHUFFDH = jbig2_build_huffman_table(ctx, huffman_params);
  913|    130|            ++table_index;
  914|    130|            break;
  915|      1|        case 2:
  ------------------
  |  Branch (915:9): [True: 1, False: 3.38k]
  ------------------
  916|      1|        default:
  ------------------
  |  Branch (916:9): [True: 0, False: 3.38k]
  ------------------
  917|      1|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "symbol dictionary specified invalid huffman table");
  918|  3.38k|        }
  919|  3.37k|        if (params.SDHUFFDH == NULL) {
  ------------------
  |  Branch (919:13): [True: 4, False: 3.37k]
  ------------------
  920|      4|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate DH huffman table");
  921|      4|            goto cleanup;
  922|      4|        }
  923|       |
  924|  3.37k|        switch ((flags & 0x0030) >> 4) {
  925|  2.89k|        case 0:                /* Table B.2 */
  ------------------
  |  Branch (925:9): [True: 2.89k, False: 477]
  ------------------
  926|  2.89k|            params.SDHUFFDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_B);
  927|  2.89k|            break;
  928|    394|        case 1:                /* Table B.3 */
  ------------------
  |  Branch (928:9): [True: 394, False: 2.97k]
  ------------------
  929|    394|            params.SDHUFFDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_C);
  930|    394|            break;
  931|     78|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (931:9): [True: 78, False: 3.29k]
  ------------------
  932|     78|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  933|     78|            if (huffman_params == NULL) {
  ------------------
  |  Branch (933:17): [True: 4, False: 74]
  ------------------
  934|      4|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DW huffman table not found (%d)", table_index);
  935|      4|                goto cleanup;
  936|      4|            }
  937|     74|            params.SDHUFFDW = jbig2_build_huffman_table(ctx, huffman_params);
  938|     74|            ++table_index;
  939|     74|            break;
  940|      5|        case 2:
  ------------------
  |  Branch (940:9): [True: 5, False: 3.36k]
  ------------------
  941|      5|        default:
  ------------------
  |  Branch (941:9): [True: 0, False: 3.37k]
  ------------------
  942|      5|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "symbol dictionary specified invalid huffman table");
  943|      5|            goto cleanup;       /* Jump direct to cleanup to avoid 2 errors being given */
  944|  3.37k|        }
  945|  3.36k|        if (params.SDHUFFDW == NULL) {
  ------------------
  |  Branch (945:13): [True: 6, False: 3.35k]
  ------------------
  946|      6|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate DW huffman table");
  947|      6|            goto cleanup;
  948|      6|        }
  949|       |
  950|  3.35k|        if (flags & 0x0040) {
  ------------------
  |  Branch (950:13): [True: 104, False: 3.25k]
  ------------------
  951|       |            /* Custom table from referred segment */
  952|    104|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  953|    104|            if (huffman_params == NULL) {
  ------------------
  |  Branch (953:17): [True: 8, False: 96]
  ------------------
  954|      8|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom BMSIZE huffman table not found (%d)", table_index);
  955|      8|                goto cleanup;
  956|      8|            }
  957|     96|            params.SDHUFFBMSIZE = jbig2_build_huffman_table(ctx, huffman_params);
  958|     96|            ++table_index;
  959|  3.25k|        } else {
  960|       |            /* Table B.1 */
  961|  3.25k|            params.SDHUFFBMSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
  962|  3.25k|        }
  963|  3.34k|        if (params.SDHUFFBMSIZE == NULL) {
  ------------------
  |  Branch (963:13): [True: 1, False: 3.34k]
  ------------------
  964|      1|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate BMSIZE huffman table");
  965|      1|            goto cleanup;
  966|      1|        }
  967|       |
  968|  3.34k|        if (flags & 0x0080) {
  ------------------
  |  Branch (968:13): [True: 71, False: 3.27k]
  ------------------
  969|       |            /* Custom table from referred segment */
  970|     71|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  971|     71|            if (huffman_params == NULL) {
  ------------------
  |  Branch (971:17): [True: 3, False: 68]
  ------------------
  972|      3|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom REFAGG huffman table not found (%d)", table_index);
  973|      3|                goto cleanup;
  974|      3|            }
  975|     68|            params.SDHUFFAGGINST = jbig2_build_huffman_table(ctx, huffman_params);
  976|     68|            ++table_index;
  977|  3.27k|        } else {
  978|       |            /* Table B.1 */
  979|  3.27k|            params.SDHUFFAGGINST = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
  980|  3.27k|        }
  981|  3.34k|        if (params.SDHUFFAGGINST == NULL) {
  ------------------
  |  Branch (981:13): [True: 1, False: 3.34k]
  ------------------
  982|      1|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate REFAGG huffman table");
  983|      1|            goto cleanup;
  984|      1|        }
  985|  3.34k|    }
  986|       |
  987|       |    /* FIXME: there are quite a few of these conditions to check */
  988|       |    /* maybe #ifdef CONFORMANCE and a separate routine */
  989|  6.04k|    if (!params.SDHUFF) {
  ------------------
  |  Branch (989:9): [True: 2.69k, False: 3.34k]
  ------------------
  990|  2.69k|        if (flags & 0x000c) {
  ------------------
  |  Branch (990:13): [True: 6, False: 2.69k]
  ------------------
  991|      6|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "SDHUFF is zero, but contrary to spec SDHUFFDH is not.");
  992|      6|            goto cleanup;
  993|      6|        }
  994|  2.69k|        if (flags & 0x0030) {
  ------------------
  |  Branch (994:13): [True: 2, False: 2.69k]
  ------------------
  995|      2|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "SDHUFF is zero, but contrary to spec SDHUFFDW is not.");
  996|      2|            goto cleanup;
  997|      2|        }
  998|  2.69k|    }
  999|       |
 1000|       |    /* 7.4.2.1.2 */
 1001|  6.03k|    sdat_bytes = params.SDHUFF ? 0 : params.SDTEMPLATE == 0 ? 8 : 2;
  ------------------
  |  Branch (1001:18): [True: 3.34k, False: 2.69k]
  |  Branch (1001:38): [True: 375, False: 2.31k]
  ------------------
 1002|  6.03k|    memcpy(params.sdat, segment_data + 2, sdat_bytes);
 1003|  6.03k|    offset = 2 + sdat_bytes;
 1004|       |
 1005|       |    /* 7.4.2.1.3 */
 1006|  6.03k|    if (params.SDREFAGG && !params.SDRTEMPLATE) {
  ------------------
  |  Branch (1006:9): [True: 1.21k, False: 4.82k]
  |  Branch (1006:28): [True: 827, False: 384]
  ------------------
 1007|    827|        if (offset + 4 > segment->data_length)
  ------------------
  |  Branch (1007:13): [True: 1, False: 826]
  ------------------
 1008|      1|            goto too_short;
 1009|    826|        memcpy(params.sdrat, segment_data + offset, 4);
 1010|    826|        offset += 4;
 1011|    826|    }
 1012|       |
 1013|  6.03k|    if (offset + 8 > segment->data_length)
  ------------------
  |  Branch (1013:9): [True: 10, False: 6.02k]
  ------------------
 1014|     10|        goto too_short;
 1015|       |
 1016|       |    /* 7.4.2.1.4 */
 1017|  6.02k|    params.SDNUMEXSYMS = jbig2_get_uint32(segment_data + offset);
 1018|       |    /* 7.4.2.1.5 */
 1019|  6.02k|    params.SDNUMNEWSYMS = jbig2_get_uint32(segment_data + offset + 4);
 1020|  6.02k|    offset += 8;
 1021|       |
 1022|  6.02k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
 1023|  6.02k|                "symbol dictionary, flags=%04x, %u exported syms, %u new syms", flags, params.SDNUMEXSYMS, params.SDNUMNEWSYMS);
 1024|       |
 1025|       |    /* 7.4.2.2 (2) */
 1026|  6.02k|    {
 1027|  6.02k|        uint32_t n_dicts = jbig2_sd_count_referred(ctx, segment);
 1028|  6.02k|        Jbig2SymbolDict **dicts = NULL;
 1029|       |
 1030|  6.02k|        if (n_dicts > 0) {
  ------------------
  |  Branch (1030:13): [True: 1.01k, False: 5.01k]
  ------------------
 1031|  1.01k|            dicts = jbig2_sd_list_referred(ctx, segment);
 1032|  1.01k|            if (dicts == NULL) {
  ------------------
  |  Branch (1032:17): [True: 1, False: 1.01k]
  ------------------
 1033|      1|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate dicts in symbol dictionary");
 1034|      1|                goto cleanup;
 1035|      1|            }
 1036|  1.01k|            params.SDINSYMS = jbig2_sd_cat(ctx, n_dicts, dicts);
 1037|  1.01k|            if (params.SDINSYMS == NULL) {
  ------------------
  |  Branch (1037:17): [True: 7, False: 1.00k]
  ------------------
 1038|      7|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol array in symbol dictionary");
 1039|      7|                jbig2_free(ctx->allocator, dicts);
 1040|      7|                goto cleanup;
 1041|      7|            }
 1042|  1.00k|            jbig2_free(ctx->allocator, dicts);
 1043|  1.00k|        }
 1044|  6.01k|        if (params.SDINSYMS != NULL) {
  ------------------
  |  Branch (1044:13): [True: 1.00k, False: 5.01k]
  ------------------
 1045|  1.00k|            params.SDNUMINSYMS = params.SDINSYMS->n_symbols;
 1046|  1.00k|        }
 1047|  6.01k|    }
 1048|       |
 1049|       |    /* 7.4.2.2 (3, 4) */
 1050|  6.01k|    if (flags & 0x0100) {
  ------------------
  |  Branch (1050:9): [True: 16, False: 5.99k]
  ------------------
 1051|     16|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "segment marks bitmap coding context as used (NYI)");
 1052|     16|        goto cleanup;
 1053|  5.99k|    } else {
 1054|  5.99k|        int stats_size = params.SDTEMPLATE == 0 ? 65536 : params.SDTEMPLATE == 1 ? 8192 : 1024;
  ------------------
  |  Branch (1054:26): [True: 2.96k, False: 3.03k]
  |  Branch (1054:59): [True: 679, False: 2.35k]
  ------------------
 1055|       |
 1056|  5.99k|        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  5.99k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
 1057|  5.99k|        if (GB_stats == NULL) {
  ------------------
  |  Branch (1057:13): [True: 1, False: 5.99k]
  ------------------
 1058|      1|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder states for generic regions");
 1059|      1|            goto cleanup;
 1060|      1|        }
 1061|  5.99k|        memset(GB_stats, 0, sizeof (Jbig2ArithCx) * stats_size);
 1062|       |
 1063|  5.99k|        stats_size = params.SDRTEMPLATE ? 1 << 10 : 1 << 13;
  ------------------
  |  Branch (1063:22): [True: 638, False: 5.35k]
  ------------------
 1064|  5.99k|        GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  5.99k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
 1065|  5.99k|        if (GR_stats == NULL) {
  ------------------
  |  Branch (1065:13): [True: 1, False: 5.99k]
  ------------------
 1066|      1|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder states for generic refinement regions");
 1067|      1|            jbig2_free(ctx->allocator, GB_stats);
 1068|      1|            goto cleanup;
 1069|      1|        }
 1070|  5.99k|        memset(GR_stats, 0, sizeof (Jbig2ArithCx) * stats_size);
 1071|  5.99k|    }
 1072|       |
 1073|  5.99k|    segment->result = (void *)jbig2_decode_symbol_dict(ctx, segment, &params, segment_data + offset, segment->data_length - offset, GB_stats, GR_stats);
 1074|       |#ifdef DUMP_SYMDICT
 1075|       |    if (segment->result)
 1076|       |        jbig2_dump_symbol_dict(ctx, segment);
 1077|       |#endif
 1078|       |
 1079|       |    /* 7.4.2.2 (7) */
 1080|  5.99k|    if (flags & 0x0200) {
  ------------------
  |  Branch (1080:9): [True: 324, False: 5.67k]
  ------------------
 1081|       |        /* todo: retain GB_stats, GR_stats */
 1082|    324|        jbig2_free(ctx->allocator, GR_stats);
 1083|    324|        jbig2_free(ctx->allocator, GB_stats);
 1084|    324|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "segment marks bitmap coding context as retained (NYI)");
 1085|    324|        goto cleanup;
 1086|  5.67k|    } else {
 1087|  5.67k|        jbig2_free(ctx->allocator, GR_stats);
 1088|  5.67k|        jbig2_free(ctx->allocator, GB_stats);
 1089|  5.67k|    }
 1090|       |
 1091|  6.07k|cleanup:
 1092|  6.07k|    if (params.SDHUFF) {
  ------------------
  |  Branch (1092:9): [True: 3.38k, False: 2.69k]
  ------------------
 1093|  3.38k|        jbig2_release_huffman_table(ctx, params.SDHUFFDH);
 1094|  3.38k|        jbig2_release_huffman_table(ctx, params.SDHUFFDW);
 1095|  3.38k|        jbig2_release_huffman_table(ctx, params.SDHUFFBMSIZE);
 1096|  3.38k|        jbig2_release_huffman_table(ctx, params.SDHUFFAGGINST);
 1097|  3.38k|    }
 1098|  6.07k|    jbig2_sd_release(ctx, params.SDINSYMS);
 1099|       |
 1100|  6.07k|    return (segment->result != NULL) ? 0 : -1;
  ------------------
  |  Branch (1100:12): [True: 4.57k, False: 1.50k]
  ------------------
 1101|       |
 1102|     41|too_short:
 1103|     41|    if (params.SDHUFF) {
  ------------------
  |  Branch (1103:9): [True: 3, False: 38]
  ------------------
 1104|      3|        jbig2_release_huffman_table(ctx, params.SDHUFFDH);
 1105|      3|        jbig2_release_huffman_table(ctx, params.SDHUFFDW);
 1106|      3|        jbig2_release_huffman_table(ctx, params.SDHUFFBMSIZE);
 1107|      3|        jbig2_release_huffman_table(ctx, params.SDHUFFAGGINST);
 1108|      3|    }
 1109|     41|    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
 1110|  5.99k|}
jbig2_symbol_dict.c:jbig2_decode_symbol_dict:
  241|  5.99k|{
  242|  5.99k|    Jbig2SymbolDict *SDNEWSYMS = NULL;
  243|  5.99k|    Jbig2SymbolDict *SDEXSYMS = NULL;
  244|  5.99k|    uint32_t HCHEIGHT;
  245|  5.99k|    uint32_t NSYMSDECODED;
  246|  5.99k|    uint32_t SYMWIDTH, TOTWIDTH;
  247|  5.99k|    uint32_t HCFIRSTSYM;
  248|  5.99k|    uint32_t *SDNEWSYMWIDTHS = NULL;
  249|  5.99k|    uint8_t SBSYMCODELEN = 0;
  250|  5.99k|    Jbig2WordStream *ws = NULL;
  251|  5.99k|    Jbig2HuffmanState *hs = NULL;
  252|  5.99k|    Jbig2ArithState *as = NULL;
  253|  5.99k|    Jbig2ArithIntCtx *IADH = NULL;
  254|  5.99k|    Jbig2ArithIntCtx *IADW = NULL;
  255|  5.99k|    Jbig2ArithIntCtx *IAEX = NULL;
  256|  5.99k|    Jbig2ArithIntCtx *IAAI = NULL;
  257|  5.99k|    int code = 0;
  258|  5.99k|    Jbig2SymbolDict **refagg_dicts = NULL;
  259|  5.99k|    uint32_t i;
  260|  5.99k|    Jbig2TextRegionParams tparams;
  261|  5.99k|    Jbig2Image *image = NULL;
  262|  5.99k|    Jbig2Image *glyph = NULL;
  263|  5.99k|    uint32_t emptyruns = 0;
  264|       |
  265|  5.99k|    memset(&tparams, 0, sizeof(tparams));
  266|       |
  267|       |    /* 6.5.5 (3) */
  268|  5.99k|    HCHEIGHT = 0;
  269|  5.99k|    NSYMSDECODED = 0;
  270|       |
  271|  5.99k|    ws = jbig2_word_stream_buf_new(ctx, data, size);
  272|  5.99k|    if (ws == NULL) {
  ------------------
  |  Branch (272:9): [True: 1, False: 5.99k]
  ------------------
  273|      1|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate word stream when decoding symbol dictionary");
  274|      1|        return NULL;
  275|      1|    }
  276|       |
  277|  5.99k|    as = jbig2_arith_new(ctx, ws);
  278|  5.99k|    if (as == NULL) {
  ------------------
  |  Branch (278:9): [True: 10, False: 5.98k]
  ------------------
  279|     10|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when decoding symbol dictionary");
  280|     10|        jbig2_word_stream_buf_free(ctx, ws);
  281|     10|        return NULL;
  282|     10|    }
  283|       |
  284|  31.1k|    for (SBSYMCODELEN = 0; ((uint64_t) 1 << SBSYMCODELEN) < ((uint64_t) params->SDNUMINSYMS + params->SDNUMNEWSYMS); SBSYMCODELEN++);
  ------------------
  |  Branch (284:28): [True: 25.2k, False: 5.98k]
  ------------------
  285|       |
  286|  5.98k|    if (params->SDHUFF) {
  ------------------
  |  Branch (286:9): [True: 3.31k, False: 2.66k]
  ------------------
  287|  3.31k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "huffman coded symbol dictionary");
  288|  3.31k|        hs = jbig2_huffman_new(ctx, ws);
  289|  3.31k|        tparams.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  290|  3.31k|        tparams.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  291|  3.31k|        tparams.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A); /* Table B.1 */
  292|  3.31k|        if (hs == NULL || tparams.SBHUFFRDX == NULL ||
  ------------------
  |  Branch (292:13): [True: 1, False: 3.31k]
  |  Branch (292:27): [True: 1, False: 3.31k]
  ------------------
  293|  3.31k|                tparams.SBHUFFRDY == NULL || tparams.SBHUFFRSIZE == NULL) {
  ------------------
  |  Branch (293:17): [True: 2, False: 3.31k]
  |  Branch (293:46): [True: 1, False: 3.31k]
  ------------------
  294|      5|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate for symbol bitmap");
  295|      5|            goto cleanup;
  296|      5|        }
  297|       |        /* 6.5.5 (2) */
  298|  3.31k|        if (!params->SDREFAGG) {
  ------------------
  |  Branch (298:13): [True: 2.55k, False: 761]
  ------------------
  299|  2.55k|            SDNEWSYMWIDTHS = jbig2_new(ctx, uint32_t, params->SDNUMNEWSYMS);
  ------------------
  |  |  141|  2.55k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  300|  2.55k|            if (SDNEWSYMWIDTHS == NULL) {
  ------------------
  |  Branch (300:17): [True: 10, False: 2.54k]
  ------------------
  301|     10|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate symbol widths (%u)", params->SDNUMNEWSYMS);
  302|     10|                goto cleanup;
  303|     10|            }
  304|  2.55k|        } else {
  305|    761|            tparams.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_F);    /* Table B.6 */
  306|    761|            tparams.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_H);    /* Table B.8 */
  307|    761|            tparams.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_K);    /* Table B.11 */
  308|    761|            tparams.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  309|    761|            tparams.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  310|    761|            if (tparams.SBHUFFFS == NULL || tparams.SBHUFFDS == NULL ||
  ------------------
  |  Branch (310:17): [True: 4, False: 757]
  |  Branch (310:45): [True: 1, False: 756]
  ------------------
  311|    756|                    tparams.SBHUFFDT == NULL || tparams.SBHUFFRDW == NULL ||
  ------------------
  |  Branch (311:21): [True: 1, False: 755]
  |  Branch (311:49): [True: 1, False: 754]
  ------------------
  312|    754|                    tparams.SBHUFFRDH == NULL) {
  ------------------
  |  Branch (312:21): [True: 1, False: 753]
  ------------------
  313|      8|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "out of memory creating text region huffman decoder entries");
  314|      8|                goto cleanup;
  315|      8|            }
  316|    761|        }
  317|  3.31k|    } else {
  318|  2.66k|        IADH = jbig2_arith_int_ctx_new(ctx);
  319|  2.66k|        IADW = jbig2_arith_int_ctx_new(ctx);
  320|  2.66k|        IAEX = jbig2_arith_int_ctx_new(ctx);
  321|  2.66k|        IAAI = jbig2_arith_int_ctx_new(ctx);
  322|  2.66k|        if (IADH == NULL || IADW == NULL || IAEX == NULL || IAAI == NULL) {
  ------------------
  |  Branch (322:13): [True: 1, False: 2.66k]
  |  Branch (322:29): [True: 1, False: 2.66k]
  |  Branch (322:45): [True: 1, False: 2.66k]
  |  Branch (322:61): [True: 1, False: 2.66k]
  ------------------
  323|      4|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol bitmap");
  324|      4|            goto cleanup;
  325|      4|        }
  326|  2.66k|        tparams.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
  327|  2.66k|        tparams.IARDX = jbig2_arith_int_ctx_new(ctx);
  328|  2.66k|        tparams.IARDY = jbig2_arith_int_ctx_new(ctx);
  329|  2.66k|        if (tparams.IAID == NULL || tparams.IARDX == NULL ||
  ------------------
  |  Branch (329:13): [True: 23, False: 2.64k]
  |  Branch (329:37): [True: 1, False: 2.63k]
  ------------------
  330|  2.63k|                tparams.IARDY == NULL) {
  ------------------
  |  Branch (330:17): [True: 1, False: 2.63k]
  ------------------
  331|     25|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region arithmetic decoder contexts");
  332|     25|            goto cleanup;
  333|     25|        }
  334|  2.63k|        if (params->SDREFAGG) {
  ------------------
  |  Branch (334:13): [True: 419, False: 2.21k]
  ------------------
  335|       |            /* Values from Table 17, section 6.5.8.2 (2) */
  336|    419|            tparams.IADT = jbig2_arith_int_ctx_new(ctx);
  337|    419|            tparams.IAFS = jbig2_arith_int_ctx_new(ctx);
  338|    419|            tparams.IADS = jbig2_arith_int_ctx_new(ctx);
  339|    419|            tparams.IAIT = jbig2_arith_int_ctx_new(ctx);
  340|       |            /* Table 31 */
  341|    419|            tparams.IARI = jbig2_arith_int_ctx_new(ctx);
  342|    419|            tparams.IARDW = jbig2_arith_int_ctx_new(ctx);
  343|    419|            tparams.IARDH = jbig2_arith_int_ctx_new(ctx);
  344|    419|            if (tparams.IADT == NULL || tparams.IAFS == NULL ||
  ------------------
  |  Branch (344:17): [True: 1, False: 418]
  |  Branch (344:41): [True: 1, False: 417]
  ------------------
  345|    417|                    tparams.IADS == NULL || tparams.IAIT == NULL ||
  ------------------
  |  Branch (345:21): [True: 2, False: 415]
  |  Branch (345:45): [True: 2, False: 413]
  ------------------
  346|    413|                    tparams.IARI == NULL || tparams.IARDW == NULL ||
  ------------------
  |  Branch (346:21): [True: 2, False: 411]
  |  Branch (346:45): [True: 1, False: 410]
  ------------------
  347|    410|                    tparams.IARDH == NULL) {
  ------------------
  |  Branch (347:21): [True: 1, False: 409]
  ------------------
  348|     10|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region arith decoder contexts");
  349|     10|                goto cleanup;
  350|     10|            }
  351|    419|        }
  352|  2.63k|    }
  353|  5.92k|    tparams.SBHUFF = params->SDHUFF;
  354|  5.92k|    tparams.SBREFINE = 1;
  355|  5.92k|    tparams.SBSTRIPS = 1;
  356|  5.92k|    tparams.SBDEFPIXEL = 0;
  357|  5.92k|    tparams.SBCOMBOP = JBIG2_COMPOSE_OR;
  358|  5.92k|    tparams.TRANSPOSED = 0;
  359|  5.92k|    tparams.REFCORNER = JBIG2_CORNER_TOPLEFT;
  360|  5.92k|    tparams.SBDSOFFSET = 0;
  361|  5.92k|    tparams.SBRTEMPLATE = params->SDRTEMPLATE;
  362|       |
  363|       |    /* 6.5.5 (1) */
  364|  5.92k|    SDNEWSYMS = jbig2_sd_new(ctx, params->SDNUMNEWSYMS);
  365|  5.92k|    if (SDNEWSYMS == NULL) {
  ------------------
  |  Branch (365:9): [True: 40, False: 5.88k]
  ------------------
  366|     40|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate new symbols (%u)", params->SDNUMNEWSYMS);
  367|     40|        goto cleanup;
  368|     40|    }
  369|       |
  370|  5.88k|    refagg_dicts = jbig2_new(ctx, Jbig2SymbolDict *, 2);
  ------------------
  |  |  141|  5.88k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  371|  5.88k|    if (refagg_dicts == NULL) {
  ------------------
  |  Branch (371:9): [True: 1, False: 5.88k]
  ------------------
  372|      1|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Out of memory allocating dictionary array");
  373|      1|        goto cleanup;
  374|      1|    }
  375|  5.88k|    refagg_dicts[0] = jbig2_sd_new(ctx, params->SDNUMINSYMS);
  376|  5.88k|    if (refagg_dicts[0] == NULL) {
  ------------------
  |  Branch (376:9): [True: 5, False: 5.87k]
  ------------------
  377|      5|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "out of memory allocating symbol dictionary");
  378|      5|        goto cleanup;
  379|      5|    }
  380|  20.4M|    for (i = 0; i < params->SDNUMINSYMS; i++) {
  ------------------
  |  Branch (380:17): [True: 20.4M, False: 5.87k]
  ------------------
  381|  20.4M|        refagg_dicts[0]->glyphs[i] = jbig2_image_reference(ctx, params->SDINSYMS->glyphs[i]);
  382|  20.4M|    }
  383|  5.87k|    refagg_dicts[1] = SDNEWSYMS;
  384|       |
  385|       |    /* 6.5.5 (4a) */
  386|  1.51M|    while (NSYMSDECODED < params->SDNUMNEWSYMS) {
  ------------------
  |  Branch (386:12): [True: 1.51M, False: 4.71k]
  ------------------
  387|  1.51M|        int32_t HCDH, DW;
  388|       |
  389|       |        /* 6.5.6 */
  390|  1.51M|        if (params->SDHUFF) {
  ------------------
  |  Branch (390:13): [True: 8.82k, False: 1.50M]
  ------------------
  391|  8.82k|            HCDH = jbig2_huffman_get(hs, params->SDHUFFDH, &code);
  392|  1.50M|        } else {
  393|  1.50M|            code = jbig2_arith_int_decode(ctx, IADH, as, &HCDH);
  394|  1.50M|        }
  395|  1.51M|        if (code < 0) {
  ------------------
  |  Branch (395:13): [True: 3, False: 1.51M]
  ------------------
  396|      3|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode height class delta");
  397|      3|            goto cleanup;
  398|      3|        }
  399|  1.51M|        if (code > 0) {
  ------------------
  |  Branch (399:13): [True: 9, False: 1.51M]
  ------------------
  400|      9|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "OOB decoding height class delta");
  401|      9|            goto cleanup;
  402|      9|        }
  403|       |
  404|       |        /* 6.5.5 (4b) */
  405|  1.51M|        HCHEIGHT = HCHEIGHT + HCDH;
  406|  1.51M|        SYMWIDTH = 0;
  407|  1.51M|        TOTWIDTH = 0;
  408|  1.51M|        HCFIRSTSYM = NSYMSDECODED;
  409|       |
  410|  1.51M|        if ((int32_t) HCHEIGHT < 0) {
  ------------------
  |  Branch (410:13): [True: 45, False: 1.51M]
  ------------------
  411|     45|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid HCHEIGHT value");
  412|     45|            goto cleanup;
  413|     45|        }
  414|       |#ifdef JBIG2_DEBUG
  415|       |        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "HCHEIGHT = %d", HCHEIGHT);
  416|       |#endif
  417|  1.51M|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "decoding height class %d with %d syms decoded", HCHEIGHT, NSYMSDECODED);
  418|       |
  419|  19.7M|        for (;;) {
  420|       |            /* 6.5.7 */
  421|  19.7M|            if (params->SDHUFF) {
  ------------------
  |  Branch (421:17): [True: 18.1M, False: 1.67M]
  ------------------
  422|  18.1M|                DW = jbig2_huffman_get(hs, params->SDHUFFDW, &code);
  423|  18.1M|            } else {
  424|  1.67M|                code = jbig2_arith_int_decode(ctx, IADW, as, &DW);
  425|  1.67M|            }
  426|  19.7M|            if (code < 0)
  ------------------
  |  Branch (426:17): [True: 3, False: 19.7M]
  ------------------
  427|      3|            {
  428|      3|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode DW");
  429|      3|                goto cleanup;
  430|      3|            }
  431|       |            /* 6.5.5 (4c.i) */
  432|  19.7M|            if (code > 0) {
  ------------------
  |  Branch (432:17): [True: 1.50M, False: 18.2M]
  ------------------
  433|  1.50M|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "OOB when decoding DW signals end of height class %d", HCHEIGHT);
  434|  1.50M|                break;
  435|  1.50M|            }
  436|       |
  437|       |            /* check for broken symbol table */
  438|  18.2M|            if (NSYMSDECODED >= params->SDNUMNEWSYMS) {
  ------------------
  |  Branch (438:17): [True: 2.44k, False: 18.2M]
  ------------------
  439|  2.44k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "no OOB signaling end of height class %d, continuing", HCHEIGHT);
  440|  2.44k|                break;
  441|  2.44k|            }
  442|       |
  443|  18.2M|            if (DW < 0 && SYMWIDTH < (uint32_t) -DW) {
  ------------------
  |  Branch (443:17): [True: 9.33k, False: 18.2M]
  |  Branch (443:27): [True: 179, False: 9.15k]
  ------------------
  444|    179|                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "DW value (%d) would make SYMWIDTH (%u) negative at symbol %u", DW, SYMWIDTH, NSYMSDECODED + 1);
  445|    179|                goto cleanup;
  446|    179|            }
  447|  18.2M|            if (DW > 0 && (uint32_t) DW > UINT32_MAX - SYMWIDTH) {
  ------------------
  |  Branch (447:17): [True: 121k, False: 18.1M]
  |  Branch (447:27): [True: 1, False: 121k]
  ------------------
  448|      1|                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "DW value (%d) would make SYMWIDTH (%u) too large at symbol %u", DW, SYMWIDTH, NSYMSDECODED + 1);
  449|      1|                goto cleanup;
  450|      1|            }
  451|       |
  452|  18.2M|            SYMWIDTH = SYMWIDTH + DW;
  453|  18.2M|            if (SYMWIDTH > UINT32_MAX - TOTWIDTH) {
  ------------------
  |  Branch (453:17): [True: 38, False: 18.2M]
  ------------------
  454|     38|                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "SYMWIDTH value (%u) would make TOTWIDTH (%u) too large at symbol %u", SYMWIDTH, TOTWIDTH, NSYMSDECODED + 1);
  455|     38|                goto cleanup;
  456|     38|            }
  457|       |
  458|  18.2M|            TOTWIDTH = TOTWIDTH + SYMWIDTH;
  459|       |#ifdef JBIG2_DEBUG
  460|       |            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "SYMWIDTH = %u TOTWIDTH = %u", SYMWIDTH, TOTWIDTH);
  461|       |#endif
  462|       |            /* 6.5.5 (4c.ii) */
  463|  18.2M|            if (!params->SDHUFF || params->SDREFAGG) {
  ------------------
  |  Branch (463:17): [True: 167k, False: 18.1M]
  |  Branch (463:36): [True: 4.67k, False: 18.1M]
  ------------------
  464|       |#ifdef JBIG2_DEBUG
  465|       |                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "SDHUFF = %d; SDREFAGG = %d", params->SDHUFF, params->SDREFAGG);
  466|       |#endif
  467|       |                /* 6.5.8 */
  468|   172k|                if (!params->SDREFAGG) {
  ------------------
  |  Branch (468:21): [True: 161k, False: 10.5k]
  ------------------
  469|   161k|                    Jbig2GenericRegionParams region_params;
  470|   161k|                    int sdat_bytes;
  471|       |
  472|       |                    /* Table 16 */
  473|   161k|                    region_params.MMR = 0;
  474|   161k|                    region_params.GBTEMPLATE = params->SDTEMPLATE;
  475|   161k|                    region_params.TPGDON = 0;
  476|   161k|                    region_params.USESKIP = 0;
  477|   161k|                    sdat_bytes = params->SDTEMPLATE == 0 ? 8 : 2;
  ------------------
  |  Branch (477:34): [True: 20.6k, False: 141k]
  ------------------
  478|   161k|                    memcpy(region_params.gbat, params->sdat, sdat_bytes);
  479|       |
  480|   161k|                    image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
  481|   161k|                    if (image == NULL) {
  ------------------
  |  Branch (481:25): [True: 74, False: 161k]
  ------------------
  482|     74|                        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate image");
  483|     74|                        goto cleanup;
  484|     74|                    }
  485|       |
  486|   161k|                    code = jbig2_decode_generic_region(ctx, segment, &region_params, as, image, GB_stats);
  487|   161k|                    if (code < 0) {
  ------------------
  |  Branch (487:25): [True: 26, False: 161k]
  ------------------
  488|     26|                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode generic region");
  489|     26|                        goto cleanup;
  490|     26|                    }
  491|       |
  492|   161k|                    SDNEWSYMS->glyphs[NSYMSDECODED] = image;
  493|   161k|                    image = NULL;
  494|   161k|                } else {
  495|       |                    /* 6.5.8.2 refinement/aggregate symbol */
  496|  10.5k|                    uint32_t REFAGGNINST;
  497|       |
  498|  10.5k|                    if (params->SDHUFF) {
  ------------------
  |  Branch (498:25): [True: 4.67k, False: 5.90k]
  ------------------
  499|  4.67k|                        REFAGGNINST = jbig2_huffman_get(hs, params->SDHUFFAGGINST, &code);
  500|  5.90k|                    } else {
  501|  5.90k|                        code = jbig2_arith_int_decode(ctx, IAAI, as, (int32_t *) &REFAGGNINST);
  502|  5.90k|                    }
  503|  10.5k|                    if (code < 0) {
  ------------------
  |  Branch (503:25): [True: 1, False: 10.5k]
  ------------------
  504|      1|                        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode number of symbols in aggregate glyph");
  505|      1|                        goto cleanup;
  506|      1|                    }
  507|  10.5k|                    if (code > 0) {
  ------------------
  |  Branch (507:25): [True: 4, False: 10.5k]
  ------------------
  508|      4|                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB in number of symbols in aggregate glyph");
  509|      4|                        goto cleanup;
  510|      4|                    }
  511|  10.5k|                    if ((int32_t) REFAGGNINST <= 0) {
  ------------------
  |  Branch (511:25): [True: 137, False: 10.4k]
  ------------------
  512|    137|                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid number of symbols in aggregate glyph");
  513|    137|                        goto cleanup;
  514|    137|                    }
  515|       |
  516|  10.4k|                    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "aggregate symbol coding (%d instances)", REFAGGNINST);
  517|       |
  518|  10.4k|                    if (REFAGGNINST > 1) {
  ------------------
  |  Branch (518:25): [True: 8.28k, False: 2.15k]
  ------------------
  519|  8.28k|                        tparams.SBNUMINSTANCES = REFAGGNINST;
  520|       |
  521|  8.28k|                        image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
  522|  8.28k|                        if (image == NULL) {
  ------------------
  |  Branch (522:29): [True: 14, False: 8.26k]
  ------------------
  523|     14|                            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol image");
  524|     14|                            goto cleanup;
  525|     14|                        }
  526|       |
  527|       |                        /* multiple symbols are handled as a text region */
  528|  8.26k|                        code = jbig2_decode_text_region(ctx, segment, &tparams, (const Jbig2SymbolDict * const *)refagg_dicts,
  529|  8.26k|                                                        2, image, GR_stats, as, ws);
  530|  8.26k|                        if (code < 0) {
  ------------------
  |  Branch (530:29): [True: 344, False: 7.92k]
  ------------------
  531|    344|                            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode text region");
  532|    344|                            goto cleanup;
  533|    344|                        }
  534|       |
  535|  7.92k|                        SDNEWSYMS->glyphs[NSYMSDECODED] = image;
  536|  7.92k|                        image = NULL;
  537|  7.92k|                    } else {
  538|       |                        /* 6.5.8.2.2 */
  539|       |                        /* bool SBHUFF = params->SDHUFF; */
  540|  2.15k|                        Jbig2RefinementRegionParams rparams;
  541|  2.15k|                        uint32_t ID;
  542|  2.15k|                        int32_t RDX, RDY;
  543|  2.15k|                        size_t BMSIZE = 0;
  544|  2.15k|                        uint32_t ninsyms = params->SDNUMINSYMS;
  545|  2.15k|                        int code1 = 0;
  546|  2.15k|                        int code2 = 0;
  547|  2.15k|                        int code3 = 0;
  548|  2.15k|                        int code4 = 0;
  549|  2.15k|                        int code5 = 0;
  550|       |
  551|       |                        /* 6.5.8.2.2 (2, 3, 4, 5) */
  552|  2.15k|                        if (params->SDHUFF) {
  ------------------
  |  Branch (552:29): [True: 1.63k, False: 520]
  ------------------
  553|  1.63k|                            ID = jbig2_huffman_get_bits(hs, SBSYMCODELEN, &code1);
  554|  1.63k|                            RDX = jbig2_huffman_get(hs, tparams.SBHUFFRDX, &code2);
  555|  1.63k|                            RDY = jbig2_huffman_get(hs, tparams.SBHUFFRDY, &code3);
  556|  1.63k|                            BMSIZE = jbig2_huffman_get(hs, tparams.SBHUFFRSIZE, &code4);
  557|  1.63k|                            code5 = jbig2_huffman_skip(hs);
  558|  1.63k|                        } else {
  559|    520|                            code1 = jbig2_arith_iaid_decode(ctx, tparams.IAID, as, (int32_t *) &ID);
  560|    520|                            code2 = jbig2_arith_int_decode(ctx, tparams.IARDX, as, &RDX);
  561|    520|                            code3 = jbig2_arith_int_decode(ctx, tparams.IARDY, as, &RDY);
  562|    520|                        }
  563|       |
  564|  2.15k|                        if (code1 < 0 || code2 < 0 || code3 < 0 || code4 < 0 || code5 < 0) {
  ------------------
  |  Branch (564:29): [True: 0, False: 2.15k]
  |  Branch (564:42): [True: 2, False: 2.14k]
  |  Branch (564:55): [True: 0, False: 2.14k]
  |  Branch (564:68): [True: 0, False: 2.14k]
  |  Branch (564:81): [True: 0, False: 2.14k]
  ------------------
  565|      2|                            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode data");
  566|      2|                            goto cleanup;
  567|      2|                        }
  568|  2.14k|                        if (code1 > 0 || code2 > 0 || code3 > 0 || code4 > 0 || code5 > 0) {
  ------------------
  |  Branch (568:29): [True: 0, False: 2.14k]
  |  Branch (568:42): [True: 2, False: 2.14k]
  |  Branch (568:55): [True: 4, False: 2.14k]
  |  Branch (568:68): [True: 0, False: 2.14k]
  |  Branch (568:81): [True: 0, False: 2.14k]
  ------------------
  569|      6|                            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB in single refinement/aggregate coded symbol data");
  570|      6|                            goto cleanup;
  571|      6|                        }
  572|       |
  573|  2.14k|                        if (ID >= ninsyms + NSYMSDECODED) {
  ------------------
  |  Branch (573:29): [True: 30, False: 2.11k]
  ------------------
  574|     30|                            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "refinement references unknown symbol %d", ID);
  575|     30|                            goto cleanup;
  576|     30|                        }
  577|       |
  578|  2.11k|                        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  579|  2.11k|                                    "symbol is a refinement of ID %d with the refinement applied at (%d,%d)", ID, RDX, RDY);
  580|       |
  581|  2.11k|                        image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
  582|  2.11k|                        if (image == NULL) {
  ------------------
  |  Branch (582:29): [True: 1, False: 2.11k]
  ------------------
  583|      1|                            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol image");
  584|      1|                            goto cleanup;
  585|      1|                        }
  586|       |
  587|       |                        /* Table 18 */
  588|  2.11k|                        rparams.GRTEMPLATE = params->SDRTEMPLATE;
  589|  2.11k|                        rparams.GRREFERENCE = (ID < ninsyms) ? params->SDINSYMS->glyphs[ID] : SDNEWSYMS->glyphs[ID - ninsyms];
  ------------------
  |  Branch (589:47): [True: 391, False: 1.72k]
  ------------------
  590|       |                        /* SumatraPDF: fail on missing glyphs */
  591|  2.11k|                        if (rparams.GRREFERENCE == NULL) {
  ------------------
  |  Branch (591:29): [True: 2, False: 2.11k]
  ------------------
  592|      2|                            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "missing glyph %d/%d", ID, ninsyms);
  593|      2|                            goto cleanup;
  594|      2|                        }
  595|  2.11k|                        rparams.GRREFERENCEDX = RDX;
  596|  2.11k|                        rparams.GRREFERENCEDY = RDY;
  597|  2.11k|                        rparams.TPGRON = 0;
  598|  2.11k|                        memcpy(rparams.grat, params->sdrat, 4);
  599|  2.11k|                        code = jbig2_decode_refinement_region(ctx, segment, &rparams, as, image, GR_stats);
  600|  2.11k|                        if (code < 0) {
  ------------------
  |  Branch (600:29): [True: 1, False: 2.10k]
  ------------------
  601|      1|                            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
  602|      1|                            goto cleanup;
  603|      1|                        }
  604|       |
  605|  2.10k|                        SDNEWSYMS->glyphs[NSYMSDECODED] = image;
  606|  2.10k|                        image = NULL;
  607|       |
  608|       |                        /* 6.5.8.2.2 (7) */
  609|  2.10k|                        if (params->SDHUFF) {
  ------------------
  |  Branch (609:29): [True: 1.60k, False: 505]
  ------------------
  610|  1.60k|                            if (BMSIZE == 0)
  ------------------
  |  Branch (610:33): [True: 820, False: 784]
  ------------------
  611|    820|                                BMSIZE = (size_t) SDNEWSYMS->glyphs[NSYMSDECODED]->height *
  612|    820|                                    SDNEWSYMS->glyphs[NSYMSDECODED]->stride;
  613|  1.60k|                            code = jbig2_huffman_advance(hs, BMSIZE);
  614|  1.60k|                            if (code < 0) {
  ------------------
  |  Branch (614:33): [True: 0, False: 1.60k]
  ------------------
  615|      0|                                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to advance after huffman decoding in refinement region");
  616|      0|                                goto cleanup;
  617|      0|                            }
  618|  1.60k|                        }
  619|  2.10k|                    }
  620|  10.4k|                }
  621|       |
  622|       |#ifdef OUTPUT_PBM
  623|       |                {
  624|       |                    char name[64];
  625|       |                    FILE *out;
  626|       |                    int code;
  627|       |
  628|       |                    snprintf(name, 64, "sd.%04d.%04d.pbm", segment->number, NSYMSDECODED);
  629|       |                    out = fopen(name, "wb");
  630|       |                    code = jbig2_image_write_pbm(SDNEWSYMS->glyphs[NSYMSDECODED], out);
  631|       |                    fclose(out);
  632|       |                    if (code < 0) {
  633|       |                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write glyph");
  634|       |                        goto cleanup;
  635|       |                    }
  636|       |                    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "writing out glyph as '%s' ...", name);
  637|       |                }
  638|       |#endif
  639|       |
  640|   172k|            }
  641|       |
  642|       |            /* 6.5.5 (4c.iii) */
  643|  18.2M|            if (params->SDHUFF && !params->SDREFAGG) {
  ------------------
  |  Branch (643:17): [True: 18.1M, False: 167k]
  |  Branch (643:35): [True: 18.1M, False: 4.24k]
  ------------------
  644|  18.1M|                SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
  645|  18.1M|            }
  646|       |
  647|       |            /* 6.5.5 (4c.iv) */
  648|  18.2M|            NSYMSDECODED = NSYMSDECODED + 1;
  649|       |
  650|  18.2M|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "decoded symbol %u of %u (%ux%u)", NSYMSDECODED, params->SDNUMNEWSYMS, SYMWIDTH, HCHEIGHT);
  651|       |
  652|  18.2M|        }                       /* end height class decode loop */
  653|       |
  654|       |        /* 6.5.5 (4d) */
  655|  1.51M|        if (params->SDHUFF && !params->SDREFAGG) {
  ------------------
  |  Branch (655:13): [True: 8.31k, False: 1.50M]
  |  Branch (655:31): [True: 8.02k, False: 285]
  ------------------
  656|       |            /* 6.5.9 */
  657|  8.02k|            size_t BMSIZE;
  658|  8.02k|            uint32_t j;
  659|  8.02k|            int x;
  660|       |
  661|  8.02k|            BMSIZE = jbig2_huffman_get(hs, params->SDHUFFBMSIZE, &code);
  662|  8.02k|            if (code < 0) {
  ------------------
  |  Branch (662:17): [True: 1, False: 8.02k]
  ------------------
  663|      1|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error decoding size of collective bitmap");
  664|      1|                goto cleanup;
  665|      1|            }
  666|  8.02k|            if (code > 0) {
  ------------------
  |  Branch (666:17): [True: 1, False: 8.02k]
  ------------------
  667|      1|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding size of collective bitmap");
  668|      1|                goto cleanup;
  669|      1|            }
  670|       |
  671|       |            /* skip any bits before the next byte boundary */
  672|  8.02k|            code = jbig2_huffman_skip(hs);
  673|  8.02k|            if (code < 0) {
  ------------------
  |  Branch (673:17): [True: 0, False: 8.02k]
  ------------------
  674|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to skip to next byte when decoding collective bitmap");
  675|      0|            }
  676|       |
  677|  8.02k|            image = jbig2_image_new(ctx, TOTWIDTH, HCHEIGHT);
  678|  8.02k|            if (image == NULL) {
  ------------------
  |  Branch (678:17): [True: 40, False: 7.98k]
  ------------------
  679|     40|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate collective bitmap image");
  680|     40|                goto cleanup;
  681|     40|            }
  682|       |
  683|  7.98k|            if (BMSIZE == 0) {
  ------------------
  |  Branch (683:17): [True: 892, False: 7.09k]
  ------------------
  684|       |                /* if BMSIZE == 0 bitmap is uncompressed */
  685|    892|                const byte *src = data + jbig2_huffman_offset(hs);
  686|    892|                const int stride = (image->width >> 3) + ((image->width & 7) ? 1 : 0);
  ------------------
  |  Branch (686:59): [True: 807, False: 85]
  ------------------
  687|    892|                byte *dst = image->data;
  688|       |
  689|       |                /* SumatraPDF: prevent read access violation */
  690|    892|                if (size < jbig2_huffman_offset(hs) || (size - jbig2_huffman_offset(hs) < (size_t) image->height * stride) || (size < jbig2_huffman_offset(hs))) {
  ------------------
  |  Branch (690:21): [True: 50, False: 842]
  |  Branch (690:56): [True: 13, False: 829]
  |  Branch (690:127): [True: 0, False: 829]
  ------------------
  691|     63|                    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "not enough data for decoding uncompressed (%d/%li)", image->height * stride,
  692|     63|                                (long) (size - jbig2_huffman_offset(hs)));
  693|     63|                    goto cleanup;
  694|     63|                }
  695|       |
  696|    829|                BMSIZE = (size_t) image->height * stride;
  697|    829|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  698|    829|                            "reading %dx%d uncompressed bitmap for %d symbols (%li bytes)", image->width, image->height, NSYMSDECODED - HCFIRSTSYM, (long) BMSIZE);
  699|       |
  700|  10.1k|                for (j = 0; j < image->height; j++) {
  ------------------
  |  Branch (700:29): [True: 9.35k, False: 829]
  ------------------
  701|  9.35k|                    memcpy(dst, src, stride);
  702|  9.35k|                    dst += image->stride;
  703|  9.35k|                    src += stride;
  704|  9.35k|                }
  705|  7.09k|            } else {
  706|  7.09k|                Jbig2GenericRegionParams rparams;
  707|       |
  708|       |                /* SumatraPDF: prevent read access violation */
  709|  7.09k|                if (size < jbig2_huffman_offset(hs) || size < BMSIZE || size - jbig2_huffman_offset(hs) < BMSIZE) {
  ------------------
  |  Branch (709:21): [True: 12, False: 7.08k]
  |  Branch (709:56): [True: 81, False: 7.00k]
  |  Branch (709:73): [True: 17, False: 6.98k]
  ------------------
  710|    110|                    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "not enough data for decoding (%li/%li)", (long) BMSIZE, (long) (size - jbig2_huffman_offset(hs)));
  711|    110|                    goto cleanup;
  712|    110|                }
  713|       |
  714|  6.98k|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  715|  6.98k|                            "reading %dx%d collective bitmap for %d symbols (%li bytes)", image->width, image->height, NSYMSDECODED - HCFIRSTSYM, (long) BMSIZE);
  716|       |
  717|  6.98k|                rparams.MMR = 1;
  718|  6.98k|                code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data + jbig2_huffman_offset(hs), BMSIZE, image);
  719|  6.98k|                if (code) {
  ------------------
  |  Branch (719:21): [True: 5, False: 6.98k]
  ------------------
  720|      5|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode MMR-coded generic region");
  721|      5|                    goto cleanup;
  722|      5|                }
  723|  6.98k|            }
  724|       |
  725|       |            /* advance past the data we've just read */
  726|  7.80k|            code = jbig2_huffman_advance(hs, BMSIZE);
  727|  7.80k|            if (code < 0) {
  ------------------
  |  Branch (727:17): [True: 0, False: 7.80k]
  ------------------
  728|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to advance after huffman decoding MMR bitmap image");
  729|      0|                goto cleanup;
  730|      0|            }
  731|       |
  732|       |            /* copy the collective bitmap into the symbol dictionary */
  733|  7.80k|            x = 0;
  734|   235k|            for (j = HCFIRSTSYM; j < NSYMSDECODED; j++) {
  ------------------
  |  Branch (734:34): [True: 227k, False: 7.78k]
  ------------------
  735|   227k|                glyph = jbig2_image_new(ctx, SDNEWSYMWIDTHS[j], HCHEIGHT);
  736|   227k|                if (glyph == NULL) {
  ------------------
  |  Branch (736:21): [True: 22, False: 227k]
  ------------------
  737|     22|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to copy the collective bitmap into symbol dictionary");
  738|     22|                    goto cleanup;
  739|     22|                }
  740|   227k|                code = jbig2_image_compose(ctx, glyph, image, -x, 0, JBIG2_COMPOSE_REPLACE);
  741|   227k|                if (code) {
  ------------------
  |  Branch (741:21): [True: 0, False: 227k]
  ------------------
  742|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to compose image into glyph");
  743|      0|                    goto cleanup;
  744|      0|                }
  745|   227k|                x += SDNEWSYMWIDTHS[j];
  746|   227k|                SDNEWSYMS->glyphs[j] = glyph;
  747|   227k|                glyph = NULL;
  748|   227k|            }
  749|  7.78k|            jbig2_image_release(ctx, image);
  750|  7.78k|            image = NULL;
  751|  7.78k|        }
  752|       |
  753|  1.51M|    }                           /* end of symbol decode loop */
  754|       |
  755|       |    /* 6.5.10 */
  756|  4.71k|    SDEXSYMS = jbig2_sd_new(ctx, params->SDNUMEXSYMS);
  757|  4.71k|    if (SDEXSYMS == NULL) {
  ------------------
  |  Branch (757:9): [True: 116, False: 4.59k]
  ------------------
  758|    116|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbols exported from symbols dictionary");
  759|    116|        goto cleanup;
  760|  4.59k|    } else {
  761|  4.59k|        uint32_t i = 0;
  762|  4.59k|        uint32_t j = 0;
  763|  4.59k|        uint32_t k;
  764|  4.59k|        int exflag = 0;
  765|  4.59k|        uint32_t limit = params->SDNUMINSYMS + params->SDNUMNEWSYMS;
  766|  4.59k|        uint32_t EXRUNLENGTH;
  767|       |
  768|   131k|        while (i < limit) {
  ------------------
  |  Branch (768:16): [True: 127k, False: 4.57k]
  ------------------
  769|   127k|            if (params->SDHUFF)
  ------------------
  |  Branch (769:17): [True: 19.4k, False: 107k]
  ------------------
  770|  19.4k|                EXRUNLENGTH = jbig2_huffman_get(hs, tparams.SBHUFFRSIZE, &code);
  771|   107k|            else
  772|   107k|                code = jbig2_arith_int_decode(ctx, IAEX, as, (int32_t *) &EXRUNLENGTH);
  773|   127k|            if (code < 0) {
  ------------------
  |  Branch (773:17): [True: 1, False: 127k]
  ------------------
  774|      1|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode runlength for exported symbols");
  775|       |                /* skip to the cleanup code and return SDEXSYMS = NULL */
  776|      1|                jbig2_sd_release(ctx, SDEXSYMS);
  777|      1|                SDEXSYMS = NULL;
  778|      1|                break;
  779|      1|            }
  780|   127k|            if (code > 0) {
  ------------------
  |  Branch (780:17): [True: 9, False: 127k]
  ------------------
  781|      9|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB when decoding runlength for exported symbols");
  782|       |                /* skip to the cleanup code and return SDEXSYMS = NULL */
  783|      9|                jbig2_sd_release(ctx, SDEXSYMS);
  784|      9|                SDEXSYMS = NULL;
  785|      9|                break;
  786|      9|            }
  787|       |
  788|       |            /* prevent infinite list of empty runs, 1000 is just an arbitrary number */
  789|   127k|            if (EXRUNLENGTH <= 0 && ++emptyruns == 1000) {
  ------------------
  |  Branch (789:17): [True: 18.8k, False: 108k]
  |  Branch (789:37): [True: 16, False: 18.8k]
  ------------------
  790|     16|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "runlength too small in export symbol table (%u == 0 i = %u limit = %u)", EXRUNLENGTH, i, limit);
  791|       |                /* skip to the cleanup code and return SDEXSYMS = NULL */
  792|     16|                jbig2_sd_release(ctx, SDEXSYMS);
  793|     16|                SDEXSYMS = NULL;
  794|     16|                break;
  795|   127k|            } else if (EXRUNLENGTH > 0) {
  ------------------
  |  Branch (795:24): [True: 108k, False: 18.8k]
  ------------------
  796|   108k|                emptyruns = 0;
  797|   108k|            }
  798|       |
  799|   127k|            if (EXRUNLENGTH > limit - i) {
  ------------------
  |  Branch (799:17): [True: 3.46k, False: 123k]
  ------------------
  800|  3.46k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "exporting more symbols than available (%u > %u), capping", i + EXRUNLENGTH, limit);
  801|  3.46k|                EXRUNLENGTH = limit - i;
  802|  3.46k|            }
  803|   127k|            if (exflag && j + EXRUNLENGTH > params->SDNUMEXSYMS) {
  ------------------
  |  Branch (803:17): [True: 62.7k, False: 64.5k]
  |  Branch (803:27): [True: 2.09k, False: 60.6k]
  ------------------
  804|  2.09k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "exporting more symbols than may be exported (%u > %u), capping", j + EXRUNLENGTH, params->SDNUMEXSYMS);
  805|  2.09k|                EXRUNLENGTH = params->SDNUMEXSYMS - j;
  806|  2.09k|            }
  807|       |
  808|  16.5M|            for (k = 0; k < EXRUNLENGTH; k++) {
  ------------------
  |  Branch (808:25): [True: 16.4M, False: 127k]
  ------------------
  809|  16.4M|                if (exflag) {
  ------------------
  |  Branch (809:21): [True: 1.46M, False: 14.9M]
  ------------------
  810|  1.46M|                    Jbig2Image *img;
  811|  1.46M|                    if (i < params->SDNUMINSYMS) {
  ------------------
  |  Branch (811:25): [True: 1.44M, False: 13.9k]
  ------------------
  812|  1.44M|                        img = params->SDINSYMS->glyphs[i];
  813|  1.44M|                    } else {
  814|  13.9k|                        img = SDNEWSYMS->glyphs[i - params->SDNUMINSYMS];
  815|  13.9k|                    }
  816|  1.46M|                    SDEXSYMS->glyphs[j++] = jbig2_image_reference(ctx, img);
  817|  1.46M|                }
  818|  16.4M|                i++;
  819|  16.4M|            }
  820|   127k|            exflag = !exflag;
  821|   127k|        }
  822|  4.59k|    }
  823|       |
  824|  5.98k|cleanup:
  825|  5.98k|    jbig2_image_release(ctx, glyph);
  826|  5.98k|    jbig2_image_release(ctx, image);
  827|  5.98k|    if (refagg_dicts != NULL) {
  ------------------
  |  Branch (827:9): [True: 5.88k, False: 103]
  ------------------
  828|  5.88k|        if (refagg_dicts[0] != NULL)
  ------------------
  |  Branch (828:13): [True: 5.87k, False: 5]
  ------------------
  829|  5.87k|            jbig2_sd_release(ctx, refagg_dicts[0]);
  830|       |        /* skip releasing refagg_dicts[1] as that is the same as SDNEWSYMS */
  831|  5.88k|        jbig2_free(ctx->allocator, refagg_dicts);
  832|  5.88k|    }
  833|  5.98k|    jbig2_sd_release(ctx, SDNEWSYMS);
  834|  5.98k|    if (params->SDHUFF) {
  ------------------
  |  Branch (834:9): [True: 3.31k, False: 2.66k]
  ------------------
  835|  3.31k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRSIZE);
  836|  3.31k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDY);
  837|  3.31k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDX);
  838|  3.31k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDH);
  839|  3.31k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDW);
  840|  3.31k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFDT);
  841|  3.31k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFDS);
  842|  3.31k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFFS);
  843|  3.31k|        if (!params->SDREFAGG) {
  ------------------
  |  Branch (843:13): [True: 2.55k, False: 766]
  ------------------
  844|  2.55k|            jbig2_free(ctx->allocator, SDNEWSYMWIDTHS);
  845|  2.55k|        }
  846|  3.31k|        jbig2_huffman_free(ctx, hs);
  847|  3.31k|    } else {
  848|  2.66k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDY);
  849|  2.66k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDX);
  850|  2.66k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDH);
  851|  2.66k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDW);
  852|  2.66k|        jbig2_arith_int_ctx_free(ctx, tparams.IARI);
  853|  2.66k|        jbig2_arith_iaid_ctx_free(ctx, tparams.IAID);
  854|  2.66k|        jbig2_arith_int_ctx_free(ctx, tparams.IAIT);
  855|  2.66k|        jbig2_arith_int_ctx_free(ctx, tparams.IADS);
  856|  2.66k|        jbig2_arith_int_ctx_free(ctx, tparams.IAFS);
  857|  2.66k|        jbig2_arith_int_ctx_free(ctx, tparams.IADT);
  858|  2.66k|        jbig2_arith_int_ctx_free(ctx, IAAI);
  859|  2.66k|        jbig2_arith_int_ctx_free(ctx, IAEX);
  860|  2.66k|        jbig2_arith_int_ctx_free(ctx, IADW);
  861|  2.66k|        jbig2_arith_int_ctx_free(ctx, IADH);
  862|  2.66k|    }
  863|  5.98k|    jbig2_free(ctx->allocator, as);
  864|  5.98k|    jbig2_word_stream_buf_free(ctx, ws);
  865|       |
  866|  5.98k|    return SDEXSYMS;
  867|  4.71k|}

jbig2_decode_text_region:
   62|  11.8k|{
   63|       |    /* relevant bits of 6.4.4 */
   64|  11.8k|    uint32_t NINSTANCES;
   65|  11.8k|    uint32_t ID;
   66|  11.8k|    int32_t STRIPT;
   67|  11.8k|    int32_t FIRSTS;
   68|  11.8k|    int32_t DT;
   69|  11.8k|    int32_t DFS;
   70|  11.8k|    int32_t IDS;
   71|  11.8k|    int32_t CURS;
   72|  11.8k|    int32_t CURT;
   73|  11.8k|    int S, T;
   74|  11.8k|    int x, y;
   75|  11.8k|    bool first_symbol;
  ------------------
  |  |   45|  11.8k|#define bool int
  ------------------
   76|  11.8k|    uint32_t index, SBNUMSYMS;
   77|  11.8k|    Jbig2Image *IB = NULL;
   78|  11.8k|    Jbig2Image *IBO = NULL;
   79|  11.8k|    Jbig2Image *refimage = NULL;
   80|  11.8k|    Jbig2HuffmanState *hs = NULL;
   81|  11.8k|    Jbig2HuffmanTable *SBSYMCODES = NULL;
   82|  11.8k|    int code = 0;
   83|  11.8k|    int RI;
   84|       |
   85|  11.8k|    SBNUMSYMS = 0;
   86|  35.9k|    for (index = 0; index < n_dicts; index++) {
  ------------------
  |  Branch (86:21): [True: 24.1k, False: 11.8k]
  ------------------
   87|  24.1k|        SBNUMSYMS += dicts[index]->n_symbols;
   88|  24.1k|    }
   89|  11.8k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "symbol list contains %d glyphs in %d dictionaries", SBNUMSYMS, n_dicts);
   90|       |
   91|  11.8k|    if (params->SBHUFF) {
  ------------------
  |  Branch (91:9): [True: 3.17k, False: 8.66k]
  ------------------
   92|  3.17k|        Jbig2HuffmanTable *runcodes = NULL;
   93|  3.17k|        Jbig2HuffmanParams runcodeparams;
   94|  3.17k|        Jbig2HuffmanLine runcodelengths[35];
   95|  3.17k|        Jbig2HuffmanLine *symcodelengths = NULL;
   96|  3.17k|        Jbig2HuffmanParams symcodeparams;
   97|  3.17k|        int err, len, range, r;
   98|       |
   99|  3.17k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "huffman coded text region");
  100|  3.17k|        hs = jbig2_huffman_new(ctx, ws);
  101|  3.17k|        if (hs == NULL)
  ------------------
  |  Branch (101:13): [True: 1, False: 3.17k]
  ------------------
  102|      1|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region");
  103|       |
  104|       |        /* 7.4.3.1.7 - decode symbol ID Huffman table */
  105|       |        /* this is actually part of the segment header, but it is more
  106|       |           convenient to handle it here */
  107|       |
  108|       |        /* parse and build the runlength code huffman table */
  109|   114k|        for (index = 0; index < 35; index++) {
  ------------------
  |  Branch (109:25): [True: 111k, False: 3.17k]
  ------------------
  110|   111k|            runcodelengths[index].PREFLEN = jbig2_huffman_get_bits(hs, 4, &code);
  111|   111k|            if (code < 0) {
  ------------------
  |  Branch (111:17): [True: 0, False: 111k]
  ------------------
  112|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to read huffman runcode lengths");
  113|      0|                goto cleanup1;
  114|      0|            }
  115|   111k|            if (code > 0) {
  ------------------
  |  Branch (115:17): [True: 0, False: 111k]
  ------------------
  116|      0|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB decoding huffman runcode lengths");
  117|      0|                goto cleanup1;
  118|      0|            }
  119|   111k|            runcodelengths[index].RANGELEN = 0;
  120|   111k|            runcodelengths[index].RANGELOW = index;
  121|   111k|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "  read runcode%d length %d", index, runcodelengths[index].PREFLEN);
  122|   111k|        }
  123|  3.17k|        runcodeparams.HTOOB = 0;
  124|  3.17k|        runcodeparams.lines = runcodelengths;
  125|  3.17k|        runcodeparams.n_lines = 35;
  126|  3.17k|        runcodes = jbig2_build_huffman_table(ctx, &runcodeparams);
  127|  3.17k|        if (runcodes == NULL) {
  ------------------
  |  Branch (127:13): [True: 11, False: 3.16k]
  ------------------
  128|     11|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error constructing symbol ID runcode table");
  129|     11|            goto cleanup1;
  130|     11|        }
  131|       |
  132|       |        /* decode the symbol ID code lengths using the runlength table */
  133|  3.16k|        symcodelengths = jbig2_new(ctx, Jbig2HuffmanLine, SBNUMSYMS);
  ------------------
  |  |  141|  3.16k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  134|  3.16k|        if (symcodelengths == NULL) {
  ------------------
  |  Branch (134:13): [True: 2, False: 3.16k]
  ------------------
  135|      2|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate memory when reading symbol ID huffman table");
  136|      2|            goto cleanup1;
  137|      2|        }
  138|  3.16k|        index = 0;
  139|  12.8M|        while (index < SBNUMSYMS) {
  ------------------
  |  Branch (139:16): [True: 12.8M, False: 3.14k]
  ------------------
  140|  12.8M|            code = jbig2_huffman_get(hs, runcodes, &err);
  141|  12.8M|            if (err < 0) {
  ------------------
  |  Branch (141:17): [True: 18, False: 12.8M]
  ------------------
  142|     18|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error reading symbol ID huffman table");
  143|     18|                goto cleanup1;
  144|     18|            }
  145|  12.8M|            if (err > 0) {
  ------------------
  |  Branch (145:17): [True: 0, False: 12.8M]
  ------------------
  146|      0|                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB decoding symbol ID huffman table");
  147|      0|                goto cleanup1;
  148|      0|            }
  149|  12.8M|            if (code < 0 || code >= 35) {
  ------------------
  |  Branch (149:17): [True: 0, False: 12.8M]
  |  Branch (149:29): [True: 0, False: 12.8M]
  ------------------
  150|      0|                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "symbol ID huffman table out of range");
  151|      0|                goto cleanup1;
  152|      0|            }
  153|       |
  154|  12.8M|            if (code < 32) {
  ------------------
  |  Branch (154:17): [True: 12.3M, False: 519k]
  ------------------
  155|  12.3M|                len = code;
  156|  12.3M|                range = 1;
  157|  12.3M|            } else {
  158|   519k|                if (code == 32) {
  ------------------
  |  Branch (158:21): [True: 201k, False: 317k]
  ------------------
  159|   201k|                    if (index < 1) {
  ------------------
  |  Branch (159:25): [True: 2, False: 201k]
  ------------------
  160|      2|                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "error decoding symbol ID table: run length with no antecedent");
  161|      2|                        goto cleanup1;
  162|      2|                    }
  163|   201k|                    len = symcodelengths[index - 1].PREFLEN;
  164|   317k|                } else {
  165|   317k|                    len = 0;    /* code == 33 or 34 */
  166|   317k|                }
  167|   519k|                err = 0;
  168|   519k|                if (code == 32)
  ------------------
  |  Branch (168:21): [True: 201k, False: 317k]
  ------------------
  169|   201k|                    range = jbig2_huffman_get_bits(hs, 2, &err) + 3;
  170|   317k|                else if (code == 33)
  ------------------
  |  Branch (170:26): [True: 44.2k, False: 273k]
  ------------------
  171|  44.2k|                    range = jbig2_huffman_get_bits(hs, 3, &err) + 3;
  172|   273k|                else if (code == 34)
  ------------------
  |  Branch (172:26): [True: 273k, False: 0]
  ------------------
  173|   273k|                    range = jbig2_huffman_get_bits(hs, 7, &err) + 11;
  174|   519k|                if (err < 0) {
  ------------------
  |  Branch (174:21): [True: 0, False: 519k]
  ------------------
  175|      0|                    code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to read huffman code");
  176|      0|                    goto cleanup1;
  177|      0|                }
  178|   519k|                if (err > 0) {
  ------------------
  |  Branch (178:21): [True: 0, False: 519k]
  ------------------
  179|      0|                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB decoding huffman code");
  180|      0|                    goto cleanup1;
  181|      0|                }
  182|   519k|            }
  183|  12.8M|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "  read runcode%d at index %d (length %d range %d)", code, index, len, range);
  184|  12.8M|            if (index + range > SBNUMSYMS) {
  ------------------
  |  Branch (184:17): [True: 2.36k, False: 12.8M]
  ------------------
  185|  2.36k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  186|  2.36k|                            "runlength extends %d entries beyond the end of symbol ID table", index + range - SBNUMSYMS);
  187|  2.36k|                range = SBNUMSYMS - index;
  188|  2.36k|            }
  189|  29.4M|            for (r = 0; r < range; r++) {
  ------------------
  |  Branch (189:25): [True: 16.6M, False: 12.8M]
  ------------------
  190|  16.6M|                symcodelengths[index + r].PREFLEN = len;
  191|  16.6M|                symcodelengths[index + r].RANGELEN = 0;
  192|  16.6M|                symcodelengths[index + r].RANGELOW = index + r;
  193|  16.6M|            }
  194|  12.8M|            index += r;
  195|  12.8M|        }
  196|       |
  197|  3.14k|        if (index < SBNUMSYMS) {
  ------------------
  |  Branch (197:13): [True: 0, False: 3.14k]
  ------------------
  198|      0|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "runlength codes do not cover the available symbol set");
  199|      0|            goto cleanup1;
  200|      0|        }
  201|       |
  202|  3.14k|        symcodeparams.HTOOB = 0;
  203|  3.14k|        symcodeparams.lines = symcodelengths;
  204|  3.14k|        symcodeparams.n_lines = SBNUMSYMS;
  205|       |
  206|       |        /* skip to byte boundary */
  207|  3.14k|        err = jbig2_huffman_skip(hs);
  208|  3.14k|        if (err < 0)
  ------------------
  |  Branch (208:13): [True: 0, False: 3.14k]
  ------------------
  209|      0|        {
  210|      0|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to skip to next byte when building huffman table");
  211|      0|            goto cleanup1;
  212|      0|        }
  213|       |
  214|       |        /* finally, construct the symbol ID huffman table itself */
  215|  3.14k|        SBSYMCODES = jbig2_build_huffman_table(ctx, &symcodeparams);
  216|       |
  217|  3.17k|cleanup1:
  218|  3.17k|        jbig2_free(ctx->allocator, symcodelengths);
  219|  3.17k|        jbig2_release_huffman_table(ctx, runcodes);
  220|       |
  221|  3.17k|        if (SBSYMCODES == NULL) {
  ------------------
  |  Branch (221:13): [True: 107, False: 3.06k]
  ------------------
  222|    107|            jbig2_huffman_free(ctx, hs);
  223|    107|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to construct symbol ID huffman table");
  224|    107|        }
  225|  3.17k|    }
  226|       |
  227|       |    /* 6.4.5 (1) */
  228|  11.7k|    jbig2_image_clear(ctx, image, params->SBDEFPIXEL);
  229|       |
  230|       |    /* 6.4.6 */
  231|  11.7k|    if (params->SBHUFF) {
  ------------------
  |  Branch (231:9): [True: 3.06k, False: 8.66k]
  ------------------
  232|  3.06k|        STRIPT = jbig2_huffman_get(hs, params->SBHUFFDT, &code);
  233|  8.66k|    } else {
  234|  8.66k|        code = jbig2_arith_int_decode(ctx, params->IADT, as, &STRIPT);
  235|  8.66k|    }
  236|  11.7k|    if (code < 0) {
  ------------------
  |  Branch (236:9): [True: 3, False: 11.7k]
  ------------------
  237|      3|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode strip T");
  238|      3|        goto cleanup2;
  239|      3|    }
  240|  11.7k|    if (code > 0) {
  ------------------
  |  Branch (240:9): [True: 5, False: 11.7k]
  ------------------
  241|      5|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding strip T");
  242|      5|        goto cleanup2;
  243|      5|    }
  244|       |
  245|       |    /* 6.4.5 (2) */
  246|  11.7k|    STRIPT *= -(params->SBSTRIPS);
  247|  11.7k|    FIRSTS = 0;
  248|  11.7k|    NINSTANCES = 0;
  249|       |
  250|       |    /* 6.4.5 (3) */
  251|  43.1k|    while (NINSTANCES < params->SBNUMINSTANCES) {
  ------------------
  |  Branch (251:12): [True: 31.8k, False: 11.2k]
  ------------------
  252|       |        /* (3b) */
  253|  31.8k|        if (params->SBHUFF) {
  ------------------
  |  Branch (253:13): [True: 9.28k, False: 22.6k]
  ------------------
  254|  9.28k|            DT = jbig2_huffman_get(hs, params->SBHUFFDT, &code);
  255|  22.6k|        } else {
  256|  22.6k|            code = jbig2_arith_int_decode(ctx, params->IADT, as, &DT);
  257|  22.6k|        }
  258|  31.8k|        if (code < 0) {
  ------------------
  |  Branch (258:13): [True: 1, False: 31.8k]
  ------------------
  259|      1|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode delta T");
  260|      1|            goto cleanup2;
  261|      1|        }
  262|  31.8k|        if (code > 0) {
  ------------------
  |  Branch (262:13): [True: 43, False: 31.8k]
  ------------------
  263|     43|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding delta T");
  264|     43|            goto cleanup2;
  265|     43|        }
  266|  31.8k|        DT *= params->SBSTRIPS;
  267|  31.8k|        STRIPT += DT;
  268|       |
  269|  31.8k|        first_symbol = TRUE;
  ------------------
  |  |   53|  31.8k|#define TRUE 1
  ------------------
  270|       |        /* 6.4.5 (3c) - decode symbols in strip */
  271|  29.1M|        for (;;) {
  272|       |            /* (3c.i) */
  273|  29.1M|            if (first_symbol) {
  ------------------
  |  Branch (273:17): [True: 31.8k, False: 29.1M]
  ------------------
  274|       |                /* 6.4.7 */
  275|  31.8k|                if (params->SBHUFF) {
  ------------------
  |  Branch (275:21): [True: 9.28k, False: 22.5k]
  ------------------
  276|  9.28k|                    DFS = jbig2_huffman_get(hs, params->SBHUFFFS, &code);
  277|  22.5k|                } else {
  278|  22.5k|                    code = jbig2_arith_int_decode(ctx, params->IAFS, as, &DFS);
  279|  22.5k|                }
  280|  31.8k|                if (code < 0) {
  ------------------
  |  Branch (280:21): [True: 1, False: 31.8k]
  ------------------
  281|      1|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode strip symbol S-difference");
  282|      1|                    goto cleanup2;
  283|      1|                }
  284|  31.8k|                if (code > 0) {
  ------------------
  |  Branch (284:21): [True: 81, False: 31.7k]
  ------------------
  285|     81|                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding strip symbol S-difference");
  286|     81|                    goto cleanup2;
  287|     81|                }
  288|  31.7k|                FIRSTS += DFS;
  289|  31.7k|                CURS = FIRSTS;
  290|  31.7k|                first_symbol = FALSE;
  ------------------
  |  |   56|  31.7k|#define FALSE 0
  ------------------
  291|  29.1M|            } else {
  292|  29.1M|                if (NINSTANCES > params->SBNUMINSTANCES) {
  ------------------
  |  Branch (292:21): [True: 9.30k, False: 29.1M]
  ------------------
  293|  9.30k|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "too many NINSTANCES (%d) decoded", NINSTANCES);
  294|  9.30k|                    break;
  295|  9.30k|                }
  296|       |                /* (3c.ii) / 6.4.8 */
  297|  29.1M|                if (params->SBHUFF) {
  ------------------
  |  Branch (297:21): [True: 19.1M, False: 9.96M]
  ------------------
  298|  19.1M|                    IDS = jbig2_huffman_get(hs, params->SBHUFFDS, &code);
  299|  19.1M|                } else {
  300|  9.96M|                    code = jbig2_arith_int_decode(ctx, params->IADS, as, &IDS);
  301|  9.96M|                }
  302|  29.1M|                if (code < 0) {
  ------------------
  |  Branch (302:21): [True: 130, False: 29.1M]
  ------------------
  303|    130|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode symbol instance S coordinate");
  304|    130|                    goto cleanup2;
  305|    130|                }
  306|  29.1M|                if (code > 0) {
  ------------------
  |  Branch (306:21): [True: 22.0k, False: 29.0M]
  ------------------
  307|  22.0k|                    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "OOB obtained when decoding symbol instance S coordinate signals end of strip with T value %d", DT);
  308|  22.0k|                    break;
  309|  22.0k|                }
  310|  29.0M|                CURS += IDS + params->SBDSOFFSET;
  311|  29.0M|            }
  312|       |
  313|       |            /* (3c.iii) / 6.4.9 */
  314|  29.1M|            if (params->SBSTRIPS == 1) {
  ------------------
  |  Branch (314:17): [True: 26.8M, False: 2.22M]
  ------------------
  315|  26.8M|                CURT = 0;
  316|  26.8M|            } else if (params->SBHUFF) {
  ------------------
  |  Branch (316:24): [True: 1.54M, False: 682k]
  ------------------
  317|  1.54M|                CURT = jbig2_huffman_get_bits(hs, params->LOGSBSTRIPS, &code);
  318|  1.54M|            } else {
  319|   682k|                code = jbig2_arith_int_decode(ctx, params->IAIT, as, &CURT);
  320|   682k|            }
  321|  29.1M|            if (code < 0) {
  ------------------
  |  Branch (321:17): [True: 0, False: 29.1M]
  ------------------
  322|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode symbol instance T coordinate");
  323|      0|                goto cleanup2;
  324|      0|            }
  325|  29.1M|            if (code > 0) {
  ------------------
  |  Branch (325:17): [True: 61, False: 29.1M]
  ------------------
  326|     61|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "OOB obtained when decoding symbol instance T coordinate");
  327|     61|                goto cleanup2;
  328|     61|            }
  329|  29.1M|            T = STRIPT + CURT;
  330|       |
  331|       |            /* (3b.iv) / 6.4.10 - decode the symbol ID */
  332|  29.1M|            if (params->SBHUFF) {
  ------------------
  |  Branch (332:17): [True: 19.1M, False: 9.97M]
  ------------------
  333|  19.1M|                ID = jbig2_huffman_get(hs, SBSYMCODES, &code);
  334|  19.1M|            } else {
  335|  9.97M|                code = jbig2_arith_iaid_decode(ctx, params->IAID, as, (int *)&ID);
  336|  9.97M|            }
  337|  29.1M|            if (code < 0) {
  ------------------
  |  Branch (337:17): [True: 39, False: 29.1M]
  ------------------
  338|     39|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to obtain symbol instance symbol ID");
  339|     39|                goto cleanup2;
  340|     39|            }
  341|  29.1M|            if (code > 0) {
  ------------------
  |  Branch (341:17): [True: 0, False: 29.1M]
  ------------------
  342|      0|                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding symbol instance symbol ID");
  343|      0|                goto cleanup2;
  344|      0|            }
  345|  29.1M|            if (ID >= SBNUMSYMS) {
  ------------------
  |  Branch (345:17): [True: 4.58M, False: 24.5M]
  ------------------
  346|  4.58M|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "ignoring out of range symbol ID (%d/%d)", ID, SBNUMSYMS);
  347|  4.58M|                IB = NULL;
  348|  24.5M|            } else {
  349|       |                /* (3c.v) / 6.4.11 - look up the symbol bitmap IB */
  350|  24.5M|                uint32_t id = ID;
  351|       |
  352|  24.5M|                index = 0;
  353|  35.2M|                while (id >= dicts[index]->n_symbols)
  ------------------
  |  Branch (353:24): [True: 10.7M, False: 24.5M]
  ------------------
  354|  10.7M|                    id -= dicts[index++]->n_symbols;
  355|  24.5M|                if (dicts[index]->glyphs[id] == NULL) {
  ------------------
  |  Branch (355:21): [True: 9.38M, False: 15.1M]
  ------------------
  356|  9.38M|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "missing glyph (%d/%d), ignoring", index, id);
  357|  15.1M|                } else {
  358|  15.1M|                    IB = jbig2_image_reference(ctx, dicts[index]->glyphs[id]);
  359|  15.1M|                }
  360|  24.5M|            }
  361|  29.1M|            if (params->SBREFINE) {
  ------------------
  |  Branch (361:17): [True: 10.1M, False: 18.9M]
  ------------------
  362|  10.1M|                if (params->SBHUFF) {
  ------------------
  |  Branch (362:21): [True: 9.70M, False: 492k]
  ------------------
  363|  9.70M|                    RI = jbig2_huffman_get_bits(hs, 1, &code);
  364|  9.70M|                } else {
  365|   492k|                    code = jbig2_arith_int_decode(ctx, params->IARI, as, &RI);
  366|   492k|                }
  367|  10.1M|                if (code < 0) {
  ------------------
  |  Branch (367:21): [True: 0, False: 10.1M]
  ------------------
  368|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode symbol bitmap refinement indicator");
  369|      0|                    goto cleanup2;
  370|      0|                }
  371|  10.1M|                if (code > 0) {
  ------------------
  |  Branch (371:21): [True: 18, False: 10.1M]
  ------------------
  372|     18|                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding symbol bitmap refinement indicator");
  373|     18|                    goto cleanup2;
  374|     18|                }
  375|  18.9M|            } else {
  376|  18.9M|                RI = 0;
  377|  18.9M|            }
  378|  29.1M|            if (RI) {
  ------------------
  |  Branch (378:17): [True: 503k, False: 28.6M]
  ------------------
  379|   503k|                Jbig2RefinementRegionParams rparams;
  380|   503k|                int32_t RDW, RDH, RDX, RDY;
  381|   503k|                size_t BMSIZE = 0;
  382|   503k|                int code1 = 0;
  383|   503k|                int code2 = 0;
  384|   503k|                int code3 = 0;
  385|   503k|                int code4 = 0;
  386|   503k|                int code5 = 0;
  387|   503k|                int code6 = 0;
  388|       |
  389|       |                /* 6.4.11 (1, 2, 3, 4) */
  390|   503k|                if (!params->SBHUFF) {
  ------------------
  |  Branch (390:21): [True: 483k, False: 19.9k]
  ------------------
  391|   483k|                    code1 = jbig2_arith_int_decode(ctx, params->IARDW, as, &RDW);
  392|   483k|                    code2 = jbig2_arith_int_decode(ctx, params->IARDH, as, &RDH);
  393|   483k|                    code3 = jbig2_arith_int_decode(ctx, params->IARDX, as, &RDX);
  394|   483k|                    code4 = jbig2_arith_int_decode(ctx, params->IARDY, as, &RDY);
  395|   483k|                } else {
  396|  19.9k|                    RDW = jbig2_huffman_get(hs, params->SBHUFFRDW, &code1);
  397|  19.9k|                    RDH = jbig2_huffman_get(hs, params->SBHUFFRDH, &code2);
  398|  19.9k|                    RDX = jbig2_huffman_get(hs, params->SBHUFFRDX, &code3);
  399|  19.9k|                    RDY = jbig2_huffman_get(hs, params->SBHUFFRDY, &code4);
  400|  19.9k|                    BMSIZE = jbig2_huffman_get(hs, params->SBHUFFRSIZE, &code5);
  401|  19.9k|                    code6 = jbig2_huffman_skip(hs);
  402|  19.9k|                }
  403|       |
  404|   503k|                if (code1 < 0 || code2 < 0 || code3 < 0 || code4 < 0 || code5 < 0 || code6 < 0) {
  ------------------
  |  Branch (404:21): [True: 3, False: 503k]
  |  Branch (404:34): [True: 1, False: 503k]
  |  Branch (404:47): [True: 1, False: 503k]
  |  Branch (404:60): [True: 1, False: 503k]
  |  Branch (404:73): [True: 1, False: 503k]
  |  Branch (404:86): [True: 0, False: 503k]
  ------------------
  405|      7|                    code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode data");
  406|      7|                    goto cleanup2;
  407|      7|                }
  408|   503k|                if (code1 > 0 || code2 > 0 || code3 > 0 || code4 > 0 || code5 > 0 || code6 > 0) {
  ------------------
  |  Branch (408:21): [True: 11, False: 503k]
  |  Branch (408:34): [True: 20, False: 503k]
  |  Branch (408:47): [True: 16, False: 503k]
  |  Branch (408:60): [True: 16, False: 503k]
  |  Branch (408:73): [True: 0, False: 503k]
  |  Branch (408:86): [True: 0, False: 503k]
  ------------------
  409|     63|                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding symbol instance refinement data");
  410|     63|                    goto cleanup2;
  411|     63|                }
  412|       |
  413|       |                /* 6.4.11 (6) */
  414|   503k|                if (IB) {
  ------------------
  |  Branch (414:21): [True: 94.2k, False: 408k]
  ------------------
  415|  94.2k|                    IBO = IB;
  416|  94.2k|                    IB = NULL;
  417|  94.2k|                    if (((int32_t) IBO->width) + RDW < 0 || ((int32_t) IBO->height) + RDH < 0) {
  ------------------
  |  Branch (417:25): [True: 35, False: 94.2k]
  |  Branch (417:61): [True: 9, False: 94.2k]
  ------------------
  418|     44|                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "reference image dimensions negative");
  419|     44|                        goto cleanup2;
  420|     44|                    }
  421|  94.2k|                    refimage = jbig2_image_new(ctx, IBO->width + RDW, IBO->height + RDH);
  422|  94.2k|                    if (refimage == NULL) {
  ------------------
  |  Branch (422:25): [True: 8, False: 94.2k]
  ------------------
  423|      8|                        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate reference image");
  424|      8|                        goto cleanup2;
  425|      8|                    }
  426|  94.2k|                    jbig2_image_clear(ctx, refimage, 0x00);
  427|       |
  428|       |                    /* Table 12 */
  429|  94.2k|                    rparams.GRTEMPLATE = params->SBRTEMPLATE;
  430|  94.2k|                    rparams.GRREFERENCE = IBO;
  431|  94.2k|                    rparams.GRREFERENCEDX = (RDW >> 1) + RDX;
  432|  94.2k|                    rparams.GRREFERENCEDY = (RDH >> 1) + RDY;
  433|  94.2k|                    rparams.TPGRON = 0;
  434|  94.2k|                    memcpy(rparams.grat, params->sbrat, 4);
  435|  94.2k|                    code = jbig2_decode_refinement_region(ctx, segment, &rparams, as, refimage, GR_stats);
  436|  94.2k|                    if (code < 0) {
  ------------------
  |  Branch (436:25): [True: 7, False: 94.2k]
  ------------------
  437|      7|                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
  438|      7|                        goto cleanup2;
  439|      7|                    }
  440|       |
  441|  94.2k|                    jbig2_image_release(ctx, IBO);
  442|  94.2k|                    IBO = NULL;
  443|  94.2k|                    IB = refimage;
  444|  94.2k|                    refimage = NULL;
  445|  94.2k|                }
  446|       |
  447|       |                /* 6.4.11 (7) */
  448|   503k|                if (params->SBHUFF) {
  ------------------
  |  Branch (448:21): [True: 19.9k, False: 483k]
  ------------------
  449|  19.9k|                    code = jbig2_huffman_advance(hs, BMSIZE);
  450|  19.9k|                    if (code < 0) {
  ------------------
  |  Branch (450:25): [True: 0, False: 19.9k]
  ------------------
  451|      0|                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to advance after huffman decoding refinement region");
  452|      0|                        goto cleanup2;
  453|      0|                    }
  454|  19.9k|                }
  455|   503k|            }
  456|       |
  457|       |            /* (3c.vi) */
  458|  29.1M|            if ((!params->TRANSPOSED) && (params->REFCORNER > 1) && IB) {
  ------------------
  |  Branch (458:17): [True: 27.4M, False: 1.64M]
  |  Branch (458:42): [True: 2.84M, False: 24.6M]
  |  Branch (458:69): [True: 375k, False: 2.47M]
  ------------------
  459|   375k|                CURS += IB->width - 1;
  460|  28.7M|            } else if ((params->TRANSPOSED) && !(params->REFCORNER & 1) && IB) {
  ------------------
  |  Branch (460:24): [True: 1.64M, False: 27.1M]
  |  Branch (460:48): [True: 1.19M, False: 450k]
  |  Branch (460:76): [True: 1.11M, False: 73.0k]
  ------------------
  461|  1.11M|                CURS += IB->height - 1;
  462|  1.11M|            }
  463|       |
  464|       |            /* (3c.vii) */
  465|  29.1M|            S = CURS;
  466|       |
  467|       |            /* (3c.viii) */
  468|  29.1M|            if (!params->TRANSPOSED) {
  ------------------
  |  Branch (468:17): [True: 27.4M, False: 1.64M]
  ------------------
  469|  27.4M|                switch (params->REFCORNER) {
  470|  14.8M|                case JBIG2_CORNER_TOPLEFT:
  ------------------
  |  Branch (470:17): [True: 14.8M, False: 12.6M]
  ------------------
  471|  14.8M|                    x = S;
  472|  14.8M|                    y = T;
  473|  14.8M|                    break;
  474|  2.43M|                case JBIG2_CORNER_TOPRIGHT:
  ------------------
  |  Branch (474:17): [True: 2.43M, False: 25.0M]
  ------------------
  475|  2.43M|                    if (IB)
  ------------------
  |  Branch (475:25): [True: 8.01k, False: 2.42M]
  ------------------
  476|  8.01k|                        x = S - IB->width + 1;
  477|  2.42M|                    else
  478|  2.42M|                        x = S + 1;
  479|  2.43M|                    y = T;
  480|  2.43M|                    break;
  481|  9.83M|                case JBIG2_CORNER_BOTTOMLEFT:
  ------------------
  |  Branch (481:17): [True: 9.83M, False: 17.6M]
  ------------------
  482|  9.83M|                    x = S;
  483|  9.83M|                    if (IB)
  ------------------
  |  Branch (483:25): [True: 7.68M, False: 2.15M]
  ------------------
  484|  7.68M|                        y = T - IB->height + 1;
  485|  2.15M|                    else
  486|  2.15M|                        y = T + 1;
  487|  9.83M|                    break;
  488|      0|                default:
  ------------------
  |  Branch (488:17): [True: 0, False: 27.4M]
  ------------------
  489|   409k|                case JBIG2_CORNER_BOTTOMRIGHT:
  ------------------
  |  Branch (489:17): [True: 409k, False: 27.0M]
  ------------------
  490|   409k|                    if (IB ) {
  ------------------
  |  Branch (490:25): [True: 367k, False: 41.5k]
  ------------------
  491|   367k|                        x = S - IB->width + 1;
  492|   367k|                        y = T - IB->height + 1;
  493|   367k|                    } else {
  494|  41.5k|                        x = S + 1;
  495|  41.5k|                        y = T + 1;
  496|  41.5k|                    }
  497|   409k|                    break;
  498|  27.4M|                }
  499|  27.4M|            } else {            /* TRANSPOSED */
  500|  1.64M|                switch (params->REFCORNER) {
  501|   370k|                case JBIG2_CORNER_TOPLEFT:
  ------------------
  |  Branch (501:17): [True: 370k, False: 1.27M]
  ------------------
  502|   370k|                    x = T;
  503|   370k|                    y = S;
  504|   370k|                    break;
  505|  80.1k|                case JBIG2_CORNER_TOPRIGHT:
  ------------------
  |  Branch (505:17): [True: 80.1k, False: 1.56M]
  ------------------
  506|  80.1k|                    if (IB)
  ------------------
  |  Branch (506:25): [True: 75.1k, False: 5.00k]
  ------------------
  507|  75.1k|                        x = T - IB->width + 1;
  508|  5.00k|                    else
  509|  5.00k|                        x = T + 1;
  510|  80.1k|                    y = S;
  511|  80.1k|                    break;
  512|  6.03k|                case JBIG2_CORNER_BOTTOMLEFT:
  ------------------
  |  Branch (512:17): [True: 6.03k, False: 1.63M]
  ------------------
  513|  6.03k|                    x = T;
  514|  6.03k|                    if (IB)
  ------------------
  |  Branch (514:25): [True: 1.81k, False: 4.21k]
  ------------------
  515|  1.81k|                        y = S - IB->height + 1;
  516|  4.21k|                    else
  517|  4.21k|                        y = S + 1;
  518|  6.03k|                    break;
  519|      0|                default:
  ------------------
  |  Branch (519:17): [True: 0, False: 1.64M]
  ------------------
  520|  1.18M|                case JBIG2_CORNER_BOTTOMRIGHT:
  ------------------
  |  Branch (520:17): [True: 1.18M, False: 456k]
  ------------------
  521|  1.18M|                    if (IB) {
  ------------------
  |  Branch (521:25): [True: 1.11M, False: 68.8k]
  ------------------
  522|  1.11M|                        x = T - IB->width + 1;
  523|  1.11M|                        y = S - IB->height + 1;
  524|  1.11M|                    } else {
  525|  68.8k|                        x = T + 1;
  526|  68.8k|                        y = S + 1;
  527|  68.8k|                    }
  528|  1.18M|                    break;
  529|  1.64M|                }
  530|  1.64M|            }
  531|       |
  532|       |            /* (3c.ix) */
  533|       |#ifdef JBIG2_DEBUG
  534|       |            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  535|       |                        "composing glyph ID %d: %dx%d @ (%d,%d) symbol %d/%d", ID, IB->width, IB->height, x, y, NINSTANCES + 1, params->SBNUMINSTANCES);
  536|       |#endif
  537|  29.1M|            code = jbig2_image_compose(ctx, image, IB, x, y, params->SBCOMBOP);
  538|  29.1M|            if (code < 0) {
  ------------------
  |  Branch (538:17): [True: 0, False: 29.1M]
  ------------------
  539|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to compose symbol instance symbol bitmap into picture");
  540|      0|                goto cleanup2;
  541|      0|            }
  542|       |
  543|       |            /* (3c.x) */
  544|  29.1M|            if (IB && (!params->TRANSPOSED) && (params->REFCORNER < 2)) {
  ------------------
  |  Branch (544:17): [True: 15.1M, False: 13.9M]
  |  Branch (544:23): [True: 13.5M, False: 1.55M]
  |  Branch (544:48): [True: 13.2M, False: 375k]
  ------------------
  545|  13.2M|                CURS += IB->width - 1;
  546|  15.9M|            } else if (IB && (params->TRANSPOSED) && (params->REFCORNER & 1)) {
  ------------------
  |  Branch (546:24): [True: 1.93M, False: 13.9M]
  |  Branch (546:30): [True: 1.55M, False: 375k]
  |  Branch (546:54): [True: 439k, False: 1.11M]
  ------------------
  547|   439k|                CURS += IB->height - 1;
  548|   439k|            }
  549|       |
  550|       |            /* (3c.xi) */
  551|  29.1M|            NINSTANCES++;
  552|       |
  553|  29.1M|            jbig2_image_release(ctx, IB);
  554|  29.1M|            IB = NULL;
  555|  29.1M|        }
  556|       |        /* end strip */
  557|  31.8k|    }
  558|       |    /* 6.4.5 (4) */
  559|       |
  560|  11.7k|cleanup2:
  561|  11.7k|    jbig2_image_release(ctx, refimage);
  562|  11.7k|    jbig2_image_release(ctx, IBO);
  563|  11.7k|    jbig2_image_release(ctx, IB);
  564|  11.7k|    if (params->SBHUFF) {
  ------------------
  |  Branch (564:9): [True: 3.06k, False: 8.66k]
  ------------------
  565|  3.06k|        jbig2_release_huffman_table(ctx, SBSYMCODES);
  566|  3.06k|    }
  567|  11.7k|    jbig2_huffman_free(ctx, hs);
  568|       |
  569|  11.7k|    return code;
  570|  11.7k|}
jbig2_text_region:
  577|  3.69k|{
  578|  3.69k|    uint32_t offset = 0;
  579|  3.69k|    Jbig2RegionSegmentInfo region_info;
  580|  3.69k|    Jbig2TextRegionParams params;
  581|  3.69k|    Jbig2Image *image = NULL;
  582|  3.69k|    Jbig2SymbolDict **dicts = NULL;
  583|  3.69k|    uint32_t n_dicts = 0;
  584|  3.69k|    uint16_t flags = 0;
  585|  3.69k|    uint16_t huffman_flags = 0;
  586|  3.69k|    Jbig2ArithCx *GR_stats = NULL;
  587|  3.69k|    int code = 0;
  588|  3.69k|    Jbig2WordStream *ws = NULL;
  589|  3.69k|    Jbig2ArithState *as = NULL;
  590|  3.69k|    uint32_t table_index = 0;
  591|  3.69k|    const Jbig2HuffmanParams *huffman_params = NULL;
  592|       |
  593|       |    /* zero params to ease cleanup later */
  594|  3.69k|    memset(&params, 0, sizeof(Jbig2TextRegionParams));
  595|       |
  596|       |    /* 7.4.1 */
  597|  3.69k|    if (segment->data_length < 17) {
  ------------------
  |  Branch (597:9): [True: 9, False: 3.68k]
  ------------------
  598|      9|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  599|      9|        goto cleanup2;
  600|      9|    }
  601|  3.68k|    jbig2_get_region_segment_info(&region_info, segment_data);
  602|  3.68k|    offset += 17;
  603|       |    /* Check for T.88 amendment 3 */
  604|  3.68k|    if (region_info.flags & 8)
  ------------------
  |  Branch (604:9): [True: 4, False: 3.68k]
  ------------------
  605|      4|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "region segment flags indicate use of colored bitmap (NYI)");
  606|       |
  607|       |    /* 7.4.3.1.1 */
  608|  3.68k|    if (segment->data_length - offset < 2) {
  ------------------
  |  Branch (608:9): [True: 1, False: 3.68k]
  ------------------
  609|      1|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  610|      1|        goto cleanup2;
  611|      1|    }
  612|  3.68k|    flags = jbig2_get_uint16(segment_data + offset);
  613|  3.68k|    offset += 2;
  614|       |
  615|  3.68k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "text region header flags 0x%04x", flags);
  616|       |
  617|  3.68k|    params.SBHUFF = flags & 0x0001;
  618|  3.68k|    params.SBREFINE = flags & 0x0002;
  619|  3.68k|    params.LOGSBSTRIPS = (flags & 0x000c) >> 2;
  620|  3.68k|    params.SBSTRIPS = 1 << params.LOGSBSTRIPS;
  621|  3.68k|    params.REFCORNER = (Jbig2RefCorner)((flags & 0x0030) >> 4);
  622|  3.68k|    params.TRANSPOSED = flags & 0x0040;
  623|  3.68k|    params.SBCOMBOP = (Jbig2ComposeOp)((flags & 0x0180) >> 7);
  624|  3.68k|    params.SBDEFPIXEL = flags & 0x0200;
  625|       |    /* SBDSOFFSET is a signed 5 bit integer */
  626|  3.68k|    params.SBDSOFFSET = (flags & 0x7C00) >> 10;
  627|  3.68k|    if (params.SBDSOFFSET > 0x0f)
  ------------------
  |  Branch (627:9): [True: 223, False: 3.45k]
  ------------------
  628|    223|        params.SBDSOFFSET -= 0x20;
  629|  3.68k|    params.SBRTEMPLATE = flags & 0x8000;
  630|       |
  631|  3.68k|    if (params.SBDSOFFSET) {
  ------------------
  |  Branch (631:9): [True: 2.64k, False: 1.03k]
  ------------------
  632|  2.64k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "text region has SBDSOFFSET %d", params.SBDSOFFSET);
  633|  2.64k|    }
  634|       |
  635|  3.68k|    if (params.SBHUFF) {        /* Huffman coding */
  ------------------
  |  Branch (635:9): [True: 321, False: 3.36k]
  ------------------
  636|       |        /* 7.4.3.1.2 */
  637|    321|        if (segment->data_length - offset < 2) {
  ------------------
  |  Branch (637:13): [True: 4, False: 317]
  ------------------
  638|      4|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  639|      4|            goto cleanup2;
  640|      4|        }
  641|    317|        huffman_flags = jbig2_get_uint16(segment_data + offset);
  642|    317|        offset += 2;
  643|       |
  644|    317|        if (huffman_flags & 0x8000)
  ------------------
  |  Branch (644:13): [True: 50, False: 267]
  ------------------
  645|     50|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved bit 15 of text region huffman flags is not zero");
  646|  3.36k|    } else {                    /* arithmetic coding */
  647|       |
  648|       |        /* 7.4.3.1.3 */
  649|  3.36k|        if (segment->data_length - offset < 4) {
  ------------------
  |  Branch (649:13): [True: 4, False: 3.35k]
  ------------------
  650|      4|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  651|      4|            goto cleanup2;
  652|      4|        }
  653|  3.35k|        if ((params.SBREFINE) && !(params.SBRTEMPLATE)) {
  ------------------
  |  Branch (653:13): [True: 170, False: 3.18k]
  |  Branch (653:34): [True: 68, False: 102]
  ------------------
  654|     68|            params.sbrat[0] = segment_data[offset];
  655|     68|            params.sbrat[1] = segment_data[offset + 1];
  656|     68|            params.sbrat[2] = segment_data[offset + 2];
  657|     68|            params.sbrat[3] = segment_data[offset + 3];
  658|     68|            offset += 4;
  659|     68|        }
  660|  3.35k|    }
  661|       |
  662|       |    /* 7.4.3.1.4 */
  663|  3.67k|    if (segment->data_length - offset < 4) {
  ------------------
  |  Branch (663:9): [True: 10, False: 3.66k]
  ------------------
  664|     10|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  665|     10|        goto cleanup2;
  666|     10|    }
  667|  3.66k|    params.SBNUMINSTANCES = jbig2_get_uint32(segment_data + offset);
  668|  3.66k|    offset += 4;
  669|       |
  670|  3.66k|    if (params.SBHUFF) {
  ------------------
  |  Branch (670:9): [True: 307, False: 3.35k]
  ------------------
  671|       |        /* 7.4.3.1.5 - Symbol ID Huffman table */
  672|       |        /* ...this is handled in the segment body decoder */
  673|       |
  674|       |        /* 7.4.3.1.6 - Other Huffman table selection */
  675|    307|        switch (huffman_flags & 0x0003) {
  676|    219|        case 0:                /* Table B.6 */
  ------------------
  |  Branch (676:9): [True: 219, False: 88]
  ------------------
  677|    219|            params.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_F);
  678|    219|            break;
  679|     66|        case 1:                /* Table B.7 */
  ------------------
  |  Branch (679:9): [True: 66, False: 241]
  ------------------
  680|     66|            params.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_G);
  681|     66|            break;
  682|     19|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (682:9): [True: 19, False: 288]
  ------------------
  683|     19|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  684|     19|            if (huffman_params == NULL) {
  ------------------
  |  Branch (684:17): [True: 6, False: 13]
  ------------------
  685|      6|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom FS huffman table not found (%d)", table_index);
  686|      6|                goto cleanup1;
  687|      6|            }
  688|     13|            params.SBHUFFFS = jbig2_build_huffman_table(ctx, huffman_params);
  689|     13|            ++table_index;
  690|     13|            break;
  691|      3|        case 2:                /* invalid */
  ------------------
  |  Branch (691:9): [True: 3, False: 304]
  ------------------
  692|      3|        default:
  ------------------
  |  Branch (692:9): [True: 0, False: 307]
  ------------------
  693|      3|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid FS huffman table");
  694|      3|            goto cleanup1;
  695|      0|            break;
  696|    307|        }
  697|    298|        if (params.SBHUFFFS == NULL) {
  ------------------
  |  Branch (697:13): [True: 1, False: 297]
  ------------------
  698|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified FS huffman table");
  699|      1|            goto cleanup1;
  700|      1|        }
  701|       |
  702|    297|        switch ((huffman_flags & 0x000c) >> 2) {
  ------------------
  |  Branch (702:17): [True: 297, False: 0]
  ------------------
  703|    177|        case 0:                /* Table B.8 */
  ------------------
  |  Branch (703:9): [True: 177, False: 120]
  ------------------
  704|    177|            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_H);
  705|    177|            break;
  706|     75|        case 1:                /* Table B.9 */
  ------------------
  |  Branch (706:9): [True: 75, False: 222]
  ------------------
  707|     75|            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_I);
  708|     75|            break;
  709|     28|        case 2:                /* Table B.10 */
  ------------------
  |  Branch (709:9): [True: 28, False: 269]
  ------------------
  710|     28|            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_J);
  711|     28|            break;
  712|     17|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (712:9): [True: 17, False: 280]
  ------------------
  713|     17|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  714|     17|            if (huffman_params == NULL) {
  ------------------
  |  Branch (714:17): [True: 2, False: 15]
  ------------------
  715|      2|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DS huffman table not found (%d)", table_index);
  716|      2|                goto cleanup1;
  717|      2|            }
  718|     15|            params.SBHUFFDS = jbig2_build_huffman_table(ctx, huffman_params);
  719|     15|            ++table_index;
  720|     15|            break;
  721|    297|        }
  722|    295|        if (params.SBHUFFDS == NULL) {
  ------------------
  |  Branch (722:13): [True: 3, False: 292]
  ------------------
  723|      3|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified DS huffman table");
  724|      3|            goto cleanup1;
  725|      3|        }
  726|       |
  727|    292|        switch ((huffman_flags & 0x0030) >> 4) {
  ------------------
  |  Branch (727:17): [True: 292, False: 0]
  ------------------
  728|     50|        case 0:                /* Table B.11 */
  ------------------
  |  Branch (728:9): [True: 50, False: 242]
  ------------------
  729|     50|            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_K);
  730|     50|            break;
  731|    127|        case 1:                /* Table B.12 */
  ------------------
  |  Branch (731:9): [True: 127, False: 165]
  ------------------
  732|    127|            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_L);
  733|    127|            break;
  734|     99|        case 2:                /* Table B.13 */
  ------------------
  |  Branch (734:9): [True: 99, False: 193]
  ------------------
  735|     99|            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_M);
  736|     99|            break;
  737|     16|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (737:9): [True: 16, False: 276]
  ------------------
  738|     16|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  739|     16|            if (huffman_params == NULL) {
  ------------------
  |  Branch (739:17): [True: 1, False: 15]
  ------------------
  740|      1|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DT huffman table not found (%d)", table_index);
  741|      1|                goto cleanup1;
  742|      1|            }
  743|     15|            params.SBHUFFDT = jbig2_build_huffman_table(ctx, huffman_params);
  744|     15|            ++table_index;
  745|     15|            break;
  746|    292|        }
  747|    291|        if (params.SBHUFFDT == NULL) {
  ------------------
  |  Branch (747:13): [True: 1, False: 290]
  ------------------
  748|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified DT huffman table");
  749|      1|            goto cleanup1;
  750|      1|        }
  751|       |
  752|    290|        switch ((huffman_flags & 0x00c0) >> 6) {
  753|    229|        case 0:                /* Table B.14 */
  ------------------
  |  Branch (753:9): [True: 229, False: 61]
  ------------------
  754|    229|            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
  755|    229|            break;
  756|     28|        case 1:                /* Table B.15 */
  ------------------
  |  Branch (756:9): [True: 28, False: 262]
  ------------------
  757|     28|            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
  758|     28|            break;
  759|     32|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (759:9): [True: 32, False: 258]
  ------------------
  760|     32|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  761|     32|            if (huffman_params == NULL) {
  ------------------
  |  Branch (761:17): [True: 2, False: 30]
  ------------------
  762|      2|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RDW huffman table not found (%d)", table_index);
  763|      2|                goto cleanup1;
  764|      2|            }
  765|     30|            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, huffman_params);
  766|     30|            ++table_index;
  767|     30|            break;
  768|      1|        case 2:                /* invalid */
  ------------------
  |  Branch (768:9): [True: 1, False: 289]
  ------------------
  769|      1|        default:
  ------------------
  |  Branch (769:9): [True: 0, False: 290]
  ------------------
  770|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDW huffman table");
  771|      1|            goto cleanup1;
  772|      0|            break;
  773|    290|        }
  774|    287|        if (params.SBHUFFRDW == NULL) {
  ------------------
  |  Branch (774:13): [True: 1, False: 286]
  ------------------
  775|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RDW huffman table");
  776|      1|            goto cleanup1;
  777|      1|        }
  778|       |
  779|    286|        switch ((huffman_flags & 0x0300) >> 8) {
  780|    144|        case 0:                /* Table B.14 */
  ------------------
  |  Branch (780:9): [True: 144, False: 142]
  ------------------
  781|    144|            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
  782|    144|            break;
  783|    133|        case 1:                /* Table B.15 */
  ------------------
  |  Branch (783:9): [True: 133, False: 153]
  ------------------
  784|    133|            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
  785|    133|            break;
  786|      7|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (786:9): [True: 7, False: 279]
  ------------------
  787|      7|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  788|      7|            if (huffman_params == NULL) {
  ------------------
  |  Branch (788:17): [True: 2, False: 5]
  ------------------
  789|      2|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RDH huffman table not found (%d)", table_index);
  790|      2|                goto cleanup1;
  791|      2|            }
  792|      5|            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, huffman_params);
  793|      5|            ++table_index;
  794|      5|            break;
  795|      2|        case 2:                /* invalid */
  ------------------
  |  Branch (795:9): [True: 2, False: 284]
  ------------------
  796|      2|        default:
  ------------------
  |  Branch (796:9): [True: 0, False: 286]
  ------------------
  797|      2|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDH huffman table");
  798|      2|            goto cleanup1;
  799|      0|            break;
  800|    286|        }
  801|    282|        if (params.SBHUFFRDH == NULL) {
  ------------------
  |  Branch (801:13): [True: 1, False: 281]
  ------------------
  802|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RDH huffman table");
  803|      1|            goto cleanup1;
  804|      1|        }
  805|       |
  806|    281|        switch ((huffman_flags & 0x0c00) >> 10) {
  807|    207|        case 0:                /* Table B.14 */
  ------------------
  |  Branch (807:9): [True: 207, False: 74]
  ------------------
  808|    207|            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
  809|    207|            break;
  810|     64|        case 1:                /* Table B.15 */
  ------------------
  |  Branch (810:9): [True: 64, False: 217]
  ------------------
  811|     64|            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
  812|     64|            break;
  813|      9|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (813:9): [True: 9, False: 272]
  ------------------
  814|      9|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  815|      9|            if (huffman_params == NULL) {
  ------------------
  |  Branch (815:17): [True: 3, False: 6]
  ------------------
  816|      3|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RDX huffman table not found (%d)", table_index);
  817|      3|                goto cleanup1;
  818|      3|            }
  819|      6|            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, huffman_params);
  820|      6|            ++table_index;
  821|      6|            break;
  822|      1|        case 2:                /* invalid */
  ------------------
  |  Branch (822:9): [True: 1, False: 280]
  ------------------
  823|      1|        default:
  ------------------
  |  Branch (823:9): [True: 0, False: 281]
  ------------------
  824|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDX huffman table");
  825|      1|            goto cleanup1;
  826|      0|            break;
  827|    281|        }
  828|    277|        if (params.SBHUFFRDX == NULL) {
  ------------------
  |  Branch (828:13): [True: 1, False: 276]
  ------------------
  829|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RDX huffman table");
  830|      1|            goto cleanup1;
  831|      1|        }
  832|       |
  833|    276|        switch ((huffman_flags & 0x3000) >> 12) {
  834|    224|        case 0:                /* Table B.14 */
  ------------------
  |  Branch (834:9): [True: 224, False: 52]
  ------------------
  835|    224|            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
  836|    224|            break;
  837|     38|        case 1:                /* Table B.15 */
  ------------------
  |  Branch (837:9): [True: 38, False: 238]
  ------------------
  838|     38|            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
  839|     38|            break;
  840|     13|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (840:9): [True: 13, False: 263]
  ------------------
  841|     13|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  842|     13|            if (huffman_params == NULL) {
  ------------------
  |  Branch (842:17): [True: 5, False: 8]
  ------------------
  843|      5|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RDY huffman table not found (%d)", table_index);
  844|      5|                goto cleanup1;
  845|      5|            }
  846|      8|            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, huffman_params);
  847|      8|            ++table_index;
  848|      8|            break;
  849|      1|        case 2:                /* invalid */
  ------------------
  |  Branch (849:9): [True: 1, False: 275]
  ------------------
  850|      1|        default:
  ------------------
  |  Branch (850:9): [True: 0, False: 276]
  ------------------
  851|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDY huffman table");
  852|      1|            goto cleanup1;
  853|      0|            break;
  854|    276|        }
  855|    270|        if (params.SBHUFFRDY == NULL) {
  ------------------
  |  Branch (855:13): [True: 1, False: 269]
  ------------------
  856|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RDY huffman table");
  857|      1|            goto cleanup1;
  858|      1|        }
  859|       |
  860|    269|        switch ((huffman_flags & 0x4000) >> 14) {
  ------------------
  |  Branch (860:17): [True: 269, False: 0]
  ------------------
  861|    261|        case 0:                /* Table B.1 */
  ------------------
  |  Branch (861:9): [True: 261, False: 8]
  ------------------
  862|    261|            params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
  863|    261|            break;
  864|      8|        case 1:                /* Custom table from referred segment */
  ------------------
  |  Branch (864:9): [True: 8, False: 261]
  ------------------
  865|      8|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  866|      8|            if (huffman_params == NULL) {
  ------------------
  |  Branch (866:17): [True: 3, False: 5]
  ------------------
  867|      3|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RSIZE huffman table not found (%d)", table_index);
  868|      3|                goto cleanup1;
  869|      3|            }
  870|      5|            params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, huffman_params);
  871|      5|            ++table_index;
  872|      5|            break;
  873|    269|        }
  874|    266|        if (params.SBHUFFRSIZE == NULL) {
  ------------------
  |  Branch (874:13): [True: 1, False: 265]
  ------------------
  875|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RSIZE huffman table");
  876|      1|            goto cleanup1;
  877|      1|        }
  878|       |
  879|    265|        if (huffman_flags & 0x8000) {
  ------------------
  |  Branch (879:13): [True: 22, False: 243]
  ------------------
  880|     22|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "text region huffman flags bit 15 is set, contrary to spec");
  881|     22|        }
  882|       |
  883|       |        /* 7.4.3.1.7 */
  884|       |        /* For convenience this is done in the body decoder routine */
  885|    265|    }
  886|       |
  887|  3.62k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  888|  3.62k|                "text region: %d x %d @ (%d,%d) %d symbols", region_info.width, region_info.height, region_info.x, region_info.y, params.SBNUMINSTANCES);
  889|       |
  890|       |    /* 7.4.3.2 (2) - compose the list of symbol dictionaries */
  891|  3.62k|    n_dicts = jbig2_sd_count_referred(ctx, segment);
  892|  3.62k|    if (n_dicts == 0) {
  ------------------
  |  Branch (892:9): [True: 729, False: 2.89k]
  ------------------
  893|    729|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "text region refers to no symbol dictionaries");
  894|  2.89k|    } else {
  895|  2.89k|        dicts = jbig2_sd_list_referred(ctx, segment);
  896|  2.89k|        if (dicts == NULL) {
  ------------------
  |  Branch (896:13): [True: 1, False: 2.89k]
  ------------------
  897|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unable to retrieve symbol dictionaries! previous parsing error?");
  898|      1|            goto cleanup1;
  899|  2.89k|        } else {
  900|  2.89k|            uint32_t index;
  901|       |
  902|  2.89k|            if (dicts[0] == NULL) {
  ------------------
  |  Branch (902:17): [True: 0, False: 2.89k]
  ------------------
  903|      0|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to find first referenced symbol dictionary");
  904|      0|                goto cleanup1;
  905|      0|            }
  906|  8.42k|            for (index = 1; index < n_dicts; index++)
  ------------------
  |  Branch (906:29): [True: 5.53k, False: 2.89k]
  ------------------
  907|  5.53k|                if (dicts[index] == NULL) {
  ------------------
  |  Branch (907:21): [True: 0, False: 5.53k]
  ------------------
  908|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to find all referenced symbol dictionaries");
  909|      0|                    n_dicts = index;
  910|      0|                }
  911|  2.89k|        }
  912|  2.89k|    }
  913|       |
  914|       |    /* 7.4.3.2 (3) */
  915|  3.62k|    {
  916|  3.62k|        int stats_size = params.SBRTEMPLATE ? 1 << 10 : 1 << 13;
  ------------------
  |  Branch (916:26): [True: 230, False: 3.39k]
  ------------------
  917|       |
  918|  3.62k|        GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  3.62k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  919|  3.62k|        if (GR_stats == NULL) {
  ------------------
  |  Branch (919:13): [True: 1, False: 3.62k]
  ------------------
  920|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "could not allocate arithmetic decoder state");
  921|      1|            goto cleanup1;
  922|      1|        }
  923|  3.62k|        memset(GR_stats, 0, stats_size);
  924|  3.62k|    }
  925|       |
  926|      0|    image = jbig2_image_new(ctx, region_info.width, region_info.height);
  927|  3.62k|    if (image == NULL) {
  ------------------
  |  Branch (927:9): [True: 34, False: 3.58k]
  ------------------
  928|     34|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region image");
  929|     34|        goto cleanup2;
  930|     34|    }
  931|       |
  932|  3.58k|    if (offset >= segment->data_length) {
  ------------------
  |  Branch (932:9): [True: 2, False: 3.58k]
  ------------------
  933|      2|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  934|      2|        goto cleanup2;
  935|      2|    }
  936|  3.58k|    ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
  937|  3.58k|    if (ws == NULL) {
  ------------------
  |  Branch (937:9): [True: 1, False: 3.58k]
  ------------------
  938|      1|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate word stream when handling text region image");
  939|      1|        goto cleanup2;
  940|      1|    }
  941|       |
  942|  3.58k|    as = jbig2_arith_new(ctx, ws);
  943|  3.58k|    if (as == NULL) {
  ------------------
  |  Branch (943:9): [True: 1, False: 3.58k]
  ------------------
  944|      1|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding context when handling text region image");
  945|      1|        goto cleanup3;
  946|      1|    }
  947|       |
  948|  3.58k|    if (!params.SBHUFF) {
  ------------------
  |  Branch (948:9): [True: 3.32k, False: 260]
  ------------------
  949|  3.32k|        uint8_t SBSYMCODELEN;
  950|  3.32k|        uint32_t index;
  951|  3.32k|        uint32_t SBNUMSYMS = 0;
  952|       |
  953|  10.4k|        for (index = 0; index < n_dicts; index++) {
  ------------------
  |  Branch (953:25): [True: 7.16k, False: 3.32k]
  ------------------
  954|  7.16k|            SBNUMSYMS += dicts[index]->n_symbols;
  955|  7.16k|        }
  956|       |
  957|  3.32k|        params.IADT = jbig2_arith_int_ctx_new(ctx);
  958|  3.32k|        params.IAFS = jbig2_arith_int_ctx_new(ctx);
  959|  3.32k|        params.IADS = jbig2_arith_int_ctx_new(ctx);
  960|  3.32k|        params.IAIT = jbig2_arith_int_ctx_new(ctx);
  961|  3.32k|        if (params.IADT == NULL || params.IAFS == NULL || params.IADS == NULL || params.IAIT == NULL) {
  ------------------
  |  Branch (961:13): [True: 1, False: 3.32k]
  |  Branch (961:36): [True: 1, False: 3.32k]
  |  Branch (961:59): [True: 1, False: 3.31k]
  |  Branch (961:82): [True: 2, False: 3.31k]
  ------------------
  962|      5|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region image data");
  963|      5|            goto cleanup4;
  964|      5|        }
  965|       |
  966|       |        /* Table 31 */
  967|  7.26k|        for (SBSYMCODELEN = 0; ((uint64_t) 1 << SBSYMCODELEN) < (uint64_t) SBNUMSYMS; SBSYMCODELEN++);
  ------------------
  |  Branch (967:32): [True: 3.94k, False: 3.31k]
  ------------------
  968|       |
  969|  3.31k|        params.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
  970|  3.31k|        params.IARI = jbig2_arith_int_ctx_new(ctx);
  971|  3.31k|        params.IARDW = jbig2_arith_int_ctx_new(ctx);
  972|  3.31k|        params.IARDH = jbig2_arith_int_ctx_new(ctx);
  973|  3.31k|        params.IARDX = jbig2_arith_int_ctx_new(ctx);
  974|  3.31k|        params.IARDY = jbig2_arith_int_ctx_new(ctx);
  975|  3.31k|        if (params.IAID == NULL || params.IARI == NULL ||
  ------------------
  |  Branch (975:13): [True: 3, False: 3.31k]
  |  Branch (975:36): [True: 1, False: 3.31k]
  ------------------
  976|  3.31k|            params.IARDW == NULL || params.IARDH == NULL || params.IARDX == NULL || params.IARDY == NULL) {
  ------------------
  |  Branch (976:13): [True: 1, False: 3.31k]
  |  Branch (976:37): [True: 1, False: 3.31k]
  |  Branch (976:61): [True: 1, False: 3.31k]
  |  Branch (976:85): [True: 1, False: 3.30k]
  ------------------
  977|      8|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region image data");
  978|      8|            goto cleanup5;
  979|      8|        }
  980|  3.31k|    }
  981|       |
  982|  3.56k|    code = jbig2_decode_text_region(ctx, segment, &params,
  983|  3.56k|                                    (const Jbig2SymbolDict * const *)dicts, n_dicts, image, GR_stats, as, ws);
  984|  3.56k|    if (code < 0) {
  ------------------
  |  Branch (984:9): [True: 275, False: 3.29k]
  ------------------
  985|    275|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode text region image data");
  986|    275|        goto cleanup5;
  987|    275|    }
  988|       |
  989|  3.29k|    if ((segment->flags & 63) == 4) {
  ------------------
  |  Branch (989:9): [True: 146, False: 3.14k]
  ------------------
  990|       |        /* we have an intermediate region here. save it for later */
  991|    146|        segment->result = jbig2_image_reference(ctx, image);
  992|  3.14k|    } else {
  993|       |        /* otherwise composite onto the page */
  994|  3.14k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  995|  3.14k|                    "composing %dx%d decoded text region onto page at (%d, %d)", region_info.width, region_info.height, region_info.x, region_info.y);
  996|  3.14k|        code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, region_info.x, region_info.y, region_info.op);
  997|  3.14k|        if (code < 0)
  ------------------
  |  Branch (997:13): [True: 14, False: 3.13k]
  ------------------
  998|     14|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add text region to page");
  999|  3.14k|    }
 1000|       |
 1001|  3.57k|cleanup5:
 1002|  3.57k|    if (!params.SBHUFF) {
  ------------------
  |  Branch (1002:9): [True: 3.31k, False: 260]
  ------------------
 1003|  3.31k|        jbig2_arith_iaid_ctx_free(ctx, params.IAID);
 1004|  3.31k|        jbig2_arith_int_ctx_free(ctx, params.IARI);
 1005|  3.31k|        jbig2_arith_int_ctx_free(ctx, params.IARDW);
 1006|  3.31k|        jbig2_arith_int_ctx_free(ctx, params.IARDH);
 1007|  3.31k|        jbig2_arith_int_ctx_free(ctx, params.IARDX);
 1008|  3.31k|        jbig2_arith_int_ctx_free(ctx, params.IARDY);
 1009|  3.31k|    }
 1010|       |
 1011|  3.58k|cleanup4:
 1012|  3.58k|    if (!params.SBHUFF) {
  ------------------
  |  Branch (1012:9): [True: 3.32k, False: 260]
  ------------------
 1013|  3.32k|        jbig2_arith_int_ctx_free(ctx, params.IADT);
 1014|  3.32k|        jbig2_arith_int_ctx_free(ctx, params.IAFS);
 1015|  3.32k|        jbig2_arith_int_ctx_free(ctx, params.IADS);
 1016|  3.32k|        jbig2_arith_int_ctx_free(ctx, params.IAIT);
 1017|  3.32k|    }
 1018|       |
 1019|  3.58k|cleanup3:
 1020|  3.58k|    jbig2_free(ctx->allocator, as);
 1021|  3.58k|    jbig2_word_stream_buf_free(ctx, ws);
 1022|       |
 1023|  3.64k|cleanup2:
 1024|  3.64k|    jbig2_free(ctx->allocator, GR_stats);
 1025|  3.64k|    jbig2_image_release(ctx, image);
 1026|       |
 1027|  3.69k|cleanup1:
 1028|  3.69k|    if (params.SBHUFF) {
  ------------------
  |  Branch (1028:9): [True: 321, False: 3.37k]
  ------------------
 1029|    321|        jbig2_release_huffman_table(ctx, params.SBHUFFFS);
 1030|    321|        jbig2_release_huffman_table(ctx, params.SBHUFFDS);
 1031|    321|        jbig2_release_huffman_table(ctx, params.SBHUFFDT);
 1032|    321|        jbig2_release_huffman_table(ctx, params.SBHUFFRDX);
 1033|    321|        jbig2_release_huffman_table(ctx, params.SBHUFFRDY);
 1034|    321|        jbig2_release_huffman_table(ctx, params.SBHUFFRDW);
 1035|    321|        jbig2_release_huffman_table(ctx, params.SBHUFFRDH);
 1036|    321|        jbig2_release_huffman_table(ctx, params.SBHUFFRSIZE);
 1037|    321|    }
 1038|  3.69k|    jbig2_free(ctx->allocator, dicts);
 1039|       |
 1040|  3.69k|    return code;
 1041|  3.64k|}

