-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlayout.pas
475 lines (424 loc) · 11 KB
/
layout.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
unit layout;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
fpg_main,
fpg_base,
fpg_img_utils,
utils,
FPimage,
graphics,
viewercanvas,
seriecell,
imagecell,
basiccell,
baselayout,
Scout,
series,
hangingprotocol,
layoutdefinition,
paperprint,
bitmapprint,
printers,
images;
type
(* TLayoutCells container *)
TLayout = class(TBaseLayout)
private
FOwner: TObject;
FCells: TSerieCells;
FOnRepaint: TNotifyEvent;
FSeries: TSeries;
procedure SetSeries(AValue: TSeries);
procedure ToPrinter(AImage: TfpgImage);
procedure DrawToCanvas(ACanvas: TViewerCanvas);
public
constructor Create(AOwner: TObject);
destructor Destroy; override;
procedure CreateLayout(ALayoutDefinition: TLayoutDefinition);
procedure Draw(AViewerCanvas: TViewerCanvas);
procedure SetCurrentCell(AX, AY: Integer);
procedure SetAllChanged;
procedure DeleteSelectedVector;
procedure ApplyHangingProtocol(AHangingProtocols: THangingProtocols);
procedure ToBmp(AWidth, AHeight: Integer);
procedure ToPrinter;
property Series: TSeries read FSeries write SetSeries;
property Cells: TSerieCells read FCells;
property OnRepaint: TNotifyEvent read FOnRepaint write FOnRepaint;
end;
{ TSingleImageLayout }
TSingleImageLayout = class(TLayout)
public
constructor Create;
end;
implementation
{ TSingleImageLayout }
constructor TSingleImageLayout.Create;
var
lCell: TSerieCell;
lImageCell: TImageCell;
begin
(* Este layout se crea cuando el usuario
hace doble click sobre una celda,
muestra una imágen a pantalla completa.
*)
CurrentCell := nil;
FCells := TSerieCells.Create;
(* Celda 0,0 *)
lCell := TSerieCell.Create(Self);
lCell.Left := 0;
lCell.Top := 0;
lCell.Width:= 1;
lCell.Height:= 1;
lImageCell := TImageCell.Create(lCell);
lImageCell.Left := 0;
lImageCell.Top := 0;
lImageCell.Width := 1;
lImageCell.Height := 1;
lCell.ImageCells.Add(lImageCell);
FCells.Add(lCell);
end;
{ TLayout }
constructor TLayout.Create(AOwner: TObject);
begin
FOwner := AOwner;
CurrentCell := nil;
FCells := TSerieCells.Create;
end;
destructor TLayout.Destroy;
var
lCell: TSerieCell;
begin
for lCell in FCells do
begin
lCell.free;
end;
FCells.Free;
inherited Destroy;
end;
procedure TLayout.CreateLayout(ALayoutDefinition: TLayoutDefinition);
var
lX: Integer;
lY: Integer;
lCell: TSerieCell;
lImageCell: TImageCell;
I: Integer;
begin
FCells.Clear;
for I := 0 to Length(ALayoutDefinition) - 1 do
begin
lCell := TSerieCell.Create(Self);
lCell.Left := ALayoutDefinition[I].Left;
lCell.Top := ALayoutDefinition[I].Top;
lCell.Width:= ALayoutDefinition[I].width;
lCell.Height:= ALayoutDefinition[I].height;
lImageCell := TImageCell.Create(lCell);
lImageCell.Left := 0;
lImageCell.Top := 0;
lImageCell.Width := 1;
lImageCell.Height := 1;
lCell.ImageCells.Add(lImageCell);
FCells.Add(lCell);
end;
end;
procedure TLayout.Draw(AViewerCanvas: TViewerCanvas);
var
lCell: TSerieCell;
lImageType: string;
lSerie: TSerie;
lScout: TScout;
lCurrentImage: TImage;
lRect: TBasicCellRect;
begin
(* Se calcula el localizer *)
for lCell in FCells do
begin
(* En el caso de CT, buscamos el Localizer *)
if lCell.ImageCells.Count = 0 then
continue;
if lCell.ImageCells[0].Image = nil then
continue;
if CurrentCell = nil then
continue;
lCurrentImage := (CurrentCell as TSerieCell).ImageCells[0].Image;
if lCurrentImage = nil then
continue;
if (lCell.ImageCells[0].Image.Modality = 'CT') or (lCell.ImageCells[0].Image.Modality = 'MR') then
begin
lImageType := lCell.ImageCells[0].Image.ImageType;
if lImageType = '' then
continue;
lSerie := (lCell.ImageCells[0].Image.Serie as TSerie);
if (lSerie.Images.Count > 0) then //and ((lImageType = 'LOCALIZER') or (lImageType = 'MPR')) then
begin
(* Encontrado el Localizer, se dibuja el SCOUT LINE *)
lScout := TScout.Create(lCurrentImage, lCell.ImageCells[0].Image);
try
if lScout.DoCalcs then
begin
lSerie.ScoutColPixel := lScout.ColPixel;
lSerie.ScoutRowPixel := lScout.RowPixel;
end;
finally
lScout.Free;
end;
end;
end;
end;
// se dibujan celdas de series
if FCells.Count = 0 then
begin
AViewerCanvas.AggCanvas2D.FillColor($AA, $BB, $CC);
AViewerCanvas.AggCanvas2D.Rectangle(0, 0, Width, Height);
end
else
for lCell in FCells do
begin
if lCell = CurrentCell then
lCell.Draw(AViewerCanvas, True)
else
lCell.Draw(AViewerCanvas, False);
//lRect := lCell.GetRect;
// se dibuja el recuadro del layout
//AViewerCanvas.AggCanvas2D.LineColor($00, $FF, $FF);
//AViewerCanvas.AggCanvas2D.Rectangle(lRect.left, lRect.Top, lRect.Right, lRect.Bottom);
end;
AViewerCanvas.CanvasImage.UpdateImage;
end;
procedure TLayout.SetCurrentCell(AX, AY: Integer);
var
lCell: TSerieCell;
lRect: TBasicCellRect;
begin
for lCell in FCells do
begin
// se dibuja el recuadro de la celda
lRect := lCell.GetRect;
if (AX >= lRect.Left) and (AX <= lRect.Left + (lRect.Right - lRect.Left)) then
if (AY >= lRect.Top) and (AY <= lRect.Top + (lRect.Bottom - lRect.Top)) then
begin
CurrentCell := lCell;
lCell.SetCurrentImageCell(AX, AY);
if Assigned(FOnRepaint) then
FOnRepaint(Self);
Break;
end;
end;
end;
procedure TLayout.SetAllChanged;
var
lCell: TSerieCell;
begin
for lCell in FCells do
lCell.SetAllChanged;
end;
procedure TLayout.DeleteSelectedVector;
begin
if CurrentImageCell <> nil then
begin
(CurrentImageCell as TImageCell).DeleteCurrentVector;
end;
end;
procedure TLayout.ApplyHangingProtocol(AHangingProtocols: THangingProtocols);
var
lHp: THangingProtocol;
lHPCell: THangingProtocolCell;
lSeriesFound: Boolean;
procedure CreateOneCell(AHPCell: THangingProtocolCell);
var
lCell: TSerieCell;
lImageCell: TImageCell;
begin
lCell := TSerieCell.Create(Self);
lCell.Left:= AHPCell.Left;
lCell.Top:= AHPCell.Top;
lCell.Width:= AHPCell.Width;
lCell.Height:= AHPCell.Height;
lCell.Serie := AHPCell.FindSerie(FSeries);
lImageCell := TImageCell.Create(lCell);
lImageCell.Left := 0;
lImageCell.Top := 0;
lImageCell.Width := 1;
lImageCell.Height := 1;
lImageCell.RightScale := AHPCell.HangingProtocol.RightScale;
lCell.ImageCells.Add(lImageCell);
FCells.Add(lCell);
end;
begin
FCells.Clear;
CurrentCell := nil;
CurrentImageCell := nil;
lSeriesFound:= False;
lHp := AHangingProtocols.Current;
if lHp = nil then
begin
lHp := THangingProtocol.Create(AHangingProtocols);
lHPCell := THangingProtocolCell.Create;
lHPCell.Left:= 0;
lHPCell.Top:= 0;
lHPCell.Width:= 1;
lHPCell.Height:= 1;
lHPCell.HangingProtocol := lHp;
lHP.Cells.Add(lHPCell);
CreateOneCell(lHpCell);
AHangingProtocols.Current := lHp;
end
else
begin
for lHPCell in lHp.Cells do
begin
lHPCell.HangingProtocol := lHp;
CreateOneCell(lHPCell);
lSeriesFound := lSeriesFound or FCells.HasImages;
end;
if not lSeriesFound then
begin
(* Si no se encontraron imagenes y se fue cambiando de h.p. hasta
que se llegó al último, entonces se setea el 1er H.P. *)
if AHangingProtocols.Current <> AHangingProtocols.Items[AHangingProtocols.Count - 1] then
begin
AHangingProtocols.Next;
ApplyHangingProtocol(AHangingProtocols);
end
else
AHangingProtocols.Current := AHangingProtocols.Items[0];
end;
end;
end;
procedure TLayout.ToBmp(AWidth, AHeight: Integer);
var
lCanvas: TViewerCanvas;
lBmp: TFPMemoryImage;
lWidth: Integer;
lHeight: Integer;
lFile: string;
begin
lWidth := AWidth;
lHeight := AHeight;
if TdlgBitmapPrint.Execute(lWidth, lHeight, lFile) then
begin
lBmp := TFPMemoryImage.create(lWidth, lHeight);
lCanvas := TViewerCanvas.Create(72);
try
lCanvas.Allocate(lBmp.Width, lBmp.Height);
DrawToCanvas(lCanvas);
FpgImageToFPImage(lCanvas.CanvasImage, lBmp);
lBmp.SaveToFile(lFile);
finally
lCanvas.Free;
lBmp.Free;
end;
end;
end;
procedure TLayout.ToPrinter;
var
lCanvas: TViewerCanvas;
begin
if TdlgPaperPrint.Execute then
begin
lCanvas := TViewerCanvas.Create(Printer.XDPI);
try
lCanvas.Allocate(Printer.PageWidth, Printer.PageHeight);
DrawToCanvas(lCanvas);
ToPrinter(lCanvas.CanvasImage);
//lBmp := TFPMemoryImage.create(lCanvas.CanvasImage.Width, lCanvas.CanvasImage.Height);
//FpgImageToFPImage(lCanvas.CanvasImage, lBmp);
//lBmp.SaveToFile(ExtractFilePath(ParamStr(0)) + 'salida.jpeg');
//lBmp.Free;
finally
lCanvas.Free;
end;
end;
end;
procedure TLayout.ToPrinter(AImage: TfpgImage);
var
Y: Integer;
X: Integer;
lScanLine: PByte;
lBitmap: TBitmap;
procedure SetScanLinePixel(x,y:integer; c:longword; var surface:TBitmap);
begin
{ this typecast makes X add by multiples of 3 }
PLongWord(NativeInt(surface.Scanline[y]) + x * 4)^:= c;
end;
begin
lBitmap := TBitmap.Create;
try
lBitmap.Width := AImage.Width;
lBitmap.Height := AImage.Height;
for Y := 0 to lBitmap.Height - 1 do
begin
for X := 0 to lBitmap.Width - 1 do
begin
SetScanLinePixel(X, Y, AImage.Colors[X, Y], lBitmap);
end;
end;
Printer.BeginDoc;
Printer.Canvas.Draw(0, 0, lBitmap);
Printer.EndDoc;
finally
lBitmap.Free;
end;
end;
procedure TLayout.DrawToCanvas(ACanvas: TViewerCanvas);
var
lFontSize: double;
lCell: TSerieCell;
lOrigPixelWidth: Integer;
lOrigPixelHeight: Integer;
lImageCell: TImageCell;
begin
// para hoja A4 se usa fuente 24
lFontSize := ACanvas.CanvasImage.Height * 24/3300;
ACanvas.AggCanvas2D.Font(utils.GetFont, lFontSize);
// se setean los nuevos valores
for lCell in FCells do
begin
lOrigPixelWidth := lCell.PixelWidth;
lOrigPixelHeight := lCell.PixelHeight;
lCell.PixelWidth := ACanvas.CanvasImage.Width;
lCell.PixelHeight := ACanvas.CanvasImage.Height;
for lImageCell in lCell.ImageCells do
begin
if lImageCell.Image <> nil then
begin
lImageCell.ZoomToFit;
lImageCell.AlignByModality;
end;
end;
end;
SetAllChanged;
Draw(ACanvas);
// se vuelve todo a la normalidad
for lCell in FCells do
begin
lCell.PixelWidth := lOrigPixelWidth;
lCell.PixelHeight := lOrigPixelHeight;
for lImageCell in lCell.ImageCells do
begin
if lImageCell.Image <> nil then
begin
lImageCell.ZoomToFit;
lImageCell.AlignByModality;
end;
end;
end;
SetAllChanged;
end;
procedure TLayout.SetSeries(AValue: TSeries);
var
I: Integer;
lCell: TSerieCell;
begin
FSeries := AValue;
for I := 0 to FCells.Count - 1 do
begin
lCell := FCells[I];
if FSeries.Count > I then
lCell.Serie := FSeries[I];
end;
SetAllChanged;
end;
end.