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

jbig2_alloc:
   66|  48.2M|{
   67|       |    /* Check for integer multiplication overflow when computing
   68|       |    the full size of the allocation. */
   69|  48.2M|    if (num > 0 && size > SIZE_MAX / num)
  ------------------
  |  Branch (69:9): [True: 48.2M, False: 5.79k]
  |  Branch (69:20): [True: 0, False: 48.2M]
  ------------------
   70|      0|        return NULL;
   71|  48.2M|    return allocator->alloc(allocator, size * num);
   72|  48.2M|}
jbig2_error:
   93|   170M|{
   94|   170M|    char buf[1024];
   95|   170M|    va_list ap;
   96|   170M|    int n;
   97|       |
   98|   170M|    va_start(ap, fmt);
   99|   170M|    n = vsnprintf(buf, sizeof(buf), fmt, ap);
  100|   170M|    va_end(ap);
  101|   170M|    if (n < 0 || n == sizeof(buf))
  ------------------
  |  Branch (101:9): [True: 0, False: 170M]
  |  Branch (101:18): [True: 0, False: 170M]
  ------------------
  102|      0|        ctx->error_callback(ctx->error_callback_data, "failed to generate error string", severity, segment_number);
  103|   170M|    else
  104|   170M|        ctx->error_callback(ctx->error_callback_data, buf, severity, segment_number);
  105|   170M|    return -1;
  106|   170M|}
jbig2_ctx_new_imp:
  110|  6.92k|{
  111|  6.92k|    Jbig2Ctx *result;
  112|       |
  113|  6.92k|    if (jbig2_version_major != JBIG2_VERSION_MAJOR || jbig2_version_minor != JBIG2_VERSION_MINOR) {
  ------------------
  |  |   28|  13.8k|#define JBIG2_VERSION_MAJOR (0)
  ------------------
                  if (jbig2_version_major != JBIG2_VERSION_MAJOR || jbig2_version_minor != JBIG2_VERSION_MINOR) {
  ------------------
  |  |   29|  6.92k|#define JBIG2_VERSION_MINOR (20)
  ------------------
  |  Branch (113:9): [True: 0, False: 6.92k]
  |  Branch (113:55): [True: 0, False: 6.92k]
  ------------------
  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.92k|    if (allocator == NULL)
  ------------------
  |  Branch (122:9): [True: 0, False: 6.92k]
  ------------------
  123|      0|        allocator = &jbig2_default_allocator;
  124|  6.92k|    if (error_callback == NULL)
  ------------------
  |  Branch (124:9): [True: 6.92k, False: 0]
  ------------------
  125|  6.92k|        error_callback = &jbig2_default_error;
  126|       |
  127|  6.92k|    result = (Jbig2Ctx *) jbig2_alloc(allocator, sizeof(Jbig2Ctx), 1);
  128|  6.92k|    if (result == NULL) {
  ------------------
  |  Branch (128:9): [True: 0, False: 6.92k]
  ------------------
  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.92k|    result->allocator = allocator;
  134|  6.92k|    result->options = options;
  135|  6.92k|    result->global_ctx = (const Jbig2Ctx *)global_ctx;
  136|  6.92k|    result->error_callback = error_callback;
  137|  6.92k|    result->error_callback_data = error_callback_data;
  138|       |
  139|  6.92k|    result->state = (options & JBIG2_OPTIONS_EMBEDDED) ? JBIG2_FILE_HEADER_MAYBE : JBIG2_FILE_HEADER;
  ------------------
  |  Branch (139:21): [True: 0, False: 6.92k]
  ------------------
  140|       |
  141|  6.92k|    result->buf = NULL;
  142|       |
  143|  6.92k|    result->n_segments = 0;
  144|  6.92k|    result->n_segments_max = 16;
  145|  6.92k|    result->segments = jbig2_new(result, Jbig2Segment *, result->n_segments_max);
  ------------------
  |  |  141|  6.92k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  146|  6.92k|    if (result->segments == NULL) {
  ------------------
  |  Branch (146:9): [True: 0, False: 6.92k]
  ------------------
  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.92k|    result->segment_index = 0;
  152|       |
  153|  6.92k|    result->current_page = 0;
  154|  6.92k|    result->max_page_index = 4;
  155|  6.92k|    result->pages = jbig2_new(result, Jbig2Page, result->max_page_index);
  ------------------
  |  |  141|  6.92k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  156|  6.92k|    if (result->pages == NULL) {
  ------------------
  |  Branch (156:9): [True: 0, False: 6.92k]
  ------------------
  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.92k|    {
  163|  6.92k|        uint32_t index;
  164|       |
  165|  34.6k|        for (index = 0; index < result->max_page_index; index++) {
  ------------------
  |  Branch (165:25): [True: 27.7k, False: 6.92k]
  ------------------
  166|  27.7k|            result->pages[index].state = JBIG2_PAGE_FREE;
  167|  27.7k|            result->pages[index].number = 0;
  168|  27.7k|            result->pages[index].width = 0;
  169|  27.7k|            result->pages[index].height = 0xffffffff;
  170|  27.7k|            result->pages[index].x_resolution = 0;
  171|  27.7k|            result->pages[index].y_resolution = 0;
  172|  27.7k|            result->pages[index].stripe_size = 0;
  173|  27.7k|            result->pages[index].striped = 0;
  174|  27.7k|            result->pages[index].end_row = 0;
  175|  27.7k|            result->pages[index].flags = 0;
  176|  27.7k|            result->pages[index].image = NULL;
  177|  27.7k|        }
  178|  6.92k|    }
  179|       |
  180|  6.92k|    return result;
  181|  6.92k|}
jbig2_get_int16:
  192|  9.03k|{
  193|  9.03k|    return get_int16(bptr);
  ------------------
  |  |  186|  9.03k|    (((int)get_uint16(bptr) ^ 0x8000) - 0x8000)
  |  |  ------------------
  |  |  |  |  184|  9.03k|    (((bptr)[0] << 8) | (bptr)[1])
  |  |  ------------------
  ------------------
  194|  9.03k|}
jbig2_get_uint16:
  200|  20.5k|{
  201|  20.5k|    return get_uint16(bptr);
  ------------------
  |  |  184|  20.5k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
  202|  20.5k|}
jbig2_get_int32:
  208|  7.48k|{
  209|  7.48k|    return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  186|  7.48k|    (((int)get_uint16(bptr) ^ 0x8000) - 0x8000)
  |  |  ------------------
  |  |  |  |  184|  7.48k|    (((bptr)[0] << 8) | (bptr)[1])
  |  |  ------------------
  ------------------
                  return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  184|  7.48k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
  210|  7.48k|}
jbig2_get_uint32:
  216|   403k|{
  217|   403k|    return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  184|   403k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
                  return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2);
  ------------------
  |  |  184|   403k|    (((bptr)[0] << 8) | (bptr)[1])
  ------------------
  218|   403k|}
jbig2_data_in:
  250|  6.92k|{
  251|  6.92k|    if (ctx->buf == NULL) {
  ------------------
  |  Branch (251:9): [True: 6.92k, False: 0]
  ------------------
  252|  6.92k|        size_t buf_size = jbig2_find_buffer_size(size);
  253|  6.92k|        ctx->buf = jbig2_new(ctx, byte, buf_size);
  ------------------
  |  |  141|  6.92k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  254|  6.92k|        if (ctx->buf == NULL) {
  ------------------
  |  Branch (254:13): [True: 0, False: 6.92k]
  ------------------
  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.92k|        ctx->buf_size = buf_size;
  258|  6.92k|        ctx->buf_rd_ix = 0;
  259|  6.92k|        ctx->buf_wr_ix = 0;
  260|  6.92k|    } 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.92k|    memcpy(ctx->buf + ctx->buf_wr_ix, data, size);
  289|  6.92k|    ctx->buf_wr_ix += size;
  290|       |
  291|       |    /* data has now been added to buffer */
  292|       |
  293|   140k|    for (;;) {
  294|   140k|        const byte jbig2_id_string[8] = { 0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a };
  295|   140k|        Jbig2Segment *segment;
  296|   140k|        size_t header_size;
  297|   140k|        int code;
  298|       |
  299|   140k|        switch (ctx->state) {
  ------------------
  |  Branch (299:17): [True: 140k, False: 0]
  ------------------
  300|      0|        case JBIG2_FILE_HEADER_MAYBE:
  ------------------
  |  Branch (300:9): [True: 0, False: 140k]
  ------------------
  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.92k|        case JBIG2_FILE_HEADER:
  ------------------
  |  Branch (312:9): [True: 6.92k, False: 133k]
  ------------------
  313|       |            /* D.4.1 */
  314|  6.92k|            if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9)
  ------------------
  |  Branch (314:17): [True: 6, False: 6.92k]
  ------------------
  315|      6|                return 0;
  316|  6.92k|            if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8))
  ------------------
  |  Branch (316:17): [True: 67, False: 6.85k]
  ------------------
  317|     67|                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "not a JBIG2 file header");
  ------------------
  |  |   68|     67|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  318|       |            /* D.4.2 */
  319|  6.85k|            ctx->file_header_flags = ctx->buf[ctx->buf_rd_ix + 8];
  320|       |            /* Check for T.88 amendment 2 */
  321|  6.85k|            if (ctx->file_header_flags & 0x04)
  ------------------
  |  Branch (321:17): [True: 1, False: 6.85k]
  ------------------
  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.85k|            if (ctx->file_header_flags & 0x08)
  ------------------
  |  Branch (324:17): [True: 1, False: 6.85k]
  ------------------
  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.85k|            if (ctx->file_header_flags & 0xFC) {
  ------------------
  |  Branch (326:17): [True: 1.58k, False: 5.26k]
  ------------------
  327|  1.58k|                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.58k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  328|  1.58k|            }
  329|       |            /* D.4.3 */
  330|  6.85k|            if (!(ctx->file_header_flags & 2)) {        /* number of pages is known */
  ------------------
  |  Branch (330:17): [True: 2.23k, False: 4.61k]
  ------------------
  331|  2.23k|                if (ctx->buf_wr_ix - ctx->buf_rd_ix < 13)
  ------------------
  |  Branch (331:21): [True: 6, False: 2.23k]
  ------------------
  332|      6|                    return 0;
  333|  2.23k|                ctx->n_pages = jbig2_get_uint32(ctx->buf + ctx->buf_rd_ix + 9);
  334|  2.23k|                ctx->buf_rd_ix += 13;
  335|  2.23k|                if (ctx->n_pages == 1)
  ------------------
  |  Branch (335:21): [True: 35, False: 2.19k]
  ------------------
  336|     35|                    jbig2_error(ctx, JBIG2_SEVERITY_INFO, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates a single page document");
  ------------------
  |  |   68|     35|#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|  4.61k|            } else {            /* number of pages not known */
  340|  4.61k|                ctx->n_pages = 0;
  341|  4.61k|                ctx->buf_rd_ix += 9;
  342|  4.61k|            }
  343|       |            /* determine the file organization based on the flags - D.4.2 again */
  344|  6.84k|            if (ctx->file_header_flags & 1) {
  ------------------
  |  Branch (344:17): [True: 5.98k, False: 861]
  ------------------
  345|  5.98k|                ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  346|  5.98k|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates sequential organization");
  ------------------
  |  |   68|  5.98k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  347|  5.98k|            } else {
  348|    861|                ctx->state = JBIG2_FILE_RANDOM_HEADERS;
  349|    861|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates random-access organization");
  ------------------
  |  |   68|    861|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  350|    861|            }
  351|  6.84k|            break;
  352|  53.4k|        case JBIG2_FILE_SEQUENTIAL_HEADER:
  ------------------
  |  Branch (352:9): [True: 53.4k, False: 87.1k]
  ------------------
  353|  80.4k|        case JBIG2_FILE_RANDOM_HEADERS:
  ------------------
  |  Branch (353:9): [True: 27.0k, False: 113k]
  ------------------
  354|  80.4k|            segment = jbig2_parse_segment_header(ctx, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix, &header_size);
  355|  80.4k|            if (segment == NULL)
  ------------------
  |  Branch (355:17): [True: 1.26k, False: 79.1k]
  ------------------
  356|  1.26k|                return 0; /* need more data */
  357|  79.1k|            ctx->buf_rd_ix += header_size;
  358|       |
  359|  79.1k|            if (ctx->n_segments >= ctx->n_segments_max) {
  ------------------
  |  Branch (359:17): [True: 603, False: 78.5k]
  ------------------
  360|    603|                Jbig2Segment **segments;
  361|       |
  362|    603|                if (ctx->n_segments_max == UINT32_MAX) {
  ------------------
  |  Branch (362:21): [True: 0, False: 603]
  ------------------
  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|    603|                else if (ctx->n_segments_max > (UINT32_MAX >> 2)) {
  ------------------
  |  Branch (367:26): [True: 0, False: 603]
  ------------------
  368|      0|                    ctx->n_segments_max = UINT32_MAX;
  369|    603|                } else {
  370|    603|                    ctx->n_segments_max <<= 2;
  371|    603|                }
  372|       |
  373|    603|                segments = jbig2_renew(ctx, ctx->segments, Jbig2Segment *, ctx->n_segments_max);
  ------------------
  |  |  143|    603|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
  374|    603|                if (segments == NULL) {
  ------------------
  |  Branch (374:21): [True: 2, False: 601]
  ------------------
  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|    601|                ctx->segments = segments;
  380|    601|            }
  381|       |
  382|  79.1k|            ctx->segments[ctx->n_segments++] = segment;
  383|  79.1k|            if (ctx->state == JBIG2_FILE_RANDOM_HEADERS) {
  ------------------
  |  Branch (383:17): [True: 26.2k, False: 52.9k]
  ------------------
  384|  26.2k|                if ((segment->flags & 63) == 51)        /* end of file */
  ------------------
  |  Branch (384:21): [True: 35, False: 26.1k]
  ------------------
  385|     35|                    ctx->state = JBIG2_FILE_RANDOM_BODIES;
  386|  26.2k|            } else              /* JBIG2_FILE_SEQUENTIAL_HEADER */
  387|  52.9k|                ctx->state = JBIG2_FILE_SEQUENTIAL_BODY;
  388|  79.1k|            break;
  389|  52.9k|        case JBIG2_FILE_SEQUENTIAL_BODY:
  ------------------
  |  Branch (389:9): [True: 52.9k, False: 87.5k]
  ------------------
  390|  53.1k|        case JBIG2_FILE_RANDOM_BODIES:
  ------------------
  |  Branch (390:9): [True: 212, False: 140k]
  ------------------
  391|  53.1k|            segment = ctx->segments[ctx->segment_index];
  392|       |
  393|       |            /* immediate generic regions may have unknown size */
  394|  53.1k|            if (segment->data_length == 0xffffffff && (segment->flags & 63) == 38) {
  ------------------
  |  Branch (394:17): [True: 18.7k, False: 34.4k]
  |  Branch (394:55): [True: 15.1k, False: 3.53k]
  ------------------
  395|  15.1k|                byte *s, *e, *p;
  396|  15.1k|                int mmr;
  397|  15.1k|                byte mmr_marker[2] = { 0x00, 0x00 };
  398|  15.1k|                byte arith_marker[2] = { 0xff, 0xac };
  399|  15.1k|                byte *desired_marker;
  400|       |
  401|  15.1k|                s = p = ctx->buf + ctx->buf_rd_ix;
  402|  15.1k|                e = ctx->buf + ctx->buf_wr_ix;
  403|       |
  404|  15.1k|                if (e - p < 18)
  ------------------
  |  Branch (404:21): [True: 36, False: 15.1k]
  ------------------
  405|     36|                        return 0; /* need more data */
  406|       |
  407|  15.1k|                mmr = p[17] & 1;
  408|  15.1k|                p += 18;
  409|  15.1k|                desired_marker = mmr ? mmr_marker : arith_marker;
  ------------------
  |  Branch (409:34): [True: 1.30k, False: 13.8k]
  ------------------
  410|       |
  411|       |                /* look for two byte marker */
  412|  15.1k|                if (e - p < 2)
  ------------------
  |  Branch (412:21): [True: 51, False: 15.0k]
  ------------------
  413|     51|                    return 0; /* need more data */
  414|       |
  415|  1.32M|                while (p[0] != desired_marker[0] || p[1] != desired_marker[1]) {
  ------------------
  |  Branch (415:24): [True: 1.13M, False: 189k]
  |  Branch (415:53): [True: 174k, False: 14.5k]
  ------------------
  416|  1.31M|                    p++;
  417|  1.31M|                    if (e - p < 2)
  ------------------
  |  Branch (417:25): [True: 498, False: 1.31M]
  ------------------
  418|    498|                        return 0; /* need more data */
  419|  1.31M|                }
  420|  14.5k|                p += 2;
  421|       |
  422|       |                /* the marker is followed by a four byte row count */
  423|  14.5k|                if (e - p < 4)
  ------------------
  |  Branch (423:21): [True: 23, False: 14.5k]
  ------------------
  424|     23|                        return 0; /* need more data */
  425|  14.5k|                segment->rows = jbig2_get_uint32(p);
  426|  14.5k|                p += 4;
  427|       |
  428|  14.5k|                segment->data_length = (size_t) (p - s);
  429|  14.5k|                jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "unknown length determined to be %lu", (long) segment->data_length);
  430|  14.5k|            }
  431|  38.0k|            else if (segment->data_length > ctx->buf_wr_ix - ctx->buf_rd_ix)
  ------------------
  |  Branch (431:22): [True: 3.97k, False: 34.0k]
  ------------------
  432|  3.97k|                    return 0; /* need more data */
  433|       |
  434|  48.6k|            code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
  435|  48.6k|            ctx->buf_rd_ix += segment->data_length;
  436|  48.6k|            ctx->segment_index++;
  437|  48.6k|            if (ctx->state == JBIG2_FILE_RANDOM_BODIES) {
  ------------------
  |  Branch (437:17): [True: 194, False: 48.4k]
  ------------------
  438|    194|                if (ctx->segment_index == ctx->n_segments)
  ------------------
  |  Branch (438:21): [True: 0, False: 194]
  ------------------
  439|      0|                    ctx->state = JBIG2_FILE_EOF;
  440|  48.4k|            } else {            /* JBIG2_FILE_SEQUENTIAL_BODY */
  441|  48.4k|                ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  442|  48.4k|            }
  443|  48.6k|            if (code < 0) {
  ------------------
  |  Branch (443:17): [True: 991, False: 47.6k]
  ------------------
  444|    991|                ctx->state = JBIG2_FILE_EOF;
  445|    991|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode; treating as end of file");
  446|    991|            }
  447|  47.6k|            break;
  448|  47.6k|        case JBIG2_FILE_EOF:
  ------------------
  |  Branch (448:9): [True: 0, False: 140k]
  ------------------
  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|   140k|        }
  453|   140k|    }
  454|  6.92k|}
jbig2_ctx_free:
  458|  6.92k|{
  459|  6.92k|    Jbig2Allocator *ca;
  460|  6.92k|    uint32_t i;
  461|       |
  462|  6.92k|    if (ctx == NULL)
  ------------------
  |  Branch (462:9): [True: 0, False: 6.92k]
  ------------------
  463|      0|        return NULL;
  464|       |
  465|  6.92k|    ca = ctx->allocator;
  466|  6.92k|    jbig2_free(ca, ctx->buf);
  467|  6.92k|    if (ctx->segments != NULL) {
  ------------------
  |  Branch (467:9): [True: 6.92k, False: 0]
  ------------------
  468|  86.1k|        for (i = 0; i < ctx->n_segments; i++)
  ------------------
  |  Branch (468:21): [True: 79.1k, False: 6.92k]
  ------------------
  469|  79.1k|            jbig2_free_segment(ctx, ctx->segments[i]);
  470|  6.92k|        jbig2_free(ca, ctx->segments);
  471|  6.92k|    }
  472|       |
  473|  6.92k|    if (ctx->pages != NULL) {
  ------------------
  |  Branch (473:9): [True: 6.92k, False: 0]
  ------------------
  474|  21.7k|        for (i = 0; i <= ctx->current_page; i++)
  ------------------
  |  Branch (474:21): [True: 14.8k, False: 6.92k]
  ------------------
  475|  14.8k|            if (ctx->pages[i].image != NULL)
  ------------------
  |  Branch (475:17): [True: 8.93k, False: 5.92k]
  ------------------
  476|  8.93k|                jbig2_image_release(ctx, ctx->pages[i].image);
  477|  6.92k|        jbig2_free(ca, ctx->pages);
  478|  6.92k|    }
  479|       |
  480|  6.92k|    jbig2_free(ca, ctx);
  481|       |
  482|  6.92k|    return ca;
  483|  6.92k|}
jbig2_word_stream_buf_new:
  545|  27.4k|{
  546|  27.4k|    Jbig2WordStreamBuf *result = jbig2_new(ctx, Jbig2WordStreamBuf, 1);
  ------------------
  |  |  141|  27.4k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  547|       |
  548|  27.4k|    if (result == NULL) {
  ------------------
  |  Branch (548:9): [True: 6, False: 27.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|  27.4k|    result->super.get_next_word = jbig2_word_stream_buf_get_next_word;
  554|  27.4k|    result->data = data;
  555|  27.4k|    result->size = size;
  556|       |
  557|  27.4k|    return &result->super;
  558|  27.4k|}
jbig2_word_stream_buf_free:
  562|  27.9k|{
  563|  27.9k|    jbig2_free(ctx->allocator, ws);
  564|  27.9k|}
jbig2_free:
  577|  48.3M|{
  578|  48.3M|    allocator->free(allocator, p);
  579|  48.3M|}
jbig2_realloc:
  583|  7.96k|{
  584|       |    /* check for integer multiplication overflow */
  585|  7.96k|    if (num > 0 && size >= SIZE_MAX / num)
  ------------------
  |  Branch (585:9): [True: 7.96k, False: 0]
  |  Branch (585:20): [True: 0, False: 7.96k]
  ------------------
  586|      0|        return NULL;
  587|  7.96k|    return allocator->realloc(allocator, p, size * num);
  588|  7.96k|}
jbig2.c:jbig2_default_error:
   78|   170M|{
   79|   170M|    (void) data;
   80|       |
   81|       |    /* report only fatal errors by default */
   82|   170M|    if (severity == JBIG2_SEVERITY_FATAL) {
  ------------------
  |  Branch (82:9): [True: 6.02k, False: 170M]
  ------------------
   83|  6.02k|        fprintf(stderr, "jbig2 decoder FATAL ERROR: %s", msg);
   84|  6.02k|        if (seg_idx != JBIG2_UNKNOWN_SEGMENT_NUMBER)
  ------------------
  |  |   68|  6.02k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  |  Branch (84:13): [True: 1.73k, False: 4.29k]
  ------------------
   85|  1.73k|            fprintf(stderr, " (segment 0x%02x)", seg_idx);
   86|  6.02k|        fprintf(stderr, "\n");
   87|       |        fflush(stderr);
   88|  6.02k|    }
   89|   170M|}
jbig2.c:jbig2_find_buffer_size:
  222|  6.92k|{
  223|  6.92k|    const size_t initial_buf_size = 1024;
  224|  6.92k|    size_t size = initial_buf_size;
  225|       |
  226|  6.92k|    if (desired == SIZE_MAX)
  ------------------
  |  Branch (226:9): [True: 0, False: 6.92k]
  ------------------
  227|      0|        return SIZE_MAX;
  228|       |
  229|  10.3k|    while (size < desired)
  ------------------
  |  Branch (229:12): [True: 3.45k, False: 6.92k]
  ------------------
  230|  3.45k|        size <<= 1;
  231|       |
  232|  6.92k|    return size;
  233|  6.92k|}
jbig2.c:jbig2_word_stream_buf_get_next_word:
  510|  6.91M|{
  511|  6.91M|    Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self;
  512|  6.91M|    uint32_t val = 0;
  513|  6.91M|    int ret = 0;
  514|       |
  515|  6.91M|    if (self == NULL || word == NULL) {
  ------------------
  |  Branch (515:9): [True: 0, False: 6.91M]
  |  Branch (515:25): [True: 0, False: 6.91M]
  ------------------
  516|      0|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read next word of stream because stream or output missing");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  517|      0|    }
  518|  6.91M|    if (offset >= z->size) {
  ------------------
  |  Branch (518:9): [True: 6.55M, False: 358k]
  ------------------
  519|  6.55M|        *word = 0;
  520|  6.55M|        return 0;
  521|  6.55M|    }
  522|       |
  523|   358k|    if (offset < z->size) {
  ------------------
  |  Branch (523:9): [True: 358k, False: 0]
  ------------------
  524|   358k|        val = (uint32_t) z->data[offset] << 24;
  525|   358k|        ret++;
  526|   358k|    }
  527|   358k|    if (offset + 1 < z->size) {
  ------------------
  |  Branch (527:9): [True: 352k, False: 6.37k]
  ------------------
  528|   352k|        val |= (uint32_t) z->data[offset + 1] << 16;
  529|   352k|        ret++;
  530|   352k|    }
  531|   358k|    if (offset + 2 < z->size) {
  ------------------
  |  Branch (531:9): [True: 348k, False: 10.3k]
  ------------------
  532|   348k|        val |= (uint32_t) z->data[offset + 2] << 8;
  533|   348k|        ret++;
  534|   348k|    }
  535|   358k|    if (offset + 3 < z->size) {
  ------------------
  |  Branch (535:9): [True: 345k, False: 13.3k]
  ------------------
  536|   345k|        val |= z->data[offset + 3];
  537|   345k|        ret++;
  538|   345k|    }
  539|   358k|    *word = val;
  540|   358k|    return ret;
  541|  6.91M|}

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

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

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

jbig2_generic_stats_size:
  360|  16.9k|{
  361|  16.9k|    int stats_size = template == 0 ? 1 << 16 : template == 1 ? 1 << 13 : 1 << 10;
  ------------------
  |  Branch (361:22): [True: 8.99k, False: 7.93k]
  |  Branch (361:48): [True: 1.84k, False: 6.09k]
  ------------------
  362|       |
  363|  16.9k|    (void) ctx;
  364|       |
  365|  16.9k|    return stats_size;
  366|  16.9k|}
jbig2_decode_generic_region:
 1372|   179k|{
 1373|   179k|    const int8_t *gbat = params->gbat;
 1374|       |
 1375|   179k|    if (!params->MMR && params->TPGDON)
  ------------------
  |  Branch (1375:9): [True: 179k, False: 0]
  |  Branch (1375:25): [True: 15.9k, False: 163k]
  ------------------
 1376|  15.9k|        return jbig2_decode_generic_region_TPGDON(ctx, segment, params, as, image, GB_stats);
 1377|       |
 1378|   163k|    if (!params->MMR && params->GBTEMPLATE == 0) {
  ------------------
  |  Branch (1378:9): [True: 163k, False: 0]
  |  Branch (1378:25): [True: 20.8k, False: 142k]
  ------------------
 1379|  20.8k|        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: 20.3k, False: 415]
  |  Branch (1379:33): [True: 17.8k, False: 2.54k]
  |  Branch (1379:50): [True: 15.5k, False: 2.28k]
  |  Branch (1379:67): [True: 13.5k, False: 2.03k]
  |  Branch (1379:84): [True: 11.8k, False: 1.63k]
  |  Branch (1379:101): [True: 11.5k, False: 343]
  |  Branch (1379:118): [True: 9.63k, False: 1.92k]
  |  Branch (1379:135): [True: 7.23k, False: 2.39k]
  |  Branch (1379:152): [True: 6.92k, False: 313]
  ------------------
 1380|  6.92k|            return jbig2_decode_generic_template0(ctx, segment, params, as, image, GB_stats);
 1381|  13.8k|        else
 1382|  13.8k|            return jbig2_decode_generic_template0_unopt(ctx, segment, params, as, image, GB_stats);
 1383|   142k|    } else if (!params->MMR && params->GBTEMPLATE == 1) {
  ------------------
  |  Branch (1383:16): [True: 142k, False: 0]
  |  Branch (1383:32): [True: 11.1k, False: 131k]
  ------------------
 1384|  11.1k|        if (!params->USESKIP && gbat[0] == +3 && gbat[1] == -1)
  ------------------
  |  Branch (1384:13): [True: 10.2k, False: 931]
  |  Branch (1384:33): [True: 5.75k, False: 4.48k]
  |  Branch (1384:50): [True: 5.43k, False: 318]
  ------------------
 1385|  5.43k|            return jbig2_decode_generic_template1(ctx, segment, params, as, image, GB_stats);
 1386|  5.73k|        else
 1387|  5.73k|            return jbig2_decode_generic_template1_unopt(ctx, segment, params, as, image, GB_stats);
 1388|  11.1k|    }
 1389|   131k|    else if (!params->MMR && params->GBTEMPLATE == 2) {
  ------------------
  |  Branch (1389:14): [True: 131k, False: 0]
  |  Branch (1389:30): [True: 120k, False: 10.7k]
  ------------------
 1390|   120k|        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
  ------------------
  |  Branch (1390:13): [True: 120k, False: 757]
  |  Branch (1390:33): [True: 84.3k, False: 35.6k]
  |  Branch (1390:49): [True: 82.3k, False: 2.02k]
  ------------------
 1391|  82.3k|            return jbig2_decode_generic_template2(ctx, segment, params, as, image, GB_stats);
 1392|  38.3k|        else
 1393|  38.3k|            return jbig2_decode_generic_template2_unopt(ctx, segment, params, as, image, GB_stats);
 1394|   120k|    } else if (!params->MMR && params->GBTEMPLATE == 3) {
  ------------------
  |  Branch (1394:16): [True: 10.7k, False: 0]
  |  Branch (1394:32): [True: 10.7k, False: 0]
  ------------------
 1395|  10.7k|        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
  ------------------
  |  Branch (1395:13): [True: 10.5k, False: 202]
  |  Branch (1395:33): [True: 5.31k, False: 5.19k]
  |  Branch (1395:49): [True: 4.67k, False: 645]
  ------------------
 1396|  4.67k|            return jbig2_decode_generic_template3(ctx, segment, params, as, image, GB_stats);
 1397|  6.04k|        else
 1398|  6.04k|            return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats);
 1399|  10.7k|    }
 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|   163k|}
jbig2_immediate_generic_region:
 1416|  20.9k|{
 1417|  20.9k|    Jbig2RegionSegmentInfo rsi;
 1418|  20.9k|    byte seg_flags;
 1419|  20.9k|    int8_t gbat[8];
 1420|  20.9k|    int offset;
 1421|  20.9k|    uint32_t gbat_bytes = 0;
 1422|  20.9k|    Jbig2GenericRegionParams params;
 1423|  20.9k|    int code = 0;
 1424|  20.9k|    Jbig2Image *image = NULL;
 1425|  20.9k|    Jbig2WordStream *ws = NULL;
 1426|  20.9k|    Jbig2ArithState *as = NULL;
 1427|  20.9k|    Jbig2ArithCx *GB_stats = NULL;
 1428|  20.9k|    uint32_t height;
 1429|  20.9k|    Jbig2Page *page = &ctx->pages[ctx->current_page];
 1430|       |
 1431|       |    /* 7.4.6 */
 1432|  20.9k|    if (segment->data_length < 18)
  ------------------
  |  Branch (1432:9): [True: 46, False: 20.8k]
  ------------------
 1433|     46|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
 1434|       |
 1435|  20.8k|    jbig2_get_region_segment_info(&rsi, segment_data);
 1436|  20.8k|    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|  20.8k|    height = rsi.height;
 1440|  20.8k|    if (segment->rows != UINT32_MAX) {
  ------------------
  |  Branch (1440:9): [True: 14.5k, False: 6.33k]
  ------------------
 1441|  14.5k|        if (segment->rows > rsi.height)
  ------------------
  |  Branch (1441:13): [True: 42, False: 14.5k]
  ------------------
 1442|     42|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment contains more rows than stated in header");
 1443|  14.5k|        height = segment->rows;
 1444|  14.5k|    }
 1445|       |
 1446|       |    /* 7.4.6.2 */
 1447|  20.8k|    seg_flags = segment_data[17];
 1448|  20.8k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "segment flags = %02x", seg_flags);
 1449|  20.8k|    if ((seg_flags & 1) && (seg_flags & 6))
  ------------------
  |  Branch (1449:9): [True: 1.55k, False: 19.2k]
  |  Branch (1449:28): [True: 673, False: 886]
  ------------------
 1450|    673|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "MMR is 1, but GBTEMPLATE is not 0");
 1451|       |
 1452|       |    /* 7.4.6.3 */
 1453|  20.8k|    if (!(seg_flags & 1)) {
  ------------------
  |  Branch (1453:9): [True: 19.2k, False: 1.55k]
  ------------------
 1454|  19.2k|        gbat_bytes = (seg_flags & 6) ? 2 : 8;
  ------------------
  |  Branch (1454:22): [True: 9.61k, False: 9.68k]
  ------------------
 1455|  19.2k|        if (18 + gbat_bytes > segment->data_length)
  ------------------
  |  Branch (1455:13): [True: 12, False: 19.2k]
  ------------------
 1456|     12|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
 1457|  19.2k|        memcpy(gbat, segment_data + 18, gbat_bytes);
 1458|  19.2k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "gbat: %d, %d", gbat[0], gbat[1]);
 1459|  19.2k|    }
 1460|       |
 1461|  20.8k|    offset = 18 + gbat_bytes;
 1462|       |
 1463|       |    /* Check for T.88 amendment 2 */
 1464|  20.8k|    if ((seg_flags >> 5) & 1)
  ------------------
  |  Branch (1464:9): [True: 34, False: 20.8k]
  ------------------
 1465|     34|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment uses 12 adaptive template pixels (NYI)");
 1466|       |
 1467|       |    /* Table 34 */
 1468|  20.8k|    params.MMR = seg_flags & 1;
 1469|  20.8k|    params.GBTEMPLATE = (seg_flags & 6) >> 1;
 1470|  20.8k|    params.TPGDON = (seg_flags & 8) >> 3;
 1471|  20.8k|    params.USESKIP = 0;
 1472|  20.8k|    memcpy(params.gbat, gbat, gbat_bytes);
 1473|       |
 1474|  20.8k|    if (page->height == 0xffffffff && page->striped && page->stripe_size > 0) {
  ------------------
  |  Branch (1474:9): [True: 13.9k, False: 6.81k]
  |  Branch (1474:39): [True: 12.0k, False: 1.90k]
  |  Branch (1474:56): [True: 12.0k, False: 0]
  ------------------
 1475|  12.0k|        if (rsi.y >= page->end_row + page->stripe_size) {
  ------------------
  |  Branch (1475:13): [True: 2.30k, False: 9.78k]
  ------------------
 1476|  2.30k|            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.30k|            return 0;
 1478|  2.30k|        }
 1479|  9.78k|        if (height > page->end_row + page->stripe_size) {
  ------------------
  |  Branch (1479:13): [True: 8.28k, False: 1.50k]
  ------------------
 1480|  8.28k|            height = page->end_row + page->stripe_size;
 1481|  8.28k|        }
 1482|  9.78k|    } else {
 1483|  8.71k|        if (rsi.y >= page->height) {
  ------------------
  |  Branch (1483:13): [True: 732, False: 7.98k]
  ------------------
 1484|    732|            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|    732|            return 0;
 1486|    732|        }
 1487|  7.98k|        if (height > page->height - rsi .y) {
  ------------------
  |  Branch (1487:13): [True: 6.08k, False: 1.89k]
  ------------------
 1488|  6.08k|            height = page->height - rsi.y;
 1489|  6.08k|        }
 1490|  7.98k|    }
 1491|  17.7k|    if (height == 0) {
  ------------------
  |  Branch (1491:9): [True: 1.05k, False: 16.7k]
  ------------------
 1492|  1.05k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "nothing remains of region, ignoring");
 1493|  1.05k|        return 0;
 1494|  1.05k|    }
 1495|       |
 1496|  16.7k|    image = jbig2_image_new(ctx, rsi.width, height);
 1497|  16.7k|    if (image == NULL)
  ------------------
  |  Branch (1497:9): [True: 70, False: 16.6k]
  ------------------
 1498|     70|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate generic image");
 1499|  16.6k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, height);
 1500|       |
 1501|  16.6k|    if (params.MMR) {
  ------------------
  |  Branch (1501:9): [True: 430, False: 16.2k]
  ------------------
 1502|    430|        code = jbig2_decode_generic_mmr(ctx, segment, &params, segment_data + offset, segment->data_length - offset, image);
 1503|    430|        if (code < 0) {
  ------------------
  |  Branch (1503:13): [True: 29, False: 401]
  ------------------
 1504|     29|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode MMR-coded generic region");
 1505|     29|            goto cleanup;
 1506|     29|        }
 1507|  16.2k|    } else {
 1508|  16.2k|        int stats_size = jbig2_generic_stats_size(ctx, params.GBTEMPLATE);
 1509|       |
 1510|  16.2k|        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  16.2k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
 1511|  16.2k|        if (GB_stats == NULL) {
  ------------------
  |  Branch (1511:13): [True: 1, False: 16.2k]
  ------------------
 1512|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder states when handling immediate generic region");
 1513|      1|            goto cleanup;
 1514|      1|        }
 1515|  16.2k|        memset(GB_stats, 0, stats_size);
 1516|       |
 1517|  16.2k|        ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
 1518|  16.2k|        if (ws == NULL) {
  ------------------
  |  Branch (1518:13): [True: 1, False: 16.2k]
  ------------------
 1519|      1|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocated word stream when handling immediate generic region");
 1520|      1|            goto cleanup;
 1521|      1|        }
 1522|  16.2k|        as = jbig2_arith_new(ctx, ws);
 1523|  16.2k|        if (as == NULL) {
  ------------------
  |  Branch (1523:13): [True: 5, False: 16.2k]
  ------------------
 1524|      5|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling immediate generic region");
 1525|      5|            goto cleanup;
 1526|      5|        }
 1527|  16.2k|        code = jbig2_decode_generic_region(ctx, segment, &params, as, image, GB_stats);
 1528|  16.2k|        if (code < 0) {
  ------------------
  |  Branch (1528:13): [True: 76, False: 16.1k]
  ------------------
 1529|     76|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode immediate generic region");
 1530|     76|            goto cleanup;
 1531|     76|        }
 1532|  16.2k|    }
 1533|       |
 1534|  16.5k|    code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
 1535|  16.5k|    if (code < 0)
  ------------------
  |  Branch (1535:9): [True: 729, False: 15.8k]
  ------------------
 1536|    729|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add result to page");
 1537|       |
 1538|  16.6k|cleanup:
 1539|  16.6k|    jbig2_free(ctx->allocator, as);
 1540|  16.6k|    jbig2_word_stream_buf_free(ctx, ws);
 1541|  16.6k|    jbig2_free(ctx->allocator, GB_stats);
 1542|  16.6k|    jbig2_image_release(ctx, image);
 1543|       |
 1544|  16.6k|    return code;
 1545|  16.5k|}
