0

I'm really sorry to be asking for help but I've been searching for answers and I just don't understand what I'm doing wrong.

I think I must be messing something up with the horizontal resize and the seek because the output for small.bmp is the same size the pixel color are just mixed up. This leads me to believe that the seek is going wrong and the file is interpreting the padding as part of the pixels but I don't know how to fix any of this or what is going wrong.

Sorry. I've been working on this one problem for around 1 week and a half so I decided I need to ask for help.

// Copies a BMP file
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"

int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)

{

    fprintf(stderr, "Please give 3 arguments; size to be multiplied, infile, outfile\n");

    return 1;
}


// number by which the file is resized.
int n = atoi(argv[1]);
if(n < 0 || n > 101)
{
    fprintf(stderr,"The number given must be between 100 and 0 %i\n",n);
}
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];

// 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;
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;
}
// this needs to be initialized for it to start
BITMAPFILEHEADER bfNew = bf;
BITMAPINFOHEADER biNew = bi;
// Update Width
biNew.biWidth = bi.biWidth * n;
// Update Height
biNew.biHeight =bi.biHeight * n;
// Update Padding
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int newPadding = (4 - (biNew.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;;
// Update SizeImage
biNew.biSizeImage = abs(biNew.biHeight)*(newPadding +biNew.biWidth*sizeof(RGBTRIPLE));
// Update Size Total
biNew.biSize = bfNew.bfOffBits + biNew.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, biHeight = abs(bi.biHeight); i < biHeight; i++)
    // iterate over pixels in scanline
    // deals with height
    for (int j = 0; j < n; j++)
    {
        // temporary storage
        RGBTRIPLE triple;
        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
        // write pixel n amount of times
        for(int x = 0;x < n; x++)
        {
            // write RGB triple to outfile
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
        // add new padding to the file
        for (int k = 0; k < newPadding; k++)
        {
            fputc(0x00, outptr);
        }
        // skips over padding
        fseek(inptr, newPadding, SEEK_CUR);



    }
// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

1

I see three problems here.

First, the code used the output padding var to skip over padding in the input file.

Second, for each line of pixels being processed, it is only processing n pixels. It should be processing the total number of pixels in the line.

Third, the structure of your for loops needs work. Because of the design, the code adds padding to the output file and skips padding in the input file for EACH PIXEL processed.

All of these issues have the common theme of knowing generally what to do, but not using the correct variables or carefully implementing the pseudocode correctly. Attention to detail is always important in creating a program.

That will address the problems when n=1. There are more issues, but I'll give you a chance to try to fix them when you go to n=2. ;-)

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

1
  • Thank you, I really appreciate the help.
    – Ryan
    Nov 19 '18 at 14:59

You must log in to answer this question.

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