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

jbig2_alloc:
   66|  41.7M|{
   67|       |    /* Check for integer multiplication overflow when computing
   68|       |    the full size of the allocation. */
   69|  41.7M|    if (num > 0 && size > SIZE_MAX / num)
  ------------------
  |  Branch (69:9): [True: 41.7M, False: 5.42k]
  |  Branch (69:20): [True: 0, False: 41.7M]
  ------------------
   70|      0|        return NULL;
   71|  41.7M|    return allocator->alloc(allocator, size * num);
   72|  41.7M|}
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.10k|{
  111|  6.10k|    Jbig2Ctx *result;
  112|       |
  113|  6.10k|    if (jbig2_version_major != JBIG2_VERSION_MAJOR || jbig2_version_minor != JBIG2_VERSION_MINOR) {
  ------------------
  |  |   28|  12.2k|#define JBIG2_VERSION_MAJOR (0)
  ------------------
                  if (jbig2_version_major != JBIG2_VERSION_MAJOR || jbig2_version_minor != JBIG2_VERSION_MINOR) {
  ------------------
  |  |   29|  6.10k|#define JBIG2_VERSION_MINOR (20)
  ------------------
  |  Branch (113:9): [True: 0, False: 6.10k]
  |  Branch (113:55): [True: 0, False: 6.10k]
  ------------------
  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.10k|    if (allocator == NULL)
  ------------------
  |  Branch (122:9): [True: 0, False: 6.10k]
  ------------------
  123|      0|        allocator = &jbig2_default_allocator;
  124|  6.10k|    if (error_callback == NULL)
  ------------------
  |  Branch (124:9): [True: 6.10k, False: 0]
  ------------------
  125|  6.10k|        error_callback = &jbig2_default_error;
  126|       |
  127|  6.10k|    result = (Jbig2Ctx *) jbig2_alloc(allocator, sizeof(Jbig2Ctx), 1);
  128|  6.10k|    if (result == NULL) {
  ------------------
  |  Branch (128:9): [True: 0, False: 6.10k]
  ------------------
  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.10k|    result->allocator = allocator;
  134|  6.10k|    result->options = options;
  135|  6.10k|    result->global_ctx = (const Jbig2Ctx *)global_ctx;
  136|  6.10k|    result->error_callback = error_callback;
  137|  6.10k|    result->error_callback_data = error_callback_data;
  138|       |
  139|  6.10k|    result->state = (options & JBIG2_OPTIONS_EMBEDDED) ? JBIG2_FILE_HEADER_MAYBE : JBIG2_FILE_HEADER;
  ------------------
  |  Branch (139:21): [True: 0, False: 6.10k]
  ------------------
  140|       |
  141|  6.10k|    result->buf = NULL;
  142|       |
  143|  6.10k|    result->n_segments = 0;
  144|  6.10k|    result->n_segments_max = 16;
  145|  6.10k|    result->segments = jbig2_new(result, Jbig2Segment *, result->n_segments_max);
  ------------------
  |  |  141|  6.10k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  146|  6.10k|    if (result->segments == NULL) {
  ------------------
  |  Branch (146:9): [True: 0, False: 6.10k]
  ------------------
  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.10k|    result->segment_index = 0;
  152|       |
  153|  6.10k|    result->current_page = 0;
  154|  6.10k|    result->max_page_index = 4;
  155|  6.10k|    result->pages = jbig2_new(result, Jbig2Page, result->max_page_index);
  ------------------
  |  |  141|  6.10k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  156|  6.10k|    if (result->pages == NULL) {
  ------------------
  |  Branch (156:9): [True: 0, False: 6.10k]
  ------------------
  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.10k|    {
  163|  6.10k|        uint32_t index;
  164|       |
  165|  30.5k|        for (index = 0; index < result->max_page_index; index++) {
  ------------------
  |  Branch (165:25): [True: 24.4k, False: 6.10k]
  ------------------
  166|  24.4k|            result->pages[index].state = JBIG2_PAGE_FREE;
  167|  24.4k|            result->pages[index].number = 0;
  168|  24.4k|            result->pages[index].width = 0;
  169|  24.4k|            result->pages[index].height = 0xffffffff;
  170|  24.4k|            result->pages[index].x_resolution = 0;
  171|  24.4k|            result->pages[index].y_resolution = 0;
  172|  24.4k|            result->pages[index].stripe_size = 0;
  173|  24.4k|            result->pages[index].striped = 0;
  174|  24.4k|            result->pages[index].end_row = 0;
  175|  24.4k|            result->pages[index].flags = 0;
  176|  24.4k|            result->pages[index].image = NULL;
  177|  24.4k|        }
  178|  6.10k|    }
  179|       |
  180|  6.10k|    return result;
  181|  6.10k|}
jbig2_get_int16:
  192|  5.57k|{
  193|  5.57k|    return get_int16(bptr);
  ------------------
  |  |  186|  5.57k|    (((int)get_uint16(bptr) ^ 0x8000) - 0x8000)
  |  |  ------------------
  |  |  |  |  184|  5.57k|    (((bptr)[0] << 8) | (bptr)[1])
  |  |  ------------------
  ------------------
  194|  5.57k|}
jbig2_get_uint16:
  200|  23.5k|{
  201|  23.5k|    return get_uint16(bptr);
  ------------------
  |  |  184|  23.5k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
  202|  23.5k|}
jbig2_get_int32:
  208|  7.76k|{
  209|  7.76k|    return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  186|  7.76k|    (((int)get_uint16(bptr) ^ 0x8000) - 0x8000)
  |  |  ------------------
  |  |  |  |  184|  7.76k|    (((bptr)[0] << 8) | (bptr)[1])
  |  |  ------------------
  ------------------
                  return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  184|  7.76k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
  210|  7.76k|}
jbig2_get_uint32:
  216|   404k|{
  217|   404k|    return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  184|   404k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
                  return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  184|   404k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
  218|   404k|}
jbig2_data_in:
  250|  6.10k|{
  251|  6.10k|    if (ctx->buf == NULL) {
  ------------------
  |  Branch (251:9): [True: 6.10k, False: 0]
  ------------------
  252|  6.10k|        size_t buf_size = jbig2_find_buffer_size(size);
  253|  6.10k|        ctx->buf = jbig2_new(ctx, byte, buf_size);
  ------------------
  |  |  141|  6.10k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  254|  6.10k|        if (ctx->buf == NULL) {
  ------------------
  |  Branch (254:13): [True: 0, False: 6.10k]
  ------------------
  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.10k|        ctx->buf_size = buf_size;
  258|  6.10k|        ctx->buf_rd_ix = 0;
  259|  6.10k|        ctx->buf_wr_ix = 0;
  260|  6.10k|    } 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.10k|    memcpy(ctx->buf + ctx->buf_wr_ix, data, size);
  289|  6.10k|    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.10k|        case JBIG2_FILE_HEADER:
  ------------------
  |  Branch (312:9): [True: 6.10k, False: 135k]
  ------------------
  313|       |            /* D.4.1 */
  314|  6.10k|            if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9)
  ------------------
  |  Branch (314:17): [True: 6, False: 6.09k]
  ------------------
  315|      6|                return 0;
  316|  6.09k|            if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8))
  ------------------
  |  Branch (316:17): [True: 69, False: 6.02k]
  ------------------
  317|     69|                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "not a JBIG2 file header");
  ------------------
  |  |   68|     69|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  318|       |            /* D.4.2 */
  319|  6.02k|            ctx->file_header_flags = ctx->buf[ctx->buf_rd_ix + 8];
  320|       |            /* Check for T.88 amendment 2 */
  321|  6.02k|            if (ctx->file_header_flags & 0x04)
  ------------------
  |  Branch (321:17): [True: 1, False: 6.02k]
  ------------------
  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.02k|            if (ctx->file_header_flags & 0x08)
  ------------------
  |  Branch (324:17): [True: 1, False: 6.02k]
  ------------------
  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.02k|            if (ctx->file_header_flags & 0xFC) {
  ------------------
  |  Branch (326:17): [True: 1.44k, False: 4.58k]
  ------------------
  327|  1.44k|                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.44k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  328|  1.44k|            }
  329|       |            /* D.4.3 */
  330|  6.02k|            if (!(ctx->file_header_flags & 2)) {        /* number of pages is known */
  ------------------
  |  Branch (330:17): [True: 2.25k, False: 3.77k]
  ------------------
  331|  2.25k|                if (ctx->buf_wr_ix - ctx->buf_rd_ix < 13)
  ------------------
  |  Branch (331:21): [True: 7, False: 2.24k]
  ------------------
  332|      7|                    return 0;
  333|  2.24k|                ctx->n_pages = jbig2_get_uint32(ctx->buf + ctx->buf_rd_ix + 9);
  334|  2.24k|                ctx->buf_rd_ix += 13;
  335|  2.24k|                if (ctx->n_pages == 1)
  ------------------
  |  Branch (335:21): [True: 53, False: 2.19k]
  ------------------
  336|     53|                    jbig2_error(ctx, JBIG2_SEVERITY_INFO, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates a single page document");
  ------------------
  |  |   68|     53|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  337|  2.19k|                else
  338|  2.19k|                    jbig2_error(ctx, JBIG2_SEVERITY_INFO, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates a %d page document", ctx->n_pages);
  ------------------
  |  |   68|  2.19k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  339|  3.77k|            } else {            /* number of pages not known */
  340|  3.77k|                ctx->n_pages = 0;
  341|  3.77k|                ctx->buf_rd_ix += 9;
  342|  3.77k|            }
  343|       |            /* determine the file organization based on the flags - D.4.2 again */
  344|  6.01k|            if (ctx->file_header_flags & 1) {
  ------------------
  |  Branch (344:17): [True: 5.19k, False: 827]
  ------------------
  345|  5.19k|                ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  346|  5.19k|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates sequential organization");
  ------------------
  |  |   68|  5.19k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  347|  5.19k|            } else {
  348|    827|                ctx->state = JBIG2_FILE_RANDOM_HEADERS;
  349|    827|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates random-access organization");
  ------------------
  |  |   68|    827|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  350|    827|            }
  351|  6.01k|            break;
  352|  46.3k|        case JBIG2_FILE_SEQUENTIAL_HEADER:
  ------------------
  |  Branch (352:9): [True: 46.3k, False: 95.1k]
  ------------------
  353|  89.2k|        case JBIG2_FILE_RANDOM_HEADERS:
  ------------------
  |  Branch (353:9): [True: 42.8k, False: 98.7k]
  ------------------
  354|  89.2k|            segment = jbig2_parse_segment_header(ctx, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix, &header_size);
  355|  89.2k|            if (segment == NULL)
  ------------------
  |  Branch (355:17): [True: 1.20k, False: 87.9k]
  ------------------
  356|  1.20k|                return 0; /* need more data */
  357|  87.9k|            ctx->buf_rd_ix += header_size;
  358|       |
  359|  87.9k|            if (ctx->n_segments >= ctx->n_segments_max) {
  ------------------
  |  Branch (359:17): [True: 610, False: 87.3k]
  ------------------
  360|    610|                Jbig2Segment **segments;
  361|       |
  362|    610|                if (ctx->n_segments_max == UINT32_MAX) {
  ------------------
  |  Branch (362:21): [True: 0, False: 610]
  ------------------
  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|    610|                else if (ctx->n_segments_max > (UINT32_MAX >> 2)) {
  ------------------
  |  Branch (367:26): [True: 0, False: 610]
  ------------------
  368|      0|                    ctx->n_segments_max = UINT32_MAX;
  369|    610|                } else {
  370|    610|                    ctx->n_segments_max <<= 2;
  371|    610|                }
  372|       |
  373|    610|                segments = jbig2_renew(ctx, ctx->segments, Jbig2Segment *, ctx->n_segments_max);
  ------------------
  |  |  143|    610|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
  374|    610|                if (segments == NULL) {
  ------------------
  |  Branch (374:21): [True: 2, False: 608]
  ------------------
  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|    608|                ctx->segments = segments;
  380|    608|            }
  381|       |
  382|  87.9k|            ctx->segments[ctx->n_segments++] = segment;
  383|  87.9k|            if (ctx->state == JBIG2_FILE_RANDOM_HEADERS) {
  ------------------
  |  Branch (383:17): [True: 42.0k, False: 45.9k]
  ------------------
  384|  42.0k|                if ((segment->flags & 63) == 51)        /* end of file */
  ------------------
  |  Branch (384:21): [True: 49, False: 42.0k]
  ------------------
  385|     49|                    ctx->state = JBIG2_FILE_RANDOM_BODIES;
  386|  42.0k|            } else              /* JBIG2_FILE_SEQUENTIAL_HEADER */
  387|  45.9k|                ctx->state = JBIG2_FILE_SEQUENTIAL_BODY;
  388|  87.9k|            break;
  389|  45.9k|        case JBIG2_FILE_SEQUENTIAL_BODY:
  ------------------
  |  Branch (389:9): [True: 45.9k, False: 95.6k]
  ------------------
  390|  46.2k|        case JBIG2_FILE_RANDOM_BODIES:
  ------------------
  |  Branch (390:9): [True: 309, False: 141k]
  ------------------
  391|  46.2k|            segment = ctx->segments[ctx->segment_index];
  392|       |
  393|       |            /* immediate generic regions may have unknown size */
  394|  46.2k|            if (segment->data_length == 0xffffffff && (segment->flags & 63) == 38) {
  ------------------
  |  Branch (394:17): [True: 15.4k, False: 30.7k]
  |  Branch (394:55): [True: 12.6k, False: 2.83k]
  ------------------
  395|  12.6k|                byte *s, *e, *p;
  396|  12.6k|                int mmr;
  397|  12.6k|                byte mmr_marker[2] = { 0x00, 0x00 };
  398|  12.6k|                byte arith_marker[2] = { 0xff, 0xac };
  399|  12.6k|                byte *desired_marker;
  400|       |
  401|  12.6k|                s = p = ctx->buf + ctx->buf_rd_ix;
  402|  12.6k|                e = ctx->buf + ctx->buf_wr_ix;
  403|       |
  404|  12.6k|                if (e - p < 18)
  ------------------
  |  Branch (404:21): [True: 30, False: 12.6k]
  ------------------
  405|     30|                        return 0; /* need more data */
  406|       |
  407|  12.6k|                mmr = p[17] & 1;
  408|  12.6k|                p += 18;
  409|  12.6k|                desired_marker = mmr ? mmr_marker : arith_marker;
  ------------------
  |  Branch (409:34): [True: 1.12k, False: 11.5k]
  ------------------
  410|       |
  411|       |                /* look for two byte marker */
  412|  12.6k|                if (e - p < 2)
  ------------------
  |  Branch (412:21): [True: 34, False: 12.5k]
  ------------------
  413|     34|                    return 0; /* need more data */
  414|       |
  415|  1.08M|                while (p[0] != desired_marker[0] || p[1] != desired_marker[1]) {
  ------------------
  |  Branch (415:24): [True: 886k, False: 196k]
  |  Branch (415:53): [True: 183k, False: 12.1k]
  ------------------
  416|  1.07M|                    p++;
  417|  1.07M|                    if (e - p < 2)
  ------------------
  |  Branch (417:25): [True: 397, False: 1.06M]
  ------------------
  418|    397|                        return 0; /* need more data */
  419|  1.07M|                }
  420|  12.1k|                p += 2;
  421|       |
  422|       |                /* the marker is followed by a four byte row count */
  423|  12.1k|                if (e - p < 4)
  ------------------
  |  Branch (423:21): [True: 16, False: 12.1k]
  ------------------
  424|     16|                        return 0; /* need more data */
  425|  12.1k|                segment->rows = jbig2_get_uint32(p);
  426|  12.1k|                p += 4;
  427|       |
  428|  12.1k|                segment->data_length = (size_t) (p - s);
  429|  12.1k|                jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "unknown length determined to be %lu", (long) segment->data_length);
  430|  12.1k|            }
  431|  33.5k|            else if (segment->data_length > ctx->buf_wr_ix - ctx->buf_rd_ix)
  ------------------
  |  Branch (431:22): [True: 3.33k, False: 30.2k]
  ------------------
  432|  3.33k|                    return 0; /* need more data */
  433|       |
  434|  42.4k|            code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
  435|  42.4k|            ctx->buf_rd_ix += segment->data_length;
  436|  42.4k|            ctx->segment_index++;
  437|  42.4k|            if (ctx->state == JBIG2_FILE_RANDOM_BODIES) {
  ------------------
  |  Branch (437:17): [True: 272, False: 42.1k]
  ------------------
  438|    272|                if (ctx->segment_index == ctx->n_segments)
  ------------------
  |  Branch (438:21): [True: 0, False: 272]
  ------------------
  439|      0|                    ctx->state = JBIG2_FILE_EOF;
  440|  42.1k|            } else {            /* JBIG2_FILE_SEQUENTIAL_BODY */
  441|  42.1k|                ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  442|  42.1k|            }
  443|  42.4k|            if (code < 0) {
  ------------------
  |  Branch (443:17): [True: 1.00k, False: 41.4k]
  ------------------
  444|  1.00k|                ctx->state = JBIG2_FILE_EOF;
  445|  1.00k|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode; treating as end of file");
  446|  1.00k|            }
  447|  41.4k|            break;
  448|  41.4k|        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.10k|}
jbig2_ctx_free:
  458|  6.10k|{
  459|  6.10k|    Jbig2Allocator *ca;
  460|  6.10k|    uint32_t i;
  461|       |
  462|  6.10k|    if (ctx == NULL)
  ------------------
  |  Branch (462:9): [True: 0, False: 6.10k]
  ------------------
  463|      0|        return NULL;
  464|       |
  465|  6.10k|    ca = ctx->allocator;
  466|  6.10k|    jbig2_free(ca, ctx->buf);
  467|  6.10k|    if (ctx->segments != NULL) {
  ------------------
  |  Branch (467:9): [True: 6.10k, False: 0]
  ------------------
  468|  94.1k|        for (i = 0; i < ctx->n_segments; i++)
  ------------------
  |  Branch (468:21): [True: 87.9k, False: 6.10k]
  ------------------
  469|  87.9k|            jbig2_free_segment(ctx, ctx->segments[i]);
  470|  6.10k|        jbig2_free(ca, ctx->segments);
  471|  6.10k|    }
  472|       |
  473|  6.10k|    if (ctx->pages != NULL) {
  ------------------
  |  Branch (473:9): [True: 6.10k, False: 0]
  ------------------
  474|  16.6k|        for (i = 0; i <= ctx->current_page; i++)
  ------------------
  |  Branch (474:21): [True: 10.5k, False: 6.10k]
  ------------------
  475|  10.5k|            if (ctx->pages[i].image != NULL)
  ------------------
  |  Branch (475:17): [True: 5.48k, False: 5.09k]
  ------------------
  476|  5.48k|                jbig2_image_release(ctx, ctx->pages[i].image);
  477|  6.10k|        jbig2_free(ca, ctx->pages);
  478|  6.10k|    }
  479|       |
  480|  6.10k|    jbig2_free(ca, ctx);
  481|       |
  482|  6.10k|    return ca;
  483|  6.10k|}
jbig2_word_stream_buf_new:
  545|  23.4k|{
  546|  23.4k|    Jbig2WordStreamBuf *result = jbig2_new(ctx, Jbig2WordStreamBuf, 1);
  ------------------
  |  |  141|  23.4k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  547|       |
  548|  23.4k|    if (result == NULL) {
  ------------------
  |  Branch (548:9): [True: 6, False: 23.4k]
  ------------------
  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|  23.4k|    result->super.get_next_word = jbig2_word_stream_buf_get_next_word;
  554|  23.4k|    result->data = data;
  555|  23.4k|    result->size = size;
  556|       |
  557|  23.4k|    return &result->super;
  558|  23.4k|}
jbig2_word_stream_buf_free:
  562|  23.8k|{
  563|  23.8k|    jbig2_free(ctx->allocator, ws);
  564|  23.8k|}
jbig2_free:
  577|  41.8M|{
  578|  41.8M|    allocator->free(allocator, p);
  579|  41.8M|}
jbig2_realloc:
  583|  4.73k|{
  584|       |    /* check for integer multiplication overflow */
  585|  4.73k|    if (num > 0 && size >= SIZE_MAX / num)
  ------------------
  |  Branch (585:9): [True: 4.73k, False: 0]
  |  Branch (585:20): [True: 0, False: 4.73k]
  ------------------
  586|      0|        return NULL;
  587|  4.73k|    return allocator->realloc(allocator, p, size * num);
  588|  4.73k|}
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.25k, False: 171M]
  ------------------
   83|  5.25k|        fprintf(stderr, "jbig2 decoder FATAL ERROR: %s", msg);
   84|  5.25k|        if (seg_idx != JBIG2_UNKNOWN_SEGMENT_NUMBER)
  ------------------
  |  |   68|  5.25k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  |  Branch (84:13): [True: 1.55k, False: 3.70k]
  ------------------
   85|  1.55k|            fprintf(stderr, " (segment 0x%02x)", seg_idx);
   86|  5.25k|        fprintf(stderr, "\n");
   87|       |        fflush(stderr);
   88|  5.25k|    }
   89|   171M|}
jbig2.c:jbig2_find_buffer_size:
  222|  6.10k|{
  223|  6.10k|    const size_t initial_buf_size = 1024;
  224|  6.10k|    size_t size = initial_buf_size;
  225|       |
  226|  6.10k|    if (desired == SIZE_MAX)
  ------------------
  |  Branch (226:9): [True: 0, False: 6.10k]
  ------------------
  227|      0|        return SIZE_MAX;
  228|       |
  229|  9.31k|    while (size < desired)
  ------------------
  |  Branch (229:12): [True: 3.20k, False: 6.10k]
  ------------------
  230|  3.20k|        size <<= 1;
  231|       |
  232|  6.10k|    return size;
  233|  6.10k|}
jbig2.c:jbig2_word_stream_buf_get_next_word:
  510|  4.33M|{
  511|  4.33M|    Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self;
  512|  4.33M|    uint32_t val = 0;
  513|  4.33M|    int ret = 0;
  514|       |
  515|  4.33M|    if (self == NULL || word == NULL) {
  ------------------
  |  Branch (515:9): [True: 0, False: 4.33M]
  |  Branch (515:25): [True: 0, False: 4.33M]
  ------------------
  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|  4.33M|    if (offset >= z->size) {
  ------------------
  |  Branch (518:9): [True: 3.80M, False: 530k]
  ------------------
  519|  3.80M|        *word = 0;
  520|  3.80M|        return 0;
  521|  3.80M|    }
  522|       |
  523|   530k|    if (offset < z->size) {
  ------------------
  |  Branch (523:9): [True: 530k, False: 0]
  ------------------
  524|   530k|        val = (uint32_t) z->data[offset] << 24;
  525|   530k|        ret++;
  526|   530k|    }
  527|   530k|    if (offset + 1 < z->size) {
  ------------------
  |  Branch (527:9): [True: 525k, False: 4.98k]
  ------------------
  528|   525k|        val |= (uint32_t) z->data[offset + 1] << 16;
  529|   525k|        ret++;
  530|   525k|    }
  531|   530k|    if (offset + 2 < z->size) {
  ------------------
  |  Branch (531:9): [True: 522k, False: 8.57k]
  ------------------
  532|   522k|        val |= (uint32_t) z->data[offset + 2] << 8;
  533|   522k|        ret++;
  534|   522k|    }
  535|   530k|    if (offset + 3 < z->size) {
  ------------------
  |  Branch (535:9): [True: 519k, False: 11.3k]
  ------------------
  536|   519k|        val |= z->data[offset + 3];
  537|   519k|        ret++;
  538|   519k|    }
  539|   530k|    *word = val;
  540|   530k|    return ret;
  541|  4.33M|}

jbig2_arith_new:
  191|  23.4k|{
  192|  23.4k|    Jbig2ArithState *result;
  193|  23.4k|    int ret;
  194|       |
  195|  23.4k|    result = jbig2_new(ctx, Jbig2ArithState, 1);
  ------------------
  |  |  141|  23.4k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  196|  23.4k|    if (result == NULL) {
  ------------------
  |  Branch (196:9): [True: 1, False: 23.4k]
  ------------------
  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|  23.4k|    result->err = 0;
  202|  23.4k|    result->ws = ws;
  203|  23.4k|    result->offset = 0;
  204|       |
  205|  23.4k|    ret = result->ws->get_next_word(ctx, result->ws, result->offset, &result->next_word);
  206|  23.4k|    if (ret < 0) {
  ------------------
  |  Branch (206:9): [True: 0, False: 23.4k]
  ------------------
  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|  23.4k|    result->next_word_bytes = (size_t) ret;
  213|  23.4k|    if (result->next_word_bytes == 0) {
  ------------------
  |  Branch (213:9): [True: 26, False: 23.4k]
  ------------------
  214|     26|        jbig2_free(ctx->allocator, result);
  215|     26|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read first byte from underlying stream when initializing arithmetic decoder");
  ------------------
  |  |   68|     26|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  216|     26|        return NULL;
  217|     26|    }
  218|       |
  219|  23.4k|    result->offset += result->next_word_bytes;
  220|       |
  221|       |    /* Figure F.1 */
  222|  23.4k|    result->C = (~(result->next_word >> 8)) & 0xFF0000;
  223|       |
  224|       |    /* Figure E.20 (2) */
  225|  23.4k|    if (jbig2_arith_bytein(ctx, result) < 0) {
  ------------------
  |  Branch (225:9): [True: 0, False: 23.4k]
  ------------------
  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|  23.4k|    result->C <<= 7;
  233|  23.4k|    result->CT -= 7;
  234|  23.4k|    result->A = 0x8000;
  235|       |
  236|  23.4k|    return result;
  237|  23.4k|}
jbig2_arith_decode:
  319|  9.33G|{
  320|  9.33G|    Jbig2ArithCx cx = *pcx;
  321|  9.33G|    const Jbig2ArithQe *pqe;
  322|  9.33G|    unsigned int index = cx & 0x7f;
  323|  9.33G|    bool D;
  ------------------
  |  |   45|  9.33G|#define bool int
  ------------------
  324|       |
  325|  9.33G|    if (index >= MAX_QE_ARRAY_SIZE) {
  ------------------
  |  |  239|  9.33G|#define MAX_QE_ARRAY_SIZE 47
  ------------------
  |  Branch (325:9): [True: 0, False: 9.33G]
  ------------------
  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|  9.33G|    pqe = &jbig2_arith_Qe[index];
  330|       |
  331|       |    /* Figure F.2 */
  332|  9.33G|    as->A -= pqe->Qe;
  333|  9.33G|    if ((as->C >> 16) < as->A) {
  ------------------
  |  Branch (333:9): [True: 9.21G, False: 122M]
  ------------------
  334|  9.21G|        if ((as->A & 0x8000) == 0) {
  ------------------
  |  Branch (334:13): [True: 157M, False: 9.05G]
  ------------------
  335|       |            /* MPS_EXCHANGE, Figure E.16 */
  336|   157M|            if (as->A < pqe->Qe) {
  ------------------
  |  Branch (336:17): [True: 22.8M, False: 135M]
  ------------------
  337|  22.8M|                D = 1 - (cx >> 7);
  338|  22.8M|                *pcx ^= pqe->lps_xor;
  339|   135M|            } else {
  340|   135M|                D = cx >> 7;
  341|   135M|                *pcx ^= pqe->mps_xor;
  342|   135M|            }
  343|   157M|            if (jbig2_arith_renormd(ctx, as) < 0) {
  ------------------
  |  Branch (343:17): [True: 0, False: 157M]
  ------------------
  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|   157M|            return D;
  348|  9.05G|        } else {
  349|  9.05G|            return cx >> 7;
  350|  9.05G|        }
  351|  9.21G|    } else {
  352|   122M|        as->C -= (as->A) << 16;
  353|       |        /* LPS_EXCHANGE, Figure E.17 */
  354|   122M|        if (as->A < pqe->Qe) {
  ------------------
  |  Branch (354:13): [True: 22.2M, False: 99.7M]
  ------------------
  355|  22.2M|            as->A = pqe->Qe;
  356|  22.2M|            D = cx >> 7;
  357|  22.2M|            *pcx ^= pqe->mps_xor;
  358|  99.7M|        } else {
  359|  99.7M|            as->A = pqe->Qe;
  360|  99.7M|            D = 1 - (cx >> 7);
  361|  99.7M|            *pcx ^= pqe->lps_xor;
  362|  99.7M|        }
  363|   122M|        if (jbig2_arith_renormd(ctx, as) < 0) {
  ------------------
  |  Branch (363:13): [True: 0, False: 122M]
  ------------------
  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|   122M|        return D;
  368|   122M|    }
  369|  9.33G|}
jbig2_arith.c:jbig2_arith_bytein:
   59|  45.9M|{
   60|  45.9M|    byte B;
   61|       |
   62|       |    /* Treat both errors and reading beyond end of stream as an error. */
   63|  45.9M|    if (as->err != 0) {
  ------------------
  |  Branch (63:9): [True: 0, False: 45.9M]
  ------------------
   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|  45.9M|    if (as->next_word_bytes == 0) {
  ------------------
  |  Branch (67:9): [True: 0, False: 45.9M]
  ------------------
   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|  45.9M|    B = (byte)((as->next_word >> 24) & 0xFF);
   93|  45.9M|    if (B == 0xFF) {
  ------------------
  |  Branch (93:9): [True: 44.5M, False: 1.37M]
  ------------------
   94|  44.5M|        byte B1;
   95|       |
   96|       |        /* next_word_bytes can only be == 1 here, but let's be defensive. */
   97|  44.5M|        if (as->next_word_bytes <= 1) {
  ------------------
  |  Branch (97:13): [True: 6.66k, False: 44.5M]
  ------------------
   98|  6.66k|            int ret = as->ws->get_next_word(ctx, as->ws, as->offset, &as->next_word);
   99|  6.66k|            if (ret < 0) {
  ------------------
  |  Branch (99:17): [True: 0, False: 6.66k]
  ------------------
  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|  6.66k|            as->next_word_bytes = (size_t) ret;
  104|       |
  105|  6.66k|            if (as->next_word_bytes == 0) {
  ------------------
  |  Branch (105:17): [True: 241, False: 6.42k]
  ------------------
  106|    241|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read end of possible terminating marker code, assuming terminating marker code");
  ------------------
  |  |   68|    241|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  107|    241|                as->next_word = 0xFF900000;
  108|    241|                as->next_word_bytes = 2;
  109|    241|                as->C += 0xFF00;
  110|    241|                as->CT = 8;
  111|    241|                return 0;
  112|    241|            }
  113|       |
  114|  6.42k|            as->offset += as->next_word_bytes;
  115|       |
  116|  6.42k|            B1 = (byte)((as->next_word >> 24) & 0xFF);
  117|  6.42k|            if (B1 > 0x8F) {
  ------------------
  |  Branch (117:17): [True: 4.75k, False: 1.66k]
  ------------------
  118|       |#ifdef JBIG2_DEBUG_ARITH
  119|       |                fprintf(stderr, "read %02x (aa)\n", B);
  120|       |#endif
  121|  4.75k|                as->CT = 8;
  122|  4.75k|                as->next_word = 0xFF000000 | (as->next_word >> 8);
  123|  4.75k|                as->next_word_bytes = 2;
  124|  4.75k|                as->offset--;
  125|  4.75k|            } else {
  126|       |#ifdef JBIG2_DEBUG_ARITH
  127|       |                fprintf(stderr, "read %02x (a)\n", B);
  128|       |#endif
  129|  1.66k|                as->C += 0xFE00 - (B1 << 9);
  130|  1.66k|                as->CT = 7;
  131|  1.66k|            }
  132|  44.5M|        } else {
  133|  44.5M|            B1 = (byte)((as->next_word >> 16) & 0xFF);
  134|  44.5M|            if (B1 > 0x8F) {
  ------------------
  |  Branch (134:17): [True: 44.5M, False: 7.40k]
  ------------------
  135|       |#ifdef JBIG2_DEBUG_ARITH
  136|       |                fprintf(stderr, "read %02x (ba)\n", B);
  137|       |#endif
  138|  44.5M|                as->CT = 8;
  139|  44.5M|            } else {
  140|  7.40k|                as->next_word_bytes--;
  141|  7.40k|                as->next_word <<= 8;
  142|       |#ifdef JBIG2_DEBUG_ARITH
  143|       |                fprintf(stderr, "read %02x (b)\n", B);
  144|       |#endif
  145|       |
  146|  7.40k|                as->C += 0xFE00 - (B1 << 9);
  147|  7.40k|                as->CT = 7;
  148|  7.40k|            }
  149|  44.5M|        }
  150|  44.5M|    } else {
  151|       |#ifdef JBIG2_DEBUG_ARITH
  152|       |        fprintf(stderr, "read %02x\n", B);
  153|       |#endif
  154|  1.37M|        as->next_word <<= 8;
  155|  1.37M|        as->next_word_bytes--;
  156|       |
  157|  1.37M|        if (as->next_word_bytes == 0) {
  ------------------
  |  Branch (157:13): [True: 339k, False: 1.03M]
  ------------------
  158|   339k|            int ret = as->ws->get_next_word(ctx, as->ws, as->offset, &as->next_word);
  159|   339k|            if (ret < 0) {
  ------------------
  |  Branch (159:17): [True: 0, False: 339k]
  ------------------
  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|   339k|            as->next_word_bytes = (size_t) ret;
  164|       |
  165|   339k|            if (as->next_word_bytes == 0) {
  ------------------
  |  Branch (165:17): [True: 4.37k, False: 334k]
  ------------------
  166|  4.37k|                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|  4.37k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  167|  4.37k|                as->next_word = 0xFF900000;
  168|  4.37k|                as->next_word_bytes = 2;
  169|  4.37k|                as->C += 0xFF00;
  170|  4.37k|                as->CT = 8;
  171|  4.37k|                return 0;
  172|  4.37k|            }
  173|       |
  174|   334k|            as->offset += as->next_word_bytes;
  175|   334k|        }
  176|       |
  177|  1.37M|        B = (byte)((as->next_word >> 24) & 0xFF);
  178|  1.37M|        as->C += 0xFF00 - (B << 8);
  179|  1.37M|        as->CT = 8;
  180|  1.37M|    }
  181|       |
  182|  45.9M|    return 0;
  183|  45.9M|}
jbig2_arith.c:jbig2_arith_renormd:
  303|   280M|{
  304|       |    /* Figure E.18 */
  305|   367M|    do {
  306|   367M|        if (as->CT == 0 && jbig2_arith_bytein(ctx, as) < 0) {
  ------------------
  |  Branch (306:13): [True: 45.8M, False: 321M]
  |  Branch (306:28): [True: 0, False: 45.8M]
  ------------------
  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|   367M|        as->A <<= 1;
  310|   367M|        as->C <<= 1;
  311|   367M|        as->CT--;
  312|   367M|    } while ((as->A & 0x8000) == 0);
  ------------------
  |  Branch (312:14): [True: 87.0M, False: 280M]
  ------------------
  313|       |
  314|   280M|    return 0;
  315|   280M|}

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

jbig2_arith_int_ctx_new:
   41|  48.6k|{
   42|  48.6k|    Jbig2ArithIntCtx *result = jbig2_new(ctx, Jbig2ArithIntCtx, 1);
  ------------------
  |  |  141|  48.6k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   43|       |
   44|  48.6k|    if (result == NULL) {
  ------------------
  |  Branch (44:9): [True: 90, False: 48.5k]
  ------------------
   45|     90|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate arithmetic integer coding state");
  ------------------
  |  |   68|     90|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   46|     90|        return NULL;
   47|  48.5k|    } else {
   48|  48.5k|        memset(result->IAx, 0, sizeof(result->IAx));
   49|  48.5k|    }
   50|       |
   51|  48.5k|    return result;
   52|  48.6k|}
jbig2_arith_int_decode:
   58|  22.5M|{
   59|  22.5M|    Jbig2ArithCx *IAx = actx->IAx;
   60|  22.5M|    int PREV = 1;
   61|  22.5M|    int S;
   62|  22.5M|    int32_t V;
   63|  22.5M|    int bit;
   64|  22.5M|    int n_tail, offset;
   65|  22.5M|    int i;
   66|       |
   67|  22.5M|    S = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   68|  22.5M|    if (S < 0)
  ------------------
  |  Branch (68:9): [True: 0, False: 22.5M]
  ------------------
   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|  22.5M|    PREV = (PREV << 1) | S;
   71|       |
   72|  22.5M|    bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   73|  22.5M|    if (bit < 0)
  ------------------
  |  Branch (73:9): [True: 0, False: 22.5M]
  ------------------
   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|  22.5M|    PREV = (PREV << 1) | bit;
   76|  22.5M|    if (bit) {
  ------------------
  |  Branch (76:9): [True: 11.4M, False: 11.1M]
  ------------------
   77|  11.4M|        bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   78|  11.4M|        if (bit < 0)
  ------------------
  |  Branch (78:13): [True: 0, False: 11.4M]
  ------------------
   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|  11.4M|        PREV = (PREV << 1) | bit;
   81|       |
   82|  11.4M|        if (bit) {
  ------------------
  |  Branch (82:13): [True: 6.51M, False: 4.92M]
  ------------------
   83|  6.51M|            bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   84|  6.51M|            if (bit < 0)
  ------------------
  |  Branch (84:17): [True: 0, False: 6.51M]
  ------------------
   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|  6.51M|            PREV = (PREV << 1) | bit;
   87|       |
   88|  6.51M|            if (bit) {
  ------------------
  |  Branch (88:17): [True: 4.30M, False: 2.20M]
  ------------------
   89|  4.30M|                bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   90|  4.30M|                if (bit < 0)
  ------------------
  |  Branch (90:21): [True: 0, False: 4.30M]
  ------------------
   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.30M|                PREV = (PREV << 1) | bit;
   93|       |
   94|  4.30M|                if (bit) {
  ------------------
  |  Branch (94:21): [True: 1.82M, False: 2.47M]
  ------------------
   95|  1.82M|                    bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
   96|  1.82M|                    if (bit < 0)
  ------------------
  |  Branch (96:25): [True: 0, False: 1.82M]
  ------------------
   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.82M|                    PREV = (PREV << 1) | bit;
   99|       |
  100|  1.82M|                    if (bit) {
  ------------------
  |  Branch (100:25): [True: 163k, False: 1.66M]
  ------------------
  101|   163k|                        n_tail = 32;
  102|   163k|                        offset = 4436;
  103|  1.66M|                    } else {
  104|  1.66M|                        n_tail = 12;
  105|  1.66M|                        offset = 340;
  106|  1.66M|                    }
  107|  2.47M|                } else {
  108|  2.47M|                    n_tail = 8;
  109|  2.47M|                    offset = 84;
  110|  2.47M|                }
  111|  4.30M|            } else {
  112|  2.20M|                n_tail = 6;
  113|  2.20M|                offset = 20;
  114|  2.20M|            }
  115|  6.51M|        } else {
  116|  4.92M|            n_tail = 4;
  117|  4.92M|            offset = 4;
  118|  4.92M|        }
  119|  11.4M|    } else {
  120|  11.1M|        n_tail = 2;
  121|  11.1M|        offset = 0;
  122|  11.1M|    }
  123|       |
  124|  22.5M|    V = 0;
  125|   122M|    for (i = 0; i < n_tail; i++) {
  ------------------
  |  Branch (125:17): [True: 100M, False: 22.5M]
  ------------------
  126|   100M|        bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
  127|   100M|        if (bit < 0)
  ------------------
  |  Branch (127:13): [True: 0, False: 100M]
  ------------------
  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|   100M|        PREV = ((PREV << 1) & 511) | (PREV & 256) | bit;
  130|   100M|        V = (V << 1) | bit;
  131|   100M|    }
  132|       |
  133|       |    /* offset is always >=0, so underflow can't happen. */
  134|       |    /* avoid overflow by clamping 32 bit value. */
  135|  22.5M|    if (V > INT32_MAX - offset)
  ------------------
  |  Branch (135:9): [True: 248, False: 22.5M]
  ------------------
  136|    248|        V = INT32_MAX;
  137|  22.5M|    else
  138|  22.5M|        V += offset;
  139|  22.5M|    V = S ? -V : V;
  ------------------
  |  Branch (139:9): [True: 13.5M, False: 9.05M]
  ------------------
  140|  22.5M|    *p_result = V;
  141|  22.5M|    return S && V == 0 ? 1 : 0;
  ------------------
  |  Branch (141:12): [True: 13.5M, False: 9.05M]
  |  Branch (141:17): [True: 1.52M, False: 12.0M]
  ------------------
  142|  22.5M|}
jbig2_arith_int_ctx_free:
  146|  64.1k|{
  147|  64.1k|    jbig2_free(ctx->allocator, iax);
  148|  64.1k|}

jbig2_generic_stats_size:
  360|  13.2k|{
  361|  13.2k|    int stats_size = template == 0 ? 1 << 16 : template == 1 ? 1 << 13 : 1 << 10;
  ------------------
  |  Branch (361:22): [True: 8.79k, False: 4.46k]
  |  Branch (361:48): [True: 1.95k, False: 2.50k]
  ------------------
  362|       |
  363|  13.2k|    (void) ctx;
  364|       |
  365|  13.2k|    return stats_size;
  366|  13.2k|}
jbig2_decode_generic_region:
 1372|   103k|{
 1373|   103k|    const int8_t *gbat = params->gbat;
 1374|       |
 1375|   103k|    if (!params->MMR && params->TPGDON)
  ------------------
  |  Branch (1375:9): [True: 103k, False: 0]
  |  Branch (1375:25): [True: 12.3k, False: 91.0k]
  ------------------
 1376|  12.3k|        return jbig2_decode_generic_region_TPGDON(ctx, segment, params, as, image, GB_stats);
 1377|       |
 1378|  91.0k|    if (!params->MMR && params->GBTEMPLATE == 0) {
  ------------------
  |  Branch (1378:9): [True: 91.0k, False: 0]
  |  Branch (1378:25): [True: 21.5k, False: 69.4k]
  ------------------
 1379|  21.5k|        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: 388]
  |  Branch (1379:33): [True: 20.0k, False: 1.07k]
  |  Branch (1379:50): [True: 17.8k, False: 2.27k]
  |  Branch (1379:67): [True: 15.7k, False: 2.05k]
  |  Branch (1379:84): [True: 14.0k, False: 1.72k]
  |  Branch (1379:101): [True: 13.7k, False: 323]
  |  Branch (1379:118): [True: 11.5k, False: 2.12k]
  |  Branch (1379:135): [True: 9.02k, False: 2.55k]
  |  Branch (1379:152): [True: 8.81k, False: 213]
  ------------------
 1380|  8.81k|            return jbig2_decode_generic_template0(ctx, segment, params, as, image, GB_stats);
 1381|  12.7k|        else
 1382|  12.7k|            return jbig2_decode_generic_template0_unopt(ctx, segment, params, as, image, GB_stats);
 1383|  69.4k|    } else if (!params->MMR && params->GBTEMPLATE == 1) {
  ------------------
  |  Branch (1383:16): [True: 69.4k, False: 0]
  |  Branch (1383:32): [True: 15.0k, False: 54.4k]
  ------------------
 1384|  15.0k|        if (!params->USESKIP && gbat[0] == +3 && gbat[1] == -1)
  ------------------
  |  Branch (1384:13): [True: 14.8k, False: 171]
  |  Branch (1384:33): [True: 8.03k, False: 6.83k]
  |  Branch (1384:50): [True: 2.02k, False: 6.01k]
  ------------------
 1385|  2.02k|            return jbig2_decode_generic_template1(ctx, segment, params, as, image, GB_stats);
 1386|  13.0k|        else
 1387|  13.0k|            return jbig2_decode_generic_template1_unopt(ctx, segment, params, as, image, GB_stats);
 1388|  15.0k|    }
 1389|  54.4k|    else if (!params->MMR && params->GBTEMPLATE == 2) {
  ------------------
  |  Branch (1389:14): [True: 54.4k, False: 0]
  |  Branch (1389:30): [True: 44.8k, False: 9.59k]
  ------------------
 1390|  44.8k|        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
  ------------------
  |  Branch (1390:13): [True: 43.7k, False: 1.12k]
  |  Branch (1390:33): [True: 8.10k, False: 35.6k]
  |  Branch (1390:49): [True: 5.86k, False: 2.23k]
  ------------------
 1391|  5.86k|            return jbig2_decode_generic_template2(ctx, segment, params, as, image, GB_stats);
 1392|  38.9k|        else
 1393|  38.9k|            return jbig2_decode_generic_template2_unopt(ctx, segment, params, as, image, GB_stats);
 1394|  44.8k|    } else if (!params->MMR && params->GBTEMPLATE == 3) {
  ------------------
  |  Branch (1394:16): [True: 9.59k, False: 0]
  |  Branch (1394:32): [True: 9.59k, False: 0]
  ------------------
 1395|  9.59k|        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
  ------------------
  |  Branch (1395:13): [True: 9.41k, False: 187]
  |  Branch (1395:33): [True: 6.29k, False: 3.11k]
  |  Branch (1395:49): [True: 4.29k, False: 2.00k]
  ------------------
 1396|  4.29k|            return jbig2_decode_generic_template3(ctx, segment, params, as, image, GB_stats);
 1397|  5.30k|        else
 1398|  5.30k|            return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats);
 1399|  9.59k|    }
 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|  91.0k|}
jbig2_immediate_generic_region:
 1416|  17.6k|{
 1417|  17.6k|    Jbig2RegionSegmentInfo rsi;
 1418|  17.6k|    byte seg_flags;
 1419|  17.6k|    int8_t gbat[8];
 1420|  17.6k|    int offset;
 1421|  17.6k|    uint32_t gbat_bytes = 0;
 1422|  17.6k|    Jbig2GenericRegionParams params;
 1423|  17.6k|    int code = 0;
 1424|  17.6k|    Jbig2Image *image = NULL;
 1425|  17.6k|    Jbig2WordStream *ws = NULL;
 1426|  17.6k|    Jbig2ArithState *as = NULL;
 1427|  17.6k|    Jbig2ArithCx *GB_stats = NULL;
 1428|  17.6k|    uint32_t height;
 1429|  17.6k|    Jbig2Page *page = &ctx->pages[ctx->current_page];
 1430|       |
 1431|       |    /* 7.4.6 */
 1432|  17.6k|    if (segment->data_length < 18)
  ------------------
  |  Branch (1432:9): [True: 36, False: 17.6k]
  ------------------
 1433|     36|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
 1434|       |
 1435|  17.6k|    jbig2_get_region_segment_info(&rsi, segment_data);
 1436|  17.6k|    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|  17.6k|    height = rsi.height;
 1440|  17.6k|    if (segment->rows != UINT32_MAX) {
  ------------------
  |  Branch (1440:9): [True: 12.1k, False: 5.43k]
  ------------------
 1441|  12.1k|        if (segment->rows > rsi.height)
  ------------------
  |  Branch (1441:13): [True: 40, False: 12.1k]
  ------------------
 1442|     40|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment contains more rows than stated in header");
 1443|  12.1k|        height = segment->rows;
 1444|  12.1k|    }
 1445|       |
 1446|       |    /* 7.4.6.2 */
 1447|  17.5k|    seg_flags = segment_data[17];
 1448|  17.5k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "segment flags = %02x", seg_flags);
 1449|  17.5k|    if ((seg_flags & 1) && (seg_flags & 6))
  ------------------
  |  Branch (1449:9): [True: 1.41k, False: 16.1k]
  |  Branch (1449:28): [True: 602, False: 812]
  ------------------
 1450|    602|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "MMR is 1, but GBTEMPLATE is not 0");
 1451|       |
 1452|       |    /* 7.4.6.3 */
 1453|  17.5k|    if (!(seg_flags & 1)) {
  ------------------
  |  Branch (1453:9): [True: 16.1k, False: 1.41k]
  ------------------
 1454|  16.1k|        gbat_bytes = (seg_flags & 6) ? 2 : 8;
  ------------------
  |  Branch (1454:22): [True: 6.53k, False: 9.61k]
  ------------------
 1455|  16.1k|        if (18 + gbat_bytes > segment->data_length)
  ------------------
  |  Branch (1455:13): [True: 9, False: 16.1k]
  ------------------
 1456|      9|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
 1457|  16.1k|        memcpy(gbat, segment_data + 18, gbat_bytes);
 1458|  16.1k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "gbat: %d, %d", gbat[0], gbat[1]);
 1459|  16.1k|    }
 1460|       |
 1461|  17.5k|    offset = 18 + gbat_bytes;
 1462|       |
 1463|       |    /* Check for T.88 amendment 2 */
 1464|  17.5k|    if ((seg_flags >> 5) & 1)
  ------------------
  |  Branch (1464:9): [True: 41, False: 17.5k]
  ------------------
 1465|     41|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment uses 12 adaptive template pixels (NYI)");
 1466|       |
 1467|       |    /* Table 34 */
 1468|  17.5k|    params.MMR = seg_flags & 1;
 1469|  17.5k|    params.GBTEMPLATE = (seg_flags & 6) >> 1;
 1470|  17.5k|    params.TPGDON = (seg_flags & 8) >> 3;
 1471|  17.5k|    params.USESKIP = 0;
 1472|  17.5k|    memcpy(params.gbat, gbat, gbat_bytes);
 1473|       |
 1474|  17.5k|    if (page->height == 0xffffffff && page->striped && page->stripe_size > 0) {
  ------------------
  |  Branch (1474:9): [True: 11.0k, False: 6.49k]
  |  Branch (1474:39): [True: 9.34k, False: 1.67k]
  |  Branch (1474:56): [True: 9.34k, False: 0]
  ------------------
 1475|  9.34k|        if (rsi.y >= page->end_row + page->stripe_size) {
  ------------------
  |  Branch (1475:13): [True: 2.68k, False: 6.66k]
  ------------------
 1476|  2.68k|            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.68k|            return 0;
 1478|  2.68k|        }
 1479|  6.66k|        if (height > page->end_row + page->stripe_size) {
  ------------------
  |  Branch (1479:13): [True: 4.93k, False: 1.72k]
  ------------------
 1480|  4.93k|            height = page->end_row + page->stripe_size;
 1481|  4.93k|        }
 1482|  8.17k|    } else {
 1483|  8.17k|        if (rsi.y >= page->height) {
  ------------------
  |  Branch (1483:13): [True: 787, False: 7.38k]
  ------------------
 1484|    787|            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|    787|            return 0;
 1486|    787|        }
 1487|  7.38k|        if (height > page->height - rsi .y) {
  ------------------
  |  Branch (1487:13): [True: 5.70k, False: 1.68k]
  ------------------
 1488|  5.70k|            height = page->height - rsi.y;
 1489|  5.70k|        }
 1490|  7.38k|    }
 1491|  14.0k|    if (height == 0) {
  ------------------
  |  Branch (1491:9): [True: 930, False: 13.1k]
  ------------------
 1492|    930|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "nothing remains of region, ignoring");
 1493|    930|        return 0;
 1494|    930|    }
 1495|       |
 1496|  13.1k|    image = jbig2_image_new(ctx, rsi.width, height);
 1497|  13.1k|    if (image == NULL)
  ------------------
  |  Branch (1497:9): [True: 69, False: 13.0k]
  ------------------
 1498|     69|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate generic image");
 1499|  13.0k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, height);
 1500|       |
 1501|  13.0k|    if (params.MMR) {
  ------------------
  |  Branch (1501:9): [True: 418, False: 12.6k]
  ------------------
 1502|    418|        code = jbig2_decode_generic_mmr(ctx, segment, &params, segment_data + offset, segment->data_length - offset, image);
 1503|    418|        if (code < 0) {
  ------------------
  |  Branch (1503:13): [True: 27, False: 391]
  ------------------
 1504|     27|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode MMR-coded generic region");
 1505|     27|            goto cleanup;
 1506|     27|        }
 1507|  12.6k|    } else {
 1508|  12.6k|        int stats_size = jbig2_generic_stats_size(ctx, params.GBTEMPLATE);
 1509|       |
 1510|  12.6k|        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  12.6k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
 1511|  12.6k|        if (GB_stats == NULL) {
  ------------------
  |  Branch (1511:13): [True: 1, False: 12.6k]
  ------------------
 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|  12.6k|        memset(GB_stats, 0, stats_size);
 1516|       |
 1517|  12.6k|        ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
 1518|  12.6k|        if (ws == NULL) {
  ------------------
  |  Branch (1518:13): [True: 1, False: 12.6k]
  ------------------
 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|  12.6k|        as = jbig2_arith_new(ctx, ws);
 1523|  12.6k|        if (as == NULL) {
  ------------------
  |  Branch (1523:13): [True: 8, False: 12.6k]
  ------------------
 1524|      8|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling immediate generic region");
 1525|      8|            goto cleanup;
 1526|      8|        }
 1527|  12.6k|        code = jbig2_decode_generic_region(ctx, segment, &params, as, image, GB_stats);
 1528|  12.6k|        if (code < 0) {
  ------------------
  |  Branch (1528:13): [True: 84, False: 12.5k]
  ------------------
 1529|     84|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode immediate generic region");
 1530|     84|            goto cleanup;
 1531|     84|        }
 1532|  12.6k|    }
 1533|       |
 1534|  12.9k|    code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
 1535|  12.9k|    if (code < 0)
  ------------------
  |  Branch (1535:9): [True: 627, False: 12.3k]
  ------------------
 1536|    627|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add result to page");
 1537|       |
 1538|  13.0k|cleanup:
 1539|  13.0k|    jbig2_free(ctx->allocator, as);
 1540|  13.0k|    jbig2_word_stream_buf_free(ctx, ws);
 1541|  13.0k|    jbig2_free(ctx->allocator, GB_stats);
 1542|  13.0k|    jbig2_image_release(ctx, image);
 1543|       |
 1544|  13.0k|    return code;
 1545|  12.9k|}
jbig2_generic.c:jbig2_decode_generic_region_TPGDON:
 1337|  12.3k|{
 1338|  12.3k|    switch (params->GBTEMPLATE) {
  ------------------
  |  Branch (1338:13): [True: 12.3k, False: 0]
  ------------------
 1339|  8.40k|    case 0:
  ------------------
  |  Branch (1339:5): [True: 8.40k, False: 3.97k]
  ------------------
 1340|  8.40k|        return jbig2_decode_generic_template0_TPGDON(ctx, segment, params, as, image, GB_stats);
 1341|  1.83k|    case 1:
  ------------------
  |  Branch (1341:5): [True: 1.83k, False: 10.5k]
  ------------------
 1342|  1.83k|        return jbig2_decode_generic_template1_TPGDON(ctx, segment, params, as, image, GB_stats);
 1343|    726|    case 2:
  ------------------
  |  Branch (1343:5): [True: 726, False: 11.6k]
  ------------------
 1344|    726|        return jbig2_decode_generic_template2_TPGDON(ctx, segment, params, as, image, GB_stats);
 1345|  1.41k|    case 3:
  ------------------
  |  Branch (1345:5): [True: 1.41k, False: 10.9k]
  ------------------
 1346|  1.41k|        return jbig2_decode_generic_template3_TPGDON(ctx, segment, params, as, image, GB_stats);
 1347|  12.3k|    }
 1348|       |
 1349|      0|    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported GBTEMPLATE (%d)", params->GBTEMPLATE);
 1350|  12.3k|}
jbig2_generic.c:jbig2_decode_generic_template0_TPGDON:
  924|  8.40k|{
  925|  8.40k|    const int64_t GBW = image->width;
  926|  8.40k|    const int64_t GBH = image->height;
  927|  8.40k|    uint32_t CONTEXT;
  928|  8.40k|    int64_t x, y;
  929|  8.40k|    int LTP = 0;
  930|  8.40k|    int gmin, gmax;
  931|  8.40k|    int64_t left, right, top;
  932|       |
  933|  8.40k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
  ------------------
  |  |  440|  16.8k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.40k]
  |  |  |  Branch (440:20): [True: 1, False: 8.40k]
  |  |  |  Branch (440:31): [True: 0, False: 8.40k]
  |  |  |  Branch (440:46): [True: 5.74k, False: 2.66k]
  |  |  |  Branch (440:57): [True: 0, False: 5.74k]
  |  |  |  Branch (440:72): [True: 2.66k, False: 5.74k]
  |  |  |  Branch (440:84): [True: 7, False: 2.65k]
  |  |  ------------------
  ------------------
  934|  8.39k|        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
  ------------------
  |  |  440|  16.7k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.39k]
  |  |  |  Branch (440:20): [True: 4, False: 8.38k]
  |  |  |  Branch (440:31): [True: 0, False: 8.38k]
  |  |  |  Branch (440:46): [True: 7.51k, False: 872]
  |  |  |  Branch (440:57): [True: 0, False: 7.51k]
  |  |  |  Branch (440:72): [True: 872, False: 7.51k]
  |  |  |  Branch (440:84): [True: 5, False: 867]
  |  |  ------------------
  ------------------
  935|  8.38k|        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
  ------------------
  |  |  440|  16.7k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.38k]
  |  |  |  Branch (440:20): [True: 7, False: 8.37k]
  |  |  |  Branch (440:31): [True: 0, False: 8.37k]
  |  |  |  Branch (440:46): [True: 6.81k, False: 1.56k]
  |  |  |  Branch (440:57): [True: 0, False: 6.81k]
  |  |  |  Branch (440:72): [True: 1.56k, False: 6.81k]
  |  |  |  Branch (440:84): [True: 7, False: 1.56k]
  |  |  ------------------
  ------------------
  936|  8.37k|        pixel_outside_field(params->gbat[6], params->gbat[7]))
  ------------------
  |  |  440|  8.37k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.37k]
  |  |  |  Branch (440:20): [True: 4, False: 8.36k]
  |  |  |  Branch (440:31): [True: 0, False: 8.36k]
  |  |  |  Branch (440:46): [True: 6.18k, False: 2.18k]
  |  |  |  Branch (440:57): [True: 0, False: 6.18k]
  |  |  |  Branch (440:72): [True: 2.18k, False: 6.18k]
  |  |  |  Branch (440:84): [True: 8, False: 2.17k]
  |  |  ------------------
  ------------------
  937|     43|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  938|     43|                           "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.35k|    if (params->gbat[0] ==  3 && params->gbat[1] == -1 &&
  ------------------
  |  Branch (944:9): [True: 3.85k, False: 4.50k]
  |  Branch (944:34): [True: 3.80k, False: 45]
  ------------------
  945|  3.80k|        params->gbat[2] == -3 && params->gbat[3] == -1 &&
  ------------------
  |  Branch (945:9): [True: 3.07k, False: 732]
  |  Branch (945:34): [True: 2.90k, False: 168]
  ------------------
  946|  2.90k|        params->gbat[4] ==  2 && params->gbat[5] == -2 &&
  ------------------
  |  Branch (946:9): [True: 2.62k, False: 288]
  |  Branch (946:34): [True: 2.54k, False: 72]
  ------------------
  947|  2.54k|        params->gbat[6] == -2 && params->gbat[7] == -2)
  ------------------
  |  Branch (947:9): [True: 1.38k, False: 1.16k]
  |  Branch (947:34): [True: 1.27k, False: 118]
  ------------------
  948|  1.27k|    {
  949|  38.1M|        for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (949:21): [True: 38.1M, False: 1.27k]
  ------------------
  950|  38.1M|            int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
  951|  38.1M|            if (bit < 0)
  ------------------
  |  Branch (951:17): [True: 0, False: 38.1M]
  ------------------
  952|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
  953|  38.1M|            LTP ^= bit;
  954|  38.1M|            if (!LTP) {
  ------------------
  |  Branch (954:17): [True: 19.2M, False: 18.8M]
  ------------------
  955|  19.2M|                uint32_t out_byte = 0;
  956|  19.2M|                int out_bits_to_go_in_byte = 8;
  957|  19.2M|                uint8_t *d = &image->data[image->stride * y];
  958|  19.2M|                uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (958:35): [True: 19.2M, False: 745]
  ------------------
  959|  19.2M|                uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (959:35): [True: 19.2M, False: 1.82k]
  ------------------
  960|  19.2M|                uint32_t pd = 0;
  961|  19.2M|                uint32_t ppd = 0;
  962|  19.2M|                if (y > 0 && pline) {
  ------------------
  |  Branch (962:21): [True: 19.2M, False: 745]
  |  Branch (962:30): [True: 19.2M, False: 0]
  ------------------
  963|  19.2M|                    pd = (*pline++ << 8);
  964|  19.2M|                    if (GBW > 8)
  ------------------
  |  Branch (964:25): [True: 1.38M, False: 17.8M]
  ------------------
  965|  1.38M|                        pd |= *pline++;
  966|  19.2M|                    if (y > 1 && ppline) {
  ------------------
  |  Branch (966:25): [True: 19.2M, False: 1.08k]
  |  Branch (966:34): [True: 19.2M, False: 0]
  ------------------
  967|  19.2M|                        ppd = (*ppline++ << 8);
  968|  19.2M|                        if (GBW > 8)
  ------------------
  |  Branch (968:29): [True: 1.38M, False: 17.8M]
  ------------------
  969|  1.38M|                            ppd |= *ppline++;
  970|  19.2M|                    }
  971|  19.2M|                }
  972|   454M|                for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (972:29): [True: 435M, False: 19.2M]
  ------------------
  973|   435M|                    if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (973:25): [True: 0, False: 435M]
  |  Branch (973:44): [True: 0, False: 0]
  ------------------
  974|      0|                        bit = 0;
  975|   435M|                    } else {
  976|   435M|                        CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
  977|   435M|                        CONTEXT |= (pd>>8) & 0x7F0; /* Next 7 pixels */
  978|   435M|                        CONTEXT |= (ppd>>2) & 0xF800; /* Final 5 pixels */
  979|   435M|                        bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  980|   435M|                        if (bit < 0)
  ------------------
  |  Branch (980:29): [True: 0, False: 435M]
  ------------------
  981|      0|                            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
  982|   435M|                    }
  983|   435M|                    pd = pd<<1;
  984|   435M|                    ppd = ppd<<1;
  985|   435M|                    out_byte = (out_byte<<1) | bit;
  986|   435M|                    out_bits_to_go_in_byte--;
  987|   435M|                    if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (987:25): [True: 52.5M, False: 382M]
  ------------------
  988|  52.5M|                        out_bits_to_go_in_byte = 8;
  989|  52.5M|                        *d++ = (uint8_t)out_byte;
  990|  52.5M|                        if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (990:29): [True: 41.7M, False: 10.8M]
  |  Branch (990:42): [True: 15.4M, False: 26.3M]
  |  Branch (990:51): [True: 15.4M, False: 0]
  ------------------
  991|  15.4M|                            pd |= *pline++;
  992|  15.4M|                            if (y > 1 && ppline)
  ------------------
  |  Branch (992:33): [True: 15.1M, False: 243k]
  |  Branch (992:42): [True: 15.1M, False: 0]
  ------------------
  993|  15.1M|                                ppd |= *ppline++;
  994|  15.4M|                        }
  995|  52.5M|                    }
  996|   435M|                }
  997|  19.2M|                if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (997:21): [True: 9.81M, False: 9.45M]
  ------------------
  998|  9.81M|                    *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  999|  19.2M|            } else {
 1000|  18.8M|                copy_prev_row(image, y);
 1001|  18.8M|            }
 1002|  38.1M|        }
 1003|  1.27k|        return 0;
 1004|  1.27k|    }
 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.08k|    left = 4;
 1010|  7.08k|    right = 2;
 1011|  7.08k|    gmin = gmax = params->gbat[0];
 1012|  7.08k|    if (params->gbat[2] < gmin)
  ------------------
  |  Branch (1012:9): [True: 2.05k, False: 5.03k]
  ------------------
 1013|  2.05k|        gmin = params->gbat[2];
 1014|  7.08k|    if (gmax < params->gbat[2])
  ------------------
  |  Branch (1014:9): [True: 3.37k, False: 3.71k]
  ------------------
 1015|  3.37k|        gmax = params->gbat[2];
 1016|  7.08k|    if (params->gbat[4] < gmin)
  ------------------
  |  Branch (1016:9): [True: 914, False: 6.17k]
  ------------------
 1017|    914|        gmin = params->gbat[4];
 1018|  7.08k|    if (gmax < params->gbat[4])
  ------------------
  |  Branch (1018:9): [True: 923, False: 6.16k]
  ------------------
 1019|    923|        gmax = params->gbat[4];
 1020|  7.08k|    if (params->gbat[6] < gmin)
  ------------------
  |  Branch (1020:9): [True: 910, False: 6.17k]
  ------------------
 1021|    910|        gmin = params->gbat[6];
 1022|  7.08k|    if (gmax < params->gbat[6])
  ------------------
  |  Branch (1022:9): [True: 2.65k, False: 4.43k]
  ------------------
 1023|  2.65k|        gmax = params->gbat[6];
 1024|  7.08k|    if (left < -((int64_t) gmin))
  ------------------
  |  Branch (1024:9): [True: 5.14k, False: 1.94k]
  ------------------
 1025|  5.14k|        left = -((int64_t) gmin);
 1026|  7.08k|    if (right < gmax)
  ------------------
  |  Branch (1026:9): [True: 2.84k, False: 4.24k]
  ------------------
 1027|  2.84k|        right = gmax;
 1028|  7.08k|    if (right > GBW)
  ------------------
  |  Branch (1028:9): [True: 1.59k, False: 5.49k]
  ------------------
 1029|  1.59k|        right = GBW;
 1030|  7.08k|    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.08k|    top = 2;
 1036|  7.08k|    gmin = params->gbat[1];
 1037|  7.08k|    if (params->gbat[3] < gmin)
  ------------------
  |  Branch (1037:9): [True: 2.12k, False: 4.96k]
  ------------------
 1038|  2.12k|        gmin = params->gbat[3];
 1039|  7.08k|    if (params->gbat[5] < gmin)
  ------------------
  |  Branch (1039:9): [True: 3.70k, False: 3.38k]
  ------------------
 1040|  3.70k|        gmin = params->gbat[5];
 1041|  7.08k|    if (params->gbat[7] < gmin)
  ------------------
  |  Branch (1041:9): [True: 1.51k, False: 5.56k]
  ------------------
 1042|  1.51k|        gmin = params->gbat[7];
 1043|  7.08k|    if (top < -((int64_t) gmin))
  ------------------
  |  Branch (1043:9): [True: 4.64k, False: 2.44k]
  ------------------
 1044|  4.64k|        top = -((int64_t) gmin);
 1045|       |    /* So 0 <= y < top needs bounds checking. */
 1046|       |
 1047|  86.9M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1047:17): [True: 86.9M, False: 7.08k]
  ------------------
 1048|  86.9M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
 1049|  86.9M|        if (bit < 0)
  ------------------
  |  Branch (1049:13): [True: 0, False: 86.9M]
  ------------------
 1050|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
 1051|  86.9M|        LTP ^= bit;
 1052|  86.9M|        if (!LTP) {
  ------------------
  |  Branch (1052:13): [True: 43.9M, False: 43.0M]
  ------------------
 1053|  43.9M|            uint32_t out_byte = 0;
 1054|  43.9M|            int out_bits_to_go_in_byte = 8;
 1055|  43.9M|            uint8_t *d = &image->data[image->stride * y];
 1056|  43.9M|            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1056:31): [True: 43.9M, False: 3.18k]
  ------------------
 1057|  43.9M|            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (1057:31): [True: 43.8M, False: 8.43k]
  ------------------
 1058|  43.9M|            uint32_t pd = 0;
 1059|  43.9M|            uint32_t ppd = 0;
 1060|  43.9M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1060:17): [True: 43.9M, False: 3.18k]
  |  Branch (1060:26): [True: 43.9M, False: 0]
  ------------------
 1061|  43.9M|                pd = (*pline++ << 8);
 1062|  43.9M|                if (GBW > 8)
  ------------------
  |  Branch (1062:21): [True: 2.58M, False: 41.3M]
  ------------------
 1063|  2.58M|                    pd |= *pline++;
 1064|  43.9M|                if (y > 1 && ppline) {
  ------------------
  |  Branch (1064:21): [True: 43.8M, False: 5.25k]
  |  Branch (1064:30): [True: 43.8M, False: 0]
  ------------------
 1065|  43.8M|                    ppd = (*ppline++ << 8);
 1066|  43.8M|                    if (GBW > 8)
  ------------------
  |  Branch (1066:25): [True: 2.57M, False: 41.3M]
  ------------------
 1067|  2.57M|                        ppd |= *ppline++;
 1068|  43.8M|                }
 1069|  43.9M|            }
 1070|   585M|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1070:25): [True: 541M, False: 43.9M]
  ------------------
 1071|   541M|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1071:21): [True: 0, False: 541M]
  |  Branch (1071:40): [True: 0, False: 0]
  ------------------
 1072|      0|                    bit = 0;
 1073|   541M|                } else {
 1074|   541M|                    CONTEXT = out_byte & 0x000F; /* First 4 pixels */
 1075|   541M|                    CONTEXT |= (pd>>8) & 0x03E0; /* Skip one, next 5 pixels */
 1076|   541M|                    CONTEXT |= (ppd>>2) & 0x7000; /* Skip 2, next 3 pixels, skip one */
 1077|   541M|                    if (y >= top && x >= left && x < right)
  ------------------
  |  Branch (1077:25): [True: 425M, False: 116M]
  |  Branch (1077:37): [True: 259M, False: 165M]
  |  Branch (1077:50): [True: 239M, False: 20.0M]
  ------------------
 1078|   239M|                    {
 1079|   239M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[0], y + params->gbat[1]) << 4;
 1080|   239M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[2], y + params->gbat[3]) << 10;
 1081|   239M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[4], y + params->gbat[5]) << 11;
 1082|   239M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[6], y + params->gbat[7]) << 15;
 1083|   239M|                    }
 1084|   302M|                    else
 1085|   302M|                    {
 1086|   302M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
 1087|   302M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
 1088|   302M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
 1089|   302M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
 1090|   302M|                    }
 1091|   541M|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1092|   541M|                    if (bit < 0)
  ------------------
  |  Branch (1092:25): [True: 0, False: 541M]
  ------------------
 1093|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
 1094|   541M|                }
 1095|   541M|                pd = pd<<1;
 1096|   541M|                ppd = ppd<<1;
 1097|   541M|                out_byte = (out_byte<<1) | bit;
 1098|   541M|                out_bits_to_go_in_byte--;
 1099|   541M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1100|   541M|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1100:21): [True: 54.7M, False: 487M]
  ------------------
 1101|  54.7M|                    out_bits_to_go_in_byte = 8;
 1102|  54.7M|                    d++;
 1103|  54.7M|                    if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (1103:25): [True: 50.5M, False: 4.17M]
  |  Branch (1103:38): [True: 45.1M, False: 5.44M]
  |  Branch (1103:47): [True: 45.1M, False: 0]
  ------------------
 1104|  45.1M|                        pd |= *pline++;
 1105|  45.1M|                        if (y > 1 && ppline)
  ------------------
  |  Branch (1105:29): [True: 37.9M, False: 7.17M]
  |  Branch (1105:38): [True: 37.9M, False: 0]
  ------------------
 1106|  37.9M|                            ppd |= *ppline++;
 1107|  45.1M|                    }
 1108|  54.7M|                }
 1109|   541M|            }
 1110|  43.9M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1110:17): [True: 42.3M, False: 1.59M]
  ------------------
 1111|  42.3M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1112|  43.9M|        } else {
 1113|  43.0M|            copy_prev_row(image, y);
 1114|  43.0M|        }
 1115|  86.9M|    }
 1116|       |
 1117|  7.08k|    return 0;
 1118|  7.08k|}
jbig2_generic.c:copy_prev_row:
  908|   197M|{
  909|   197M|    if (!row) {
  ------------------
  |  Branch (909:9): [True: 6.59k, False: 197M]
  ------------------
  910|       |        /* no previous row */
  911|  6.59k|        memset(image->data, 0, image->stride);
  912|   197M|    } else {
  913|       |        /* duplicate data from the previous row */
  914|   197M|        uint8_t *src = image->data + (row - 1) * image->stride;
  915|       |
  916|   197M|        memcpy(src + image->stride, src, image->stride);
  917|   197M|    }
  918|   197M|}
jbig2_generic.c:jbig2_image_get_pixel_fast:
  348|   959M|{
  349|   959M|    size_t sx = (size_t) x;
  350|   959M|    size_t sy = (size_t) y;
  351|   959M|    size_t byte = (sx >> 3) + sy * image->stride;
  352|   959M|    int bit = 7 - ((int) (sx & 7));
  353|       |
  354|   959M|    return ((image->data[byte] >> bit) & 1);
  355|   959M|}
jbig2_generic.c:jbig2_decode_generic_template1_TPGDON:
 1124|  1.83k|{
 1125|  1.83k|    const int64_t GBW = image->width;
 1126|  1.83k|    const int64_t GBH = image->height;
 1127|  1.83k|    uint32_t CONTEXT;
 1128|  1.83k|    int64_t x, y;
 1129|  1.83k|    int LTP = 0;
 1130|       |
 1131|  1.83k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  1.83k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 1.83k]
  |  |  |  Branch (440:20): [True: 2, False: 1.82k]
  |  |  |  Branch (440:31): [True: 0, False: 1.82k]
  |  |  |  Branch (440:46): [True: 1.14k, False: 685]
  |  |  |  Branch (440:57): [True: 0, False: 1.14k]
  |  |  |  Branch (440:72): [True: 685, False: 1.14k]
  |  |  |  Branch (440:84): [True: 8, False: 677]
  |  |  ------------------
  ------------------
 1132|     10|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 1133|     10|                           "adaptive template pixel is out of field");
 1134|       |
 1135|  66.3M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1135:17): [True: 66.3M, False: 1.82k]
  ------------------
 1136|  66.3M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0795]);
 1137|  66.3M|        if (bit < 0)
  ------------------
  |  Branch (1137:13): [True: 0, False: 66.3M]
  ------------------
 1138|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON1");
 1139|  66.3M|        LTP ^= bit;
 1140|  66.3M|        if (!LTP) {
  ------------------
  |  Branch (1140:13): [True: 34.2M, False: 32.0M]
  ------------------
 1141|  34.2M|            uint32_t out_byte = 0;
 1142|  34.2M|            int out_bits_to_go_in_byte = 8;
 1143|  34.2M|            uint8_t *d = &image->data[image->stride * y];
 1144|  34.2M|            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1144:31): [True: 34.2M, False: 1.18k]
  ------------------
 1145|  34.2M|            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (1145:31): [True: 34.2M, False: 2.41k]
  ------------------
 1146|  34.2M|            uint32_t pd = 0;
 1147|  34.2M|            uint32_t ppd = 0;
 1148|  34.2M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1148:17): [True: 34.2M, False: 1.18k]
  |  Branch (1148:26): [True: 34.2M, False: 0]
  ------------------
 1149|  34.2M|                pd = (*pline++ << 8);
 1150|  34.2M|                if (GBW > 8)
  ------------------
  |  Branch (1150:21): [True: 3.28M, False: 31.0M]
  ------------------
 1151|  3.28M|                    pd |= *pline++;
 1152|  34.2M|                if (y > 1 && ppline) {
  ------------------
  |  Branch (1152:21): [True: 34.2M, False: 1.23k]
  |  Branch (1152:30): [True: 34.2M, False: 0]
  ------------------
 1153|  34.2M|                    ppd = (*ppline++ << 8);
 1154|  34.2M|                    if (GBW > 8)
  ------------------
  |  Branch (1154:25): [True: 3.28M, False: 31.0M]
  ------------------
 1155|  3.28M|                        ppd |= *ppline++;
 1156|  34.2M|                }
 1157|  34.2M|            }
 1158|   854M|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1158:25): [True: 820M, False: 34.2M]
  ------------------
 1159|   820M|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1159:21): [True: 0, False: 820M]
  |  Branch (1159:40): [True: 0, False: 0]
  ------------------
 1160|      0|                    bit = 0;
 1161|   820M|                } else {
 1162|   820M|                    CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
 1163|   820M|                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
 1164|   820M|                    CONTEXT |= (pd>>9) & 0x01F0; /* next 5 pixels */
 1165|   820M|                    CONTEXT |= (ppd>>4) & 0x1E00; /* next 4 pixels */
 1166|   820M|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1167|   820M|                    if (bit < 0)
  ------------------
  |  Branch (1167:25): [True: 0, False: 820M]
  ------------------
 1168|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON2");
 1169|   820M|                }
 1170|   820M|                pd = pd<<1;
 1171|   820M|                ppd = ppd<<1;
 1172|   820M|                out_byte = (out_byte<<1) | bit;
 1173|   820M|                out_bits_to_go_in_byte--;
 1174|   820M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1175|   820M|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1175:21): [True: 94.3M, False: 726M]
  ------------------
 1176|  94.3M|                    out_bits_to_go_in_byte = 8;
 1177|  94.3M|                    d++;
 1178|  94.3M|                    if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (1178:25): [True: 88.1M, False: 6.13M]
  |  Branch (1178:38): [True: 79.2M, False: 8.89M]
  |  Branch (1178:47): [True: 79.2M, False: 0]
  ------------------
 1179|  79.2M|                        pd |= *pline++;
 1180|  79.2M|                        if (y > 1 && ppline)
  ------------------
  |  Branch (1180:29): [True: 64.6M, False: 14.6M]
  |  Branch (1180:38): [True: 64.6M, False: 0]
  ------------------
 1181|  64.6M|                            ppd |= *ppline++;
 1182|  79.2M|                    }
 1183|  94.3M|                }
 1184|   820M|            }
 1185|  34.2M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1185:17): [True: 31.4M, False: 2.84M]
  ------------------
 1186|  31.4M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1187|  34.2M|        } else {
 1188|  32.0M|            copy_prev_row(image, y);
 1189|  32.0M|        }
 1190|  66.3M|    }
 1191|       |
 1192|  1.82k|    return 0;
 1193|  1.82k|}
jbig2_generic.c:jbig2_decode_generic_template2_TPGDON:
 1199|    726|{
 1200|    726|    const int64_t GBW = image->width;
 1201|    726|    const int64_t GBH = image->height;
 1202|    726|    uint32_t CONTEXT;
 1203|    726|    int64_t x, y;
 1204|    726|    int LTP = 0;
 1205|       |
 1206|    726|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|    726|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 726]
  |  |  |  Branch (440:20): [True: 4, False: 722]
  |  |  |  Branch (440:31): [True: 0, False: 722]
  |  |  |  Branch (440:46): [True: 412, False: 310]
  |  |  |  Branch (440:57): [True: 0, False: 412]
  |  |  |  Branch (440:72): [True: 310, False: 412]
  |  |  |  Branch (440:84): [True: 5, False: 305]
  |  |  ------------------
  ------------------
 1207|      9|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 1208|      9|                           "adaptive template pixel is out of field");
 1209|       |
 1210|   101M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1210:17): [True: 101M, False: 717]
  ------------------
 1211|   101M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0xE5]);
 1212|   101M|        if (bit < 0)
  ------------------
  |  Branch (1212:13): [True: 0, False: 101M]
  ------------------
 1213|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON1");
 1214|   101M|        LTP ^= bit;
 1215|   101M|        if (!LTP) {
  ------------------
  |  Branch (1215:13): [True: 48.1M, False: 53.4M]
  ------------------
 1216|  48.1M|            uint32_t out_byte = 0;
 1217|  48.1M|            int out_bits_to_go_in_byte = 8;
 1218|  48.1M|            uint8_t *d = &image->data[image->stride * y];
 1219|  48.1M|            uint8_t *pline  =  y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1219:32): [True: 48.1M, False: 425]
  ------------------
 1220|  48.1M|            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (1220:31): [True: 48.1M, False: 1.11k]
  ------------------
 1221|  48.1M|            uint32_t pd = 0;
 1222|  48.1M|            uint32_t ppd = 0;
 1223|  48.1M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1223:17): [True: 48.1M, False: 425]
  |  Branch (1223:26): [True: 48.1M, False: 0]
  ------------------
 1224|  48.1M|                pd = (*pline++ << 8);
 1225|  48.1M|                if (GBW > 8)
  ------------------
  |  Branch (1225:21): [True: 1.08M, False: 47.1M]
  ------------------
 1226|  1.08M|                    pd |= *pline++;
 1227|  48.1M|                if (y > 1 && ppline) {
  ------------------
  |  Branch (1227:21): [True: 48.1M, False: 686]
  |  Branch (1227:30): [True: 48.1M, False: 0]
  ------------------
 1228|  48.1M|                    ppd = (*ppline++ << 8);
 1229|  48.1M|                    if (GBW > 8)
  ------------------
  |  Branch (1229:25): [True: 1.08M, False: 47.1M]
  ------------------
 1230|  1.08M|                        ppd |= *ppline++;
 1231|  48.1M|                }
 1232|  48.1M|            }
 1233|   304M|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1233:25): [True: 255M, False: 48.1M]
  ------------------
 1234|   255M|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1234:21): [True: 0, False: 255M]
  |  Branch (1234:40): [True: 0, False: 0]
  ------------------
 1235|      0|                    bit = 0;
 1236|   255M|                } else {
 1237|   255M|                    CONTEXT  = out_byte & 0x003; /* First 2 pixels */
 1238|   255M|                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
 1239|   255M|                    CONTEXT |= (pd>>11) & 0x078; /* next 4 pixels */
 1240|   255M|                    CONTEXT |= (ppd>>7) & 0x380; /* next 3 pixels */
 1241|   255M|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1242|   255M|                    if (bit < 0)
  ------------------
  |  Branch (1242:25): [True: 0, False: 255M]
  ------------------
 1243|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON2");
 1244|   255M|                }
 1245|   255M|                pd = pd<<1;
 1246|   255M|                ppd = ppd<<1;
 1247|   255M|                out_byte = (out_byte<<1) | bit;
 1248|   255M|                out_bits_to_go_in_byte--;
 1249|   255M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1250|   255M|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1250:21): [True: 22.8M, False: 233M]
  ------------------
 1251|  22.8M|                    out_bits_to_go_in_byte = 8;
 1252|  22.8M|                    d++;
 1253|  22.8M|                    if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (1253:25): [True: 19.9M, False: 2.82M]
  |  Branch (1253:38): [True: 18.6M, False: 1.36M]
  |  Branch (1253:47): [True: 18.6M, False: 0]
  ------------------
 1254|  18.6M|                        pd |= *pline++;
 1255|  18.6M|                        if (y > 1 && ppline)
  ------------------
  |  Branch (1255:29): [True: 17.6M, False: 1.01M]
  |  Branch (1255:38): [True: 17.6M, False: 0]
  ------------------
 1256|  17.6M|                            ppd |= *ppline++;
 1257|  18.6M|                    }
 1258|  22.8M|                }
 1259|   255M|            }
 1260|  48.1M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1260:17): [True: 46.4M, False: 1.74M]
  ------------------
 1261|  46.4M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1262|  53.4M|        } else {
 1263|  53.4M|            copy_prev_row(image, y);
 1264|  53.4M|        }
 1265|   101M|    }
 1266|       |
 1267|    717|    return 0;
 1268|    717|}
jbig2_generic.c:jbig2_decode_generic_template3_TPGDON:
 1274|  1.41k|{
 1275|  1.41k|    const int64_t GBW = image->width;
 1276|  1.41k|    const int64_t GBH = image->height;
 1277|  1.41k|    uint32_t CONTEXT;
 1278|  1.41k|    int64_t x, y;
 1279|  1.41k|    int LTP = 0;
 1280|       |
 1281|  1.41k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  1.41k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 1.41k]
  |  |  |  Branch (440:20): [True: 1, False: 1.41k]
  |  |  |  Branch (440:31): [True: 0, False: 1.41k]
  |  |  |  Branch (440:46): [True: 730, False: 683]
  |  |  |  Branch (440:57): [True: 0, False: 730]
  |  |  |  Branch (440:72): [True: 683, False: 730]
  |  |  |  Branch (440:84): [True: 4, False: 679]
  |  |  ------------------
  ------------------
 1282|      5|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 1283|      5|                           "adaptive template pixel is out of field");
 1284|       |
 1285|   101M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1285:17): [True: 101M, False: 1.40k]
  ------------------
 1286|   101M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0195]);
 1287|   101M|        if (bit < 0)
  ------------------
  |  Branch (1287:13): [True: 0, False: 101M]
  ------------------
 1288|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON1");
 1289|   101M|        LTP ^= bit;
 1290|   101M|        if (!LTP) {
  ------------------
  |  Branch (1290:13): [True: 51.3M, False: 49.8M]
  ------------------
 1291|  51.3M|            uint32_t out_byte = 0;
 1292|  51.3M|            int out_bits_to_go_in_byte = 8;
 1293|  51.3M|            uint8_t *d = &image->data[image->stride * y];
 1294|  51.3M|            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1294:31): [True: 51.3M, False: 174]
  ------------------
 1295|  51.3M|            uint32_t pd = 0;
 1296|  51.3M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1296:17): [True: 51.3M, False: 174]
  |  Branch (1296:26): [True: 51.3M, False: 0]
  ------------------
 1297|  51.3M|                pd = (*pline++ << 8);
 1298|  51.3M|                if (GBW > 8)
  ------------------
  |  Branch (1298:21): [True: 997k, False: 50.3M]
  ------------------
 1299|   997k|                    pd |= *pline++;
 1300|  51.3M|            }
 1301|   710M|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1301:25): [True: 658M, False: 51.3M]
  ------------------
 1302|   658M|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1302:21): [True: 0, False: 658M]
  |  Branch (1302:40): [True: 0, False: 0]
  ------------------
 1303|      0|                    bit = 0;
 1304|   658M|                } else {
 1305|   658M|                    CONTEXT  = out_byte & 0x0F; /* First 4 pixels */
 1306|   658M|                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
 1307|   658M|                    CONTEXT |= (pd>>9) & 0x3E0; /* next 5 pixels */
 1308|   658M|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1309|   658M|                    if (bit < 0)
  ------------------
  |  Branch (1309:25): [True: 0, False: 658M]
  ------------------
 1310|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON2");
 1311|   658M|                }
 1312|   658M|                pd = pd<<1;
 1313|   658M|                out_byte = (out_byte<<1) | bit;
 1314|   658M|                out_bits_to_go_in_byte--;
 1315|   658M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1316|   658M|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1316:21): [True: 72.3M, False: 586M]
  ------------------
 1317|  72.3M|                    out_bits_to_go_in_byte = 8;
 1318|  72.3M|                    d++;
 1319|  72.3M|                    if (x+9 < GBW && y > 0 && pline)
  ------------------
  |  Branch (1319:25): [True: 69.6M, False: 2.69M]
  |  Branch (1319:38): [True: 58.5M, False: 11.0M]
  |  Branch (1319:47): [True: 58.5M, False: 0]
  ------------------
 1320|  58.5M|                        pd |= *pline++;
 1321|  72.3M|                }
 1322|   658M|            }
 1323|  51.3M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1323:17): [True: 49.6M, False: 1.69M]
  ------------------
 1324|  49.6M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1325|  51.3M|        } else {
 1326|  49.8M|            copy_prev_row(image, y);
 1327|  49.8M|        }
 1328|   101M|    }
 1329|       |
 1330|  1.40k|    return 0;
 1331|  1.40k|}
jbig2_generic.c:jbig2_decode_generic_template0:
  373|  8.81k|{
  374|  8.81k|    const int64_t GBW = image->width;
  375|  8.81k|    const int64_t GBH = image->height;
  376|  8.81k|    const uint32_t rowstride = image->stride;
  377|  8.81k|    int64_t x, y;
  378|  8.81k|    byte *line2 = NULL;
  379|  8.81k|    byte *line1 = NULL;
  380|  8.81k|    byte *gbreg_line = (byte *) image->data;
  381|       |
  382|  8.81k|    (void) ctx;
  383|  8.81k|    (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|  8.81k|    if (GBW <= 0)
  ------------------
  |  Branch (391:9): [True: 0, False: 8.81k]
  ------------------
  392|      0|        return 0;
  393|       |
  394|   191k|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (394:17): [True: 182k, False: 8.81k]
  ------------------
  395|   182k|        uint32_t CONTEXT;
  396|   182k|        uint32_t line_m1;
  397|   182k|        uint32_t line_m2;
  398|   182k|        int64_t padded_width = (GBW + 7) & -8;
  399|       |
  400|   182k|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (400:19): [True: 174k, False: 8.81k]
  ------------------
  401|   182k|        line_m2 = line2 ? line2[0] << 6 : 0;
  ------------------
  |  Branch (401:19): [True: 165k, False: 17.4k]
  ------------------
  402|   182k|        CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800);
  403|       |
  404|       |        /* 6.2.5.7 3d */
  405|  41.4M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (405:21): [True: 41.2M, False: 182k]
  ------------------
  406|  41.2M|            byte result = 0;
  407|  41.2M|            int64_t x_minor;
  408|  41.2M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (408:35): [True: 41.1M, False: 182k]
  ------------------
  409|       |
  410|  41.2M|            if (line1)
  ------------------
  |  Branch (410:17): [True: 31.2M, False: 9.99M]
  ------------------
  411|  31.2M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (411:45): [True: 31.1M, False: 174k]
  ------------------
  412|       |
  413|  41.2M|            if (line2)
  ------------------
  |  Branch (413:17): [True: 23.6M, False: 17.6M]
  ------------------
  414|  23.6M|                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 6 : 0);
  ------------------
  |  Branch (414:45): [True: 23.4M, False: 165k]
  ------------------
  415|       |
  416|       |            /* This is the speed-critical inner loop. */
  417|   370M|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (417:31): [True: 329M, False: 41.2M]
  ------------------
  418|   329M|                int bit;
  419|       |
  420|   329M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  421|   329M|                if (bit < 0)
  ------------------
  |  Branch (421:21): [True: 0, False: 329M]
  ------------------
  422|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 optimized");
  423|   329M|                result |= bit << (7 - x_minor);
  424|   329M|                CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800);
  425|   329M|            }
  426|  41.2M|            gbreg_line[x >> 3] = result;
  427|  41.2M|        }
  428|       |#ifdef OUTPUT_PBM
  429|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  430|       |#endif
  431|   182k|        line2 = line1;
  432|   182k|        line1 = gbreg_line;
  433|   182k|        gbreg_line += rowstride;
  434|   182k|    }
  435|       |
  436|  8.81k|    return 0;
  437|  8.81k|}
jbig2_generic.c:jbig2_decode_generic_template0_unopt:
  446|  12.7k|{
  447|  12.7k|    const int64_t GBW = image->width;
  448|  12.7k|    const int64_t GBH = image->height;
  449|  12.7k|    uint32_t CONTEXT;
  450|  12.7k|    int64_t x, y;
  451|  12.7k|    int bit;
  452|       |
  453|  12.7k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
  ------------------
  |  |  440|  25.4k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 12.7k]
  |  |  |  Branch (440:20): [True: 5, False: 12.7k]
  |  |  |  Branch (440:31): [True: 0, False: 12.7k]
  |  |  |  Branch (440:46): [True: 12.1k, False: 622]
  |  |  |  Branch (440:57): [True: 0, False: 12.1k]
  |  |  |  Branch (440:72): [True: 622, False: 12.1k]
  |  |  |  Branch (440:84): [True: 12, False: 610]
  |  |  ------------------
  ------------------
  454|  12.7k|        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
  ------------------
  |  |  440|  25.4k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 12.7k]
  |  |  |  Branch (440:20): [True: 2, False: 12.7k]
  |  |  |  Branch (440:31): [True: 0, False: 12.7k]
  |  |  |  Branch (440:46): [True: 12.2k, False: 494]
  |  |  |  Branch (440:57): [True: 0, False: 12.2k]
  |  |  |  Branch (440:72): [True: 494, False: 12.2k]
  |  |  |  Branch (440:84): [True: 4, False: 490]
  |  |  ------------------
  ------------------
  455|  12.7k|        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
  ------------------
  |  |  440|  25.4k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 12.7k]
  |  |  |  Branch (440:20): [True: 2, False: 12.7k]
  |  |  |  Branch (440:31): [True: 0, False: 12.7k]
  |  |  |  Branch (440:46): [True: 11.6k, False: 1.03k]
  |  |  |  Branch (440:57): [True: 0, False: 11.6k]
  |  |  |  Branch (440:72): [True: 1.03k, False: 11.6k]
  |  |  |  Branch (440:84): [True: 2, False: 1.03k]
  |  |  ------------------
  ------------------
  456|  12.7k|        pixel_outside_field(params->gbat[6], params->gbat[7]))
  ------------------
  |  |  440|  12.7k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 12.7k]
  |  |  |  Branch (440:20): [True: 5, False: 12.7k]
  |  |  |  Branch (440:31): [True: 0, False: 12.7k]
  |  |  |  Branch (440:46): [True: 10.8k, False: 1.88k]
  |  |  |  Branch (440:57): [True: 0, False: 10.8k]
  |  |  |  Branch (440:72): [True: 1.88k, False: 10.8k]
  |  |  |  Branch (440:84): [True: 2, False: 1.88k]
  |  |  ------------------
  ------------------
  457|     34|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  458|     34|                           "adaptive template pixel is out of field");
  459|       |
  460|  43.1M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (460:17): [True: 43.1M, False: 12.7k]
  ------------------
  461|  43.1M|        uint32_t out_byte = 0;
  462|  43.1M|        int out_bits_to_go_in_byte = 8;
  463|  43.1M|        uint8_t *d = &image->data[image->stride * y];
  464|  43.1M|        uint32_t pd = 0;
  465|  43.1M|        uint32_t ppd = 0;
  466|  43.1M|        uint8_t *pline = NULL;
  467|  43.1M|        uint8_t *ppline = NULL;
  468|  43.1M|        if (y >= 1)
  ------------------
  |  Branch (468:13): [True: 43.1M, False: 12.7k]
  ------------------
  469|  43.1M|        {
  470|  43.1M|            pline  = &image->data[image->stride * (y-1)];
  471|  43.1M|            pd = (*pline++ << 8);
  472|  43.1M|            if (GBW > 8)
  ------------------
  |  Branch (472:17): [True: 928k, False: 42.2M]
  ------------------
  473|   928k|                pd |= *pline++;
  474|  43.1M|        }
  475|  43.1M|        if (y >= 2) {
  ------------------
  |  Branch (475:13): [True: 43.1M, False: 22.2k]
  ------------------
  476|  43.1M|            ppline = &image->data[image->stride * (y-2)];
  477|  43.1M|            ppd = (*ppline++ << 8);
  478|  43.1M|            if (GBW > 8)
  ------------------
  |  Branch (478:17): [True: 919k, False: 42.2M]
  ------------------
  479|   919k|                ppd |= *ppline++;
  480|  43.1M|        }
  481|   616M|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (481:21): [True: 573M, False: 43.1M]
  ------------------
  482|   573M|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (482:17): [True: 126M, False: 447M]
  |  Branch (482:36): [True: 114M, False: 12.0M]
  ------------------
  483|   114M|                bit = 0;
  484|   459M|            } else {
  485|   459M|                CONTEXT  = out_byte & 0x000F; /* First 4 pixels */
  486|   459M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
  487|   459M|                CONTEXT |= (pd>>8) & 0x03E0; /* Next 5 pixels */
  488|   459M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
  489|   459M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
  490|   459M|                CONTEXT |= (ppd>>2) & 0x7000; /* Next 3 pixels */
  491|   459M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
  492|   459M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  493|   459M|                if (bit < 0)
  ------------------
  |  Branch (493:21): [True: 0, False: 459M]
  ------------------
  494|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 unoptimized");
  495|   459M|            }
  496|   573M|            pd = pd<<1;
  497|   573M|            ppd = ppd<<1;
  498|   573M|            out_byte = (out_byte<<1) | bit;
  499|   573M|            out_bits_to_go_in_byte--;
  500|   573M|            *d = out_byte<<out_bits_to_go_in_byte;
  501|   573M|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (501:17): [True: 59.1M, False: 514M]
  ------------------
  502|  59.1M|                out_bits_to_go_in_byte = 8;
  503|  59.1M|                d++;
  504|  59.1M|                if (x+9 < GBW && pline != NULL) {
  ------------------
  |  Branch (504:21): [True: 57.9M, False: 1.16M]
  |  Branch (504:34): [True: 39.3M, False: 18.6M]
  ------------------
  505|  39.3M|                    pd |= *pline++;
  506|  39.3M|                    if (ppline != NULL)
  ------------------
  |  Branch (506:25): [True: 32.6M, False: 6.77M]
  ------------------
  507|  32.6M|                        ppd |= *ppline++;
  508|  39.3M|                }
  509|  59.1M|            }
  510|   573M|        }
  511|  43.1M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (511:13): [True: 42.9M, False: 228k]
  ------------------
  512|  42.9M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  513|  43.1M|    }
  514|  12.7k|    return 0;
  515|  12.7k|}
jbig2_generic.c:jbig2_decode_generic_template1:
  589|  2.02k|{
  590|  2.02k|    const int64_t GBW = image->width;
  591|  2.02k|    const int64_t GBH = image->height;
  592|  2.02k|    const uint32_t rowstride = image->stride;
  593|  2.02k|    int64_t x, y;
  594|  2.02k|    byte *line2 = NULL;
  595|  2.02k|    byte *line1 = NULL;
  596|  2.02k|    byte *gbreg_line = (byte *) image->data;
  597|       |
  598|  2.02k|    (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|  2.02k|    if (GBW <= 0)
  ------------------
  |  Branch (606:9): [True: 0, False: 2.02k]
  ------------------
  607|      0|        return 0;
  608|       |
  609|   982k|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (609:17): [True: 980k, False: 2.02k]
  ------------------
  610|   980k|        uint32_t CONTEXT;
  611|   980k|        uint32_t line_m1;
  612|   980k|        uint32_t line_m2;
  613|   980k|        int64_t padded_width = (GBW + 7) & -8;
  614|       |
  615|   980k|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (615:19): [True: 978k, False: 2.02k]
  ------------------
  616|   980k|        line_m2 = line2 ? line2[0] << 5 : 0;
  ------------------
  |  Branch (616:19): [True: 977k, False: 3.47k]
  ------------------
  617|   980k|        CONTEXT = ((line_m1 >> 1) & 0x1f8) | ((line_m2 >> 1) & 0x1e00);
  618|       |
  619|       |        /* 6.2.5.7 3d */
  620|   111M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (620:21): [True: 110M, False: 980k]
  ------------------
  621|   110M|            byte result = 0;
  622|   110M|            int64_t x_minor;
  623|   110M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (623:35): [True: 109M, False: 980k]
  ------------------
  624|       |
  625|   110M|            if (line1)
  ------------------
  |  Branch (625:17): [True: 56.2M, False: 54.1M]
  ------------------
  626|  56.2M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (626:45): [True: 55.2M, False: 978k]
  ------------------
  627|       |
  628|   110M|            if (line2)
  ------------------
  |  Branch (628:17): [True: 50.5M, False: 59.8M]
  ------------------
  629|  50.5M|                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 5 : 0);
  ------------------
  |  Branch (629:45): [True: 49.5M, False: 977k]
  ------------------
  630|       |
  631|       |            /* This is the speed-critical inner loop. */
  632|   991M|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (632:31): [True: 880M, False: 110M]
  ------------------
  633|   880M|                int bit;
  634|       |
  635|   880M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  636|   880M|                if (bit < 0)
  ------------------
  |  Branch (636:21): [True: 0, False: 880M]
  ------------------
  637|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 optimized");
  638|   880M|                result |= bit << (7 - x_minor);
  639|   880M|                CONTEXT = ((CONTEXT & 0xefb) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x8) | ((line_m2 >> (8 - x_minor)) & 0x200);
  640|   880M|            }
  641|   110M|            gbreg_line[x >> 3] = result;
  642|   110M|        }
  643|       |#ifdef OUTPUT_PBM
  644|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  645|       |#endif
  646|   980k|        line2 = line1;
  647|   980k|        line1 = gbreg_line;
  648|   980k|        gbreg_line += rowstride;
  649|   980k|    }
  650|       |
  651|  2.02k|    return 0;
  652|  2.02k|}
jbig2_generic.c:jbig2_decode_generic_template1_unopt:
  521|  13.0k|{
  522|  13.0k|    const int64_t GBW = image->width;
  523|  13.0k|    const int64_t GBH = image->height;
  524|  13.0k|    uint32_t CONTEXT;
  525|  13.0k|    int64_t x, y;
  526|  13.0k|    int bit;
  527|       |
  528|  13.0k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  13.0k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 13.0k]
  |  |  |  Branch (440:20): [True: 2, False: 13.0k]
  |  |  |  Branch (440:31): [True: 0, False: 13.0k]
  |  |  |  Branch (440:46): [True: 11.4k, False: 1.56k]
  |  |  |  Branch (440:57): [True: 0, False: 11.4k]
  |  |  |  Branch (440:72): [True: 1.56k, False: 11.4k]
  |  |  |  Branch (440:84): [True: 5, False: 1.56k]
  |  |  ------------------
  ------------------
  529|      7|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  530|      7|                           "adaptive template pixel is out of field");
  531|       |
  532|   150M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (532:17): [True: 150M, False: 13.0k]
  ------------------
  533|   150M|        uint32_t out_byte = 0;
  534|   150M|        int out_bits_to_go_in_byte = 8;
  535|   150M|        uint8_t *d = &image->data[image->stride * y];
  536|   150M|        uint32_t pd = 0;
  537|   150M|        uint32_t ppd = 0;
  538|   150M|        uint8_t *pline = NULL;
  539|   150M|        uint8_t *ppline = NULL;
  540|   150M|        if (y >= 1) {
  ------------------
  |  Branch (540:13): [True: 150M, False: 13.0k]
  ------------------
  541|   150M|            pline  = &image->data[image->stride * (y-1)];
  542|   150M|            pd = (*pline++ << 8);
  543|   150M|            if (GBW > 8)
  ------------------
  |  Branch (543:17): [True: 260k, False: 150M]
  ------------------
  544|   260k|                pd |= *pline++;
  545|   150M|        }
  546|   150M|        if (y >= 2) {
  ------------------
  |  Branch (546:13): [True: 150M, False: 15.2k]
  ------------------
  547|   150M|            ppline = &image->data[image->stride * (y-2)];
  548|   150M|            ppd = (*ppline++ << 8);
  549|   150M|            if (GBW > 8)
  ------------------
  |  Branch (549:17): [True: 258k, False: 150M]
  ------------------
  550|   258k|                ppd |= *ppline++;
  551|   150M|        }
  552|  1.14G|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (552:21): [True: 990M, False: 150M]
  ------------------
  553|   990M|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (553:17): [True: 51.4M, False: 938M]
  |  Branch (553:36): [True: 51.4M, False: 653]
  ------------------
  554|  51.4M|                bit = 0;
  555|   938M|            } else {
  556|   938M|                CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
  557|   938M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
  558|   938M|                CONTEXT |= (pd>>9) & 0x01F0; /* Next 5 pixels */
  559|   938M|                CONTEXT |= (ppd>>4) & 0x1E00; /* Next 4 pixels */
  560|   938M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  561|   938M|                if (bit < 0)
  ------------------
  |  Branch (561:21): [True: 0, False: 938M]
  ------------------
  562|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 unoptimized");
  563|   938M|            }
  564|   990M|            pd = pd<<1;
  565|   990M|            ppd = ppd<<1;
  566|   990M|            out_byte = (out_byte<<1) | bit;
  567|   990M|            out_bits_to_go_in_byte--;
  568|   990M|            *d = out_byte<<out_bits_to_go_in_byte;
  569|   990M|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (569:17): [True: 102M, False: 888M]
  ------------------
  570|   102M|                out_bits_to_go_in_byte = 8;
  571|   102M|                d++;
  572|   102M|                if (x+9 < GBW && pline != NULL) {
  ------------------
  |  Branch (572:21): [True: 89.1M, False: 12.8M]
  |  Branch (572:34): [True: 26.5M, False: 62.6M]
  ------------------
  573|  26.5M|                    pd |= *pline++;
  574|  26.5M|                    if (ppline != NULL)
  ------------------
  |  Branch (574:25): [True: 18.7M, False: 7.74M]
  ------------------
  575|  18.7M|                        ppd |= *ppline++;
  576|  26.5M|                }
  577|   102M|            }
  578|   990M|        }
  579|   150M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (579:13): [True: 137M, False: 12.5M]
  ------------------
  580|   137M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  581|   150M|    }
  582|  13.0k|    return 0;
  583|  13.0k|}
jbig2_generic.c:jbig2_decode_generic_template2:
  725|  5.86k|{
  726|  5.86k|    const int64_t GBW = image->width;
  727|  5.86k|    const int64_t GBH = image->height;
  728|  5.86k|    const uint32_t rowstride = image->stride;
  729|  5.86k|    int64_t x, y;
  730|  5.86k|    byte *line2 = NULL;
  731|  5.86k|    byte *line1 = NULL;
  732|  5.86k|    byte *gbreg_line = (byte *) image->data;
  733|       |
  734|  5.86k|    (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|  5.86k|    if (GBW <= 0)
  ------------------
  |  Branch (742:9): [True: 0, False: 5.86k]
  ------------------
  743|      0|        return 0;
  744|       |
  745|  58.7M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (745:17): [True: 58.7M, False: 5.86k]
  ------------------
  746|  58.7M|        uint32_t CONTEXT;
  747|  58.7M|        uint32_t line_m1;
  748|  58.7M|        uint32_t line_m2;
  749|  58.7M|        int64_t padded_width = (GBW + 7) & -8;
  750|       |
  751|  58.7M|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (751:19): [True: 58.6M, False: 5.86k]
  ------------------
  752|  58.7M|        line_m2 = line2 ? line2[0] << 4 : 0;
  ------------------
  |  Branch (752:19): [True: 58.6M, False: 11.7k]
  ------------------
  753|  58.7M|        CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380);
  754|       |
  755|       |        /* 6.2.5.7 3d */
  756|   149M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (756:21): [True: 90.6M, False: 58.7M]
  ------------------
  757|  90.6M|            byte result = 0;
  758|  90.6M|            int64_t x_minor;
  759|  90.6M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (759:35): [True: 31.9M, False: 58.7M]
  ------------------
  760|       |
  761|  90.6M|            if (line1)
  ------------------
  |  Branch (761:17): [True: 75.2M, False: 15.4M]
  ------------------
  762|  75.2M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (762:45): [True: 16.5M, False: 58.6M]
  ------------------
  763|       |
  764|  90.6M|            if (line2)
  ------------------
  |  Branch (764:17): [True: 66.0M, False: 24.5M]
  ------------------
  765|  66.0M|                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 4 : 0);
  ------------------
  |  Branch (765:45): [True: 7.37M, False: 58.6M]
  ------------------
  766|       |
  767|       |            /* This is the speed-critical inner loop. */
  768|   406M|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (768:31): [True: 315M, False: 90.6M]
  ------------------
  769|   315M|                int bit;
  770|       |
  771|   315M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  772|   315M|                if (bit < 0)
  ------------------
  |  Branch (772:21): [True: 0, False: 315M]
  ------------------
  773|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 optimized");
  774|   315M|                result |= bit << (7 - x_minor);
  775|   315M|                CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80);
  776|   315M|            }
  777|  90.6M|            gbreg_line[x >> 3] = result;
  778|  90.6M|        }
  779|       |#ifdef OUTPUT_PBM
  780|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  781|       |#endif
  782|  58.7M|        line2 = line1;
  783|  58.7M|        line1 = gbreg_line;
  784|  58.7M|        gbreg_line += rowstride;
  785|  58.7M|    }
  786|       |
  787|  5.86k|    return 0;
  788|  5.86k|}
jbig2_generic.c:jbig2_decode_generic_template2_unopt:
  658|  38.9k|{
  659|  38.9k|    const int64_t GBW = image->width;
  660|  38.9k|    const int64_t GBH = image->height;
  661|  38.9k|    uint32_t CONTEXT;
  662|  38.9k|    int64_t x, y;
  663|  38.9k|    int bit;
  664|       |
  665|  38.9k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  38.9k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 38.9k]
  |  |  |  Branch (440:20): [True: 2, False: 38.9k]
  |  |  |  Branch (440:31): [True: 0, False: 38.9k]
  |  |  |  Branch (440:46): [True: 38.4k, False: 560]
  |  |  |  Branch (440:57): [True: 0, False: 38.4k]
  |  |  |  Branch (440:72): [True: 560, False: 38.4k]
  |  |  |  Branch (440:84): [True: 5, False: 555]
  |  |  ------------------
  ------------------
  666|      7|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  667|      7|                           "adaptive template pixel is out of field");
  668|       |
  669|   136M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (669:17): [True: 136M, False: 38.9k]
  ------------------
  670|   136M|        uint32_t out_byte = 0;
  671|   136M|        int out_bits_to_go_in_byte = 8;
  672|   136M|        uint8_t *d = &image->data[image->stride * y];
  673|   136M|        uint8_t *pline  = &image->data[image->stride * (y-1)];
  674|   136M|        uint8_t *ppline = &image->data[image->stride * (y-2)];
  675|   136M|        uint32_t pd = 0;
  676|   136M|        uint32_t ppd = 0;
  677|   136M|        if (y > 0) {
  ------------------
  |  Branch (677:13): [True: 136M, False: 38.9k]
  ------------------
  678|   136M|            pd = (*pline++ << 8);
  679|   136M|            if (GBW > 8)
  ------------------
  |  Branch (679:17): [True: 3.24M, False: 133M]
  ------------------
  680|  3.24M|                pd |= *pline++;
  681|   136M|            if (y > 1) {
  ------------------
  |  Branch (681:17): [True: 136M, False: 38.6k]
  ------------------
  682|   136M|                ppd = (*ppline++ << 8);
  683|   136M|                if (GBW > 8)
  ------------------
  |  Branch (683:21): [True: 3.20M, False: 133M]
  ------------------
  684|  3.20M|                    ppd |= *ppline++;
  685|   136M|            }
  686|   136M|        }
  687|  1.34G|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (687:21): [True: 1.20G, False: 136M]
  ------------------
  688|  1.20G|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (688:17): [True: 102M, False: 1.10G]
  |  Branch (688:36): [True: 102M, False: 64.9k]
  ------------------
  689|   102M|                bit = 0;
  690|  1.10G|            } else {
  691|  1.10G|                CONTEXT  = out_byte & 0x003; /* First 2 pixels */
  692|  1.10G|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
  693|  1.10G|                CONTEXT |= (pd>>11) & 0x078; /* Next 4 pixels */
  694|  1.10G|                CONTEXT |= (ppd>>7) & 0x380; /* Next 3 pixels */
  695|  1.10G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  696|  1.10G|                if (bit < 0)
  ------------------
  |  Branch (696:21): [True: 0, False: 1.10G]
  ------------------
  697|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 unoptimized");
  698|  1.10G|            }
  699|  1.20G|            pd = pd<<1;
  700|  1.20G|            ppd = ppd<<1;
  701|  1.20G|            out_byte = (out_byte<<1) | bit;
  702|  1.20G|            out_bits_to_go_in_byte--;
  703|  1.20G|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  704|  1.20G|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (704:17): [True: 114M, False: 1.09G]
  ------------------
  705|   114M|                out_bits_to_go_in_byte = 8;
  706|   114M|                d++;
  707|   114M|                if (x+9 < GBW && y > 0) {
  ------------------
  |  Branch (707:21): [True: 110M, False: 3.67M]
  |  Branch (707:34): [True: 57.1M, False: 53.7M]
  ------------------
  708|  57.1M|                    pd |= *pline++;
  709|  57.1M|                    if (y > 1)
  ------------------
  |  Branch (709:25): [True: 45.5M, False: 11.5M]
  ------------------
  710|  45.5M|                        ppd |= *ppline++;
  711|  57.1M|                }
  712|   114M|            }
  713|  1.20G|        }
  714|   136M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (714:13): [True: 136M, False: 391k]
  ------------------
  715|   136M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  716|   136M|    }
  717|       |
  718|  38.9k|    return 0;
  719|  38.9k|}
jbig2_generic.c:jbig2_decode_generic_template3:
  794|  4.29k|{
  795|  4.29k|    const int64_t GBW = image->width;
  796|  4.29k|    const int64_t GBH = image->height;
  797|  4.29k|    const uint32_t rowstride = image->stride;
  798|  4.29k|    byte *line1 = NULL;
  799|  4.29k|    byte *gbreg_line = (byte *) image->data;
  800|  4.29k|    int64_t x, y;
  801|       |
  802|  4.29k|    (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.29k|    if (GBW <= 0)
  ------------------
  |  Branch (810:9): [True: 0, False: 4.29k]
  ------------------
  811|      0|        return 0;
  812|       |
  813|  43.8k|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (813:17): [True: 39.5k, False: 4.29k]
  ------------------
  814|  39.5k|        uint32_t CONTEXT;
  815|  39.5k|        uint32_t line_m1;
  816|  39.5k|        int64_t padded_width = (GBW + 7) & -8;
  817|       |
  818|  39.5k|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (818:19): [True: 35.2k, False: 4.29k]
  ------------------
  819|  39.5k|        CONTEXT = (line_m1 >> 1) & 0x3f0;
  820|       |
  821|       |        /* 6.2.5.7 3d */
  822|   103M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (822:21): [True: 103M, False: 39.5k]
  ------------------
  823|   103M|            byte result = 0;
  824|   103M|            int64_t x_minor;
  825|   103M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (825:35): [True: 103M, False: 39.5k]
  ------------------
  826|       |
  827|   103M|            if (line1)
  ------------------
  |  Branch (827:17): [True: 89.8M, False: 13.7M]
  ------------------
  828|  89.8M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (828:45): [True: 89.7M, False: 35.2k]
  ------------------
  829|       |
  830|       |            /* This is the speed-critical inner loop. */
  831|   932M|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (831:31): [True: 828M, False: 103M]
  ------------------
  832|   828M|                int bit;
  833|       |
  834|   828M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  835|   828M|                if (bit < 0)
  ------------------
  |  Branch (835:21): [True: 0, False: 828M]
  ------------------
  836|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 optimized");
  837|   828M|                result |= bit << (7 - x_minor);
  838|   828M|                CONTEXT = ((CONTEXT & 0x1f7) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x10);
  839|   828M|            }
  840|   103M|            gbreg_line[x >> 3] = result;
  841|   103M|        }
  842|       |#ifdef OUTPUT_PBM
  843|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  844|       |#endif
  845|  39.5k|        line1 = gbreg_line;
  846|  39.5k|        gbreg_line += rowstride;
  847|  39.5k|    }
  848|       |
  849|  4.29k|    return 0;
  850|  4.29k|}
jbig2_generic.c:jbig2_decode_generic_template3_unopt:
  856|  5.30k|{
  857|  5.30k|    const int64_t GBW = image->width;
  858|  5.30k|    const int64_t GBH = image->height;
  859|  5.30k|    uint32_t CONTEXT;
  860|  5.30k|    int64_t x, y;
  861|  5.30k|    int bit;
  862|       |
  863|  5.30k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  5.30k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 5.30k]
  |  |  |  Branch (440:20): [True: 2, False: 5.30k]
  |  |  |  Branch (440:31): [True: 0, False: 5.30k]
  |  |  |  Branch (440:46): [True: 4.25k, False: 1.04k]
  |  |  |  Branch (440:57): [True: 0, False: 4.25k]
  |  |  |  Branch (440:72): [True: 1.04k, False: 4.25k]
  |  |  |  Branch (440:84): [True: 2, False: 1.04k]
  |  |  ------------------
  ------------------
  864|      4|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  865|      4|                           "adaptive template pixel is out of field");
  866|       |
  867|  24.5M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (867:17): [True: 24.5M, False: 5.29k]
  ------------------
  868|  24.5M|        uint32_t out_byte = 0;
  869|  24.5M|        int out_bits_to_go_in_byte = 8;
  870|  24.5M|        uint8_t *d = &image->data[image->stride * y];
  871|  24.5M|        uint8_t *pline  = &image->data[image->stride * (y-1)];
  872|  24.5M|        uint32_t pd = 0;
  873|  24.5M|        if (y > 0) {
  ------------------
  |  Branch (873:13): [True: 24.5M, False: 5.29k]
  ------------------
  874|  24.5M|            pd = (*pline++ << 8);
  875|  24.5M|            if (GBW > 8)
  ------------------
  |  Branch (875:17): [True: 1.93M, False: 22.5M]
  ------------------
  876|  1.93M|                pd |= *pline++;
  877|  24.5M|        }
  878|   883M|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (878:21): [True: 859M, False: 24.5M]
  ------------------
  879|   859M|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (879:17): [True: 65.4M, False: 793M]
  |  Branch (879:36): [True: 65.4M, False: 20.8k]
  ------------------
  880|  65.4M|                bit = 0;
  881|   793M|            } else {
  882|   793M|                CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
  883|   793M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
  884|   793M|                CONTEXT |= (pd>>9) & 0x3E0; /* Next 5 pixels */
  885|   793M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  886|   793M|                if (bit < 0)
  ------------------
  |  Branch (886:21): [True: 0, False: 793M]
  ------------------
  887|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 unoptimized");
  888|   793M|            }
  889|   859M|            pd = pd<<1;
  890|   859M|            out_byte = (out_byte<<1) | bit;
  891|   859M|            out_bits_to_go_in_byte--;
  892|   859M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  893|   859M|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (893:17): [True: 103M, False: 755M]
  ------------------
  894|   103M|                out_bits_to_go_in_byte = 8;
  895|   103M|                d++;
  896|   103M|                if (x+9 < GBW && y > 0)
  ------------------
  |  Branch (896:21): [True: 100M, False: 3.05M]
  |  Branch (896:34): [True: 60.7M, False: 39.6M]
  ------------------
  897|  60.7M|                    pd |= *pline++;
  898|   103M|            }
  899|   859M|        }
  900|  24.5M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (900:13): [True: 23.4M, False: 1.11M]
  ------------------
  901|  23.4M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  902|  24.5M|    }
  903|  5.29k|    return 0;
  904|  5.29k|}

jbig2_hd_release:
  109|  2.88k|{
  110|  2.88k|    int i;
  111|       |
  112|  2.88k|    if (dict == NULL)
  ------------------
  |  Branch (112:9): [True: 0, False: 2.88k]
  ------------------
  113|      0|        return;
  114|  2.88k|    if (dict->patterns != NULL)
  ------------------
  |  Branch (114:9): [True: 2.88k, False: 0]
  ------------------
  115|  9.16M|        for (i = 0; i < dict->n_patterns; i++)
  ------------------
  |  Branch (115:21): [True: 9.16M, False: 2.88k]
  ------------------
  116|  9.16M|            jbig2_image_release(ctx, dict->patterns[i]);
  117|  2.88k|    jbig2_free(ctx->allocator, dict->patterns);
  118|  2.88k|    jbig2_free(ctx->allocator, dict);
  119|  2.88k|}
jbig2_pattern_dictionary:
  200|  3.78k|{
  201|  3.78k|    Jbig2PatternDictParams params;
  202|  3.78k|    Jbig2ArithCx *GB_stats = NULL;
  203|  3.78k|    byte flags;
  204|  3.78k|    int offset = 0;
  205|       |
  206|       |    /* 7.4.4.1 - Data header */
  207|  3.78k|    if (segment->data_length < 7) {
  ------------------
  |  Branch (207:9): [True: 12, False: 3.77k]
  ------------------
  208|     12|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  209|     12|    }
  210|  3.77k|    flags = segment_data[0];
  211|  3.77k|    params.HDMMR = flags & 1;
  212|  3.77k|    params.HDTEMPLATE = (flags & 6) >> 1;
  213|  3.77k|    params.HDPW = segment_data[1];
  214|  3.77k|    params.HDPH = segment_data[2];
  215|  3.77k|    params.GRAYMAX = jbig2_get_uint32(segment_data + 3);
  216|  3.77k|    offset += 7;
  217|       |
  218|  3.77k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  219|  3.77k|                "pattern dictionary, flags=%02x, %d grays (%dx%d cell)", flags, params.GRAYMAX + 1, params.HDPW, params.HDPH);
  220|       |
  221|  3.77k|    if (params.HDMMR && params.HDTEMPLATE) {
  ------------------
  |  Branch (221:9): [True: 3.45k, False: 315]
  |  Branch (221:25): [True: 2.89k, False: 566]
  ------------------
  222|  2.89k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HDTEMPLATE is %d when HDMMR is %d, contrary to spec", params.HDTEMPLATE, params.HDMMR);
  223|  2.89k|    }
  224|  3.77k|    if (flags & 0xf8) {
  ------------------
  |  Branch (224:9): [True: 3.28k, False: 493]
  ------------------
  225|  3.28k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved flag bits non-zero");
  226|  3.28k|    }
  227|       |
  228|       |    /* 7.4.4.2 */
  229|  3.77k|    if (!params.HDMMR) {
  ------------------
  |  Branch (229:9): [True: 315, False: 3.45k]
  ------------------
  230|       |        /* allocate and zero arithmetic coding stats */
  231|    315|        int stats_size = jbig2_generic_stats_size(ctx, params.HDTEMPLATE);
  232|       |
  233|    315|        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|    315|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  234|    315|        if (GB_stats == NULL)
  ------------------
  |  Branch (234:13): [True: 1, False: 314]
  ------------------
  235|      1|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling pattern dictionary");
  236|    314|        memset(GB_stats, 0, stats_size);
  237|    314|    }
  238|       |
  239|  3.77k|    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|  3.77k|    if (!params.HDMMR) {
  ------------------
  |  Branch (242:9): [True: 314, False: 3.45k]
  ------------------
  243|    314|        jbig2_free(ctx->allocator, GB_stats);
  244|    314|    }
  245|       |
  246|  3.77k|    return (segment->result != NULL) ? 0 : -1;
  ------------------
  |  Branch (246:12): [True: 2.88k, False: 889]
  ------------------
  247|  3.77k|}
jbig2_halftone_region:
  560|  2.17k|{
  561|  2.17k|    int offset = 0;
  562|  2.17k|    Jbig2RegionSegmentInfo region_info;
  563|  2.17k|    Jbig2HalftoneRegionParams params;
  564|  2.17k|    Jbig2Image *image = NULL;
  565|  2.17k|    Jbig2ArithCx *GB_stats = NULL;
  566|  2.17k|    int code = 0;
  567|       |
  568|       |    /* 7.4.5.1 */
  569|  2.17k|    if (segment->data_length < 17)
  ------------------
  |  Branch (569:9): [True: 4, False: 2.17k]
  ------------------
  570|      4|        goto too_short;
  571|  2.17k|    jbig2_get_region_segment_info(&region_info, segment_data);
  572|  2.17k|    offset += 17;
  573|       |
  574|  2.17k|    if (segment->data_length < 18)
  ------------------
  |  Branch (574:9): [True: 1, False: 2.17k]
  ------------------
  575|      1|        goto too_short;
  576|       |
  577|       |    /* 7.4.5.1.1 Figure 42 */
  578|  2.17k|    params.flags = segment_data[offset];
  579|  2.17k|    params.HMMR = params.flags & 1;
  580|  2.17k|    params.HTEMPLATE = (params.flags & 6) >> 1;
  581|  2.17k|    params.HENABLESKIP = (params.flags & 8) >> 3;
  582|  2.17k|    params.HCOMBOP = (Jbig2ComposeOp)((params.flags & 0x70) >> 4);
  583|  2.17k|    params.HDEFPIXEL = (params.flags & 0x80) >> 7;
  584|  2.17k|    offset += 1;
  585|       |
  586|  2.17k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  587|  2.17k|                "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.17k|    if (params.HMMR && params.HTEMPLATE) {
  ------------------
  |  Branch (589:9): [True: 1.85k, False: 314]
  |  Branch (589:24): [True: 110, False: 1.74k]
  ------------------
  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.17k|    if (params.HMMR && params.HENABLESKIP) {
  ------------------
  |  Branch (592:9): [True: 1.85k, False: 314]
  |  Branch (592:24): [True: 77, False: 1.78k]
  ------------------
  593|     77|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HENABLESKIP is %d when HMMR is %d, contrary to spec", params.HENABLESKIP, params.HMMR);
  594|     77|    }
  595|       |
  596|       |    /* 7.4.5.1.2 Figure 43 */
  597|  2.17k|    if (segment->data_length - offset < 16)
  ------------------
  |  Branch (597:9): [True: 7, False: 2.16k]
  ------------------
  598|      7|        goto too_short;
  599|  2.16k|    params.HGW = jbig2_get_uint32(segment_data + offset);
  600|  2.16k|    params.HGH = jbig2_get_uint32(segment_data + offset + 4);
  601|  2.16k|    params.HGX = jbig2_get_int32(segment_data + offset + 8);
  602|  2.16k|    params.HGY = jbig2_get_int32(segment_data + offset + 12);
  603|  2.16k|    offset += 16;
  604|       |
  605|       |    /* 7.4.5.1.3 Figure 44 */
  606|  2.16k|    if (segment->data_length - offset < 4)
  ------------------
  |  Branch (606:9): [True: 2, False: 2.16k]
  ------------------
  607|      2|        goto too_short;
  608|  2.16k|    params.HRX = jbig2_get_uint16(segment_data + offset);
  609|  2.16k|    params.HRY = jbig2_get_uint16(segment_data + offset + 2);
  610|  2.16k|    offset += 4;
  611|       |
  612|  2.16k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  613|  2.16k|                "grid %d x %d @ (%d.%d,%d.%d) vector (%d.%d,%d.%d)",
  614|  2.16k|                params.HGW, params.HGH,
  615|  2.16k|                params.HGX >> 8, params.HGX & 0xff,
  616|  2.16k|                params.HGY >> 8, params.HGY & 0xff,
  617|  2.16k|                params.HRX >> 8, params.HRX & 0xff,
  618|  2.16k|                params.HRY >> 8, params.HRY & 0xff);
  619|       |
  620|       |    /* 7.4.5.2 */
  621|  2.16k|    if (!params.HMMR) {
  ------------------
  |  Branch (621:9): [True: 311, False: 1.85k]
  ------------------
  622|       |        /* allocate and zero arithmetic coding stats */
  623|    311|        int stats_size = jbig2_generic_stats_size(ctx, params.HTEMPLATE);
  624|       |
  625|    311|        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|    311|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  626|    311|        if (GB_stats == NULL) {
  ------------------
  |  Branch (626:13): [True: 1, False: 310]
  ------------------
  627|      1|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder states in halftone region");
  628|      1|        }
  629|    310|        memset(GB_stats, 0, stats_size);
  630|    310|    }
  631|       |
  632|  2.16k|    image = jbig2_image_new(ctx, region_info.width, region_info.height);
  633|  2.16k|    if (image == NULL) {
  ------------------
  |  Branch (633:9): [True: 21, False: 2.14k]
  ------------------
  634|     21|        jbig2_free(ctx->allocator, GB_stats);
  635|     21|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate halftone image");
  636|     21|    }
  637|       |
  638|  2.14k|    code = jbig2_decode_halftone_region(ctx, segment, &params, segment_data + offset, segment->data_length - offset, image, GB_stats);
  639|  2.14k|    if (code < 0) {
  ------------------
  |  Branch (639:9): [True: 131, False: 2.01k]
  ------------------
  640|    131|        jbig2_image_release(ctx, image);
  641|    131|        jbig2_free(ctx->allocator, GB_stats);
  642|    131|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode halftone region");
  643|    131|    }
  644|       |
  645|       |    /* todo: retain GB_stats? */
  646|  2.01k|    if (!params.HMMR) {
  ------------------
  |  Branch (646:9): [True: 251, False: 1.76k]
  ------------------
  647|    251|        jbig2_free(ctx->allocator, GB_stats);
  648|    251|    }
  649|       |
  650|  2.01k|    code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, region_info.x, region_info.y, region_info.op);
  651|  2.01k|    if (code < 0) {
  ------------------
  |  Branch (651:9): [True: 124, False: 1.88k]
  ------------------
  652|    124|        jbig2_image_release(ctx, image);
  653|    124|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add halftone region to page");
  654|    124|    }
  655|       |
  656|  1.88k|    jbig2_image_release(ctx, image);
  657|       |
  658|  1.88k|    return code;
  659|       |
  660|     14|too_short:
  661|     14|    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  662|  2.01k|}
jbig2_halftone.c:jbig2_decode_pattern_dict:
  140|  3.77k|{
  141|  3.77k|    Jbig2PatternDict *hd = NULL;
  142|  3.77k|    Jbig2Image *image = NULL;
  143|  3.77k|    Jbig2GenericRegionParams rparams;
  144|  3.77k|    int code = 0;
  145|       |
  146|       |    /* allocate the collective image */
  147|  3.77k|    image = jbig2_image_new(ctx, params->HDPW * (params->GRAYMAX + 1), params->HDPH);
  148|  3.77k|    if (image == NULL) {
  ------------------
  |  Branch (148:9): [True: 41, False: 3.73k]
  ------------------
  149|     41|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate collective bitmap for halftone dictionary");
  150|     41|        return NULL;
  151|     41|    }
  152|       |
  153|       |    /* fill out the generic region decoder parameters */
  154|  3.73k|    rparams.MMR = params->HDMMR;
  155|  3.73k|    rparams.GBTEMPLATE = params->HDTEMPLATE;
  156|  3.73k|    rparams.TPGDON = 0;         /* not used if HDMMR = 1 */
  157|  3.73k|    rparams.USESKIP = 0;
  158|  3.73k|    rparams.gbat[0] = -(int8_t) params->HDPW;
  159|  3.73k|    rparams.gbat[1] = 0;
  160|  3.73k|    rparams.gbat[2] = -3;
  161|  3.73k|    rparams.gbat[3] = -1;
  162|  3.73k|    rparams.gbat[4] = 2;
  163|  3.73k|    rparams.gbat[5] = -2;
  164|  3.73k|    rparams.gbat[6] = -2;
  165|  3.73k|    rparams.gbat[7] = -2;
  166|       |
  167|  3.73k|    if (params->HDMMR) {
  ------------------
  |  Branch (167:9): [True: 3.42k, False: 303]
  ------------------
  168|  3.42k|        code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data, size, image);
  169|  3.42k|    } else {
  170|    303|        Jbig2WordStream *ws = jbig2_word_stream_buf_new(ctx, data, size);
  171|       |
  172|    303|        if (ws != NULL) {
  ------------------
  |  Branch (172:13): [True: 302, False: 1]
  ------------------
  173|    302|            Jbig2ArithState *as = jbig2_arith_new(ctx, ws);
  174|       |
  175|    302|            if (as != NULL) {
  ------------------
  |  Branch (175:17): [True: 297, False: 5]
  ------------------
  176|    297|                code = jbig2_decode_generic_region(ctx, segment, &rparams, as, image, GB_stats);
  177|    297|            } 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|    302|            jbig2_free(ctx->allocator, as);
  182|    302|            jbig2_word_stream_buf_free(ctx, ws);
  183|    302|        } else {
  184|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate word stream when handling halftone dictionary");
  185|      1|        }
  186|    303|    }
  187|       |
  188|  3.73k|    if (code == 0)
  ------------------
  |  Branch (188:9): [True: 3.58k, False: 144]
  ------------------
  189|  3.58k|        hd = jbig2_hd_new(ctx, params, image);
  190|    144|    else
  191|    144|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode immediate generic region");
  192|  3.73k|    jbig2_image_release(ctx, image);
  193|       |
  194|  3.73k|    return hd;
  195|  3.77k|}
jbig2_halftone.c:jbig2_hd_new:
   44|  3.58k|{
   45|  3.58k|    Jbig2PatternDict *new;
   46|  3.58k|    const uint32_t N = params->GRAYMAX + 1;
   47|  3.58k|    const uint32_t HPW = params->HDPW;
   48|  3.58k|    const uint32_t HPH = params->HDPH;
   49|  3.58k|    int code;
   50|  3.58k|    uint32_t i, j;
   51|       |
   52|  3.58k|    if (N == 0) {
  ------------------
  |  Branch (52:9): [True: 0, False: 3.58k]
  ------------------
   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.58k|    new = jbig2_new(ctx, Jbig2PatternDict, 1);
  ------------------
  |  |  141|  3.58k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   60|  3.58k|    if (new != NULL) {
  ------------------
  |  Branch (60:9): [True: 3.58k, False: 1]
  ------------------
   61|  3.58k|        new->patterns = jbig2_new(ctx, Jbig2Image *, N);
  ------------------
  |  |  141|  3.58k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   62|  3.58k|        if (new->patterns == NULL) {
  ------------------
  |  Branch (62:13): [True: 594, False: 2.99k]
  ------------------
   63|    594|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate pattern in collective bitmap dictionary");
  ------------------
  |  |   68|    594|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   64|    594|            jbig2_free(ctx->allocator, new);
   65|    594|            return NULL;
   66|    594|        }
   67|  2.99k|        new->n_patterns = N;
   68|  2.99k|        new->HPW = HPW;
   69|  2.99k|        new->HPH = HPH;
   70|       |
   71|       |        /* 6.7.5(4) - copy out the individual pattern images */
   72|  14.5M|        for (i = 0; i < N; i++) {
  ------------------
  |  Branch (72:21): [True: 14.5M, False: 2.88k]
  ------------------
   73|  14.5M|            new->patterns[i] = jbig2_image_new(ctx, HPW, HPH);
   74|  14.5M|            if (new->patterns[i] == NULL) {
  ------------------
  |  Branch (74:17): [True: 109, False: 14.5M]
  ------------------
   75|    109|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate pattern element image");
  ------------------
  |  |   68|    109|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   76|       |                /* new->patterns[i] above did not succeed, so releasing patterns 0..i-1 is enough */
   77|  5.42M|                for (j = 0; j < i; j++)
  ------------------
  |  Branch (77:29): [True: 5.42M, False: 109]
  ------------------
   78|  5.42M|                    jbig2_image_release(ctx, new->patterns[j]);
   79|    109|                jbig2_free(ctx->allocator, new->patterns);
   80|    109|                jbig2_free(ctx->allocator, new);
   81|    109|                return NULL;
   82|    109|            }
   83|       |            /* compose with the REPLACE operator; the source
   84|       |               will be clipped to the destination, selecting the
   85|       |               proper sub image */
   86|  14.5M|            code = jbig2_image_compose(ctx, new->patterns[i], image, -((int64_t) i) * HPW, 0, JBIG2_COMPOSE_REPLACE);
   87|  14.5M|            if (code < 0) {
  ------------------
  |  Branch (87:17): [True: 0, False: 14.5M]
  ------------------
   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|  14.5M|        }
   97|  2.99k|    } 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.88k|    return new;
  102|  3.58k|}
jbig2_halftone.c:jbig2_decode_halftone_region:
  464|  2.14k|{
  465|  2.14k|    uint32_t HBPP;
  466|  2.14k|    uint32_t HNUMPATS;
  467|  2.14k|    uint16_t **GI = NULL;
  468|  2.14k|    Jbig2Image *HSKIP = NULL;
  469|  2.14k|    Jbig2PatternDict *HPATS;
  470|  2.14k|    uint32_t i;
  471|  2.14k|    int64_t mg, ng;
  472|  2.14k|    uint16_t gray_val;
  473|  2.14k|    int code = 0;
  474|       |
  475|       |    /* We need the patterns used in this region, get them from the referred pattern dictionary */
  476|  2.14k|    HPATS = jbig2_decode_ht_region_get_hpats(ctx, segment);
  477|  2.14k|    if (!HPATS) {
  ------------------
  |  Branch (477:9): [True: 16, False: 2.12k]
  ------------------
  478|     16|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "no pattern dictionary found, skipping halftone image");
  479|     16|        goto cleanup;
  480|     16|    }
  481|       |
  482|       |    /* 6.6.5 point 1. Fill bitmap with HDEFPIXEL */
  483|  2.12k|    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.12k|    if (params->HENABLESKIP == 1) {
  ------------------
  |  Branch (486:9): [True: 304, False: 1.82k]
  ------------------
  487|    304|        HSKIP = jbig2_image_new(ctx, params->HGW, params->HGH);
  488|    304|        if (HSKIP == NULL)
  ------------------
  |  Branch (488:13): [True: 3, False: 301]
  ------------------
  489|      3|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate skip image");
  490|       |
  491|   179M|        for (mg = 0; mg < params->HGH; ++mg) {
  ------------------
  |  Branch (491:22): [True: 179M, False: 301]
  ------------------
  492|  1.20G|            for (ng = 0; ng < params->HGW; ++ng) {
  ------------------
  |  Branch (492:26): [True: 1.02G, False: 179M]
  ------------------
  493|  1.02G|                int64_t x = ((int64_t) params->HGX + mg * params->HRY + ng * params->HRX) >> 8;
  494|  1.02G|                int64_t y = ((int64_t) params->HGY + mg * params->HRX - ng * params->HRY) >> 8;
  495|       |
  496|  1.02G|                if (x + HPATS->HPW <= 0 || x >= image->width || y + HPATS->HPH <= 0 || y >= image->height) {
  ------------------
  |  Branch (496:21): [True: 205M, False: 815M]
  |  Branch (496:44): [True: 724M, False: 91.1M]
  |  Branch (496:65): [True: 9.99M, False: 81.1M]
  |  Branch (496:88): [True: 78.3M, False: 2.80M]
  ------------------
  497|  1.01G|                    jbig2_image_set_pixel(HSKIP, ng, mg, 1);
  498|  1.01G|                } else {
  499|  2.80M|                    jbig2_image_set_pixel(HSKIP, ng, mg, 0);
  500|  2.80M|                }
  501|  1.02G|            }
  502|   179M|        }
  503|    301|    }
  504|       |
  505|       |    /* 6.6.5 point 3. set HBPP to ceil(log2(HNUMPATS)): */
  506|  2.12k|    HNUMPATS = HPATS->n_patterns;
  507|  2.12k|    HBPP = 0;
  508|  16.5k|    while (HNUMPATS > (1U << ++HBPP));
  ------------------
  |  Branch (508:12): [True: 14.4k, False: 2.12k]
  ------------------
  509|  2.12k|    if (HBPP > 16) {
  ------------------
  |  Branch (509:9): [True: 3, False: 2.12k]
  ------------------
  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.12k|    GI = jbig2_decode_gray_scale_image(ctx, segment, data, size,
  516|  2.12k|                                       params->HMMR, params->HGW, params->HGH, HBPP, params->HENABLESKIP, HSKIP, params->HTEMPLATE, GB_stats);
  517|  2.12k|    if (!GI) {
  ------------------
  |  Branch (517:9): [True: 109, False: 2.01k]
  ------------------
  518|    109|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to acquire gray-scale image, skipping halftone image");
  519|    109|        goto cleanup;
  520|    109|    }
  521|       |
  522|       |    /* 6.6.5 point 5. place patterns with procedure mentioned in 6.6.5.2 */
  523|  5.45M|    for (mg = 0; mg < params->HGH; ++mg) {
  ------------------
  |  Branch (523:18): [True: 5.45M, False: 2.01k]
  ------------------
  524|  99.5M|        for (ng = 0; ng < params->HGW; ++ng) {
  ------------------
  |  Branch (524:22): [True: 94.1M, False: 5.45M]
  ------------------
  525|  94.1M|            int64_t x = ((int64_t) params->HGX + mg * params->HRY + ng * params->HRX) >> 8;
  526|  94.1M|            int64_t y = ((int64_t) params->HGY + mg * params->HRX - ng * params->HRY) >> 8;
  527|       |
  528|       |            /* prevent pattern index >= HNUMPATS */
  529|  94.1M|            gray_val = GI[ng][mg];
  530|  94.1M|            if (gray_val >= HNUMPATS) {
  ------------------
  |  Branch (530:17): [True: 12.5M, False: 81.5M]
  ------------------
  531|  12.5M|                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|  12.5M|                gray_val = HNUMPATS - 1;
  534|  12.5M|            }
  535|  94.1M|            code = jbig2_image_compose(ctx, image, HPATS->patterns[gray_val], x, y, params->HCOMBOP);
  536|  94.1M|            if (code < 0) {
  ------------------
  |  Branch (536:17): [True: 0, False: 94.1M]
  ------------------
  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|  94.1M|        }
  541|  5.45M|    }
  542|       |
  543|  2.13k|cleanup:
  544|  2.13k|    if (GI) {
  ------------------
  |  Branch (544:9): [True: 2.01k, False: 128]
  ------------------
  545|  5.67M|        for (i = 0; i < params->HGW; ++i) {
  ------------------
  |  Branch (545:21): [True: 5.67M, False: 2.01k]
  ------------------
  546|  5.67M|            jbig2_free(ctx->allocator, GI[i]);
  547|  5.67M|        }
  548|  2.01k|    }
  549|  2.13k|    jbig2_free(ctx->allocator, GI);
  550|  2.13k|    jbig2_image_release(ctx, HSKIP);
  551|       |
  552|  2.13k|    return code;
  553|  2.01k|}
jbig2_halftone.c:jbig2_decode_ht_region_get_hpats:
  425|  2.14k|{
  426|  2.14k|    int index = 0;
  427|  2.14k|    Jbig2PatternDict *pattern_dict = NULL;
  428|  2.14k|    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.84k, False: 16]
  ------------------
  432|  2.84k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  433|  2.84k|        if (rsegment) {
  ------------------
  |  Branch (433:13): [True: 2.48k, False: 355]
  ------------------
  434|       |            /* segment type is pattern dictionary and result is not empty */
  435|  2.48k|            if ((rsegment->flags & 0x3f) == 16 && rsegment->result) {
  ------------------
  |  Branch (435:17): [True: 2.12k, False: 359]
  |  Branch (435:51): [True: 2.12k, False: 0]
  ------------------
  436|  2.12k|                pattern_dict = (Jbig2PatternDict *) rsegment->result;
  437|  2.12k|                return pattern_dict;
  438|  2.12k|            }
  439|  2.48k|        }
  440|    714|        index++;
  441|    714|    }
  442|     16|    return pattern_dict;
  443|  2.14k|}
jbig2_halftone.c:jbig2_decode_gray_scale_image:
  275|  2.12k|{
  276|  2.12k|    uint16_t **GSVALS = NULL;
  277|  2.12k|    size_t consumed_bytes = 0;
  278|  2.12k|    uint32_t i, j, stride;
  279|  2.12k|    int64_t x, y;
  280|  2.12k|    int code;
  281|  2.12k|    Jbig2Image **GSPLANES;
  282|  2.12k|    Jbig2GenericRegionParams rparams;
  283|  2.12k|    Jbig2WordStream *ws = NULL;
  284|  2.12k|    Jbig2ArithState *as = NULL;
  285|       |
  286|       |    /* allocate GSPLANES */
  287|  2.12k|    GSPLANES = jbig2_new(ctx, Jbig2Image *, GSBPP);
  ------------------
  |  |  141|  2.12k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  288|  2.12k|    if (GSPLANES == NULL) {
  ------------------
  |  Branch (288:9): [True: 1, False: 2.11k]
  ------------------
  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|  18.4k|    for (i = 0; i < GSBPP; ++i) {
  ------------------
  |  Branch (293:17): [True: 16.3k, False: 2.10k]
  ------------------
  294|  16.3k|        GSPLANES[i] = jbig2_image_new(ctx, GSW, GSH);
  295|  16.3k|        if (GSPLANES[i] == NULL) {
  ------------------
  |  Branch (295:13): [True: 19, False: 16.3k]
  ------------------
  296|     19|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate %dx%d image for GSPLANES", GSW, GSH);
  297|       |            /* free already allocated */
  298|     55|            for (j = i; j > 0;)
  ------------------
  |  Branch (298:25): [True: 36, False: 19]
  ------------------
  299|     36|                jbig2_image_release(ctx, GSPLANES[--j]);
  300|     19|            jbig2_free(ctx->allocator, GSPLANES);
  301|     19|            return NULL;
  302|     19|        }
  303|  16.3k|    }
  304|       |
  305|       |    /* C.5 step 1. Decode GSPLANES[GSBPP-1] */
  306|       |    /* fill generic region decoder parameters */
  307|  2.10k|    rparams.MMR = GSMMR;
  308|  2.10k|    rparams.GBTEMPLATE = GSTEMPLATE;
  309|  2.10k|    rparams.TPGDON = 0;
  310|  2.10k|    rparams.USESKIP = GSUSESKIP;
  311|  2.10k|    rparams.SKIP = GSKIP;
  312|  2.10k|    rparams.gbat[0] = (GSTEMPLATE <= 1 ? 3 : 2);
  ------------------
  |  Branch (312:24): [True: 1.90k, False: 196]
  ------------------
  313|  2.10k|    rparams.gbat[1] = -1;
  314|  2.10k|    rparams.gbat[2] = -3;
  315|  2.10k|    rparams.gbat[3] = -1;
  316|  2.10k|    rparams.gbat[4] = 2;
  317|  2.10k|    rparams.gbat[5] = -2;
  318|  2.10k|    rparams.gbat[6] = -2;
  319|  2.10k|    rparams.gbat[7] = -2;
  320|       |
  321|  2.10k|    if (GSMMR) {
  ------------------
  |  Branch (321:9): [True: 1.82k, False: 275]
  ------------------
  322|  1.82k|        code = jbig2_decode_halftone_mmr(ctx, &rparams, data, size, GSPLANES[GSBPP - 1], &consumed_bytes);
  323|  1.82k|    } else {
  324|    275|        ws = jbig2_word_stream_buf_new(ctx, data, size);
  325|    275|        if (ws == NULL) {
  ------------------
  |  Branch (325:13): [True: 1, False: 274]
  ------------------
  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|    274|        as = jbig2_arith_new(ctx, ws);
  331|    274|        if (as == NULL) {
  ------------------
  |  Branch (331:13): [True: 2, False: 272]
  ------------------
  332|      2|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when decoding gray scale image");
  333|      2|            goto cleanup;
  334|      2|        }
  335|       |
  336|    272|        code = jbig2_decode_generic_region(ctx, segment, &rparams, as, GSPLANES[GSBPP - 1], GB_stats);
  337|    272|    }
  338|  2.09k|    if (code < 0) {
  ------------------
  |  Branch (338:9): [True: 2, False: 2.09k]
  ------------------
  339|      2|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error decoding GSPLANES for halftone image");
  340|      2|        goto cleanup;
  341|      2|    }
  342|       |
  343|       |    /* C.5 step 2. Set j = GSBPP-2 */
  344|  2.09k|    j = GSBPP - 1;
  345|       |    /* C.5 step 3. decode loop */
  346|  16.1k|    while (j > 0) {
  ------------------
  |  Branch (346:12): [True: 14.0k, False: 2.05k]
  ------------------
  347|  14.0k|        j--;
  348|       |        /*  C.5 step 3. (a) */
  349|  14.0k|        if (GSMMR) {
  ------------------
  |  Branch (349:13): [True: 12.2k, False: 1.81k]
  ------------------
  350|  12.2k|            code = jbig2_decode_halftone_mmr(ctx, &rparams, data + consumed_bytes, size - consumed_bytes, GSPLANES[j], &consumed_bytes);
  351|  12.2k|        } else {
  352|  1.81k|            code = jbig2_decode_generic_region(ctx, segment, &rparams, as, GSPLANES[j], GB_stats);
  353|  1.81k|        }
  354|  14.0k|        if (code < 0) {
  ------------------
  |  Branch (354:13): [True: 43, False: 14.0k]
  ------------------
  355|     43|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode GSPLANES for halftone image");
  356|     43|            goto cleanup;
  357|     43|        }
  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.0k|        stride = GSPLANES[j]->stride;
  363|   241M|        for (i = 0; i < stride * GSH; ++i)
  ------------------
  |  Branch (363:21): [True: 241M, False: 14.0k]
  ------------------
  364|   241M|            GSPLANES[j]->data[i] ^= GSPLANES[j + 1]->data[i];
  365|       |
  366|       |        /*  C.5 step 3. (c) */
  367|  14.0k|    }
  368|       |
  369|       |    /* allocate GSVALS */
  370|  2.05k|    GSVALS = jbig2_new(ctx, uint16_t *, GSW);
  ------------------
  |  |  141|  2.05k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  371|  2.05k|    if (GSVALS == NULL) {
  ------------------
  |  Branch (371:9): [True: 9, False: 2.04k]
  ------------------
  372|      9|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSW);
  373|      9|        goto cleanup;
  374|      9|    }
  375|  11.1M|    for (i = 0; i < GSW; ++i) {
  ------------------
  |  Branch (375:17): [True: 11.1M, False: 2.01k]
  ------------------
  376|  11.1M|        GSVALS[i] = jbig2_new(ctx, uint16_t, GSH);
  ------------------
  |  |  141|  11.1M|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  377|  11.1M|        if (GSVALS[i] == NULL) {
  ------------------
  |  Branch (377:13): [True: 32, False: 11.1M]
  ------------------
  378|     32|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSH * GSW);
  379|       |            /* free already allocated */
  380|  5.43M|            for (j = i; j > 0;)
  ------------------
  |  Branch (380:25): [True: 5.43M, False: 32]
  ------------------
  381|  5.43M|                jbig2_free(ctx->allocator, GSVALS[--j]);
  382|     32|            jbig2_free(ctx->allocator, GSVALS);
  383|     32|            GSVALS = NULL;
  384|     32|            goto cleanup;
  385|     32|        }
  386|  11.1M|    }
  387|       |
  388|       |    /*  C.5 step 4.  */
  389|  5.67M|    for (x = 0; x < GSW; ++x) {
  ------------------
  |  Branch (389:17): [True: 5.67M, False: 2.01k]
  ------------------
  390|  99.7M|        for (y = 0; y < GSH; ++y) {
  ------------------
  |  Branch (390:21): [True: 94.1M, False: 5.67M]
  ------------------
  391|  94.1M|            GSVALS[x][y] = 0;
  392|       |
  393|   626M|            for (j = 0; j < GSBPP; ++j)
  ------------------
  |  Branch (393:25): [True: 532M, False: 94.1M]
  ------------------
  394|   532M|                GSVALS[x][y] += jbig2_image_get_pixel(GSPLANES[j], x, y) << j;
  395|  94.1M|        }
  396|  5.67M|    }
  397|       |
  398|  2.10k|cleanup:
  399|       |    /* free memory */
  400|  2.10k|    if (!GSMMR) {
  ------------------
  |  Branch (400:9): [True: 275, False: 1.82k]
  ------------------
  401|    275|        jbig2_free(ctx->allocator, as);
  402|    275|        jbig2_word_stream_buf_free(ctx, ws);
  403|    275|    }
  404|  18.4k|    for (i = 0; i < GSBPP; ++i)
  ------------------
  |  Branch (404:17): [True: 16.3k, False: 2.10k]
  ------------------
  405|  16.3k|        jbig2_image_release(ctx, GSPLANES[i]);
  406|       |
  407|  2.10k|    jbig2_free(ctx->allocator, GSPLANES);
  408|       |
  409|  2.10k|    return GSVALS;
  410|  2.01k|}

jbig2_huffman_new:
   69|  5.62k|{
   70|  5.62k|    Jbig2HuffmanState *result = NULL;
   71|  5.62k|    int code;
   72|       |
   73|  5.62k|    result = jbig2_new(ctx, Jbig2HuffmanState, 1);
  ------------------
  |  |  141|  5.62k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   74|       |
   75|  5.62k|    if (result != NULL) {
  ------------------
  |  Branch (75:9): [True: 5.62k, False: 2]
  ------------------
   76|  5.62k|        result->offset = 0;
   77|  5.62k|        result->offset_bits = 0;
   78|  5.62k|        result->offset_limit = 0;
   79|  5.62k|        result->ws = ws;
   80|  5.62k|        result->ctx = ctx;
   81|  5.62k|        code = huff_get_next_word(result, 0, &result->this_word);
  ------------------
  |  |   61|  5.62k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
   82|  5.62k|        if (code < 0) {
  ------------------
  |  Branch (82:13): [True: 0, False: 5.62k]
  ------------------
   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|  5.62k|        code = huff_get_next_word(result, 4, &result->next_word);
  ------------------
  |  |   61|  5.62k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
   88|  5.62k|        if (code < 0) {
  ------------------
  |  Branch (88:13): [True: 0, False: 5.62k]
  ------------------
   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|  5.62k|    } 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|  5.62k|    return result;
   99|  5.62k|}
jbig2_huffman_free:
  106|  14.3k|{
  107|  14.3k|    jbig2_free(ctx->allocator, hs);
  108|  14.3k|}
jbig2_huffman_skip:
  182|  23.3k|{
  183|  23.3k|    uint32_t bits = hs->offset_bits & 7;
  184|  23.3k|    int code;
  185|       |
  186|  23.3k|    if (bits) {
  ------------------
  |  Branch (186:9): [True: 18.4k, False: 4.91k]
  ------------------
  187|  18.4k|        bits = 8 - bits;
  188|  18.4k|        hs->offset_bits += bits;
  189|  18.4k|        hs->this_word = (hs->this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
  190|  18.4k|    }
  191|       |
  192|  23.3k|    if (hs->offset_bits >= 32) {
  ------------------
  |  Branch (192:9): [True: 5.46k, False: 17.9k]
  ------------------
  193|  5.46k|        hs->this_word = hs->next_word;
  194|  5.46k|        hs->offset += 4;
  195|  5.46k|        code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
  ------------------
  |  |   61|  5.46k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  196|  5.46k|        if (code < 0) {
  ------------------
  |  Branch (196:13): [True: 0, False: 5.46k]
  ------------------
  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.46k|        hs->offset_bits -= 32;
  200|  5.46k|        if (hs->offset_bits) {
  ------------------
  |  Branch (200:13): [True: 47, False: 5.42k]
  ------------------
  201|     47|            hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
  202|     47|        }
  203|  5.46k|    }
  204|  23.3k|    return 0;
  205|  23.3k|}
jbig2_huffman_advance:
  211|  20.7k|{
  212|  20.7k|    int code;
  213|  20.7k|    hs->offset += advance & ~3;
  214|  20.7k|    hs->offset_bits += ((uint32_t) (advance & 3)) << 3;
  215|  20.7k|    if (hs->offset_bits >= 32) {
  ------------------
  |  Branch (215:9): [True: 8.25k, False: 12.5k]
  ------------------
  216|  8.25k|        hs->offset += 4;
  217|  8.25k|        hs->offset_bits -= 32;
  218|  8.25k|    }
  219|  20.7k|    code = huff_get_next_word(hs, hs->offset, &hs->this_word);
  ------------------
  |  |   61|  20.7k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  220|  20.7k|    if (code < 0) {
  ------------------
  |  Branch (220:9): [True: 0, False: 20.7k]
  ------------------
  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|  20.7k|    code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
  ------------------
  |  |   61|  20.7k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  224|  20.7k|    if (code < 0) {
  ------------------
  |  Branch (224:9): [True: 0, False: 20.7k]
  ------------------
  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|  20.7k|    if (hs->offset_bits > 0)
  ------------------
  |  Branch (227:9): [True: 12.5k, False: 8.24k]
  ------------------
  228|  12.5k|        hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
  229|  20.7k|    return 0;
  230|  20.7k|}
jbig2_huffman_offset:
  237|  29.2k|{
  238|  29.2k|    return hs->offset + (hs->offset_bits >> 3);
  239|  29.2k|}
jbig2_huffman_get_bits:
  246|  6.38M|{
  247|  6.38M|    uint32_t this_word = hs->this_word;
  248|  6.38M|    int32_t result;
  249|  6.38M|    int code;
  250|       |
  251|  6.38M|    if (hs->offset_limit && hs->offset >= hs->offset_limit) {
  ------------------
  |  Branch (251:9): [True: 5.58M, False: 803k]
  |  Branch (251:29): [True: 0, False: 5.58M]
  ------------------
  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|  6.38M|    result = this_word >> (32 - bits);
  257|  6.38M|    hs->offset_bits += bits;
  258|  6.38M|    if (hs->offset_bits >= 32) {
  ------------------
  |  Branch (258:9): [True: 150k, False: 6.23M]
  ------------------
  259|   150k|        hs->offset += 4;
  260|   150k|        hs->offset_bits -= 32;
  261|   150k|        hs->this_word = hs->next_word;
  262|   150k|        code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
  ------------------
  |  |   61|   150k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  263|   150k|        if (code < 0) {
  ------------------
  |  Branch (263:13): [True: 0, False: 150k]
  ------------------
  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|   150k|        if (hs->offset_bits) {
  ------------------
  |  Branch (266:13): [True: 67.8k, False: 82.1k]
  ------------------
  267|  67.8k|            hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
  268|  82.1k|        } else {
  269|  82.1k|            hs->this_word = (hs->this_word << hs->offset_bits);
  270|  82.1k|        }
  271|  6.23M|    } else {
  272|  6.23M|        hs->this_word = (this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
  273|  6.23M|    }
  274|       |
  275|  6.38M|    return result;
  276|  6.38M|}
jbig2_huffman_get:
  280|  55.5M|{
  281|  55.5M|    Jbig2HuffmanEntry *entry;
  282|  55.5M|    byte flags;
  283|  55.5M|    int offset_bits = hs->offset_bits;
  284|  55.5M|    uint32_t this_word = hs->this_word;
  285|  55.5M|    uint32_t next_word;
  286|  55.5M|    int RANGELEN;
  287|  55.5M|    int32_t result;
  288|       |
  289|  55.5M|    if (hs->offset_limit && hs->offset >= hs->offset_limit) {
  ------------------
  |  Branch (289:9): [True: 53.7M, False: 1.80M]
  |  Branch (289:29): [True: 138, False: 53.7M]
  ------------------
  290|    138|        if (oob)
  ------------------
  |  Branch (290:13): [True: 138, False: 0]
  ------------------
  291|    138|            *oob = -1;
  292|    138|        return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "end of Jbig2WordStream reached at offset "FMTZ, (FMTZ_CAST) hs->offset);
  ------------------
  |  |   68|    138|#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|    138|#define FMTZ "%zu"
  ------------------
  293|    138|    }
  294|       |
  295|  55.5M|    for (;;) {
  296|  55.5M|        int log_table_size = table->log_table_size;
  297|  55.5M|        int PREFLEN;
  298|  55.5M|        int code;
  299|       |
  300|       |        /* SumatraPDF: shifting by the size of the operand is undefined */
  301|  55.5M|        entry = &table->entries[log_table_size > 0 ? this_word >> (32 - log_table_size) : 0];
  ------------------
  |  Branch (301:33): [True: 55.5M, False: 311]
  ------------------
  302|  55.5M|        flags = entry->flags;
  303|  55.5M|        PREFLEN = entry->PREFLEN;
  304|  55.5M|        if (flags == (byte) -1 || PREFLEN == (byte) -1) {
  ------------------
  |  Branch (304:13): [True: 54, False: 55.5M]
  |  Branch (304:35): [True: 0, False: 55.5M]
  ------------------
  305|     54|            if (oob)
  ------------------
  |  Branch (305:17): [True: 54, False: 0]
  ------------------
  306|     54|                *oob = -1;
  307|     54|            return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "encountered unpopulated huffman table entry");
  ------------------
  |  |   68|     54|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  308|     54|        }
  309|       |
  310|  55.5M|        next_word = hs->next_word;
  311|  55.5M|        offset_bits += PREFLEN;
  312|  55.5M|        if (offset_bits >= 32) {
  ------------------
  |  Branch (312:13): [True: 3.75M, False: 51.8M]
  ------------------
  313|  3.75M|            this_word = next_word;
  314|  3.75M|            hs->offset += 4;
  315|  3.75M|            code = huff_get_next_word(hs, hs->offset + 4, &next_word);
  ------------------
  |  |   61|  3.75M|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  316|  3.75M|            if (code < 0) {
  ------------------
  |  Branch (316:17): [True: 0, False: 3.75M]
  ------------------
  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|  3.75M|            else if (code < 4)
  ------------------
  |  Branch (319:22): [True: 3.70M, False: 45.8k]
  ------------------
  320|  3.70M|            {
  321|  3.70M|                hs->offset_limit = hs->offset + 4 + code;
  322|  3.70M|            }
  323|  3.75M|            offset_bits -= 32;
  324|  3.75M|            hs->next_word = next_word;
  325|  3.75M|            PREFLEN = offset_bits;
  326|  3.75M|        }
  327|  55.5M|        if (PREFLEN)
  ------------------
  |  Branch (327:13): [True: 53.2M, False: 2.34M]
  ------------------
  328|  53.2M|            this_word = (this_word << PREFLEN) | (next_word >> (32 - offset_bits));
  329|  55.5M|        if (flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
  ------------------
  |  |   44|  55.5M|#define JBIG2_HUFFMAN_FLAGS_ISEXT 4
  ------------------
  |  Branch (329:13): [True: 0, False: 55.5M]
  ------------------
  330|      0|            table = entry->u.ext_table;
  331|      0|        } else
  332|  55.5M|            break;
  333|  55.5M|    }
  334|  55.5M|    result = entry->u.RANGELOW;
  335|  55.5M|    RANGELEN = entry->RANGELEN;
  336|  55.5M|    if (RANGELEN > 0) {
  ------------------
  |  Branch (336:9): [True: 5.11k, False: 55.5M]
  ------------------
  337|  5.11k|        int32_t HTOFFSET;
  338|  5.11k|        int code;
  339|       |
  340|  5.11k|        HTOFFSET = this_word >> (32 - RANGELEN);
  341|  5.11k|        if (flags & JBIG2_HUFFMAN_FLAGS_ISLOW)
  ------------------
  |  |   43|  5.11k|#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
  ------------------
  |  Branch (341:13): [True: 572, False: 4.54k]
  ------------------
  342|    572|            result -= HTOFFSET;
  343|  4.54k|        else
  344|  4.54k|            result += HTOFFSET;
  345|       |
  346|  5.11k|        offset_bits += RANGELEN;
  347|  5.11k|        if (offset_bits >= 32) {
  ------------------
  |  Branch (347:13): [True: 4.51k, False: 602]
  ------------------
  348|  4.51k|            this_word = next_word;
  349|  4.51k|            hs->offset += 4;
  350|  4.51k|            code = huff_get_next_word(hs, hs->offset + 4, &next_word);
  ------------------
  |  |   61|  4.51k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  351|  4.51k|            if (code < 0) {
  ------------------
  |  Branch (351:17): [True: 0, False: 4.51k]
  ------------------
  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|  4.51k|            else if (code < 4)
  ------------------
  |  Branch (354:22): [True: 2.01k, False: 2.50k]
  ------------------
  355|  2.01k|            {
  356|  2.01k|                hs->offset_limit = hs->offset + 4 + code;
  357|  2.01k|            }
  358|  4.51k|            offset_bits -= 32;
  359|  4.51k|            hs->next_word = next_word;
  360|  4.51k|            RANGELEN = offset_bits;
  361|  4.51k|        }
  362|  5.11k|        if (RANGELEN)
  ------------------
  |  Branch (362:13): [True: 4.54k, False: 574]
  ------------------
  363|  4.54k|            this_word = (this_word << RANGELEN) | (next_word >> (32 - offset_bits));
  364|  5.11k|    }
  365|       |
  366|  55.5M|    hs->this_word = this_word;
  367|  55.5M|    hs->offset_bits = offset_bits;
  368|       |
  369|  55.5M|    if (oob != NULL)
  ------------------
  |  Branch (369:9): [True: 55.5M, False: 0]
  ------------------
  370|  55.5M|        *oob = (flags & JBIG2_HUFFMAN_FLAGS_ISOOB);
  ------------------
  |  |   42|  55.5M|#define JBIG2_HUFFMAN_FLAGS_ISOOB 1
  ------------------
  371|       |
  372|  55.5M|    return result;
  373|  55.5M|}
jbig2_build_huffman_table:
  385|  32.7k|{
  386|  32.7k|    int *LENCOUNT;
  387|  32.7k|    int LENMAX = -1;
  388|  32.7k|    const int lencountcount = 256;
  389|  32.7k|    const Jbig2HuffmanLine *lines = params->lines;
  390|  32.7k|    int n_lines = params->n_lines;
  391|  32.7k|    int i, j;
  392|  32.7k|    uint32_t max_j;
  393|  32.7k|    int log_table_size = 0;
  394|  32.7k|    Jbig2HuffmanTable *result;
  395|  32.7k|    Jbig2HuffmanEntry *entries;
  396|  32.7k|    int CURLEN;
  397|  32.7k|    int firstcode = 0;
  398|  32.7k|    int CURCODE;
  399|  32.7k|    int CURTEMP;
  400|       |
  401|  32.7k|    LENCOUNT = jbig2_new(ctx, int, lencountcount);
  ------------------
  |  |  141|  32.7k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  402|       |
  403|  32.7k|    if (LENCOUNT == NULL) {
  ------------------
  |  Branch (403:9): [True: 4, False: 32.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|  32.7k|    memset(LENCOUNT, 0, sizeof(int) * lencountcount);
  408|       |
  409|       |    /* B.3, 1. */
  410|  17.9M|    for (i = 0; i < params->n_lines; i++) {
  ------------------
  |  Branch (410:17): [True: 17.9M, False: 32.7k]
  ------------------
  411|  17.9M|        int PREFLEN = lines[i].PREFLEN;
  412|  17.9M|        int lts;
  413|       |
  414|  17.9M|        if (PREFLEN > LENMAX) {
  ------------------
  |  Branch (414:13): [True: 96.7k, False: 17.8M]
  ------------------
  415|   324k|            for (j = LENMAX + 1; j < PREFLEN + 1; j++)
  ------------------
  |  Branch (415:34): [True: 227k, False: 96.7k]
  ------------------
  416|   227k|                LENCOUNT[j] = 0;
  417|  96.7k|            LENMAX = PREFLEN;
  418|  96.7k|        }
  419|  17.9M|        LENCOUNT[PREFLEN]++;
  420|       |
  421|  17.9M|        lts = PREFLEN + lines[i].RANGELEN;
  422|  17.9M|        if (lts > LOG_TABLE_SIZE_MAX)
  ------------------
  |  |  378|  17.9M|#define LOG_TABLE_SIZE_MAX 16
  ------------------
  |  Branch (422:13): [True: 4.04M, False: 13.9M]
  ------------------
  423|  4.04M|            lts = PREFLEN;
  424|  17.9M|        if (lts <= LOG_TABLE_SIZE_MAX && log_table_size < lts)
  ------------------
  |  |  378|  35.8M|#define LOG_TABLE_SIZE_MAX 16
  ------------------
  |  Branch (424:13): [True: 13.9M, False: 3.98M]
  |  Branch (424:42): [True: 88.3k, False: 13.8M]
  ------------------
  425|  88.3k|            log_table_size = lts;
  426|  17.9M|    }
  427|  32.7k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "constructing huffman table log size %d", log_table_size);
  ------------------
  |  |   68|  32.7k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  428|  32.7k|    max_j = 1 << log_table_size;
  429|       |
  430|  32.7k|    result = jbig2_new(ctx, Jbig2HuffmanTable, 1);
  ------------------
  |  |  141|  32.7k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  431|  32.7k|    if (result == NULL) {
  ------------------
  |  Branch (431:9): [True: 6, False: 32.7k]
  ------------------
  432|      6|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate result");
  ------------------
  |  |   68|      6|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  433|      6|        jbig2_free(ctx->allocator, LENCOUNT);
  434|      6|        return NULL;
  435|      6|    }
  436|  32.7k|    result->log_table_size = log_table_size;
  437|  32.7k|    entries = jbig2_new(ctx, Jbig2HuffmanEntry, max_j);
  ------------------
  |  |  141|  32.7k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  438|  32.7k|    if (entries == NULL) {
  ------------------
  |  Branch (438:9): [True: 18, False: 32.6k]
  ------------------
  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|  32.6k|    memset(entries, 0xFF, sizeof(Jbig2HuffmanEntry) * max_j);
  446|  32.6k|    result->entries = entries;
  447|       |
  448|  32.6k|    LENCOUNT[0] = 0;
  449|       |
  450|   226k|    for (CURLEN = 1; CURLEN <= LENMAX; CURLEN++) {
  ------------------
  |  Branch (450:22): [True: 194k, False: 32.6k]
  ------------------
  451|   194k|        int shift = log_table_size - CURLEN;
  452|       |
  453|       |        /* B.3 3.(a) */
  454|   194k|        firstcode = (firstcode + LENCOUNT[CURLEN - 1]) << 1;
  455|   194k|        CURCODE = firstcode;
  456|       |        /* B.3 3.(b) */
  457|   172M|        for (CURTEMP = 0; CURTEMP < n_lines; CURTEMP++) {
  ------------------
  |  Branch (457:27): [True: 171M, False: 194k]
  ------------------
  458|   171M|            int PREFLEN = lines[CURTEMP].PREFLEN;
  459|       |
  460|   171M|            if (PREFLEN == CURLEN) {
  ------------------
  |  Branch (460:17): [True: 457k, False: 171M]
  ------------------
  461|   457k|                int RANGELEN = lines[CURTEMP].RANGELEN;
  462|   457k|                uint32_t start_j = CURCODE << shift;
  463|   457k|                uint32_t end_j = (CURCODE + 1) << shift;
  464|   457k|                uint32_t cur_j;
  465|   457k|                byte eflags = 0;
  466|       |
  467|   457k|                if (end_j > max_j) {
  ------------------
  |  Branch (467:21): [True: 92, False: 457k]
  ------------------
  468|     92|                    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|     92|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  469|     92|                    jbig2_free(ctx->allocator, result->entries);
  470|     92|                    jbig2_free(ctx->allocator, result);
  471|     92|                    jbig2_free(ctx->allocator, LENCOUNT);
  472|     92|                    return NULL;
  473|     92|                }
  474|       |                /* todo: build extension tables */
  475|   457k|                if (params->HTOOB && CURTEMP == n_lines - 1)
  ------------------
  |  Branch (475:21): [True: 42.5k, False: 414k]
  |  Branch (475:38): [True: 4.32k, False: 38.2k]
  ------------------
  476|  4.32k|                    eflags |= JBIG2_HUFFMAN_FLAGS_ISOOB;
  ------------------
  |  |   42|  4.32k|#define JBIG2_HUFFMAN_FLAGS_ISOOB 1
  ------------------
  477|   457k|                if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2))
  ------------------
  |  Branch (477:21): [True: 12.8k, False: 444k]
  |  Branch (477:43): [True: 42.5k, False: 414k]
  ------------------
  478|  12.8k|                    eflags |= JBIG2_HUFFMAN_FLAGS_ISLOW;
  ------------------
  |  |   43|  12.8k|#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
  ------------------
  479|   457k|                if (PREFLEN + RANGELEN > LOG_TABLE_SIZE_MAX) {
  ------------------
  |  |  378|   457k|#define LOG_TABLE_SIZE_MAX 16
  ------------------
  |  Branch (479:21): [True: 48.2k, False: 409k]
  ------------------
  480|  5.05M|                    for (cur_j = start_j; cur_j < end_j; cur_j++) {
  ------------------
  |  Branch (480:43): [True: 5.01M, False: 48.2k]
  ------------------
  481|  5.01M|                        entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW;
  482|  5.01M|                        entries[cur_j].PREFLEN = PREFLEN;
  483|  5.01M|                        entries[cur_j].RANGELEN = RANGELEN;
  484|  5.01M|                        entries[cur_j].flags = eflags;
  485|  5.01M|                    }
  486|   409k|                } else {
  487|   219M|                    for (cur_j = start_j; cur_j < end_j; cur_j++) {
  ------------------
  |  Branch (487:43): [True: 219M, False: 409k]
  ------------------
  488|   219M|                        int32_t HTOFFSET = (cur_j >> (shift - RANGELEN)) & ((1 << RANGELEN) - 1);
  489|       |
  490|   219M|                        if (eflags & JBIG2_HUFFMAN_FLAGS_ISLOW)
  ------------------
  |  |   43|   219M|#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
  ------------------
  |  Branch (490:29): [True: 427k, False: 219M]
  ------------------
  491|   427k|                            entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW - HTOFFSET;
  492|   219M|                        else
  493|   219M|                            entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW + HTOFFSET;
  494|   219M|                        entries[cur_j].PREFLEN = PREFLEN + RANGELEN;
  495|   219M|                        entries[cur_j].RANGELEN = 0;
  496|   219M|                        entries[cur_j].flags = eflags;
  497|   219M|                    }
  498|   409k|                }
  499|   457k|                CURCODE++;
  500|   457k|            }
  501|   171M|        }
  502|   194k|    }
  503|       |
  504|  32.6k|    jbig2_free(ctx->allocator, LENCOUNT);
  505|       |
  506|  32.6k|    return result;
  507|  32.6k|}
jbig2_release_huffman_table:
  512|  46.3k|{
  513|  46.3k|    if (table != NULL) {
  ------------------
  |  Branch (513:9): [True: 32.6k, False: 13.7k]
  ------------------
  514|  32.6k|        jbig2_free(ctx->allocator, table->entries);
  515|  32.6k|        jbig2_free(ctx->allocator, table);
  516|  32.6k|    }
  517|  46.3k|}
jbig2_table:
  550|  1.72k|{
  551|  1.72k|    Jbig2HuffmanParams *params = NULL;
  552|  1.72k|    Jbig2HuffmanLine *line = NULL;
  553|       |
  554|  1.72k|    segment->result = NULL;
  555|  1.72k|    if (segment->data_length < 10)
  ------------------
  |  Branch (555:9): [True: 11, False: 1.71k]
  ------------------
  556|     11|        goto too_short;
  557|  1.71k|    if (segment->data_length > SIZE_MAX / 8)
  ------------------
  |  Branch (557:9): [True: 0, False: 1.71k]
  ------------------
  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.71k|    {
  564|       |        /* B.2 1) (B.2.1) Code table flags */
  565|  1.71k|        const int code_table_flags = segment_data[0];
  566|  1.71k|        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.71k|        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.71k|        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.71k|        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.71k|        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.71k|        const size_t lines_max = (segment->data_length * 8 - HTPS * (HTOOB ? 3 : 2)) / (HTPS + HTRS) + (HTOOB ? 3 : 2);
  ------------------
  |  Branch (582:70): [True: 817, False: 898]
  |  Branch (582:105): [True: 817, False: 898]
  ------------------
  583|       |
  584|       |        /* points to a first table line data */
  585|  1.71k|        const byte *lines_data = segment_data + 9;
  586|  1.71k|        const size_t lines_data_bitlen = (segment->data_length - 9) * 8;        /* length in bit */
  587|       |
  588|       |        /* bit offset: controls bit reading */
  589|  1.71k|        size_t boffset = 0;
  590|       |
  591|       |        /* B.2 4) */
  592|  1.71k|        int32_t CURRANGELOW = HTLOW;
  593|  1.71k|        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.71k|        if (segment->data_length > SIZE_MAX / 8) {
  ------------------
  |  Branch (601:13): [True: 0, False: 1.71k]
  ------------------
  602|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Huffman Table size too large");
  603|      0|            goto error_exit;
  604|      0|        }
  605|  1.71k|        if (HTLOW >= HTHIGH) {
  ------------------
  |  Branch (605:13): [True: 36, False: 1.67k]
  ------------------
  606|     36|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid Huffman Table range");
  607|     36|            goto error_exit;
  608|     36|        }
  609|       |
  610|       |        /* allocate HuffmanParams & HuffmanLine */
  611|  1.67k|        params = jbig2_new(ctx, Jbig2HuffmanParams, 1);
  ------------------
  |  |  141|  1.67k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  612|  1.67k|        if (params == NULL) {
  ------------------
  |  Branch (612:13): [True: 1, False: 1.67k]
  ------------------
  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.67k|        line = jbig2_new(ctx, Jbig2HuffmanLine, lines_max);
  ------------------
  |  |  141|  1.67k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  617|  1.67k|        if (line == NULL) {
  ------------------
  |  Branch (617:13): [True: 1, False: 1.67k]
  ------------------
  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|  1.29M|        while (CURRANGELOW < HTHIGH) {
  ------------------
  |  Branch (622:16): [True: 1.29M, False: 1.61k]
  ------------------
  623|       |            /* B.2 5) a) */
  624|  1.29M|            if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (624:17): [True: 22, False: 1.29M]
  ------------------
  625|     22|                goto too_short;
  626|  1.29M|            if (NTEMP >= lines_max) {
  ------------------
  |  Branch (626:17): [True: 0, False: 1.29M]
  ------------------
  627|      0|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  628|      0|                goto error_exit;
  629|      0|            }
  630|  1.29M|            line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  631|       |            /* B.2 5) b) */
  632|  1.29M|            if (boffset + HTRS >= lines_data_bitlen)
  ------------------
  |  Branch (632:17): [True: 36, False: 1.29M]
  ------------------
  633|     36|                goto too_short;
  634|  1.29M|            line[NTEMP].RANGELEN = jbig2_table_read_bits(lines_data, &boffset, HTRS);
  635|       |            /* B.2 5) c) */
  636|  1.29M|            line[NTEMP].RANGELOW = CURRANGELOW;
  637|  1.29M|            CURRANGELOW += (1 << line[NTEMP].RANGELEN);
  638|  1.29M|            NTEMP++;
  639|  1.29M|        }
  640|       |        /* B.2 6), B.2 7) lower range table line */
  641|  1.61k|        if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (641:13): [True: 4, False: 1.61k]
  ------------------
  642|      4|            goto too_short;
  643|  1.61k|        if (NTEMP >= lines_max) {
  ------------------
  |  Branch (643:13): [True: 0, False: 1.61k]
  ------------------
  644|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  645|      0|            goto error_exit;
  646|      0|        }
  647|  1.61k|        line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  648|  1.61k|        line[NTEMP].RANGELEN = 32;
  649|  1.61k|        line[NTEMP].RANGELOW = HTLOW - 1;
  650|  1.61k|        NTEMP++;
  651|       |        /* B.2 8), B.2 9) upper range table line */
  652|  1.61k|        if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (652:13): [True: 5, False: 1.61k]
  ------------------
  653|      5|            goto too_short;
  654|  1.61k|        if (NTEMP >= lines_max) {
  ------------------
  |  Branch (654:13): [True: 0, False: 1.61k]
  ------------------
  655|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  656|      0|            goto error_exit;
  657|      0|        }
  658|  1.61k|        line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  659|  1.61k|        line[NTEMP].RANGELEN = 32;
  660|  1.61k|        line[NTEMP].RANGELOW = HTHIGH;
  661|  1.61k|        NTEMP++;
  662|       |        /* B.2 10) */
  663|  1.61k|        if (HTOOB) {
  ------------------
  |  Branch (663:13): [True: 770, False: 840]
  ------------------
  664|       |            /* B.2 10) a), B.2 10) b) out-of-bound table line */
  665|    770|            if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (665:17): [True: 5, False: 765]
  ------------------
  666|      5|                goto too_short;
  667|    765|            if (NTEMP >= lines_max) {
  ------------------
  |  Branch (667:17): [True: 0, False: 765]
  ------------------
  668|      0|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  669|      0|                goto error_exit;
  670|      0|            }
  671|    765|            line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  672|    765|            line[NTEMP].RANGELEN = 0;
  673|    765|            line[NTEMP].RANGELOW = 0;
  674|    765|            NTEMP++;
  675|    765|        }
  676|  1.60k|        if (NTEMP != lines_max) {
  ------------------
  |  Branch (676:13): [True: 1.60k, False: 0]
  ------------------
  677|  1.60k|            Jbig2HuffmanLine *new_line = jbig2_renew(ctx, line,
  ------------------
  |  |  143|  1.60k|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
  678|  1.60k|                                                     Jbig2HuffmanLine, NTEMP);
  679|       |
  680|  1.60k|            if (new_line == NULL) {
  ------------------
  |  Branch (680:17): [True: 0, False: 1.60k]
  ------------------
  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.60k|            line = new_line;
  685|  1.60k|        }
  686|  1.60k|        params->HTOOB = HTOOB;
  687|       |        /* Assuming int >= int32_t here. */
  688|  1.60k|        if (NTEMP > INT32_MAX) {
  ------------------
  |  Branch (688:13): [True: 0, False: 1.60k]
  ------------------
  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.60k|        params->n_lines = (int)NTEMP;
  693|  1.60k|        params->lines = line;
  694|  1.60k|        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.60k|    }
  708|      0|    return 0;
  709|       |
  710|     83|too_short:
  711|     83|    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  712|    121|error_exit:
  713|    121|    jbig2_free(ctx->allocator, line);
  714|    121|    jbig2_free(ctx->allocator, params);
  715|    121|    return -1;
  716|     83|}
jbig2_table_free:
  721|  1.60k|{
  722|  1.60k|    if (params != NULL) {
  ------------------
  |  Branch (722:9): [True: 1.60k, False: 0]
  ------------------
  723|  1.60k|        jbig2_free(ctx->allocator, (void *)params->lines);
  724|  1.60k|        jbig2_free(ctx->allocator, params);
  725|  1.60k|    }
  726|  1.60k|}
jbig2_find_table:
  731|    490|{
  732|    490|    int i, table_index = 0;
  733|       |
  734|  1.38k|    for (i = 0; i < segment->referred_to_segment_count; i++) {
  ------------------
  |  Branch (734:17): [True: 1.33k, False: 57]
  ------------------
  735|  1.33k|        const Jbig2Segment *const rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[i]);
  736|       |
  737|  1.33k|        if (rsegment && (rsegment->flags & 63) == 53) {
  ------------------
  |  Branch (737:13): [True: 846, False: 485]
  |  Branch (737:25): [True: 570, False: 276]
  ------------------
  738|    570|            if (table_index == index)
  ------------------
  |  Branch (738:17): [True: 433, False: 137]
  ------------------
  739|    433|                return (const Jbig2HuffmanParams *)rsegment->result;
  740|    137|            ++table_index;
  741|    137|        }
  742|  1.33k|    }
  743|       |
  744|     57|    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table not found (%d)", index);
  745|       |    return NULL;
  746|    490|}
jbig2_huffman.c:jbig2_table_read_bits:
  524|  2.59M|{
  525|  2.59M|    uint32_t result = 0;
  526|  2.59M|    size_t byte_offset = *bitoffset / 8;
  527|  2.59M|    const int endbit = ((int) (*bitoffset & 7)) + bitlen;
  528|  2.59M|    const int n_proc_bytes = (endbit + 7) / 8;
  529|  2.59M|    const int rshift = n_proc_bytes * 8 - endbit;
  530|  2.59M|    int i;
  531|       |
  532|  5.72M|    for (i = n_proc_bytes - 1; i >= 0; i--) {
  ------------------
  |  Branch (532:32): [True: 3.13M, False: 2.59M]
  ------------------
  533|  3.13M|        uint32_t d = data[byte_offset++];
  534|  3.13M|        const int nshift = i * 8 - rshift;
  535|       |
  536|  3.13M|        if (nshift > 0)
  ------------------
  |  Branch (536:13): [True: 539k, False: 2.59M]
  ------------------
  537|   539k|            d <<= nshift;
  538|  2.59M|        else if (nshift < 0)
  ------------------
  |  Branch (538:18): [True: 2.26M, False: 324k]
  ------------------
  539|  2.26M|            d >>= -nshift;
  540|  3.13M|        result |= d;
  541|  3.13M|    }
  542|       |    result &= ~(UINT32_MAX << bitlen);
  543|  2.59M|    *bitoffset += bitlen;
  544|  2.59M|    return result;
  545|  2.59M|}

jbig2_image_new:
   36|  15.1M|{
   37|  15.1M|    Jbig2Image *image;
   38|  15.1M|    uint32_t stride;
   39|       |
   40|  15.1M|    if (width == 0 || height == 0) {
  ------------------
  |  Branch (40:9): [True: 93, False: 15.1M]
  |  Branch (40:23): [True: 13, False: 15.1M]
  ------------------
   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|  15.1M|    image = jbig2_new(ctx, Jbig2Image, 1);
  ------------------
  |  |  141|  15.1M|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   46|  15.1M|    if (image == NULL) {
  ------------------
  |  Branch (46:9): [True: 35, False: 15.1M]
  ------------------
   47|     35|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate image");
  ------------------
  |  |   68|     35|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   48|     35|        return NULL;
   49|     35|    }
   50|       |
   51|  15.1M|    stride = ((width - 1) >> 3) + 1;    /* generate a byte-aligned stride */
   52|       |
   53|       |    /* check for integer multiplication overflow */
   54|  15.1M|    if (height > (INT32_MAX / stride)) {
  ------------------
  |  Branch (54:9): [True: 236, False: 15.1M]
  ------------------
   55|    236|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "integer multiplication overflow (stride=%u, height=%u)", stride, height);
  ------------------
  |  |   68|    236|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   56|    236|        jbig2_free(ctx->allocator, image);
   57|    236|        return NULL;
   58|    236|    }
   59|  15.1M|    image->data = jbig2_new(ctx, uint8_t, (size_t) height * stride);
  ------------------
  |  |  141|  15.1M|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   60|  15.1M|    if (image->data == NULL) {
  ------------------
  |  Branch (60:9): [True: 180, False: 15.1M]
  ------------------
   61|    180|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate image data buffer (stride=%u, height=%u)", stride, height);
  ------------------
  |  |   68|    180|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   62|    180|        jbig2_free(ctx->allocator, image);
   63|    180|        return NULL;
   64|    180|    }
   65|       |
   66|  15.1M|    image->width = width;
   67|  15.1M|    image->height = height;
   68|  15.1M|    image->stride = stride;
   69|  15.1M|    image->refcount = 1;
   70|       |
   71|  15.1M|    return image;
   72|  15.1M|}
jbig2_image_reference:
   77|  44.9M|{
   78|  44.9M|    (void) ctx;
   79|  44.9M|    if (image)
  ------------------
  |  Branch (79:9): [True: 13.4M, False: 31.4M]
  ------------------
   80|  13.4M|        image->refcount++;
   81|  44.9M|    return image;
   82|  44.9M|}
jbig2_image_release:
   87|   444M|{
   88|   444M|    if (image == NULL)
  ------------------
  |  Branch (88:9): [True: 415M, False: 28.5M]
  ------------------
   89|   415M|        return;
   90|  28.5M|    image->refcount--;
   91|  28.5M|    if (image->refcount == 0)
  ------------------
  |  Branch (91:9): [True: 15.1M, False: 13.4M]
  ------------------
   92|  15.1M|        jbig2_image_free(ctx, image);
   93|  28.5M|}
jbig2_image_free:
   98|  15.1M|{
   99|  15.1M|    if (image != NULL) {
  ------------------
  |  Branch (99:9): [True: 15.1M, False: 0]
  ------------------
  100|  15.1M|        jbig2_free(ctx->allocator, image->data);
  101|  15.1M|        jbig2_free(ctx->allocator, image);
  102|  15.1M|    }
  103|  15.1M|}
jbig2_image_resize:
  108|  2.36k|{
  109|  2.36k|    if (width == image->width) {
  ------------------
  |  Branch (109:9): [True: 2.36k, False: 0]
  ------------------
  110|  2.36k|        uint8_t *data;
  111|       |
  112|       |        /* check for integer multiplication overflow */
  113|  2.36k|        if (image->height > (INT32_MAX / image->stride)) {
  ------------------
  |  Branch (113:13): [True: 0, False: 2.36k]
  ------------------
  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|  2.36k|        data = jbig2_renew(ctx, image->data, uint8_t, (size_t) height * image->stride);
  ------------------
  |  |  143|  2.36k|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
  119|  2.36k|        if (data == NULL) {
  ------------------
  |  Branch (119:13): [True: 56, False: 2.31k]
  ------------------
  120|     56|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to reallocate image");
  ------------------
  |  |   68|     56|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  121|     56|            return NULL;
  122|     56|        }
  123|  2.31k|        image->data = data;
  124|  2.31k|        if (height > image->height) {
  ------------------
  |  Branch (124:13): [True: 2.31k, False: 0]
  ------------------
  125|  2.31k|            const uint8_t fill = value ? 0xFF : 0x00;
  ------------------
  |  Branch (125:34): [True: 329, False: 1.98k]
  ------------------
  126|  2.31k|            memset(image->data + (size_t) image->height * image->stride, fill, ((size_t) height - image->height) * image->stride);
  127|  2.31k|        }
  128|  2.31k|        image->height = height;
  129|       |
  130|  2.31k|    } 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|  2.31k|    return image;
  162|  2.36k|}
jbig2_image_compose:
  336|   134M|{
  337|   134M|    uint32_t w, h;
  338|   134M|    uint32_t shift;
  339|   134M|    uint32_t leftbyte;
  340|   134M|    uint8_t *ss;
  341|   134M|    uint8_t *dd;
  342|   134M|    uint8_t leftmask, rightmask;
  343|   134M|    int early = x >= 0;
  344|   134M|    int late;
  345|   134M|    uint32_t bytewidth;
  346|   134M|    uint32_t syoffset = 0;
  347|       |
  348|   134M|    (void) ctx;
  349|       |
  350|   134M|    if (src == NULL)
  ------------------
  |  Branch (350:9): [True: 11.8M, False: 122M]
  ------------------
  351|  11.8M|        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|   122M|    if (
  361|   122M|            (x <= -((int64_t) src->width)) || (x >= (int64_t) dst->width) ||
  ------------------
  |  Branch (361:13): [True: 20.7M, False: 101M]
  |  Branch (361:47): [True: 70.4M, False: 31.0M]
  ------------------
  362|  31.0M|            (y <= -((int64_t) src->height)) || (y >= (int64_t) dst->height))
  ------------------
  |  Branch (362:13): [True: 2.50M, False: 28.5M]
  |  Branch (362:48): [True: 10.8M, False: 17.7M]
  ------------------
  363|   104M|    {
  364|   104M|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "src image entirely outside dst image in compose_image");
  ------------------
  |  |   68|   104M|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  365|   104M|        return 0;
  366|   104M|    }
  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|  17.7M|    w = src->width;
  387|  17.7M|    h = src->height;
  388|  17.7M|    shift = (uint32_t) (x & 7);
  389|  17.7M|    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|  17.7M|    if (x < 0) {
  ------------------
  |  Branch (398:9): [True: 14.8M, False: 2.92M]
  ------------------
  399|  14.8M|        uint32_t negx = (uint32_t) (-x);
  400|  14.8M|        w -= negx;
  401|  14.8M|        ss += (negx-1)>>3;
  402|  14.8M|        x = 0;
  403|  14.8M|    }
  404|  17.7M|    if (y < 0) {
  ------------------
  |  Branch (404:9): [True: 7.90k, False: 17.7M]
  ------------------
  405|  7.90k|        uint32_t negy = (uint32_t) (-y);
  406|  7.90k|        h -= negy;
  407|  7.90k|        syoffset = negy * src->stride;
  408|  7.90k|        y = 0;
  409|  7.90k|    }
  410|       |
  411|       |    /* clip right/bottom */
  412|  17.7M|    if ((uint32_t)x + w > dst->width)
  ------------------
  |  Branch (412:9): [True: 14.8M, False: 2.92M]
  ------------------
  413|  14.8M|        w = dst->width - ((uint32_t) x);
  414|  17.7M|    if ((uint32_t)y + h > dst->height)
  ------------------
  |  Branch (414:9): [True: 11.4k, False: 17.7M]
  ------------------
  415|  11.4k|        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|  17.7M|    if ((w <= 0) || (h <= 0)) {
  ------------------
  |  Branch (422:9): [True: 0, False: 17.7M]
  |  Branch (422:21): [True: 0, False: 17.7M]
  ------------------
  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|  17.7M|    leftbyte = (uint32_t) x >> 3;
  430|  17.7M|    dd = dst->data + y * dst->stride + leftbyte;
  431|  17.7M|    bytewidth = (((uint32_t) x + w - 1) >> 3) - leftbyte + 1;
  432|  17.7M|    leftmask = 255>>(x&7);
  433|  17.7M|    rightmask = (((x+w)&7) == 0) ? 255 : ~(255>>((x+w)&7));
  ------------------
  |  Branch (433:17): [True: 1.73M, False: 16.0M]
  ------------------
  434|  17.7M|    if (bytewidth == 1)
  ------------------
  |  Branch (434:9): [True: 9.45M, False: 8.34M]
  ------------------
  435|  9.45M|        leftmask &= rightmask;
  436|  17.7M|    late = (ss + bytewidth >= src->data + ((src->width+7)>>3));
  437|  17.7M|    ss += syoffset;
  438|       |
  439|  17.7M|    switch(op)
  ------------------
  |  Branch (439:12): [True: 17.7M, False: 5.61k]
  ------------------
  440|  17.7M|    {
  441|  2.90M|    case JBIG2_COMPOSE_OR:
  ------------------
  |  Branch (441:5): [True: 2.90M, False: 14.8M]
  ------------------
  442|  2.90M|        jbig2_image_compose_opt_OR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  443|  2.90M|        break;
  444|  1.75k|    case JBIG2_COMPOSE_AND:
  ------------------
  |  Branch (444:5): [True: 1.75k, False: 17.7M]
  ------------------
  445|  1.75k|        jbig2_image_compose_opt_AND(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  446|  1.75k|        break;
  447|  5.29k|    case JBIG2_COMPOSE_XOR:
  ------------------
  |  Branch (447:5): [True: 5.29k, False: 17.7M]
  ------------------
  448|  5.29k|        jbig2_image_compose_opt_XOR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  449|  5.29k|        break;
  450|  1.84k|    case JBIG2_COMPOSE_XNOR:
  ------------------
  |  Branch (450:5): [True: 1.84k, False: 17.7M]
  ------------------
  451|  1.84k|        jbig2_image_compose_opt_XNOR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  452|  1.84k|        break;
  453|  14.8M|    case JBIG2_COMPOSE_REPLACE:
  ------------------
  |  Branch (453:5): [True: 14.8M, False: 2.92M]
  ------------------
  454|  14.8M|        jbig2_image_compose_opt_REPLACE(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  455|  14.8M|        break;
  456|  17.7M|    }
  457|       |
  458|  17.7M|    return 0;
  459|  17.7M|}
jbig2_image_clear:
  464|   116k|{
  465|   116k|    const uint8_t fill = value ? 0xFF : 0x00;
  ------------------
  |  Branch (465:26): [True: 1.40k, False: 114k]
  ------------------
  466|       |
  467|   116k|    (void) ctx;
  468|       |
  469|   116k|    memset(image->data, fill, image->stride * image->height);
  470|   116k|}
jbig2_image_get_pixel:
  478|  13.2G|{
  479|  13.2G|    const int64_t w = image->width;
  480|  13.2G|    const int64_t h = image->height;
  481|  13.2G|    size_t sx, sy;
  482|  13.2G|    size_t byte;
  483|  13.2G|    int bit;
  484|       |
  485|  13.2G|    if ((x < 0) || (x >= w))
  ------------------
  |  Branch (485:9): [True: 1.39G, False: 11.8G]
  |  Branch (485:20): [True: 2.27G, False: 9.52G]
  ------------------
  486|  3.67G|        return 0;
  487|  9.52G|    if ((y < 0) || (y >= h))
  ------------------
  |  Branch (487:9): [True: 2.27G, False: 7.25G]
  |  Branch (487:20): [True: 1.01G, False: 6.24G]
  ------------------
  488|  3.28G|        return 0;
  489|       |
  490|  6.24G|    sx = (size_t) x;
  491|  6.24G|    sy = (size_t) y;
  492|       |
  493|  6.24G|    byte = (sx >> 3) + sy * image->stride;
  494|  6.24G|    bit = 7 - ((int) (sx & 7));
  495|       |
  496|  6.24G|    return ((image->data[byte] >> bit) & 1);
  497|  9.52G|}
jbig2_image_set_pixel:
  502|  1.47G|{
  503|  1.47G|    const int64_t w = image->width;
  504|  1.47G|    const int64_t h = image->height;
  505|  1.47G|    uint8_t scratch, mask;
  506|  1.47G|    size_t sx, sy;
  507|  1.47G|    size_t byte;
  508|  1.47G|    int bit;
  509|       |
  510|  1.47G|    if ((x < 0) || (x >= w))
  ------------------
  |  Branch (510:9): [True: 0, False: 1.47G]
  |  Branch (510:20): [True: 0, False: 1.47G]
  ------------------
  511|      0|        return;
  512|  1.47G|    if ((y < 0) || (y >= h))
  ------------------
  |  Branch (512:9): [True: 0, False: 1.47G]
  |  Branch (512:20): [True: 0, False: 1.47G]
  ------------------
  513|      0|        return;
  514|       |
  515|  1.47G|    sx = (size_t) x;
  516|  1.47G|    sy = (size_t) y;
  517|       |
  518|  1.47G|    byte = (sx >> 3) + sy * image->stride;
  519|  1.47G|    bit = 7 - ((int) (sx & 7));
  520|  1.47G|    mask = (uint8_t) ((1 << bit) ^ 0xff);
  521|       |
  522|  1.47G|    scratch = image->data[byte] & mask;
  523|  1.47G|    image->data[byte] = scratch | (value << bit);
  524|  1.47G|}
jbig2_image.c:jbig2_image_compose_opt_OR:
  290|  2.90M|{
  291|  2.90M|    if (early || late)
  ------------------
  |  Branch (291:9): [True: 2.90M, False: 4.86k]
  |  Branch (291:18): [True: 3.74k, False: 1.11k]
  ------------------
  292|  2.90M|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_OR);
  293|  1.11k|    else
  294|  1.11k|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_OR);
  295|  2.90M|}
jbig2_image.c:template_image_compose_opt:
  166|  17.7M|{
  167|  17.7M|    int i;
  168|  17.7M|    uint32_t j;
  169|  17.7M|    int bytewidth = (int)bytewidth_;
  170|       |
  171|  17.7M|    if (bytewidth == 1) {
  ------------------
  |  Branch (171:9): [True: 9.44M, False: 8.33M]
  ------------------
  172|   131M|        for (j = 0; j < h; j++) {
  ------------------
  |  Branch (172:21): [True: 121M, False: 9.44M]
  ------------------
  173|       |            /* Only 1 byte! */
  174|   121M|            uint8_t v = (((early ? 0 : ss[0]<<8) | (late ? 0 : ss[1]))>>shift);
  ------------------
  |  Branch (174:28): [True: 41.1M, False: 80.8M]
  |  Branch (174:53): [True: 181k, False: 121M]
  ------------------
  175|   121M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (175:17): [True: 40.0M, False: 81.9M]
  ------------------
  176|  40.0M|                *dd |= v & leftmask;
  177|  81.9M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (177:22): [True: 12.4k, False: 81.9M]
  ------------------
  178|  12.4k|                *dd &= (v & leftmask) | ~leftmask;
  179|  81.9M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (179:22): [True: 153k, False: 81.7M]
  ------------------
  180|   153k|                *dd ^= v & leftmask;
  181|  81.7M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (181:22): [True: 113k, False: 81.6M]
  ------------------
  182|   113k|                *dd ^= (~v) & leftmask;
  183|  81.6M|            else /* Replace */
  184|  81.6M|                *dd = (v & leftmask) | (*dd & ~leftmask);
  185|   121M|            dd += dstride;
  186|   121M|            ss += sstride;
  187|   121M|        }
  188|  9.44M|        return;
  189|  9.44M|    }
  190|  8.33M|    bytewidth -= 2;
  191|  8.33M|    if (shift == 0) {
  ------------------
  |  Branch (191:9): [True: 2.53M, False: 5.80M]
  ------------------
  192|  2.53M|        ss++;
  193|  44.9M|        for (j = 0; j < h; j++) {
  ------------------
  |  Branch (193:21): [True: 42.3M, False: 2.53M]
  ------------------
  194|       |            /* Left byte */
  195|  42.3M|            const uint8_t * JBIG2_RESTRICT s = ss;
  196|  42.3M|            uint8_t * JBIG2_RESTRICT d = dd;
  197|  42.3M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (197:17): [True: 1.48M, False: 40.9M]
  ------------------
  198|  1.48M|                *d++ |= *s++ & leftmask;
  199|  40.9M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (199:22): [True: 46.8k, False: 40.8M]
  ------------------
  200|  46.8k|                *d++ &= (*s++ & leftmask) | ~leftmask;
  201|  40.8M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (201:22): [True: 34.8k, False: 40.8M]
  ------------------
  202|  34.8k|                *d++ ^= *s++ & leftmask;
  203|  40.8M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (203:22): [True: 66.3k, False: 40.7M]
  ------------------
  204|  66.3k|                *d++ ^= (~*s++) & leftmask;
  205|  40.7M|            else /* Replace */
  206|  40.7M|                *d = (*s++ & leftmask) | (*d & ~leftmask), d++;
  207|       |            /* Central run */
  208|  1.53G|            for (i = bytewidth; i != 0; i--) {
  ------------------
  |  Branch (208:33): [True: 1.49G, False: 42.3M]
  ------------------
  209|  1.49G|                if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (209:21): [True: 1.15G, False: 336M]
  ------------------
  210|  1.15G|                    *d++ |= *s++;
  211|   336M|                else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (211:26): [True: 90.9k, False: 335M]
  ------------------
  212|  90.9k|                    *d++ &= *s++;
  213|   335M|                else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (213:26): [True: 224k, False: 335M]
  ------------------
  214|   224k|                    *d++ ^= *s++;
  215|   335M|                else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (215:26): [True: 69.8k, False: 335M]
  ------------------
  216|  69.8k|                    *d++ ^= ~*s++;
  217|   335M|                else /* Replace */
  218|   335M|                    *d++ = *s++;
  219|  1.49G|            }
  220|       |            /* Right byte */
  221|  42.3M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (221:17): [True: 1.48M, False: 40.9M]
  ------------------
  222|  1.48M|                *d |= *s & rightmask;
  223|  40.9M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (223:22): [True: 46.8k, False: 40.8M]
  ------------------
  224|  46.8k|                *d &= (*s & rightmask) | ~rightmask;
  225|  40.8M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (225:22): [True: 34.8k, False: 40.8M]
  ------------------
  226|  34.8k|                *d ^= *s & rightmask;
  227|  40.8M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (227:22): [True: 66.3k, False: 40.7M]
  ------------------
  228|  66.3k|                *d ^= (~*s) & rightmask;
  229|  40.7M|            else /* Replace */
  230|  40.7M|                *d = (*s & rightmask) | (*d & ~rightmask);
  231|  42.3M|            dd += dstride;
  232|  42.3M|            ss += sstride;
  233|  42.3M|        }
  234|  5.80M|    } else {
  235|  89.3M|        for (j = 0; j < h; j++) {
  ------------------
  |  Branch (235:21): [True: 83.5M, False: 5.80M]
  ------------------
  236|       |            /* Left byte */
  237|  83.5M|            const uint8_t * JBIG2_RESTRICT s = ss;
  238|  83.5M|            uint8_t * JBIG2_RESTRICT d = dd;
  239|  83.5M|            uint8_t s0, s1, v;
  240|  83.5M|            s0 = early ? 0 : *s;
  ------------------
  |  Branch (240:18): [True: 7.20M, False: 76.3M]
  ------------------
  241|  83.5M|            s++;
  242|  83.5M|            s1 = *s++;
  243|  83.5M|            v = ((s0<<8) | s1)>>shift;
  244|  83.5M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (244:17): [True: 7.09M, False: 76.4M]
  ------------------
  245|  7.09M|                *d++ |= v & leftmask;
  246|  76.4M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (246:22): [True: 42.4k, False: 76.4M]
  ------------------
  247|  42.4k|                *d++ &= (v & leftmask) | ~leftmask;
  248|  76.4M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (248:22): [True: 140k, False: 76.2M]
  ------------------
  249|   140k|                *d++ ^= v & leftmask;
  250|  76.2M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (250:22): [True: 14.4k, False: 76.2M]
  ------------------
  251|  14.4k|                *d++ ^= (~v) & leftmask;
  252|  76.2M|            else /* Replace */
  253|  76.2M|                *d = (v & leftmask) | (*d & ~leftmask), d++;
  254|       |            /* Central run */
  255|  1.10G|            for (i = bytewidth; i > 0; i--) {
  ------------------
  |  Branch (255:33): [True: 1.01G, False: 83.5M]
  ------------------
  256|  1.01G|                s0 = s1; s1 = *s++;
  257|  1.01G|                v = ((s0<<8) | s1)>>shift;
  258|  1.01G|                if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (258:21): [True: 290M, False: 726M]
  ------------------
  259|   290M|                    *d++ |= v;
  260|   726M|                else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (260:26): [True: 425k, False: 726M]
  ------------------
  261|   425k|                    *d++ &= v;
  262|   726M|                else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (262:26): [True: 923k, False: 725M]
  ------------------
  263|   923k|                    *d++ ^= v;
  264|   725M|                else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (264:26): [True: 864k, False: 724M]
  ------------------
  265|   864k|                    *d++ ^= ~v;
  266|   724M|                else /* Replace */
  267|   724M|                    *d++ = v;
  268|  1.01G|            }
  269|       |            /* Right byte */
  270|  83.5M|            s0 = s1; s1 = (late ? 0 : *s);
  ------------------
  |  Branch (270:28): [True: 5.38M, False: 78.1M]
  ------------------
  271|  83.5M|            v = (((s0<<8) | s1)>>shift);
  272|  83.5M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (272:17): [True: 7.09M, False: 76.4M]
  ------------------
  273|  7.09M|                *d |= v & rightmask;
  274|  76.4M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (274:22): [True: 42.4k, False: 76.4M]
  ------------------
  275|  42.4k|                *d &= (v & rightmask) | ~rightmask;
  276|  76.4M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (276:22): [True: 140k, False: 76.2M]
  ------------------
  277|   140k|                *d ^= v & rightmask;
  278|  76.2M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (278:22): [True: 14.4k, False: 76.2M]
  ------------------
  279|  14.4k|                *d ^= ~v & rightmask;
  280|  76.2M|            else /* Replace */
  281|  76.2M|                *d = (v & rightmask) | (*d & ~rightmask);
  282|  83.5M|            dd += dstride;
  283|  83.5M|            ss += sstride;
  284|  83.5M|        }
  285|  5.80M|    }
  286|  8.33M|}
jbig2_image.c:jbig2_image_compose_opt_AND:
  299|  1.75k|{
  300|  1.75k|    if (early || late)
  ------------------
  |  Branch (300:9): [True: 974, False: 783]
  |  Branch (300:18): [True: 304, False: 479]
  ------------------
  301|  1.27k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_AND);
  302|    479|    else
  303|    479|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_AND);
  304|  1.75k|}
jbig2_image.c:jbig2_image_compose_opt_XOR:
  308|  5.29k|{
  309|  5.29k|    if (early || late)
  ------------------
  |  Branch (309:9): [True: 2.07k, False: 3.22k]
  |  Branch (309:18): [True: 1.00k, False: 2.21k]
  ------------------
  310|  3.08k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XOR);
  311|  2.21k|    else
  312|  2.21k|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XOR);
  313|  5.29k|}
jbig2_image.c:jbig2_image_compose_opt_XNOR:
  317|  1.84k|{
  318|  1.84k|    if (early || late)
  ------------------
  |  Branch (318:9): [True: 720, False: 1.12k]
  |  Branch (318:18): [True: 574, False: 548]
  ------------------
  319|  1.29k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XNOR);
  320|    548|    else
  321|    548|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XNOR);
  322|  1.84k|}
jbig2_image.c:jbig2_image_compose_opt_REPLACE:
  326|  14.8M|{
  327|  14.8M|    if (early || late)
  ------------------
  |  Branch (327:9): [True: 12.5k, False: 14.8M]
  |  Branch (327:18): [True: 4.15k, False: 14.8M]
  ------------------
  328|  16.7k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_REPLACE);
  329|  14.8M|    else
  330|  14.8M|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_REPLACE);
  331|  14.8M|}

jbig2_decode_generic_mmr:
 1207|  12.2k|{
 1208|  12.2k|    Jbig2MmrCtx mmr;
 1209|  12.2k|    const uint32_t rowstride = image->stride;
 1210|  12.2k|    byte *dst = image->data;
 1211|  12.2k|    byte *ref = NULL;
 1212|  12.2k|    uint32_t y;
 1213|  12.2k|    int code = 0;
 1214|  12.2k|    int eofb = 0;
 1215|       |
 1216|  12.2k|    (void) params;
 1217|       |
 1218|  12.2k|    jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
 1219|       |
 1220|   134M|    for (y = 0; !eofb && y < image->height; y++) {
  ------------------
  |  Branch (1220:17): [True: 134M, False: 2.04k]
  |  Branch (1220:26): [True: 134M, False: 10.0k]
  ------------------
 1221|   134M|        memset(dst, 0, rowstride);
 1222|   134M|        code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb);
 1223|   134M|        if (code < 0)
  ------------------
  |  Branch (1223:13): [True: 166, False: 134M]
  ------------------
 1224|    166|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode mmr line");
 1225|   134M|        ref = dst;
 1226|   134M|        dst += rowstride;
 1227|   134M|    }
 1228|       |
 1229|  12.0k|    if (eofb && y < image->height) {
  ------------------
  |  Branch (1229:9): [True: 2.04k, False: 10.0k]
  |  Branch (1229:17): [True: 1.91k, False: 125]
  ------------------
 1230|  1.91k|        memset(dst, 0, rowstride * (image->height - y));
 1231|  1.91k|    }
 1232|       |
 1233|  12.0k|    return code;
 1234|  12.2k|}
jbig2_decode_halftone_mmr:
 1252|  14.0k|{
 1253|  14.0k|    Jbig2MmrCtx mmr;
 1254|  14.0k|    const uint32_t rowstride = image->stride;
 1255|  14.0k|    byte *dst = image->data;
 1256|  14.0k|    byte *ref = NULL;
 1257|  14.0k|    uint32_t y;
 1258|  14.0k|    int code = 0;
 1259|  14.0k|    const uint32_t EOFB = 0x001001;
 1260|  14.0k|    int eofb = 0;
 1261|       |
 1262|  14.0k|    (void) params;
 1263|       |
 1264|  14.0k|    jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
 1265|       |
 1266|  88.7M|    for (y = 0; !eofb && y < image->height; y++) {
  ------------------
  |  Branch (1266:17): [True: 88.7M, False: 1.96k]
  |  Branch (1266:26): [True: 88.6M, False: 12.0k]
  ------------------
 1267|  88.6M|        memset(dst, 0, rowstride);
 1268|  88.6M|        code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb);
 1269|  88.6M|        if (code < 0)
  ------------------
  |  Branch (1269:13): [True: 45, False: 88.6M]
  ------------------
 1270|     45|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode halftone mmr line");
  ------------------
  |  |   68|     45|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1271|  88.6M|        ref = dst;
 1272|  88.6M|        dst += rowstride;
 1273|  88.6M|    }
 1274|       |
 1275|  14.0k|    if (eofb && y < image->height) {
  ------------------
  |  Branch (1275:9): [True: 1.96k, False: 12.0k]
  |  Branch (1275:17): [True: 1.70k, False: 257]
  ------------------
 1276|  1.70k|        memset(dst, 0, rowstride * (image->height - y));
 1277|  1.70k|    }
 1278|       |
 1279|       |    /* test for EOFB (see section 6.2.6) */
 1280|  14.0k|    if (mmr.word >> 8 == EOFB) {
  ------------------
  |  Branch (1280:9): [True: 1.22k, False: 12.7k]
  ------------------
 1281|  1.22k|        jbig2_decode_mmr_consume(&mmr, 24);
 1282|  1.22k|    }
 1283|       |
 1284|  14.0k|    *consumed_bytes += (mmr.consumed_bits + 7) / 8;
 1285|  14.0k|    return code;
 1286|  14.0k|}
jbig2_mmr.c:jbig2_decode_mmr_init:
   61|  26.3k|{
   62|  26.3k|    mmr->width = width;
   63|  26.3k|    mmr->height = height;
   64|  26.3k|    mmr->data = data;
   65|  26.3k|    mmr->size = size;
   66|  26.3k|    mmr->data_index = 0;
   67|  26.3k|    mmr->bit_index = 32;
   68|  26.3k|    mmr->word = 0;
   69|  26.3k|    mmr->consumed_bits = 0;
   70|       |
   71|   107k|    while (mmr->bit_index >= 8 && mmr->data_index < mmr->size) {
  ------------------
  |  Branch (71:12): [True: 91.5k, False: 15.6k]
  |  Branch (71:35): [True: 80.8k, False: 10.7k]
  ------------------
   72|  80.8k|        mmr->bit_index -= 8;
   73|  80.8k|        mmr->word |= (mmr->data[mmr->data_index] << mmr->bit_index);
   74|  80.8k|        mmr->data_index++;
   75|  80.8k|    }
   76|  26.3k|}
jbig2_mmr.c:jbig2_decode_mmr_line:
  985|   223M|{
  986|   223M|    uint32_t a0 = MINUS1;
  ------------------
  |  |   54|   223M|#define MINUS1 UINT32_MAX
  ------------------
  987|   223M|    uint32_t a1, a2, b1, b2;
  988|   223M|    int c = 0;                  /* 0 is white, black is 1 */
  989|       |
  990|   226M|    while (1) {
  ------------------
  |  Branch (990:12): [True: 226M, Folded]
  ------------------
  991|   226M|        uint32_t word = mmr->word;
  992|       |
  993|       |        /* printf ("%08x\n", word); */
  994|       |
  995|   226M|        if (a0 != MINUS1 && a0 >= mmr->width)
  ------------------
  |  |   54|   453M|#define MINUS1 UINT32_MAX
  ------------------
  |  Branch (995:13): [True: 3.39M, False: 223M]
  |  Branch (995:29): [True: 1.19M, False: 2.19M]
  ------------------
  996|  1.19M|            break;
  997|       |
  998|   225M|        if ((word >> (32 - 3)) == 1) {
  ------------------
  |  Branch (998:13): [True: 677k, False: 224M]
  ------------------
  999|   677k|            int white_run, black_run;
 1000|       |
 1001|   677k|            jbig2_decode_mmr_consume(mmr, 3);
 1002|       |
 1003|   677k|            if (a0 == MINUS1)
  ------------------
  |  |   54|   677k|#define MINUS1 UINT32_MAX
  ------------------
  |  Branch (1003:17): [True: 62.5k, False: 615k]
  ------------------
 1004|  62.5k|                a0 = 0;
 1005|       |
 1006|   677k|            if (c == 0) {
  ------------------
  |  Branch (1006:17): [True: 589k, False: 88.2k]
  ------------------
 1007|   589k|                white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8);
 1008|   589k|                if (white_run < 0)
  ------------------
  |  Branch (1008:21): [True: 35, False: 589k]
  ------------------
 1009|     35|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode white H run");
  ------------------
  |  |   68|     35|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1010|   589k|                black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7);
 1011|   589k|                if (black_run < 0)
  ------------------
  |  Branch (1011:21): [True: 121, False: 589k]
  ------------------
 1012|    121|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode black H run");
  ------------------
  |  |   68|    121|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1013|       |                /* printf ("H %d %d\n", white_run, black_run); */
 1014|   589k|                a1 = a0 + white_run;
 1015|   589k|                a2 = a1 + black_run;
 1016|   589k|                if (a1 > mmr->width)
  ------------------
  |  Branch (1016:21): [True: 36.6k, False: 552k]
  ------------------
 1017|  36.6k|                    a1 = mmr->width;
 1018|   589k|                if (a2 > mmr->width)
  ------------------
  |  Branch (1018:21): [True: 40.7k, False: 548k]
  ------------------
 1019|  40.7k|                    a2 = mmr->width;
 1020|   589k|                if (a2 < a1) {
  ------------------
  |  Branch (1020:21): [True: 0, False: 589k]
  ------------------
 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|   589k|                if (a1 < mmr->width)
  ------------------
  |  Branch (1024:21): [True: 552k, False: 37.1k]
  ------------------
 1025|   552k|                    jbig2_set_bits(dst, a1, a2);
 1026|   589k|                a0 = a2;
 1027|   589k|            } else {
 1028|  88.2k|                black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7);
 1029|  88.2k|                if (black_run < 0)
  ------------------
  |  Branch (1029:21): [True: 7, False: 88.2k]
  ------------------
 1030|      7|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode black H run");
  ------------------
  |  |   68|      7|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1031|  88.2k|                white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8);
 1032|  88.2k|                if (white_run < 0)
  ------------------
  |  Branch (1032:21): [True: 48, False: 88.2k]
  ------------------
 1033|     48|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode white H run");
  ------------------
  |  |   68|     48|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1034|       |                /* printf ("H %d %d\n", black_run, white_run); */
 1035|  88.2k|                a1 = a0 + black_run;
 1036|  88.2k|                a2 = a1 + white_run;
 1037|  88.2k|                if (a1 > mmr->width)
  ------------------
  |  Branch (1037:21): [True: 17.7k, False: 70.4k]
  ------------------
 1038|  17.7k|                    a1 = mmr->width;
 1039|  88.2k|                if (a2 > mmr->width)
  ------------------
  |  Branch (1039:21): [True: 31.5k, False: 56.6k]
  ------------------
 1040|  31.5k|                    a2 = mmr->width;
 1041|  88.2k|                if (a1 < a0) {
  ------------------
  |  Branch (1041:21): [True: 0, False: 88.2k]
  ------------------
 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|  88.2k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1045:21): [True: 88.2k, False: 0]
  ------------------
 1046|  88.2k|                    jbig2_set_bits(dst, a0, a1);
 1047|  88.2k|                a0 = a2;
 1048|  88.2k|            }
 1049|   677k|        }
 1050|       |
 1051|   224M|        else if ((word >> (32 - 4)) == 1) {
  ------------------
  |  Branch (1051:18): [True: 250k, False: 224M]
  ------------------
 1052|       |            /* printf ("P\n"); */
 1053|   250k|            jbig2_decode_mmr_consume(mmr, 4);
 1054|   250k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1055|   250k|            b2 = jbig2_find_changing_element(ref, b1, mmr->width);
 1056|   250k|            if (c) {
  ------------------
  |  Branch (1056:17): [True: 121k, False: 128k]
  ------------------
 1057|   121k|                if (b2 < a0) {
  ------------------
  |  Branch (1057:21): [True: 0, False: 121k]
  ------------------
 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|   121k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1061:21): [True: 121k, False: 0]
  ------------------
 1062|   121k|                    jbig2_set_bits(dst, a0, b2);
 1063|   121k|            }
 1064|   250k|            a0 = b2;
 1065|   250k|        }
 1066|       |
 1067|   224M|        else if ((word >> (32 - 1)) == 1) {
  ------------------
  |  Branch (1067:18): [True: 1.26M, False: 223M]
  ------------------
 1068|       |            /* printf ("V(0)\n"); */
 1069|  1.26M|            jbig2_decode_mmr_consume(mmr, 1);
 1070|  1.26M|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1071|  1.26M|            if (c) {
  ------------------
  |  Branch (1071:17): [True: 450k, False: 815k]
  ------------------
 1072|   450k|                if (b1 < a0) {
  ------------------
  |  Branch (1072:21): [True: 0, False: 450k]
  ------------------
 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|   450k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1076:21): [True: 450k, False: 0]
  ------------------
 1077|   450k|                    jbig2_set_bits(dst, a0, b1);
 1078|   450k|            }
 1079|  1.26M|            a0 = b1;
 1080|  1.26M|            c = !c;
 1081|  1.26M|        }
 1082|       |
 1083|   223M|        else if ((word >> (32 - 3)) == 3) {
  ------------------
  |  Branch (1083:18): [True: 490k, False: 222M]
  ------------------
 1084|       |            /* printf ("VR(1)\n"); */
 1085|   490k|            jbig2_decode_mmr_consume(mmr, 3);
 1086|   490k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1087|   490k|            if (b1 + 1 <= mmr->width)
  ------------------
  |  Branch (1087:17): [True: 210k, False: 280k]
  ------------------
 1088|   210k|                b1 += 1;
 1089|   490k|            if (c) {
  ------------------
  |  Branch (1089:17): [True: 187k, False: 303k]
  ------------------
 1090|   187k|                if (b1 < a0) {
  ------------------
  |  Branch (1090:21): [True: 0, False: 187k]
  ------------------
 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|   187k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1094:21): [True: 187k, False: 0]
  ------------------
 1095|   187k|                    jbig2_set_bits(dst, a0, b1);
 1096|   187k|            }
 1097|   490k|            a0 = b1;
 1098|   490k|            c = !c;
 1099|   490k|        }
 1100|       |
 1101|   222M|        else if ((word >> (32 - 6)) == 3) {
  ------------------
  |  Branch (1101:18): [True: 110k, False: 222M]
  ------------------
 1102|       |            /* printf ("VR(2)\n"); */
 1103|   110k|            jbig2_decode_mmr_consume(mmr, 6);
 1104|   110k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1105|   110k|            if (b1 + 2 <= mmr->width)
  ------------------
  |  Branch (1105:17): [True: 33.0k, False: 77.3k]
  ------------------
 1106|  33.0k|                b1 += 2;
 1107|   110k|            if (c) {
  ------------------
  |  Branch (1107:17): [True: 63.8k, False: 46.5k]
  ------------------
 1108|  63.8k|                if (b1 < a0) {
  ------------------
  |  Branch (1108:21): [True: 0, False: 63.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|  63.8k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1112:21): [True: 63.8k, False: 0]
  ------------------
 1113|  63.8k|                    jbig2_set_bits(dst, a0, b1);
 1114|  63.8k|            }
 1115|   110k|            a0 = b1;
 1116|   110k|            c = !c;
 1117|   110k|        }
 1118|       |
 1119|   222M|        else if ((word >> (32 - 7)) == 3) {
  ------------------
  |  Branch (1119:18): [True: 48.0k, False: 222M]
  ------------------
 1120|       |            /* printf ("VR(3)\n"); */
 1121|  48.0k|            jbig2_decode_mmr_consume(mmr, 7);
 1122|  48.0k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1123|  48.0k|            if (b1 + 3 <= mmr->width)
  ------------------
  |  Branch (1123:17): [True: 24.7k, False: 23.3k]
  ------------------
 1124|  24.7k|                b1 += 3;
 1125|  48.0k|            if (c) {
  ------------------
  |  Branch (1125:17): [True: 19.0k, False: 29.0k]
  ------------------
 1126|  19.0k|                if (b1 < a0) {
  ------------------
  |  Branch (1126:21): [True: 0, False: 19.0k]
  ------------------
 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|  19.0k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1130:21): [True: 19.0k, False: 0]
  ------------------
 1131|  19.0k|                    jbig2_set_bits(dst, a0, b1);
 1132|  19.0k|            }
 1133|  48.0k|            a0 = b1;
 1134|  48.0k|            c = !c;
 1135|  48.0k|        }
 1136|       |
 1137|   222M|        else if ((word >> (32 - 3)) == 2) {
  ------------------
  |  Branch (1137:18): [True: 347k, False: 222M]
  ------------------
 1138|       |            /* printf ("VL(1)\n"); */
 1139|   347k|            jbig2_decode_mmr_consume(mmr, 3);
 1140|   347k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1141|   347k|            if (b1 >= 1)
  ------------------
  |  Branch (1141:17): [True: 344k, False: 2.55k]
  ------------------
 1142|   344k|                b1 -= 1;
 1143|   347k|            if (c) {
  ------------------
  |  Branch (1143:17): [True: 107k, False: 239k]
  ------------------
 1144|   107k|                if (b1 < a0) {
  ------------------
  |  Branch (1144:21): [True: 0, False: 107k]
  ------------------
 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|   107k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1148:21): [True: 107k, False: 0]
  ------------------
 1149|   107k|                    jbig2_set_bits(dst, a0, b1);
 1150|   107k|            }
 1151|   347k|            a0 = b1;
 1152|   347k|            c = !c;
 1153|   347k|        }
 1154|       |
 1155|   222M|        else if ((word >> (32 - 6)) == 2) {
  ------------------
  |  Branch (1155:18): [True: 127k, False: 222M]
  ------------------
 1156|       |            /* printf ("VL(2)\n"); */
 1157|   127k|            jbig2_decode_mmr_consume(mmr, 6);
 1158|   127k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1159|   127k|            if (b1 >= 2)
  ------------------
  |  Branch (1159:17): [True: 126k, False: 1.72k]
  ------------------
 1160|   126k|                b1 -= 2;
 1161|   127k|            if (c) {
  ------------------
  |  Branch (1161:17): [True: 34.9k, False: 93.0k]
  ------------------
 1162|  34.9k|                if (b1 < a0) {
  ------------------
  |  Branch (1162:21): [True: 7.16k, False: 27.7k]
  ------------------
 1163|  7.16k|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(2) run");
  ------------------
  |  |   68|  7.16k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1164|  7.16k|                    b1 = a0;
 1165|  7.16k|                }
 1166|  34.9k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1166:21): [True: 34.9k, False: 0]
  ------------------
 1167|  34.9k|                    jbig2_set_bits(dst, a0, b1);
 1168|  34.9k|            }
 1169|   127k|            a0 = b1;
 1170|   127k|            c = !c;
 1171|   127k|        }
 1172|       |
 1173|   222M|        else if ((word >> (32 - 7)) == 2) {
  ------------------
  |  Branch (1173:18): [True: 77.9k, False: 222M]
  ------------------
 1174|       |            /* printf ("VL(3)\n"); */
 1175|  77.9k|            jbig2_decode_mmr_consume(mmr, 7);
 1176|  77.9k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1177|  77.9k|            if (b1 >= 3)
  ------------------
  |  Branch (1177:17): [True: 72.7k, False: 5.17k]
  ------------------
 1178|  72.7k|                b1 -= 3;
 1179|  77.9k|            if (c) {
  ------------------
  |  Branch (1179:17): [True: 29.9k, False: 47.9k]
  ------------------
 1180|  29.9k|                if (b1 < a0) {
  ------------------
  |  Branch (1180:21): [True: 7.66k, False: 22.2k]
  ------------------
 1181|  7.66k|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(3) run");
  ------------------
  |  |   68|  7.66k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1182|  7.66k|                    b1 = a0;
 1183|  7.66k|                }
 1184|  29.9k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1184:21): [True: 29.9k, False: 0]
  ------------------
 1185|  29.9k|                    jbig2_set_bits(dst, a0, b1);
 1186|  29.9k|            }
 1187|  77.9k|            a0 = b1;
 1188|  77.9k|            c = !c;
 1189|  77.9k|        }
 1190|       |
 1191|   222M|        else if ((word >> (32 - 24)) == 0x1001) {
  ------------------
  |  Branch (1191:18): [True: 4.00k, False: 222M]
  ------------------
 1192|       |            /* printf ("EOFB\n"); */
 1193|  4.00k|            jbig2_decode_mmr_consume(mmr, 24);
 1194|  4.00k|            *eofb = 1;
 1195|  4.00k|            break;
 1196|  4.00k|        }
 1197|       |
 1198|   222M|        else
 1199|   222M|            break;
 1200|   225M|    }
 1201|       |
 1202|   223M|    return 0;
 1203|   223M|}
jbig2_mmr.c:jbig2_decode_get_run:
  965|  1.35M|{
  966|  1.35M|    int result = 0;
  967|  1.35M|    int val;
  968|       |
  969|  3.72M|    do {
  970|  3.72M|        val = jbig2_decode_get_code(mmr, table, initial_bits);
  971|  3.72M|        if (val == ERROR)
  ------------------
  |  |   55|  3.72M|#define ERROR -1
  ------------------
  |  Branch (971:13): [True: 5, False: 3.72M]
  ------------------
  972|      5|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "invalid code detected in MMR-coded data");
  ------------------
  |  |   68|      5|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  973|  3.72M|        else if (val == UNCOMPRESSED)
  ------------------
  |  |   57|  3.72M|#define UNCOMPRESSED -3
  ------------------
  |  Branch (973:18): [True: 4, False: 3.72M]
  ------------------
  974|      4|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "uncompressed code in MMR-coded data");
  ------------------
  |  |   68|      4|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  975|  3.72M|        else if (val == ZEROES)
  ------------------
  |  |   56|  3.72M|#define ZEROES -2
  ------------------
  |  Branch (975:18): [True: 202, False: 3.72M]
  ------------------
  976|    202|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "zeroes code in MMR-coded data");
  ------------------
  |  |   68|    202|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  977|  3.72M|        result += val;
  978|  3.72M|    } while (val >= 64);
  ------------------
  |  Branch (978:14): [True: 2.36M, False: 1.35M]
  ------------------
  979|       |
  980|  1.35M|    return result;
  981|  1.35M|}
jbig2_mmr.c:jbig2_decode_get_code:
  944|  3.72M|{
  945|  3.72M|    uint32_t word = mmr->word;
  946|  3.72M|    int table_ix = word >> (32 - initial_bits);
  947|  3.72M|    int val = table[table_ix].val;
  948|  3.72M|    int n_bits = table[table_ix].n_bits;
  949|       |
  950|  3.72M|    if (n_bits > initial_bits) {
  ------------------
  |  Branch (950:9): [True: 14.8k, False: 3.70M]
  ------------------
  951|  14.8k|        int mask = (1 << (32 - initial_bits)) - 1;
  952|       |
  953|  14.8k|        table_ix = val + ((word & mask) >> (32 - n_bits));
  954|  14.8k|        val = table[table_ix].val;
  955|  14.8k|        n_bits = initial_bits + table[table_ix].n_bits;
  956|  14.8k|    }
  957|       |
  958|  3.72M|    jbig2_decode_mmr_consume(mmr, n_bits);
  959|       |
  960|  3.72M|    return val;
  961|  3.72M|}
jbig2_mmr.c:jbig2_set_bits:
  922|  1.65M|{
  923|  1.65M|    uint32_t a0, a1, b0, b1, a;
  924|       |
  925|  1.65M|    a0 = x0 >> 3;
  926|  1.65M|    a1 = x1 >> 3;
  927|       |
  928|  1.65M|    b0 = x0 & 7;
  929|  1.65M|    b1 = x1 & 7;
  930|       |
  931|  1.65M|    if (a0 == a1) {
  ------------------
  |  Branch (931:9): [True: 728k, False: 926k]
  ------------------
  932|   728k|        line[a0] |= lm[b0] & rm[b1];
  933|   926k|    } else {
  934|   926k|        line[a0] |= lm[b0];
  935|  2.12G|        for (a = a0 + 1; a < a1; a++)
  ------------------
  |  Branch (935:26): [True: 2.12G, False: 926k]
  ------------------
  936|  2.12G|            line[a] = 0xFF;
  937|   926k|        if (b1)
  ------------------
  |  Branch (937:13): [True: 705k, False: 220k]
  ------------------
  938|   705k|            line[a1] |= rm[b1];
  939|   926k|    }
  940|  1.65M|}
jbig2_mmr.c:jbig2_find_changing_element_of_color:
  908|  2.71M|{
  909|  2.71M|    if (line == NULL)
  ------------------
  |  Branch (909:9): [True: 19.0k, False: 2.69M]
  ------------------
  910|  19.0k|        return w;
  911|  2.69M|    x = jbig2_find_changing_element(line, x, w);
  912|  2.69M|    if (x < w && getbit(line, x) != color)
  ------------------
  |  |  745|  1.62M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  |  Branch (912:9): [True: 1.62M, False: 1.07M]
  |  Branch (912:18): [True: 323k, False: 1.29M]
  ------------------
  913|   323k|        x = jbig2_find_changing_element(line, x, w);
  914|  2.69M|    return x;
  915|  2.71M|}
jbig2_mmr.c:jbig2_find_changing_element:
  759|  3.27M|{
  760|  3.27M|    int a;
  761|  3.27M|    uint8_t     all8;
  762|  3.27M|    uint16_t    all16;
  763|  3.27M|    uint32_t    all32;
  764|       |
  765|  3.27M|    if (line == NULL)
  ------------------
  |  Branch (765:9): [True: 842, False: 3.27M]
  ------------------
  766|    842|        return w;
  767|       |
  768|  3.27M|    if (x == MINUS1) {
  ------------------
  |  |   54|  3.27M|#define MINUS1 UINT32_MAX
  ------------------
  |  Branch (768:9): [True: 1.12M, False: 2.14M]
  ------------------
  769|  1.12M|        a = 0;
  770|  1.12M|        x = 0;
  771|  2.14M|    } else if (x < w) {
  ------------------
  |  Branch (771:16): [True: 1.97M, False: 167k]
  ------------------
  772|  1.97M|        a = getbit(line, x);
  ------------------
  |  |  745|  1.97M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  773|  1.97M|        x++;
  774|  1.97M|    } else {
  775|   167k|        return x;
  776|   167k|    }
  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.10M|    all8  = (a) ? 0xff : 0;
  ------------------
  |  Branch (780:13): [True: 1.10M, False: 1.99M]
  ------------------
  781|  3.10M|    all16 = (a) ? 0xffff : 0;
  ------------------
  |  Branch (781:13): [True: 1.10M, False: 1.99M]
  ------------------
  782|  3.10M|    all32 = (a) ? 0xffffffff : 0;
  ------------------
  |  Branch (782:13): [True: 1.10M, False: 1.99M]
  ------------------
  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.10M|    if ( ((uint8_t*) line)[ x / 8] == all8) {
  ------------------
  |  Branch (789:10): [True: 1.31M, False: 1.78M]
  ------------------
  790|       |        /* Don't bother checking individual bits if the enclosing uint8 equals
  791|       |        all8 - just move to the next byte. */
  792|  1.31M|        x = x / 8 * 8 + 8;
  793|  1.31M|        if (x >= w) {
  ------------------
  |  Branch (793:13): [True: 131k, False: 1.18M]
  ------------------
  794|   131k|            x = w;
  795|   131k|            goto end;
  796|   131k|        }
  797|  1.78M|    } else {
  798|  4.09M|        for(;;) {
  799|  4.09M|            if (x == w) {
  ------------------
  |  Branch (799:17): [True: 393k, False: 3.70M]
  ------------------
  800|   393k|                goto end;
  801|   393k|            }
  802|  3.70M|            if (x % 8 == 0) {
  ------------------
  |  Branch (802:17): [True: 916k, False: 2.78M]
  ------------------
  803|   916k|                break;
  804|   916k|            }
  805|  2.78M|            if (getbit(line, x) != a) {
  ------------------
  |  |  745|  2.78M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  |  Branch (805:17): [True: 476k, False: 2.31M]
  ------------------
  806|   476k|                goto end;
  807|   476k|            }
  808|  2.31M|            x += 1;
  809|  2.31M|        }
  810|  1.78M|    }
  811|       |
  812|  3.10M|    assert(x % 8 == 0);
  ------------------
  |  Branch (812:5): [True: 0, False: 2.10M]
  |  Branch (812:5): [True: 2.10M, False: 0]
  ------------------
  813|       |    /* Check next uint8 if we are not on 16-bit boundary. */
  814|  2.10M|    if (x % 16) {
  ------------------
  |  Branch (814:9): [True: 1.35M, False: 749k]
  ------------------
  815|  1.35M|        if (w - x < 8) {
  ------------------
  |  Branch (815:13): [True: 6.64k, False: 1.34M]
  ------------------
  816|  6.64k|            goto check1;
  817|  6.64k|        }
  818|  1.34M|        if ( ((uint8_t*) line)[ x / 8] != all8) {
  ------------------
  |  Branch (818:14): [True: 173k, False: 1.17M]
  ------------------
  819|   173k|            goto check1;
  820|   173k|        }
  821|  1.17M|        x += 8; /* This will make x a multiple of 16. */
  822|  1.17M|    }
  823|       |
  824|  2.10M|    assert(x % 16 == 0);
  ------------------
  |  Branch (824:5): [True: 0, False: 1.92M]
  |  Branch (824:5): [True: 1.92M, False: 0]
  ------------------
  825|       |    /* Check next uint16 if we are not on 32-bit boundary. */
  826|  1.92M|    if (x % 32) {
  ------------------
  |  Branch (826:9): [True: 1.28M, False: 643k]
  ------------------
  827|  1.28M|        if (w - x < 16) {
  ------------------
  |  Branch (827:13): [True: 16.4k, False: 1.26M]
  ------------------
  828|  16.4k|            goto check8;
  829|  16.4k|        }
  830|  1.26M|        if ( getword16((line + (x / 8))) != all16) {
  ------------------
  |  |  754|  1.26M|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  ------------------
  |  Branch (830:14): [True: 114k, False: 1.14M]
  ------------------
  831|   114k|            goto check8_no_eof;
  832|   114k|        }
  833|  1.14M|        x += 16; /* This will make x a multiple of 32. */
  834|  1.14M|    }
  835|       |
  836|       |    /* We are now on a 32-bit boundary. Check uint32's until we reach last
  837|       |    sub-32-bit region. */
  838|  1.92M|    assert(x % 32 == 0);
  ------------------
  |  Branch (838:5): [True: 0, False: 1.79M]
  |  Branch (838:5): [True: 1.79M, False: 0]
  ------------------
  839|  1.27G|    for(;;) {
  840|  1.27G|        if (w - x < 32) {
  ------------------
  |  Branch (840:13): [True: 1.12M, False: 1.26G]
  ------------------
  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.12M|            goto check16;
  847|  1.12M|        }
  848|  1.26G|        if ( getword32((line + (x / 8))) != all32) {
  ------------------
  |  |  755|  1.26G|#define getword32(b)  ((uint32_t)(getword16(b) | (getword16((b + 2)) << 16)))
  |  |  ------------------
  |  |  |  |  754|  1.26G|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  |  |  ------------------
  |  |               #define getword32(b)  ((uint32_t)(getword16(b) | (getword16((b + 2)) << 16)))
  |  |  ------------------
  |  |  |  |  754|  1.26G|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  |  |  ------------------
  ------------------
  |  Branch (848:14): [True: 665k, False: 1.26G]
  ------------------
  849|   665k|            goto check16_no_eof;
  850|   665k|        }
  851|  1.26G|        x += 32;
  852|  1.26G|    }
  853|       |
  854|       |    /* Check next uint16. */
  855|  1.12M|check16:
  856|  1.12M|    assert(x % 16 == 0);
  ------------------
  |  Branch (856:5): [True: 0, False: 1.12M]
  |  Branch (856:5): [True: 1.12M, False: 0]
  ------------------
  857|  1.12M|    if (w - x < 16) {
  ------------------
  |  Branch (857:9): [True: 1.06M, False: 59.6k]
  ------------------
  858|  1.06M|        goto check8;
  859|  1.06M|    }
  860|   724k|check16_no_eof:
  861|   724k|    assert(w - x >= 16);
  ------------------
  |  Branch (861:5): [True: 0, False: 724k]
  |  Branch (861:5): [True: 724k, False: 0]
  ------------------
  862|   724k|    if ( getword16((line + (x / 8))) != all16) {
  ------------------
  |  |  754|   724k|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  ------------------
  |  Branch (862:10): [True: 309k, False: 415k]
  ------------------
  863|   309k|        goto check8_no_eof;
  864|   309k|    }
  865|   415k|    x += 16;
  866|       |
  867|       |    /* Check next uint8. */
  868|  1.49M|check8:
  869|  1.49M|    assert(x % 8 == 0);
  ------------------
  |  Branch (869:5): [True: 0, False: 1.49M]
  |  Branch (869:5): [True: 1.49M, False: 0]
  ------------------
  870|  1.49M|    if (w - x < 8) {
  ------------------
  |  Branch (870:9): [True: 1.06M, False: 431k]
  ------------------
  871|  1.06M|        goto check1;
  872|  1.06M|    }
  873|   855k|check8_no_eof:
  874|   855k|    assert(w - x >= 8);
  ------------------
  |  Branch (874:5): [True: 0, False: 855k]
  |  Branch (874:5): [True: 855k, False: 0]
  ------------------
  875|   855k|    if ( ((uint8_t*) line)[x/8] != all8) {
  ------------------
  |  Branch (875:10): [True: 416k, False: 438k]
  ------------------
  876|   416k|        goto check1;
  877|   416k|    }
  878|   438k|    x += 8;
  879|       |
  880|       |    /* Check up to the next 8 bits. */
  881|  2.10M|check1:
  882|  2.10M|    assert(x % 8 == 0);
  ------------------
  |  Branch (882:5): [True: 0, False: 2.10M]
  |  Branch (882:5): [True: 2.10M, False: 0]
  ------------------
  883|  2.10M|    if ( ((uint8_t*) line)[ x / 8] == all8) {
  ------------------
  |  Branch (883:10): [True: 502k, False: 1.60M]
  ------------------
  884|   502k|        x = w;
  885|   502k|        goto end;
  886|   502k|    }
  887|  1.60M|    {
  888|  5.94M|        for(;;) {
  889|  5.94M|            if (x == w) {
  ------------------
  |  Branch (889:17): [True: 249k, False: 5.69M]
  ------------------
  890|   249k|                goto end;
  891|   249k|            }
  892|  5.69M|            if (getbit(line, x) != a) {
  ------------------
  |  |  745|  5.69M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  |  Branch (892:17): [True: 1.35M, False: 4.34M]
  ------------------
  893|  1.35M|                goto end;
  894|  1.35M|            }
  895|  4.34M|            x += 1;
  896|  4.34M|        }
  897|  1.60M|    }
  898|       |
  899|  3.10M|end:
  900|  3.10M|    return x;
  901|  1.60M|}
jbig2_mmr.c:jbig2_decode_mmr_consume:
   80|  7.12M|{
   81|  7.12M|    mmr->consumed_bits += n_bits;
   82|  7.12M|    if (mmr->consumed_bits > mmr->size * 8)
  ------------------
  |  Branch (82:9): [True: 3.32k, False: 7.12M]
  ------------------
   83|  3.32k|        mmr->consumed_bits = mmr->size * 8;
   84|       |
   85|  7.12M|    mmr->word <<= n_bits;
   86|  7.12M|    mmr->bit_index += n_bits;
   87|  11.1M|    while (mmr->bit_index >= 8 && mmr->data_index < mmr->size) {
  ------------------
  |  Branch (87:12): [True: 4.07M, False: 7.06M]
  |  Branch (87:35): [True: 4.01M, False: 56.6k]
  ------------------
   88|  4.01M|        mmr->bit_index -= 8;
   89|  4.01M|        mmr->word |= (mmr->data[mmr->data_index] << mmr->bit_index);
   90|  4.01M|        mmr->data_index++;
   91|  4.01M|    }
   92|  7.12M|}

jbig2_page_info:
   67|  5.60k|{
   68|  5.60k|    Jbig2Page *page, *pages;
   69|       |
   70|       |    /* a new page info segment implies the previous page is finished */
   71|  5.60k|    page = &(ctx->pages[ctx->current_page]);
   72|  5.60k|    if (page->number != 0 && (page->state == JBIG2_PAGE_NEW || page->state == JBIG2_PAGE_FREE)) {
  ------------------
  |  Branch (72:9): [True: 3.80k, False: 1.79k]
  |  Branch (72:31): [True: 3.69k, False: 111]
  |  Branch (72:64): [True: 0, False: 111]
  ------------------
   73|  3.69k|        page->state = JBIG2_PAGE_COMPLETE;
   74|  3.69k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unexpected page info segment, marking previous page finished");
   75|  3.69k|    }
   76|       |
   77|       |    /* find a free page */
   78|  5.60k|    {
   79|  5.60k|        uint32_t index, j;
   80|       |
   81|  5.60k|        index = ctx->current_page;
   82|  10.0k|        while (ctx->pages[index].state != JBIG2_PAGE_FREE) {
  ------------------
  |  Branch (82:16): [True: 4.47k, False: 5.59k]
  ------------------
   83|  4.47k|            index++;
   84|  4.47k|            if (index >= ctx->max_page_index) {
  ------------------
  |  Branch (84:17): [True: 152, False: 4.32k]
  ------------------
   85|       |                /* grow the list */
   86|       |
   87|    152|                if (ctx->max_page_index == UINT32_MAX) {
  ------------------
  |  Branch (87:21): [True: 0, False: 152]
  ------------------
   88|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "too many pages in jbig2 image");
   89|      0|                }
   90|    152|                else if (ctx->max_page_index > (UINT32_MAX >> 2)) {
  ------------------
  |  Branch (90:26): [True: 0, False: 152]
  ------------------
   91|      0|                    ctx->max_page_index = UINT32_MAX;
   92|    152|                } else {
   93|    152|                    ctx->max_page_index <<= 2;
   94|    152|                }
   95|       |
   96|    152|                pages = jbig2_renew(ctx, ctx->pages, Jbig2Page, ctx->max_page_index);
  ------------------
  |  |  143|    152|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
   97|    152|                if (pages == NULL) {
  ------------------
  |  Branch (97:21): [True: 1, False: 151]
  ------------------
   98|      1|                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to reallocate pages");
   99|      1|                }
  100|    151|                ctx->pages = pages;
  101|  9.01k|                for (j = index; j < ctx->max_page_index; j++) {
  ------------------
  |  Branch (101:33): [True: 8.86k, False: 151]
  ------------------
  102|  8.86k|                    ctx->pages[j].state = JBIG2_PAGE_FREE;
  103|  8.86k|                    ctx->pages[j].number = 0;
  104|  8.86k|                    ctx->pages[j].image = NULL;
  105|  8.86k|                }
  106|    151|            }
  107|  4.47k|        }
  108|  5.59k|        page = &(ctx->pages[index]);
  109|  5.59k|        ctx->current_page = index;
  110|  5.59k|        page->state = JBIG2_PAGE_NEW;
  111|  5.59k|        page->number = segment->page_association;
  112|  5.59k|    }
  113|       |
  114|       |    /* FIXME: would be nice if we tried to work around this */
  115|  5.59k|    if (segment->data_length < 19) {
  ------------------
  |  Branch (115:9): [True: 12, False: 5.58k]
  ------------------
  116|     12|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  117|     12|    }
  118|       |
  119|       |    /* 7.4.8.x */
  120|  5.58k|    page->width = jbig2_get_uint32(segment_data);
  121|  5.58k|    page->height = jbig2_get_uint32(segment_data + 4);
  122|       |
  123|  5.58k|    page->x_resolution = jbig2_get_uint32(segment_data + 8);
  124|  5.58k|    page->y_resolution = jbig2_get_uint32(segment_data + 12);
  125|  5.58k|    page->flags = segment_data[16];
  126|       |    /* Check for T.88 amendment 3 */
  127|  5.58k|    if (page->flags & 0x80)
  ------------------
  |  Branch (127:9): [True: 12, False: 5.57k]
  ------------------
  128|     12|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "page segment indicates use of color segments (NYI)");
  129|       |
  130|       |    /* 7.4.8.6 */
  131|  5.57k|    {
  132|  5.57k|        int16_t striping = jbig2_get_int16(segment_data + 17);
  133|       |
  134|  5.57k|        if (striping & 0x8000) {
  ------------------
  |  Branch (134:13): [True: 2.60k, False: 2.97k]
  ------------------
  135|  2.60k|            page->striped = TRUE;
  ------------------
  |  |   53|  2.60k|#define TRUE 1
  ------------------
  136|  2.60k|            page->stripe_size = striping & 0x7FFF;
  137|  2.97k|        } else {
  138|  2.97k|            page->striped = FALSE;
  ------------------
  |  |   56|  2.97k|#define FALSE 0
  ------------------
  139|  2.97k|            page->stripe_size = 0;      /* would page->height be better? */
  140|  2.97k|        }
  141|  5.57k|    }
  142|  5.57k|    if (page->height == 0xFFFFFFFF && page->striped == FALSE) {
  ------------------
  |  |   56|  2.79k|#define FALSE 0
  ------------------
  |  Branch (142:9): [True: 2.79k, False: 2.78k]
  |  Branch (142:39): [True: 323, False: 2.46k]
  ------------------
  143|    323|        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|    323|        page->striped = TRUE;
  ------------------
  |  |   53|    323|#define TRUE 1
  ------------------
  145|    323|        page->stripe_size = 0x7FFF;
  146|    323|    }
  147|  5.57k|    page->end_row = 0;
  148|       |
  149|  5.57k|    if (segment->data_length > 19) {
  ------------------
  |  Branch (149:9): [True: 3.90k, False: 1.67k]
  ------------------
  150|  3.90k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extra data in segment");
  151|  3.90k|    }
  152|       |
  153|  5.57k|    dump_page_info(ctx, segment, page);
  154|       |
  155|       |    /* allocate an appropriate page image buffer */
  156|       |    /* 7.4.8.2 */
  157|  5.57k|    if (page->height == 0xFFFFFFFF) {
  ------------------
  |  Branch (157:9): [True: 2.79k, False: 2.78k]
  ------------------
  158|  2.79k|        page->image = jbig2_image_new(ctx, page->width, page->stripe_size);
  159|  2.79k|    } else {
  160|  2.78k|        page->image = jbig2_image_new(ctx, page->width, page->height);
  161|  2.78k|    }
  162|  5.57k|    if (page->image == NULL) {
  ------------------
  |  Branch (162:9): [True: 89, False: 5.48k]
  ------------------
  163|     89|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate buffer for page image");
  164|  5.48k|    } else {
  165|       |        /* 8.2 (3) fill the page with the default pixel value */
  166|  5.48k|        jbig2_image_clear(ctx, page->image, (page->flags & 4));
  167|  5.48k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  168|  5.48k|                    "allocated %dx%d page image (%d bytes)", page->image->width, page->image->height, page->image->stride * page->image->height);
  169|  5.48k|    }
  170|       |
  171|  5.48k|    return 0;
  172|  5.57k|}
jbig2_end_of_stripe:
  179|    433|{
  180|    433|    Jbig2Page *page = &ctx->pages[ctx->current_page];
  181|    433|    uint32_t end_row;
  182|       |
  183|    433|    if (segment->data_length < 4)
  ------------------
  |  Branch (183:9): [True: 8, False: 425]
  ------------------
  184|      8|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  185|    425|    end_row = jbig2_get_uint32(segment_data);
  186|    425|    if (end_row < page->end_row) {
  ------------------
  |  Branch (186:9): [True: 142, False: 283]
  ------------------
  187|    142|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  188|    142|                    "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|    283|    } else {
  190|    283|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of stripe: advancing end row from %u to %u", page->end_row, end_row);
  191|    283|    }
  192|       |
  193|    425|    page->end_row = end_row;
  194|       |
  195|    425|    return 0;
  196|    433|}
jbig2_complete_page:
  208|  6.49k|{
  209|  6.49k|    int code;
  210|       |
  211|       |    /* check for unfinished segments */
  212|  6.49k|    if (ctx->segment_index != ctx->n_segments) {
  ------------------
  |  Branch (212:9): [True: 6.03k, False: 462]
  ------------------
  213|  6.03k|        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.03k|        if ((segment->data_length & 0xffffffff) == 0xffffffff) {
  ------------------
  |  Branch (218:13): [True: 3.99k, False: 2.04k]
  ------------------
  219|  3.99k|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "file has an invalid segment data length; trying to decode using the available data");
  220|  3.99k|            segment->data_length = ctx->buf_wr_ix - ctx->buf_rd_ix;
  221|  3.99k|            code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
  222|  3.99k|            ctx->buf_rd_ix += segment->data_length;
  223|  3.99k|            ctx->segment_index++;
  224|  3.99k|            if (code < 0) {
  ------------------
  |  Branch (224:17): [True: 3.46k, False: 525]
  ------------------
  225|  3.46k|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to parse segment");
  226|  3.46k|            }
  227|  3.99k|        }
  228|  6.03k|    }
  229|       |
  230|       |    /* ensure image exists before marking page as complete */
  231|  3.03k|    if (ctx->pages[ctx->current_page].image == NULL) {
  ------------------
  |  Branch (231:9): [True: 986, False: 2.04k]
  ------------------
  232|    986|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page has no image, cannot be completed");
  ------------------
  |  |   68|    986|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  233|    986|    }
  234|       |
  235|  2.04k|    ctx->pages[ctx->current_page].state = JBIG2_PAGE_COMPLETE;
  236|  2.04k|    return 0;
  237|  3.03k|}
jbig2_end_of_page:
  244|  1.47k|{
  245|  1.47k|    uint32_t page_number = ctx->pages[ctx->current_page].number;
  246|  1.47k|    int code;
  247|       |
  248|  1.47k|    (void) segment_data;
  249|       |
  250|  1.47k|    if (segment->page_association != page_number) {
  ------------------
  |  Branch (250:9): [True: 844, False: 631]
  ------------------
  251|    844|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  252|    844|                    "end of page marker for page %d doesn't match current page number %d", segment->page_association, page_number);
  253|    844|    }
  254|       |
  255|  1.47k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of page %d", page_number);
  256|       |
  257|  1.47k|    code = jbig2_complete_page(ctx);
  258|  1.47k|    if (code < 0)
  ------------------
  |  Branch (258:9): [True: 40, False: 1.43k]
  ------------------
  259|     40|        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.43k|    return 0;
  268|  1.47k|}
jbig2_page_add_result:
  278|  18.6k|{
  279|  18.6k|    int code;
  280|       |
  281|  18.6k|    if (x > INT32_MAX || y > INT32_MAX)
  ------------------
  |  Branch (281:9): [True: 267, False: 18.4k]
  |  Branch (281:26): [True: 279, False: 18.1k]
  ------------------
  282|    546|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unsupported image coordinates");
  ------------------
  |  |   68|    546|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  283|       |
  284|       |    /* ensure image exists first */
  285|  18.1k|    if (page->image == NULL)
  ------------------
  |  Branch (285:9): [True: 265, False: 17.8k]
  ------------------
  286|    265|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page info possibly missing, no image defined");
  ------------------
  |  |   68|    265|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  287|       |
  288|       |    /* grow the page to accommodate a new stripe if necessary */
  289|  17.8k|    if (page->striped && page->height == 0xFFFFFFFF) {
  ------------------
  |  Branch (289:9): [True: 8.80k, False: 9.06k]
  |  Branch (289:26): [True: 6.70k, False: 2.09k]
  ------------------
  290|  6.70k|        uint32_t new_height;
  291|       |
  292|  6.70k|        if (y > UINT32_MAX - image->height)
  ------------------
  |  Branch (292:13): [True: 0, False: 6.70k]
  ------------------
  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|  6.70k|        new_height = y + image->height;
  295|       |
  296|  6.70k|        if (page->image->height < new_height) {
  ------------------
  |  Branch (296:13): [True: 2.36k, False: 4.33k]
  ------------------
  297|  2.36k|            Jbig2Image *resized_image = NULL;
  298|       |
  299|  2.36k|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "growing page buffer to %u rows to accommodate new stripe", new_height);
  ------------------
  |  |   68|  2.36k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  300|  2.36k|            resized_image = jbig2_image_resize(ctx, page->image, page->image->width, new_height, page->flags & 4);
  301|  2.36k|            if (resized_image == NULL) {
  ------------------
  |  Branch (301:17): [True: 56, False: 2.31k]
  ------------------
  302|     56|                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unable to resize image to accommodate new stripe");
  ------------------
  |  |   68|     56|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  303|     56|            }
  304|  2.31k|            page->image = resized_image;
  305|  2.31k|        }
  306|  6.70k|    }
  307|       |
  308|  17.8k|    code = jbig2_image_compose(ctx, page->image, image, x, y, op);
  309|  17.8k|    if (code < 0)
  ------------------
  |  Branch (309:9): [True: 0, False: 17.8k]
  ------------------
  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|  17.8k|    return 0;
  313|  17.8k|}
jbig2_page_out:
  329|    610|{
  330|    610|    uint32_t index;
  331|       |
  332|       |    /* search for a completed page */
  333|    752|    for (index = 0; index < ctx->max_page_index; index++) {
  ------------------
  |  Branch (333:21): [True: 752, False: 0]
  ------------------
  334|    752|        if (ctx->pages[index].state == JBIG2_PAGE_COMPLETE) {
  ------------------
  |  Branch (334:13): [True: 610, False: 142]
  ------------------
  335|    610|            Jbig2Image *img = ctx->pages[index].image;
  336|    610|            uint32_t page_number = ctx->pages[index].number;
  337|       |
  338|    610|            if (img == NULL) {
  ------------------
  |  Branch (338:17): [True: 0, False: 610]
  ------------------
  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|    610|            ctx->pages[index].state = JBIG2_PAGE_RETURNED;
  344|    610|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d returned to the client", page_number);
  ------------------
  |  |   68|    610|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  345|    610|            return jbig2_image_reference(ctx, img);
  346|    610|        }
  347|    752|    }
  348|       |
  349|       |    /* no pages available */
  350|      0|    return NULL;
  351|    610|}
jbig2_release_page:
  358|    610|{
  359|    610|    uint32_t index;
  360|       |
  361|    610|    if (image == NULL)
  ------------------
  |  Branch (361:9): [True: 0, False: 610]
  ------------------
  362|      0|        return;
  363|       |
  364|       |    /* find the matching page struct and mark it released */
  365|    752|    for (index = 0; index < ctx->max_page_index; index++) {
  ------------------
  |  Branch (365:21): [True: 752, False: 0]
  ------------------
  366|    752|        if (ctx->pages[index].image == image) {
  ------------------
  |  Branch (366:13): [True: 610, False: 142]
  ------------------
  367|    610|            jbig2_image_release(ctx, image);
  368|    610|            ctx->pages[index].state = JBIG2_PAGE_RELEASED;
  369|    610|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d released by the client", ctx->pages[index].number);
  ------------------
  |  |   68|    610|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  370|    610|            return;
  371|    610|        }
  372|    752|    }
  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|  5.57k|{
   45|  5.57k|    if (page->x_resolution == 0) {
  ------------------
  |  Branch (45:9): [True: 1.13k, False: 4.43k]
  ------------------
   46|  1.13k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (unknown res)", page->number, page->width, page->height);
   47|  4.43k|    } else if (page->x_resolution == page->y_resolution) {
  ------------------
  |  Branch (47:16): [True: 185, False: 4.25k]
  ------------------
   48|    185|        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|  4.25k|    } else {
   50|  4.25k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
   51|  4.25k|                    "page %d image is %dx%d (%dx%d ppm)", page->number, page->width, page->height, page->x_resolution, page->y_resolution);
   52|  4.25k|    }
   53|  5.57k|    if (page->striped) {
  ------------------
  |  Branch (53:9): [True: 2.92k, False: 2.64k]
  ------------------
   54|  2.92k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "\tmaximum stripe size: %d", page->stripe_size);
   55|  2.92k|    }
   56|  5.57k|}

jbig2_decode_refinement_region:
  375|   101k|{
  376|   101k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  377|   101k|                "decoding generic refinement region with offset %d,%x, GRTEMPLATE=%d, TPGRON=%d",
  378|   101k|                params->GRREFERENCEDX, params->GRREFERENCEDY, params->GRTEMPLATE, params->TPGRON);
  379|       |
  380|   101k|    if (params->TPGRON)
  ------------------
  |  Branch (380:9): [True: 520, False: 101k]
  ------------------
  381|    520|        return jbig2_decode_refinement_TPGRON(ctx, params, as, image, GR_stats);
  382|       |
  383|   101k|    if (params->GRTEMPLATE)
  ------------------
  |  Branch (383:9): [True: 3.23k, False: 98.1k]
  ------------------
  384|  3.23k|        return jbig2_decode_refinement_template1_unopt(ctx, segment, params, as, image, GR_stats);
  385|  98.1k|    else
  386|  98.1k|        return jbig2_decode_refinement_template0_unopt(ctx, segment, params, as, image, GR_stats);
  387|   101k|}
jbig2_refinement_region:
  427|    851|{
  428|    851|    Jbig2RefinementRegionParams params;
  429|    851|    Jbig2RegionSegmentInfo rsi;
  430|    851|    int offset = 0;
  431|    851|    byte seg_flags;
  432|    851|    int code = 0;
  433|       |
  434|       |    /* 7.4.7 */
  435|    851|    if (segment->data_length < 18)
  ------------------
  |  Branch (435:9): [True: 6, False: 845]
  ------------------
  436|      6|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  437|       |
  438|    845|    jbig2_get_region_segment_info(&rsi, segment_data);
  439|    845|    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|    845|    seg_flags = segment_data[17];
  443|    845|    params.GRTEMPLATE = seg_flags & 0x01;
  444|    845|    params.TPGRON = seg_flags & 0x02 ? 1 : 0;
  ------------------
  |  Branch (444:21): [True: 544, False: 301]
  ------------------
  445|    845|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  446|    845|                "segment flags = %02x %s%s", seg_flags, params.GRTEMPLATE ? " GRTEMPLATE" : "", params.TPGRON ? " TPGRON" : "");
  ------------------
  |  Branch (446:57): [True: 331, False: 514]
  |  Branch (446:97): [True: 544, False: 301]
  ------------------
  447|    845|    if (seg_flags & 0xFC)
  ------------------
  |  Branch (447:9): [True: 452, False: 393]
  ------------------
  448|    452|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved segment flag bits are non-zero");
  449|    845|    offset += 18;
  450|       |
  451|       |    /* 7.4.7.3 */
  452|    845|    if (!params.GRTEMPLATE) {
  ------------------
  |  Branch (452:9): [True: 514, False: 331]
  ------------------
  453|    514|        if (segment->data_length < 22)
  ------------------
  |  Branch (453:13): [True: 6, False: 508]
  ------------------
  454|      6|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  455|    508|        params.grat[0] = segment_data[offset + 0];
  456|    508|        params.grat[1] = segment_data[offset + 1];
  457|    508|        params.grat[2] = segment_data[offset + 2];
  458|    508|        params.grat[3] = segment_data[offset + 3];
  459|    508|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  460|    508|                    "grat1: (%d, %d) grat2: (%d, %d)", params.grat[0], params.grat[1], params.grat[2], params.grat[3]);
  461|    508|        offset += 4;
  462|    508|    }
  463|       |
  464|       |    /* 7.4.7.4 - set up the reference image */
  465|    839|    if (segment->referred_to_segment_count) {
  ------------------
  |  Branch (465:9): [True: 172, False: 667]
  ------------------
  466|    172|        Jbig2Segment *ref;
  467|       |
  468|    172|        ref = jbig2_region_find_referred(ctx, segment);
  469|    172|        if (ref == NULL)
  ------------------
  |  Branch (469:13): [True: 23, False: 149]
  ------------------
  470|     23|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to find reference bitmap");
  471|    149|        if (ref->result == NULL)
  ------------------
  |  Branch (471:13): [True: 0, False: 149]
  ------------------
  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|    149|        params.GRREFERENCE = jbig2_image_reference(ctx, (Jbig2Image *) ref->result);
  479|    149|        jbig2_image_release(ctx, (Jbig2Image *) ref->result);
  480|    149|        ref->result = NULL;
  481|    149|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "found reference bitmap in segment %d", ref->number);
  482|    667|    } else {
  483|       |        /* the reference is just (a subset of) the page buffer */
  484|    667|        if (ctx->pages[ctx->current_page].image == NULL)
  ------------------
  |  Branch (484:13): [True: 5, False: 662]
  ------------------
  485|      5|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "reference page bitmap has no decoded image");
  486|    662|        params.GRREFERENCE = jbig2_image_reference(ctx, ctx->pages[ctx->current_page].image);
  487|       |        /* TODO: subset the image if appropriate */
  488|    662|    }
  489|       |
  490|       |    /* 7.4.7.5 */
  491|    811|    params.GRREFERENCEDX = 0;
  492|    811|    params.GRREFERENCEDY = 0;
  493|    811|    {
  494|    811|        Jbig2WordStream *ws = NULL;
  495|    811|        Jbig2ArithState *as = NULL;
  496|    811|        Jbig2ArithCx *GR_stats = NULL;
  497|    811|        int stats_size;
  498|    811|        Jbig2Image *image = NULL;
  499|       |
  500|    811|        image = jbig2_image_new(ctx, rsi.width, rsi.height);
  501|    811|        if (image == NULL) {
  ------------------
  |  Branch (501:13): [True: 10, False: 801]
  ------------------
  502|     10|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate refinement image");
  503|     10|            goto cleanup;
  504|     10|        }
  505|    801|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, rsi.height);
  506|       |
  507|    801|        stats_size = params.GRTEMPLATE ? 1 << 10 : 1 << 13;
  ------------------
  |  Branch (507:22): [True: 312, False: 489]
  ------------------
  508|    801|        GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|    801|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  509|    801|        if (GR_stats == NULL) {
  ------------------
  |  Branch (509:13): [True: 1, False: 800]
  ------------------
  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|    800|        memset(GR_stats, 0, stats_size);
  514|       |
  515|    800|        ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
  516|    800|        if (ws == NULL) {
  ------------------
  |  Branch (516:13): [True: 1, False: 799]
  ------------------
  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|    799|        as = jbig2_arith_new(ctx, ws);
  522|    799|        if (as == NULL) {
  ------------------
  |  Branch (522:13): [True: 2, False: 797]
  ------------------
  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|    797|        code = jbig2_decode_refinement_region(ctx, segment, &params, as, image, GR_stats);
  528|    797|        if (code < 0) {
  ------------------
  |  Branch (528:13): [True: 8, False: 789]
  ------------------
  529|      8|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
  530|      8|            goto cleanup;
  531|      8|        }
  532|       |
  533|    789|        if ((segment->flags & 63) == 40) {
  ------------------
  |  Branch (533:13): [True: 175, False: 614]
  ------------------
  534|       |            /* intermediate region. save the result for later */
  535|    175|            segment->result = jbig2_image_reference(ctx, image);
  536|    614|        } else {
  537|       |            /* immediate region. composite onto the page */
  538|    614|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  539|    614|                        "composing %dx%d decoded refinement region onto page at (%d, %d)", rsi.width, rsi.height, rsi.x, rsi.y);
  540|    614|            code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
  541|    614|            if (code < 0) {
  ------------------
  |  Branch (541:17): [True: 102, False: 512]
  ------------------
  542|    102|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add refinement region to page");
  543|    102|                goto cleanup;
  544|    102|            }
  545|    614|        }
  546|       |
  547|    811|cleanup:
  548|    811|        jbig2_image_release(ctx, image);
  549|    811|        jbig2_image_release(ctx, params.GRREFERENCE);
  550|    811|        jbig2_free(ctx->allocator, as);
  551|    811|        jbig2_word_stream_buf_free(ctx, ws);
  552|    811|        jbig2_free(ctx->allocator, GR_stats);
  553|    811|    }
  554|       |
  555|      0|    return code;
  556|    789|}
jbig2_refinement.c:jbig2_decode_refinement_TPGRON:
  311|    520|{
  312|    520|    const int64_t GRW = image->width;
  313|    520|    const int64_t GRH = image->height;
  314|    520|    int64_t x, y;
  315|    520|    int iv, LTP = 0;
  316|    520|    uint32_t start_context = (params->GRTEMPLATE ? 0x40 : 0x100);
  ------------------
  |  Branch (316:31): [True: 162, False: 358]
  ------------------
  317|    520|    ContextBuilder mkctx = (params->GRTEMPLATE ? mkctx1 : mkctx0);
  ------------------
  |  Branch (317:29): [True: 162, False: 358]
  ------------------
  318|       |
  319|    520|    if (params->GRTEMPLATE == 0 &&
  ------------------
  |  Branch (319:9): [True: 358, False: 162]
  ------------------
  320|    358|        (pixel_outside_field(params->grat[0], params->grat[1]) ||
  ------------------
  |  |   47|    716|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (47:6): [True: 0, False: 358]
  |  |  |  Branch (47:20): [True: 4, False: 354]
  |  |  |  Branch (47:31): [True: 0, False: 354]
  |  |  |  Branch (47:46): [True: 146, False: 208]
  |  |  |  Branch (47:57): [True: 0, False: 146]
  |  |  |  Branch (47:72): [True: 208, False: 146]
  |  |  |  Branch (47:84): [True: 1, False: 207]
  |  |  ------------------
  ------------------
  321|    353|        refpixel_outside_field(params->grat[2], params->grat[3])))
  ------------------
  |  |   49|    353|    ((y) < -128 || (y) > 127 || (x) < -128 || (x) > 127)
  |  |  ------------------
  |  |  |  Branch (49:6): [True: 0, False: 353]
  |  |  |  Branch (49:20): [True: 0, False: 353]
  |  |  |  Branch (49:33): [True: 0, False: 353]
  |  |  |  Branch (49:47): [True: 0, False: 353]
  |  |  ------------------
  ------------------
  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|  23.4M|    for (y = 0; y < GRH; y++) {
  ------------------
  |  Branch (325:17): [True: 23.4M, False: 515]
  ------------------
  326|  23.4M|        int bit = jbig2_arith_decode(ctx, as, &GR_stats[start_context]);
  327|  23.4M|        if (bit < 0)
  ------------------
  |  Branch (327:13): [True: 0, False: 23.4M]
  ------------------
  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|  23.4M|        LTP ^= bit;
  330|  23.4M|        if (!LTP) {
  ------------------
  |  Branch (330:13): [True: 21.7M, False: 1.73M]
  ------------------
  331|   171M|            for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (331:25): [True: 150M, False: 21.7M]
  ------------------
  332|   150M|                bit = jbig2_arith_decode(ctx, as, &GR_stats[mkctx(params, image, x, y)]);
  333|   150M|                if (bit < 0)
  ------------------
  |  Branch (333:21): [True: 0, False: 150M]
  ------------------
  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|   150M|                jbig2_image_set_pixel(image, x, y, bit);
  336|   150M|            }
  337|  21.7M|        } else {
  338|  88.3M|            for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (338:25): [True: 86.6M, False: 1.73M]
  ------------------
  339|  86.6M|                iv = implicit_value(params, image, x, y);
  340|  86.6M|                if (iv < 0) {
  ------------------
  |  Branch (340:21): [True: 221k, False: 86.4M]
  ------------------
  341|   221k|                    int bit = jbig2_arith_decode(ctx, as, &GR_stats[mkctx(params, image, x, y)]);
  342|   221k|                    if (bit < 0)
  ------------------
  |  Branch (342:25): [True: 0, False: 221k]
  ------------------
  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|   221k|                    jbig2_image_set_pixel(image, x, y, bit);
  345|   221k|                } else
  346|  86.4M|                    jbig2_image_set_pixel(image, x, y, iv);
  347|  86.6M|            }
  348|  1.73M|        }
  349|  23.4M|    }
  350|       |
  351|    515|    return 0;
  352|    515|}
jbig2_refinement.c:mkctx1:
  290|  92.3M|{
  291|  92.3M|    Jbig2Image *ref = params->GRREFERENCE;
  292|  92.3M|    const int32_t dx = params->GRREFERENCEDX;
  293|  92.3M|    const int32_t dy = params->GRREFERENCEDY;
  294|  92.3M|    uint32_t CONTEXT;
  295|       |
  296|  92.3M|    CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
  297|  92.3M|    CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  298|  92.3M|    CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  299|  92.3M|    CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
  300|  92.3M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  301|  92.3M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  302|  92.3M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
  303|  92.3M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
  304|  92.3M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
  305|  92.3M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
  306|  92.3M|    return CONTEXT;
  307|  92.3M|}
jbig2_refinement.c:mkctx0:
  266|  57.8M|{
  267|  57.8M|    Jbig2Image *ref = params->GRREFERENCE;
  268|  57.8M|    const int32_t dx = params->GRREFERENCEDX;
  269|  57.8M|    const int32_t dy = params->GRREFERENCEDY;
  270|  57.8M|    uint32_t CONTEXT;
  271|       |
  272|  57.8M|    CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
  273|  57.8M|    CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  274|  57.8M|    CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  275|  57.8M|    CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y + params->grat[1]) << 3;
  276|  57.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  277|  57.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  278|  57.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
  279|  57.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
  280|  57.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
  281|  57.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
  282|  57.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
  283|  57.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
  284|  57.8M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
  285|  57.8M|    return CONTEXT;
  286|  57.8M|}
jbig2_refinement.c:implicit_value:
  245|  86.6M|{
  246|  86.6M|    Jbig2Image *ref = params->GRREFERENCE;
  247|  86.6M|    const int64_t i = x - params->GRREFERENCEDX;
  248|  86.6M|    const int64_t j = y - params->GRREFERENCEDY;
  249|  86.6M|    int m = jbig2_image_get_pixel(ref, i, j);
  250|       |
  251|  86.6M|    (void) image;
  252|       |
  253|  86.6M|    return ((jbig2_image_get_pixel(ref, i - 1, j - 1) == m) &&
  ------------------
  |  Branch (253:13): [True: 86.5M, False: 125k]
  ------------------
  254|  86.5M|            (jbig2_image_get_pixel(ref, i, j - 1) == m) &&
  ------------------
  |  Branch (254:13): [True: 86.4M, False: 53.3k]
  ------------------
  255|  86.4M|            (jbig2_image_get_pixel(ref, i + 1, j - 1) == m) &&
  ------------------
  |  Branch (255:13): [True: 86.4M, False: 18.2k]
  ------------------
  256|  86.4M|            (jbig2_image_get_pixel(ref, i - 1, j) == m) &&
  ------------------
  |  Branch (256:13): [True: 86.4M, False: 10.0k]
  ------------------
  257|  86.4M|            (jbig2_image_get_pixel(ref, i + 1, j) == m) &&
  ------------------
  |  Branch (257:13): [True: 86.4M, False: 4.97k]
  ------------------
  258|  86.4M|            (jbig2_image_get_pixel(ref, i - 1, j + 1) == m) &&
  ------------------
  |  Branch (258:13): [True: 86.4M, False: 6.85k]
  ------------------
  259|  86.4M|            (jbig2_image_get_pixel(ref, i, j + 1) == m) &&
  ------------------
  |  Branch (259:13): [True: 86.4M, False: 1.46k]
  ------------------
  260|  86.4M|            (jbig2_image_get_pixel(ref, i + 1, j + 1) == m)
  ------------------
  |  Branch (260:13): [True: 86.4M, False: 1.45k]
  ------------------
  261|  86.6M|           )? m : -1;
  262|  86.6M|}
jbig2_refinement.c:jbig2_decode_refinement_template1_unopt:
  117|  3.23k|{
  118|  3.23k|    const int64_t GRW = image->width;
  119|  3.23k|    const int64_t GRH = image->height;
  120|  3.23k|    Jbig2Image *ref = params->GRREFERENCE;
  121|  3.23k|    const int32_t dx = params->GRREFERENCEDX;
  122|  3.23k|    const int32_t dy = params->GRREFERENCEDY;
  123|  3.23k|    uint32_t CONTEXT;
  124|  3.23k|    int64_t x, y;
  125|  3.23k|    int bit;
  126|       |
  127|  13.0M|    for (y = 0; y < GRH; y++) {
  ------------------
  |  Branch (127:17): [True: 13.0M, False: 3.23k]
  ------------------
  128|   196M|        for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (128:21): [True: 183M, False: 13.0M]
  ------------------
  129|   183M|            CONTEXT = 0;
  130|   183M|            CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
  131|   183M|            CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  132|   183M|            CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  133|   183M|            CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
  134|   183M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  135|   183M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  136|   183M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
  137|   183M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
  138|   183M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
  139|   183M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
  140|   183M|            bit = jbig2_arith_decode(ctx, as, &GR_stats[CONTEXT]);
  141|   183M|            if (bit < 0)
  ------------------
  |  Branch (141:17): [True: 0, False: 183M]
  ------------------
  142|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling refinement template0");
  143|   183M|            jbig2_image_set_pixel(image, x, y, bit);
  144|   183M|        }
  145|  13.0M|    }
  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|  3.23k|    return 0;
  166|  3.23k|}
jbig2_refinement.c:jbig2_decode_refinement_template0_unopt:
   55|  98.1k|{
   56|  98.1k|    const int64_t GRW = image->width;
   57|  98.1k|    const int64_t GRH = image->height;
   58|  98.1k|    Jbig2Image *ref = params->GRREFERENCE;
   59|  98.1k|    const int32_t dx = params->GRREFERENCEDX;
   60|  98.1k|    const int32_t dy = params->GRREFERENCEDY;
   61|  98.1k|    uint32_t CONTEXT;
   62|  98.1k|    int64_t x, y;
   63|  98.1k|    int bit;
   64|       |
   65|  98.1k|    if (pixel_outside_field(params->grat[0], params->grat[1]) ||
  ------------------
  |  |   47|   196k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (47:6): [True: 0, False: 98.1k]
  |  |  |  Branch (47:20): [True: 3, False: 98.1k]
  |  |  |  Branch (47:31): [True: 0, False: 98.1k]
  |  |  |  Branch (47:46): [True: 31.7k, False: 66.4k]
  |  |  |  Branch (47:57): [True: 0, False: 31.7k]
  |  |  |  Branch (47:72): [True: 66.4k, False: 31.7k]
  |  |  |  Branch (47:84): [True: 7, False: 66.3k]
  |  |  ------------------
  ------------------
   66|  98.1k|        refpixel_outside_field(params->grat[2], params->grat[3]))
  ------------------
  |  |   49|  98.1k|    ((y) < -128 || (y) > 127 || (x) < -128 || (x) > 127)
  |  |  ------------------
  |  |  |  Branch (49:6): [True: 0, False: 98.1k]
  |  |  |  Branch (49:20): [True: 0, False: 98.1k]
  |  |  |  Branch (49:33): [True: 0, False: 98.1k]
  |  |  |  Branch (49:47): [True: 0, False: 98.1k]
  |  |  ------------------
  ------------------
   67|     10|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
   68|     10|                           "adaptive template pixel is out of field");
   69|       |
   70|  1.14M|    for (y = 0; y < GRH; y++) {
  ------------------
  |  Branch (70:17): [True: 1.04M, False: 98.1k]
  ------------------
   71|  32.7M|        for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (71:21): [True: 31.6M, False: 1.04M]
  ------------------
   72|  31.6M|            CONTEXT = 0;
   73|  31.6M|            CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
   74|  31.6M|            CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
   75|  31.6M|            CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
   76|  31.6M|            CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y + params->grat[1]) << 3;
   77|  31.6M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
   78|  31.6M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
   79|  31.6M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
   80|  31.6M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
   81|  31.6M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
   82|  31.6M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
   83|  31.6M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
   84|  31.6M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
   85|  31.6M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
   86|  31.6M|            bit = jbig2_arith_decode(ctx, as, &GR_stats[CONTEXT]);
   87|  31.6M|            if (bit < 0)
  ------------------
  |  Branch (87:17): [True: 0, False: 31.6M]
  ------------------
   88|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling refinement template0");
   89|  31.6M|            jbig2_image_set_pixel(image, x, y, bit);
   90|  31.6M|        }
   91|  1.04M|    }
   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|  98.1k|    return 0;
  111|  98.1k|}
jbig2_refinement.c:jbig2_region_find_referred:
  395|    172|{
  396|    172|    const int nsegments = segment->referred_to_segment_count;
  397|    172|    Jbig2Segment *rsegment;
  398|    172|    int index;
  399|       |
  400|  4.37k|    for (index = 0; index < nsegments; index++) {
  ------------------
  |  Branch (400:21): [True: 4.35k, False: 23]
  ------------------
  401|  4.35k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  402|  4.35k|        if (rsegment == NULL) {
  ------------------
  |  Branch (402:13): [True: 2.04k, False: 2.31k]
  ------------------
  403|  2.04k|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to find referred to segment %d", segment->referred_to_segments[index]);
  404|  2.04k|            continue;
  405|  2.04k|        }
  406|  2.31k|        switch (rsegment->flags & 63) {
  407|  1.58k|        case 4:                /* intermediate text region */
  ------------------
  |  Branch (407:9): [True: 1.58k, False: 726]
  ------------------
  408|  1.59k|        case 20:               /* intermediate halftone region */
  ------------------
  |  Branch (408:9): [True: 4, False: 2.31k]
  ------------------
  409|  1.59k|        case 36:               /* intermediate generic region */
  ------------------
  |  Branch (409:9): [True: 0, False: 2.31k]
  ------------------
  410|  1.75k|        case 40:               /* intermediate generic refinement region */
  ------------------
  |  Branch (410:9): [True: 164, False: 2.15k]
  ------------------
  411|  1.75k|            if (rsegment->result)
  ------------------
  |  Branch (411:17): [True: 149, False: 1.60k]
  ------------------
  412|    149|                return rsegment;
  413|  1.60k|            break;
  414|  1.60k|        default:               /* keep looking */
  ------------------
  |  Branch (414:9): [True: 558, False: 1.75k]
  ------------------
  415|    558|            break;
  416|  2.31k|        }
  417|  2.31k|    }
  418|       |    /* no appropriate reference was found. */
  419|     23|    return NULL;
  420|    172|}

jbig2_parse_segment_header:
   44|  89.2k|{
   45|  89.2k|    Jbig2Segment *result;
   46|  89.2k|    uint8_t rtscarf;
   47|  89.2k|    uint32_t rtscarf_long;
   48|  89.2k|    uint32_t *referred_to_segments;
   49|  89.2k|    uint32_t referred_to_segment_count;
   50|  89.2k|    uint32_t referred_to_segment_size;
   51|  89.2k|    uint32_t pa_size;
   52|  89.2k|    uint32_t offset;
   53|       |
   54|       |    /* minimum possible size of a jbig2 segment header */
   55|  89.2k|    if (buf_size < 11)
  ------------------
  |  Branch (55:9): [True: 488, False: 88.7k]
  ------------------
   56|    488|        return NULL;
   57|       |
   58|  88.7k|    result = jbig2_new(ctx, Jbig2Segment, 1);
  ------------------
  |  |  141|  88.7k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   59|  88.7k|    if (result == NULL) {
  ------------------
  |  Branch (59:9): [True: 1, False: 88.7k]
  ------------------
   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|  88.7k|    result->number = jbig2_get_uint32(buf);
   66|  88.7k|    if (result->number == JBIG2_UNKNOWN_SEGMENT_NUMBER) {
  ------------------
  |  |   68|  88.7k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  |  Branch (66:9): [True: 14, False: 88.6k]
  ------------------
   67|     14|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "segment number too large");
  ------------------
  |  |   68|     14|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   68|     14|        jbig2_free(ctx->allocator, result);
   69|     14|        return NULL;
   70|     14|    }
   71|       |
   72|       |    /* 7.2.3 */
   73|  88.6k|    result->flags = buf[4];
   74|       |
   75|       |    /* 7.2.4 referred-to segments */
   76|  88.6k|    rtscarf = buf[5];
   77|  88.6k|    if ((rtscarf & 0xe0) == 0xe0) {
  ------------------
  |  Branch (77:9): [True: 941, False: 87.7k]
  ------------------
   78|    941|        rtscarf_long = jbig2_get_uint32(buf + 5);
   79|    941|        referred_to_segment_count = rtscarf_long & 0x1fffffff;
   80|    941|        offset = 5 + 4 + (referred_to_segment_count + 1) / 8;
   81|  87.7k|    } else {
   82|  87.7k|        referred_to_segment_count = (rtscarf >> 5);
   83|  87.7k|        offset = 5 + 1;
   84|  87.7k|    }
   85|  88.6k|    result->referred_to_segment_count = referred_to_segment_count;
   86|       |
   87|       |    /* we now have enough information to compute the full header length */
   88|  88.6k|    referred_to_segment_size = result->number <= 256 ? 1 : result->number <= 65536 ? 2 : 4;     /* 7.2.5 */
  ------------------
  |  Branch (88:32): [True: 26.7k, False: 61.9k]
  |  Branch (88:60): [True: 7.37k, False: 54.5k]
  ------------------
   89|  88.6k|    pa_size = result->flags & 0x40 ? 4 : 1;     /* 7.2.6 */
  ------------------
  |  Branch (89:15): [True: 11.4k, False: 77.2k]
  ------------------
   90|  88.6k|    if (offset + referred_to_segment_count * referred_to_segment_size + pa_size + 4 > buf_size) {
  ------------------
  |  Branch (90:9): [True: 699, False: 88.0k]
  ------------------
   91|    699|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "attempted to parse segment header with insufficient data, asking for more data");
   92|    699|        jbig2_free(ctx->allocator, result);
   93|    699|        return NULL;
   94|    699|    }
   95|       |
   96|       |    /* 7.2.5 */
   97|  88.0k|    if (referred_to_segment_count) {
  ------------------
  |  Branch (97:9): [True: 22.0k, False: 65.9k]
  ------------------
   98|  22.0k|        uint32_t i;
   99|       |
  100|  22.0k|        referred_to_segments = jbig2_new(ctx, uint32_t, referred_to_segment_count * referred_to_segment_size);
  ------------------
  |  |  141|  22.0k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  101|  22.0k|        if (referred_to_segments == NULL) {
  ------------------
  |  Branch (101:13): [True: 1, False: 22.0k]
  ------------------
  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|   203k|        for (i = 0; i < referred_to_segment_count; i++) {
  ------------------
  |  Branch (107:21): [True: 181k, False: 22.0k]
  ------------------
  108|   181k|            referred_to_segments[i] =
  109|   181k|                (referred_to_segment_size == 1) ? buf[offset] :
  ------------------
  |  Branch (109:17): [True: 124k, False: 56.7k]
  ------------------
  110|   181k|                (referred_to_segment_size == 2) ? jbig2_get_uint16(buf + offset) : jbig2_get_uint32(buf + offset);
  ------------------
  |  Branch (110:17): [True: 9.34k, False: 47.3k]
  ------------------
  111|   181k|            offset += referred_to_segment_size;
  112|   181k|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "segment %d refers to segment %d", result->number, referred_to_segments[i]);
  113|   181k|        }
  114|  22.0k|        result->referred_to_segments = referred_to_segments;
  115|  65.9k|    } else {                    /* no referred-to segments */
  116|       |
  117|  65.9k|        result->referred_to_segments = NULL;
  118|  65.9k|    }
  119|       |
  120|       |    /* 7.2.6 */
  121|  87.9k|    if (pa_size == 4) {
  ------------------
  |  Branch (121:9): [True: 11.0k, False: 76.9k]
  ------------------
  122|  11.0k|        result->page_association = jbig2_get_uint32(buf + offset);
  123|  11.0k|        offset += 4;
  124|  76.9k|    } else {
  125|  76.9k|        result->page_association = buf[offset++];
  126|  76.9k|    }
  127|  87.9k|    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|  87.9k|    result->rows = UINT32_MAX;
  131|  87.9k|    result->data_length = jbig2_get_uint32(buf + offset);
  132|  87.9k|    *p_header_size = offset + 4;
  133|       |
  134|       |    /* no body parsing results yet */
  135|  87.9k|    result->result = NULL;
  136|       |
  137|  87.9k|    return result;
  138|  88.0k|}
jbig2_free_segment:
  142|  87.9k|{
  143|  87.9k|    if (segment == NULL)
  ------------------
  |  Branch (143:9): [True: 0, False: 87.9k]
  ------------------
  144|      0|        return;
  145|       |
  146|  87.9k|    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|  87.9k|    switch (segment->flags & 63) {
  151|  22.9k|    case 0:                    /* symbol dictionary */
  ------------------
  |  Branch (151:5): [True: 22.9k, False: 65.0k]
  ------------------
  152|  22.9k|        if (segment->result != NULL)
  ------------------
  |  Branch (152:13): [True: 4.65k, False: 18.2k]
  ------------------
  153|  4.65k|            jbig2_sd_release(ctx, (Jbig2SymbolDict *) segment->result);
  154|  22.9k|        break;
  155|  7.00k|    case 4:                    /* intermediate text region */
  ------------------
  |  Branch (155:5): [True: 7.00k, False: 80.9k]
  ------------------
  156|  7.41k|    case 40:                   /* intermediate refinement region */
  ------------------
  |  Branch (156:5): [True: 410, False: 87.5k]
  ------------------
  157|  7.41k|        if (segment->result != NULL)
  ------------------
  |  Branch (157:13): [True: 183, False: 7.23k]
  ------------------
  158|    183|            jbig2_image_release(ctx, (Jbig2Image *) segment->result);
  159|  7.41k|        break;
  160|  4.18k|    case 16:                   /* pattern dictionary */
  ------------------
  |  Branch (160:5): [True: 4.18k, False: 83.8k]
  ------------------
  161|  4.18k|        if (segment->result != NULL)
  ------------------
  |  Branch (161:13): [True: 2.88k, False: 1.29k]
  ------------------
  162|  2.88k|            jbig2_hd_release(ctx, (Jbig2PatternDict *) segment->result);
  163|  4.18k|        break;
  164|  2.08k|    case 53:                   /* user-supplied huffman table */
  ------------------
  |  Branch (164:5): [True: 2.08k, False: 85.9k]
  ------------------
  165|  2.08k|        if (segment->result != NULL)
  ------------------
  |  Branch (165:13): [True: 1.60k, False: 481]
  ------------------
  166|  1.60k|            jbig2_table_free(ctx, (Jbig2HuffmanParams *) segment->result);
  167|  2.08k|        break;
  168|  51.3k|    default:
  ------------------
  |  Branch (168:5): [True: 51.3k, False: 36.6k]
  ------------------
  169|       |        /* anything else is probably an undefined pointer */
  170|  51.3k|        break;
  171|  87.9k|    }
  172|  87.9k|    jbig2_free(ctx->allocator, segment);
  173|  87.9k|}
jbig2_find_segment:
  178|   338k|{
  179|   338k|    int index, index_max = ctx->segment_index - 1;
  180|   338k|    const Jbig2Ctx *global_ctx = ctx->global_ctx;
  181|       |
  182|       |    /* FIXME: binary search would be better */
  183|  2.19M|    for (index = index_max; index >= 0; index--)
  ------------------
  |  Branch (183:29): [True: 2.01M, False: 181k]
  ------------------
  184|  2.01M|        if (ctx->segments[index]->number == number)
  ------------------
  |  Branch (184:13): [True: 157k, False: 1.85M]
  ------------------
  185|   157k|            return (ctx->segments[index]);
  186|       |
  187|   181k|    if (global_ctx)
  ------------------
  |  Branch (187:9): [True: 0, False: 181k]
  ------------------
  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|   181k|    return NULL;
  194|   181k|}
jbig2_get_region_segment_info:
  199|  24.2k|{
  200|       |    /* 7.4.1 */
  201|  24.2k|    info->width = jbig2_get_uint32(segment_data);
  202|  24.2k|    info->height = jbig2_get_uint32(segment_data + 4);
  203|  24.2k|    info->x = jbig2_get_uint32(segment_data + 8);
  204|  24.2k|    info->y = jbig2_get_uint32(segment_data + 12);
  205|  24.2k|    info->flags = segment_data[16];
  206|  24.2k|    info->op = (Jbig2ComposeOp)(info->flags & 0x7);
  207|  24.2k|}
jbig2_parse_segment:
  337|  46.4k|{
  338|  46.4k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  339|  46.4k|                "segment %d, flags=%x, type=%d, data_length=%ld", segment->number, segment->flags, segment->flags & 63, (long) segment->data_length);
  340|  46.4k|    switch (segment->flags & 63) {
  341|  6.02k|    case 0:
  ------------------
  |  Branch (341:5): [True: 6.02k, False: 40.4k]
  ------------------
  342|  6.02k|        return jbig2_symbol_dictionary(ctx, segment, segment_data);
  343|    320|    case 4:                    /* intermediate text region */
  ------------------
  |  Branch (343:5): [True: 320, False: 46.1k]
  ------------------
  344|    481|    case 6:                    /* immediate text region */
  ------------------
  |  Branch (344:5): [True: 161, False: 46.2k]
  ------------------
  345|  3.66k|    case 7:                    /* immediate lossless text region */
  ------------------
  |  Branch (345:5): [True: 3.18k, False: 43.2k]
  ------------------
  346|  3.66k|        return jbig2_text_region(ctx, segment, segment_data);
  347|  3.78k|    case 16:
  ------------------
  |  Branch (347:5): [True: 3.78k, False: 42.6k]
  ------------------
  348|  3.78k|        return jbig2_pattern_dictionary(ctx, segment, segment_data);
  349|     47|    case 20:                   /* intermediate halftone region */
  ------------------
  |  Branch (349:5): [True: 47, False: 46.3k]
  ------------------
  350|    104|    case 22:                   /* immediate halftone region */
  ------------------
  |  Branch (350:5): [True: 57, False: 46.3k]
  ------------------
  351|  2.17k|    case 23:                   /* immediate lossless halftone region */
  ------------------
  |  Branch (351:5): [True: 2.07k, False: 44.3k]
  ------------------
  352|  2.17k|        return jbig2_halftone_region(ctx, segment, segment_data);
  353|      3|    case 36:
  ------------------
  |  Branch (353:5): [True: 3, False: 46.4k]
  ------------------
  354|      3|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unhandled segment type 'intermediate generic region' (NYI)");
  355|  12.6k|    case 38:                   /* immediate generic region */
  ------------------
  |  Branch (355:5): [True: 12.6k, False: 33.7k]
  ------------------
  356|  17.6k|    case 39:                   /* immediate lossless generic region */
  ------------------
  |  Branch (356:5): [True: 4.97k, False: 41.4k]
  ------------------
  357|  17.6k|        return jbig2_immediate_generic_region(ctx, segment, segment_data);
  358|    191|    case 40:                   /* intermediate generic refinement region */
  ------------------
  |  Branch (358:5): [True: 191, False: 46.2k]
  ------------------
  359|    749|    case 42:                   /* immediate generic refinement region */
  ------------------
  |  Branch (359:5): [True: 558, False: 45.8k]
  ------------------
  360|    851|    case 43:                   /* immediate lossless generic refinement region */
  ------------------
  |  Branch (360:5): [True: 102, False: 46.3k]
  ------------------
  361|    851|        return jbig2_refinement_region(ctx, segment, segment_data);
  362|  5.60k|    case 48:
  ------------------
  |  Branch (362:5): [True: 5.60k, False: 40.8k]
  ------------------
  363|  5.60k|        return jbig2_page_info(ctx, segment, segment_data);
  364|  1.47k|    case 49:
  ------------------
  |  Branch (364:5): [True: 1.47k, False: 44.9k]
  ------------------
  365|  1.47k|        return jbig2_end_of_page(ctx, segment, segment_data);
  366|    433|    case 50:
  ------------------
  |  Branch (366:5): [True: 433, False: 45.9k]
  ------------------
  367|    433|        return jbig2_end_of_stripe(ctx, segment, segment_data);
  368|    519|    case 51:
  ------------------
  |  Branch (368:5): [True: 519, False: 45.9k]
  ------------------
  369|    519|        ctx->state = JBIG2_FILE_EOF;
  370|    519|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of file");
  371|    519|        break;
  372|    138|    case 52:
  ------------------
  |  Branch (372:5): [True: 138, False: 46.2k]
  ------------------
  373|    138|        return jbig2_parse_profile_segment(ctx, segment, segment_data);
  374|  1.72k|    case 53:                   /* user-supplied huffman table */
  ------------------
  |  Branch (374:5): [True: 1.72k, False: 44.7k]
  ------------------
  375|  1.72k|        return jbig2_table(ctx, segment, segment_data);
  376|      3|    case 54:
  ------------------
  |  Branch (376:5): [True: 3, False: 46.4k]
  ------------------
  377|      3|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled segment type 'color palette' (NYI)");
  378|    620|    case 62:
  ------------------
  |  Branch (378:5): [True: 620, False: 45.8k]
  ------------------
  379|    620|        return jbig2_parse_extension_segment(ctx, segment, segment_data);
  380|  1.76k|    default:
  ------------------
  |  Branch (380:5): [True: 1.76k, False: 44.6k]
  ------------------
  381|  1.76k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unknown segment type %d", segment->flags & 63);
  382|  46.4k|    }
  383|  2.28k|    return 0;
  384|  46.4k|}
jbig2_segment.c:jbig2_parse_profile_segment:
  253|    138|{
  254|    138|    uint32_t profiles;
  255|    138|    uint32_t i;
  256|    138|    uint32_t profile;
  257|    138|    int index;
  258|    138|    const char *requirements;
  259|    138|    const char *generic_region;
  260|    138|    const char *refinement_region;
  261|    138|    const char *halftone_region;
  262|    138|    const char *numerical_data;
  263|       |
  264|    138|    if (segment->data_length < 4)
  ------------------
  |  Branch (264:9): [True: 9, False: 129]
  ------------------
  265|      9|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
  266|    129|    index = 0;
  267|       |
  268|    129|    profiles = jbig2_get_uint32(&segment_data[index]);
  269|    129|    index += 4;
  270|       |
  271|  9.68k|    for (i = 0; i < profiles; i++) {
  ------------------
  |  Branch (271:17): [True: 9.62k, False: 58]
  ------------------
  272|  9.62k|        if (segment->data_length - index < 4)
  ------------------
  |  Branch (272:13): [True: 71, False: 9.55k]
  ------------------
  273|     71|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short to store profile");
  274|       |
  275|  9.55k|        profile = jbig2_get_uint32(&segment_data[index]);
  276|  9.55k|        index += 4;
  277|       |
  278|  9.55k|        switch (profile) {
  279|    277|        case 0x00000001:
  ------------------
  |  Branch (279:9): [True: 277, False: 9.27k]
  ------------------
  280|    277|            requirements = "All JBIG2 capabilities";
  281|    277|            generic_region = "No restriction";
  282|    277|            refinement_region = "No restriction";
  283|    277|            halftone_region = "No restriction";
  284|    277|            numerical_data = "No restriction";
  285|    277|            break;
  286|     40|        case 0x00000002:
  ------------------
  |  Branch (286:9): [True: 40, False: 9.51k]
  ------------------
  287|     40|            requirements = "Maximum compression";
  288|     40|            generic_region = "Arithmetic only; any template used";
  289|     40|            refinement_region = "No restriction";
  290|     40|            halftone_region = "No restriction";
  291|     40|            numerical_data = "Arithmetic only";
  292|     40|            break;
  293|    201|        case 0x00000003:
  ------------------
  |  Branch (293:9): [True: 201, False: 9.35k]
  ------------------
  294|    201|            requirements = "Medium complexity and medium compression";
  295|    201|            generic_region = "Arithmetic only; only 10-pixel and 13-pixel templates";
  296|    201|            refinement_region = "10-pixel template only";
  297|    201|            halftone_region = "No skip mask used";
  298|    201|            numerical_data = "Arithmetic only";
  299|    201|            break;
  300|      4|        case 0x00000004:
  ------------------
  |  Branch (300:9): [True: 4, False: 9.55k]
  ------------------
  301|      4|            requirements = "Low complexity with progressive lossless capability";
  302|      4|            generic_region = "MMR only";
  303|      4|            refinement_region = "10-pixel template only";
  304|      4|            halftone_region = "No skip mask used";
  305|      4|            numerical_data = "Huffman only";
  306|      4|            break;
  307|     16|        case 0x00000005:
  ------------------
  |  Branch (307:9): [True: 16, False: 9.53k]
  ------------------
  308|     16|            requirements = "Low complexity";
  309|     16|            generic_region = "MMR only";
  310|     16|            refinement_region = "Not available";
  311|     16|            halftone_region = "No skip mask used";
  312|     16|            numerical_data = "Huffman only";
  313|     16|            break;
  314|  9.01k|        default:
  ------------------
  |  Branch (314:9): [True: 9.01k, False: 538]
  ------------------
  315|  9.01k|            requirements = "Unknown";
  316|  9.01k|            generic_region = "Unknown";
  317|  9.01k|            refinement_region = "Unknown";
  318|  9.01k|            halftone_region = "Unknown";
  319|  9.01k|            numerical_data = "Unknown";
  320|  9.01k|            break;
  321|  9.55k|        }
  322|       |
  323|  9.55k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "Supported profile: 0x%08x", profile);
  324|  9.55k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Requirements: %s", requirements);
  325|  9.55k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Generic region coding: %s", generic_region);
  326|  9.55k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Refinement region coding: %s", refinement_region);
  327|  9.55k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Halftone region coding: %s", halftone_region);
  328|  9.55k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Numerical data: %s", numerical_data);
  329|  9.55k|    }
  330|       |
  331|     58|    return 0;
  332|    129|}
jbig2_segment.c:jbig2_parse_extension_segment:
  212|    620|{
  213|    620|    uint32_t type;
  214|    620|    bool reserved;
  ------------------
  |  |   45|    620|#define bool int
  ------------------
  215|    620|    bool necessary;
  ------------------
  |  |   45|    620|#define bool int
  ------------------
  216|       |
  217|    620|    if (segment->data_length < 4)
  ------------------
  |  Branch (217:9): [True: 12, False: 608]
  ------------------
  218|     12|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  219|       |
  220|    608|    type = jbig2_get_uint32(segment_data);
  221|    608|    reserved = type & 0x20000000;
  222|       |    /* Not implemented since this bit
  223|       |    is only needed by encoders.
  224|       |    dependent = type & 0x40000000;
  225|       |    */
  226|    608|    necessary = type & 0x80000000;
  227|       |
  228|    608|    if (necessary && !reserved) {
  ------------------
  |  Branch (228:9): [True: 36, False: 572]
  |  Branch (228:22): [True: 4, False: 32]
  ------------------
  229|      4|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extension segment is marked 'necessary' but not 'reserved' contrary to spec");
  230|      4|    }
  231|       |
  232|    608|    switch (type) {
  233|    265|    case 0x20000000:
  ------------------
  |  Branch (233:5): [True: 265, False: 343]
  ------------------
  234|    265|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "ignoring ASCII comment");
  235|    265|        break;
  236|     70|    case 0x20000002:
  ------------------
  |  Branch (236:5): [True: 70, False: 538]
  ------------------
  237|     70|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "ignoring UCS-2 comment");
  238|     70|        break;
  239|    273|    default:
  ------------------
  |  Branch (239:5): [True: 273, False: 335]
  ------------------
  240|    273|        if (necessary) {
  ------------------
  |  Branch (240:13): [True: 36, False: 237]
  ------------------
  241|     36|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unhandled necessary extension segment type 0x%08x", type);
  242|    237|        } else {
  243|    237|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled non-necessary extension segment, skipping");
  244|    237|        }
  245|    608|    }
  246|       |
  247|    572|    return 0;
  248|    608|}

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.9k, False: 5.56k]
  ------------------
  113|  11.9k|        memset(new_dict->glyphs, 0, n_symbols * sizeof(Jbig2Image *));
  114|  11.9k|    } else if (new_dict->n_symbols > 0) {
  ------------------
  |  Branch (114:16): [True: 146, False: 5.42k]
  ------------------
  115|    146|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate glyphs for new empty symbol dictionary");
  ------------------
  |  |   68|    146|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  116|    146|        jbig2_free(ctx->allocator, new_dict);
  117|    146|        return NULL;
  118|    146|    }
  119|       |
  120|  17.3k|    return new_dict;
  121|  17.5k|}
jbig2_sd_release:
  126|  22.3k|{
  127|  22.3k|    uint32_t i;
  128|       |
  129|  22.3k|    if (dict == NULL)
  ------------------
  |  Branch (129:9): [True: 5.01k, False: 17.3k]
  ------------------
  130|  5.01k|        return;
  131|  17.3k|    if (dict->glyphs != NULL)
  ------------------
  |  Branch (131:9): [True: 11.9k, False: 5.42k]
  ------------------
  132|   404M|        for (i = 0; i < dict->n_symbols; i++)
  ------------------
  |  Branch (132:21): [True: 403M, False: 11.9k]
  ------------------
  133|   403M|            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.4k|{
  151|  13.4k|    int index;
  152|  13.4k|    Jbig2Segment *rsegment;
  153|  13.4k|    uint32_t n_dicts = 0;
  154|       |
  155|   243k|    for (index = 0; index < segment->referred_to_segment_count; index++) {
  ------------------
  |  Branch (155:21): [True: 230k, False: 13.4k]
  ------------------
  156|   230k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  157|   230k|        if (rsegment && ((rsegment->flags & 63) == 0) &&
  ------------------
  |  Branch (157:13): [True: 104k, False: 125k]
  |  Branch (157:25): [True: 78.4k, False: 25.8k]
  ------------------
  158|  78.4k|            rsegment->result && (((Jbig2SymbolDict *) rsegment->result)->n_symbols > 0) && ((*((Jbig2SymbolDict *) rsegment->result)->glyphs) != NULL))
  ------------------
  |  Branch (158:13): [True: 78.4k, False: 0]
  |  Branch (158:33): [True: 72.8k, False: 5.64k]
  |  Branch (158:92): [True: 69.9k, False: 2.86k]
  ------------------
  159|  69.9k|            n_dicts++;
  160|   230k|    }
  161|       |
  162|  13.4k|    return (n_dicts);
  163|  13.4k|}
jbig2_sd_list_referred:
  168|  3.92k|{
  169|  3.92k|    int index;
  170|  3.92k|    Jbig2Segment *rsegment;
  171|  3.92k|    Jbig2SymbolDict **dicts;
  172|  3.92k|    uint32_t n_dicts = jbig2_sd_count_referred(ctx, segment);
  173|  3.92k|    uint32_t dindex = 0;
  174|       |
  175|  3.92k|    dicts = jbig2_new(ctx, Jbig2SymbolDict *, n_dicts);
  ------------------
  |  |  141|  3.92k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  176|  3.92k|    if (dicts == NULL) {
  ------------------
  |  Branch (176:9): [True: 2, False: 3.92k]
  ------------------
  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|   104k|    for (index = 0; index < segment->referred_to_segment_count; index++) {
  ------------------
  |  Branch (181:21): [True: 100k, False: 3.92k]
  ------------------
  182|   100k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  183|   100k|        if (rsegment && ((rsegment->flags & 63) == 0) && rsegment->result &&
  ------------------
  |  Branch (183:13): [True: 47.3k, False: 52.6k]
  |  Branch (183:25): [True: 35.1k, False: 12.2k]
  |  Branch (183:58): [True: 35.1k, False: 0]
  ------------------
  184|  35.1k|                (((Jbig2SymbolDict *) rsegment->result)->n_symbols > 0) && ((*((Jbig2SymbolDict *) rsegment->result)->glyphs) != NULL)) {
  ------------------
  |  Branch (184:17): [True: 32.6k, False: 2.40k]
  |  Branch (184:76): [True: 31.6k, False: 1.03k]
  ------------------
  185|       |            /* add this referred to symbol dictionary */
  186|  31.6k|            dicts[dindex++] = (Jbig2SymbolDict *) rsegment->result;
  187|  31.6k|        }
  188|   100k|    }
  189|       |
  190|  3.92k|    if (dindex != n_dicts) {
  ------------------
  |  Branch (190:9): [True: 0, False: 3.92k]
  ------------------
  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.92k|    return (dicts);
  198|  3.92k|}
jbig2_sd_cat:
  204|  1.05k|{
  205|  1.05k|    uint32_t i, j, k, symbols;
  206|  1.05k|    Jbig2SymbolDict *new_dict = NULL;
  207|       |
  208|       |    /* count the imported symbols and allocate a new array */
  209|  1.05k|    symbols = 0;
  210|  22.3k|    for (i = 0; i < n_dicts; i++)
  ------------------
  |  Branch (210:17): [True: 21.2k, False: 1.05k]
  ------------------
  211|  21.2k|    {
  212|  21.2k|        if (dicts[i]->n_symbols > UINT32_MAX - symbols)
  ------------------
  |  Branch (212:13): [True: 0, False: 21.2k]
  ------------------
  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|  21.2k|        symbols += dicts[i]->n_symbols;
  218|  21.2k|    }
  219|       |
  220|       |    /* fill a new array with new references to glyph pointers */
  221|  1.05k|    new_dict = jbig2_sd_new(ctx, symbols);
  222|  1.05k|    if (new_dict != NULL) {
  ------------------
  |  Branch (222:9): [True: 1.05k, False: 4]
  ------------------
  223|  1.05k|        k = 0;
  224|  22.3k|        for (i = 0; i < n_dicts; i++)
  ------------------
  |  Branch (224:21): [True: 21.2k, False: 1.05k]
  ------------------
  225|  16.5M|            for (j = 0; j < dicts[i]->n_symbols; j++)
  ------------------
  |  Branch (225:25): [True: 16.5M, False: 21.2k]
  ------------------
  226|  16.5M|                new_dict->glyphs[k++] = jbig2_image_reference(ctx, dicts[i]->glyphs[j]);
  227|  1.05k|    } else {
  228|      4|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate new symbol dictionary");
  ------------------
  |  |   68|      4|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  229|      4|    }
  230|       |
  231|  1.05k|    return new_dict;
  232|  1.05k|}
jbig2_symbol_dictionary:
  872|  6.02k|{
  873|  6.02k|    Jbig2SymbolDictParams params;
  874|  6.02k|    uint16_t flags;
  875|  6.02k|    uint32_t sdat_bytes;
  876|  6.02k|    uint32_t offset;
  877|  6.02k|    Jbig2ArithCx *GB_stats = NULL;
  878|  6.02k|    Jbig2ArithCx *GR_stats = NULL;
  879|  6.02k|    int table_index = 0;
  880|  6.02k|    const Jbig2HuffmanParams *huffman_params;
  881|       |
  882|  6.02k|    params.SDHUFF = 0;
  883|       |
  884|  6.02k|    if (segment->data_length < 10)
  ------------------
  |  Branch (884:9): [True: 32, False: 5.99k]
  ------------------
  885|     32|        goto too_short;
  886|       |
  887|       |    /* 7.4.2.1.1 */
  888|  5.99k|    flags = jbig2_get_uint16(segment_data);
  889|       |
  890|       |    /* zero params to ease cleanup later */
  891|  5.99k|    memset(&params, 0, sizeof(Jbig2SymbolDictParams));
  892|       |
  893|  5.99k|    params.SDHUFF = flags & 1;
  894|  5.99k|    params.SDREFAGG = (flags >> 1) & 1;
  895|  5.99k|    params.SDTEMPLATE = (flags >> 10) & 3;
  896|  5.99k|    params.SDRTEMPLATE = (flags >> 12) & 1;
  897|       |
  898|  5.99k|    if (params.SDHUFF) {
  ------------------
  |  Branch (898:9): [True: 3.32k, False: 2.67k]
  ------------------
  899|  3.32k|        switch ((flags & 0x000c) >> 2) {
  900|  2.96k|        case 0:                /* Table B.4 */
  ------------------
  |  Branch (900:9): [True: 2.96k, False: 361]
  ------------------
  901|  2.96k|            params.SDHUFFDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_D);
  902|  2.96k|            break;
  903|    238|        case 1:                /* Table B.5 */
  ------------------
  |  Branch (903:9): [True: 238, False: 3.08k]
  ------------------
  904|    238|            params.SDHUFFDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_E);
  905|    238|            break;
  906|    122|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (906:9): [True: 122, False: 3.20k]
  ------------------
  907|    122|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  908|    122|            if (huffman_params == NULL) {
  ------------------
  |  Branch (908:17): [True: 10, False: 112]
  ------------------
  909|     10|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DH huffman table not found (%d)", table_index);
  910|     10|                goto cleanup;
  911|     10|            }
  912|    112|            params.SDHUFFDH = jbig2_build_huffman_table(ctx, huffman_params);
  913|    112|            ++table_index;
  914|    112|            break;
  915|      1|        case 2:
  ------------------
  |  Branch (915:9): [True: 1, False: 3.32k]
  ------------------
  916|      1|        default:
  ------------------
  |  Branch (916:9): [True: 0, False: 3.32k]
  ------------------
  917|      1|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "symbol dictionary specified invalid huffman table");
  918|  3.32k|        }
  919|  3.31k|        if (params.SDHUFFDH == NULL) {
  ------------------
  |  Branch (919:13): [True: 4, False: 3.30k]
  ------------------
  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.30k|        switch ((flags & 0x0030) >> 4) {
  925|  2.87k|        case 0:                /* Table B.2 */
  ------------------
  |  Branch (925:9): [True: 2.87k, False: 438]
  ------------------
  926|  2.87k|            params.SDHUFFDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_B);
  927|  2.87k|            break;
  928|    336|        case 1:                /* Table B.3 */
  ------------------
  |  Branch (928:9): [True: 336, False: 2.97k]
  ------------------
  929|    336|            params.SDHUFFDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_C);
  930|    336|            break;
  931|     98|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (931:9): [True: 98, False: 3.21k]
  ------------------
  932|     98|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  933|     98|            if (huffman_params == NULL) {
  ------------------
  |  Branch (933:17): [True: 10, False: 88]
  ------------------
  934|     10|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DW huffman table not found (%d)", table_index);
  935|     10|                goto cleanup;
  936|     10|            }
  937|     88|            params.SDHUFFDW = jbig2_build_huffman_table(ctx, huffman_params);
  938|     88|            ++table_index;
  939|     88|            break;
  940|      4|        case 2:
  ------------------
  |  Branch (940:9): [True: 4, False: 3.30k]
  ------------------
  941|      4|        default:
  ------------------
  |  Branch (941:9): [True: 0, False: 3.30k]
  ------------------
  942|      4|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "symbol dictionary specified invalid huffman table");
  943|      4|            goto cleanup;       /* Jump direct to cleanup to avoid 2 errors being given */
  944|  3.30k|        }
  945|  3.29k|        if (params.SDHUFFDW == NULL) {
  ------------------
  |  Branch (945:13): [True: 7, False: 3.28k]
  ------------------
  946|      7|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate DW huffman table");
  947|      7|            goto cleanup;
  948|      7|        }
  949|       |
  950|  3.28k|        if (flags & 0x0040) {
  ------------------
  |  Branch (950:13): [True: 102, False: 3.18k]
  ------------------
  951|       |            /* Custom table from referred segment */
  952|    102|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  953|    102|            if (huffman_params == NULL) {
  ------------------
  |  Branch (953:17): [True: 7, False: 95]
  ------------------
  954|      7|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom BMSIZE huffman table not found (%d)", table_index);
  955|      7|                goto cleanup;
  956|      7|            }
  957|     95|            params.SDHUFFBMSIZE = jbig2_build_huffman_table(ctx, huffman_params);
  958|     95|            ++table_index;
  959|  3.18k|        } else {
  960|       |            /* Table B.1 */
  961|  3.18k|            params.SDHUFFBMSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
  962|  3.18k|        }
  963|  3.28k|        if (params.SDHUFFBMSIZE == NULL) {
  ------------------
  |  Branch (963:13): [True: 1, False: 3.27k]
  ------------------
  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.27k|        if (flags & 0x0080) {
  ------------------
  |  Branch (968:13): [True: 49, False: 3.23k]
  ------------------
  969|       |            /* Custom table from referred segment */
  970|     49|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  971|     49|            if (huffman_params == NULL) {
  ------------------
  |  Branch (971:17): [True: 4, False: 45]
  ------------------
  972|      4|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom REFAGG huffman table not found (%d)", table_index);
  973|      4|                goto cleanup;
  974|      4|            }
  975|     45|            params.SDHUFFAGGINST = jbig2_build_huffman_table(ctx, huffman_params);
  976|     45|            ++table_index;
  977|  3.23k|        } else {
  978|       |            /* Table B.1 */
  979|  3.23k|            params.SDHUFFAGGINST = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
  980|  3.23k|        }
  981|  3.27k|        if (params.SDHUFFAGGINST == NULL) {
  ------------------
  |  Branch (981:13): [True: 1, False: 3.27k]
  ------------------
  982|      1|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate REFAGG huffman table");
  983|      1|            goto cleanup;
  984|      1|        }
  985|  3.27k|    }
  986|       |
  987|       |    /* FIXME: there are quite a few of these conditions to check */
  988|       |    /* maybe #ifdef CONFORMANCE and a separate routine */
  989|  5.94k|    if (!params.SDHUFF) {
  ------------------
  |  Branch (989:9): [True: 2.67k, False: 3.27k]
  ------------------
  990|  2.67k|        if (flags & 0x000c) {
  ------------------
  |  Branch (990:13): [True: 5, False: 2.66k]
  ------------------
  991|      5|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "SDHUFF is zero, but contrary to spec SDHUFFDH is not.");
  992|      5|            goto cleanup;
  993|      5|        }
  994|  2.66k|        if (flags & 0x0030) {
  ------------------
  |  Branch (994:13): [True: 2, False: 2.66k]
  ------------------
  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.66k|    }
  999|       |
 1000|       |    /* 7.4.2.1.2 */
 1001|  5.93k|    sdat_bytes = params.SDHUFF ? 0 : params.SDTEMPLATE == 0 ? 8 : 2;
  ------------------
  |  Branch (1001:18): [True: 3.27k, False: 2.66k]
  |  Branch (1001:38): [True: 275, False: 2.38k]
  ------------------
 1002|  5.93k|    memcpy(params.sdat, segment_data + 2, sdat_bytes);
 1003|  5.93k|    offset = 2 + sdat_bytes;
 1004|       |
 1005|       |    /* 7.4.2.1.3 */
 1006|  5.93k|    if (params.SDREFAGG && !params.SDRTEMPLATE) {
  ------------------
  |  Branch (1006:9): [True: 1.06k, False: 4.86k]
  |  Branch (1006:28): [True: 790, False: 278]
  ------------------
 1007|    790|        if (offset + 4 > segment->data_length)
  ------------------
  |  Branch (1007:13): [True: 1, False: 789]
  ------------------
 1008|      1|            goto too_short;
 1009|    789|        memcpy(params.sdrat, segment_data + offset, 4);
 1010|    789|        offset += 4;
 1011|    789|    }
 1012|       |
 1013|  5.93k|    if (offset + 8 > segment->data_length)
  ------------------
  |  Branch (1013:9): [True: 8, False: 5.92k]
  ------------------
 1014|      8|        goto too_short;
 1015|       |
 1016|       |    /* 7.4.2.1.4 */
 1017|  5.92k|    params.SDNUMEXSYMS = jbig2_get_uint32(segment_data + offset);
 1018|       |    /* 7.4.2.1.5 */
 1019|  5.92k|    params.SDNUMNEWSYMS = jbig2_get_uint32(segment_data + offset + 4);
 1020|  5.92k|    offset += 8;
 1021|       |
 1022|  5.92k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
 1023|  5.92k|                "symbol dictionary, flags=%04x, %u exported syms, %u new syms", flags, params.SDNUMEXSYMS, params.SDNUMNEWSYMS);
 1024|       |
 1025|       |    /* 7.4.2.2 (2) */
 1026|  5.92k|    {
 1027|  5.92k|        uint32_t n_dicts = jbig2_sd_count_referred(ctx, segment);
 1028|  5.92k|        Jbig2SymbolDict **dicts = NULL;
 1029|       |
 1030|  5.92k|        if (n_dicts > 0) {
  ------------------
  |  Branch (1030:13): [True: 1.05k, False: 4.86k]
  ------------------
 1031|  1.05k|            dicts = jbig2_sd_list_referred(ctx, segment);
 1032|  1.05k|            if (dicts == NULL) {
  ------------------
  |  Branch (1032:17): [True: 1, False: 1.05k]
  ------------------
 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.05k|            params.SDINSYMS = jbig2_sd_cat(ctx, n_dicts, dicts);
 1037|  1.05k|            if (params.SDINSYMS == NULL) {
  ------------------
  |  Branch (1037:17): [True: 4, False: 1.05k]
  ------------------
 1038|      4|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol array in symbol dictionary");
 1039|      4|                jbig2_free(ctx->allocator, dicts);
 1040|      4|                goto cleanup;
 1041|      4|            }
 1042|  1.05k|            jbig2_free(ctx->allocator, dicts);
 1043|  1.05k|        }
 1044|  5.92k|        if (params.SDINSYMS != NULL) {
  ------------------
  |  Branch (1044:13): [True: 1.05k, False: 4.86k]
  ------------------
 1045|  1.05k|            params.SDNUMINSYMS = params.SDINSYMS->n_symbols;
 1046|  1.05k|        }
 1047|  5.92k|    }
 1048|       |
 1049|       |    /* 7.4.2.2 (3, 4) */
 1050|  5.92k|    if (flags & 0x0100) {
  ------------------
  |  Branch (1050:9): [True: 10, False: 5.91k]
  ------------------
 1051|     10|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "segment marks bitmap coding context as used (NYI)");
 1052|     10|        goto cleanup;
 1053|  5.91k|    } else {
 1054|  5.91k|        int stats_size = params.SDTEMPLATE == 0 ? 65536 : params.SDTEMPLATE == 1 ? 8192 : 1024;
  ------------------
  |  Branch (1054:26): [True: 2.79k, False: 3.11k]
  |  Branch (1054:59): [True: 621, False: 2.49k]
  ------------------
 1055|       |
 1056|  5.91k|        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  5.91k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
 1057|  5.91k|        if (GB_stats == NULL) {
  ------------------
  |  Branch (1057:13): [True: 1, False: 5.91k]
  ------------------
 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.91k|        memset(GB_stats, 0, sizeof (Jbig2ArithCx) * stats_size);
 1062|       |
 1063|  5.91k|        stats_size = params.SDRTEMPLATE ? 1 << 10 : 1 << 13;
  ------------------
  |  Branch (1063:22): [True: 502, False: 5.41k]
  ------------------
 1064|  5.91k|        GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  5.91k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
 1065|  5.91k|        if (GR_stats == NULL) {
  ------------------
  |  Branch (1065:13): [True: 1, False: 5.91k]
  ------------------
 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.91k|        memset(GR_stats, 0, sizeof (Jbig2ArithCx) * stats_size);
 1071|  5.91k|    }
 1072|       |
 1073|  5.91k|    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.91k|    if (flags & 0x0200) {
  ------------------
  |  Branch (1080:9): [True: 310, False: 5.60k]
  ------------------
 1081|       |        /* todo: retain GB_stats, GR_stats */
 1082|    310|        jbig2_free(ctx->allocator, GR_stats);
 1083|    310|        jbig2_free(ctx->allocator, GB_stats);
 1084|    310|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "segment marks bitmap coding context as retained (NYI)");
 1085|    310|        goto cleanup;
 1086|  5.60k|    } else {
 1087|  5.60k|        jbig2_free(ctx->allocator, GR_stats);
 1088|  5.60k|        jbig2_free(ctx->allocator, GB_stats);
 1089|  5.60k|    }
 1090|       |
 1091|  5.98k|cleanup:
 1092|  5.98k|    if (params.SDHUFF) {
  ------------------
  |  Branch (1092:9): [True: 3.32k, False: 2.66k]
  ------------------
 1093|  3.32k|        jbig2_release_huffman_table(ctx, params.SDHUFFDH);
 1094|  3.32k|        jbig2_release_huffman_table(ctx, params.SDHUFFDW);
 1095|  3.32k|        jbig2_release_huffman_table(ctx, params.SDHUFFBMSIZE);
 1096|  3.32k|        jbig2_release_huffman_table(ctx, params.SDHUFFAGGINST);
 1097|  3.32k|    }
 1098|  5.98k|    jbig2_sd_release(ctx, params.SDINSYMS);
 1099|       |
 1100|  5.98k|    return (segment->result != NULL) ? 0 : -1;
  ------------------
  |  Branch (1100:12): [True: 4.65k, False: 1.32k]
  ------------------
 1101|       |
 1102|     41|too_short:
 1103|     41|    if (params.SDHUFF) {
  ------------------
  |  Branch (1103:9): [True: 2, False: 39]
  ------------------
 1104|      2|        jbig2_release_huffman_table(ctx, params.SDHUFFDH);
 1105|      2|        jbig2_release_huffman_table(ctx, params.SDHUFFDW);
 1106|      2|        jbig2_release_huffman_table(ctx, params.SDHUFFBMSIZE);
 1107|      2|        jbig2_release_huffman_table(ctx, params.SDHUFFAGGINST);
 1108|      2|    }
 1109|     41|    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
 1110|  5.91k|}
jbig2_symbol_dict.c:jbig2_decode_symbol_dict:
  241|  5.91k|{
  242|  5.91k|    Jbig2SymbolDict *SDNEWSYMS = NULL;
  243|  5.91k|    Jbig2SymbolDict *SDEXSYMS = NULL;
  244|  5.91k|    uint32_t HCHEIGHT;
  245|  5.91k|    uint32_t NSYMSDECODED;
  246|  5.91k|    uint32_t SYMWIDTH, TOTWIDTH;
  247|  5.91k|    uint32_t HCFIRSTSYM;
  248|  5.91k|    uint32_t *SDNEWSYMWIDTHS = NULL;
  249|  5.91k|    uint8_t SBSYMCODELEN = 0;
  250|  5.91k|    Jbig2WordStream *ws = NULL;
  251|  5.91k|    Jbig2HuffmanState *hs = NULL;
  252|  5.91k|    Jbig2ArithState *as = NULL;
  253|  5.91k|    Jbig2ArithIntCtx *IADH = NULL;
  254|  5.91k|    Jbig2ArithIntCtx *IADW = NULL;
  255|  5.91k|    Jbig2ArithIntCtx *IAEX = NULL;
  256|  5.91k|    Jbig2ArithIntCtx *IAAI = NULL;
  257|  5.91k|    int code = 0;
  258|  5.91k|    Jbig2SymbolDict **refagg_dicts = NULL;
  259|  5.91k|    uint32_t i;
  260|  5.91k|    Jbig2TextRegionParams tparams;
  261|  5.91k|    Jbig2Image *image = NULL;
  262|  5.91k|    Jbig2Image *glyph = NULL;
  263|  5.91k|    uint32_t emptyruns = 0;
  264|       |
  265|  5.91k|    memset(&tparams, 0, sizeof(tparams));
  266|       |
  267|       |    /* 6.5.5 (3) */
  268|  5.91k|    HCHEIGHT = 0;
  269|  5.91k|    NSYMSDECODED = 0;
  270|       |
  271|  5.91k|    ws = jbig2_word_stream_buf_new(ctx, data, size);
  272|  5.91k|    if (ws == NULL) {
  ------------------
  |  Branch (272:9): [True: 1, False: 5.91k]
  ------------------
  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.91k|    as = jbig2_arith_new(ctx, ws);
  278|  5.91k|    if (as == NULL) {
  ------------------
  |  Branch (278:9): [True: 9, False: 5.90k]
  ------------------
  279|      9|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when decoding symbol dictionary");
  280|      9|        jbig2_word_stream_buf_free(ctx, ws);
  281|      9|        return NULL;
  282|      9|    }
  283|       |
  284|  30.1k|    for (SBSYMCODELEN = 0; ((uint64_t) 1 << SBSYMCODELEN) < ((uint64_t) params->SDNUMINSYMS + params->SDNUMNEWSYMS); SBSYMCODELEN++);
  ------------------
  |  Branch (284:28): [True: 24.2k, False: 5.90k]
  ------------------
  285|       |
  286|  5.90k|    if (params->SDHUFF) {
  ------------------
  |  Branch (286:9): [True: 3.25k, False: 2.64k]
  ------------------
  287|  3.25k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "huffman coded symbol dictionary");
  288|  3.25k|        hs = jbig2_huffman_new(ctx, ws);
  289|  3.25k|        tparams.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  290|  3.25k|        tparams.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  291|  3.25k|        tparams.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A); /* Table B.1 */
  292|  3.25k|        if (hs == NULL || tparams.SBHUFFRDX == NULL ||
  ------------------
  |  Branch (292:13): [True: 1, False: 3.25k]
  |  Branch (292:27): [True: 2, False: 3.25k]
  ------------------
  293|  3.25k|                tparams.SBHUFFRDY == NULL || tparams.SBHUFFRSIZE == NULL) {
  ------------------
  |  Branch (293:17): [True: 2, False: 3.25k]
  |  Branch (293:46): [True: 2, False: 3.24k]
  ------------------
  294|      7|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate for symbol bitmap");
  295|      7|            goto cleanup;
  296|      7|        }
  297|       |        /* 6.5.5 (2) */
  298|  3.24k|        if (!params->SDREFAGG) {
  ------------------
  |  Branch (298:13): [True: 2.63k, False: 611]
  ------------------
  299|  2.63k|            SDNEWSYMWIDTHS = jbig2_new(ctx, uint32_t, params->SDNUMNEWSYMS);
  ------------------
  |  |  141|  2.63k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  300|  2.63k|            if (SDNEWSYMWIDTHS == NULL) {
  ------------------
  |  Branch (300:17): [True: 7, False: 2.63k]
  ------------------
  301|      7|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate symbol widths (%u)", params->SDNUMNEWSYMS);
  302|      7|                goto cleanup;
  303|      7|            }
  304|  2.63k|        } else {
  305|    611|            tparams.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_F);    /* Table B.6 */
  306|    611|            tparams.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_H);    /* Table B.8 */
  307|    611|            tparams.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_K);    /* Table B.11 */
  308|    611|            tparams.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  309|    611|            tparams.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  310|    611|            if (tparams.SBHUFFFS == NULL || tparams.SBHUFFDS == NULL ||
  ------------------
  |  Branch (310:17): [True: 2, False: 609]
  |  Branch (310:45): [True: 1, False: 608]
  ------------------
  311|    608|                    tparams.SBHUFFDT == NULL || tparams.SBHUFFRDW == NULL ||
  ------------------
  |  Branch (311:21): [True: 1, False: 607]
  |  Branch (311:49): [True: 1, False: 606]
  ------------------
  312|    606|                    tparams.SBHUFFRDH == NULL) {
  ------------------
  |  Branch (312:21): [True: 1, False: 605]
  ------------------
  313|      6|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "out of memory creating text region huffman decoder entries");
  314|      6|                goto cleanup;
  315|      6|            }
  316|    611|        }
  317|  3.24k|    } else {
  318|  2.64k|        IADH = jbig2_arith_int_ctx_new(ctx);
  319|  2.64k|        IADW = jbig2_arith_int_ctx_new(ctx);
  320|  2.64k|        IAEX = jbig2_arith_int_ctx_new(ctx);
  321|  2.64k|        IAAI = jbig2_arith_int_ctx_new(ctx);
  322|  2.64k|        if (IADH == NULL || IADW == NULL || IAEX == NULL || IAAI == NULL) {
  ------------------
  |  Branch (322:13): [True: 1, False: 2.64k]
  |  Branch (322:29): [True: 1, False: 2.64k]
  |  Branch (322:45): [True: 1, False: 2.64k]
  |  Branch (322:61): [True: 1, False: 2.64k]
  ------------------
  323|      4|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol bitmap");
  324|      4|            goto cleanup;
  325|      4|        }
  326|  2.64k|        tparams.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
  327|  2.64k|        tparams.IARDX = jbig2_arith_int_ctx_new(ctx);
  328|  2.64k|        tparams.IARDY = jbig2_arith_int_ctx_new(ctx);
  329|  2.64k|        if (tparams.IAID == NULL || tparams.IARDX == NULL ||
  ------------------
  |  Branch (329:13): [True: 16, False: 2.62k]
  |  Branch (329:37): [True: 1, False: 2.62k]
  ------------------
  330|  2.62k|                tparams.IARDY == NULL) {
  ------------------
  |  Branch (330:17): [True: 1, False: 2.62k]
  ------------------
  331|     18|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region arithmetic decoder contexts");
  332|     18|            goto cleanup;
  333|     18|        }
  334|  2.62k|        if (params->SDREFAGG) {
  ------------------
  |  Branch (334:13): [True: 427, False: 2.19k]
  ------------------
  335|       |            /* Values from Table 17, section 6.5.8.2 (2) */
  336|    427|            tparams.IADT = jbig2_arith_int_ctx_new(ctx);
  337|    427|            tparams.IAFS = jbig2_arith_int_ctx_new(ctx);
  338|    427|            tparams.IADS = jbig2_arith_int_ctx_new(ctx);
  339|    427|            tparams.IAIT = jbig2_arith_int_ctx_new(ctx);
  340|       |            /* Table 31 */
  341|    427|            tparams.IARI = jbig2_arith_int_ctx_new(ctx);
  342|    427|            tparams.IARDW = jbig2_arith_int_ctx_new(ctx);
  343|    427|            tparams.IARDH = jbig2_arith_int_ctx_new(ctx);
  344|    427|            if (tparams.IADT == NULL || tparams.IAFS == NULL ||
  ------------------
  |  Branch (344:17): [True: 1, False: 426]
  |  Branch (344:41): [True: 1, False: 425]
  ------------------
  345|    425|                    tparams.IADS == NULL || tparams.IAIT == NULL ||
  ------------------
  |  Branch (345:21): [True: 1, False: 424]
  |  Branch (345:45): [True: 2, False: 422]
  ------------------
  346|    422|                    tparams.IARI == NULL || tparams.IARDW == NULL ||
  ------------------
  |  Branch (346:21): [True: 2, False: 420]
  |  Branch (346:45): [True: 1, False: 419]
  ------------------
  347|    419|                    tparams.IARDH == NULL) {
  ------------------
  |  Branch (347:21): [True: 1, False: 418]
  ------------------
  348|      9|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region arith decoder contexts");
  349|      9|                goto cleanup;
  350|      9|            }
  351|    427|        }
  352|  2.62k|    }
  353|  5.85k|    tparams.SBHUFF = params->SDHUFF;
  354|  5.85k|    tparams.SBREFINE = 1;
  355|  5.85k|    tparams.SBSTRIPS = 1;
  356|  5.85k|    tparams.SBDEFPIXEL = 0;
  357|  5.85k|    tparams.SBCOMBOP = JBIG2_COMPOSE_OR;
  358|  5.85k|    tparams.TRANSPOSED = 0;
  359|  5.85k|    tparams.REFCORNER = JBIG2_CORNER_TOPLEFT;
  360|  5.85k|    tparams.SBDSOFFSET = 0;
  361|  5.85k|    tparams.SBRTEMPLATE = params->SDRTEMPLATE;
  362|       |
  363|       |    /* 6.5.5 (1) */
  364|  5.85k|    SDNEWSYMS = jbig2_sd_new(ctx, params->SDNUMNEWSYMS);
  365|  5.85k|    if (SDNEWSYMS == NULL) {
  ------------------
  |  Branch (365:9): [True: 38, False: 5.81k]
  ------------------
  366|     38|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate new symbols (%u)", params->SDNUMNEWSYMS);
  367|     38|        goto cleanup;
  368|     38|    }
  369|       |
  370|  5.81k|    refagg_dicts = jbig2_new(ctx, Jbig2SymbolDict *, 2);
  ------------------
  |  |  141|  5.81k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  371|  5.81k|    if (refagg_dicts == NULL) {
  ------------------
  |  Branch (371:9): [True: 1, False: 5.81k]
  ------------------
  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.81k|    refagg_dicts[0] = jbig2_sd_new(ctx, params->SDNUMINSYMS);
  376|  5.81k|    if (refagg_dicts[0] == NULL) {
  ------------------
  |  Branch (376:9): [True: 3, False: 5.80k]
  ------------------
  377|      3|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "out of memory allocating symbol dictionary");
  378|      3|        goto cleanup;
  379|      3|    }
  380|  14.3M|    for (i = 0; i < params->SDNUMINSYMS; i++) {
  ------------------
  |  Branch (380:17): [True: 14.3M, False: 5.80k]
  ------------------
  381|  14.3M|        refagg_dicts[0]->glyphs[i] = jbig2_image_reference(ctx, params->SDINSYMS->glyphs[i]);
  382|  14.3M|    }
  383|  5.80k|    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.78k]
  ------------------
  387|  1.51M|        int32_t HCDH, DW;
  388|       |
  389|       |        /* 6.5.6 */
  390|  1.51M|        if (params->SDHUFF) {
  ------------------
  |  Branch (390:13): [True: 10.2k, False: 1.50M]
  ------------------
  391|  10.2k|            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: 4, False: 1.51M]
  ------------------
  396|      4|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode height class delta");
  397|      4|            goto cleanup;
  398|      4|        }
  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: 42, False: 1.51M]
  ------------------
  411|     42|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid HCHEIGHT value");
  412|     42|            goto cleanup;
  413|     42|        }
  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|  24.6M|        for (;;) {
  420|       |            /* 6.5.7 */
  421|  24.6M|            if (params->SDHUFF) {
  ------------------
  |  Branch (421:17): [True: 23.0M, False: 1.59M]
  ------------------
  422|  23.0M|                DW = jbig2_huffman_get(hs, params->SDHUFFDW, &code);
  423|  23.0M|            } else {
  424|  1.59M|                code = jbig2_arith_int_decode(ctx, IADW, as, &DW);
  425|  1.59M|            }
  426|  24.6M|            if (code < 0)
  ------------------
  |  Branch (426:17): [True: 1, False: 24.6M]
  ------------------
  427|      1|            {
  428|      1|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode DW");
  429|      1|                goto cleanup;
  430|      1|            }
  431|       |            /* 6.5.5 (4c.i) */
  432|  24.6M|            if (code > 0) {
  ------------------
  |  Branch (432:17): [True: 1.51M, False: 23.1M]
  ------------------
  433|  1.51M|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "OOB when decoding DW signals end of height class %d", HCHEIGHT);
  434|  1.51M|                break;
  435|  1.51M|            }
  436|       |
  437|       |            /* check for broken symbol table */
  438|  23.1M|            if (NSYMSDECODED >= params->SDNUMNEWSYMS) {
  ------------------
  |  Branch (438:17): [True: 2.56k, False: 23.1M]
  ------------------
  439|  2.56k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "no OOB signaling end of height class %d, continuing", HCHEIGHT);
  440|  2.56k|                break;
  441|  2.56k|            }
  442|       |
  443|  23.1M|            if (DW < 0 && SYMWIDTH < (uint32_t) -DW) {
  ------------------
  |  Branch (443:17): [True: 6.92k, False: 23.1M]
  |  Branch (443:27): [True: 172, False: 6.75k]
  ------------------
  444|    172|                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|    172|                goto cleanup;
  446|    172|            }
  447|  23.1M|            if (DW > 0 && (uint32_t) DW > UINT32_MAX - SYMWIDTH) {
  ------------------
  |  Branch (447:17): [True: 140k, False: 23.0M]
  |  Branch (447:27): [True: 1, False: 140k]
  ------------------
  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|  23.1M|            SYMWIDTH = SYMWIDTH + DW;
  453|  23.1M|            if (SYMWIDTH > UINT32_MAX - TOTWIDTH) {
  ------------------
  |  Branch (453:17): [True: 26, False: 23.1M]
  ------------------
  454|     26|                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|     26|                goto cleanup;
  456|     26|            }
  457|       |
  458|  23.1M|            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|  23.1M|            if (!params->SDHUFF || params->SDREFAGG) {
  ------------------
  |  Branch (463:17): [True: 94.4k, False: 23.0M]
  |  Branch (463:36): [True: 3.11k, False: 23.0M]
  ------------------
  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|  97.5k|                if (!params->SDREFAGG) {
  ------------------
  |  Branch (468:21): [True: 88.4k, False: 9.09k]
  ------------------
  469|  88.4k|                    Jbig2GenericRegionParams region_params;
  470|  88.4k|                    int sdat_bytes;
  471|       |
  472|       |                    /* Table 16 */
  473|  88.4k|                    region_params.MMR = 0;
  474|  88.4k|                    region_params.GBTEMPLATE = params->SDTEMPLATE;
  475|  88.4k|                    region_params.TPGDON = 0;
  476|  88.4k|                    region_params.USESKIP = 0;
  477|  88.4k|                    sdat_bytes = params->SDTEMPLATE == 0 ? 8 : 2;
  ------------------
  |  Branch (477:34): [True: 20.7k, False: 67.7k]
  ------------------
  478|  88.4k|                    memcpy(region_params.gbat, params->sdat, sdat_bytes);
  479|       |
  480|  88.4k|                    image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
  481|  88.4k|                    if (image == NULL) {
  ------------------
  |  Branch (481:25): [True: 71, False: 88.4k]
  ------------------
  482|     71|                        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate image");
  483|     71|                        goto cleanup;
  484|     71|                    }
  485|       |
  486|  88.4k|                    code = jbig2_decode_generic_region(ctx, segment, &region_params, as, image, GB_stats);
  487|  88.4k|                    if (code < 0) {
  ------------------
  |  Branch (487:25): [True: 24, False: 88.3k]
  ------------------
  488|     24|                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode generic region");
  489|     24|                        goto cleanup;
  490|     24|                    }
  491|       |
  492|  88.3k|                    SDNEWSYMS->glyphs[NSYMSDECODED] = image;
  493|  88.3k|                    image = NULL;
  494|  88.3k|                } else {
  495|       |                    /* 6.5.8.2 refinement/aggregate symbol */
  496|  9.09k|                    uint32_t REFAGGNINST;
  497|       |
  498|  9.09k|                    if (params->SDHUFF) {
  ------------------
  |  Branch (498:25): [True: 3.11k, False: 5.97k]
  ------------------
  499|  3.11k|                        REFAGGNINST = jbig2_huffman_get(hs, params->SDHUFFAGGINST, &code);
  500|  5.97k|                    } else {
  501|  5.97k|                        code = jbig2_arith_int_decode(ctx, IAAI, as, (int32_t *) &REFAGGNINST);
  502|  5.97k|                    }
  503|  9.09k|                    if (code < 0) {
  ------------------
  |  Branch (503:25): [True: 1, False: 9.09k]
  ------------------
  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|  9.09k|                    if (code > 0) {
  ------------------
  |  Branch (507:25): [True: 2, False: 9.08k]
  ------------------
  508|      2|                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB in number of symbols in aggregate glyph");
  509|      2|                        goto cleanup;
  510|      2|                    }
  511|  9.08k|                    if ((int32_t) REFAGGNINST <= 0) {
  ------------------
  |  Branch (511:25): [True: 74, False: 9.01k]
  ------------------
  512|     74|                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid number of symbols in aggregate glyph");
  513|     74|                        goto cleanup;
  514|     74|                    }
  515|       |
  516|  9.01k|                    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "aggregate symbol coding (%d instances)", REFAGGNINST);
  517|       |
  518|  9.01k|                    if (REFAGGNINST > 1) {
  ------------------
  |  Branch (518:25): [True: 7.60k, False: 1.41k]
  ------------------
  519|  7.60k|                        tparams.SBNUMINSTANCES = REFAGGNINST;
  520|       |
  521|  7.60k|                        image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
  522|  7.60k|                        if (image == NULL) {
  ------------------
  |  Branch (522:29): [True: 14, False: 7.58k]
  ------------------
  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|  7.58k|                        code = jbig2_decode_text_region(ctx, segment, &tparams, (const Jbig2SymbolDict * const *)refagg_dicts,
  529|  7.58k|                                                        2, image, GR_stats, as, ws);
  530|  7.58k|                        if (code < 0) {
  ------------------
  |  Branch (530:29): [True: 280, False: 7.30k]
  ------------------
  531|    280|                            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode text region");
  532|    280|                            goto cleanup;
  533|    280|                        }
  534|       |
  535|  7.30k|                        SDNEWSYMS->glyphs[NSYMSDECODED] = image;
  536|  7.30k|                        image = NULL;
  537|  7.30k|                    } else {
  538|       |                        /* 6.5.8.2.2 */
  539|       |                        /* bool SBHUFF = params->SDHUFF; */
  540|  1.41k|                        Jbig2RefinementRegionParams rparams;
  541|  1.41k|                        uint32_t ID;
  542|  1.41k|                        int32_t RDX, RDY;
  543|  1.41k|                        size_t BMSIZE = 0;
  544|  1.41k|                        uint32_t ninsyms = params->SDNUMINSYMS;
  545|  1.41k|                        int code1 = 0;
  546|  1.41k|                        int code2 = 0;
  547|  1.41k|                        int code3 = 0;
  548|  1.41k|                        int code4 = 0;
  549|  1.41k|                        int code5 = 0;
  550|       |
  551|       |                        /* 6.5.8.2.2 (2, 3, 4, 5) */
  552|  1.41k|                        if (params->SDHUFF) {
  ------------------
  |  Branch (552:29): [True: 912, False: 501]
  ------------------
  553|    912|                            ID = jbig2_huffman_get_bits(hs, SBSYMCODELEN, &code1);
  554|    912|                            RDX = jbig2_huffman_get(hs, tparams.SBHUFFRDX, &code2);
  555|    912|                            RDY = jbig2_huffman_get(hs, tparams.SBHUFFRDY, &code3);
  556|    912|                            BMSIZE = jbig2_huffman_get(hs, tparams.SBHUFFRSIZE, &code4);
  557|    912|                            code5 = jbig2_huffman_skip(hs);
  558|    912|                        } else {
  559|    501|                            code1 = jbig2_arith_iaid_decode(ctx, tparams.IAID, as, (int32_t *) &ID);
  560|    501|                            code2 = jbig2_arith_int_decode(ctx, tparams.IARDX, as, &RDX);
  561|    501|                            code3 = jbig2_arith_int_decode(ctx, tparams.IARDY, as, &RDY);
  562|    501|                        }
  563|       |
  564|  1.41k|                        if (code1 < 0 || code2 < 0 || code3 < 0 || code4 < 0 || code5 < 0) {
  ------------------
  |  Branch (564:29): [True: 0, False: 1.41k]
  |  Branch (564:42): [True: 1, False: 1.41k]
  |  Branch (564:55): [True: 0, False: 1.41k]
  |  Branch (564:68): [True: 0, False: 1.41k]
  |  Branch (564:81): [True: 0, False: 1.41k]
  ------------------
  565|      1|                            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode data");
  566|      1|                            goto cleanup;
  567|      1|                        }
  568|  1.41k|                        if (code1 > 0 || code2 > 0 || code3 > 0 || code4 > 0 || code5 > 0) {
  ------------------
  |  Branch (568:29): [True: 0, False: 1.41k]
  |  Branch (568:42): [True: 1, False: 1.41k]
  |  Branch (568:55): [True: 3, False: 1.40k]
  |  Branch (568:68): [True: 0, False: 1.40k]
  |  Branch (568:81): [True: 0, False: 1.40k]
  ------------------
  569|      4|                            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB in single refinement/aggregate coded symbol data");
  570|      4|                            goto cleanup;
  571|      4|                        }
  572|       |
  573|  1.40k|                        if (ID >= ninsyms + NSYMSDECODED) {
  ------------------
  |  Branch (573:29): [True: 26, False: 1.38k]
  ------------------
  574|     26|                            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "refinement references unknown symbol %d", ID);
  575|     26|                            goto cleanup;
  576|     26|                        }
  577|       |
  578|  1.38k|                        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  579|  1.38k|                                    "symbol is a refinement of ID %d with the refinement applied at (%d,%d)", ID, RDX, RDY);
  580|       |
  581|  1.38k|                        image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
  582|  1.38k|                        if (image == NULL) {
  ------------------
  |  Branch (582:29): [True: 1, False: 1.38k]
  ------------------
  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|  1.38k|                        rparams.GRTEMPLATE = params->SDRTEMPLATE;
  589|  1.38k|                        rparams.GRREFERENCE = (ID < ninsyms) ? params->SDINSYMS->glyphs[ID] : SDNEWSYMS->glyphs[ID - ninsyms];
  ------------------
  |  Branch (589:47): [True: 418, False: 963]
  ------------------
  590|       |                        /* SumatraPDF: fail on missing glyphs */
  591|  1.38k|                        if (rparams.GRREFERENCE == NULL) {
  ------------------
  |  Branch (591:29): [True: 3, False: 1.37k]
  ------------------
  592|      3|                            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "missing glyph %d/%d", ID, ninsyms);
  593|      3|                            goto cleanup;
  594|      3|                        }
  595|  1.37k|                        rparams.GRREFERENCEDX = RDX;
  596|  1.37k|                        rparams.GRREFERENCEDY = RDY;
  597|  1.37k|                        rparams.TPGRON = 0;
  598|  1.37k|                        memcpy(rparams.grat, params->sdrat, 4);
  599|  1.37k|                        code = jbig2_decode_refinement_region(ctx, segment, &rparams, as, image, GR_stats);
  600|  1.37k|                        if (code < 0) {
  ------------------
  |  Branch (600:29): [True: 2, False: 1.37k]
  ------------------
  601|      2|                            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
  602|      2|                            goto cleanup;
  603|      2|                        }
  604|       |
  605|  1.37k|                        SDNEWSYMS->glyphs[NSYMSDECODED] = image;
  606|  1.37k|                        image = NULL;
  607|       |
  608|       |                        /* 6.5.8.2.2 (7) */
  609|  1.37k|                        if (params->SDHUFF) {
  ------------------
  |  Branch (609:29): [True: 888, False: 488]
  ------------------
  610|    888|                            if (BMSIZE == 0)
  ------------------
  |  Branch (610:33): [True: 439, False: 449]
  ------------------
  611|    439|                                BMSIZE = (size_t) SDNEWSYMS->glyphs[NSYMSDECODED]->height *
  612|    439|                                    SDNEWSYMS->glyphs[NSYMSDECODED]->stride;
  613|    888|                            code = jbig2_huffman_advance(hs, BMSIZE);
  614|    888|                            if (code < 0) {
  ------------------
  |  Branch (614:33): [True: 0, False: 888]
  ------------------
  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|    888|                        }
  619|  1.37k|                    }
  620|  9.01k|                }
  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|  97.5k|            }
  641|       |
  642|       |            /* 6.5.5 (4c.iii) */
  643|  23.1M|            if (params->SDHUFF && !params->SDREFAGG) {
  ------------------
  |  Branch (643:17): [True: 23.0M, False: 94.2k]
  |  Branch (643:35): [True: 23.0M, False: 2.79k]
  ------------------
  644|  23.0M|                SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
  645|  23.0M|            }
  646|       |
  647|       |            /* 6.5.5 (4c.iv) */
  648|  23.1M|            NSYMSDECODED = NSYMSDECODED + 1;
  649|       |
  650|  23.1M|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "decoded symbol %u of %u (%ux%u)", NSYMSDECODED, params->SDNUMNEWSYMS, SYMWIDTH, HCHEIGHT);
  651|       |
  652|  23.1M|        }                       /* end height class decode loop */
  653|       |
  654|       |        /* 6.5.5 (4d) */
  655|  1.51M|        if (params->SDHUFF && !params->SDREFAGG) {
  ------------------
  |  Branch (655:13): [True: 9.83k, False: 1.50M]
  |  Branch (655:31): [True: 9.52k, False: 311]
  ------------------
  656|       |            /* 6.5.9 */
  657|  9.52k|            size_t BMSIZE;
  658|  9.52k|            uint32_t j;
  659|  9.52k|            int x;
  660|       |
  661|  9.52k|            BMSIZE = jbig2_huffman_get(hs, params->SDHUFFBMSIZE, &code);
  662|  9.52k|            if (code < 0) {
  ------------------
  |  Branch (662:17): [True: 1, False: 9.52k]
  ------------------
  663|      1|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error decoding size of collective bitmap");
  664|      1|                goto cleanup;
  665|      1|            }
  666|  9.52k|            if (code > 0) {
  ------------------
  |  Branch (666:17): [True: 1, False: 9.52k]
  ------------------
  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|  9.52k|            code = jbig2_huffman_skip(hs);
  673|  9.52k|            if (code < 0) {
  ------------------
  |  Branch (673:17): [True: 0, False: 9.52k]
  ------------------
  674|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to skip to next byte when decoding collective bitmap");
  675|      0|            }
  676|       |
  677|  9.52k|            image = jbig2_image_new(ctx, TOTWIDTH, HCHEIGHT);
  678|  9.52k|            if (image == NULL) {
  ------------------
  |  Branch (678:17): [True: 48, False: 9.47k]
  ------------------
  679|     48|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate collective bitmap image");
  680|     48|                goto cleanup;
  681|     48|            }
  682|       |
  683|  9.47k|            if (BMSIZE == 0) {
  ------------------
  |  Branch (683:17): [True: 968, False: 8.50k]
  ------------------
  684|       |                /* if BMSIZE == 0 bitmap is uncompressed */
  685|    968|                const byte *src = data + jbig2_huffman_offset(hs);
  686|    968|                const int stride = (image->width >> 3) + ((image->width & 7) ? 1 : 0);
  ------------------
  |  Branch (686:59): [True: 890, False: 78]
  ------------------
  687|    968|                byte *dst = image->data;
  688|       |
  689|       |                /* SumatraPDF: prevent read access violation */
  690|    968|                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: 63, False: 905]
  |  Branch (690:56): [True: 14, False: 891]
  |  Branch (690:127): [True: 0, False: 891]
  ------------------
  691|     77|                    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "not enough data for decoding uncompressed (%d/%li)", image->height * stride,
  692|     77|                                (long) (size - jbig2_huffman_offset(hs)));
  693|     77|                    goto cleanup;
  694|     77|                }
  695|       |
  696|    891|                BMSIZE = (size_t) image->height * stride;
  697|    891|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  698|    891|                            "reading %dx%d uncompressed bitmap for %d symbols (%li bytes)", image->width, image->height, NSYMSDECODED - HCFIRSTSYM, (long) BMSIZE);
  699|       |
  700|  10.2k|                for (j = 0; j < image->height; j++) {
  ------------------
  |  Branch (700:29): [True: 9.32k, False: 891]
  ------------------
  701|  9.32k|                    memcpy(dst, src, stride);
  702|  9.32k|                    dst += image->stride;
  703|  9.32k|                    src += stride;
  704|  9.32k|                }
  705|  8.50k|            } else {
  706|  8.50k|                Jbig2GenericRegionParams rparams;
  707|       |
  708|       |                /* SumatraPDF: prevent read access violation */
  709|  8.50k|                if (size < jbig2_huffman_offset(hs) || size < BMSIZE || size - jbig2_huffman_offset(hs) < BMSIZE) {
  ------------------
  |  Branch (709:21): [True: 9, False: 8.50k]
  |  Branch (709:56): [True: 84, False: 8.41k]
  |  Branch (709:73): [True: 12, False: 8.40k]
  ------------------
  710|    105|                    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "not enough data for decoding (%li/%li)", (long) BMSIZE, (long) (size - jbig2_huffman_offset(hs)));
  711|    105|                    goto cleanup;
  712|    105|                }
  713|       |
  714|  8.40k|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  715|  8.40k|                            "reading %dx%d collective bitmap for %d symbols (%li bytes)", image->width, image->height, NSYMSDECODED - HCFIRSTSYM, (long) BMSIZE);
  716|       |
  717|  8.40k|                rparams.MMR = 1;
  718|  8.40k|                code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data + jbig2_huffman_offset(hs), BMSIZE, image);
  719|  8.40k|                if (code) {
  ------------------
  |  Branch (719:21): [True: 12, False: 8.39k]
  ------------------
  720|     12|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode MMR-coded generic region");
  721|     12|                    goto cleanup;
  722|     12|                }
  723|  8.40k|            }
  724|       |
  725|       |            /* advance past the data we've just read */
  726|  9.28k|            code = jbig2_huffman_advance(hs, BMSIZE);
  727|  9.28k|            if (code < 0) {
  ------------------
  |  Branch (727:17): [True: 0, False: 9.28k]
  ------------------
  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|  9.28k|            x = 0;
  734|   286k|            for (j = HCFIRSTSYM; j < NSYMSDECODED; j++) {
  ------------------
  |  Branch (734:34): [True: 277k, False: 9.26k]
  ------------------
  735|   277k|                glyph = jbig2_image_new(ctx, SDNEWSYMWIDTHS[j], HCHEIGHT);
  736|   277k|                if (glyph == NULL) {
  ------------------
  |  Branch (736:21): [True: 20, False: 277k]
  ------------------
  737|     20|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to copy the collective bitmap into symbol dictionary");
  738|     20|                    goto cleanup;
  739|     20|                }
  740|   277k|                code = jbig2_image_compose(ctx, glyph, image, -x, 0, JBIG2_COMPOSE_REPLACE);
  741|   277k|                if (code) {
  ------------------
  |  Branch (741:21): [True: 0, False: 277k]
  ------------------
  742|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to compose image into glyph");
  743|      0|                    goto cleanup;
  744|      0|                }
  745|   277k|                x += SDNEWSYMWIDTHS[j];
  746|   277k|                SDNEWSYMS->glyphs[j] = glyph;
  747|   277k|                glyph = NULL;
  748|   277k|            }
  749|  9.26k|            jbig2_image_release(ctx, image);
  750|  9.26k|            image = NULL;
  751|  9.26k|        }
  752|       |
  753|  1.51M|    }                           /* end of symbol decode loop */
  754|       |
  755|       |    /* 6.5.10 */
  756|  4.78k|    SDEXSYMS = jbig2_sd_new(ctx, params->SDNUMEXSYMS);
  757|  4.78k|    if (SDEXSYMS == NULL) {
  ------------------
  |  Branch (757:9): [True: 102, False: 4.68k]
  ------------------
  758|    102|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbols exported from symbols dictionary");
  759|    102|        goto cleanup;
  760|  4.68k|    } else {
  761|  4.68k|        uint32_t i = 0;
  762|  4.68k|        uint32_t j = 0;
  763|  4.68k|        uint32_t k;
  764|  4.68k|        int exflag = 0;
  765|  4.68k|        uint32_t limit = params->SDNUMINSYMS + params->SDNUMNEWSYMS;
  766|  4.68k|        uint32_t EXRUNLENGTH;
  767|       |
  768|  33.3k|        while (i < limit) {
  ------------------
  |  Branch (768:16): [True: 28.6k, False: 4.65k]
  ------------------
  769|  28.6k|            if (params->SDHUFF)
  ------------------
  |  Branch (769:17): [True: 14.6k, False: 14.0k]
  ------------------
  770|  14.6k|                EXRUNLENGTH = jbig2_huffman_get(hs, tparams.SBHUFFRSIZE, &code);
  771|  14.0k|            else
  772|  14.0k|                code = jbig2_arith_int_decode(ctx, IAEX, as, (int32_t *) &EXRUNLENGTH);
  773|  28.6k|            if (code < 0) {
  ------------------
  |  Branch (773:17): [True: 1, False: 28.6k]
  ------------------
  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|  28.6k|            if (code > 0) {
  ------------------
  |  Branch (780:17): [True: 12, False: 28.6k]
  ------------------
  781|     12|                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|     12|                jbig2_sd_release(ctx, SDEXSYMS);
  784|     12|                SDEXSYMS = NULL;
  785|     12|                break;
  786|     12|            }
  787|       |
  788|       |            /* prevent infinite list of empty runs, 1000 is just an arbitrary number */
  789|  28.6k|            if (EXRUNLENGTH <= 0 && ++emptyruns == 1000) {
  ------------------
  |  Branch (789:17): [True: 15.1k, False: 13.5k]
  |  Branch (789:37): [True: 12, False: 15.1k]
  ------------------
  790|     12|                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|     12|                jbig2_sd_release(ctx, SDEXSYMS);
  793|     12|                SDEXSYMS = NULL;
  794|     12|                break;
  795|  28.6k|            } else if (EXRUNLENGTH > 0) {
  ------------------
  |  Branch (795:24): [True: 13.5k, False: 15.1k]
  ------------------
  796|  13.5k|                emptyruns = 0;
  797|  13.5k|            }
  798|       |
  799|  28.6k|            if (EXRUNLENGTH > limit - i) {
  ------------------
  |  Branch (799:17): [True: 3.63k, False: 25.0k]
  ------------------
  800|  3.63k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "exporting more symbols than available (%u > %u), capping", i + EXRUNLENGTH, limit);
  801|  3.63k|                EXRUNLENGTH = limit - i;
  802|  3.63k|            }
  803|  28.6k|            if (exflag && j + EXRUNLENGTH > params->SDNUMEXSYMS) {
  ------------------
  |  Branch (803:17): [True: 13.4k, False: 15.2k]
  |  Branch (803:27): [True: 2.35k, False: 11.0k]
  ------------------
  804|  2.35k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "exporting more symbols than may be exported (%u > %u), capping", j + EXRUNLENGTH, params->SDNUMEXSYMS);
  805|  2.35k|                EXRUNLENGTH = params->SDNUMEXSYMS - j;
  806|  2.35k|            }
  807|       |
  808|  12.8M|            for (k = 0; k < EXRUNLENGTH; k++) {
  ------------------
  |  Branch (808:25): [True: 12.8M, False: 28.6k]
  ------------------
  809|  12.8M|                if (exflag) {
  ------------------
  |  Branch (809:21): [True: 635k, False: 12.2M]
  ------------------
  810|   635k|                    Jbig2Image *img;
  811|   635k|                    if (i < params->SDNUMINSYMS) {
  ------------------
  |  Branch (811:25): [True: 614k, False: 21.1k]
  ------------------
  812|   614k|                        img = params->SDINSYMS->glyphs[i];
  813|   614k|                    } else {
  814|  21.1k|                        img = SDNEWSYMS->glyphs[i - params->SDNUMINSYMS];
  815|  21.1k|                    }
  816|   635k|                    SDEXSYMS->glyphs[j++] = jbig2_image_reference(ctx, img);
  817|   635k|                }
  818|  12.8M|                i++;
  819|  12.8M|            }
  820|  28.6k|            exflag = !exflag;
  821|  28.6k|        }
  822|  4.68k|    }
  823|       |
  824|  5.90k|cleanup:
  825|  5.90k|    jbig2_image_release(ctx, glyph);
  826|  5.90k|    jbig2_image_release(ctx, image);
  827|  5.90k|    if (refagg_dicts != NULL) {
  ------------------
  |  Branch (827:9): [True: 5.81k, False: 90]
  ------------------
  828|  5.81k|        if (refagg_dicts[0] != NULL)
  ------------------
  |  Branch (828:13): [True: 5.80k, False: 3]
  ------------------
  829|  5.80k|            jbig2_sd_release(ctx, refagg_dicts[0]);
  830|       |        /* skip releasing refagg_dicts[1] as that is the same as SDNEWSYMS */
  831|  5.81k|        jbig2_free(ctx->allocator, refagg_dicts);
  832|  5.81k|    }
  833|  5.90k|    jbig2_sd_release(ctx, SDNEWSYMS);
  834|  5.90k|    if (params->SDHUFF) {
  ------------------
  |  Branch (834:9): [True: 3.25k, False: 2.64k]
  ------------------
  835|  3.25k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRSIZE);
  836|  3.25k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDY);
  837|  3.25k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDX);
  838|  3.25k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDH);
  839|  3.25k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDW);
  840|  3.25k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFDT);
  841|  3.25k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFDS);
  842|  3.25k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFFS);
  843|  3.25k|        if (!params->SDREFAGG) {
  ------------------
  |  Branch (843:13): [True: 2.63k, False: 618]
  ------------------
  844|  2.63k|            jbig2_free(ctx->allocator, SDNEWSYMWIDTHS);
  845|  2.63k|        }
  846|  3.25k|        jbig2_huffman_free(ctx, hs);
  847|  3.25k|    } else {
  848|  2.64k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDY);
  849|  2.64k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDX);
  850|  2.64k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDH);
  851|  2.64k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDW);
  852|  2.64k|        jbig2_arith_int_ctx_free(ctx, tparams.IARI);
  853|  2.64k|        jbig2_arith_iaid_ctx_free(ctx, tparams.IAID);
  854|  2.64k|        jbig2_arith_int_ctx_free(ctx, tparams.IAIT);
  855|  2.64k|        jbig2_arith_int_ctx_free(ctx, tparams.IADS);
  856|  2.64k|        jbig2_arith_int_ctx_free(ctx, tparams.IAFS);
  857|  2.64k|        jbig2_arith_int_ctx_free(ctx, tparams.IADT);
  858|  2.64k|        jbig2_arith_int_ctx_free(ctx, IAAI);
  859|  2.64k|        jbig2_arith_int_ctx_free(ctx, IAEX);
  860|  2.64k|        jbig2_arith_int_ctx_free(ctx, IADW);
  861|  2.64k|        jbig2_arith_int_ctx_free(ctx, IADH);
  862|  2.64k|    }
  863|  5.90k|    jbig2_free(ctx->allocator, as);
  864|  5.90k|    jbig2_word_stream_buf_free(ctx, ws);
  865|       |
  866|  5.90k|    return SDEXSYMS;
  867|  4.78k|}

jbig2_decode_text_region:
   62|  11.1k|{
   63|       |    /* relevant bits of 6.4.4 */
   64|  11.1k|    uint32_t NINSTANCES;
   65|  11.1k|    uint32_t ID;
   66|  11.1k|    int32_t STRIPT;
   67|  11.1k|    int32_t FIRSTS;
   68|  11.1k|    int32_t DT;
   69|  11.1k|    int32_t DFS;
   70|  11.1k|    int32_t IDS;
   71|  11.1k|    int32_t CURS;
   72|  11.1k|    int32_t CURT;
   73|  11.1k|    int S, T;
   74|  11.1k|    int x, y;
   75|  11.1k|    bool first_symbol;
  ------------------
  |  |   45|  11.1k|#define bool int
  ------------------
   76|  11.1k|    uint32_t index, SBNUMSYMS;
   77|  11.1k|    Jbig2Image *IB = NULL;
   78|  11.1k|    Jbig2Image *IBO = NULL;
   79|  11.1k|    Jbig2Image *refimage = NULL;
   80|  11.1k|    Jbig2HuffmanState *hs = NULL;
   81|  11.1k|    Jbig2HuffmanTable *SBSYMCODES = NULL;
   82|  11.1k|    int code = 0;
   83|  11.1k|    int RI;
   84|       |
   85|  11.1k|    SBNUMSYMS = 0;
   86|  36.1k|    for (index = 0; index < n_dicts; index++) {
  ------------------
  |  Branch (86:21): [True: 25.0k, False: 11.1k]
  ------------------
   87|  25.0k|        SBNUMSYMS += dicts[index]->n_symbols;
   88|  25.0k|    }
   89|  11.1k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "symbol list contains %d glyphs in %d dictionaries", SBNUMSYMS, n_dicts);
   90|       |
   91|  11.1k|    if (params->SBHUFF) {
  ------------------
  |  Branch (91:9): [True: 2.36k, False: 8.74k]
  ------------------
   92|  2.36k|        Jbig2HuffmanTable *runcodes = NULL;
   93|  2.36k|        Jbig2HuffmanParams runcodeparams;
   94|  2.36k|        Jbig2HuffmanLine runcodelengths[35];
   95|  2.36k|        Jbig2HuffmanLine *symcodelengths = NULL;
   96|  2.36k|        Jbig2HuffmanParams symcodeparams;
   97|  2.36k|        int err, len, range, r;
   98|       |
   99|  2.36k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "huffman coded text region");
  100|  2.36k|        hs = jbig2_huffman_new(ctx, ws);
  101|  2.36k|        if (hs == NULL)
  ------------------
  |  Branch (101:13): [True: 1, False: 2.36k]
  ------------------
  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|  85.2k|        for (index = 0; index < 35; index++) {
  ------------------
  |  Branch (109:25): [True: 82.8k, False: 2.36k]
  ------------------
  110|  82.8k|            runcodelengths[index].PREFLEN = jbig2_huffman_get_bits(hs, 4, &code);
  111|  82.8k|            if (code < 0) {
  ------------------
  |  Branch (111:17): [True: 0, False: 82.8k]
  ------------------
  112|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to read huffman runcode lengths");
  113|      0|                goto cleanup1;
  114|      0|            }
  115|  82.8k|            if (code > 0) {
  ------------------
  |  Branch (115:17): [True: 0, False: 82.8k]
  ------------------
  116|      0|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB decoding huffman runcode lengths");
  117|      0|                goto cleanup1;
  118|      0|            }
  119|  82.8k|            runcodelengths[index].RANGELEN = 0;
  120|  82.8k|            runcodelengths[index].RANGELOW = index;
  121|  82.8k|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "  read runcode%d length %d", index, runcodelengths[index].PREFLEN);
  122|  82.8k|        }
  123|  2.36k|        runcodeparams.HTOOB = 0;
  124|  2.36k|        runcodeparams.lines = runcodelengths;
  125|  2.36k|        runcodeparams.n_lines = 35;
  126|  2.36k|        runcodes = jbig2_build_huffman_table(ctx, &runcodeparams);
  127|  2.36k|        if (runcodes == NULL) {
  ------------------
  |  Branch (127:13): [True: 14, False: 2.35k]
  ------------------
  128|     14|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error constructing symbol ID runcode table");
  129|     14|            goto cleanup1;
  130|     14|        }
  131|       |
  132|       |        /* decode the symbol ID code lengths using the runlength table */
  133|  2.35k|        symcodelengths = jbig2_new(ctx, Jbig2HuffmanLine, SBNUMSYMS);
  ------------------
  |  |  141|  2.35k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  134|  2.35k|        if (symcodelengths == NULL) {
  ------------------
  |  Branch (134:13): [True: 3, False: 2.35k]
  ------------------
  135|      3|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate memory when reading symbol ID huffman table");
  136|      3|            goto cleanup1;
  137|      3|        }
  138|  2.35k|        index = 0;
  139|  14.6M|        while (index < SBNUMSYMS) {
  ------------------
  |  Branch (139:16): [True: 14.6M, False: 2.32k]
  ------------------
  140|  14.6M|            code = jbig2_huffman_get(hs, runcodes, &err);
  141|  14.6M|            if (err < 0) {
  ------------------
  |  Branch (141:17): [True: 21, False: 14.6M]
  ------------------
  142|     21|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error reading symbol ID huffman table");
  143|     21|                goto cleanup1;
  144|     21|            }
  145|  14.6M|            if (err > 0) {
  ------------------
  |  Branch (145:17): [True: 0, False: 14.6M]
  ------------------
  146|      0|                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB decoding symbol ID huffman table");
  147|      0|                goto cleanup1;
  148|      0|            }
  149|  14.6M|            if (code < 0 || code >= 35) {
  ------------------
  |  Branch (149:17): [True: 0, False: 14.6M]
  |  Branch (149:29): [True: 0, False: 14.6M]
  ------------------
  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|  14.6M|            if (code < 32) {
  ------------------
  |  Branch (154:17): [True: 14.3M, False: 363k]
  ------------------
  155|  14.3M|                len = code;
  156|  14.3M|                range = 1;
  157|  14.3M|            } else {
  158|   363k|                if (code == 32) {
  ------------------
  |  Branch (158:21): [True: 92.8k, False: 270k]
  ------------------
  159|  92.8k|                    if (index < 1) {
  ------------------
  |  Branch (159:25): [True: 2, False: 92.8k]
  ------------------
  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|  92.8k|                    len = symcodelengths[index - 1].PREFLEN;
  164|   270k|                } else {
  165|   270k|                    len = 0;    /* code == 33 or 34 */
  166|   270k|                }
  167|   363k|                err = 0;
  168|   363k|                if (code == 32)
  ------------------
  |  Branch (168:21): [True: 92.8k, False: 270k]
  ------------------
  169|  92.8k|                    range = jbig2_huffman_get_bits(hs, 2, &err) + 3;
  170|   270k|                else if (code == 33)
  ------------------
  |  Branch (170:26): [True: 43.5k, False: 227k]
  ------------------
  171|  43.5k|                    range = jbig2_huffman_get_bits(hs, 3, &err) + 3;
  172|   227k|                else if (code == 34)
  ------------------
  |  Branch (172:26): [True: 227k, False: 0]
  ------------------
  173|   227k|                    range = jbig2_huffman_get_bits(hs, 7, &err) + 11;
  174|   363k|                if (err < 0) {
  ------------------
  |  Branch (174:21): [True: 0, False: 363k]
  ------------------
  175|      0|                    code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to read huffman code");
  176|      0|                    goto cleanup1;
  177|      0|                }
  178|   363k|                if (err > 0) {
  ------------------
  |  Branch (178:21): [True: 0, False: 363k]
  ------------------
  179|      0|                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB decoding huffman code");
  180|      0|                    goto cleanup1;
  181|      0|                }
  182|   363k|            }
  183|  14.6M|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "  read runcode%d at index %d (length %d range %d)", code, index, len, range);
  184|  14.6M|            if (index + range > SBNUMSYMS) {
  ------------------
  |  Branch (184:17): [True: 1.81k, False: 14.6M]
  ------------------
  185|  1.81k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  186|  1.81k|                            "runlength extends %d entries beyond the end of symbol ID table", index + range - SBNUMSYMS);
  187|  1.81k|                range = SBNUMSYMS - index;
  188|  1.81k|            }
  189|  32.2M|            for (r = 0; r < range; r++) {
  ------------------
  |  Branch (189:25): [True: 17.6M, False: 14.6M]
  ------------------
  190|  17.6M|                symcodelengths[index + r].PREFLEN = len;
  191|  17.6M|                symcodelengths[index + r].RANGELEN = 0;
  192|  17.6M|                symcodelengths[index + r].RANGELOW = index + r;
  193|  17.6M|            }
  194|  14.6M|            index += r;
  195|  14.6M|        }
  196|       |
  197|  2.32k|        if (index < SBNUMSYMS) {
  ------------------
  |  Branch (197:13): [True: 0, False: 2.32k]
  ------------------
  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|  2.32k|        symcodeparams.HTOOB = 0;
  203|  2.32k|        symcodeparams.lines = symcodelengths;
  204|  2.32k|        symcodeparams.n_lines = SBNUMSYMS;
  205|       |
  206|       |        /* skip to byte boundary */
  207|  2.32k|        err = jbig2_huffman_skip(hs);
  208|  2.32k|        if (err < 0)
  ------------------
  |  Branch (208:13): [True: 0, False: 2.32k]
  ------------------
  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|  2.32k|        SBSYMCODES = jbig2_build_huffman_table(ctx, &symcodeparams);
  216|       |
  217|  2.36k|cleanup1:
  218|  2.36k|        jbig2_free(ctx->allocator, symcodelengths);
  219|  2.36k|        jbig2_release_huffman_table(ctx, runcodes);
  220|       |
  221|  2.36k|        if (SBSYMCODES == NULL) {
  ------------------
  |  Branch (221:13): [True: 95, False: 2.27k]
  ------------------
  222|     95|            jbig2_huffman_free(ctx, hs);
  223|     95|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to construct symbol ID huffman table");
  224|     95|        }
  225|  2.36k|    }
  226|       |
  227|       |    /* 6.4.5 (1) */
  228|  11.0k|    jbig2_image_clear(ctx, image, params->SBDEFPIXEL);
  229|       |
  230|       |    /* 6.4.6 */
  231|  11.0k|    if (params->SBHUFF) {
  ------------------
  |  Branch (231:9): [True: 2.27k, False: 8.74k]
  ------------------
  232|  2.27k|        STRIPT = jbig2_huffman_get(hs, params->SBHUFFDT, &code);
  233|  8.74k|    } else {
  234|  8.74k|        code = jbig2_arith_int_decode(ctx, params->IADT, as, &STRIPT);
  235|  8.74k|    }
  236|  11.0k|    if (code < 0) {
  ------------------
  |  Branch (236:9): [True: 4, False: 11.0k]
  ------------------
  237|      4|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode strip T");
  238|      4|        goto cleanup2;
  239|      4|    }
  240|  11.0k|    if (code > 0) {
  ------------------
  |  Branch (240:9): [True: 3, False: 11.0k]
  ------------------
  241|      3|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding strip T");
  242|      3|        goto cleanup2;
  243|      3|    }
  244|       |
  245|       |    /* 6.4.5 (2) */
  246|  11.0k|    STRIPT *= -(params->SBSTRIPS);
  247|  11.0k|    FIRSTS = 0;
  248|  11.0k|    NINSTANCES = 0;
  249|       |
  250|       |    /* 6.4.5 (3) */
  251|   413k|    while (NINSTANCES < params->SBNUMINSTANCES) {
  ------------------
  |  Branch (251:12): [True: 402k, False: 10.5k]
  ------------------
  252|       |        /* (3b) */
  253|   402k|        if (params->SBHUFF) {
  ------------------
  |  Branch (253:13): [True: 372k, False: 30.5k]
  ------------------
  254|   372k|            DT = jbig2_huffman_get(hs, params->SBHUFFDT, &code);
  255|   372k|        } else {
  256|  30.5k|            code = jbig2_arith_int_decode(ctx, params->IADT, as, &DT);
  257|  30.5k|        }
  258|   402k|        if (code < 0) {
  ------------------
  |  Branch (258:13): [True: 1, False: 402k]
  ------------------
  259|      1|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode delta T");
  260|      1|            goto cleanup2;
  261|      1|        }
  262|   402k|        if (code > 0) {
  ------------------
  |  Branch (262:13): [True: 32, False: 402k]
  ------------------
  263|     32|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding delta T");
  264|     32|            goto cleanup2;
  265|     32|        }
  266|   402k|        DT *= params->SBSTRIPS;
  267|   402k|        STRIPT += DT;
  268|       |
  269|   402k|        first_symbol = TRUE;
  ------------------
  |  |   53|   402k|#define TRUE 1
  ------------------
  270|       |        /* 6.4.5 (3c) - decode symbols in strip */
  271|  25.6M|        for (;;) {
  272|       |            /* (3c.i) */
  273|  25.6M|            if (first_symbol) {
  ------------------
  |  Branch (273:17): [True: 402k, False: 25.2M]
  ------------------
  274|       |                /* 6.4.7 */
  275|   402k|                if (params->SBHUFF) {
  ------------------
  |  Branch (275:21): [True: 372k, False: 30.5k]
  ------------------
  276|   372k|                    DFS = jbig2_huffman_get(hs, params->SBHUFFFS, &code);
  277|   372k|                } else {
  278|  30.5k|                    code = jbig2_arith_int_decode(ctx, params->IAFS, as, &DFS);
  279|  30.5k|                }
  280|   402k|                if (code < 0) {
  ------------------
  |  Branch (280:21): [True: 1, False: 402k]
  ------------------
  281|      1|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode strip symbol S-difference");
  282|      1|                    goto cleanup2;
  283|      1|                }
  284|   402k|                if (code > 0) {
  ------------------
  |  Branch (284:21): [True: 68, False: 402k]
  ------------------
  285|     68|                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding strip symbol S-difference");
  286|     68|                    goto cleanup2;
  287|     68|                }
  288|   402k|                FIRSTS += DFS;
  289|   402k|                CURS = FIRSTS;
  290|   402k|                first_symbol = FALSE;
  ------------------
  |  |   56|   402k|#define FALSE 0
  ------------------
  291|  25.2M|            } else {
  292|  25.2M|                if (NINSTANCES > params->SBNUMINSTANCES) {
  ------------------
  |  Branch (292:21): [True: 8.45k, False: 25.2M]
  ------------------
  293|  8.45k|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "too many NINSTANCES (%d) decoded", NINSTANCES);
  294|  8.45k|                    break;
  295|  8.45k|                }
  296|       |                /* (3c.ii) / 6.4.8 */
  297|  25.2M|                if (params->SBHUFF) {
  ------------------
  |  Branch (297:21): [True: 8.48M, False: 16.7M]
  ------------------
  298|  8.48M|                    IDS = jbig2_huffman_get(hs, params->SBHUFFDS, &code);
  299|  16.7M|                } else {
  300|  16.7M|                    code = jbig2_arith_int_decode(ctx, params->IADS, as, &IDS);
  301|  16.7M|                }
  302|  25.2M|                if (code < 0) {
  ------------------
  |  Branch (302:21): [True: 107, False: 25.2M]
  ------------------
  303|    107|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode symbol instance S coordinate");
  304|    107|                    goto cleanup2;
  305|    107|                }
  306|  25.2M|                if (code > 0) {
  ------------------
  |  Branch (306:21): [True: 394k, False: 24.8M]
  ------------------
  307|   394k|                    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|   394k|                    break;
  309|   394k|                }
  310|  24.8M|                CURS += IDS + params->SBDSOFFSET;
  311|  24.8M|            }
  312|       |
  313|       |            /* (3c.iii) / 6.4.9 */
  314|  25.2M|            if (params->SBSTRIPS == 1) {
  ------------------
  |  Branch (314:17): [True: 23.2M, False: 2.04M]
  ------------------
  315|  23.2M|                CURT = 0;
  316|  23.2M|            } else if (params->SBHUFF) {
  ------------------
  |  Branch (316:24): [True: 1.88M, False: 158k]
  ------------------
  317|  1.88M|                CURT = jbig2_huffman_get_bits(hs, params->LOGSBSTRIPS, &code);
  318|  1.88M|            } else {
  319|   158k|                code = jbig2_arith_int_decode(ctx, params->IAIT, as, &CURT);
  320|   158k|            }
  321|  25.2M|            if (code < 0) {
  ------------------
  |  Branch (321:17): [True: 0, False: 25.2M]
  ------------------
  322|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode symbol instance T coordinate");
  323|      0|                goto cleanup2;
  324|      0|            }
  325|  25.2M|            if (code > 0) {
  ------------------
  |  Branch (325:17): [True: 62, False: 25.2M]
  ------------------
  326|     62|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "OOB obtained when decoding symbol instance T coordinate");
  327|     62|                goto cleanup2;
  328|     62|            }
  329|  25.2M|            T = STRIPT + CURT;
  330|       |
  331|       |            /* (3b.iv) / 6.4.10 - decode the symbol ID */
  332|  25.2M|            if (params->SBHUFF) {
  ------------------
  |  Branch (332:17): [True: 8.49M, False: 16.7M]
  ------------------
  333|  8.49M|                ID = jbig2_huffman_get(hs, SBSYMCODES, &code);
  334|  16.7M|            } else {
  335|  16.7M|                code = jbig2_arith_iaid_decode(ctx, params->IAID, as, (int *)&ID);
  336|  16.7M|            }
  337|  25.2M|            if (code < 0) {
  ------------------
  |  Branch (337:17): [True: 36, False: 25.2M]
  ------------------
  338|     36|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to obtain symbol instance symbol ID");
  339|     36|                goto cleanup2;
  340|     36|            }
  341|  25.2M|            if (code > 0) {
  ------------------
  |  Branch (341:17): [True: 0, False: 25.2M]
  ------------------
  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|  25.2M|            if (ID >= SBNUMSYMS) {
  ------------------
  |  Branch (345:17): [True: 7.68M, False: 17.5M]
  ------------------
  346|  7.68M|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "ignoring out of range symbol ID (%d/%d)", ID, SBNUMSYMS);
  347|  7.68M|                IB = NULL;
  348|  17.5M|            } else {
  349|       |                /* (3c.v) / 6.4.11 - look up the symbol bitmap IB */
  350|  17.5M|                uint32_t id = ID;
  351|       |
  352|  17.5M|                index = 0;
  353|  22.1M|                while (id >= dicts[index]->n_symbols)
  ------------------
  |  Branch (353:24): [True: 4.55M, False: 17.5M]
  ------------------
  354|  4.55M|                    id -= dicts[index++]->n_symbols;
  355|  17.5M|                if (dicts[index]->glyphs[id] == NULL) {
  ------------------
  |  Branch (355:21): [True: 4.20M, False: 13.3M]
  ------------------
  356|  4.20M|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "missing glyph (%d/%d), ignoring", index, id);
  357|  13.3M|                } else {
  358|  13.3M|                    IB = jbig2_image_reference(ctx, dicts[index]->glyphs[id]);
  359|  13.3M|                }
  360|  17.5M|            }
  361|  25.2M|            if (params->SBREFINE) {
  ------------------
  |  Branch (361:17): [True: 4.55M, False: 20.7M]
  ------------------
  362|  4.55M|                if (params->SBHUFF) {
  ------------------
  |  Branch (362:21): [True: 4.05M, False: 504k]
  ------------------
  363|  4.05M|                    RI = jbig2_huffman_get_bits(hs, 1, &code);
  364|  4.05M|                } else {
  365|   504k|                    code = jbig2_arith_int_decode(ctx, params->IARI, as, &RI);
  366|   504k|                }
  367|  4.55M|                if (code < 0) {
  ------------------
  |  Branch (367:21): [True: 0, False: 4.55M]
  ------------------
  368|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode symbol bitmap refinement indicator");
  369|      0|                    goto cleanup2;
  370|      0|                }
  371|  4.55M|                if (code > 0) {
  ------------------
  |  Branch (371:21): [True: 17, False: 4.55M]
  ------------------
  372|     17|                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding symbol bitmap refinement indicator");
  373|     17|                    goto cleanup2;
  374|     17|                }
  375|  20.7M|            } else {
  376|  20.7M|                RI = 0;
  377|  20.7M|            }
  378|  25.2M|            if (RI) {
  ------------------
  |  Branch (378:17): [True: 502k, False: 24.7M]
  ------------------
  379|   502k|                Jbig2RefinementRegionParams rparams;
  380|   502k|                int32_t RDW, RDH, RDX, RDY;
  381|   502k|                size_t BMSIZE = 0;
  382|   502k|                int code1 = 0;
  383|   502k|                int code2 = 0;
  384|   502k|                int code3 = 0;
  385|   502k|                int code4 = 0;
  386|   502k|                int code5 = 0;
  387|   502k|                int code6 = 0;
  388|       |
  389|       |                /* 6.4.11 (1, 2, 3, 4) */
  390|   502k|                if (!params->SBHUFF) {
  ------------------
  |  Branch (390:21): [True: 492k, False: 10.6k]
  ------------------
  391|   492k|                    code1 = jbig2_arith_int_decode(ctx, params->IARDW, as, &RDW);
  392|   492k|                    code2 = jbig2_arith_int_decode(ctx, params->IARDH, as, &RDH);
  393|   492k|                    code3 = jbig2_arith_int_decode(ctx, params->IARDX, as, &RDX);
  394|   492k|                    code4 = jbig2_arith_int_decode(ctx, params->IARDY, as, &RDY);
  395|   492k|                } else {
  396|  10.6k|                    RDW = jbig2_huffman_get(hs, params->SBHUFFRDW, &code1);
  397|  10.6k|                    RDH = jbig2_huffman_get(hs, params->SBHUFFRDH, &code2);
  398|  10.6k|                    RDX = jbig2_huffman_get(hs, params->SBHUFFRDX, &code3);
  399|  10.6k|                    RDY = jbig2_huffman_get(hs, params->SBHUFFRDY, &code4);
  400|  10.6k|                    BMSIZE = jbig2_huffman_get(hs, params->SBHUFFRSIZE, &code5);
  401|  10.6k|                    code6 = jbig2_huffman_skip(hs);
  402|  10.6k|                }
  403|       |
  404|   502k|                if (code1 < 0 || code2 < 0 || code3 < 0 || code4 < 0 || code5 < 0 || code6 < 0) {
  ------------------
  |  Branch (404:21): [True: 3, False: 502k]
  |  Branch (404:34): [True: 1, False: 502k]
  |  Branch (404:47): [True: 1, False: 502k]
  |  Branch (404:60): [True: 1, False: 502k]
  |  Branch (404:73): [True: 1, False: 502k]
  |  Branch (404:86): [True: 0, False: 502k]
  ------------------
  405|      7|                    code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode data");
  406|      7|                    goto cleanup2;
  407|      7|                }
  408|   502k|                if (code1 > 0 || code2 > 0 || code3 > 0 || code4 > 0 || code5 > 0 || code6 > 0) {
  ------------------
  |  Branch (408:21): [True: 9, False: 502k]
  |  Branch (408:34): [True: 14, False: 502k]
  |  Branch (408:47): [True: 13, False: 502k]
  |  Branch (408:60): [True: 8, False: 502k]
  |  Branch (408:73): [True: 0, False: 502k]
  |  Branch (408:86): [True: 0, False: 502k]
  ------------------
  409|     44|                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding symbol instance refinement data");
  410|     44|                    goto cleanup2;
  411|     44|                }
  412|       |
  413|       |                /* 6.4.11 (6) */
  414|   502k|                if (IB) {
  ------------------
  |  Branch (414:21): [True: 99.7k, False: 402k]
  ------------------
  415|  99.7k|                    IBO = IB;
  416|  99.7k|                    IB = NULL;
  417|  99.7k|                    if (((int32_t) IBO->width) + RDW < 0 || ((int32_t) IBO->height) + RDH < 0) {
  ------------------
  |  Branch (417:25): [True: 18, False: 99.7k]
  |  Branch (417:61): [True: 9, False: 99.7k]
  ------------------
  418|     27|                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "reference image dimensions negative");
  419|     27|                        goto cleanup2;
  420|     27|                    }
  421|  99.7k|                    refimage = jbig2_image_new(ctx, IBO->width + RDW, IBO->height + RDH);
  422|  99.7k|                    if (refimage == NULL) {
  ------------------
  |  Branch (422:25): [True: 11, False: 99.7k]
  ------------------
  423|     11|                        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate reference image");
  424|     11|                        goto cleanup2;
  425|     11|                    }
  426|  99.7k|                    jbig2_image_clear(ctx, refimage, 0x00);
  427|       |
  428|       |                    /* Table 12 */
  429|  99.7k|                    rparams.GRTEMPLATE = params->SBRTEMPLATE;
  430|  99.7k|                    rparams.GRREFERENCE = IBO;
  431|  99.7k|                    rparams.GRREFERENCEDX = (RDW >> 1) + RDX;
  432|  99.7k|                    rparams.GRREFERENCEDY = (RDH >> 1) + RDY;
  433|  99.7k|                    rparams.TPGRON = 0;
  434|  99.7k|                    memcpy(rparams.grat, params->sbrat, 4);
  435|  99.7k|                    code = jbig2_decode_refinement_region(ctx, segment, &rparams, as, refimage, GR_stats);
  436|  99.7k|                    if (code < 0) {
  ------------------
  |  Branch (436:25): [True: 5, False: 99.6k]
  ------------------
  437|      5|                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
  438|      5|                        goto cleanup2;
  439|      5|                    }
  440|       |
  441|  99.6k|                    jbig2_image_release(ctx, IBO);
  442|  99.6k|                    IBO = NULL;
  443|  99.6k|                    IB = refimage;
  444|  99.6k|                    refimage = NULL;
  445|  99.6k|                }
  446|       |
  447|       |                /* 6.4.11 (7) */
  448|   502k|                if (params->SBHUFF) {
  ------------------
  |  Branch (448:21): [True: 10.5k, False: 491k]
  ------------------
  449|  10.5k|                    code = jbig2_huffman_advance(hs, BMSIZE);
  450|  10.5k|                    if (code < 0) {
  ------------------
  |  Branch (450:25): [True: 0, False: 10.5k]
  ------------------
  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|  10.5k|                }
  455|   502k|            }
  456|       |
  457|       |            /* (3c.vi) */
  458|  25.2M|            if ((!params->TRANSPOSED) && (params->REFCORNER > 1) && IB) {
  ------------------
  |  Branch (458:17): [True: 24.3M, False: 895k]
  |  Branch (458:42): [True: 2.47M, False: 21.8M]
  |  Branch (458:69): [True: 8.67k, False: 2.47M]
  ------------------
  459|  8.67k|                CURS += IB->width - 1;
  460|  25.2M|            } else if ((params->TRANSPOSED) && !(params->REFCORNER & 1) && IB) {
  ------------------
  |  Branch (460:24): [True: 895k, False: 24.3M]
  |  Branch (460:48): [True: 80.0k, False: 815k]
  |  Branch (460:76): [True: 7.41k, False: 72.6k]
  ------------------
  461|  7.41k|                CURS += IB->height - 1;
  462|  7.41k|            }
  463|       |
  464|       |            /* (3c.vii) */
  465|  25.2M|            S = CURS;
  466|       |
  467|       |            /* (3c.viii) */
  468|  25.2M|            if (!params->TRANSPOSED) {
  ------------------
  |  Branch (468:17): [True: 24.3M, False: 895k]
  ------------------
  469|  24.3M|                switch (params->REFCORNER) {
  470|  4.81M|                case JBIG2_CORNER_TOPLEFT:
  ------------------
  |  Branch (470:17): [True: 4.81M, False: 19.5M]
  ------------------
  471|  4.81M|                    x = S;
  472|  4.81M|                    y = T;
  473|  4.81M|                    break;
  474|  2.43M|                case JBIG2_CORNER_TOPRIGHT:
  ------------------
  |  Branch (474:17): [True: 2.43M, False: 21.9M]
  ------------------
  475|  2.43M|                    if (IB)
  ------------------
  |  Branch (475:25): [True: 4.65k, False: 2.43M]
  ------------------
  476|  4.65k|                        x = S - IB->width + 1;
  477|  2.43M|                    else
  478|  2.43M|                        x = S + 1;
  479|  2.43M|                    y = T;
  480|  2.43M|                    break;
  481|  17.0M|                case JBIG2_CORNER_BOTTOMLEFT:
  ------------------
  |  Branch (481:17): [True: 17.0M, False: 7.29M]
  ------------------
  482|  17.0M|                    x = S;
  483|  17.0M|                    if (IB)
  ------------------
  |  Branch (483:25): [True: 12.2M, False: 4.84M]
  ------------------
  484|  12.2M|                        y = T - IB->height + 1;
  485|  4.84M|                    else
  486|  4.84M|                        y = T + 1;
  487|  17.0M|                    break;
  488|      0|                default:
  ------------------
  |  Branch (488:17): [True: 0, False: 24.3M]
  ------------------
  489|  43.9k|                case JBIG2_CORNER_BOTTOMRIGHT:
  ------------------
  |  Branch (489:17): [True: 43.9k, False: 24.3M]
  ------------------
  490|  43.9k|                    if (IB ) {
  ------------------
  |  Branch (490:25): [True: 4.02k, False: 39.9k]
  ------------------
  491|  4.02k|                        x = S - IB->width + 1;
  492|  4.02k|                        y = T - IB->height + 1;
  493|  39.9k|                    } else {
  494|  39.9k|                        x = S + 1;
  495|  39.9k|                        y = T + 1;
  496|  39.9k|                    }
  497|  43.9k|                    break;
  498|  24.3M|                }
  499|  24.3M|            } else {            /* TRANSPOSED */
  500|   895k|                switch (params->REFCORNER) {
  501|   736k|                case JBIG2_CORNER_TOPLEFT:
  ------------------
  |  Branch (501:17): [True: 736k, False: 159k]
  ------------------
  502|   736k|                    x = T;
  503|   736k|                    y = S;
  504|   736k|                    break;
  505|  79.4k|                case JBIG2_CORNER_TOPRIGHT:
  ------------------
  |  Branch (505:17): [True: 79.4k, False: 816k]
  ------------------
  506|  79.4k|                    if (IB)
  ------------------
  |  Branch (506:25): [True: 74.8k, False: 4.55k]
  ------------------
  507|  74.8k|                        x = T - IB->width + 1;
  508|  4.55k|                    else
  509|  4.55k|                        x = T + 1;
  510|  79.4k|                    y = S;
  511|  79.4k|                    break;
  512|  9.86k|                case JBIG2_CORNER_BOTTOMLEFT:
  ------------------
  |  Branch (512:17): [True: 9.86k, False: 886k]
  ------------------
  513|  9.86k|                    x = T;
  514|  9.86k|                    if (IB)
  ------------------
  |  Branch (514:25): [True: 5.93k, False: 3.92k]
  ------------------
  515|  5.93k|                        y = S - IB->height + 1;
  516|  3.92k|                    else
  517|  3.92k|                        y = S + 1;
  518|  9.86k|                    break;
  519|      0|                default:
  ------------------
  |  Branch (519:17): [True: 0, False: 895k]
  ------------------
  520|  70.1k|                case JBIG2_CORNER_BOTTOMRIGHT:
  ------------------
  |  Branch (520:17): [True: 70.1k, False: 825k]
  ------------------
  521|  70.1k|                    if (IB) {
  ------------------
  |  Branch (521:25): [True: 1.47k, False: 68.6k]
  ------------------
  522|  1.47k|                        x = T - IB->width + 1;
  523|  1.47k|                        y = S - IB->height + 1;
  524|  68.6k|                    } else {
  525|  68.6k|                        x = T + 1;
  526|  68.6k|                        y = S + 1;
  527|  68.6k|                    }
  528|  70.1k|                    break;
  529|   895k|                }
  530|   895k|            }
  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|  25.2M|            code = jbig2_image_compose(ctx, image, IB, x, y, params->SBCOMBOP);
  538|  25.2M|            if (code < 0) {
  ------------------
  |  Branch (538:17): [True: 0, False: 25.2M]
  ------------------
  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|  25.2M|            if (IB && (!params->TRANSPOSED) && (params->REFCORNER < 2)) {
  ------------------
  |  Branch (544:17): [True: 13.3M, False: 11.8M]
  |  Branch (544:23): [True: 12.5M, False: 810k]
  |  Branch (544:48): [True: 12.5M, False: 8.67k]
  ------------------
  545|  12.5M|                CURS += IB->width - 1;
  546|  12.7M|            } else if (IB && (params->TRANSPOSED) && (params->REFCORNER & 1)) {
  ------------------
  |  Branch (546:24): [True: 818k, False: 11.8M]
  |  Branch (546:30): [True: 810k, False: 8.67k]
  |  Branch (546:54): [True: 802k, False: 7.41k]
  ------------------
  547|   802k|                CURS += IB->height - 1;
  548|   802k|            }
  549|       |
  550|       |            /* (3c.xi) */
  551|  25.2M|            NINSTANCES++;
  552|       |
  553|  25.2M|            jbig2_image_release(ctx, IB);
  554|  25.2M|            IB = NULL;
  555|  25.2M|        }
  556|       |        /* end strip */
  557|   402k|    }
  558|       |    /* 6.4.5 (4) */
  559|       |
  560|  11.0k|cleanup2:
  561|  11.0k|    jbig2_image_release(ctx, refimage);
  562|  11.0k|    jbig2_image_release(ctx, IBO);
  563|  11.0k|    jbig2_image_release(ctx, IB);
  564|  11.0k|    if (params->SBHUFF) {
  ------------------
  |  Branch (564:9): [True: 2.27k, False: 8.74k]
  ------------------
  565|  2.27k|        jbig2_release_huffman_table(ctx, SBSYMCODES);
  566|  2.27k|    }
  567|  11.0k|    jbig2_huffman_free(ctx, hs);
  568|       |
  569|  11.0k|    return code;
  570|  11.0k|}
jbig2_text_region:
  577|  3.66k|{
  578|  3.66k|    uint32_t offset = 0;
  579|  3.66k|    Jbig2RegionSegmentInfo region_info;
  580|  3.66k|    Jbig2TextRegionParams params;
  581|  3.66k|    Jbig2Image *image = NULL;
  582|  3.66k|    Jbig2SymbolDict **dicts = NULL;
  583|  3.66k|    uint32_t n_dicts = 0;
  584|  3.66k|    uint16_t flags = 0;
  585|  3.66k|    uint16_t huffman_flags = 0;
  586|  3.66k|    Jbig2ArithCx *GR_stats = NULL;
  587|  3.66k|    int code = 0;
  588|  3.66k|    Jbig2WordStream *ws = NULL;
  589|  3.66k|    Jbig2ArithState *as = NULL;
  590|  3.66k|    uint32_t table_index = 0;
  591|  3.66k|    const Jbig2HuffmanParams *huffman_params = NULL;
  592|       |
  593|       |    /* zero params to ease cleanup later */
  594|  3.66k|    memset(&params, 0, sizeof(Jbig2TextRegionParams));
  595|       |
  596|       |    /* 7.4.1 */
  597|  3.66k|    if (segment->data_length < 17) {
  ------------------
  |  Branch (597:9): [True: 12, False: 3.65k]
  ------------------
  598|     12|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  599|     12|        goto cleanup2;
  600|     12|    }
  601|  3.65k|    jbig2_get_region_segment_info(&region_info, segment_data);
  602|  3.65k|    offset += 17;
  603|       |    /* Check for T.88 amendment 3 */
  604|  3.65k|    if (region_info.flags & 8)
  ------------------
  |  Branch (604:9): [True: 5, False: 3.64k]
  ------------------
  605|      5|        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.64k|    if (segment->data_length - offset < 2) {
  ------------------
  |  Branch (608:9): [True: 3, False: 3.64k]
  ------------------
  609|      3|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  610|      3|        goto cleanup2;
  611|      3|    }
  612|  3.64k|    flags = jbig2_get_uint16(segment_data + offset);
  613|  3.64k|    offset += 2;
  614|       |
  615|  3.64k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "text region header flags 0x%04x", flags);
  616|       |
  617|  3.64k|    params.SBHUFF = flags & 0x0001;
  618|  3.64k|    params.SBREFINE = flags & 0x0002;
  619|  3.64k|    params.LOGSBSTRIPS = (flags & 0x000c) >> 2;
  620|  3.64k|    params.SBSTRIPS = 1 << params.LOGSBSTRIPS;
  621|  3.64k|    params.REFCORNER = (Jbig2RefCorner)((flags & 0x0030) >> 4);
  622|  3.64k|    params.TRANSPOSED = flags & 0x0040;
  623|  3.64k|    params.SBCOMBOP = (Jbig2ComposeOp)((flags & 0x0180) >> 7);
  624|  3.64k|    params.SBDEFPIXEL = flags & 0x0200;
  625|       |    /* SBDSOFFSET is a signed 5 bit integer */
  626|  3.64k|    params.SBDSOFFSET = (flags & 0x7C00) >> 10;
  627|  3.64k|    if (params.SBDSOFFSET > 0x0f)
  ------------------
  |  Branch (627:9): [True: 223, False: 3.42k]
  ------------------
  628|    223|        params.SBDSOFFSET -= 0x20;
  629|  3.64k|    params.SBRTEMPLATE = flags & 0x8000;
  630|       |
  631|  3.64k|    if (params.SBDSOFFSET) {
  ------------------
  |  Branch (631:9): [True: 2.61k, False: 1.02k]
  ------------------
  632|  2.61k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "text region has SBDSOFFSET %d", params.SBDSOFFSET);
  633|  2.61k|    }
  634|       |
  635|  3.64k|    if (params.SBHUFF) {        /* Huffman coding */
  ------------------
  |  Branch (635:9): [True: 295, False: 3.34k]
  ------------------
  636|       |        /* 7.4.3.1.2 */
  637|    295|        if (segment->data_length - offset < 2) {
  ------------------
  |  Branch (637:13): [True: 3, False: 292]
  ------------------
  638|      3|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  639|      3|            goto cleanup2;
  640|      3|        }
  641|    292|        huffman_flags = jbig2_get_uint16(segment_data + offset);
  642|    292|        offset += 2;
  643|       |
  644|    292|        if (huffman_flags & 0x8000)
  ------------------
  |  Branch (644:13): [True: 35, False: 257]
  ------------------
  645|     35|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved bit 15 of text region huffman flags is not zero");
  646|  3.34k|    } else {                    /* arithmetic coding */
  647|       |
  648|       |        /* 7.4.3.1.3 */
  649|  3.34k|        if (segment->data_length - offset < 4) {
  ------------------
  |  Branch (649:13): [True: 2, False: 3.34k]
  ------------------
  650|      2|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  651|      2|            goto cleanup2;
  652|      2|        }
  653|  3.34k|        if ((params.SBREFINE) && !(params.SBRTEMPLATE)) {
  ------------------
  |  Branch (653:13): [True: 182, False: 3.16k]
  |  Branch (653:34): [True: 80, False: 102]
  ------------------
  654|     80|            params.sbrat[0] = segment_data[offset];
  655|     80|            params.sbrat[1] = segment_data[offset + 1];
  656|     80|            params.sbrat[2] = segment_data[offset + 2];
  657|     80|            params.sbrat[3] = segment_data[offset + 3];
  658|     80|            offset += 4;
  659|     80|        }
  660|  3.34k|    }
  661|       |
  662|       |    /* 7.4.3.1.4 */
  663|  3.63k|    if (segment->data_length - offset < 4) {
  ------------------
  |  Branch (663:9): [True: 7, False: 3.63k]
  ------------------
  664|      7|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  665|      7|        goto cleanup2;
  666|      7|    }
  667|  3.63k|    params.SBNUMINSTANCES = jbig2_get_uint32(segment_data + offset);
  668|  3.63k|    offset += 4;
  669|       |
  670|  3.63k|    if (params.SBHUFF) {
  ------------------
  |  Branch (670:9): [True: 286, False: 3.34k]
  ------------------
  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|    286|        switch (huffman_flags & 0x0003) {
  676|    191|        case 0:                /* Table B.6 */
  ------------------
  |  Branch (676:9): [True: 191, False: 95]
  ------------------
  677|    191|            params.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_F);
  678|    191|            break;
  679|     69|        case 1:                /* Table B.7 */
  ------------------
  |  Branch (679:9): [True: 69, False: 217]
  ------------------
  680|     69|            params.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_G);
  681|     69|            break;
  682|     20|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (682:9): [True: 20, False: 266]
  ------------------
  683|     20|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  684|     20|            if (huffman_params == NULL) {
  ------------------
  |  Branch (684:17): [True: 7, False: 13]
  ------------------
  685|      7|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom FS huffman table not found (%d)", table_index);
  686|      7|                goto cleanup1;
  687|      7|            }
  688|     13|            params.SBHUFFFS = jbig2_build_huffman_table(ctx, huffman_params);
  689|     13|            ++table_index;
  690|     13|            break;
  691|      6|        case 2:                /* invalid */
  ------------------
  |  Branch (691:9): [True: 6, False: 280]
  ------------------
  692|      6|        default:
  ------------------
  |  Branch (692:9): [True: 0, False: 286]
  ------------------
  693|      6|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid FS huffman table");
  694|      6|            goto cleanup1;
  695|      0|            break;
  696|    286|        }
  697|    273|        if (params.SBHUFFFS == NULL) {
  ------------------
  |  Branch (697:13): [True: 1, False: 272]
  ------------------
  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|    272|        switch ((huffman_flags & 0x000c) >> 2) {
  ------------------
  |  Branch (702:17): [True: 272, False: 0]
  ------------------
  703|    162|        case 0:                /* Table B.8 */
  ------------------
  |  Branch (703:9): [True: 162, False: 110]
  ------------------
  704|    162|            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_H);
  705|    162|            break;
  706|     73|        case 1:                /* Table B.9 */
  ------------------
  |  Branch (706:9): [True: 73, False: 199]
  ------------------
  707|     73|            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_I);
  708|     73|            break;
  709|     18|        case 2:                /* Table B.10 */
  ------------------
  |  Branch (709:9): [True: 18, False: 254]
  ------------------
  710|     18|            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_J);
  711|     18|            break;
  712|     19|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (712:9): [True: 19, False: 253]
  ------------------
  713|     19|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  714|     19|            if (huffman_params == NULL) {
  ------------------
  |  Branch (714:17): [True: 3, False: 16]
  ------------------
  715|      3|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DS huffman table not found (%d)", table_index);
  716|      3|                goto cleanup1;
  717|      3|            }
  718|     16|            params.SBHUFFDS = jbig2_build_huffman_table(ctx, huffman_params);
  719|     16|            ++table_index;
  720|     16|            break;
  721|    272|        }
  722|    269|        if (params.SBHUFFDS == NULL) {
  ------------------
  |  Branch (722:13): [True: 3, False: 266]
  ------------------
  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|    266|        switch ((huffman_flags & 0x0030) >> 4) {
  ------------------
  |  Branch (727:17): [True: 266, False: 0]
  ------------------
  728|     49|        case 0:                /* Table B.11 */
  ------------------
  |  Branch (728:9): [True: 49, False: 217]
  ------------------
  729|     49|            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_K);
  730|     49|            break;
  731|    119|        case 1:                /* Table B.12 */
  ------------------
  |  Branch (731:9): [True: 119, False: 147]
  ------------------
  732|    119|            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_L);
  733|    119|            break;
  734|     81|        case 2:                /* Table B.13 */
  ------------------
  |  Branch (734:9): [True: 81, False: 185]
  ------------------
  735|     81|            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_M);
  736|     81|            break;
  737|     17|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (737:9): [True: 17, False: 249]
  ------------------
  738|     17|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  739|     17|            if (huffman_params == NULL) {
  ------------------
  |  Branch (739:17): [True: 2, False: 15]
  ------------------
  740|      2|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DT huffman table not found (%d)", table_index);
  741|      2|                goto cleanup1;
  742|      2|            }
  743|     15|            params.SBHUFFDT = jbig2_build_huffman_table(ctx, huffman_params);
  744|     15|            ++table_index;
  745|     15|            break;
  746|    266|        }
  747|    264|        if (params.SBHUFFDT == NULL) {
  ------------------
  |  Branch (747:13): [True: 1, False: 263]
  ------------------
  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|    263|        switch ((huffman_flags & 0x00c0) >> 6) {
  753|    213|        case 0:                /* Table B.14 */
  ------------------
  |  Branch (753:9): [True: 213, False: 50]
  ------------------
  754|    213|            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
  755|    213|            break;
  756|     18|        case 1:                /* Table B.15 */
  ------------------
  |  Branch (756:9): [True: 18, False: 245]
  ------------------
  757|     18|            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
  758|     18|            break;
  759|     31|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (759:9): [True: 31, False: 232]
  ------------------
  760|     31|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  761|     31|            if (huffman_params == NULL) {
  ------------------
  |  Branch (761:17): [True: 4, False: 27]
  ------------------
  762|      4|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RDW huffman table not found (%d)", table_index);
  763|      4|                goto cleanup1;
  764|      4|            }
  765|     27|            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, huffman_params);
  766|     27|            ++table_index;
  767|     27|            break;
  768|      1|        case 2:                /* invalid */
  ------------------
  |  Branch (768:9): [True: 1, False: 262]
  ------------------
  769|      1|        default:
  ------------------
  |  Branch (769:9): [True: 0, False: 263]
  ------------------
  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|    263|        }
  774|    258|        if (params.SBHUFFRDW == NULL) {
  ------------------
  |  Branch (774:13): [True: 2, False: 256]
  ------------------
  775|      2|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RDW huffman table");
  776|      2|            goto cleanup1;
  777|      2|        }
  778|       |
  779|    256|        switch ((huffman_flags & 0x0300) >> 8) {
  780|    120|        case 0:                /* Table B.14 */
  ------------------
  |  Branch (780:9): [True: 120, False: 136]
  ------------------
  781|    120|            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
  782|    120|            break;
  783|    128|        case 1:                /* Table B.15 */
  ------------------
  |  Branch (783:9): [True: 128, False: 128]
  ------------------
  784|    128|            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
  785|    128|            break;
  786|      6|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (786:9): [True: 6, False: 250]
  ------------------
  787|      6|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  788|      6|            if (huffman_params == NULL) {
  ------------------
  |  Branch (788:17): [True: 2, False: 4]
  ------------------
  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|      4|            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, huffman_params);
  793|      4|            ++table_index;
  794|      4|            break;
  795|      2|        case 2:                /* invalid */
  ------------------
  |  Branch (795:9): [True: 2, False: 254]
  ------------------
  796|      2|        default:
  ------------------
  |  Branch (796:9): [True: 0, False: 256]
  ------------------
  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|    256|        }
  801|    252|        if (params.SBHUFFRDH == NULL) {
  ------------------
  |  Branch (801:13): [True: 1, False: 251]
  ------------------
  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|    251|        switch ((huffman_flags & 0x0c00) >> 10) {
  807|    198|        case 0:                /* Table B.14 */
  ------------------
  |  Branch (807:9): [True: 198, False: 53]
  ------------------
  808|    198|            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
  809|    198|            break;
  810|     42|        case 1:                /* Table B.15 */
  ------------------
  |  Branch (810:9): [True: 42, False: 209]
  ------------------
  811|     42|            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
  812|     42|            break;
  813|      9|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (813:9): [True: 9, False: 242]
  ------------------
  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|      2|        case 2:                /* invalid */
  ------------------
  |  Branch (822:9): [True: 2, False: 249]
  ------------------
  823|      2|        default:
  ------------------
  |  Branch (823:9): [True: 0, False: 251]
  ------------------
  824|      2|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDX huffman table");
  825|      2|            goto cleanup1;
  826|      0|            break;
  827|    251|        }
  828|    246|        if (params.SBHUFFRDX == NULL) {
  ------------------
  |  Branch (828:13): [True: 1, False: 245]
  ------------------
  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|    245|        switch ((huffman_flags & 0x3000) >> 12) {
  834|    194|        case 0:                /* Table B.14 */
  ------------------
  |  Branch (834:9): [True: 194, False: 51]
  ------------------
  835|    194|            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
  836|    194|            break;
  837|     40|        case 1:                /* Table B.15 */
  ------------------
  |  Branch (837:9): [True: 40, False: 205]
  ------------------
  838|     40|            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
  839|     40|            break;
  840|     10|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (840:9): [True: 10, False: 235]
  ------------------
  841|     10|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  842|     10|            if (huffman_params == NULL) {
  ------------------
  |  Branch (842:17): [True: 3, False: 7]
  ------------------
  843|      3|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RDY huffman table not found (%d)", table_index);
  844|      3|                goto cleanup1;
  845|      3|            }
  846|      7|            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, huffman_params);
  847|      7|            ++table_index;
  848|      7|            break;
  849|      1|        case 2:                /* invalid */
  ------------------
  |  Branch (849:9): [True: 1, False: 244]
  ------------------
  850|      1|        default:
  ------------------
  |  Branch (850:9): [True: 0, False: 245]
  ------------------
  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|    245|        }
  855|    241|        if (params.SBHUFFRDY == NULL) {
  ------------------
  |  Branch (855:13): [True: 1, False: 240]
  ------------------
  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|    240|        switch ((huffman_flags & 0x4000) >> 14) {
  ------------------
  |  Branch (860:17): [True: 240, False: 0]
  ------------------
  861|    233|        case 0:                /* Table B.1 */
  ------------------
  |  Branch (861:9): [True: 233, False: 7]
  ------------------
  862|    233|            params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
  863|    233|            break;
  864|      7|        case 1:                /* Custom table from referred segment */
  ------------------
  |  Branch (864:9): [True: 7, False: 233]
  ------------------
  865|      7|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  866|      7|            if (huffman_params == NULL) {
  ------------------
  |  Branch (866:17): [True: 2, False: 5]
  ------------------
  867|      2|                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RSIZE huffman table not found (%d)", table_index);
  868|      2|                goto cleanup1;
  869|      2|            }
  870|      5|            params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, huffman_params);
  871|      5|            ++table_index;
  872|      5|            break;
  873|    240|        }
  874|    238|        if (params.SBHUFFRSIZE == NULL) {
  ------------------
  |  Branch (874:13): [True: 1, False: 237]
  ------------------
  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|    237|        if (huffman_flags & 0x8000) {
  ------------------
  |  Branch (879:13): [True: 4, False: 233]
  ------------------
  880|      4|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "text region huffman flags bit 15 is set, contrary to spec");
  881|      4|        }
  882|       |
  883|       |        /* 7.4.3.1.7 */
  884|       |        /* For convenience this is done in the body decoder routine */
  885|    237|    }
  886|       |
  887|  3.58k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  888|  3.58k|                "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.58k|    n_dicts = jbig2_sd_count_referred(ctx, segment);
  892|  3.58k|    if (n_dicts == 0) {
  ------------------
  |  Branch (892:9): [True: 714, False: 2.86k]
  ------------------
  893|    714|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "text region refers to no symbol dictionaries");
  894|  2.86k|    } else {
  895|  2.86k|        dicts = jbig2_sd_list_referred(ctx, segment);
  896|  2.86k|        if (dicts == NULL) {
  ------------------
  |  Branch (896:13): [True: 1, False: 2.86k]
  ------------------
  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.86k|        } else {
  900|  2.86k|            uint32_t index;
  901|       |
  902|  2.86k|            if (dicts[0] == NULL) {
  ------------------
  |  Branch (902:17): [True: 0, False: 2.86k]
  ------------------
  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|  10.4k|            for (index = 1; index < n_dicts; index++)
  ------------------
  |  Branch (906:29): [True: 7.53k, False: 2.86k]
  ------------------
  907|  7.53k|                if (dicts[index] == NULL) {
  ------------------
  |  Branch (907:21): [True: 0, False: 7.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.86k|        }
  912|  2.86k|    }
  913|       |
  914|       |    /* 7.4.3.2 (3) */
  915|  3.58k|    {
  916|  3.58k|        int stats_size = params.SBRTEMPLATE ? 1 << 10 : 1 << 13;
  ------------------
  |  Branch (916:26): [True: 239, False: 3.34k]
  ------------------
  917|       |
  918|  3.58k|        GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  3.58k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  919|  3.58k|        if (GR_stats == NULL) {
  ------------------
  |  Branch (919:13): [True: 1, False: 3.58k]
  ------------------
  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.58k|        memset(GR_stats, 0, stats_size);
  924|  3.58k|    }
  925|       |
  926|      0|    image = jbig2_image_new(ctx, region_info.width, region_info.height);
  927|  3.58k|    if (image == NULL) {
  ------------------
  |  Branch (927:9): [True: 31, False: 3.54k]
  ------------------
  928|     31|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region image");
  929|     31|        goto cleanup2;
  930|     31|    }
  931|       |
  932|  3.54k|    if (offset >= segment->data_length) {
  ------------------
  |  Branch (932:9): [True: 3, False: 3.54k]
  ------------------
  933|      3|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  934|      3|        goto cleanup2;
  935|      3|    }
  936|  3.54k|    ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
  937|  3.54k|    if (ws == NULL) {
  ------------------
  |  Branch (937:9): [True: 1, False: 3.54k]
  ------------------
  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.54k|    as = jbig2_arith_new(ctx, ws);
  943|  3.54k|    if (as == NULL) {
  ------------------
  |  Branch (943:9): [True: 1, False: 3.54k]
  ------------------
  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.54k|    if (!params.SBHUFF) {
  ------------------
  |  Branch (948:9): [True: 3.31k, False: 232]
  ------------------
  949|  3.31k|        uint8_t SBSYMCODELEN;
  950|  3.31k|        uint32_t index;
  951|  3.31k|        uint32_t SBNUMSYMS = 0;
  952|       |
  953|  12.8k|        for (index = 0; index < n_dicts; index++) {
  ------------------
  |  Branch (953:25): [True: 9.50k, False: 3.31k]
  ------------------
  954|  9.50k|            SBNUMSYMS += dicts[index]->n_symbols;
  955|  9.50k|        }
  956|       |
  957|  3.31k|        params.IADT = jbig2_arith_int_ctx_new(ctx);
  958|  3.31k|        params.IAFS = jbig2_arith_int_ctx_new(ctx);
  959|  3.31k|        params.IADS = jbig2_arith_int_ctx_new(ctx);
  960|  3.31k|        params.IAIT = jbig2_arith_int_ctx_new(ctx);
  961|  3.31k|        if (params.IADT == NULL || params.IAFS == NULL || params.IADS == NULL || params.IAIT == NULL) {
  ------------------
  |  Branch (961:13): [True: 1, False: 3.31k]
  |  Branch (961:36): [True: 1, False: 3.31k]
  |  Branch (961:59): [True: 1, False: 3.30k]
  |  Branch (961:82): [True: 4, False: 3.30k]
  ------------------
  962|      7|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region image data");
  963|      7|            goto cleanup4;
  964|      7|        }
  965|       |
  966|       |        /* Table 31 */
  967|  7.10k|        for (SBSYMCODELEN = 0; ((uint64_t) 1 << SBSYMCODELEN) < (uint64_t) SBNUMSYMS; SBSYMCODELEN++);
  ------------------
  |  Branch (967:32): [True: 3.80k, False: 3.30k]
  ------------------
  968|       |
  969|  3.30k|        params.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
  970|  3.30k|        params.IARI = jbig2_arith_int_ctx_new(ctx);
  971|  3.30k|        params.IARDW = jbig2_arith_int_ctx_new(ctx);
  972|  3.30k|        params.IARDH = jbig2_arith_int_ctx_new(ctx);
  973|  3.30k|        params.IARDX = jbig2_arith_int_ctx_new(ctx);
  974|  3.30k|        params.IARDY = jbig2_arith_int_ctx_new(ctx);
  975|  3.30k|        if (params.IAID == NULL || params.IARI == NULL ||
  ------------------
  |  Branch (975:13): [True: 3, False: 3.30k]
  |  Branch (975:36): [True: 2, False: 3.30k]
  ------------------
  976|  3.30k|            params.IARDW == NULL || params.IARDH == NULL || params.IARDX == NULL || params.IARDY == NULL) {
  ------------------
  |  Branch (976:13): [True: 2, False: 3.29k]
  |  Branch (976:37): [True: 1, False: 3.29k]
  |  Branch (976:61): [True: 1, False: 3.29k]
  |  Branch (976:85): [True: 1, False: 3.29k]
  ------------------
  977|     10|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region image data");
  978|     10|            goto cleanup5;
  979|     10|        }
  980|  3.30k|    }
  981|       |
  982|  3.52k|    code = jbig2_decode_text_region(ctx, segment, &params,
  983|  3.52k|                                    (const Jbig2SymbolDict * const *)dicts, n_dicts, image, GR_stats, as, ws);
  984|  3.52k|    if (code < 0) {
  ------------------
  |  Branch (984:9): [True: 241, False: 3.28k]
  ------------------
  985|    241|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode text region image data");
  986|    241|        goto cleanup5;
  987|    241|    }
  988|       |
  989|  3.28k|    if ((segment->flags & 63) == 4) {
  ------------------
  |  Branch (989:9): [True: 157, False: 3.12k]
  ------------------
  990|       |        /* we have an intermediate region here. save it for later */
  991|    157|        segment->result = jbig2_image_reference(ctx, image);
  992|  3.12k|    } else {
  993|       |        /* otherwise composite onto the page */
  994|  3.12k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  995|  3.12k|                    "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.12k|        code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, region_info.x, region_info.y, region_info.op);
  997|  3.12k|        if (code < 0)
  ------------------
  |  Branch (997:13): [True: 14, False: 3.11k]
  ------------------
  998|     14|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add text region to page");
  999|  3.12k|    }
 1000|       |
 1001|  3.53k|cleanup5:
 1002|  3.53k|    if (!params.SBHUFF) {
  ------------------
  |  Branch (1002:9): [True: 3.30k, False: 232]
  ------------------
 1003|  3.30k|        jbig2_arith_iaid_ctx_free(ctx, params.IAID);
 1004|  3.30k|        jbig2_arith_int_ctx_free(ctx, params.IARI);
 1005|  3.30k|        jbig2_arith_int_ctx_free(ctx, params.IARDW);
 1006|  3.30k|        jbig2_arith_int_ctx_free(ctx, params.IARDH);
 1007|  3.30k|        jbig2_arith_int_ctx_free(ctx, params.IARDX);
 1008|  3.30k|        jbig2_arith_int_ctx_free(ctx, params.IARDY);
 1009|  3.30k|    }
 1010|       |
 1011|  3.54k|cleanup4:
 1012|  3.54k|    if (!params.SBHUFF) {
  ------------------
  |  Branch (1012:9): [True: 3.31k, False: 232]
  ------------------
 1013|  3.31k|        jbig2_arith_int_ctx_free(ctx, params.IADT);
 1014|  3.31k|        jbig2_arith_int_ctx_free(ctx, params.IAFS);
 1015|  3.31k|        jbig2_arith_int_ctx_free(ctx, params.IADS);
 1016|  3.31k|        jbig2_arith_int_ctx_free(ctx, params.IAIT);
 1017|  3.31k|    }
 1018|       |
 1019|  3.54k|cleanup3:
 1020|  3.54k|    jbig2_free(ctx->allocator, as);
 1021|  3.54k|    jbig2_word_stream_buf_free(ctx, ws);
 1022|       |
 1023|  3.60k|cleanup2:
 1024|  3.60k|    jbig2_free(ctx->allocator, GR_stats);
 1025|  3.60k|    jbig2_image_release(ctx, image);
 1026|       |
 1027|  3.65k|cleanup1:
 1028|  3.65k|    if (params.SBHUFF) {
  ------------------
  |  Branch (1028:9): [True: 295, False: 3.36k]
  ------------------
 1029|    295|        jbig2_release_huffman_table(ctx, params.SBHUFFFS);
 1030|    295|        jbig2_release_huffman_table(ctx, params.SBHUFFDS);
 1031|    295|        jbig2_release_huffman_table(ctx, params.SBHUFFDT);
 1032|    295|        jbig2_release_huffman_table(ctx, params.SBHUFFRDX);
 1033|    295|        jbig2_release_huffman_table(ctx, params.SBHUFFRDY);
 1034|    295|        jbig2_release_huffman_table(ctx, params.SBHUFFRDW);
 1035|    295|        jbig2_release_huffman_table(ctx, params.SBHUFFRDH);
 1036|    295|        jbig2_release_huffman_table(ctx, params.SBHUFFRSIZE);
 1037|    295|    }
 1038|  3.65k|    jbig2_free(ctx->allocator, dicts);
 1039|       |
 1040|  3.65k|    return code;
 1041|  3.60k|}

