-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add draft support to retrieve Exif from HEIF file #13443
Open
benstonezhang
wants to merge
1
commit into
php:master
Choose a base branch
from
benstonezhang:exif_heif
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1284,6 +1284,18 @@ typedef struct { | |
mn_offset_mode_t offset_mode; | ||
} maker_note_type; | ||
|
||
#define FOURCC(id) (((uint32_t)(id[0])<<24) | (id[1]<<16) | (id[2]<<8) | (id[3])) | ||
|
||
typedef struct { | ||
uint64_t size; | ||
uint32_t type; | ||
} isobmff_box_type; | ||
|
||
typedef struct { | ||
uint32_t offset; | ||
uint32_t size; | ||
} isobmff_item_pos_type; | ||
|
||
/* Some maker notes (e.g. DJI info tag) require custom parsing */ | ||
#define REQUIRES_CUSTOM_PARSING NULL | ||
|
||
|
@@ -4285,11 +4297,128 @@ static bool exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offs | |
return result; | ||
} | ||
|
||
static int exif_isobmff_parse_box(unsigned char *buf, isobmff_box_type *box) | ||
{ | ||
box->size = php_ifd_get32u(buf, 1); | ||
buf += 4; | ||
box->type = php_ifd_get32u(buf, 1); | ||
if (box->size != 1) { | ||
return 8; | ||
} | ||
buf += 4; | ||
box->size = php_ifd_get64u(buf, 1); | ||
return 16; | ||
} | ||
|
||
static void exif_isobmff_parse_meta(unsigned char *data, unsigned char *end, isobmff_item_pos_type *pos) | ||
{ | ||
isobmff_box_type box, item; | ||
unsigned char *box_offset, *p, *p2; | ||
int header_size, exif_id = -1, version, item_count, i; | ||
|
||
for (box_offset = data + 4; box_offset < end; box_offset += box.size) { | ||
header_size = exif_isobmff_parse_box(box_offset, &box); | ||
if (box.type == FOURCC("iinf")) { | ||
p = box_offset + header_size; | ||
version = p[0]; | ||
p += 4; | ||
if (version < 2) { | ||
item_count = php_ifd_get16u(p, 1); | ||
p += 2; | ||
} else { | ||
item_count = php_ifd_get32u(p, 1); | ||
p += 4; | ||
} | ||
for (i=0; i < item_count; i++) { | ||
header_size = exif_isobmff_parse_box(p, &item); | ||
if (!memcmp(p + header_size + 8, "Exif", 4)) { | ||
exif_id = php_ifd_get16u(p + header_size + 4, 1); | ||
break; | ||
} | ||
p += item.size; | ||
} | ||
if (exif_id < 0) { | ||
break; | ||
} | ||
} | ||
else if (box.type == FOURCC("iloc")) { | ||
p = box_offset + header_size; | ||
version = p[0]; | ||
p += 6; | ||
if (version < 2) { | ||
item_count = php_ifd_get16u(p, 1); | ||
p += 2; | ||
} else { | ||
item_count = php_ifd_get32u(p, 1); | ||
p += 4; | ||
} | ||
for (i=0, p2=p; i<item_count; i++, p2 += 16) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CS: missing spaces before and after |
||
if (php_ifd_get16u(p2, 1) == exif_id) { | ||
pos->offset = php_ifd_get32u(p2 + 8, 1); | ||
pos->size = php_ifd_get32u(p2 + 12, 1); | ||
break; | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
static bool exif_scan_HEIF_header(image_info_type *ImageInfo, unsigned char *buf) | ||
{ | ||
isobmff_box_type box; | ||
isobmff_item_pos_type pos; | ||
unsigned char *data; | ||
off_t offset; | ||
uint64_t limit; | ||
int box_header_size, remain; | ||
bool ret = false; | ||
|
||
pos.size = 0; | ||
for (offset = php_ifd_get32u(buf, 1); ImageInfo->FileSize > offset + 16; offset += box.size) { | ||
if ((php_stream_seek(ImageInfo->infile, offset, SEEK_SET) < 0) || | ||
(exif_read_from_stream_file_looped(ImageInfo->infile, (char*)buf, 16) != 16)) { | ||
break; | ||
} | ||
box_header_size = exif_isobmff_parse_box(buf, &box); | ||
if (box.type == FOURCC("meta")) { | ||
limit = box.size - box_header_size; | ||
data = (unsigned char *)emalloc(limit); | ||
remain = 16 - box_header_size; | ||
if (remain) { | ||
memcpy(data, buf + box_header_size, remain); | ||
} | ||
if (exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(data + remain), limit - remain) == limit - remain) { | ||
exif_isobmff_parse_meta(data, data + limit, &pos); | ||
} | ||
if ((pos.size) && | ||
(ImageInfo->FileSize >= pos.offset + pos.size) && | ||
(php_stream_seek(ImageInfo->infile, pos.offset + 2, SEEK_SET) >= 0)) { | ||
if (limit >= pos.size - 2) { | ||
limit = pos.size - 2; | ||
} else { | ||
limit = pos.size - 2; | ||
efree(data); | ||
data = (unsigned char *)emalloc(limit); | ||
} | ||
if (exif_read_from_stream_file_looped(ImageInfo->infile, (char*)data, limit) == limit) { | ||
exif_process_APP1(ImageInfo, (char*)data, limit, pos.offset + 2); | ||
ret = true; | ||
} | ||
} | ||
efree(data); | ||
break; | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
/* {{{ exif_scan_FILE_header | ||
* Parse the marker stream until SOS or EOI is seen; */ | ||
static bool exif_scan_FILE_header(image_info_type *ImageInfo) | ||
{ | ||
unsigned char file_header[8]; | ||
unsigned char file_header[16]; | ||
bool ret = false; | ||
|
||
ImageInfo->FileType = IMAGE_FILETYPE_UNKNOWN; | ||
|
@@ -4338,6 +4467,16 @@ static bool exif_scan_FILE_header(image_info_type *ImageInfo) | |
} else { | ||
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file"); | ||
} | ||
} else if ((ImageInfo->FileSize > 12) && | ||
(!memcmp(file_header + 4, "ftyp", 4)) && | ||
(exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(file_header + 8), 4) == 4) && | ||
((!memcmp(file_header + 8, "heic", 4)) || (!memcmp(file_header + 8, "heix", 4)) || (!memcmp(file_header + 8, "mif1", 4)))) { | ||
if (exif_scan_HEIF_header(ImageInfo, file_header)) { | ||
ImageInfo->FileType = IMAGE_FILETYPE_HEIF; | ||
ret = true; | ||
} else { | ||
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid HEIF file"); | ||
} | ||
} else { | ||
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File not supported"); | ||
return false; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
--TEST-- | ||
Check for exif_read_data, HEIF with IFD0 and EXIF data in Motorola byte-order. | ||
--EXTENSIONS-- | ||
exif | ||
--INI-- | ||
output_handler= | ||
zlib.output_compression=0 | ||
--FILE-- | ||
<?php | ||
var_dump(exif_read_data(__DIR__.'/../../standard/tests/image/test4pix.heic')); | ||
?> | ||
--EXPECTF-- | ||
array(53) { | ||
["FileName"]=> | ||
string(13) "test4pix.heic" | ||
["FileDateTime"]=> | ||
int(%d) | ||
["FileSize"]=> | ||
int(42199) | ||
["FileType"]=> | ||
int(20) | ||
["MimeType"]=> | ||
string(10) "image/heif" | ||
["SectionsFound"]=> | ||
string(19) "ANY_TAG, IFD0, EXIF" | ||
["COMPUTED"]=> | ||
array(3) { | ||
["IsColor"]=> | ||
int(0) | ||
["ByteOrderMotorola"]=> | ||
int(1) | ||
["ApertureFNumber"]=> | ||
string(5) "f/1.8" | ||
} | ||
["Make"]=> | ||
string(5) "Apple" | ||
["Model"]=> | ||
string(26) "iPhone SE (3rd generation)" | ||
["Orientation"]=> | ||
int(1) | ||
["XResolution"]=> | ||
string(4) "72/1" | ||
["YResolution"]=> | ||
string(4) "72/1" | ||
["ResolutionUnit"]=> | ||
int(2) | ||
["Software"]=> | ||
string(6) "17.2.1" | ||
["DateTime"]=> | ||
string(19) "2024:02:21 16:03:50" | ||
["HostComputer"]=> | ||
string(26) "iPhone SE (3rd generation)" | ||
["TileWidth"]=> | ||
int(512) | ||
["TileLength"]=> | ||
int(512) | ||
["Exif_IFD_Pointer"]=> | ||
int(264) | ||
["ExposureTime"]=> | ||
string(4) "1/60" | ||
["FNumber"]=> | ||
string(3) "9/5" | ||
["ExposureProgram"]=> | ||
int(2) | ||
["ISOSpeedRatings"]=> | ||
int(200) | ||
["ExifVersion"]=> | ||
string(4) "0232" | ||
["DateTimeOriginal"]=> | ||
string(19) "2024:02:21 16:03:50" | ||
["DateTimeDigitized"]=> | ||
string(19) "2024:02:21 16:03:50" | ||
["UndefinedTag:0x9010"]=> | ||
string(6) "+08:00" | ||
["UndefinedTag:0x9011"]=> | ||
string(6) "+08:00" | ||
["UndefinedTag:0x9012"]=> | ||
string(6) "+08:00" | ||
["ShutterSpeedValue"]=> | ||
string(12) "159921/27040" | ||
["ApertureValue"]=> | ||
string(11) "54823/32325" | ||
["BrightnessValue"]=> | ||
string(11) "29968/13467" | ||
["ExposureBiasValue"]=> | ||
string(3) "0/1" | ||
["MeteringMode"]=> | ||
int(5) | ||
["Flash"]=> | ||
int(16) | ||
["FocalLength"]=> | ||
string(7) "399/100" | ||
["SubjectLocation"]=> | ||
array(4) { | ||
[0]=> | ||
int(1995) | ||
[1]=> | ||
int(1507) | ||
[2]=> | ||
int(2217) | ||
[3]=> | ||
int(1332) | ||
} | ||
["MakerNote"]=> | ||
string(9) "Apple iOS" | ||
["SubSecTimeOriginal"]=> | ||
string(3) "598" | ||
["SubSecTimeDigitized"]=> | ||
string(3) "598" | ||
["ColorSpace"]=> | ||
int(65535) | ||
["ExifImageWidth"]=> | ||
int(4032) | ||
["ExifImageLength"]=> | ||
int(3024) | ||
["SensingMethod"]=> | ||
int(2) | ||
["SceneType"]=> | ||
string(1) "" | ||
["ExposureMode"]=> | ||
int(0) | ||
["WhiteBalance"]=> | ||
int(0) | ||
["DigitalZoomRatio"]=> | ||
string(7) "756/151" | ||
["FocalLengthIn35mmFilm"]=> | ||
int(140) | ||
["UndefinedTag:0xA432"]=> | ||
array(4) { | ||
[0]=> | ||
string(15) "4183519/1048501" | ||
[1]=> | ||
string(15) "4183519/1048501" | ||
[2]=> | ||
string(3) "9/5" | ||
[3]=> | ||
string(3) "9/5" | ||
} | ||
["UndefinedTag:0xA433"]=> | ||
string(5) "Apple" | ||
["UndefinedTag:0xA434"]=> | ||
string(51) "iPhone SE (3rd generation) back camera 3.99mm f/1.8" | ||
["UndefinedTag:0xA460"]=> | ||
int(2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it supposed to be always little endian for heif?