HelpCreate bitmap containing text.

Posts 1–6 of 6 · Page 1 of 1
Create bitmap containing text.
Hi everyone,i'm just a beginner and i know very little about coding,but since i'm an addict i always find up akward situations to solve so to make my life needlessly complex.

Anyway this is what i have done in the first place:
Took a file text,wrote in this:
Code:
bca321
Opened the file text with a hex editor with this result:
Code:
63 62 61 33 32 31
Converted to decimal:
Code:
99 98 97 51 50 59
Applied the decimal codes in a 2x1 BMP image as if they were RGB colors. (which is 2 pixels)
Saved the image.
Opening the image with a hex editor showed this:
Code:
BM>       6   (                                  123abc
As i'm kinda amused of the result,i'd want to have a program that given a text converts it into a bmp image containing the text (not the inverted one D:)

So it has 3 steps:
1) Take some text and convert it to hex
2) Invert the string just obtained
3) Export to .bmp

The first step is done with this site ATM: Convert String To Hexadecimal Online
But i'd be really cool if it could be done with the program.
The second part is done with this little code:
Code:
cin>>code;
	code = string ( code.rbegin(), code.rend() );
	cout<<code;
Now the real issue is the third part.
I have an inverted string that contains hex code,i'd need the code to convert it into RGB for a .bmp file.
in case there are missing characters to complete the RGB (like if i have 10 bytes,like: FF FF FF FF FF FF FF FF FF 00) then there should be additional spaces at the beginning.

Would anyone be so awesome to actually make such thing for me?
I've been googling around all day but had no luck :/
P.s. step 1 would also be amazing :3

Thanks in advance! :D
Code:
#include <iostream>
#include <fstream>
#include <string>

#pragma pack(push, 1)
struct bitmap_header {
  char magic[2];
  unsigned int file_size;
  unsigned short reserved1;
  unsigned short reserved2;
  unsigned int data_offset;
};

struct dib_header {
  unsigned int header_size;
  unsigned int bitmap_width;
  unsigned int bitmap_height;
  unsigned short color_plane;
  unsigned short bpp;
  unsigned int compression;
  unsigned int raw_image_size;
  unsigned int h_res;
  unsigned int v_res;
  unsigned int n_colors;
  unsigned int n_imp_colors;
};
#pragma pack(pop)

int main() {
  std::string bmpstr;

  std::cout << "Enter in a string: ";
  std::cin >> bmpstr;
  
  unsigned int padding = bmpstr.length() % 3 == 0 ? 0 : 3 - (bmpstr.length() % 3);
  bmpstr.insert(0, padding, ' ');

  std::ofstream bitmap("str2bmp.bmp", std::ofstream::out | std::ofstream::binary);
  
  bitmap_header bmp_head = {{'B', 'M'}, 54 + bmpstr.length() / 3 * 4, 0, 0, 0x36};
  dib_header dib_head = {40, bmpstr.length() / 3, 1, 1, 24, 0, 8, 0, 0, 0, 0};

  bitmap.write(reinterpret_cast<char*>(&bmp_head), sizeof(bitmap_header));
  bitmap.write(reinterpret_cast<char*>(&dib_head), sizeof(dib_header));

  bitmap.write(bmpstr.c_str(), bmpstr.length());
  padding = bmpstr.length() % 4 == 0 ? 0 : 4 - (bmpstr.length() % 4);
  
  unsigned int x = 0;
  bitmap.write(reinterpret_cast<char*>(&x), padding);

  bitmap.close()
}
Due to endianness, the string representing 'rgb' will have 'r' as blue, 'g' as green, and 'b' as red.
Quote Originally Posted by Fovea View Post
Code:
#include <iostream>
#include <fstream>
#include <string>

#pragma pack(push, 1)
struct bitmap_header {
  char magic[2];
  unsigned int file_size;
  unsigned short reserved1;
  unsigned short reserved2;
  unsigned int data_offset;
};

struct dib_header {
  unsigned int header_size;
  unsigned int bitmap_width;
  unsigned int bitmap_height;
  unsigned short color_plane;
  unsigned short bpp;
  unsigned int compression;
  unsigned int raw_image_size;
  unsigned int h_res;
  unsigned int v_res;
  unsigned int n_colors;
  unsigned int n_imp_colors;
};
#pragma pack(pop)

int main() {
  std::string bmpstr;

  std::cout << "Enter in a string: ";
  std::cin >> bmpstr;
  
  unsigned int padding = bmpstr.length() % 3 == 0 ? 0 : 3 - (bmpstr.length() % 3);
  bmpstr.insert(0, padding, ' ');

  std::ofstream bitmap("str2bmp.bmp", std::ofstream::out | std::ofstream::binary);
  
  bitmap_header bmp_head = {{'B', 'M'}, 54 + bmpstr.length() / 3 * 4, 0, 0, 0x36};
  dib_header dib_head = {40, bmpstr.length() / 3, 1, 1, 24, 0, 8, 0, 0, 0, 0};

  bitmap.write(reinterpret_cast<char*>(&bmp_head), sizeof(bitmap_header));
  bitmap.write(reinterpret_cast<char*>(&dib_head), sizeof(dib_header));

  bitmap.write(bmpstr.c_str(), bmpstr.length());
  padding = bmpstr.length() % 4 == 0 ? 0 : 4 - (bmpstr.length() % 4);
  
  unsigned int x = 0;
  bitmap.write(reinterpret_cast<char*>(&x), padding);

  bitmap.close()
}
Due to endianness, the string representing 'rgb' will have 'r' as blue, 'g' as green, and 'b' as red.
I didn't even think anyone would ever reply...it works wonderfully mate!
Ty alot!


*edit*

It has an issue when entering spaces tho.
If i type in "a b c" it'll only output the a,skipping " b c"
I'll try to see if i can get it fixed,but i probably won't be able to x)
Quote Originally Posted by IV2B View Post
If i type in "a b c" it'll only output the a,skipping " b c"
Use
Code:
std::getline(std::cin, bmpstr);
to get the input
Quote Originally Posted by Biesi View Post


Use
Code:
std::getline(std::cin, bmpstr);
to get the input
Awesome,however it still has two issues.
The first one is that i can't input (even with copy/paste) the enter key.
The second one is that the image is saved on just one line,which looks awkward when converting a long string.

Wouldn't it be possible to load the string from a text file?
Like:
If there is no string.txt then make such file and receive input
If there is string.txt use the content to convert.

About the second...i don't know how to implement it to code but it could work this way:
Let's say there are 200 pixels to put in.
Code:
x=200;
y=200/1.7
so it'd become:
x=200
y=117

Issue would be having the excact number of pixels...maybe y=200/1.7+1 then putting empty pixels in the last like? idk really :x
Quote Originally Posted by IV2B View Post
Awesome,however it still has two issues.
The first one is that i can't input (even with copy/paste) the enter key.
The second one is that the image is saved on just one line,which looks awkward when converting a long string.

Wouldn't it be possible to load the string from a text file?
Like:
If there is no string.txt then make such file and receive input
If there is string.txt use the content to convert.

About the second...i don't know how to implement it to code but it could work this way:
Let's say there are 200 pixels to put in.
Code:
x=200;
y=200/1.7
so it'd become:
x=200
y=117

Issue would be having the excact number of pixels...maybe y=200/1.7+1 then putting empty pixels in the last like? idk really :x
integer division doesn't work with decimals, so you will end up with an exact number, though it might be slightly off, anyways.
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us