Debian Codegraph @

docs/reference/html/iptc-overview.html

raw symbols SCIP
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>libiptcdata Overview: libiptcdata Reference Manual</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="index.html" title="libiptcdata Reference Manual">
8 <link rel="up" href="index.html" title="libiptcdata Reference Manual">
9 <link rel="prev" href="iptc-commandline.html" title="The IPTC Command-Line Utility">
10 <link rel="next" href="iptc-libjpeg.html" title="libjpeg Interoperability">
11 <meta name="generator" content="GTK-Doc V1.28 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 </head>
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
16 <td width="100%" align="left" class="shortcuts"></td>
17 <td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
18 <td><img src="up-insensitive.png" width="16" height="16" border="0"></td>
19 <td><a accesskey="p" href="iptc-commandline.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
20 <td><a accesskey="n" href="iptc-libjpeg.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
21 </tr></table>
22 <div class="refentry">
23 <a name="iptc-overview"></a><div class="titlepage"></div>
24 <div class="refnamediv"><table width="100%"><tr>
25 <td valign="top">
26 <h2><span class="refentrytitle">libiptcdata Overview</span></h2>
27 <p>libiptcdata Overview — how to use libiptcdata in an application</p>
28 </td>
29 <td class="gallery_image" valign="top" align="right"></td>
30 </tr></table></div>
31 <div class="refsect1">
32 <a name="jpeg-loading"></a><h2>Extracting IPTC data from a JPEG file</h2>
33 <p>
34 When using libiptcdata, the first task you are probably interested in
35 is reading the IPTC metadata from an existing file. At present, JPEG is
36 the only format supported by libiptcdata. The API is designed in
37 a multi-level fashion so that if your application already does some form
38 of JPEG decoding, you can integrate libiptcdata without having to parse
39 the file twice.
40 </p>
41 <p>
42 The <a class="ulink" href="http://www.iptc.org/IIM/" target="_top">IPTC IIM standard</a>
43 was originally conceived so that the JPEG file itself
44 would be encapsulated inside an IPTC wrapper. Since this is not backward
45 compatable with regular JPEGs, Adobe devised its own standard where an IPTC
46 data block is placed inside the Adobe Photoshop application-specific header.
47 This Photoshop
48 metadata is stored in the APP13 header section of a JPEG file. So reading
49 the IPTC data block is a two-step process: First, extract Photoshop metadata
50 from the APP13 header, and second, extract the IPTC data block from the
51 Photoshop metadata.
52 </p>
53 <p>
54 <a class="ulink" href="http://controlledvocabulary.com/imagedatabases/iptc_naa.html" target="_top">More
55 information about the history of IPTC metadata</a> is available at
56 controlledvocabulary.com.
57 </p>
58 <p>
59 For the impatient, the IPTC data can be extracted with a single
60 line of code using the <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-new-from-jpeg" title="iptc_data_new_from_jpeg ()">iptc_data_new_from_jpeg</a>()</code>
61 function if you have no need for fine-grained error checking or you don't
62 want to share code with a pre-existing JPEG decoding routine in your
63 application. Here's an example:
64 </p>
65 <pre class="programlisting">
66 IptcData * d;
67 char * filename = "foo.jpg";
68
69 d = iptc_data_new_from_jpeg (filename);
70 if (!d) {
71 fprintf (stderr, "Error reading IPTC data\n");
72 return 1;
73 }
74 </pre>
75 <p>
76 However, for all but the most simplistic of applications, you will probably
77 want finer control of the process. For that, you use three functions.
78 The first, <code class="function"><a class="link" href="libiptcdata-jpeg.html#iptc-jpeg-read-ps3" title="iptc_jpeg_read_ps3 ()">iptc_jpeg_read_ps3</a>()</code>
79 extracts the
80 Photoshop metadata from the APP13 header. If your application already
81 decodes the JPEG file, you don't need this function: Simply store the
82 contents of the APP13 header as you decode the JPEG. See
83 <a class="link" href="iptc-libjpeg.html" title="libjpeg Interoperability">libjpeg Interoperability</a> for
84 an example.
85 </p>
86 <p>
87 The second function,
88 <code class="function"><a class="link" href="libiptcdata-jpeg.html#iptc-jpeg-ps3-find-iptc" title="iptc_jpeg_ps3_find_iptc ()">iptc_jpeg_ps3_find_iptc</a>()</code>
89 extracts the IPTC data block from the Photoshop metadata.
90 </p>
91 <p>
92 The third function, <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-new-from-data" title="iptc_data_new_from_data ()">iptc_data_new_from_data</a>()</code>
93 takes the IPTC data block and decodes it into a data structure of type
94 <span class="structname"><a class="link" href="libiptcdata-data.html#IptcData" title="struct IptcData">IptcData</a></span> which you can use with the rest of the
95 libiptcdata API. Here's an example of all three functions in use:
96 </p>
97 <pre class="programlisting">
98 FILE * infile;
99 int ps3_len, iptc_off, iptc_len;
100 IptcData * d;
101 char * filename = "foo.jpg";
102 unsigned char buf[256*256];
103
104 infile = fopen (filename, "r");
105 if (!infile) {
106 fprintf (stderr, "Error opening %s\n", filename);
107 return 1;
108 }
109
110 ps3_len = iptc_jpeg_read_ps3 (infile, buf, sizeof(buf));
111 fclose (infile);
112 if (ps3_len &lt; 0) {
113 fprintf (stderr, "Error parsing JPEG file\n");
114 return 1;
115 }
116
117 if (ps3_len == 0) {
118 fprintf (stderr, "File contains no PS3 header\n");
119 return 1;
120 }
121
122 iptc_off = iptc_jpeg_ps3_find_iptc (buf, ps3_len, &amp;iptc_len);
123 if (iptc_off &lt; 0) {
124 fprintf (stderr, "Error parsing PS3 header\n");
125 return 1;
126 }
127
128 if (iptc_off == 0) {
129 fprintf (stderr, "PS3 header contains no IPTC data\n");
130 return 1;
131 }
132
133 d = iptc_data_new_from_data (buf + iptc_off, iptc_len);
134 if (!d) {
135 fprintf (stderr, "Error parsing IPTC data\n");
136 return 1;
137 }
138 </pre>
139 </div>
140 <div class="refsect1">
141 <a name="creating-iptc"></a><h2>Creating IPTC data from scratch</h2>
142 <p>
143 If you want to create an empty <span class="structname"><a class="link" href="libiptcdata-data.html#IptcData" title="struct IptcData">IptcData</a></span> structure from scratch without
144 reading it from a preexisting file, use the <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-new" title="iptc_data_new ()">iptc_data_new</a>()</code> function:
145 </p>
146 <pre class="programlisting">
147 IptcData * d;
148
149 d = iptc_data_new ();
150 </pre>
151 </div>
152 <div class="refsect1">
153 <a name="viewing-iptc"></a><h2>Viewing IPTC data</h2>
154 <p>
155 IPTC metadata is simply a list of "datasets." Each dataset contains a
156 record number, dataset number, and the data itself. The record number
157 and dataset number, taken together, determine the purpose of the dataset,
158 according to the IPTC IIM specification. For example, dataset 2:120 (record 2,
159 dataset 120) contains an image caption. Some datasets can be repeated, such
160 as dataset 2:25, which contains a single image keyword. Each repeated dataset
161 would contain a separate keyword. The IPTC IIM specification determines
162 the content of each dataset, the type (string, byte, short, etc.), and any
163 restrictions such as minimum/maximum length, repeatability, etc.
164 </p>
165 <p>
166 The following code snippet shows how to iterate through all the datasets in
167 an <span class="structname"><a class="link" href="libiptcdata-data.html#IptcData" title="struct IptcData">IptcData</a></span> object
168 and print their contents. The <code class="function"><a class="link" href="libiptcdata-tag.html#iptc-tag-get-title" title="iptc_tag_get_title ()">iptc_tag_get_title</a>()</code>
169 function gets the string name of a dataset from its record and dataset numbers.
170 <code class="function"><a class="link" href="libiptcdata-dataset.html#iptc-dataset-get-format" title="iptc_dataset_get_format ()">iptc_dataset_get_format</a>()</code>
171 retrieves the type of the data contained in the dataset. For datasets with
172 integer types, <code class="function"><a class="link" href="libiptcdata-dataset.html#iptc-dataset-get-value" title="iptc_dataset_get_value ()">iptc_dataset_get_value</a>()</code>
173 gets the contents of the tag, and for datasets with string types,
174 <code class="function"><a class="link" href="libiptcdata-dataset.html#iptc-dataset-get-data" title="iptc_dataset_get_data ()">iptc_dataset_get_data</a>()</code>
175 will do the same.
176 <code class="function"><a class="link" href="libiptcdata-dataset.html#iptc-dataset-get-as-str" title="iptc_dataset_get_as_str ()">iptc_dataset_get_as_str</a>()</code>
177 formats the data as a string regardless of its type.
178 </p>
179 <pre class="programlisting">
180 int i;
181
182 printf("%6.6s %-20.20s %-9.9s %6s %s\n", "Tag", "Name", "Type",
183 "Size", "Value");
184 printf(" ----- -------------------- --------- ------ -----\n");
185
186 for (i=0; i &lt; d-&gt;count; i++) {
187 IptcDataSet * e = d-&gt;datasets[i];
188 char buf[256];
189
190 printf("%2d:%03d %-20.20s %-9.9s %6d ",
191 e-&gt;record, e-&gt;tag,
192 iptc_tag_get_title (e-&gt;record, e-&gt;tag),
193 iptc_format_get_name (iptc_dataset_get_format (e)),
194 e-&gt;size);
195 switch (iptc_dataset_get_format (e)) {
196 case IPTC_FORMAT_BYTE:
197 case IPTC_FORMAT_SHORT:
198 case IPTC_FORMAT_LONG:
199 printf("%d\n", iptc_dataset_get_value (e));
200 break;
201 case IPTC_FORMAT_BINARY:
202 iptc_dataset_get_as_str (e, buf, sizeof(buf));
203 printf("%s\n", buf);
204 break;
205 default:
206 iptc_dataset_get_data (e, buf, sizeof(buf));
207 printf("%s\n", buf);
208 break;
209 }
210 }
211 </pre>
212 <p>
213 This next code snippet prints out the list of keywords stored in the
214 IPTC metadata. First, in case we don't know the record and dataset
215 numbers for the "Keywords" tag, the
216 <code class="function"><a class="link" href="libiptcdata-tag.html#iptc-tag-find-by-name" title="iptc_tag_find_by_name ()">iptc_tag_find_by_name</a>()</code>
217 function retrieves them from the IPTC specification. Next, the
218 <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-get-next-dataset" title="iptc_data_get_next_dataset ()">iptc_data_get_next_dataset</a>()</code>
219 function iterates through each Keyword tag, printing out the contents.
220 </p>
221 <p>
222 Another important concept presented in this example is the use of reference
223 counts. Whenever a function, such as <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-get-next-dataset" title="iptc_data_get_next_dataset ()">iptc_data_get_next_dataset</a>()</code>
224 returns a pointer, the reference count for the associated object is incremented by one.
225 It is the application's responsibility to later unreference that pointer
226 so that the count does not keep growing. When the count reaches zero, the
227 object is automatically freed. This must be done for both
228 <span class="structname"><a class="link" href="libiptcdata-data.html#IptcData" title="struct IptcData">IptcData</a></span>
229 (with <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-unref" title="iptc_data_unref ()">iptc_data_unref</a></code>) and
230 <span class="structname"><a class="link" href="libiptcdata-dataset.html#IptcDataSet" title="struct IptcDataSet">IptcDataSet</a></span> (with
231 <code class="function"><a class="link" href="libiptcdata-dataset.html#iptc-dataset-unref" title="iptc_dataset_unref ()">iptc_dataset_unref</a></code>) objects.
232 </p>
233 <pre class="programlisting">
234 IptcDataSet * ds = NULL;
235 IptcRecord record;
236 IptcTag tag;
237 char buf[256];
238
239 if (iptc_tag_find_by_name ("Keywords", &amp;record, &amp;tag) &lt; 0) {
240 fprintf (stderr, "Invalid tag name\n");
241 return 1;
242 }
243
244 while (1) {
245 ds = iptc_data_get_next_dataset (d, ds, record, tag);
246 if (!ds)
247 break;
248 iptc_dataset_get_data (ds, buf, sizeof(buf));
249 printf("%s\n", buf);
250 iptc_dataset_unref(ds);
251 }
252 </pre>
253 </div>
254 <div class="refsect1">
255 <a name="adding-tag"></a><h2>Adding a new tag</h2>
256 <p>
257 New tags can be added to an
258 <span class="structname"><a class="link" href="libiptcdata-data.html#IptcData" title="struct IptcData">IptcData</a></span>
259 object with the <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-add-dataset" title="iptc_data_add_dataset ()">iptc_data_add_dataset</a>()</code> function.
260 This new dataset can be obtained from another
261 <span class="structname"><a class="link" href="libiptcdata-data.html#IptcData" title="struct IptcData">IptcData</a></span> object,
262 or it can be created from scratch with the
263 <code class="function"><a class="link" href="libiptcdata-dataset.html#iptc-dataset-new" title="iptc_dataset_new ()">iptc_dataset_new</a>()</code> function.
264 If created from scratch,
265 <code class="function"><a class="link" href="libiptcdata-dataset.html#iptc-dataset-set-tag" title="iptc_dataset_set_tag ()">iptc_dataset_set_tag</a>()</code>
266 is first used to set the record and dataset number for the tag. Next,
267 <code class="function"><a class="link" href="libiptcdata-dataset.html#iptc-dataset-set-tag" title="iptc_dataset_set_tag ()">iptc_dataset_set_data</a>()</code>
268 is used to set the actual contents of the tag. The following code snippet
269 demonstrates adding a new keyword, "travel" to an existing set of IPTC
270 data:
271 </p>
272 <pre class="programlisting">
273 IptcDataSet * ds;
274 IptcRecord record;
275 IptcTag tag;
276
277 if (iptc_tag_find_by_name ("Keywords", &amp;record, &amp;tag) &lt; 0) {
278 fprintf (stderr, "Invalid tag name\n");
279 return 1;
280 }
281
282 ds = iptc_dataset_new ();
283 iptc_dataset_set_tag (ds, record, tag);
284 iptc_dataset_set_data (ds, "travel", 6, IPTC_DONT_VALIDATE);
285 iptc_data_add_dataset (d, ds);
286 iptc_dataset_unref (ds);
287 </pre>
288 </div>
289 <div class="refsect1">
290 <a name="modifying-tag"></a><h2>Modifying an existing tag</h2>
291 <p>
292 Combining the functions we have seen above, the following code
293 snippet modifies an existing caption in the IPTC data. It first
294 finds the Caption tag with the
295 <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-get-dataset" title="iptc_data_get_dataset ()">iptc_data_get_dataset</a>()</code>
296 function and then changes the data with
297 <code class="function"><a class="link" href="libiptcdata-dataset.html#iptc-dataset-set-data" title="iptc_dataset_set_data ()">iptc_dataset_set_data</a>()</code>:
298 </p>
299 <pre class="programlisting">
300 IptcDataSet * ds;
301 IptcRecord record;
302 IptcTag tag;
303 char * str = "This is a caption";
304
305 if (iptc_tag_find_by_name ("Caption", &amp;record, &amp;tag) &lt; 0) {
306 fprintf (stderr, "Invalid tag name\n");
307 return 1;
308 }
309
310 ds = iptc_data_get_dataset (d, record, tag);
311 if (!ds) {
312 fprintf (stderr, "Tag not found\n");
313 return 1;
314 }
315 iptc_dataset_set_data (ds, str, strlen (str), IPTC_DONT_VALIDATE);
316 iptc_dataset_unref (ds);
317 </pre>
318 </div>
319 <div class="refsect1">
320 <a name="deleting-tag"></a><h2>Deleting a tag</h2>
321 <p>
322 A dataset can be deleted using the
323 <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-remove-dataset" title="iptc_data_remove_dataset ()">iptc_data_remove_dataset</a>()</code> function, as seen in this example, which deletes the Caption from the
324 IPTC data:
325 </p>
326 <pre class="programlisting">
327 IptcDataSet * ds;
328 IptcRecord record;
329 IptcTag tag;
330
331 if (iptc_tag_find_by_name ("Caption", &amp;record, &amp;tag) &lt; 0) {
332 fprintf (stderr, "Invalid tag name\n");
333 return 1;
334 }
335
336 ds = iptc_data_get_dataset (d, record, tag);
337 if (!ds) {
338 fprintf (stderr, "Tag not found\n");
339 return 1;
340 }
341 iptc_data_remove_dataset (d, ds);
342 iptc_dataset_unref (ds);
343 </pre>
344 </div>
345 <div class="refsect1">
346 <a name="jpeg-saving"></a><h2>Saving IPTC data to a JPEG file</h2>
347 <p>
348 Saving an <span class="structname"><a class="link" href="libiptcdata-data.html#IptcData" title="struct IptcData">IptcData</a></span>
349 object back to a JPEG file is a multi-step process requiring several
350 different function calls. Although this may seem cumbersome at first,
351 it allows you to integrate the process into any existing JPEG-saving
352 code already written in your application.
353 </p>
354 <p>
355 First, a call to
356 <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-sort" title="iptc_data_sort ()">iptc_data_sort</a>()</code>
357 sorts the datasets by tag number prior to saving. This step
358 is optional.
359 </p>
360 <p>
361 Next, by calling
362 <code class="function"><a class="link" href="libiptcdata-data.html#iptc-data-save" title="iptc_data_save ()">iptc_data_save</a>()</code>
363 we convert the
364 <span class="structname"><a class="link" href="libiptcdata-data.html#IptcData" title="struct IptcData">IptcData</a></span>
365 object into a bytestream. This IPTC data block is then encapsulated
366 inside a Photoshop header using
367 <code class="function"><a class="link" href="libiptcdata-jpeg.html#iptc-jpeg-ps3-save-iptc" title="iptc_jpeg_ps3_save_iptc ()">iptc_jpeg_ps3_save_iptc</a>()</code>.
368 This function will use an existing Photoshop header, if available, so that
369 all unrelated portions of the Photoshop header are preserved. If no
370 pre-existing Photoshop header is provided, a new one will be created from
371 scratch.
372 </p>
373 <p>
374 The new Photoshop header can be saved inside the APP13 header of the JPEG
375 file using your application code, or you can use
376 <code class="function"><a class="link" href="libiptcdata-jpeg.html#iptc-jpeg-save-with-ps3" title="iptc_jpeg_save_with_ps3 ()">iptc_jpeg_save_with_ps3</a>()</code> to do the same thing.
377 </p>
378 <p>
379 The following example code demonstrates the entire process:
380 </p>
381 <pre class="programlisting">
382 unsigned char * iptc_buf = NULL;
383 unsigned char outbuf[256*256];
384 char tmpfile[strlen(filename)+8];
385 int v;
386
387 iptc_data_sort (d);
388
389 if (iptc_data_save (d, &amp;iptc_buf, &amp;iptc_len) &lt; 0) {
390 fprintf (stderr, "Failed to generate IPTC bytestream\n");
391 return 1;
392 }
393 ps3_len = iptc_jpeg_ps3_save_iptc (buf, ps3_len,
394 iptc_buf, iptc_len, outbuf, sizeof(outbuf));
395 iptc_data_free_buf (d, iptc_buf);
396 if (ps3_len &lt; 0) {
397 fprintf (stderr, "Failed to generate PS3 header\n");
398 return 1;
399 }
400
401 infile = fopen (filename, "r");
402 if (!infile) {
403 fprintf (stderr, "Can't reopen input file\n");
404 return 1;
405 }
406 sprintf (tmpfile, "%s.%d", filename, getpid());
407 outfile = fopen (tmpfile, "w");
408 if (!outfile) {
409 fprintf (stderr, "Can't open temporary file for writing\n");
410 return 1;
411 }
412
413 v = iptc_jpeg_save_with_ps3 (infile, outfile, outbuf, ps3_len);
414 fclose (infile);
415 fclose (outfile);
416 if (v &lt; 0) {
417 unlink (tmpfile);
418 fprintf (stderr, "Failed to save image\n");
419 return 1;
420 }
421
422 if (rename (tmpfile, filename) &lt; 0) {
423 fprintf (stderr, "Failed to save image\n");
424 unlink (tmpfile);
425 return 1;
426 }
427 fprintf (stderr, "Image saved\n");
428 </pre>
429 </div>
430 </div>
431 <div class="footer">
432 <hr>Generated by GTK-Doc V1.28</div>
433 </body>
434 </html>