gd_error:
   95|    235|{
   96|    235|    va_list args;
   97|       |
   98|    235|    va_start(args, format);
   99|    235|    _gd_error_ex(GD_WARNING, format, args);
  ------------------
  |  |   38|    235|#define GD_WARNING LOG_WARNING
  ------------------
  100|       |    va_end(args);
  101|    235|}
gd_error_ex:
  103|    111|{
  104|    111|    va_list args;
  105|       |
  106|    111|    va_start(args, format);
  107|    111|    _gd_error_ex(priority, format, args);
  108|       |    va_end(args);
  109|    111|}
gdImageCreateTrueColor:
  270|    561|{
  271|    561|    int i;
  272|    561|    gdImagePtr im;
  273|       |
  274|    561|    if (overflow2(sx, sy)) {
  ------------------
  |  Branch (274:9): [True: 0, False: 561]
  ------------------
  275|      0|        return NULL;
  276|      0|    }
  277|    561|    if (overflow2(sizeof(int *), sy)) {
  ------------------
  |  Branch (277:9): [True: 3, False: 558]
  ------------------
  278|      3|        return NULL;
  279|      3|    }
  280|    558|    if (overflow2(sizeof(int), sx)) {
  ------------------
  |  Branch (280:9): [True: 0, False: 558]
  ------------------
  281|      0|        return NULL;
  282|      0|    }
  283|       |
  284|    558|    im = (gdImage *)gdMalloc(sizeof(gdImage));
  285|    558|    if (!im) {
  ------------------
  |  Branch (285:9): [True: 0, False: 558]
  ------------------
  286|      0|        return 0;
  287|      0|    }
  288|    558|    memset(im, 0, sizeof(gdImage));
  289|       |
  290|    558|    im->tpixels = (int **)gdMalloc(sizeof(int *) * sy);
  291|    558|    if (!im->tpixels) {
  ------------------
  |  Branch (291:9): [True: 0, False: 558]
  ------------------
  292|      0|        gdFree(im);
  293|      0|        return 0;
  294|      0|    }
  295|    558|    im->polyInts = 0;
  296|    558|    im->polyAllocated = 0;
  297|    558|    im->brush = 0;
  298|    558|    im->tile = 0;
  299|    558|    im->style = 0;
  300|  97.7k|    for (i = 0; (i < sy); i++) {
  ------------------
  |  Branch (300:17): [True: 97.1k, False: 558]
  ------------------
  301|  97.1k|        im->tpixels[i] = (int *)gdCalloc(sx, sizeof(int));
  302|  97.1k|        if (!im->tpixels[i]) {
  ------------------
  |  Branch (302:13): [True: 0, False: 97.1k]
  ------------------
  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|  97.1k|    }
  314|    558|    im->sx = sx;
  315|    558|    im->sy = sy;
  316|    558|    im->transparent = (-1);
  317|    558|    im->interlace = 0;
  318|    558|    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|    558|    im->saveAlphaFlag = 0;
  325|    558|    im->alphaBlendingFlag = 1;
  326|    558|    im->thick = 1;
  327|    558|    im->AA = 0;
  328|    558|    im->cx1 = 0;
  329|    558|    im->cy1 = 0;
  330|    558|    im->cx2 = im->sx - 1;
  331|    558|    im->cy2 = im->sy - 1;
  332|    558|    im->res_x = GD_RESOLUTION;
  ------------------
  |  | 8517|    558|#define GD_RESOLUTION 96 /* pixels per inch */
  ------------------
  333|    558|    im->res_y = GD_RESOLUTION;
  ------------------
  |  | 8517|    558|#define GD_RESOLUTION 96 /* pixels per inch */
  ------------------
  334|       |    im->interpolation = NULL;
  335|    558|    im->interpolation_id = GD_BILINEAR_FIXED;
  336|    558|    return im;
  337|    558|}
gdImageDestroy:
  340|    558|{
  341|    558|    int i;
  342|    558|    if (im->pixels) {
  ------------------
  |  Branch (342:9): [True: 0, False: 558]
  ------------------
  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|    558|    if (im->tpixels) {
  ------------------
  |  Branch (348:9): [True: 558, False: 0]
  ------------------
  349|  97.7k|        for (i = 0; (i < im->sy); i++) {
  ------------------
  |  Branch (349:21): [True: 97.1k, False: 558]
  ------------------
  350|  97.1k|            gdFree(im->tpixels[i]);
  351|  97.1k|        }
  352|    558|        gdFree(im->tpixels);
  353|    558|    }
  354|    558|    if (im->polyInts) {
  ------------------
  |  Branch (354:9): [True: 0, False: 558]
  ------------------
  355|      0|        gdFree(im->polyInts);
  356|      0|    }
  357|    558|    if (im->style) {
  ------------------
  |  Branch (357:9): [True: 0, False: 558]
  ------------------
  358|      0|        gdFree(im->style);
  359|      0|    }
  360|    558|    gdFree(im);
  361|    558|}
gdImageAlphaBlending:
 3406|    344|{
 3407|    344|    im->alphaBlendingFlag = alphaBlendingArg;
 3408|    344|}
gdImageSaveAlpha:
 3411|    344|{
 3412|    344|    im->saveAlphaFlag = saveAlphaArg;
 3413|    344|}
gd.c:_gd_error_ex:
   88|    346|{
   89|    346|    if (gd_error_method) {
  ------------------
  |  Branch (89:9): [True: 346, False: 0]
  ------------------
   90|    346|        gd_error_method(priority, format, args);
   91|    346|    }
   92|    346|}
gd.c:gd_stderr_error:
   63|    346|{
   64|    346|    switch (priority) {
  ------------------
  |  Branch (64:13): [True: 346, False: 0]
  ------------------
   65|      0|    case GD_ERROR:
  ------------------
  |  |   37|      0|#define GD_ERROR LOG_ERR
  ------------------
  |  Branch (65:5): [True: 0, False: 346]
  ------------------
   66|      0|        fputs("GD Error: ", stderr);
   67|      0|        break;
   68|    346|    case GD_WARNING:
  ------------------
  |  |   38|    346|#define GD_WARNING LOG_WARNING
  ------------------
  |  Branch (68:5): [True: 346, False: 0]
  ------------------
   69|    346|        fputs("GD Warning: ", stderr);
   70|    346|        break;
   71|      0|    case GD_NOTICE:
  ------------------
  |  |   39|      0|#define GD_NOTICE LOG_NOTICE
  ------------------
  |  Branch (71:5): [True: 0, False: 346]
  ------------------
   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: 346]
  ------------------
   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: 346]
  ------------------
   78|      0|        fputs("GD Debug: ", stderr);
   79|      0|        break;
   80|    346|    }
   81|    346|    vfprintf(stderr, format, args);
   82|       |    fflush(stderr);
   83|    346|}

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

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

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: 36, False: 3.42k]
  ------------------
   30|     36|        gd_error_ex(GD_WARNING, "product of memory allocation multiplication would exceed "
  ------------------
  |  |   38|     36|#define GD_WARNING LOG_WARNING
  ------------------
   31|     36|                                "100000, failing operation gracefully\n");
   32|     36|        return 1;
   33|     36|    }
   34|  3.42k|    return 0;
   35|  3.46k|}

