0

I'm getting a segmentation fault and tbh have no idea why. I run valgrind and it all seems good, no memory leak. If I run debug50 the code just switches off halfway through execution on line 43 giving me a segmentation fault. I'd appreciate any advice or explanation as to why this might be happening.

my code: https://gist.github.com/Zuzeek/3de4aae4acf43020260b5e2e00d8bab2

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        fprintf(stderr, "Usage: ./resize n infile outfile\n");
        return 1;
    }

    // 'n' -> a user given number and scale factor
    int n = atoi(argv[1]);

    // 'n' must be a positive int <= 100
    if (n < 0 || n > 100)
    {
        printf("Usage: Resize value must be positive less than or equal to 100.");
        return 1;
    }

    // remember filenames
    char *infile = argv[2];
    char *outfile = argv[-1];

    // open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }

    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf, bfOut;
    fread(&bf , sizeof(BITMAPFILEHEADER), 1, inptr);
    // set outfile BITMAPFILEHEADER for manipulation
    bfOut = bf;

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi, biOut;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    // set outfile BITMAPINFOHEADER for manipulation
    biOut = bi;

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
        bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    // update header info for outfile resized by the value 'n'
    biOut.biWidth = bi.biWidth * n;
    biOut.biHeight = bi.biHeight * n;

    // determine padding for scanlines
    // infile
    int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    // outfile
    int outPadding = (4 - (biOut.biWidth *sizeof(RGBTRIPLE)) % 4) % 4;

    // update header info for outfile resized by the value 'n'
    biOut.biSizeImage = ((sizeof(RGBTRIPLE) * biOut.biWidth) + outPadding) * abs(biOut.biHeight);
    bfOut.bfSize = biOut.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

    // write those pixels into outfile's BITMAPFILEHEADER
    fwrite(&bfOut, sizeof(BITMAPFILEHEADER), 1, outptr);
    fwrite(&biOut, sizeof(BITMAPINFOHEADER), 1, outptr);

    // resize horizontally & vertically
    // iterate over infile's scanlines (each row)
    for (int i = 0, biHeight = abs(biOut.biHeight); i < biHeight; i++)
    {
        // repeat 'n' times
        for (int j = 0; j < n; j++)
        {
            // resize horizontally
            // iterate over pixels in scanline
            for (int k = 0; k < bi.biWidth; k++)
            {
                // temporary storage
                RGBTRIPLE triple;

                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                // write RGB triple(pixels) to outfile 'n' times
                for (int l = 0; l < n; l++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }

            // write padding to outfile
            for (int m = 0; m < outPadding; m++)
            {
                fputc(0x00, outptr);
            }

            // resize vertically using recopy method
            if (j < n - 1)
            {
                fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);
            }
        }

        // skip over infile's padding, if any
        fseek(inptr, padding, SEEK_CUR);
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
1
  • ok, I found the cause of my seg fault char outfile = argv[-1]; I've changed it back to *argv[3] the program runs, but the resized image has too many bytes.. I've gone through it thousands of times and can't see what's wrong. Does anyone have an idea please? – Ania Jan 31 '19 at 15:48
1

You answered your own original question. Your second question

[...] the resized image has too many bytes.. [...]

is in the structure of your loops.

    for (int i = 0, biHeight = abs(biOut.biHeight); i < biHeight; i++)
    {
        // code here is run once per output row
        for (int j = 0; j < n; j++)
        {
            // code here is run n times per output row

The inner of those two loops should run n times per input row, which totals to once per output row. Use input, not output height.

1
  • thanks @Blauelf. It took me a while to understand what you meant, but when I did, the program resized bmp's correctly :) – Ania Jan 31 '19 at 17:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .