forked from AngusJohnson/Image32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImg32.Fmt.JPG.pas
147 lines (124 loc) · 4.53 KB
/
Img32.Fmt.JPG.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
unit Img32.Fmt.JPG;
(*******************************************************************************
* Author : Angus Johnson *
* Version : 4.4 *
* Date : 12 March 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2019-2023 *
* Purpose : JPG/JPEG file format extension for TImage32 *
* License : http://www.boost.org/LICENSE_1_0.txt *
*******************************************************************************)
interface
{$IFNDEF FPC}
{$I Img32.inc}
uses
SysUtils, Classes, Windows, Math, Img32, JPEG;
type
TImageFormat_JPG = class(TImageFormat)
public
class function IsValidImageStream(stream: TStream): Boolean; override;
function LoadFromStream(stream: TStream;
img32: TImage32; imgIndex: integer = 0): Boolean; override;
procedure SaveToStream(stream: TStream;
img32: TImage32; quality: integer = 0); override;
class function CopyToClipboard(img32: TImage32): Boolean; override;
class function CanPasteFromClipboard: Boolean; override;
class function PasteFromClipboard(img32: TImage32): Boolean; override;
end;
var
CF_JPG: Cardinal = 0; //Windows Clipboard
CF_IMAGEJPG: Cardinal = 0;
{$ENDIF}
implementation
{$IFNDEF FPC}
//------------------------------------------------------------------------------
// Loading (reading) Jpeg images from file ...
//------------------------------------------------------------------------------
type
TJpegImageHack = class(TJpegImage);
class function TImageFormat_JPG.IsValidImageStream(stream: TStream): Boolean;
var
savedPos: integer;
flag: Cardinal;
begin
Result := false;
savedPos := stream.position;
if stream.size - savedPos <= 4 then Exit;
stream.read(flag, SizeOf(flag));
stream.Position := savedPos;
result := flag and $FFFFFF = $FFD8FF;
end;
//------------------------------------------------------------------------------
function TImageFormat_JPG.LoadFromStream(stream: TStream;
img32: TImage32; imgIndex: integer = 0): Boolean;
var
jpeg: TJpegImage;
begin
result := false;
jpeg := TJpegImage.Create;
try
jpeg.LoadFromStream(stream);
if jpeg.Empty then Exit;
with TJpegImageHack(jpeg).Bitmap do
try
if GetCurrentThreadId <> MainThreadID then Canvas.Lock;
img32.CopyFromDC(Canvas.Handle, Rect(0,0, Width, Height));
finally
if GetCurrentThreadId <> MainThreadID then Canvas.Unlock;
end;
result := true;
finally
jpeg.Free;
end;
end;
//------------------------------------------------------------------------------
// Saving (writing) Jpeg images to file ...
//------------------------------------------------------------------------------
procedure TImageFormat_JPG.SaveToStream(stream: TStream;
img32: TImage32; quality: integer = 0);
var
Jpeg: TJpegImage;
begin
Jpeg := TJpegImage.Create;
with TJpegImageHack(jpeg) do
try
if (quality > 0) and (quality <= 100) then
CompressionQuality := quality;
NewImage;
NewBitmap;
Bitmap.Width := img32.Width;
Bitmap.Height := img32.Height;
if GetCurrentThreadId <> MainThreadID then Bitmap.Canvas.Lock;
try
img32.CopyToDc(Bitmap.Canvas.Handle, 0, 0, false);
finally
if GetCurrentThreadId <> MainThreadID then Bitmap.Canvas.Unlock;
end;
SaveToStream(stream);
finally
Free;
end;
end;
//------------------------------------------------------------------------------
class function TImageFormat_JPG.CopyToClipboard(img32: TImage32): Boolean;
begin
result := false; //not implemented
end;
//------------------------------------------------------------------------------
class function TImageFormat_JPG.CanPasteFromClipboard: Boolean;
begin
result := false;
end;
//------------------------------------------------------------------------------
class function TImageFormat_JPG.PasteFromClipboard(img32: TImage32): Boolean;
begin
result := false; //not implemented
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
initialization
TImage32.RegisterImageFormatClass('JPG', TImageFormat_JPG, cpLow);
TImage32.RegisterImageFormatClass('JPEG', TImageFormat_JPG, cpLow);
//don't bother with clipboard formats as PNG and BMP formats are preferred
{$ENDIF}
end.