0

In resize.c, the image created in my resized image is a tiny image with a black blotch in the middle, regardless of whether I use small.bmp or smiley.bmp as input.

When I run xxd, the header in the bmp file I created is extremely long (it scrolls so far in a rapid fashion that I can't see the beginning after it's done). It seems that one of my for loops is causing fwrite to perform far too many times. The walkthrough wasn't very helpful IMO...I think I may need to do a malloc() somewhere, but i'm not sure if it would be in this portion or not.

*n is the factor I am using to resize.

Code snippet:

    // n is declared and given a value
    char *n = argv[1];

 // iterate over infile's scanlines
        // which for loop is running too many times?
        // fwrites too many times (shows when xxd is run)
        for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            for (int y = 0; y < *n; y++)
            {
                // 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 z = 0; z < *n; z++)
                    {
                        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 < padding; k++)
            {
                fputc(0x00, outptr);
             }
         }
   }
  • Can you add the statement(s) that declares n and sets it's value please? – Cliff B Jul 17 '15 at 19:09
  • Done..is that what you were looking for? – Yami Medina Jul 17 '15 at 19:12
1

char *n = argv[1]; explains the problem. Your goal is to get a number as input, but that's not what you are doing here. Remember that all arguments in argv[] are strings. You have essentially grabbed the first character in argv[1] and put it in n. But then you treat n as an integer, something that is allowed, but in this case, is wrong.

Let's say that the user input was 5. That means that *n points to the ASCII value of 5, which is 53. So, your loops are running 53 times!

When you want numeric inputs as parameters for your program, you need to convert them to the appropriate type using the appropriate function for the target data type. In this case, try this:

int n = atoi(argv[1]);

No guarantees that there aren't any other issues, but this should get you going.

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

  • and then use n wherever I have *n in my code? this makes sense. I thought that *n pointed to the value as opposed to the address in memory, but I guess it's a different thing entirely. I won't actually have the chance to test this out for a few hours, but I promise this question will be closed by the end of the day :) – Yami Medina Jul 17 '15 at 19:38
  • I have other problems with it, but since this solved an immediate problem (xxd's results are still far too large but they at least fit in one terminal screen now), i'll mark it answered. – Yami Medina Jul 17 '15 at 23:24

You must log in to answer this question.

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