jbig2_generic.c:jbig2_decode_generic_region_TPGDON:
 1337|  15.9k|{
 1338|  15.9k|    switch (params->GBTEMPLATE) {
  ------------------
  |  Branch (1338:13): [True: 15.9k, False: 0]
  ------------------
 1339|  8.57k|    case 0:
  ------------------
  |  Branch (1339:5): [True: 8.57k, False: 7.34k]
  ------------------
 1340|  8.57k|        return jbig2_decode_generic_template0_TPGDON(ctx, segment, params, as, image, GB_stats);
 1341|  1.62k|    case 1:
  ------------------
  |  Branch (1341:5): [True: 1.62k, False: 14.2k]
  ------------------
 1342|  1.62k|        return jbig2_decode_generic_template1_TPGDON(ctx, segment, params, as, image, GB_stats);
 1343|  4.97k|    case 2:
  ------------------
  |  Branch (1343:5): [True: 4.97k, False: 10.9k]
  ------------------
 1344|  4.97k|        return jbig2_decode_generic_template2_TPGDON(ctx, segment, params, as, image, GB_stats);
 1345|    746|    case 3:
  ------------------
  |  Branch (1345:5): [True: 746, False: 15.1k]
  ------------------
 1346|    746|        return jbig2_decode_generic_template3_TPGDON(ctx, segment, params, as, image, GB_stats);
 1347|  15.9k|    }
 1348|       |
 1349|      0|    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported GBTEMPLATE (%d)", params->GBTEMPLATE);
 1350|  15.9k|}
jbig2_generic.c:jbig2_decode_generic_template0_TPGDON:
  924|  8.57k|{
  925|  8.57k|    const int64_t GBW = image->width;
  926|  8.57k|    const int64_t GBH = image->height;
  927|  8.57k|    uint32_t CONTEXT;
  928|  8.57k|    int64_t x, y;
  929|  8.57k|    int LTP = 0;
  930|  8.57k|    int gmin, gmax;
  931|  8.57k|    int64_t left, right, top;
  932|       |
  933|  8.57k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
  ------------------
  |  |  440|  17.1k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.57k]
  |  |  |  Branch (440:20): [True: 4, False: 8.57k]
  |  |  |  Branch (440:31): [True: 0, False: 8.57k]
  |  |  |  Branch (440:46): [True: 6.11k, False: 2.46k]
  |  |  |  Branch (440:57): [True: 0, False: 6.11k]
  |  |  |  Branch (440:72): [True: 2.46k, False: 6.11k]
  |  |  |  Branch (440:84): [True: 7, False: 2.45k]
  |  |  ------------------
  ------------------
  934|  8.56k|        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
  ------------------
  |  |  440|  17.1k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.56k]
  |  |  |  Branch (440:20): [True: 6, False: 8.56k]
  |  |  |  Branch (440:31): [True: 0, False: 8.56k]
  |  |  |  Branch (440:46): [True: 7.46k, False: 1.09k]
  |  |  |  Branch (440:57): [True: 0, False: 7.46k]
  |  |  |  Branch (440:72): [True: 1.09k, False: 7.46k]
  |  |  |  Branch (440:84): [True: 4, False: 1.09k]
  |  |  ------------------
  ------------------
  935|  8.55k|        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
  ------------------
  |  |  440|  17.1k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.55k]
  |  |  |  Branch (440:20): [True: 5, False: 8.55k]
  |  |  |  Branch (440:31): [True: 0, False: 8.55k]
  |  |  |  Branch (440:46): [True: 7.45k, False: 1.10k]
  |  |  |  Branch (440:57): [True: 0, False: 7.45k]
  |  |  |  Branch (440:72): [True: 1.10k, False: 7.45k]
  |  |  |  Branch (440:84): [True: 6, False: 1.09k]
  |  |  ------------------
  ------------------
  936|  8.54k|        pixel_outside_field(params->gbat[6], params->gbat[7]))
  ------------------
  |  |  440|  8.54k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 8.54k]
  |  |  |  Branch (440:20): [True: 3, False: 8.54k]
  |  |  |  Branch (440:31): [True: 0, False: 8.54k]
  |  |  |  Branch (440:46): [True: 7.04k, False: 1.49k]
  |  |  |  Branch (440:57): [True: 0, False: 7.04k]
  |  |  |  Branch (440:72): [True: 1.49k, False: 7.04k]
  |  |  |  Branch (440:84): [True: 9, False: 1.48k]
  |  |  ------------------
  ------------------
  937|     44|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  938|     44|                           "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.53k|    if (params->gbat[0] ==  3 && params->gbat[1] == -1 &&
  ------------------
  |  Branch (944:9): [True: 4.74k, False: 3.78k]
  |  Branch (944:34): [True: 4.69k, False: 48]
  ------------------
  945|  4.69k|        params->gbat[2] == -3 && params->gbat[3] == -1 &&
  ------------------
  |  Branch (945:9): [True: 3.87k, False: 826]
  |  Branch (945:34): [True: 3.20k, False: 672]
  ------------------
  946|  3.20k|        params->gbat[4] ==  2 && params->gbat[5] == -2 &&
  ------------------
  |  Branch (946:9): [True: 2.93k, False: 263]
  |  Branch (946:34): [True: 2.71k, False: 219]
  ------------------
  947|  2.71k|        params->gbat[6] == -2 && params->gbat[7] == -2)
  ------------------
  |  Branch (947:9): [True: 1.44k, False: 1.26k]
  |  Branch (947:34): [True: 1.34k, False: 106]
  ------------------
  948|  1.34k|    {
  949|  54.3M|        for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (949:21): [True: 54.3M, False: 1.34k]
  ------------------
  950|  54.3M|            int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
  951|  54.3M|            if (bit < 0)
  ------------------
  |  Branch (951:17): [True: 0, False: 54.3M]
  ------------------
  952|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
  953|  54.3M|            LTP ^= bit;
  954|  54.3M|            if (!LTP) {
  ------------------
  |  Branch (954:17): [True: 27.1M, False: 27.1M]
  ------------------
  955|  27.1M|                uint32_t out_byte = 0;
  956|  27.1M|                int out_bits_to_go_in_byte = 8;
  957|  27.1M|                uint8_t *d = &image->data[image->stride * y];
  958|  27.1M|                uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (958:35): [True: 27.1M, False: 848]
  ------------------
  959|  27.1M|                uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (959:35): [True: 27.1M, False: 2.03k]
  ------------------
  960|  27.1M|                uint32_t pd = 0;
  961|  27.1M|                uint32_t ppd = 0;
  962|  27.1M|                if (y > 0 && pline) {
  ------------------
  |  Branch (962:21): [True: 27.1M, False: 848]
  |  Branch (962:30): [True: 27.1M, False: 0]
  ------------------
  963|  27.1M|                    pd = (*pline++ << 8);
  964|  27.1M|                    if (GBW > 8)
  ------------------
  |  Branch (964:25): [True: 291k, False: 26.8M]
  ------------------
  965|   291k|                        pd |= *pline++;
  966|  27.1M|                    if (y > 1 && ppline) {
  ------------------
  |  Branch (966:25): [True: 27.1M, False: 1.18k]
  |  Branch (966:34): [True: 27.1M, False: 0]
  ------------------
  967|  27.1M|                        ppd = (*ppline++ << 8);
  968|  27.1M|                        if (GBW > 8)
  ------------------
  |  Branch (968:29): [True: 290k, False: 26.8M]
  ------------------
  969|   290k|                            ppd |= *ppline++;
  970|  27.1M|                    }
  971|  27.1M|                }
  972|   670M|                for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (972:29): [True: 643M, False: 27.1M]
  ------------------
  973|   643M|                    if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (973:25): [True: 0, False: 643M]
  |  Branch (973:44): [True: 0, False: 0]
  ------------------
  974|      0|                        bit = 0;
  975|   643M|                    } else {
  976|   643M|                        CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
  977|   643M|                        CONTEXT |= (pd>>8) & 0x7F0; /* Next 7 pixels */
  978|   643M|                        CONTEXT |= (ppd>>2) & 0xF800; /* Final 5 pixels */
  979|   643M|                        bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  980|   643M|                        if (bit < 0)
  ------------------
  |  Branch (980:29): [True: 0, False: 643M]
  ------------------
  981|      0|                            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
  982|   643M|                    }
  983|   643M|                    pd = pd<<1;
  984|   643M|                    ppd = ppd<<1;
  985|   643M|                    out_byte = (out_byte<<1) | bit;
  986|   643M|                    out_bits_to_go_in_byte--;
  987|   643M|                    if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (987:25): [True: 79.1M, False: 564M]
  ------------------
  988|  79.1M|                        out_bits_to_go_in_byte = 8;
  989|  79.1M|                        *d++ = (uint8_t)out_byte;
  990|  79.1M|                        if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (990:29): [True: 60.1M, False: 18.9M]
  |  Branch (990:42): [True: 23.5M, False: 36.6M]
  |  Branch (990:51): [True: 23.5M, False: 0]
  ------------------
  991|  23.5M|                            pd |= *pline++;
  992|  23.5M|                            if (y > 1 && ppline)
  ------------------
  |  Branch (992:33): [True: 20.8M, False: 2.69M]
  |  Branch (992:42): [True: 20.8M, False: 0]
  ------------------
  993|  20.8M|                                ppd |= *ppline++;
  994|  23.5M|                        }
  995|  79.1M|                    }
  996|   643M|                }
  997|  27.1M|                if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (997:21): [True: 8.51M, False: 18.6M]
  ------------------
  998|  8.51M|                    *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  999|  27.1M|            } else {
 1000|  27.1M|                copy_prev_row(image, y);
 1001|  27.1M|            }
 1002|  54.3M|        }
 1003|  1.34k|        return 0;
 1004|  1.34k|    }
 1005|       |
 1006|       |    /* We divide the width into 3 regions 0..left...right...GBW,
 1007|       |     * between left and right, we know that our accesses will never
 1008|       |     * step outside the image, enabling us to use faster accessors. */
 1009|  7.19k|    left = 4;
 1010|  7.19k|    right = 2;
 1011|  7.19k|    gmin = gmax = params->gbat[0];
 1012|  7.19k|    if (params->gbat[2] < gmin)
  ------------------
  |  Branch (1012:9): [True: 2.71k, False: 4.47k]
  ------------------
 1013|  2.71k|        gmin = params->gbat[2];
 1014|  7.19k|    if (gmax < params->gbat[2])
  ------------------
  |  Branch (1014:9): [True: 3.27k, False: 3.92k]
  ------------------
 1015|  3.27k|        gmax = params->gbat[2];
 1016|  7.19k|    if (params->gbat[4] < gmin)
  ------------------
  |  Branch (1016:9): [True: 994, False: 6.19k]
  ------------------
 1017|    994|        gmin = params->gbat[4];
 1018|  7.19k|    if (gmax < params->gbat[4])
  ------------------
  |  Branch (1018:9): [True: 1.03k, False: 6.16k]
  ------------------
 1019|  1.03k|        gmax = params->gbat[4];
 1020|  7.19k|    if (params->gbat[6] < gmin)
  ------------------
  |  Branch (1020:9): [True: 1.10k, False: 6.08k]
  ------------------
 1021|  1.10k|        gmin = params->gbat[6];
 1022|  7.19k|    if (gmax < params->gbat[6])
  ------------------
  |  Branch (1022:9): [True: 2.98k, False: 4.20k]
  ------------------
 1023|  2.98k|        gmax = params->gbat[6];
 1024|  7.19k|    if (left < -((int64_t) gmin))
  ------------------
  |  Branch (1024:9): [True: 4.46k, False: 2.73k]
  ------------------
 1025|  4.46k|        left = -((int64_t) gmin);
 1026|  7.19k|    if (right < gmax)
  ------------------
  |  Branch (1026:9): [True: 3.86k, False: 3.33k]
  ------------------
 1027|  3.86k|        right = gmax;
 1028|  7.19k|    if (right > GBW)
  ------------------
  |  Branch (1028:9): [True: 2.49k, False: 4.69k]
  ------------------
 1029|  2.49k|        right = GBW;
 1030|  7.19k|    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.19k|    top = 2;
 1036|  7.19k|    gmin = params->gbat[1];
 1037|  7.19k|    if (params->gbat[3] < gmin)
  ------------------
  |  Branch (1037:9): [True: 2.17k, False: 5.01k]
  ------------------
 1038|  2.17k|        gmin = params->gbat[3];
 1039|  7.19k|    if (params->gbat[5] < gmin)
  ------------------
  |  Branch (1039:9): [True: 3.91k, False: 3.27k]
  ------------------
 1040|  3.91k|        gmin = params->gbat[5];
 1041|  7.19k|    if (params->gbat[7] < gmin)
  ------------------
  |  Branch (1041:9): [True: 1.51k, False: 5.67k]
  ------------------
 1042|  1.51k|        gmin = params->gbat[7];
 1043|  7.19k|    if (top < -((int64_t) gmin))
  ------------------
  |  Branch (1043:9): [True: 4.47k, False: 2.71k]
  ------------------
 1044|  4.47k|        top = -((int64_t) gmin);
 1045|       |    /* So 0 <= y < top needs bounds checking. */
 1046|       |
 1047|   142M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1047:17): [True: 142M, False: 7.19k]
  ------------------
 1048|   142M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
 1049|   142M|        if (bit < 0)
  ------------------
  |  Branch (1049:13): [True: 0, False: 142M]
  ------------------
 1050|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
 1051|   142M|        LTP ^= bit;
 1052|   142M|        if (!LTP) {
  ------------------
  |  Branch (1052:13): [True: 93.7M, False: 49.1M]
  ------------------
 1053|  93.7M|            uint32_t out_byte = 0;
 1054|  93.7M|            int out_bits_to_go_in_byte = 8;
 1055|  93.7M|            uint8_t *d = &image->data[image->stride * y];
 1056|  93.7M|            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1056:31): [True: 93.7M, False: 3.92k]
  ------------------
 1057|  93.7M|            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (1057:31): [True: 93.7M, False: 9.17k]
  ------------------
 1058|  93.7M|            uint32_t pd = 0;
 1059|  93.7M|            uint32_t ppd = 0;
 1060|  93.7M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1060:17): [True: 93.7M, False: 3.92k]
  |  Branch (1060:26): [True: 93.7M, False: 0]
  ------------------
 1061|  93.7M|                pd = (*pline++ << 8);
 1062|  93.7M|                if (GBW > 8)
  ------------------
  |  Branch (1062:21): [True: 3.24M, False: 90.5M]
  ------------------
 1063|  3.24M|                    pd |= *pline++;
 1064|  93.7M|                if (y > 1 && ppline) {
  ------------------
  |  Branch (1064:21): [True: 93.7M, False: 5.25k]
  |  Branch (1064:30): [True: 93.7M, False: 0]
  ------------------
 1065|  93.7M|                    ppd = (*ppline++ << 8);
 1066|  93.7M|                    if (GBW > 8)
  ------------------
  |  Branch (1066:25): [True: 3.24M, False: 90.5M]
  ------------------
 1067|  3.24M|                        ppd |= *ppline++;
 1068|  93.7M|                }
 1069|  93.7M|            }
 1070|  1.07G|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1070:25): [True: 983M, False: 93.7M]
  ------------------
 1071|   983M|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1071:21): [True: 0, False: 983M]
  |  Branch (1071:40): [True: 0, False: 0]
  ------------------
 1072|      0|                    bit = 0;
 1073|   983M|                } else {
 1074|   983M|                    CONTEXT = out_byte & 0x000F; /* First 4 pixels */
 1075|   983M|                    CONTEXT |= (pd>>8) & 0x03E0; /* Skip one, next 5 pixels */
 1076|   983M|                    CONTEXT |= (ppd>>2) & 0x7000; /* Skip 2, next 3 pixels, skip one */
 1077|   983M|                    if (y >= top && x >= left && x < right)
  ------------------
  |  Branch (1077:25): [True: 687M, False: 295M]
  |  Branch (1077:37): [True: 476M, False: 211M]
  |  Branch (1077:50): [True: 435M, False: 40.2M]
  ------------------
 1078|   435M|                    {
 1079|   435M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[0], y + params->gbat[1]) << 4;
 1080|   435M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[2], y + params->gbat[3]) << 10;
 1081|   435M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[4], y + params->gbat[5]) << 11;
 1082|   435M|                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[6], y + params->gbat[7]) << 15;
 1083|   435M|                    }
 1084|   547M|                    else
 1085|   547M|                    {
 1086|   547M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
 1087|   547M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
 1088|   547M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
 1089|   547M|                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
 1090|   547M|                    }
 1091|   983M|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1092|   983M|                    if (bit < 0)
  ------------------
  |  Branch (1092:25): [True: 0, False: 983M]
  ------------------
 1093|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
 1094|   983M|                }
 1095|   983M|                pd = pd<<1;
 1096|   983M|                ppd = ppd<<1;
 1097|   983M|                out_byte = (out_byte<<1) | bit;
 1098|   983M|                out_bits_to_go_in_byte--;
 1099|   983M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1100|   983M|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1100:21): [True: 100M, False: 882M]
  ------------------
 1101|   100M|                    out_bits_to_go_in_byte = 8;
 1102|   100M|                    d++;
 1103|   100M|                    if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (1103:25): [True: 96.3M, False: 4.43M]
  |  Branch (1103:38): [True: 74.8M, False: 21.5M]
  |  Branch (1103:47): [True: 74.8M, False: 0]
  ------------------
 1104|  74.8M|                        pd |= *pline++;
 1105|  74.8M|                        if (y > 1 && ppline)
  ------------------
  |  Branch (1105:29): [True: 69.8M, False: 5.00M]
  |  Branch (1105:38): [True: 69.8M, False: 0]
  ------------------
 1106|  69.8M|                            ppd |= *ppline++;
 1107|  74.8M|                    }
 1108|   100M|                }
 1109|   983M|            }
 1110|  93.7M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1110:17): [True: 92.5M, False: 1.18M]
  ------------------
 1111|  92.5M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1112|  93.7M|        } else {
 1113|  49.1M|            copy_prev_row(image, y);
 1114|  49.1M|        }
 1115|   142M|    }
 1116|       |
 1117|  7.19k|    return 0;
 1118|  7.19k|}
jbig2_generic.c:copy_prev_row:
  908|   240M|{
  909|   240M|    if (!row) {
  ------------------
  |  Branch (909:9): [True: 5.28k, False: 240M]
  ------------------
  910|       |        /* no previous row */
  911|  5.28k|        memset(image->data, 0, image->stride);
  912|   240M|    } else {
  913|       |        /* duplicate data from the previous row */
  914|   240M|        uint8_t *src = image->data + (row - 1) * image->stride;
  915|       |
  916|   240M|        memcpy(src + image->stride, src, image->stride);
  917|   240M|    }
  918|   240M|}
jbig2_generic.c:jbig2_image_get_pixel_fast:
  348|  1.74G|{
  349|  1.74G|    size_t sx = (size_t) x;
  350|  1.74G|    size_t sy = (size_t) y;
  351|  1.74G|    size_t byte = (sx >> 3) + sy * image->stride;
  352|  1.74G|    int bit = 7 - ((int) (sx & 7));
  353|       |
  354|  1.74G|    return ((image->data[byte] >> bit) & 1);
  355|  1.74G|}
jbig2_generic.c:jbig2_decode_generic_template1_TPGDON:
 1124|  1.62k|{
 1125|  1.62k|    const int64_t GBW = image->width;
 1126|  1.62k|    const int64_t GBH = image->height;
 1127|  1.62k|    uint32_t CONTEXT;
 1128|  1.62k|    int64_t x, y;
 1129|  1.62k|    int LTP = 0;
 1130|       |
 1131|  1.62k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  1.62k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 1.62k]
  |  |  |  Branch (440:20): [True: 2, False: 1.62k]
  |  |  |  Branch (440:31): [True: 0, False: 1.62k]
  |  |  |  Branch (440:46): [True: 905, False: 720]
  |  |  |  Branch (440:57): [True: 0, False: 905]
  |  |  |  Branch (440:72): [True: 720, False: 905]
  |  |  |  Branch (440:84): [True: 4, False: 716]
  |  |  ------------------
  ------------------
 1132|      6|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 1133|      6|                           "adaptive template pixel is out of field");
 1134|       |
 1135|   106M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1135:17): [True: 106M, False: 1.62k]
  ------------------
 1136|   106M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0795]);
 1137|   106M|        if (bit < 0)
  ------------------
  |  Branch (1137:13): [True: 0, False: 106M]
  ------------------
 1138|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON1");
 1139|   106M|        LTP ^= bit;
 1140|   106M|        if (!LTP) {
  ------------------
  |  Branch (1140:13): [True: 52.4M, False: 54.0M]
  ------------------
 1141|  52.4M|            uint32_t out_byte = 0;
 1142|  52.4M|            int out_bits_to_go_in_byte = 8;
 1143|  52.4M|            uint8_t *d = &image->data[image->stride * y];
 1144|  52.4M|            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1144:31): [True: 52.4M, False: 902]
  ------------------
 1145|  52.4M|            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (1145:31): [True: 52.4M, False: 1.85k]
  ------------------
 1146|  52.4M|            uint32_t pd = 0;
 1147|  52.4M|            uint32_t ppd = 0;
 1148|  52.4M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1148:17): [True: 52.4M, False: 902]
  |  Branch (1148:26): [True: 52.4M, False: 0]
  ------------------
 1149|  52.4M|                pd = (*pline++ << 8);
 1150|  52.4M|                if (GBW > 8)
  ------------------
  |  Branch (1150:21): [True: 3.14M, False: 49.3M]
  ------------------
 1151|  3.14M|                    pd |= *pline++;
 1152|  52.4M|                if (y > 1 && ppline) {
  ------------------
  |  Branch (1152:21): [True: 52.4M, False: 949]
  |  Branch (1152:30): [True: 52.4M, False: 0]
  ------------------
 1153|  52.4M|                    ppd = (*ppline++ << 8);
 1154|  52.4M|                    if (GBW > 8)
  ------------------
  |  Branch (1154:25): [True: 3.14M, False: 49.3M]
  ------------------
 1155|  3.14M|                        ppd |= *ppline++;
 1156|  52.4M|                }
 1157|  52.4M|            }
 1158|  1.56G|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1158:25): [True: 1.51G, False: 52.4M]
  ------------------
 1159|  1.51G|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1159:21): [True: 0, False: 1.51G]
  |  Branch (1159:40): [True: 0, False: 0]
  ------------------
 1160|      0|                    bit = 0;
 1161|  1.51G|                } else {
 1162|  1.51G|                    CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
 1163|  1.51G|                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
 1164|  1.51G|                    CONTEXT |= (pd>>9) & 0x01F0; /* next 5 pixels */
 1165|  1.51G|                    CONTEXT |= (ppd>>4) & 0x1E00; /* next 4 pixels */
 1166|  1.51G|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1167|  1.51G|                    if (bit < 0)
  ------------------
  |  Branch (1167:25): [True: 0, False: 1.51G]
  ------------------
 1168|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON2");
 1169|  1.51G|                }
 1170|  1.51G|                pd = pd<<1;
 1171|  1.51G|                ppd = ppd<<1;
 1172|  1.51G|                out_byte = (out_byte<<1) | bit;
 1173|  1.51G|                out_bits_to_go_in_byte--;
 1174|  1.51G|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1175|  1.51G|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1175:21): [True: 165M, False: 1.34G]
  ------------------
 1176|   165M|                    out_bits_to_go_in_byte = 8;
 1177|   165M|                    d++;
 1178|   165M|                    if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (1178:25): [True: 160M, False: 5.38M]
  |  Branch (1178:38): [True: 141M, False: 18.9M]
  |  Branch (1178:47): [True: 141M, False: 0]
  ------------------
 1179|   141M|                        pd |= *pline++;
 1180|   141M|                        if (y > 1 && ppline)
  ------------------
  |  Branch (1180:29): [True: 101M, False: 39.7M]
  |  Branch (1180:38): [True: 101M, False: 0]
  ------------------
 1181|   101M|                            ppd |= *ppline++;
 1182|   141M|                    }
 1183|   165M|                }
 1184|  1.51G|            }
 1185|  52.4M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1185:17): [True: 50.2M, False: 2.23M]
  ------------------
 1186|  50.2M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1187|  54.0M|        } else {
 1188|  54.0M|            copy_prev_row(image, y);
 1189|  54.0M|        }
 1190|   106M|    }
 1191|       |
 1192|  1.62k|    return 0;
 1193|  1.62k|}
jbig2_generic.c:jbig2_decode_generic_template2_TPGDON:
 1199|  4.97k|{
 1200|  4.97k|    const int64_t GBW = image->width;
 1201|  4.97k|    const int64_t GBH = image->height;
 1202|  4.97k|    uint32_t CONTEXT;
 1203|  4.97k|    int64_t x, y;
 1204|  4.97k|    int LTP = 0;
 1205|       |
 1206|  4.97k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  4.97k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 4.97k]
  |  |  |  Branch (440:20): [True: 3, False: 4.97k]
  |  |  |  Branch (440:31): [True: 0, False: 4.97k]
  |  |  |  Branch (440:46): [True: 4.58k, False: 387]
  |  |  |  Branch (440:57): [True: 0, False: 4.58k]
  |  |  |  Branch (440:72): [True: 387, False: 4.58k]
  |  |  |  Branch (440:84): [True: 4, False: 383]
  |  |  ------------------
  ------------------
 1207|      7|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 1208|      7|                           "adaptive template pixel is out of field");
 1209|       |
 1210|  79.3M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1210:17): [True: 79.3M, False: 4.96k]
  ------------------
 1211|  79.3M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0xE5]);
 1212|  79.3M|        if (bit < 0)
  ------------------
  |  Branch (1212:13): [True: 0, False: 79.3M]
  ------------------
 1213|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON1");
 1214|  79.3M|        LTP ^= bit;
 1215|  79.3M|        if (!LTP) {
  ------------------
  |  Branch (1215:13): [True: 28.7M, False: 50.5M]
  ------------------
 1216|  28.7M|            uint32_t out_byte = 0;
 1217|  28.7M|            int out_bits_to_go_in_byte = 8;
 1218|  28.7M|            uint8_t *d = &image->data[image->stride * y];
 1219|  28.7M|            uint8_t *pline  =  y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1219:32): [True: 28.7M, False: 4.66k]
  ------------------
 1220|  28.7M|            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
  ------------------
  |  Branch (1220:31): [True: 28.7M, False: 9.60k]
  ------------------
 1221|  28.7M|            uint32_t pd = 0;
 1222|  28.7M|            uint32_t ppd = 0;
 1223|  28.7M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1223:17): [True: 28.7M, False: 4.66k]
  |  Branch (1223:26): [True: 28.7M, False: 0]
  ------------------
 1224|  28.7M|                pd = (*pline++ << 8);
 1225|  28.7M|                if (GBW > 8)
  ------------------
  |  Branch (1225:21): [True: 1.18M, False: 27.6M]
  ------------------
 1226|  1.18M|                    pd |= *pline++;
 1227|  28.7M|                if (y > 1 && ppline) {
  ------------------
  |  Branch (1227:21): [True: 28.7M, False: 4.93k]
  |  Branch (1227:30): [True: 28.7M, False: 0]
  ------------------
 1228|  28.7M|                    ppd = (*ppline++ << 8);
 1229|  28.7M|                    if (GBW > 8)
  ------------------
  |  Branch (1229:25): [True: 1.18M, False: 27.6M]
  ------------------
 1230|  1.18M|                        ppd |= *ppline++;
 1231|  28.7M|                }
 1232|  28.7M|            }
 1233|   379M|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1233:25): [True: 350M, False: 28.7M]
  ------------------
 1234|   350M|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1234:21): [True: 0, False: 350M]
  |  Branch (1234:40): [True: 0, False: 0]
  ------------------
 1235|      0|                    bit = 0;
 1236|   350M|                } else {
 1237|   350M|                    CONTEXT  = out_byte & 0x003; /* First 2 pixels */
 1238|   350M|                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
 1239|   350M|                    CONTEXT |= (pd>>11) & 0x078; /* next 4 pixels */
 1240|   350M|                    CONTEXT |= (ppd>>7) & 0x380; /* next 3 pixels */
 1241|   350M|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1242|   350M|                    if (bit < 0)
  ------------------
  |  Branch (1242:25): [True: 0, False: 350M]
  ------------------
 1243|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON2");
 1244|   350M|                }
 1245|   350M|                pd = pd<<1;
 1246|   350M|                ppd = ppd<<1;
 1247|   350M|                out_byte = (out_byte<<1) | bit;
 1248|   350M|                out_bits_to_go_in_byte--;
 1249|   350M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1250|   350M|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1250:21): [True: 39.8M, False: 310M]
  ------------------
 1251|  39.8M|                    out_bits_to_go_in_byte = 8;
 1252|  39.8M|                    d++;
 1253|  39.8M|                    if (x+9 < GBW && y > 0 && pline) {
  ------------------
  |  Branch (1253:25): [True: 37.0M, False: 2.73M]
  |  Branch (1253:38): [True: 30.5M, False: 6.59M]
  |  Branch (1253:47): [True: 30.5M, False: 0]
  ------------------
 1254|  30.5M|                        pd |= *pline++;
 1255|  30.5M|                        if (y > 1 && ppline)
  ------------------
  |  Branch (1255:29): [True: 27.1M, False: 3.31M]
  |  Branch (1255:38): [True: 27.1M, False: 0]
  ------------------
 1256|  27.1M|                            ppd |= *ppline++;
 1257|  30.5M|                    }
 1258|  39.8M|                }
 1259|   350M|            }
 1260|  28.7M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1260:17): [True: 27.2M, False: 1.54M]
  ------------------
 1261|  27.2M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1262|  50.5M|        } else {
 1263|  50.5M|            copy_prev_row(image, y);
 1264|  50.5M|        }
 1265|  79.3M|    }
 1266|       |
 1267|  4.96k|    return 0;
 1268|  4.96k|}
jbig2_generic.c:jbig2_decode_generic_template3_TPGDON:
 1274|    746|{
 1275|    746|    const int64_t GBW = image->width;
 1276|    746|    const int64_t GBH = image->height;
 1277|    746|    uint32_t CONTEXT;
 1278|    746|    int64_t x, y;
 1279|    746|    int LTP = 0;
 1280|       |
 1281|    746|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|    746|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 746]
  |  |  |  Branch (440:20): [True: 2, False: 744]
  |  |  |  Branch (440:31): [True: 0, False: 744]
  |  |  |  Branch (440:46): [True: 502, False: 242]
  |  |  |  Branch (440:57): [True: 0, False: 502]
  |  |  |  Branch (440:72): [True: 242, False: 502]
  |  |  |  Branch (440:84): [True: 3, False: 239]
  |  |  ------------------
  ------------------
 1282|      5|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
 1283|      5|                           "adaptive template pixel is out of field");
 1284|       |
 1285|   185M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (1285:17): [True: 185M, False: 741]
  ------------------
 1286|   185M|        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0195]);
 1287|   185M|        if (bit < 0)
  ------------------
  |  Branch (1287:13): [True: 0, False: 185M]
  ------------------
 1288|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON1");
 1289|   185M|        LTP ^= bit;
 1290|   185M|        if (!LTP) {
  ------------------
  |  Branch (1290:13): [True: 125M, False: 59.3M]
  ------------------
 1291|   125M|            uint32_t out_byte = 0;
 1292|   125M|            int out_bits_to_go_in_byte = 8;
 1293|   125M|            uint8_t *d = &image->data[image->stride * y];
 1294|   125M|            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
  ------------------
  |  Branch (1294:31): [True: 125M, False: 238]
  ------------------
 1295|   125M|            uint32_t pd = 0;
 1296|   125M|            if (y > 0 && pline) {
  ------------------
  |  Branch (1296:17): [True: 125M, False: 238]
  |  Branch (1296:26): [True: 125M, False: 0]
  ------------------
 1297|   125M|                pd = (*pline++ << 8);
 1298|   125M|                if (GBW > 8)
  ------------------
  |  Branch (1298:21): [True: 475k, False: 125M]
  ------------------
 1299|   475k|                    pd |= *pline++;
 1300|   125M|            }
 1301|  1.13G|            for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (1301:25): [True: 1.01G, False: 125M]
  ------------------
 1302|  1.01G|                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (1302:21): [True: 0, False: 1.01G]
  |  Branch (1302:40): [True: 0, False: 0]
  ------------------
 1303|      0|                    bit = 0;
 1304|  1.01G|                } else {
 1305|  1.01G|                    CONTEXT  = out_byte & 0x0F; /* First 4 pixels */
 1306|  1.01G|                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
 1307|  1.01G|                    CONTEXT |= (pd>>9) & 0x3E0; /* next 5 pixels */
 1308|  1.01G|                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
 1309|  1.01G|                    if (bit < 0)
  ------------------
  |  Branch (1309:25): [True: 0, False: 1.01G]
  ------------------
 1310|      0|                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON2");
 1311|  1.01G|                }
 1312|  1.01G|                pd = pd<<1;
 1313|  1.01G|                out_byte = (out_byte<<1) | bit;
 1314|  1.01G|                out_bits_to_go_in_byte--;
 1315|  1.01G|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1316|  1.01G|                if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (1316:21): [True: 96.2M, False: 914M]
  ------------------
 1317|  96.2M|                    out_bits_to_go_in_byte = 8;
 1318|  96.2M|                    d++;
 1319|  96.2M|                    if (x+9 < GBW && y > 0 && pline)
  ------------------
  |  Branch (1319:25): [True: 95.3M, False: 959k]
  |  Branch (1319:38): [True: 62.3M, False: 32.9M]
  |  Branch (1319:47): [True: 62.3M, False: 0]
  ------------------
 1320|  62.3M|                        pd |= *pline++;
 1321|  96.2M|                }
 1322|  1.01G|            }
 1323|   125M|            if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (1323:17): [True: 125M, False: 483k]
  ------------------
 1324|   125M|                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
 1325|   125M|        } else {
 1326|  59.3M|            copy_prev_row(image, y);
 1327|  59.3M|        }
 1328|   185M|    }
 1329|       |
 1330|    741|    return 0;
 1331|    741|}
jbig2_generic.c:jbig2_decode_generic_template0:
  373|  6.92k|{
  374|  6.92k|    const int64_t GBW = image->width;
  375|  6.92k|    const int64_t GBH = image->height;
  376|  6.92k|    const uint32_t rowstride = image->stride;
  377|  6.92k|    int64_t x, y;
  378|  6.92k|    byte *line2 = NULL;
  379|  6.92k|    byte *line1 = NULL;
  380|  6.92k|    byte *gbreg_line = (byte *) image->data;
  381|       |
  382|  6.92k|    (void) ctx;
  383|  6.92k|    (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|  6.92k|    if (GBW <= 0)
  ------------------
  |  Branch (391:9): [True: 0, False: 6.92k]
  ------------------
  392|      0|        return 0;
  393|       |
  394|   148M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (394:17): [True: 148M, False: 6.92k]
  ------------------
  395|   148M|        uint32_t CONTEXT;
  396|   148M|        uint32_t line_m1;
  397|   148M|        uint32_t line_m2;
  398|   148M|        int64_t padded_width = (GBW + 7) & -8;
  399|       |
  400|   148M|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (400:19): [True: 148M, False: 6.92k]
  ------------------
  401|   148M|        line_m2 = line2 ? line2[0] << 6 : 0;
  ------------------
  |  Branch (401:19): [True: 148M, False: 13.7k]
  ------------------
  402|   148M|        CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800);
  403|       |
  404|       |        /* 6.2.5.7 3d */
  405|   374M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (405:21): [True: 225M, False: 148M]
  ------------------
  406|   225M|            byte result = 0;
  407|   225M|            int64_t x_minor;
  408|   225M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (408:35): [True: 77.2M, False: 148M]
  ------------------
  409|       |
  410|   225M|            if (line1)
  ------------------
  |  Branch (410:17): [True: 199M, False: 25.8M]
  ------------------
  411|   199M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (411:45): [True: 51.3M, False: 148M]
  ------------------
  412|       |
  413|   225M|            if (line2)
  ------------------
  |  Branch (413:17): [True: 186M, False: 38.7M]
  ------------------
  414|   186M|                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 6 : 0);
  ------------------
  |  Branch (414:45): [True: 38.4M, False: 148M]
  ------------------
  415|       |
  416|       |            /* This is the speed-critical inner loop. */
  417|  1.35G|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (417:31): [True: 1.12G, False: 225M]
  ------------------
  418|  1.12G|                int bit;
  419|       |
  420|  1.12G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  421|  1.12G|                if (bit < 0)
  ------------------
  |  Branch (421:21): [True: 0, False: 1.12G]
  ------------------
  422|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 optimized");
  423|  1.12G|                result |= bit << (7 - x_minor);
  424|  1.12G|                CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800);
  425|  1.12G|            }
  426|   225M|            gbreg_line[x >> 3] = result;
  427|   225M|        }
  428|       |#ifdef OUTPUT_PBM
  429|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  430|       |#endif
  431|   148M|        line2 = line1;
  432|   148M|        line1 = gbreg_line;
  433|   148M|        gbreg_line += rowstride;
  434|   148M|    }
  435|       |
  436|  6.92k|    return 0;
  437|  6.92k|}
