gd_error:
   95|    245|{
   96|    245|    va_list args;
   97|       |
   98|    245|    va_start(args, format);
   99|    245|    _gd_error_ex(GD_WARNING, format, args);
  ------------------
  |  |   38|    245|#define GD_WARNING LOG_WARNING
  ------------------
  100|       |    va_end(args);
  101|    245|}
gd_error_ex:
  103|    112|{
  104|    112|    va_list args;
  105|       |
  106|    112|    va_start(args, format);
  107|    112|    _gd_error_ex(priority, format, args);
  108|       |    va_end(args);
  109|    112|}
gdImageCreateTrueColor:
  270|    554|{
  271|    554|    int i;
  272|    554|    gdImagePtr im;
  273|       |
  274|    554|    if (overflow2(sx, sy)) {
  ------------------
  |  Branch (274:9): [True: 0, False: 554]
  ------------------
  275|      0|        return NULL;
  276|      0|    }
  277|    554|    if (overflow2(sizeof(int *), sy)) {
  ------------------
  |  Branch (277:9): [True: 4, False: 550]
  ------------------
  278|      4|        return NULL;
  279|      4|    }
  280|    550|    if (overflow2(sizeof(int), sx)) {
  ------------------
  |  Branch (280:9): [True: 0, False: 550]
  ------------------
  281|      0|        return NULL;
  282|      0|    }
  283|       |
  284|    550|    im = (gdImage *)gdMalloc(sizeof(gdImage));
  285|    550|    if (!im) {
  ------------------
  |  Branch (285:9): [True: 0, False: 550]
  ------------------
  286|      0|        return 0;
  287|      0|    }
  288|    550|    memset(im, 0, sizeof(gdImage));
  289|       |
  290|    550|    im->tpixels = (int **)gdMalloc(sizeof(int *) * sy);
  291|    550|    if (!im->tpixels) {
  ------------------
  |  Branch (291:9): [True: 0, False: 550]
  ------------------
  292|      0|        gdFree(im);
  293|      0|        return 0;
  294|      0|    }
  295|    550|    im->polyInts = 0;
  296|    550|    im->polyAllocated = 0;
  297|    550|    im->brush = 0;
  298|    550|    im->tile = 0;
  299|    550|    im->style = 0;
  300|   108k|    for (i = 0; (i < sy); i++) {
  ------------------
  |  Branch (300:17): [True: 107k, False: 550]
  ------------------
  301|   107k|        im->tpixels[i] = (int *)gdCalloc(sx, sizeof(int));
  302|   107k|        if (!im->tpixels[i]) {
  ------------------
  |  Branch (302:13): [True: 0, False: 107k]
  ------------------
  303|       |            /* 2.0.34 */
  304|      0|            i--;
  305|      0|            while (i >= 0) {
  ------------------
  |  Branch (305:20): [True: 0, False: 0]
  ------------------
  306|      0|                gdFree(im->tpixels[i]);
  307|      0|                i--;
  308|      0|            }
  309|      0|            gdFree(im->tpixels);
  310|      0|            gdFree(im);
  311|      0|            return 0;
  312|      0|        }
  313|   107k|    }
  314|    550|    im->sx = sx;
  315|    550|    im->sy = sy;
  316|    550|    im->transparent = (-1);
  317|    550|    im->interlace = 0;
  318|    550|    im->trueColor = 1;
  319|       |    /* 2.0.2: alpha blending is now on by default, and saving of alpha is
  320|       |       off by default. This allows font antialiasing to work as expected
  321|       |       on the first try in JPEGs -- quite important -- and also allows
  322|       |       for smaller PNGs when saving of alpha channel is not really
  323|       |       desired, which it usually isn't! */
  324|    550|    im->saveAlphaFlag = 0;
  325|    550|    im->alphaBlendingFlag = 1;
  326|    550|    im->thick = 1;
  327|    550|    im->AA = 0;
  328|    550|    im->cx1 = 0;
  329|    550|    im->cy1 = 0;
  330|    550|    im->cx2 = im->sx - 1;
  331|    550|    im->cy2 = im->sy - 1;
  332|    550|    im->res_x = GD_RESOLUTION;
  ------------------
  |  | 8517|    550|#define GD_RESOLUTION 96 /* pixels per inch */
  ------------------
  333|    550|    im->res_y = GD_RESOLUTION;
  ------------------
  |  | 8517|    550|#define GD_RESOLUTION 96 /* pixels per inch */
  ------------------
  334|       |    im->interpolation = NULL;
  335|    550|    im->interpolation_id = GD_BILINEAR_FIXED;
  336|    550|    return im;
  337|    550|}
gdImageDestroy:
  340|    550|{
  341|    550|    int i;
  342|    550|    if (im->pixels) {
  ------------------
  |  Branch (342:9): [True: 0, False: 550]
  ------------------
  343|      0|        for (i = 0; (i < im->sy); i++) {
  ------------------
  |  Branch (343:21): [True: 0, False: 0]
  ------------------
  344|      0|            gdFree(im->pixels[i]);
  345|      0|        }
  346|      0|        gdFree(im->pixels);
  347|      0|    }
  348|    550|    if (im->tpixels) {
  ------------------
  |  Branch (348:9): [True: 550, False: 0]
  ------------------
  349|   108k|        for (i = 0; (i < im->sy); i++) {
  ------------------
  |  Branch (349:21): [True: 107k, False: 550]
  ------------------
  350|   107k|            gdFree(im->tpixels[i]);
  351|   107k|        }
  352|    550|        gdFree(im->tpixels);
  353|    550|    }
  354|    550|    if (im->polyInts) {
  ------------------
  |  Branch (354:9): [True: 0, False: 550]
  ------------------
  355|      0|        gdFree(im->polyInts);
  356|      0|    }
  357|    550|    if (im->style) {
  ------------------
  |  Branch (357:9): [True: 0, False: 550]
  ------------------
  358|      0|        gdFree(im->style);
  359|      0|    }
  360|    550|    gdFree(im);
  361|    550|}
gdImageAlphaBlending:
 3406|    339|{
 3407|    339|    im->alphaBlendingFlag = alphaBlendingArg;
 3408|    339|}
gdImageSaveAlpha:
 3411|    339|{
 3412|    339|    im->saveAlphaFlag = saveAlphaArg;
 3413|    339|}
gd.c:_gd_error_ex:
   88|    357|{
   89|    357|    if (gd_error_method) {
  ------------------
  |  Branch (89:9): [True: 357, False: 0]
  ------------------
   90|    357|        gd_error_method(priority, format, args);
   91|    357|    }
   92|    357|}
gd.c:gd_stderr_error:
   63|    357|{
   64|    357|    switch (priority) {
  ------------------
  |  Branch (64:13): [True: 357, False: 0]
  ------------------
   65|      0|    case GD_ERROR:
  ------------------
  |  |   37|      0|#define GD_ERROR LOG_ERR
  ------------------
  |  Branch (65:5): [True: 0, False: 357]
  ------------------
   66|      0|        fputs("GD Error: ", stderr);
   67|      0|        break;
   68|    357|    case GD_WARNING:
  ------------------
  |  |   38|    357|#define GD_WARNING LOG_WARNING
  ------------------
  |  Branch (68:5): [True: 357, False: 0]
  ------------------
   69|    357|        fputs("GD Warning: ", stderr);
   70|    357|        break;
   71|      0|    case GD_NOTICE:
  ------------------
  |  |   39|      0|#define GD_NOTICE LOG_NOTICE
  ------------------
  |  Branch (71:5): [True: 0, False: 357]
  ------------------
   72|      0|        fputs("GD Notice: ", stderr);
   73|      0|        break;
   74|      0|    case GD_INFO:
  ------------------
  |  |   40|      0|#define GD_INFO LOG_INFO
  ------------------
  |  Branch (74:5): [True: 0, False: 357]
  ------------------
   75|      0|        fputs("GD Info: ", stderr);
   76|      0|        break;
   77|      0|    case GD_DEBUG:
  ------------------
  |  |   41|      0|#define GD_DEBUG LOG_DEBUG
  ------------------
  |  Branch (77:5): [True: 0, False: 357]
  ------------------
   78|      0|        fputs("GD Debug: ", stderr);
   79|      0|        break;
   80|    357|    }
   81|    357|    vfprintf(stderr, format, args);
   82|       |    fflush(stderr);
   83|    357|}

