0

not sure what is wrong with code, but it works for smiley and not for small

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

    if (n > 100 || n <= 0)
    {
        printf("Usage: ./resize n infile outfile\n");
        return 1;
    }

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

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

    //new bf & bi info
    //BITMAPFILEHEADER bf_old = bf;
    BITMAPINFOHEADER bi_old = bi;

    int padding_old =  (4 - (bi_old.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    //new width & height
    bi.biWidth = bi.biWidth * n;
    bi.biHeight = bi.biHeight * n;

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

    //new size of image
    bi.biSizeImage = bi.biWidth * sizeof(RGBTRIPLE) * abs(bi.biHeight) + padding * n;
    bf.bfSize = bi.biSizeImage + sizeof(bf);

    // 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, oldbiHeight = abs(bi_old.biHeight); i < oldbiHeight; i++)
    {
        for (int l = 0; l < n; l++)
        {
            // iterate over pixels in scanline
            for (int j = 0; j < bi_old.biWidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;

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

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

            // then add it back (to demonstrate how)
            for (int k = 0; k < padding; k++)
            {
                fputc(0x00, outptr);
            }
            fseek(inptr, -(bi_old.biWidth * 3 + padding_old), SEEK_CUR);

        }
        fseek(inptr, (bi_old.biWidth * 3 + padding_old), SEEK_CUR);
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

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

You have some issues with both the header and the image data. While it would be easy to point them out, it's more important to learn how to find them.

Try starting with ./resize 1 small.bmp s2.bmp. The output file should be identical to the input file. First, check the header data, using the peek tool. Fix any errors. Then, once the header is right, move on to the image data and compare using the xxd tool. Once everything works for resize of 1, try 2, repeating the same process. This should get you going.

If you have specific questions after identifying the problems, please feel free to post a new question.

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

3
  • Thnks for help, I fixed header files but it still not working with 2, any advice on how to use xxd? I mean I print out hex values, but I got no idea how to deal with this information. – Garve Worm Sep 13 '16 at 9:16
  • The pset instructions detail how to use xxd. You might want to modify the parameters to display the image data, using the horizontal width plus padding. Use the output from the staff example to compare your output against. Look for patterns in the image data of your output. Is each line off by a certain amount? Are there other patterns in how it differs from what it should be? – Cliff B Sep 13 '16 at 9:37
  • Thanks, now it works correctly, the probllem was with skipping padding and fseek – Garve Worm Sep 13 '16 at 9:53

You must log in to answer this question.

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