Search

Atalasoft Knowledge Base

HOWTO: Embed XMP Data Into a TIFF File

Administrator
DotImage

XMP annotation data can be embedded into a TIFF file rather than saving it as a separate file. This is done by saving the annotations into an XMP packet with the XmpFormatter and embedding the data into the XMP TIFF tag. Below is a code example:

C#

using Atalasoft.Annotate;
using Atalasoft.Annotate.UI;
using Atalasoft.Annotate.Formatters;
using Atalasoft.Imaging;
using Atalasoft.Imaging.Codec;
using System.IO;
//...

private void SaveXmpToTiff(Stream outputStream, AnnotateViewer viewer)
{
	SaveXmpToTiff(outputStream, viewer.Annotations, viewer.Image);
}

private void SaveXmpToTiff(Stream outputStream, AnnotationController controller, AtalaImage image)
{
	// Create an XmpFormatter and tell it to create a packet.
	// Packets are required when embedding the data into an image.
	XmpFormatter formatter = new XmpFormatter();
	formatter.CreateXmpPacket = true;
	// Create a TiffEncoder and set its Xmp property to the annotation data.
	TiffEncoder tif = new TiffEncoder();
	tif.Xmp = controller.Save(formatter);
	// Save the Tiff.
	tif.Save(outputStream, image, null);
}

VB.NET

Imports Atalasoft.Annotate
Imports Atalasoft.Annotate.UI
Imports Atalasoft.Annotate.Formatters
Imports Atalasoft.Imaging
Imports Atalasoft.Imaging.Codec
Imports System.IO
'...

Private Sub SaveXmpToTiff(ByVal outputStream As Stream, ByVal viewer As AnnotateViewer)
  SaveXmpToTiff(outputStream, viewer.Annotations, viewer.Image)
End Sub
 
Private Sub SaveXmpToTiff(ByVal outputStream As Stream, ByVal controller As AnnotationController, ByVal image As AtalaImage)
  ' Create an XmpFormatter and tell it to create a packet.
  ' Packets are required when embedding the data into an image.
  Dim formatter As XmpFormatter = New XmpFormatter() 
  formatter.CreateXmpPacket = True
 
  ' Create a TiffEncoder and set its Xmp property to the annotation data.
  Dim tif As TiffEncoder =  New TiffEncoder() 
  tif.Xmp = controller.Save(formatter)
 
  ' Save the Tiff.
  tif.Save(outputStream, image, Nothing)
End Sub

To read the XMP annotation back into DotAnnotate, you can either use the AnnotateViewer.Open method with the loadAnnotations argument set to true or you can manually extract the XMP TIFF tag data using the following code.

C#

using Atalasoft.Annotate.UI;
using Atalasoft.Annotate.Formatters;using Atalasoft.Imaging.Metadata;using Atalasoft.Imaging.Codec;using System.IO;
//...

private void LoadXmpFromTiff(Stream inputStream, AnnotateViewer viewer)
{  
    TiffTag tag = TiffDecoder.GetTiffTag((int)TiffTagID.XmpData, inputStream, 0);
    if (tag != null) 
        viewer.Annotations.Load((byte[])tag.Data, new XmpFormatter());
}

VB.NET

Imports Atalasoft.Annotate.UI
Imports Atalasoft.Annotate.Formatters
Imports Atalasoft.Imaging.Codec
Imports Atalasoft.Imaging.Metadata
Imports System.IO
'... 

Private Sub LoadXmpFromTiff(ByVal inputStream As Stream, ByVal viewer As AnnotateViewer)  
    Dim tag As TiffTag = TiffDecoder.GetTiffTag(CType(TiffTagID.XmpData, Integer), inputStream, 0)
    If Not tag Is Nothing Then
        viewer.Annotations.Load(CType(tag.Data, Byte()), New XmpFormatter())
    End If
End Sub

Original Article:
Q10220 - HOWTO: Embed XMP data into a TIFF file

Details
Last Modified: 6 Years Ago
Last Modified By: Administrator
Type: HOWTO
Article not rated yet.
Article has been viewed 1.6K times.
Options
Also In This Category