0

When I try to run the program, I get the message "file size limit exceeded." After looking over my code many times, I can't figure out what might be going wrong??

/**
* copy.c
*
* Computer Science 50
* Problem Set 4
*
* Copies 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;
}

//store arg[0] as int n
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)
{
    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;
}


    //Keep track of original header info to be updated
  //uint16_t original_bfSize = bf.bfSize;
  // uint32_t original_biSize = bi.biSize;
  LONG original_biWidth = bi.biWidth;
  LONG original_biHeight = bi.biHeight;

  //update header info
  bi.biWidth = n * bi.biWidth;
  bi.biHeight = n * bi.biHeight;

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

// rest of header info 
bi.biSize = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * bi.biHeight;
bf.bfSize = sizeof(bf) + bi.biSize;

// 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, O_biHeight = abs(original_biHeight); i < O_biHeight; i++)
{
    for(int m = 0; m < n; m++)
    {
    // iterate over pixels in scanline
    for (int j = 0; j < original_biWidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

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

        // replicate pixel n times to outfile
        for(int l = 0; l < n; i++)
        {
        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;
}

1 Answer 1

1

Can you say "typo = infinite loop" ?

            // replicate pixel n times to outfile
            for(int l = 0; l < n; i++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }

Your for loop initialized L but then increments I (caps added for visibility), resulting in an infinite loop that just keeps writing pixels out to the output file, probably until the environment says that you've run out of space.

There may be other problems in your code (like the lack of a padding calculation for the input file - it may or may not be different from the output file), but that wasn't the question. ;-)

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

You must log in to answer this question.

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