Cache the address of the mode struct in the Image header

This commit is contained in:
wiredfool 2025-10-20 22:15:21 +01:00
parent 139e6d0984
commit 8df33d19d9
3 changed files with 16 additions and 17 deletions

View File

@ -62,7 +62,6 @@ image_band_json(Imaging im) {
// Bands can be 4 bands * 2 characters each
int len = strlen(format) + 8 + 1;
int err;
ModeData *modedata = getModeData(im->mode);
json = calloc(1, len);
@ -74,10 +73,10 @@ image_band_json(Imaging im) {
json,
len,
format,
modedata->band_names[0],
modedata->band_names[1],
modedata->band_names[2],
modedata->band_names[3]
im->modedata->band_names[0],
im->modedata->band_names[1],
im->modedata->band_names[2],
im->modedata->band_names[3]
);
if (err < 0) {
return NULL;
@ -99,7 +98,7 @@ single_band_json(Imaging im) {
return NULL;
}
err = PyOS_snprintf(json, len, format, getModeData(im->mode)->band_names[0]);
err = PyOS_snprintf(json, len, format, im->modedata->band_names[0]);
if (err < 0) {
return NULL;
}
@ -189,9 +188,8 @@ int
export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
int retval = 0;
char *band_json;
ModeData *modedata = getModeData(im->mode);
if (strcmp(modedata->arrow_band_format, "") == 0) {
if (strcmp(im->modedata->arrow_band_format, "") == 0) {
return IMAGING_ARROW_INCOMPATIBLE_MODE;
}
@ -200,10 +198,10 @@ export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
return IMAGING_ARROW_MEMORY_LAYOUT;
}
modedata = getModeData(im->mode);
if (im->bands == 1) {
retval = export_named_type(schema, modedata->arrow_band_format, modedata->band_names[0]);
retval = export_named_type(schema,
im->modedata->arrow_band_format,
im->modedata->band_names[0]);
if (retval != 0) {
return retval;
}
@ -225,7 +223,7 @@ export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
schema->children = calloc(1, sizeof(struct ArrowSchema *));
schema->children[0] = (struct ArrowSchema *)calloc(1, sizeof(struct ArrowSchema));
retval = export_named_type(
schema->children[0], modedata->arrow_band_format, modedata->name
schema->children[0], im->modedata->arrow_band_format, im->modedata->name
);
if (retval != 0) {
free(schema->children[0]);
@ -409,7 +407,7 @@ err:
int
export_imaging_array(Imaging im, struct ArrowArray *array) {
if (strcmp(getModeData(im->mode)->arrow_band_format, "") == 0) {
if (strcmp(im->modedata->arrow_band_format, "") == 0) {
return IMAGING_ARROW_INCOMPATIBLE_MODE;
}

View File

@ -80,6 +80,7 @@ typedef struct {
struct ImagingMemoryInstance {
/* Format */
ModeID mode; /* Image mode (IMAGING_MODE_*) */
const ModeData *modedata; /* mode data struct */
int type; /* Data type (IMAGING_TYPE_*) */
int depth; /* Depth (ignored in this version) */
int bands; /* Number of bands (1, 2, 3, or 4) */

View File

@ -60,6 +60,7 @@ ImagingNewPrologueSubtype(const ModeID mode, int xsize, int ysize, int size) {
im->ysize = ysize;
im->refcount = 1;
im->type = IMAGING_TYPE_UINT8;
im->modedata = getModeData(mode);
if (mode == IMAGING_MODE_1) {
/* 1-bit images */
@ -635,7 +636,6 @@ ImagingNewArrow(
if (!im) {
return NULL;
}
ModeData *modedata = getModeData(mode);
int64_t pixels = (int64_t)xsize * (int64_t)ysize;
@ -646,7 +646,7 @@ ImagingNewArrow(
&& im->pixelsize == 4 // 4xchar* storage
&& im->bands >= 2) // INT32 into any INT32 Storage mode
|| // (()||()) &&
(strcmp(schema->format, modedata->arrow_band_format) == 0 // same mode
(strcmp(schema->format, im->modedata->arrow_band_format) == 0 // same mode
&& im->bands == 1)) // Single band match
&& pixels == external_array->length) {
// one arrow element per, and it matches a pixelsize*char
@ -660,7 +660,7 @@ ImagingNewArrow(
&& schema->n_children > 0 // make sure schema is well formed.
&& schema->children // make sure schema is well formed
&& strcmp(schema->children[0]->format, "C") == 0 // Expected format
&& strcmp(modedata->arrow_band_format, "C") == 0 // Expected Format
&& strcmp(im->modedata->arrow_band_format, "C") == 0 // Expected Format
&& pixels == external_array->length // expected length
&& external_array->n_children == 1 // array is well formed
&& external_array->children // array is well formed
@ -674,7 +674,7 @@ ImagingNewArrow(
if (strcmp(schema->format, "C") == 0 // uint8
&& im->pixelsize == 4 // storage as 32 bpc
&& schema->n_children == 0 // make sure schema is well formed.
&& strcmp(modedata->arrow_band_format, "C") == 0 // expected format
&& strcmp(im->modedata->arrow_band_format, "C") == 0 // expected format
&& 4 * pixels == external_array->length) { // expected length
// single flat array, interleaved storage.
if (ImagingBorrowArrow(im, external_array, 1, array_capsule)) {