How to copy bmp 8bit file

hi all!

I'm trying to:
1. read a bmp grey scale 8 bit file image, and extract data matrix
2. create a new bmp file with the same header and data matrix of the first
but I haven't a copy of the first file image.

I know that I can do the file copy with a while EOF charbychar, but I must do this type of copy, because I must modify image data matrix and after write it in the new bmp file.

To extract data matrix from the original file, after defining bitmap header:
1
2
3
4
5
6
7
8
9
10
    fseek(fd, hdr.offset - sizeof(hdr) - sizeof(infoHdr), SEEK_CUR);

    for(y = 0; y < infoHdr.height; y++){
        for(x = 0; x < infoHdr.width; x++){
            (*data_matrix)[(y * infoHdr.width + x)] = fgetc(fd); 
        }
        //padding
        for(x = 0; x < (4 - (3 * infoHdr.width) % 4) % 4; x++)
            fgetc(fd);
    }


To create a copy of the original file image:
1
2
3
4
5
6
    a=fread(&hdr, sizeof(BMPHeader), 1, origine);
    fwrite(&hdr, sizeof(BMPHeader), 1, copy);
    a=fread(&infoHdr, sizeof(BMPInfoHeader), 1, origine);
    fwrite(&infoHdr, sizeof(BMPInfoHeader), 1, copy);
    fseek(copy, sizeof(BMPHeader) + sizeof(BMPInfoHeader), SEEK_SET);
    fwrite(new_data_matrix,256*256*sizeof(unsigned char),1,copy);

but I don't get the same image, also if data_matrix==new_data_matrix

thank you very much for help!
Bye

ps:sorry for my bad english! ;)
anyone can help me?
If new_data_matrix is the pointer that points to the data being written into the new file, you need to make new_data_matrix=data_matrix

However, why would you do that when data_matrix already points to the data? Just read everything into data_matrix, as you already are doing, and then write out from data_matrix.
I have used == and not = to say that also if use the same matrix, my code don't work.

I need to do this, because in new_data_matrix will be data_matrix modded with other operation.

Now for test I use new_data_matrix=data_matrix, and don't work fine.

But in a 8bit grey scale bmp, is each pixel 1 matrix element ?

Do I insert correctly pixel image in data_matrix from the original file?

thanks!
I have found the problem, it's the palette.
In the copy.bmp file there isn't the entire palette, but only a part.

How to solve this problem?
I have defined the bmp header in this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
typedef struct{
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char luminance;
} palette_struct;

/*********************** STRUTTURA BITMAP **********************/
typedef struct{
    short type;            /* 2 bytes */
    int size;              /* 4 bytes */
    short reserved1;       /* 2 bytes */
    short reserved2;       /* 2 bytes */
    int offset;            /* 4 bytes */
} BMPHeader;               /*14 bytes TOTALI */

typedef struct{
    int size;              /* 4 bytes */
    int width;		   /* 4 bytes */
    int height;            /* 4 bytes */
    short planes;          /* 2 bytes */
    short bitsPerPixel;    /* 2 bytes */
    unsigned compression;  /* 4 bytes */
    unsigned imageSize;    /* 4 bytes */
    int xPelsPerMeter;     /* 4 bytes */
    int yPelsPerMeter;     /* 4 bytes */
    int clrUsed;           /* 4 bytes */
    int clrImportant;      /* 4 bytes */
    palette_struct palette;/* 4 bytes */
} BMPInfoHeader;           /*44 bytes TOTALI */
/*********************** STRUTTURA BITMAP **********************/
Topic archived. No new replies allowed.