0

I am trying to solve pset4/resize.c

I have been spending hours on trying to determine the mistake. Here is the code:

// Copies a BMP file

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

#include "bmp.h"

int isNumber(char number[]);

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

    char *c = argv[1];
    //  ensure the first argument must be an integer
    if (isNumber(c) == 1) {
        fprintf(stderr, "n, the resize factor, must be an integer.\n");
        return 1;
    }

    //integer n the resize factor
    int n = atoi(argv[1]);

    // resize factor n must be less than or equal to 100
    if (n > 100) {
        fprintf(stderr, "n, the resize factor, must satisfy 0 < n <= 100.\n");
        return 1;
    }

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

    // open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "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;
    }

    //  BITMAPFILEHEADER
    BITMAPFILEHEADER new_bf;

    //  BITMAPINFOHEADER
    BITMAPINFOHEADER new_bi;

    new_bf = bf;
    new_bi = bi;

    // Width and Height increase by factor of n for resizing
    new_bi.biWidth = bi.biWidth * n;
    new_bi.biHeight = bi.biHeight * n;

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

    // new size of image and file
    new_bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_bi.biWidth) + new_padding) * abs(new_bi.biHeight);
    new_bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);


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

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

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



    RGBTRIPLE array[new_bi.biWidth];

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        // 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);

            for (int k = 0; k < n; k++) {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }

            array[j] = triple;

        }

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


        for (int y = 0; y < n; y++) {

            fwrite(array, sizeof(RGBTRIPLE), new_bi.biWidth, outptr);

            // 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);

    // success
    return 0;
}

int isNumber(char number[])
{
    int i = 0;

    //checking for negative numbers
    if (number[0] == '-')
        i = 1;
    for (; number[i] != 0; i++)
    {
        //if (number[i] > '9' || number[i] < '0')
        if (!isdigit(number[i]))
            return 1;

    }
    if (atoi(number) <= 0)
        return 1;
    return 0;
}

I updated my code. I correct my headers using peek tool.

offset  type   name              argv[1]   argv[2]
0       WORD   bfType               4d42      4d42
2       DWORD  bfSize           00000c36  00000c36
6       WORD   bfReserved1          0000      0000
8       WORD   bfReserved2          0000      0000
10      DWORD  bfOffBits        00000036  00000036
14      DWORD  biSize           00000028  00000028
18      LONG   biWidth          00000020  00000020
22      LONG   biHeight         ffffffe0  ffffffe0
26      WORD   biPlanes             0001      0001
28      WORD   biBitCount           0018      0018
30      DWORD  biCompression    00000000  00000000
34      DWORD  biSizeImage      00000c00  00000c00
38      LONG   biXPelsPerMeter  00000b12  00000b12
42      LONG   biYPelsPerMeter  00000b12  00000b12
46      DWORD  biClrUsed        00000000  00000000
50      DWORD  biClrImportant   00000000  00000000

But the output is still incorrect

The output shown of smiley with a factor of 4 is: enter image description here

The correct image of smiley with a factor of 4 is: enter image description here

I don't know why still output is incorrect.

7
  • Can you describe the problems? Does it compile? Have you run it and inspected the output? You should start with scale factor of 1 and smiley.bmp and compare the headers to the input file using the peek and xxd tools. Once the header is right, work on getting the image right. Again, use the xxd tool to look at the input and output files. Once that's working, change to scaling factor of 2 and repeat the process.
    – Cliff B
    Jul 5 '18 at 0:26
  • I added the output Jul 6 '18 at 16:04
  • Have you looked at the raw output using the xxd and peek tools? Are the output headers correct? Do you see any patterns in the hex data (from xxd tool) that tell you anything? What have you done to try and fix the problem(s)? What do you believe might be the problem? The goal here is to get you to think about what's wrong and to see what your thinking is here and to get you to think about what steps are needed to first identify the problem(s) and then to fix them. One of the most important skills to develop is the ability to debug a program. This is the time to learn that skill.
    – Cliff B
    Jul 6 '18 at 16:27
  • I edit my code and correct my output headers but output of image shown is still incorrect. Jul 7 '18 at 7:53
  • Still output of image shown is incorrect. Jul 10 '18 at 7:13

You must log in to answer this question.

Browse other questions tagged .