-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvxl_doompatch.pas
151 lines (133 loc) · 3.9 KB
/
vxl_doompatch.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//------------------------------------------------------------------------------
//
// Voxelizer
// Copyright (C) 2021-2022 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//------------------------------------------------------------------------------
// Site : https://sourceforge.net/projects/voxelizer/
//------------------------------------------------------------------------------
unit vxl_doompatch;
interface
uses
Windows, Classes, SysUtils, Graphics, vxl_utils;
function BmpAsPatch(const b: TBitmap; const palarray: PByteArray;
const offsl: integer = MAXINT; const offst: integer = MAXINT): TMemoryStream;
implementation
uses
vxl_palettes;
type
patch_t = packed record
width: smallint; // bounding box size
height: smallint;
leftoffset: smallint; // pixels to the left of origin
topoffset: smallint; // pixels below the origin
end;
column_t = packed record
topdelta: byte; // -1 is the last post in a column
length: byte; // length data bytes follows
end;
function BmpAsPatch(const b: TBitmap; const palarray: PByteArray;
const offsl: integer = MAXINT; const offst: integer = MAXINT): TMemoryStream;
var
m: TMemoryStream;
patch: patch_t;
column: column_t;
columnofs: TDNumberList;
columndata: TDNumberList;
x, y: integer;
palette: TDoomPalette;
c: LongWord;
i: integer;
procedure flashcolumnend;
begin
column.topdelta := 255;
column.length := 0;
m.Write(column, SizeOf(column));
end;
procedure flashcolumndata;
var
ii: integer;
bb: byte;
begin
if columndata.Count > 0 then
begin
column.topdelta := y - columndata.Count;
column.length := columndata.Count;
m.Write(column, SizeOf(column));
bb := 0;
m.Write(bb, SizeOf(bb));
for ii := 0 to columndata.Count - 1 do
begin
bb := columndata.Numbers[ii];
m.Write(bb, SizeOf(bb));
end;
bb := 0;
m.Write(bb, SizeOf(bb));
columndata.Clear;
end;
end;
begin
for i := 0 to 255 do
palette[i] := RGB(palarray[3 * i], palarray[3 * i + 1], palarray[3 * i + 2]);
m := TMemoryStream.Create;
Result := TMemoryStream.Create;
columnofs := TDNumberList.Create;
columndata := TDNumberList.Create;
try
patch.width := b.Width;
patch.height := b.Height;
if offsl = MAXINT then
patch.leftoffset := b.Width div 2
else
patch.leftoffset := offsl;
if offst = MAXINT then
patch.topoffset := b.Height
else
patch.topoffset := offst;
Result.Write(patch, SizeOf(patch_t));
for x := 0 to b.Width - 1 do
begin
columnofs.Add(m.Position + SizeOf(patch_t) + b.Width * SizeOf(integer));
columndata.Clear;
for y := 0 to b.Height - 1 do
begin
c := b.Canvas.Pixels[x, y];
if c = 0 then
begin
flashcolumndata;
continue;
end;
columndata.Add(DT_FindAproxColorIndex(@palette, c))
end;
flashcolumndata;
flashcolumnend;
end;
for i := 0 to columnofs.Count - 1 do
begin
x := columnofs.Numbers[i];
Result.Write(x, SizeOf(integer));
end;
m.Position := 0;
Result.CopyFrom(m, m.Size);
finally
m.Free;
columnofs.Free;
columndata.Free;
end;
end;
end.