-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamemmapimage.pas
417 lines (369 loc) · 10 KB
/
camemmapimage.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
unit caMemMapImage;
{$INCLUDE ca.inc}
interface
uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
caControls;
type
TcaBMPFilename = type string;
//----------------------------------------------------------------------------
// TcaMemMapImage
//----------------------------------------------------------------------------
TcaMemMapImage = class(TcaGraphicControl)
private
FPalette: HPalette;
FData: Pointer;
FBitmapWidth: Integer;
FBitmapHeight: Integer;
FColours: Integer;
FFileHeader: PBitmapFileHeader;
FInfoHeader: PBitmapInfoHeader;
FInfo: PBitmapInfo;
FPixelStart: Pointer;
// Property fields
FActive: Boolean;
FAutoSize: Boolean;
FFileName: TcaBMPFilename;
FOffsetX: Integer;
FOffsetY: Integer;
FStretch: Boolean;
FCentre: Boolean;
// Property methods
procedure SetActive(Value: Boolean);
procedure SetFilename(const Value: TcaBMPFilename);
procedure SetOffsetX(const Value: Integer);
procedure SetOffsetY(const Value: Integer);
procedure SetStretch(Value: Boolean);
procedure SetCentre(Value: Boolean);
protected
// Protected property methods
procedure SetAutoSize(Value: Boolean); override;
// Protected methods
procedure Changed; virtual;
procedure CloseViewer; virtual;
procedure GetBitmapPalette;
procedure OpenViewer; virtual;
procedure BufferedPaint(C: TCanvas; R: TRect); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// Public methods
procedure Close;
procedure Open;
// Public properties
property BitmapFileHeader: PBitmapFileHeader read FFileHeader;
property BitmapInfoHeader: PBitmapInfoHeader read FInfoHeader;
property BitmapInfo: PBitmapInfo read FInfo;
property PixelStart: Pointer read FPixelStart;
property Palette: HPalette read FPalette;
published
// Published properties
property AutoSize: Boolean read FAutoSize write SetAutoSize default False;
property Centre: Boolean read FCentre write SetCentre default False;
property Filename: TcaBMPFilename read FFilename write SetFilename;
property OffsetX: Integer read FOffsetX write SetOffsetX;
property OffsetY: Integer read FOffsetY write SetOffsetY;
property Stretch: Boolean read FStretch write SetStretch default False;
// Read-only properties
property Colours: Integer read FColours;
property BitmapHeight: Integer read FBitmapHeight;
property BitmapWidth: Integer read FBitmapWidth;
// Promoted properties
property Align;
property DragCursor;
property DragMode;
property Enabled;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Visible;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
// This must be streamed last
property Active: Boolean read FActive write SetActive default False;
end;
implementation
const
BitmapSignature = $4D42;
procedure InvalidBitmap;
begin
raise Exception.Create('Bitmap image is not valid')
end;
procedure NotWhenActive;
begin
raise Exception.Create('Not on an active big bitmap viewer')
end;
constructor TcaMemMapImage.Create(AOwner: TComponent);
begin
inherited;
Width := 150;
Height := 150;
end;
destructor TcaMemMapImage.Destroy;
begin
CloseViewer;
inherited;
end;
// Public methods
procedure TcaMemMapImage.Close;
begin
Active := False;
end;
procedure TcaMemMapImage.Open;
begin
Active := True;
end;
// Protected methods
procedure TcaMemMapImage.Changed;
begin
if (BitmapWidth >= Width) and (BitmapHeight >= Height) then
ControlStyle := ControlStyle + [csOpaque]
else
ControlStyle := ControlStyle - [csOpaque];
if AutoSize and (BitmapWidth > 0) and (BitmapHeight > 0) then
SetBounds(Left, Top, BitmapWidth, BitmapHeight)
else
Invalidate;
end;
procedure TcaMemMapImage.CloseViewer;
begin
if FActive then
begin
FActive := False;
if FData <> nil then
begin
UnmapViewOfFile(FData);
FData := nil
end;
if FPalette <> 0 then
DeleteObject(FPalette);
end;
end;
procedure TcaMemMapImage.GetBitmapPalette;
var
SysPalSize: Integer;
Index: Integer;
LogSize: Integer;
LogPalette: PLogPalette;
DC: HDC;
Focus: HWND;
begin
if FColours > 2 then
begin
LogSize := SizeOf(TLogPalette) + pred(FColours) * SizeOf(TPaletteEntry);
LogPalette := AllocMem(LogSize);
try
LogPalette^.palNumEntries := FColours;
LogPalette^.palVersion := $0300;
{$IFOPT R+}
{$DEFINE R_PLUS}
{$R-}
{$ENDIF}
Focus := GetFocus;
DC := GetDC(Focus);
try
SysPalSize := GetDeviceCaps(DC, SIZEPALETTE);
if (FColours = 16) and (SysPalSize >= 16) then
begin
GetSystemPaletteEntries(DC, 0, 8, LogPalette^.palPalEntry);
Index := 8;
GetSystemPaletteEntries(DC, SysPalSize - Index, Index, LogPalette^.palPalEntry[Index])
end
else
begin
for Index := 0 to pred(FColours) do
begin
LogPalette^.palPalEntry[Index].peRed := FInfo^.bmiColors[Index].rgbRed;
LogPalette^.palPalEntry[Index].peGreen := FInfo^.bmiColors[Index].rgbGreen;
LogPalette^.palPalEntry[Index].peBlue := FInfo^.bmiColors[Index].rgbBlue
end
end;
finally
ReleaseDC(Focus, DC)
end;
{$IFDEF R_PLUS}
{$R+}
{$UNDEF R_PLUS}
{$ENDIF}
FPalette := CreatePalette(LogPalette^)
finally
FreeMem(LogPalette, LogSize)
end
end
end;
procedure TcaMemMapImage.OpenViewer;
var
FileHandle: THandle;
MapHandle: THandle;
begin
if FActive then exit;
// Open file
FileHandle := FileOpen(FFileName, fmOpenRead + fmShareDenyNone);
if FileHandle = INVALID_HANDLE_VALUE then
raise Exception.Create('Failed to open ' + FFilename);
// Create file map
try
MapHandle := CreateFileMapping(FileHandle, nil, PAGE_READONLY, 0, 0, nil);
if MapHandle = 0 then
raise Exception.Create('Failed to map file')
finally
CloseHandle(FileHandle)
end;
// View file map
try
FData := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 0);
if FData = nil then
raise Exception.Create('Failed to view map file')
finally
CloseHandle(MapHandle)
end;
// Set Pointers into file view
FFileHeader := FData;
// Test for valid bitmap file:
if FFileHeader^.bfType <> BitmapSignature then
begin
UnmapViewOfFile(FData);
FData := nil;
InvalidBitmap
end;
// Set up other Pointers
FInfoHeader := Pointer(integer(FData) + sizeof(TBitmapFileHeader));
FInfo := Pointer(FInfoHeader);
FPixelStart := Pointer(LongWord(FData) + FFileHeader^.bfOffBits);
// Get number of colours
if FInfoHeader^.biClrUsed <> 0 then
FColours := FInfoHeader^.biClrUsed
else
begin
case FInfoHeader^.biBitCount of
1, 4, 8 : FColours := 1 shl FInfoHeader^.biBitCount
else
FColours := 0
end;
end;
// Get bitmap size into easy to access variables
FBitmapHeight := FInfoHeader^.biHeight;
FBitmapWidth := FInfoHeader^.biWidth;
// Fetch the palette
GetBitmapPalette;
// Trigger the changes
FActive := True;
Changed;
end;
procedure TcaMemMapImage.BufferedPaint(C: TCanvas; R: TRect);
var
OldPalette: HPalette;
Dest: TRect;
begin
if (csDesigning in ComponentState) and not FActive then
begin
C.Pen.Style := psDash;
C.Brush.Style := bsClear;
C.Rectangle(0, 0, Width, Height)
end
else
begin
if FPalette <> 0 then
OldPalette := SelectPalette(C.Handle, FPalette, False)
else
OldPalette := 0;
try
RealizePalette(C.Handle);
if FStretch then
Dest := ClientRect
else
begin
if Centre then
Dest := Rect((Width - FBitmapWidth) div 2,
(Height - FBitmapHeight) div 2,
FBitmapWidth, FBitmapHeight)
else
Dest := Rect(0, 0, FBitmapWidth, FBitmapHeight);
end;
// OffsetRect(Dest, FOffsetX, FOffsetY);
Dest.Left := Dest.Left + FOffsetX;
Dest.Right := Dest.Right + FOffsetX;
Dest.Top := Dest.Top + FOffsetY;
Dest.Bottom := Dest.Bottom + FOffsetY;
StretchDIBits(C.Handle,
Dest.Left, Dest.Top, Dest.Right, Dest.Bottom,
0, 0, FBitmapWidth, FBitmapHeight,
FPixelStart, FInfo^,
DIB_RGB_COLORS, SRCCOPY)
finally
if OldPalette <> 0 then
SelectPalette(C.Handle, OldPalette, False)
end;
end;
end;
// Property methods
procedure TcaMemMapImage.SetActive(Value : boolean);
begin
if Value <> FActive then
if Value then
OpenViewer
else
CloseViewer
end;
procedure TcaMemMapImage.SetAutoSize(Value : boolean);
begin
if Value <> FAutoSize then
begin
FAutoSize := Value;
Changed
end
end;
procedure TcaMemMapImage.SetStretch(Value : boolean);
begin
if Value <> FStretch then
begin
FStretch := Value;
Changed
end
end;
procedure TcaMemMapImage.SetCentre(Value : boolean);
begin
if Value <> FCentre then
begin
FCentre := Value;
Changed
end
end;
procedure TcaMemMapImage.SetFilename(const Value : TcaBMPFilename);
begin
if Value <> FFilename then
begin
if FActive then
NotWhenActive;
FFilename := Value
end
end;
procedure TcaMemMapImage.SetOffsetX(const Value: Integer);
begin
if Value <> FOffsetX then
begin
FOffsetX := Value;
Invalidate;
end;
end;
procedure TcaMemMapImage.SetOffsetY(const Value: Integer);
begin
if Value <> FOffsetY then
begin
FOffsetY := Value;
Invalidate;
end;
end;
end.