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

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

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

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

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

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

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

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

