Mercurial > geeqie.yaz
comparison src/exif-common.c @ 449:115db540bd0c
read color profiles from jpeg also with Exiv2
author | nadvornik |
---|---|
date | Sun, 20 Apr 2008 21:35:03 +0000 |
parents | 4b2d7f9af171 |
children | 48c8e49b571c |
comparison
equal
deleted
inserted
replaced
448:a73cc0fa14d0 | 449:115db540bd0c |
---|---|
385 } | 385 } |
386 if (strcmp(key, "fColorProfile") == 0) | 386 if (strcmp(key, "fColorProfile") == 0) |
387 { | 387 { |
388 const gchar *name = ""; | 388 const gchar *name = ""; |
389 const gchar *source = ""; | 389 const gchar *source = ""; |
390 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile"); | 390 unsigned char *profile_data; |
391 if (!prof_item) | 391 guint profile_len; |
392 profile_data = exif_get_color_profile(exif, &profile_len); | |
393 if (!profile_data) | |
392 { | 394 { |
393 gint cs; | 395 gint cs; |
394 gchar *interop_index; | 396 gchar *interop_index; |
395 | 397 |
396 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */ | 398 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */ |
408 source = (cs == 2) ? "ColorSpace" : "Iop"; | 410 source = (cs == 2) ? "ColorSpace" : "Iop"; |
409 } | 411 } |
410 | 412 |
411 g_free(interop_index); | 413 g_free(interop_index); |
412 } | 414 } |
413 | 415 else |
414 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED) | |
415 { | 416 { |
416 source = _("embedded"); | 417 source = _("embedded"); |
417 #ifdef HAVE_LCMS | 418 #ifdef HAVE_LCMS |
418 | 419 |
419 { | 420 { |
420 unsigned char *data; | |
421 guint data_len; | |
422 cmsHPROFILE profile; | 421 cmsHPROFILE profile; |
423 | 422 |
424 data = (unsigned char *) exif_item_get_data(prof_item, &data_len); | 423 profile = cmsOpenProfileFromMem(profile_data, profile_len); |
425 if (data) | 424 if (profile) |
426 { | 425 { |
427 profile = cmsOpenProfileFromMem(data, data_len); | 426 name = cmsTakeProductName(profile); |
428 if (profile) | 427 cmsCloseProfile(profile); |
429 { | |
430 name = cmsTakeProductName(profile); | |
431 cmsCloseProfile(profile); | |
432 } | |
433 g_free(data); | |
434 } | 428 } |
429 g_free(profile_data); | |
435 } | 430 } |
436 #endif | 431 #endif |
437 } | 432 } |
438 if (name[0] == 0 && source[0] == 0) return NULL; | 433 if (name[0] == 0 && source[0] == 0) return NULL; |
439 return g_strdup_printf("%s (%s)", name, source); | 434 return g_strdup_printf("%s (%s)", name, source); |
490 if (item) return exif_item_get_data_as_text(item); | 485 if (item) return exif_item_get_data_as_text(item); |
491 | 486 |
492 return NULL; | 487 return NULL; |
493 } | 488 } |
494 | 489 |
495 ExifData *exif_read_fd(FileData *fd, gint parse_color_profile) | 490 ExifData *exif_read_fd(FileData *fd) |
496 { | 491 { |
497 GList *work; | 492 GList *work; |
498 gchar *sidecar_path = NULL; | 493 gchar *sidecar_path = NULL; |
499 | 494 |
500 if (!fd) return NULL; | 495 if (!fd) return NULL; |
515 } | 510 } |
516 } | 511 } |
517 | 512 |
518 | 513 |
519 // FIXME: some caching would be nice | 514 // FIXME: some caching would be nice |
520 return exif_read(fd->path, sidecar_path, parse_color_profile); | 515 return exif_read(fd->path, sidecar_path); |
521 } | 516 } |
517 | |
518 | |
519 | |
520 /* embedded icc in jpeg */ | |
521 | |
522 | |
523 #define JPEG_MARKER 0xFF | |
524 #define JPEG_MARKER_SOI 0xD8 | |
525 #define JPEG_MARKER_EOI 0xD9 | |
526 #define JPEG_MARKER_APP1 0xE1 | |
527 #define JPEG_MARKER_APP2 0xE2 | |
528 | |
529 /* jpeg container format: | |
530 all data markers start with 0XFF | |
531 2 byte long file start and end markers: 0xFFD8(SOI) and 0XFFD9(EOI) | |
532 4 byte long data segment markers in format: 0xFFTTSSSSNNN... | |
533 FF: 1 byte standard marker identifier | |
534 TT: 1 byte data type | |
535 SSSS: 2 bytes in Motorola byte alignment for length of the data. | |
536 This value includes these 2 bytes in the count, making actual | |
537 length of NN... == SSSS - 2. | |
538 NNN.: the data in this segment | |
539 */ | |
540 | |
541 gint exif_jpeg_segment_find(unsigned char *data, guint size, | |
542 guchar app_marker, const gchar *magic, guint magic_len, | |
543 guint *seg_offset, guint *seg_length) | |
544 { | |
545 guchar marker = 0; | |
546 guint offset = 0; | |
547 guint length = 0; | |
548 | |
549 while (marker != app_marker && | |
550 marker != JPEG_MARKER_EOI) | |
551 { | |
552 offset += length; | |
553 length = 2; | |
554 | |
555 if (offset + 2 >= size || | |
556 data[offset] != JPEG_MARKER) return FALSE; | |
557 | |
558 marker = data[offset + 1]; | |
559 if (marker != JPEG_MARKER_SOI && | |
560 marker != JPEG_MARKER_EOI) | |
561 { | |
562 if (offset + 4 >= size) return FALSE; | |
563 length += ((guint)data[offset + 2] << 8) + data[offset + 3]; | |
564 } | |
565 } | |
566 | |
567 if (marker == app_marker && | |
568 offset + length < size && | |
569 length >= 4 + magic_len && | |
570 memcmp(data + offset + 4, magic, magic_len) == 0) | |
571 { | |
572 *seg_offset = offset + 4; | |
573 *seg_length = length - 4; | |
574 return TRUE; | |
575 } | |
576 | |
577 return FALSE; | |
578 } | |
579 | |
580 gint exif_jpeg_parse_color(ExifData *exif, unsigned char *data, guint size) | |
581 { | |
582 guint seg_offset = 0; | |
583 guint seg_length = 0; | |
584 guint chunk_offset[255]; | |
585 guint chunk_length[255]; | |
586 guint chunk_count = 0; | |
587 | |
588 /* For jpeg/jfif, ICC color profile data can be in more than one segment. | |
589 the data is in APP2 data segments that start with "ICC_PROFILE\x00\xNN\xTT" | |
590 NN = segment number for data | |
591 TT = total number of ICC segments (TT in each ICC segment should match) | |
592 */ | |
593 | |
594 while (exif_jpeg_segment_find(data + seg_offset + seg_length, | |
595 size - seg_offset - seg_length, | |
596 JPEG_MARKER_APP2, | |
597 "ICC_PROFILE\x00", 12, | |
598 &seg_offset, &seg_length)) | |
599 { | |
600 guchar chunk_num; | |
601 guchar chunk_tot; | |
602 | |
603 if (seg_length < 14) return FALSE; | |
604 | |
605 chunk_num = data[seg_offset + 12]; | |
606 chunk_tot = data[seg_offset + 13]; | |
607 | |
608 if (chunk_num == 0 || chunk_tot == 0) return FALSE; | |
609 | |
610 if (chunk_count == 0) | |
611 { | |
612 guint i; | |
613 | |
614 chunk_count = (guint)chunk_tot; | |
615 for (i = 0; i < chunk_count; i++) chunk_offset[i] = 0; | |
616 for (i = 0; i < chunk_count; i++) chunk_length[i] = 0; | |
617 } | |
618 | |
619 if (chunk_tot != chunk_count || | |
620 chunk_num > chunk_count) return FALSE; | |
621 | |
622 chunk_num--; | |
623 chunk_offset[chunk_num] = seg_offset + 14; | |
624 chunk_length[chunk_num] = seg_length - 14; | |
625 } | |
626 | |
627 if (chunk_count > 0) | |
628 { | |
629 unsigned char *cp_data; | |
630 guint cp_length = 0; | |
631 guint i; | |
632 | |
633 for (i = 0; i < chunk_count; i++) cp_length += chunk_length[i]; | |
634 cp_data = g_malloc(cp_length); | |
635 | |
636 for (i = 0; i < chunk_count; i++) | |
637 { | |
638 if (chunk_offset[i] == 0) | |
639 { | |
640 /* error, we never saw this chunk */ | |
641 g_free(cp_data); | |
642 return FALSE; | |
643 } | |
644 memcpy(cp_data, data + chunk_offset[i], chunk_length[i]); | |
645 } | |
646 if (debug) printf("Found embedded icc profile in jpeg\n"); | |
647 exif_add_jpeg_color_profile(exif, cp_data, cp_length); | |
648 | |
649 return TRUE; | |
650 } | |
651 | |
652 return FALSE; | |
653 } |