jbig2_generic.c:jbig2_decode_generic_template0_unopt:
  446|  13.8k|{
  447|  13.8k|    const int64_t GBW = image->width;
  448|  13.8k|    const int64_t GBH = image->height;
  449|  13.8k|    uint32_t CONTEXT;
  450|  13.8k|    int64_t x, y;
  451|  13.8k|    int bit;
  452|       |
  453|  13.8k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
  ------------------
  |  |  440|  27.7k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 13.8k]
  |  |  |  Branch (440:20): [True: 4, False: 13.8k]
  |  |  |  Branch (440:31): [True: 0, False: 13.8k]
  |  |  |  Branch (440:46): [True: 13.4k, False: 470]
  |  |  |  Branch (440:57): [True: 0, False: 13.4k]
  |  |  |  Branch (440:72): [True: 470, False: 13.4k]
  |  |  |  Branch (440:84): [True: 10, False: 460]
  |  |  ------------------
  ------------------
  454|  13.8k|        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
  ------------------
  |  |  440|  27.7k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 13.8k]
  |  |  |  Branch (440:20): [True: 3, False: 13.8k]
  |  |  |  Branch (440:31): [True: 0, False: 13.8k]
  |  |  |  Branch (440:46): [True: 13.4k, False: 412]
  |  |  |  Branch (440:57): [True: 0, False: 13.4k]
  |  |  |  Branch (440:72): [True: 412, False: 13.4k]
  |  |  |  Branch (440:84): [True: 3, False: 409]
  |  |  ------------------
  ------------------
  455|  13.8k|        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
  ------------------
  |  |  440|  27.7k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 13.8k]
  |  |  |  Branch (440:20): [True: 3, False: 13.8k]
  |  |  |  Branch (440:31): [True: 0, False: 13.8k]
  |  |  |  Branch (440:46): [True: 12.6k, False: 1.21k]
  |  |  |  Branch (440:57): [True: 0, False: 12.6k]
  |  |  |  Branch (440:72): [True: 1.21k, False: 12.6k]
  |  |  |  Branch (440:84): [True: 3, False: 1.21k]
  |  |  ------------------
  ------------------
  456|  13.8k|        pixel_outside_field(params->gbat[6], params->gbat[7]))
  ------------------
  |  |  440|  13.8k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 13.8k]
  |  |  |  Branch (440:20): [True: 4, False: 13.8k]
  |  |  |  Branch (440:31): [True: 0, False: 13.8k]
  |  |  |  Branch (440:46): [True: 11.7k, False: 2.07k]
  |  |  |  Branch (440:57): [True: 0, False: 11.7k]
  |  |  |  Branch (440:72): [True: 2.07k, False: 11.7k]
  |  |  |  Branch (440:84): [True: 1, False: 2.07k]
  |  |  ------------------
  ------------------
  457|     31|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  458|     31|                           "adaptive template pixel is out of field");
  459|       |
  460|   132M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (460:17): [True: 132M, False: 13.8k]
  ------------------
  461|   132M|        uint32_t out_byte = 0;
  462|   132M|        int out_bits_to_go_in_byte = 8;
  463|   132M|        uint8_t *d = &image->data[image->stride * y];
  464|   132M|        uint32_t pd = 0;
  465|   132M|        uint32_t ppd = 0;
  466|   132M|        uint8_t *pline = NULL;
  467|   132M|        uint8_t *ppline = NULL;
  468|   132M|        if (y >= 1)
  ------------------
  |  Branch (468:13): [True: 132M, False: 13.8k]
  ------------------
  469|   132M|        {
  470|   132M|            pline  = &image->data[image->stride * (y-1)];
  471|   132M|            pd = (*pline++ << 8);
  472|   132M|            if (GBW > 8)
  ------------------
  |  Branch (472:17): [True: 779k, False: 131M]
  ------------------
  473|   779k|                pd |= *pline++;
  474|   132M|        }
  475|   132M|        if (y >= 2) {
  ------------------
  |  Branch (475:13): [True: 132M, False: 24.5k]
  ------------------
  476|   132M|            ppline = &image->data[image->stride * (y-2)];
  477|   132M|            ppd = (*ppline++ << 8);
  478|   132M|            if (GBW > 8)
  ------------------
  |  Branch (478:17): [True: 768k, False: 131M]
  ------------------
  479|   768k|                ppd |= *ppline++;
  480|   132M|        }
  481|  1.03G|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (481:21): [True: 903M, False: 132M]
  ------------------
  482|   903M|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (482:17): [True: 173M, False: 729M]
  |  Branch (482:36): [True: 172M, False: 726k]
  ------------------
  483|   172M|                bit = 0;
  484|   730M|            } else {
  485|   730M|                CONTEXT  = out_byte & 0x000F; /* First 4 pixels */
  486|   730M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
  487|   730M|                CONTEXT |= (pd>>8) & 0x03E0; /* Next 5 pixels */
  488|   730M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
  489|   730M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
  490|   730M|                CONTEXT |= (ppd>>2) & 0x7000; /* Next 3 pixels */
  491|   730M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
  492|   730M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  493|   730M|                if (bit < 0)
  ------------------
  |  Branch (493:21): [True: 0, False: 730M]
  ------------------
  494|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 unoptimized");
  495|   730M|            }
  496|   903M|            pd = pd<<1;
  497|   903M|            ppd = ppd<<1;
  498|   903M|            out_byte = (out_byte<<1) | bit;
  499|   903M|            out_bits_to_go_in_byte--;
  500|   903M|            *d = out_byte<<out_bits_to_go_in_byte;
  501|   903M|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (501:17): [True: 80.5M, False: 822M]
  ------------------
  502|  80.5M|                out_bits_to_go_in_byte = 8;
  503|  80.5M|                d++;
  504|  80.5M|                if (x+9 < GBW && pline != NULL) {
  ------------------
  |  Branch (504:21): [True: 77.4M, False: 3.09M]
  |  Branch (504:34): [True: 38.9M, False: 38.5M]
  ------------------
  505|  38.9M|                    pd |= *pline++;
  506|  38.9M|                    if (ppline != NULL)
  ------------------
  |  Branch (506:25): [True: 33.2M, False: 5.70M]
  ------------------
  507|  33.2M|                        ppd |= *ppline++;
  508|  38.9M|                }
  509|  80.5M|            }
  510|   903M|        }
  511|   132M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (511:13): [True: 130M, False: 2.29M]
  ------------------
  512|   130M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  513|   132M|    }
  514|  13.8k|    return 0;
  515|  13.8k|}
jbig2_generic.c:jbig2_decode_generic_template1:
  589|  5.43k|{
  590|  5.43k|    const int64_t GBW = image->width;
  591|  5.43k|    const int64_t GBH = image->height;
  592|  5.43k|    const uint32_t rowstride = image->stride;
  593|  5.43k|    int64_t x, y;
  594|  5.43k|    byte *line2 = NULL;
  595|  5.43k|    byte *line1 = NULL;
  596|  5.43k|    byte *gbreg_line = (byte *) image->data;
  597|       |
  598|  5.43k|    (void) params;
  599|       |
  600|       |#ifdef OUTPUT_PBM
  601|       |    printf("P4\n%u %u\n",
  602|       |        (uint32_t) GBW,
  603|       |        (uint32_t) GBH);
  604|       |#endif
  605|       |
  606|  5.43k|    if (GBW <= 0)
  ------------------
  |  Branch (606:9): [True: 0, False: 5.43k]
  ------------------
  607|      0|        return 0;
  608|       |
  609|  98.4M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (609:17): [True: 98.4M, False: 5.43k]
  ------------------
  610|  98.4M|        uint32_t CONTEXT;
  611|  98.4M|        uint32_t line_m1;
  612|  98.4M|        uint32_t line_m2;
  613|  98.4M|        int64_t padded_width = (GBW + 7) & -8;
  614|       |
  615|  98.4M|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (615:19): [True: 98.4M, False: 5.43k]
  ------------------
  616|  98.4M|        line_m2 = line2 ? line2[0] << 5 : 0;
  ------------------
  |  Branch (616:19): [True: 98.4M, False: 10.6k]
  ------------------
  617|  98.4M|        CONTEXT = ((line_m1 >> 1) & 0x1f8) | ((line_m2 >> 1) & 0x1e00);
  618|       |
  619|       |        /* 6.2.5.7 3d */
  620|   325M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (620:21): [True: 226M, False: 98.4M]
  ------------------
  621|   226M|            byte result = 0;
  622|   226M|            int64_t x_minor;
  623|   226M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (623:35): [True: 128M, False: 98.4M]
  ------------------
  624|       |
  625|   226M|            if (line1)
  ------------------
  |  Branch (625:17): [True: 195M, False: 31.7M]
  ------------------
  626|   195M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (626:45): [True: 96.6M, False: 98.4M]
  ------------------
  627|       |
  628|   226M|            if (line2)
  ------------------
  |  Branch (628:17): [True: 180M, False: 46.7M]
  ------------------
  629|   180M|                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 5 : 0);
  ------------------
  |  Branch (629:45): [True: 81.6M, False: 98.4M]
  ------------------
  630|       |
  631|       |            /* This is the speed-critical inner loop. */
  632|  1.37G|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (632:31): [True: 1.14G, False: 226M]
  ------------------
  633|  1.14G|                int bit;
  634|       |
  635|  1.14G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  636|  1.14G|                if (bit < 0)
  ------------------
  |  Branch (636:21): [True: 0, False: 1.14G]
  ------------------
  637|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 optimized");
  638|  1.14G|                result |= bit << (7 - x_minor);
  639|  1.14G|                CONTEXT = ((CONTEXT & 0xefb) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x8) | ((line_m2 >> (8 - x_minor)) & 0x200);
  640|  1.14G|            }
  641|   226M|            gbreg_line[x >> 3] = result;
  642|   226M|        }
  643|       |#ifdef OUTPUT_PBM
  644|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  645|       |#endif
  646|  98.4M|        line2 = line1;
  647|  98.4M|        line1 = gbreg_line;
  648|  98.4M|        gbreg_line += rowstride;
  649|  98.4M|    }
  650|       |
  651|  5.43k|    return 0;
  652|  5.43k|}
jbig2_generic.c:jbig2_decode_generic_template1_unopt:
  521|  5.73k|{
  522|  5.73k|    const int64_t GBW = image->width;
  523|  5.73k|    const int64_t GBH = image->height;
  524|  5.73k|    uint32_t CONTEXT;
  525|  5.73k|    int64_t x, y;
  526|  5.73k|    int bit;
  527|       |
  528|  5.73k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  5.73k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 5.73k]
  |  |  |  Branch (440:20): [True: 3, False: 5.73k]
  |  |  |  Branch (440:31): [True: 0, False: 5.73k]
  |  |  |  Branch (440:46): [True: 4.70k, False: 1.02k]
  |  |  |  Branch (440:57): [True: 0, False: 4.70k]
  |  |  |  Branch (440:72): [True: 1.02k, False: 4.70k]
  |  |  |  Branch (440:84): [True: 5, False: 1.02k]
  |  |  ------------------
  ------------------
  529|      8|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  530|      8|                           "adaptive template pixel is out of field");
  531|       |
  532|   180M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (532:17): [True: 180M, False: 5.72k]
  ------------------
  533|   180M|        uint32_t out_byte = 0;
  534|   180M|        int out_bits_to_go_in_byte = 8;
  535|   180M|        uint8_t *d = &image->data[image->stride * y];
  536|   180M|        uint32_t pd = 0;
  537|   180M|        uint32_t ppd = 0;
  538|   180M|        uint8_t *pline = NULL;
  539|   180M|        uint8_t *ppline = NULL;
  540|   180M|        if (y >= 1) {
  ------------------
  |  Branch (540:13): [True: 180M, False: 5.72k]
  ------------------
  541|   180M|            pline  = &image->data[image->stride * (y-1)];
  542|   180M|            pd = (*pline++ << 8);
  543|   180M|            if (GBW > 8)
  ------------------
  |  Branch (543:17): [True: 697k, False: 179M]
  ------------------
  544|   697k|                pd |= *pline++;
  545|   180M|        }
  546|   180M|        if (y >= 2) {
  ------------------
  |  Branch (546:13): [True: 180M, False: 8.86k]
  ------------------
  547|   180M|            ppline = &image->data[image->stride * (y-2)];
  548|   180M|            ppd = (*ppline++ << 8);
  549|   180M|            if (GBW > 8)
  ------------------
  |  Branch (549:17): [True: 694k, False: 179M]
  ------------------
  550|   694k|                ppd |= *ppline++;
  551|   180M|        }
  552|  1.39G|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (552:21): [True: 1.21G, False: 180M]
  ------------------
  553|  1.21G|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (553:17): [True: 30.9M, False: 1.18G]
  |  Branch (553:36): [True: 30.9M, False: 17.0k]
  ------------------
  554|  30.9M|                bit = 0;
  555|  1.18G|            } else {
  556|  1.18G|                CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
  557|  1.18G|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
  558|  1.18G|                CONTEXT |= (pd>>9) & 0x01F0; /* Next 5 pixels */
  559|  1.18G|                CONTEXT |= (ppd>>4) & 0x1E00; /* Next 4 pixels */
  560|  1.18G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  561|  1.18G|                if (bit < 0)
  ------------------
  |  Branch (561:21): [True: 0, False: 1.18G]
  ------------------
  562|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 unoptimized");
  563|  1.18G|            }
  564|  1.21G|            pd = pd<<1;
  565|  1.21G|            ppd = ppd<<1;
  566|  1.21G|            out_byte = (out_byte<<1) | bit;
  567|  1.21G|            out_bits_to_go_in_byte--;
  568|  1.21G|            *d = out_byte<<out_bits_to_go_in_byte;
  569|  1.21G|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (569:17): [True: 126M, False: 1.09G]
  ------------------
  570|   126M|                out_bits_to_go_in_byte = 8;
  571|   126M|                d++;
  572|   126M|                if (x+9 < GBW && pline != NULL) {
  ------------------
  |  Branch (572:21): [True: 113M, False: 13.3M]
  |  Branch (572:34): [True: 46.0M, False: 67.4M]
  ------------------
  573|  46.0M|                    pd |= *pline++;
  574|  46.0M|                    if (ppline != NULL)
  ------------------
  |  Branch (574:25): [True: 37.5M, False: 8.50M]
  ------------------
  575|  37.5M|                        ppd |= *ppline++;
  576|  46.0M|                }
  577|   126M|            }
  578|  1.21G|        }
  579|   180M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (579:13): [True: 167M, False: 12.6M]
  ------------------
  580|   167M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  581|   180M|    }
  582|  5.72k|    return 0;
  583|  5.72k|}
jbig2_generic.c:jbig2_decode_generic_template2:
  725|  82.3k|{
  726|  82.3k|    const int64_t GBW = image->width;
  727|  82.3k|    const int64_t GBH = image->height;
  728|  82.3k|    const uint32_t rowstride = image->stride;
  729|  82.3k|    int64_t x, y;
  730|  82.3k|    byte *line2 = NULL;
  731|  82.3k|    byte *line1 = NULL;
  732|  82.3k|    byte *gbreg_line = (byte *) image->data;
  733|       |
  734|  82.3k|    (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|  82.3k|    if (GBW <= 0)
  ------------------
  |  Branch (742:9): [True: 0, False: 82.3k]
  ------------------
  743|      0|        return 0;
  744|       |
  745|   132M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (745:17): [True: 131M, False: 82.3k]
  ------------------
  746|   131M|        uint32_t CONTEXT;
  747|   131M|        uint32_t line_m1;
  748|   131M|        uint32_t line_m2;
  749|   131M|        int64_t padded_width = (GBW + 7) & -8;
  750|       |
  751|   131M|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (751:19): [True: 131M, False: 82.3k]
  ------------------
  752|   131M|        line_m2 = line2 ? line2[0] << 4 : 0;
  ------------------
  |  Branch (752:19): [True: 131M, False: 94.9k]
  ------------------
  753|   131M|        CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380);
  754|       |
  755|       |        /* 6.2.5.7 3d */
  756|   384M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (756:21): [True: 252M, False: 131M]
  ------------------
  757|   252M|            byte result = 0;
  758|   252M|            int64_t x_minor;
  759|   252M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (759:35): [True: 120M, False: 131M]
  ------------------
  760|       |
  761|   252M|            if (line1)
  ------------------
  |  Branch (761:17): [True: 190M, False: 62.1M]
  ------------------
  762|   190M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (762:45): [True: 58.3M, False: 131M]
  ------------------
  763|       |
  764|   252M|            if (line2)
  ------------------
  |  Branch (764:17): [True: 160M, False: 92.2M]
  ------------------
  765|   160M|                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 4 : 0);
  ------------------
  |  Branch (765:45): [True: 28.2M, False: 131M]
  ------------------
  766|       |
  767|       |            /* This is the speed-critical inner loop. */
  768|  1.38G|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (768:31): [True: 1.13G, False: 252M]
  ------------------
  769|  1.13G|                int bit;
  770|       |
  771|  1.13G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  772|  1.13G|                if (bit < 0)
  ------------------
  |  Branch (772:21): [True: 0, False: 1.13G]
  ------------------
  773|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 optimized");
  774|  1.13G|                result |= bit << (7 - x_minor);
  775|  1.13G|                CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80);
  776|  1.13G|            }
  777|   252M|            gbreg_line[x >> 3] = result;
  778|   252M|        }
  779|       |#ifdef OUTPUT_PBM
  780|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  781|       |#endif
  782|   131M|        line2 = line1;
  783|   131M|        line1 = gbreg_line;
  784|   131M|        gbreg_line += rowstride;
  785|   131M|    }
  786|       |
  787|  82.3k|    return 0;
  788|  82.3k|}
jbig2_generic.c:jbig2_decode_generic_template2_unopt:
  658|  38.3k|{
  659|  38.3k|    const int64_t GBW = image->width;
  660|  38.3k|    const int64_t GBH = image->height;
  661|  38.3k|    uint32_t CONTEXT;
  662|  38.3k|    int64_t x, y;
  663|  38.3k|    int bit;
  664|       |
  665|  38.3k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  38.3k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 38.3k]
  |  |  |  Branch (440:20): [True: 2, False: 38.3k]
  |  |  |  Branch (440:31): [True: 0, False: 38.3k]
  |  |  |  Branch (440:46): [True: 37.3k, False: 1.01k]
  |  |  |  Branch (440:57): [True: 0, False: 37.3k]
  |  |  |  Branch (440:72): [True: 1.01k, False: 37.3k]
  |  |  |  Branch (440:84): [True: 5, False: 1.01k]
  |  |  ------------------
  ------------------
  666|      7|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  667|      7|                           "adaptive template pixel is out of field");
  668|       |
  669|   199M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (669:17): [True: 199M, False: 38.3k]
  ------------------
  670|   199M|        uint32_t out_byte = 0;
  671|   199M|        int out_bits_to_go_in_byte = 8;
  672|   199M|        uint8_t *d = &image->data[image->stride * y];
  673|   199M|        uint8_t *pline  = &image->data[image->stride * (y-1)];
  674|   199M|        uint8_t *ppline = &image->data[image->stride * (y-2)];
  675|   199M|        uint32_t pd = 0;
  676|   199M|        uint32_t ppd = 0;
  677|   199M|        if (y > 0) {
  ------------------
  |  Branch (677:13): [True: 199M, False: 38.3k]
  ------------------
  678|   199M|            pd = (*pline++ << 8);
  679|   199M|            if (GBW > 8)
  ------------------
  |  Branch (679:17): [True: 793k, False: 198M]
  ------------------
  680|   793k|                pd |= *pline++;
  681|   199M|            if (y > 1) {
  ------------------
  |  Branch (681:17): [True: 199M, False: 38.3k]
  ------------------
  682|   199M|                ppd = (*ppline++ << 8);
  683|   199M|                if (GBW > 8)
  ------------------
  |  Branch (683:21): [True: 755k, False: 198M]
  ------------------
  684|   755k|                    ppd |= *ppline++;
  685|   199M|            }
  686|   199M|        }
  687|  1.84G|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (687:21): [True: 1.64G, False: 199M]
  ------------------
  688|  1.64G|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (688:17): [True: 107M, False: 1.53G]
  |  Branch (688:36): [True: 107M, False: 48.1k]
  ------------------
  689|   107M|                bit = 0;
  690|  1.53G|            } else {
  691|  1.53G|                CONTEXT  = out_byte & 0x003; /* First 2 pixels */
  692|  1.53G|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
  693|  1.53G|                CONTEXT |= (pd>>11) & 0x078; /* Next 4 pixels */
  694|  1.53G|                CONTEXT |= (ppd>>7) & 0x380; /* Next 3 pixels */
  695|  1.53G|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  696|  1.53G|                if (bit < 0)
  ------------------
  |  Branch (696:21): [True: 0, False: 1.53G]
  ------------------
  697|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 unoptimized");
  698|  1.53G|            }
  699|  1.64G|            pd = pd<<1;
  700|  1.64G|            ppd = ppd<<1;
  701|  1.64G|            out_byte = (out_byte<<1) | bit;
  702|  1.64G|            out_bits_to_go_in_byte--;
  703|  1.64G|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  704|  1.64G|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (704:17): [True: 162M, False: 1.48G]
  ------------------
  705|   162M|                out_bits_to_go_in_byte = 8;
  706|   162M|                d++;
  707|   162M|                if (x+9 < GBW && y > 0) {
  ------------------
  |  Branch (707:21): [True: 161M, False: 1.15M]
  |  Branch (707:34): [True: 92.2M, False: 69.1M]
  ------------------
  708|  92.2M|                    pd |= *pline++;
  709|  92.2M|                    if (y > 1)
  ------------------
  |  Branch (709:25): [True: 68.3M, False: 23.9M]
  ------------------
  710|  68.3M|                        ppd |= *ppline++;
  711|  92.2M|                }
  712|   162M|            }
  713|  1.64G|        }
  714|   199M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (714:13): [True: 199M, False: 324k]
  ------------------
  715|   199M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  716|   199M|    }
  717|       |
  718|  38.3k|    return 0;
  719|  38.3k|}
jbig2_generic.c:jbig2_decode_generic_template3:
  794|  4.67k|{
  795|  4.67k|    const int64_t GBW = image->width;
  796|  4.67k|    const int64_t GBH = image->height;
  797|  4.67k|    const uint32_t rowstride = image->stride;
  798|  4.67k|    byte *line1 = NULL;
  799|  4.67k|    byte *gbreg_line = (byte *) image->data;
  800|  4.67k|    int64_t x, y;
  801|       |
  802|  4.67k|    (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.67k|    if (GBW <= 0)
  ------------------
  |  Branch (810:9): [True: 0, False: 4.67k]
  ------------------
  811|      0|        return 0;
  812|       |
  813|  78.2k|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (813:17): [True: 73.5k, False: 4.67k]
  ------------------
  814|  73.5k|        uint32_t CONTEXT;
  815|  73.5k|        uint32_t line_m1;
  816|  73.5k|        int64_t padded_width = (GBW + 7) & -8;
  817|       |
  818|  73.5k|        line_m1 = line1 ? line1[0] : 0;
  ------------------
  |  Branch (818:19): [True: 68.8k, False: 4.67k]
  ------------------
  819|  73.5k|        CONTEXT = (line_m1 >> 1) & 0x3f0;
  820|       |
  821|       |        /* 6.2.5.7 3d */
  822|  92.2M|        for (x = 0; x < padded_width; x += 8) {
  ------------------
  |  Branch (822:21): [True: 92.2M, False: 73.5k]
  ------------------
  823|  92.2M|            byte result = 0;
  824|  92.2M|            int64_t x_minor;
  825|  92.2M|            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
  ------------------
  |  Branch (825:35): [True: 92.1M, False: 73.5k]
  ------------------
  826|       |
  827|  92.2M|            if (line1)
  ------------------
  |  Branch (827:17): [True: 72.4M, False: 19.7M]
  ------------------
  828|  72.4M|                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
  ------------------
  |  Branch (828:45): [True: 72.3M, False: 68.8k]
  ------------------
  829|       |
  830|       |            /* This is the speed-critical inner loop. */
  831|   829M|            for (x_minor = 0; x_minor < minor_width; x_minor++) {
  ------------------
  |  Branch (831:31): [True: 737M, False: 92.2M]
  ------------------
  832|   737M|                int bit;
  833|       |
  834|   737M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  835|   737M|                if (bit < 0)
  ------------------
  |  Branch (835:21): [True: 0, False: 737M]
  ------------------
  836|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 optimized");
  837|   737M|                result |= bit << (7 - x_minor);
  838|   737M|                CONTEXT = ((CONTEXT & 0x1f7) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x10);
  839|   737M|            }
  840|  92.2M|            gbreg_line[x >> 3] = result;
  841|  92.2M|        }
  842|       |#ifdef OUTPUT_PBM
  843|       |        fwrite(gbreg_line, 1, rowstride, stdout);
  844|       |#endif
  845|  73.5k|        line1 = gbreg_line;
  846|  73.5k|        gbreg_line += rowstride;
  847|  73.5k|    }
  848|       |
  849|  4.67k|    return 0;
  850|  4.67k|}
jbig2_generic.c:jbig2_decode_generic_template3_unopt:
  856|  6.04k|{
  857|  6.04k|    const int64_t GBW = image->width;
  858|  6.04k|    const int64_t GBH = image->height;
  859|  6.04k|    uint32_t CONTEXT;
  860|  6.04k|    int64_t x, y;
  861|  6.04k|    int bit;
  862|       |
  863|  6.04k|    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
  ------------------
  |  |  440|  6.04k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (440:6): [True: 0, False: 6.04k]
  |  |  |  Branch (440:20): [True: 2, False: 6.03k]
  |  |  |  Branch (440:31): [True: 0, False: 6.03k]
  |  |  |  Branch (440:46): [True: 2.88k, False: 3.15k]
  |  |  |  Branch (440:57): [True: 0, False: 2.88k]
  |  |  |  Branch (440:72): [True: 3.15k, False: 2.88k]
  |  |  |  Branch (440:84): [True: 4, False: 3.14k]
  |  |  ------------------
  ------------------
  864|      6|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  865|      6|                           "adaptive template pixel is out of field");
  866|       |
  867|   107M|    for (y = 0; y < GBH; y++) {
  ------------------
  |  Branch (867:17): [True: 107M, False: 6.03k]
  ------------------
  868|   107M|        uint32_t out_byte = 0;
  869|   107M|        int out_bits_to_go_in_byte = 8;
  870|   107M|        uint8_t *d = &image->data[image->stride * y];
  871|   107M|        uint8_t *pline  = &image->data[image->stride * (y-1)];
  872|   107M|        uint32_t pd = 0;
  873|   107M|        if (y > 0) {
  ------------------
  |  Branch (873:13): [True: 107M, False: 6.03k]
  ------------------
  874|   107M|            pd = (*pline++ << 8);
  875|   107M|            if (GBW > 8)
  ------------------
  |  Branch (875:17): [True: 1.92M, False: 105M]
  ------------------
  876|  1.92M|                pd |= *pline++;
  877|   107M|        }
  878|  1.08G|        for (x = 0; x < GBW; x++) {
  ------------------
  |  Branch (878:21): [True: 977M, False: 107M]
  ------------------
  879|   977M|            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
  ------------------
  |  Branch (879:17): [True: 74.5M, False: 903M]
  |  Branch (879:36): [True: 74.5M, False: 22.1k]
  ------------------
  880|  74.5M|                bit = 0;
  881|   903M|            } else {
  882|   903M|                CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
  883|   903M|                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
  884|   903M|                CONTEXT |= (pd>>9) & 0x3E0; /* Next 5 pixels */
  885|   903M|                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
  886|   903M|                if (bit < 0)
  ------------------
  |  Branch (886:21): [True: 0, False: 903M]
  ------------------
  887|      0|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 unoptimized");
  888|   903M|            }
  889|   977M|            pd = pd<<1;
  890|   977M|            out_byte = (out_byte<<1) | bit;
  891|   977M|            out_bits_to_go_in_byte--;
  892|   977M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  893|   977M|            if (out_bits_to_go_in_byte == 0) {
  ------------------
  |  Branch (893:17): [True: 104M, False: 873M]
  ------------------
  894|   104M|                out_bits_to_go_in_byte = 8;
  895|   104M|                d++;
  896|   104M|                if (x+9 < GBW && y > 0)
  ------------------
  |  Branch (896:21): [True: 99.1M, False: 5.00M]
  |  Branch (896:34): [True: 42.7M, False: 56.3M]
  ------------------
  897|  42.7M|                    pd |= *pline++;
  898|   104M|            }
  899|   977M|        }
  900|   107M|        if (out_bits_to_go_in_byte != 8)
  ------------------
  |  Branch (900:13): [True: 104M, False: 3.07M]
  ------------------
  901|   104M|            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
  902|   107M|    }
  903|  6.03k|    return 0;
  904|  6.03k|}

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

jbig2_huffman_new:
   69|  6.43k|{
   70|  6.43k|    Jbig2HuffmanState *result = NULL;
   71|  6.43k|    int code;
   72|       |
   73|  6.43k|    result = jbig2_new(ctx, Jbig2HuffmanState, 1);
  ------------------
  |  |  141|  6.43k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   74|       |
   75|  6.43k|    if (result != NULL) {
  ------------------
  |  Branch (75:9): [True: 6.43k, False: 2]
  ------------------
   76|  6.43k|        result->offset = 0;
   77|  6.43k|        result->offset_bits = 0;
   78|  6.43k|        result->offset_limit = 0;
   79|  6.43k|        result->ws = ws;
   80|  6.43k|        result->ctx = ctx;
   81|  6.43k|        code = huff_get_next_word(result, 0, &result->this_word);
  ------------------
  |  |   61|  6.43k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
   82|  6.43k|        if (code < 0) {
  ------------------
  |  Branch (82:13): [True: 0, False: 6.43k]
  ------------------
   83|      0|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read first huffman word");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   84|      0|            jbig2_huffman_free(ctx, result);
   85|      0|            return NULL;
   86|      0|        }
   87|  6.43k|        code = huff_get_next_word(result, 4, &result->next_word);
  ------------------
  |  |   61|  6.43k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
   88|  6.43k|        if (code < 0) {
  ------------------
  |  Branch (88:13): [True: 0, False: 6.43k]
  ------------------
   89|      0|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read second huffman word");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   90|      0|            jbig2_huffman_free(ctx, result);
   91|      0|            return NULL;
   92|      0|        }
   93|  6.43k|    } else {
   94|      2|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate new huffman coding state");
  ------------------
  |  |   68|      2|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   95|      2|        return NULL;
   96|      2|    }
   97|       |
   98|  6.43k|    return result;
   99|  6.43k|}
jbig2_huffman_free:
  106|  15.1k|{
  107|  15.1k|    jbig2_free(ctx->allocator, hs);
  108|  15.1k|}