gdGetC:
   52|  16.8M|int gdGetC(gdIOCtx *ctx) { return ((ctx->getC)(ctx)); }
gdGetBuf:
  189|   424k|int gdGetBuf(void *buf, int size, gdIOCtx *ctx) { return (ctx->getBuf)(ctx, buf, size); }
gdSeek:
  192|    190|{
  193|    190|    IO_DBG(printf("Seeking...\n"));
  194|    190|    return ((ctx->seek)(ctx, pos));
  195|      0|    IO_DBG(printf("Done.\n"));
  196|      0|}
gdTell:
  199|    432|{
  200|    432|    IO_DBG(printf("Telling...\n"));
  201|    432|    return ((ctx->tell)(ctx));
  202|      0|    IO_DBG(printf("told.\n"));
  203|      0|}

gdNewDynamicCtxEx:
   81|    966|{
   82|    966|    dpIOCtx *ctx;
   83|    966|    dynamicPtr *dp;
   84|       |
   85|    966|    ctx = (dpIOCtx *)gdMalloc(sizeof(dpIOCtx));
   86|    966|    if (ctx == NULL) {
  ------------------
  |  Branch (86:9): [True: 0, False: 966]
  ------------------
   87|      0|        return NULL;
   88|      0|    }
   89|       |
   90|    966|    dp = newDynamic(initialSize, data, freeOKFlag);
   91|    966|    if (!dp) {
  ------------------
  |  Branch (91:9): [True: 0, False: 966]
  ------------------
   92|      0|        gdFree(ctx);
   93|      0|        return NULL;
   94|    966|    };
   95|       |
   96|    966|    ctx->dp = dp;
   97|       |
   98|    966|    ctx->ctx.getC = dynamicGetchar;
   99|    966|    ctx->ctx.putC = dynamicPutchar;
  100|       |
  101|    966|    ctx->ctx.getBuf = dynamicGetbuf;
  102|    966|    ctx->ctx.putBuf = dynamicPutbuf;
  103|       |
  104|    966|    ctx->ctx.seek = dynamicSeek;
  105|    966|    ctx->ctx.tell = dynamicTell;
  106|       |
  107|    966|    ctx->ctx.gd_free = gdFreeDynamicCtx;
  108|       |
  109|    966|    return (gdIOCtx *)ctx;
  110|    966|}
gd_io_dp.c:gdFreeDynamicCtx:
  146|    966|{
  147|    966|    dynamicPtr *dp;
  148|    966|    dpIOCtx *dctx;
  149|       |
  150|    966|    dctx = (dpIOCtx *)ctx;
  151|    966|    dp = dctx->dp;
  152|       |
  153|    966|    gdFree(ctx);
  154|       |
  155|       |    /* clean up the data block and return it */
  156|       |    /* 2.0.21: never free memory we don't own */
  157|    966|    if ((dp->data != NULL) && (dp->freeOK)) {
  ------------------
  |  Branch (157:9): [True: 966, False: 0]
  |  Branch (157:31): [True: 0, False: 966]
  ------------------
  158|      0|        gdFree(dp->data);
  159|      0|        dp->data = NULL;
  160|      0|    }
  161|       |
  162|    966|    dp->realSize = 0;
  163|    966|    dp->logicalSize = 0;
  164|       |
  165|    966|    gdFree(dp);
  166|    966|}
gd_io_dp.c:dynamicTell:
  169|    432|{
  170|    432|    dpIOCtx *dctx;
  171|       |
  172|    432|    dctx = (dpIOCtx *)ctx;
  173|    432|    return (dctx->dp->pos);
  174|    432|}
gd_io_dp.c:dynamicSeek:
  177|    190|{
  178|    190|    int bytesNeeded;
  179|    190|    dynamicPtr *dp;
  180|    190|    dpIOCtx *dctx;
  181|       |
  182|    190|    if (pos < 0) {
  ------------------
  |  Branch (182:9): [True: 0, False: 190]
  ------------------
  183|      0|        return FALSE;
  ------------------
  |  |   29|      0|#define FALSE 0
  ------------------
  184|      0|    }
  185|    190|    dctx = (dpIOCtx *)ctx;
  186|    190|    dp = dctx->dp;
  187|       |
  188|    190|    if (!dp->dataGood) {
  ------------------
  |  Branch (188:9): [True: 0, False: 190]
  ------------------
  189|      0|        return FALSE;
  ------------------
  |  |   29|      0|#define FALSE 0
  ------------------
  190|      0|    }
  191|       |
  192|    190|    bytesNeeded = pos;
  193|    190|    if (bytesNeeded > dp->realSize) {
  ------------------
  |  Branch (193:9): [True: 0, False: 190]
  ------------------
  194|       |        /* 2.0.21 */
  195|      0|        if (!dp->freeOK) {
  ------------------
  |  Branch (195:13): [True: 0, False: 0]
  ------------------
  196|      0|            return FALSE;
  ------------------
  |  |   29|      0|#define FALSE 0
  ------------------
  197|      0|        }
  198|       |
  199|      0|        if (overflow2(dp->realSize, 2)) {
  ------------------
  |  Branch (199:13): [True: 0, False: 0]
  ------------------
  200|      0|            return FALSE;
  ------------------
  |  |   29|      0|#define FALSE 0
  ------------------
  201|      0|        }
  202|       |
  203|      0|        if (!gdReallocDynamic(dp, dp->realSize * 2)) {
  ------------------
  |  Branch (203:13): [True: 0, False: 0]
  ------------------
  204|      0|            dp->dataGood = FALSE;
  ------------------
  |  |   29|      0|#define FALSE 0
  ------------------
  205|      0|            return FALSE;
  ------------------
  |  |   29|      0|#define FALSE 0
  ------------------
  206|      0|        }
  207|      0|    }
  208|       |
  209|       |    /* if we get here, we can be sure that we have enough bytes
  210|       |     * to copy safely */
  211|       |
  212|       |    /* Extend the logical size if we seek beyond EOF. */
  213|    190|    if (pos > dp->logicalSize) {
  ------------------
  |  Branch (213:9): [True: 0, False: 190]
  ------------------
  214|      0|        dp->logicalSize = pos;
  215|      0|    };
  216|       |
  217|    190|    dp->pos = pos;
  218|       |
  219|    190|    return TRUE;
  ------------------
  |  |   28|    190|#define TRUE 1
  ------------------
  220|    190|}
gd_io_dp.c:newDynamic:
  224|    966|{
  225|    966|    dynamicPtr *dp;
  226|       |
  227|    966|    dp = (dynamicPtr *)gdMalloc(sizeof(dynamicPtr));
  228|    966|    if (dp == NULL) {
  ------------------
  |  Branch (228:9): [True: 0, False: 966]
  ------------------
  229|      0|        return NULL;
  230|      0|    }
  231|       |
  232|    966|    if (!allocDynamic(dp, initialSize, data)) {
  ------------------
  |  Branch (232:9): [True: 0, False: 966]
  ------------------
  233|      0|        gdFree(dp);
  234|      0|        return NULL;
  235|      0|    }
  236|       |
  237|    966|    dp->pos = 0;
  238|    966|    dp->freeOK = freeOKFlag;
  239|       |
  240|    966|    return dp;
  241|    966|}
