Opening picture file in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shahnaz

    Opening picture file in C

    Can anybody help me?

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

    Many Thanks,
    Shahnaz

  • Vladimir S. Oka

    #2
    Re: Opening picture file in C


    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.

    --
    BR, Vladimir

    Comment

    • Chris Dollin

      #3
      Re: Opening picture file in C

      Shahnaz wrote:
      [color=blue]
      > How to open picture file i.e.bmp of windows through C language?[/color]

      Use `fopen`.

      --
      Chris "sparqling" Dollin
      "Who do you serve, and who do you trust?"

      Comment

      • August Karlstrom

        #4
        Re: Opening picture file in C

        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]

        #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

        --
        I am the "ILOVEGNU" signature virus. Just copy me to your
        signature. This email was infected under the terms of the GNU
        General Public License.

        Comment

        • Romulo Carneiro

          #5
          Re: Opening picture file in C

          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 ;

          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) ;
          }

          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.r eserved1,&heade r.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(&infohead er,0,SEEK_SET);
          /*printf("%x\n", &infoheader.siz e);*/


          }

          Comment

          • Keith Thompson

            #6
            Re: Opening picture file in C

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

            Comment

            • Romulo Carneiro

              #7
              Re: Opening picture file in C

              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.

              Comment

              • Romulo Carneiro

                #8
                Re: Opening picture file in C

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

                Comment

                • Vladimir S. Oka

                  #9
                  Re: Opening picture file in C

                  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.


                  --
                  BR, Vladimir

                  Victory uber allies!

                  Comment

                  • Keith Thompson

                    #10
                    Re: Opening picture file in C

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

                    Comment

                    • John Tsiombikas (Nuclear / Mindlapse)

                      #11
                      Re: Opening picture file in C

                      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.

                      --
                      John Tsiombikas (Nuclear / Mindlapse)
                      nuclear@siggrap h.org

                      Comment

                      Working...