Forum: PC-Programmierung Aufbau Bitmap Hex-file


von Chandler B. (chandler)


Lesenswert?

Hallo,
ich habe eine Frage zum aufbau von Bin-Files.
ich habe eine bitmap datei, welche ich mir im Hex-Editor anschauen kann.
1
00000000: 424d 662a 0000 0000 0000 3600 0000 2800  BMf*......6...(.
2
00000010: 0000 3c00 0000 3c00 0000 0100 1800 0000  ..<...<.........
3
00000020: 0000 302a 0000 7412 0000 7412 0000 0000  ..0*..t...t.....
4
00000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
5
00000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
6
00000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
7
00000060: 0000 0000 0000 0000 0000 0000 0000 0000  ................
8
00000070: 0000 0000 0000 0000 0000 0000 0000 0000  ................
9
00000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
10
00000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................
11
000000a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
12
000000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
13
000000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
14
000000d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
15
000000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
16
000000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
17
00000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
18
00000110: 0000 0000 0000 0000 0000 0000 0000 0000  ................
19
00000120: 0000 0000 0000 0000 0000 0000 0000 0000  ................
20
00000130: 0000 0000 0000 0000 0000 0000 0000 241c  ..............$.
21
00000140: ed24 1ced 241c ed24 1ced 0000 0000 0000  .$..$..$........
22
00000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................
23
00000160: 0000 0000 0000 0000 0000 0000 0000 0000  ................
24
00000170: 0000 0000 0000 0000 0000 0000 0000 0000  ................
25
00000180: 0000 0000 0000 0000 0000 0000 0000 0000  ................

Dies ist nur ein Ausschnitt.
Was sind das für Werte ganz vorne?
Ich bin ausgegangen, dass es die Adresse ist (in der Datei selber, daher 
start bei 0).
Aber in den Zeilen stehen ja 16 Bytes. Wieso geht die Adresse nur 10 
Bytes weiter?

von Sebastian R. (sebastian_r569)


Lesenswert?

Chandler B. schrieb:
> Ich bin ausgegangen, dass es die Adresse ist (in der Datei selber, daher
> start bei 0).
> Aber in den Zeilen stehen ja 16 Bytes. Wieso geht die Adresse nur 10
> Bytes weiter?

Du bist ganz knapp davor selber drauf zu kommen:

Chandler B. schrieb:
> Hex-Editor

Wenn du es in 5 Minuten nicht hast, meld dich noch mal, dann verrate ich 
dir die Lösung!

von Frank D. (Firma: LAPD) (frank_s634)


Lesenswert?

Weil in der ersten Zeile 16 Werte stehen, dann kommt die nächste Zeile 
wieder mit 16 Werten...

von Daniel A. (daniel-a)


Lesenswert?

Chandler B. schrieb:
> Aber in den Zeilen stehen ja 16 Bytes. Wieso geht die Adresse nur 10
> Bytes weiter?

Hexadezimal.
1
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
2
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F

von Sebastian R. (sebastian_r569)


Lesenswert?

Sebastian R. schrieb:
> Wenn du es in 5 Minuten nicht hast, meld dich noch mal, dann verrate ich
> dir die Lösung!

Kleiner Tipp noch: Schau dir mal an, was in Zeile 11 mit der Adresse 
passiert.

von Chandler B. (chandler)


Lesenswert?

Sebastian R. schrieb:
> Kleiner Tipp noch: Schau dir mal an, was in Zeile 11 mit der Adresse
> passiert.

ahh ja,
dann hat der Editor wohl doch recht :D

Daniel A. schrieb:
> Hexadezimal.
> 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
> 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F



Die Adressen sind ja auch in HEX -> 10 Hex = 16 Dec

Danke für die Denkanstöße

: Bearbeitet durch User
von Ob S. (Firma: 1984now) (observer)


Lesenswert?

Chandler B. schrieb:

> Was sind das für Werte ganz vorne?
> Ich bin ausgegangen, dass es die Adresse ist (in der Datei selber, daher
> start bei 0).

So eine "Adresse" nennt man Offset. Und es steht tatsächlich so einer 
darin. Siehe:

https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapfileheader

> Aber in den Zeilen stehen ja 16 Bytes. Wieso geht die Adresse nur 10
> Bytes weiter?

Weil der Header nicht wissen kann, wieviele Bytes pro Zeile dein 
Hexviewer anzeigen wird (das ist nämlich prinzipiell beliebig) und es 
ihm im Übrigen auch völlig Wurscht ist.

von Johann L. (gjlayde) Benutzerseite


Lesenswert?

Schaut man sich einen BMP Header an. Elemente sind Little Endian.
1
#ifndef BMP_H
2
#define BMP_H
3
4
#include <stdint.h>
5
6
typedef struct
7
{
8
  char bfType[2];    // 19778 must always be set to 'BM' to declare that this is a .bmp-file.
9
  uint32_t bfSize;    // the size of the file in bytes.
10
  uint16_t bfReserved1;  // must always be set to zero.
11
  uint16_t bfReserved2;  // must always be set to zero.
12
  uint32_t bfOffBits;  // 1078 offset from the beginning of the file to the bitmap data.
13
} __attribute__ ((__packed__)) BMPFileHeader;
14
15
typedef struct
16
{
17
  uint32_t biSize;    // 40 size of the BITMAPINFOHEADER structure, in bytes.
18
  uint32_t biWidth;    // width of the image, in pixels.
19
  uint32_t biHeight;    // height of the image, in pixels.
20
  uint16_t biPlanes;    // 1 planes of the target device, must be set to zero.
21
  uint16_t biBitCount;  // 8 bits per pixel.
22
  uint32_t biCompression; // 0 the type of compression, usually set to zero (no compression).
23
  uint32_t biSizeImage;  // 0 the size of the image data, in bytes. If there is no compression, it is valid to set this member to zero.
24
  uint32_t biXPelsPerMeter; // 0 usually set to zero.
25
  uint32_t biYPelsPerMeter; // 0 usually set to zero.
26
  uint32_t biClrUsed;    // 0 number of colors in the bitmap, if zero, the number of colors is calculated using biBitCount
27
  uint32_t biClrImportant; // 0 number of color that are 'important' for the bitmap, if zero, all colors are important.
28
} __attribute__ ((__packed__)) BMPInfoHeader;
29
30
typedef struct
31
{
32
  uint8_t blue;
33
  uint8_t green;
34
  uint8_t red;
35
  uint8_t Reserved;
36
} __attribute__ ((__packed__)) RGBQUAD;
37
38
typedef struct
39
{
40
  BMPFileHeader bmfh;
41
  BMPInfoHeader bmih;
42
  RGBQUAD    *colors;
43
  uint8_t *data;
44
  int rowlen;
45
} BMP;
46
47
#endif /* BMP_H */
48
49
/*
50
51
The .bmp file format
52
53
Introduction:
54
The .bmp file format (sometimes also saved as .dib) is the standard
55
for a Windows 3.0 or later DIB (device independent bitmap) file. It may
56
use compression (though I never came across a compressed .bmp-file)
57
and is (by itself) not capable of storing animation. However, you can
58
animate a bitmap using different methods but you have to write the
59
code which performs the animation. There are different ways to
60
compress a .bmp-file, but I won't explain them here because they are
61
so rarely used. The image data itself can either contain pointers to
62
entries in a color table or literal RGB values (this is explained
63
later).
64
65
Basic structure:
66
A .bmp file contains of the following data structures:
67
68
BITMAPFILEHEADER    bmfh;
69
BITMAPINFOHEADER    bmih;
70
RGBQUAD             aColors[];
71
BYTE                aBitmapBits[];
72
73
bmfh contains some information about the bitmap file (about the file,
74
not about the bitmap itself). bmih contains information about the
75
bitmap such as size, colors,... The aColors array contains a color
76
table. The rest is the image data, which format is specified by the
77
bmih structure.
78
79
Exact structure:
80
The following tables give exact information about the data structures
81
and also contain the settings for a bitmap with the following
82
dimensions: size 100x100, 256 colors, no compression. The start-value
83
is the position of the byte in the file at which the explained data
84
element of the structure starts, the size-value contains the nuber of
85
bytes used by this data element, the name-value is the name assigned
86
to this data element by the Microsoft API documentation. Stdvalue
87
stands for standard value. There actually is no such a thing as a
88
standard value but this is the value Paint assigns to the data element
89
if using the bitmap dimensions specified above (100x100x256). The
90
meaning-column gives a short explanation of the purpose of this data
91
element.
92
93
The BITMAPFILEHEADER:
94
start   size   name   stdvalue   purpose
95
1   2   bfType   19778   must always be set to 'BM' to declare that this is a .bmp-file.
96
3   4   bfSize   ??   specifies the size of the file in bytes.
97
7   2   bfReserved1   0   must always be set to zero.
98
9   2   bfReserved2   0   must always be set to zero.
99
11   4   bfOffBits   1078   specifies the offset from the beginning of the file to the bitmap data.
100
101
The BITMAPINFOHEADER:
102
start   size   name   stdvalue   purpose
103
15   4   biSize   40   specifies the size of the BITMAPINFOHEADER structure, in bytes.
104
19   4   biWidth   100   specifies the width of the image, in pixels.
105
23   4   biHeight   100   specifies the height of the image, in pixels.
106
27   2   biPlanes   1   specifies the number of planes of the target device, must be set to zero.
107
29   2   biBitCount   8   specifies the number of bits per pixel.
108
31   4   biCompression   0   Specifies the type of compression, usually set to zero (no compression).
109
35   4   biSizeImage   0   specifies the size of the image data, in bytes. If there is no compression, it is valid to set this member to zero.
110
39   4   biXPelsPerMeter   0   specifies the the horizontal pixels per meter on the designated targer device, usually set to zero.
111
43   4   biYPelsPerMeter   0   specifies the the vertical pixels per meter on the designated targer device, usually set to zero.
112
47   4   biClrUsed   0   specifies the number of colors used in the bitmap, if set to zero the number of colors is calculated using the biBitCount member.
113
51   4   biClrImportant   0   specifies the number of color that are 'important' for the bitmap, if set to zero, all colors are important.
114
115
Note that biBitCount actually specifies the color resolution of the
116
bitmap. The possible values are: 1 (black/white); 4 (16 colors); 8
117
(256 colors); 24 (16.7 million colors). The biBitCount data element
118
also decides if there is a color table in the file and how it looks
119
like. In 1-bit mode the color table has to contain 2 entries (usually
120
white and black). If a bit in the image data is clear, it points to
121
the first palette entry. If the bit is set, it points to the
122
second.
123
124
In 4-bit mode the color table must contain 16 colors. Every
125
byte in the image data represents two pixels. The byte is split into
126
the higher 4 bits and the lower 4 bits and each value of them points
127
to a palette entry. There are also standard colors for 16 colors mode
128
(16 out of Windows 20 reserved colors (without the entries 8, 9, 246,
129
247)). Note that you do not need to use this standard colors if the
130
bitmap is to be displayed on a screen which support 256 colors or
131
more, however (nearly) every 4-bit image uses this standard colors.
132
133
In 8-bit mode every byte represents a pixel. The value points to an entry
134
in the color table which contains 256 entries (for details see
135
Palettes in Windows.
136
137
In 24-bit mode three bytes represent one
138
pixel. The first byte represents the red part, the second the green
139
and the third the blue part. There is no need for a palette because
140
every pixel contains a literal RGB-value, so the palette is omitted.
141
142
143
The RGBQUAD array:
144
The following table shows a single RGBQUAD structure:
145
start   size   name   stdvalue   purpose
146
1   1   rgbBlue   -   specifies the blue part of the color.
147
2   1   rgbGreen   -   specifies the green part of the color.
148
3   1   rgbRed   -   specifies the red part of the color.
149
4   1   rgbReserved   -   must always be set to zero.
150
151
Note that the term palette does not refer to a RGBQUAD array, which is
152
called color table instead. Also note that, in a color table
153
(RGBQUAD), the specification for a color starts with the blue byte. In
154
a palette a color always starts with the red byte. There is no simple
155
way to map the whole color table into a LOGPALETTE structure, which
156
you will need to display the bitmap. You will have to write a function
157
that copies byte after byte.
158
159
160
The pixel data:
161
It depens on the BITMAPINFOHEADER structure how the pixel data is to
162
be interpreted (see above).
163
It is important to know that the rows of a DIB are stored upside
164
down. That means that the uppest row which appears on the screen
165
actually is the lowest row stored in the bitmap, a short example:
166
167
168
  
169
pixels displayed on the screen   pixels stored in .bmp-file
170
171
You do not need to turn around the rows manually. The API functions
172
which also display the bitmap will do that for you automatically.
173
174
Another important thing is that the number of bytes in one row must
175
always be adjusted to fit into the border of a multiple of four. You
176
simply append zero bytes until the number of bytes in a row reaches a
177
multiple of four, an example:
178
179
180
6 bytes that represent a row in the bitmap:   A0 37 F2 8B 31 C4
181
must be saved as:   A0 37 F2 8B 31 C4 00 00
182
183
to reach the multiple of four which is the next higher after six
184
(eight). If you keep these few rules in mind while working with .bmp
185
files it should be easy for you, to master it.
186
*/

von Yalu X. (yalu) (Moderator)


Lesenswert?

Ich glaube, dem TE ging es nicht um den Inhalt der BMP-Datei, sondern um
die Adressspalte im Hexdump (die Zahlen vor dem Doppelpunkt), von der er
glaubte, sie sei in Dezimaldarstellung. Nach den entsprechenden
Hinweisen hat er seinen Irrtum eingesehen:

Chandler B. schrieb:
> Die Adressen sind ja auch in HEX -> 10 Hex = 16 Dec
>
> Danke für die Denkanstöße

von Bradward B. (Firma: Starfleet) (ltjg_boimler)


Lesenswert?

> Was sind das für Werte ganz vorne?

Tipp: Auch wenn man schon seit 2013 bei mikrocontroller.net 
eingeschrieben ist, sollte man den Wert öffentlicher Büchereien wie der 
wikipedia nicht zu gering schätzen: 
https://de.wikipedia.org/wiki/Hex-Editor#%C3%9Cbliche_Darstellung_einer_Datei_durch_einen_Hex-Editor

"Ganz links wird ein Offset angezeigt. Dieser gibt die Anzahl der 
vorausgegangenen Bytes in Hexadezimalschreibweise an.
In der ersten Zeile ist der Offset 0, was dem Dateianfang entspricht; in 
der zweiten Zeile beträgt er 0x10, d. h., es sind 16 Bytes 
vorangegangen."

: Bearbeitet durch User
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.