首页 > 其他分享 >通过HH8WilEdit学习WIL 文件编码 12 对WIL文件调色板的读取

通过HH8WilEdit学习WIL 文件编码 12 对WIL文件调色板的读取

时间:2023-03-11 22:44:09浏览次数:39  
标签:24 文件 16 WIL 调色板 41 74 49 90

unit WilPalette;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TRGBQuads = packed array[0..255] of TRGBQuad;

   TWMImageHeader = record
     Title: string[40];       // 0
     ImageCount: Integer;     //48
     ColorCount: Integer;    //52
     PaletteSize: Integer;   //56
   end;


  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    Label1: TLabel;
    Image1: TImage;
    Label2: TLabel;
    Label3: TLabel;
    Memo1: TMemo;
    Label4: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  WilFileName: string;
  WilFileStream: TFileStream;
  //WilLogPalette: TMaxLogPalette;
  pWilLogPalette: PMaxLogPalette;
  MainPalette:  TRGBQuads;
  bitmap1: TBitmap;
  str: string;
  wmheader: TWMImageHeader;

implementation

{$R *.dfm}
function ExtractFileNameOnly(const fname: string): string;
var
  extpos: Integer;
  ext, fn: string;
begin
  ext := ExtractFileExt(fname);
  fn := ExtractFileName(fname);
  if ext <> '' then
  begin
    extpos := Pos(ext, fn);
    Result := Copy(fn, 1, extpos - 1);
  end else
    Result := fn;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i, j: Integer;
  x, y: Integer;
  BmpFileName: string;
  Row: PByteArray;

begin
  if OpenDialog1.Execute then
    WilFileName := OpenDialog1.FileName;
    Label1.Caption := WilFileName;

    WilFileStream := TFileStream.Create(WilFileName, fmOpenRead or fmShareDenyNone);
    WilFileStream.Read(wmheader, SizeOf(wmheader));
    WilFileStream.Seek(56, 0);
    WilFileStream.Read(MainPalette, SizeOf(TRGBQuads));    //1024 ,256 * 4


    Label3.Caption := 'WIL 文件标志: ' +  wmheader.Title;

  //  Label3.Caption := 'MainPalette的大小:' +  IntToStr(SizeOf(TPALETTEENTRY));
   GetMem(pWilLogPalette, SizeOf(TMaxLogPalette));   // 这里是建立内存空间给 一个指针

    Label2.Caption := 'MaxLogPalette的大小:' + IntToStr(SizeOf(TMaxLogPalette));
                                          // 1028 ,2 + 2 + 1024
    pWilLogPalette.palVersion := $300;       //调色板的版本??
    pWilLogPalette.palNumEntries := 256;

    for i := 0 to 255 do
      begin                                         //    RGB ,   bgr 记录里面元素的位置不同,
        pWilLogPalette.palPalEntry[i].peRed :=  MainPalette[i].rgbRed;
        pWilLogPalette.palPalEntry[i].peGreen := MainPalette[i].rgbGreen;
        pWilLogPalette.palPalEntry[i].peBlue := MainPalette[i].rgbBlue;
        pWilLogPalette.palPalEntry[i].peFlags := PC_RESERVED;
        str := '';
        str :=  IntToStr(i) + ':' + '[' + IntToStr(MainPalette[i].rgbRed) + '] [' + IntToStr(MainPalette[i].rgbGreen) +
           '] [' + IntToStr(MainPalette[i].rgbBlue) +  ']' ;
        Memo1.Lines.Add(str);
      end;


    bitmap1 := TBitmap.Create;
    bitmap1.PixelFormat := pf8bit;
    bitmap1.Height := 256 ;
    bitmap1.Width := 256 ;

    bitmap1.Palette := CreatePalette(pLogPalette(pWilLogPalette)^);
                                  //pLogPalette
  {  for y := 0 to 255 do
    begin
      Row := bitmap1.ScanLine[y];       //扫描线,唯一可以直接写BMP点数据的地方?
      for x:= 0 to 255 do                // 给一个数组指针,PByteArray = ^TByteArray;
                                         //          TByteArray = array[0..32767] of Byte;
      begin                              //实际只要 256 的字节数组就够了。这个是15位的,为什么不直接 16 到65535?

      Row[x] := y;
      end;
    end;
   }

  {  i := 0;
   for y := 0 to 15 do
     for x := 0 to 15 do
     begin
     RectBlock := Rect(x * 16, y * 16, x * 16 + 16, y * 16 + 16);
     bitmap1.Canvas.Brush.Color := Integer(MainPalette[i]);

     bitmap1.Canvas.FillRect(RectBlock);
     i := i + 1;

     end;
   }
    i := 0;

   for y := 0 to 255 do
    begin
      Row := bitmap1.ScanLine[y];      //返回的是个指针,无属性指针。
     // if (y <> 0) and (y mod 16 = 0) then Inc(i);
      j := 0;
      for x:= 0 to 255 do
      begin

         if (x <> 0 )and (x mod 16 = 0) then Inc(j);
         Row[x] := i + j;

      end;
      if (y <> 0) and (y mod 16 = 0) then i := i + 16;       // 填充像素的颜色代码 ,划分16 X 16 块。
    end;



    Image1.Picture.Graphic := bitmap1;       // 显示到图片

    BmpFileName := ExtractFilePath(WilFileName) + ExtractFileNameOnly(WilFileName) +
      '.bmp';
   // Image1.Picture.SaveToFile(BmpFileName);       //保存图片?

    Label4.Caption := '图片保存在:' + BmpFileName;
end;

end.

 

编写了wilpalette

功能读出 WIL 文件中的 palette
在 56 开始的1024字节中

定义了一个数组,256的 的TGRBQUAD 的数组,quad 是方院的意思
TRGBQuad是 windows.pas 中定义的,我重新装了一个DELPHI ,这个里面带有COMPOMENT源码了。

PRGBQuad = ^TRGBQuad;

tagRGBQUAD = packed record 顺序 BGR Reserve ,reserve 保留的意思
rgbBlue: Byte;
rgbGreen: Byte;
rgbRed: Byte;
rgbReserved: Byte;
end;
TRGBQuad = tagRGBQUAD;

RGBQUAD = tagRGBQUAD;

正好大小就是 256 * 4

对于 WIN的调色板
又是这样定义的。

PPaletteEntry = ^TPaletteEntry; entry 入口,大门的 也有条目,登记的意思 这里应作条目理解?

tagPALETTEENTRY = packed record // 4个字节 顺序 RGB F,
peRed: Byte;
peGreen: Byte;
peBlue: Byte;
peFlags: Byte; //flags 标记 ,旗帜的意思
end;
TPaletteEntry = tagPALETTEENTRY;

PALETTEENTRY = tagPALETTEENTRY;


{ Logical Palette }
PLogPalette = ^TLogPalette;

tagLOGPALETTE = packed record 逻辑调色板?
palVersion: Word; pal 是 palette 的缩写?
palNumEntries: Word;
palPalEntry: array[0..0] of TPaletteEntry; 立
end;
TLogPalette = tagLOGPALETTE;
r
一个元素的数组? 我想这是需要通过 palNumEntries的值,就是颜色数目来实际
数组的大小。 function CreatePalette(const LogPalette: TLogPalette): HPalette; stdcall;;
通过这个WIN函数来建立

LOGPALETTE = tagLOGPALETTE;

PMaxLogPalette = ^TMaxLogPalette; // not in Windows Headers 不是 WIN 的头文件?
TMaxLogPalette = packed record // 定义的最大调色板?
palVersion: Word;
palNumEntries: Word;
palPalEntry: array [Byte] of TPaletteEntry; //这里是256了。


end;

TRGBQuads = packed array[0..255] of TRGBQuad; 这个是DIB 里面的定义,需要拿出放在本单元中。

GetMem(pWilLogPalette, SizeOf(TMaxLogPalette)); // 这里是建立内存空间给 一个指针
用到了求 内存 函数。,但实际也可以声明一个记录变量,TMaxLogPalette,而不是用指针
但是在用CreatePalette 就需要CreatePalette(pLogPalette(@WilLogPalette)^);
取其地址成指针,强制类型为pLogPalette,再取内容。。。
利用指针的进行类型强制转换。

CreatePalette(pLogPalette(pWilLogPalette)^); 否则就是指针类型强制转换,取内容。
因为 CreatePalette的参数要求是const LogPalette: TLogPalette

bitmap1.ScanLine[y]; //返回的是个指针,无属性指针。

这个大概是唯一能对BMP里面的像素进行直接读写的函数了吧,

