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

gdGetC:
   52|  15.2M|int gdGetC(gdIOCtx *ctx) { return ((ctx->getC)(ctx)); }
gdGetBuf:
  189|   383k|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|    452|{
  200|    452|    IO_DBG(printf("Telling...\n"));
  201|    452|    return ((ctx->tell)(ctx));
  202|      0|    IO_DBG(printf("told.\n"));
  203|      0|}

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

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

gdImageCreateFromTgaPtr:
   57|    936|{
   58|    936|    gdImagePtr im;
   59|    936|    gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
   60|    936|    if (in == NULL)
  ------------------
  |  Branch (60:9): [True: 0, False: 936]
  ------------------
   61|      0|        return NULL;
   62|    936|    im = gdImageCreateFromTgaCtx(in);
   63|    936|    in->gd_free(in);
   64|    936|    return im;
   65|    936|}
gdImageCreateFromTgaCtx:
   76|    936|{
   77|    936|    int bitmap_caret = 0;
   78|    936|    oTga *tga = NULL;
   79|    936|    volatile gdImagePtr image = NULL;
   80|    936|    int x = 0;
   81|    936|    int y = 0;
   82|       |
   83|    936|    if (ctx == NULL) {
  ------------------
  |  Branch (83:9): [True: 0, False: 936]
  ------------------
   84|      0|        return NULL;
   85|      0|    }
   86|       |
   87|    936|    tga = (oTga *)gdMalloc(sizeof(oTga));
   88|    936|    if (!tga) {
  ------------------
  |  Branch (88:9): [True: 0, False: 936]
  ------------------
   89|      0|        return NULL;
   90|      0|    }
   91|       |
   92|    936|    tga->bitmap = NULL;
   93|    936|    tga->colormap = NULL;
   94|    936|    tga->ident = NULL;
   95|    936|    tga->has_alpha = 0;
   96|       |
   97|    936|    if (read_header_tga(ctx, tga) < 0) {
  ------------------
  |  Branch (97:9): [True: 117, False: 819]
  ------------------
   98|    117|        free_tga(tga);
   99|    117|        return NULL;
  100|    117|    }
  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: 8, False: 524]
  ------------------
  110|      8|        free_tga(tga);
  111|      8|        return NULL;
  112|      8|    }
  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|    524|    if (tga->has_alpha) {
  ------------------
  |  Branch (118:9): [True: 362, False: 162]
  ------------------
  119|    362|        gdImageAlphaBlending(image, 0);
  120|    362|        gdImageSaveAlpha(image, 1);
  121|    362|    }
  122|       |
  123|  85.9k|    for (y = 0; y < tga->height; y++) {
  ------------------
  |  Branch (123:17): [True: 85.3k, False: 524]
  ------------------
  124|  85.3k|        register int *tpix = image->tpixels[y];
  125|   405k|        for (x = 0; x < tga->width; x++, tpix++) {
  ------------------
  |  Branch (125:21): [True: 320k, False: 85.3k]
  ------------------
  126|   320k|            *tpix = tga->bitmap[bitmap_caret++];
  127|   320k|        }
  128|  85.3k|    }
  129|       |
  130|    524|    if (tga->flipv && tga->fliph) {
  ------------------
  |  Branch (130:9): [True: 350, False: 174]
  |  Branch (130:23): [True: 311, False: 39]
  ------------------
  131|    311|        gdImageFlipBoth(image);
  132|    311|    } else if (tga->flipv) {
  ------------------
  |  Branch (132:16): [True: 39, False: 174]
  ------------------
  133|     39|        gdImageFlipVertical(image);
  134|    174|    } else if (tga->fliph) {
  ------------------
  |  Branch (134:16): [True: 155, False: 19]
  ------------------
  135|    155|        gdImageFlipHorizontal(image);
  136|    155|    }
  137|       |
  138|    524|    free_tga(tga);
  139|       |
  140|    524|    return image;
  141|    532|}