jbig2_huffman_skip:
  182|  31.2k|{
  183|  31.2k|    uint32_t bits = hs->offset_bits & 7;
  184|  31.2k|    int code;
  185|       |
  186|  31.2k|    if (bits) {
  ------------------
  |  Branch (186:9): [True: 26.7k, False: 4.49k]
  ------------------
  187|  26.7k|        bits = 8 - bits;
  188|  26.7k|        hs->offset_bits += bits;
  189|  26.7k|        hs->this_word = (hs->this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
  190|  26.7k|    }
  191|       |
  192|  31.2k|    if (hs->offset_bits >= 32) {
  ------------------
  |  Branch (192:9): [True: 5.84k, False: 25.3k]
  ------------------
  193|  5.84k|        hs->this_word = hs->next_word;
  194|  5.84k|        hs->offset += 4;
  195|  5.84k|        code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
  ------------------
  |  |   61|  5.84k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  196|  5.84k|        if (code < 0) {
  ------------------
  |  Branch (196:13): [True: 0, False: 5.84k]
  ------------------
  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.84k|        hs->offset_bits -= 32;
  200|  5.84k|        if (hs->offset_bits) {
  ------------------
  |  Branch (200:13): [True: 29, False: 5.81k]
  ------------------
  201|     29|            hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
  202|     29|        }
  203|  5.84k|    }
  204|  31.2k|    return 0;
  205|  31.2k|}
jbig2_huffman_advance:
  211|  27.8k|{
  212|  27.8k|    int code;
  213|  27.8k|    hs->offset += advance & ~3;
  214|  27.8k|    hs->offset_bits += ((uint32_t) (advance & 3)) << 3;
  215|  27.8k|    if (hs->offset_bits >= 32) {
  ------------------
  |  Branch (215:9): [True: 13.4k, False: 14.4k]
  ------------------
  216|  13.4k|        hs->offset += 4;
  217|  13.4k|        hs->offset_bits -= 32;
  218|  13.4k|    }
  219|  27.8k|    code = huff_get_next_word(hs, hs->offset, &hs->this_word);
  ------------------
  |  |   61|  27.8k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  220|  27.8k|    if (code < 0) {
  ------------------
  |  Branch (220:9): [True: 0, False: 27.8k]
  ------------------
  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|  27.8k|    code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
  ------------------
  |  |   61|  27.8k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  224|  27.8k|    if (code < 0) {
  ------------------
  |  Branch (224:9): [True: 0, False: 27.8k]
  ------------------
  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|  27.8k|    if (hs->offset_bits > 0)
  ------------------
  |  Branch (227:9): [True: 15.2k, False: 12.6k]
  ------------------
  228|  15.2k|        hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
  229|  27.8k|    return 0;
  230|  27.8k|}
jbig2_huffman_offset:
  237|  23.8k|{
  238|  23.8k|    return hs->offset + (hs->offset_bits >> 3);
  239|  23.8k|}
jbig2_huffman_get_bits:
  246|  19.9M|{
  247|  19.9M|    uint32_t this_word = hs->this_word;
  248|  19.9M|    int32_t result;
  249|  19.9M|    int code;
  250|       |
  251|  19.9M|    if (hs->offset_limit && hs->offset >= hs->offset_limit) {
  ------------------
  |  Branch (251:9): [True: 9.79M, False: 10.1M]
  |  Branch (251:29): [True: 0, False: 9.79M]
  ------------------
  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|  19.9M|    result = this_word >> (32 - bits);
  257|  19.9M|    hs->offset_bits += bits;
  258|  19.9M|    if (hs->offset_bits >= 32) {
  ------------------
  |  Branch (258:9): [True: 1.29M, False: 18.6M]
  ------------------
  259|  1.29M|        hs->offset += 4;
  260|  1.29M|        hs->offset_bits -= 32;
  261|  1.29M|        hs->this_word = hs->next_word;
  262|  1.29M|        code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
  ------------------
  |  |   61|  1.29M|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  263|  1.29M|        if (code < 0) {
  ------------------
  |  Branch (263:13): [True: 0, False: 1.29M]
  ------------------
  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|  1.29M|        if (hs->offset_bits) {
  ------------------
  |  Branch (266:13): [True: 76.9k, False: 1.21M]
  ------------------
  267|  76.9k|            hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
  268|  1.21M|        } else {
  269|  1.21M|            hs->this_word = (hs->this_word << hs->offset_bits);
  270|  1.21M|        }
  271|  18.6M|    } else {
  272|  18.6M|        hs->this_word = (this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
  273|  18.6M|    }
  274|       |
  275|  19.9M|    return result;
  276|  19.9M|}
jbig2_huffman_get:
  280|  82.8M|{
  281|  82.8M|    Jbig2HuffmanEntry *entry;
  282|  82.8M|    byte flags;
  283|  82.8M|    int offset_bits = hs->offset_bits;
  284|  82.8M|    uint32_t this_word = hs->this_word;
  285|  82.8M|    uint32_t next_word;
  286|  82.8M|    int RANGELEN;
  287|  82.8M|    int32_t result;
  288|       |
  289|  82.8M|    if (hs->offset_limit && hs->offset >= hs->offset_limit) {
  ------------------
  |  Branch (289:9): [True: 72.1M, False: 10.7M]
  |  Branch (289:29): [True: 166, False: 72.1M]
  ------------------
  290|    166|        if (oob)
  ------------------
  |  Branch (290:13): [True: 166, False: 0]
  ------------------
  291|    166|            *oob = -1;
  292|    166|        return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "end of Jbig2WordStream reached at offset "FMTZ, (FMTZ_CAST) hs->offset);
  ------------------
  |  |   68|    166|#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|    166|#define FMTZ "%zu"
  ------------------
  293|    166|    }
  294|       |
  295|  82.8M|    for (;;) {
  296|  82.8M|        int log_table_size = table->log_table_size;
  297|  82.8M|        int PREFLEN;
  298|  82.8M|        int code;
  299|       |
  300|       |        /* SumatraPDF: shifting by the size of the operand is undefined */
  301|  82.8M|        entry = &table->entries[log_table_size > 0 ? this_word >> (32 - log_table_size) : 0];
  ------------------
  |  Branch (301:33): [True: 82.8M, False: 597]
  ------------------
  302|  82.8M|        flags = entry->flags;
  303|  82.8M|        PREFLEN = entry->PREFLEN;
  304|  82.8M|        if (flags == (byte) -1 || PREFLEN == (byte) -1) {
  ------------------
  |  Branch (304:13): [True: 49, False: 82.8M]
  |  Branch (304:35): [True: 0, False: 82.8M]
  ------------------
  305|     49|            if (oob)
  ------------------
  |  Branch (305:17): [True: 49, False: 0]
  ------------------
  306|     49|                *oob = -1;
  307|     49|            return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "encountered unpopulated huffman table entry");
  ------------------
  |  |   68|     49|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  308|     49|        }
  309|       |
  310|  82.8M|        next_word = hs->next_word;
  311|  82.8M|        offset_bits += PREFLEN;
  312|  82.8M|        if (offset_bits >= 32) {
  ------------------
  |  Branch (312:13): [True: 5.35M, False: 77.4M]
  ------------------
  313|  5.35M|            this_word = next_word;
  314|  5.35M|            hs->offset += 4;
  315|  5.35M|            code = huff_get_next_word(hs, hs->offset + 4, &next_word);
  ------------------
  |  |   61|  5.35M|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  316|  5.35M|            if (code < 0) {
  ------------------
  |  Branch (316:17): [True: 0, False: 5.35M]
  ------------------
  317|      0|                return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get next huffman word");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  318|      0|            }
  319|  5.35M|            else if (code < 4)
  ------------------
  |  Branch (319:22): [True: 5.30M, False: 54.0k]
  ------------------
  320|  5.30M|            {
  321|  5.30M|                hs->offset_limit = hs->offset + 4 + code;
  322|  5.30M|            }
  323|  5.35M|            offset_bits -= 32;
  324|  5.35M|            hs->next_word = next_word;
  325|  5.35M|            PREFLEN = offset_bits;
  326|  5.35M|        }
  327|  82.8M|        if (PREFLEN)
  ------------------
  |  Branch (327:13): [True: 79.8M, False: 3.01M]
  ------------------
  328|  79.8M|            this_word = (this_word << PREFLEN) | (next_word >> (32 - offset_bits));
  329|  82.8M|        if (flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
  ------------------
  |  |   44|  82.8M|#define JBIG2_HUFFMAN_FLAGS_ISEXT 4
  ------------------
  |  Branch (329:13): [True: 0, False: 82.8M]
  ------------------
  330|      0|            table = entry->u.ext_table;
  331|      0|        } else
  332|  82.8M|            break;
  333|  82.8M|    }
  334|  82.8M|    result = entry->u.RANGELOW;
  335|  82.8M|    RANGELEN = entry->RANGELEN;
  336|  82.8M|    if (RANGELEN > 0) {
  ------------------
  |  Branch (336:9): [True: 6.68k, False: 82.8M]
  ------------------
  337|  6.68k|        int32_t HTOFFSET;
  338|  6.68k|        int code;
  339|       |
  340|  6.68k|        HTOFFSET = this_word >> (32 - RANGELEN);
  341|  6.68k|        if (flags & JBIG2_HUFFMAN_FLAGS_ISLOW)
  ------------------
  |  |   43|  6.68k|#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
  ------------------
  |  Branch (341:13): [True: 665, False: 6.01k]
  ------------------
  342|    665|            result -= HTOFFSET;
  343|  6.01k|        else
  344|  6.01k|            result += HTOFFSET;
  345|       |
  346|  6.68k|        offset_bits += RANGELEN;
  347|  6.68k|        if (offset_bits >= 32) {
  ------------------
  |  Branch (347:13): [True: 5.97k, False: 708]
  ------------------
  348|  5.97k|            this_word = next_word;
  349|  5.97k|            hs->offset += 4;
  350|  5.97k|            code = huff_get_next_word(hs, hs->offset + 4, &next_word);
  ------------------
  |  |   61|  5.97k|    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))
  ------------------
  351|  5.97k|            if (code < 0) {
  ------------------
  |  Branch (351:17): [True: 0, False: 5.97k]
  ------------------
  352|      0|                return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get next huffman word");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  353|      0|            }
  354|  5.97k|            else if (code < 4)
  ------------------
  |  Branch (354:22): [True: 2.94k, False: 3.03k]
  ------------------
  355|  2.94k|            {
  356|  2.94k|                hs->offset_limit = hs->offset + 4 + code;
  357|  2.94k|            }
  358|  5.97k|            offset_bits -= 32;
  359|  5.97k|            hs->next_word = next_word;
  360|  5.97k|            RANGELEN = offset_bits;
  361|  5.97k|        }
  362|  6.68k|        if (RANGELEN)
  ------------------
  |  Branch (362:13): [True: 6.04k, False: 639]
  ------------------
  363|  6.04k|            this_word = (this_word << RANGELEN) | (next_word >> (32 - offset_bits));
  364|  6.68k|    }
  365|       |
  366|  82.8M|    hs->this_word = this_word;
  367|  82.8M|    hs->offset_bits = offset_bits;
  368|       |
  369|  82.8M|    if (oob != NULL)
  ------------------
  |  Branch (369:9): [True: 82.8M, False: 0]
  ------------------
  370|  82.8M|        *oob = (flags & JBIG2_HUFFMAN_FLAGS_ISOOB);
  ------------------
  |  |   42|  82.8M|#define JBIG2_HUFFMAN_FLAGS_ISOOB 1
  ------------------
  371|       |
  372|  82.8M|    return result;
  373|  82.8M|}
jbig2_build_huffman_table:
  385|  35.6k|{
  386|  35.6k|    int *LENCOUNT;
  387|  35.6k|    int LENMAX = -1;
  388|  35.6k|    const int lencountcount = 256;
  389|  35.6k|    const Jbig2HuffmanLine *lines = params->lines;
  390|  35.6k|    int n_lines = params->n_lines;
  391|  35.6k|    int i, j;
  392|  35.6k|    uint32_t max_j;
  393|  35.6k|    int log_table_size = 0;
  394|  35.6k|    Jbig2HuffmanTable *result;
  395|  35.6k|    Jbig2HuffmanEntry *entries;
  396|  35.6k|    int CURLEN;
  397|  35.6k|    int firstcode = 0;
  398|  35.6k|    int CURCODE;
  399|  35.6k|    int CURTEMP;
  400|       |
  401|  35.6k|    LENCOUNT = jbig2_new(ctx, int, lencountcount);
  ------------------
  |  |  141|  35.6k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  402|       |
  403|  35.6k|    if (LENCOUNT == NULL) {
  ------------------
  |  Branch (403:9): [True: 4, False: 35.6k]
  ------------------
  404|      4|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate huffman histogram");
  ------------------
  |  |   68|      4|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  405|      4|        return NULL;
  406|      4|    }
  407|  35.6k|    memset(LENCOUNT, 0, sizeof(int) * lencountcount);
  408|       |
  409|       |    /* B.3, 1. */
  410|  18.0M|    for (i = 0; i < params->n_lines; i++) {
  ------------------
  |  Branch (410:17): [True: 18.0M, False: 35.6k]
  ------------------
  411|  18.0M|        int PREFLEN = lines[i].PREFLEN;
  412|  18.0M|        int lts;
  413|       |
  414|  18.0M|        if (PREFLEN > LENMAX) {
  ------------------
  |  Branch (414:13): [True: 105k, False: 17.9M]
  ------------------
  415|   359k|            for (j = LENMAX + 1; j < PREFLEN + 1; j++)
  ------------------
  |  Branch (415:34): [True: 254k, False: 105k]
  ------------------
  416|   254k|                LENCOUNT[j] = 0;
  417|   105k|            LENMAX = PREFLEN;
  418|   105k|        }
  419|  18.0M|        LENCOUNT[PREFLEN]++;
  420|       |
  421|  18.0M|        lts = PREFLEN + lines[i].RANGELEN;
  422|  18.0M|        if (lts > LOG_TABLE_SIZE_MAX)
  ------------------
  |  |  378|  18.0M|#define LOG_TABLE_SIZE_MAX 16
  ------------------
  |  Branch (422:13): [True: 4.38M, False: 13.6M]
  ------------------
  423|  4.38M|            lts = PREFLEN;
  424|  18.0M|        if (lts <= LOG_TABLE_SIZE_MAX && log_table_size < lts)
  ------------------
  |  |  378|  36.0M|#define LOG_TABLE_SIZE_MAX 16
  ------------------
  |  Branch (424:13): [True: 13.7M, False: 4.31M]
  |  Branch (424:42): [True: 97.4k, False: 13.6M]
  ------------------
  425|  97.4k|            log_table_size = lts;
  426|  18.0M|    }
  427|  35.6k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "constructing huffman table log size %d", log_table_size);
  ------------------
  |  |   68|  35.6k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  428|  35.6k|    max_j = 1 << log_table_size;
  429|       |
  430|  35.6k|    result = jbig2_new(ctx, Jbig2HuffmanTable, 1);
  ------------------
  |  |  141|  35.6k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  431|  35.6k|    if (result == NULL) {
  ------------------
  |  Branch (431:9): [True: 11, False: 35.6k]
  ------------------
  432|     11|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate result");
  ------------------
  |  |   68|     11|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  433|     11|        jbig2_free(ctx->allocator, LENCOUNT);
  434|     11|        return NULL;
  435|     11|    }
  436|  35.6k|    result->log_table_size = log_table_size;
  437|  35.6k|    entries = jbig2_new(ctx, Jbig2HuffmanEntry, max_j);
  ------------------
  |  |  141|  35.6k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  438|  35.6k|    if (entries == NULL) {
  ------------------
  |  Branch (438:9): [True: 19, False: 35.6k]
  ------------------
  439|     19|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate result entries");
  ------------------
  |  |   68|     19|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  440|     19|        jbig2_free(ctx->allocator, result);
  441|     19|        jbig2_free(ctx->allocator, LENCOUNT);
  442|     19|        return NULL;
  443|     19|    }
  444|       |    /* fill now to catch missing JBIG2Globals later */
  445|  35.6k|    memset(entries, 0xFF, sizeof(Jbig2HuffmanEntry) * max_j);
  446|  35.6k|    result->entries = entries;
  447|       |
  448|  35.6k|    LENCOUNT[0] = 0;
  449|       |
  450|   253k|    for (CURLEN = 1; CURLEN <= LENMAX; CURLEN++) {
  ------------------
  |  Branch (450:22): [True: 218k, False: 35.5k]
  ------------------
  451|   218k|        int shift = log_table_size - CURLEN;
  452|       |
  453|       |        /* B.3 3.(a) */
  454|   218k|        firstcode = (firstcode + LENCOUNT[CURLEN - 1]) << 1;
  455|   218k|        CURCODE = firstcode;
  456|       |        /* B.3 3.(b) */
  457|   175M|        for (CURTEMP = 0; CURTEMP < n_lines; CURTEMP++) {
  ------------------
  |  Branch (457:27): [True: 175M, False: 218k]
  ------------------
  458|   175M|            int PREFLEN = lines[CURTEMP].PREFLEN;
  459|       |
  460|   175M|            if (PREFLEN == CURLEN) {
  ------------------
  |  Branch (460:17): [True: 666k, False: 174M]
  ------------------
  461|   666k|                int RANGELEN = lines[CURTEMP].RANGELEN;
  462|   666k|                uint32_t start_j = CURCODE << shift;
  463|   666k|                uint32_t end_j = (CURCODE + 1) << shift;
  464|   666k|                uint32_t cur_j;
  465|   666k|                byte eflags = 0;
  466|       |
  467|   666k|                if (end_j > max_j) {
  ------------------
  |  Branch (467:21): [True: 112, False: 666k]
  ------------------
  468|    112|                    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|    112|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  469|    112|                    jbig2_free(ctx->allocator, result->entries);
  470|    112|                    jbig2_free(ctx->allocator, result);
  471|    112|                    jbig2_free(ctx->allocator, LENCOUNT);
  472|    112|                    return NULL;
  473|    112|                }
  474|       |                /* todo: build extension tables */
  475|   666k|                if (params->HTOOB && CURTEMP == n_lines - 1)
  ------------------
  |  Branch (475:21): [True: 47.2k, False: 618k]
  |  Branch (475:38): [True: 4.59k, False: 42.6k]
  ------------------
  476|  4.59k|                    eflags |= JBIG2_HUFFMAN_FLAGS_ISOOB;
  ------------------
  |  |   42|  4.59k|#define JBIG2_HUFFMAN_FLAGS_ISOOB 1
  ------------------
  477|   666k|                if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2))
  ------------------
  |  Branch (477:21): [True: 14.5k, False: 651k]
  |  Branch (477:43): [True: 47.2k, False: 618k]
  ------------------
  478|  14.5k|                    eflags |= JBIG2_HUFFMAN_FLAGS_ISLOW;
  ------------------
  |  |   43|  14.5k|#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
  ------------------
  479|   666k|                if (PREFLEN + RANGELEN > LOG_TABLE_SIZE_MAX) {
  ------------------
  |  |  378|   666k|#define LOG_TABLE_SIZE_MAX 16
  ------------------
  |  Branch (479:21): [True: 50.5k, False: 615k]
  ------------------
  480|  5.48M|                    for (cur_j = start_j; cur_j < end_j; cur_j++) {
  ------------------
  |  Branch (480:43): [True: 5.43M, False: 50.5k]
  ------------------
  481|  5.43M|                        entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW;
  482|  5.43M|                        entries[cur_j].PREFLEN = PREFLEN;
  483|  5.43M|                        entries[cur_j].RANGELEN = RANGELEN;
  484|  5.43M|                        entries[cur_j].flags = eflags;
  485|  5.43M|                    }
  486|   615k|                } else {
  487|   264M|                    for (cur_j = start_j; cur_j < end_j; cur_j++) {
  ------------------
  |  Branch (487:43): [True: 264M, False: 615k]
  ------------------
  488|   264M|                        int32_t HTOFFSET = (cur_j >> (shift - RANGELEN)) & ((1 << RANGELEN) - 1);
  489|       |
  490|   264M|                        if (eflags & JBIG2_HUFFMAN_FLAGS_ISLOW)
  ------------------
  |  |   43|   264M|#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
  ------------------
  |  Branch (490:29): [True: 516k, False: 263M]
  ------------------
  491|   516k|                            entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW - HTOFFSET;
  492|   263M|                        else
  493|   263M|                            entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW + HTOFFSET;
  494|   264M|                        entries[cur_j].PREFLEN = PREFLEN + RANGELEN;
  495|   264M|                        entries[cur_j].RANGELEN = 0;
  496|   264M|                        entries[cur_j].flags = eflags;
  497|   264M|                    }
  498|   615k|                }
  499|   666k|                CURCODE++;
  500|   666k|            }
  501|   175M|        }
  502|   218k|    }
  503|       |
  504|  35.5k|    jbig2_free(ctx->allocator, LENCOUNT);
  505|       |
  506|  35.5k|    return result;
  507|  35.6k|}
jbig2_release_huffman_table:
  512|  48.6k|{
  513|  48.6k|    if (table != NULL) {
  ------------------
  |  Branch (513:9): [True: 35.5k, False: 13.1k]
  ------------------
  514|  35.5k|        jbig2_free(ctx->allocator, table->entries);
  515|  35.5k|        jbig2_free(ctx->allocator, table);
  516|  35.5k|    }
  517|  48.6k|}
jbig2_table:
  550|  1.82k|{
  551|  1.82k|    Jbig2HuffmanParams *params = NULL;
  552|  1.82k|    Jbig2HuffmanLine *line = NULL;
  553|       |
  554|  1.82k|    segment->result = NULL;
  555|  1.82k|    if (segment->data_length < 10)
  ------------------
  |  Branch (555:9): [True: 12, False: 1.81k]
  ------------------
  556|     12|        goto too_short;
  557|  1.81k|    if (segment->data_length > SIZE_MAX / 8)
  ------------------
  |  Branch (557:9): [True: 0, False: 1.81k]
  ------------------
  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.81k|    {
  564|       |        /* B.2 1) (B.2.1) Code table flags */
  565|  1.81k|        const int code_table_flags = segment_data[0];
  566|  1.81k|        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.81k|        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.81k|        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.81k|        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.81k|        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.81k|        const size_t lines_max = (segment->data_length * 8 - HTPS * (HTOOB ? 3 : 2)) / (HTPS + HTRS) + (HTOOB ? 3 : 2);
  ------------------
  |  Branch (582:70): [True: 873, False: 937]
  |  Branch (582:105): [True: 873, False: 937]
  ------------------
  583|       |
  584|       |        /* points to a first table line data */
  585|  1.81k|        const byte *lines_data = segment_data + 9;
  586|  1.81k|        const size_t lines_data_bitlen = (segment->data_length - 9) * 8;        /* length in bit */
  587|       |
  588|       |        /* bit offset: controls bit reading */
  589|  1.81k|        size_t boffset = 0;
  590|       |
  591|       |        /* B.2 4) */
  592|  1.81k|        int32_t CURRANGELOW = HTLOW;
  593|  1.81k|        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.81k|        if (segment->data_length > SIZE_MAX / 8) {
  ------------------
  |  Branch (601:13): [True: 0, False: 1.81k]
  ------------------
  602|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Huffman Table size too large");
  603|      0|            goto error_exit;
  604|      0|        }
  605|  1.81k|        if (HTLOW >= HTHIGH) {
  ------------------
  |  Branch (605:13): [True: 37, False: 1.77k]
  ------------------
  606|     37|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid Huffman Table range");
  607|     37|            goto error_exit;
  608|     37|        }
  609|       |
  610|       |        /* allocate HuffmanParams & HuffmanLine */
  611|  1.77k|        params = jbig2_new(ctx, Jbig2HuffmanParams, 1);
  ------------------
  |  |  141|  1.77k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  612|  1.77k|        if (params == NULL) {
  ------------------
  |  Branch (612:13): [True: 1, False: 1.77k]
  ------------------
  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.77k|        line = jbig2_new(ctx, Jbig2HuffmanLine, lines_max);
  ------------------
  |  |  141|  1.77k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  617|  1.77k|        if (line == NULL) {
  ------------------
  |  Branch (617:13): [True: 1, False: 1.77k]
  ------------------
  618|      1|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate huffman table lines");
  619|      1|            goto error_exit;
  620|      1|        }
  621|       |        /* B.2 5) */
  622|  7.16M|        while (CURRANGELOW < HTHIGH) {
  ------------------
  |  Branch (622:16): [True: 7.16M, False: 1.70k]
  ------------------
  623|       |            /* B.2 5) a) */
  624|  7.16M|            if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (624:17): [True: 23, False: 7.16M]
  ------------------
  625|     23|                goto too_short;
  626|  7.16M|            if (NTEMP >= lines_max) {
  ------------------
  |  Branch (626:17): [True: 0, False: 7.16M]
  ------------------
  627|      0|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  628|      0|                goto error_exit;
  629|      0|            }
  630|  7.16M|            line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  631|       |            /* B.2 5) b) */
  632|  7.16M|            if (boffset + HTRS >= lines_data_bitlen)
  ------------------
  |  Branch (632:17): [True: 45, False: 7.16M]
  ------------------
  633|     45|                goto too_short;
  634|  7.16M|            line[NTEMP].RANGELEN = jbig2_table_read_bits(lines_data, &boffset, HTRS);
  635|       |            /* B.2 5) c) */
  636|  7.16M|            line[NTEMP].RANGELOW = CURRANGELOW;
  637|  7.16M|            CURRANGELOW += (1 << line[NTEMP].RANGELEN);
  638|  7.16M|            NTEMP++;
  639|  7.16M|        }
  640|       |        /* B.2 6), B.2 7) lower range table line */
  641|  1.70k|        if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (641:13): [True: 4, False: 1.69k]
  ------------------
  642|      4|            goto too_short;
  643|  1.69k|        if (NTEMP >= lines_max) {
  ------------------
  |  Branch (643:13): [True: 0, False: 1.69k]
  ------------------
  644|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  645|      0|            goto error_exit;
  646|      0|        }
  647|  1.69k|        line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  648|  1.69k|        line[NTEMP].RANGELEN = 32;
  649|  1.69k|        line[NTEMP].RANGELOW = HTLOW - 1;
  650|  1.69k|        NTEMP++;
  651|       |        /* B.2 8), B.2 9) upper range table line */
  652|  1.69k|        if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (652:13): [True: 5, False: 1.69k]
  ------------------
  653|      5|            goto too_short;
  654|  1.69k|        if (NTEMP >= lines_max) {
  ------------------
  |  Branch (654:13): [True: 0, False: 1.69k]
  ------------------
  655|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  656|      0|            goto error_exit;
  657|      0|        }
  658|  1.69k|        line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  659|  1.69k|        line[NTEMP].RANGELEN = 32;
  660|  1.69k|        line[NTEMP].RANGELOW = HTHIGH;
  661|  1.69k|        NTEMP++;
  662|       |        /* B.2 10) */
  663|  1.69k|        if (HTOOB) {
  ------------------
  |  Branch (663:13): [True: 817, False: 877]
  ------------------
  664|       |            /* B.2 10) a), B.2 10) b) out-of-bound table line */
  665|    817|            if (boffset + HTPS >= lines_data_bitlen)
  ------------------
  |  Branch (665:17): [True: 7, False: 810]
  ------------------
  666|      7|                goto too_short;
  667|    810|            if (NTEMP >= lines_max) {
  ------------------
  |  Branch (667:17): [True: 0, False: 810]
  ------------------
  668|      0|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table line count exceeded");
  669|      0|                goto error_exit;
  670|      0|            }
  671|    810|            line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
  672|    810|            line[NTEMP].RANGELEN = 0;
  673|    810|            line[NTEMP].RANGELOW = 0;
  674|    810|            NTEMP++;
  675|    810|        }
  676|  1.68k|        if (NTEMP != lines_max) {
  ------------------
  |  Branch (676:13): [True: 1.68k, False: 0]
  ------------------
  677|  1.68k|            Jbig2HuffmanLine *new_line = jbig2_renew(ctx, line,
  ------------------
  |  |  143|  1.68k|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
  678|  1.68k|                                                     Jbig2HuffmanLine, NTEMP);
  679|       |
  680|  1.68k|            if (new_line == NULL) {
  ------------------
  |  Branch (680:17): [True: 0, False: 1.68k]
  ------------------
  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.68k|            line = new_line;
  685|  1.68k|        }
  686|  1.68k|        params->HTOOB = HTOOB;
  687|       |        /* Assuming int >= int32_t here. */
  688|  1.68k|        if (NTEMP > INT32_MAX) {
  ------------------
  |  Branch (688:13): [True: 0, False: 1.68k]
  ------------------
  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.68k|        params->n_lines = (int)NTEMP;
  693|  1.68k|        params->lines = line;
  694|  1.68k|        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.68k|    }
  708|      0|    return 0;
  709|       |
  710|     96|too_short:
  711|     96|    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  712|    135|error_exit:
  713|    135|    jbig2_free(ctx->allocator, line);
  714|    135|    jbig2_free(ctx->allocator, params);
  715|    135|    return -1;
  716|     96|}
jbig2_table_free:
  721|  1.68k|{
  722|  1.68k|    if (params != NULL) {
  ------------------
  |  Branch (722:9): [True: 1.68k, False: 0]
  ------------------
  723|  1.68k|        jbig2_free(ctx->allocator, (void *)params->lines);
  724|  1.68k|        jbig2_free(ctx->allocator, params);
  725|  1.68k|    }
  726|  1.68k|}
jbig2_find_table:
  731|    518|{
  732|    518|    int i, table_index = 0;
  733|       |
  734|  1.39k|    for (i = 0; i < segment->referred_to_segment_count; i++) {
  ------------------
  |  Branch (734:17): [True: 1.35k, False: 49]
  ------------------
  735|  1.35k|        const Jbig2Segment *const rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[i]);
  736|       |
  737|  1.35k|        if (rsegment && (rsegment->flags & 63) == 53) {
  ------------------
  |  Branch (737:13): [True: 838, False: 512]
  |  Branch (737:25): [True: 602, False: 236]
  ------------------
  738|    602|            if (table_index == index)
  ------------------
  |  Branch (738:17): [True: 469, False: 133]
  ------------------
  739|    469|                return (const Jbig2HuffmanParams *)rsegment->result;
  740|    133|            ++table_index;
  741|    133|        }
  742|  1.35k|    }
  743|       |
  744|     49|    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table not found (%d)", index);
  745|       |    return NULL;
  746|    518|}
jbig2_huffman.c:jbig2_table_read_bits:
  524|  14.3M|{
  525|  14.3M|    uint32_t result = 0;
  526|  14.3M|    size_t byte_offset = *bitoffset / 8;
  527|  14.3M|    const int endbit = ((int) (*bitoffset & 7)) + bitlen;
  528|  14.3M|    const int n_proc_bytes = (endbit + 7) / 8;
  529|  14.3M|    const int rshift = n_proc_bytes * 8 - endbit;
  530|  14.3M|    int i;
  531|       |
  532|  29.0M|    for (i = n_proc_bytes - 1; i >= 0; i--) {
  ------------------
  |  Branch (532:32): [True: 14.6M, False: 14.3M]
  ------------------
  533|  14.6M|        uint32_t d = data[byte_offset++];
  534|  14.6M|        const int nshift = i * 8 - rshift;
  535|       |
  536|  14.6M|        if (nshift > 0)
  ------------------
  |  Branch (536:13): [True: 357k, False: 14.3M]
  ------------------
  537|   357k|            d <<= nshift;
  538|  14.3M|        else if (nshift < 0)
  ------------------
  |  Branch (538:18): [True: 12.5M, False: 1.79M]
  ------------------
  539|  12.5M|            d >>= -nshift;
  540|  14.6M|        result |= d;
  541|  14.6M|    }
  542|       |    result &= ~(UINT32_MAX << bitlen);
  543|  14.3M|    *bitoffset += bitlen;
  544|  14.3M|    return result;
  545|  14.3M|}

jbig2_image_new:
   36|  18.5M|{
   37|  18.5M|    Jbig2Image *image;
   38|  18.5M|    uint32_t stride;
   39|       |
   40|  18.5M|    if (width == 0 || height == 0) {
  ------------------
  |  Branch (40:9): [True: 105, False: 18.5M]
  |  Branch (40:23): [True: 13, False: 18.5M]
  ------------------
   41|    118|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to create zero sized image");
  ------------------
  |  |   68|    118|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   42|    118|        return NULL;
   43|    118|    }
   44|       |
   45|  18.5M|    image = jbig2_new(ctx, Jbig2Image, 1);
  ------------------
  |  |  141|  18.5M|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   46|  18.5M|    if (image == NULL) {
  ------------------
  |  Branch (46:9): [True: 41, False: 18.5M]
  ------------------
   47|     41|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate image");
  ------------------
  |  |   68|     41|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   48|     41|        return NULL;
   49|     41|    }
   50|       |
   51|  18.5M|    stride = ((width - 1) >> 3) + 1;    /* generate a byte-aligned stride */
   52|       |
   53|       |    /* check for integer multiplication overflow */
   54|  18.5M|    if (height > (INT32_MAX / stride)) {
  ------------------
  |  Branch (54:9): [True: 253, False: 18.5M]
  ------------------
   55|    253|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "integer multiplication overflow (stride=%u, height=%u)", stride, height);
  ------------------
  |  |   68|    253|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   56|    253|        jbig2_free(ctx->allocator, image);
   57|    253|        return NULL;
   58|    253|    }
   59|  18.5M|    image->data = jbig2_new(ctx, uint8_t, (size_t) height * stride);
  ------------------
  |  |  141|  18.5M|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   60|  18.5M|    if (image->data == NULL) {
  ------------------
  |  Branch (60:9): [True: 195, False: 18.5M]
  ------------------
   61|    195|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate image data buffer (stride=%u, height=%u)", stride, height);
  ------------------
  |  |   68|    195|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   62|    195|        jbig2_free(ctx->allocator, image);
   63|    195|        return NULL;
   64|    195|    }
   65|       |
   66|  18.5M|    image->width = width;
   67|  18.5M|    image->height = height;
   68|  18.5M|    image->stride = stride;
   69|  18.5M|    image->refcount = 1;
   70|       |
   71|  18.5M|    return image;
   72|  18.5M|}
jbig2_image_reference:
   77|  66.1M|{
   78|  66.1M|    (void) ctx;
   79|  66.1M|    if (image)
  ------------------
  |  Branch (79:9): [True: 18.6M, False: 47.4M]
  ------------------
   80|  18.6M|        image->refcount++;
   81|  66.1M|    return image;
   82|  66.1M|}
jbig2_image_release:
   87|   559M|{
   88|   559M|    if (image == NULL)
  ------------------
  |  Branch (88:9): [True: 521M, False: 37.1M]
  ------------------
   89|   521M|        return;
   90|  37.1M|    image->refcount--;
   91|  37.1M|    if (image->refcount == 0)
  ------------------
  |  Branch (91:9): [True: 18.5M, False: 18.6M]
  ------------------
   92|  18.5M|        jbig2_image_free(ctx, image);
   93|  37.1M|}
jbig2_image_free:
   98|  18.5M|{
   99|  18.5M|    if (image != NULL) {
  ------------------
  |  Branch (99:9): [True: 18.5M, False: 0]
  ------------------
  100|  18.5M|        jbig2_free(ctx->allocator, image->data);
  101|  18.5M|        jbig2_free(ctx->allocator, image);
  102|  18.5M|    }
  103|  18.5M|}
jbig2_image_resize:
  108|  5.51k|{
  109|  5.51k|    if (width == image->width) {
  ------------------
  |  Branch (109:9): [True: 5.51k, False: 0]
  ------------------
  110|  5.51k|        uint8_t *data;
  111|       |
  112|       |        /* check for integer multiplication overflow */
  113|  5.51k|        if (image->height > (INT32_MAX / image->stride)) {
  ------------------
  |  Branch (113:13): [True: 0, False: 5.51k]
  ------------------
  114|      0|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "integer multiplication overflow during resize (stride=%u, height=%u)", image->stride, height);
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  115|      0|            return NULL;
  116|      0|        }
  117|       |        /* use the same stride, just change the length */
  118|  5.51k|        data = jbig2_renew(ctx, image->data, uint8_t, (size_t) height * image->stride);
  ------------------
  |  |  143|  5.51k|#define jbig2_renew(ctx, p, t, num) ((t *)jbig2_realloc(ctx->allocator, (p), sizeof(t), num))
  ------------------
  119|  5.51k|        if (data == NULL) {
  ------------------
  |  Branch (119:13): [True: 65, False: 5.45k]
  ------------------
  120|     65|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to reallocate image");
  ------------------
  |  |   68|     65|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  121|     65|            return NULL;
  122|     65|        }
  123|  5.45k|        image->data = data;
  124|  5.45k|        if (height > image->height) {
  ------------------
  |  Branch (124:13): [True: 5.45k, False: 0]
  ------------------
  125|  5.45k|            const uint8_t fill = value ? 0xFF : 0x00;
  ------------------
  |  Branch (125:34): [True: 3.72k, False: 1.72k]
  ------------------
  126|  5.45k|            memset(image->data + (size_t) image->height * image->stride, fill, ((size_t) height - image->height) * image->stride);
  127|  5.45k|        }
  128|  5.45k|        image->height = height;
  129|       |
  130|  5.45k|    } else {
  131|      0|        Jbig2Image *newimage;
  132|      0|        int code;
  133|       |
  134|       |        /* Unoptimized implementation, but it works. */
  135|       |
  136|      0|        newimage = jbig2_image_new(ctx, width, height);
  137|      0|        if (newimage == NULL) {
  ------------------
  |  Branch (137:13): [True: 0, False: 0]
  ------------------
  138|      0|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate resized image");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  139|      0|            return NULL;
  140|      0|        }
  141|      0|        jbig2_image_clear(ctx, newimage, value);
  142|       |
  143|      0|        code = jbig2_image_compose(ctx, newimage, image, 0, 0, JBIG2_COMPOSE_REPLACE);
  144|      0|        if (code < 0) {
  ------------------
  |  Branch (144:13): [True: 0, False: 0]
  ------------------
  145|      0|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to compose image buffers when resizing");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  146|      0|            jbig2_image_release(ctx, newimage);
  147|      0|            return NULL;
  148|      0|        }
  149|       |
  150|       |        /* if refcount > 1 the original image, its pointer must
  151|       |        be kept, so simply replaces its innards, and throw away
  152|       |        the empty new image shell. */
  153|      0|        jbig2_free(ctx->allocator, image->data);
  154|      0|        image->width = newimage->width;
  155|      0|        image->height = newimage->height;
  156|      0|        image->stride = newimage->stride;
  157|      0|        image->data = newimage->data;
  158|      0|        jbig2_free(ctx->allocator, newimage);
  159|      0|    }
  160|       |
  161|  5.45k|    return image;
  162|  5.51k|}
jbig2_image_compose:
  336|   140M|{
  337|   140M|    uint32_t w, h;
  338|   140M|    uint32_t shift;
  339|   140M|    uint32_t leftbyte;
  340|   140M|    uint8_t *ss;
  341|   140M|    uint8_t *dd;
  342|   140M|    uint8_t leftmask, rightmask;
  343|   140M|    int early = x >= 0;
  344|   140M|    int late;
  345|   140M|    uint32_t bytewidth;
  346|   140M|    uint32_t syoffset = 0;
  347|       |
  348|   140M|    (void) ctx;
  349|       |
  350|   140M|    if (src == NULL)
  ------------------
  |  Branch (350:9): [True: 13.8M, False: 127M]
  ------------------
  351|  13.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|   127M|    if (
  361|   127M|            (x <= -((int64_t) src->width)) || (x >= (int64_t) dst->width) ||
  ------------------
  |  Branch (361:13): [True: 19.5M, False: 107M]
  |  Branch (361:47): [True: 69.7M, False: 37.7M]
  ------------------
  362|  37.7M|            (y <= -((int64_t) src->height)) || (y >= (int64_t) dst->height))
  ------------------
  |  Branch (362:13): [True: 3.11M, False: 34.5M]
  |  Branch (362:48): [True: 15.6M, False: 18.9M]
  ------------------
  363|   108M|    {
  364|   108M|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "src image entirely outside dst image in compose_image");
  ------------------
  |  |   68|   108M|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  365|   108M|        return 0;
  366|   108M|    }
  367|       |
  368|       |    /* This code takes a src image and combines it onto dst at offset (x,y), with operation op. */
  369|       |
  370|       |    /* Data is packed msb first within a byte, so with bits numbered: 01234567.
  371|       |     * Second byte is: 89abcdef. So to combine into a run, we use:
  372|       |     *       (s[0]<<8) | s[1] == 0123456789abcdef.
  373|       |     * To read from src into dst at offset 3, we need to read:
  374|       |     *    read:      0123456789abcdef...
  375|       |     *    write:  0123456798abcdef...
  376|       |     * In general, to read from src and write into dst at offset x, we need to shift
  377|       |     * down by (x&7) bits to allow for bit alignment. So shift = x&7.
  378|       |     * So the 'central' part of our runs will see us doing:
  379|       |     *   *d++ op= ((s[0]<<8)|s[1])>>shift;
  380|       |     * with special cases on the left and right edges of the run to mask.
  381|       |     * With the left hand edge, we have to be careful not to 'underread' the start of
  382|       |     * the src image; this is what the early flag is about. Similarly we have to be
  383|       |     * careful not to read off the right hand edge; this is what the late flag is for.
  384|       |     */
  385|       |
  386|  18.9M|    w = src->width;
  387|  18.9M|    h = src->height;
  388|  18.9M|    shift = (uint32_t) (x & 7);
  389|  18.9M|    ss = src->data - early;
  390|       |
  391|       |    /* Since we know that x/y are now limited to [-UINT32_MAX + 1, UINT32_MAX - 1],
  392|       |     * we know that we can't accidentally negate an INT64_MIN. Moreover, we know
  393|       |     * that their negated values fit within an uint32_t, so casting to uint32_t is
  394|       |     * safe.
  395|       |     */
  396|       |
  397|       |    /* clip left/top */
  398|  18.9M|    if (x < 0) {
  ------------------
  |  Branch (398:9): [True: 18.2M, False: 774k]
  ------------------
  399|  18.2M|        uint32_t negx = (uint32_t) (-x);
  400|  18.2M|        w -= negx;
  401|  18.2M|        ss += (negx-1)>>3;
  402|  18.2M|        x = 0;
  403|  18.2M|    }
  404|  18.9M|    if (y < 0) {
  ------------------
  |  Branch (404:9): [True: 35.3k, False: 18.9M]
  ------------------
  405|  35.3k|        uint32_t negy = (uint32_t) (-y);
  406|  35.3k|        h -= negy;
  407|  35.3k|        syoffset = negy * src->stride;
  408|  35.3k|        y = 0;
  409|  35.3k|    }
  410|       |
  411|       |    /* clip right/bottom */
  412|  18.9M|    if ((uint32_t)x + w > dst->width)
  ------------------
  |  Branch (412:9): [True: 18.1M, False: 793k]
  ------------------
  413|  18.1M|        w = dst->width - ((uint32_t) x);
  414|  18.9M|    if ((uint32_t)y + h > dst->height)
  ------------------
  |  Branch (414:9): [True: 338k, False: 18.6M]
  ------------------
  415|   338k|        h = dst->height - ((uint32_t) y);
  416|       |#ifdef JBIG2_DEBUG
  417|       |    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "compositing %ux%u at (%u, %u) after clipping",
  418|       |        w, h, (uint32_t) x, (uint32_t) y);
  419|       |#endif
  420|       |
  421|       |    /* check for zero clipping region */
  422|  18.9M|    if ((w <= 0) || (h <= 0)) {
  ------------------
  |  Branch (422:9): [True: 0, False: 18.9M]
  |  Branch (422:21): [True: 0, False: 18.9M]
  ------------------
  423|       |#ifdef JBIG2_DEBUG
  424|       |        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "zero clipping region");
  425|       |#endif
  426|      0|        return 0;
  427|      0|    }
  428|       |
  429|  18.9M|    leftbyte = (uint32_t) x >> 3;
  430|  18.9M|    dd = dst->data + y * dst->stride + leftbyte;
  431|  18.9M|    bytewidth = (((uint32_t) x + w - 1) >> 3) - leftbyte + 1;
  432|  18.9M|    leftmask = 255>>(x&7);
  433|  18.9M|    rightmask = (((x+w)&7) == 0) ? 255 : ~(255>>((x+w)&7));
  ------------------
  |  Branch (433:17): [True: 1.62M, False: 17.3M]
  ------------------
  434|  18.9M|    if (bytewidth == 1)
  ------------------
  |  Branch (434:9): [True: 8.46M, False: 10.5M]
  ------------------
  435|  8.46M|        leftmask &= rightmask;
  436|  18.9M|    late = (ss + bytewidth >= src->data + ((src->width+7)>>3));
  437|  18.9M|    ss += syoffset;
  438|       |
  439|  18.9M|    switch(op)
  ------------------
  |  Branch (439:12): [True: 18.8M, False: 95.0k]
  ------------------
  440|  18.9M|    {
  441|   434k|    case JBIG2_COMPOSE_OR:
  ------------------
  |  Branch (441:5): [True: 434k, False: 18.5M]
  ------------------
  442|   434k|        jbig2_image_compose_opt_OR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  443|   434k|        break;
  444|  2.15k|    case JBIG2_COMPOSE_AND:
  ------------------
  |  Branch (444:5): [True: 2.15k, False: 18.9M]
  ------------------
  445|  2.15k|        jbig2_image_compose_opt_AND(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  446|  2.15k|        break;
  447|   239k|    case JBIG2_COMPOSE_XOR:
  ------------------
  |  Branch (447:5): [True: 239k, False: 18.7M]
  ------------------
  448|   239k|        jbig2_image_compose_opt_XOR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  449|   239k|        break;
  450|  2.35k|    case JBIG2_COMPOSE_XNOR:
  ------------------
  |  Branch (450:5): [True: 2.35k, False: 18.9M]
  ------------------
  451|  2.35k|        jbig2_image_compose_opt_XNOR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  452|  2.35k|        break;
  453|  18.2M|    case JBIG2_COMPOSE_REPLACE:
  ------------------
  |  Branch (453:5): [True: 18.2M, False: 772k]
  ------------------
  454|  18.2M|        jbig2_image_compose_opt_REPLACE(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
  455|  18.2M|        break;
  456|  18.9M|    }
  457|       |
  458|  18.9M|    return 0;
  459|  18.9M|}
jbig2_image_clear:
  464|   110k|{
  465|   110k|    const uint8_t fill = value ? 0xFF : 0x00;
  ------------------
  |  Branch (465:26): [True: 5.01k, False: 105k]
  ------------------
  466|       |
  467|   110k|    (void) ctx;
  468|       |
  469|   110k|    memset(image->data, fill, image->stride * image->height);
  470|   110k|}
jbig2_image_get_pixel:
  478|  18.9G|{
  479|  18.9G|    const int64_t w = image->width;
  480|  18.9G|    const int64_t h = image->height;
  481|  18.9G|    size_t sx, sy;
  482|  18.9G|    size_t byte;
  483|  18.9G|    int bit;
  484|       |
  485|  18.9G|    if ((x < 0) || (x >= w))
  ------------------
  |  Branch (485:9): [True: 2.02G, False: 16.9G]
  |  Branch (485:20): [True: 3.07G, False: 13.8G]
  ------------------
  486|  5.09G|        return 0;
  487|  13.8G|    if ((y < 0) || (y >= h))
  ------------------
  |  Branch (487:9): [True: 3.92G, False: 9.90G]
  |  Branch (487:20): [True: 1.42G, False: 8.47G]
  ------------------
  488|  5.35G|        return 0;
  489|       |
  490|  8.47G|    sx = (size_t) x;
  491|  8.47G|    sy = (size_t) y;
  492|       |
  493|  8.47G|    byte = (sx >> 3) + sy * image->stride;
  494|  8.47G|    bit = 7 - ((int) (sx & 7));
  495|       |
  496|  8.47G|    return ((image->data[byte] >> bit) & 1);
  497|  13.8G|}
jbig2_image_set_pixel:
  502|  2.16G|{
  503|  2.16G|    const int64_t w = image->width;
  504|  2.16G|    const int64_t h = image->height;
  505|  2.16G|    uint8_t scratch, mask;
  506|  2.16G|    size_t sx, sy;
  507|  2.16G|    size_t byte;
  508|  2.16G|    int bit;
  509|       |
  510|  2.16G|    if ((x < 0) || (x >= w))
  ------------------
  |  Branch (510:9): [True: 0, False: 2.16G]
  |  Branch (510:20): [True: 0, False: 2.16G]
  ------------------
  511|      0|        return;
  512|  2.16G|    if ((y < 0) || (y >= h))
  ------------------
  |  Branch (512:9): [True: 0, False: 2.16G]
  |  Branch (512:20): [True: 0, False: 2.16G]
  ------------------
  513|      0|        return;
  514|       |
  515|  2.16G|    sx = (size_t) x;
  516|  2.16G|    sy = (size_t) y;
  517|       |
  518|  2.16G|    byte = (sx >> 3) + sy * image->stride;
  519|  2.16G|    bit = 7 - ((int) (sx & 7));
  520|  2.16G|    mask = (uint8_t) ((1 << bit) ^ 0xff);
  521|       |
  522|  2.16G|    scratch = image->data[byte] & mask;
  523|  2.16G|    image->data[byte] = scratch | (value << bit);
  524|  2.16G|}
jbig2_image.c:jbig2_image_compose_opt_OR:
  290|   434k|{
  291|   434k|    if (early || late)
  ------------------
  |  Branch (291:9): [True: 430k, False: 3.19k]
  |  Branch (291:18): [True: 1.34k, False: 1.85k]
  ------------------
  292|   432k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_OR);
  293|  1.85k|    else
  294|  1.85k|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_OR);
  295|   434k|}
