Raw image in header file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • giovanniparodi79@yahoo.it

    Raw image in header file

    Hello everybody is there some utility to convert a raw image in an
    header file?
    Thanks everybody
    Gio

  • Vladimir S. Oka

    #2
    Re: Raw image in header file

    On Friday 17 March 2006 15:28, giovanniparodi7 9@yahoo.it opined (in
    <1142609304.893 761.195710@v46g 2000cwv.googleg roups.com>):[color=blue]
    > Hello everybody is there some utility to convert a raw image in an
    > header file?[/color]

    I don't think I understand exactly what you want. You may want to
    clarify. However, I have a sneaky suspicion that what you're asking is
    off-topic (unless you're asking for a portable way to implement this in
    a C header file, in which case you still want to be more specific).

    --
    BR, Vladimir

    He walks as if balancing the family tree on his nose.

    Comment

    • giovanniparodi79@yahoo.it

      #3
      Re: Raw image in header file

      Sorry I would like to take a raw image file and convert it to an header
      file, generating an array that contains in each cell the gray level
      that corresponds to the pixel.
      It will be useful in order to avoid read from hd that on my target
      platform (an embedded system) can become very slow.
      Hope this is not ot.

      Comment

      • John Devereux

        #4
        Re: Raw image in header file

        giovanniparodi7 9@yahoo.it writes:
        [color=blue]
        > Sorry I would like to take a raw image file and convert it to an header
        > file, generating an array that contains in each cell the gray level
        > that corresponds to the pixel.
        > It will be useful in order to avoid read from hd that on my target
        > platform (an embedded system) can become very slow.
        > Hope this is not ot.[/color]


        <http://www.embedded.co m/1999/9907/9907feat1.htm>

        Discusses how to convert bitmaps (and fonts) into C code.

        --

        John Devereux

        Comment

        • Vladimir S. Oka

          #5
          Re: Raw image in header file

          On Friday 17 March 2006 16:18, giovanniparodi7 9@yahoo.it opined (in
          <1142612338.416 396.143550@i39g 2000cwa.googleg roups.com>):
          [color=blue]
          > Sorry I would like to take a raw image file and convert it to an
          > header file, generating an array that contains in each cell the gray
          > level that corresponds to the pixel.
          > It will be useful in order to avoid read from hd that on my target
          > platform (an embedded system) can become very slow.
          > Hope this is not ot.[/color]

          Well, I don't know of any such utility, but I believe that, quite
          recently, it was mentioned in this group that there exists image format
          that is already close to what you require. Search the group with Google
          and you may strike lucky. (BTW, it is OT).

          --
          BR, Vladimir

          WHERE CAN THE MATTER BE
          Oh, dear, where can the matter be
          When it's converted to energy?
          There is a slight loss of parity.
          Johnny's so long at the fair.

          Comment

          • Richard Heathfield

            #6
            Re: Raw image in header file

            giovanniparodi7 9@yahoo.it said:
            [color=blue]
            > Sorry I would like to take a raw image file and convert it to an header
            > file, generating an array that contains in each cell the gray level
            > that corresponds to the pixel.[/color]

            <OT>
            Have a look at the xpm file format.
            </OT>

            --
            Richard Heathfield
            "Usenet is a strange place" - dmr 29/7/1999

            email: rjh at above domain (but drop the www, obviously)

            Comment

            • jacob navia

              #7
              Re: Raw image in header file

              giovanniparodi7 9@yahoo.it a écrit :[color=blue]
              > Hello everybody is there some utility to convert a raw image in an
              > header file?
              > Thanks everybody
              > Gio
              >[/color]

              Not tested, no error checking, here is a way to do it:
              usage: first arg is name of the image file, second is name of header file
              #include <stdio.h>
              int main(int argc,char *argv[])
              {
              FILE *infile = fopen(argv[1],"rb"); // open image
              FILE *outfile = fopen(argv[2],"w"); // open header
              int c,col=0;

              fprintf(outfile ,"unsigned char myimage[] = {\n");
              while ((c=fgetc(infil e)) != EOF) {
              fprintf(outfile ,"0x%02x,",c );
              col += 5;
              if (col > 70) {
              fprintf(outfile ,"\n");
              col=0;
              }
              }
              fprintf(outfile ,"\n};\n");
              }

              Comment

              • giovanniparodi79@yahoo.it

                #8
                Re: Raw image in header file

                Thanks a lot to everyone helped me,
                Gio

                Comment

                • Peter Shaggy Haywood

                  #9
                  Re: Raw image in header file

                  Groovy hepcat giovanniparodi7 9@yahoo.it was jivin' on 17 Mar 2006
                  08:18:58 -0800 in comp.lang.c.
                  Re: Raw image in header file's a cool scene! Dig it!
                  [color=blue]
                  >Sorry I would like to take a raw image file and convert it to an header
                  >file, generating an array that contains in each cell the gray level
                  >that corresponds to the pixel.
                  >It will be useful in order to avoid read from hd that on my target
                  >platform (an embedded system) can become very slow.
                  >Hope this is not ot.[/color]

                  Such things don't belong in headers. They belong in the main part of
                  a translation unit (ie., the .c file). Headers should contain external
                  declarations of functions and variables, macro definitions and such.
                  But, to your question, how do you store an image (or other) file in
                  a C source file? It's really quite easy, believe it or not. All you
                  have to do is read each byte of the file and use its value as an
                  initialiser for an array. "Tedious, time consuming and error prone," I
                  hear you say? Au contraire. It's extremely easy and efficient, if you
                  let the computer do it. Now I hear you ask, "But how do I do that?"
                  You use a simple utility that reads a file and outputs C source code
                  containing an array initialised with the contents of the file. Your
                  next question: "Where do I get a utility like that?"
                  You write one. Yes, that's right. You write your own utility. It's
                  really quite trivial. But if you feel that such a beastie is beyond
                  your skills at this time, you can use the following program, which I
                  wrote and have used. I call it c-embed. (I realise someone already
                  posted a program like this, but it was untested and not as good. If
                  you want something that has at least been tried out, use my program.)

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

                  #define NELEMENTS_PER_L INE 10

                  void instruct(void)
                  {
                  const char *const helptext =
                  "c-embed\n\n"
                  "Use:\n"
                  "c-embed <output_prefi x> <file_1> [<file_2> [<file_3>"
                  " [...<file_n>]]] > <output_file>\n \n"
                  "c-embed takes one or more filenames on the command line,\n"
                  "reads in those files, converts them to C (or C++) arrays\n"
                  "then outputs each array to <output_prefix> .c and puts extern\n"
                  "declaratio ns of these arrays in <output_prefix> .h, thus enabling"
                  " you\n"
                  "to embed files within programs written in C (or C++) as"
                  " arrays.\n"
                  "<output_prefix > may be a dash, in which case arrays will be"
                  " output\n"
                  "to standard output and no header will be created.\n"
                  "The arrays will be called embed_N, where N is the number of"
                  " the\n"
                  "file. For example, the first file will be embedded as embed_1,"
                  " the\n"
                  "second as embed_2, the third as embed_3, etc.\n";

                  fputs(helptext, stderr);
                  }

                  int main(int argc, char **argv)
                  {
                  size_t i;
                  FILE *cfp, *hfp;
                  char cfn[FILENAME_MAX + 1];
                  char hfn[FILENAME_MAX + 1];

                  if(3 > argc)
                  {
                  instruct();
                  return EXIT_FAILURE;
                  }

                  if(0 != strcmp("-", argv[1]))
                  {
                  sprintf(cfn, "%s.c", argv[1]);
                  cfp = fopen(cfn, "w");
                  if(!cfp)
                  {
                  fprintf(stderr, "Error opening file \"%s\"\n", argv[1]);
                  return EXIT_FAILURE;
                  }

                  sprintf(hfn, "%s.h", argv[1]);
                  hfp = fopen(hfn, "w");
                  if(!hfp)
                  {
                  fprintf(stderr, "Error opening file \"%s\"\n", argv[1]);
                  fclose(cfp);
                  return EXIT_FAILURE;
                  }
                  }
                  else
                  {
                  cfp = stdout;
                  hfp = NULL;
                  }

                  for(i = 2; i < argc; i++)
                  {
                  FILE *ifp;

                  ifp = fopen(argv[i], "rb");
                  if(!ifp)
                  {
                  fprintf(stderr, "Error opening file \"%s\"\n", argv[i]);
                  if(hfp)
                  {
                  fclose(hfp);
                  }
                  fclose(cfp);
                  return EXIT_FAILURE;
                  }
                  else
                  {
                  size_t j, n;
                  int ch;

                  fprintf(cfp, "/* embed_%lu contains file \"%s\". */\n",
                  (unsigned long)i - 1, argv[i]);
                  fprintf(cfp, "const unsigned char embed_%lu[] =\n{",
                  (unsigned long)i - 1);

                  for(j = 0, n = 0; EOF != (ch = fgetc(ifp)); j++, n++)
                  {
                  if(0 == (j % NELEMENTS_PER_L INE))
                  {
                  j = 0;
                  fputc('\n', cfp);
                  fputc('\t', cfp);
                  }
                  else
                  {
                  fputc(' ', cfp);
                  }

                  fprintf(cfp, "0x%2.2x,", (unsigned)ch);
                  }

                  fputs("\n};\n\n ", cfp);

                  if(hfp)
                  {
                  fprintf(hfp, "/* embed_%lu contains file \"%s\". */\n",
                  (unsigned long)i - 1, argv[i]);
                  fprintf(hfp, "extern const unsigned char embed_%lu[%lu];\n\n",
                  (unsigned long)i - 1, (unsigned long)n);
                  }
                  }

                  fclose(ifp);
                  }

                  fclose(cfp);
                  if(hfp)
                  {
                  fclose(hfp);
                  }

                  return 0;
                  }

                  Using this program is quite simple. Suppose you want to embed a file
                  named image.jpg in a C file named image.c. You do this:

                  c-embed image image.jpg

                  This creates image.c and puts an array of const unsigned char called
                  embed_1 in it, initialised with the contents of image.jpg. It also
                  creates image.h, and puts an extern declaration of the array in there.
                  You can embed any file this way, even multiple files.

                  --

                  Dig the even newer still, yet more improved, sig!


                  "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
                  I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?

                  Comment

                  • Peter Shaggy Haywood

                    #10
                    Re: Raw image in header file

                    Groovy hepcat jacob navia was jivin' on Fri, 17 Mar 2006 19:28:08
                    +0100 in comp.lang.c.
                    Re: Raw image in header file's a cool scene! Dig it!
                    [color=blue]
                    >Not tested, no error checking, here is a way to do it:
                    >usage: first arg is name of the image file, second is name of header file[/color]
                    [color=blue]
                    >#include <stdio.h>
                    >int main(int argc,char *argv[])
                    >{
                    > FILE *infile = fopen(argv[1],"rb"); // open image
                    > FILE *outfile = fopen(argv[2],"w"); // open header[/color]

                    Not good taking for granted that there will be 2 command line
                    arguments. Also not good taking for granted that the fopen() calls
                    will succeed. They may not.
                    [color=blue]
                    > int c,col=0;
                    >
                    > fprintf(outfile ,"unsigned char myimage[] = {\n");
                    > while ((c=fgetc(infil e)) != EOF) {
                    > fprintf(outfile ,"0x%02x,",c );
                    > col += 5;
                    > if (col > 70) {[/color]

                    Ummmm... Huh?
                    [color=blue]
                    > fprintf(outfile ,"\n");
                    > col=0;
                    > }
                    > }
                    > fprintf(outfile ,"\n};\n");[/color]

                    return 0;[color=blue]
                    >}[/color]

                    --

                    Dig the even newer still, yet more improved, sig!


                    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
                    I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?

                    Comment

                    • CBFalconer

                      #11
                      Re: Raw image in header file

                      Peter Shaggy Haywood wrote:[color=blue]
                      > Groovy hepcat giovanniparodi7 9@yahoo.it wrote:
                      >[/color]
                      .... snip ...[color=blue]
                      >
                      > You write one. Yes, that's right. You write your own utility. It's
                      > really quite trivial. But if you feel that such a beastie is beyond
                      > your skills at this time, you can use the following program, which I
                      > wrote and have used. I call it c-embed. (I realise someone already
                      > posted a program like this, but it was untested and not as good. If
                      > you want something that has at least been tried out, use my program.)
                      >[/color]
                      .... snip ...

                      FYI, I get the following errors:

                      c-embed.c: In function `instruct':
                      c-embed.c:56: warning: string length `702' is greater than the
                      length `509' ISO C89 compilers are required to support
                      c-embed.c: In function `main':
                      c-embed.c:99: warning: comparison between signed and unsigned

                      --
                      "If you want to post a followup via groups.google.c om, don't use
                      the broken "Reply" link at the bottom of the article. Click on
                      "show options" at the top of the article, then click on the
                      "Reply" at the bottom of the article headers." - Keith Thompson
                      More details at: <http://cfaj.freeshell. org/google/>
                      Also see <http://www.safalra.com/special/googlegroupsrep ly/>


                      Comment

                      • jacob navia

                        #12
                        Re: Raw image in header file

                        Peter Shaggy Haywood a écrit :[color=blue]
                        > Groovy hepcat jacob navia was jivin' on Fri, 17 Mar 2006 19:28:08
                        > +0100 in comp.lang.c.[color=green]
                        >>Not tested, no error checking, here is a way to do it:
                        >>usage: first arg is name of the image file, second is name of header file[/color]
                        >
                        > Not good taking for granted that there will be 2 command line
                        > arguments. Also not good taking for granted that the fopen() calls
                        > will succeed. They may not.
                        >
                        >[/color]

                        Can you read?

                        I wrote that there is no error checking!

                        It is just an outline, not production code

                        Comment

                        • Micah Cowan

                          #13
                          Re: Raw image in header file

                          jacob navia <jacob@jacob.re mcomp.fr> writes:
                          [color=blue]
                          > Peter Shaggy Haywood a écrit :[color=green]
                          > > Groovy hepcat jacob navia was jivin' on Fri, 17 Mar 2006 19:28:08
                          > > +0100 in comp.lang.c.[color=darkred]
                          > >>Not tested, no error checking, here is a way to do it:
                          > >>usage: first arg is name of the image file, second is name of header file[/color]
                          > > Not good taking for granted that there will be 2 command line
                          > > arguments. Also not good taking for granted that the fopen() calls
                          > > will succeed. They may not.
                          > >[/color]
                          >
                          > Can you read?
                          >
                          > I wrote that there is no error checking!
                          >
                          > It is just an outline, not production code[/color]

                          And so somehow that means we shouldn't post improvements upon finding flaws?

                          Comment

                          • Micah Cowan

                            #14
                            Re: Raw image in header file

                            phaywood@alphal ink.com.au.NO.S PAM (Peter "Shaggy" Haywood) writes:
                            [color=blue]
                            > Groovy hepcat giovanniparodi7 9@yahoo.it was jivin' on 17 Mar 2006
                            > 08:18:58 -0800 in comp.lang.c.
                            > Re: Raw image in header file's a cool scene! Dig it!
                            >[color=green]
                            > >Sorry I would like to take a raw image file and convert it to an header
                            > >file, generating an array that contains in each cell the gray level
                            > >that corresponds to the pixel.
                            > >It will be useful in order to avoid read from hd that on my target
                            > >platform (an embedded system) can become very slow.
                            > >Hope this is not ot.[/color]
                            >
                            > Such things don't belong in headers. They belong in the main part of
                            > a translation unit (ie., the .c file). Headers should contain external
                            > declarations of functions and variables, macro definitions and such.
                            > But, to your question, how do you store an image (or other) file in
                            > a C source file? It's really quite easy, believe it or not. All you
                            > have to do is read each byte of the file and use its value as an
                            > initialiser for an array. "Tedious, time consuming and error prone," I
                            > hear you say? Au contraire. It's extremely easy and efficient, if you
                            > let the computer do it. Now I hear you ask, "But how do I do that?"
                            > You use a simple utility that reads a file and outputs C source code
                            > containing an array initialised with the contents of the file. Your
                            > next question: "Where do I get a utility like that?"
                            > You write one. Yes, that's right. You write your own utility. It's
                            > really quite trivial.[/color]

                            Or, you could use a file format that, itself, is C code. X pixel map
                            (.xpm) files come to mind. You could use GIMP to output these.

                            Of course, this is an indexed color format, and would still require an
                            interpreter of some sort to convert it to a more native format
                            (assuming you don't use Xlib). You can get a library at
                            http://koala.ilog.fr/lehors/xpm.html . Note that there was a security
                            vulnerability in this library at some point, and I do not know whether
                            it had been addressed. You'll have to download it and check the
                            release notes (or better, the source code).

                            A more readable, C-code image format, you'd be quite
                            hard-pressed to find. Here is the complete contents of an .xpm file,
                            which consists of a white box with an X in it: one stroke black and
                            the other blue.

                            /* XPM */
                            static char * test_xpm[] = {
                            "16 16 3 1",
                            " c #000000",
                            ". c #FFFFFF",
                            "+ c #0000FF",
                            " ...........+++" ,
                            " .........++++",
                            " .......+++++",
                            ". .....+++++.",
                            ".. ...+++++..",
                            "... .+++++...",
                            ".... +++++....",
                            "..... +++++.....",
                            ".....+++++.... ..",
                            "....+++++ .....",
                            "...+++++ ...",
                            "..++++... ..",
                            ".++++..... .",
                            "++++...... . ",
                            "+++....... .. ",
                            "++........ ... "};

                            As a bonus, you don't usually have to worry about exceeding the
                            maximum supported string lenth for this format (unless the row length
                            itself does so).

                            Comment

                            Working...