Skip to content

Commit 8df33d1

Browse files
committed
Cache the address of the mode struct in the Image header
1 parent 139e6d0 commit 8df33d1

3 files changed

Lines changed: 16 additions & 17 deletions

File tree

src/libImaging/Arrow.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ image_band_json(Imaging im) {
6262
// Bands can be 4 bands * 2 characters each
6363
int len = strlen(format) + 8 + 1;
6464
int err;
65-
ModeData *modedata = getModeData(im->mode);
6665

6766
json = calloc(1, len);
6867

@@ -74,10 +73,10 @@ image_band_json(Imaging im) {
7473
json,
7574
len,
7675
format,
77-
modedata->band_names[0],
78-
modedata->band_names[1],
79-
modedata->band_names[2],
80-
modedata->band_names[3]
76+
im->modedata->band_names[0],
77+
im->modedata->band_names[1],
78+
im->modedata->band_names[2],
79+
im->modedata->band_names[3]
8180
);
8281
if (err < 0) {
8382
return NULL;
@@ -99,7 +98,7 @@ single_band_json(Imaging im) {
9998
return NULL;
10099
}
101100

102-
err = PyOS_snprintf(json, len, format, getModeData(im->mode)->band_names[0]);
101+
err = PyOS_snprintf(json, len, format, im->modedata->band_names[0]);
103102
if (err < 0) {
104103
return NULL;
105104
}
@@ -189,9 +188,8 @@ int
189188
export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
190189
int retval = 0;
191190
char *band_json;
192-
ModeData *modedata = getModeData(im->mode);
193191

194-
if (strcmp(modedata->arrow_band_format, "") == 0) {
192+
if (strcmp(im->modedata->arrow_band_format, "") == 0) {
195193
return IMAGING_ARROW_INCOMPATIBLE_MODE;
196194
}
197195

@@ -200,10 +198,10 @@ export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
200198
return IMAGING_ARROW_MEMORY_LAYOUT;
201199
}
202200

203-
modedata = getModeData(im->mode);
204-
205201
if (im->bands == 1) {
206-
retval = export_named_type(schema, modedata->arrow_band_format, modedata->band_names[0]);
202+
retval = export_named_type(schema,
203+
im->modedata->arrow_band_format,
204+
im->modedata->band_names[0]);
207205
if (retval != 0) {
208206
return retval;
209207
}
@@ -225,7 +223,7 @@ export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
225223
schema->children = calloc(1, sizeof(struct ArrowSchema *));
226224
schema->children[0] = (struct ArrowSchema *)calloc(1, sizeof(struct ArrowSchema));
227225
retval = export_named_type(
228-
schema->children[0], modedata->arrow_band_format, modedata->name
226+
schema->children[0], im->modedata->arrow_band_format, im->modedata->name
229227
);
230228
if (retval != 0) {
231229
free(schema->children[0]);
@@ -409,7 +407,7 @@ export_fixed_pixel_array(Imaging im, struct ArrowArray *array) {
409407

410408
int
411409
export_imaging_array(Imaging im, struct ArrowArray *array) {
412-
if (strcmp(getModeData(im->mode)->arrow_band_format, "") == 0) {
410+
if (strcmp(im->modedata->arrow_band_format, "") == 0) {
413411
return IMAGING_ARROW_INCOMPATIBLE_MODE;
414412
}
415413

src/libImaging/Imaging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ typedef struct {
8080
struct ImagingMemoryInstance {
8181
/* Format */
8282
ModeID mode; /* Image mode (IMAGING_MODE_*) */
83+
const ModeData *modedata; /* mode data struct */
8384
int type; /* Data type (IMAGING_TYPE_*) */
8485
int depth; /* Depth (ignored in this version) */
8586
int bands; /* Number of bands (1, 2, 3, or 4) */

src/libImaging/Storage.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ ImagingNewPrologueSubtype(const ModeID mode, int xsize, int ysize, int size) {
6060
im->ysize = ysize;
6161
im->refcount = 1;
6262
im->type = IMAGING_TYPE_UINT8;
63+
im->modedata = getModeData(mode);
6364

6465
if (mode == IMAGING_MODE_1) {
6566
/* 1-bit images */
@@ -635,7 +636,6 @@ ImagingNewArrow(
635636
if (!im) {
636637
return NULL;
637638
}
638-
ModeData *modedata = getModeData(mode);
639639

640640
int64_t pixels = (int64_t)xsize * (int64_t)ysize;
641641

@@ -646,7 +646,7 @@ ImagingNewArrow(
646646
&& im->pixelsize == 4 // 4xchar* storage
647647
&& im->bands >= 2) // INT32 into any INT32 Storage mode
648648
|| // (()||()) &&
649-
(strcmp(schema->format, modedata->arrow_band_format) == 0 // same mode
649+
(strcmp(schema->format, im->modedata->arrow_band_format) == 0 // same mode
650650
&& im->bands == 1)) // Single band match
651651
&& pixels == external_array->length) {
652652
// one arrow element per, and it matches a pixelsize*char
@@ -660,7 +660,7 @@ ImagingNewArrow(
660660
&& schema->n_children > 0 // make sure schema is well formed.
661661
&& schema->children // make sure schema is well formed
662662
&& strcmp(schema->children[0]->format, "C") == 0 // Expected format
663-
&& strcmp(modedata->arrow_band_format, "C") == 0 // Expected Format
663+
&& strcmp(im->modedata->arrow_band_format, "C") == 0 // Expected Format
664664
&& pixels == external_array->length // expected length
665665
&& external_array->n_children == 1 // array is well formed
666666
&& external_array->children // array is well formed
@@ -674,7 +674,7 @@ ImagingNewArrow(
674674
if (strcmp(schema->format, "C") == 0 // uint8
675675
&& im->pixelsize == 4 // storage as 32 bpc
676676
&& schema->n_children == 0 // make sure schema is well formed.
677-
&& strcmp(modedata->arrow_band_format, "C") == 0 // expected format
677+
&& strcmp(im->modedata->arrow_band_format, "C") == 0 // expected format
678678
&& 4 * pixels == external_array->length) { // expected length
679679
// single flat array, interleaved storage.
680680
if (ImagingBorrowArrow(im, external_array, 1, array_capsule)) {

0 commit comments

Comments
 (0)