summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-res/src/bitmapresource.pp
blob: a9baffa6354c0dfee787397ffade706824c7459d (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2008 by Giulio Bernardi

    Bitmap resource type

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    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.

 **********************************************************************}

unit bitmapresource;

{$MODE OBJFPC}

interface

uses
  Classes, SysUtils, resource;

type

  { TBitmapResource }

  TBitmapResource = class(TAbstractResource)
  private
    fType : TResourceDesc;
    fName : TResourceDesc;
    fBitmapData : TStream;
    function GetBitmapData : TStream;
  protected
    function GetType : TResourceDesc; override;
    function GetName : TResourceDesc; override;
    function ChangeDescTypeAllowed(aDesc : TResourceDesc) : boolean; override;
    function ChangeDescValueAllowed(aDesc : TResourceDesc) : boolean; override;
    procedure NotifyResourcesLoaded; override;
  public
    constructor Create; override;
    constructor Create(aType,aName : TResourceDesc); override;
    destructor Destroy; override;
    procedure UpdateRawData; override;
    procedure SetCustomBitmapDataStream(aStream : TStream);
    property BitmapData : TStream read GetBitmapData;
  end;


implementation

uses
  resfactory, resdatastream;

type
  TBitmapFileHeader = packed record
    Magic : word;
    FileSize : longword;
    Reserved : longword;
    DataOffset : longword;
  end;
  
const
  hdrsize = sizeof(TBitmapFileHeader);

type

  { TBitmapCachedDataStream }

  TBitmapCachedDataStream = class(TCachedDataStream)
  private
    fHeader : TMemoryStream;
    procedure InitHeader(aStream : TStream);
    function CalcDataOffset(aStream : TStream) : longword;
  protected
    function ReadFromSubStream(aStream : TStream; var Buffer; aPosition : int64; aCount : longint) : longint;
  public
    constructor Create(aStream : TStream;  aResource : TAbstractResource; aSize : int64); override;
    destructor Destroy; override;
    function Read(var Buffer; Count: Longint): Longint; override;
  end;

procedure TBitmapCachedDataStream.InitHeader(aStream: TStream);
var bmphdr : TBitmapFileHeader;
begin
  bmphdr.Magic:=$4d42;
  bmphdr.FileSize:=aStream.Size+hdrsize;
  bmphdr.Reserved:=0;
  bmphdr.DataOffset:=CalcDataOffset(aStream);
  {$IFDEF ENDIAN_BIG}
  bmphdr.Magic:=SwapEndian(bmphdr.Magic);
  bmphdr.FileSize:=SwapEndian(bmphdr.FileSize);
  bmphdr.Reserved:=SwapEndian(bmphdr.Reserved);
  bmphdr.DataOffset:=SwapEndian(bmphdr.DataOffset);
  {$ENDIF}
  fHeader.Position:=0;
  fHeader.WriteBuffer(bmphdr,hdrsize);
end;

function TBitmapCachedDataStream.CalcDataOffset(aStream: TStream): longword;
const
  BI_BITFIELDS = 3;
var oldpos : int64;
    compression, clrused : longword;
    bitcount,palentries : word;
    infohdrsize : longword;
begin
//  Data offset: bmp file header + bmp info header + [mask] + [palette]
  Result:=0;
  if aStream.Size<$28 then exit;
  oldpos:=aStream.Position;
  aStream.Position:=0;
  aStream.ReadBuffer(infohdrsize,4);
  aStream.Position:=14;
  aStream.ReadBuffer(bitcount,2);
  aStream.ReadBuffer(compression,4);
  aStream.Seek(12,soFromCurrent);
  aStream.ReadBuffer(clrused,4);
  {$IFDEF ENDIAN_BIG}
  infohdrsize:=SwapEndian(infohdrsize);
  bitcount:=SwapEndian(bitcount);
  compression:=SwapEndian(compression);
  clrused:=SwapEndian(clrused);
  {$ENDIF}
  aStream.Position:=oldpos;
  palentries:=0;
  if ((compression=BI_BITFIELDS) and (bitcount in [16,32])) then
    inc(Result,12)  //arbitrary mask
  else if clrused>0 then palentries:=clrused
  else if bitcount<=8 then palentries:=1 shl bitcount;
  inc(Result,palentries*4);
  inc(Result,hdrsize+infohdrsize);
