0

my resize code is not working properly.

I am trying to resize small.bmp by a factor of 100.

Here is my code:

/**
 *resize.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Resizes a BMP file by a factor of n.
 */

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

#include "bmp.h"

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

    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];

    // what is the scale input by user
    int n = atoi(argv[1]);

    //make sure input is psotive and between 0 and 100
    if(n < 0 || n > 100)
    {
        printf("Please input a positive integer between 0 and 100");
        return 5;
    }

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

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER infile_bi;
    fread(&infile_bi, sizeof(BITMAPINFOHEADER), 1, inptr);

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (infile_bf.bfType != 0x4d42 || infile_bf.bfOffBits != 54 || infile_bi.biSize != 40 || 
        infile_bi.biBitCount != 24 || infile_bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    //Need to declare new outfile headers
    BITMAPFILEHEADER new_bf = infile_bf;
    BITMAPINFOHEADER new_bi = infile_bi;


    //find out new width and height
   new_bi.biWidth = infile_bi.biWidth * n;

   new_bi.biHeight = infile_bi.biHeight * n;

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

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

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

    //new padding
    int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    //new size of image.
    new_bi.biSizeImage = (new_bi.biWidth * sizeof(RGBTRIPLE) + new_padding) * abs(new_bi.biHeight);

    //new size of the file
    new_bf.bfSize = infile_bi.biSize + new_bi.biSizeImage;

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(infile_bi.biHeight); i < biHeight; i++)
    {
          for(int k = 0; k < n; k++)
           {   
         // iterate over pixels in scanline
         for (int j = 0; j < infile_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 < n; l++)
             {
                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 < new_padding; k++)
            {
            fputc(0x00, outptr);
            }
         }
     }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}

and here is the result: enter image description here

Any help would be much appreciated.

0

It looks like you're processing each line of pixels only once when scaling up. After processing a given line from the input file, you need to reposition it at the beginning of that same line in the input file and repeat n times. After that, you need to be positioned at the beginning of the next line to be processed.

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

2
  • Got it working using two fseek's to control positioning within the for loops. Thanks for the help.
    – Steve23455
    Oct 29 '15 at 0:34
  • Uffff! Thank you very much! This question and its answer makes my resize works!!
    – user10025
    Oct 29 '15 at 5:36

You must log in to answer this question.

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