0

and thanks for helping me with my problem. I have managed to get horizontal resizing to work within my resizing program, but every time I attempt the next step, I keep failing. Here is the code of the main loop of the program:

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
  // Perform the horizontal resize many times to get a vertical one.
  for (int k = 0; k < factorint; k++)
  {
    // 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 to file the amount of times for the scale factor.
        for (int l = 0; l < factorint; l++)
        {
          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);
    }

  }
  // Tries to return to beginning of line, but fails.
  fseek(inptr, bi.biWidth, SEEK_CUR);

}

The problem, I believe, is when I try to return the input file position to the start of the line - every time I try, it results in a garbled mess. I know that is what I need to do, but every time I attempt this, I fail. Could anyone give any pointers to help me? Thanks in advance for your suggestions, and I am sorry if have asked a stupid question.

A garbled mess of test.png

1 Answer 1

1

I see at least one issue, maybe three.

First, your final fseek() that is supposed to go back to the beginning of the line is outside the for( k ) loop when it should be inside. Move it above the preceeding }.

Second, the fseek should move backwards, not forwards, so you need a negative direction, or you need to save the position of the beginning of the line in a var.

This creates another problem though. Once the loop is finished iterating over a given line, you need to NOT go back. The fix is this: instead of moving the fseek(), copy it instead so that you go forward to the next line after the for loop ends. OR, surround it with an IF statement to test if you're processing the line for the last time.

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

3
  • Hello Cliff, sorry for the late reply - I was working on some other programming projects bar CS50, meaning I didn't have time to look. I have implemented the suggestions you asked for - I have stored the initial position of the scanline for each line as an fpos_t, so it can be referred back to using fsetpos at the end of each horizontal resize, and I have also added a fseek at the end of the for i loop: fseek(inptr, bi.biWidth / factorint, SEEK_CUR); (bi.biWidth) = new width, so dividing it by scale factor to get the original line length. Yet, this still doesn't seem to work. I know I... Aug 30, 2015 at 19:44
  • have done something stupid somewhere, but I just can't seem to be able to work out the problem. Thanks for you help anyway - it's working better than it was ;-) Aug 30, 2015 at 19:47
  • Without seeing where and how you placed these changes, I can only guess. Two things come to mind. First, when you finish processing an input line multiple times, is the file pointer pointing to the end or the beginning of the line? If it points at the end, there's no need to jump forward the length of a line. Second, after processing the pixels on that line, did you remember to skip forward over the padding, if any, in the input file to get to the actual start of the next line?
    – Cliff B
    Aug 30, 2015 at 21:14

You must log in to answer this question.

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