gd_io_dp.c:allocDynamic:
  325|    966|{
  326|    966|    if (data == NULL) {
  ------------------
  |  Branch (326:9): [True: 0, False: 966]
  ------------------
  327|      0|        dp->logicalSize = 0;
  328|      0|        dp->dataGood = FALSE;
  ------------------
  |  |   29|      0|#define FALSE 0
  ------------------
  329|      0|        dp->data = gdMalloc(initialSize);
  330|    966|    } else {
  331|    966|        dp->logicalSize = initialSize;
  332|    966|        dp->dataGood = TRUE;
  ------------------
  |  |   28|    966|#define TRUE 1
  ------------------
  333|    966|        dp->data = data;
  334|    966|    }
  335|       |
  336|    966|    if (dp->data != NULL) {
  ------------------
  |  Branch (336:9): [True: 966, False: 0]
  ------------------
  337|    966|        dp->realSize = initialSize;
  338|    966|        dp->dataGood = TRUE;
  ------------------
  |  |   28|    966|#define TRUE 1
  ------------------
  339|    966|        dp->pos = 0;
  340|    966|        return TRUE;
  ------------------
  |  |   28|    966|#define TRUE 1
  ------------------
  341|    966|    } else {
  342|      0|        dp->realSize = 0;
  343|      0|        return FALSE;
  ------------------
  |  |   29|      0|#define FALSE 0
  ------------------
  344|      0|    }
  345|    966|}
gd_io_dp.c:dynamicGetbuf:
  270|  17.2M|{
  271|  17.2M|    int rlen, remain;
  272|  17.2M|    dpIOCtxPtr dctx;
  273|  17.2M|    dynamicPtr *dp;
  274|       |
  275|  17.2M|    dctx = (dpIOCtxPtr)ctx;
  276|  17.2M|    dp = dctx->dp;
  277|       |
  278|  17.2M|    if (dp->pos < 0 || dp->pos >= dp->realSize) {
  ------------------
  |  Branch (278:9): [True: 0, False: 17.2M]
  |  Branch (278:24): [True: 636, False: 17.2M]
  ------------------
  279|    636|        return 0;
  280|    636|    }
  281|       |
  282|  17.2M|    remain = dp->logicalSize - dp->pos;
  283|  17.2M|    if (remain >= len) {
  ------------------
  |  Branch (283:9): [True: 17.2M, False: 36]
  ------------------
  284|  17.2M|        rlen = len;
  285|  17.2M|    } else {
  286|     36|        if (remain <= 0) {
  ------------------
  |  Branch (286:13): [True: 0, False: 36]
  ------------------
  287|      0|            return 0;
  288|      0|        }
  289|       |
  290|     36|        rlen = remain;
  291|     36|    }
  292|       |
  293|  17.2M|    if (dp->pos + rlen > dp->realSize) {
  ------------------
  |  Branch (293:9): [True: 0, False: 17.2M]
  ------------------
  294|      0|        rlen = dp->realSize - dp->pos;
  295|      0|    }
  296|       |
  297|  17.2M|    if (rlen < 0) {
  ------------------
  |  Branch (297:9): [True: 0, False: 17.2M]
  ------------------
  298|      0|        return 0;
  299|      0|    }
  300|       |
  301|  17.2M|    memcpy(buf, (void *)((char *)dp->data + dp->pos), rlen);
  302|  17.2M|    dp->pos += rlen;
  303|       |
  304|  17.2M|    return rlen;
  305|  17.2M|}
gd_io_dp.c:dynamicGetchar:
  308|  16.8M|{
  309|  16.8M|    unsigned char b;
  310|  16.8M|    int rv;
  311|       |
  312|  16.8M|    rv = dynamicGetbuf(ctx, &b, 1);
  313|       |
  314|  16.8M|    if (rv != 1) {
  ------------------
  |  Branch (314:9): [True: 475, False: 16.8M]
  ------------------
  315|    475|        return EOF;
  316|  16.8M|    } else {
  317|  16.8M|        return b; /* (b & 0xff); */
  318|  16.8M|    }
  319|  16.8M|}

overflow2:
   23|  3.46k|{
   24|  3.46k|    if (a <= 0 || b <= 0) {
  ------------------
  |  Branch (24:9): [True: 0, False: 3.46k]
  |  Branch (24:19): [True: 0, False: 3.46k]
  ------------------
   25|      0|        gd_error_ex(GD_WARNING, "one parameter to a memory allocation multiplication is "
  ------------------
  |  |   38|      0|#define GD_WARNING LOG_WARNING
  ------------------
   26|      0|                                "negative or zero, failing operation gracefully\n");
   27|      0|        return 1;
   28|      0|    }
   29|  3.46k|    if (a > 100000 / b) {
  ------------------
  |  Branch (29:9): [True: 32, False: 3.42k]
  ------------------
   30|     32|        gd_error_ex(GD_WARNING, "product of memory allocation multiplication would exceed "
  ------------------
  |  |   38|     32|#define GD_WARNING LOG_WARNING
  ------------------
   31|     32|                                "100000, failing operation gracefully\n");
   32|     32|        return 1;
   33|     32|    }
   34|  3.42k|    return 0;
   35|  3.46k|}

gdImageCreateFromTgaPtr:
   57|    966|{
   58|    966|    gdImagePtr im;
   59|    966|    gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
   60|    966|    if (in == NULL)
  ------------------
  |  Branch (60:9): [True: 0, False: 966]
  ------------------
   61|      0|        return NULL;
   62|    966|    im = gdImageCreateFromTgaCtx(in);
   63|    966|    in->gd_free(in);
   64|    966|    return im;
   65|    966|}
