BitMapを作成する(カラー) SystemVerilog

Systemverilog

はじめに

SystemVerilogでビットマップを作成します。

カラー画像版です。

サンプルコード

bmp.svh
`ifndef _BMP
`define _BMP
package bmp;
    typedef bit [7:0] BYTE;
    typedef bit [15:0] WORD;
    typedef bit [31:0] DWORD;

    typedef struct packed {
        DWORD OffBits;
        WORD Reserved2;
        WORD Reserved1;
        DWORD Size;
        WORD Type;
    } BitMapFileHeader_t;

    typedef struct packed {
        DWORD ClrImportant;
        DWORD ClrUsed;
        DWORD YPixPerMeter;
        DWORD XPixPerMeter;
        DWORD SizeImage;
        DWORD Compression;
        WORD  BitCount;
        WORD  Planes;
        DWORD Height;
        DWORD Width;
        DWORD Size;
    } BitMapInfoHeader_t;

    function void save(byte img[], string filename, int width, int height);
        int fp;
        BitMapFileHeader_t File;
        BitMapInfoHeader_t Info;

        File.Type = 19778;
        File.Size = 14 + 40 + width*height;
        File.Reserved1 = '0;
        File.Reserved2 = '0;
        File.OffBits = 14 + 40;

        Info.Size = 40;
        Info.Width = width;
        Info.Height = height;
        Info.Planes = 1;
        Info.BitCount = 24;
        Info.Compression = 0;
        Info.SizeImage = 0;
        Info.XPixPerMeter = 0;
        Info.YPixPerMeter = 0;
        Info.ClrUsed = 0;
        Info.ClrImportant = 0;

        fp = $fopen(filename, "wb");

        for (int i = 0;i < 14; i++)
            $fwrite(fp, "%c", File[i*8+:8]);

        for (int i = 0; i < 40; i++)
            $fwrite(fp, "%c", Info[i*8+:8]);

        foreach(img[i])
            $fwrite(fp, "%c", img[i]);

        $fclose(fp);
    endfunction
endpackage
`endif
top.sv
`include "bmp.svh"
module top;
    import bmp::*;

    int width = 256;
    int height = 256;

    byte img[];
    initial begin
        img = new[width * height * 3];

        foreach(img[i])
            img[i] = (i%10)*255;

        save(img, "hoge.bmp", width, height);
    end
endmodule

使い方

bmp.svhがカレントディレクトリにある状態でtop.svを実行すると、output.bmpが作成されます。

解説

bmp.svhではビットマップのヘッダの構造体と画像を保存するメソッドを定義しています。

画像を保存するsave()メソッドを持ちます。引数は以下です。

save(<1byteの画素値×3(RGBそれぞれ)>, <保存するファイル名>, <画像の横幅>, <画像の縦幅>)

まとめ

SystemVerilogでビットマップを作成しました

コメント

タイトルとURLをコピーしました