意思是用了扫描写线, 对 进行扫描
ROW 相当于是 0,Y X ,Y 的同Y 坐标的一条线了。
Row: PByteArray; 一个数组指针,PByteArray = ^TByteArray; 这定义的有点大
TByteArray = array[0..32767] of Byte;实际只要 256 的字节数组就够了。这个是15位的,为什么不直接 16 到65535?

注意点,TFileStream.Create(WilFileName, fmOpenRead or fmShareDenyNone);
文件流打开方式很重要,有个是fmOpenWrite 这个是读出来数据都是0,我折腾了2个小时,
56后的调色板数据都是0,什么文件都是的。然后放入了WIL文件的头结构,读也是0
才发现是这里的问题。

 

可以保存出来的256色文件。RGB 颜色代码。

0:[0] [0] [0]
1:[128] [0] [0]
2:[0] [128] [0]
3:[128] [128] [0]
4:[0] [0] [128]
5:[128] [0] [128]
6:[0] [128] [128]
7:[192] [192] [192]
8:[85] [128] [151]
9:[157] [185] [200]
10:[123] [115] [115]
11:[45] [41] [41]
12:[90] [82] [82]
13:[99] [90] [90]
14:[66] [57] [57]
15:[29] [24] [24]
16:[24] [16] [16]
17:[41] [24] [24]
18:[16] [8] [8]
19:[242] [121] [113]
20:[225] [103] [95]
21:[255] [90] [90]
22:[255] [49] [49]
23:[214] [90] [82]
24:[148] [16] [0]
25:[148] [41] [24]
26:[57] [8] [0]
27:[115] [16] [0]
28:[181] [24] [0]
29:[189] [99] [82]
30:[66] [24] [16]
31:[255] [170] [153]
32:[90] [16] [0]
33:[115] [57] [41]
34:[165] [74] [49]
35:[148] [123] [115]
36:[189] [82] [49]
37:[82] [33] [16]
38:[123] [49] [24]
39:[45] [24] [16]
40:[140] [74] [49]
41:[148] [41] [0]
42:[189] [49] [0]
43:[198] [115] [82]
44:[107] [49] [24]
45:[198] [107] [66]
46:[206] [74] [0]
47:[165] [99] [57]
48:[90] [49] [24]
49:[42] [16] [0]
50:[21] [8] [0]
51:[58] [24] [0]
52:[8] [0] [0]
53:[41] [0] [0]
54:[74] [0] [0]
55:[157] [0] [0]
56:[220] [0] [0]
57:[222] [0] [0]
58:[251] [0] [0]
59:[156] [115] [82]
60:[148] [107] [74]
61:[115] [74] [41]
62:[82] [49] [24]
63:[140] [74] [24]
64:[136] [68] [17]
65:[74] [33] [0]
66:[33] [24] [16]
67:[214] [148] [90]
68:[198] [107] [33]
69:[239] [107] [0]
70:[255] [119] [0]
71:[165] [148] [132]
72:[66] [49] [33]
73:[24] [16] [8]
74:[41] [24] [8]
75:[33] [16] [0]
76:[57] [41] [24]
77:[140] [99] [57]
78:[66] [41] [16]
79:[107] [66] [24]
80:[123] [74] [24]
81:[148] [74] [0]
82:[140] [132] [123]
83:[107] [99] [90]
84:[74] [66] [57]
85:[41] [33] [24]
86:[70] [57] [41]
87:[181] [165] [148]
88:[123] [107] [90]
89:[206] [177] [148]
90:[165] [140] [115]
91:[140] [115] [90]
92:[181] [148] [115]
93:[214] [165] [115]
94:[239] [165] [74]
95:[239] [198] [140]
96:[123] [99] [66]
97:[107] [86] [57]
98:[189] [148] [90]
99:[99] [57] [0]
100:[214] [198] [173]
101:[82] [66] [41]
102:[148] [99] [24]
103:[239] [214] [173]
104:[165] [140] [99]
105:[99] [90] [74]
106:[189] [165] [123]
107:[90] [66] [24]
108:[189] [140] [49]
109:[53] [49] [41]
110:[148] [132] [99]
111:[123] [107] [74]
112:[165] [140] [90]
113:[90] [74] [41]
114:[156] [123] [57]
115:[66] [49] [16]
116:[239] [173] [33]
117:[24] [16] [0]
118:[41] [33] [0]
119:[156] [107] [0]
120:[148] [132] [90]
121:[82] [66] [24]
122:[107] [90] [41]
123:[123] [99] [33]
124:[156] [123] [33]
125:[222] [165] [0]
126:[90] [82] [57]
127:[49] [41] [16]
128:[206] [189] [123]
129:[99] [90] [57]
130:[148] [132] [74]
131:[198] [165] [41]
132:[16] [156] [24]
133:[66] [140] [74]
134:[49] [140] [66]
135:[16] [148] [41]
136:[8] [24] [16]
137:[8] [24] [24]
138:[8] [41] [16]
139:[24] [66] [41]
140:[165] [181] [173]
141:[107] [115] [115]
142:[24] [41] [41]
143:[24] [66] [74]
144:[49] [66] [74]
145:[99] [198] [222]
146:[68] [221] [255]
147:[140] [214] [239]
148:[115] [107] [57]
149:[247] [222] [57]
150:[247] [239] [140]
151:[247] [231] [0]
152:[107] [107] [90]
153:[90] [140] [165]
154:[57] [181] [239]
155:[74] [156] [206]
156:[49] [132] [181]
157:[49] [82] [107]
158:[222] [222] [214]
159:[189] [189] [181]
160:[140] [140] [132]
161:[247] [247] [222]
162:[0] [8] [24]
163:[8] [24] [57]
164:[8] [16] [41]
165:[8] [24] [0]
166:[8] [41] [0]
167:[0] [82] [165]
168:[0] [123] [222]
169:[16] [41] [74]
170:[16] [57] [107]
171:[16] [82] [140]
172:[33] [90] [165]
173:[16] [49] [90]
174:[16] [66] [132]
175:[49] [82] [132]
176:[24] [33] [49]
177:[74] [90] [123]
178:[82] [107] [165]
179:[41] [57] [99]
180:[16] [74] [222]
181:[41] [41] [33]
182:[74] [74] [57]
183:[41] [41] [24]
184:[74] [74] [41]
185:[123] [123] [66]
186:[156] [156] [74]
187:[90] [90] [41]
188:[66] [66] [20]
189:[57] [57] [0]
190:[89] [89] [0]
191:[202] [53] [44]
192:[107] [115] [33]
193:[41] [49] [0]
194:[49] [57] [16]
195:[49] [57] [24]
196:[66] [74] [0]
197:[82] [99] [24]
198:[90] [115] [41]
199:[49] [74] [24]
200:[24] [33] [0]
201:[24] [49] [0]
202:[24] [57] [16]
203:[99] [132] [74]
204:[107] [189] [74]
205:[99] [181] [74]
206:[99] [189] [74]
207:[90] [156] [74]
208:[74] [140] [57]
209:[99] [198] [74]
210:[99] [214] [74]
211:[82] [132] [74]
212:[49] [115] [41]
213:[99] [198] [90]
214:[82] [189] [74]
215:[16] [255] [0]
216:[24] [41] [24]
217:[74] [136] [74]
218:[74] [231] [74]
219:[0] [90] [0]
220:[0] [136] [0]
221:[0] [148] [0]
222:[0] [222] [0]
223:[0] [238] [0]
224:[0] [251] [0]
225:[74] [90] [148]
226:[99] [115] [181]
227:[123] [140] [214]
228:[107] [123] [214]
229:[119] [136] [255]
230:[198] [198] [206]
231:[148] [148] [156]
232:[156] [148] [198]
233:[49] [49] [57]
234:[41] [24] [132]
235:[24] [0] [132]
236:[74] [66] [82]
237:[82] [66] [123]
238:[99] [90] [115]
239:[206] [181] [247]
240:[140] [123] [156]
241:[119] [34] [204]
242:[221] [170] [255]
243:[240] [180] [42]
244:[223] [0] [159]
245:[227] [23] [179]
246:[255] [251] [240]
247:[160] [160] [164]
248:[128] [128] [128]
249:[255] [0] [0]
250:[0] [255] [0]
251:[255] [255] [0]
252:[0] [0] [255]
253:[255] [0] [255]
254:[0] [255] [255]
255:[255] [255] [255]

好了现在 WIL 文件编码完结了,

下一步开始学习 地图文件。

标签:24,文件,16,WIL,调色板,41,74,49,90
From: https://www.cnblogs.com/D7mir/p/17207260.html

相关文章