I try to add metadata to an existing JPEG file and saving the image in another file. I'm using Delphi 11 and FreeImage (Delphi wrapper). The image is produced identical to the existing image, without the added metadata tag. No error at all.
Here is a simple stand alone procedure to reproduce the issue:
procedure AddTagArtistTest;
var
fif : FREE_IMAGE_FORMAT;
dib : PFIBITMAP;
Tag : PFITAG;
TagValue : AnsiString;
TagKey : AnsiString;
TagID : WORD;
Filename : String;
Success : Boolean;
begin
Filename := 'F:\Images\ExistingImage.jpg'; // Already has metadata but no TAG_ARTIST
dib := nil;
Success := FALSE;
try
fif := FreeImage_GetFileTypeU(PChar(FileName), 0);
if fif = FIF_UNKNOWN then
fif := FreeImage_GetFIFFromFilenameU(PChar(FileName));
if fif = FIF_UNKNOWN then
Exit;
if not FreeImage_FIFSupportsReading(fif) then
Exit;
dib := FreeImage_LoadU(fif, PChar(Filename), 0);
if dib = nil then
Exit;
Tag := FreeImage_CreateTag();
TagValue := 'FRANCOIS PIETTE';
TagKey := 'Artist';
TagID := $013B; // TAG_ARTIST;
if not FreeImage_SetTagID(Tag, TagID) then
Exit;
if not FreeImage_SetTagKey(Tag, PAnsiChar(TagKey)) then
Exit;
if not FreeImage_SetTagType(Tag, FIDT_ASCII) then
Exit;
if not FreeImage_SetTagLength(Tag, Length(TagValue) + 1) then
Exit;
if not FreeImage_SetTagCount(Tag, Length(TagValue) + 1) then
Exit;
if not FreeImage_SetTagValue(Tag, PAnsiChar(TagValue)) then
Exit;
if not FreeImage_SetMetadata(FIMD_EXIF_MAIN,
dib,
PAnsiChar(TagKey),
Tag) then
Exit;
if not FreeImage_SaveU(FIF_JPEG,
dib,
PChar(ChangeFileExt(FileName, '_2.jpg')),
0) then
Exit;
Success := TRUE;
finally
if dib <> nil then
FreeImage_Unload(dib);
if Success then
WriteLn('Success')
else
WriteLn('Failed');
end;
end;
Any idea about what I'm doing wrong?