Test for size, add offset support

This commit is contained in:
wiredfool 2025-01-25 15:43:22 +00:00
parent 388da5c4a4
commit 55f5351e3d
2 changed files with 16 additions and 16 deletions

View File

@ -89,6 +89,14 @@ def test_invalid_array_type(mode: str, dest_modes: List[str]) -> None:
with pytest.raises(ValueError): with pytest.raises(ValueError):
Image.fromarrow(img, dest_mode, img.size) Image.fromarrow(img, dest_mode, img.size)
def test_invalid_array_size():
img = hopper('RGB')
assert img.size != (10,10)
with pytest.raises(ValueError):
Image.fromarrow(img, 'RGB', (10,10))
def test_lifetime(): def test_lifetime():
# valgrind shouldn't error out here. # valgrind shouldn't error out here.
# arrays should be accessible after the image is deleted. # arrays should be accessible after the image is deleted.

View File

@ -571,34 +571,26 @@ ImagingDestroyArrow(Imaging im) {
} }
Imaging Imaging
ImagingAllocateArrow(Imaging im, struct ArrowArray *external_array) { ImagingBorrowArrow(Imaging im,
/* don't really allocate, but naming patterns */ struct ArrowArray *external_array,
int offset_width) {
// offset_width is the # of char* for a single offset from arrow
Py_ssize_t y, i; Py_ssize_t y, i;
char *borrowed_buffer = NULL; char *borrowed_buffer = NULL;
struct ArrowArray *arr = external_array; struct ArrowArray *arr = external_array;
/* overflow check for malloc */
if (im->linesize && im->ysize > INT_MAX / im->linesize) {
return (Imaging)ImagingError_MemoryError();
}
if (arr->n_children == 1) { if (arr->n_children == 1) {
arr = arr->children[0]; arr = arr->children[0];
} }
if (arr->n_buffers == 2) { if (arr->n_buffers == 2) {
// undone offset. offset here is # of elements,
// so depends on the size of the element
// undone image is char*, arrow is void *
// buffer 0 is the null list // buffer 0 is the null list
// buffer 1 is the data // buffer 1 is the data
borrowed_buffer = (char *)arr->buffers[1]; borrowed_buffer = (char *)arr->buffers[1] + (offset_width*arr->offset);
} }
if (!borrowed_buffer) { if (!borrowed_buffer) {
// UNDONE better error here. return (Imaging)ImagingError_ValueError("Arrow Array, exactly 2 buffers required");
// currently, only wanting one where
return (Imaging)ImagingError_MemoryError();
} }
for (y = i = 0; y < im->ysize; y++) { for (y = i = 0; y < im->ysize; y++) {
@ -714,7 +706,7 @@ ImagingNewArrow(
&& im->bands == 1)) // Single band match && im->bands == 1)) // Single band match
&& pixels == external_array->length) { && pixels == external_array->length) {
// one arrow element per, and it matches a pixelsize*char // one arrow element per, and it matches a pixelsize*char
if (ImagingAllocateArrow(im, external_array)) { if (ImagingBorrowArrow(im, external_array, im->pixelsize)) {
return im; return im;
} }
} }
@ -730,7 +722,7 @@ ImagingNewArrow(
&& external_array->children // array is well formed && external_array->children // array is well formed
&& 4 * pixels == external_array->children[0]->length) { && 4 * pixels == external_array->children[0]->length) {
// 4 up element of char into pixelsize == 4 // 4 up element of char into pixelsize == 4
if (ImagingAllocateArrow(im, external_array)) { if (ImagingBorrowArrow(im, external_array, 1)) {
return im; return im;
} }
} }