end;

function TBitmapCachedDataStream.ReadFromSubStream(aStream: TStream;
  var Buffer; aPosition: int64; aCount: longint): longint;
var oldpos : int64;
begin
  Result:=aStream.Size-aPosition;
  if aCount<Result then Result:=aCount;
  if Result<0 then Result:=0;
  oldpos:=aStream.Position;
  aStream.Position:=aPosition;
  Result:=aStream.Read(Buffer,Result);
  aStream.Position:=oldpos;
end;

constructor TBitmapCachedDataStream.Create(aStream: TStream;  aResource : TAbstractResource; aSize: int64);
begin
  inherited Create(aStream,aResource,aSize);
  fHeader:=TMemoryStream.Create;
  inc(fSize,hdrsize);
  InitHeader(aStream);
end;

destructor TBitmapCachedDataStream.Destroy;
begin
  fHeader.Free;
end;

function TBitmapCachedDataStream.Read(var Buffer; Count: Longint): Longint;
var toread,read_in : longint;
    b : pbyte;
begin
  Result:=0;
  toread:=fSize-Position;
  if Count<toread then toread:=Count;
  if toread<0 then toread:=0;
  b:=@buffer;

  read_in:=ReadFromSubStream(fHeader,b^,fPosition,toread);
  inc(fPosition,read_in);
  inc(b,read_in);
  inc(Result,read_in);
  dec(toread,read_in);

  read_in:=ReadFromSubStream(fStream,b^,fPosition-hdrsize,toread);
  inc(fPosition,read_in);
  inc(Result,read_in);
end;


{ TBitmapResource }

function TBitmapResource.GetBitmapData: TStream;
begin
  if fBitmapData = nil then
    fBitmapData:=TResourceDataStream.Create(RawData,self,DataSize,TBitmapCachedDataStream);
  Result:=fBitmapData;
end;

procedure TBitmapResource.UpdateRawData;
begin
  if TResourceDataStream(BitmapData).Cached then exit; //no need to update rawdata
  //rawdata = bitmapdata without bitmap file header
  BitmapData.Position:=hdrsize;
  RawData.Size:=0;
  RawData.Position:=0;
  RawData.CopyFrom(BitmapData,BitmapData.Size-hdrsize);
  FreeAndNil(fBitmapData);
end;

function TBitmapResource.GetType: TResourceDesc;
begin
  Result:=fType;
end;

function TBitmapResource.GetName: TResourceDesc;
begin
  Result:=fName;
end;

function TBitmapResource.ChangeDescTypeAllowed(aDesc: TResourceDesc): boolean;
begin
  Result:=aDesc=fName;
end;

function TBitmapResource.ChangeDescValueAllowed(aDesc: TResourceDesc): boolean;
begin
  Result:=aDesc=fName;
end;

procedure TBitmapResource.NotifyResourcesLoaded;
begin
end;

constructor TBitmapResource.Create;
begin
  inherited Create;
  fType:=TResourceDesc.Create(RT_BITMAP);
  fName:=TResourceDesc.Create(1);
  SetDescOwner(fType);
  SetDescOwner(fName);
  fBitmapData:=nil;
end;

constructor TBitmapResource.Create(aType, aName: TResourceDesc);
begin
  Create;
  fName.Assign(aName);
end;

destructor TBitmapResource.Destroy;
begin
  if fBitmapData<>nil then fBitmapData.Free;
  fType.Free;
  fName.Free;
  inherited Destroy;
end;

procedure TBitmapResource.SetCustomBitmapDataStream(aStream: TStream);
begin
  TResourceDataStream(BitmapData).SetCustomStream(aStream);
end;

initialization
  TResourceFactory.RegisterResourceClass(RT_BITMAP,TBitmapResource);

end.