Skip to main content
added 1 character in body
Source Link
MARS
  • 5.2k
  • 3
  • 14
  • 23
     /**
     * resize.c
     *
     * Computer Science 50
     * Problem Set 4
     *
     * Resizes a 24-bit uncompressed bmp by a factor of n
     */
           
    #include <stdio.h>
    #include <stdlib.h>

    #include "bmp.h"

    int main(int argc, char* argv[])
    {
        // ensure proper usage
        if (argc != 4 || atoi(argv[1]) > 100 || atoi(argv[1]) < 1)
        {
            printf("Usage: ./resize n infile outfile, where n is greater than 0 and less than or equal to 100\n");
            return 1;
        }
        
        // remember filenames
        char* infile = argv[2];
        char* outfile = argv[3];
        int n = atoi(argv[1]);

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

        int inPadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // Calculates input padding before rewriting header files so I can use it to scan accurately over input file
        
       
        // resize BITMAPFILEHEADER
        bi.biHeight = bi.biHeight * n; // Multiply the height by n
        bi.biWidth = bi.biWidth * n; //  Multiplies the width by n

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

       // Determine the SizeImage after the padding is set which requires biWidth be set; normally SizeImage would be right after width
        bi.biSizeImage = bi.biWidth * abs(bi.biHeight) * sizeof(RGBTRIPLE) + padding * abs(bi.biHeight);    
        bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi.biSizeImage; // concentrates just on the size of the bitmap, subtracting out the headers, multiplies by n to expand number of pixels, multiples by the number of bytes per pixel (the number of bytes per pixel)
       
        // 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, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            for (int m = 0; m < n; m++)
            {
                // iterate over pixels in scanline
                for (int j = 0; j < bi.biWidth/n; j++)
                {
                    // temporary storage
                    RGBTRIPLE triple;

                    // read RGB triple from infile
                    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                    
                    for (int l = 0; l < n; l++)
                    {
                        // write RGB triple to outfile
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    }
                }      
                fseek(inptr, inPadding, SEEK_CUR);
                
                for (int k = 0; k < padding; k++) // Adds padding to each row
                {
                    fputc(0x00, outptr);
                }
                
                if(m < (n-1))
                {
                    fseek(inptr, -(bi.biWidth/n * sizeof(RGBTRIPLE) + inPadding), SEEK_CUR); // Resets the INPUT file selector location to the beginning of the row
                }
            }
        }

        // close infile
        fclose(inptr);

        // close outfile
        fclose(outptr);

        // that's all folks
        return 0;
    } 
     /**
     * resize.c
     *
     * Computer Science 50
     * Problem Set 4
     *
     * Resizes a 24-bit uncompressed bmp by a factor of n
     */
           
    #include <stdio.h>
    #include <stdlib.h>

    #include "bmp.h"

    int main(int argc, char* argv[])
    {
        // ensure proper usage
        if (argc != 4 || atoi(argv[1]) > 100 || atoi(argv[1]) < 1)
        {
            printf("Usage: ./resize n infile outfile, where n is greater than 0 and less than or equal to 100\n");
            return 1;
        }
        
        // remember filenames
        char* infile = argv[2];
        char* outfile = argv[3];
        int n = atoi(argv[1]);

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

        int inPadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // Calculates input padding before rewriting header files so I can use it to scan accurately over input file
        
       
        // resize BITMAPFILEHEADER
        bi.biHeight = bi.biHeight * n; // Multiply the height by n
        bi.biWidth = bi.biWidth * n; //  Multiplies the width by n

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

       // Determine the SizeImage after the padding is set which requires biWidth be set; normally SizeImage would be right after width
        bi.biSizeImage = bi.biWidth * abs(bi.biHeight) * sizeof(RGBTRIPLE) + padding * abs(bi.biHeight);    
        bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi.biSizeImage; // concentrates just on the size of the bitmap, subtracting out the headers, multiplies by n to expand number of pixels, multiples by the number of bytes per pixel (the number of bytes per pixel)
       
        // 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, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            for (int m = 0; m < n; m++)
            {
                // iterate over pixels in scanline
                for (int j = 0; j < bi.biWidth/n; j++)
                {
                    // temporary storage
                    RGBTRIPLE triple;

                    // read RGB triple from infile
                    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                    
                    for (int l = 0; l < n; l++)
                    {
                        // write RGB triple to outfile
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    }
                }      
                fseek(inptr, inPadding, SEEK_CUR);
                
                for (int k = 0; k < padding; k++) // Adds padding to each row
                {
                    fputc(0x00, outptr);
                }
                
                if(m < (n-1))
                {
                    fseek(inptr, -(bi.biWidth/n * sizeof(RGBTRIPLE) + inPadding), SEEK_CUR); // Resets the INPUT file selector location to the beginning of the row
                }
            }
        }

        // close infile
        fclose(inptr);

        // close outfile
        fclose(outptr);

        // that's all folks
        return 0;
    }
     /**
     * resize.c
     *
     * Computer Science 50
     * Problem Set 4
     *
     * Resizes a 24-bit uncompressed bmp by a factor of n
     */
           
    #include <stdio.h>
    #include <stdlib.h>

    #include "bmp.h"

    int main(int argc, char* argv[])
    {
        // ensure proper usage
        if (argc != 4 || atoi(argv[1]) > 100 || atoi(argv[1]) < 1)
        {
            printf("Usage: ./resize n infile outfile, where n is greater than 0 and less than or equal to 100\n");
            return 1;
        }
        
        // remember filenames
        char* infile = argv[2];
        char* outfile = argv[3];
        int n = atoi(argv[1]);

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

        int inPadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // Calculates input padding before rewriting header files so I can use it to scan accurately over input file
        
       
        // resize BITMAPFILEHEADER
        bi.biHeight = bi.biHeight * n; // Multiply the height by n
        bi.biWidth = bi.biWidth * n; //  Multiplies the width by n

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

       // Determine the SizeImage after the padding is set which requires biWidth be set; normally SizeImage would be right after width
        bi.biSizeImage = bi.biWidth * abs(bi.biHeight) * sizeof(RGBTRIPLE) + padding * abs(bi.biHeight);    
        bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi.biSizeImage; // concentrates just on the size of the bitmap, subtracting out the headers, multiplies by n to expand number of pixels, multiples by the number of bytes per pixel (the number of bytes per pixel)
       
        // 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, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            for (int m = 0; m < n; m++)
            {
                // iterate over pixels in scanline
                for (int j = 0; j < bi.biWidth/n; j++)
                {
                    // temporary storage
                    RGBTRIPLE triple;

                    // read RGB triple from infile
                    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                    
                    for (int l = 0; l < n; l++)
                    {
                        // write RGB triple to outfile
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    }
                }      
                fseek(inptr, inPadding, SEEK_CUR);
                
                for (int k = 0; k < padding; k++) // Adds padding to each row
                {
                    fputc(0x00, outptr);
                }
                
                if(m < (n-1))
                {
                    fseek(inptr, -(bi.biWidth/n * sizeof(RGBTRIPLE) + inPadding), SEEK_CUR); // Resets the INPUT file selector location to the beginning of the row
                }
            }
        }

        // close infile
        fclose(inptr);

        // close outfile
        fclose(outptr);

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