read_header_tga:
  149|    936|{
  150|       |
  151|    936|    unsigned char header[18];
  152|       |
  153|    936|    if (gdGetBuf(header, sizeof(header), ctx) < 18) {
  ------------------
  |  Branch (153:9): [True: 11, False: 925]
  ------------------
  154|     11|        gd_error("fail to read header");
  155|     11|        return -1;
  156|     11|    }
  157|       |
  158|    925|    tga->identsize = header[0];
  159|    925|    tga->colormaptype = header[1];
  160|    925|    tga->imagetype = header[2];
  161|    925|    tga->colormapstart = header[3] + (header[4] << 8);
  162|    925|    tga->colormaplength = header[5] + (header[6] << 8);
  163|    925|    tga->colormapbits = header[7];
  164|    925|    tga->xstart = header[8] + (header[9] << 8);
  165|    925|    tga->ystart = header[10] + (header[11] << 8);
  166|    925|    tga->width = header[12] + (header[13] << 8);
  167|    925|    tga->height = header[14] + (header[15] << 8);
  168|    925|    tga->bits = header[16];
  169|    925|    tga->alphabits = header[17] & 0x0f;
  170|    925|    tga->fliph = (header[17] & 0x10) ? 1 : 0;
  ------------------
  |  Branch (170:18): [True: 639, False: 286]
  ------------------
  171|    925|    tga->flipv = (header[17] & 0x20) ? 0 : 1;
  ------------------
  |  Branch (171:18): [True: 325, False: 600]
  ------------------
  172|    925|    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|    925|    if (tga->width <= 0 || tga->height <= 0) {
  ------------------
  |  Branch (181:9): [True: 4, False: 921]
  |  Branch (181:28): [True: 2, False: 919]
  ------------------
  182|      6|        gd_error("gd-tga: invalid image dimensions\n");
  183|      6|        return -1;
  184|      6|    }
  185|       |
  186|    919|    if (tga->colormaptype > 1) {
  ------------------
  |  Branch (186:9): [True: 14, False: 905]
  ------------------
  187|     14|        gd_error_ex(GD_WARNING, "gd-tga: unsupported color map type %u\n", tga->colormaptype);
  ------------------
  |  |   38|     14|#define GD_WARNING LOG_WARNING
  ------------------
  188|     14|        return -1;
  189|     14|    }
  190|       |
  191|    905|    if (tga->colormaptype == 1 &&
  ------------------
  |  Branch (191:9): [True: 146, False: 759]
  ------------------
  192|    146|        !(tga->colormapbits == 15 || tga->colormapbits == 16 || tga->colormapbits == 24 ||
  ------------------
  |  Branch (192:11): [True: 24, False: 122]
  |  Branch (192:38): [True: 75, False: 47]
  |  Branch (192:65): [True: 12, False: 35]
  ------------------
  193|     35|          tga->colormapbits == 32)) {
  ------------------
  |  Branch (193:11): [True: 12, False: 23]
  ------------------
  194|     23|        gd_error_ex(GD_WARNING, "gd-tga: unsupported color map entry depth %u\n", tga->colormapbits);
  ------------------
  |  |   38|     23|#define GD_WARNING LOG_WARNING
  ------------------
  195|     23|        return -1;
  196|     23|    }
  197|       |
  198|    882|    switch (tga->imagetype) {
  199|     82|    case TGA_TYPE_INDEXED:
  ------------------
  |  |   35|     82|#define TGA_TYPE_INDEXED 1
  ------------------
  |  Branch (199:5): [True: 82, False: 800]
  ------------------
  200|    112|    case TGA_TYPE_INDEXED_RLE:
  ------------------
  |  |   38|    112|#define TGA_TYPE_INDEXED_RLE 9
  ------------------
  |  Branch (200:5): [True: 30, False: 852]
  ------------------
  201|    112|        if (tga->colormaptype != 1 || tga->bits != TGA_BPP_8 ||
  ------------------
  |  |   44|    223|#define TGA_BPP_8 8
  ------------------
  |  Branch (201:13): [True: 1, False: 111]
  |  Branch (201:39): [True: 8, False: 103]
  ------------------
  202|    103|            !(tga->colormapbits == 15 || tga->colormapbits == 16 || tga->colormapbits == 24 ||
  ------------------
  |  Branch (202:15): [True: 18, False: 85]
  |  Branch (202:42): [True: 68, False: 17]
  |  Branch (202:69): [True: 9, False: 8]
  ------------------
  203|      9|              tga->colormapbits == 32)) {
  ------------------
  |  Branch (203:15): [True: 8, False: 0]
  ------------------
  204|      9|            gd_error_ex(GD_WARNING, "gd-tga: unsupported color mapped image\n");
  ------------------
  |  |   38|      9|#define GD_WARNING LOG_WARNING
  ------------------
  205|      9|            return -1;
  206|      9|        }
  207|    103|        break;
  208|       |
  209|    365|    case TGA_TYPE_RGB:
  ------------------
  |  |   36|    365|#define TGA_TYPE_RGB 2
  ------------------
  |  Branch (209:5): [True: 365, False: 517]
  ------------------
  210|    653|    case TGA_TYPE_RGB_RLE:
  ------------------
  |  |   39|    653|#define TGA_TYPE_RGB_RLE 10
  ------------------
  |  Branch (210:5): [True: 288, False: 594]
  ------------------
  211|    653|        if (!(tga->bits == TGA_BPP_16 || tga->bits == TGA_BPP_24 ||
  ------------------
  |  |   45|  1.30k|#define TGA_BPP_16 16
  ------------------
                      if (!(tga->bits == TGA_BPP_16 || tga->bits == TGA_BPP_24 ||
  ------------------
  |  |   46|    783|#define TGA_BPP_24 24
  ------------------
  |  Branch (211:15): [True: 523, False: 130]
  |  Branch (211:42): [True: 66, False: 64]
  ------------------
  212|     64|              (tga->bits == TGA_BPP_32 && tga->alphabits == 8))) {
  ------------------
  |  |   47|    128|#define TGA_BPP_32 32
  ------------------
  |  Branch (212:16): [True: 46, False: 18]
  |  Branch (212:43): [True: 40, False: 6]
  ------------------
  213|     24|            gd_error_ex(GD_WARNING, "gd-tga: unsupported truecolor depth %u\n", tga->bits);
  ------------------
  |  |   38|     24|#define GD_WARNING LOG_WARNING
  ------------------
  214|     24|            return -1;
  215|     24|        }
  216|    629|        break;
  217|       |
  218|    629|    case TGA_TYPE_GREYSCALE:
  ------------------
  |  |   37|     45|#define TGA_TYPE_GREYSCALE 3
  ------------------
  |  Branch (218:5): [True: 45, False: 837]
  ------------------
  219|    116|    case TGA_TYPE_GREYSCALE_RLE:
  ------------------
  |  |   40|    116|#define TGA_TYPE_GREYSCALE_RLE 11
  ------------------
  |  Branch (219:5): [True: 71, False: 811]
  ------------------
  220|    116|        if (tga->bits != TGA_BPP_8) {
  ------------------
  |  |   44|    116|#define TGA_BPP_8 8
  ------------------
  |  Branch (220:13): [True: 9, False: 107]
  ------------------
  221|      9|            gd_error_ex(GD_WARNING, "gd-tga: unsupported grayscale depth %u\n", tga->bits);
  ------------------
  |  |   38|      9|#define GD_WARNING LOG_WARNING
  ------------------
  222|      9|            return -1;
  223|      9|        }
  224|    107|        break;
  225|       |
  226|    107|    default:
  ------------------
  |  Branch (226:5): [True: 1, False: 881]
  ------------------
  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|    882|    }
  230|       |
  231|    839|    tga->ident = NULL;
  232|       |
  233|    839|    if (tga->identsize > 0) {
  ------------------
  |  Branch (233:9): [True: 51, False: 788]
  ------------------
  234|     51|        tga->ident = (char *)gdMalloc(tga->identsize * sizeof(char));
  235|     51|        if (tga->ident == NULL) {
  ------------------
  |  Branch (235:13): [True: 0, False: 51]
  ------------------
  236|      0|            return -1;
  237|      0|        }
  238|       |
  239|     51|        if (gdGetBuf(tga->ident, tga->identsize, ctx) != tga->identsize) {
  ------------------
  |  Branch (239:13): [True: 20, False: 31]
  ------------------
  240|     20|            gd_error("fail to read header ident");
  241|     20|            return -1;
  242|     20|        }
  243|     51|    }
  244|       |
  245|    819|    return 1;
  246|    839|}
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: 16, False: 803]
  ------------------
  259|     16|        return -1;
  260|     16|    }
  261|       |
  262|    803|    pixel_count = tga->width * tga->height;
  263|    803|    if (overflow2(pixel_count, sizeof(int))) {
  ------------------
  |  Branch (263:9): [True: 6, False: 797]
  ------------------
  264|      6|        return -1;
  265|      6|    }
  266|       |
  267|    797|    if (tga_read_color_map(ctx, tga) < 0) {
  ------------------
  |  Branch (267:9): [True: 42, False: 755]
  ------------------
  268|     42|        return -1;
  269|     42|    }
  270|       |
  271|       |    /*!	\brief Allocate memory for image block
  272|       |     *  Allocate a chunk of memory for the image block to be passed into.
  273|       |     */
  274|    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: 335, False: 420]
  ------------------
  279|  59.1k|        while (bitmap_caret < pixel_count) {
  ------------------
  |  Branch (279:16): [True: 58.9k, False: 219]
  ------------------
  280|  58.9k|            int packet = gdGetC(ctx);
  281|  58.9k|            int encoded_pixels;
  282|  58.9k|            int pixel;
  283|  58.9k|            int i;
  284|       |
  285|  58.9k|            if (packet == EOF) {
  ------------------
  |  Branch (285:17): [True: 40, False: 58.8k]
  ------------------
  286|     40|                gd_error("gd-tga: premature end of image data\n");
  287|     40|                return -1;
  288|     40|            }
  289|       |
  290|  58.8k|            encoded_pixels = (packet & ~TGA_RLE_FLAG) + 1;
  ------------------
  |  |   49|  58.8k|#define TGA_RLE_FLAG 128
  ------------------
  291|  58.8k|            if (encoded_pixels > pixel_count - bitmap_caret) {
  ------------------
  |  Branch (291:17): [True: 11, False: 58.8k]
  ------------------
  292|     11|                return -1;
  293|     11|            }
  294|       |
  295|  58.8k|            if ((packet & TGA_RLE_FLAG) == TGA_RLE_FLAG) {
  ------------------
  |  |   49|  58.8k|#define TGA_RLE_FLAG 128
  ------------------
                          if ((packet & TGA_RLE_FLAG) == TGA_RLE_FLAG) {
  ------------------
  |  |   49|  58.8k|#define TGA_RLE_FLAG 128
  ------------------
  |  Branch (295:17): [True: 3.12k, False: 55.7k]
  ------------------
  296|  3.12k|                if (tga_read_pixel(ctx, tga, &pixel) < 0) {
  ------------------
  |  Branch (296:21): [True: 9, False: 3.12k]
  ------------------
  297|      9|                    return -1;
  298|      9|                }
  299|   343k|                for (i = 0; i < encoded_pixels; i++) {
  ------------------
  |  Branch (299:29): [True: 340k, False: 3.12k]
  ------------------
  300|   340k|                    tga->bitmap[bitmap_caret++] = pixel;
  301|   340k|                }
  302|  55.7k|            } else {
  303|   200k|                for (i = 0; i < encoded_pixels; i++) {
  ------------------
  |  Branch (303:29): [True: 144k, False: 55.6k]
  ------------------
  304|   144k|                    if (tga_read_pixel(ctx, tga, &pixel) < 0) {
  ------------------
  |  Branch (304:25): [True: 56, False: 144k]
  ------------------
  305|     56|                        return -1;
  306|     56|                    }
  307|   144k|                    tga->bitmap[bitmap_caret++] = pixel;
  308|   144k|                }
  309|  55.7k|            }
  310|  58.8k|        }
  311|    420|    } else {
  312|   176k|        while (bitmap_caret < pixel_count) {
  ------------------
  |  Branch (312:16): [True: 176k, False: 313]
  ------------------
  313|   176k|            if (tga_read_pixel(ctx, tga, &tga->bitmap[bitmap_caret]) < 0) {
  ------------------
  |  Branch (313:17): [True: 107, False: 176k]
  ------------------
  314|    107|                return -1;
  315|    107|            }
  316|   176k|            bitmap_caret++;
  317|   176k|        }
  318|    420|    }
  319|       |
  320|    532|    tga_apply_attribute_type(ctx, tga, pixel_count);
  321|       |
  322|    532|    return 1;
  323|    755|}