jbig2_image.c:template_image_compose_opt:
  166|  18.8M|{
  167|  18.8M|    int i;
  168|  18.8M|    uint32_t j;
  169|  18.8M|    int bytewidth = (int)bytewidth_;
  170|       |
  171|  18.8M|    if (bytewidth == 1) {
  ------------------
  |  Branch (171:9): [True: 8.46M, False: 10.4M]
  ------------------
  172|   221M|        for (j = 0; j < h; j++) {
  ------------------
  |  Branch (172:21): [True: 212M, False: 8.46M]
  ------------------
  173|       |            /* Only 1 byte! */
  174|   212M|            uint8_t v = (((early ? 0 : ss[0]<<8) | (late ? 0 : ss[1]))>>shift);
  ------------------
  |  Branch (174:28): [True: 116M, False: 95.8M]
  |  Branch (174:53): [True: 150k, False: 212M]
  ------------------
  175|   212M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (175:17): [True: 99.6M, False: 113M]
  ------------------
  176|  99.6M|                *dd |= v & leftmask;
  177|   113M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (177:22): [True: 35.1k, False: 113M]
  ------------------
  178|  35.1k|                *dd &= (v & leftmask) | ~leftmask;
  179|   113M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (179:22): [True: 16.6M, False: 96.5M]
  ------------------
  180|  16.6M|                *dd ^= v & leftmask;
  181|  96.5M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (181:22): [True: 184k, False: 96.3M]
  ------------------
  182|   184k|                *dd ^= (~v) & leftmask;
  183|  96.3M|            else /* Replace */
  184|  96.3M|                *dd = (v & leftmask) | (*dd & ~leftmask);
  185|   212M|            dd += dstride;
  186|   212M|            ss += sstride;
  187|   212M|        }
  188|  8.46M|        return;
  189|  8.46M|    }
  190|  10.4M|    bytewidth -= 2;
  191|  10.4M|    if (shift == 0) {
  ------------------
  |  Branch (191:9): [True: 3.15M, False: 7.26M]
  ------------------
  192|  3.15M|        ss++;
  193|  51.6M|        for (j = 0; j < h; j++) {
  ------------------
  |  Branch (193:21): [True: 48.4M, False: 3.15M]
  ------------------
  194|       |            /* Left byte */
  195|  48.4M|            const uint8_t * JBIG2_RESTRICT s = ss;
  196|  48.4M|            uint8_t * JBIG2_RESTRICT d = dd;
  197|  48.4M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (197:17): [True: 486k, False: 47.9M]
  ------------------
  198|   486k|                *d++ |= *s++ & leftmask;
  199|  47.9M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (199:22): [True: 47.6k, False: 47.9M]
  ------------------
  200|  47.6k|                *d++ &= (*s++ & leftmask) | ~leftmask;
  201|  47.9M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (201:22): [True: 1.61k, False: 47.9M]
  ------------------
  202|  1.61k|                *d++ ^= *s++ & leftmask;
  203|  47.9M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (203:22): [True: 103k, False: 47.8M]
  ------------------
  204|   103k|                *d++ ^= (~*s++) & leftmask;
  205|  47.8M|            else /* Replace */
  206|  47.8M|                *d = (*s++ & leftmask) | (*d & ~leftmask), d++;
  207|       |            /* Central run */
  208|  2.17G|            for (i = bytewidth; i != 0; i--) {
  ------------------
  |  Branch (208:33): [True: 2.12G, False: 48.4M]
  ------------------
  209|  2.12G|                if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (209:21): [True: 1.74G, False: 383M]
  ------------------
  210|  1.74G|                    *d++ |= *s++;
  211|   383M|                else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (211:26): [True: 94.1k, False: 383M]
  ------------------
  212|  94.1k|                    *d++ &= *s++;
  213|   383M|                else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (213:26): [True: 91.3k, False: 383M]
  ------------------
  214|  91.3k|                    *d++ ^= *s++;
  215|   383M|                else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (215:26): [True: 1.03M, False: 381M]
  ------------------
  216|  1.03M|                    *d++ ^= ~*s++;
  217|   381M|                else /* Replace */
  218|   381M|                    *d++ = *s++;
  219|  2.12G|            }
  220|       |            /* Right byte */
  221|  48.4M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (221:17): [True: 486k, False: 47.9M]
  ------------------
  222|   486k|                *d |= *s & rightmask;
  223|  47.9M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (223:22): [True: 47.6k, False: 47.9M]
  ------------------
  224|  47.6k|                *d &= (*s & rightmask) | ~rightmask;
  225|  47.9M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (225:22): [True: 1.61k, False: 47.9M]
  ------------------
  226|  1.61k|                *d ^= *s & rightmask;
  227|  47.9M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (227:22): [True: 103k, False: 47.8M]
  ------------------
  228|   103k|                *d ^= (~*s) & rightmask;
  229|  47.8M|            else /* Replace */
  230|  47.8M|                *d = (*s & rightmask) | (*d & ~rightmask);
  231|  48.4M|            dd += dstride;
  232|  48.4M|            ss += sstride;
  233|  48.4M|        }
  234|  7.26M|    } else {
  235|   104M|        for (j = 0; j < h; j++) {
  ------------------
  |  Branch (235:21): [True: 97.2M, False: 7.26M]
  ------------------
  236|       |            /* Left byte */
  237|  97.2M|            const uint8_t * JBIG2_RESTRICT s = ss;
  238|  97.2M|            uint8_t * JBIG2_RESTRICT d = dd;
  239|  97.2M|            uint8_t s0, s1, v;
  240|  97.2M|            s0 = early ? 0 : *s;
  ------------------
  |  Branch (240:18): [True: 2.53M, False: 94.7M]
  ------------------
  241|  97.2M|            s++;
  242|  97.2M|            s1 = *s++;
  243|  97.2M|            v = ((s0<<8) | s1)>>shift;
  244|  97.2M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (244:17): [True: 2.14M, False: 95.1M]
  ------------------
  245|  2.14M|                *d++ |= v & leftmask;
  246|  95.1M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (246:22): [True: 141k, False: 94.9M]
  ------------------
  247|   141k|                *d++ &= (v & leftmask) | ~leftmask;
  248|  94.9M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (248:22): [True: 269k, False: 94.6M]
  ------------------
  249|   269k|                *d++ ^= v & leftmask;
  250|  94.6M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (250:22): [True: 37.2k, False: 94.6M]
  ------------------
  251|  37.2k|                *d++ ^= (~v) & leftmask;
  252|  94.6M|            else /* Replace */
  253|  94.6M|                *d = (v & leftmask) | (*d & ~leftmask), d++;
  254|       |            /* Central run */
  255|  1.65G|            for (i = bytewidth; i > 0; i--) {
  ------------------
  |  Branch (255:33): [True: 1.55G, False: 97.2M]
  ------------------
  256|  1.55G|                s0 = s1; s1 = *s++;
  257|  1.55G|                v = ((s0<<8) | s1)>>shift;
  258|  1.55G|                if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (258:21): [True: 684M, False: 872M]
  ------------------
  259|   684M|                    *d++ |= v;
  260|   872M|                else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (260:26): [True: 923k, False: 871M]
  ------------------
  261|   923k|                    *d++ &= v;
  262|   871M|                else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (262:26): [True: 792k, False: 870M]
  ------------------
  263|   792k|                    *d++ ^= v;
  264|   870M|                else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (264:26): [True: 3.26M, False: 867M]
  ------------------
  265|  3.26M|                    *d++ ^= ~v;
  266|   867M|                else /* Replace */
  267|   867M|                    *d++ = v;
  268|  1.55G|            }
  269|       |            /* Right byte */
  270|  97.2M|            s0 = s1; s1 = (late ? 0 : *s);
  ------------------
  |  Branch (270:28): [True: 1.54M, False: 95.6M]
  ------------------
  271|  97.2M|            v = (((s0<<8) | s1)>>shift);
  272|  97.2M|            if (op == JBIG2_COMPOSE_OR)
  ------------------
  |  Branch (272:17): [True: 2.14M, False: 95.1M]
  ------------------
  273|  2.14M|                *d |= v & rightmask;
  274|  95.1M|            else if (op == JBIG2_COMPOSE_AND)
  ------------------
  |  Branch (274:22): [True: 141k, False: 94.9M]
  ------------------
  275|   141k|                *d &= (v & rightmask) | ~rightmask;
  276|  94.9M|            else if (op == JBIG2_COMPOSE_XOR)
  ------------------
  |  Branch (276:22): [True: 269k, False: 94.6M]
  ------------------
  277|   269k|                *d ^= v & rightmask;
  278|  94.6M|            else if (op == JBIG2_COMPOSE_XNOR)
  ------------------
  |  Branch (278:22): [True: 37.2k, False: 94.6M]
  ------------------
  279|  37.2k|                *d ^= ~v & rightmask;
  280|  94.6M|            else /* Replace */
  281|  94.6M|                *d = (v & rightmask) | (*d & ~rightmask);
  282|  97.2M|            dd += dstride;
  283|  97.2M|            ss += sstride;
  284|  97.2M|        }
  285|  7.26M|    }
  286|  10.4M|}
jbig2_image.c:jbig2_image_compose_opt_AND:
  299|  2.15k|{
  300|  2.15k|    if (early || late)
  ------------------
  |  Branch (300:9): [True: 919, False: 1.23k]
  |  Branch (300:18): [True: 492, False: 746]
  ------------------
  301|  1.41k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_AND);
  302|    746|    else
  303|    746|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_AND);
  304|  2.15k|}
jbig2_image.c:jbig2_image_compose_opt_XOR:
  308|   239k|{
  309|   239k|    if (early || late)
  ------------------
  |  Branch (309:9): [True: 236k, False: 2.97k]
  |  Branch (309:18): [True: 939, False: 2.03k]
  ------------------
  310|   237k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XOR);
  311|  2.03k|    else
  312|  2.03k|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XOR);
  313|   239k|}
jbig2_image.c:jbig2_image_compose_opt_XNOR:
  317|  2.35k|{
  318|  2.35k|    if (early || late)
  ------------------
  |  Branch (318:9): [True: 781, False: 1.56k]
  |  Branch (318:18): [True: 553, False: 1.01k]
  ------------------
  319|  1.33k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XNOR);
  320|  1.01k|    else
  321|  1.01k|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XNOR);
  322|  2.35k|}
jbig2_image.c:jbig2_image_compose_opt_REPLACE:
  326|  18.2M|{
  327|  18.2M|    if (early || late)
  ------------------
  |  Branch (327:9): [True: 11.0k, False: 18.1M]
  |  Branch (327:18): [True: 10.3k, False: 18.1M]
  ------------------
  328|  21.3k|        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_REPLACE);
  329|  18.1M|    else
  330|  18.1M|        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_REPLACE);
  331|  18.2M|}

jbig2_decode_generic_mmr:
 1207|  10.8k|{
 1208|  10.8k|    Jbig2MmrCtx mmr;
 1209|  10.8k|    const uint32_t rowstride = image->stride;
 1210|  10.8k|    byte *dst = image->data;
 1211|  10.8k|    byte *ref = NULL;
 1212|  10.8k|    uint32_t y;
 1213|  10.8k|    int code = 0;
 1214|  10.8k|    int eofb = 0;
 1215|       |
 1216|  10.8k|    (void) params;
 1217|       |
 1218|  10.8k|    jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
 1219|       |
 1220|   179M|    for (y = 0; !eofb && y < image->height; y++) {
  ------------------
  |  Branch (1220:17): [True: 179M, False: 2.02k]
  |  Branch (1220:26): [True: 179M, False: 8.63k]
  ------------------
 1221|   179M|        memset(dst, 0, rowstride);
 1222|   179M|        code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb);
 1223|   179M|        if (code < 0)
  ------------------
  |  Branch (1223:13): [True: 206, False: 179M]
  ------------------
 1224|    206|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode mmr line");
 1225|   179M|        ref = dst;
 1226|   179M|        dst += rowstride;
 1227|   179M|    }
 1228|       |
 1229|  10.6k|    if (eofb && y < image->height) {
  ------------------
  |  Branch (1229:9): [True: 2.02k, False: 8.63k]
  |  Branch (1229:17): [True: 1.90k, False: 118]
  ------------------
 1230|  1.90k|        memset(dst, 0, rowstride * (image->height - y));
 1231|  1.90k|    }
 1232|       |
 1233|  10.6k|    return code;
 1234|  10.8k|}
jbig2_decode_halftone_mmr:
 1252|  13.1k|{
 1253|  13.1k|    Jbig2MmrCtx mmr;
 1254|  13.1k|    const uint32_t rowstride = image->stride;
 1255|  13.1k|    byte *dst = image->data;
 1256|  13.1k|    byte *ref = NULL;
 1257|  13.1k|    uint32_t y;
 1258|  13.1k|    int code = 0;
 1259|  13.1k|    const uint32_t EOFB = 0x001001;
 1260|  13.1k|    int eofb = 0;
 1261|       |
 1262|  13.1k|    (void) params;
 1263|       |
 1264|  13.1k|    jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
 1265|       |
 1266|  58.4M|    for (y = 0; !eofb && y < image->height; y++) {
  ------------------
  |  Branch (1266:17): [True: 58.4M, False: 1.61k]
  |  Branch (1266:26): [True: 58.4M, False: 11.5k]
  ------------------
 1267|  58.4M|        memset(dst, 0, rowstride);
 1268|  58.4M|        code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb);
 1269|  58.4M|        if (code < 0)
  ------------------
  |  Branch (1269:13): [True: 67, False: 58.4M]
  ------------------
 1270|     67|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode halftone mmr line");
  ------------------
  |  |   68|     67|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1271|  58.4M|        ref = dst;
 1272|  58.4M|        dst += rowstride;
 1273|  58.4M|    }
 1274|       |
 1275|  13.1k|    if (eofb && y < image->height) {
  ------------------
  |  Branch (1275:9): [True: 1.61k, False: 11.5k]
  |  Branch (1275:17): [True: 1.41k, False: 202]
  ------------------
 1276|  1.41k|        memset(dst, 0, rowstride * (image->height - y));
 1277|  1.41k|    }
 1278|       |
 1279|       |    /* test for EOFB (see section 6.2.6) */
 1280|  13.1k|    if (mmr.word >> 8 == EOFB) {
  ------------------
  |  Branch (1280:9): [True: 1.38k, False: 11.7k]
  ------------------
 1281|  1.38k|        jbig2_decode_mmr_consume(&mmr, 24);
 1282|  1.38k|    }
 1283|       |
 1284|  13.1k|    *consumed_bytes += (mmr.consumed_bits + 7) / 8;
 1285|  13.1k|    return code;
 1286|  13.1k|}
jbig2_mmr.c:jbig2_decode_mmr_init:
   61|  24.0k|{
   62|  24.0k|    mmr->width = width;
   63|  24.0k|    mmr->height = height;
   64|  24.0k|    mmr->data = data;
   65|  24.0k|    mmr->size = size;
   66|  24.0k|    mmr->data_index = 0;
   67|  24.0k|    mmr->bit_index = 32;
   68|  24.0k|    mmr->word = 0;
   69|  24.0k|    mmr->consumed_bits = 0;
   70|       |
   71|  96.4k|    while (mmr->bit_index >= 8 && mmr->data_index < mmr->size) {
  ------------------
  |  Branch (71:12): [True: 82.0k, False: 14.3k]
  |  Branch (71:35): [True: 72.3k, False: 9.70k]
  ------------------
   72|  72.3k|        mmr->bit_index -= 8;
   73|  72.3k|        mmr->word |= (mmr->data[mmr->data_index] << mmr->bit_index);
   74|  72.3k|        mmr->data_index++;
   75|  72.3k|    }
   76|  24.0k|}
jbig2_mmr.c:jbig2_decode_mmr_line:
  985|   237M|{
  986|   237M|    uint32_t a0 = MINUS1;
  ------------------
  |  |   54|   237M|#define MINUS1 UINT32_MAX
  ------------------
  987|   237M|    uint32_t a1, a2, b1, b2;
  988|   237M|    int c = 0;                  /* 0 is white, black is 1 */
  989|       |
  990|   241M|    while (1) {
  ------------------
  |  Branch (990:12): [True: 241M, Folded]
  ------------------
  991|   241M|        uint32_t word = mmr->word;
  992|       |
  993|       |        /* printf ("%08x\n", word); */
  994|       |
  995|   241M|        if (a0 != MINUS1 && a0 >= mmr->width)
  ------------------
  |  |   54|   482M|#define MINUS1 UINT32_MAX
  ------------------
  |  Branch (995:13): [True: 3.73M, False: 237M]
  |  Branch (995:29): [True: 1.24M, False: 2.49M]
  ------------------
  996|  1.24M|            break;
  997|       |
  998|   239M|        if ((word >> (32 - 3)) == 1) {
  ------------------
  |  Branch (998:13): [True: 876k, False: 239M]
  ------------------
  999|   876k|            int white_run, black_run;
 1000|       |
 1001|   876k|            jbig2_decode_mmr_consume(mmr, 3);
 1002|       |
 1003|   876k|            if (a0 == MINUS1)
  ------------------
  |  |   54|   876k|#define MINUS1 UINT32_MAX
  ------------------
  |  Branch (1003:17): [True: 70.8k, False: 805k]
  ------------------
 1004|  70.8k|                a0 = 0;
 1005|       |
 1006|   876k|            if (c == 0) {
  ------------------
  |  Branch (1006:17): [True: 785k, False: 91.0k]
  ------------------
 1007|   785k|                white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8);
 1008|   785k|                if (white_run < 0)
  ------------------
  |  Branch (1008:21): [True: 52, False: 785k]
  ------------------
 1009|     52|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode white H run");
  ------------------
  |  |   68|     52|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1010|   785k|                black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7);
 1011|   785k|                if (black_run < 0)
  ------------------
  |  Branch (1011:21): [True: 162, False: 785k]
  ------------------
 1012|    162|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode black H run");
  ------------------
  |  |   68|    162|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1013|       |                /* printf ("H %d %d\n", white_run, black_run); */
 1014|   785k|                a1 = a0 + white_run;
 1015|   785k|                a2 = a1 + black_run;
 1016|   785k|                if (a1 > mmr->width)
  ------------------
  |  Branch (1016:21): [True: 43.7k, False: 741k]
  ------------------
 1017|  43.7k|                    a1 = mmr->width;
 1018|   785k|                if (a2 > mmr->width)
  ------------------
  |  Branch (1018:21): [True: 47.8k, False: 737k]
  ------------------
 1019|  47.8k|                    a2 = mmr->width;
 1020|   785k|                if (a2 < a1) {
  ------------------
  |  Branch (1020:21): [True: 0, False: 785k]
  ------------------
 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|   785k|                if (a1 < mmr->width)
  ------------------
  |  Branch (1024:21): [True: 741k, False: 44.1k]
  ------------------
 1025|   741k|                    jbig2_set_bits(dst, a1, a2);
 1026|   785k|                a0 = a2;
 1027|   785k|            } else {
 1028|  91.0k|                black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7);
 1029|  91.0k|                if (black_run < 0)
  ------------------
  |  Branch (1029:21): [True: 19, False: 91.0k]
  ------------------
 1030|     19|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode black H run");
  ------------------
  |  |   68|     19|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1031|  91.0k|                white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8);
 1032|  91.0k|                if (white_run < 0)
  ------------------
  |  Branch (1032:21): [True: 40, False: 91.0k]
  ------------------
 1033|     40|                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode white H run");
  ------------------
  |  |   68|     40|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1034|       |                /* printf ("H %d %d\n", black_run, white_run); */
 1035|  91.0k|                a1 = a0 + black_run;
 1036|  91.0k|                a2 = a1 + white_run;
 1037|  91.0k|                if (a1 > mmr->width)
  ------------------
  |  Branch (1037:21): [True: 18.2k, False: 72.8k]
  ------------------
 1038|  18.2k|                    a1 = mmr->width;
 1039|  91.0k|                if (a2 > mmr->width)
  ------------------
  |  Branch (1039:21): [True: 33.7k, False: 57.2k]
  ------------------
 1040|  33.7k|                    a2 = mmr->width;
 1041|  91.0k|                if (a1 < a0) {
  ------------------
  |  Branch (1041:21): [True: 0, False: 91.0k]
  ------------------
 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|  91.0k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1045:21): [True: 91.0k, False: 0]
  ------------------
 1046|  91.0k|                    jbig2_set_bits(dst, a0, a1);
 1047|  91.0k|                a0 = a2;
 1048|  91.0k|            }
 1049|   876k|        }
 1050|       |
 1051|   239M|        else if ((word >> (32 - 4)) == 1) {
  ------------------
  |  Branch (1051:18): [True: 271k, False: 238M]
  ------------------
 1052|       |            /* printf ("P\n"); */
 1053|   271k|            jbig2_decode_mmr_consume(mmr, 4);
 1054|   271k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1055|   271k|            b2 = jbig2_find_changing_element(ref, b1, mmr->width);
 1056|   271k|            if (c) {
  ------------------
  |  Branch (1056:17): [True: 130k, False: 140k]
  ------------------
 1057|   130k|                if (b2 < a0) {
  ------------------
  |  Branch (1057:21): [True: 0, False: 130k]
  ------------------
 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|   130k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1061:21): [True: 130k, False: 0]
  ------------------
 1062|   130k|                    jbig2_set_bits(dst, a0, b2);
 1063|   130k|            }
 1064|   271k|            a0 = b2;
 1065|   271k|        }
 1066|       |
 1067|   238M|        else if ((word >> (32 - 1)) == 1) {
  ------------------
  |  Branch (1067:18): [True: 1.29M, False: 237M]
  ------------------
 1068|       |            /* printf ("V(0)\n"); */
 1069|  1.29M|            jbig2_decode_mmr_consume(mmr, 1);
 1070|  1.29M|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1071|  1.29M|            if (c) {
  ------------------
  |  Branch (1071:17): [True: 470k, False: 823k]
  ------------------
 1072|   470k|                if (b1 < a0) {
  ------------------
  |  Branch (1072:21): [True: 0, False: 470k]
  ------------------
 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|   470k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1076:21): [True: 470k, False: 0]
  ------------------
 1077|   470k|                    jbig2_set_bits(dst, a0, b1);
 1078|   470k|            }
 1079|  1.29M|            a0 = b1;
 1080|  1.29M|            c = !c;
 1081|  1.29M|        }
 1082|       |
 1083|   237M|        else if ((word >> (32 - 3)) == 3) {
  ------------------
  |  Branch (1083:18): [True: 534k, False: 236M]
  ------------------
 1084|       |            /* printf ("VR(1)\n"); */
 1085|   534k|            jbig2_decode_mmr_consume(mmr, 3);
 1086|   534k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1087|   534k|            if (b1 + 1 <= mmr->width)
  ------------------
  |  Branch (1087:17): [True: 229k, False: 305k]
  ------------------
 1088|   229k|                b1 += 1;
 1089|   534k|            if (c) {
  ------------------
  |  Branch (1089:17): [True: 203k, False: 330k]
  ------------------
 1090|   203k|                if (b1 < a0) {
  ------------------
  |  Branch (1090:21): [True: 0, False: 203k]
  ------------------
 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|   203k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1094:21): [True: 203k, False: 0]
  ------------------
 1095|   203k|                    jbig2_set_bits(dst, a0, b1);
 1096|   203k|            }
 1097|   534k|            a0 = b1;
 1098|   534k|            c = !c;
 1099|   534k|        }
 1100|       |
 1101|   236M|        else if ((word >> (32 - 6)) == 3) {
  ------------------
  |  Branch (1101:18): [True: 117k, False: 236M]
  ------------------
 1102|       |            /* printf ("VR(2)\n"); */
 1103|   117k|            jbig2_decode_mmr_consume(mmr, 6);
 1104|   117k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1105|   117k|            if (b1 + 2 <= mmr->width)
  ------------------
  |  Branch (1105:17): [True: 34.6k, False: 82.6k]
  ------------------
 1106|  34.6k|                b1 += 2;
 1107|   117k|            if (c) {
  ------------------
  |  Branch (1107:17): [True: 68.3k, False: 49.0k]
  ------------------
 1108|  68.3k|                if (b1 < a0) {
  ------------------
  |  Branch (1108:21): [True: 0, False: 68.3k]
  ------------------
 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|  68.3k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1112:21): [True: 68.3k, False: 0]
  ------------------
 1113|  68.3k|                    jbig2_set_bits(dst, a0, b1);
 1114|  68.3k|            }
 1115|   117k|            a0 = b1;
 1116|   117k|            c = !c;
 1117|   117k|        }
 1118|       |
 1119|   236M|        else if ((word >> (32 - 7)) == 3) {
  ------------------
  |  Branch (1119:18): [True: 47.7k, False: 236M]
  ------------------
 1120|       |            /* printf ("VR(3)\n"); */
 1121|  47.7k|            jbig2_decode_mmr_consume(mmr, 7);
 1122|  47.7k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1123|  47.7k|            if (b1 + 3 <= mmr->width)
  ------------------
  |  Branch (1123:17): [True: 28.6k, False: 19.1k]
  ------------------
 1124|  28.6k|                b1 += 3;
 1125|  47.7k|            if (c) {
  ------------------
  |  Branch (1125:17): [True: 21.4k, False: 26.3k]
  ------------------
 1126|  21.4k|                if (b1 < a0) {
  ------------------
  |  Branch (1126:21): [True: 0, False: 21.4k]
  ------------------
 1127|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VR(3) run");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1128|      0|                    b1 = a0;
 1129|      0|                }
 1130|  21.4k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1130:21): [True: 21.4k, False: 0]
  ------------------
 1131|  21.4k|                    jbig2_set_bits(dst, a0, b1);
 1132|  21.4k|            }
 1133|  47.7k|            a0 = b1;
 1134|  47.7k|            c = !c;
 1135|  47.7k|        }
 1136|       |
 1137|   236M|        else if ((word >> (32 - 3)) == 2) {
  ------------------
  |  Branch (1137:18): [True: 373k, False: 236M]
  ------------------
 1138|       |            /* printf ("VL(1)\n"); */
 1139|   373k|            jbig2_decode_mmr_consume(mmr, 3);
 1140|   373k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1141|   373k|            if (b1 >= 1)
  ------------------
  |  Branch (1141:17): [True: 371k, False: 2.52k]
  ------------------
 1142|   371k|                b1 -= 1;
 1143|   373k|            if (c) {
  ------------------
  |  Branch (1143:17): [True: 115k, False: 258k]
  ------------------
 1144|   115k|                if (b1 < a0) {
  ------------------
  |  Branch (1144:21): [True: 0, False: 115k]
  ------------------
 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|   115k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1148:21): [True: 115k, False: 0]
  ------------------
 1149|   115k|                    jbig2_set_bits(dst, a0, b1);
 1150|   115k|            }
 1151|   373k|            a0 = b1;
 1152|   373k|            c = !c;
 1153|   373k|        }
 1154|       |
 1155|   236M|        else if ((word >> (32 - 6)) == 2) {
  ------------------
  |  Branch (1155:18): [True: 137k, False: 236M]
  ------------------
 1156|       |            /* printf ("VL(2)\n"); */
 1157|   137k|            jbig2_decode_mmr_consume(mmr, 6);
 1158|   137k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1159|   137k|            if (b1 >= 2)
  ------------------
  |  Branch (1159:17): [True: 135k, False: 1.46k]
  ------------------
 1160|   135k|                b1 -= 2;
 1161|   137k|            if (c) {
  ------------------
  |  Branch (1161:17): [True: 37.1k, False: 100k]
  ------------------
 1162|  37.1k|                if (b1 < a0) {
  ------------------
  |  Branch (1162:21): [True: 7.62k, False: 29.5k]
  ------------------
 1163|  7.62k|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(2) run");
  ------------------
  |  |   68|  7.62k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1164|  7.62k|                    b1 = a0;
 1165|  7.62k|                }
 1166|  37.1k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1166:21): [True: 37.1k, False: 0]
  ------------------
 1167|  37.1k|                    jbig2_set_bits(dst, a0, b1);
 1168|  37.1k|            }
 1169|   137k|            a0 = b1;
 1170|   137k|            c = !c;
 1171|   137k|        }
 1172|       |
 1173|   236M|        else if ((word >> (32 - 7)) == 2) {
  ------------------
  |  Branch (1173:18): [True: 81.6k, False: 236M]
  ------------------
 1174|       |            /* printf ("VL(3)\n"); */
 1175|  81.6k|            jbig2_decode_mmr_consume(mmr, 7);
 1176|  81.6k|            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
 1177|  81.6k|            if (b1 >= 3)
  ------------------
  |  Branch (1177:17): [True: 76.1k, False: 5.54k]
  ------------------
 1178|  76.1k|                b1 -= 3;
 1179|  81.6k|            if (c) {
  ------------------
  |  Branch (1179:17): [True: 30.1k, False: 51.4k]
  ------------------
 1180|  30.1k|                if (b1 < a0) {
  ------------------
  |  Branch (1180:21): [True: 8.41k, False: 21.7k]
  ------------------
 1181|  8.41k|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(3) run");
  ------------------
  |  |   68|  8.41k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
 1182|  8.41k|                    b1 = a0;
 1183|  8.41k|                }
 1184|  30.1k|                if (a0 < mmr->width)
  ------------------
  |  Branch (1184:21): [True: 30.1k, False: 0]
  ------------------
 1185|  30.1k|                    jbig2_set_bits(dst, a0, b1);
 1186|  30.1k|            }
 1187|  81.6k|            a0 = b1;
 1188|  81.6k|            c = !c;
 1189|  81.6k|        }
 1190|       |
 1191|   236M|        else if ((word >> (32 - 24)) == 0x1001) {
  ------------------
  |  Branch (1191:18): [True: 3.64k, False: 236M]
  ------------------
 1192|       |            /* printf ("EOFB\n"); */
 1193|  3.64k|            jbig2_decode_mmr_consume(mmr, 24);
 1194|  3.64k|            *eofb = 1;
 1195|  3.64k|            break;
 1196|  3.64k|        }
 1197|       |
 1198|   236M|        else
 1199|   236M|            break;
 1200|   239M|    }
 1201|       |
 1202|   237M|    return 0;
 1203|   237M|}
jbig2_mmr.c:jbig2_decode_get_run:
  965|  1.75M|{
  966|  1.75M|    int result = 0;
  967|  1.75M|    int val;
  968|       |
  969|  5.21M|    do {
  970|  5.21M|        val = jbig2_decode_get_code(mmr, table, initial_bits);
  971|  5.21M|        if (val == ERROR)
  ------------------
  |  |   55|  5.21M|#define ERROR -1
  ------------------
  |  Branch (971:13): [True: 12, False: 5.21M]
  ------------------
  972|     12|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "invalid code detected in MMR-coded data");
  ------------------
  |  |   68|     12|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  973|  5.21M|        else if (val == UNCOMPRESSED)
  ------------------
  |  |   57|  5.21M|#define UNCOMPRESSED -3
  ------------------
  |  Branch (973:18): [True: 5, False: 5.21M]
  ------------------
  974|      5|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "uncompressed code in MMR-coded data");
  ------------------
  |  |   68|      5|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  975|  5.21M|        else if (val == ZEROES)
  ------------------
  |  |   56|  5.21M|#define ZEROES -2
  ------------------
  |  Branch (975:18): [True: 256, False: 5.21M]
  ------------------
  976|    256|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "zeroes code in MMR-coded data");
  ------------------
  |  |   68|    256|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  977|  5.21M|        result += val;
  978|  5.21M|    } while (val >= 64);
  ------------------
  |  Branch (978:14): [True: 3.46M, False: 1.75M]
  ------------------
  979|       |
  980|  1.75M|    return result;
  981|  1.75M|}
jbig2_mmr.c:jbig2_decode_get_code:
  944|  5.21M|{
  945|  5.21M|    uint32_t word = mmr->word;
  946|  5.21M|    int table_ix = word >> (32 - initial_bits);
  947|  5.21M|    int val = table[table_ix].val;
  948|  5.21M|    int n_bits = table[table_ix].n_bits;
  949|       |
  950|  5.21M|    if (n_bits > initial_bits) {
  ------------------
  |  Branch (950:9): [True: 13.0k, False: 5.20M]
  ------------------
  951|  13.0k|        int mask = (1 << (32 - initial_bits)) - 1;
  952|       |
  953|  13.0k|        table_ix = val + ((word & mask) >> (32 - n_bits));
  954|  13.0k|        val = table[table_ix].val;
  955|  13.0k|        n_bits = initial_bits + table[table_ix].n_bits;
  956|  13.0k|    }
  957|       |
  958|  5.21M|    jbig2_decode_mmr_consume(mmr, n_bits);
  959|       |
  960|  5.21M|    return val;
  961|  5.21M|}
jbig2_mmr.c:jbig2_set_bits:
  922|  1.90M|{
  923|  1.90M|    uint32_t a0, a1, b0, b1, a;
  924|       |
  925|  1.90M|    a0 = x0 >> 3;
  926|  1.90M|    a1 = x1 >> 3;
  927|       |
  928|  1.90M|    b0 = x0 & 7;
  929|  1.90M|    b1 = x1 & 7;
  930|       |
  931|  1.90M|    if (a0 == a1) {
  ------------------
  |  Branch (931:9): [True: 838k, False: 1.07M]
  ------------------
  932|   838k|        line[a0] |= lm[b0] & rm[b1];
  933|  1.07M|    } else {
  934|  1.07M|        line[a0] |= lm[b0];
  935|  2.29G|        for (a = a0 + 1; a < a1; a++)
  ------------------
  |  Branch (935:26): [True: 2.29G, False: 1.07M]
  ------------------
  936|  2.29G|            line[a] = 0xFF;
  937|  1.07M|        if (b1)
  ------------------
  |  Branch (937:13): [True: 787k, False: 283k]
  ------------------
  938|   787k|            line[a1] |= rm[b1];
  939|  1.07M|    }
  940|  1.90M|}
jbig2_mmr.c:jbig2_find_changing_element_of_color:
  908|  2.85M|{
  909|  2.85M|    if (line == NULL)
  ------------------
  |  Branch (909:9): [True: 16.7k, False: 2.84M]
  ------------------
  910|  16.7k|        return w;
  911|  2.84M|    x = jbig2_find_changing_element(line, x, w);
  912|  2.84M|    if (x < w && getbit(line, x) != color)
  ------------------
  |  |  745|  1.73M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  |  Branch (912:9): [True: 1.73M, False: 1.10M]
  |  Branch (912:18): [True: 349k, False: 1.38M]
  ------------------
  913|   349k|        x = jbig2_find_changing_element(line, x, w);
  914|  2.84M|    return x;
  915|  2.85M|}
