Opening picture file in C

S

Shahnaz

Can anybody help me?

How to open picture file i.e.bmp of windows through C language?

Many Thanks,
Shahnaz
 
V

Vladimir S. Oka

Shahnaz said:
Can anybody help me?

How to open picture file i.e.bmp of windows through C language?

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.
 
A

August Karlstrom

Shahnaz said:
Can anybody help me?

How to open picture file i.e.bmp of windows through C language?

Many Thanks,
Shahnaz

#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
char cmd[256];

if (argc > 1) {
strcpy(cmd, "start ");
strcat(cmd, argv[1]);
system(cmd);
}
return 0;
}


August
 
R

Romulo Carneiro

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,reserved2;
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,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} infoheader ;

struct imagem tmp;

/*--------------------- lendo cabeçalho imagem--------------*/
clrscr();

if ((fp=fopen("synapsis.bmp", "rb" ))==NULL){
printf("nÆo , possivel abrir o arquivo");
exit(1);
}

while (ch != EOF){
ch = getc(fp);
printf("%x",ch);
}

if (fread (&header, sizeof(struct imagem),1,fp) < 1){
printf("o arquivo nÆo pode ser lido");
fclose(fp);
exit(1);
}

fseek(&header,0,SEEK_SET);
printf("Lendo!!\n");
printf("%x\n",&header.type);
printf("%x %x\n",&header.reserved1,&header.reserved2);
printf("%x\n",&header.size);
printf("%x\n",&header.offset);

/*------------lendo mapas de bits da Imagem------------------*/


if (fread (&infoheader, sizeof(struct Bmp2),1, fp) < 1 ){
printf("o mapa de bits nÆo pode ser lido");
fclose(fp);
exit(1);
}
printf("\n");
fseek(&infoheader,0,SEEK_SET);
/*printf("%x\n", &infoheader.size);*/


}
 
K

Keith Thompson

Romulo Carneiro said:
First you have to learn more about bmp format,
about header ...

The specific format is off-topic here.
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,reserved2;
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,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} infoheader ;

struct imagem tmp;

/*--------------------- lendo cabeçalho imagem--------------*/
clrscr();

if ((fp=fopen("synapsis.bmp", "rb" ))==NULL){
printf("nÆo , possivel abrir o arquivo");
exit(1);
}

while (ch != EOF){
ch = getc(fp);
printf("%x",ch);
}

getc() returns an int, but you're assigning the result to a char
if (fread (&header, sizeof(struct imagem),1,fp) < 1){

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]
 
J

John Tsiombikas (Nuclear / Mindlapse)

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.


Answer you last question about types...this types are used in bitmap
image.

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.
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top