I'm having trouble with pset4's resize.c.
It partly works now! But there are these black vertical lines running through the picture. GDB helped.
This is my code now.
/**
* resize.c
*
* Computer Science 50
* Problem Set 4
*
* Resizes a BMP piece by piece, just because.
*/
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 4)
{
printf("Usage: ./resize n infile outfile\n");
return 1;
}
// remember filenames
int n = atoi(argv[1]);
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 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;
}
// original width, height, and padding
long inWidth = bi.biWidth;
long inHeight = bi.biHeight;
int inPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
printf("%d = bfSize\n", bf.bfSize);
printf("%d = biSize\n", bi.biSizeImage);
bi.biWidth = bi.biWidth * n;
bi.biHeight = bi.biHeight * n;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = abs((bi.biWidth * 3 + padding) * bi.biHeight);
bf.bfSize = bi.biSizeImage + bf.bfOffBits;
printf("%d = bfSize\n", bf.bfSize);
printf("%d = biSize\n", bi.biSizeImage);
printf("%d = sizeof bf\n", sizeof(bf));
// 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; i < abs(inHeight); i++)
{
// re-copy each scanline n times
for (int l = 0; l < n; l++)
{
// iterate over pixels in scanline
for (int j = 0; j < inWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile n times
fwrite(&triple, sizeof(RGBTRIPLE), n, outptr);
for (int k = 0; k < n; k++)
{
printf("[%d] ", triple.rgbtGreen);
}
}
printf("\n");
if (l != n - 1)
{
// restart scanline
fseek(inptr, -inWidth * sizeof(RGBTRIPLE), SEEK_CUR);
}
// add padding
for (int j = 0; j < padding; j++)
{
fputc(0x00, outptr);
}
}
// skip over padding, if any
fseek(inptr, inPadding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}