Skip to content

Commit

Permalink
Handle corrupt files (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
mplough-kobold authored Sep 12, 2024
1 parent da5ad74 commit d44352e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
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()
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()

0 comments on commit d44352e

Please sign in to comment.