Thread: Exporting bmp file

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    9

    Exporting bmp file

    Hi. I don't understand why I can't export this bmp file properly. I just basically got a bmp file name from the user, transferred the data to a double pointer and copied it back to a new file to be saved.
    But when I run it, and try to open the new image, it says, 'invalid image format'. I don't know what to do anymore
    Help pls.

    ifile is where i fopened the input file which is lena.bmp
    Thanks

    Code:
    char **idata = malloc( sizeof( char *)*( (*iheader).height) );
        int irow, icol, count = 0;
        int a = 0;
        for ( irow = 0; irow < (*iheader).height; irow++ ){
            idata[irow] = malloc( sizeof(char)*( roundto4s( (iheader->width)*3 ) ) );
        }
        //STORING IMAGE DATA FOR REALZZZ
        for ( irow = 0; irow < ( *iheader ).height; irow++ ){
            for ( icol = 0; icol < roundto4s( ( ( *iheader ).width )*3); icol++ ){
                a = fread( &(idata[irow][icol]), 1, 1, ifile);
                if ( a == 1 )
                    count++;
            }
        }
        if ( count != iheader->sizeimage)
            printf("a = %d. HAHAHAHA loser.\n", a);
        
        printf("%c", idata[100][100]);
        
        
        //EXPORTING IMAGE
        FILE *nfile;
        count = 0;
        nfile = fopen("lena_edited.bmp", "w+b");
        fseek(ifile, 0, SEEK_SET);
        a = fwrite(ifile, 1, fheader->offset, nfile);
            if ( a != fheader->offset )
                printf("a = %d. HAHAHAHA loser.\n", a);
        fseek(ifile, fheader->offset, SEEK_SET);
        a = fwrite(ifile, 1, iheader->sizeimage, nfile);
            if ( a != iheader->sizeimage )
                printf("a = %d. HAHAHAHA loser.\n", a);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    You don't appear to read (or write) the padding bytes at the end of each row.
    BMP file format - Wikipedia, the free encyclopedia

    > printf("%c", idata[100][100]);
    This makes no sense at all.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    9
    Oh, the printf thing was just out of curiousity what would appear. please ignore it.

    Code:
    for ( irow = 0; irow < (*iheader).height; irow++ ){
            idata[irow] = malloc( sizeof(char)*( roundto4s( (iheader->width)*3 ) ) );
        }
    that part make sures that i allocate memory that's a multiple of 4 doesnt it? Since I made a roundto4s( int a ); function that rounds stuff to multiples of four.

    So aren't I reading or writing the padding too?

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You need to post the smallest runnable program that demonstrates the problem so we can see all the details and actually run it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help in exporting to a dll
    By Shasoosh in forum C Programming
    Replies: 8
    Last Post: 01-15-2010, 06:59 PM
  2. Question about exporting to CSV file
    By Richard Hsiao in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2005, 04:07 AM
  3. Exporting
    By arti_valekar in forum C++ Programming
    Replies: 2
    Last Post: 03-01-2005, 09:46 PM
  4. Replies: 2
    Last Post: 07-15-2004, 06:03 AM
  5. Exporting to POV-ray
    By gooey kablooey in forum C++ Programming
    Replies: 4
    Last Post: 03-17-2004, 01:11 PM

Tags for this Thread