-
Notifications
You must be signed in to change notification settings - Fork 5
/
Direct2DImage.pas
374 lines (334 loc) · 8.92 KB
/
Direct2DImage.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
unit Direct2DImage;
interface
uses
Winapi.Windows,
System.Classes,
Vcl.Graphics, Vcl.Controls;
type
TWICPicture = class(TPicture)
protected
procedure FindGraphicClass(const Context: TFindGraphicClassContext;
var GraphicClass: TGraphicClass); override;
public
procedure Assign(Source: TPersistent); override;
end;
TDirect2DImage = class sealed (TGraphicControl)
strict private
FPicture: TWicPicture;
FStretch: Boolean;
FCenter: Boolean;
FDrawing: Boolean;
FProportional: Boolean;
procedure PictureChanged(Sender: TObject);
procedure SetCenter(Value: Boolean);
procedure SetPicture(Value: TWicPicture);
procedure SetStretch(Value: Boolean);
procedure SetProportional(Value: Boolean);
procedure DirectPaint(const ADestRect: TRect);
procedure FindGraphicClass(Sender: TObject; const Context: TFindGraphicClassContext;
var GraphicClass: TGraphicClass);
function DestRect: TRect;
strict protected
function CanObserve(const ID: Integer): Boolean; override;
function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override;
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Align;
property Anchors;
property AutoSize;
property Center: Boolean read FCenter write SetCenter default False;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property ParentShowHint;
property Picture: TWICPicture read FPicture write SetPicture;
property PopupMenu;
property Proportional: Boolean read FProportional write SetProportional default false;
property ShowHint;
property Stretch: Boolean read FStretch write SetStretch default False;
property Touch;
property Visible;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnGesture;
property OnMouseActivate;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
procedure Register;
implementation
uses
Winapi.D2D1, Winapi.Wincodec, Winapi.Messages,
System.SysUtils, System.Math,
Vcl.Consts, Vcl.Direct2D, Vcl.Forms;
procedure TWICPicture.FindGraphicClass(const Context: TFindGraphicClassContext;
var GraphicClass: TGraphicClass);
begin
GraphicClass := TWICImage;
end;
procedure TWICPicture.Assign(Source: TPersistent);
var
S: TStream;
G: TGraphic;
begin
if Source = nil then
begin
Graphic := nil;
end
else if Source is TWicPicture then
begin
Graphic := TWicPicture(Source).Graphic;
end
else if Source is TPicture then
begin
if TPicture(Source).Graphic = nil then
begin
Graphic := nil;
end
else
if TPicture(Source).Graphic is TWicImage then
begin
Graphic := TPicture(Source).Graphic;
end
else
begin
S := TMemoryStream.Create;
try
TPicture(Source).Graphic.SaveToStream(S);
S.Seek(0, TSeekOrigin.soBeginning);
G := TWicImage.Create;
try
G.LoadFromStream(S);
Graphic := G;
finally
G.Free;
end;
finally
S.Free;
end;
end;
end
else if Source is TWicImage then
Graphic := TWicImage(Source)
else
inherited Assign(Source);
end;
constructor TDirect2DImage.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable, csPannable];
FPicture := TWicPicture.Create;
FPicture.OnChange := PictureChanged;
FPicture.OnFindGraphicClass := FindGraphicClass;
Height := 105;
Width := 105;
end;
destructor TDirect2DImage.Destroy;
begin
FPicture.Free;
inherited Destroy;
end;
function TDirect2DImage.CanObserve(const ID: Integer): Boolean;
begin
Result := False;
if ID = TObserverMapping.EditLinkID then
Result := True;
end;
function TDirect2DImage.DestRect: TRect;
var
w, h, cw, ch: Integer;
xyaspect: Double;
begin
w := Picture.Width;
h := Picture.Height;
cw := ClientWidth;
ch := ClientHeight;
if Stretch or (Proportional and ((w > cw) or (h > ch))) then
begin
if Proportional and (w > 0) and (h > 0) then
begin
xyaspect := w / h;
if w > h then
begin
w := cw;
h := Trunc(cw / xyaspect);
if h > ch then // woops, too big
begin
h := ch;
w := Trunc(ch * xyaspect);
end;
end
else
begin
h := ch;
w := Trunc(ch * xyaspect);
if w > cw then // woops, too big
begin
w := cw;
h := Trunc(cw / xyaspect);
end;
end;
end
else
begin
w := cw;
h := ch;
end;
end;
Result := TRect.Create(0, 0, w, h);
if Center then
begin
OffsetRect(Result, (cw - w) div 2, (ch - h) div 2);
end;
end;
procedure WicCheck(AHResult: HRESULT);
begin
if Failed(AHResult) then
begin
raise EInvalidGraphic.Create(SInvalidImage + ': ' + SysErrorMessage(AHResult));
end;
end;
procedure TDirect2DImage.DirectPaint(const ADestRect: TRect);
var
DirectCanvas: TDirect2DCanvas;
DirectBitmap: ID2D1Bitmap;
RenderTarget: ID2D1RenderTarget;
DestRect: TD2D1RectF;
WicImage: TWicImage;
ImagingFactory: IWicImagingFactory;
BitmapScaler: IWICBitmapScaler;
FormatConverter: IWicFormatConverter;
begin
DirectCanvas := TDirect2DCanvas.Create(Canvas.Handle, ADestRect);
try
RenderTarget := DirectCanvas.RenderTarget;
WicImage := FPicture.Graphic as TWicImage;
ImagingFactory := WicImage.ImagingFactory;
WicCheck(ImagingFactory.CreateBitmapScaler(BitmapScaler));
WicCheck(BitmapScaler.Initialize(WicImage.Handle, ADestRect.Width, ADestRect.Height,
WICBitmapInterpolationModeFant));
WicCheck(ImagingFactory.CreateFormatConverter(FormatConverter));
WicCheck(FormatConverter.Initialize(BitmapScaler, GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone, nil, 0, WICBitmapPaletteTypeCustom));
WicCheck(RenderTarget.CreateBitmapFromWicBitmap(FormatConverter, nil, DirectBitmap));
RenderTarget.BeginDraw();
DestRect := ADestRect;
RenderTarget.DrawBitmap(DirectBitmap, @DestRect);
RenderTarget.EndDraw();
finally
DirectCanvas.Free;
end;
end;
procedure TDirect2DImage.Paint;
var
Save: Boolean;
begin
if csDesigning in ComponentState then
begin
Canvas.Pen.Style := psDash;
Canvas.Brush.Style := bsClear;
Canvas.Rectangle(0, 0, Width, Height);
end;
Save := FDrawing;
FDrawing := True;
try
if Assigned(Picture.Graphic) then
begin
DirectPaint(DestRect);
end;
finally
FDrawing := Save;
end;
end;
procedure TDirect2DImage.FindGraphicClass(Sender: TObject;
const Context: TFindGraphicClassContext; var GraphicClass: TGraphicClass);
begin
GraphicClass := TWICImage;
end;
procedure TDirect2DImage.SetCenter(Value: Boolean);
begin
if FCenter <> Value then
begin
FCenter := Value;
PictureChanged(Self);
end;
end;
procedure TDirect2DImage.SetPicture(Value: TWicPicture);
begin
FPicture.Assign(Value);
end;
procedure TDirect2DImage.SetStretch(Value: Boolean);
begin
if Value <> FStretch then
begin
FStretch := Value;
PictureChanged(Self);
end;
end;
procedure TDirect2DImage.SetProportional(Value: Boolean);
begin
if FProportional <> Value then
begin
FProportional := Value;
PictureChanged(Self);
end;
end;
procedure TDirect2DImage.PictureChanged(Sender: TObject);
var
G: TGraphic;
D: TRect;
begin
if Observers.IsObserving(TObserverMapping.EditLinkID) then
if TLinkObservers.EditLinkEdit(Observers) then
TLinkObservers.EditLinkModified(Observers);
if AutoSize and (Picture.Width > 0) and (Picture.Height > 0) then
SetBounds(Left, Top, Picture.Width, Picture.Height);
G := Picture.Graphic;
if G <> nil then
begin
D := DestRect;
if (D.Left <= 0) and (D.Top <= 0) and
(D.Right >= Width) and (D.Bottom >= Height) then
ControlStyle := ControlStyle + [csOpaque]
else // picture might not cover entire clientrect
ControlStyle := ControlStyle - [csOpaque];
end
else
ControlStyle := ControlStyle - [csOpaque];
if not FDrawing then Invalidate;
if Observers.IsObserving(TObserverMapping.EditLinkID) then
if TLinkObservers.EditLinkIsEditing(Observers) then
TLinkObservers.EditLinkUpdate(Observers);
end;
function TDirect2DImage.CanAutoSize(var NewWidth, NewHeight: Integer): Boolean;
begin
Result := True;
if not (csDesigning in ComponentState) or (Picture.Width > 0) and
(Picture.Height > 0) then
begin
if Align in [alNone, alLeft, alRight] then
NewWidth := Picture.Width;
if Align in [alNone, alTop, alBottom] then
NewHeight := Picture.Height;
end;
end;
procedure Register;
begin
RegisterComponents('Direct2DImage', [TDirect2DImage]);
end;
end.