Skip to content

Commit 3fc1731

Browse files
authored
Expose crop dimensions (#252)
1 parent 121fb50 commit 3fc1731

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

.github/scripts/build-windows.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Get-ChildItem env:
119119

120120
# Install vcpkg and build dependencies
121121
if (!(Test-Path ./vcpkg)) {
122-
exec { git clone https://github.com/microsoft/vcpkg -b 2023.11.20 --depth 1}
122+
exec { git clone https://github.com/microsoft/vcpkg -b 2025.01.13 --depth 1}
123123
exec { ./vcpkg/bootstrap-vcpkg }
124124
}
125125
exec { ./vcpkg/vcpkg install zlib libjpeg-turbo[jpeg8] jasper lcms --triplet=x64-windows-static --recurse }

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
"""

test/test_basic.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,38 @@ def testVisibleSize():
228228
assert_equal(h, s.height)
229229
assert_equal(w, s.width)
230230

231+
def testCropSizeNikon():
232+
with rawpy.imread(rawTestPath) as raw:
233+
s = raw.sizes
234+
assert_equal(s.crop_left_margin, 0)
235+
assert_equal(s.crop_top_margin, 0)
236+
assert_equal(s.crop_width, 0)
237+
assert_equal(s.crop_height, 0)
238+
239+
def testCropSizeCanon():
240+
with rawpy.imread(raw3TestPath) as raw:
241+
s = raw.sizes
242+
assert_equal(s.crop_left_margin, 168)
243+
assert_equal(s.crop_top_margin, 56)
244+
assert_equal(s.crop_width, 5616)
245+
assert_equal(s.crop_height, 3744)
246+
247+
def testCropSizeSigma():
248+
with rawpy.imread(raw4TestPath) as raw:
249+
s = raw.sizes
250+
assert_equal(s.crop_left_margin, 0)
251+
assert_equal(s.crop_top_margin, 0)
252+
assert_equal(s.crop_width, 0)
253+
assert_equal(s.crop_height, 0)
254+
255+
def testCropSizeKodak():
256+
with rawpy.imread(raw6TestPath) as raw:
257+
s = raw.sizes
258+
assert_equal(s.crop_left_margin, 0)
259+
assert_equal(s.crop_top_margin, 0)
260+
assert_equal(s.crop_width, 0)
261+
assert_equal(s.crop_height, 0)
262+
231263
def testHalfSizeParameter():
232264
raw = rawpy.imread(rawTestPath)
233265
s = raw.sizes

0 commit comments

Comments
 (0)