Converting a ( text file ) into any format of image ( png ) C++
can some one please provide me with a C++ code, for transforming a text file to any format of images, I know the image will have no meaning, bu开发者_如何转开发t I am doing this thing for security reasons
can some one please provide me with a C++ code, if there is no C++ , then JAVA will work
I really checked alot on the net, and found really nothing
I know every one will say do ur job alone, but I am really out of time please this is serious I am even willing to pay if some one is goin to do it
thanx in advance
I do not know, what you want the transformation to be, but you can just treat the text from the text file as an array of unsigned chars and store this data as an RGB array into a BMP or another lossless compressed image format. For loading and saving images there are a plenty of libraries (OpenIL/DevIL is quite simple and useful).
This way the image has no meaning but is still a valid image, in contrast to just changing the file extension.
EDIT: Your luck I'm currently bored. The following could work using OpenIL:
#include <fstream>
#include <cmath>
#include <IL/il.h>
void encode(const char *infile, const char *outfile)
{
std::ifstream in(infile);
in.seekg(0, std::ios_base::end);
unsigned int size = in.tellg();
unsigned int width = (size+6) / 3;
width = int(sqrt(double(width))) + 1;
char *data = new char[width*width*3];
*(unsigned int*)data = size;
in.seekg(0);
in.read(data+sizeof(unsigned int), size);
unsigned int image;
ilGenImages(1, &image);
ilBindImage(image);
ilTexImage(width, width, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, data);
ilSaveImage(outfile);
ilDeleteImages(1, &image);
delete[] data;
}
void decode(const char *infile, const char *outfile)
{
unsigned int image;
ilGenImages(1, &image);
ilBindImage(image);
ilLoadImage(infile);
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
unsigned char *data = ilGetData();
unsigned int size = *(unsigned int*)data;
std::ofstream out(outfile);
out.write((char*)data+sizeof(unsigned int), size);
ilDeleteImages(1, &image);
}
int main(int argc, char *argv[])
{
ilInit();
ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
ilEnable(IL_ORIGIN_SET);
ilEnable(IL_FILE_OVERWRITE);
encode(argv[1], argv[2]);
decode(argv[2], argv[3]);
return 0;
}
EDIT: Added complete sourcecode of test program. Tested it on my Windows system with this code as input and the PNG format and it works.
Just change the file extension to .png
or whatever you want. Operating systems have no notion of what's in a file and what it means; only what program to use when opening it.
If you need security, I recommend reading up a bit on encryption.
精彩评论