-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCR.Exif.IPTC.pas
1788 lines (1627 loc) · 59.7 KB
/
CCR.Exif.IPTC.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
{**************************************************************************************}
{ }
{ CCR Exif - Delphi class library for reading and writing image metadata }
{ Version 1.5.3 }
{ }
{ The contents of this file are subject to the Mozilla Public License Version 1.1 }
{ (the "License"); you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT }
{ WARRANTY OF ANY KIND, either express or implied. See the License for the specific }
{ language governing rights and limitations under the License. }
{ }
{ The Original Code is CCR.Exif.IPTC.pas. }
{ }
{ The Initial Developer of the Original Code is Chris Rolliston. Portions created by }
{ Chris Rolliston are Copyright (C) 2009-2014 Chris Rolliston. All Rights Reserved. }
{ }
{**************************************************************************************}
{$I CCR.Exif.inc}
unit CCR.Exif.IPTC;
{
As saved, IPTC data is a flat list of tags ('datasets'), no more no less, which is
reflected in the implementation of TIPTCData.LoadFromStream. However, as found in JPEG
files, they are put in an Adobe data structure, itself put inside an APP13 segment.
Note that by default, string tags are 'only' interpreted as having UTF-8 data if the
encoding tag is set, with the UTF-8 marker as its data. If you don't load any tags
before adding others, however, the default is to persist to UTF-8, writing said marker
tag of course. To force interpreting loaded tags as UTF-8, set the
AlwaysAssumeUTF8Encoding property of TIPTCData to True *before* calling
LoadFromGraphic or LoadFromStream.
}
interface
uses
Types, SysUtils, Classes, {$IFDEF HasGenerics}Generics.Collections, Generics.Defaults,{$ENDIF}
{$IFDEF VCL}Jpeg,{$ENDIF} CCR.Exif.BaseUtils, CCR.Exif.TagIDs, CCR.Exif.TiffUtils;
type
EInvalidIPTCData = class(ECCRExifException);
{$IFDEF HasGenerics}
TIPTCRepeatablePair = TPair<string, string>;
TIPTCRepeatablePairs = TArray<TIPTCRepeatablePair>;
{$ELSE}
TIPTCRepeatablePair = record
Key, Value: string;
end;
TIPTCRepeatablePairs = array of TIPTCRepeatablePair;
{$ENDIF}
TIPTCStringArray = type Types.TStringDynArray; //using 'type' means the helper defined below will only apply to it
{$IFDEF XE3+}
TIPTCStringArrayHelper = record helper for TIPTCStringArray
class function CreateFromStrings(const Strings: TStrings): TIPTCStringArray; static;
function Join(const Separator: string): string;
end;
{$ENDIF}
TIPTCData = class;
TIPTCSection = class;
TIPTCTagID = CCR.Exif.BaseUtils.TIPTCTagID;
TIPTCTagIDs = set of TIPTCTagID;
TIPTCTag = class //an instance represents a 'dataset' in IPTC jargon; instances need not be written in numerical order
public type
TChangeType = (ctID, ctData);
private
FData: Pointer;
FDataSize: Integer;
FID: TIPTCTagID;
FSection: TIPTCSection;
procedure SetDataSize(Value: Integer);
procedure SetID(const Value: TIPTCTagID);
function GetAsString: string;
procedure SetAsString(const Value: string);
function GetIndex: Integer;
procedure SetIndex(NewIndex: Integer);
public
destructor Destroy; override;
procedure Assign(Source: TIPTCTag);
procedure Changed(AType: TChangeType = ctData); overload; //call this if Data is modified directly
procedure Delete;
procedure UpdateData(const Buffer); overload; inline;
procedure UpdateData(NewDataSize: Integer; const Buffer); overload;
procedure UpdateData(NewDataSize: Integer; Source: TStream); overload;
{ ReadString treats the data as string data, whatever the spec says. It respects
TIPTCData.UTF8Encoded however. }
function ReadString: string;
{ ReadUTF8String just assumes the tag data is UTF-8 text. }
function ReadUTF8String: UTF8String; inline;
procedure WriteString(const NewValue: RawByteString); overload;
procedure WriteString(const NewValue: UnicodeString); overload;
{ AsString assumes the underlying data type is as per the spec (unlike the case of
Exif tag headers, IPTC ones do not specify their data type). }
property AsString: string read GetAsString write SetAsString;
property Data: Pointer read FData;
property DataSize: Integer read FDataSize write SetDataSize;
property ID: TIPTCTagID read FID write SetID; //tag IDs need only be unique within sections 1, 7, 8 and 9
property Index: Integer read GetIndex write SetIndex;
property Section: TIPTCSection read FSection;
end;
TIPTCSectionID = CCR.Exif.BaseUtils.TIPTCSectionID;
{$Z1} //only TIPTCImageOrientation directly maps to the stored value
TIPTCActionAdvised = (iaTagMissing, iaObjectKill, iaObjectReplace, iaObjectAppend, iaObjectReference);
TIPTCImageOrientation = (ioTagMissing, ioLandscape = Ord('L'), ioPortrait = Ord('P'),
ioSquare = Ord('S'));
TIPTCPriority = (ipTagMissing, ipLowest, ipVeryLow, ipLow, ipNormal, ipNormalHigh,
ipHigh, ipVeryHigh, ipHighest, ipUserDefined, ipReserved);
TIPTCSection = class //an instance represents a 'record' in IPTC jargon
public type
TEnumerator = record
strict private
FIndex: Integer;
FSource: TIPTCSection;
function GetCurrent: TIPTCTag;
public
constructor Create(ASection: TIPTCSection);
function MoveNext: Boolean;
property Current: TIPTCTag read GetCurrent;
end;
private type
{$IFDEF HasGenerics}
TTagList = class(TObjectList<TIPTCTag>)
constructor Create;
end;
{$ELSE}
TTagList = class(TList)
protected
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
public
procedure Sort;
end;
{$ENDIF}
private
FDefinitelySorted: Boolean;
FID: TIPTCSectionID;
FModified: Boolean;
FOwner: TIPTCData;
FTags: TTagList;
function GetTag(Index: Integer): TIPTCTag;
function GetTagCount: Integer;
public
constructor Create(AOwner: TIPTCData; AID: TIPTCSectionID);
destructor Destroy; override;
function GetEnumerator: TEnumerator;
function Add(ID: TIPTCTagID): TIPTCTag; //will try to insert in an appropriate place
function Append(ID: TIPTCTagID): TIPTCTag; //will literally just append
procedure Clear;
procedure Delete(Index: Integer);
function Find(ID: TIPTCTagID; out Tag: TIPTCTag): Boolean;
function Insert(Index: Integer; ID: TIPTCTagID = 0): TIPTCTag;
procedure Move(CurIndex, NewIndex: Integer);
function Remove(TagID: TIPTCTagID): Integer; overload; inline;
function Remove(TagIDs: TIPTCTagIDs): Integer; overload;
procedure Sort;
function TagExists(ID: TIPTCTagID; MinSize: Integer = 1): Boolean;
function AddOrUpdate(TagID: TIPTCTagID; NewDataSize: LongInt; const Buffer): TIPTCTag;
function GetDateValue(TagID: TIPTCTagID): TDateTimeTagValue;
procedure SetDateValue(TagID: TIPTCTagID; const Value: TDateTimeTagValue);
function GetDateTimeValue(DateTagID, TimeTagID: TIPTCTagID; AsUtc: Boolean = False): TDateTimeTagValue;
procedure SetDateTimeValue(DateTagID, TimeTagID: TIPTCTagID;
const Value: TDateTimeTagValue; AheadOfUtc: Boolean; UtcOffsetHrs, UtcOffsetMins: Byte); overload;
{$IFDEF HasTTimeZone}
procedure SetDateTimeValue(DateTagID, TimeTagID: TIPTCTagID; const Value: TDateTimeTagValue); overload;
{$ENDIF}
function GetPriorityValue(TagID: TIPTCTagID): TIPTCPriority;
procedure SetPriorityValue(TagID: TIPTCTagID; Value: TIPTCPriority);
function GetRepeatableValue(TagID: TIPTCTagID): TIPTCStringArray; overload;
procedure GetRepeatableValue(TagID: TIPTCTagID; Dest: TStrings; ClearDestFirst: Boolean = True); overload;
procedure SetRepeatableValue(TagID: TIPTCTagID; const Value: array of string); overload;
procedure SetRepeatableValue(TagID: TIPTCTagID; Value: TStrings); overload;
procedure GetRepeatablePairs(KeyID, ValueID: TIPTCTagID; Dest: TStrings; ClearDestFirst: Boolean = True); overload;
procedure SetRepeatablePairs(KeyID, ValueID: TIPTCTagID; Pairs: TStrings); overload;
function GetRepeatablePairs(KeyID, ValueID: TIPTCTagID): TIPTCRepeatablePairs; overload;
procedure SetRepeatablePairs(KeyID, ValueID: TIPTCTagID; const Pairs: TIPTCRepeatablePairs); overload;
function GetStringValue(TagID: TIPTCTagID): string;
procedure SetStringValue(TagID: TIPTCTagID; const Value: string);
function GetWordValue(TagID: TIPTCTagID): TWordTagValue;
procedure SetWordValue(TagID: TIPTCTagID; const Value: TWordTagValue);
property Count: Integer read GetTagCount;
property ID: TIPTCSectionID read FID;
property Modified: Boolean read FModified write FModified;
property Owner: TIPTCData read FOwner;
property Tags[Index: Integer]: TIPTCTag read GetTag; default;
end;
TIPTCData = class(TComponent, IStreamPersist, IStreamPersistEx, ITiffRewriteCallback)
public type
TEnumerator = record
private
FCurrentID: TIPTCSectionID;
FDoneFirst: Boolean;
FSource: TIPTCData;
function GetCurrent: TIPTCSection;
public
constructor Create(ASource: TIPTCData);
function MoveNext: Boolean;
property Current: TIPTCSection read GetCurrent;
end;
strict private
FAlwaysAssumeUTF8Encoding: Boolean;
FDataToLazyLoad: IMetadataBlock;
FLoadErrors: TMetadataLoadErrors;
FSections: array[TIPTCSectionID] of TIPTCSection;
FTiffRewriteCallback: TSimpleTiffRewriteCallbackImpl;
FUTF8Encoded: Boolean;
procedure GetGraphicSaveMethod(Stream: TStream; var Method: TGraphicSaveMethod);
function GetModified: Boolean;
function GetSection(ID: Integer): TIPTCSection;
function GetSectionByID(ID: TIPTCSectionID): TIPTCSection;
function GetUTF8Encoded: Boolean;
procedure SetDataToLazyLoad(const Value: IMetadataBlock);
procedure SetModified(Value: Boolean);
procedure SetUTF8Encoded(Value: Boolean);
function GetEnvelopeString(TagID: Integer): string;
procedure SetEnvelopeString(TagID: Integer; const Value: string);
function GetEnvelopeWord(TagID: Integer): TWordTagValue;
procedure SetEnvelopeWord(TagID: Integer; const Value: TWordTagValue);
function GetApplicationWord(TagID: Integer): TWordTagValue;
procedure SetApplicationWord(TagID: Integer; const Value: TWordTagValue);
function GetApplicationString(TagID: Integer): string;
procedure SetApplicationString(TagID: Integer; const Value: string);
function GetApplicationStrings(TagID: Integer): TIPTCStringArray;
procedure SetApplicationStrings(TagID: Integer; const Value: TIPTCStringArray);
function GetApplicationPairs(PackedTagIDs: Integer): TIPTCRepeatablePairs;
procedure SetApplicationPairs(PackedTagIDs: Integer; const NewPairs: TIPTCRepeatablePairs);
function GetUrgency: TIPTCPriority;
procedure SetUrgency(Value: TIPTCPriority);
function GetEnvelopePriority: TIPTCPriority;
procedure SetEnvelopePriority(const Value: TIPTCPriority);
function GetDate(PackedIndex: Integer): TDateTimeTagValue;
procedure SetDate(PackedIndex: Integer; const Value: TDateTimeTagValue);
function GetActionAdvised: TIPTCActionAdvised;
procedure SetActionAdvised(Value: TIPTCActionAdvised);
function GetDateTimeCreated: TDateTimeTagValue;
function GetDateTimeSent: TDateTimeTagValue;
{$IFDEF HasTTimeZone}
procedure SetDateTimeCreatedProp(const Value: TDateTimeTagValue);
procedure SetDateTimeSentProp(const Value: TDateTimeTagValue);
{$ENDIF}
function GetImageOrientation: TIPTCImageOrientation;
procedure SetImageOrientation(Value: TIPTCImageOrientation);
protected
function CreateAdobeBlock: IAdobeResBlock; inline;
procedure DefineProperties(Filer: TFiler); override;
procedure DoSaveToJPEG(InStream, OutStream: TStream);
procedure DoSaveToPSD(InStream, OutStream: TStream);
procedure DoSaveToTIFF(InStream, OutStream: TStream);
function GetEmpty: Boolean;
procedure NeedLazyLoadedData;
property TiffRewriteCallback: TSimpleTiffRewriteCallbackImpl read FTiffRewriteCallback implements ITiffRewriteCallback;
public const
UTF8Marker: array[1..3] of Byte = ($1B, $25, $47);
public
constructor Create(AOwner: TComponent = nil); override;
class function CreateAsSubComponent(AOwner: TComponent): TIPTCData; //use a class function rather than a constructor to avoid compiler warning re C++ accessibility
destructor Destroy; override;
function GetEnumerator: TEnumerator;
procedure AddFromStream(Stream: TStream);
procedure Assign(Source: TPersistent); override;
procedure Clear;
procedure SetDateTimeCreated(const Value: TDateTimeTagValue; AheadOfUtc: Boolean;
UtcOffsetHrs, UtcOffsetMins: Byte); overload;
procedure SetDateTimeSent(const Value: TDateTimeTagValue; AheadOfUtc: Boolean;
UtcOffsetHrs, UtcOffsetMins: Byte); overload;
{ Whether or not metadata was found, LoadFromGraphic returns True if the graphic format
was recognised as one that *could* contain relevant metadata and False otherwise. }
function LoadFromGraphic(Stream: TStream): Boolean; overload;
function LoadFromGraphic(const Graphic: IStreamPersist): Boolean; overload;
function LoadFromGraphic(const FileName: string): Boolean; overload;
procedure LoadFromStream(Stream: TStream);
{ SaveToGraphic raises an exception if the target graphic either doesn't exist, or
is neither a JPEG nor a PSD image. }
procedure SaveToGraphic(const FileName: string); overload;
procedure SaveToGraphic(const Graphic: IStreamPersist); overload;
procedure SaveToStream(Stream: TStream);
procedure SortTags;
property LoadErrors: TMetadataLoadErrors read FLoadErrors write FLoadErrors; //!!!v. rarely set at present
property Modified: Boolean read GetModified write SetModified;
property EnvelopeSection: TIPTCSection index 1 read GetSection;
property ApplicationSection: TIPTCSection index 2 read GetSection;
property DataToLazyLoad: IMetadataBlock read FDataToLazyLoad write SetDataToLazyLoad;
property FirstDescriptorSection: TIPTCSection index 7 read GetSection;
property ObjectDataSection: TIPTCSection index 8 read GetSection;
property SecondDescriptorSection: TIPTCSection index 9 read GetSection;
property Sections[ID: TIPTCSectionID]: TIPTCSection read GetSectionByID; default;
published
property AlwaysAssumeUTF8Encoding: Boolean read FAlwaysAssumeUTF8Encoding write FAlwaysAssumeUTF8Encoding default False;
property Empty: Boolean read GetEmpty;
property UTF8Encoded: Boolean read GetUTF8Encoded write SetUTF8Encoded default True;
{ record 1 }
property ModelVersion: TWordTagValue index itModelVersion read GetEnvelopeWord write SetEnvelopeWord stored False;
property Destination: string index itDestination read GetEnvelopeString write SetEnvelopeString stored False;
property FileFormat: TWordTagValue index itFileFormat read GetEnvelopeWord write SetEnvelopeWord stored False; //!!!make an enum
property FileFormatVersion: TWordTagValue index itFileFormatVersion read GetEnvelopeWord write SetEnvelopeWord stored False; //!!!make an enum
property ServiceIdentifier: string index itServiceIdentifier read GetEnvelopeString write SetEnvelopeString stored False;
property EnvelopeNumberString: string index itEnvelopeNumber read GetEnvelopeString write SetEnvelopeString stored False;
property ProductID: string index itProductID read GetEnvelopeString write SetEnvelopeString stored False;
property EnvelopePriority: TIPTCPriority read GetEnvelopePriority write SetEnvelopePriority stored False;
property DateSent: TDateTimeTagValue index isEnvelope or itDateSent shl 8 read GetDate write SetDate;
property TimeSent: string index itTimeSent read GetEnvelopeString write SetEnvelopeString stored False;
property DateTimeSent: TDateTimeTagValue read GetDateTimeSent {$IFDEF HasTTimeZone}write SetDateTimeSentProp{$ENDIF} stored False;
property UNOCode: string index itUNO read GetEnvelopeString write SetEnvelopeString stored False; //should have a specific format
property ARMIdentifier: TWordTagValue index itARMIdentifier read GetEnvelopeWord write SetEnvelopeWord stored False; //!!!make an enum
property ARMVersion: TWordTagValue index itARMVersion read GetEnvelopeWord write SetEnvelopeWord stored False; //!!!make an enum
{ record 2 }
property RecordVersion: TWordTagValue index itRecordVersion read GetApplicationWord write SetApplicationWord stored False;
property ObjectTypeRef: string index itObjectTypeRef read GetApplicationString write SetApplicationString stored False;
property ObjectAttributeRef: string index itObjectAttributeRef read GetApplicationString write SetApplicationString stored False;
property ObjectName: string index itObjectName read GetApplicationString write SetApplicationString stored False;
property EditStatus: string index itEditStatus read GetApplicationString write SetApplicationString stored False;
property Urgency: TIPTCPriority read GetUrgency write SetUrgency stored False;
property SubjectRefs: TIPTCStringArray index itSubjectRef read GetApplicationStrings write SetApplicationStrings stored False;
property CategoryCode: string index itCategory read GetApplicationString write SetApplicationString stored False; //should be a 3 character code
property SupplementaryCategories: TIPTCStringArray index itSupplementaryCategory read GetApplicationStrings write SetApplicationStrings stored False;
property FixtureIdentifier: string index itFixtureIdentifier read GetApplicationString write SetApplicationString stored False;
property Keywords: TIPTCStringArray index itKeyword read GetApplicationStrings write SetApplicationStrings stored False;
procedure GetKeywords(Dest: TStrings); overload;
procedure SetKeywords(NewWords: TStrings); overload;
property ContentLocationCodes: TIPTCStringArray index itContentLocationCode read GetApplicationStrings write SetApplicationStrings stored False;
property ContentLocationNames: TIPTCStringArray index itContentLocationName read GetApplicationStrings write SetApplicationStrings stored False;
procedure GetContentLocationValues(Strings: TStrings; ClearDestFirst: Boolean = True); //Code=Name
procedure SetContentLocationValues(Strings: TStrings);
property ContentLocations: TIPTCRepeatablePairs index itContentLocationCode or itContentLocationName shl 8 read GetApplicationPairs write SetApplicationPairs;
property ReleaseDate: TDateTimeTagValue index isApplication or itReleaseDate shl 8 read GetDate write SetDate stored False;
property ExpirationDate: TDateTimeTagValue index isApplication or itExpirationDate shl 8 read GetDate write SetDate stored False;
property SpecialInstructions: string index itSpecialInstructions read GetApplicationString write SetApplicationString stored False;
property ActionAdvised: TIPTCActionAdvised read GetActionAdvised write SetActionAdvised stored False;
property DateCreated: TDateTimeTagValue index isApplication or itDateCreated shl 8 read GetDate write SetDate stored False;
property DateTimeCreated: TDateTimeTagValue read GetDateTimeCreated {$IFDEF HasTTimeZone}write SetDateTimeCreatedProp{$ENDIF} stored False;
property DigitalCreationDate: TDateTimeTagValue index isApplication or itDigitalCreationDate shl 8 read GetDate write SetDate stored False;
property OriginatingProgram: string index itOriginatingProgram read GetApplicationString write SetApplicationString stored False;
property ProgramVersion: string index itProgramVersion read GetApplicationString write SetApplicationString stored False;
property ObjectCycleCode: string index itObjectCycle read GetApplicationString write SetApplicationString stored False; //!!!enum
property Bylines: TIPTCStringArray index itByline read GetApplicationStrings write SetApplicationStrings stored False;
property BylineTitles: TIPTCStringArray index itBylineTitle read GetApplicationStrings write SetApplicationStrings stored False;
procedure GetBylineValues(Strings: TStrings); inline; deprecated {$IFDEF DepCom}'Use GetBylineDetails instead'{$ENDIF};
procedure SetBylineValues(Strings: TStrings); inline; deprecated {$IFDEF DepCom}'Use SetBylineDetails instead'{$ENDIF};
procedure GetBylineDetails(Strings: TStrings; ClearDestFirst: Boolean = True); //Name=Title
procedure SetBylineDetails(Strings: TStrings);
property BylineDetails: TIPTCRepeatablePairs index itByline or itBylineTitle shl 8 read GetApplicationPairs write SetApplicationPairs;
property City: string index itCity read GetApplicationString write SetApplicationString stored False; //!!!enum
property SubLocation: string index itSubLocation read GetApplicationString write SetApplicationString stored False; //!!!enum
property ProvinceOrState: string index itProvinceOrState read GetApplicationString write SetApplicationString stored False; //!!!enum
property CountryCode: string index itCountryCode read GetApplicationString write SetApplicationString stored False; //!!!enum
property CountryName: string index itCountryName read GetApplicationString write SetApplicationString stored False; //!!!enum
property OriginalTransmissionRef: string index itOriginalTransmissionRef read GetApplicationString write SetApplicationString stored False; //!!!enum
property Headline: string index itHeadline read GetApplicationString write SetApplicationString stored False;
property Credit: string index itCredit read GetApplicationString write SetApplicationString stored False;
property Source: string index itSource read GetApplicationString write SetApplicationString stored False;
property CopyrightNotice: string index itCopyrightNotice read GetApplicationString write SetApplicationString stored False;
property Contacts: TIPTCStringArray index itContact read GetApplicationStrings write SetApplicationStrings stored False;
property CaptionOrAbstract: string index itCaptionOrAbstract read GetApplicationString write SetApplicationString stored False;
property WritersOrEditors: TIPTCStringArray index itWriterOrEditor read GetApplicationStrings write SetApplicationStrings stored False;
property ImageTypeCode: string index itImageType read GetApplicationString write SetApplicationString stored False;
property ImageOrientation: TIPTCImageOrientation read GetImageOrientation write SetImageOrientation stored False;
property LanguageIdentifier: string index itLanguageIdentifier read GetApplicationString write SetApplicationString stored False;
property AudioTypeCode: string index itAudioType read GetApplicationString write SetApplicationString stored False;
{ Photoshop aliases }
procedure GetPSCreatorValues(Strings: TStrings); inline;//Name=Title
procedure SetPSCreatorValues(Strings: TStrings); inline;
property PSDocumentTitle: string index itObjectName read GetApplicationString write SetApplicationString stored False;
property PSCopyrightNotice: string index itCopyrightNotice read GetApplicationString write SetApplicationString stored False;
property PSDescription: string index itCaptionOrAbstract read GetApplicationString write SetApplicationString stored False;
property PSCreator: string index itByline read GetApplicationString write SetApplicationString stored False;
property PSCreatorJobTitle: string index itBylineTitle read GetApplicationString write SetApplicationString stored False;
end;
implementation
uses
DateUtils, Math, {$IFDEF HasTTimeZone}TimeSpan,{$ENDIF}
CCR.Exif.Consts, CCR.Exif.StreamHelper;
const
PriorityChars: array[TIPTCPriority] of AnsiChar = (#0, '8', '7', '6', '5', '4',
'3', '2', '1', '9', '0');
type
TIPTCTagDataType = (idString, idWord, idBinary);
function FindProperDataType(Tag: TIPTCTag): TIPTCTagDataType;
begin
Result := idString;
if Tag.Section <> nil then
case Tag.Section.ID of
isEnvelope:
case Tag.ID of
itModelVersion, itFileFormat, itFileFormatVersion, itARMIdentifier,
itARMVersion: Result := idWord;
end;
isApplication:
case Tag.ID of
itRecordVersion: Result := idWord;
end;
end;
end;
{ TIPTCStringArrayHelper }
{$IF DECLARED(TIPTCStringArrayHelper)}
class function TIPTCStringArrayHelper.CreateFromStrings(const Strings: TStrings): TIPTCStringArray;
var
I: Integer;
begin
SetLength(Result, Strings.Count);
for I := Strings.Count - 1 downto 0 do
Result[I] := Strings[I];
end;
function TIPTCStringArrayHelper.Join(const Separator: string): string;
var
TotalLen: Integer;
S: string;
SeekPtr: PChar;
begin
TotalLen := High(Self) * Length(Separator);
for S in Self do
Inc(TotalLen, Length(S));
SetLength(Result, TotalLen);
SeekPtr := Pointer(Result);
for S in Self do
begin
MoveChars(Pointer(S)^, SeekPtr^, Length(S));
Inc(SeekPtr, Length(S) + Length(Separator));
end;
end;
{$IFEND}
{ TIPTCTag }
destructor TIPTCTag.Destroy;
begin
if FSection <> nil then FSection.FTags.Extract(Self);
ReallocMem(FData, 0);
inherited;
end;
procedure TIPTCTag.Assign(Source: TIPTCTag);
begin
if Source = nil then
SetDataSize(0)
else
begin
FID := Source.ID;
UpdateData(Source.DataSize, Source.Data^);
end;
end;
procedure TIPTCTag.Changed(AType: TChangeType);
begin
if FSection = nil then Exit;
FSection.Modified := True;
if AType = ctID then FSection.FDefinitelySorted := False;
end;
procedure TIPTCTag.Delete;
begin
{$IFDEF NEXTGEN}
DisposeOf;
{$ELSE}
Free;
{$ENDIF}
end;
function TIPTCTag.GetAsString: string;
begin
case FindProperDataType(Self) of
idString: Result := ReadString;
idBinary: Result := BinToHexStr(FData, FDataSize);
else
case DataSize of
1: Result := IntToStr(PByte(Data)^);
2: Result := IntToStr(Swap(PWord(Data)^));
4: Result := IntToStr(SwapLongInt(PLongInt(Data)^));
else Result := ReadString;
end;
end;
end;
function TIPTCTag.GetIndex: Integer;
begin
if FSection = nil then
Result := -1
else
Result := FSection.FTags.IndexOf(Self);
end;
function TIPTCTag.ReadString: string;
var
Ansi: AnsiString;
begin
if (Section <> nil) and (Section.Owner <> nil) and Section.Owner.UTF8Encoded then
begin
Result := UTF8ToString(FData, FDataSize);
Exit;
end;
SetString(Ansi, PAnsiChar(FData), FDataSize);
Result := string(Ansi);
end;
function TIPTCTag.ReadUTF8String: UTF8String;
begin
SetString(Result, PAnsiChar(FData), FDataSize);
end;
procedure TIPTCTag.SetAsString(const Value: string);
var
Bytes: TBytes;
WordVal: Integer;
begin
case FindProperDataType(Self) of
idString: WriteString(Value);
idBinary:
begin
Bytes := HexStrToBin(Value);
SetDataSize(Length(Bytes));
if FDataSize > 0 then
Move(Bytes[0], FData^, FDataSize);
end
else
{$RANGECHECKS ON}
WordVal := StrToInt(Value);
{$IFDEF RangeCheckingOff}{$RANGECHECKS OFF}{$ENDIF}
WordVal := Swap(WordVal);
UpdateData(2, WordVal);
end;
end;
procedure TIPTCTag.SetDataSize(Value: Integer);
begin
if Value = FDataSize then Exit;
ReallocMem(FData, Value);
FDataSize := Value;
Changed;
end;
procedure TIPTCTag.SetID(const Value: TIPTCTagID);
begin
if Value = FID then Exit;
FID := Value;
Changed(ctID);
end;
procedure TIPTCTag.SetIndex(NewIndex: Integer);
begin
if FSection <> nil then FSection.Move(Index, NewIndex);
end;
procedure TIPTCTag.UpdateData(const Buffer);
begin
UpdateData(DataSize, Buffer);
end;
procedure TIPTCTag.UpdateData(NewDataSize: Integer; const Buffer);
begin
ReallocMem(FData, NewDataSize);
FDataSize := NewDataSize;
Move(Buffer, FData^, NewDataSize);
Changed;
end;
procedure TIPTCTag.UpdateData(NewDataSize: Integer; Source: TStream);
begin
ReallocMem(FData, NewDataSize);
FDataSize := NewDataSize;
Source.Read(FData^, NewDataSize);
Changed;
end;
procedure TIPTCTag.WriteString(const NewValue: RawByteString);
begin
ReallocMem(FData, 0);
FDataSize := Length(NewValue);
if FDataSize <> 0 then
begin
ReallocMem(FData, FDataSize);
Move(Pointer(NewValue)^, FData^, FDataSize);
end;
Changed;
end;
procedure TIPTCTag.WriteString(const NewValue: UnicodeString);
begin
if (Section <> nil) and (Section.Owner <> nil) and Section.Owner.UTF8Encoded then
WriteString(UTF8Encode(NewValue))
else
WriteString(AnsiString(NewValue));
end;
{ TIPTCSection.TEnumerator }
constructor TIPTCSection.TEnumerator.Create(ASection: TIPTCSection);
begin
FIndex := -1;
FSource := ASection;
end;
function TIPTCSection.TEnumerator.GetCurrent: TIPTCTag;
begin
Result := FSource[FIndex];
end;
function TIPTCSection.TEnumerator.MoveNext: Boolean;
begin
Result := (Succ(FIndex) < FSource.Count);
if Result then Inc(FIndex);
end;
{ TIPTCSection.TTagList }
{$IFDEF HasGenerics}
constructor TIPTCSection.TTagList.Create;
begin
inherited Create(TComparer<TIPTCTag>.Construct(
function(const Left, Right: TIPTCTag): Integer
begin
Result := Right.ID - Left.ID;
end));
end;
{$ELSE}
procedure TIPTCSection.TTagList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if Action = lnDeleted then
TObject(Ptr).Free;
end;
function CompareIDs(Item1, Item2: TIPTCTag): Integer;
begin
Result := Item2.ID - Item1.ID;
end;
procedure TIPTCSection.TTagList.Sort;
begin
inherited Sort(@CompareIDs);
end;
{$ENDIF}
{ TIPTCSection }
constructor TIPTCSection.Create(AOwner: TIPTCData; AID: TIPTCSectionID);
begin
inherited Create;
FOwner := AOwner;
FID := AID;
FTags := TTagList.Create;
end;
destructor TIPTCSection.Destroy;
begin
FTags.Free;
inherited;
end;
function TIPTCSection.Add(ID: TIPTCTagID): TIPTCTag;
var
I: Integer;
begin
if ID = 0 then
Result := Insert(Count)
else
begin
for I := Count - 1 downto 0 do
if ID > GetTag(I).ID then
begin
Result := Insert(I + 1, ID);
Exit;
end;
Result := Insert(0, ID);
end;
end;
function TIPTCSection.AddOrUpdate(TagID: TIPTCTagID; NewDataSize: Integer;
const Buffer): TIPTCTag;
begin
if not Find(TagID, Result) then
Result := Add(TagID);
Result.UpdateData(NewDataSize, Buffer);
end;
function TIPTCSection.Append(ID: TIPTCTagID): TIPTCTag;
begin
Result := Insert(Count, ID);
end;
function TIPTCSection.Find(ID: TIPTCTagID; out Tag: TIPTCTag): Boolean;
var
I: Integer;
begin
Result := True;
for I := 0 to FTags.Count - 1 do
begin
Tag := FTags[I];
if Tag.ID = ID then Exit;
if FDefinitelySorted and (Tag.ID > ID) then Break;
end;
Tag := nil;
Result := False;
end;
function TIPTCSection.GetDateValue(TagID: TIPTCTagID): TDateTimeTagValue;
var
Date: TDateTime;
S: string;
Year, Month, Day: Integer;
begin
S := GetStringValue(TagID);
if TryStrToInt(Copy(S, 1, 4), Year) and TryStrToInt(Copy(S, 5, 2), Month) and
TryStrToInt(Copy(S, 7, 2), Day) and TryEncodeDate(Year, Month, Day, Date) then
Result := Date
else
Result := TDateTimeTagValue.CreateMissingOrInvalid;
end;
procedure TIPTCSection.SetDateValue(TagID: TIPTCTagID; const Value: TDateTimeTagValue);
var
Year, Month, Day: Word;
begin
if Value.MissingOrInvalid then
begin
Remove(TagID);
Exit;
end;
DecodeDate(Value, Year, Month, Day);
SetStringValue(TagID, Format('%.4d%.2d%.2d', [Year, Month, Day]));
end;
function TIPTCSection.GetDateTimeValue(DateTagID, TimeTagID: TIPTCTagID; AsUtc: Boolean): TDateTimeTagValue;
var
OffsetHrs, OffsetMins: Integer;
I: Integer;
S: string;
TimePart: TDateTimeTagValue;
Temp: TDateTime;
begin
TimePart := TDateTimeTagValue.CreateMissingOrInvalid;
S := GetStringValue(TimeTagID);
if (Length(S) = 11) and IsCharIn(S[7], ['-', '+']) and TryStrToInt(Copy(S, 8, 2),
OffsetHrs) and TryStrToInt(Copy(S, 10, 2), OffsetMins) then
begin
for I in [1, 2, 3, 4, 5, 6, 8, 9, 10, 11] do
if not IsCharIn(S[I], ['0'..'9']) then
begin
S := '';
Break;
end;
if (S <> '') then
begin
if not AsUtc then
begin
OffsetHrs := 0;
OffsetMins := 0;
end
else if S[7] = '+' then //i.e., if the local time is ahead of GMT
begin
OffsetHrs := -OffsetHrs;
OffsetMins := -OffsetMins;
end;
if TryEncodeTime(StrToInt(Copy(S, 1, 2)) + OffsetHrs, StrToInt(Copy(S, 3, 2)) +
OffsetMins, StrToInt(Copy(S, 5, 2)), 0, Temp) then
TimePart := Temp;
end;
end;
Result := GetDateValue(DateTagID);
if TimePart.MissingOrInvalid then Exit;
if Result.MissingOrInvalid then
Result := TimePart
else
Result := Result.Value + TimePart.Value;
end;
procedure TIPTCSection.SetDateTimeValue(DateTagID, TimeTagID: TIPTCTagID;
const Value: TDateTimeTagValue; AheadOfUtc: Boolean; UtcOffsetHrs, UtcOffsetMins: Byte);
const
PlusOrMinus: array[Boolean] of string = ('-', '+');
var
Year, Month, Day, Hour, Minute, Second, MilliSecond: Word;
begin
if Value.MissingOrInvalid then
begin
Remove([DateTagID, TimeTagID]);
Exit;
end;
DecodeDateTime(Value, Year, Month, Day, Hour, Minute, Second, MilliSecond);
SetStringValue(DateTagID, Format('%.4d%.2d%.2d', [Year, Month, Day]));
SetStringValue(TimeTagID, Format('%.2d%.2d%.2d%s%.2d%.2d', [Hour, Minute, Second,
PlusOrMinus[AheadOfUtc], UtcOffsetHrs, UtcOffsetMins]));
end;
{$IFDEF HasTTimeZone}
procedure TIPTCSection.SetDateTimeValue(DateTagID, TimeTagID: TIPTCTagID; const Value: TDateTimeTagValue);
var
Offset: TTimeSpan;
begin
Offset := TTimeZone.Local.UtcOffset;
SetDateTimeValue(DateTagID, TimeTagID, Value, Offset.Ticks >= 0, Abs(Offset.Hours),
Abs(Offset.Minutes));
end;
{$ENDIF}
function TIPTCSection.GetEnumerator: TEnumerator;
begin
Result := TEnumerator.Create(Self);
end;
function TIPTCSection.GetStringValue(TagID: TIPTCTagID): string;
var
Tag: TIPTCTag;
begin
if Find(TagID, Tag) then
Result := Tag.ReadString
else
Result := '';
end;
function TIPTCSection.Remove(TagID: TIPTCTagID): Integer;
begin
Result := Remove([TagID]);
end;
function TIPTCSection.Remove(TagIDs: TIPTCTagIDs): Integer;
var
I: Integer;
Tag: TIPTCTag;
begin
Result := -1;
for I := Count - 1 downto 0 do
begin
Tag := Tags[I];
if Tag.ID in TagIDs then
begin
Delete(I);
Result := I;
end;
end;
end;
procedure TIPTCSection.Clear;
var
I: Integer;
begin
for I := Count - 1 downto 0 do
Delete(I);
end;
procedure TIPTCSection.Delete(Index: Integer);
var
Tag: TIPTCTag;
begin
Tag := FTags[Index];
Tag.FSection := nil;
FTags.Delete(Index);
FModified := True;
if FTags.Count <= 1 then FDefinitelySorted := True;
end;
function TIPTCSection.GetPriorityValue(TagID: TIPTCTagID): TIPTCPriority;
var
Tag: TIPTCTag;
begin
if Find(TagID, Tag) and (Tag.DataSize = 1) then
for Result := Low(TIPTCPriority) to High(TIPTCPriority) do
if PriorityChars[Result] = PAnsiChar(Tag.Data)^ then Exit;
Result := ipTagMissing;
end;
function TIPTCSection.GetRepeatableValue(TagID: TIPTCTagID): TIPTCStringArray;
var
Count: Integer;
Tag: TIPTCTag;
begin
Count := 0;
Result := nil;
for Tag in Self do
if Tag.ID = TagID then
begin
if Count = Length(Result) then SetLength(Result, Count + 8);
Result[Count] := Tag.AsString;
Inc(Count);
end;
if Count <> 0 then SetLength(Result, Count);
end;
procedure TIPTCSection.GetRepeatableValue(TagID: TIPTCTagID; Dest: TStrings;
ClearDestFirst: Boolean);
var
Tag: TIPTCTag;
begin
Dest.BeginUpdate;
try
if ClearDestFirst then Dest.Clear;
for Tag in Self do
if Tag.ID = TagID then Dest.Add(Tag.AsString);
finally
Dest.EndUpdate;
end;
end;
procedure TIPTCSection.GetRepeatablePairs(KeyID, ValueID: TIPTCTagID; Dest: TStrings;
ClearDestFirst: Boolean = True);
var
Keys, Values: TIPTCStringArray;
I: Integer;
begin
Dest.BeginUpdate;
try
if ClearDestFirst then Dest.Clear;
Keys := GetRepeatableValue(KeyID);
Values := GetRepeatableValue(ValueID);
if Length(Values) > Length(Keys) then
SetLength(Keys, Length(Values))
else
SetLength(Values, Length(Keys));
for I := 0 to High(Keys) do
Dest.Add(Keys[I] + Dest.NameValueSeparator + Values[I]);
finally
Dest.EndUpdate;
end;
end;
function TIPTCSection.GetRepeatablePairs(KeyID, ValueID: TIPTCTagID): TIPTCRepeatablePairs;
var
Keys, Values: TIPTCStringArray;
I: Integer;
begin
Keys := GetRepeatableValue(KeyID);
Values := GetRepeatableValue(ValueID);
if Length(Values) > Length(Keys) then
SetLength(Keys, Length(Values))
else
SetLength(Values, Length(Keys));
SetLength(Result, Length(Keys));
for I := 0 to High(Result) do
begin
Result[I].Key := Keys[I];
Result[I].Value := Values[I];
end;
end;
procedure TIPTCSection.SetPriorityValue(TagID: TIPTCTagID; Value: TIPTCPriority);
begin
if Value = ipTagMissing then
Remove(TagID)
else
AddOrUpdate(TagID, 1, PriorityChars[Value]);
end;
procedure TIPTCSection.SetRepeatableValue(TagID: TIPTCTagID; const Value: array of string);
var
I, DestIndex: Integer;
begin
DestIndex := Remove(TagID);
if DestIndex < 0 then
begin
DestIndex := 0;
for I := Count - 1 downto 0 do
if TagID > GetTag(I).ID then
begin
DestIndex := I + 1;
Break;
end;
end;
for I := 0 to High(Value) do
begin
Insert(DestIndex, TagID).AsString := Value[I];
Inc(DestIndex);
end;
end;
procedure TIPTCSection.SetRepeatableValue(TagID: TIPTCTagID; Value: TStrings);
var
DynArray: TIPTCStringArray;
I: Integer;
begin
SetLength(DynArray, Value.Count);
for I := High(DynArray) downto 0 do
DynArray[I] := Value[I];
SetRepeatableValue(TagID, DynArray);
end;
procedure TIPTCSection.SetRepeatablePairs(KeyID, ValueID: TIPTCTagID; Pairs: TStrings);
var
I, DestIndex: Integer;
Key, Value: string;
begin
DestIndex := Remove([KeyID, ValueID]);
if DestIndex < 0 then
begin
DestIndex := 0;
for I := Count - 1 downto 0 do