free_tga:
  554|    936|{
  555|    936|    if (tga) {
  ------------------
  |  Branch (555:9): [True: 936, False: 0]
  ------------------
  556|    936|        if (tga->ident)
  ------------------
  |  Branch (556:13): [True: 51, False: 885]
  ------------------
  557|     51|            gdFree(tga->ident);
  558|    936|        if (tga->bitmap)
  ------------------
  |  Branch (558:13): [True: 755, False: 181]
  ------------------
  559|    755|            gdFree(tga->bitmap);
  560|    936|        if (tga->colormap)
  ------------------
  |  Branch (560:13): [True: 103, False: 833]
  ------------------
  561|    103|            gdFree(tga->colormap);
  562|    936|        gdFree(tga);
  563|    936|    }
  564|    936|}
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: 14, False: 741]
  |  Branch (327:49): [True: 258, False: 483]
  ------------------
  328|    483|           imagetype == TGA_TYPE_GREYSCALE_RLE;
  ------------------
  |  |   40|    483|#define TGA_TYPE_GREYSCALE_RLE 11
  ------------------
  |  Branch (328:12): [True: 63, False: 420]
  ------------------
  329|    755|}
gd_tga.c:tga_read_color_map:
  334|    797|{
  335|    797|    int i;
  336|    797|    int has_alpha = 0;
  337|    797|    int entry_size;
  338|       |
  339|    797|    if (tga->colormaptype == 0) {
  ------------------
  |  Branch (339:9): [True: 687, False: 110]
  ------------------
  340|    687|        return 1;
  341|    687|    }
  342|       |
  343|    110|    if (tga->colormaplength <= 0) {
  ------------------
  |  Branch (343:9): [True: 1, False: 109]
  ------------------
  344|      1|        return -1;
  345|      1|    }
  346|       |
  347|    109|    if (overflow2(tga->colormaplength, sizeof(int))) {
  ------------------
  |  Branch (347:9): [True: 6, False: 103]
  ------------------
  348|      6|        return -1;
  349|      6|    }
  350|       |
  351|    103|    entry_size = tga_pixel_size(tga->colormapbits);
  352|    103|    tga->colormap = (int *)gdMalloc(tga->colormaplength * sizeof(int));
  353|    103|    if (tga->colormap == NULL) {
  ------------------
  |  Branch (353:9): [True: 0, False: 103]
  ------------------
  354|      0|        return -1;
  355|      0|    }
  356|       |
  357|  58.4k|    for (i = 0; i < tga->colormaplength; i++) {
  ------------------
  |  Branch (357:17): [True: 58.4k, False: 68]
  ------------------
  358|  58.4k|        unsigned char buf[4] = {0, 0, 0, 0};
  359|       |
  360|  58.4k|        if (gdGetBuf(buf, entry_size, ctx) != entry_size) {
  ------------------
  |  Branch (360:13): [True: 35, False: 58.3k]
  ------------------
  361|     35|            gd_error("gd-tga: premature end of color map data\n");
  362|     35|            return -1;
  363|     35|        }
  364|       |
  365|  58.3k|        tga->colormap[i] =
  366|  58.3k|            tga_decode_color(buf, tga->colormapbits, tga->colormapbits == 32 ? 8 : 0, &has_alpha);
  ------------------
  |  Branch (366:54): [True: 1.20k, False: 57.1k]
  ------------------
  367|  58.3k|    }
  368|       |
  369|     68|    if (has_alpha) {
  ------------------
  |  Branch (369:9): [True: 3, False: 65]
  ------------------
  370|      3|        tga->has_alpha = 1;
  371|      3|    }
  372|       |
  373|     68|    return 1;
  374|    103|}
