Creating images on your computer?

Ok so I think people misunderstand the question I'm about to ask and maybe it's not possible. So I understand input output for creating text files. In other words you click on the executable and it creates a text file with the text you want.

Now say I have a picture of an air plane on my computer that I took with a camera just for the sake of explanation. I want that picture to be created when someone clicks my executable just like the text file example shown on this site only when you click the executable bam! that picture of an air plane is created on the users computer. Is this possible? IF you can create a text file when you click executable code why can't you create an image file you already have?

Thanks in advance friends.
Last edited on
You can. It's just a file containing data. You can create a file, write out data to it, and close the file. It's up to you to make sure that the data you write is a correct image file format; if you want to write out data that's dependent on some data you already have, again it's up to you to get that data and output it correctly.
Last edited on
You definitely can!! It's actually quite easy! Have you worked with fstreams?
You definitely can!! It's actually quite easy! Have you worked with fstreams?


Yes for text files. I get the science of this sort of. I was thinking technically if you opened up my air plane file in notepad copied that data, wrote it to a text file and then changed that text file to a .jpg extension for instance that it would create that file, but it doesn't.

How do you actually get an executable file to write using file stream an image your ALREADY have? I'm sorry just very confused on how this is done beyond writing text files.?
What he wants: Taking a picture of an airplane, converting it to hex, then inserting that value into his source code, so the picture is part of the executable in the same way as

1
2
string name = "This is example";
image example = 0xf68ca892fba;
What he wants: Taking a picture of an airplane, converting it to hex, then inserting that value into his source code, so the picture is part of the executable in the same way as



Yes that's exactly what I want friend. Is that possible? How would someone go about doing that? I just don't understand the means of going about this?

Thanks in advance Austin
It's going to be very difficult/tedious unless you write a tool to write the code for you...

You would literally have to record a value for every pixel, and incorporate it into your code somehow.
I don't suppose the tool would be too hard to make tho. Would it be possible to take something like sfml, load an image, take the loaded image, and recast that to a byte array, then save that byte array, and then take that saved byte array as a literal, and recast it back to image at runtime?

Edit: I'm going to fiddle with that, just throwing out there what I'm trying so other people may give input
Last edited on
Intrexa (100) Jun 27, 2011 at 9:11pm
I don't suppose the tool would be too hard to make tho. Would it be possible to take something like sfml, load an image, take the loaded image, and recast that to a byte array, then save that byte array, and then take that saved byte array as a literal, and recast it back to image at runtime?

Edit: I'm going to fiddle with that, just throwing out there what I'm trying so other people may give input


Oh thank you. Yes I guess this concept isn't as easy as it seems . Let me know what you find out I'm very curious and this is very interesting. Thank you so much.

Austin
well I've tried writing a simple bitmap image first pixel by pixel and it isn't working out for me thus far lol. Let me know where people have gotten.
You could write a program to do this.

- The output of this program is a file called "airplane.cpp"
- Steps:

1. Output the following to "airplane.cpp":

1
2
3
4
5
6
#include <fstream>
using namespace std;
int main()
{
ofstream generatedAirplane("generatedAirplane.whatever", ios::out | ios::binary);
char buffer;

2. Read in the image file "airplane.whatever" in binary format

3. For each byte, output the following to "airplane.cpp":

1
2
buffer=byteValue;
generatedAirplane.write(&buffer,1);

4. Finally, output the following to "airplane.cpp":

}

Once you've run the program, you'll have "airplane.cpp". Compile and run airplane.cpp and bam: you've got your airplane in "generatedAirplane.whatever"!

EDIT:
Alternatively, you could figure out the size of "airplane.whatever" and output an array of bytes to "airplane.cpp". That way, you would only need one write operation.

char bytes[fileSize]={

1
2
3
4
byteValue1,
byteValue2,
...
byteValueN

1
2
};
generatedAirplane.write(bytes, fileSize);

}
Last edited on
shacktar I figured out a better way. I have a program that outputs in the binary thanks to the help of a personal friend as well. I know how to do this now if anyone would like to know.
Does your program depend on the image, or some other file? The above "airplane.cpp" program doesn't require any supplementary files so all you need is the exe. I'd like to see your solution, if you may.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <fstream>
#include <iomanip>

int main(int argc, char *argv[])
{
	const char *imageName = "image.png";	
	std::ifstream myFile (imageName, std::ios::in | std::ios::binary);	
	std::ofstream outFile ("image.h", std::ios::out);

	outFile << "unsigned char image[] = {";
	char buffer[2] = {0};
	while(myFile.read(buffer, 1))
		outFile << "0x" << std::hex << (unsigned int)(unsigned char)buffer[0] << ", ";
	outFile << "};" << std::endl;
	return 0;	
}





Using an image.png file in the same directory that's image.png but you could use argv[1]; note my friend helped write most of this


THEN TO WRITE THE IMAGE ONCE YOU HAVE THE HEX BYTE VALUES PER PIXEL


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 #include <fstream>
 #include <windows.h>

 int main(int argc, char *argv[])

 { unsigned char image[] = {0xb1, etc, etc, copy paste it if you want from the other program };

        std::fstream myFile;

        myFile.open ("image.png", std::ios::out | std::ios::binary);

      if(myFile.is_open())

                 myFile.write((char*)image, sizeof(image)); 




return 0;
}



but my friends a freaking genius so this didn't take him but 5 minutes sad to say. I felt like an idiot when we got done talking - (thought I've learned a lot) - he explained it well when we were done. I can't always ask my friend programming questions though so I might have to bug you guys sometimes lol.

Thanks again




Last edited on
That solution is identical to the one I gave (see my edit).
I see. For some reason your solution even if it's the same is harder for me to understand. That is the way you explained it. (no offense). It's really hard for me to understand this stuff being new to c++ and stuff. I have a hard time following through with your solution but then again we aren't talking 1 on 1 though. I appreciate the help though.
It's probably the way I laid out the steps. It was half-English/half-partial-code as opposed to full-code (or full English). We frown on giving full solutions on this site, otherwise I would have.
Topic archived. No new replies allowed.