0

I can't figure out why my code wont work. Please help. My idea for solving the height problem was

  1. write one line of the image
  2. move the file cursor to the beginning of the newly written line
  3. read in that line into an array called line (including padding)
  4. write the entire line n - 1 times
  5. advance to the next line of the source file

I think that my code for extending the image width wise is correct. The code that copies the lines is not correct. The rainbow colors of the file output make me think that the write output is offsetting a byte. The piece I am stuck on is at the end of the program.

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

#include "bmp.h"

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

// 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;
}
// update header
//width
int original_file_line_width = bi.biWidth;
bi.biWidth = bi.biWidth * n;

//height
int original_file_line_Height = bi.biHeight;
bi.biHeight = bi.biHeight * n;    

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

//image size, total size of image for pixels and padding
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);

//file size
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

//dynamic memory space for storing a single read line of the pic 
RGBTRIPLE line[bi.biWidth + padding/3 ];
//printf("%lu",sizeof(line));

// iterate over infile's scanlines
for (int i = 0, o = abs(original_file_line_Height); i < o; i++)
{

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

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

        for (int k = 0; k < n; k ++)
        {
            // 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);
    }
    //printf("%lu",sizeof(line));
    for(int r = 0; r < n - 1; r++)
    {
        fseek(outptr, -(int)(sizeof(line)), SEEK_CUR);//move file pointer to the begining of the line
        fread(line, (int)sizeof(line), 1, outptr);//read the entire row of pixels into line
        fwrite(line, (int)sizeof(line), 1, outptr);//write the contense of line

    }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);
// success
return 0;

}

1 Answer 1

0

enter image description here

When you update the files here, on your 2nd statement you update not only bi.biWidth but the original_file_line _width too. So the original_file_line _width is also bi.biWidth * n. ( It is like: x = y; y = z. So x = z).Use another variable for the updated bi.biWidth. The same applies to bi.biHeight.

1
  • Thanks for the help, I managed to fix the program after a couple hours in the debugger and with the watch expression. I ended up restarting and trying a different approach and it ended up working.
    – Gupy Fish
    Commented Jun 14, 2017 at 18:21

You must log in to answer this question.

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