gd_tga.c:tga_pixel_size:
  331|   324k|static int tga_pixel_size(uint8_t bits) { return (bits + 7) / 8; }
gd_tga.c:tga_decode_color:
  494|   378k|{
  495|   378k|    switch (bits) {
  ------------------
  |  Branch (495:13): [True: 378k, False: 0]
  ------------------
  496|  4.32k|    case 15:
  ------------------
  |  Branch (496:5): [True: 4.32k, False: 374k]
  ------------------
  497|   235k|    case 16:
  ------------------
  |  Branch (497:5): [True: 231k, False: 147k]
  ------------------
  498|   235k|        return tga_decode_16((unsigned int)buf[0] | ((unsigned int)buf[1] << 8), alpha_bits,
  499|   235k|                             has_alpha);
  500|       |
  501|  71.9k|    case 24:
  ------------------
  |  Branch (501:5): [True: 71.9k, False: 307k]
  ------------------
  502|  71.9k|        return gdTrueColor(buf[2], buf[1], buf[0]);
  ------------------
  |  | 5682|  71.9k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  503|       |
  504|  71.1k|    case 32:
  ------------------
  |  Branch (504:5): [True: 71.1k, False: 307k]
  ------------------
  505|  71.1k|        if (has_alpha) {
  ------------------
  |  Branch (505:13): [True: 71.1k, False: 0]
  ------------------
  506|  71.1k|            *has_alpha = 1;
  507|  71.1k|        }
  508|  71.1k|        return gdTrueColorAlpha(buf[2], buf[1], buf[0], tga_alpha_8_to_gd(buf[3]));
  ------------------
  |  | 5699|  71.1k|#define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
  ------------------
  509|   378k|    }
  510|       |
  511|      0|    return 0;
  512|   378k|}
