Not in FIle

Pages: 123
So I should add that to the program? And does the #endif go before or after the code?

Edit:
Nevermind. I tried putting it in place of the original _setmode() and that worked...at least it got rid of the problem!

Edit 2:
Well, after a bit of trial and error, I got it to work!

Cmd in Bash shell script
$ echo "424D3E00 00000000 00003600 00002800 00000100 00000200 00000100 18000000 00000800 00000000 00000000 00000000 00000000 00000000 FF000000 0000" | ./hex_to_bin >twopix.bmp
Last edited on
how do you know which bytes in the Hex editor match up to what?

-- you do not, when opening a random binary file. you have to know what location has what data, which you do have from the BMP wiki and similar sites because you know its a bmp file. you know that byte 2 is 'M'. you know that the file size is wherever it is, byte 20 or whatever.
the hex editor assists you: it tells you that whatever in hex is whatever in decimal at 2, 4, 8 bytes or even what it is in float/double. But if you are at the wrong location, that assistance is just noise / totally wrong.
you could write a bmp specific hex editor that delimited the fields, but that would only be useful if you hacked on images constantly.
A dead one that I used to use, the AXE, was able to let you define a C-style struct and pattern and it would do that. Maybe something modern has this; I have not needed it and that was from like 1995 era.
Last edited on
Here is an adaptation of the program in Horstmann.
It shows the file details and data.

An improvement would be to read the file header info as the normal struct's and then decipher it. Similarly the breakdown into the rgb details could be done.

Nevertheless, it shows how to decode the info in a simple way.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <string>

int get_int(std::fstream& stream, int offset)
{
    stream.seekg(offset);
    int result = 0;
    int base = 1;
    for (int i = 0; i < 4; i++)
    {
        result = result + stream.get() * base;
        base = base * 256;
    }
    return result;
}

int main()
{
    std::string filename{"image_2.bmp"}; // i.e the test .bmp file

    std::fstream stream;
    // Open as a binary file
    stream.open(filename.c_str(), std::ios::in | std::ios::binary);

    char file_type1 = get_int(stream, 0);
    char file_type2 = get_int(stream, 1);

    int file_size = get_int(stream, 2);
    int start = get_int(stream, 10);
    int width = get_int(stream, 18);
    int height = get_int(stream, 22);


    std::cout
    << " File type: " << file_type1 << file_type2 << '\n'
    << " File size: " << file_size << '\n'
    << "   Start: : " << start << '\n'
    << "   Width: : " << width << '\n'
    << "  Height: : " << height << '\n'
    << "Pixel data:\n";
    for(int i = start; i < file_size; i+=4) // 4 IS THE 'PADDING'
    {
        std::cout << i << '\t' << get_int(stream, i) << '\n';
    }

    return 0;
}



 File type: BM
 File size: 62
   Start: : 54
   Width: : 1
  Height: : 2
Pixel data:
54	16711680
58	0
Program ended with exit code: 0
How would you modify it so you can pass it a filename as an argument?

Would something like, say,
20
21
22
23
24
25
26
27
    std::string filename{};
    
    std::cout << "Enter the name of the file. \n: ";
    std::getline (cin, filename);

    std::fstream stream;
    // Open as a binary file
    stream.open(filename.c_str(), std::ios::in | std::ios::binary);

Would that work?
Last edited on
@max
Best is to try it! Probably works OK
Keep in mind the filename is not part of the header or the data on the file.
I made a few small changes but this is essentially the same:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <string>

int get_int(std::fstream& stream, int offset)
{
    stream.seekg(offset);
    int result = 0;
    int base = 1;
    for (int i = 0; i < 4; i++)
    {
        result = result + stream.get() * base;
        base = base * 256;
    }
    return result;
}

int main()
{
    std::string filename{};//{"image_2.bmp"};
    std::cout << "Please enter a file name: "; // <--
    std::getline(std::cin, filename); // <--

    std::fstream stream;
    // Open as a binary file
    stream.open(filename, std::ios::in | std::ios::binary); // <--

    char file_type1 = get_int(stream, 0);
    char file_type2 = get_int(stream, 1);

    int file_size = get_int(stream, 2);
    int start = get_int(stream, 10);
    int width = get_int(stream, 18);
    int height = get_int(stream, 22);


    std::cout
    << " File name: " << filename << '\n' // <--
    << " File type: " << file_type1 << file_type2 << '\n'
    << " File size: " << file_size << '\n'
    << "   Start: : " << start << '\n'
    << "   Width: : " << width << '\n'
    << "  Height: : " << height << '\n'
    << "Pixel data:\n";
    for(int i = start; i < file_size; i+=4)
    {
        std::cout << i << '\t' << get_int(stream, i) << '\n';
    }

    return 0;
}


Please enter a file name: image 2.bmp
File name: image 2.bmp
File type: BM
File size: 62
Start: : 54
Width: : 1
Height: : 2
Pixel data:
54 16711680
58 0
Program ended with exit code: 0
Topic archived. No new replies allowed.
Pages: 123