gdImageCreateFromTgaPtr:
   57|    957|{
   58|    957|    gdImagePtr im;
   59|    957|    gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
   60|    957|    if (in == NULL)
  ------------------
  |  Branch (60:9): [True: 0, False: 957]
  ------------------
   61|      0|        return NULL;
   62|    957|    im = gdImageCreateFromTgaCtx(in);
   63|    957|    in->gd_free(in);
   64|    957|    return im;
   65|    957|}
gdImageCreateFromTgaCtx:
   76|    957|{
   77|    957|    int bitmap_caret = 0;
   78|    957|    oTga *tga = NULL;
   79|    957|    volatile gdImagePtr image = NULL;
   80|    957|    int x = 0;
   81|    957|    int y = 0;
   82|       |
   83|    957|    if (ctx == NULL) {
  ------------------
  |  Branch (83:9): [True: 0, False: 957]
  ------------------
   84|      0|        return NULL;
   85|      0|    }
   86|       |
   87|    957|    tga = (oTga *)gdMalloc(sizeof(oTga));
   88|    957|    if (!tga) {
  ------------------
  |  Branch (88:9): [True: 0, False: 957]
  ------------------
   89|      0|        return NULL;
   90|      0|    }
   91|       |
   92|    957|    tga->bitmap = NULL;
   93|    957|    tga->colormap = NULL;
   94|    957|    tga->ident = NULL;
   95|    957|    tga->has_alpha = 0;
   96|       |
   97|    957|    if (read_header_tga(ctx, tga) < 0) {
  ------------------
  |  Branch (97:9): [True: 109, False: 848]
  ------------------
   98|    109|        free_tga(tga);
   99|    109|        return NULL;
  100|    109|    }
  101|       |
  102|    848|    if (read_image_tga(ctx, tga) < 0) {
  ------------------
  |  Branch (102:9): [True: 287, False: 561]
  ------------------
  103|    287|        free_tga(tga);
  104|    287|        return NULL;
  105|    287|    }
  106|       |
  107|    561|    image = gdImageCreateTrueColor((int)tga->width, (int)tga->height);
  108|       |
  109|    561|    if (image == 0) {
  ------------------
  |  Branch (109:9): [True: 3, False: 558]
  ------------------
  110|      3|        free_tga(tga);
  111|      3|        return NULL;
  112|      3|    }
  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|    558|    if (tga->has_alpha) {
  ------------------
  |  Branch (118:9): [True: 344, False: 214]
  ------------------
  119|    344|        gdImageAlphaBlending(image, 0);
  120|    344|        gdImageSaveAlpha(image, 1);
  121|    344|    }
  122|       |
  123|  97.7k|    for (y = 0; y < tga->height; y++) {
  ------------------
  |  Branch (123:17): [True: 97.1k, False: 558]
  ------------------
  124|  97.1k|        register int *tpix = image->tpixels[y];
  125|   640k|        for (x = 0; x < tga->width; x++, tpix++) {
  ------------------
  |  Branch (125:21): [True: 542k, False: 97.1k]
  ------------------
  126|   542k|            *tpix = tga->bitmap[bitmap_caret++];
  127|   542k|        }
  128|  97.1k|    }
  129|       |
  130|    558|    if (tga->flipv && tga->fliph) {
  ------------------
  |  Branch (130:9): [True: 331, False: 227]
  |  Branch (130:23): [True: 203, False: 128]
  ------------------
  131|    203|        gdImageFlipBoth(image);
  132|    355|    } else if (tga->flipv) {
  ------------------
  |  Branch (132:16): [True: 128, False: 227]
  ------------------
  133|    128|        gdImageFlipVertical(image);
  134|    227|    } else if (tga->fliph) {
  ------------------
  |  Branch (134:16): [True: 182, False: 45]
  ------------------
  135|    182|        gdImageFlipHorizontal(image);
  136|    182|    }
  137|       |
  138|    558|    free_tga(tga);
  139|       |
  140|    558|    return image;
  141|    561|}