gd_tga.c:tga_decode_16:
  515|   235k|{
  516|   235k|    int b = tga_scale_5_to_8(value & 0x1f);
  517|   235k|    int g = tga_scale_5_to_8((value >> 5) & 0x1f);
  518|   235k|    int r = tga_scale_5_to_8((value >> 10) & 0x1f);
  519|       |
  520|   235k|    if (alpha_bits > 0) {
  ------------------
  |  Branch (520:9): [True: 199k, False: 36.0k]
  ------------------
  521|   199k|        int a = (value & 0x8000) ? gdAlphaOpaque : gdAlphaTransparent;
  ------------------
  |  |  141|  65.9k|#define gdAlphaOpaque 0
  ------------------
                      int a = (value & 0x8000) ? gdAlphaOpaque : gdAlphaTransparent;
  ------------------
  |  |  142|   133k|#define gdAlphaTransparent 127
  ------------------
  |  Branch (521:17): [True: 65.9k, False: 133k]
  ------------------
  522|       |
  523|   199k|        if (has_alpha) {
  ------------------
  |  Branch (523:13): [True: 199k, False: 0]
  ------------------
  524|   199k|            *has_alpha = 1;
  525|   199k|        }
  526|   199k|        return gdTrueColorAlpha(r, g, b, a);
  ------------------
  |  | 5699|   199k|#define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
  ------------------
  527|   199k|    }
  528|       |
  529|  36.0k|    return gdTrueColor(r, g, b);
  ------------------
  |  | 5682|  36.0k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  530|   235k|}
