This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvcmi.image_formats.h3pcx.pas
129 lines (99 loc) · 2.97 KB
/
vcmi.image_formats.h3pcx.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
{ This file is a part of Map editor for VCMI project.
Copyright (C) 2016-2017 Alexander Shishkin [email protected]
This source 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 code 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.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit vcmi.image_formats.h3pcx;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, FPImage, Graphics, IntfGraphics, GraphType, stream_adapter, editor_types;
procedure LoadH3Pcx(ASourceStream: TStream; ADest: TLazIntfImage);
implementation
procedure LoadH3Pcx(ASourceStream: TStream; ADest: TLazIntfImage);
var
size, width, height: UInt32;
source: TStreamReadAdapter;
procedure InitBitmap();
begin
ADest.SetSize(width, height);
end;
procedure LoadH3Pcx24;
var
c: TH3DefColor;
i, j: Integer;
begin
ADest.BeginUpdate();
try
for i := 0 to height - 1 do
begin
for j := 0 to width - 1 do
begin
ASourceStream.Read(c, 3);
ADest.Colors[j,i] := FPColor(word(c.r) shl 8 + c.r, word(c.g) shl 8 + c.g, word(c.b) shl 8 + c.b);
end;
end;
finally
ADest.EndUpdate()
end;
end;
procedure LoadH3Pcx8;
var
initial_pos: Int64;
c: TH3DefColor;
buffer: packed array of byte;
i, j: Integer;
p: PByte;
begin
ADest.BeginUpdate();
try
ADest.UsePalette:=true;
ADest.Palette.Create(256);
initial_pos := ASourceStream.Position;
//load palette from end of file
ASourceStream.Seek(size, soCurrent);
for i := 0 to 256 - 1 do
begin
ASourceStream.Read(c, 3);
ADest.Palette.Color[i] := FPColor(word(c.r) shl 8 + c.r, word(c.g) shl 8 + c.g, word(c.b) shl 8 + c.b);
end;
//load graphics itself
ASourceStream.Seek(initial_pos, soBeginning);
SetLength(buffer, size);
ASourceStream.Read(buffer[0], size);
p := @buffer[0];
for i := 0 to height - 1 do
begin
for j := 0 to width - 1 do
begin
ADest.Pixels[j, i] := p^;
inc(p);
end;
end;
finally
ADest.EndUpdate();
end;
end;
begin
source.Create(ASourceStream);
size := source.ReadDWord;
width := source.ReadDWord;
height := source.ReadDWord;
InitBitmap();
if size = width * height * 3 then
begin
LoadH3Pcx24();
end
else if size = width * height then
begin
LoadH3Pcx8();
end;
end;
end.