Skip to content

Commit 139e6d0

Browse files
committed
Add arrow_band_format and band_names to the mode data struct
1 parent 693df7b commit 139e6d0

5 files changed

Lines changed: 42 additions & 79 deletions

File tree

src/libImaging/Arrow.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ 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);
6566

6667
json = calloc(1, len);
6768

@@ -73,10 +74,10 @@ image_band_json(Imaging im) {
7374
json,
7475
len,
7576
format,
76-
im->band_names[0],
77-
im->band_names[1],
78-
im->band_names[2],
79-
im->band_names[3]
77+
modedata->band_names[0],
78+
modedata->band_names[1],
79+
modedata->band_names[2],
80+
modedata->band_names[3]
8081
);
8182
if (err < 0) {
8283
return NULL;
@@ -98,7 +99,7 @@ single_band_json(Imaging im) {
9899
return NULL;
99100
}
100101

101-
err = PyOS_snprintf(json, len, format, im->band_names[0]);
102+
err = PyOS_snprintf(json, len, format, getModeData(im->mode)->band_names[0]);
102103
if (err < 0) {
103104
return NULL;
104105
}
@@ -188,8 +189,9 @@ int
188189
export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
189190
int retval = 0;
190191
char *band_json;
192+
ModeData *modedata = getModeData(im->mode);
191193

192-
if (strcmp(im->arrow_band_format, "") == 0) {
194+
if (strcmp(modedata->arrow_band_format, "") == 0) {
193195
return IMAGING_ARROW_INCOMPATIBLE_MODE;
194196
}
195197

@@ -198,8 +200,10 @@ export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
198200
return IMAGING_ARROW_MEMORY_LAYOUT;
199201
}
200202

203+
modedata = getModeData(im->mode);
204+
201205
if (im->bands == 1) {
202-
retval = export_named_type(schema, im->arrow_band_format, im->band_names[0]);
206+
retval = export_named_type(schema, modedata->arrow_band_format, modedata->band_names[0]);
203207
if (retval != 0) {
204208
return retval;
205209
}
@@ -221,7 +225,7 @@ export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
221225
schema->children = calloc(1, sizeof(struct ArrowSchema *));
222226
schema->children[0] = (struct ArrowSchema *)calloc(1, sizeof(struct ArrowSchema));
223227
retval = export_named_type(
224-
schema->children[0], im->arrow_band_format, getModeData(im->mode)->name
228+
schema->children[0], modedata->arrow_band_format, modedata->name
225229
);
226230
if (retval != 0) {
227231
free(schema->children[0]);
@@ -405,7 +409,7 @@ export_fixed_pixel_array(Imaging im, struct ArrowArray *array) {
405409

406410
int
407411
export_imaging_array(Imaging im, struct ArrowArray *array) {
408-
if (strcmp(im->arrow_band_format, "") == 0) {
412+
if (strcmp(getModeData(im->mode)->arrow_band_format, "") == 0) {
409413
return IMAGING_ARROW_INCOMPATIBLE_MODE;
410414
}
411415

src/libImaging/Imaging.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ struct ImagingMemoryInstance {
106106

107107
/* arrow */
108108
int refcount; /* Number of arrow arrays that have been allocated */
109-
char band_names[4][3]; /* names of bands, max 2 char + null terminator */
110-
char arrow_band_format[2]; /* single character + null terminator */
111109

112110
int read_only; /* flag for read-only. set for arrow borrowed arrays */
113111
PyObject *arrow_array_capsule; /* upstream arrow array source */

src/libImaging/Mode.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,30 @@
99
const ModeData MODES[] = {
1010
[IMAGING_MODE_UNKNOWN] = {""},
1111

12-
[IMAGING_MODE_1] = {"1"}, [IMAGING_MODE_CMYK] = {"CMYK"},
13-
[IMAGING_MODE_F] = {"F"}, [IMAGING_MODE_HSV] = {"HSV"},
14-
[IMAGING_MODE_I] = {"I"}, [IMAGING_MODE_L] = {"L"},
15-
[IMAGING_MODE_LA] = {"LA"}, [IMAGING_MODE_LAB] = {"LAB"},
16-
[IMAGING_MODE_La] = {"La"}, [IMAGING_MODE_P] = {"P"},
17-
[IMAGING_MODE_PA] = {"PA"}, [IMAGING_MODE_RGB] = {"RGB"},
18-
[IMAGING_MODE_RGBA] = {"RGBA"}, [IMAGING_MODE_RGBX] = {"RGBX"},
19-
[IMAGING_MODE_RGBa] = {"RGBa"}, [IMAGING_MODE_YCbCr] = {"YCbCr"},
12+
// Name, Arrow Format, Band Names
13+
[IMAGING_MODE_1] = {"1", "C", {"1"}},
14+
[IMAGING_MODE_CMYK] = {"CMYK", "C", {"C", "M", "Y", "K"}},
15+
[IMAGING_MODE_F] = {"F", "f", {"F"}},
16+
[IMAGING_MODE_HSV] = {"HSV", "C", {"H", "S", "V", "X"}},
17+
[IMAGING_MODE_I] = {"I", "i", {"I"}},
18+
[IMAGING_MODE_L] = {"L", "C", {"L"}},
19+
[IMAGING_MODE_LA] = {"LA", "C", {"L", "X", "X", "A"}},
20+
[IMAGING_MODE_LAB] = {"LAB", "C", {"L", "a", "b", "X"}},
21+
[IMAGING_MODE_La] = {"La", "C", {"L", "X", "X", "a"}},
22+
[IMAGING_MODE_P] = {"P", "C", {"P"}},
23+
[IMAGING_MODE_PA] = {"PA", "C", {"P", "X", "X", "A"}},
24+
[IMAGING_MODE_RGB] = {"RGB", "C", {"R", "G", "B", "X"}},
25+
[IMAGING_MODE_RGBA] = {"RGBA", "C", {"R", "G", "B", "A"}},
26+
[IMAGING_MODE_RGBX] = {"RGBX", "C", {"R", "G", "B", "X"}},
27+
[IMAGING_MODE_RGBa] = {"RGBa", "C", {"R", "G", "B", "a"}},
28+
[IMAGING_MODE_YCbCr] = {"YCbCr", "C", {"Y", "Cb", "Cr", "X"}},
2029

21-
[IMAGING_MODE_I_16] = {"I;16"}, [IMAGING_MODE_I_16L] = {"I;16L"},
22-
[IMAGING_MODE_I_16B] = {"I;16B"}, [IMAGING_MODE_I_16N] = {"I;16N"},
23-
[IMAGING_MODE_I_32L] = {"I;32L"}, [IMAGING_MODE_I_32B] = {"I;32B"},
30+
[IMAGING_MODE_I_16] = {"I;16", "s", {"I"}},
31+
[IMAGING_MODE_I_16L] = {"I;16L", "s", {"I"}},
32+
[IMAGING_MODE_I_16B] = {"I;16B", "s", {"I"}},
33+
[IMAGING_MODE_I_16N] = {"I;16N", "s", {"I"}},
34+
[IMAGING_MODE_I_32L] = {"I;32L", "i", {"I"}},
35+
[IMAGING_MODE_I_32B] = {"I;32B", "i", {"I"}},
2436
};
2537

2638
const ModeID

src/libImaging/Mode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ typedef enum {
3131

3232
typedef struct {
3333
const char *const name;
34+
const char *const arrow_band_format;
35+
const char *band_names[4]; /* names of bands */
3436
} ModeData;
3537

3638
const ModeID

src/libImaging/Storage.c

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -60,75 +60,55 @@ 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-
strcpy(im->arrow_band_format, "C");
6463

6564
if (mode == IMAGING_MODE_1) {
6665
/* 1-bit images */
6766
im->bands = im->pixelsize = 1;
6867
im->linesize = xsize;
69-
strcpy(im->band_names[0], "1");
7068

7169
} else if (mode == IMAGING_MODE_P) {
7270
/* 8-bit palette mapped images */
7371
im->bands = im->pixelsize = 1;
7472
im->linesize = xsize;
7573
im->palette = ImagingPaletteNew(IMAGING_MODE_RGB);
76-
strcpy(im->band_names[0], "P");
7774

7875
} else if (mode == IMAGING_MODE_PA) {
7976
/* 8-bit palette with alpha */
8077
im->bands = 2;
8178
im->pixelsize = 4; /* store in image32 memory */
8279
im->linesize = xsize * 4;
8380
im->palette = ImagingPaletteNew(IMAGING_MODE_RGB);
84-
strcpy(im->band_names[0], "P");
85-
strcpy(im->band_names[1], "X");
86-
strcpy(im->band_names[2], "X");
87-
strcpy(im->band_names[3], "A");
8881

8982
} else if (mode == IMAGING_MODE_L) {
9083
/* 8-bit grayscale (luminance) images */
9184
im->bands = im->pixelsize = 1;
9285
im->linesize = xsize;
93-
strcpy(im->band_names[0], "L");
9486

9587
} else if (mode == IMAGING_MODE_LA) {
9688
/* 8-bit grayscale (luminance) with alpha */
9789
im->bands = 2;
9890
im->pixelsize = 4; /* store in image32 memory */
9991
im->linesize = xsize * 4;
100-
strcpy(im->band_names[0], "L");
101-
strcpy(im->band_names[1], "X");
102-
strcpy(im->band_names[2], "X");
103-
strcpy(im->band_names[3], "A");
10492

10593
} else if (mode == IMAGING_MODE_La) {
10694
/* 8-bit grayscale (luminance) with premultiplied alpha */
10795
im->bands = 2;
10896
im->pixelsize = 4; /* store in image32 memory */
10997
im->linesize = xsize * 4;
110-
strcpy(im->band_names[0], "L");
111-
strcpy(im->band_names[1], "X");
112-
strcpy(im->band_names[2], "X");
113-
strcpy(im->band_names[3], "a");
11498

11599
} else if (mode == IMAGING_MODE_F) {
116100
/* 32-bit floating point images */
117101
im->bands = 1;
118102
im->pixelsize = 4;
119103
im->linesize = xsize * 4;
120104
im->type = IMAGING_TYPE_FLOAT32;
121-
strcpy(im->arrow_band_format, "f");
122-
strcpy(im->band_names[0], "F");
123105

124106
} else if (mode == IMAGING_MODE_I) {
125107
/* 32-bit integer images */
126108
im->bands = 1;
127109
im->pixelsize = 4;
128110
im->linesize = xsize * 4;
129111
im->type = IMAGING_TYPE_INT32;
130-
strcpy(im->arrow_band_format, "i");
131-
strcpy(im->band_names[0], "I");
132112

133113
} else if (isModeI16(mode)) {
134114
/* EXPERIMENTAL */
@@ -137,86 +117,52 @@ ImagingNewPrologueSubtype(const ModeID mode, int xsize, int ysize, int size) {
137117
im->pixelsize = 2;
138118
im->linesize = xsize * 2;
139119
im->type = IMAGING_TYPE_SPECIAL;
140-
strcpy(im->arrow_band_format, "s");
141-
strcpy(im->band_names[0], "I");
142120

143121
} else if (mode == IMAGING_MODE_RGB) {
144122
/* 24-bit true colour images */
145123
im->bands = 3;
146124
im->pixelsize = 4;
147125
im->linesize = xsize * 4;
148-
strcpy(im->band_names[0], "R");
149-
strcpy(im->band_names[1], "G");
150-
strcpy(im->band_names[2], "B");
151-
strcpy(im->band_names[3], "X");
152126

153127
} else if (mode == IMAGING_MODE_RGBX) {
154128
/* 32-bit true colour images with padding */
155129
im->bands = im->pixelsize = 4;
156130
im->linesize = xsize * 4;
157-
strcpy(im->band_names[0], "R");
158-
strcpy(im->band_names[1], "G");
159-
strcpy(im->band_names[2], "B");
160-
strcpy(im->band_names[3], "X");
161131

162132
} else if (mode == IMAGING_MODE_RGBA) {
163133
/* 32-bit true colour images with alpha */
164134
im->bands = im->pixelsize = 4;
165135
im->linesize = xsize * 4;
166-
strcpy(im->band_names[0], "R");
167-
strcpy(im->band_names[1], "G");
168-
strcpy(im->band_names[2], "B");
169-
strcpy(im->band_names[3], "A");
170136

171137
} else if (mode == IMAGING_MODE_RGBa) {
172138
/* 32-bit true colour images with premultiplied alpha */
173139
im->bands = im->pixelsize = 4;
174140
im->linesize = xsize * 4;
175-
strcpy(im->band_names[0], "R");
176-
strcpy(im->band_names[1], "G");
177-
strcpy(im->band_names[2], "B");
178-
strcpy(im->band_names[3], "a");
179141

180142
} else if (mode == IMAGING_MODE_CMYK) {
181143
/* 32-bit colour separation */
182144
im->bands = im->pixelsize = 4;
183145
im->linesize = xsize * 4;
184-
strcpy(im->band_names[0], "C");
185-
strcpy(im->band_names[1], "M");
186-
strcpy(im->band_names[2], "Y");
187-
strcpy(im->band_names[3], "K");
188146

189147
} else if (mode == IMAGING_MODE_YCbCr) {
190148
/* 24-bit video format */
191149
im->bands = 3;
192150
im->pixelsize = 4;
193151
im->linesize = xsize * 4;
194-
strcpy(im->band_names[0], "Y");
195-
strcpy(im->band_names[1], "Cb");
196-
strcpy(im->band_names[2], "Cr");
197-
strcpy(im->band_names[3], "X");
198152

199153
} else if (mode == IMAGING_MODE_LAB) {
200154
/* 24-bit color, luminance, + 2 color channels */
201155
/* L is uint8, a,b are int8 */
202156
im->bands = 3;
203157
im->pixelsize = 4;
204158
im->linesize = xsize * 4;
205-
strcpy(im->band_names[0], "L");
206-
strcpy(im->band_names[1], "a");
207-
strcpy(im->band_names[2], "b");
208-
strcpy(im->band_names[3], "X");
209159

210160
} else if (mode == IMAGING_MODE_HSV) {
211161
/* 24-bit color, luminance, + 2 color channels */
212162
/* L is uint8, a,b are int8 */
213163
im->bands = 3;
214164
im->pixelsize = 4;
215165
im->linesize = xsize * 4;
216-
strcpy(im->band_names[0], "H");
217-
strcpy(im->band_names[1], "S");
218-
strcpy(im->band_names[2], "V");
219-
strcpy(im->band_names[3], "X");
220166

221167
} else {
222168
free(im);
@@ -689,6 +635,7 @@ ImagingNewArrow(
689635
if (!im) {
690636
return NULL;
691637
}
638+
ModeData *modedata = getModeData(mode);
692639

693640
int64_t pixels = (int64_t)xsize * (int64_t)ysize;
694641

@@ -699,7 +646,7 @@ ImagingNewArrow(
699646
&& im->pixelsize == 4 // 4xchar* storage
700647
&& im->bands >= 2) // INT32 into any INT32 Storage mode
701648
|| // (()||()) &&
702-
(strcmp(schema->format, im->arrow_band_format) == 0 // same mode
649+
(strcmp(schema->format, modedata->arrow_band_format) == 0 // same mode
703650
&& im->bands == 1)) // Single band match
704651
&& pixels == external_array->length) {
705652
// one arrow element per, and it matches a pixelsize*char
@@ -713,7 +660,7 @@ ImagingNewArrow(
713660
&& schema->n_children > 0 // make sure schema is well formed.
714661
&& schema->children // make sure schema is well formed
715662
&& strcmp(schema->children[0]->format, "C") == 0 // Expected format
716-
&& strcmp(im->arrow_band_format, "C") == 0 // Expected Format
663+
&& strcmp(modedata->arrow_band_format, "C") == 0 // Expected Format
717664
&& pixels == external_array->length // expected length
718665
&& external_array->n_children == 1 // array is well formed
719666
&& external_array->children // array is well formed
@@ -727,7 +674,7 @@ ImagingNewArrow(
727674
if (strcmp(schema->format, "C") == 0 // uint8
728675
&& im->pixelsize == 4 // storage as 32 bpc
729676
&& schema->n_children == 0 // make sure schema is well formed.
730-
&& strcmp(im->arrow_band_format, "C") == 0 // expected format
677+
&& strcmp(modedata->arrow_band_format, "C") == 0 // expected format
731678
&& 4 * pixels == external_array->length) { // expected length
732679
// single flat array, interleaved storage.
733680
if (ImagingBorrowArrow(im, external_array, 1, array_capsule)) {

0 commit comments

Comments
 (0)