read_header_tga:
  149|    957|{
  150|       |
  151|    957|    unsigned char header[18];
  152|       |
  153|    957|    if (gdGetBuf(header, sizeof(header), ctx) < 18) {
  ------------------
  |  Branch (153:9): [True: 11, False: 946]
  ------------------
  154|     11|        gd_error("fail to read header");
  155|     11|        return -1;
  156|     11|    }
  157|       |
  158|    946|    tga->identsize = header[0];
  159|    946|    tga->colormaptype = header[1];
  160|    946|    tga->imagetype = header[2];
  161|    946|    tga->colormapstart = header[3] + (header[4] << 8);
  162|    946|    tga->colormaplength = header[5] + (header[6] << 8);
  163|    946|    tga->colormapbits = header[7];
  164|    946|    tga->xstart = header[8] + (header[9] << 8);
  165|    946|    tga->ystart = header[10] + (header[11] << 8);
  166|    946|    tga->width = header[12] + (header[13] << 8);
  167|    946|    tga->height = header[14] + (header[15] << 8);
  168|    946|    tga->bits = header[16];
  169|    946|    tga->alphabits = header[17] & 0x0f;
  170|    946|    tga->fliph = (header[17] & 0x10) ? 1 : 0;
  ------------------
  |  Branch (170:18): [True: 540, False: 406]
  ------------------
  171|    946|    tga->flipv = (header[17] & 0x20) ? 0 : 1;
  ------------------
  |  Branch (171:18): [True: 372, False: 574]
  ------------------
  172|    946|    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|    946|    if (tga->width <= 0 || tga->height <= 0) {
  ------------------
  |  Branch (181:9): [True: 4, False: 942]
  |  Branch (181:28): [True: 1, False: 941]
  ------------------
  182|      5|        gd_error("gd-tga: invalid image dimensions\n");
  183|      5|        return -1;
  184|      5|    }
  185|       |
  186|    941|    if (tga->colormaptype > 1) {
  ------------------
  |  Branch (186:9): [True: 10, False: 931]
  ------------------
  187|     10|        gd_error_ex(GD_WARNING, "gd-tga: unsupported color map type %u\n", tga->colormaptype);
  ------------------
  |  |   38|     10|#define GD_WARNING LOG_WARNING
  ------------------
  188|     10|        return -1;
  189|     10|    }
  190|       |
  191|    931|    if (tga->colormaptype == 1 &&
  ------------------
  |  Branch (191:9): [True: 159, False: 772]
  ------------------
  192|    159|        !(tga->colormapbits == 15 || tga->colormapbits == 16 || tga->colormapbits == 24 ||
  ------------------
  |  Branch (192:11): [True: 37, False: 122]
  |  Branch (192:38): [True: 73, False: 49]
  |  Branch (192:65): [True: 9, 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|    907|    switch (tga->imagetype) {
  199|     94|    case TGA_TYPE_INDEXED:
  ------------------
  |  |   35|     94|#define TGA_TYPE_INDEXED 1
  ------------------
  |  Branch (199:5): [True: 94, False: 813]
  ------------------
  200|    128|    case TGA_TYPE_INDEXED_RLE:
  ------------------
  |  |   38|    128|#define TGA_TYPE_INDEXED_RLE 9
  ------------------
  |  Branch (200:5): [True: 34, False: 873]
  ------------------
  201|    128|        if (tga->colormaptype != 1 || tga->bits != TGA_BPP_8 ||
  ------------------
  |  |   44|    255|#define TGA_BPP_8 8
  ------------------
  |  Branch (201:13): [True: 1, False: 127]
  |  Branch (201:39): [True: 8, False: 119]
  ------------------
  202|    119|            !(tga->colormapbits == 15 || tga->colormapbits == 16 || tga->colormapbits == 24 ||
  ------------------
  |  Branch (202:15): [True: 32, False: 87]
  |  Branch (202:42): [True: 67, False: 20]
  |  Branch (202:69): [True: 7, False: 13]
  ------------------
  203|     13|              tga->colormapbits == 32)) {
  ------------------
  |  Branch (203:15): [True: 13, False: 0]
  ------------------
  204|      9|            gd_error_ex(GD_WARNING, "gd-tga: unsupported color mapped image\n");
  ------------------
  |  |   38|      9|#define GD_WARNING LOG_WARNING
  ------------------
  205|      9|            return -1;
  206|      9|        }
  207|    119|        break;
  208|       |
  209|    459|    case TGA_TYPE_RGB:
  ------------------
  |  |   36|    459|#define TGA_TYPE_RGB 2
  ------------------
  |  Branch (209:5): [True: 459, False: 448]
  ------------------
  210|    549|    case TGA_TYPE_RGB_RLE:
  ------------------
  |  |   39|    549|#define TGA_TYPE_RGB_RLE 10
  ------------------
  |  Branch (210:5): [True: 90, False: 817]
  ------------------
  211|    549|        if (!(tga->bits == TGA_BPP_16 || tga->bits == TGA_BPP_24 ||
  ------------------
  |  |   45|  1.09k|#define TGA_BPP_16 16
  ------------------
                      if (!(tga->bits == TGA_BPP_16 || tga->bits == TGA_BPP_24 ||
  ------------------
  |  |   46|    611|#define TGA_BPP_24 24
  ------------------
  |  Branch (211:15): [True: 487, False: 62]
  |  Branch (211:42): [True: 16, False: 46]
  ------------------
  212|     46|              (tga->bits == TGA_BPP_32 && tga->alphabits == 8))) {
  ------------------
  |  |   47|     92|#define TGA_BPP_32 32
  ------------------
  |  Branch (212:16): [True: 30, False: 16]
  |  Branch (212:43): [True: 25, False: 5]
  ------------------
  213|     21|            gd_error_ex(GD_WARNING, "gd-tga: unsupported truecolor depth %u\n", tga->bits);
  ------------------
  |  |   38|     21|#define GD_WARNING LOG_WARNING
  ------------------
  214|     21|            return -1;
  215|     21|        }
  216|    528|        break;
  217|       |
  218|    528|    case TGA_TYPE_GREYSCALE:
  ------------------
  |  |   37|     44|#define TGA_TYPE_GREYSCALE 3
  ------------------
  |  Branch (218:5): [True: 44, False: 863]
  ------------------
  219|    229|    case TGA_TYPE_GREYSCALE_RLE:
  ------------------
  |  |   40|    229|#define TGA_TYPE_GREYSCALE_RLE 11
  ------------------
  |  Branch (219:5): [True: 185, False: 722]
  ------------------
  220|    229|        if (tga->bits != TGA_BPP_8) {
  ------------------
  |  |   44|    229|#define TGA_BPP_8 8
  ------------------
  |  Branch (220:13): [True: 10, False: 219]
  ------------------
  221|     10|            gd_error_ex(GD_WARNING, "gd-tga: unsupported grayscale depth %u\n", tga->bits);
  ------------------
  |  |   38|     10|#define GD_WARNING LOG_WARNING
  ------------------
  222|     10|            return -1;
  223|     10|        }
  224|    219|        break;
  225|       |
  226|    219|    default:
  ------------------
  |  Branch (226:5): [True: 1, False: 906]
  ------------------
  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|    907|    }
  230|       |
  231|    866|    tga->ident = NULL;
  232|       |
  233|    866|    if (tga->identsize > 0) {
  ------------------
  |  Branch (233:9): [True: 48, False: 818]
  ------------------
  234|     48|        tga->ident = (char *)gdMalloc(tga->identsize * sizeof(char));
  235|     48|        if (tga->ident == NULL) {
  ------------------
  |  Branch (235:13): [True: 0, False: 48]
  ------------------
  236|      0|            return -1;
  237|      0|        }
  238|       |
  239|     48|        if (gdGetBuf(tga->ident, tga->identsize, ctx) != tga->identsize) {
  ------------------
  |  Branch (239:13): [True: 18, False: 30]
  ------------------
  240|     18|            gd_error("fail to read header ident");
  241|     18|            return -1;
  242|     18|        }
  243|     48|    }
  244|       |
  245|    848|    return 1;
  246|    866|}
read_image_tga:
  254|    848|{
  255|    848|    int pixel_count;
  256|    848|    int bitmap_caret = 0;
  257|       |
  258|    848|    if (overflow2(tga->width, tga->height)) {
  ------------------
  |  Branch (258:9): [True: 22, False: 826]
  ------------------
  259|     22|        return -1;
  260|     22|    }
  261|       |
  262|    826|    pixel_count = tga->width * tga->height;
  263|    826|    if (overflow2(pixel_count, sizeof(int))) {
  ------------------
  |  Branch (263:9): [True: 5, False: 821]
  ------------------
  264|      5|        return -1;
  265|      5|    }
  266|       |
  267|    821|    if (tga_read_color_map(ctx, tga) < 0) {
  ------------------
  |  Branch (267:9): [True: 42, False: 779]
  ------------------
  268|     42|        return -1;
  269|     42|    }
  270|       |
  271|       |    /*!	\brief Allocate memory for image block
  272|       |     *  Allocate a chunk of memory for the image block to be passed into.
  273|       |     */
  274|    779|    tga->bitmap = (int *)gdMalloc(pixel_count * sizeof(int));
  275|    779|    if (tga->bitmap == NULL)
  ------------------
  |  Branch (275:9): [True: 0, False: 779]
  ------------------
  276|      0|        return -1;
  277|       |
  278|    779|    if (tga_is_rle(tga->imagetype)) {
  ------------------
  |  Branch (278:9): [True: 265, False: 514]
  ------------------
  279|   248k|        while (bitmap_caret < pixel_count) {
  ------------------
  |  Branch (279:16): [True: 248k, False: 157]
  ------------------
  280|   248k|            int packet = gdGetC(ctx);
  281|   248k|            int encoded_pixels;
  282|   248k|            int pixel;
  283|   248k|            int i;
  284|       |
  285|   248k|            if (packet == EOF) {
  ------------------
  |  Branch (285:17): [True: 35, False: 248k]
  ------------------
  286|     35|                gd_error("gd-tga: premature end of image data\n");
  287|     35|                return -1;
  288|     35|            }
  289|       |
  290|   248k|            encoded_pixels = (packet & ~TGA_RLE_FLAG) + 1;
  ------------------
  |  |   49|   248k|#define TGA_RLE_FLAG 128
  ------------------
  291|   248k|            if (encoded_pixels > pixel_count - bitmap_caret) {
  ------------------
  |  Branch (291:17): [True: 14, False: 248k]
  ------------------
  292|     14|                return -1;
  293|     14|            }
  294|       |
  295|   248k|            if ((packet & TGA_RLE_FLAG) == TGA_RLE_FLAG) {
  ------------------
  |  |   49|   248k|#define TGA_RLE_FLAG 128
  ------------------
                          if ((packet & TGA_RLE_FLAG) == TGA_RLE_FLAG) {
  ------------------
  |  |   49|   248k|#define TGA_RLE_FLAG 128
  ------------------
  |  Branch (295:17): [True: 4.06k, False: 244k]
  ------------------
  296|  4.06k|                if (tga_read_pixel(ctx, tga, &pixel) < 0) {
  ------------------
  |  Branch (296:21): [True: 9, False: 4.05k]
  ------------------
  297|      9|                    return -1;
  298|      9|                }
  299|   446k|                for (i = 0; i < encoded_pixels; i++) {
  ------------------
  |  Branch (299:29): [True: 442k, False: 4.05k]
  ------------------
  300|   442k|                    tga->bitmap[bitmap_caret++] = pixel;
  301|   442k|                }
  302|   244k|            } else {
  303|   565k|                for (i = 0; i < encoded_pixels; i++) {
  ------------------
  |  Branch (303:29): [True: 320k, False: 244k]
  ------------------
  304|   320k|                    if (tga_read_pixel(ctx, tga, &pixel) < 0) {
  ------------------
  |  Branch (304:25): [True: 50, False: 320k]
  ------------------
  305|     50|                        return -1;
  306|     50|                    }
  307|   320k|                    tga->bitmap[bitmap_caret++] = pixel;
  308|   320k|                }
  309|   244k|            }
  310|   248k|        }
  311|    514|    } else {
  312|  40.6k|        while (bitmap_caret < pixel_count) {
  ------------------
  |  Branch (312:16): [True: 40.2k, False: 404]
  ------------------
  313|  40.2k|            if (tga_read_pixel(ctx, tga, &tga->bitmap[bitmap_caret]) < 0) {
  ------------------
  |  Branch (313:17): [True: 110, False: 40.1k]
  ------------------
  314|    110|                return -1;
  315|    110|            }
  316|  40.1k|            bitmap_caret++;
  317|  40.1k|        }
  318|    514|    }
  319|       |
  320|    561|    tga_apply_attribute_type(ctx, tga, pixel_count);
  321|       |
  322|    561|    return 1;
  323|    779|}
free_tga:
  554|    957|{
  555|    957|    if (tga) {
  ------------------
  |  Branch (555:9): [True: 957, False: 0]
  ------------------
  556|    957|        if (tga->ident)
  ------------------
  |  Branch (556:13): [True: 48, False: 909]
  ------------------
  557|     48|            gdFree(tga->ident);
  558|    957|        if (tga->bitmap)
  ------------------
  |  Branch (558:13): [True: 779, False: 178]
  ------------------
  559|    779|            gdFree(tga->bitmap);
  560|    957|        if (tga->colormap)
  ------------------
  |  Branch (560:13): [True: 105, False: 852]
  ------------------
  561|    105|            gdFree(tga->colormap);
  562|    957|        gdFree(tga);
  563|    957|    }
  564|    957|}
gd_tga.c:tga_is_rle:
  326|    779|{
  327|    779|    return imagetype == TGA_TYPE_INDEXED_RLE || imagetype == TGA_TYPE_RGB_RLE ||
  ------------------
  |  |   38|  1.55k|#define TGA_TYPE_INDEXED_RLE 9
  ------------------
                  return imagetype == TGA_TYPE_INDEXED_RLE || imagetype == TGA_TYPE_RGB_RLE ||
  ------------------
  |  |   39|  1.54k|#define TGA_TYPE_RGB_RLE 10
  ------------------
  |  Branch (327:12): [True: 12, False: 767]
  |  Branch (327:49): [True: 86, False: 681]
  ------------------
  328|    681|           imagetype == TGA_TYPE_GREYSCALE_RLE;
  ------------------
  |  |   40|    681|#define TGA_TYPE_GREYSCALE_RLE 11
  ------------------
  |  Branch (328:12): [True: 167, False: 514]
  ------------------
  329|    779|}
gd_tga.c:tga_read_color_map:
  334|    821|{
  335|    821|    int i;
  336|    821|    int has_alpha = 0;
  337|    821|    int entry_size;
  338|       |
  339|    821|    if (tga->colormaptype == 0) {
  ------------------
  |  Branch (339:9): [True: 709, False: 112]
  ------------------
  340|    709|        return 1;
  341|    709|    }
  342|       |
  343|    112|    if (tga->colormaplength <= 0) {
  ------------------
  |  Branch (343:9): [True: 1, False: 111]
  ------------------
  344|      1|        return -1;
  345|      1|    }
  346|       |
  347|    111|    if (overflow2(tga->colormaplength, sizeof(int))) {
  ------------------
  |  Branch (347:9): [True: 6, False: 105]
  ------------------
  348|      6|        return -1;
  349|      6|    }
  350|       |
  351|    105|    entry_size = tga_pixel_size(tga->colormapbits);
  352|    105|    tga->colormap = (int *)gdMalloc(tga->colormaplength * sizeof(int));
  353|    105|    if (tga->colormap == NULL) {
  ------------------
  |  Branch (353:9): [True: 0, False: 105]
  ------------------
  354|      0|        return -1;
  355|      0|    }
  356|       |
  357|  32.5k|    for (i = 0; i < tga->colormaplength; i++) {
  ------------------
  |  Branch (357:17): [True: 32.4k, False: 70]
  ------------------
  358|  32.4k|        unsigned char buf[4] = {0, 0, 0, 0};
  359|       |
  360|  32.4k|        if (gdGetBuf(buf, entry_size, ctx) != entry_size) {
  ------------------
  |  Branch (360:13): [True: 35, False: 32.4k]
  ------------------
  361|     35|            gd_error("gd-tga: premature end of color map data\n");
  362|     35|            return -1;
  363|     35|        }
  364|       |
  365|  32.4k|        tga->colormap[i] =
  366|  32.4k|            tga_decode_color(buf, tga->colormapbits, tga->colormapbits == 32 ? 8 : 0, &has_alpha);
  ------------------
  |  Branch (366:54): [True: 251, False: 32.1k]
  ------------------
  367|  32.4k|    }
  368|       |
  369|     70|    if (has_alpha) {
  ------------------
  |  Branch (369:9): [True: 3, False: 67]
  ------------------
  370|      3|        tga->has_alpha = 1;
  371|      3|    }
  372|       |
  373|     70|    return 1;
  374|    105|}
gd_tga.c:tga_pixel_size:
  331|   365k|static int tga_pixel_size(uint8_t bits) { return (bits + 7) / 8; }
gd_tga.c:tga_decode_color:
  494|   346k|{
  495|   346k|    switch (bits) {
  ------------------
  |  Branch (495:13): [True: 346k, False: 0]
  ------------------
  496|  5.09k|    case 15:
  ------------------
  |  Branch (496:5): [True: 5.09k, False: 341k]
  ------------------
  497|   121k|    case 16:
  ------------------
  |  Branch (497:5): [True: 116k, False: 230k]
  ------------------
  498|   121k|        return tga_decode_16((unsigned int)buf[0] | ((unsigned int)buf[1] << 8), alpha_bits,
  499|   121k|                             has_alpha);
  500|       |
  501|  12.6k|    case 24:
  ------------------
  |  Branch (501:5): [True: 12.6k, False: 334k]
  ------------------
  502|  12.6k|        return gdTrueColor(buf[2], buf[1], buf[0]);
  ------------------
  |  | 6044|  12.6k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  503|       |
  504|   212k|    case 32:
  ------------------
  |  Branch (504:5): [True: 212k, False: 134k]
  ------------------
  505|   212k|        if (has_alpha) {
  ------------------
  |  Branch (505:13): [True: 212k, False: 0]
  ------------------
  506|   212k|            *has_alpha = 1;
  507|   212k|        }
  508|   212k|        return gdTrueColorAlpha(buf[2], buf[1], buf[0], tga_alpha_8_to_gd(buf[3]));
  ------------------
  |  | 6061|   212k|#define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
  ------------------
  509|   346k|    }
  510|       |
  511|      0|    return 0;
  512|   346k|}
gd_tga.c:tga_decode_16:
  515|   121k|{
  516|   121k|    int b = tga_scale_5_to_8(value & 0x1f);
  517|   121k|    int g = tga_scale_5_to_8((value >> 5) & 0x1f);
  518|   121k|    int r = tga_scale_5_to_8((value >> 10) & 0x1f);
  519|       |
  520|   121k|    if (alpha_bits > 0) {
  ------------------
  |  Branch (520:9): [True: 89.5k, False: 32.2k]
  ------------------
  521|  89.5k|        int a = (value & 0x8000) ? gdAlphaOpaque : gdAlphaTransparent;
  ------------------
  |  |  141|  6.45k|#define gdAlphaOpaque 0
  ------------------
                      int a = (value & 0x8000) ? gdAlphaOpaque : gdAlphaTransparent;
  ------------------
  |  |  142|  83.1k|#define gdAlphaTransparent 127
  ------------------
  |  Branch (521:17): [True: 6.45k, False: 83.1k]
  ------------------
  522|       |
  523|  89.5k|        if (has_alpha) {
  ------------------
  |  Branch (523:13): [True: 89.5k, False: 0]
  ------------------
  524|  89.5k|            *has_alpha = 1;
  525|  89.5k|        }
  526|  89.5k|        return gdTrueColorAlpha(r, g, b, a);
  ------------------
  |  | 6061|  89.5k|#define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
  ------------------
  527|  89.5k|    }
  528|       |
  529|  32.2k|    return gdTrueColor(r, g, b);
  ------------------
  |  | 6044|  32.2k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  530|   121k|}
gd_tga.c:tga_scale_5_to_8:
  532|   365k|static int tga_scale_5_to_8(int c) { return (c * 255) / 31; }
gd_tga.c:tga_alpha_8_to_gd:
  534|   212k|static int tga_alpha_8_to_gd(int a) { return gdAlphaMax - (a >> 1); }
  ------------------
  |  |  140|   212k|#define gdAlphaMax 127
  ------------------
gd_tga.c:tga_read_pixel:
  377|   365k|{
  378|   365k|    unsigned char buf[4] = {0, 0, 0, 0};
  379|   365k|    int size = tga_pixel_size(tga->bits);
  380|   365k|    int has_alpha = 0;
  381|       |
  382|   365k|    if (gdGetBuf(buf, size, ctx) != size) {
  ------------------
  |  Branch (382:9): [True: 131, False: 365k]
  ------------------
  383|    131|        gd_error("gd-tga: premature end of image data\n");
  384|    131|        return -1;
  385|    131|    }
  386|       |
  387|   365k|    switch (tga->imagetype) {
  388|  5.98k|    case TGA_TYPE_INDEXED:
  ------------------
  |  |   35|  5.98k|#define TGA_TYPE_INDEXED 1
  ------------------
  |  Branch (388:5): [True: 5.98k, False: 359k]
  ------------------
  389|  6.20k|    case TGA_TYPE_INDEXED_RLE: {
  ------------------
  |  |   38|  6.20k|#define TGA_TYPE_INDEXED_RLE 9
  ------------------
  |  Branch (389:5): [True: 224, False: 364k]
  ------------------
  390|  6.20k|        int index = buf[0] - tga->colormapstart;
  391|       |
  392|  6.20k|        if (index < 0 || index >= tga->colormaplength || tga->colormap == NULL) {
  ------------------
  |  Branch (392:13): [True: 22, False: 6.18k]
  |  Branch (392:26): [True: 16, False: 6.16k]
  |  Branch (392:58): [True: 0, False: 6.16k]
  ------------------
  393|     38|            return -1;
  394|     38|        }
  395|  6.16k|        *pixel = tga->colormap[index];
  396|  6.16k|        break;
  397|  6.20k|    }
  398|       |
  399|  12.1k|    case TGA_TYPE_RGB:
  ------------------
  |  |   36|  12.1k|#define TGA_TYPE_RGB 2
  ------------------
  |  Branch (399:5): [True: 12.1k, False: 352k]
  ------------------
  400|   314k|    case TGA_TYPE_RGB_RLE:
  ------------------
  |  |   39|   314k|#define TGA_TYPE_RGB_RLE 10
  ------------------
  |  Branch (400:5): [True: 302k, False: 62.6k]
  ------------------
  401|   314k|        *pixel = tga_decode_color(buf, tga->bits, tga->alphabits, &has_alpha);
  402|   314k|        if (has_alpha) {
  ------------------
  |  Branch (402:13): [True: 301k, False: 12.7k]
  ------------------
  403|   301k|            tga->has_alpha = 1;
  404|   301k|        }
  405|   314k|        break;
  406|       |
  407|  22.0k|    case TGA_TYPE_GREYSCALE:
  ------------------
  |  |   37|  22.0k|#define TGA_TYPE_GREYSCALE 3
  ------------------
  |  Branch (407:5): [True: 22.0k, False: 343k]
  ------------------
  408|  44.3k|    case TGA_TYPE_GREYSCALE_RLE:
  ------------------
  |  |   40|  44.3k|#define TGA_TYPE_GREYSCALE_RLE 11
  ------------------
  |  Branch (408:5): [True: 22.3k, False: 342k]
  ------------------
  409|  44.3k|        *pixel = gdTrueColor(buf[0], buf[0], buf[0]);
  ------------------
  |  | 6044|  44.3k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  410|  44.3k|        break;
  411|       |
  412|      0|    default:
  ------------------
  |  Branch (412:5): [True: 0, False: 365k]
  ------------------
  413|      0|        return -1;
  414|   365k|    }
  415|       |
  416|   365k|    return 1;
  417|   365k|}
gd_tga.c:tga_apply_attribute_type:
  420|    561|{
  421|    561|    static const unsigned char signature[18] = {'T', 'R', 'U', 'E', 'V', 'I', 'S', 'I', 'O',
  422|    561|                                                'N', '-', 'X', 'F', 'I', 'L', 'E', '.', '\0'};
  423|    561|    unsigned char footer_ring[26];
  424|    561|    unsigned char footer[26];
  425|    561|    size_t trailing_size = 0;
  426|    561|    size_t i;
  427|    561|    int c;
  428|    561|    long start;
  429|    561|    uint32_t extension_offset;
  430|    561|    uint32_t attr_type_offset;
  431|    561|    uint32_t end_offset;
  432|    561|    int attr_type;
  433|       |
  434|    561|    if (!tga->has_alpha) {
  ------------------
  |  Branch (434:9): [True: 123, False: 438]
  ------------------
  435|    123|        return;
  436|    123|    }
  437|       |
  438|    438|    if (ctx->tell == NULL || ctx->seek == NULL) {
  ------------------
  |  Branch (438:9): [True: 0, False: 438]
  |  Branch (438:30): [True: 0, False: 438]
  ------------------
  439|      0|        return;
  440|      0|    }
  441|       |
  442|    438|    start = gdTell(ctx);
  443|    438|    if (start < 0 || start > INT_MAX) {
  ------------------
  |  Branch (443:9): [True: 0, False: 438]
  |  Branch (443:22): [True: 0, False: 438]
  ------------------
  444|      0|        return;
  445|      0|    }
  446|       |
  447|  16.5M|    while ((c = gdGetC(ctx)) != EOF) {
  ------------------
  |  Branch (447:12): [True: 16.5M, False: 438]
  ------------------
  448|  16.5M|        footer_ring[trailing_size % sizeof(footer_ring)] = (unsigned char)c;
  449|  16.5M|        if (trailing_size == SIZE_MAX) {
  ------------------
  |  Branch (449:13): [True: 0, False: 16.5M]
  ------------------
  450|      0|            return;
  451|      0|        }
  452|  16.5M|        trailing_size++;
  453|  16.5M|    }
  454|       |
  455|    438|    if (trailing_size < sizeof(footer)) {
  ------------------
  |  Branch (455:9): [True: 60, False: 378]
  ------------------
  456|     60|        return;
  457|     60|    }
  458|       |
  459|  10.2k|    for (i = 0; i < sizeof(footer); i++) {
  ------------------
  |  Branch (459:17): [True: 9.82k, False: 378]
  ------------------
  460|  9.82k|        footer[i] = footer_ring[(trailing_size + i) % sizeof(footer_ring)];
  461|  9.82k|    }
  462|    378|    if (memcmp(footer + 8, signature, sizeof(signature)) != 0) {
  ------------------
  |  Branch (462:9): [True: 146, False: 232]
  ------------------
  463|    146|        return;
  464|    146|    }
  465|       |
  466|    232|    extension_offset = (uint32_t)footer[0] | ((uint32_t)footer[1] << 8) |
  467|    232|                       ((uint32_t)footer[2] << 16) | ((uint32_t)footer[3] << 24);
  468|    232|    if (extension_offset == 0 || extension_offset < (uint32_t)start ||
  ------------------
  |  Branch (468:9): [True: 1, False: 231]
  |  Branch (468:34): [True: 2, False: 229]
  ------------------
  469|    229|        extension_offset > (uint32_t)INT_MAX - 494) {
  ------------------
  |  Branch (469:9): [True: 37, False: 192]
  ------------------
  470|     40|        return;
  471|     40|    }
  472|       |
  473|    192|    attr_type_offset = extension_offset + 494;
  474|    192|    if (trailing_size > (size_t)INT_MAX - (size_t)start ||
  ------------------
  |  Branch (474:9): [True: 0, False: 192]
  ------------------
  475|    192|        attr_type_offset >= (uint32_t)start + (uint32_t)trailing_size - sizeof(footer)) {
  ------------------
  |  Branch (475:9): [True: 94, False: 98]
  ------------------
  476|     94|        return;
  477|     94|    }
  478|     98|    end_offset = (uint32_t)start + (uint32_t)trailing_size;
  479|       |
  480|     98|    if (!gdSeek(ctx, (int)attr_type_offset)) {
  ------------------
  |  Branch (480:9): [True: 0, False: 98]
  ------------------
  481|      0|        return;
  482|      0|    }
  483|     98|    attr_type = gdGetC(ctx);
  484|     98|    gdSeek(ctx, (int)end_offset);
  485|     98|    if (attr_type == EOF) {
  ------------------
  |  Branch (485:9): [True: 0, False: 98]
  ------------------
  486|      0|        return;
  487|      0|    }
  488|     98|    if (attr_type != 3 && attr_type != 4) {
  ------------------
  |  Branch (488:9): [True: 95, False: 3]
  |  Branch (488:27): [True: 94, False: 1]
  ------------------
  489|     94|        tga_strip_alpha(tga, pixel_count);
  490|     94|    }
  491|     98|}
gd_tga.c:tga_strip_alpha:
  537|     94|{
  538|     94|    int i;
  539|       |
  540|  32.7k|    for (i = 0; i < pixel_count; i++) {
  ------------------
  |  Branch (540:17): [True: 32.6k, False: 94]
  ------------------
  541|  32.6k|        int pixel = tga->bitmap[i];
  542|       |
  543|  32.6k|        tga->bitmap[i] = gdTrueColor(gdTrueColorGetRed(pixel), gdTrueColorGetGreen(pixel),
  ------------------
  |  | 6044|  32.6k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  544|  32.6k|                                     gdTrueColorGetBlue(pixel));
  545|  32.6k|    }
  546|     94|    tga->has_alpha = 0;
  547|     94|}

gdImageFlipVertical:
   12|    331|{
   13|    331|    register int x, y;
   14|       |
   15|    331|    if (im->trueColor) {
  ------------------
  |  Branch (15:9): [True: 331, False: 0]
  ------------------
   16|  37.8k|        for (y = 0; y < im->sy / 2; y++) {
  ------------------
  |  Branch (16:21): [True: 37.4k, False: 331]
  ------------------
   17|  37.4k|            int *row_dst = im->tpixels[y];
   18|  37.4k|            int *row_src = im->tpixels[im->sy - 1 - y];
   19|   113k|            for (x = 0; x < im->sx; x++) {
  ------------------
  |  Branch (19:25): [True: 76.3k, False: 37.4k]
  ------------------
   20|  76.3k|                register int p;
   21|  76.3k|                p = row_dst[x];
   22|  76.3k|                row_dst[x] = im->tpixels[im->sy - 1 - y][x];
   23|  76.3k|                row_src[x] = p;
   24|  76.3k|            }
   25|  37.4k|        }
   26|    331|    } 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|    331|    return;
   37|    331|}
gdImageFlipHorizontal:
   40|    385|{
   41|       |
   42|    385|    int x, y;
   43|       |
   44|    385|    if (im->trueColor) {
  ------------------
  |  Branch (44:9): [True: 385, False: 0]
  ------------------
   45|    385|        int *px1, *px2, tmp;
   46|       |
   47|  84.7k|        for (y = 0; y < im->sy; y++) {
  ------------------
  |  Branch (47:21): [True: 84.3k, False: 385]
  ------------------
   48|  84.3k|            px1 = im->tpixels[y];
   49|  84.3k|            px2 = im->tpixels[y] + im->sx - 1;
   50|   320k|            for (x = 0; x < (im->sx >> 1); x++) {
  ------------------
  |  Branch (50:25): [True: 235k, False: 84.3k]
  ------------------
   51|   235k|                tmp = *px1;
   52|   235k|                *px1 = *px2;
   53|   235k|                *px2 = tmp;
   54|   235k|                px1++;
   55|   235k|                px2--;
   56|   235k|            }
   57|  84.3k|        }
   58|    385|    } 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|    385|}
gdImageFlipBoth:
   76|    203|{
   77|    203|    gdImageFlipVertical(im);
   78|    203|    gdImageFlipHorizontal(im);
   79|    203|}

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

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

