473,657 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Raw image in header file

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

Mar 17 '06 #1
13 12099
On Friday 17 March 2006 15:28, gi************* *@yahoo.it opined (in
<11************ **********@v46g 2000cwv.googleg roups.com>):
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).

--
BR, Vladimir

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

Mar 17 '06 #2
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.

Mar 17 '06 #3
gi************* *@yahoo.it writes:
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.co m/1999/9907/9907feat1.htm>

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

--

John Devereux
Mar 17 '06 #4
On Friday 17 March 2006 16:18, gi************* *@yahoo.it opined (in
<11************ **********@i39g 2000cwa.googleg roups.com>):
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.

Mar 17 '06 #5
gi************* *@yahoo.it 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>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 17 '06 #6
gi************* *@yahoo.it 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(infil e)) != EOF) {
fprintf(outfile ,"0x%02x,",c );
col += 5;
if (col > 70) {
fprintf(outfile ,"\n");
col=0;
}
}
fprintf(outfile ,"\n};\n");
}
Mar 17 '06 #7
Thanks a lot to everyone helped me,
Gio

Mar 18 '06 #8
Groovy hepcat gi************* *@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!
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_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!

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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Mar 21 '06 #9
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(infil e)) != 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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Mar 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2368
by: Michael J. Astrauskas | last post by:
I have a script, which I've called test-loadpic.php and some pages reference it by means of <img src="test-loadpic.php?sourcepic=$picNum"> where $picNum stores a number. This part itself works fine. test-loadpic.php uses the "sourcepic" GET variable to reference a file on disk, get it's file/MIME type, transmit the headers and then the raw image. The important code is below:
6
3550
by: Ken | last post by:
How does IE6 control the display of images? I change the content of a image file image1.jpg without changing the file name. Then jump to a new page to display it. IE6 does not displays the original picture, not the new image. If I press F5, the correct image is displayed. I tried:
7
3585
by: theonlydrayk | last post by:
the script that show image is : <?php include('dbinfo.inc.php'); mysql_connect($localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "SELECT file,filesize,filetype FROM user WHERE id=1;"; $result=mysql_query($query); mysql_close();
6
3312
by: QuasiChameleon | last post by:
Hi, I'm trying to create a grayscale image class that reads and writes grayscale Targa format. This works well with smaller images, but corrupts larger images and creates a "Segmentation fault (core dumped)" error. I am at a loss as to why this works only part of the time. I hope it's proper etiquette to post the code below (3 files). Can someone please show me what's wrong with the following code? I
8
5596
by: jbrewer | last post by:
I'm trying to read in a FITs image file for my research, and I decided that writing a file decoder for the Python imaging library would be the easiest way to accomplish this for my needs. FITs is a raw data format used in astronomy. Anyway, I followed the example in the PIL documentation online, and I also had a look at the FITs image stub file included with PIL 1.1.5. I cooked up something that should work (or nearly work), but I keep...
9
10696
by: b007uk | last post by:
Hi! Really need help, dont know whats wrong :( I am trying to show images using php script, it works fine, pictures are shown, but if i right click on it and choose "save picture as" it offers me to save it as "untitled.bmp", not jpeg the picture actually is :( And if i do save it and then check it - it is actually bmp image, not jpeg (it has bmp header) Could you help please, i need to be able to save it as jpeg, using
4
7503
by: Vertuas | last post by:
Hi All, Is there a way to get PHP to change the mime type for a file sent to a browser. I need my website viewers to be able to click a link to directly download and save an image, rather than the browser displaying it. Is there some other way to make this happen?
7
3781
by: Ben | last post by:
Hi We are looking for a component that offers that offers the below for Tiff files: Image clean-up (deskew, despeckle) Printing capabilities from VB The ability to add text to image, e.g. time / date Nice to have:
2
4122
by: Adam Teale | last post by:
hey guys Is there a builtin/standard install method in python for retrieving or finding out an image's dimensions? A quick google found me this: http://www.pythonware.com/library/pil/handbook/introduction.htm but it looks like it is something I will need to install - I'd like to be able to pass my script around to people without them needing any
0
8395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.