更新 HEICFile 类以支持新特性,添加图像尺寸获取功能,重构解析逻辑,增强代码可读性和健壮性
This commit is contained in:
+55
-28
@@ -1,10 +1,13 @@
|
||||
# pyheic_struct/heic_file.py
|
||||
|
||||
import os
|
||||
from base import Box
|
||||
from parser import parse_boxes
|
||||
from heic_types import ItemLocationBox
|
||||
from heic_types import ItemLocationBox, PrimaryItemBox, ItemInfoBox
|
||||
from heic_types import (
|
||||
ItemLocationBox, PrimaryItemBox, ItemInfoBox, ItemPropertiesBox,
|
||||
ImageSpatialExtentsBox
|
||||
)
|
||||
|
||||
# Import handlers
|
||||
from handlers.base_handler import VendorHandler
|
||||
from handlers.apple_handler import AppleHandler
|
||||
from handlers.samsung_handler import SamsungHandler
|
||||
@@ -12,34 +15,68 @@ from handlers.samsung_handler import SamsungHandler
|
||||
class HEICFile:
|
||||
def __init__(self, filepath: str):
|
||||
self.filepath = filepath
|
||||
self._iloc_box: ItemLocationBox = None
|
||||
self._ftyp_box: Box = None
|
||||
self._iinf_box: ItemInfoBox = None # <-- Add this line
|
||||
self._pitm_box: PrimaryItemBox = None
|
||||
self.handler: VendorHandler = None # Will hold our strategy object
|
||||
self._iloc_box: ItemLocationBox | None = None
|
||||
self._ftyp_box: Box | None = None
|
||||
self._iinf_box: ItemInfoBox | None = None
|
||||
self._pitm_box: PrimaryItemBox | None = None
|
||||
self._iprp_box: ItemPropertiesBox | None = None
|
||||
self.handler: VendorHandler | None = None
|
||||
|
||||
with open(self.filepath, 'rb') as f:
|
||||
file_size = os.fstat(f.fileno()).st_size
|
||||
self.boxes = parse_boxes(f, file_size)
|
||||
|
||||
self._find_essential_boxes(self.boxes)
|
||||
self._detect_vendor()
|
||||
|
||||
# --- FIX: Changed 'elif' to separate 'if' statements for robustness ---
|
||||
def _find_essential_boxes(self, boxes: list[Box]):
|
||||
"""Recursively search for essential boxes."""
|
||||
for box in boxes:
|
||||
if box.type == 'iloc': self._iloc_box = box
|
||||
if box.type == 'ftyp': self._ftyp_box = box
|
||||
if box.type == 'iinf': self._iinf_box = box # <-- Add this line
|
||||
if box.type == 'pitm': self._pitm_box = box
|
||||
if box.children: self._find_essential_boxes(box.children)
|
||||
|
||||
if isinstance(box, ItemLocationBox):
|
||||
self._iloc_box = box
|
||||
if isinstance(box, ItemInfoBox):
|
||||
self._iinf_box = box
|
||||
if isinstance(box, PrimaryItemBox):
|
||||
self._pitm_box = box
|
||||
if isinstance(box, ItemPropertiesBox):
|
||||
self._iprp_box = box
|
||||
if box.type == 'ftyp':
|
||||
self._ftyp_box = box
|
||||
|
||||
if box.children:
|
||||
self._find_essential_boxes(box.children)
|
||||
|
||||
# All other methods remain unchanged
|
||||
def get_image_size(self, item_id: int) -> tuple[int, int] | None:
|
||||
"""
|
||||
Gets the width and height of a given image item ID.
|
||||
"""
|
||||
if not self._iprp_box or not self._iprp_box.ipma or not self._iprp_box.ipco:
|
||||
print("Warning: 'iprp' box or its sub-boxes ('ipma', 'ipco') not found.")
|
||||
return None
|
||||
|
||||
if item_id not in self._iprp_box.ipma.entries:
|
||||
print(f"Warning: No property association found for item ID {item_id}.")
|
||||
return None
|
||||
|
||||
item_associations = self._iprp_box.ipma.entries[item_id].associations
|
||||
|
||||
for assoc in item_associations:
|
||||
property_index = assoc - 1
|
||||
if property_index < len(self._iprp_box.ipco.children):
|
||||
prop = self._iprp_box.ipco.children[property_index]
|
||||
if isinstance(prop, ImageSpatialExtentsBox):
|
||||
return (prop.image_width, prop.image_height)
|
||||
|
||||
print(f"Warning: 'ispe' property not found for item ID {item_id}.")
|
||||
return None
|
||||
|
||||
def get_primary_item_id(self) -> int | None:
|
||||
if not self._pitm_box:
|
||||
print("Warning: 'pitm' box not found. Cannot determine primary item.")
|
||||
return None
|
||||
return self._pitm_box.item_id
|
||||
|
||||
|
||||
def list_items(self):
|
||||
if not self._iinf_box:
|
||||
print("Warning: 'iinf' box not found. Cannot list items.")
|
||||
@@ -50,9 +87,6 @@ class HEICFile:
|
||||
print(f" - {entry}")
|
||||
|
||||
def _detect_vendor(self):
|
||||
"""
|
||||
Inspects the 'ftyp' box to determine the vendor and assign the correct handler.
|
||||
"""
|
||||
if self._ftyp_box:
|
||||
compatible_brands = self._ftyp_box.raw_data[4:].decode('ascii', errors='ignore')
|
||||
if 'samsung' in compatible_brands.lower() or 'mpvd' in [b.type for b in self.boxes]:
|
||||
@@ -62,13 +96,10 @@ class HEICFile:
|
||||
self.handler = AppleHandler()
|
||||
return
|
||||
|
||||
# If no specific handler is found, use the base one.
|
||||
print("Could not detect a specific vendor. Using default handler.")
|
||||
self.handler = VendorHandler()
|
||||
|
||||
|
||||
def get_item_data(self, item_id: int) -> bytes | None:
|
||||
# ... (this method remains unchanged) ...
|
||||
if not self._iloc_box:
|
||||
print("Error: 'iloc' box not found.")
|
||||
return None
|
||||
@@ -77,20 +108,16 @@ class HEICFile:
|
||||
print(f"Error: Item with ID {item_id} not found in 'iloc' box.")
|
||||
return None
|
||||
with open(self.filepath, 'rb') as f:
|
||||
# IMPORTANT: iloc offsets are absolute from the start of the file.
|
||||
# The mdat box's offset is not needed for this calculation.
|
||||
f.seek(location.offset)
|
||||
data = f.read(location.length)
|
||||
return data
|
||||
|
||||
def get_motion_photo_data(self) -> bytes | None:
|
||||
"""
|
||||
Delegates the search for motion photo data to the assigned vendor handler.
|
||||
"""
|
||||
if not self.handler:
|
||||
self._detect_vendor()
|
||||
offset = self.handler.find_motion_photo_offset(self)
|
||||
if offset is not None:
|
||||
with open(self.filepath, 'rb') as f:
|
||||
# We assume the video data goes to the end of the file from its offset
|
||||
f.seek(offset)
|
||||
return f.read()
|
||||
return None
|
||||
Reference in New Issue
Block a user