0

I'm having a rough go trying to get the following code for pset4 resize to compile. I get a massive error message as follows:

~/workspace/pset4/bmp/ $ make resize
clang -ggdb3 -O0 -std=c11 -Wall -Werror -o resize resize.c -lcs50 -lm
0  libLLVM-3.6.so.1 0x00002b618899f912 llvm::sys::PrintStackTrace(_IO_FILE*) + 34
1  libLLVM-3.6.so.1 0x00002b618899deb1
2  libpthread.so.0  0x00002b6189696330
3  libpthread.so.0  0x0000000002372750
Stack dump:
0.      Program arguments: /usr/lib/llvm-3.6/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name resize.c -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-linker-version 2.24 -g -dwarf-column-info -resource-dir /usr/lib/llvm-3.6/bin/../lib/clang/3.6.0 -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-3.6/bin/../lib/clang/3.6.0/include -internal-externc-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wall -Werror -std=c11 -fdebug-compilation-dir /home/ubuntu/workspace/pset4/bmp -ferror-limit 19 -fmessage-length 155 -mstackrealign -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/resize-086bef.o -x c resize.c 
1.      resize.c:85:84: current parser token ')'
2.      resize.c:16:1: parsing function body 'main'
3.      resize.c:16:1: in compound statement ('{}')
clang: error: unable to execute command: Segmentation fault
clang: error: clang frontend command failed due to signal (use -v to see invocation)
Ubuntu clang version 3.6.0-2ubuntu1~trusty1 (tags/RELEASE_360/final) (based on LLVM 3.6.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
clang: note: diagnostic msg: PLEASE submit a bug report to http://bugs.debian.org/ and include the crash backtrace, preprocessed source, and associated run script.
clang: note: diagnostic msg: 

I'm assuming it's an issue with insufficient memory allocation when writing RGB triples? Any help here would be much appreciated.

#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

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

// remember filenames
int n = atoi(argv[1]);
char* infile = argv[2];
char* outfile = argv[3];

// checks n for proper int
if(n < 0 || n > 100)
{
    fprintf(stderr, "Unsupported n for resize. Must be an int between 1 and 100.\n");
    return 5;
}


// open input file 
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("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;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

// 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;
}

// create bitmap headers for outptr
int origHeight = bi.biHeight;
int origWidth = bi.biWidth;

//inject new data with regard to n
bi.biWidth = (bi.biWidth)*n;
bi.biHeight = (bi.biHeight)*n;

// determine padding for scanlines
int in_padding =  (4 - (origWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int out_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// determine file and image size
bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + out_padding) * abs(bi.Height);
bf.bfSize = 52 + bi.biSizeImage;


// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

// iterate over infile's scanlines
for (int i = 0; i < origHeight; i++)
    {
    // write each scanline n times
    for (int i = 0; i < n; i++)
        {
                // iterate over pixels in scanline
                for (int j = 0; j < origWidth; j++)
                    {
                    // temporary storage
                    RGBTRIPLE triple;

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

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

                // skip over padding, if any
                fseek(inptr, in_padding, SEEK_CUR);

                // then add correct padding to outfile
                for (int k = 0; k < out_padding; k++)
                    {
                        fputc(0x00, outptr);
                    }
        }
    }

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;
}
0

Actually, this is not a seg fault. It's a compiler error. A seg fault is a runtime error.

It's unfortunate that the compiler isn't giving you a proper error. At least, it told you where to look: 1. resize.c:76:80: current parser token ')'

If you look carefully at that line, you should find an error, probably a typo or just a little carelessness. (We all do it sooner or later. ;-) )

bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + out_padding) * abs(bi.Height);

You may want to report the bug as a problem handling structures, per the compiler output message.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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