Thread: writing an image

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    writing an image

    I'm given a bmp and I'm trying to create a new image and output that to bmp file. I'm not sure if I'm doing it correctly.

    Bmp consist of three 3structs:

    Header, InfoHeader, Data

    I'm able to get the Header,InfoHeader written to the file, but the data is what alludes me. I just get a black image with random lines. All the numbers such as pixeltype,size,etc all match up but the image just won't come out to the orginal. Wondering if there are any image experts that might know something? Thanks

    Code:
    Bool WriteImage(Image im, String filename)
    {
        FILE *op;
        char *outname;
        outname = filename;
    
        op = fopen(outname,"w");
    
        int s1 = fwrite((void *)&im.head,sizeof(im.head),1,op);
        fflush(op);
    
        int s2 = fwrite((void *)&im.infoheader,sizeof(im.infoheader),1,op);
        fflush(op);
    
        int s3 = fwrite((void *)&im.data,GetTotalSize(im),1, op);
        fflush(op);
    
        fclose(op);
    
        return(s1 && s2 && s3);
    }

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Are you modifying the image in any way? Bear in mind that BMP image data is doubleword-aligned, so each line must be exactly divisible by 4. If not, you'll need to add padding bytes to each line.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    No, the image isn't getting changed at all. Yes our professo told us about the padding. He said with our bmp that he gave us, we won't have to compensate for that issue. All information from header and infoheader seem correct. We get 'an' image, but it's all black.

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    In which case your problem is likely to lie outside the WriteImage function (i.e. there's something wrong with im.data).

    You shouldn't need to cast to void * either, it literally means "pointer to anything" so any pointer will resolve correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. open and read bmp image files
    By cnewbie85 in forum C Programming
    Replies: 2
    Last Post: 05-19-2009, 01:36 AM
  2. Prob with padding in BMP image files
    By tin in forum Game Programming
    Replies: 2
    Last Post: 01-09-2006, 08:23 AM
  3. making a proper image for floppy
    By EvBladeRunnervE in forum Tech Board
    Replies: 4
    Last Post: 07-31-2004, 11:27 PM
  4. Memory Allocation in Intell Image processing liberary
    By nisar in forum Windows Programming
    Replies: 0
    Last Post: 01-12-2003, 07:29 AM
  5. Writing Bitmap Files
    By HappyDude in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2001, 05:48 PM