0

I cant seem to get my enlarge code to work. I am trying to make it go back and do the pixel n number of times but my code is faulty but I cant figure out why. I have only included my padding and enlarge code Can anyone tell me what is wrong with my code?

int oldpadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int newpadding =  (4 - (newinfo.biWidth * n * sizeof(RGBTRIPLE)) % 4) % 4;

BITMAPINFOHEADER newinfo;


// write outfile's BITMAPFILEHEADER
    BITMAPFILEHEADER newfile;

    newfile = bf;
    newinfo = bi;
    newinfo.biWidth = bi.biWidth * n;
    int oldpadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int newpadding =  (4 - (newinfo.biWidth * n * sizeof(RGBTRIPLE)) % 4) % 4;
    newinfo.biHeight = bi.biHeight * n;
    newinfo.biSizeImage = (bi.biWidth * n * sizeof(RGBTRIPLE) + newpadding) * abs(newinfo.biHeight);
    newfile.bfSize = newinfo.biSizeImage + sizeof(newfile)+ sizeof(newinfo);

fwrite(&newfile, sizeof(newfile), 1, outptr);

// write outfile's BITMAPINFOHEADER

fwrite(&newinfo, sizeof(newinfo), 1, outptr);

// determine padding for scanlines

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    //enlarge vertically
    for (int hi = 0; hi < n; hi++)
    {
      if(hi > 0)
      {
      fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
      }
    // 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 and enlarge horizontally
            for (int jn = 0; jn < n; jn++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }

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

    // then add it back (to demonstrate how)
    for (int k = 0; k < newpadding; k++)
    {
        fputc(0x00, outptr);
    }
   }
}
9
  • The first thing I can see is that you are resetting the file pointer to beginning of line before reading the line - on the first iteration I expect this is backing over the header data. Next, it looks like you are using the output file width to determine whether you have reached EOL on the input file data ( j < newinfo.biWidth) - additionally, not sure why you then add n to j in the read loop - surely you will reach the biWidth before you have read all the pixels on the input line? Aug 16 '15 at 16:55
  • @JohnFinnigan i get the first error . what is the issue with the second error though? my idea was that j should iterate over the whole of the new width now not the old one hence I went with the new width. Aug 17 '15 at 16:43
  • Also I did j = j+n because from my understanding. this is what I was achieving. if lets say I had 3 pixels and I enlarged it three times. j will write the first pixels on the first three pixels hence the next pixel j should use is j + n Aug 17 '15 at 16:47
  • Are we looking at the code to do the initial horizontal enlargement? If not, I may be mistaken in what I am saying. If it is the first set of scans over the input file, then you need to read the pixels one by one so j would increase by 1 (j++) else if n is anything other than 1 it will skip pixels when you read them from the input file as per the fread on inptr - also you dont want to read past the end of the input line length which you will do if you use the new width which is larger Aug 17 '15 at 17:20
  • Just for clarity... this loop - for (int j = 0; j < newinfo.biWidth; j = j + n) - this should be to loop over the width of the input line, so your fread picks up each pixel - hence the line width should be that of the input file, not the output file - then apply j=j+1 as i mention above so as not to miss any pixels - I hope this makes sense Aug 17 '15 at 19:47
0

The first thing I can see is that you are resetting the file pointer to beginning of line before reading the line - on the first iteration I expect this is backing over the header data. Next, it looks like you are using the output file width to determine whether you have reached EOL on the input file data ( j < newinfo.biWidth) - additionally, not sure why you then add n to j in the read loop - surely you will reach the biWidth before you have read all the pixels on the input line? by John Finnigan

You must log in to answer this question.

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