Raw image in header file

G

giovanniparodi79

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

Vladimir S. Oka

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

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).
 
G

giovanniparodi79

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

Vladimir S. Oka

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.

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

John Devereux

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.


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

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

Richard Heathfield

(e-mail address removed) said:
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.

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

jacob navia

(e-mail address removed) a écrit :
Hello everybody is there some utility to convert a raw image in an
header file?
Thanks everybody
Gio

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(infile)) != EOF) {
fprintf(outfile,"0x%02x,",c);
col += 5;
if (col > 70) {
fprintf(outfile,"\n");
col=0;
}
}
fprintf(outfile,"\n};\n");
}
 
P

Peter Shaggy Haywood

Groovy hepcat (e-mail address removed) 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!
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.

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_LINE 10

void instruct(void)
{
const char *const helptext =
"c-embed\n\n"
"Use:\n"
"c-embed <output_prefix> <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"
"declarations 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, "rb");
if(!ifp)
{
fprintf(stderr, "Error opening file \"%s\"\n", argv);
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);
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_LINE))
{
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);
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!

http://alphalink.com.au/~phaywood/
"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 "technically correct" English; but since when was rock & roll "technically correct"?
 
P

Peter Shaggy Haywood

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!
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

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.
int c,col=0;

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

Ummmm... Huh?
fprintf(outfile,"\n");
col=0;
}
}
fprintf(outfile,"\n};\n");

return 0;

--

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

http://alphalink.com.au/~phaywood/
"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 "technically correct" English; but since when was rock & roll "technically correct"?
 
C

CBFalconer

Peter said:
Groovy hepcat (e-mail address removed) wrote:
.... snip ...

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.)
.... 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.com, 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/googlegroupsreply/>
 
J

jacob navia

Peter Shaggy Haywood a écrit :
Groovy hepcat jacob navia was jivin' on Fri, 17 Mar 2006 19:28:08
+0100 in comp.lang.c.

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.

Can you read?

I wrote that there is no error checking!

It is just an outline, not production code
 
M

Micah Cowan

jacob navia said:
Peter Shaggy Haywood a écrit :

Can you read?

I wrote that there is no error checking!

It is just an outline, not production code

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

Micah Cowan

Groovy hepcat (e-mail address removed) 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!


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.

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

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
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top