jbig2_mmr.c:jbig2_find_changing_element:
  759|  3.46M|{
  760|  3.46M|    int a;
  761|  3.46M|    uint8_t     all8;
  762|  3.46M|    uint16_t    all16;
  763|  3.46M|    uint32_t    all32;
  764|       |
  765|  3.46M|    if (line == NULL)
  ------------------
  |  Branch (765:9): [True: 629, False: 3.46M]
  ------------------
  766|    629|        return w;
  767|       |
  768|  3.46M|    if (x == MINUS1) {
  ------------------
  |  |   54|  3.46M|#define MINUS1 UINT32_MAX
  ------------------
  |  Branch (768:9): [True: 1.16M, False: 2.29M]
  ------------------
  769|  1.16M|        a = 0;
  770|  1.16M|        x = 0;
  771|  2.29M|    } else if (x < w) {
  ------------------
  |  Branch (771:16): [True: 2.10M, False: 183k]
  ------------------
  772|  2.10M|        a = getbit(line, x);
  ------------------
  |  |  745|  2.10M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  773|  2.10M|        x++;
  774|  2.10M|    } else {
  775|   183k|        return x;
  776|   183k|    }
  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.27M|    all8  = (a) ? 0xff : 0;
  ------------------
  |  Branch (780:13): [True: 1.17M, False: 2.10M]
  ------------------
  781|  3.27M|    all16 = (a) ? 0xffff : 0;
  ------------------
  |  Branch (781:13): [True: 1.17M, False: 2.10M]
  ------------------
  782|  3.27M|    all32 = (a) ? 0xffffffff : 0;
  ------------------
  |  Branch (782:13): [True: 1.17M, False: 2.10M]
  ------------------
  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.27M|    if ( ((uint8_t*) line)[ x / 8] == all8) {
  ------------------
  |  Branch (789:10): [True: 1.37M, False: 1.90M]
  ------------------
  790|       |        /* Don't bother checking individual bits if the enclosing uint8 equals
  791|       |        all8 - just move to the next byte. */
  792|  1.37M|        x = x / 8 * 8 + 8;
  793|  1.37M|        if (x >= w) {
  ------------------
  |  Branch (793:13): [True: 134k, False: 1.23M]
  ------------------
  794|   134k|            x = w;
  795|   134k|            goto end;
  796|   134k|        }
  797|  1.90M|    } else {
  798|  4.36M|        for(;;) {
  799|  4.36M|            if (x == w) {
  ------------------
  |  Branch (799:17): [True: 417k, False: 3.94M]
  ------------------
  800|   417k|                goto end;
  801|   417k|            }
  802|  3.94M|            if (x % 8 == 0) {
  ------------------
  |  Branch (802:17): [True: 970k, False: 2.97M]
  ------------------
  803|   970k|                break;
  804|   970k|            }
  805|  2.97M|            if (getbit(line, x) != a) {
  ------------------
  |  |  745|  2.97M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  |  Branch (805:17): [True: 516k, False: 2.46M]
  ------------------
  806|   516k|                goto end;
  807|   516k|            }
  808|  2.46M|            x += 1;
  809|  2.46M|        }
  810|  1.90M|    }
  811|       |
  812|  3.27M|    assert(x % 8 == 0);
  ------------------
  |  Branch (812:5): [True: 0, False: 2.20M]
  |  Branch (812:5): [True: 2.20M, False: 0]
  ------------------
  813|       |    /* Check next uint8 if we are not on 16-bit boundary. */
  814|  2.20M|    if (x % 16) {
  ------------------
  |  Branch (814:9): [True: 1.41M, False: 791k]
  ------------------
  815|  1.41M|        if (w - x < 8) {
  ------------------
  |  Branch (815:13): [True: 7.22k, False: 1.41M]
  ------------------
  816|  7.22k|            goto check1;
  817|  7.22k|        }
  818|  1.41M|        if ( ((uint8_t*) line)[ x / 8] != all8) {
  ------------------
  |  Branch (818:14): [True: 184k, False: 1.22M]
  ------------------
  819|   184k|            goto check1;
  820|   184k|        }
  821|  1.22M|        x += 8; /* This will make x a multiple of 16. */
  822|  1.22M|    }
  823|       |
  824|  2.20M|    assert(x % 16 == 0);
  ------------------
  |  Branch (824:5): [True: 0, False: 2.01M]
  |  Branch (824:5): [True: 2.01M, False: 0]
  ------------------
  825|       |    /* Check next uint16 if we are not on 32-bit boundary. */
  826|  2.01M|    if (x % 32) {
  ------------------
  |  Branch (826:9): [True: 1.33M, False: 680k]
  ------------------
  827|  1.33M|        if (w - x < 16) {
  ------------------
  |  Branch (827:13): [True: 11.9k, False: 1.32M]
  ------------------
  828|  11.9k|            goto check8;
  829|  11.9k|        }
  830|  1.32M|        if ( getword16((line + (x / 8))) != all16) {
  ------------------
  |  |  754|  1.32M|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  ------------------
  |  Branch (830:14): [True: 125k, False: 1.20M]
  ------------------
  831|   125k|            goto check8_no_eof;
  832|   125k|        }
  833|  1.20M|        x += 16; /* This will make x a multiple of 32. */
  834|  1.20M|    }
  835|       |
  836|       |    /* We are now on a 32-bit boundary. Check uint32's until we reach last
  837|       |    sub-32-bit region. */
  838|  2.01M|    assert(x % 32 == 0);
  ------------------
  |  Branch (838:5): [True: 0, False: 1.88M]
  |  Branch (838:5): [True: 1.88M, False: 0]
  ------------------
  839|  1.35G|    for(;;) {
  840|  1.35G|        if (w - x < 32) {
  ------------------
  |  Branch (840:13): [True: 1.16M, False: 1.35G]
  ------------------
  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.16M|            goto check16;
  847|  1.16M|        }
  848|  1.35G|        if ( getword32((line + (x / 8))) != all32) {
  ------------------
  |  |  755|  1.35G|#define getword32(b)  ((uint32_t)(getword16(b) | (getword16((b + 2)) << 16)))
  |  |  ------------------
  |  |  |  |  754|  1.35G|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  |  |  ------------------
  |  |               #define getword32(b)  ((uint32_t)(getword16(b) | (getword16((b + 2)) << 16)))
  |  |  ------------------
  |  |  |  |  754|  1.35G|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  |  |  ------------------
  ------------------
  |  Branch (848:14): [True: 716k, False: 1.35G]
  ------------------
  849|   716k|            goto check16_no_eof;
  850|   716k|        }
  851|  1.35G|        x += 32;
  852|  1.35G|    }
  853|       |
  854|       |    /* Check next uint16. */
  855|  1.16M|check16:
  856|  1.16M|    assert(x % 16 == 0);
  ------------------
  |  Branch (856:5): [True: 0, False: 1.16M]
  |  Branch (856:5): [True: 1.16M, False: 0]
  ------------------
  857|  1.16M|    if (w - x < 16) {
  ------------------
  |  Branch (857:9): [True: 1.11M, False: 45.1k]
  ------------------
  858|  1.11M|        goto check8;
  859|  1.11M|    }
  860|   761k|check16_no_eof:
  861|   761k|    assert(w - x >= 16);
  ------------------
  |  Branch (861:5): [True: 0, False: 761k]
  |  Branch (861:5): [True: 761k, False: 0]
  ------------------
  862|   761k|    if ( getword16((line + (x / 8))) != all16) {
  ------------------
  |  |  754|   761k|#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
  ------------------
  |  Branch (862:10): [True: 323k, False: 437k]
  ------------------
  863|   323k|        goto check8_no_eof;
  864|   323k|    }
  865|   437k|    x += 16;
  866|       |
  867|       |    /* Check next uint8. */
  868|  1.56M|check8:
  869|  1.56M|    assert(x % 8 == 0);
  ------------------
  |  Branch (869:5): [True: 0, False: 1.56M]
  |  Branch (869:5): [True: 1.56M, False: 0]
  ------------------
  870|  1.56M|    if (w - x < 8) {
  ------------------
  |  Branch (870:9): [True: 1.11M, False: 455k]
  ------------------
  871|  1.11M|        goto check1;
  872|  1.11M|    }
  873|   904k|check8_no_eof:
  874|   904k|    assert(w - x >= 8);
  ------------------
  |  Branch (874:5): [True: 0, False: 904k]
  |  Branch (874:5): [True: 904k, False: 0]
  ------------------
  875|   904k|    if ( ((uint8_t*) line)[x/8] != all8) {
  ------------------
  |  Branch (875:10): [True: 435k, False: 469k]
  ------------------
  876|   435k|        goto check1;
  877|   435k|    }
  878|   469k|    x += 8;
  879|       |
  880|       |    /* Check up to the next 8 bits. */
  881|  2.20M|check1:
  882|  2.20M|    assert(x % 8 == 0);
  ------------------
  |  Branch (882:5): [True: 0, False: 2.20M]
  |  Branch (882:5): [True: 2.20M, False: 0]
  ------------------
  883|  2.20M|    if ( ((uint8_t*) line)[ x / 8] == all8) {
  ------------------
  |  Branch (883:10): [True: 509k, False: 1.70M]
  ------------------
  884|   509k|        x = w;
  885|   509k|        goto end;
  886|   509k|    }
  887|  1.70M|    {
  888|  6.35M|        for(;;) {
  889|  6.35M|            if (x == w) {
  ------------------
  |  Branch (889:17): [True: 264k, False: 6.09M]
  ------------------
  890|   264k|                goto end;
  891|   264k|            }
  892|  6.09M|            if (getbit(line, x) != a) {
  ------------------
  |  |  745|  6.09M|#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  ------------------
  |  Branch (892:17): [True: 1.43M, False: 4.65M]
  ------------------
  893|  1.43M|                goto end;
  894|  1.43M|            }
  895|  4.65M|            x += 1;
  896|  4.65M|        }
  897|  1.70M|    }
  898|       |
  899|  3.27M|end:
  900|  3.27M|    return x;
  901|  1.70M|}
jbig2_mmr.c:jbig2_decode_mmr_consume:
   80|  8.95M|{
   81|  8.95M|    mmr->consumed_bits += n_bits;
   82|  8.95M|    if (mmr->consumed_bits > mmr->size * 8)
  ------------------
  |  Branch (82:9): [True: 2.66k, False: 8.95M]
  ------------------
   83|  2.66k|        mmr->consumed_bits = mmr->size * 8;
   84|       |
   85|  8.95M|    mmr->word <<= n_bits;
   86|  8.95M|    mmr->bit_index += n_bits;
   87|  14.3M|    while (mmr->bit_index >= 8 && mmr->data_index < mmr->size) {
  ------------------
  |  Branch (87:12): [True: 5.42M, False: 8.90M]
  |  Branch (87:35): [True: 5.37M, False: 46.9k]
  ------------------
   88|  5.37M|        mmr->bit_index -= 8;
   89|  5.37M|        mmr->word |= (mmr->data[mmr->data_index] << mmr->bit_index);
   90|  5.37M|        mmr->data_index++;
   91|  5.37M|    }
   92|  8.95M|}

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

jbig2_decode_refinement_region:
  375|  92.9k|{
  376|  92.9k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  377|  92.9k|                "decoding generic refinement region with offset %d,%x, GRTEMPLATE=%d, TPGRON=%d",
  378|  92.9k|                params->GRREFERENCEDX, params->GRREFERENCEDY, params->GRTEMPLATE, params->TPGRON);
  379|       |
  380|  92.9k|    if (params->TPGRON)
  ------------------
  |  Branch (380:9): [True: 533, False: 92.4k]
  ------------------
  381|    533|        return jbig2_decode_refinement_TPGRON(ctx, params, as, image, GR_stats);
  382|       |
  383|  92.4k|    if (params->GRTEMPLATE)
  ------------------
  |  Branch (383:9): [True: 6.65k, False: 85.7k]
  ------------------
  384|  6.65k|        return jbig2_decode_refinement_template1_unopt(ctx, segment, params, as, image, GR_stats);
  385|  85.7k|    else
  386|  85.7k|        return jbig2_decode_refinement_template0_unopt(ctx, segment, params, as, image, GR_stats);
  387|  92.4k|}
jbig2_refinement_region:
  427|    930|{
  428|    930|    Jbig2RefinementRegionParams params;
  429|    930|    Jbig2RegionSegmentInfo rsi;
  430|    930|    int offset = 0;
  431|    930|    byte seg_flags;
  432|    930|    int code = 0;
  433|       |
  434|       |    /* 7.4.7 */
  435|    930|    if (segment->data_length < 18)
  ------------------
  |  Branch (435:9): [True: 8, False: 922]
  ------------------
  436|      8|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  437|       |
  438|    922|    jbig2_get_region_segment_info(&rsi, segment_data);
  439|    922|    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|    922|    seg_flags = segment_data[17];
  443|    922|    params.GRTEMPLATE = seg_flags & 0x01;
  444|    922|    params.TPGRON = seg_flags & 0x02 ? 1 : 0;
  ------------------
  |  Branch (444:21): [True: 554, False: 368]
  ------------------
  445|    922|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  446|    922|                "segment flags = %02x %s%s", seg_flags, params.GRTEMPLATE ? " GRTEMPLATE" : "", params.TPGRON ? " TPGRON" : "");
  ------------------
  |  Branch (446:57): [True: 336, False: 586]
  |  Branch (446:97): [True: 554, False: 368]
  ------------------
  447|    922|    if (seg_flags & 0xFC)
  ------------------
  |  Branch (447:9): [True: 477, False: 445]
  ------------------
  448|    477|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved segment flag bits are non-zero");
  449|    922|    offset += 18;
  450|       |
  451|       |    /* 7.4.7.3 */
  452|    922|    if (!params.GRTEMPLATE) {
  ------------------
  |  Branch (452:9): [True: 586, False: 336]
  ------------------
  453|    586|        if (segment->data_length < 22)
  ------------------
  |  Branch (453:13): [True: 4, False: 582]
  ------------------
  454|      4|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  455|    582|        params.grat[0] = segment_data[offset + 0];
  456|    582|        params.grat[1] = segment_data[offset + 1];
  457|    582|        params.grat[2] = segment_data[offset + 2];
  458|    582|        params.grat[3] = segment_data[offset + 3];
  459|    582|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  460|    582|                    "grat1: (%d, %d) grat2: (%d, %d)", params.grat[0], params.grat[1], params.grat[2], params.grat[3]);
  461|    582|        offset += 4;
  462|    582|    }
  463|       |
  464|       |    /* 7.4.7.4 - set up the reference image */
  465|    918|    if (segment->referred_to_segment_count) {
  ------------------
  |  Branch (465:9): [True: 238, False: 680]
  ------------------
  466|    238|        Jbig2Segment *ref;
  467|       |
  468|    238|        ref = jbig2_region_find_referred(ctx, segment);
  469|    238|        if (ref == NULL)
  ------------------
  |  Branch (469:13): [True: 27, False: 211]
  ------------------
  470|     27|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to find reference bitmap");
  471|    211|        if (ref->result == NULL)
  ------------------
  |  Branch (471:13): [True: 0, False: 211]
  ------------------
  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|    211|        params.GRREFERENCE = jbig2_image_reference(ctx, (Jbig2Image *) ref->result);
  479|    211|        jbig2_image_release(ctx, (Jbig2Image *) ref->result);
  480|    211|        ref->result = NULL;
  481|    211|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "found reference bitmap in segment %d", ref->number);
  482|    680|    } else {
  483|       |        /* the reference is just (a subset of) the page buffer */
  484|    680|        if (ctx->pages[ctx->current_page].image == NULL)
  ------------------
  |  Branch (484:13): [True: 5, False: 675]
  ------------------
  485|      5|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "reference page bitmap has no decoded image");
  486|    675|        params.GRREFERENCE = jbig2_image_reference(ctx, ctx->pages[ctx->current_page].image);
  487|       |        /* TODO: subset the image if appropriate */
  488|    675|    }
  489|       |
  490|       |    /* 7.4.7.5 */
  491|    886|    params.GRREFERENCEDX = 0;
  492|    886|    params.GRREFERENCEDY = 0;
  493|    886|    {
  494|    886|        Jbig2WordStream *ws = NULL;
  495|    886|        Jbig2ArithState *as = NULL;
  496|    886|        Jbig2ArithCx *GR_stats = NULL;
  497|    886|        int stats_size;
  498|    886|        Jbig2Image *image = NULL;
  499|       |
  500|    886|        image = jbig2_image_new(ctx, rsi.width, rsi.height);
  501|    886|        if (image == NULL) {
  ------------------
  |  Branch (501:13): [True: 10, False: 876]
  ------------------
  502|     10|            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate refinement image");
  503|     10|            goto cleanup;
  504|     10|        }
  505|    876|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, rsi.height);
  506|       |
  507|    876|        stats_size = params.GRTEMPLATE ? 1 << 10 : 1 << 13;
  ------------------
  |  Branch (507:22): [True: 315, False: 561]
  ------------------
  508|    876|        GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|    876|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  509|    876|        if (GR_stats == NULL) {
  ------------------
  |  Branch (509:13): [True: 1, False: 875]
  ------------------
  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|    875|        memset(GR_stats, 0, stats_size);
  514|       |
  515|    875|        ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
  516|    875|        if (ws == NULL) {
  ------------------
  |  Branch (516:13): [True: 1, False: 874]
  ------------------
  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|    874|        as = jbig2_arith_new(ctx, ws);
  522|    874|        if (as == NULL) {
  ------------------
  |  Branch (522:13): [True: 2, False: 872]
  ------------------
  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|    872|        code = jbig2_decode_refinement_region(ctx, segment, &params, as, image, GR_stats);
  528|    872|        if (code < 0) {
  ------------------
  |  Branch (528:13): [True: 8, False: 864]
  ------------------
  529|      8|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
  530|      8|            goto cleanup;
  531|      8|        }
  532|       |
  533|    864|        if ((segment->flags & 63) == 40) {
  ------------------
  |  Branch (533:13): [True: 231, False: 633]
  ------------------
  534|       |            /* intermediate region. save the result for later */
  535|    231|            segment->result = jbig2_image_reference(ctx, image);
  536|    633|        } else {
  537|       |            /* immediate region. composite onto the page */
  538|    633|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  539|    633|                        "composing %dx%d decoded refinement region onto page at (%d, %d)", rsi.width, rsi.height, rsi.x, rsi.y);
  540|    633|            code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
  541|    633|            if (code < 0) {
  ------------------
  |  Branch (541:17): [True: 108, False: 525]
  ------------------
  542|    108|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add refinement region to page");
  543|    108|                goto cleanup;
  544|    108|            }
  545|    633|        }
  546|       |
  547|    886|cleanup:
  548|    886|        jbig2_image_release(ctx, image);
  549|    886|        jbig2_image_release(ctx, params.GRREFERENCE);
  550|    886|        jbig2_free(ctx->allocator, as);
  551|    886|        jbig2_word_stream_buf_free(ctx, ws);
  552|    886|        jbig2_free(ctx->allocator, GR_stats);
  553|    886|    }
  554|       |
  555|      0|    return code;
  556|    864|}
jbig2_refinement.c:jbig2_decode_refinement_TPGRON:
  311|    533|{
  312|    533|    const int64_t GRW = image->width;
  313|    533|    const int64_t GRH = image->height;
  314|    533|    int64_t x, y;
  315|    533|    int iv, LTP = 0;
  316|    533|    uint32_t start_context = (params->GRTEMPLATE ? 0x40 : 0x100);
  ------------------
  |  Branch (316:31): [True: 156, False: 377]
  ------------------
  317|    533|    ContextBuilder mkctx = (params->GRTEMPLATE ? mkctx1 : mkctx0);
  ------------------
  |  Branch (317:29): [True: 156, False: 377]
  ------------------
  318|       |
  319|    533|    if (params->GRTEMPLATE == 0 &&
  ------------------
  |  Branch (319:9): [True: 377, False: 156]
  ------------------
  320|    377|        (pixel_outside_field(params->grat[0], params->grat[1]) ||
  ------------------
  |  |   47|    754|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (47:6): [True: 0, False: 377]
  |  |  |  Branch (47:20): [True: 2, False: 375]
  |  |  |  Branch (47:31): [True: 0, False: 375]
  |  |  |  Branch (47:46): [True: 171, False: 204]
  |  |  |  Branch (47:57): [True: 0, False: 171]
  |  |  |  Branch (47:72): [True: 204, False: 171]
  |  |  |  Branch (47:84): [True: 3, False: 201]
  |  |  ------------------
  ------------------
  321|    372|        refpixel_outside_field(params->grat[2], params->grat[3])))
  ------------------
  |  |   49|    372|    ((y) < -128 || (y) > 127 || (x) < -128 || (x) > 127)
  |  |  ------------------
  |  |  |  Branch (49:6): [True: 0, False: 372]
  |  |  |  Branch (49:20): [True: 0, False: 372]
  |  |  |  Branch (49:33): [True: 0, False: 372]
  |  |  |  Branch (49:47): [True: 0, False: 372]
  |  |  ------------------
  ------------------
  322|      5|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER,
  ------------------
  |  |   68|      5|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  323|      5|                           "adaptive template pixel is out of field");
  324|       |
  325|  22.2M|    for (y = 0; y < GRH; y++) {
  ------------------
  |  Branch (325:17): [True: 22.2M, False: 528]
  ------------------
  326|  22.2M|        int bit = jbig2_arith_decode(ctx, as, &GR_stats[start_context]);
  327|  22.2M|        if (bit < 0)
  ------------------
  |  Branch (327:13): [True: 0, False: 22.2M]
  ------------------
  328|      0|            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode arithmetic code when handling refinement TPGRON1");
  ------------------
  |  |   68|      0|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  329|  22.2M|        LTP ^= bit;
  330|  22.2M|        if (!LTP) {
  ------------------
  |  Branch (330:13): [True: 13.0M, False: 9.25M]
  ------------------
  331|   189M|            for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (331:25): [True: 176M, False: 13.0M]
  ------------------
  332|   176M|                bit = jbig2_arith_decode(ctx, as, &GR_stats[mkctx(params, image, x, y)]);
  333|   176M|                if (bit < 0)
  ------------------
  |  Branch (333:21): [True: 0, False: 176M]
  ------------------
  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|   176M|                jbig2_image_set_pixel(image, x, y, bit);
  336|   176M|            }
  337|  13.0M|        } else {
  338|   114M|            for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (338:25): [True: 105M, False: 9.25M]
  ------------------
  339|   105M|                iv = implicit_value(params, image, x, y);
  340|   105M|                if (iv < 0) {
  ------------------
  |  Branch (340:21): [True: 734k, False: 104M]
  ------------------
  341|   734k|                    int bit = jbig2_arith_decode(ctx, as, &GR_stats[mkctx(params, image, x, y)]);
  342|   734k|                    if (bit < 0)
  ------------------
  |  Branch (342:25): [True: 0, False: 734k]
  ------------------
  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|   734k|                    jbig2_image_set_pixel(image, x, y, bit);
  345|   734k|                } else
  346|   104M|                    jbig2_image_set_pixel(image, x, y, iv);
  347|   105M|            }
  348|  9.25M|        }
  349|  22.2M|    }
  350|       |
  351|    528|    return 0;
  352|    528|}
jbig2_refinement.c:mkctx1:
  290|  88.0M|{
  291|  88.0M|    Jbig2Image *ref = params->GRREFERENCE;
  292|  88.0M|    const int32_t dx = params->GRREFERENCEDX;
  293|  88.0M|    const int32_t dy = params->GRREFERENCEDY;
  294|  88.0M|    uint32_t CONTEXT;
  295|       |
  296|  88.0M|    CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
  297|  88.0M|    CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  298|  88.0M|    CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  299|  88.0M|    CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
  300|  88.0M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  301|  88.0M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  302|  88.0M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
  303|  88.0M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
  304|  88.0M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
  305|  88.0M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
  306|  88.0M|    return CONTEXT;
  307|  88.0M|}
jbig2_refinement.c:mkctx0:
  266|  88.9M|{
  267|  88.9M|    Jbig2Image *ref = params->GRREFERENCE;
  268|  88.9M|    const int32_t dx = params->GRREFERENCEDX;
  269|  88.9M|    const int32_t dy = params->GRREFERENCEDY;
  270|  88.9M|    uint32_t CONTEXT;
  271|       |
  272|  88.9M|    CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
  273|  88.9M|    CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  274|  88.9M|    CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  275|  88.9M|    CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y + params->grat[1]) << 3;
  276|  88.9M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  277|  88.9M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  278|  88.9M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
  279|  88.9M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
  280|  88.9M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
  281|  88.9M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
  282|  88.9M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
  283|  88.9M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
  284|  88.9M|    CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
  285|  88.9M|    return CONTEXT;
  286|  88.9M|}
jbig2_refinement.c:implicit_value:
  245|   105M|{
  246|   105M|    Jbig2Image *ref = params->GRREFERENCE;
  247|   105M|    const int64_t i = x - params->GRREFERENCEDX;
  248|   105M|    const int64_t j = y - params->GRREFERENCEDY;
  249|   105M|    int m = jbig2_image_get_pixel(ref, i, j);
  250|       |
  251|   105M|    (void) image;
  252|       |
  253|   105M|    return ((jbig2_image_get_pixel(ref, i - 1, j - 1) == m) &&
  ------------------
  |  Branch (253:13): [True: 104M, False: 514k]
  ------------------
  254|   104M|            (jbig2_image_get_pixel(ref, i, j - 1) == m) &&
  ------------------
  |  Branch (254:13): [True: 104M, False: 84.4k]
  ------------------
  255|   104M|            (jbig2_image_get_pixel(ref, i + 1, j - 1) == m) &&
  ------------------
  |  Branch (255:13): [True: 104M, False: 33.1k]
  ------------------
  256|   104M|            (jbig2_image_get_pixel(ref, i - 1, j) == m) &&
  ------------------
  |  Branch (256:13): [True: 104M, False: 22.1k]
  ------------------
  257|   104M|            (jbig2_image_get_pixel(ref, i + 1, j) == m) &&
  ------------------
  |  Branch (257:13): [True: 104M, False: 7.49k]
  ------------------
  258|   104M|            (jbig2_image_get_pixel(ref, i - 1, j + 1) == m) &&
  ------------------
  |  Branch (258:13): [True: 104M, False: 67.8k]
  ------------------
  259|   104M|            (jbig2_image_get_pixel(ref, i, j + 1) == m) &&
  ------------------
  |  Branch (259:13): [True: 104M, False: 2.16k]
  ------------------
  260|   104M|            (jbig2_image_get_pixel(ref, i + 1, j + 1) == m)
  ------------------
  |  Branch (260:13): [True: 104M, False: 1.99k]
  ------------------
  261|   105M|           )? m : -1;
  262|   105M|}
jbig2_refinement.c:jbig2_decode_refinement_template1_unopt:
  117|  6.65k|{
  118|  6.65k|    const int64_t GRW = image->width;
  119|  6.65k|    const int64_t GRH = image->height;
  120|  6.65k|    Jbig2Image *ref = params->GRREFERENCE;
  121|  6.65k|    const int32_t dx = params->GRREFERENCEDX;
  122|  6.65k|    const int32_t dy = params->GRREFERENCEDY;
  123|  6.65k|    uint32_t CONTEXT;
  124|  6.65k|    int64_t x, y;
  125|  6.65k|    int bit;
  126|       |
  127|  23.8M|    for (y = 0; y < GRH; y++) {
  ------------------
  |  Branch (127:17): [True: 23.8M, False: 6.65k]
  ------------------
  128|   253M|        for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (128:21): [True: 229M, False: 23.8M]
  ------------------
  129|   229M|            CONTEXT = 0;
  130|   229M|            CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
  131|   229M|            CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  132|   229M|            CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  133|   229M|            CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
  134|   229M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  135|   229M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  136|   229M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
  137|   229M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
  138|   229M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
  139|   229M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
  140|   229M|            bit = jbig2_arith_decode(ctx, as, &GR_stats[CONTEXT]);
  141|   229M|            if (bit < 0)
  ------------------
  |  Branch (141:17): [True: 0, False: 229M]
  ------------------
  142|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling refinement template0");
  143|   229M|            jbig2_image_set_pixel(image, x, y, bit);
  144|   229M|        }
  145|  23.8M|    }
  146|       |
  147|       |#ifdef JBIG2_DEBUG_DUMP
  148|       |    {
  149|       |        static unsigned int count = 0;
  150|       |        char name[32];
  151|       |        int code;
  152|       |
  153|       |        snprintf(name, 32, "refin-%d.pbm", count);
  154|       |        code = jbig2_image_write_pbm_file(ref, name);
  155|       |        if (code < 0)
  156|       |            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write refinement input");
  157|       |        snprintf(name, 32, "refout-%d.pbm", count);
  158|       |        code = jbig2_image_write_pbm_file(image, name);
  159|       |        if (code < 0)
  160|       |            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write refinement output");
  161|       |        count++;
  162|       |    }
  163|       |#endif
  164|       |
  165|  6.65k|    return 0;
  166|  6.65k|}
jbig2_refinement.c:jbig2_decode_refinement_template0_unopt:
   55|  85.7k|{
   56|  85.7k|    const int64_t GRW = image->width;
   57|  85.7k|    const int64_t GRH = image->height;
   58|  85.7k|    Jbig2Image *ref = params->GRREFERENCE;
   59|  85.7k|    const int32_t dx = params->GRREFERENCEDX;
   60|  85.7k|    const int32_t dy = params->GRREFERENCEDY;
   61|  85.7k|    uint32_t CONTEXT;
   62|  85.7k|    int64_t x, y;
   63|  85.7k|    int bit;
   64|       |
   65|  85.7k|    if (pixel_outside_field(params->grat[0], params->grat[1]) ||
  ------------------
  |  |   47|   171k|    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  |  |  ------------------
  |  |  |  Branch (47:6): [True: 0, False: 85.7k]
  |  |  |  Branch (47:20): [True: 4, False: 85.7k]
  |  |  |  Branch (47:31): [True: 0, False: 85.7k]
  |  |  |  Branch (47:46): [True: 18.9k, False: 66.7k]
  |  |  |  Branch (47:57): [True: 0, False: 18.9k]
  |  |  |  Branch (47:72): [True: 66.7k, False: 18.9k]
  |  |  |  Branch (47:84): [True: 6, False: 66.7k]
  |  |  ------------------
  ------------------
   66|  85.7k|        refpixel_outside_field(params->grat[2], params->grat[3]))
  ------------------
  |  |   49|  85.7k|    ((y) < -128 || (y) > 127 || (x) < -128 || (x) > 127)
  |  |  ------------------
  |  |  |  Branch (49:6): [True: 0, False: 85.7k]
  |  |  |  Branch (49:20): [True: 0, False: 85.7k]
  |  |  |  Branch (49:33): [True: 0, False: 85.7k]
  |  |  |  Branch (49:47): [True: 0, False: 85.7k]
  |  |  ------------------
  ------------------
   67|     10|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
   68|     10|                           "adaptive template pixel is out of field");
   69|       |
   70|  2.35M|    for (y = 0; y < GRH; y++) {
  ------------------
  |  Branch (70:17): [True: 2.26M, False: 85.7k]
  ------------------
   71|  88.0M|        for (x = 0; x < GRW; x++) {
  ------------------
  |  Branch (71:21): [True: 85.8M, False: 2.26M]
  ------------------
   72|  85.8M|            CONTEXT = 0;
   73|  85.8M|            CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
   74|  85.8M|            CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
   75|  85.8M|            CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
   76|  85.8M|            CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y + params->grat[1]) << 3;
   77|  85.8M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
   78|  85.8M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
   79|  85.8M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
   80|  85.8M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
   81|  85.8M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
   82|  85.8M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
   83|  85.8M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
   84|  85.8M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
   85|  85.8M|            CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
   86|  85.8M|            bit = jbig2_arith_decode(ctx, as, &GR_stats[CONTEXT]);
   87|  85.8M|            if (bit < 0)
  ------------------
  |  Branch (87:17): [True: 0, False: 85.8M]
  ------------------
   88|      0|                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling refinement template0");
   89|  85.8M|            jbig2_image_set_pixel(image, x, y, bit);
   90|  85.8M|        }
   91|  2.26M|    }
   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|  85.7k|    return 0;
  111|  85.7k|}
jbig2_refinement.c:jbig2_region_find_referred:
  395|    238|{
  396|    238|    const int nsegments = segment->referred_to_segment_count;
  397|    238|    Jbig2Segment *rsegment;
  398|    238|    int index;
  399|       |
  400|  4.42k|    for (index = 0; index < nsegments; index++) {
  ------------------
  |  Branch (400:21): [True: 4.39k, False: 27]
  ------------------
  401|  4.39k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  402|  4.39k|        if (rsegment == NULL) {
  ------------------
  |  Branch (402:13): [True: 2.74k, False: 1.64k]
  ------------------
  403|  2.74k|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to find referred to segment %d", segment->referred_to_segments[index]);
  404|  2.74k|            continue;
  405|  2.74k|        }
  406|  1.64k|        switch (rsegment->flags & 63) {
  407|    749|        case 4:                /* intermediate text region */
  ------------------
  |  Branch (407:9): [True: 749, False: 897]
  ------------------
  408|    754|        case 20:               /* intermediate halftone region */
  ------------------
  |  Branch (408:9): [True: 5, False: 1.64k]
  ------------------
  409|    754|        case 36:               /* intermediate generic region */
  ------------------
  |  Branch (409:9): [True: 0, False: 1.64k]
  ------------------
  410|    996|        case 40:               /* intermediate generic refinement region */
  ------------------
  |  Branch (410:9): [True: 242, False: 1.40k]
  ------------------
  411|    996|            if (rsegment->result)
  ------------------
  |  Branch (411:17): [True: 211, False: 785]
  ------------------
  412|    211|                return rsegment;
  413|    785|            break;
  414|    785|        default:               /* keep looking */
  ------------------
  |  Branch (414:9): [True: 650, False: 996]
  ------------------
  415|    650|            break;
  416|  1.64k|        }
  417|  1.64k|    }
  418|       |    /* no appropriate reference was found. */
  419|     27|    return NULL;
  420|    238|}

jbig2_parse_segment_header:
   44|  80.4k|{
   45|  80.4k|    Jbig2Segment *result;
   46|  80.4k|    uint8_t rtscarf;
   47|  80.4k|    uint32_t rtscarf_long;
   48|  80.4k|    uint32_t *referred_to_segments;
   49|  80.4k|    uint32_t referred_to_segment_count;
   50|  80.4k|    uint32_t referred_to_segment_size;
   51|  80.4k|    uint32_t pa_size;
   52|  80.4k|    uint32_t offset;
   53|       |
   54|       |    /* minimum possible size of a jbig2 segment header */
   55|  80.4k|    if (buf_size < 11)
  ------------------
  |  Branch (55:9): [True: 552, False: 79.8k]
  ------------------
   56|    552|        return NULL;
   57|       |
   58|  79.8k|    result = jbig2_new(ctx, Jbig2Segment, 1);
  ------------------
  |  |  141|  79.8k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
   59|  79.8k|    if (result == NULL) {
  ------------------
  |  Branch (59:9): [True: 1, False: 79.8k]
  ------------------
   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|  79.8k|    result->number = jbig2_get_uint32(buf);
   66|  79.8k|    if (result->number == JBIG2_UNKNOWN_SEGMENT_NUMBER) {
  ------------------
  |  |   68|  79.8k|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  |  Branch (66:9): [True: 6, False: 79.8k]
  ------------------
   67|      6|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "segment number too large");
  ------------------
  |  |   68|      6|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
   68|      6|        jbig2_free(ctx->allocator, result);
   69|      6|        return NULL;
   70|      6|    }
   71|       |
   72|       |    /* 7.2.3 */
   73|  79.8k|    result->flags = buf[4];
   74|       |
   75|       |    /* 7.2.4 referred-to segments */
   76|  79.8k|    rtscarf = buf[5];
   77|  79.8k|    if ((rtscarf & 0xe0) == 0xe0) {
  ------------------
  |  Branch (77:9): [True: 927, False: 78.9k]
  ------------------
   78|    927|        rtscarf_long = jbig2_get_uint32(buf + 5);
   79|    927|        referred_to_segment_count = rtscarf_long & 0x1fffffff;
   80|    927|        offset = 5 + 4 + (referred_to_segment_count + 1) / 8;
   81|  78.9k|    } else {
   82|  78.9k|        referred_to_segment_count = (rtscarf >> 5);
   83|  78.9k|        offset = 5 + 1;
   84|  78.9k|    }
   85|  79.8k|    result->referred_to_segment_count = referred_to_segment_count;
   86|       |
   87|       |    /* we now have enough information to compute the full header length */
   88|  79.8k|    referred_to_segment_size = result->number <= 256 ? 1 : result->number <= 65536 ? 2 : 4;     /* 7.2.5 */
  ------------------
  |  Branch (88:32): [True: 26.7k, False: 53.1k]
  |  Branch (88:60): [True: 4.75k, False: 48.3k]
  ------------------
   89|  79.8k|    pa_size = result->flags & 0x40 ? 4 : 1;     /* 7.2.6 */
  ------------------
  |  Branch (89:15): [True: 5.97k, False: 73.9k]
  ------------------
   90|  79.8k|    if (offset + referred_to_segment_count * referred_to_segment_size + pa_size + 4 > buf_size) {
  ------------------
  |  Branch (90:9): [True: 709, False: 79.1k]
  ------------------
   91|    709|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "attempted to parse segment header with insufficient data, asking for more data");
   92|    709|        jbig2_free(ctx->allocator, result);
   93|    709|        return NULL;
   94|    709|    }
   95|       |
   96|       |    /* 7.2.5 */
   97|  79.1k|    if (referred_to_segment_count) {
  ------------------
  |  Branch (97:9): [True: 17.9k, False: 61.2k]
  ------------------
   98|  17.9k|        uint32_t i;
   99|       |
  100|  17.9k|        referred_to_segments = jbig2_new(ctx, uint32_t, referred_to_segment_count * referred_to_segment_size);
  ------------------
  |  |  141|  17.9k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  101|  17.9k|        if (referred_to_segments == NULL) {
  ------------------
  |  Branch (101:13): [True: 1, False: 17.9k]
  ------------------
  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|   159k|        for (i = 0; i < referred_to_segment_count; i++) {
  ------------------
  |  Branch (107:21): [True: 141k, False: 17.9k]
  ------------------
  108|   141k|            referred_to_segments[i] =
  109|   141k|                (referred_to_segment_size == 1) ? buf[offset] :
  ------------------
  |  Branch (109:17): [True: 100k, False: 40.6k]
  ------------------
  110|   141k|                (referred_to_segment_size == 2) ? jbig2_get_uint16(buf + offset) : jbig2_get_uint32(buf + offset);
  ------------------
  |  Branch (110:17): [True: 6.49k, False: 34.1k]
  ------------------
  111|   141k|            offset += referred_to_segment_size;
  112|   141k|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "segment %d refers to segment %d", result->number, referred_to_segments[i]);
  113|   141k|        }
  114|  17.9k|        result->referred_to_segments = referred_to_segments;
  115|  61.2k|    } else {                    /* no referred-to segments */
  116|       |
  117|  61.2k|        result->referred_to_segments = NULL;
  118|  61.2k|    }
  119|       |
  120|       |    /* 7.2.6 */
  121|  79.1k|    if (pa_size == 4) {
  ------------------
  |  Branch (121:9): [True: 5.59k, False: 73.5k]
  ------------------
  122|  5.59k|        result->page_association = jbig2_get_uint32(buf + offset);
  123|  5.59k|        offset += 4;
  124|  73.5k|    } else {
  125|  73.5k|        result->page_association = buf[offset++];
  126|  73.5k|    }
  127|  79.1k|    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "segment %d is associated with page %d", result->number, result->page_association);
  128|       |
  129|       |    /* 7.2.7 */
  130|  79.1k|    result->rows = UINT32_MAX;
  131|  79.1k|    result->data_length = jbig2_get_uint32(buf + offset);
  132|  79.1k|    *p_header_size = offset + 4;
  133|       |
  134|       |    /* no body parsing results yet */
  135|  79.1k|    result->result = NULL;
  136|       |
  137|  79.1k|    return result;
  138|  79.1k|}
jbig2_free_segment:
  142|  79.1k|{
  143|  79.1k|    if (segment == NULL)
  ------------------
  |  Branch (143:9): [True: 0, False: 79.1k]
  ------------------
  144|      0|        return;
  145|       |
  146|  79.1k|    jbig2_free(ctx->allocator, segment->referred_to_segments);
  147|       |    /* todo: we need either some separate fields or
  148|       |       a more complex result object rather than this
  149|       |       brittle special casing */
  150|  79.1k|    switch (segment->flags & 63) {
  151|  16.5k|    case 0:                    /* symbol dictionary */
  ------------------
  |  Branch (151:5): [True: 16.5k, False: 62.6k]
  ------------------
  152|  16.5k|        if (segment->result != NULL)
  ------------------
  |  Branch (152:13): [True: 4.69k, False: 11.8k]
  ------------------
  153|  4.69k|            jbig2_sd_release(ctx, (Jbig2SymbolDict *) segment->result);
  154|  16.5k|        break;
  155|  4.17k|    case 4:                    /* intermediate text region */
  ------------------
  |  Branch (155:5): [True: 4.17k, False: 74.9k]
  ------------------
  156|  4.63k|    case 40:                   /* intermediate refinement region */
  ------------------
  |  Branch (156:5): [True: 453, False: 78.7k]
  ------------------
  157|  4.63k|        if (segment->result != NULL)
  ------------------
  |  Branch (157:13): [True: 166, False: 4.46k]
  ------------------
  158|    166|            jbig2_image_release(ctx, (Jbig2Image *) segment->result);
  159|  4.63k|        break;
  160|  4.59k|    case 16:                   /* pattern dictionary */
  ------------------
  |  Branch (160:5): [True: 4.59k, False: 74.5k]
  ------------------
  161|  4.59k|        if (segment->result != NULL)
  ------------------
  |  Branch (161:13): [True: 2.99k, False: 1.60k]
  ------------------
  162|  2.99k|            jbig2_hd_release(ctx, (Jbig2PatternDict *) segment->result);
  163|  4.59k|        break;
  164|  2.04k|    case 53:                   /* user-supplied huffman table */
  ------------------
  |  Branch (164:5): [True: 2.04k, False: 77.1k]
  ------------------
  165|  2.04k|        if (segment->result != NULL)
  ------------------
  |  Branch (165:13): [True: 1.68k, False: 354]
  ------------------
  166|  1.68k|            jbig2_table_free(ctx, (Jbig2HuffmanParams *) segment->result);
  167|  2.04k|        break;
  168|  51.3k|    default:
  ------------------
  |  Branch (168:5): [True: 51.3k, False: 27.7k]
  ------------------
  169|       |        /* anything else is probably an undefined pointer */
  170|  51.3k|        break;
  171|  79.1k|    }
  172|  79.1k|    jbig2_free(ctx->allocator, segment);
  173|  79.1k|}
