structure's shape

R

ratrak

Hi All

I'm writing a code to manipulate BMP files and somethink isnt work as
expected.
In print the bitmap file:

struct Pixel
{
char Blue;
char Green;
char Red;
char Reserved;
} *pixel;

pixel=(struct Pixel*)malloc(sizeof(struct
Pixel)*infoheader.BiWidth*infoheader.BiHeight);

fseek(filein,header.BfOffSetBits,SEEK_SET);

fread(pixel,infoheader.BiWidth*infoheader.BiHeight,sizeof(struct
Pixel),filein);

fseek(fileout,header.BfOffSetBits,SEEK_SET);

fwrite(pixel,infoheader.BiWidth*infoheader.BiHeight,sizeof(struct
Pixel),fileout);

the result is an black picture or some image wich can't be opened by
the view.
Maybe this hapen because some character of the structure shape have
been printed?

Thanks
Rafael
 
S

Seebs

I'm writing a code to manipulate BMP files and somethink isnt work as
expected.

Your question has virtually nothing to do with C, and a great deal to do
with the BMP format, so far as I can tell.
pixel=(struct Pixel*)malloc(sizeof(struct
Pixel)*infoheader.BiWidth*infoheader.BiHeight);

This cast is probably not needed. Also, you really ought to be writing
with better use of whitespace:
pixel = malloc(sizeof(*pixel) *
infoheader.BiWidth *
infoheader.BiHeight);
fread(pixel,infoheader.BiWidth*infoheader.BiHeight,sizeof(struct
Pixel),filein);

I don't see any check here that you actually got the data you expected.
the result is an black picture or some image wich can't be opened by
the view.
Maybe this hapen because some character of the structure shape have
been printed?

This doesn't make any sense. It really doesn't matter what's in the structure
in this case, because you're just reading and writing blocks of data. As
long as the structure ended up with a size of 4, you're reading and writing
4 bytes times width times height, and the structure definition is completely
irrelevant to this, so far as I can tell.

-s
 
J

Jens Thoms Toerring

ratrak said:
I'm writing a code to manipulate BMP files and somethink isnt work as
expected.
In print the bitmap file:
struct Pixel
{
char Blue;
char Green;
char Red;
char Reserved;
} *pixel;
pixel=(struct Pixel*)malloc(sizeof(struct
Pixel)*infoheader.BiWidth*infoheader.BiHeight);

The cast in front of malloc() isn't needed and keeps the
compiler from complaining when <stdlib.h> hasn't been in-
cluded, which in turn may result in a nasty bug (at least
on machines with dedicated address and data registers or
where the size of a pointer differs from that of an int).
fseek(filein,header.BfOffSetBits,SEEK_SET);
fread(pixel,infoheader.BiWidth*infoheader.BiHeight,sizeof(struct
Pixel),filein);

Be careful: there might be padding between the members of the
structure, i.e. sizeof(struct Pixel) may be larger than 4. Pro-
bably not too much of a problem here (and not too likely on most
modern machines) but if you did read in the file header in the
same way (i.e. by fread()ing into a structure) then things may
look more critical and the width and height as well as the off-
set you use may be wrong. (BTW, why is it called "OffSetBits"
when the offset given in the file is in bytes?)

In any case you should check how many "items" you did read
from the file (and for that you should exchange the second
and third argument in the fread() call since the first on
tells how large an item (a struct Pixel) is and the second
is how many items you want to read). If this isn't equal
to the number of items you expected to read then something
fishy is going on.
fseek(fileout,header.BfOffSetBits,SEEK_SET);
fwrite(pixel,infoheader.BiWidth*infoheader.BiHeight,sizeof(struct
Pixel),fileout);

Also here the second and third argument should be the other way
round (and you should avoid writing more than you read). Other-
wise all you do here (at least as shown) is copying a certain
amount of bytes from one file to another.
the result is an black picture or some image wich can't be opened by
the view.

Assuming that nothing went wrong with the above copying (i.e.
the return value of fread() is what you expected and is equal
to the return value of fwrite()) then the most likely expla-
nation seems to be that there were some issues with copying
the file header(s).
Maybe this hapen because some character of the structure shape have
been printed?

What is a "structure shape" and how should it get "printed'?

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top