Skip to content
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

Handle corrupt files #236

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rawpy/_rawpy.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ IF UNAME_SYSNAME == "Windows":
int unpack() nogil
int unpack_thumb() nogil
int COLOR(int row, int col) nogil
int error_count() nogil
int dcraw_process() nogil
libraw_processed_image_t* dcraw_make_mem_image(int *errcode) nogil
libraw_processed_image_t* dcraw_make_mem_thumb(int *errcode) nogil
Expand All @@ -218,6 +219,7 @@ ELSE:
int unpack() nogil
int unpack_thumb() nogil
int COLOR(int row, int col)
int error_count() nogil
int dcraw_process() nogil
libraw_processed_image_t* dcraw_make_mem_image(int *errcode) nogil
libraw_processed_image_t* dcraw_make_mem_thumb(int *errcode) nogil
Expand Down Expand Up @@ -953,6 +955,12 @@ cdef class RawPy:
else:
raise LibRawNonFatalError(errstr)

cdef int error_count
with nogil:
error_count = self.p.error_count()
letmaik marked this conversation as resolved.
Show resolved Hide resolved
if error_count > 0:
raise LibRawDataError("Data error or unsupported file format")

class DemosaicAlgorithm(Enum):
"""
Identifiers for demosaic algorithms.
Expand Down
Binary file added test/M0054341_01_00005.cr2
Binary file not shown.
14 changes: 12 additions & 2 deletions test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
# Kodak DC50 with special characters in filename
raw6TestPath = os.path.join(thisDir, 'RAW_KODAK_DC50_é.KDC')

# CR2 file with corrupted data
raw7TestPath = os.path.join(thisDir, 'M0054341_01_00005.cr2')

def testVersion():
print('using libraw', rawpy.libraw_version)
pprint(rawpy.flags)
Expand Down Expand Up @@ -267,7 +270,14 @@ def testLibRawOutOfOrderCallError():
with pytest.raises(rawpy.LibRawOutOfOrderCallError):
raw = rawpy.RawPy()
raw.unpack()


def testCorruptFile():
im = rawpy.imread(raw7TestPath)
with pytest.raises(rawpy.LibRawDataError):
im.postprocess()
with pytest.raises(rawpy.LibRawDataError):
im.extract_thumb()

def save(path, im):
# both imageio and skimage currently save uint16 images with 180deg rotation
# as they both use freeimage and this has some weird internal formats
Expand All @@ -289,4 +299,4 @@ def print_stats(rgb):
#testFileOpenAndPostProcess()
#testBadPixelRepair()
testFindBadPixelsNikonD4()

testCorruptFile()
Loading