gdImageCreateFromTgaCtx:
   76|    966|{
   77|    966|    int bitmap_caret = 0;
   78|    966|    oTga *tga = NULL;
   79|    966|    volatile gdImagePtr image = NULL;
   80|    966|    int x = 0;
   81|    966|    int y = 0;
   82|       |
   83|    966|    if (ctx == NULL) {
  ------------------
  |  Branch (83:9): [True: 0, False: 966]
  ------------------
   84|      0|        return NULL;
   85|      0|    }
   86|       |
   87|    966|    tga = (oTga *)gdMalloc(sizeof(oTga));
   88|    966|    if (!tga) {
  ------------------
  |  Branch (88:9): [True: 0, False: 966]
  ------------------
   89|      0|        return NULL;
   90|      0|    }
   91|       |
   92|    966|    tga->bitmap = NULL;
   93|    966|    tga->colormap = NULL;
   94|    966|    tga->ident = NULL;
   95|    966|    tga->has_alpha = 0;
   96|       |
   97|    966|    if (read_header_tga(ctx, tga) < 0) {
  ------------------
  |  Branch (97:9): [True: 115, False: 851]
  ------------------
   98|    115|        free_tga(tga);
   99|    115|        return NULL;
  100|    115|    }
  101|       |
  102|    851|    if (read_image_tga(ctx, tga) < 0) {
  ------------------
  |  Branch (102:9): [True: 297, False: 554]
  ------------------
  103|    297|        free_tga(tga);
  104|    297|        return NULL;
  105|    297|    }
  106|       |
  107|    554|    image = gdImageCreateTrueColor((int)tga->width, (int)tga->height);
  108|       |
  109|    554|    if (image == 0) {
  ------------------
  |  Branch (109:9): [True: 4, False: 550]
  ------------------
  110|      4|        free_tga(tga);
  111|      4|        return NULL;
  112|      4|    }
  113|       |
  114|       |    /*!	\brief Populate GD image object
  115|       |     *  Copy the pixel data from our tga bitmap buffer into the GD image
  116|       |     *  Disable blending and save the alpha channel per default
  117|       |     */
  118|    550|    if (tga->has_alpha) {
  ------------------
  |  Branch (118:9): [True: 339, False: 211]
  ------------------
  119|    339|        gdImageAlphaBlending(image, 0);
  120|    339|        gdImageSaveAlpha(image, 1);
  121|    339|    }
  122|       |
  123|   108k|    for (y = 0; y < tga->height; y++) {
  ------------------
  |  Branch (123:17): [True: 107k, False: 550]
  ------------------
  124|   107k|        register int *tpix = image->tpixels[y];
  125|   686k|        for (x = 0; x < tga->width; x++, tpix++) {
  ------------------
  |  Branch (125:21): [True: 578k, False: 107k]
  ------------------
  126|   578k|            *tpix = tga->bitmap[bitmap_caret++];
  127|   578k|        }
  128|   107k|    }
  129|       |
  130|    550|    if (tga->flipv && tga->fliph) {
  ------------------
  |  Branch (130:9): [True: 326, False: 224]
  |  Branch (130:23): [True: 250, False: 76]
  ------------------
  131|    250|        gdImageFlipBoth(image);
  132|    300|    } else if (tga->flipv) {
  ------------------
  |  Branch (132:16): [True: 76, False: 224]
  ------------------
  133|     76|        gdImageFlipVertical(image);
  134|    224|    } else if (tga->fliph) {
  ------------------
  |  Branch (134:16): [True: 174, False: 50]
  ------------------
  135|    174|        gdImageFlipHorizontal(image);
  136|    174|    }
  137|       |
  138|    550|    free_tga(tga);
  139|       |
  140|    550|    return image;
  141|    554|}
