0

Can anyone help me with pset4 resize? I keep getting these black pixels recurring at regular intervals when I try to resize small.bmp (see picture). Not sure where I'm going wrong. Any help much appreciated.

enter image description here

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

#include "bmp.h"

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



// remember filenames
int factor = atoi(argv[1]);
char *infile = argv[2];
char *outfile = argv[3];

if(factor <= 0 || factor > 100)
{
    fprintf(stderr, "Resize factor must be greater than 0 and less than 100\n");
    return 1;
}

// 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, bf_new;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bf_new = bf;

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, bi_new;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
bi_new = bi;

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

//change dimensions of infile
bi_new.biWidth = bi.biWidth * factor;
bi_new.biHeight = bi.biHeight * factor;

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

int padding_new = (4 - (bi_new.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

bi_new.biSizeImage = ((sizeof(RGBTRIPLE) * bi_new.biWidth) + padding_new) * abs(bi_new.biHeight);




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

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



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


    //multiply the scanlines by the resize factor
    for (int m = 0; m < factor; m++)
    {



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

                // write RGB triple to outfile
                for(int l = 0; l < factor; l++)
                {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }

                     // add padding
                     for (int k = 0; k < padding_new; k++)
                     {
                     fputc(0x00, outptr);
                     }

                    //move cursor
                    fseek(inptr, 54 + (bi.biWidth * 3 + padding) * factor, SEEK_SET); 




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

          }

    }
}    

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

0
fseek(inptr, 54 + (bi.biWidth * 3 + padding) * factor, SEEK_SET); 

I'm not sure this is doing what you think it is. Won't that simply take you to the exact same place each time? (there are no changing variables in that equation).

You must log in to answer this question.

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