My code is as follows:
/** * resize.c * * Computer Science 50 * Problem Set 4 * * Resize a BMP. */ #include <stdio.h> #include <stdlib.h> #include "bmp.h" int main(int argc, char* argv[]) { // ensure proper usage if (argc != 4) { printf("Usage: ./copy n infile outfile\n"); return 1; } // remember filenames and n 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; } // write outfile's BITMAPINFOHEADER BITMAPINFOHEADER biout = bi; biout.biWidth = bi.biWidth * n; biout.biHeight = bi.biHeight * n; biout.biSizeImage = (((4 - (biout.biWidth * sizeof(RGBTRIPLE)) % 4) % 4 ) * abs(biout.biHeight)) + (biout.biWidth * abs(biout.biHeight) * 3); fwrite(&biout, sizeof(BITMAPINFOHEADER), 1, outptr); // write outfile's BITMAPFILEHEADER BITMAPFILEHEADER bfout = bf; bfout.bfSize = biout.biSizeImage + 14 + 40; fwrite(&bfout, sizeof(BITMAPFILEHEADER), 1, outptr); // determine padding for scanlines int padding = (4 - (biout.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // iterate over infile's scanlines for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) { // Writes scanline n times for (int l = 0; l < n; l++) { // moves SEEK_CUR if repeating the line vertically if (l > 0) { fseek(inptr, ((((4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4) + bi.biWidth * 3) * -1), SEEK_CUR); } // 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 n times for (int k = 0; k < n; k++) { 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); } } } // close infile fclose(inptr); // close outfile fclose(outptr); // that's all folks return 0; } After running resize.c with ./resize 2 small.bmp large.bmp, I was unable to open the large.bmp created as there was an error. So going back to my code, I ran gdb and went line by line to find out what exactly is happening. I found out that after the second iteration of for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) (i = 1 at this point), at line 98 (for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)) executes correctly and reads triple as 00FF00. However the moment the next line executes, triple becomes 000000 instead.
Why is this so? And if there is anything I missed out, please do give me a clue! Coming here is a last resort for me but I have been stuck here for several days so thank you for reading this. :)