2014-10-02 66 views
1

我是Ada編程語言的新成員,我想在Ada中讀取和操作圖像,而不需要綁定其他類似opencv的庫。 有沒有什麼方法可以讀取Ada中的圖像?或將其轉換爲像素值(RGB),特別是位圖圖像。我爲保存位圖圖像像素做了上述類型,但我不知道如何讀取圖像並填充圖像類型。如何閱讀Ada中的圖像?

type Byte is range 0..255; 

    for Byte'Size use 8; 

    type Pixel is record 

    R:Byte; 

    G:Byte; 

    B:Byte; 

    end record; 

    for Pixel'Size use 24; 

    type Image is array(Positive range <>, Positive range <>) of Pixel; 

    Pragma Pack(Image); 
+0

由於太寬泛,超出範圍或不清楚,您的問題已接近關閉。既然你已經定義了一個「正確的類型」來存儲圖像,你可以通過張貼*,並詢問如何填充它來改善問題。 – 2014-10-03 11:35:55

+0

爲什麼你不想使用現有的庫? – 2014-10-03 14:54:33

+0

我會做GIMME2板上使用綁定庫的一些圖像處理是一個壞主意 – 2014-10-04 19:10:37

回答

0

如果您不想綁定其他庫,則必須編寫自己的閱讀器並以像素格式存儲數據。然後你可以隨心所欲地做任何事情。 根據輸入格式,這是一項簡單或相對複雜的任務。 如果你主要對BMP文件感興趣,運氣就在你身邊,因爲它是一種特別簡單的格式,基本上由頭部和數據組成。我多年前在C做過一個讀者,而且很簡單。 您可以在網上找到任何格式定義,例如Wikipedia上的here。如果我沒有記錯的話,BMP圖像會被存儲在「自下而上」的位置,所以當你閱讀圖片時,你必須反轉Y行,因爲大多數人更喜歡頂行是數組中第一個或者你選擇的任何結構。

+0

感謝您的評論,是的,我正在處理的BMP圖像,但問題是我是新的阿達,我不知道如何閱讀圖像我創建了一個適當的類型來存儲圖像,但我不知道如何閱讀它。如果我可以讀取圖像,那麼其餘的將很容易 – 2014-10-03 07:29:24

+0

看了維基百科ref,我不得不說,閱讀BMP看起來並不簡單! – 2014-10-04 17:32:21

1

定義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或任何格式。我知道最接近像素格式的解釋是檢查像素大小,但不顯示顏色分量的順序。