~ubuntu-branches/ubuntu/karmic/exif/karmic

« back to all changes in this revision

Viewing changes to libjpeg/jpeg-data.c

  • Committer: Bazaar Package Importer
  • Author(s): christophe barbe
  • Date: 2004-06-18 14:44:26 UTC
  • Revision ID: james.westby@ubuntu.com-20040618144426-36li8gkxq3f3kau3
Tags: upstream-0.6.9
Import upstream version 0.6.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* jpeg-data.c
 
2
 *
 
3
 * Copyright � 2001 Lutz M�ller <lutz@users.sourceforge.net>
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
#include "jpeg-data.h"
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
 
 
28
/* #define DEBUG */
 
29
 
 
30
struct _JPEGDataPrivate
 
31
{
 
32
        unsigned int ref_count;
 
33
};
 
34
 
 
35
JPEGData *
 
36
jpeg_data_new (void)
 
37
{
 
38
        JPEGData *data;
 
39
 
 
40
        data = malloc (sizeof (JPEGData));
 
41
        if (!data)
 
42
                return (NULL);
 
43
        memset (data, 0, sizeof (JPEGData));
 
44
        data->priv = malloc (sizeof (JPEGDataPrivate));
 
45
        if (!data->priv) {
 
46
                free (data);
 
47
                return (NULL);
 
48
        }
 
49
        memset (data->priv, 0, sizeof (JPEGDataPrivate));
 
50
        data->priv->ref_count = 1;
 
51
 
 
52
        return (data);
 
53
}
 
54
 
 
55
void
 
56
jpeg_data_append_section (JPEGData *data)
 
57
{
 
58
        JPEGSection *s;
 
59
 
 
60
        if (!data->count)
 
61
                s = malloc (sizeof (JPEGSection));
 
62
        else
 
63
                s = realloc (data->sections,
 
64
                             sizeof (JPEGSection) * (data->count + 1));
 
65
        if (!s)
 
66
                return;
 
67
 
 
68
        data->sections = s;
 
69
        data->count++;
 
70
}
 
71
 
 
72
/* jpeg_data_save_file returns 1 on succes, 0 on failure */
 
73
int
 
74
jpeg_data_save_file (JPEGData *data, const char *path)
 
75
{
 
76
        FILE *f;
 
77
        unsigned char *d = NULL;
 
78
        unsigned int size = 0, written;
 
79
 
 
80
        jpeg_data_save_data (data, &d, &size);
 
81
        if (!d)
 
82
                return 0;
 
83
 
 
84
        remove (path);
 
85
        f = fopen (path, "wb");
 
86
        if (!f) {
 
87
                free (d);
 
88
                return 0;
 
89
        }
 
90
        written = fwrite (d, 1, size, f);
 
91
        fclose (f);
 
92
        free (d);
 
93
        if (written == size)  {
 
94
                return 1;
 
95
        }
 
96
        remove(path);
 
97
        return 0;
 
98
}
 
99
 
 
100
void
 
101
jpeg_data_save_data (JPEGData *data, unsigned char **d, unsigned int *ds)
 
102
{
 
103
        unsigned int i, eds = 0;
 
104
        JPEGSection s;
 
105
        unsigned char *ed = NULL;
 
106
 
 
107
        if (!data)
 
108
                return;
 
109
        if (!d)
 
110
                return;
 
111
        if (!ds)
 
112
                return;
 
113
 
 
114
        for (*ds = i = 0; i < data->count; i++) {
 
115
                s = data->sections[i];
 
116
#ifdef DEBUG
 
117
                printf ("Writing marker 0x%x at position %i...\n",
 
118
                        s.marker, *ds);
 
119
#endif
 
120
 
 
121
                /* Write the marker */
 
122
                *d = realloc (*d, sizeof (char) * (*ds + 2));
 
123
                (*d)[*ds + 0] = 0xff;
 
124
                (*d)[*ds + 1] = s.marker;
 
125
                *ds += 2;
 
126
 
 
127
                switch (s.marker) {
 
128
                case JPEG_MARKER_SOI:
 
129
                case JPEG_MARKER_EOI:
 
130
                        break;
 
131
                case JPEG_MARKER_APP1:
 
132
                        exif_data_save_data (s.content.app1, &ed, &eds);
 
133
                        if (!ed) break;
 
134
                        *d = realloc (*d, sizeof (char) * (*ds + 2));
 
135
                        (*d)[*ds + 0] = (eds + 2) >> 8;
 
136
                        (*d)[*ds + 1] = (eds + 2) >> 0;
 
137
                        *ds += 2;
 
138
                        *d = realloc (*d, sizeof (char) * (*ds + eds));
 
139
                        memcpy (*d + *ds, ed, eds);
 
140
                        *ds += eds;
 
141
                        free (ed);
 
142
                        break;
 
143
                default:
 
144
                        *d = realloc (*d, sizeof (char) *
 
145
                                        (*ds + s.content.generic.size + 2));
 
146
                        (*d)[*ds + 0] = (s.content.generic.size + 2) >> 8;
 
147
                        (*d)[*ds + 1] = (s.content.generic.size + 2) >> 0;
 
148
                        *ds += 2;
 
149
                        memcpy (*d + *ds, s.content.generic.data,
 
150
                                s.content.generic.size);
 
151
                        *ds += s.content.generic.size;
 
152
 
 
153
                        /* In case of SOS, we need to write the data. */
 
154
                        if (s.marker == JPEG_MARKER_SOS) {
 
155
                                *d = realloc (*d, *ds + data->size);
 
156
                                memcpy (*d + *ds, data->data, data->size);
 
157
                                *ds += data->size;
 
158
                        }
 
159
                        break;
 
160
                }
 
161
        }
 
162
}
 
163
 
 
164
JPEGData *
 
165
jpeg_data_new_from_data (const unsigned char *d,
 
166
                         unsigned int size)
 
167
{
 
168
        JPEGData *data;
 
169
 
 
170
        data = jpeg_data_new ();
 
171
        jpeg_data_load_data (data, d, size);
 
172
        return (data);
 
173
}
 
174
 
 
175
void
 
176
jpeg_data_load_data (JPEGData *data, const unsigned char *d,
 
177
                     unsigned int size)
 
178
{
 
179
        unsigned int i, o, len;
 
180
        JPEGSection *s;
 
181
        JPEGMarker marker;
 
182
 
 
183
        if (!data)
 
184
                return;
 
185
        if (!d)
 
186
                return;
 
187
 
 
188
#ifdef DEBUG
 
189
        printf ("Parsing %i bytes...\n", size);
 
190
#endif
 
191
 
 
192
        for (o = 0; o < size;) {
 
193
 
 
194
                /*
 
195
                 * JPEG sections start with 0xff. The first byte that is
 
196
                 * not 0xff is a marker (hopefully).
 
197
                 */
 
198
                for (i = 0; i < 7; i++)
 
199
                        if (d[o + i] != 0xff)
 
200
                                break;
 
201
                if (!JPEG_IS_MARKER (d[o + i]))
 
202
                        return;
 
203
                marker = d[o + i];
 
204
 
 
205
#ifdef DEBUG
 
206
                printf ("Found marker 0x%x ('%s') at %i.\n", marker,
 
207
                        jpeg_marker_get_name (marker), o + i);
 
208
#endif
 
209
 
 
210
                /* Append this section */
 
211
                jpeg_data_append_section (data);
 
212
                s = &data->sections[data->count - 1];
 
213
                s->marker = marker;
 
214
                s->content.generic.data = NULL;
 
215
                o += i + 1;
 
216
 
 
217
                switch (s->marker) {
 
218
                case JPEG_MARKER_SOI:
 
219
                case JPEG_MARKER_EOI:
 
220
                        break;
 
221
                default:
 
222
 
 
223
                        /* Read the length of the section */
 
224
                        len = ((d[o] << 8) | d[o + 1]) - 2;
 
225
                        if (len > size) { o = size; break; }
 
226
                        o += 2;
 
227
                        if (o + len > size) { o = size; break; }
 
228
 
 
229
                        switch (s->marker) {
 
230
                        case JPEG_MARKER_APP1:
 
231
                                s->content.app1 = exif_data_new_from_data (
 
232
                                                        d + o - 4, len + 4);
 
233
                                break;
 
234
                        default:
 
235
                                s->content.generic.size = len;
 
236
                                s->content.generic.data =
 
237
                                                malloc (sizeof (char) * len);
 
238
                                memcpy (s->content.generic.data, &d[o], len);
 
239
 
 
240
                                /* In case of SOS, image data will follow. */
 
241
                                if (s->marker == JPEG_MARKER_SOS) {
 
242
                                        data->size = size - 2 - o - len;
 
243
                                        data->data = malloc (
 
244
                                                sizeof (char) * data->size);
 
245
                                        memcpy (data->data, d + o + len,
 
246
                                                data->size);
 
247
                                        o += data->size;
 
248
                                }
 
249
                                break;
 
250
                        }
 
251
                        o += len;
 
252
                        break;
 
253
                }
 
254
        }
 
255
}
 
256
 
 
257
JPEGData *
 
258
jpeg_data_new_from_file (const char *path)
 
259
{
 
260
        JPEGData *data;
 
261
 
 
262
        data = jpeg_data_new ();
 
263
        jpeg_data_load_file (data, path);
 
264
        return (data);
 
265
}
 
266
 
 
267
void
 
268
jpeg_data_load_file (JPEGData *data, const char *path)
 
269
{
 
270
        FILE *f;
 
271
        unsigned char *d;
 
272
        unsigned int size;
 
273
 
 
274
        if (!data)
 
275
                return;
 
276
        if (!path)
 
277
                return;
 
278
 
 
279
        f = fopen (path, "rb");
 
280
        if (!f)
 
281
                return;
 
282
 
 
283
        /* For now, we read the data into memory. Patches welcome... */
 
284
        fseek (f, 0, SEEK_END);
 
285
        size = ftell (f);
 
286
        fseek (f, 0, SEEK_SET);
 
287
        d = malloc (sizeof (char) * size);
 
288
        if (!d) {
 
289
                fclose (f);
 
290
                return;
 
291
        }
 
292
        if (fread (d, 1, size, f) != size) {
 
293
                free (d);
 
294
                fclose (f);
 
295
                return;
 
296
        }
 
297
        fclose (f);
 
298
 
 
299
        jpeg_data_load_data (data, d, size);
 
300
        free (d);
 
301
}
 
302
 
 
303
void
 
304
jpeg_data_ref (JPEGData *data)
 
305
{
 
306
        if (!data)
 
307
                return;
 
308
 
 
309
        data->priv->ref_count++;
 
310
}
 
311
 
 
312
void
 
313
jpeg_data_unref (JPEGData *data)
 
314
{
 
315
        if (!data)
 
316
                return;
 
317
 
 
318
        data->priv->ref_count--;
 
319
        if (!data->priv->ref_count)
 
320
                jpeg_data_free (data);
 
321
}
 
322
 
 
323
void
 
324
jpeg_data_free (JPEGData *data)
 
325
{
 
326
        unsigned int i;
 
327
        JPEGSection s;
 
328
 
 
329
        if (!data)
 
330
                return;
 
331
 
 
332
        if (data->count) {
 
333
                for (i = 0; i < data->count; i++) {
 
334
                        s = data->sections[i];
 
335
                        switch (s.marker) {
 
336
                        case JPEG_MARKER_SOI:
 
337
                        case JPEG_MARKER_EOI:
 
338
                                break;
 
339
                        case JPEG_MARKER_APP1:
 
340
                                exif_data_unref (s.content.app1);
 
341
                                break;
 
342
                        default:
 
343
                                free (s.content.generic.data);
 
344
                                break;
 
345
                        }
 
346
                }
 
347
                free (data->sections);
 
348
        }
 
349
 
 
350
        if (data->data)
 
351
                free (data->data);
 
352
        free (data->priv);
 
353
        free (data);
 
354
}
 
355
 
 
356
void
 
357
jpeg_data_dump (JPEGData *data)
 
358
{
 
359
        unsigned int i;
 
360
        JPEGContent content;
 
361
        JPEGMarker marker;
 
362
 
 
363
        if (!data)
 
364
                return;
 
365
 
 
366
        printf ("Dumping JPEG data (%i bytes of data)...\n", data->size);
 
367
        for (i = 0; i < data->count; i++) {
 
368
                marker = data->sections[i].marker;
 
369
                content = data->sections[i].content;
 
370
                printf ("Section %i (marker 0x%x - %s):\n", i, marker,
 
371
                        jpeg_marker_get_name (marker));
 
372
                printf ("  Description: %s\n",
 
373
                        jpeg_marker_get_description (marker));
 
374
                switch (marker) {
 
375
                case JPEG_MARKER_SOI:
 
376
                case JPEG_MARKER_EOI:
 
377
                        break;
 
378
                case JPEG_MARKER_APP1:
 
379
                        exif_data_dump (content.app1);
 
380
                        break;
 
381
                default:
 
382
                        printf ("  Size: %i\n", content.generic.size);
 
383
                        printf ("  Unknown content.\n");
 
384
                        break;
 
385
                }
 
386
        }
 
387
}
 
388
 
 
389
static JPEGSection *
 
390
jpeg_data_get_section (JPEGData *data, JPEGMarker marker)
 
391
{
 
392
        unsigned int i;
 
393
 
 
394
        if (!data)
 
395
                return (NULL);
 
396
 
 
397
        for (i = 0; i < data->count; i++)
 
398
                if (data->sections[i].marker == marker)
 
399
                        return (&data->sections[i]);
 
400
        return (NULL);
 
401
}
 
402
 
 
403
ExifData *
 
404
jpeg_data_get_exif_data (JPEGData *data)
 
405
{
 
406
        JPEGSection *section;
 
407
 
 
408
        if (!data)
 
409
                return NULL;
 
410
 
 
411
        section = jpeg_data_get_section (data, JPEG_MARKER_APP1);
 
412
        if (section) {
 
413
                exif_data_ref (section->content.app1);
 
414
                return (section->content.app1);
 
415
        }
 
416
 
 
417
        return (NULL);
 
418
}
 
419
 
 
420
void
 
421
jpeg_data_set_exif_data (JPEGData *data, ExifData *exif_data)
 
422
{
 
423
        JPEGSection *section;
 
424
 
 
425
        section = jpeg_data_get_section (data, JPEG_MARKER_APP1);
 
426
        if (!section) {
 
427
                jpeg_data_append_section (data);
 
428
                memmove (&data->sections[2], &data->sections[1],
 
429
                         sizeof (JPEGSection) * (data->count - 2));
 
430
                section = &data->sections[1];
 
431
        } else {
 
432
                exif_data_unref (section->content.app1);
 
433
        }
 
434
        section->marker = JPEG_MARKER_APP1;
 
435
        section->content.app1 = exif_data;
 
436
        exif_data_ref (exif_data);
 
437
}