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

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

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

overflow2:
   23|  3.32k|{
   24|  3.32k|    if (a <= 0 || b <= 0) {
  ------------------
  |  Branch (24:9): [True: 0, False: 3.32k]
  |  Branch (24:19): [True: 0, False: 3.32k]
  ------------------
   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.32k|    if (a > 100000 / b) {
  ------------------
  |  Branch (29:9): [True: 34, False: 3.28k]
  ------------------
   30|     34|        gd_error_ex(GD_WARNING, "product of memory allocation multiplication would exceed "
  ------------------
  |  |   38|     34|#define GD_WARNING LOG_WARNING
  ------------------
   31|     34|                                "100000, failing operation gracefully\n");
   32|     34|        return 1;
   33|     34|    }
   34|  3.28k|    return 0;
   35|  3.32k|}

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

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

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

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

