@@ -239,6 +239,16 @@ struct gpujpeg_exif_tags {
239239 } tags [2 ]; // 0 - tiff, 1 - exif
240240};
241241
242+ static int
243+ ifd_sort (const void * a , const void * b )
244+ {
245+ const uint8_t * aa = a ;
246+ const uint8_t * bb = b ;
247+ int a_tag_id = aa [0 ] << 8 | aa [1 ];
248+ int b_tag_id = bb [0 ] << 8 | bb [1 ];
249+ return a_tag_id - b_tag_id ;
250+ }
251+
242252/**
243253 * @param tags array of tags, should be ordered awcending according to exif_tiff_tag_info_t.id
244254 */
@@ -253,6 +263,7 @@ gpujpeg_write_ifd(struct gpujpeg_writer* writer, const uint8_t* start, size_t co
253263 uint8_t * end = writer -> buffer_current + EXIF_IFD_NUM_SZ + (count_all * IFD_ITEM_SZ ) + NEXT_IFD_PTR_SZ ;
254264 gpujpeg_writer_emit_2byte (writer , count_all ); // IFD Item Count
255265
266+ uint8_t * first_rec = writer -> buffer_current ;
256267 for ( unsigned i = 0 ; i < count ; ++ i ) {
257268 const struct tag_value * info = & tags [i ];
258269 union value_u value = info -> value ;
@@ -266,6 +277,9 @@ gpujpeg_write_ifd(struct gpujpeg_writer* writer, const uint8_t* start, size_t co
266277 write_exif_tag (writer , custom_tags -> vals [i ].type , custom_tags -> vals [i ].tag_id , custom_tags -> vals [i ].value ,
267278 start , & end );
268279 }
280+ if (custom_tags -> count > 0 ) { // ensure custom_tags are in-ordered
281+ qsort (first_rec , (writer -> buffer_current - first_rec ) /IFD_ITEM_SZ ,IFD_ITEM_SZ , ifd_sort );
282+ }
269283 gpujpeg_writer_emit_4byte (writer , 0 ); // Next IFD Offset (none)
270284 writer -> buffer_current = end ; // jump after the section Value longer than 4Byte of 0th IFD
271285}
@@ -408,4 +422,9 @@ gpujpeg_exif_add_tag(struct gpujpeg_exif_tags** exif_tags, const char* cfg)
408422void
409423gpujpeg_exif_tags_destroy (struct gpujpeg_exif_tags * exif_tags )
410424{
425+ if (exif_tags == NULL ) {
426+ return ;
427+ }
428+ free (exif_tags -> tags [0 ].vals );
429+ free (exif_tags -> tags [1 ].vals );
411430}
0 commit comments