-
Notifications
You must be signed in to change notification settings - Fork 19
/
cGraphLoader.pas
1101 lines (891 loc) · 38.1 KB
/
cGraphLoader.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
UNIT cGraphLoader;
{=============================================================================================================
Gabriel Moraru
2024.12
See Copyright.txt
--------------------------------------------------------------------------------------------------------------
Helps you load common file formats (GIF, JPG, BMP, PNG, WB1, RainDrop, JPG2K) from disk.
LoadGraph is the main function.
The functions will not fail if the input image is corrupted. Instead it will simply output the error to the log.
This is useful if we want to process thousands of images at batch because we don't want to stop everytime we encounter a broken image.
Speed
The functions support WIC which is much faster that the classic VCL loading functions
(for example for JPG, WIC is at least 8x faster than Delphi's JPG decoding function).
External dependencies:
* CCR Exif lib -> Github.com/exilon/ccr-exif
* Jpeg2000 lib
* FastJpg
Undefine CCRExif, FastJpg, and Jpg2000 if you don't want to download and compile those libraries. In this case, some functionality will not be available.
TESTER:
c:\Myprojects\Project Testers\gr LoadGraph\
c:\MyProjects\Projects GRAPHICS Resamplers\GLOBAL Tester\TEST IMAGES\
-------------------------------------------------------------------------------------------------------------}
{PNG: see this: http://talkdelphi.blogspot.com/2009_03_01_archive.html }
{todo: is it possible to extract the EXIF information from a jpeg file without loading the file in memory? }
INTERFACE
USES
Winapi.Wincodec, System.SysUtils, System.Math, System.Types, System.Classes,
Vcl.Graphics, Vcl.Imaging.Jpeg, Vcl.Imaging.PngImage, Vcl.ExtCtrls, Vcl.Imaging.GIFImg
{$IFDEF CCRExif},CCR.Exif{$ENDIF};
CONST
DelphiJpgQuality = 70; { Average image quality. Seems that 70 is indeed the best value }
{$IFNDEF CCRExif}
TYPE
TJpegImageEx= TJpegImage;
{$ENDIF}
{-------------------------------------------------------------------------------------------------------------
MAIN FUNCTION
-------------------------------------------------------------------------------------------------------------}
function LoadGraph (CONST FileName: string; ExifRotate: Boolean = True; UseWic: Boolean = True): TBitmap; overload;
function LoadGraph (CONST FileName: string; OUT FrameCount: Cardinal): TBitmap; overload;
procedure LoadGraphToImg(CONST FileName: string; Image: TImage; ExifRotate: Boolean = True; UseWic: Boolean = TRUE);
{-------------------------------------------------------------------------------------------------------------
VCL LOADERS
-------------------------------------------------------------------------------------------------------------}
function LoadFromResource (CONST RsrcName: string): TJPEGImage; { Load an image from a resource file }
function LoadTPicture (CONST FileName: string): TPicture; { Based on TPicture. Better than LoadGraph. }
procedure LoadToTImage (CONST FileName: string; ExifRotate: Boolean; Image: TImage); { Loads a file directly into a TImage component }
{-------------------------------------------------------------------------------------------------------------
SPECIFIC LOADERS
These functions are based on VCL (which is slower than WIC)
-------------------------------------------------------------------------------------------------------------}
{$IFDEF Jpg2000}
function LoadJ2K (CONST FileName: string): TBitmap; {$ENDIF}
function LoadJpg (CONST FileName: string; Scale: TJPEGScale= jsFullSize): TBitmap;
function LoadPNG (CONST FileName: string): TBitmap; overload;
function LoadGIF (CONST FileName: string): TBitmap; overload; { Load GIF and convert it to BMP }
function LoadGIF (CONST FileName: string; OUT FrameCount: Cardinal): TBitmap; overload;
function LoadWB1 (CONST FileName: string): TBitmap;
function LoadICO (CONST FileName: string): TBitmap;
function LoadEMF (CONST FileName: string): TBitmap;
function LoadBMP (CONST FileName: string): TBitmap; { Loads a BMP and supress error messages }
// JPG THUMBNAIL LOADER
function ExtractThumbnailJpg(CONST FileName: string; ThumbWidth, ThumbHeight: integer; OUT ResolutionX, ResolutionY: Integer): TBitmap; { Extracts from a JPEG image using scaling. The scale is automatically chousen based on the original image size and required thumb size } { Old name: LoadJpgThumbnail }
function ExtractThumbnail (CONST FileName: string; ThumbWidth: integer; OUT ResolutionX, ResolutionY: Integer; OUT FrameCount: Cardinal): TBitmap; overload; { Extracts the thumbnail from a gif, avi, jpg, png, etc file }
function ExtractThumbnail (CONST FileName: string; ThumbWidth: Integer): TBitmap; overload;
function loadGraphWic (CONST FileName: string): TBitmap; { Supports: GIF, PNG, JPG. Use LoadGraph instead }
{-------------------------------------------------------------------------------------------------------------
GRAY LOADERS
-------------------------------------------------------------------------------------------------------------}
function LoadGraphAsGrayScale(FileName: string): TBitmap; overload;
procedure LoadGraphAsGrayScale(FileName: string; BMP: TBitmap); overload;
{-------------------------------------------------------------------------------------------------------------
Imgage format utils
-------------------------------------------------------------------------------------------------------------}
function DetectGraphSignature(CONST FileName: string): Integer;
function CheckValidImage (CONST FileName: string): Boolean;
{$IFDEF CCRExif}
function GetExif (CONST FileName: string): TExifData; {$ENDIF}
IMPLEMENTATION
USES
{$IFDEF Jpg2000}OpenJpeg2000Bitmap,{$ENDIF} // Download OpenJpeg Pas library from: www.github.com/galfar/PasJpeg2000
{$IFDEF FastJpg}FastJpegDecHelper,{$ENDIF}
cGraphResize, cGraphResizeVCL, cGraphLoader.Resolution, cGraphUtilGray,
cGraphLoader.WB1, cGraphLoader.RainDrop, ccIO, cmIO, cGraphFx.Rotate, cbAppData, ccCore, cGraphAviFrame, cGraphGIF;
{-------------------------------------------------------------------------------------------------------------
IMAGE LOADERS
Load image based on signature and not on file extension.
Supported formats: GIF, JPG (+Exif), BMP, PNG, J2K, WB1
Guarantees not to crash if the image is bad.
-------------------------------------------------------------------------------------------------------------}
{-------------------------------------------------------------------------------------------------------------
LOAD WIC
-------------------------------------------------------------------------------------------------------------}
{ Uses Vcl.Graphics.TWICImage
At least 8x faster than Delphi's JPG function
Formats:
Should work with: BMP, GIF, ICO, JPEG, PNG, TIF and Windows Media Photo
Tested with animated GIF, PNG, JPG.
Fails with JPEG2K, WB1
Source: https://stackoverflow.com/questions/53382075/jpeg-to-bmp-conversion-takes-unreasonable-amount-of-time/53387410#53387410 }
function loadGraphWic(CONST FileName: string): TBitmap; //Todo: test with BMP
VAR
wic: TWICImage;
begin
Result:= NIL;
Assert(FileExistsMsg(FileName));
wic := TWICImage.Create;
TRY
TRY
wic.LoadFromFile(FileName);
EXCEPT
on E: Exception do
begin
{ Don't crash on invalid images. Show the problem in log }
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
EXIT(NIL);
end;
END;
Result := TBitmap.Create;
TRY
Result.Assign(wic);
EXCEPT
on E: Exception do
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result);
end;
END;
FINALLY
FreeAndNil(wic);
END;
end;
//ToDo: open JPG in Wic and in JpegDecHelper. which one is faster?
{-------------------------------------------------------------------------------------------------------------
MAIN LOADER
Supports:
GIF, PNG, JPG, JPEG2K, BMP, WB1, TIF and Windows Media Photo
Speed:
Fast
The only library better is \Third party packages\JpegTurbo\ but I have to deliver a DLL.
Decoders:
JPG: via AsmJpegDec wich is VERY fast compared with Delphi and WIC but it is not working with all jpeg images. In this case we fall back to WIC or the standard LoadGraph loader (WIC).
GIF: via WIC
PNG: via WIC
Warning:
Looks like WIC will convert all images to pf32. I tried it with a 8bit (grayscale) bitmap and it converted the image to pf32.
-------------------------------------------------------------------------------------------------------------}
{$IFDEF CCRExif}
function GetExif(CONST FileName: string): TExifData;
begin
Assert(FileExistsMsg(FileName));
// Prevents this issue: Cannot open file "D:\FTP\imageWilkinDriveway.jpg". The process cannot access the file because it is being used by another process.
// The check is better to be done in LoadGraph: if (Result <> NIL) AND ExifRotate then
// Delete this check here as soon as it is confirmed that check works!
Result:= TExifData.Create;
TRY
Result.LoadFromGraphic(FileName);
EXCEPT
on E: Exception do //todo 1: trap only specific exceptions
begin
FreeAndNil(Result);
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
end;
END;
end;
{$ELSE}
//RAISE Exception.Create('CCREXIF NOT AVAILABLE');
{$ENDIF}
function LoadGraph(CONST FileName: string; ExifRotate: Boolean = True; UseWic: Boolean = TRUE): TBitmap;
//todo 1: CAN I LOAD THE JPEG WITH WIC AND THEN REOPEN IT AND CHECK IF THERE IS EXIF INSIDE?
VAR
Signature: Integer;
begin
Assert(FileExistsMsg(FileName));
TRY
{ Detect image by signature, not by extension }
Signature:= DetectGraphSignature(FileName);
EXCEPT
on E: Exception do
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
EXIT(NIL); { Do not crash on failure }
end;
END;
{ Before loading an image (any image) check if its file extension matches the binary header.
Note: No need to do it for jpeg images with png ext. The algorithm already recognizes that and loads the image just fine. }
if IsJpg(FileName) AND (Signature= 2) then
begin
AppData.LogWarn(FileName+ CRLF+ ' is a PNG file with invalid file extension (jpg). You can fix this by changing the extension from JPG to PNG.');
EXIT(NIL);
end;
{ Create the right type of loader }
case Signature of
{ BMP}
1 : Result:= LoadBMP(FileName); { Don't use WIC to load BMP files! For misterious reasons, WIC is terrible slow when loading bmp files! }
{ PNG}
2 : if UseWic
then Result:= loadGraphWic(FileName)
else Result:= LoadPNG(FileName);
{ GIF}
3 : if UseWic
then Result:= loadGraphWic(FileName)
else Result:= LoadGIF(FileName);
{ JPG}
4 : begin
{$IFDEF FastJpg}
Result:= FastJpegDecHelper.FastJpgDecode(FileName);
if Result = NIL then { Not all jpegs are supported by JpegDecHelper. In this case we fall back to WIC or the standard LoadGraph loader (WIC). }
{$ELSE}
AppData.LogWarn('FastJpg not available!');
{$ENDIF}
{ToDo 1: is JpegDecHelper indeed faster than WIC? }
if UseWic
then Result:= loadGraphWic(FileName)
else Result:= LoadJpg(FileName);
{ Support for EXIF Jpegs }
if (Result <> NIL) AND ExifRotate then
begin
{$IFDEF CCRExif}
VAR ExifData:= GetExif(FileName);
cGraphFx.Rotate.RotateExif(Result, ExifData);
FreeAndNil(ExifData);
{$ELSE}
AppData.LogVerb('FastJpg not available!');
{$ENDIF}
end;
end;
{ Jpeg2000 }
{$IFDEF Jpg2000}
5 : Result:= LoadJ2K(FileName);
{$ELSE}
5 : RAISE Exception.Create('Jpeg2000 not supported yet on Win 64 bit!');
{$ENDIF}
{ WB1}
6 : Result:= LoadWB1(FileName);
{ RainDrop }
7 : Result:= LoadRainShelter(FileName);
else
{ TIF or something else? }
Result:= loadGraphWic(FileName);
end;
end;
{ Use it for animated files }
function LoadGraph(CONST FileName: string; OUT FrameCount: Cardinal): TBitmap; { FrameCount is -1 in case of error (gif cannot be decoded), 0 if the image is not a gif, 1 for static gifs and >1 for animated gifs }
VAR Signature: Integer;
begin
Assert(FileExistsMsg(FileName));
Signature:= DetectGraphSignature(FileName); { Detect image by signature, not by extension }
FrameCount:= 0;
if Signature = 3
then Result:= LoadGIF(FileName, FrameCount)
else Result:= LoadGraph (FileName, True);
end;
procedure LoadGraphToImg(CONST FileName: string; Image: TImage; ExifRotate: Boolean = True; UseWic: Boolean = TRUE);
begin
VAR BMP:= LoadGraph(FileName, ExifRotate, UseWic);
TRY
Image.Picture.Assign(BMP);
FINALLY
FreeAndNil(BMP);
END;
end;
function LoadGraphAsGrayScale(FileName: string): TBitmap;
begin
Result:= LoadGraph(FileName, FALSE, TRUE);
TRY
if NOT HasGrayscalePalette(Result)
then cGraphUtilGray.ConvertToGrayscale(Result);
EXCEPT
FreeAndNil(Result);
END;
end;
procedure LoadGraphAsGrayScale(FileName: string; BMP: TBitmap);
begin
Assert(BMP <> NIL);
VAR Temp:= LoadGraph(FileName, FALSE, TRUE);
TRY
if NOT HasGrayscalePalette(Temp)
then cGraphUtilGray.ConvertToGrayscale(Temp);
BMP.Assign(Temp);
FINALLY
FreeAndNil(Temp);
END;
end;
(*
{ Same as the 'standard' LoadGraph but it uses the special LoadJpg function for JPGs in order to retrieve the EXIF information.
Use it when you need exif info from JPEG files, but there could also be other files there. }
function LoadGraph(CONST FileName: string; OUT ExifData: TExifData): TBitmap;
VAR Signature: Integer;
begin
Assert(FileExistsMsg(FileName));
Signature:= DetectGraphSignature(FileName); { Detect image by signature, not by extension }
if Signature = 4
then
begin
Result:= LoadJpgBorland(FileName, ExifRotate, jsFullSize);
{ Support for EXIF Jpegs }
if ExifRotate then
begin
ExifData:= GetExif(FileName);
cGraphics.RotateExif(Result, ExifData);
end;
end
else
Result:= LoadGraph(FileName, True);
end;
*)
{--------------------------------------------------------------------------------------------------
VCL LOADERS
note: TImage.LoadFromFile supports JPG2K (confirm that)
--------------------------------------------------------------------------------------------------}
{ Loads a file directly into a TImage component }
procedure LoadToTImage(CONST FileName: string; ExifRotate: Boolean; Image: TImage);
VAR BMP: TBitmap;
begin
BMP:= LoadGraph(FileName, ExifRotate);
TRY
Image.Picture.Bitmap.Assign(BMP);
FINALLY
FreeAndNil(BMP);
end;
end;
{ Uses TPicture's facilities to load the image.
Does not support Jpg2K.
Takes less RAM than LoadGraph but it is slower. }
function LoadTPicture(CONST FileName: string): TPicture;
begin
Assert(FileExistsMsg(FileName));
Result:= TPicture.Create;
TRY
Result.LoadFromFile(FileName);
EXCEPT
on E: Exception do //todo 1: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result); { Don't crash on invalid images }
end;
end;
end;
{ Load a JPG image from a resource file }
function LoadFromResource(CONST RsrcName: string): TJPEGImage;
VAR
Stream: TStream;
begin
Result := TJPEGImage.Create;
TRY
Stream := TResourceStream.Create(HInstance, RsrcName, RT_RCDATA);
TRY
Result.LoadFromStream(Stream);
FINALLY
FreeAndNil(Stream);
END;
EXCEPT
FreeAndNil(Result);
RAISE;
END;
end;
{--------------------------------------------------------------------------------------------------
LOAD THUMBANAIL
--------------------------------------------------------------------------------------------------}
{ Extracts the thumbnail from a gif, avi, jpg, png, etc file. Very fast!
If the file is animated it extracts the frame from the middle of the animation.
The scale is automatically chousen based on the original image size and required thumb size.
The higher the scaling factor, the faster the loading time.
Exif:
The image will be automatically rotated according to the EXIF data
Parameters:
ThumbWidth is the desiered size of the tumbnail. Small images will actually resized up if they are smaller than ThumbWidth.
ResolutionX, ResolutionY returns the ORIGINAL resolution (before thumbnail)
Log can be NIL
Result:
Returns the thumbnail or NIL in case of failure.
} // old name: LoadJpgThumbnail
function ExtractThumbnailJpg(CONST FileName: string; ThumbWidth, ThumbHeight: Integer; OUT ResolutionX, ResolutionY: Integer): TBitmap;
VAR
Ratio: Real;
JpgLoader: TJpegImageEx;
SizeDecrease: Integer;
begin
Assert(FileExistsMsg(FileName));
JpgLoader:= TJpegImageEx.Create;
TRY
// Autodetect the best Scale factor
cGraphLoader.Resolution.GetImageRes(FileName, ResolutionX, ResolutionY);
if (ResolutionX < 0) OR (ResolutionY < 0) then EXIT(NIL);
if ThumbHeight= -1
then SizeDecrease:= trunc( ResolutionX/ThumbWidth )
else SizeDecrease:= trunc( min(ResolutionX/ThumbWidth, ResolutionY/ThumbHeight) );
case SizeDecrease of
0..1: JpgLoader.Scale:= jsFullSize;
2..3: JpgLoader.Scale:= jsHalf;
4..7: JpgLoader.Scale:= jsQuarter;
else
JpgLoader.Scale:= jsEighth;
end;
TRY
JpgLoader.LoadFromFile(FileName);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
EXIT(NIL);
end;
END;
// We don't want small or huge images
if (JpgLoader.Width <= 0) or (JpgLoader.Width > 32768)
or (JpgLoader.Height<= 0) or (JpgLoader.Height > 32768)
then Exit(NIL);
// We have the thumbnail but it migh not have the EXACT size we specified in the ExtractThumbnailJpg so we resize one more time
Result:= TBitmap.Create;
TRY
Result.HandleType:= bmDIB;
Result.Assign(JpgLoader); // This is where the (decoding) time is actually wasted.
{$IFDEF CCRExif}
// Rotate EXIF
cGraphFx.Rotate.RotateExif(Result, JpgLoader.ExifData);
{$ENDIF}
if ThumbHeight= -1 then
begin
Ratio:= ResolutionX / ResolutionY;
ThumbHeight:= Round(ThumbWidth / Ratio);
end;
SmartStretch(Result, ThumbWidth, ThumbHeight);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result); { We only free the result in case of failure }
end;
END;
FINALLY
FreeAndNil(JpgLoader);
end;
end;
{-------------------------------------------------------------------------------
Same as above
BUT In this function, the Height will be ignored. The thumbnail will be proportionally stretched to fit into the size specified by ThumbWidth
If you set ThumbHeight to -1 then the Height will be ignored.
The thumbnail will be proportionally stretched to fit into the size specified by ThumbWidth
Called on Playlist.DrawCell or Playlist.Click
-------------------------------------------------------------------------------}
function ExtractThumbnail(const FileName: string; ThumbWidth: integer; OUT ResolutionX, ResolutionY: Integer; OUT FrameCount: Cardinal): TBitmap;
begin
Assert(FileExistsMsg(FileName));
FrameCount:= 1; // 1 for: jpeg, png, normal gif, bmp
{ TRY TO OPEN STATIC/ANIMATED IMAGE }
if IsAnimated(FileName)
then
begin
if IsGIF(FileName)
then Result:= cGraphAviFrame.GetVideoPlayerLogo
else Result:= cGraphAviFrame.GetVideoPlayerLogo;
FrameCount:= 2; // 2 for avi/gif
end
else
if IsJpg(FileName)
then
begin
Result:= ExtractThumbnailJpg(FileName, ThumbWidth, -1, ResolutionX, ResolutionY); // This will do "RotateEXIF"
EXIT; // Jpegs are special because a thumbnail was already generated for them by ExtractThumbnailJpg. So, we do no furthure processing
end
else
Result:= LoadGraph(FileName, TRUE);
{ GET RESOLUTION }
if (Result<> NIL)
and (Result.Height > 0) { Tratez cazul Ric Koval - totusi nu ar trebuie sa mi sa intample asta niciodata }
and (Result.Width > 0)
then
begin
// Get resolution before we resize !!!
ResolutionX:= Result.Width;
ResolutionY:= Result.Height;
// Resize
StretchProport(Result, ThumbWidth)
end
else
begin
ResolutionX:= -1;
ResolutionY:= -1;
FrameCount:= 0; { 0 = extraction error in ConvertGIF. 1= static gif/jpg. >1= animated gif }
end;
end;
{ Save as above but it does not return the original image resolution }
function ExtractThumbnail(CONST FileName: string; ThumbWidth: Integer): TBitmap;
VAR ResolutionX, ResolutionY: Integer;
FrameCount: Cardinal;
begin
Result:= ExtractThumbnail(FileName, ThumbWidth, ResolutionX, ResolutionY, FrameCount);
end;
{---------------------------------------------------------------------------------------------------
LOAD JPG
----------------------------------------------------------------------------------------------------
Speed:
This function is too slow. Use WIC instead. A 1.44MB jpeg file takes 4.2 seconds to load.
Thumbnail:
The function cal load the JPEG at a reduced scale with 'Scale'
Exif:
The image is automatically rotated if RotateExif is true.
The Exif data is output to ExifData. You MUST free the object after using the function!
---------------------------------------------------------------------------------------------------}
function LoadJpg(CONST FileName: string; Scale: TJPEGScale = jsFullSize): TBitmap;
VAR
JPG: TJpegImage;
begin
Assert(FileExistsMsg(FileName));
JPG:= TJpegImage.Create;
TRY
TRY
JPG.Scale:= Scale; // jsFullSize, jsHalf, jsQuarter, jsEighth
JPG.LoadFromFile(FileName);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(JPG); { We only free the result in case of failure }
EXIT(NIL);
end;
END;
// We don't want small or huge images
if (JPG.Width <= 0) or (JPG.Width > 32768)
or (JPG.Height<= 0) or (JPG.Height > 32768)
then Exit(NIL);
Result:= TBitmap.Create;
TRY
Result.PixelFormat:= pf24bit; //? necessary or is default 24?
Result.HandleType := bmDIB;
Result.Assign(JPG); // This takes 4 sec
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result); { We only free the result in case of failure }
end;
end;
FINALLY
FreeAndNil(JPG);
end;
end;
{$IFDEF Jpg2000}
function LoadJ2K(CONST FileName: string): TBitmap;
VAR JP2: TJpeg2000Bitmap;
begin
Assert(FileExistsMsg(FileName));
JP2:= TJpeg2000Bitmap.Create;
TRY
TRY
// JP2.Scale:= Scale; // jsFullSize, jsHalf, jsQuarter, jsEighth
JP2.LoadFromFile(FileName);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(JP2); { We only free the result in case of failure }
EXIT(NIL);
end;
end;
if (JP2.Width <= 0) or (JP2.Width > 32768)
or (JP2.Height<= 0) or (JP2.Height > 32768)
then Exit(NIL);
Result:= TBitmap.Create;
TRY
Result.Assign(JP2)
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result); { We only free the result in case of failure }
end;
end;
FINALLY
FreeAndNil(JP2);
end;
end;
{$ENDIF}
{--------------------------------------------------------------------------------------------------
LOAD GIF
--------------------------------------------------------------------------------------------------}
function LoadGIF(CONST FileName: string): TBitmap; { Load GIF and convert it to BMP }
var Dummy: Cardinal;
begin
Result:= LoadGIF(FileName, Dummy);
end;
{ Load GIF and convert it to BMP.
It also returns the number of frames }
function LoadGIF(CONST FileName: string; OUT FrameCount: Cardinal): TBitmap;
VAR GIF: TGIFImage;
begin
Assert(FileExistsMsg(FileName));
Result:= NIL;
FrameCount:= 0; { 0 means error. Do not change this value. bxWallpaper relies on it to detect if the frame count was extracted (FrameCount>0) or not (FrameCount= 0) }
GIF:= TGIFImage.Create;
TRY
TRY
GIF.LoadFromFile(FileName); { This takes ~12sec for a 50MB GIF }
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(GIF); { We only free the result in case of failure }
EXIT(NIL);
end;
END;
// We don't want small or huge images
if (gif.Width <= 0) or (gif.Width > 32768)
or (gif.Height<= 0) or (gif.Height > 32768)
then Exit(NIL);
Result:= TBitmap.Create;
TRY
Result.Assign(GIF);
FrameCount:= Gif.Images.Count; { This returns 1 for static images }
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result); { We only free the result in case of failure }
end;
end;
FINALLY
FreeAndNil(GIF);
end;
end;
{--------------------------------------------------------------------------------------------------
LOAD PNG
--------------------------------------------------------------------------------------------------}
TYPE
TPNGObject= class(TPNGImage);
function LoadPNG(CONST FileName: string): TBitmap;
VAR PNG: TPNGObject;
begin
Assert(FileExistsMsg(FileName));
PNG:= TPNGObject.Create;
TRY
TRY
PNG.LoadFromFile(FileName);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(PNG); { We only free the result in case of failure }
EXIT(NIL);
end;
end;
// We don't want small or huge images
if (PNG.Width <= 0) or (PNG.Width > 32768)
or (PNG.Height<= 0) or (PNG.Height > 32768)
then Exit(NIL);
Result:= TBitmap.Create;
TRY
Result.Assign(PNG)
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result); { We only free the result in case of failure }
end;
end;
FINALLY
FreeAndNil(PNG);
end;
end;
(*procedure LoadPNG(CONST FileName: string; BMP: TBitmap); { The BMP object must exist }
VAR PNG: TPNGObject;
begin
if not fileexists(FileName) then raise__;
PNG:= TPNGObject.Create;
TRY
TRY
TRY
PNG.LoadFromFile(FileName);
except_
EXIT(NIL); { Don't crash on invalid images }
end;
{ if (PNG.Width < 1) OR (PNG.Width >= 32768)
OR (PNG.Height< 1) OR (PNG.Height >= 32768)
then EXIT(NIL); }
BMP.Assign(PNG)
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result_); { We only free the result in case of failure }
end;
END;
FINALLY
FreeAndNil(PNG);
end;
end; *)
{-------------------------------------------------------------------------------------------------------------
LOAD OTHERS
-------------------------------------------------------------------------------------------------------------}
{ Loads a BMP and supress error messages }
function LoadBMP(CONST FileName: string): TBitmap;
begin
Assert(FileExistsMsg(FileName));
Result:= TBitmap.Create;
TRY
Result.LoadFromFile(FileName);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result); { We only free the result in case of failure }
EXIT(NIL);
end;
end;
if (Result.Width< 1)
OR (Result.Height< 1)
then FreeAndNil(Result);
end;
function LoadEMF(CONST FileName: string): TBitmap; { Converts a Enhanced Metafile (*BMP }
VAR Metafile: TMetafile;
MetaCanvas: TMetafileCanvas;
begin
Assert(FileExistsMsg(FileName));
Result:= NIL;
Metafile := TMetaFile.Create;
TRY
TRY
Metafile.LoadFromFile(FileName);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(MetaFile); { We only free the result in case of failure }
EXIT(NIL);
end;
end;
MetaCanvas:= TMetafileCanvas.Create(Metafile, 0);
TRY
Result:= TBitmap.Create;
TRY
Result.Height:= Metafile.Height;
Result.Width := Metafile.Width;
Result.Canvas.Draw(0, 0, Metafile);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result); { We only free the result in case of failure }
end;
end;
FINALLY
FreeAndNil(MetaCanvas); { We only free the result in case of failure }
end;
FINALLY
FreeAndNil(Metafile);
end;
end;
function LoadWB1(CONST FileName: string): TBitmap;
VAR WB1: cGraphLoader.WB1.TWb1Obj;
begin
Assert(FileExistsMsg(FileName));
WB1:= TWb1Obj.Create;
TRY
TRY
WB1.LoadFromFile(FileName);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(WB1); { We only free the result in case of failure }
EXIT(NIL);
end;
end;
// We don't want small or huge images
if (WB1.InternalJPG.Width <= 0) or (WB1.InternalJPG.Width > 32768)
or (WB1.InternalJPG.Height<= 0) or (WB1.InternalJPG.Height > 32768)
then Exit(NIL);
Result:= TBitmap.Create;
TRY
Result.Assign(WB1.InternalJPG)
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result); { We only free the result in case of failure }
end;
end;
FINALLY
FreeAndNil(WB1);
end;
end;
function LoadICO(CONST FileName: string): TBitmap;
VAR ICO1: TIcon;
begin
Assert(FileExistsMsg(FileName));
Result:= NIL;
ICO1:= TIcon.Create;
TRY
TRY
ICO1.LoadFromFile(FileName);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(ICO1); { We only free the result in case of failure }
EXIT(NIL);
end;
end;
Result:= TBitmap.Create;
TRY
Result.Assign(ICO1);
EXCEPT
on E: Exception do { Don't crash on invalid images. Common encountered errors: EInvalidGraphic (JPEG error #53), EReadError (Stream read error), etc } //todo: trap only specific exceptions
begin
AppData.LogError(E.ClassName+': '+ E.Message + ' - '+ FileName);
FreeAndNil(Result); { We only free the result in case of failure }
end;
end;