root / UOLib / UArt.pas @ 119:66352054ce4d
History | View | Annotate | Download (10.9 kB)
| 1 | (*
|
|---|---|
| 2 | * CDDL HEADER START |
| 3 | * |
| 4 | * The contents of this file are subject to the terms of the |
| 5 | * Common Development and Distribution License, Version 1.0 only |
| 6 | * (the "License"). You may not use this file except in compliance |
| 7 | * with the License. |
| 8 | * |
| 9 | * You can obtain a copy of the license at |
| 10 | * http://www.opensource.org/licenses/cddl1.php. |
| 11 | * See the License for the specific language governing permissions |
| 12 | * and limitations under the License. |
| 13 | * |
| 14 | * When distributing Covered Code, include this CDDL HEADER in each |
| 15 | * file and include the License file at |
| 16 | * http://www.opensource.org/licenses/cddl1.php. If applicable, |
| 17 | * add the following below this CDDL HEADER, with the fields enclosed |
| 18 | * by brackets "[]" replaced with your own identifying * information: |
| 19 | * Portions Copyright [yyyy] [name of copyright owner] |
| 20 | * |
| 21 | * CDDL HEADER END |
| 22 | * |
| 23 | * |
| 24 | * Portions Copyright 2009 Andreas Schneider |
| 25 | *) |
| 26 | unit UArt;
|
| 27 | |
| 28 | {$mode objfpc}{$H+} |
| 29 | |
| 30 | interface
|
| 31 | |
| 32 | uses
|
| 33 | Classes, Imaging, ImagingTypes, ImagingCanvases, ImagingClasses, |
| 34 | UMulBlock, UGenericIndex, UHue; |
| 35 | |
| 36 | type
|
| 37 | TArtType = (atLand, atStatic, atLandFlat); |
| 38 | TArt = class(TMulBlock)
|
| 39 | constructor Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType); overload; |
| 40 | constructor Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AHue: THue; APartialHue: Boolean); overload; |
| 41 | constructor Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AArtColor: Word; AHue: THue; APartialHue: Boolean); overload; |
| 42 | destructor Destroy; override; |
| 43 | function Clone: TArt; override; |
| 44 | function GetSize: Integer; override; |
| 45 | procedure Write(AData: TStream); override; |
| 46 | procedure RefreshBuffer;
|
| 47 | protected
|
| 48 | FArtType: TArtType; |
| 49 | FHeader: LongInt; |
| 50 | FGraphic: TSingleImage; |
| 51 | FBuffer: TStream; |
| 52 | public
|
| 53 | property ArtType: TArtType read FArtType write FArtType; |
| 54 | property Header: LongInt read FHeader write FHeader; |
| 55 | property Graphic: TSingleImage read FGraphic; |
| 56 | property Buffer: TStream read FBuffer; |
| 57 | end;
|
| 58 | |
| 59 | implementation
|
| 60 | |
| 61 | type
|
| 62 | PWordArray = ^TWordArray; |
| 63 | TWordArray = array[0..16383] of Word; |
| 64 | |
| 65 | constructor TArt.Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType);
|
| 66 | begin
|
| 67 | Create(AData, AIndex, AArtType, 0, nil, False); |
| 68 | end;
|
| 69 | |
| 70 | constructor TArt.Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AHue: THue; APartialHue: Boolean);
|
| 71 | begin
|
| 72 | Create(AData, AIndex, AArtType, 0, AHue, APartialHue);
|
| 73 | end;
|
| 74 | |
| 75 | constructor TArt.Create(AData: TStream; AIndex: TGenericIndex; AArtType: TArtType; AArtColor: Word; AHue: THue; APartialHue: Boolean);
|
| 76 | var
|
| 77 | i, x, y, start: Integer; |
| 78 | iCurrentHeight, iCurrentWidth: Integer; |
| 79 | width, height: SmallInt; |
| 80 | lookup: array of integer; |
| 81 | color, run, offset: Word; |
| 82 | block: TMemoryStream; |
| 83 | P: PWordArray; |
| 84 | r, g, b: Byte; |
| 85 | |
| 86 | begin
|
| 87 | FBuffer := TMemoryStream.Create; |
| 88 | FArtType := AArtType; |
| 89 | AArtColor := AArtColor or $8000; //set alpha bit on background |
| 90 | if Assigned(AData) and (AIndex.Lookup > -1) then |
| 91 | begin
|
| 92 | AData.Position := AIndex.Lookup; |
| 93 | block := TMemoryStream.Create; |
| 94 | block.CopyFrom(AData, AIndex.Size); |
| 95 | block.Position := 0;
|
| 96 | |
| 97 | if AArtType = atLand then |
| 98 | begin
|
| 99 | FGraphic:= TSingleImage.CreateFromParams(44, 44, ifA1R5G5B5); |
| 100 | FillWord(FGraphic.Bits^, 44 * 44, AArtColor); |
| 101 | for y := 0 to 21 do |
| 102 | begin
|
| 103 | P := FGraphic.Bits + y * 44 * 2; |
| 104 | block.Read(P^[22 - (y + 1)], (y + 1) * 4); |
| 105 | end;
|
| 106 | for y := 0 to 21 do |
| 107 | begin
|
| 108 | P := FGraphic.Bits + (22 + y) * 44 * 2; |
| 109 | block.Read(P^[y], (22 - y) * 4); |
| 110 | end;
|
| 111 | for i := 0 to 44 * 44 - 1 do |
| 112 | PWordArray(FGraphic.Bits)^[i] := PWordArray(FGraphic.Bits)^[i] xor $8000; //invert alpha bit |
| 113 | end else if AArtType = atLandFlat then |
| 114 | begin
|
| 115 | FGraphic:= TSingleImage.CreateFromParams(44, 44, ifA1R5G5B5); |
| 116 | for i := 1 to 22 do |
| 117 | begin
|
| 118 | for x := 0 to i * 2 - 1 do |
| 119 | begin
|
| 120 | y := i * 2 - x - 1; |
| 121 | block.Read(color, SizeOf(Word)); |
| 122 | PWordArray(FGraphic.Bits + y * 44 * 2)^[x] := color; |
| 123 | if y > 0 then |
| 124 | PWordArray(FGraphic.Bits + (y - 1) * 44 * 2)^[x] := color; |
| 125 | end;
|
| 126 | end;
|
| 127 | for i := 22 to 43 do |
| 128 | begin
|
| 129 | for y := 0 to (44 - i) * 2 - 1 do |
| 130 | begin
|
| 131 | x := 42 - (43 - i) * 2 + y; |
| 132 | block.Read(color, SizeOf(Word)); |
| 133 | PWordArray(FGraphic.Bits + (43 - y) * 44 * 2)^[x] := color; |
| 134 | if y > 0 then |
| 135 | PWordArray(FGraphic.Bits + (44 - y) * 44 * 2)^[x] := color; |
| 136 | end;
|
| 137 | end;
|
| 138 | for i := 0 to 44 * 44 - 1 do |
| 139 | PWordArray(FGraphic.Bits)^[i] := PWordArray(FGraphic.Bits)^[i] xor $8000; //invert alpha bit |
| 140 | end else if AArtType = atStatic then |
| 141 | begin
|
| 142 | block.Read(FHeader, SizeOf(LongInt)); |
| 143 | block.Read(width, SizeOf(SmallInt)); |
| 144 | block.Read(height, SizeOf(SmallInt)); |
| 145 | FGraphic:= TSingleImage.CreateFromParams(width, height, ifA1R5G5B5); |
| 146 | FillWord(FGraphic.Bits^, width * height, AArtColor); |
| 147 | SetLength(lookup, height); |
| 148 | start := block.Position + (height * 2);
|
| 149 | for i := 0 to height - 1 do |
| 150 | begin
|
| 151 | block.Read(offset, SizeOf(Word)); |
| 152 | lookup[i] := start + (offset * 2);
|
| 153 | end;
|
| 154 | for iCurrentHeight := 0 to height - 1 do |
| 155 | begin
|
| 156 | block.Position := lookup[iCurrentHeight]; |
| 157 | iCurrentWidth := 0;
|
| 158 | P := FGraphic.Bits + iCurrentHeight * width * 2;
|
| 159 | while (block.Read(offset, SizeOf(Word)) = SizeOf(Word)) and |
| 160 | (block.Read(run, SizeOf(Word)) = SizeOf(Word)) and
|
| 161 | (offset + run <> 0) do |
| 162 | begin
|
| 163 | inc(iCurrentWidth, offset); |
| 164 | for i := 0 to run - 1 do |
| 165 | begin
|
| 166 | block.Read(color, SizeOf(Word)); |
| 167 | P^[iCurrentWidth + i] := color; |
| 168 | end;
|
| 169 | inc(iCurrentWidth, run); |
| 170 | end;
|
| 171 | end;
|
| 172 | |
| 173 | if AHue <> nil then |
| 174 | begin
|
| 175 | for i := 0 to width * height - 1 do |
| 176 | begin
|
| 177 | color := PWordArray(FGraphic.Bits)^[i]; |
| 178 | if color <> AArtColor then |
| 179 | begin
|
| 180 | r := (color and $7C00) shr 10; |
| 181 | if APartialHue then |
| 182 | begin
|
| 183 | g := (color and $3E0) shr 5; |
| 184 | b := color and $1F; |
| 185 | if (r = g) and (g = b) then |
| 186 | color := AHue.ColorTable[r]; |
| 187 | end else |
| 188 | color := AHue.ColorTable[r]; |
| 189 | end;
|
| 190 | PWordArray(FGraphic.Bits)^[i] := color; |
| 191 | end;
|
| 192 | end;
|
| 193 | |
| 194 | for i := 0 to width * height - 1 do |
| 195 | PWordArray(FGraphic.Bits)^[i] := PWordArray(FGraphic.Bits)^[i] xor $8000; //invert alpha bit |
| 196 | end else |
| 197 | FGraphic:= TSingleImage.Create; |
| 198 | if Assigned(block) then block.Free; |
| 199 | end else |
| 200 | begin
|
| 201 | FHeader := 1;
|
| 202 | FGraphic := TSingleImage.Create; |
| 203 | end;
|
| 204 | FGraphic.Format := ifA8R8G8B8; |
| 205 | end;
|
| 206 | |
| 207 | destructor TArt.Destroy;
|
| 208 | begin
|
| 209 | if assigned(FGraphic) then FGraphic.Free; |
| 210 | if assigned(FBuffer) then FBuffer.Free; |
| 211 | inherited;
|
| 212 | end;
|
| 213 | |
| 214 | function TArt.Clone: TArt;
|
| 215 | begin
|
| 216 | Result := TArt.Create(nil, nil, FArtType); |
| 217 | Result.FHeader := FHeader; |
| 218 | Result.FGraphic.Assign(FGraphic); |
| 219 | end;
|
| 220 | |
| 221 | procedure TArt.Write(AData: TStream);
|
| 222 | begin
|
| 223 | FBuffer.Position := 0;
|
| 224 | AData.CopyFrom(FBuffer, FBuffer.Size); |
| 225 | end;
|
| 226 | |
| 227 | function TArt.GetSize: Integer;
|
| 228 | begin
|
| 229 | RefreshBuffer; |
| 230 | Result := FBuffer.Size |
| 231 | end;
|
| 232 | |
| 233 | procedure TArt.RefreshBuffer;
|
| 234 | var
|
| 235 | argbGraphic: TSingleImage; |
| 236 | i, x, y, lineWidth, start: Integer; |
| 237 | iCurrentHeight, iCurrentWidth: Integer; |
| 238 | width, height: SmallInt; |
| 239 | color, run, offset: Word; |
| 240 | lookup: array of SmallInt; |
| 241 | begin
|
| 242 | argbGraphic := TSingleImage.CreateFromImage(FGraphic); |
| 243 | argbGraphic.Format := ifA1R5G5B5; |
| 244 | for i := 0 to argbGraphic.Width * argbGraphic.Height - 1 do |
| 245 | PWordArray(argbGraphic.Bits)^[i] := PWordArray(argbGraphic.Bits)^[i] xor $8000; //invert alpha bit |
| 246 | FBuffer.Size := 0;
|
| 247 | if FArtType = atLand then |
| 248 | begin
|
| 249 | if (argbGraphic.Height <> 44) or (argbGraphic.Width <> 44) then Exit; |
| 250 | x := 21;
|
| 251 | y := 0;
|
| 252 | lineWidth := 2;
|
| 253 | for i := 1 to 22 do |
| 254 | begin
|
| 255 | Dec(x); |
| 256 | FBuffer.Write(PWordArray(argbGraphic.Bits + y * 44 * 2)^[x + i], lineWidth); |
| 257 | Inc(y); |
| 258 | Inc(lineWidth, 2);
|
| 259 | end;
|
| 260 | for i := 1 to 22 do |
| 261 | begin
|
| 262 | Dec(lineWidth, 2);
|
| 263 | FBuffer.Write(PWordArray(argbGraphic.Bits + y * 44 * 2)^[x + i], lineWidth); |
| 264 | Inc(x); |
| 265 | Inc(y); |
| 266 | end;
|
| 267 | end else if FArtType = atStatic then |
| 268 | begin
|
| 269 | if (argbGraphic.Height = 0) or (argbGraphic.Width = 0) then Exit; |
| 270 | width := argbGraphic.Width; |
| 271 | height := argbGraphic.Height; |
| 272 | FBuffer.Write(FHeader, SizeOf(LongInt)); |
| 273 | FBuffer.Write(width, SizeOf(SmallInt)); |
| 274 | FBuffer.Write(height, SizeOf(SmallInt)); |
| 275 | SetLength(lookup, height); |
| 276 | for i := 0 to height - 1 do |
| 277 | FBuffer.Write(lookup[i], SizeOf(SmallInt)); //placeholders for the lookup table
|
| 278 | start := FBuffer.Position; |
| 279 | for iCurrentHeight := 0 to height - 1 do |
| 280 | begin
|
| 281 | lookup[iCurrentHeight] := SmallInt((FBuffer.Position - start) div 2); //remember the lookup offset for the current line |
| 282 | offset := 0;
|
| 283 | run := 0;
|
| 284 | for iCurrentWidth := 0 to width - 1 do //process every pixel on the current line |
| 285 | begin
|
| 286 | color := PWordArray(FGraphic.Bits + iCurrentHeight * width * 2)^[iCurrentWidth];
|
| 287 | if (color and $8000 = 0) and (run = 0) then //new visible pixel found |
| 288 | begin
|
| 289 | FBuffer.Write(offset, SizeOf(Word)); |
| 290 | FBuffer.Write(offset, SizeOf(Word)); //just a placeholder for the "run length"
|
| 291 | run := 1;
|
| 292 | FBuffer.Write(color, SizeOf(Word)); |
| 293 | end else if (color and $8000 = 0) and (run > 0) then //another visible pixel found |
| 294 | begin
|
| 295 | inc(run); |
| 296 | FBuffer.Write(color, SizeOf(Word)); |
| 297 | end else if (color and $8000 = $8000) and (run > 0) then //after some visible pixels this one is invisible, so stop the current run |
| 298 | begin
|
| 299 | FBuffer.Seek(Integer(-((run + 1) * 2)), soFromCurrent); //jump back ... |
| 300 | FBuffer.Write(run, SizeOf(Word)); //... to write the actual "run length" ...
|
| 301 | FBuffer.Seek(Integer(run * 2), soFromCurrent); //... and jump forth again to proceed |
| 302 | run := 0;
|
| 303 | offset := 1;
|
| 304 | end else |
| 305 | inc(offset); |
| 306 | end;
|
| 307 | if run > 0 then //no more pixels but the "run" didn't end yet ;-) |
| 308 | begin
|
| 309 | FBuffer.Seek(Integer(-((run + 1) * 2)), soFromCurrent); |
| 310 | FBuffer.Write(run, SizeOf(Word)); |
| 311 | FBuffer.Seek(Integer(run * 2), soFromCurrent);
|
| 312 | run := 0;
|
| 313 | end;
|
| 314 | FBuffer.Write(run, SizeOf(Word)); //just write "0"
|
| 315 | FBuffer.Write(run, SizeOf(Word)); //... two times, to indicate the end of that line
|
| 316 | end;
|
| 317 | FBuffer.Position := start - (height * 2); //now update the lookup table with our new values |
| 318 | for i := 0 to height - 1 do |
| 319 | FBuffer.Write(lookup[i], SizeOf(SmallInt)); |
| 320 | end;
|
| 321 | argbGraphic.Free; |
| 322 | end;
|
| 323 | |
| 324 | end.
|
| 325 |