Skip to content

Commit 61692c0

Browse files
committed
Expose LibRaw's raw_inset_crops in rawpy's ImageSizes
1 parent 121fb50 commit 61692c0

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

rawpy/_rawpy.pyx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import sys
2020
import warnings
2121
from enum import Enum
2222

23+
cdef extern from "limits.h":
24+
cdef unsigned short USHRT_MAX
25+
2326
cdef extern from "Python.h":
2427
wchar_t* PyUnicode_AsWideCharString(object, Py_ssize_t *)
2528

@@ -61,13 +64,18 @@ cdef extern from "libraw.h":
6164
LIBRAW_IMAGE_JPEG
6265
LIBRAW_IMAGE_BITMAP
6366

67+
ctypedef struct libraw_raw_inset_crop_t:
68+
ushort cleft, ctop
69+
ushort cwidth, cheight
70+
6471
ctypedef struct libraw_image_sizes_t:
6572
ushort raw_height, raw_width
6673
ushort height, width
6774
ushort top_margin, left_margin
6875
ushort iheight, iwidth
6976
double pixel_aspect
7077
int flip
78+
libraw_raw_inset_crop_t[2] raw_inset_crops
7179

7280
ctypedef struct libraw_colordata_t:
7381
float cam_mul[4]
@@ -249,7 +257,9 @@ ImageSizes = namedtuple('ImageSizes', ['raw_height', 'raw_width',
249257
'height', 'width',
250258
'top_margin', 'left_margin',
251259
'iheight', 'iwidth',
252-
'pixel_aspect', 'flip'])
260+
'pixel_aspect', 'flip',
261+
'crop_left_margin', 'crop_top_margin', 'crop_width', 'crop_height'
262+
])
253263

254264
class RawType(Enum):
255265
"""
@@ -568,11 +578,19 @@ cdef class RawPy:
568578
def __get__(self):
569579
self.ensure_unpack()
570580
cdef libraw_image_sizes_t* s = &self.p.imgdata.sizes
581+
582+
# LibRaw returns 65535 for cleft and ctop in some files - probably those that do not specify them
583+
cdef bint has_cleft = s.raw_inset_crops[0].cleft != USHRT_MAX
584+
cdef bint has_ctop = s.raw_inset_crops[0].ctop != USHRT_MAX
585+
571586
return ImageSizes(raw_height=s.raw_height, raw_width=s.raw_width,
572587
height=s.height, width=s.width,
573588
top_margin=s.top_margin, left_margin=s.left_margin,
574589
iheight=s.iheight, iwidth=s.iwidth,
575-
pixel_aspect=s.pixel_aspect, flip=s.flip)
590+
pixel_aspect=s.pixel_aspect, flip=s.flip,
591+
crop_left_margin=s.raw_inset_crops[0].cleft if has_cleft else 0,
592+
crop_top_margin=s.raw_inset_crops[0].ctop if has_ctop else 0,
593+
crop_width=s.raw_inset_crops[0].cwidth, crop_height=s.raw_inset_crops[0].cheight)
576594

577595
property num_colors:
578596
"""

0 commit comments

Comments
 (0)