0

after I thought I got every thing right following exactly Zamayla pseudocode,

I got:

runtime error: unsigned integer overflow: 18446744073709551615 * 3 cannot be represented in type 'unsigned long' 

when I used the code with n = 1 it compiles and runs but the result is only a shadow enter image description here

/**
 * resize a BMP image.
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.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;
    }

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

    // make sure it's a digit and assign it to n
    if (!isdigit(*argv[1]))
    {
        fprintf(stderr, "n must be a number!\n");
        return 2;
     } else
    {
        n = atoi(argv[1]);
    }

    // make sure it's a positive number
    if (n < 1 || n > 100)
    {
        fprintf(stderr, "n must be a number between 1 - 100");
        return 3;
    }



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

    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 5;
    }

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

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

    bi = old_bi;

    // 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 6;
    }

    // update width and height
    bi.biWidth *= n;
    bi.biHeight *= n;

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

    bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) *abs(bi.biHeight);
    bf.bfSize = bi.biSizeImage + 54;

    // 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, biHeight = abs(old_bi.biHeight); i < biHeight; i++)
    {

        // for each row
        for (int m = 0; m < n - 1; m++)
        {
            // read and write pixels and new padding
            for (int j = 0; j < old_bi.biWidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;

                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                for (int f = 0; f < n; f++)
                {
                    // write RGB triple to outfile
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
                    // write new padding
                    for (int k = 0; k < padding; k++)
                {
                    fputc(0x00, outptr);
                }

                // move pointer back
                fseek(inptr, -1 * sizeof(RGBTRIPLE) * j, SEEK_CUR);
            }

             // read and write pixels and new padding again
            for (int j = 0; j < old_bi.biWidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;

                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                for (int f = 0; f < n; f++)
                {
                    // write RGB triple to outfile
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
                    // write new padding
                    for (int k = 0; k < padding; k++)
                {
                    fputc(0x00, outptr);
                }

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




    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
2
  • The fseek line would be much more useful than that screenshot. Maybe cast the variable to signed integer first, depending on the order of operations and the type of operands, this happens implicitly. – Blauelf Mar 6 '17 at 9:48
  • ohh actually sorry I wanted to share all the code, but didn't,,, I will edit the post now – Yasser Altamimi Mar 6 '17 at 9:49
1

Hmmm..... Have you taken a good look at the output file contents? At n=1, there are headers but no data. Nothing is being written out after the headers. I'll leave it to you to locate the issue, but here's a hint. Run debug to see what for loops are executing when n=1. Or, insert some strategic printf commands.

You should make use of xxd and look at the image portion of the input and output files and make sure they are correct. Do this for n=1 before trying to do n>1.

Also, I don't understand why there are two complete blocks of code tp "read and write pixels". Only one is necessary. If you're doing it twice to accommodate n=2, then what happens at n=3, 4, or larger?

This will get you going. There may be more issues, but these major problems need to be handled first.

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

2
  • Thanks man I'll run debug50 and see, but why am I writing it twice is because of Zamayla pseudocod that I'm following, and in all honesty I tried so hard to understand her algorithm but I couldn't, so I thought I'll just follow it only to move ahead as this is getting me so frustrated not being able to understand the problem and the padding and all the equations behind it – Yasser Altamimi Mar 6 '17 at 10:43
  • I'm also having troubles understanding how to combine the vertical and horizontal pseudocod – Yasser Altamimi Mar 6 '17 at 10:45

You must log in to answer this question.

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