fread/fwrite issues

This is a discussion on fread/fwrite issues within the C Programming forums, part of the General Programming Boards category; Ok so I am having trouble editing a bitmap image file. I have successfully copied the header of the bitmap ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    40

    fread/fwrite issues

    Ok so I am having trouble editing a bitmap image file. I have successfully copied the header of the bitmap from one file to the other, but now I want to copy the pixel data also. However I want to invert the colours of the image as I do this, therefore i want to have 255-value for each pixel.

    So basically what I want is a loop that reads in one byte at a time, somehow does 255-x, be it through converting to decimal or bits etc. Then write it to the new file.

    I had tried using a for loop like this but it did not work.
    Code:
    fileA=fopen(strng1, "rb");
    fileB=fopen(strng2, "wb");
    fread(fileheader, 1, 52, fileA);
    fwrite(bmaptype, 1, 2, fileB);
    fwrite(fileheader, 52, 1, fileB);
    
    char pixel[12287];
    char pixnew[12287];
    
    for (i=0; i<12287; i++)
        {
            fread(pixel[i], 1, 1, fileA);
            pixnew[i]=(255-atoi(pixel[i]));
            fwrite(pixnew[i], 1, 1, fileB);
    }
    
    fclose(fileA);
    fclose(fileB);
    P.s. The array size is down to the image being 64x64 and 3 bytes for each pixel (RGB)

    Any help would be greatly appreciated. Thank you.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,763
    Wouldn't you invert the bits instead of subtracting from some magic number?

    You would do something like

    pixel[i] = atoi(pixel[i]);
    pixel[i] = ~pixel[i];
    fwrite(pixel[i] , 1 , 1 , fileB);

    Also, arrays start at zero, not one.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You could even skip an instruction
    Code:
    // pixel[i] = ~pixel[i];
    fwrite(~pixel[i] , 1 , 1 , fileB);
    Also, what's the point of having an char array 12287 bytes long if you are going to read and write a character at a time?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    I hadn't originally been using an array, I had just changed that to try it out to see if it would work but it didn't.

    I'm not sure what you mean by '~', i've never seen this used, is it some sort of inversion operator?

    So would you suggest somethin along the lines of
    Code:
    fread(pixel, 1, 1, fileA);
    pixel=atoi(pixel);
    fwrite(~pixel, 1, 1, fileB);

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yes, tilde is the inverse operator. It will flip all bits, same as subtracting 255.

    I don't see the point of the atoi() function either. You should be reading and writing at the binary level, so there are no ascii characters to take into consideration. Lose the atoi() function too.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    What about pixel, i had been defining that as char because it was one byte long, but it could be defined as BYTE couldn't it? I just remembered that its in one of the header files. I had been using that atoi and such, because i thought it would be reading in a character.

    But no, I think i get how it should be working now. This is the loop i'm trying to use at the minute and it won't work, there are no errors, but it crashes when it runs.

    Code:
    BYTE pixel;
    
    for (i=0; i<12287; i++)
        {
            fread(pixel, 1, 1, fileA);
            fwrite(~pixel, 1, 1, fileB);
    }
    I'm assuming it is something to do with this loop as I had it running before when I had been using fprintf in the loop, and haven't really changed anything else. I can show you the rest of the program if you wish? Thank you very much for the help so far, its greatly appreciated. I hope you can continue to help


    EDIT: Ok so I'm just realising that I could actually read in the entire image data and flip it that way without the loop. I'll give it a go now.
    Last edited by ineedmunchies; 01-09-2010 at 07:11 PM.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Don't get the mistaken impression that you can always perform a value reflection by a bit inverse. That only works for certain values, specifically, (2^N)-1 for any positive N. 255 works. But if the range of pixel values was, say, [0,1000] then simply inverting the bits doesn't work.

    I wouldn't muddy the waters by "optimizing" with a bit flip. I think explicitly subtracting from 255 is clearer and less mysterious. The compiler will figure it out anyway.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Aside from all the other issues mentioned, these need to be addresses.

    fread(&pixel[i], 1, 1, fileA);

    Likewise for the fwrite.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Hmm well I have it running now and working with this code:

    Code:
    BYTE image[imagesize1];
    fread(image, 1, imagesize1, fileA);
    for(i=0; i<imagesize1; i++)
        {
            image[i]=~image[i];
        }
    fwrite(image, imagesize1, 1, fileB);
    Where imagesize1 is the size of the image data as read from the header of the image.

    However it crashes when the image is more than 832x832, any idea why this could be? I'm guessing it's some sort of memory allocation problem. I'm happy enough with it as it is, but would like to be able to make it work with larger images.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > However it crashes when the image is more than 832x832, any idea why this could be?
    832*832 is nearly 700KB of memory (your default stack is likely to be 1MB).

    Also, declaring arrays inline (like you appear to do) is only supported in the latest C standard.

    Since all you're doing is reading, modifying and writing, there seems little need to store the whole thing in memory for what is a very short moment of time.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I hate pointers or Pointer Issues
    By bolivartech in forum C Programming
    Replies: 9
    Last Post: 11-14-2009, 11:48 AM
  2. Preprocessor issues
    By sedontane in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2009, 05:58 AM
  3. iterator issues
    By Elkvis in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2009, 09:52 PM
  4. fread/fwrite issues
    By elios in forum C Programming
    Replies: 7
    Last Post: 08-17-2003, 07:44 AM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21