定義BMP頭
Information約報頭。
with Interfaces; use Interfaces;
package BMP is
type Header is record
Signature : Integer_16;
Size : Integer_32; -- File size in bytes
Reserved1 : Integer_16;
Reserved2 : Integer_16;
Offset : Integer_32; -- Start address in bytes where the image data can be found.
end record;
Information有關信息標題
type Info is record
Struct_Size : Integer_32;
Width : Integer_32; -- Image width in pixels
Height : Integer_32; -- Image hieght in pixels
Planes : Integer_16;
Pixel_Size : Integer_16; -- Bits per pixel
Compression : Integer_32; -- Zero means no compression
Image_Size : Integer_32; -- Size of the image data in bytes
PPMX : Integer_32; -- Pixels per meter in x led
PPMY : Integer_32; -- Pixels per meter in y led
Palette_Size : Integer_32; -- Number of colors
Important : Integer_32;
end record;
有許多類型像素的。 這裏有兩種類型:
type Pixel_G8 is new Integer_8; -- 8 bit pixel grayscale
type Image_G8 is array (Integer range <>) of Pixel_G8;
type Pixel_ARGB32 is record -- 32 bit pixel (alpha, red, green, blue)
A, R, G, B : Integer_8; -- 8 bit * 4 = 32 bit
end record;
type Image_ARGB32 is array (Integer range <>) of Pixel_ARGB32;
如何將數據放在類型
Ada.Streams.Stream_IO允許我們通過讀屬性讀入從同一個文件對象的不同類型。關於不同IO的更多info。
with Ada; use Ada;
with Ada.Text_IO;
with Ada.Streams.Stream_IO;
use Ada.Streams;
with BMP;
procedure Test_BMP is
File : Stream_IO.File_Type;
Stream : Stream_IO.Stream_Access;
Header : BMP.Header;
Info : BMP.Info;
Name : constant String := "lena512.bmp";
begin
Stream_IO.Open(File, Stream_IO.In_File, Name);
Stream := Stream_IO.Stream(File);
BMP.Header'Read(Stream, Header);
Text_IO.Put_Line("Signature " & Header.Signature'Img);
Text_IO.Put_Line("Size " & Header.Size'Img);
Text_IO.Put_Line("Reserved1 " & Header.Reserved1'Img);
Text_IO.Put_Line("Reserved2 " & Header.Reserved2'Img);
Text_IO.Put_Line("Offset " & Header.Offset'Img);
BMP.Info'Read(Stream, Info);
Text_IO.Put_Line("Struct_Size " & Info.Struct_Size'Img);
Text_IO.Put_Line("Width " & Info.Width'Img);
Text_IO.Put_Line("Height " & Info.Height'Img);
Text_IO.Put_Line("Planes " & Info.Planes'Img);
Text_IO.Put_Line("Pixel_Size " & Info.Pixel_Size'Img);
Text_IO.Put_Line("Compression " & Info.Compression'Img);
Text_IO.Put_Line("Image_Size " & Info.Image_Size'Img);
Text_IO.Put_Line("PPMX " & Info.PPMX'Img);
Text_IO.Put_Line("PPMY " & Info.PPMY'Img);
Text_IO.Put_Line("Palette_Size " & Info.Palette_Size'Img);
Text_IO.Put_Line("Important " & Info.Important'Img);
delay 2.0;
-- Move read pointer to where the image data starts.
Stream_IO.Set_Index(File, Stream_IO.Positive_Count(Header.Offset));
declare
subtype Image is BMP.Image_G8(1..Integer(Info.Image_Size));
I : Image;
begin
Image'Read(Stream, I);
Stream_IO.Close(File);
for P of I loop
Text_IO.Put(P'Img);
end loop;
end;
end;
旁註
如果Info.Compression不是0,你將不得不解壓縮圖像數據。我不知道目前如何,所以我不會解釋。但你可以堅持未壓縮的bmp。
我不知道如何檢查格式是RGB還是GBR或任何格式。我知道最接近像素格式的解釋是檢查像素大小,但不顯示顏色分量的順序。
由於太寬泛,超出範圍或不清楚,您的問題已接近關閉。既然你已經定義了一個「正確的類型」來存儲圖像,你可以通過張貼*,並詢問如何填充它來改善問題。 – 2014-10-03 11:35:55
爲什麼你不想使用現有的庫? – 2014-10-03 14:54:33
我會做GIMME2板上使用綁定庫的一些圖像處理是一個壞主意 – 2014-10-04 19:10:37