jbig2_find_segment:
  178|   265k|{
  179|   265k|    int index, index_max = ctx->segment_index - 1;
  180|   265k|    const Jbig2Ctx *global_ctx = ctx->global_ctx;
  181|       |
  182|       |    /* FIXME: binary search would be better */
  183|  2.02M|    for (index = index_max; index >= 0; index--)
  ------------------
  |  Branch (183:29): [True: 1.87M, False: 148k]
  ------------------
  184|  1.87M|        if (ctx->segments[index]->number == number)
  ------------------
  |  Branch (184:13): [True: 117k, False: 1.75M]
  ------------------
  185|   117k|            return (ctx->segments[index]);
  186|       |
  187|   148k|    if (global_ctx)
  ------------------
  |  Branch (187:9): [True: 0, False: 148k]
  ------------------
  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|   148k|    return NULL;
  194|   148k|}
jbig2_get_region_segment_info:
  199|  27.4k|{
  200|       |    /* 7.4.1 */
  201|  27.4k|    info->width = jbig2_get_uint32(segment_data);
  202|  27.4k|    info->height = jbig2_get_uint32(segment_data + 4);
  203|  27.4k|    info->x = jbig2_get_uint32(segment_data + 8);
  204|  27.4k|    info->y = jbig2_get_uint32(segment_data + 12);
  205|  27.4k|    info->flags = segment_data[16];
  206|  27.4k|    info->op = (Jbig2ComposeOp)(info->flags & 0x7);
  207|  27.4k|}
jbig2_parse_segment:
  337|  53.4k|{
  338|  53.4k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  339|  53.4k|                "segment %d, flags=%x, type=%d, data_length=%ld", segment->number, segment->flags, segment->flags & 63, (long) segment->data_length);
  340|  53.4k|    switch (segment->flags & 63) {
  341|  6.25k|    case 0:
  ------------------
  |  Branch (341:5): [True: 6.25k, False: 47.2k]
  ------------------
  342|  6.25k|        return jbig2_symbol_dictionary(ctx, segment, segment_data);
  343|    317|    case 4:                    /* intermediate text region */
  ------------------
  |  Branch (343:5): [True: 317, False: 53.1k]
  ------------------
  344|    533|    case 6:                    /* immediate text region */
  ------------------
  |  Branch (344:5): [True: 216, False: 53.2k]
  ------------------
  345|  3.69k|    case 7:                    /* immediate lossless text region */
  ------------------
  |  Branch (345:5): [True: 3.16k, False: 50.3k]
  ------------------
  346|  3.69k|        return jbig2_text_region(ctx, segment, segment_data);
  347|  4.11k|    case 16:
  ------------------
  |  Branch (347:5): [True: 4.11k, False: 49.3k]
  ------------------
  348|  4.11k|        return jbig2_pattern_dictionary(ctx, segment, segment_data);
  349|     40|    case 20:                   /* intermediate halftone region */
  ------------------
  |  Branch (349:5): [True: 40, False: 53.4k]
  ------------------
  350|     82|    case 22:                   /* immediate halftone region */
  ------------------
  |  Branch (350:5): [True: 42, False: 53.4k]
  ------------------
  351|  1.94k|    case 23:                   /* immediate lossless halftone region */
  ------------------
  |  Branch (351:5): [True: 1.86k, False: 51.6k]
  ------------------
  352|  1.94k|        return jbig2_halftone_region(ctx, segment, segment_data);
  353|      3|    case 36:
  ------------------
  |  Branch (353:5): [True: 3, False: 53.4k]
  ------------------
  354|      3|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unhandled segment type 'intermediate generic region' (NYI)");
  355|  15.1k|    case 38:                   /* immediate generic region */
  ------------------
  |  Branch (355:5): [True: 15.1k, False: 38.2k]
  ------------------
  356|  20.9k|    case 39:                   /* immediate lossless generic region */
  ------------------
  |  Branch (356:5): [True: 5.76k, False: 47.7k]
  ------------------
  357|  20.9k|        return jbig2_immediate_generic_region(ctx, segment, segment_data);
  358|    252|    case 40:                   /* intermediate generic refinement region */
  ------------------
  |  Branch (358:5): [True: 252, False: 53.2k]
  ------------------
  359|    832|    case 42:                   /* immediate generic refinement region */
  ------------------
  |  Branch (359:5): [True: 580, False: 52.8k]
  ------------------
  360|    930|    case 43:                   /* immediate lossless generic refinement region */
  ------------------
  |  Branch (360:5): [True: 98, False: 53.3k]
  ------------------
  361|    930|        return jbig2_refinement_region(ctx, segment, segment_data);
  362|  9.06k|    case 48:
  ------------------
  |  Branch (362:5): [True: 9.06k, False: 44.4k]
  ------------------
  363|  9.06k|        return jbig2_page_info(ctx, segment, segment_data);
  364|  1.21k|    case 49:
  ------------------
  |  Branch (364:5): [True: 1.21k, False: 52.2k]
  ------------------
  365|  1.21k|        return jbig2_end_of_page(ctx, segment, segment_data);
  366|    680|    case 50:
  ------------------
  |  Branch (366:5): [True: 680, False: 52.7k]
  ------------------
  367|    680|        return jbig2_end_of_stripe(ctx, segment, segment_data);
  368|    527|    case 51:
  ------------------
  |  Branch (368:5): [True: 527, False: 52.9k]
  ------------------
  369|    527|        ctx->state = JBIG2_FILE_EOF;
  370|    527|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of file");
  371|    527|        break;
  372|    142|    case 52:
  ------------------
  |  Branch (372:5): [True: 142, False: 53.3k]
  ------------------
  373|    142|        return jbig2_parse_profile_segment(ctx, segment, segment_data);
  374|  1.82k|    case 53:                   /* user-supplied huffman table */
  ------------------
  |  Branch (374:5): [True: 1.82k, False: 51.6k]
  ------------------
  375|  1.82k|        return jbig2_table(ctx, segment, segment_data);
  376|      4|    case 54:
  ------------------
  |  Branch (376:5): [True: 4, False: 53.4k]
  ------------------
  377|      4|        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled segment type 'color palette' (NYI)");
  378|    584|    case 62:
  ------------------
  |  Branch (378:5): [True: 584, False: 52.8k]
  ------------------
  379|    584|        return jbig2_parse_extension_segment(ctx, segment, segment_data);
  380|  1.55k|    default:
  ------------------
  |  Branch (380:5): [True: 1.55k, False: 51.9k]
  ------------------
  381|  1.55k|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unknown segment type %d", segment->flags & 63);
  382|  53.4k|    }
  383|  2.07k|    return 0;
  384|  53.4k|}
jbig2_segment.c:jbig2_parse_profile_segment:
  253|    142|{
  254|    142|    uint32_t profiles;
  255|    142|    uint32_t i;
  256|    142|    uint32_t profile;
  257|    142|    int index;
  258|    142|    const char *requirements;
  259|    142|    const char *generic_region;
  260|    142|    const char *refinement_region;
  261|    142|    const char *halftone_region;
  262|    142|    const char *numerical_data;
  263|       |
  264|    142|    if (segment->data_length < 4)
  ------------------
  |  Branch (264:9): [True: 4, False: 138]
  ------------------
  265|      4|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
  266|    138|    index = 0;
  267|       |
  268|    138|    profiles = jbig2_get_uint32(&segment_data[index]);
  269|    138|    index += 4;
  270|       |
  271|  15.8k|    for (i = 0; i < profiles; i++) {
  ------------------
  |  Branch (271:17): [True: 15.8k, False: 59]
  ------------------
  272|  15.8k|        if (segment->data_length - index < 4)
  ------------------
  |  Branch (272:13): [True: 79, False: 15.7k]
  ------------------
  273|     79|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short to store profile");
  274|       |
  275|  15.7k|        profile = jbig2_get_uint32(&segment_data[index]);
  276|  15.7k|        index += 4;
  277|       |
  278|  15.7k|        switch (profile) {
  279|    267|        case 0x00000001:
  ------------------
  |  Branch (279:9): [True: 267, False: 15.4k]
  ------------------
  280|    267|            requirements = "All JBIG2 capabilities";
  281|    267|            generic_region = "No restriction";
  282|    267|            refinement_region = "No restriction";
  283|    267|            halftone_region = "No restriction";
  284|    267|            numerical_data = "No restriction";
  285|    267|            break;
  286|     62|        case 0x00000002:
  ------------------
  |  Branch (286:9): [True: 62, False: 15.6k]
  ------------------
  287|     62|            requirements = "Maximum compression";
  288|     62|            generic_region = "Arithmetic only; any template used";
  289|     62|            refinement_region = "No restriction";
  290|     62|            halftone_region = "No restriction";
  291|     62|            numerical_data = "Arithmetic only";
  292|     62|            break;
  293|    232|        case 0x00000003:
  ------------------
  |  Branch (293:9): [True: 232, False: 15.4k]
  ------------------
  294|    232|            requirements = "Medium complexity and medium compression";
  295|    232|            generic_region = "Arithmetic only; only 10-pixel and 13-pixel templates";
  296|    232|            refinement_region = "10-pixel template only";
  297|    232|            halftone_region = "No skip mask used";
  298|    232|            numerical_data = "Arithmetic only";
  299|    232|            break;
  300|    136|        case 0x00000004:
  ------------------
  |  Branch (300:9): [True: 136, False: 15.5k]
  ------------------
  301|    136|            requirements = "Low complexity with progressive lossless capability";
  302|    136|            generic_region = "MMR only";
  303|    136|            refinement_region = "10-pixel template only";
  304|    136|            halftone_region = "No skip mask used";
  305|    136|            numerical_data = "Huffman only";
  306|    136|            break;
  307|     78|        case 0x00000005:
  ------------------
  |  Branch (307:9): [True: 78, False: 15.6k]
  ------------------
  308|     78|            requirements = "Low complexity";
  309|     78|            generic_region = "MMR only";
  310|     78|            refinement_region = "Not available";
  311|     78|            halftone_region = "No skip mask used";
  312|     78|            numerical_data = "Huffman only";
  313|     78|            break;
  314|  14.9k|        default:
  ------------------
  |  Branch (314:9): [True: 14.9k, False: 775]
  ------------------
  315|  14.9k|            requirements = "Unknown";
  316|  14.9k|            generic_region = "Unknown";
  317|  14.9k|            refinement_region = "Unknown";
  318|  14.9k|            halftone_region = "Unknown";
  319|  14.9k|            numerical_data = "Unknown";
  320|  14.9k|            break;
  321|  15.7k|        }
  322|       |
  323|  15.7k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "Supported profile: 0x%08x", profile);
  324|  15.7k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Requirements: %s", requirements);
  325|  15.7k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Generic region coding: %s", generic_region);
  326|  15.7k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Refinement region coding: %s", refinement_region);
  327|  15.7k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Halftone region coding: %s", halftone_region);
  328|  15.7k|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "  Numerical data: %s", numerical_data);
  329|  15.7k|    }
  330|       |
  331|     59|    return 0;
  332|    138|}
jbig2_segment.c:jbig2_parse_extension_segment:
  212|    584|{
  213|    584|    uint32_t type;
  214|    584|    bool reserved;
  ------------------
  |  |   45|    584|#define bool int
  ------------------
  215|    584|    bool necessary;
  ------------------
  |  |   45|    584|#define bool int
  ------------------
  216|       |
  217|    584|    if (segment->data_length < 4)
  ------------------
  |  Branch (217:9): [True: 9, False: 575]
  ------------------
  218|      9|        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  219|       |
  220|    575|    type = jbig2_get_uint32(segment_data);
  221|    575|    reserved = type & 0x20000000;
  222|       |    /* Not implemented since this bit
  223|       |    is only needed by encoders.
  224|       |    dependent = type & 0x40000000;
  225|       |    */
  226|    575|    necessary = type & 0x80000000;
  227|       |
  228|    575|    if (necessary && !reserved) {
  ------------------
  |  Branch (228:9): [True: 40, False: 535]
  |  Branch (228:22): [True: 8, False: 32]
  ------------------
  229|      8|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extension segment is marked 'necessary' but not 'reserved' contrary to spec");
  230|      8|    }
  231|       |
  232|    575|    switch (type) {
  233|    230|    case 0x20000000:
  ------------------
  |  Branch (233:5): [True: 230, False: 345]
  ------------------
  234|    230|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "ignoring ASCII comment");
  235|    230|        break;
  236|     67|    case 0x20000002:
  ------------------
  |  Branch (236:5): [True: 67, False: 508]
  ------------------
  237|     67|        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "ignoring UCS-2 comment");
  238|     67|        break;
  239|    278|    default:
  ------------------
  |  Branch (239:5): [True: 278, False: 297]
  ------------------
  240|    278|        if (necessary) {
  ------------------
  |  Branch (240:13): [True: 40, False: 238]
  ------------------
  241|     40|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unhandled necessary extension segment type 0x%08x", type);
  242|    238|        } else {
  243|    238|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled non-necessary extension segment, skipping");
  244|    238|        }
  245|    575|    }
  246|       |
  247|    535|    return 0;
  248|    575|}

jbig2_sd_new:
  100|  18.0k|{
  101|  18.0k|    Jbig2SymbolDict *new_dict = NULL;
  102|       |
  103|  18.0k|    new_dict = jbig2_new(ctx, Jbig2SymbolDict, 1);
  ------------------
  |  |  141|  18.0k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  104|  18.0k|    if (new_dict != NULL) {
  ------------------
  |  Branch (104:9): [True: 18.0k, False: 1]
  ------------------
  105|  18.0k|        new_dict->glyphs = jbig2_new(ctx, Jbig2Image *, n_symbols);
  ------------------
  |  |  141|  18.0k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  106|  18.0k|        new_dict->n_symbols = n_symbols;
  107|  18.0k|    } 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|  18.0k|    if (new_dict->glyphs != NULL) {
  ------------------
  |  Branch (112:9): [True: 12.0k, False: 5.96k]
  ------------------
  113|  12.0k|        memset(new_dict->glyphs, 0, n_symbols * sizeof(Jbig2Image *));
  114|  12.0k|    } else if (new_dict->n_symbols > 0) {
  ------------------
  |  Branch (114:16): [True: 172, False: 5.79k]
  ------------------
  115|    172|        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate glyphs for new empty symbol dictionary");
  ------------------
  |  |   68|    172|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  116|    172|        jbig2_free(ctx->allocator, new_dict);
  117|    172|        return NULL;
  118|    172|    }
  119|       |
  120|  17.8k|    return new_dict;
  121|  18.0k|}
jbig2_sd_release:
  126|  23.0k|{
  127|  23.0k|    uint32_t i;
  128|       |
  129|  23.0k|    if (dict == NULL)
  ------------------
  |  Branch (129:9): [True: 5.19k, False: 17.8k]
  ------------------
  130|  5.19k|        return;
  131|  17.8k|    if (dict->glyphs != NULL)
  ------------------
  |  Branch (131:9): [True: 12.0k, False: 5.79k]
  ------------------
  132|   508M|        for (i = 0; i < dict->n_symbols; i++)
  ------------------
  |  Branch (132:21): [True: 508M, False: 12.0k]
  ------------------
  133|   508M|            jbig2_image_release(ctx, dict->glyphs[i]);
  134|  17.8k|    jbig2_free(ctx->allocator, dict->glyphs);
  135|  17.8k|    jbig2_free(ctx->allocator, dict);
  136|  17.8k|}
jbig2_sd_count_referred:
  150|  13.8k|{
  151|  13.8k|    int index;
  152|  13.8k|    Jbig2Segment *rsegment;
  153|  13.8k|    uint32_t n_dicts = 0;
  154|       |
  155|   195k|    for (index = 0; index < segment->referred_to_segment_count; index++) {
  ------------------
  |  Branch (155:21): [True: 182k, False: 13.8k]
  ------------------
  156|   182k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  157|   182k|        if (rsegment && ((rsegment->flags & 63) == 0) &&
  ------------------
  |  Branch (157:13): [True: 78.0k, False: 104k]
  |  Branch (157:25): [True: 63.3k, False: 14.7k]
  ------------------
  158|  63.3k|            rsegment->result && (((Jbig2SymbolDict *) rsegment->result)->n_symbols > 0) && ((*((Jbig2SymbolDict *) rsegment->result)->glyphs) != NULL))
  ------------------
  |  Branch (158:13): [True: 63.3k, False: 0]
  |  Branch (158:33): [True: 58.2k, False: 5.11k]
  |  Branch (158:92): [True: 55.4k, False: 2.83k]
  ------------------
  159|  55.4k|            n_dicts++;
  160|   182k|    }
  161|       |
  162|  13.8k|    return (n_dicts);
  163|  13.8k|}
