text
stringlengths
0
234
begin
n := 0;
for i in 1 .. Number'Size / 8 loop
GL.Ubyte'Read (S, b);
n := n + m * Number (b);
m := m * 256;
end loop;
end Read_Intel_x86_number;
procedure Read_Intel is new Read_Intel_x86_number (U16);
procedure Read_Intel is new Read_Intel_x86_number (U32);
begin
-- First 14 bytes is file header structure.
-- Pos= 1, read 2 bytes, file signature word
String'Read (S, Str2);
if Str2 /= "BM" then
raise Not_BMP_format;
end if;
-- Pos= 3, read the file size
Read_Intel (fsz);
-- Pos= 7, read four bytes, unknown
String'Read (S, Str4);
-- Pos= 11, read four bytes offset, file top to bitmap data.
-- For 256 colors, this is usually 36 04 00 00
Read_Intel (offset);
-- Pos= 15. The beginning of Bitmap information header.
-- Data expected : 28H, denoting 40 byte header
Read_Intel (ih);
-- Pos= 19. Bitmap width, in pixels. Four bytes
Read_Intel (n);
width := X_Loc (n);
-- Pos= 23. Bitmap height, in pixels. Four bytes
Read_Intel (n);
height := Y_Loc (n);
-- Pos= 27, skip two bytes. Data is number of Bitmap planes.
Read_Intel (dummy16); -- perform the skip
-- Pos= 29, Number of bits per pixel
-- Value 8, denoting 256 color, is expected
Read_Intel (w);
if w /= 8 and then w /= 4 and then w /= 1 then
raise BMP_Unsupported_Bits_per_Pixel;
end if;
image_bits := Integer (w);
-- Pos= 31, read four bytees
Read_Intel (n); -- Type of compression used
if n /= 0 then
raise Unsupported_compression;
end if;
-- Pos= 35 (23H), skip twenty bytes
String'Read (S, Str20); -- perform the skip
-- Pos= 55 (36H), - start of palette
end Read_BMP_Header;
procedure Load_BMP_Palette (S : Stream_Access;
Image_Bits : Integer;
BMP_Palette : out Color_Palette) is
dummy : GL.Ubyte;
mc : constant Color_Type := (2**Image_Bits) - 1;
begin
for DAC in 0 .. mc loop
GL.Ubyte'Read (S, BMP_Palette (DAC).Blue);
GL.Ubyte'Read (S, BMP_Palette (DAC).Green);
GL.Ubyte'Read (S, BMP_Palette (DAC).Red);
GL.Ubyte'Read (S, dummy);
end loop;
end Load_BMP_Palette;
-- Load image only from stream (after having read header and palette!)
procedure Load_BMP_Image (S : Stream_Access;
width : X_Loc;
height : Y_Loc;
Buffer : in out Byte_Array;
BMP_bits : Integer;
BMP_Palette : Color_Palette) is
idx : Natural;
b01, b : GL.Ubyte := 0;
pair : Boolean := True;
bit : Natural range 0 .. 7 := 0;
--
x3 : Natural; -- idx + x*3 (each pixel takes 3 bytes)
x3_max : Natural;
--
procedure Fill_palettized is
pragma Inline (Fill_palettized);
begin
Buffer (x3) := Ubyte (BMP_Palette (b).Red);
Buffer (x3 + 1) := Ubyte (BMP_Palette (b).Green);
Buffer (x3 + 2) := Ubyte (BMP_Palette (b).Blue);
end Fill_palettized;
--
begin
Attach_Stream (stream_buf, S);
for y in 0 .. height - 1 loop