Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > Ok, but for sure, in your humble opinion, strcat is the way to go to append the pixels to the buffer?
    No NO NO NO NO!!!!!!!!
    You're taking it all way too literally.
    strcat is USELESS for appending pixels to the buffer.

    I'm trying to get you to understand that the way strcat works is the same way that your 'append pixels to the buffer' would work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    15
    Rep Power
    0
    Ok, so the loop to write out n number of times would be in my loop to write out the pixels:
    for int i=0;i<factor;i++)
    {
    fwrite(buffer, sizeof(RGBTRIPLE),factor, outptr)
    }

    Cool. Thank you both for that. But the loop to factor the pixels out? I dont follow on that. What is wrong with what I had(besides not working like how it should of course):
    buffer[k]=triple;?
  4. #18
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > buffer[k]=triple;?
    Because k goes 0 1 2 0 1 2 0 1 2 for each pixel you read.

    buffer index needs to go 0 1 2 3 4 5 6 7 8 .... if you want three copies of first pixel in 0 1 2, three copies of the second pixel in 3 4 5 and three copies of the third pixel in 6 7 8
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    Some of the issues are documented in the code:

    Code:
    //  Issue.You're using OldHeight and OldWidth in your outer and inner loops
    //   You should be using your newly calculated width and height for your
    //	new bitmap image
    
     // iterate over infile's scanlines
      for (int i = 0; i < abs(OldHeight); i++) {
        // iterate over pixels in scanline
        for (int j = 0; j < Oldwidth; j++) {
          // temporary storage
          RGBTRIPLE triple;
          // read RGB triple from infile
          fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    
    // Issue.  Here you continually update the same area in buffer 
    //  buffer[0] to buffer[factor-1]
    //  You must "expand" the pixels here  to completely fill in the buffer file
    //  This means you must use a temporary index variable to iterate thru the 
    //  buffer variable in order to fill it with the expanded RGB pixels
          //iterate over each pixel factor times
          for (int k = 0; k < factor; k++) {
            buffer[k] = triple;
          }
    // Issue.  Here you are ONLY writing THREE bytes in triple to your output file,
    //  You should be writing the completely filled in buffer variable.
    // In other words, the fwrite is completely wrong
      
    // write RGB triple to outfile
    // fwrite(&triple, sizeof(RGBTRIPLE), bi.biWidth, outptr);
        }
    // Issue.  The following is totally meaningless.  You should skipping over the padding at the end
    // of the old file row.  padding, if correctly computed will range between zero and three.
    // Why are you adding 54?
    	
        // skip over padding, if any
        fseek(inptr, 54 + (Oldwidth * sizeof(RGBTRIPLE) * i), SEEK_SET);
    
    //  Ok. You got this one almost right.  You should be using the width of the new file
        for (int r = 0; r < factor; r++) {
          fwrite(buffer, sizeof(RGBTRIPLE), bi.biWidth, outptr);
        }
    // Ok.  the following is correct	
        // write padding to outfile
        for (int p = 0; p < padding; p++)
          fputc(0x00, outptr);
      }
    You're putting the cart before the horse on this assignment. You should create the header for the new BMP file first by cloning the old header file and then updating the required fields such as biWidth, biHeight, biSizeImage and bfSize. The new BMP file will NOT display properly in a Paint program if these fields are not updated to reflect the properties of the new file.

    Once you have correctly built the new header for the BMP file, you can then concentrate on adding the pixel data to the file.

    To reiterate, build the BMP header file first and then add the data to the file.

    Finally, you don't come close to building a valid header for your new BMP file in the code that you have provided so far.
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    15
    Rep Power
    0
    Ok, so after getting some time to review over what I need to do, I realized that I need the memcpy function to copy the pixels into the buffer(the destination address). That works like a charm. However, to continously append to the destination address, everything I'm doing doesnt seem to work. For example:

    in my code, I have buffer incrementing by one to move to the next memory location:
    buffer=buffer+1;

    The program compiles for sure, but it crashes on me everytime. would I need to, for example in my memcpy
    implement something of this nature:

    memcpy(destination address+num bytes,source address, num)?

    Or is my question a question of preference?
  10. #21
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    I dunno - I did this, and it seemed to work (for 1 test)
    Code:
      // iterate over infile's scanlines
      for (int i = 0, BHeight = abs(OldHeight); i < BHeight; i++) {
        int bp = 0;
        // iterate over pixels in scanline
        for (int j = 0; j < Oldwidth; j++) {
          // temporary storage
          RGBTRIPLE triple = { 0xde, 0xad, 0xca };
    
          // read RGB triple from infile
          fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    
          //iterate over each pixel factor times  
          for (int k = 0; k < factor; k++) {
            buffer[bp++] = triple;
          }
        }
        // write RGB triple to outfile 
        // fwrite(&triple, sizeof(RGBTRIPLE), bi.biWidth, outptr); 
        for (int k = 0; k < factor; k++) {
          fwrite(buffer, 1, sizeof(RGBTRIPLE) * bi.biWidth, outptr);
          for (int p = 0; p < padding; p++)
            fputc(0x00, outptr);
        }
        // skip over padding, if any
        fseek(inptr, oldpadding, SEEK_CUR);
      }
    Like I said, you're over thinking the buffer appending.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo