summaryrefslogtreecommitdiff
path: root/libgphoto2/exif.c
blob: 4ed64d7674e20da764f49a83d90348f9b9f726a5 (plain)
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
#include "exif.h"
/*
  EXIF file library for GPHOTO package
  Copyright (C) 1999 Matthew G. Martin

  Generic gphoto implementation and extension by Edouard Lafargue.

  Tag descriptions and an ever-increasing number of structural details
  Taken from "exifdump.py" by Thierry Bousch <bousch@topo.math.u-psud.fr>

  Thanks to Paul Wood <pwood@cs.bris.ac.uk> for sub-ifd parsing.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


/***********
 *  Conventions:
 *
 * "tag number" (tagnum) : ID number of a tag (see list just below).
 * "tag index"  (tagind) : index number of a tag inside an EXIF structure
 *                         (for example, tag number one if the first tag, etc..)
 * "tag name" (tagname)  : name of the tag as defined in the EXIF standard.
 */

int exif_debug=0;

/* Size of various tags */
static int exif_sizetab[13]={
  0,1,1,2,4,8,1,1,2,4,8,4,8
};

/**************************************************************/
/* Foward declarations. move to exif.h if you want to export. */
/**************************************************************/
int gpe_getvalue(unsigned char *data,int tagind);
int gpe_datsize(unsigned char *data,int tagind);
int gpe_tagnum( char *data,int tagind);
int gpe_getintval(unsigned char *data, int tagnum);

/**************************************************************/
/*  Utility functions: get fields in little-endian format,    */
/* initialize data structures, etc.                           */
/**************************************************************/

/**
 * Convert to Intel-Endian number.
 *
 * data : pointer to the data to convert
 * size : size of the data to convert
 *
 * Returns : the value
 */
long exif_get_lilend(unsigned char *data, int size){
  long total;

  total=0;
  for (--size;size>=0;size--){
    total<<=8;
    total+=data[size];
  };

  return(total);
};

/**
 * Convert to  signed Intel-Endian number
 */
long exif_get_slilend(unsigned char *data, int size){
  long total;
  long unsigned mask=1<<(size*8-1);

  total=0;
  for (--size;size>=0;size--){
    total<<=8;
    total+=data[size];
  };

  if (total&mask) total-=mask;

  return(total);
};


/**
 *  Return "value" of directory entry at tagind
 */
int gpe_theval(unsigned char *data,int tagind){
 return(exif_get_lilend(data+tagind*12+10,4));
};


/**
 *  Set the "value" of directory entry at tagind.
 */
void gpe_setval(unsigned char *data,int tagind,long newval){
  int i;
  for (i=0;i<4;i++) data[tagind*12+10+i]=0xff&(newval>>(i*8));
  if (gpe_getvalue(data,tagind)!=newval) 
    printf("Setptr: error %d inst %ld\n",gpe_theval(data,tagind),newval);
};


/*
 * Returns the offset of the next IFD entry after
 * offset "num".
 *
 * exif : exif data
 * num  : start offset
 *
 * returns: offset of next IFD.
 */
long exif_next_ifd(unsigned char *exif,int num){

  int offset=(exif[num]+(exif[num+1]<<8))*12+num+2;
  if (exif_debug) printf("next_ifd,offset=%d\n",offset);
  return(exif_get_lilend(exif+offset,4));

}


/**
 * Parse an exif data structure and initialise the "exifparser".
 *
 * exifdat : the exif data
 *
 * returns : size of the rest of the file.
 *           -1 if failed.
 */
int exif_parse_data(exifparser *exifdat) {
  long offset=0;
  ExifData tagdat;

  exif_debug=1;
  /* Check that it's not already preparsed */
  // Doesn't work if we don't initialize this first...
  //  if (exifdat->preparsed) return 0;
  if (exif_debug) printf("Parsing exif structure\n");

  /* First, verify that we really have an EXIF file */
  /* Note: maybe we could also generalize to TIFF... */
  if ((strncmp("Exif",exifdat->header+6,4)) ||
      strncmp("\377\330\377\341",exifdat->header,4)){
    fprintf(stderr,"Not exif data\n");
    return(-1);
  }

  exifdat->exiflen = exifdat->header[5] + (exifdat->header[4]<<8) - 8;
  if (exif_debug) printf("Exif length is %i\n",exifdat->exiflen);

  /* Count the number of IFD's and register their pointers. */
  exifdat->endian=0;
  if (exifdat->data[0]!='I') {
    exifdat->endian=1;
    printf("%c,Intel-Endian format only supported right now!\n",
	   exifdat->data[0]);
    return(-1);
  };

  offset=exif_get_lilend(exifdat->data+4,4); /*Get offset of first IFD */
  exifdat->ifdcnt=-1;

  /*Step through each IFD */
  do {
    exifdat->ifdcnt++;
    exifdat->ifds[exifdat->ifdcnt]     = exifdat->data+offset;
    exifdat->ifdtags[exifdat->ifdcnt]  = exif_get_lilend(exifdat->data+offset,2);
  } while ((offset=exif_next_ifd(exifdat->data,offset)));

  exifdat->ifdcnt++;

  /* Look for the EXIF IFD */
  exifdat->preparsed=1; /* Indicate that our exifparser is initialized */
  /* Has to be done before calling exif_get_field */
  if ( ! exif_get_field( EXIF_ExifOffset, 0, exifdat, &tagdat)) {
    printf("No EXIF information stored in this image\n");
  } else {
    if (exif_debug) printf("Offset to the Exif subIFD is %d\n",tagdat.intval);
    exifdat->ifds[exifdat->ifdcnt] = exifdat->data + tagdat.intval;
    exifdat->ifdtags[exifdat->ifdcnt]  = exif_get_lilend(exifdat->data+tagdat.intval,2);
    exifdat->ifdcnt++;
  }

  if (exif_debug) printf("Finished parsing exif structure\n");
  return exifdat->exiflen;
}

/**************************************************************/
/* tag-level functions: get/set tags in the EXIF structure    */
/**************************************************************/

/**
 * Returns the value of a field, identified by its tag and the IFD.
 *
 * Specifying an IFD of '-1' will return the first occurence of
 * the tag in the first IFD where it finds it.
 *
 * tag_number: tag that identifies the field
 * ifd       : image file directory where to look for that tag
 * exifdata  : pointer to the EXIF data
 * tag_data  : filled by this function, contains the actual data
 *
 * returns   : 1 on success, 0 on failure
 */
int exif_get_field( int tag_number, int ifd, exifparser *exifdata, ExifData *tag_data) {
  int numtags,i, tag;
  char *ifdp, *data;

  // Sanity check first:
  exif_debug=1;
  if ( !exifdata->preparsed ) {
    if(exif_parse_data(exifdata) < 0)
      return 0;
  }

  if (ifd == -1) {
    for (i = 0; i < exifdata->ifdcnt+1; i++){
      if (exif_debug) printf("Searching in IFD %d\n",i);
      if ( exif_get_field(tag_number, i, exifdata, tag_data) == 1)
	return 1;
    }
    return 0; // We did not find it.
  } else {

    // Find tag in a specific IFD
    ifdp = exifdata->ifds[ifd];
    numtags=exif_get_lilend(ifdp,2); // How many tags in the IFD
    if (exif_debug)  printf("exif_get_field: %d tags in ifd %d\n",numtags, ifd);
    i=-1;
    do {
      i++;
      tag = exif_get_lilend(ifdp+i*12+2, 2); // Get the tag ID
      // if (exif_debug) fprintf(stderr,"Found tag %d \n",tag);
    } while ((i < numtags) && (tag != tag_number));
    
    if(tag != tag_number) {
      if (exif_debug) fprintf(stderr,"Tag %d not found\n",tag_number);
      return 0;
    }
    
    ifdp = ifdp+i*12+2; // Place pointer at TAG type position
    tag_data->tag = tag;
    tag_data->type = exif_get_lilend(ifdp+2,2);    /* tag type */
    tag_data->size = exif_sizetab[tag_data->type]*exif_get_lilend(ifdp+4,4);
    if (exif_debug) printf("(%d bytes) ",tag_data->size);

    ifdp += 8; // place pointer at beginning of the contents of the field

    /* Data types smaller than 4 bytes are stored directly in
       the IFD field value. Otherwise, that value is an offset
       to the real data */
    if (tag_data->size > 4) {
	ifdp = exifdata->data + exif_get_lilend(ifdp,4);
    }

    /* Last operation, get the data itself: */
    /* Allocate memory, and copy the data */
      data=malloc(tag_data->size);
      if (data==NULL){
	fprintf(stderr,"exif_get_field: could not malloc\n");
	return 0;
      };

    if (tag_data->type == EXIF_ASCII) {
	memcpy(data,ifdp,tag_data->size);  // Normally, the exif data includes a terminating 0
	tag_data->data = data;
	if(exif_debug) printf("\"%s\"",data);
    } else {
      for (i = 0; i < tag_data->size; i += exif_sizetab[tag_data->type]) {
	//if (exif_debug) fprintf(stderr,".");
	if ( tag_data->type % 5 ) {
	  memcpy(data+i,ifdp+i,exif_sizetab[tag_data->type]);	  
	} else { // Fraction
	    tag_data->num =exif_get_lilend(ifdp+i,4);
	    tag_data->den =exif_get_lilend(ifdp+i+4,4);
	  
	  if(exif_debug)  printf("%d/%d=%.3g ",tag_data->num,
		                  tag_data->den,
				 (tag_data->den==0)?0:(1.0*tag_data->num/tag_data->den));
	}	
      }
      // If the value can be put into an int, save the trouble and store
      // it into "intval" right away...
      if(tag_data->type != EXIF_ASCII && tag_data->type != EXIF_RATIONAL &&
	 tag_data->type != EXIF_UNDEFINED && tag_data->type != EXIF_SRATIONAL) {
	tag_data->intval = exif_get_lilend(data,exif_sizetab[tag_data->type]);
	if(exif_debug) printf("'%d'",tag_data->intval);
      }
      tag_data->data = data; // Save the data field
    }
    if (exif_debug) printf("\n"); /*end of this tag */
    return 1;
  }
  return 0;
}

/**
 * Gets an ASCII tag.
 *
 * Specifying an IFD of '-1' will return the first occurence of
 * the tag in the first IFD where it finds it.
 *
 * tag_number: tag that identifies the field
 * ifd       : image file directory where to look for that tag
 * exifdata  : pointer to the EXIF data
 *
 * returns   : pointer to the tag value.
 */
char * exif_get_ascii_field( int tag_number, int ifd, exifparser *exifdat) {
  ExifData tagdat;

  if (exif_parse_data(exifdat)<0) return NULL;

  if ( ! exif_get_field( tag_number, ifd, exifdat, &tagdat)) {
    if (exif_debug) printf("No comment field in this image\n");
    return NULL;
  } else {
    return tagdat.data;
  }
}

/**
 * Gets a numeric tag.
 *
 * Specifying an IFD of '-1' will return the first occurence of
 * the tag in the first IFD where it finds it.
 *
 * tag_number: tag that identifies the field
 * ifd       : image file directory where to look for that tag
 * exifdata  : pointer to the EXIF data
 *
 * returns   : tag value.
 */
int exif_get_int_field( int tag_number, int ifd, exifparser *exifdat) {
  ExifData tagdat;
  int tmp;

  if (exif_parse_data(exifdat)<0) return 0;

  if ( ! exif_get_field( tag_number, ifd, exifdat, &tagdat)) {
    if (exif_debug) printf("Field not found in this image.\n");
    return 0;
  } else {
    tmp = tagdat.intval;
    free(tagdat.data);   // ???
    return tmp;
  }  
  return 0;

}


/**
 * Returns the name of a given tag number
 */
char *exif_get_tagname(int tag_number) {
  int i=-1;
  do {
    i++;
    if (tagnames[i].num == tag_number) return (tagnames[i].desc);
  } while (tagnames[i].num);
  return("Unknown");  
}

/**************************************************************/
/* Higher-level functions: get/set logical entities, such as  */
/* comments, thumbnail, etc.                                  */
/**************************************************************/

/**
 * Gets the comment field if it exists.
 */
int exif_get_comment(exifparser *exifdat, char *comment) {
  ExifData tagdat;

  if (exif_parse_data(exifdat)<0) return 0;

  /* User Comment is in IFD number 2 */
  if ( ! exif_get_field( EXIF_UserComment, 2, exifdat, &tagdat)) {
    if (exif_debug) printf("No comment field in this image\n");
    return 0;
  } else {
    comment = tagdat.data;
    return tagdat.size;
  }  
  return 0;
}

/**
 * Sets the comment field if it exists.
 */
int exif_set_comment(exifparser *exifdat, char *comment) {
  return 0;
}

/**
 * Gets the thumbnail of an EXIF image.
 *
 * The returned thumbnail might be in JPEG or TIFF format.
 */
unsigned char *exif_get_thumbnail(exifparser *exifdat) {
  unsigned char *imagedata,*exifimg,*newimg,*curptr;
  unsigned int entry;
  long dataptr,dsize,tag,datvec,tmp;
  int i,j;

  exif_debug=1;
  if (exif_parse_data(exifdat)<0) return(NULL);

  newimg=malloc(exifdat->exiflen);
  if (newimg==NULL){
    fprintf(stderr,"exif_get_thumbnail: could not malloc\n");
    return(NULL);
  };

  /* Copy header*/
  memcpy(newimg,exifdat->data,8);
  curptr=newimg+8;

  if (exif_debug) {    
      ExifData owner;
      char *comment=NULL;
      printf("Decoding EXIF fields in thumbnail\n");
      exif_get_field( EXIF_Model, -1, exifdat, &owner);
      printf("Camera model: %s\n",owner.data);
      printf("Comment for this picture (%d chars)",exif_get_comment( exifdat, comment));
      //printf(" -> %s\n",comment); Not OK on Linux
      exif_get_field( EXIF_SubjectDistance, 2, exifdat, &owner);
      //      dump_exif(exifdat);      
  };

  /* Skip to Thumbnail image data */
  if(exifdat->ifdcnt<2) {
    if (exif_debug) {
      fprintf(stderr,"Too few ifds, doesn't look right. Giving up\n");
    };
    return(NULL); /* Things don't look right...*/
  };

  /* Jump to thumbnail image data */
  exifimg=exifdat->ifds[1];

  /* Copy number of entries */
  memcpy(curptr,exifimg,2);
  curptr+=2;

  entry=exif_get_lilend(exifimg,2);

  if (exif_debug) printf("Entry is %d \n",entry);

  /* See if thumb is a JPEG */
  tmp=gpe_getintval(exifimg,EXIF_JPEGInterchangeFormat); /*imagedata start*/
  if (tmp>0) { /* jpeg image */
    if (exif_debug) fprintf(stderr,"Found jpeg thumb data\n");
    dsize=gpe_getintval(exifimg,EXIF_JPEGInterchangeFormatLength);
    if (dsize==-1){
      fprintf(stderr,"No Jpeg size tag for thumbnail, skipping\n");
      return(NULL);
    };
    imagedata=exifdat->data+tmp;
    memcpy(newimg,imagedata,dsize);
    return(newimg);
  };

  /* Try a TIFF */
  tmp=gpe_getintval(exifimg,EXIF_StripOffsets); /*imagedata start*/
  if (tmp==-1) {
    fprintf(stderr,"gpe_get_thumbnail: Tiff or jpeg data not found, skipping\n");
    return(NULL);
  };
  imagedata=exifdat->data+tmp;

  dataptr=gpe_getintval(exifimg,EXIF_StripByteCounts);        /* imagedata size */
  if (dataptr==-1) {
    printf("Split two\n");
    return(NULL);
  };

  if (exif_debug) printf("Imagedata size is %ld bytes\n",dataptr);

  for (i=0;i<entry;i++){
    dsize=gpe_datsize(exifimg,i);
    tag=gpe_tagnum(exifimg,i);

    /*
      if (exif_debug) printf("Datsize %d (tag=%ld) is %ld\n",i,tag,dsize);
    */

    if (tag==EXIF_StripOffsets) {
      gpe_setval(exifimg,i,12*entry+14); /* set to end of directory */
      memcpy(curptr,exifimg+12*i+2,12);
      curptr+=12;
    }
    else {
      if (dsize<5){
	/* Just copy the field if small */
        memcpy(curptr,exifimg+12*i+2,12);
	curptr+=12;
      }
      else {
	datvec=gpe_getvalue(exifimg,i);
	gpe_setval(exifimg,i,dataptr+12*entry+14);
	for (j=0;j<dsize;j++) imagedata[dataptr++]=exifdat->data[datvec+j];
        memcpy(curptr,exifimg+12*i+2,12);
	curptr+=12;
      };
    };
  };
  memcpy(curptr,exifimg+12*entry+10,4); /* Write 4 zero bytes */
  curptr+=4;
  memcpy(curptr,imagedata,dataptr);/* ? */
  curptr+=dataptr;
  return newimg;

  return 0;
}

/**
 * Return tag number of directory entry at tag index
 *
 * data   : pointer to EXIF data, aligned to the beginning of an IFD
 * tagind : index of the tag within the IFD
 *
 * returns: tag ID number
 */
int gpe_tagnum( char *data,int tagind){
 return(exif_get_lilend(data+tagind*12+2,2));
};

/*
 * Get the value of a tag as an integer.
 * only works for 4 byte values.
 *
 * data : exif data, aligned to the beginning of an IFD.
 * tagnum : tag number.
 *
 * returns: -1 if the tag is not found.
 */
int gpe_getintval(unsigned char *data, int tagnum) {
  int numtags,i,tag,tagtype;

  numtags=exif_get_lilend(data,2);

  if (exif_debug)  printf("getval:%d tags\n",numtags);

 i=-1;
 do {
   i++;
   tag=exif_get_lilend(data+i*12+2,2);
 } while ((i<numtags) && (tag!=tagnum));

 if(tag!=tagnum) {
   if (exif_debug) fprintf(stderr,"Tag %d not found\n",tagnum);
   return(-1);
 };

 tagtype=exif_get_lilend(data+i*12+4,2);    /* tag type */

 return(exif_get_lilend(data+i*12+10,exif_sizetab[tagtype-1]));
};

/**
 *  Return "value" of directory entry at tagind
 *
 * data  : exif data
 * tagind: tag index
 */
int gpe_getvalue(unsigned char *data,int tagind){
 return(exif_get_lilend(data+tagind*12+10,4));
};


/**
 * Add all tag names and values in an ifd to an array of strings for gphoto
 */
//int gpe_exif_add_all(exifparser *exifdata,int ifdnum,char **datastrings){
//  int i;
//  for (i=0;i<exifdata->ifdtags[ifdnum];i++){
//    togphotostr(exifdata,ifdnum,i,datastrings+i*2,datastrings+i*2+1);
//    /*    printf("%s = %s\n",datastrings[i*2],datastrings[i*2+1]);*/
//  };
//};



int gpe_dump_ifd(int ifdnum,exifparser *exifdata,char **allpars){
  int i,j,tag,numtags,tagtype,count,typelen, value=-1,tmp1,tmp2;
  char tmpstr[256];
  unsigned char* thistag;
  unsigned char* thedata;
  unsigned char* thisisd;
  
  
    thisisd=exifdata->ifds[ifdnum];
    numtags=exif_get_lilend(thisisd,2);
    printf("has %d tags ----------------------\n",numtags);
    for (i=0;i<numtags;i++){

      thistag=thisisd+i*12+2;   /* pointer to data for this tag */

      tag=exif_get_lilend(thistag,2);          /* tag identifier */
      tagtype=exif_get_lilend(thistag+2,2);    /* tag type */
      count = exif_get_lilend(thistag+4, 4);   /* how many */
      typelen=exif_sizetab[tagtype];/* length of this type */

      if (exif_debug) printf("(%dX) ",count);

      thedata=thistag+8;
      if (count*typelen > 4)   /* find it in a data block elsewhere */
	thedata = exifdata->data+exif_get_lilend(thistag+8, 4);

      printf("Tag 0x%X %s = ",tag,exif_get_tagname(tag));

      if (tagtype==2) {
	/* Do an ASCII tag */
	  strncpy(tmpstr,thedata,count+1);
	  tmpstr[count+1]='\0';
	  printf("'%s'",tmpstr);
	} 
      else for (j=0;j<count;j++) {

	if ((tagtype==5)||(tagtype==10)) {/* Fractional */
	  tmp1=exif_get_slilend(thedata+j*typelen,4);
	  tmp2=exif_get_slilend(thedata+4+j*typelen,4);
	      printf("%d/%d=%.3g ",tmp1,tmp2,(tmp2==0)?0:(1.0*tmp1/tmp2));
	}
	else {
	  value =exif_get_lilend(thedata+j*typelen,typelen);
	  printf("%d ",value);
	};
	
      };

      printf("\n"); /*end of this tag */

/* Print SubIfd tags */
      if ( tag == 0x8769 ) {
         printf("Exif SubIFD at offset %d\n", value );
         exifdata->ifds[exifdata->ifdcnt]     = exifdata->data+value;  
//       exifdata->ifds[exifdata->ifdcnt];
         exifdata->ifdtags[exifdata->ifdcnt]=exif_get_lilend(exifdata->data+value,2);
         exifdata->ifdcnt++;

}
/***/

    };
    return 1;
};



/* Walk through EXIF structure, printing values */
int gpe_dump_exif(exifparser *exifdata){ 
  int i;

  if (!exifdata->preparsed) 
    if (stat_exif(exifdata)) return(-1);

  for (i=0;i<exifdata->ifdcnt;i++){
    switch (i) {
       case 0:      
          printf("IFD %d, %s ",i,"Main Image");
          break;
       case 1:
          printf("IFD %d, %s ",i,"Thumbnail");
         break;
       case 2:
          printf("IFD %d, %s ",i,"Sub IFD");
          break;
   
   }       
       

    gpe_dump_ifd(i,exifdata,NULL);
  };
  return 1;
};

/**
 * Return data size of directory entry at tagind
 */
int gpe_datsize(unsigned char *data,int tagind){
  return(exif_sizetab[exif_get_lilend(data+tagind*12+4,2)]*exif_get_lilend(data+tagind*12+6,4));
};

int stat_exif(exifparser *exifdata) {
  long offset=0;

  exifdata->endian=0;
  if (exifdata->data[0]!='I') {
    exifdata->endian=1;
    printf("%c,Intel-Endian format only supported right now!\n",
           exifdata->data[0]);
    return(-1);
  };

  offset=exif_get_lilend(exifdata->data+4,4); /*Get offset of first IFD*/

  exifdata->ifdcnt=-1;

  /*Step through each IFD (looks like there should be 2 or 3)*/
  do{
    exifdata->ifdcnt++;
    exifdata->ifds[exifdata->ifdcnt]     = exifdata->data+offset;
    exifdata->ifdtags[exifdata->ifdcnt]  = 
    exif_get_lilend(exifdata->data+offset,2);

  }while((offset=exif_next_ifd(exifdata->data,offset)));
  exifdata->ifdcnt++;

  exifdata->preparsed=1;
  return(0);
};