read_header_tga:
  149|    966|{
  150|       |
  151|    966|    unsigned char header[18];
  152|       |
  153|    966|    if (gdGetBuf(header, sizeof(header), ctx) < 18) {
  ------------------
  |  Branch (153:9): [True: 11, False: 955]
  ------------------
  154|     11|        gd_error("fail to read header");
  155|     11|        return -1;
  156|     11|    }
  157|       |
  158|    955|    tga->identsize = header[0];
  159|    955|    tga->colormaptype = header[1];
  160|    955|    tga->imagetype = header[2];
  161|    955|    tga->colormapstart = header[3] + (header[4] << 8);
  162|    955|    tga->colormaplength = header[5] + (header[6] << 8);
  163|    955|    tga->colormapbits = header[7];
  164|    955|    tga->xstart = header[8] + (header[9] << 8);
  165|    955|    tga->ystart = header[10] + (header[11] << 8);
  166|    955|    tga->width = header[12] + (header[13] << 8);
  167|    955|    tga->height = header[14] + (header[15] << 8);
  168|    955|    tga->bits = header[16];
  169|    955|    tga->alphabits = header[17] & 0x0f;
  170|    955|    tga->fliph = (header[17] & 0x10) ? 1 : 0;
  ------------------
  |  Branch (170:18): [True: 619, False: 336]
  ------------------
  171|    955|    tga->flipv = (header[17] & 0x20) ? 0 : 1;
  ------------------
  |  Branch (171:18): [True: 405, False: 550]
  ------------------
  172|    955|    tga->has_alpha = 0;
  173|       |
  174|       |#ifdef DEBUG
  175|       |    printf("format bps: %i\n", tga->bits);
  176|       |    printf("flip h/v: %i / %i\n", tga->fliph, tga->flipv);
  177|       |    printf("alpha: %i\n", tga->alphabits);
  178|       |    printf("wxh: %i %i\n", tga->width, tga->height);
  179|       |#endif
  180|       |
  181|    955|    if (tga->width <= 0 || tga->height <= 0) {
  ------------------
  |  Branch (181:9): [True: 4, False: 951]
  |  Branch (181:28): [True: 1, False: 950]
  ------------------
  182|      5|        gd_error("gd-tga: invalid image dimensions\n");
  183|      5|        return -1;
  184|      5|    }
  185|       |
  186|    950|    if (tga->colormaptype > 1) {
  ------------------
  |  Branch (186:9): [True: 12, False: 938]
  ------------------
  187|     12|        gd_error_ex(GD_WARNING, "gd-tga: unsupported color map type %u\n", tga->colormaptype);
  ------------------
  |  |   38|     12|#define GD_WARNING LOG_WARNING
  ------------------
  188|     12|        return -1;
  189|     12|    }
  190|       |
  191|    938|    if (tga->colormaptype == 1 &&
  ------------------
  |  Branch (191:9): [True: 161, False: 777]
  ------------------
  192|    161|        !(tga->colormapbits == 15 || tga->colormapbits == 16 || tga->colormapbits == 24 ||
  ------------------
  |  Branch (192:11): [True: 67, False: 94]
  |  Branch (192:38): [True: 47, False: 47]
  |  Branch (192:65): [True: 7, False: 40]
  ------------------
  193|     40|          tga->colormapbits == 32)) {
  ------------------
  |  Branch (193:11): [True: 16, False: 24]
  ------------------
  194|     24|        gd_error_ex(GD_WARNING, "gd-tga: unsupported color map entry depth %u\n", tga->colormapbits);
  ------------------
  |  |   38|     24|#define GD_WARNING LOG_WARNING
  ------------------
  195|     24|        return -1;
  196|     24|    }
  197|       |
  198|    914|    switch (tga->imagetype) {
  199|     94|    case TGA_TYPE_INDEXED:
  ------------------
  |  |   35|     94|#define TGA_TYPE_INDEXED 1
  ------------------
  |  Branch (199:5): [True: 94, False: 820]
  ------------------
  200|    123|    case TGA_TYPE_INDEXED_RLE:
  ------------------
  |  |   38|    123|#define TGA_TYPE_INDEXED_RLE 9
  ------------------
  |  Branch (200:5): [True: 29, False: 885]
  ------------------
  201|    123|        if (tga->colormaptype != 1 || tga->bits != TGA_BPP_8 ||
  ------------------
  |  |   44|    244|#define TGA_BPP_8 8
  ------------------
  |  Branch (201:13): [True: 2, False: 121]
  |  Branch (201:39): [True: 8, False: 113]
  ------------------
  202|    113|            !(tga->colormapbits == 15 || tga->colormapbits == 16 || tga->colormapbits == 24 ||
  ------------------
  |  Branch (202:15): [True: 58, False: 55]
  |  Branch (202:42): [True: 44, False: 11]
  |  Branch (202:69): [True: 3, False: 8]
  ------------------
  203|     10|              tga->colormapbits == 32)) {
  ------------------
  |  Branch (203:15): [True: 8, False: 0]
  ------------------
  204|     10|            gd_error_ex(GD_WARNING, "gd-tga: unsupported color mapped image\n");
  ------------------
  |  |   38|     10|#define GD_WARNING LOG_WARNING
  ------------------
  205|     10|            return -1;
  206|     10|        }
  207|    113|        break;
  208|       |
  209|    442|    case TGA_TYPE_RGB:
  ------------------
  |  |   36|    442|#define TGA_TYPE_RGB 2
  ------------------
  |  Branch (209:5): [True: 442, False: 472]
  ------------------
  210|    575|    case TGA_TYPE_RGB_RLE:
  ------------------
  |  |   39|    575|#define TGA_TYPE_RGB_RLE 10
  ------------------
  |  Branch (210:5): [True: 133, False: 781]
  ------------------
  211|    575|        if (!(tga->bits == TGA_BPP_16 || tga->bits == TGA_BPP_24 ||
  ------------------
  |  |   45|  1.15k|#define TGA_BPP_16 16
  ------------------
                      if (!(tga->bits == TGA_BPP_16 || tga->bits == TGA_BPP_24 ||
  ------------------
  |  |   46|    650|#define TGA_BPP_24 24
  ------------------
  |  Branch (211:15): [True: 500, False: 75]
  |  Branch (211:42): [True: 22, False: 53]
  ------------------
  212|     53|              (tga->bits == TGA_BPP_32 && tga->alphabits == 8))) {
  ------------------
  |  |   47|    106|#define TGA_BPP_32 32
  ------------------
  |  Branch (212:16): [True: 35, False: 18]
  |  Branch (212:43): [True: 29, False: 6]
  ------------------
  213|     24|            gd_error_ex(GD_WARNING, "gd-tga: unsupported truecolor depth %u\n", tga->bits);
  ------------------
  |  |   38|     24|#define GD_WARNING LOG_WARNING
  ------------------
  214|     24|            return -1;
  215|     24|        }
  216|    551|        break;
  217|       |
  218|    551|    case TGA_TYPE_GREYSCALE:
  ------------------
  |  |   37|     40|#define TGA_TYPE_GREYSCALE 3
  ------------------
  |  Branch (218:5): [True: 40, False: 874]
  ------------------
  219|    215|    case TGA_TYPE_GREYSCALE_RLE:
  ------------------
  |  |   40|    215|#define TGA_TYPE_GREYSCALE_RLE 11
  ------------------
  |  Branch (219:5): [True: 175, False: 739]
  ------------------
  220|    215|        if (tga->bits != TGA_BPP_8) {
  ------------------
  |  |   44|    215|#define TGA_BPP_8 8
  ------------------
  |  Branch (220:13): [True: 9, False: 206]
  ------------------
  221|      9|            gd_error_ex(GD_WARNING, "gd-tga: unsupported grayscale depth %u\n", tga->bits);
  ------------------
  |  |   38|      9|#define GD_WARNING LOG_WARNING
  ------------------
  222|      9|            return -1;
  223|      9|        }
  224|    206|        break;
  225|       |
  226|    206|    default:
  ------------------
  |  Branch (226:5): [True: 1, False: 913]
  ------------------
  227|      1|        gd_error_ex(GD_WARNING, "gd-tga: unsupported image type %u\n", tga->imagetype);
  ------------------
  |  |   38|      1|#define GD_WARNING LOG_WARNING
  ------------------
  228|      1|        return -1;
  229|    914|    }
  230|       |
  231|    870|    tga->ident = NULL;
  232|       |
  233|    870|    if (tga->identsize > 0) {
  ------------------
  |  Branch (233:9): [True: 57, False: 813]
  ------------------
  234|     57|        tga->ident = (char *)gdMalloc(tga->identsize * sizeof(char));
  235|     57|        if (tga->ident == NULL) {
  ------------------
  |  Branch (235:13): [True: 0, False: 57]
  ------------------
  236|      0|            return -1;
  237|      0|        }
  238|       |
  239|     57|        if (gdGetBuf(tga->ident, tga->identsize, ctx) != tga->identsize) {
  ------------------
  |  Branch (239:13): [True: 19, False: 38]
  ------------------
  240|     19|            gd_error("fail to read header ident");
  241|     19|            return -1;
  242|     19|        }
  243|     57|    }
  244|       |
  245|    851|    return 1;
  246|    870|}
read_image_tga:
  254|    851|{
  255|    851|    int pixel_count;
  256|    851|    int bitmap_caret = 0;
  257|       |
  258|    851|    if (overflow2(tga->width, tga->height)) {
  ------------------
  |  Branch (258:9): [True: 21, False: 830]
  ------------------
  259|     21|        return -1;
  260|     21|    }
  261|       |
  262|    830|    pixel_count = tga->width * tga->height;
  263|    830|    if (overflow2(pixel_count, sizeof(int))) {
  ------------------
  |  Branch (263:9): [True: 3, False: 827]
  ------------------
  264|      3|        return -1;
  265|      3|    }
  266|       |
  267|    827|    if (tga_read_color_map(ctx, tga) < 0) {
  ------------------
  |  Branch (267:9): [True: 43, False: 784]
  ------------------
  268|     43|        return -1;
  269|     43|    }
  270|       |
  271|       |    /*!	\brief Allocate memory for image block
  272|       |     *  Allocate a chunk of memory for the image block to be passed into.
  273|       |     */
  274|    784|    tga->bitmap = (int *)gdMalloc(pixel_count * sizeof(int));
  275|    784|    if (tga->bitmap == NULL)
  ------------------
  |  Branch (275:9): [True: 0, False: 784]
  ------------------
  276|      0|        return -1;
  277|       |
  278|    784|    if (tga_is_rle(tga->imagetype)) {
  ------------------
  |  Branch (278:9): [True: 292, False: 492]
  ------------------
  279|   222k|        while (bitmap_caret < pixel_count) {
  ------------------
  |  Branch (279:16): [True: 222k, False: 175]
  ------------------
  280|   222k|            int packet = gdGetC(ctx);
  281|   222k|            int encoded_pixels;
  282|   222k|            int pixel;
  283|   222k|            int i;
  284|       |
  285|   222k|            if (packet == EOF) {
  ------------------
  |  Branch (285:17): [True: 43, False: 222k]
  ------------------
  286|     43|                gd_error("gd-tga: premature end of image data\n");
  287|     43|                return -1;
  288|     43|            }
  289|       |
  290|   222k|            encoded_pixels = (packet & ~TGA_RLE_FLAG) + 1;
  ------------------
  |  |   49|   222k|#define TGA_RLE_FLAG 128
  ------------------
  291|   222k|            if (encoded_pixels > pixel_count - bitmap_caret) {
  ------------------
  |  Branch (291:17): [True: 13, False: 222k]
  ------------------
  292|     13|                return -1;
  293|     13|            }
  294|       |
  295|   222k|            if ((packet & TGA_RLE_FLAG) == TGA_RLE_FLAG) {
  ------------------
  |  |   49|   222k|#define TGA_RLE_FLAG 128
  ------------------
                          if ((packet & TGA_RLE_FLAG) == TGA_RLE_FLAG) {
  ------------------
  |  |   49|   222k|#define TGA_RLE_FLAG 128
  ------------------
  |  Branch (295:17): [True: 4.74k, False: 217k]
  ------------------
  296|  4.74k|                if (tga_read_pixel(ctx, tga, &pixel) < 0) {
  ------------------
  |  Branch (296:21): [True: 5, False: 4.73k]
  ------------------
  297|      5|                    return -1;
  298|      5|                }
  299|   527k|                for (i = 0; i < encoded_pixels; i++) {
  ------------------
  |  Branch (299:29): [True: 522k, False: 4.73k]
  ------------------
  300|   522k|                    tga->bitmap[bitmap_caret++] = pixel;
  301|   522k|                }
  302|   217k|            } else {
  303|   514k|                for (i = 0; i < encoded_pixels; i++) {
  ------------------
  |  Branch (303:29): [True: 296k, False: 217k]
  ------------------
  304|   296k|                    if (tga_read_pixel(ctx, tga, &pixel) < 0) {
  ------------------
  |  Branch (304:25): [True: 56, False: 296k]
  ------------------
  305|     56|                        return -1;
  306|     56|                    }
  307|   296k|                    tga->bitmap[bitmap_caret++] = pixel;
  308|   296k|                }
  309|   217k|            }
  310|   222k|        }
  311|    492|    } else {
  312|  80.5k|        while (bitmap_caret < pixel_count) {
  ------------------
  |  Branch (312:16): [True: 80.2k, False: 379]
  ------------------
  313|  80.2k|            if (tga_read_pixel(ctx, tga, &tga->bitmap[bitmap_caret]) < 0) {
  ------------------
  |  Branch (313:17): [True: 113, False: 80.0k]
  ------------------
  314|    113|                return -1;
  315|    113|            }
  316|  80.0k|            bitmap_caret++;
  317|  80.0k|        }
  318|    492|    }
  319|       |
  320|    554|    tga_apply_attribute_type(ctx, tga, pixel_count);
  321|       |
  322|    554|    return 1;
  323|    784|}
free_tga:
  554|    966|{
  555|    966|    if (tga) {
  ------------------
  |  Branch (555:9): [True: 966, False: 0]
  ------------------
  556|    966|        if (tga->ident)
  ------------------
  |  Branch (556:13): [True: 57, False: 909]
  ------------------
  557|     57|            gdFree(tga->ident);
  558|    966|        if (tga->bitmap)
  ------------------
  |  Branch (558:13): [True: 784, False: 182]
  ------------------
  559|    784|            gdFree(tga->bitmap);
  560|    966|        if (tga->colormap)
  ------------------
  |  Branch (560:13): [True: 118, False: 848]
  ------------------
  561|    118|            gdFree(tga->colormap);
  562|    966|        gdFree(tga);
  563|    966|    }
  564|    966|}
gd_tga.c:tga_is_rle:
  326|    784|{
  327|    784|    return imagetype == TGA_TYPE_INDEXED_RLE || imagetype == TGA_TYPE_RGB_RLE ||
  ------------------
  |  |   38|  1.56k|#define TGA_TYPE_INDEXED_RLE 9
  ------------------
                  return imagetype == TGA_TYPE_INDEXED_RLE || imagetype == TGA_TYPE_RGB_RLE ||
  ------------------
  |  |   39|  1.55k|#define TGA_TYPE_RGB_RLE 10
  ------------------
  |  Branch (327:12): [True: 16, False: 768]
  |  Branch (327:49): [True: 120, False: 648]
  ------------------
  328|    648|           imagetype == TGA_TYPE_GREYSCALE_RLE;
  ------------------
  |  |   40|    648|#define TGA_TYPE_GREYSCALE_RLE 11
  ------------------
  |  Branch (328:12): [True: 156, False: 492]
  ------------------
  329|    784|}
gd_tga.c:tga_read_color_map:
  334|    827|{
  335|    827|    int i;
  336|    827|    int has_alpha = 0;
  337|    827|    int entry_size;
  338|       |
  339|    827|    if (tga->colormaptype == 0) {
  ------------------
  |  Branch (339:9): [True: 704, False: 123]
  ------------------
  340|    704|        return 1;
  341|    704|    }
  342|       |
  343|    123|    if (tga->colormaplength <= 0) {
  ------------------
  |  Branch (343:9): [True: 1, False: 122]
  ------------------
  344|      1|        return -1;
  345|      1|    }
  346|       |
  347|    122|    if (overflow2(tga->colormaplength, sizeof(int))) {
  ------------------
  |  Branch (347:9): [True: 4, False: 118]
  ------------------
  348|      4|        return -1;
  349|      4|    }
  350|       |
  351|    118|    entry_size = tga_pixel_size(tga->colormapbits);
  352|    118|    tga->colormap = (int *)gdMalloc(tga->colormaplength * sizeof(int));
  353|    118|    if (tga->colormap == NULL) {
  ------------------
  |  Branch (353:9): [True: 0, False: 118]
  ------------------
  354|      0|        return -1;
  355|      0|    }
  356|       |
  357|  41.7k|    for (i = 0; i < tga->colormaplength; i++) {
  ------------------
  |  Branch (357:17): [True: 41.6k, False: 80]
  ------------------
  358|  41.6k|        unsigned char buf[4] = {0, 0, 0, 0};
  359|       |
  360|  41.6k|        if (gdGetBuf(buf, entry_size, ctx) != entry_size) {
  ------------------
  |  Branch (360:13): [True: 38, False: 41.5k]
  ------------------
  361|     38|            gd_error("gd-tga: premature end of color map data\n");
  362|     38|            return -1;
  363|     38|        }
  364|       |
  365|  41.5k|        tga->colormap[i] =
  366|  41.5k|            tga_decode_color(buf, tga->colormapbits, tga->colormapbits == 32 ? 8 : 0, &has_alpha);
  ------------------
  |  Branch (366:54): [True: 471, False: 41.1k]
  ------------------
  367|  41.5k|    }
  368|       |
  369|     80|    if (has_alpha) {
  ------------------
  |  Branch (369:9): [True: 5, False: 75]
  ------------------
  370|      5|        tga->has_alpha = 1;
  371|      5|    }
  372|       |
  373|     80|    return 1;
  374|    118|}
gd_tga.c:tga_pixel_size:
  331|   382k|static int tga_pixel_size(uint8_t bits) { return (bits + 7) / 8; }
gd_tga.c:tga_decode_color:
  494|   397k|{
  495|   397k|    switch (bits) {
  ------------------
  |  Branch (495:13): [True: 397k, False: 0]
  ------------------
  496|  4.43k|    case 15:
  ------------------
  |  Branch (496:5): [True: 4.43k, False: 393k]
  ------------------
  497|   163k|    case 16:
  ------------------
  |  Branch (497:5): [True: 158k, False: 239k]
  ------------------
  498|   163k|        return tga_decode_16((unsigned int)buf[0] | ((unsigned int)buf[1] << 8), alpha_bits,
  499|   163k|                             has_alpha);
  500|       |
  501|  4.97k|    case 24:
  ------------------
  |  Branch (501:5): [True: 4.97k, False: 392k]
  ------------------
  502|  4.97k|        return gdTrueColor(buf[2], buf[1], buf[0]);
  ------------------
  |  | 6044|  4.97k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  503|       |
  504|   229k|    case 32:
  ------------------
  |  Branch (504:5): [True: 229k, False: 168k]
  ------------------
  505|   229k|        if (has_alpha) {
  ------------------
  |  Branch (505:13): [True: 229k, False: 0]
  ------------------
  506|   229k|            *has_alpha = 1;
  507|   229k|        }
  508|   229k|        return gdTrueColorAlpha(buf[2], buf[1], buf[0], tga_alpha_8_to_gd(buf[3]));
  ------------------
  |  | 6061|   229k|#define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
  ------------------
  509|   397k|    }
  510|       |
  511|      0|    return 0;
  512|   397k|}
gd_tga.c:tga_decode_16:
  515|   163k|{
  516|   163k|    int b = tga_scale_5_to_8(value & 0x1f);
  517|   163k|    int g = tga_scale_5_to_8((value >> 5) & 0x1f);
  518|   163k|    int r = tga_scale_5_to_8((value >> 10) & 0x1f);
  519|       |
  520|   163k|    if (alpha_bits > 0) {
  ------------------
  |  Branch (520:9): [True: 119k, False: 43.8k]
  ------------------
  521|   119k|        int a = (value & 0x8000) ? gdAlphaOpaque : gdAlphaTransparent;
  ------------------
  |  |  141|  25.3k|#define gdAlphaOpaque 0
  ------------------
                      int a = (value & 0x8000) ? gdAlphaOpaque : gdAlphaTransparent;
  ------------------
  |  |  142|  93.8k|#define gdAlphaTransparent 127
  ------------------
  |  Branch (521:17): [True: 25.3k, False: 93.8k]
  ------------------
  522|       |
  523|   119k|        if (has_alpha) {
  ------------------
  |  Branch (523:13): [True: 119k, False: 0]
  ------------------
  524|   119k|            *has_alpha = 1;
  525|   119k|        }
  526|   119k|        return gdTrueColorAlpha(r, g, b, a);
  ------------------
  |  | 6061|   119k|#define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
  ------------------
  527|   119k|    }
  528|       |
  529|  43.8k|    return gdTrueColor(r, g, b);
  ------------------
  |  | 6044|  43.8k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  530|   163k|}
gd_tga.c:tga_scale_5_to_8:
  532|   489k|static int tga_scale_5_to_8(int c) { return (c * 255) / 31; }
gd_tga.c:tga_alpha_8_to_gd:
  534|   229k|static int tga_alpha_8_to_gd(int a) { return gdAlphaMax - (a >> 1); }
  ------------------
  |  |  140|   229k|#define gdAlphaMax 127
  ------------------
gd_tga.c:tga_read_pixel:
  377|   381k|{
  378|   381k|    unsigned char buf[4] = {0, 0, 0, 0};
  379|   381k|    int size = tga_pixel_size(tga->bits);
  380|   381k|    int has_alpha = 0;
  381|       |
  382|   381k|    if (gdGetBuf(buf, size, ctx) != size) {
  ------------------
  |  Branch (382:9): [True: 129, False: 381k]
  ------------------
  383|    129|        gd_error("gd-tga: premature end of image data\n");
  384|    129|        return -1;
  385|    129|    }
  386|       |
  387|   381k|    switch (tga->imagetype) {
  388|  7.92k|    case TGA_TYPE_INDEXED:
  ------------------
  |  |   35|  7.92k|#define TGA_TYPE_INDEXED 1
  ------------------
  |  Branch (388:5): [True: 7.92k, False: 373k]
  ------------------
  389|  8.15k|    case TGA_TYPE_INDEXED_RLE: {
  ------------------
  |  |   38|  8.15k|#define TGA_TYPE_INDEXED_RLE 9
  ------------------
  |  Branch (389:5): [True: 226, False: 381k]
  ------------------
  390|  8.15k|        int index = buf[0] - tga->colormapstart;
  391|       |
  392|  8.15k|        if (index < 0 || index >= tga->colormaplength || tga->colormap == NULL) {
  ------------------
  |  Branch (392:13): [True: 27, False: 8.12k]
  |  Branch (392:26): [True: 18, False: 8.10k]
  |  Branch (392:58): [True: 0, False: 8.10k]
  ------------------
  393|     45|            return -1;
  394|     45|        }
  395|  8.10k|        *pixel = tga->colormap[index];
  396|  8.10k|        break;
  397|  8.15k|    }
  398|       |
  399|  61.0k|    case TGA_TYPE_RGB:
  ------------------
  |  |   36|  61.0k|#define TGA_TYPE_RGB 2
  ------------------
  |  Branch (399:5): [True: 61.0k, False: 320k]
  ------------------
  400|   356k|    case TGA_TYPE_RGB_RLE:
  ------------------
  |  |   39|   356k|#define TGA_TYPE_RGB_RLE 10
  ------------------
  |  Branch (400:5): [True: 295k, False: 86.6k]
  ------------------
  401|   356k|        *pixel = tga_decode_color(buf, tga->bits, tga->alphabits, &has_alpha);
  402|   356k|        if (has_alpha) {
  ------------------
  |  Branch (402:13): [True: 348k, False: 7.73k]
  ------------------
  403|   348k|            tga->has_alpha = 1;
  404|   348k|        }
  405|   356k|        break;
  406|       |
  407|  11.1k|    case TGA_TYPE_GREYSCALE:
  ------------------
  |  |   37|  11.1k|#define TGA_TYPE_GREYSCALE 3
  ------------------
  |  Branch (407:5): [True: 11.1k, False: 370k]
  ------------------
  408|  17.4k|    case TGA_TYPE_GREYSCALE_RLE:
  ------------------
  |  |   40|  17.4k|#define TGA_TYPE_GREYSCALE_RLE 11
  ------------------
  |  Branch (408:5): [True: 6.33k, False: 375k]
  ------------------
  409|  17.4k|        *pixel = gdTrueColor(buf[0], buf[0], buf[0]);
  ------------------
  |  | 6044|  17.4k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  410|  17.4k|        break;
  411|       |
  412|      0|    default:
  ------------------
  |  Branch (412:5): [True: 0, False: 381k]
  ------------------
  413|      0|        return -1;
  414|   381k|    }
  415|       |
  416|   381k|    return 1;
  417|   381k|}
gd_tga.c:tga_apply_attribute_type:
  420|    554|{
  421|    554|    static const unsigned char signature[18] = {'T', 'R', 'U', 'E', 'V', 'I', 'S', 'I', 'O',
  422|    554|                                                'N', '-', 'X', 'F', 'I', 'L', 'E', '.', '\0'};
  423|    554|    unsigned char footer_ring[26];
  424|    554|    unsigned char footer[26];
  425|    554|    size_t trailing_size = 0;
  426|    554|    size_t i;
  427|    554|    int c;
  428|    554|    long start;
  429|    554|    uint32_t extension_offset;
  430|    554|    uint32_t attr_type_offset;
  431|    554|    uint32_t end_offset;
  432|    554|    int attr_type;
  433|       |
  434|    554|    if (!tga->has_alpha) {
  ------------------
  |  Branch (434:9): [True: 122, False: 432]
  ------------------
  435|    122|        return;
  436|    122|    }
  437|       |
  438|    432|    if (ctx->tell == NULL || ctx->seek == NULL) {
  ------------------
  |  Branch (438:9): [True: 0, False: 432]
  |  Branch (438:30): [True: 0, False: 432]
  ------------------
  439|      0|        return;
  440|      0|    }
  441|       |
  442|    432|    start = gdTell(ctx);
  443|    432|    if (start < 0 || start > INT_MAX) {
  ------------------
  |  Branch (443:9): [True: 0, False: 432]
  |  Branch (443:22): [True: 0, False: 432]
  ------------------
  444|      0|        return;
  445|      0|    }
  446|       |
  447|  16.6M|    while ((c = gdGetC(ctx)) != EOF) {
  ------------------
  |  Branch (447:12): [True: 16.6M, False: 432]
  ------------------
  448|  16.6M|        footer_ring[trailing_size % sizeof(footer_ring)] = (unsigned char)c;
  449|  16.6M|        if (trailing_size == SIZE_MAX) {
  ------------------
  |  Branch (449:13): [True: 0, False: 16.6M]
  ------------------
  450|      0|            return;
  451|      0|        }
  452|  16.6M|        trailing_size++;
  453|  16.6M|    }
  454|       |
  455|    432|    if (trailing_size < sizeof(footer)) {
  ------------------
  |  Branch (455:9): [True: 57, False: 375]
  ------------------
  456|     57|        return;
  457|     57|    }
  458|       |
  459|  10.1k|    for (i = 0; i < sizeof(footer); i++) {
  ------------------
  |  Branch (459:17): [True: 9.75k, False: 375]
  ------------------
  460|  9.75k|        footer[i] = footer_ring[(trailing_size + i) % sizeof(footer_ring)];
  461|  9.75k|    }
  462|    375|    if (memcmp(footer + 8, signature, sizeof(signature)) != 0) {
  ------------------
  |  Branch (462:9): [True: 145, False: 230]
  ------------------
  463|    145|        return;
  464|    145|    }
  465|       |
  466|    230|    extension_offset = (uint32_t)footer[0] | ((uint32_t)footer[1] << 8) |
  467|    230|                       ((uint32_t)footer[2] << 16) | ((uint32_t)footer[3] << 24);
  468|    230|    if (extension_offset == 0 || extension_offset < (uint32_t)start ||
  ------------------
  |  Branch (468:9): [True: 1, False: 229]
  |  Branch (468:34): [True: 1, False: 228]
  ------------------
  469|    228|        extension_offset > (uint32_t)INT_MAX - 494) {
  ------------------
  |  Branch (469:9): [True: 39, False: 189]
  ------------------
  470|     41|        return;
  471|     41|    }
  472|       |
  473|    189|    attr_type_offset = extension_offset + 494;
  474|    189|    if (trailing_size > (size_t)INT_MAX - (size_t)start ||
  ------------------
  |  Branch (474:9): [True: 0, False: 189]
  ------------------
  475|    189|        attr_type_offset >= (uint32_t)start + (uint32_t)trailing_size - sizeof(footer)) {
  ------------------
  |  Branch (475:9): [True: 94, False: 95]
  ------------------
  476|     94|        return;
  477|     94|    }
  478|     95|    end_offset = (uint32_t)start + (uint32_t)trailing_size;
  479|       |
  480|     95|    if (!gdSeek(ctx, (int)attr_type_offset)) {
  ------------------
  |  Branch (480:9): [True: 0, False: 95]
  ------------------
  481|      0|        return;
  482|      0|    }
  483|     95|    attr_type = gdGetC(ctx);
  484|     95|    gdSeek(ctx, (int)end_offset);
  485|     95|    if (attr_type == EOF) {
  ------------------
  |  Branch (485:9): [True: 0, False: 95]
  ------------------
  486|      0|        return;
  487|      0|    }
  488|     95|    if (attr_type != 3 && attr_type != 4) {
  ------------------
  |  Branch (488:9): [True: 94, False: 1]
  |  Branch (488:27): [True: 92, False: 2]
  ------------------
  489|     92|        tga_strip_alpha(tga, pixel_count);
  490|     92|    }
  491|     95|}
gd_tga.c:tga_strip_alpha:
  537|     92|{
  538|     92|    int i;
  539|       |
  540|  61.8k|    for (i = 0; i < pixel_count; i++) {
  ------------------
  |  Branch (540:17): [True: 61.7k, False: 92]
  ------------------
  541|  61.7k|        int pixel = tga->bitmap[i];
  542|       |
  543|  61.7k|        tga->bitmap[i] = gdTrueColor(gdTrueColorGetRed(pixel), gdTrueColorGetGreen(pixel),
  ------------------
  |  | 6044|  61.7k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  544|  61.7k|                                     gdTrueColorGetBlue(pixel));
  545|  61.7k|    }
  546|     92|    tga->has_alpha = 0;
  547|     92|}

gdImageFlipVertical:
   12|    326|{
   13|    326|    register int x, y;
   14|       |
   15|    326|    if (im->trueColor) {
  ------------------
  |  Branch (15:9): [True: 326, False: 0]
  ------------------
   16|  34.8k|        for (y = 0; y < im->sy / 2; y++) {
  ------------------
  |  Branch (16:21): [True: 34.5k, False: 326]
  ------------------
   17|  34.5k|            int *row_dst = im->tpixels[y];
   18|  34.5k|            int *row_src = im->tpixels[im->sy - 1 - y];
   19|   105k|            for (x = 0; x < im->sx; x++) {
  ------------------
  |  Branch (19:25): [True: 71.4k, False: 34.5k]
  ------------------
   20|  71.4k|                register int p;
   21|  71.4k|                p = row_dst[x];
   22|  71.4k|                row_dst[x] = im->tpixels[im->sy - 1 - y][x];
   23|  71.4k|                row_src[x] = p;
   24|  71.4k|            }
   25|  34.5k|        }
   26|    326|    } else {
   27|      0|        unsigned char p;
   28|      0|        for (y = 0; y < im->sy / 2; y++) {
  ------------------
  |  Branch (28:21): [True: 0, False: 0]
  ------------------
   29|      0|            for (x = 0; x < im->sx; x++) {
  ------------------
  |  Branch (29:25): [True: 0, False: 0]
  ------------------
   30|      0|                p = im->pixels[y][x];
   31|      0|                im->pixels[y][x] = im->pixels[im->sy - 1 - y][x];
   32|      0|                im->pixels[im->sy - 1 - y][x] = p;
   33|      0|            }
   34|      0|        }
   35|      0|    }
   36|    326|    return;
   37|    326|}
gdImageFlipHorizontal:
   40|    424|{
   41|       |
   42|    424|    int x, y;
   43|       |
   44|    424|    if (im->trueColor) {
  ------------------
  |  Branch (44:9): [True: 424, False: 0]
  ------------------
   45|    424|        int *px1, *px2, tmp;
   46|       |
   47|  93.1k|        for (y = 0; y < im->sy; y++) {
  ------------------
  |  Branch (47:21): [True: 92.7k, False: 424]
  ------------------
   48|  92.7k|            px1 = im->tpixels[y];
   49|  92.7k|            px2 = im->tpixels[y] + im->sx - 1;
   50|   336k|            for (x = 0; x < (im->sx >> 1); x++) {
  ------------------
  |  Branch (50:25): [True: 243k, False: 92.7k]
  ------------------
   51|   243k|                tmp = *px1;
   52|   243k|                *px1 = *px2;
   53|   243k|                *px2 = tmp;
   54|   243k|                px1++;
   55|   243k|                px2--;
   56|   243k|            }
   57|  92.7k|        }
   58|    424|    } else {
   59|      0|        unsigned char *px1, *px2, tmp;
   60|       |
   61|      0|        for (y = 0; y < im->sy; y++) {
  ------------------
  |  Branch (61:21): [True: 0, False: 0]
  ------------------
   62|      0|            px1 = im->pixels[y];
   63|      0|            px2 = im->pixels[y] + im->sx - 1;
   64|      0|            for (x = 0; x < (im->sx >> 1); x++) {
  ------------------
  |  Branch (64:25): [True: 0, False: 0]
  ------------------
   65|      0|                tmp = *px1;
   66|      0|                *px1 = *px2;
   67|      0|                *px2 = tmp;
   68|      0|                px1++;
   69|      0|                px2--;
   70|      0|            }
   71|      0|        }
   72|      0|    }
   73|    424|}
gdImageFlipBoth:
   76|    250|{
   77|    250|    gdImageFlipVertical(im);
   78|    250|    gdImageFlipHorizontal(im);
   79|    250|}

gdCalloc:
   66|   107k|void *gdCalloc(size_t nmemb, size_t size) { return calloc(nmemb, size); }
gdMalloc:
   68|  4.95k|void *gdMalloc(size_t size) { return malloc(size); }
gdFree:
  100|   112k|BGD_DECLARE(void) gdFree(void *ptr) { free(ptr); }

LLVMFuzzerTestOneInput:
   23|    966|extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
   24|    966|    gdImagePtr im = CREATE_IMAGE(FUZZ_GD_FORMAT)(Size, (void*) Data);
  ------------------
  |  |   21|    966|#define CREATE_IMAGE(FORMAT) PASTE(FORMAT)
  |  |  ------------------
  |  |  |  |   20|    966|#define PASTE(x) gdImageCreateFrom ## x ## Ptr
  |  |  ------------------
  ------------------
   25|    966|    if (im) gdImageDestroy(im);
  ------------------
  |  Branch (25:9): [True: 550, False: 416]
  ------------------
   26|    966|    return 0;
   27|    966|}

