0

ok been working on resize for some days and I thought I completed it but when I checked the image it has a full green box without the inner white box in the image.Would really appreciate if someoen could help me as to what is wrong with this code

/**

* copy.c * * Computer Science 50 * Problem Set 4 * * Copies a BMP piece by piece, just because. */

include

include

include "bmp.h"

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

// converting n to an actual int
int factor = atoi(argv[1]);

// limiting the range of n to be bw 1 and 100
if (factor < 1 || factor > 100)
{
    printf("n must be between 1 and 100 \n");
    return 2;
}

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

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

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

// declaring new header and info
BITMAPFILEHEADER bf2 = bf;
BITMAPINFOHEADER bi2 = bi;

// Declaring new elements of BITMAPFILEHEADER and BITMAPINFOHEADER that needs to be changed
bi2.biWidth = bi.biWidth * factor;
bi2.biHeight = bi.biHeight * factor;
bf2.bfSize = bi2.biSizeImage + 54;   



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

// new padding
int newpadding =  (4 - (bi2.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;


//calculating the total size of image in bytes : bfSize and the size of image.
bf2.bfSize = 54 + (bi2.biWidth * abs(bi2.biHeight) * sizeof(RGBTRIPLE)) + abs(bi2.biHeight) * newpadding;
bi2.biSizeImage = bf2.bfSize - 54;

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

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

// loop to increase the height by n times
for (int b = 0; b < factor; b++)
{
    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {

        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

            //loop to multiply the pixels in row n times in the new resized file
            for (int a = 0; a < factor; a++)
            {
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            } 
        }


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

        if(newpadding > 0)
        {
            // then add it back (to demonstrate how)
            for (int k = 0; k < newpadding; k++)
            {
                fputc(0x00, outptr);
            }
        }

        fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE) + padding), SEEK_CUR);     
    }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}enter image description here

1 Answer 1

1

Yor program is repeating the first line of pixels, but never moves on to the second line in the input file. You correctly figured out that you needed to reset to the beginning of each input line to reprocess it in order to handle the vertical scaling, but you overlooked moving forward to the next line after processing each line the required number of times for the vertical scaling.

Add a line to move forward after processing each line factor times, and that should address this problem. I'm assuming that you're processing the headers correctly, so there may or may not be other issues, but that wasn't part of your question. ;-)

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

1
  • that has been the most encrypted solution I have gotten from here so far. But it worked thanks Dec 4, 2015 at 11:45

You must log in to answer this question.

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