jbig2_sd_list_referred:
  168|  4.03k|{
  169|  4.03k|    int index;
  170|  4.03k|    Jbig2Segment *rsegment;
  171|  4.03k|    Jbig2SymbolDict **dicts;
  172|  4.03k|    uint32_t n_dicts = jbig2_sd_count_referred(ctx, segment);
  173|  4.03k|    uint32_t dindex = 0;
  174|       |
  175|  4.03k|    dicts = jbig2_new(ctx, Jbig2SymbolDict *, n_dicts);
  ------------------
  |  |  141|  4.03k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  176|  4.03k|    if (dicts == NULL) {
  ------------------
  |  Branch (176:9): [True: 2, False: 4.03k]
  ------------------
  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|  79.5k|    for (index = 0; index < segment->referred_to_segment_count; index++) {
  ------------------
  |  Branch (181:21): [True: 75.4k, False: 4.03k]
  ------------------
  182|  75.4k|        rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  183|  75.4k|        if (rsegment && ((rsegment->flags & 63) == 0) && rsegment->result &&
  ------------------
  |  Branch (183:13): [True: 34.3k, False: 41.1k]
  |  Branch (183:25): [True: 27.5k, False: 6.80k]
  |  Branch (183:58): [True: 27.5k, False: 0]
  ------------------
  184|  27.5k|                (((Jbig2SymbolDict *) rsegment->result)->n_symbols > 0) && ((*((Jbig2SymbolDict *) rsegment->result)->glyphs) != NULL)) {
  ------------------
  |  Branch (184:17): [True: 25.4k, False: 2.07k]
  |  Branch (184:76): [True: 24.3k, False: 1.05k]
  ------------------
  185|       |            /* add this referred to symbol dictionary */
  186|  24.3k|            dicts[dindex++] = (Jbig2SymbolDict *) rsegment->result;
  187|  24.3k|        }
  188|  75.4k|    }
  189|       |
  190|  4.03k|    if (dindex != n_dicts) {
  ------------------
  |  Branch (190:9): [True: 0, False: 4.03k]
  ------------------
  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|  4.03k|    return (dicts);
  198|  4.03k|}
jbig2_sd_cat:
  204|  1.12k|{
  205|  1.12k|    uint32_t i, j, k, symbols;
  206|  1.12k|    Jbig2SymbolDict *new_dict = NULL;
  207|       |
  208|       |    /* count the imported symbols and allocate a new array */
  209|  1.12k|    symbols = 0;
  210|  17.0k|    for (i = 0; i < n_dicts; i++)
  ------------------
  |  Branch (210:17): [True: 15.9k, False: 1.12k]
  ------------------
  211|  15.9k|    {
  212|  15.9k|        if (dicts[i]->n_symbols > UINT32_MAX - symbols)
  ------------------
  |  Branch (212:13): [True: 0, False: 15.9k]
  ------------------
  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|  15.9k|        symbols += dicts[i]->n_symbols;
  218|  15.9k|    }
  219|       |
  220|       |    /* fill a new array with new references to glyph pointers */
  221|  1.12k|    new_dict = jbig2_sd_new(ctx, symbols);
  222|  1.12k|    if (new_dict != NULL) {
  ------------------
  |  Branch (222:9): [True: 1.12k, False: 7]
  ------------------
  223|  1.12k|        k = 0;
  224|  17.0k|        for (i = 0; i < n_dicts; i++)
  ------------------
  |  Branch (224:21): [True: 15.9k, False: 1.12k]
  ------------------
  225|  25.1M|            for (j = 0; j < dicts[i]->n_symbols; j++)
  ------------------
  |  Branch (225:25): [True: 25.1M, False: 15.9k]
  ------------------
  226|  25.1M|                new_dict->glyphs[k++] = jbig2_image_reference(ctx, dicts[i]->glyphs[j]);
  227|  1.12k|    } else {
  228|      7|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate new symbol dictionary");
  ------------------
  |  |   68|      7|#define JBIG2_UNKNOWN_SEGMENT_NUMBER ~0U
  ------------------
  229|      7|    }
  230|       |
  231|  1.12k|    return new_dict;
  232|  1.12k|}
jbig2_symbol_dictionary:
  872|  6.25k|{
  873|  6.25k|    Jbig2SymbolDictParams params;
  874|  6.25k|    uint16_t flags;
  875|  6.25k|    uint32_t sdat_bytes;
  876|  6.25k|    uint32_t offset;
  877|  6.25k|    Jbig2ArithCx *GB_stats = NULL;
  878|  6.25k|    Jbig2ArithCx *GR_stats = NULL;
  879|  6.25k|    int table_index = 0;
  880|  6.25k|    const Jbig2HuffmanParams *huffman_params;
  881|       |
  882|  6.25k|    params.SDHUFF = 0;
  883|       |
  884|  6.25k|    if (segment->data_length < 10)
  ------------------
  |  Branch (884:9): [True: 33, False: 6.21k]
  ------------------
  885|     33|        goto too_short;
  886|       |
  887|       |    /* 7.4.2.1.1 */
  888|  6.21k|    flags = jbig2_get_uint16(segment_data);
  889|       |
  890|       |    /* zero params to ease cleanup later */
  891|  6.21k|    memset(&params, 0, sizeof(Jbig2SymbolDictParams));
  892|       |
  893|  6.21k|    params.SDHUFF = flags & 1;
  894|  6.21k|    params.SDREFAGG = (flags >> 1) & 1;
  895|  6.21k|    params.SDTEMPLATE = (flags >> 10) & 3;
  896|  6.21k|    params.SDRTEMPLATE = (flags >> 12) & 1;
  897|       |
  898|  6.21k|    if (params.SDHUFF) {
  ------------------
  |  Branch (898:9): [True: 3.36k, False: 2.85k]
  ------------------
  899|  3.36k|        switch ((flags & 0x000c) >> 2) {
  900|  3.04k|        case 0:                /* Table B.4 */
  ------------------
  |  Branch (900:9): [True: 3.04k, False: 321]
  ------------------
  901|  3.04k|            params.SDHUFFDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_D);
  902|  3.04k|            break;
  903|    179|        case 1:                /* Table B.5 */
  ------------------
  |  Branch (903:9): [True: 179, False: 3.18k]
  ------------------
  904|    179|            params.SDHUFFDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_E);
  905|    179|            break;
  906|    141|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (906:9): [True: 141, False: 3.22k]
  ------------------
  907|    141|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  908|    141|            if (huffman_params == NULL) {
  ------------------
  |  Branch (908:17): [True: 12, False: 129]
  ------------------
  909|     12|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DH huffman table not found (%d)", table_index);
  910|     12|                goto cleanup;
  911|     12|            }
  912|    129|            params.SDHUFFDH = jbig2_build_huffman_table(ctx, huffman_params);
  913|    129|            ++table_index;
  914|    129|            break;
  915|      1|        case 2:
  ------------------
  |  Branch (915:9): [True: 1, False: 3.36k]
  ------------------
  916|      1|        default:
  ------------------
  |  Branch (916:9): [True: 0, False: 3.36k]
  ------------------
  917|      1|            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "symbol dictionary specified invalid huffman table");
  918|  3.36k|        }
  919|  3.35k|        if (params.SDHUFFDH == NULL) {
  ------------------
  |  Branch (919:13): [True: 4, False: 3.34k]
  ------------------
  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.34k|        switch ((flags & 0x0030) >> 4) {
  925|  2.86k|        case 0:                /* Table B.2 */
  ------------------
  |  Branch (925:9): [True: 2.86k, False: 485]
  ------------------
  926|  2.86k|            params.SDHUFFDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_B);
  927|  2.86k|            break;
  928|    400|        case 1:                /* Table B.3 */
  ------------------
  |  Branch (928:9): [True: 400, False: 2.94k]
  ------------------
  929|    400|            params.SDHUFFDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_C);
  930|    400|            break;
  931|     81|        case 3:                /* Custom table from referred segment */
  ------------------
  |  Branch (931:9): [True: 81, False: 3.26k]
  ------------------
  932|     81|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  933|     81|            if (huffman_params == NULL) {
  ------------------
  |  Branch (933:17): [True: 5, False: 76]
  ------------------
  934|      5|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DW huffman table not found (%d)", table_index);
  935|      5|                goto cleanup;
  936|      5|            }
  937|     76|            params.SDHUFFDW = jbig2_build_huffman_table(ctx, huffman_params);
  938|     76|            ++table_index;
  939|     76|            break;
  940|      4|        case 2:
  ------------------
  |  Branch (940:9): [True: 4, False: 3.34k]
  ------------------
  941|      4|        default:
  ------------------
  |  Branch (941:9): [True: 0, False: 3.34k]
  ------------------
  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.34k|        }
  945|  3.33k|        if (params.SDHUFFDW == NULL) {
  ------------------
  |  Branch (945:13): [True: 8, False: 3.33k]
  ------------------
  946|      8|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate DW huffman table");
  947|      8|            goto cleanup;
  948|      8|        }
  949|       |
  950|  3.33k|        if (flags & 0x0040) {
  ------------------
  |  Branch (950:13): [True: 103, False: 3.22k]
  ------------------
  951|       |            /* Custom table from referred segment */
  952|    103|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  953|    103|            if (huffman_params == NULL) {
  ------------------
  |  Branch (953:17): [True: 8, False: 95]
  ------------------
  954|      8|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom BMSIZE huffman table not found (%d)", table_index);
  955|      8|                goto cleanup;
  956|      8|            }
  957|     95|            params.SDHUFFBMSIZE = jbig2_build_huffman_table(ctx, huffman_params);
  958|     95|            ++table_index;
  959|  3.22k|        } else {
  960|       |            /* Table B.1 */
  961|  3.22k|            params.SDHUFFBMSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
  962|  3.22k|        }
  963|  3.32k|        if (params.SDHUFFBMSIZE == NULL) {
  ------------------
  |  Branch (963:13): [True: 1, False: 3.32k]
  ------------------
  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.32k|        if (flags & 0x0080) {
  ------------------
  |  Branch (968:13): [True: 70, False: 3.25k]
  ------------------
  969|       |            /* Custom table from referred segment */
  970|     70|            huffman_params = jbig2_find_table(ctx, segment, table_index);
  971|     70|            if (huffman_params == NULL) {
  ------------------
  |  Branch (971:17): [True: 2, False: 68]
  ------------------
  972|      2|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom REFAGG huffman table not found (%d)", table_index);
  973|      2|                goto cleanup;
  974|      2|            }
  975|     68|            params.SDHUFFAGGINST = jbig2_build_huffman_table(ctx, huffman_params);
  976|     68|            ++table_index;
  977|  3.25k|        } else {
  978|       |            /* Table B.1 */
  979|  3.25k|            params.SDHUFFAGGINST = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
  980|  3.25k|        }
  981|  3.32k|        if (params.SDHUFFAGGINST == NULL) {
  ------------------
  |  Branch (981:13): [True: 1, False: 3.31k]
  ------------------
  982|      1|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate REFAGG huffman table");
  983|      1|            goto cleanup;
  984|      1|        }
  985|  3.32k|    }
  986|       |
  987|       |    /* FIXME: there are quite a few of these conditions to check */
  988|       |    /* maybe #ifdef CONFORMANCE and a separate routine */
  989|  6.17k|    if (!params.SDHUFF) {
  ------------------
  |  Branch (989:9): [True: 2.85k, False: 3.31k]
  ------------------
  990|  2.85k|        if (flags & 0x000c) {
  ------------------
  |  Branch (990:13): [True: 6, False: 2.84k]
  ------------------
  991|      6|            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "SDHUFF is zero, but contrary to spec SDHUFFDH is not.");
  992|      6|            goto cleanup;
  993|      6|        }
  994|  2.84k|        if (flags & 0x0030) {
  ------------------
  |  Branch (994:13): [True: 2, False: 2.84k]
  ------------------
  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.84k|    }
  999|       |
 1000|       |    /* 7.4.2.1.2 */
 1001|  6.16k|    sdat_bytes = params.SDHUFF ? 0 : params.SDTEMPLATE == 0 ? 8 : 2;
  ------------------
  |  Branch (1001:18): [True: 3.31k, False: 2.84k]
  |  Branch (1001:38): [True: 392, False: 2.45k]
  ------------------
 1002|  6.16k|    memcpy(params.sdat, segment_data + 2, sdat_bytes);
 1003|  6.16k|    offset = 2 + sdat_bytes;
 1004|       |
 1005|       |    /* 7.4.2.1.3 */
 1006|  6.16k|    if (params.SDREFAGG && !params.SDRTEMPLATE) {
  ------------------
  |  Branch (1006:9): [True: 1.21k, False: 4.95k]
  |  Branch (1006:28): [True: 822, False: 391]
  ------------------
 1007|    822|        if (offset + 4 > segment->data_length)
  ------------------
  |  Branch (1007:13): [True: 1, False: 821]
  ------------------
 1008|      1|            goto too_short;
 1009|    821|        memcpy(params.sdrat, segment_data + offset, 4);
 1010|    821|        offset += 4;
 1011|    821|    }
 1012|       |
 1013|  6.16k|    if (offset + 8 > segment->data_length)
  ------------------
  |  Branch (1013:9): [True: 10, False: 6.15k]
  ------------------
 1014|     10|        goto too_short;
 1015|       |
 1016|       |    /* 7.4.2.1.4 */
 1017|  6.15k|    params.SDNUMEXSYMS = jbig2_get_uint32(segment_data + offset);
 1018|       |    /* 7.4.2.1.5 */
 1019|  6.15k|    params.SDNUMNEWSYMS = jbig2_get_uint32(segment_data + offset + 4);
 1020|  6.15k|    offset += 8;
 1021|       |
 1022|  6.15k|    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
 1023|  6.15k|                "symbol dictionary, flags=%04x, %u exported syms, %u new syms", flags, params.SDNUMEXSYMS, params.SDNUMNEWSYMS);
 1024|       |
 1025|       |    /* 7.4.2.2 (2) */
 1026|  6.15k|    {
 1027|  6.15k|        uint32_t n_dicts = jbig2_sd_count_referred(ctx, segment);
 1028|  6.15k|        Jbig2SymbolDict **dicts = NULL;
 1029|       |
 1030|  6.15k|        if (n_dicts > 0) {
  ------------------
  |  Branch (1030:13): [True: 1.13k, False: 5.02k]
  ------------------
 1031|  1.13k|            dicts = jbig2_sd_list_referred(ctx, segment);
 1032|  1.13k|            if (dicts == NULL) {
  ------------------
  |  Branch (1032:17): [True: 1, False: 1.12k]
  ------------------
 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.12k|            params.SDINSYMS = jbig2_sd_cat(ctx, n_dicts, dicts);
 1037|  1.12k|            if (params.SDINSYMS == NULL) {
  ------------------
  |  Branch (1037:17): [True: 7, False: 1.12k]
  ------------------
 1038|      7|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol array in symbol dictionary");
 1039|      7|                jbig2_free(ctx->allocator, dicts);
 1040|      7|                goto cleanup;
 1041|      7|            }
 1042|  1.12k|            jbig2_free(ctx->allocator, dicts);
 1043|  1.12k|        }
 1044|  6.14k|        if (params.SDINSYMS != NULL) {
  ------------------
  |  Branch (1044:13): [True: 1.12k, False: 5.02k]
  ------------------
 1045|  1.12k|            params.SDNUMINSYMS = params.SDINSYMS->n_symbols;
 1046|  1.12k|        }
 1047|  6.14k|    }
 1048|       |
 1049|       |    /* 7.4.2.2 (3, 4) */
 1050|  6.14k|    if (flags & 0x0100) {
  ------------------
  |  Branch (1050:9): [True: 15, False: 6.13k]
  ------------------
 1051|     15|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "segment marks bitmap coding context as used (NYI)");
 1052|     15|        goto cleanup;
 1053|  6.13k|    } else {
 1054|  6.13k|        int stats_size = params.SDTEMPLATE == 0 ? 65536 : params.SDTEMPLATE == 1 ? 8192 : 1024;
  ------------------
  |  Branch (1054:26): [True: 2.96k, False: 3.16k]
  |  Branch (1054:59): [True: 683, False: 2.48k]
  ------------------
 1055|       |
 1056|  6.13k|        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  6.13k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
 1057|  6.13k|        if (GB_stats == NULL) {
  ------------------
  |  Branch (1057:13): [True: 1, False: 6.13k]
  ------------------
 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|  6.13k|        memset(GB_stats, 0, sizeof (Jbig2ArithCx) * stats_size);
 1062|       |
 1063|  6.13k|        stats_size = params.SDRTEMPLATE ? 1 << 10 : 1 << 13;
  ------------------
  |  Branch (1063:22): [True: 642, False: 5.48k]
  ------------------
 1064|  6.13k|        GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  ------------------
  |  |  141|  6.13k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
 1065|  6.13k|        if (GR_stats == NULL) {
  ------------------
  |  Branch (1065:13): [True: 1, False: 6.12k]
  ------------------
 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|  6.12k|        memset(GR_stats, 0, sizeof (Jbig2ArithCx) * stats_size);
 1071|  6.12k|    }
 1072|       |
 1073|  6.12k|    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|  6.12k|    if (flags & 0x0200) {
  ------------------
  |  Branch (1080:9): [True: 315, False: 5.81k]
  ------------------
 1081|       |        /* todo: retain GB_stats, GR_stats */
 1082|    315|        jbig2_free(ctx->allocator, GR_stats);
 1083|    315|        jbig2_free(ctx->allocator, GB_stats);
 1084|    315|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "segment marks bitmap coding context as retained (NYI)");
 1085|    315|        goto cleanup;
 1086|  5.81k|    } else {
 1087|  5.81k|        jbig2_free(ctx->allocator, GR_stats);
 1088|  5.81k|        jbig2_free(ctx->allocator, GB_stats);
 1089|  5.81k|    }
 1090|       |
 1091|  6.20k|cleanup:
 1092|  6.20k|    if (params.SDHUFF) {
  ------------------
  |  Branch (1092:9): [True: 3.36k, False: 2.84k]
  ------------------
 1093|  3.36k|        jbig2_release_huffman_table(ctx, params.SDHUFFDH);
 1094|  3.36k|        jbig2_release_huffman_table(ctx, params.SDHUFFDW);
 1095|  3.36k|        jbig2_release_huffman_table(ctx, params.SDHUFFBMSIZE);
 1096|  3.36k|        jbig2_release_huffman_table(ctx, params.SDHUFFAGGINST);
 1097|  3.36k|    }
 1098|  6.20k|    jbig2_sd_release(ctx, params.SDINSYMS);
 1099|       |
 1100|  6.20k|    return (segment->result != NULL) ? 0 : -1;
  ------------------
  |  Branch (1100:12): [True: 4.69k, False: 1.51k]
  ------------------
 1101|       |
 1102|     44|too_short:
 1103|     44|    if (params.SDHUFF) {
  ------------------
  |  Branch (1103:9): [True: 2, False: 42]
  ------------------
 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|     44|    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
 1110|  6.12k|}
jbig2_symbol_dict.c:jbig2_decode_symbol_dict:
  241|  6.12k|{
  242|  6.12k|    Jbig2SymbolDict *SDNEWSYMS = NULL;
  243|  6.12k|    Jbig2SymbolDict *SDEXSYMS = NULL;
  244|  6.12k|    uint32_t HCHEIGHT;
  245|  6.12k|    uint32_t NSYMSDECODED;
  246|  6.12k|    uint32_t SYMWIDTH, TOTWIDTH;
  247|  6.12k|    uint32_t HCFIRSTSYM;
  248|  6.12k|    uint32_t *SDNEWSYMWIDTHS = NULL;
  249|  6.12k|    uint8_t SBSYMCODELEN = 0;
  250|  6.12k|    Jbig2WordStream *ws = NULL;
  251|  6.12k|    Jbig2HuffmanState *hs = NULL;
  252|  6.12k|    Jbig2ArithState *as = NULL;
  253|  6.12k|    Jbig2ArithIntCtx *IADH = NULL;
  254|  6.12k|    Jbig2ArithIntCtx *IADW = NULL;
  255|  6.12k|    Jbig2ArithIntCtx *IAEX = NULL;
  256|  6.12k|    Jbig2ArithIntCtx *IAAI = NULL;
  257|  6.12k|    int code = 0;
  258|  6.12k|    Jbig2SymbolDict **refagg_dicts = NULL;
  259|  6.12k|    uint32_t i;
  260|  6.12k|    Jbig2TextRegionParams tparams;
  261|  6.12k|    Jbig2Image *image = NULL;
  262|  6.12k|    Jbig2Image *glyph = NULL;
  263|  6.12k|    uint32_t emptyruns = 0;
  264|       |
  265|  6.12k|    memset(&tparams, 0, sizeof(tparams));
  266|       |
  267|       |    /* 6.5.5 (3) */
  268|  6.12k|    HCHEIGHT = 0;
  269|  6.12k|    NSYMSDECODED = 0;
  270|       |
  271|  6.12k|    ws = jbig2_word_stream_buf_new(ctx, data, size);
  272|  6.12k|    if (ws == NULL) {
  ------------------
  |  Branch (272:9): [True: 1, False: 6.12k]
  ------------------
  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|  6.12k|    as = jbig2_arith_new(ctx, ws);
  278|  6.12k|    if (as == NULL) {
  ------------------
  |  Branch (278:9): [True: 10, False: 6.11k]
  ------------------
  279|     10|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when decoding symbol dictionary");
  280|     10|        jbig2_word_stream_buf_free(ctx, ws);
  281|     10|        return NULL;
  282|     10|    }
  283|       |
  284|  32.3k|    for (SBSYMCODELEN = 0; ((uint64_t) 1 << SBSYMCODELEN) < ((uint64_t) params->SDNUMINSYMS + params->SDNUMNEWSYMS); SBSYMCODELEN++);
  ------------------
  |  Branch (284:28): [True: 26.2k, False: 6.11k]
  ------------------
  285|       |
  286|  6.11k|    if (params->SDHUFF) {
  ------------------
  |  Branch (286:9): [True: 3.29k, False: 2.82k]
  ------------------
  287|  3.29k|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "huffman coded symbol dictionary");
  288|  3.29k|        hs = jbig2_huffman_new(ctx, ws);
  289|  3.29k|        tparams.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  290|  3.29k|        tparams.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  291|  3.29k|        tparams.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A); /* Table B.1 */
  292|  3.29k|        if (hs == NULL || tparams.SBHUFFRDX == NULL ||
  ------------------
  |  Branch (292:13): [True: 1, False: 3.29k]
  |  Branch (292:27): [True: 2, False: 3.29k]
  ------------------
  293|  3.29k|                tparams.SBHUFFRDY == NULL || tparams.SBHUFFRSIZE == NULL) {
  ------------------
  |  Branch (293:17): [True: 2, False: 3.29k]
  |  Branch (293:46): [True: 1, False: 3.29k]
  ------------------
  294|      6|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate for symbol bitmap");
  295|      6|            goto cleanup;
  296|      6|        }
  297|       |        /* 6.5.5 (2) */
  298|  3.29k|        if (!params->SDREFAGG) {
  ------------------
  |  Branch (298:13): [True: 2.52k, False: 764]
  ------------------
  299|  2.52k|            SDNEWSYMWIDTHS = jbig2_new(ctx, uint32_t, params->SDNUMNEWSYMS);
  ------------------
  |  |  141|  2.52k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  300|  2.52k|            if (SDNEWSYMWIDTHS == NULL) {
  ------------------
  |  Branch (300:17): [True: 9, False: 2.51k]
  ------------------
  301|      9|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate symbol widths (%u)", params->SDNUMNEWSYMS);
  302|      9|                goto cleanup;
  303|      9|            }
  304|  2.52k|        } else {
  305|    764|            tparams.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_F);    /* Table B.6 */
  306|    764|            tparams.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_H);    /* Table B.8 */
  307|    764|            tparams.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_K);    /* Table B.11 */
  308|    764|            tparams.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  309|    764|            tparams.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);   /* Table B.15 */
  310|    764|            if (tparams.SBHUFFFS == NULL || tparams.SBHUFFDS == NULL ||
  ------------------
  |  Branch (310:17): [True: 4, False: 760]
  |  Branch (310:45): [True: 1, False: 759]
  ------------------
  311|    759|                    tparams.SBHUFFDT == NULL || tparams.SBHUFFRDW == NULL ||
  ------------------
  |  Branch (311:21): [True: 1, False: 758]
  |  Branch (311:49): [True: 1, False: 757]
  ------------------
  312|    757|                    tparams.SBHUFFRDH == NULL) {
  ------------------
  |  Branch (312:21): [True: 1, False: 756]
  ------------------
  313|      8|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "out of memory creating text region huffman decoder entries");
  314|      8|                goto cleanup;
  315|      8|            }
  316|    764|        }
  317|  3.29k|    } else {
  318|  2.82k|        IADH = jbig2_arith_int_ctx_new(ctx);
  319|  2.82k|        IADW = jbig2_arith_int_ctx_new(ctx);
  320|  2.82k|        IAEX = jbig2_arith_int_ctx_new(ctx);
  321|  2.82k|        IAAI = jbig2_arith_int_ctx_new(ctx);
  322|  2.82k|        if (IADH == NULL || IADW == NULL || IAEX == NULL || IAAI == NULL) {
  ------------------
  |  Branch (322:13): [True: 1, False: 2.82k]
  |  Branch (322:29): [True: 1, False: 2.82k]
  |  Branch (322:45): [True: 1, False: 2.81k]
  |  Branch (322:61): [True: 1, False: 2.81k]
  ------------------
  323|      4|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol bitmap");
  324|      4|            goto cleanup;
  325|      4|        }
  326|  2.81k|        tparams.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
  327|  2.81k|        tparams.IARDX = jbig2_arith_int_ctx_new(ctx);
  328|  2.81k|        tparams.IARDY = jbig2_arith_int_ctx_new(ctx);
  329|  2.81k|        if (tparams.IAID == NULL || tparams.IARDX == NULL ||
  ------------------
  |  Branch (329:13): [True: 25, False: 2.79k]
  |  Branch (329:37): [True: 1, False: 2.79k]
  ------------------
  330|  2.79k|                tparams.IARDY == NULL) {
  ------------------
  |  Branch (330:17): [True: 1, False: 2.79k]
  ------------------
  331|     27|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region arithmetic decoder contexts");
  332|     27|            goto cleanup;
  333|     27|        }
  334|  2.79k|        if (params->SDREFAGG) {
  ------------------
  |  Branch (334:13): [True: 418, False: 2.37k]
  ------------------
  335|       |            /* Values from Table 17, section 6.5.8.2 (2) */
  336|    418|            tparams.IADT = jbig2_arith_int_ctx_new(ctx);
  337|    418|            tparams.IAFS = jbig2_arith_int_ctx_new(ctx);
  338|    418|            tparams.IADS = jbig2_arith_int_ctx_new(ctx);
  339|    418|            tparams.IAIT = jbig2_arith_int_ctx_new(ctx);
  340|       |            /* Table 31 */
  341|    418|            tparams.IARI = jbig2_arith_int_ctx_new(ctx);
  342|    418|            tparams.IARDW = jbig2_arith_int_ctx_new(ctx);
  343|    418|            tparams.IARDH = jbig2_arith_int_ctx_new(ctx);
  344|    418|            if (tparams.IADT == NULL || tparams.IAFS == NULL ||
  ------------------
  |  Branch (344:17): [True: 1, False: 417]
  |  Branch (344:41): [True: 1, False: 416]
  ------------------
  345|    416|                    tparams.IADS == NULL || tparams.IAIT == NULL ||
  ------------------
  |  Branch (345:21): [True: 1, False: 415]
  |  Branch (345:45): [True: 2, False: 413]
  ------------------
  346|    413|                    tparams.IARI == NULL || tparams.IARDW == NULL ||
  ------------------
  |  Branch (346:21): [True: 2, False: 411]
  |  Branch (346:45): [True: 1, False: 410]
  ------------------
  347|    410|                    tparams.IARDH == NULL) {
  ------------------
  |  Branch (347:21): [True: 1, False: 409]
  ------------------
  348|      9|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region arith decoder contexts");
  349|      9|                goto cleanup;
  350|      9|            }
  351|    418|        }
  352|  2.79k|    }
  353|  6.05k|    tparams.SBHUFF = params->SDHUFF;
  354|  6.05k|    tparams.SBREFINE = 1;
  355|  6.05k|    tparams.SBSTRIPS = 1;
  356|  6.05k|    tparams.SBDEFPIXEL = 0;
  357|  6.05k|    tparams.SBCOMBOP = JBIG2_COMPOSE_OR;
  358|  6.05k|    tparams.TRANSPOSED = 0;
  359|  6.05k|    tparams.REFCORNER = JBIG2_CORNER_TOPLEFT;
  360|  6.05k|    tparams.SBDSOFFSET = 0;
  361|  6.05k|    tparams.SBRTEMPLATE = params->SDRTEMPLATE;
  362|       |
  363|       |    /* 6.5.5 (1) */
  364|  6.05k|    SDNEWSYMS = jbig2_sd_new(ctx, params->SDNUMNEWSYMS);
  365|  6.05k|    if (SDNEWSYMS == NULL) {
  ------------------
  |  Branch (365:9): [True: 44, False: 6.01k]
  ------------------
  366|     44|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate new symbols (%u)", params->SDNUMNEWSYMS);
  367|     44|        goto cleanup;
  368|     44|    }
  369|       |
  370|  6.01k|    refagg_dicts = jbig2_new(ctx, Jbig2SymbolDict *, 2);
  ------------------
  |  |  141|  6.01k|#define jbig2_new(ctx, t, num) ((t *)jbig2_alloc(ctx->allocator, sizeof(t), num))
  ------------------
  371|  6.01k|    if (refagg_dicts == NULL) {
  ------------------
  |  Branch (371:9): [True: 1, False: 6.01k]
  ------------------
  372|      1|        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Out of memory allocating dictionary array");
  373|      1|        goto cleanup;
  374|      1|    }
  375|  6.01k|    refagg_dicts[0] = jbig2_sd_new(ctx, params->SDNUMINSYMS);
  376|  6.01k|    if (refagg_dicts[0] == NULL) {
  ------------------
  |  Branch (376:9): [True: 5, False: 6.00k]
  ------------------
  377|      5|        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "out of memory allocating symbol dictionary");
  378|      5|        goto cleanup;
  379|      5|    }
  380|  20.2M|    for (i = 0; i < params->SDNUMINSYMS; i++) {
  ------------------
  |  Branch (380:17): [True: 20.2M, False: 6.00k]
  ------------------
  381|  20.2M|        refagg_dicts[0]->glyphs[i] = jbig2_image_reference(ctx, params->SDINSYMS->glyphs[i]);
  382|  20.2M|    }
  383|  6.00k|    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.83k]
  ------------------
  387|  1.51M|        int32_t HCDH, DW;
  388|       |
  389|       |        /* 6.5.6 */
  390|  1.51M|        if (params->SDHUFF) {
  ------------------
  |  Branch (390:13): [True: 8.56k, False: 1.50M]
  ------------------
  391|  8.56k|            HCDH = jbig2_huffman_get(hs, params->SDHUFFDH, &code);
  392|  1.50M|        } else {
  393|  1.50M|            code = jbig2_arith_int_decode(ctx, IADH, as, &HCDH);
  394|  1.50M|        }
  395|  1.51M|        if (code < 0) {
  ------------------
  |  Branch (395:13): [True: 3, False: 1.51M]
  ------------------
  396|      3|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode height class delta");
  397|      3|            goto cleanup;
  398|      3|        }
  399|  1.51M|        if (code > 0) {
  ------------------
  |  Branch (399:13): [True: 9, False: 1.51M]
  ------------------
  400|      9|            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "OOB decoding height class delta");
  401|      9|            goto cleanup;
  402|      9|        }
  403|       |
  404|       |        /* 6.5.5 (4b) */
  405|  1.51M|        HCHEIGHT = HCHEIGHT + HCDH;
  406|  1.51M|        SYMWIDTH = 0;
  407|  1.51M|        TOTWIDTH = 0;
  408|  1.51M|        HCFIRSTSYM = NSYMSDECODED;
  409|       |
  410|  1.51M|        if ((int32_t) HCHEIGHT < 0) {
  ------------------
  |  Branch (410:13): [True: 45, False: 1.51M]
  ------------------
  411|     45|            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid HCHEIGHT value");
  412|     45|            goto cleanup;
  413|     45|        }
  414|       |#ifdef JBIG2_DEBUG
  415|       |        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "HCHEIGHT = %d", HCHEIGHT);
  416|       |#endif
  417|  1.51M|        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "decoding height class %d with %d syms decoded", HCHEIGHT, NSYMSDECODED);
  418|       |
  419|  25.3M|        for (;;) {
  420|       |            /* 6.5.7 */
  421|  25.3M|            if (params->SDHUFF) {
  ------------------
  |  Branch (421:17): [True: 23.6M, False: 1.66M]
  ------------------
  422|  23.6M|                DW = jbig2_huffman_get(hs, params->SDHUFFDW, &code);
  423|  23.6M|            } else {
  424|  1.66M|                code = jbig2_arith_int_decode(ctx, IADW, as, &DW);
  425|  1.66M|            }
  426|  25.3M|            if (code < 0)
  ------------------
  |  Branch (426:17): [True: 3, False: 25.3M]
  ------------------
  427|      3|            {
  428|      3|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode DW");
  429|      3|                goto cleanup;
  430|      3|            }
  431|       |            /* 6.5.5 (4c.i) */
  432|  25.3M|            if (code > 0) {
  ------------------
  |  Branch (432:17): [True: 1.50M, False: 23.8M]
  ------------------
  433|  1.50M|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "OOB when decoding DW signals end of height class %d", HCHEIGHT);
  434|  1.50M|                break;
  435|  1.50M|            }
  436|       |
  437|       |            /* check for broken symbol table */
  438|  23.8M|            if (NSYMSDECODED >= params->SDNUMNEWSYMS) {
  ------------------
  |  Branch (438:17): [True: 2.54k, False: 23.8M]
  ------------------
  439|  2.54k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "no OOB signaling end of height class %d, continuing", HCHEIGHT);
  440|  2.54k|                break;
  441|  2.54k|            }
  442|       |
  443|  23.8M|            if (DW < 0 && SYMWIDTH < (uint32_t) -DW) {
  ------------------
  |  Branch (443:17): [True: 8.67k, False: 23.7M]
  |  Branch (443:27): [True: 183, False: 8.49k]
  ------------------
  444|    183|                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|    183|                goto cleanup;
  446|    183|            }
  447|  23.8M|            if (DW > 0 && (uint32_t) DW > UINT32_MAX - SYMWIDTH) {
  ------------------
  |  Branch (447:17): [True: 117k, False: 23.6M]
  |  Branch (447:27): [True: 1, False: 117k]
  ------------------
  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.8M|            SYMWIDTH = SYMWIDTH + DW;
  453|  23.8M|            if (SYMWIDTH > UINT32_MAX - TOTWIDTH) {
  ------------------
  |  Branch (453:17): [True: 36, False: 23.8M]
  ------------------
  454|     36|                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|     36|                goto cleanup;
  456|     36|            }
  457|       |
  458|  23.8M|            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.8M|            if (!params->SDHUFF || params->SDREFAGG) {
  ------------------
  |  Branch (463:17): [True: 166k, False: 23.6M]
  |  Branch (463:36): [True: 4.44k, False: 23.6M]
  ------------------
  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|   170k|                if (!params->SDREFAGG) {
  ------------------
  |  Branch (468:21): [True: 160k, False: 10.3k]
  ------------------
  469|   160k|                    Jbig2GenericRegionParams region_params;
  470|   160k|                    int sdat_bytes;
  471|       |
  472|       |                    /* Table 16 */
  473|   160k|                    region_params.MMR = 0;
  474|   160k|                    region_params.GBTEMPLATE = params->SDTEMPLATE;
  475|   160k|                    region_params.TPGDON = 0;
  476|   160k|                    region_params.USESKIP = 0;
  477|   160k|                    sdat_bytes = params->SDTEMPLATE == 0 ? 8 : 2;
  ------------------
  |  Branch (477:34): [True: 19.9k, False: 140k]
  ------------------
  478|   160k|                    memcpy(region_params.gbat, params->sdat, sdat_bytes);
  479|       |
  480|   160k|                    image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
  481|   160k|                    if (image == NULL) {
  ------------------
  |  Branch (481:25): [True: 76, False: 160k]
  ------------------
  482|     76|                        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate image");
  483|     76|                        goto cleanup;
  484|     76|                    }
  485|       |
  486|   160k|                    code = jbig2_decode_generic_region(ctx, segment, &region_params, as, image, GB_stats);
  487|   160k|                    if (code < 0) {
  ------------------
  |  Branch (487:25): [True: 26, False: 160k]
  ------------------
  488|     26|                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode generic region");
  489|     26|                        goto cleanup;
  490|     26|                    }
  491|       |
  492|   160k|                    SDNEWSYMS->glyphs[NSYMSDECODED] = image;
  493|   160k|                    image = NULL;
  494|   160k|                } else {
  495|       |                    /* 6.5.8.2 refinement/aggregate symbol */
  496|  10.3k|                    uint32_t REFAGGNINST;
  497|       |
  498|  10.3k|                    if (params->SDHUFF) {
  ------------------
  |  Branch (498:25): [True: 4.44k, False: 5.95k]
  ------------------
  499|  4.44k|                        REFAGGNINST = jbig2_huffman_get(hs, params->SDHUFFAGGINST, &code);
  500|  5.95k|                    } else {
  501|  5.95k|                        code = jbig2_arith_int_decode(ctx, IAAI, as, (int32_t *) &REFAGGNINST);
  502|  5.95k|                    }
  503|  10.3k|                    if (code < 0) {
  ------------------
  |  Branch (503:25): [True: 1, False: 10.3k]
  ------------------
  504|      1|                        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode number of symbols in aggregate glyph");
  505|      1|                        goto cleanup;
  506|      1|                    }
  507|  10.3k|                    if (code > 0) {
  ------------------
  |  Branch (507:25): [True: 4, False: 10.3k]
  ------------------
  508|      4|                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB in number of symbols in aggregate glyph");
  509|      4|                        goto cleanup;
  510|      4|                    }
  511|  10.3k|                    if ((int32_t) REFAGGNINST <= 0) {
  ------------------
  |  Branch (511:25): [True: 139, False: 10.2k]
  ------------------
  512|    139|                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid number of symbols in aggregate glyph");
  513|    139|                        goto cleanup;
  514|    139|                    }
  515|       |
  516|  10.2k|                    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "aggregate symbol coding (%d instances)", REFAGGNINST);
  517|       |
  518|  10.2k|                    if (REFAGGNINST > 1) {
  ------------------
  |  Branch (518:25): [True: 8.28k, False: 1.96k]
  ------------------
  519|  8.28k|                        tparams.SBNUMINSTANCES = REFAGGNINST;
  520|       |
  521|  8.28k|                        image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
  522|  8.28k|                        if (image == NULL) {
  ------------------
  |  Branch (522:29): [True: 15, False: 8.27k]
  ------------------
  523|     15|                            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol image");
  524|     15|                            goto cleanup;
  525|     15|                        }
  526|       |
  527|       |                        /* multiple symbols are handled as a text region */
  528|  8.27k|                        code = jbig2_decode_text_region(ctx, segment, &tparams, (const Jbig2SymbolDict * const *)refagg_dicts,
  529|  8.27k|                                                        2, image, GR_stats, as, ws);
  530|  8.27k|                        if (code < 0) {
  ------------------
  |  Branch (530:29): [True: 342, False: 7.93k]
  ------------------
  531|    342|                            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode text region");
  532|    342|                            goto cleanup;
  533|    342|                        }
  534|       |
  535|  7.93k|                        SDNEWSYMS->glyphs[NSYMSDECODED] = image;
  536|  7.93k|                        image = NULL;
  537|  7.93k|                    } else {
  538|       |                        /* 6.5.8.2.2 */
  539|       |                        /* bool SBHUFF = params->SDHUFF; */
  540|  1.96k|                        Jbig2RefinementRegionParams rparams;
  541|  1.96k|                        uint32_t ID;
  542|  1.96k|                        int32_t RDX, RDY;
  543|  1.96k|                        size_t BMSIZE = 0;
  544|  1.96k|                        uint32_t ninsyms = params->SDNUMINSYMS;
  545|  1.96k|                        int code1 = 0;
  546|  1.96k|                        int code2 = 0;
  547|  1.96k|                        int code3 = 0;
  548|  1.96k|                        int code4 = 0;
  549|  1.96k|                        int code5 = 0;
  550|       |
  551|       |                        /* 6.5.8.2.2 (2, 3, 4, 5) */
  552|  1.96k|                        if (params->SDHUFF) {
  ------------------
  |  Branch (552:29): [True: 1.43k, False: 523]
  ------------------
  553|  1.43k|                            ID = jbig2_huffman_get_bits(hs, SBSYMCODELEN, &code1);
  554|  1.43k|                            RDX = jbig2_huffman_get(hs, tparams.SBHUFFRDX, &code2);
  555|  1.43k|                            RDY = jbig2_huffman_get(hs, tparams.SBHUFFRDY, &code3);
  556|  1.43k|                            BMSIZE = jbig2_huffman_get(hs, tparams.SBHUFFRSIZE, &code4);
  557|  1.43k|                            code5 = jbig2_huffman_skip(hs);
  558|  1.43k|                        } else {
  559|    523|                            code1 = jbig2_arith_iaid_decode(ctx, tparams.IAID, as, (int32_t *) &ID);
  560|    523|                            code2 = jbig2_arith_int_decode(ctx, tparams.IARDX, as, &RDX);
  561|    523|                            code3 = jbig2_arith_int_decode(ctx, tparams.IARDY, as, &RDY);
  562|    523|                        }
  563|       |
  564|  1.96k|                        if (code1 < 0 || code2 < 0 || code3 < 0 || code4 < 0 || code5 < 0) {
  ------------------
  |  Branch (564:29): [True: 0, False: 1.96k]
  |  Branch (564:42): [True: 2, False: 1.96k]
  |  Branch (564:55): [True: 0, False: 1.96k]
  |  Branch (564:68): [True: 0, False: 1.96k]
  |  Branch (564:81): [True: 0, False: 1.96k]
  ------------------
  565|      2|                            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode data");
  566|      2|                            goto cleanup;
  567|      2|                        }
  568|  1.96k|                        if (code1 > 0 || code2 > 0 || code3 > 0 || code4 > 0 || code5 > 0) {
  ------------------
  |  Branch (568:29): [True: 0, False: 1.96k]
  |  Branch (568:42): [True: 2, False: 1.95k]
  |  Branch (568:55): [True: 4, False: 1.95k]
  |  Branch (568:68): [True: 0, False: 1.95k]
  |  Branch (568:81): [True: 0, False: 1.95k]
  ------------------
  569|      6|                            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB in single refinement/aggregate coded symbol data");
  570|      6|                            goto cleanup;
  571|      6|                        }
  572|       |
  573|  1.95k|                        if (ID >= ninsyms + NSYMSDECODED) {
  ------------------
  |  Branch (573:29): [True: 30, False: 1.92k]
  ------------------
  574|     30|                            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "refinement references unknown symbol %d", ID);
  575|     30|                            goto cleanup;
  576|     30|                        }
  577|       |
  578|  1.92k|                        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  579|  1.92k|                                    "symbol is a refinement of ID %d with the refinement applied at (%d,%d)", ID, RDX, RDY);
  580|       |
  581|  1.92k|                        image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
  582|  1.92k|                        if (image == NULL) {
  ------------------
  |  Branch (582:29): [True: 1, False: 1.92k]
  ------------------
  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.92k|                        rparams.GRTEMPLATE = params->SDRTEMPLATE;
  589|  1.92k|                        rparams.GRREFERENCE = (ID < ninsyms) ? params->SDINSYMS->glyphs[ID] : SDNEWSYMS->glyphs[ID - ninsyms];
  ------------------
  |  Branch (589:47): [True: 394, False: 1.52k]
  ------------------
  590|       |                        /* SumatraPDF: fail on missing glyphs */
  591|  1.92k|                        if (rparams.GRREFERENCE == NULL) {
  ------------------
  |  Branch (591:29): [True: 3, False: 1.92k]
  ------------------
  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.92k|                        rparams.GRREFERENCEDX = RDX;
  596|  1.92k|                        rparams.GRREFERENCEDY = RDY;
  597|  1.92k|                        rparams.TPGRON = 0;
  598|  1.92k|                        memcpy(rparams.grat, params->sdrat, 4);
  599|  1.92k|                        code = jbig2_decode_refinement_region(ctx, segment, &rparams, as, image, GR_stats);
  600|  1.92k|                        if (code < 0) {
  ------------------
  |  Branch (600:29): [True: 1, False: 1.91k]
  ------------------
  601|      1|                            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
  602|      1|                            goto cleanup;
  603|      1|                        }
  604|       |
  605|  1.91k|                        SDNEWSYMS->glyphs[NSYMSDECODED] = image;
  606|  1.91k|                        image = NULL;
  607|       |
  608|       |                        /* 6.5.8.2.2 (7) */
  609|  1.91k|                        if (params->SDHUFF) {
  ------------------
  |  Branch (609:29): [True: 1.41k, False: 507]
  ------------------
  610|  1.41k|                            if (BMSIZE == 0)
  ------------------
  |  Branch (610:33): [True: 738, False: 674]
  ------------------
  611|    738|                                BMSIZE = (size_t) SDNEWSYMS->glyphs[NSYMSDECODED]->height *
  612|    738|                                    SDNEWSYMS->glyphs[NSYMSDECODED]->stride;
  613|  1.41k|                            code = jbig2_huffman_advance(hs, BMSIZE);
  614|  1.41k|                            if (code < 0) {
  ------------------
  |  Branch (614:33): [True: 0, False: 1.41k]
  ------------------
  615|      0|                                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to advance after huffman decoding in refinement region");
  616|      0|                                goto cleanup;
  617|      0|                            }
  618|  1.41k|                        }
  619|  1.91k|                    }
  620|  10.2k|                }
  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|   170k|            }
  641|       |
  642|       |            /* 6.5.5 (4c.iii) */
  643|  23.7M|            if (params->SDHUFF && !params->SDREFAGG) {
  ------------------
  |  Branch (643:17): [True: 23.6M, False: 166k]
  |  Branch (643:35): [True: 23.6M, False: 4.01k]
  ------------------
  644|  23.6M|                SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
  645|  23.6M|            }
  646|       |
  647|       |            /* 6.5.5 (4c.iv) */
  648|  23.7M|            NSYMSDECODED = NSYMSDECODED + 1;
  649|       |
  650|  23.7M|            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "decoded symbol %u of %u (%ux%u)", NSYMSDECODED, params->SDNUMNEWSYMS, SYMWIDTH, HCHEIGHT);
  651|       |
  652|  23.7M|        }                       /* end height class decode loop */
  653|       |
  654|       |        /* 6.5.5 (4d) */
  655|  1.51M|        if (params->SDHUFF && !params->SDREFAGG) {
  ------------------
  |  Branch (655:13): [True: 8.05k, False: 1.50M]
  |  Branch (655:31): [True: 7.76k, False: 287]
  ------------------
  656|       |            /* 6.5.9 */
  657|  7.76k|            size_t BMSIZE;
  658|  7.76k|            uint32_t j;
  659|  7.76k|            int x;
  660|       |
  661|  7.76k|            BMSIZE = jbig2_huffman_get(hs, params->SDHUFFBMSIZE, &code);
  662|  7.76k|            if (code < 0) {
  ------------------
  |  Branch (662:17): [True: 1, False: 7.76k]
  ------------------
  663|      1|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error decoding size of collective bitmap");
  664|      1|                goto cleanup;
  665|      1|            }
  666|  7.76k|            if (code > 0) {
  ------------------
  |  Branch (666:17): [True: 1, False: 7.76k]
  ------------------
  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|  7.76k|            code = jbig2_huffman_skip(hs);
  673|  7.76k|            if (code < 0) {
  ------------------
  |  Branch (673:17): [True: 0, False: 7.76k]
  ------------------
  674|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to skip to next byte when decoding collective bitmap");
  675|      0|            }
  676|       |
  677|  7.76k|            image = jbig2_image_new(ctx, TOTWIDTH, HCHEIGHT);
  678|  7.76k|            if (image == NULL) {
  ------------------
  |  Branch (678:17): [True: 51, False: 7.71k]
  ------------------
  679|     51|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate collective bitmap image");
  680|     51|                goto cleanup;
  681|     51|            }
  682|       |
  683|  7.71k|            if (BMSIZE == 0) {
  ------------------
  |  Branch (683:17): [True: 865, False: 6.85k]
  ------------------
  684|       |                /* if BMSIZE == 0 bitmap is uncompressed */
  685|    865|                const byte *src = data + jbig2_huffman_offset(hs);
  686|    865|                const int stride = (image->width >> 3) + ((image->width & 7) ? 1 : 0);
  ------------------
  |  Branch (686:59): [True: 792, False: 73]
  ------------------
  687|    865|                byte *dst = image->data;
  688|       |
  689|       |                /* SumatraPDF: prevent read access violation */
  690|    865|                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: 44, False: 821]
  |  Branch (690:56): [True: 10, False: 811]
  |  Branch (690:127): [True: 0, False: 811]
  ------------------
  691|     54|                    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "not enough data for decoding uncompressed (%d/%li)", image->height * stride,
  692|     54|                                (long) (size - jbig2_huffman_offset(hs)));
  693|     54|                    goto cleanup;
  694|     54|                }
  695|       |
  696|    811|                BMSIZE = (size_t) image->height * stride;
  697|    811|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  698|    811|                            "reading %dx%d uncompressed bitmap for %d symbols (%li bytes)", image->width, image->height, NSYMSDECODED - HCFIRSTSYM, (long) BMSIZE);
  699|       |
  700|  9.52k|                for (j = 0; j < image->height; j++) {
  ------------------
  |  Branch (700:29): [True: 8.71k, False: 811]
  ------------------
  701|  8.71k|                    memcpy(dst, src, stride);
  702|  8.71k|                    dst += image->stride;
  703|  8.71k|                    src += stride;
  704|  8.71k|                }
  705|  6.85k|            } else {
  706|  6.85k|                Jbig2GenericRegionParams rparams;
  707|       |
  708|       |                /* SumatraPDF: prevent read access violation */
  709|  6.85k|                if (size < jbig2_huffman_offset(hs) || size < BMSIZE || size - jbig2_huffman_offset(hs) < BMSIZE) {
  ------------------
  |  Branch (709:21): [True: 14, False: 6.83k]
  |  Branch (709:56): [True: 79, False: 6.75k]
  |  Branch (709:73): [True: 14, False: 6.74k]
  ------------------
  710|    107|                    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "not enough data for decoding (%li/%li)", (long) BMSIZE, (long) (size - jbig2_huffman_offset(hs)));
  711|    107|                    goto cleanup;
  712|    107|                }
  713|       |
  714|  6.74k|                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  715|  6.74k|                            "reading %dx%d collective bitmap for %d symbols (%li bytes)", image->width, image->height, NSYMSDECODED - HCFIRSTSYM, (long) BMSIZE);
  716|       |
  717|  6.74k|                rparams.MMR = 1;
  718|  6.74k|                code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data + jbig2_huffman_offset(hs), BMSIZE, image);
  719|  6.74k|                if (code) {
  ------------------
  |  Branch (719:21): [True: 4, False: 6.73k]
  ------------------
  720|      4|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode MMR-coded generic region");
  721|      4|                    goto cleanup;
  722|      4|                }
  723|  6.74k|            }
  724|       |
  725|       |            /* advance past the data we've just read */
  726|  7.55k|            code = jbig2_huffman_advance(hs, BMSIZE);
  727|  7.55k|            if (code < 0) {
  ------------------
  |  Branch (727:17): [True: 0, False: 7.55k]
  ------------------
  728|      0|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to advance after huffman decoding MMR bitmap image");
  729|      0|                goto cleanup;
  730|      0|            }
  731|       |
  732|       |            /* copy the collective bitmap into the symbol dictionary */
  733|  7.55k|            x = 0;
  734|   230k|            for (j = HCFIRSTSYM; j < NSYMSDECODED; j++) {
  ------------------
  |  Branch (734:34): [True: 223k, False: 7.52k]
  ------------------
  735|   223k|                glyph = jbig2_image_new(ctx, SDNEWSYMWIDTHS[j], HCHEIGHT);
  736|   223k|                if (glyph == NULL) {
  ------------------
  |  Branch (736:21): [True: 22, False: 223k]
  ------------------
  737|     22|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to copy the collective bitmap into symbol dictionary");
  738|     22|                    goto cleanup;
  739|     22|                }
  740|   223k|                code = jbig2_image_compose(ctx, glyph, image, -x, 0, JBIG2_COMPOSE_REPLACE);
  741|   223k|                if (code) {
  ------------------
  |  Branch (741:21): [True: 0, False: 223k]
  ------------------
  742|      0|                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to compose image into glyph");
  743|      0|                    goto cleanup;
  744|      0|                }
  745|   223k|                x += SDNEWSYMWIDTHS[j];
  746|   223k|                SDNEWSYMS->glyphs[j] = glyph;
  747|   223k|                glyph = NULL;
  748|   223k|            }
  749|  7.52k|            jbig2_image_release(ctx, image);
  750|  7.52k|            image = NULL;
  751|  7.52k|        }
  752|       |
  753|  1.51M|    }                           /* end of symbol decode loop */
  754|       |
  755|       |    /* 6.5.10 */
  756|  4.83k|    SDEXSYMS = jbig2_sd_new(ctx, params->SDNUMEXSYMS);
  757|  4.83k|    if (SDEXSYMS == NULL) {
  ------------------
  |  Branch (757:9): [True: 117, False: 4.72k]
  ------------------
  758|    117|        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbols exported from symbols dictionary");
  759|    117|        goto cleanup;
  760|  4.72k|    } else {
  761|  4.72k|        uint32_t i = 0;
  762|  4.72k|        uint32_t j = 0;
  763|  4.72k|        uint32_t k;
  764|  4.72k|        int exflag = 0;
  765|  4.72k|        uint32_t limit = params->SDNUMINSYMS + params->SDNUMNEWSYMS;
  766|  4.72k|        uint32_t EXRUNLENGTH;
  767|       |
  768|   132k|        while (i < limit) {
  ------------------
  |  Branch (768:16): [True: 128k, False: 4.69k]
  ------------------
  769|   128k|            if (params->SDHUFF)
  ------------------
  |  Branch (769:17): [True: 19.4k, False: 108k]
  ------------------
  770|  19.4k|                EXRUNLENGTH = jbig2_huffman_get(hs, tparams.SBHUFFRSIZE, &code);
  771|   108k|            else
  772|   108k|                code = jbig2_arith_int_decode(ctx, IAEX, as, (int32_t *) &EXRUNLENGTH);
  773|   128k|            if (code < 0) {
  ------------------
  |  Branch (773:17): [True: 1, False: 128k]
  ------------------
  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|   128k|            if (code > 0) {
  ------------------
  |  Branch (780:17): [True: 10, False: 128k]
  ------------------
  781|     10|                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|     10|                jbig2_sd_release(ctx, SDEXSYMS);
  784|     10|                SDEXSYMS = NULL;
  785|     10|                break;
  786|     10|            }
  787|       |
  788|       |            /* prevent infinite list of empty runs, 1000 is just an arbitrary number */
  789|   128k|            if (EXRUNLENGTH <= 0 && ++emptyruns == 1000) {
  ------------------
  |  Branch (789:17): [True: 19.0k, False: 109k]
  |  Branch (789:37): [True: 16, False: 19.0k]
  ------------------
  790|     16|                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "runlength too small in export symbol table (%u == 0 i = %u limit = %u)", EXRUNLENGTH, i, limit);
  791|       |                /* skip to the cleanup code and return SDEXSYMS = NULL */
  792|     16|                jbig2_sd_release(ctx, SDEXSYMS);
  793|     16|                SDEXSYMS = NULL;
  794|     16|                break;
  795|   128k|            } else if (EXRUNLENGTH > 0) {
  ------------------
  |  Branch (795:24): [True: 109k, False: 19.0k]
  ------------------
  796|   109k|                emptyruns = 0;
  797|   109k|            }
  798|       |
  799|   128k|            if (EXRUNLENGTH > limit - i) {
  ------------------
  |  Branch (799:17): [True: 3.62k, False: 124k]
  ------------------
  800|  3.62k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "exporting more symbols than available (%u > %u), capping", i + EXRUNLENGTH, limit);
  801|  3.62k|                EXRUNLENGTH = limit - i;
  802|  3.62k|            }
  803|   128k|            if (exflag && j + EXRUNLENGTH > params->SDNUMEXSYMS) {
  ------------------
  |  Branch (803:17): [True: 63.0k, False: 64.9k]
  |  Branch (803:27): [True: 2.16k, False: 60.9k]
  ------------------
  804|  2.16k|                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "exporting more symbols than may be exported (%u > %u), capping", j + EXRUNLENGTH, params->SDNUMEXSYMS);
  805|  2.16k|                EXRUNLENGTH = params->SDNUMEXSYMS - j;
  806|  2.16k|            }
  807|       |
  808|  16.1M|            for (k = 0; k < EXRUNLENGTH; k++) {
  ------------------
  |  Branch (808:25): [True: 16.0M, False: 128k]
  ------------------
  809|  16.0M|                if (exflag) {
  ------------------
  |  Branch (809:21): [True: 2.11M, False: 13.9M]
  ------------------
  810|  2.11M|                    Jbig2Image *img;
  811|  2.11M|                    if (i < params->SDNUMINSYMS) {
  ------------------
  |  Branch (811:25): [True: 2.10M, False: 13.6k]
  ------------------
  812|  2.10M|                        img = params->SDINSYMS->glyphs[i];
  813|  2.10M|                    } else {
  814|  13.6k|                        img = SDNEWSYMS->glyphs[i - params->SDNUMINSYMS];
  815|  13.6k|                    }
  816|  2.11M|                    SDEXSYMS->glyphs[j++] = jbig2_image_reference(ctx, img);
  817|  2.11M|                }
  818|  16.0M|                i++;
  819|  16.0M|            }
  820|   128k|            exflag = !exflag;
  821|   128k|        }
  822|  4.72k|    }
  823|       |
  824|  6.11k|cleanup:
  825|  6.11k|    jbig2_image_release(ctx, glyph);
  826|  6.11k|    jbig2_image_release(ctx, image);
  827|  6.11k|    if (refagg_dicts != NULL) {
  ------------------
  |  Branch (827:9): [True: 6.01k, False: 108]
  ------------------
  828|  6.01k|        if (refagg_dicts[0] != NULL)
  ------------------
  |  Branch (828:13): [True: 6.00k, False: 5]
  ------------------
  829|  6.00k|            jbig2_sd_release(ctx, refagg_dicts[0]);
  830|       |        /* skip releasing refagg_dicts[1] as that is the same as SDNEWSYMS */
  831|  6.01k|        jbig2_free(ctx->allocator, refagg_dicts);
  832|  6.01k|    }
  833|  6.11k|    jbig2_sd_release(ctx, SDNEWSYMS);
  834|  6.11k|    if (params->SDHUFF) {
  ------------------
  |  Branch (834:9): [True: 3.29k, False: 2.82k]
  ------------------
  835|  3.29k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRSIZE);
  836|  3.29k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDY);
  837|  3.29k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDX);
  838|  3.29k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDH);
  839|  3.29k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFRDW);
  840|  3.29k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFDT);
  841|  3.29k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFDS);
  842|  3.29k|        jbig2_release_huffman_table(ctx, tparams.SBHUFFFS);
  843|  3.29k|        if (!params->SDREFAGG) {
  ------------------
  |  Branch (843:13): [True: 2.52k, False: 770]
  ------------------
  844|  2.52k|            jbig2_free(ctx->allocator, SDNEWSYMWIDTHS);
  845|  2.52k|        }
  846|  3.29k|        jbig2_huffman_free(ctx, hs);
  847|  3.29k|    } else {
  848|  2.82k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDY);
  849|  2.82k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDX);
  850|  2.82k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDH);
  851|  2.82k|        jbig2_arith_int_ctx_free(ctx, tparams.IARDW);
  852|  2.82k|        jbig2_arith_int_ctx_free(ctx, tparams.IARI);
  853|  2.82k|        jbig2_arith_iaid_ctx_free(ctx, tparams.IAID);
  854|  2.82k|        jbig2_arith_int_ctx_free(ctx, tparams.IAIT);
  855|  2.82k|        jbig2_arith_int_ctx_free(ctx, tparams.IADS);
  856|  2.82k|        jbig2_arith_int_ctx_free(ctx, tparams.IAFS);
  857|  2.82k|        jbig2_arith_int_ctx_free(ctx, tparams.IADT);
  858|  2.82k|        jbig2_arith_int_ctx_free(ctx, IAAI);
  859|  2.82k|        jbig2_arith_int_ctx_free(ctx, IAEX);
  860|  2.82k|        jbig2_arith_int_ctx_free(ctx, IADW);
  861|  2.82k|        jbig2_arith_int_ctx_free(ctx, IADH);
  862|  2.82k|    }
  863|  6.11k|    jbig2_free(ctx->allocator, as);
  864|  6.11k|    jbig2_word_stream_buf_free(ctx, ws);
  865|       |
  866|  6.11k|    return SDEXSYMS;
  867|  4.83k|}

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

