Shahnaz wrote:[color=blue]
> Can anybody help me?
>
> How to open picture file i.e.bmp of windows through C language?[/color]
Get the filename into a string. Use `fopen` to open the file for
reading (do check whether it succeeded). Use `fread` to read out the
data. You may neet to `fseek`/`fgetpos`/`fsetpos` depending on the file
structure. Do not forget to close the file with `fclose`. Have a go,
and post your code if you have any problems (look up the functions
mentioned in a good C textbook or a manual).
If you're interested in the *format* of BMP files (or other image
formats), this is not the place to ask. Look for groups that deal with
graphics formats.
Shahnaz wrote:[color=blue]
> Can anybody help me?
>
> How to open picture file i.e.bmp of windows through C language?
>
> Many Thanks,
> Shahnaz
>[/color]
Hi Shahnaz,
First you have to learn more about bmp format,
about header ...
Second, this code example only open a picture and read first part of
header..
#include<stdio. h>
int main(void){
FILE *fp;
FILE *sai;
char ch;
int i,j,k;
unsigned short int tipo;
/*---------------- Cabe‡alho da Imagem----------------- */
struct imagem {
unsigned short int type;
unsigned int size;
unsigned short int reserved1,reser ved2;
unsigned int offset;
} header;
/*-----------------Cabe‡alho de mapa de bits------------*/
struct Bmp2 {
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yre solution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolour s; /* Important colours */
} infoheader ;
"Romulo Carneiro" <romulo.ferrer@ gmail.com> writes:[color=blue]
> First you have to learn more about bmp format,
> about header ...[/color]
The specific format is off-topic here.
[color=blue]
> Second, this code example only open a picture and read first part of
> header..
> #include<stdio. h>
>
> int main(void){
>
> FILE *fp;
> FILE *sai;
> char ch;
>
> int i,j,k;
> unsigned short int tipo;
>
> /*---------------- Cabe‡alho da Imagem----------------- */
>
> struct imagem {
> unsigned short int type;
> unsigned int size;
> unsigned short int reserved1,reser ved2;
> unsigned int offset;
> } header;
>
> /*-----------------Cabe‡alho de mapa de bits------------*/
>
> struct Bmp2 {
> unsigned int size; /* Header size in bytes */
> int width,height; /* Width and height of image */
> unsigned short int planes; /* Number of colour planes */
> unsigned short int bits; /* Bits per pixel */
> unsigned int compression; /* Compression type */
> unsigned int imagesize; /* Image size in bytes */
> int xresolution,yre solution; /* Pixels per meter */
> unsigned int ncolours; /* Number of colours */
> unsigned int importantcolour s; /* Important colours */
> } infoheader ;
>
> struct imagem tmp;
>
> /*--------------------- lendo cabeçalho imagem--------------*/
> clrscr();
>
> if ((fp=fopen("syn apsis.bmp", "rb" ))==NULL){
> printf("nÆo , possivel abrir o arquivo");
> exit(1);
> }
>
> while (ch != EOF){
> ch = getc(fp);
> printf("%x",ch) ;
> }[/color]
getc() returns an int, but you're assigning the result to a char
object. See the comp.lang.c FAQ, <http://www.c-faq.com/>,
particularly question 12.1.
[color=blue]
> if (fread (&header, sizeof(struct imagem),1,fp) < 1){[/color]
You've just read the entire input file up to EOF (or tried to), and
printed each character in hexadecimal (though I don't know why). Now
you try to use fread() to read more data from the file.
Assuming you fix that, what makes you think that the types unsigned
short int and unsigned int in your particular C implementation match
the sizes of the corresponding fields in the input file? And how do
you know that the layout of "struct imagem" will match the layout of
the file? Hint: you don't.
[snip]
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
yes thompson, you are right. I would like to show his how he can open
image file this example code . This code works but is only a short
template to begin understand. Thank for show me the details.
On Wednesday 15 March 2006 16:54, Romulo Carneiro opined (in
<1142441699.867 776.234710@z34g 2000cwc.googleg roups.com>):
[color=blue]
> Answer you last question about types...this types are used in bitmap
> image.[/color]
Nobody knows what you're on about, since you didn't quote any context.
Read <http://cfaj.freeshell. org/google/>. This
one <http://clc-wiki.net/wiki/Introduction_to _comp.lang.c> is also
useful.
"Romulo Carneiro" <romulo.ferrer@ gmail.com> writes:[color=blue]
> Answer you last question about types...this types are used in bitmap
> image.[/color]
This doens't really answer the question.
BTW, what question. It's difficult to tell without context. Read
<http://cfaj.freeshell. org/google/>.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
First of all *please* include context with your replies, here I restored
the context manually, by including the appropriate quotes and
attributions from the previous messages.
On 2006-03-15, Romulo Carneiro <romulo.ferrer@ gmail.com> wrote:[color=blue]
>"Keith Thompson" <kst-u@mib.org> wrote:[color=green]
>> Assuming you fix that, what makes you think that the types unsigned
>> short int and unsigned int in your particular C implementation match
>> the sizes of the corresponding fields in the input file? And how do
>> you know that the layout of "struct imagem" will match the layout of
>> the file? Hint: you don't.[/color]
>
> Answer you last question about types...this types are used in bitmap
> image.
>[/color]
No they are not. Any binary file format specifies fields in respect to
their size. You use an unsigned short
for some field of the bmp structure with the implicit assumption that it
would be 2 bytes. While this might be true on your particular
platform, that does not make it true in general. The C standard only
specifies that an unsigned short must have a USHRT_MAX >= 65535. So when
you take your code to a platform where unsigned shorts are 32bits long,
you are in trouble.
Also, by reading the whole struct at once with an fread() you assume
that the struct is layed out in memory in exactly the same way as it is
in the bmp file format. This of course is not a portable assumption as a
compiler is free to include padding bits between the elements of a
structure.
Comment