Resize - output files look right but check50 gives errors

I'm running resize.

The headers are right when I check them using peek.

The files look right (smiley, small, even whodunit).

But when I run check50 I get these errors:

:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2
:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3
:( resizes 1x1-pixel BMP to 4x4 correctly when n is 4
:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5
:( resizes 2x2-pixel BMP to 4x4 correctly when n is 2

Any ideas? I notice my file sizes are larger than the staff versions.

Here is my code:

     /**
     * resize.c
     *
     * Computer Science 50
     * Problem Set 4
     *
     * Resizes a 24-bit uncompressed bmp by a factor of n
     */
           
    #include <stdio.h>
    #include <stdlib.h>

    #include "bmp.h"

    int main(int argc, char* argv[])
    {
        // ensure proper usage
        if (argc != 4 || atoi(argv[1]) > 100 || atoi(argv[1]) < 1)
        {
            printf("Usage: ./resize n infile outfile, where n is greater than 0 and less than or equal to 100\n");
            return 1;
        }
        
        // remember filenames
        char* infile = argv[2];
        char* outfile = argv[3];
        int n = atoi(argv[1]);

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

        int inPadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // Calculates input padding before rewriting header files so I can use it to scan accurately over input file
        
       
        // resize BITMAPFILEHEADER
        bi.biHeight = bi.biHeight * n; // Multiply the height by n
        bi.biWidth = bi.biWidth * n; //  Multiplies the width by n

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

       // Determine the SizeImage after the padding is set which requires biWidth be set; normally SizeImage would be right after width
        bi.biSizeImage = bi.biWidth * abs(bi.biHeight) * sizeof(RGBTRIPLE) + padding * abs(bi.biHeight);    
        bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi.biSizeImage; // concentrates just on the size of the bitmap, subtracting out the headers, multiplies by n to expand number of pixels, multiples by the number of bytes per pixel (the number of bytes per pixel)
       
        // 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, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            for (int m = 0; m < n; m++)
            {
                // iterate over pixels in scanline
                for (int j = 0; j < bi.biWidth/n; j++)
                {
                    // temporary storage
                    RGBTRIPLE triple;

                    // read RGB triple from infile
                    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                    
                    for (int l = 0; l < n; l++)
                    {
                        // write RGB triple to outfile
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    }
                }      
                fseek(inptr, inPadding, SEEK_CUR);
                
                for (int k = 0; k < padding; k++) // Adds padding to each row
                {
                    fputc(0x00, outptr);
                }
                
                if(m < (n-1))
                {
                    fseek(inptr, -(bi.biWidth/n * sizeof(RGBTRIPLE) + inPadding), SEEK_CUR); // Resets the INPUT file selector location to the beginning of the row
                }
            }
        }

        // close infile
        fclose(inptr);

        // close outfile
        fclose(outptr);

        // that's all folks
        return 0;
    }