gd_tga.c:tga_scale_5_to_8:
  532|   707k|static int tga_scale_5_to_8(int c) { return (c * 255) / 31; }
gd_tga.c:tga_alpha_8_to_gd:
  534|  71.1k|static int tga_alpha_8_to_gd(int a) { return gdAlphaMax - (a >> 1); }
  ------------------
  |  |  140|  71.1k|#define gdAlphaMax 127
  ------------------
gd_tga.c:tga_read_pixel:
  377|   324k|{
  378|   324k|    unsigned char buf[4] = {0, 0, 0, 0};
  379|   324k|    int size = tga_pixel_size(tga->bits);
  380|   324k|    int has_alpha = 0;
  381|       |
  382|   324k|    if (gdGetBuf(buf, size, ctx) != size) {
  ------------------
  |  Branch (382:9): [True: 135, False: 323k]
  ------------------
  383|    135|        gd_error("gd-tga: premature end of image data\n");
  384|    135|        return -1;
  385|    135|    }
  386|       |
  387|   323k|    switch (tga->imagetype) {
  388|    327|    case TGA_TYPE_INDEXED:
  ------------------
  |  |   35|    327|#define TGA_TYPE_INDEXED 1
  ------------------
  |  Branch (388:5): [True: 327, False: 323k]
  ------------------
  389|    583|    case TGA_TYPE_INDEXED_RLE: {
  ------------------
  |  |   38|    583|#define TGA_TYPE_INDEXED_RLE 9
  ------------------
  |  Branch (389:5): [True: 256, False: 323k]
  ------------------
  390|    583|        int index = buf[0] - tga->colormapstart;
  391|       |
  392|    583|        if (index < 0 || index >= tga->colormaplength || tga->colormap == NULL) {
  ------------------
  |  Branch (392:13): [True: 21, False: 562]
  |  Branch (392:26): [True: 16, False: 546]
  |  Branch (392:58): [True: 0, False: 546]
  ------------------
  393|     37|            return -1;
  394|     37|        }
  395|    546|        *pixel = tga->colormap[index];
  396|    546|        break;
  397|    583|    }
  398|       |
  399|   175k|    case TGA_TYPE_RGB:
  ------------------
  |  |   36|   175k|#define TGA_TYPE_RGB 2
  ------------------
  |  Branch (399:5): [True: 175k, False: 148k]
  ------------------
  400|   320k|    case TGA_TYPE_RGB_RLE:
  ------------------
  |  |   39|   320k|#define TGA_TYPE_RGB_RLE 10
  ------------------
  |  Branch (400:5): [True: 144k, False: 179k]
  ------------------
  401|   320k|        *pixel = tga_decode_color(buf, tga->bits, tga->alphabits, &has_alpha);
  402|   320k|        if (has_alpha) {
  ------------------
  |  Branch (402:13): [True: 269k, False: 50.8k]
  ------------------
  403|   269k|            tga->has_alpha = 1;
  404|   269k|        }
  405|   320k|        break;
  406|       |
  407|    616|    case TGA_TYPE_GREYSCALE:
  ------------------
  |  |   37|    616|#define TGA_TYPE_GREYSCALE 3
  ------------------
  |  Branch (407:5): [True: 616, False: 323k]
  ------------------
  408|  2.78k|    case TGA_TYPE_GREYSCALE_RLE:
  ------------------
  |  |   40|  2.78k|#define TGA_TYPE_GREYSCALE_RLE 11
  ------------------
  |  Branch (408:5): [True: 2.16k, False: 321k]
  ------------------
  409|  2.78k|        *pixel = gdTrueColor(buf[0], buf[0], buf[0]);
  ------------------
  |  | 5682|  2.78k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  410|  2.78k|        break;
  411|       |
  412|      0|    default:
  ------------------
  |  Branch (412:5): [True: 0, False: 323k]
  ------------------
  413|      0|        return -1;
  414|   323k|    }
  415|       |
  416|   323k|    return 1;
  417|   323k|}
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: 80, False: 452]
  ------------------
  435|     80|        return;
  436|     80|    }
  437|       |
  438|    452|    if (ctx->tell == NULL || ctx->seek == NULL) {
  ------------------
  |  Branch (438:9): [True: 0, False: 452]
  |  Branch (438:30): [True: 0, False: 452]
  ------------------
  439|      0|        return;
  440|      0|    }
  441|       |
  442|    452|    start = gdTell(ctx);
  443|    452|    if (start < 0 || start > INT_MAX) {
  ------------------
  |  Branch (443:9): [True: 0, False: 452]
  |  Branch (443:22): [True: 0, False: 452]
  ------------------
  444|      0|        return;
  445|      0|    }
  446|       |
  447|  15.2M|    while ((c = gdGetC(ctx)) != EOF) {
  ------------------
  |  Branch (447:12): [True: 15.2M, False: 452]
  ------------------
  448|  15.2M|        footer_ring[trailing_size % sizeof(footer_ring)] = (unsigned char)c;
  449|  15.2M|        if (trailing_size == SIZE_MAX) {
  ------------------
  |  Branch (449:13): [True: 0, False: 15.2M]
  ------------------
  450|      0|            return;
  451|      0|        }
  452|  15.2M|        trailing_size++;
  453|  15.2M|    }
  454|       |
  455|    452|    if (trailing_size < sizeof(footer)) {
  ------------------
  |  Branch (455:9): [True: 103, False: 349]
  ------------------
  456|    103|        return;
  457|    103|    }
  458|       |
  459|  9.42k|    for (i = 0; i < sizeof(footer); i++) {
  ------------------
  |  Branch (459:17): [True: 9.07k, False: 349]
  ------------------
  460|  9.07k|        footer[i] = footer_ring[(trailing_size + i) % sizeof(footer_ring)];
  461|  9.07k|    }
  462|    349|    if (memcmp(footer + 8, signature, sizeof(signature)) != 0) {
  ------------------
  |  Branch (462:9): [True: 140, False: 209]
  ------------------
  463|    140|        return;
  464|    140|    }
  465|       |
  466|    209|    extension_offset = (uint32_t)footer[0] | ((uint32_t)footer[1] << 8) |
  467|    209|                       ((uint32_t)footer[2] << 16) | ((uint32_t)footer[3] << 24);
  468|    209|    if (extension_offset == 0 || extension_offset < (uint32_t)start ||
  ------------------
  |  Branch (468:9): [True: 1, False: 208]
  |  Branch (468:34): [True: 1, False: 207]
  ------------------
  469|    207|        extension_offset > (uint32_t)INT_MAX - 494) {
  ------------------
  |  Branch (469:9): [True: 38, False: 169]
  ------------------
  470|     40|        return;
  471|     40|    }
  472|       |
  473|    169|    attr_type_offset = extension_offset + 494;
  474|    169|    if (trailing_size > (size_t)INT_MAX - (size_t)start ||
  ------------------
  |  Branch (474:9): [True: 0, False: 169]
  ------------------
  475|    169|        attr_type_offset >= (uint32_t)start + (uint32_t)trailing_size - sizeof(footer)) {
  ------------------
  |  Branch (475:9): [True: 81, False: 88]
  ------------------
  476|     81|        return;
  477|     81|    }
  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: 87, False: 1]
  |  Branch (488:27): [True: 85, False: 2]
  ------------------
  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|  11.9k|    for (i = 0; i < pixel_count; i++) {
  ------------------
  |  Branch (540:17): [True: 11.8k, False: 85]
  ------------------
  541|  11.8k|        int pixel = tga->bitmap[i];
  542|       |
  543|  11.8k|        tga->bitmap[i] = gdTrueColor(gdTrueColorGetRed(pixel), gdTrueColorGetGreen(pixel),
  ------------------
  |  | 5682|  11.8k|#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  ------------------
  544|  11.8k|                                     gdTrueColorGetBlue(pixel));
  545|  11.8k|    }
  546|     85|    tga->has_alpha = 0;
  547|     85|}

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

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

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

