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
| unit main;
interface
uses
Classes, Controls, Forms, StdCtrls, Db, Grids, DBTables, DBGrids, ExtCtrls;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Image1: TImage;
Button1: TButton;
Table1: TTable;
{ Ik gebruik een tabel van de vorm:
ID | Naam | Plaatje
Fields heb ik maar voor het gemak toegevoegd
}
Table1ID: TAutoIncField;
Table1Naam: TStringField;
Table1Plaatje: TBlobField;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
Graphics,
JPEG, // JPEG Unit die bij Delphi zit
GifImage; // GifImage van Anders Melander
{$R *.DFM}
type
TPicType = (ptGif, ptJPEG);
{ Een plaatje heeft een bepaalde header:
Bij gif plaatjes : GIF89a = $47, $49, $46, $38, $39, $61
Bij JPEG plaatjes: = $FF, $D8, $FF
Deze header staat dus bij deze streams niet vooraan
omdat microsoft er nog iets voor zet [OLE-pakket, naam
bestand, lokatie etc]
We moeten de cursor dus positioneren op $47 of $FF
BTW: Het kan natuurlijk voorkomen dat in een gif plaatje eerst een JPEG header
komt of andersom, of dat in een niet gif/JPEG plaatje toch een gif/JPEG
header voorkomt... [Oplossing?]
}
function PositionBlob(ABlobStream: TBlobStream; var PicType: TPicType): Boolean;
var
B: Byte;
Status: Integer;
procedure Iff(const EqualValue, Exp1, Exp2: Integer);
begin
if Status = EqualValue then
Status := Exp1
else
Status := Exp2;
end;
begin
Result := False;
Status := 0;
{ Status = 0: Nog nixs gevonden
= 1: $47 gevonden
= 2: $47, $49 gevonden
= 3: $47, $49, $46 gevonden
= 4: $47, $49, $46, $38 gevonden
= 5: $47, $49, $46, $38, $39 gevonden
= 6: $47, $49, $46, $38, $39, $61 gevonden, GIF!
= 7: $FF gevonden
= 8: $FF, $D8 gevonden
= 9: $FF, $D8, $FF gevonden, JPEG!
}
{ ABlobStream.Read(B, 1) is behoorlijk inefficient, sneller zou zijn om een
hele hap in te lezen, maar daar had ik niet zo'n zin in :) }
while (Status <> 9) and (Status <> 6) and (ABlobStream.Read(B, 1) = 1) do
case B of
$38:
Iff(3, 4, 0);
$39:
Iff(4, 5, 0);
$46:
Iff(2, 3, 0);
$47:
Status := 1;
$49:
Iff(1, 2, 0);
$61:
Iff(5, 6, 0);
$D8:
Iff(7, 8, 0);
$FF:
Iff(8, 9, 7);
else
Status := 0;
end;
case Status of
6:
begin
{ Gif gevonden, cursor op de 'G' zetten }
ABlobStream.Position := ABlobStream.Position - 6;
PicType := ptGif;
Result := True;
end;
9:
begin
{ JPEG gevonden, cursor op de eerste $FF zetten }
ABlobStream.Position := ABlobStream.Position - 3;
PicType := ptJPEG;
Result := True;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
BlobStream: TBlobStream;
Pic: TGraphic;
PicType: TPicType;
begin
BlobStream := TBlobStream.Create(Table1Plaatje, bmRead);
try
if not PositionBlob(BlobStream, PicType) then Exit;
case PicType of
ptJPEG:
Pic := TJPEGImage.Create;
ptGif:
Pic := TGIFImage.Create;
else
Exit;
end;
try
Pic.LoadFromStream(BlobStream);
Image1.Picture.Graphic := Pic;
finally
Pic.Free;
end;
finally
BlobStream.Free
end;
end;
end. |