0

So now my square is completely green, but I believe it ressizes correctly.

my code is as follows:

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

#include "bmp.h"


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


int n = atoi(argv[1]);
char *infile = argv[2];
char *outfile = argv[3];

//if scalefactor 'n' is wrong 
if (99 < n)  
{
     fprintf(stderr, "Usage: n must be less than or equal to 100\n");
        return 1;
}

if (n < 0)  
{
     fprintf(stderr, "Usage: n must be a positive integer\n");
        return 1;
}

//if image cannot be opened
FILE* image = fopen(infile, "r");
if (image == NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 1;
}


//if resized image cannot be created
FILE* resizedimage = fopen(outfile, "w");
if (resizedimage == NULL)
{
    fprintf(stderr, "Could not open %s.\n", outfile);
    return 1;
}


//**************************************************************************

// read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, image);

// read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, image);

// read infile's RGB triple
    RGBTRIPLE rgbt;
    fread(&rgbt, sizeof(RGBTRIPLE), 1, image);

// **********************************************************************

 // ensure infile image 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(resizedimage);
    fclose(image);
    fprintf(stderr, "Unsupported file format.\n");
    return 1;
}
//************************************************************************

//UPDATING outfiles HEADER INFO... 


//update width
int biWidthoriginal = bi.biWidth;
bi.biWidth = (bi.biWidth * n);


//update height
int biHeightoriginal = bi.biHeight;
bi.biHeight = (bi.biHeight * n);

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


//update image size
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);


 //update file size
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);


//***********************************************

// write outfile's BITMAPFILEHEADER
            fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, resizedimage);

// write outfile's BITMAPINFOHEADER
            fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, resizedimage);


//************************************************************

//RESIZING

 // for each row
    for (int i = 0; i < abs(biHeightoriginal); i++)
    {
        // VERTICALLY ITERATE
        for (int z = 0; z < n; z++)
        {

           // for each pixel in a row
           for (int j = 0; j < abs(biWidthoriginal); j++)
           {
              //write to outfile 'n' number of times - HORIZONTALLY ITERATE
              for (int k = 0; k < n; k++)
               {
               fwrite(&rgbt, sizeof(RGBTRIPLE), 1, resizedimage);
               }

           }

              //write outfile's padding
            for (int k = 0; k < padding; k++)
            {
            fputc(0x00, resizedimage);
            }


            // skip over infiles padding if any
            fseek(image, paddingoriginal, SEEK_CUR);

            //return to beginning of scanline
            fseek(image, abs(-biWidthoriginal*3 - paddingoriginal), SEEK_CUR);
         }

         //go to end of scanline before iterating over next row
            fseek(image, abs(biWidthoriginal*3 + paddingoriginal), SEEK_CUR);

    }


    // close infile
    fclose(image);

    // close outfile
    fclose(resizedimage);

    // success
    return 0;
}

at my wits end! any help would be much appreciated. :)

1 Answer 1

0

Ummm..... Where does the code actually read anything from the input file? If it doesn't read the data in, it can't process it out.

If you've read all the data in earlier, please edit the question and post the code.

1
  • full code posted :) all data was read in earlier!
    – Arhaz
    Commented Jul 28, 2017 at 13:52

You must log in to answer this question.

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