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

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

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

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

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

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

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

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

