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

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

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

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

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

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

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

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

