0

I have been working on pset4 Resize for a few days now. I have tried researching online for the functions I didn't understand and been going through the lecture and walkthroughs and the notes but I'm feeling alittle overwhelmed at the moment. This is my code, and I really can't seem to figure out why my output is not coming out as I intended. I'll really appreciate any directions where I can better improve my situation Q.Q

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bmp.h"
#include <ctype.h>

int main (int argc, char *argv[])
{

if(argc == 1)
{
    fprintf(stderr, "Please enter command line arguments.\n Usage: ./resize [floating-point value for resizing] [Name of file to be resized] [Name of file that was resized]\n");
    return 1;
}

char *f = argv[1];
int flo = atoi(f);

if(argc != 4)
{
    fprintf(stderr, "Usage: ./resize [floating-point value for resizing] [Name of file to be resized] [Name of file that was resized]\n");
    return 1;
}

else if(isalpha(flo))
{
    fprintf(stderr, "Please enter a floating-point value for the first command line input.\n");
    return 1;
}

char *infile = argv[2];
char *outfile = argv[3];

FILE *inptr = fopen(infile, "r");
if(inptr == NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}

FILE *outptr = fopen(outfile, "w");
if(outptr == NULL)
{
    fprintf(stderr, "Could not create %s.\n", outfile);
    return 2;
}


//READ Infile's BITMAPFILEHEADER (bf) and BITMAPINFOHEADER (bi)
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

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"); // fprintf ( file printf) can be used to write to files, like notepad etc.
    return 4;
}


BITMAPFILEHEADER outbf;
BITMAPINFOHEADER outbi;

outbf = bf;
outbi = bi;

outbi.biWidth = bi.biWidth * flo;
outbi.biHeight = bi.biHeight * flo;

//padding

int inpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int outpadding = (4 - (outbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

outbi.biSizeImage = ((sizeof(RGBTRIPLE) * outbi.biWidth) + outpadding) * abs(outbi.biHeight);
outbf.bfSize = outbi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

//write outfile's BITMAPFILEHEADER and BITMAPINFOHEADER

fwrite(&outbf, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&outbi, sizeof(BITMAPINFOHEADER), 1, outptr);



// iterate over infile's scanlines
// Horizontal is the one doing work here
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
        for (int repeat = 0; repeat < flo; repeat++)
        {
                // 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
                    for(int r = 0; r < flo; r++)
                    {
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

                    }

                }

                // skip over padding, if any
                fseek(inptr, inpadding, SEEK_CUR);

                // then add it back (to demonstrate how)
                for (int k = 0; k < outpadding; k++)
                {
                    fputc(0x00, outptr);
                }
                fseek(inptr, -(outpadding), SEEK_CUR);
        }
        fseek(inptr, outpadding, SEEK_CUR);
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
/*printf("argv[1] : %s\n", argv[1]);
printf("argv[2] : %s\n", argv[2]);
printf("argv[3] : %s\n", argv[3]);*/
}

enter image description here Its really tiny lol, but this is my large.bmp.

This is the Hexadecimel

424d ae00 0000 0000 0000 3600 0000 2800 0000 0600 0000 faff ffff 0100 1800 0000 0000 7800 0000 120b 0000 120b 0000 0000 0000 0000 0000 00ff 0000 ff00 00ff 0000 ff00 00ff 0000 ff00 0000 0000 0000 0000 ff00 ffff 00ff ffff 00ff ff00 0000 0000 0000 0000 ff00 00ff 0000 ff00 00ff 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

Any help will be greatly appreciated!

0

I solved it!

I needed to change these 2 lines

              fseek(inptr, -(inpadding + bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);

        fseek(inptr, inpadding + bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);

You must log in to answer this question.

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