use mode structs in Access.c

This commit is contained in:
Yay295 2024-04-21 13:05:05 -05:00
parent 97e29dcd2d
commit 820f22c120
3 changed files with 71 additions and 71 deletions

View File

@ -11,38 +11,9 @@
#include "Imaging.h" #include "Imaging.h"
/* use make_hash.py from the pillow-scripts repository to calculate these values */ #define ACCESS_TABLE_SIZE 24
#define ACCESS_TABLE_SIZE 35 static struct ImagingAccessInstance ACCESS_TABLE[ACCESS_TABLE_SIZE];
#define ACCESS_TABLE_HASH 8940
static struct ImagingAccessInstance access_table[ACCESS_TABLE_SIZE];
static inline UINT32
hash(const char *mode) {
UINT32 i = ACCESS_TABLE_HASH;
while (*mode) {
i = ((i << 5) + i) ^ (UINT8)*mode++;
}
return i % ACCESS_TABLE_SIZE;
}
static ImagingAccess
add_item(const char *mode) {
UINT32 i = hash(mode);
/* printf("hash %s => %d\n", mode, i); */
if (access_table[i].mode && strcmp(access_table[i].mode, mode) != 0) {
fprintf(
stderr,
"AccessInit: hash collision: %d for both %s and %s\n",
i,
mode,
access_table[i].mode
);
exit(1);
}
access_table[i].mode = mode;
return &access_table[i];
}
/* fetch individual pixel */ /* fetch individual pixel */
@ -184,54 +155,71 @@ put_pixel_32(Imaging im, int x, int y, const void *color) {
memcpy(&im->image32[y][x], color, sizeof(INT32)); memcpy(&im->image32[y][x], color, sizeof(INT32));
} }
void
ImagingAccessInit(void) { static void
#define ADD(mode_, get_pixel_, put_pixel_) \ set_access_table_item(
{ \ const int index,
ImagingAccess access = add_item(mode_); \ const Mode * const mode,
access->get_pixel = get_pixel_; \ void (*get_pixel)(Imaging im, int x, int y, void *pixel),
access->put_pixel = put_pixel_; \ void (*put_pixel)(Imaging im, int x, int y, const void *pixel)
) {
ACCESS_TABLE[index].mode = mode;
ACCESS_TABLE[index].get_pixel = get_pixel;
ACCESS_TABLE[index].put_pixel = put_pixel;
} }
/* populate access table */ void
ADD("1", get_pixel_8, put_pixel_8); ImagingAccessInit(void) {
ADD("L", get_pixel_8, put_pixel_8); int i = 0;
ADD("LA", get_pixel_32_2bands, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_1, get_pixel_8, put_pixel_8);
ADD("La", get_pixel_32_2bands, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_L, get_pixel_8, put_pixel_8);
ADD("I", get_pixel_32, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_LA, get_pixel_32_2bands, put_pixel_32);
ADD("I;16", get_pixel_16L, put_pixel_16L); set_access_table_item(i++, IMAGING_MODE_La, get_pixel_32_2bands, put_pixel_32);
ADD("I;16L", get_pixel_16L, put_pixel_16L); set_access_table_item(i++, IMAGING_MODE_I, get_pixel_32, put_pixel_32);
ADD("I;16B", get_pixel_16B, put_pixel_16B); set_access_table_item(i++, IMAGING_MODE_I_16, get_pixel_16L, put_pixel_16L);
set_access_table_item(i++, IMAGING_MODE_I_16L, get_pixel_16L, put_pixel_16L);
set_access_table_item(i++, IMAGING_MODE_I_16B, get_pixel_16B, put_pixel_16B);
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
ADD("I;16N", get_pixel_16B, put_pixel_16B); set_access_table_item(i++, IMAGING_MODE_I_16N, get_pixel_16B, put_pixel_16B);
#else #else
ADD("I;16N", get_pixel_16L, put_pixel_16L); set_access_table_item(i++, IMAGING_MODE_I_16N, get_pixel_16L, put_pixel_16L);
#endif #endif
ADD("I;32L", get_pixel_32L, put_pixel_32L); set_access_table_item(i++, IMAGING_MODE_I_32L, get_pixel_32L, put_pixel_32L);
ADD("I;32B", get_pixel_32B, put_pixel_32B); set_access_table_item(i++, IMAGING_MODE_I_32B, get_pixel_32B, put_pixel_32B);
ADD("F", get_pixel_32, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_F, get_pixel_32, put_pixel_32);
ADD("P", get_pixel_8, put_pixel_8); set_access_table_item(i++, IMAGING_MODE_P, get_pixel_8, put_pixel_8);
ADD("PA", get_pixel_32_2bands, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_PA, get_pixel_32_2bands, put_pixel_32);
ADD("BGR;15", get_pixel_BGR15, put_pixel_BGR1516); set_access_table_item(i++, IMAGING_MODE_BGR_15, get_pixel_BGR15, put_pixel_BGR1516);
ADD("BGR;16", get_pixel_BGR16, put_pixel_BGR1516); set_access_table_item(i++, IMAGING_MODE_BGR_16, get_pixel_BGR16, put_pixel_BGR1516);
ADD("BGR;24", get_pixel_BGR24, put_pixel_BGR24); set_access_table_item(i++, IMAGING_MODE_BGR_24, get_pixel_BGR24, put_pixel_BGR24);
ADD("RGB", get_pixel_32, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_RGB, get_pixel_32, put_pixel_32);
ADD("RGBA", get_pixel_32, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_RGBA, get_pixel_32, put_pixel_32);
ADD("RGBa", get_pixel_32, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_RGBa, get_pixel_32, put_pixel_32);
ADD("RGBX", get_pixel_32, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_RGBX, get_pixel_32, put_pixel_32);
ADD("CMYK", get_pixel_32, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_CMYK, get_pixel_32, put_pixel_32);
ADD("YCbCr", get_pixel_32, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_YCbCr, get_pixel_32, put_pixel_32);
ADD("LAB", get_pixel_32, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_LAB, get_pixel_32, put_pixel_32);
ADD("HSV", get_pixel_32, put_pixel_32); set_access_table_item(i++, IMAGING_MODE_HSV, get_pixel_32, put_pixel_32);
if (i != ACCESS_TABLE_SIZE) {
fprintf(
stderr,
"AccessInit: incorrect number of items added to ACCESS_TABLE; expected %i but got %i\n",
ACCESS_TABLE_SIZE,
i);
exit(1);
}
} }
ImagingAccess ImagingAccess
ImagingAccessNew(Imaging im) { ImagingAccessNew(const Imaging im) {
ImagingAccess access = &access_table[hash(im->mode)]; int i;
if (im->mode[0] != access->mode[0] || strcmp(im->mode, access->mode) != 0) { for (i = 0; i < ACCESS_TABLE_SIZE; i++) {
return NULL; if (im->mode == ACCESS_TABLE[i].mode) {
return &ACCESS_TABLE[i];
} }
return access; }
return NULL;
} }
void void

View File

@ -32,6 +32,8 @@ CREATE_MODE(Mode, MODE_I_16, {"I;16"})
CREATE_MODE(Mode, MODE_I_16L, {"I;16L"}) CREATE_MODE(Mode, MODE_I_16L, {"I;16L"})
CREATE_MODE(Mode, MODE_I_16B, {"I;16B"}) CREATE_MODE(Mode, MODE_I_16B, {"I;16B"})
CREATE_MODE(Mode, MODE_I_16N, {"I;16N"}) CREATE_MODE(Mode, MODE_I_16N, {"I;16N"})
CREATE_MODE(Mode, MODE_I_32L, {"I;32L"})
CREATE_MODE(Mode, MODE_I_32B, {"I;32B"})
const Mode * const MODES[] = { const Mode * const MODES[] = {
IMAGING_MODE_1, IMAGING_MODE_1,
@ -59,6 +61,8 @@ const Mode * const MODES[] = {
IMAGING_MODE_I_16L, IMAGING_MODE_I_16L,
IMAGING_MODE_I_16B, IMAGING_MODE_I_16B,
IMAGING_MODE_I_16N, IMAGING_MODE_I_16N,
IMAGING_MODE_I_32L,
IMAGING_MODE_I_32B,
NULL NULL
}; };
@ -102,6 +106,8 @@ ALIAS_MODE_AS_RAWMODE(I_16)
ALIAS_MODE_AS_RAWMODE(I_16L) ALIAS_MODE_AS_RAWMODE(I_16L)
ALIAS_MODE_AS_RAWMODE(I_16B) ALIAS_MODE_AS_RAWMODE(I_16B)
ALIAS_MODE_AS_RAWMODE(I_16N) ALIAS_MODE_AS_RAWMODE(I_16N)
ALIAS_MODE_AS_RAWMODE(I_32L)
ALIAS_MODE_AS_RAWMODE(I_32B)
const RawMode * const RAWMODES[] = { const RawMode * const RAWMODES[] = {
IMAGING_RAWMODE_1, IMAGING_RAWMODE_1,
@ -129,6 +135,8 @@ const RawMode * const RAWMODES[] = {
IMAGING_RAWMODE_I_16L, IMAGING_RAWMODE_I_16L,
IMAGING_RAWMODE_I_16B, IMAGING_RAWMODE_I_16B,
IMAGING_RAWMODE_I_16N, IMAGING_RAWMODE_I_16N,
IMAGING_RAWMODE_I_32L,
IMAGING_RAWMODE_I_32B,
NULL NULL
}; };

View File

@ -35,6 +35,8 @@ extern const Mode * const IMAGING_MODE_I_16;
extern const Mode * const IMAGING_MODE_I_16L; extern const Mode * const IMAGING_MODE_I_16L;
extern const Mode * const IMAGING_MODE_I_16B; extern const Mode * const IMAGING_MODE_I_16B;
extern const Mode * const IMAGING_MODE_I_16N; extern const Mode * const IMAGING_MODE_I_16N;
extern const Mode * const IMAGING_MODE_I_32L;
extern const Mode * const IMAGING_MODE_I_32B;
const Mode * findMode(const char * const name); const Mode * findMode(const char * const name);
@ -67,11 +69,13 @@ extern const RawMode * const IMAGING_RAWMODE_BGR_16;
extern const RawMode * const IMAGING_RAWMODE_BGR_24; extern const RawMode * const IMAGING_RAWMODE_BGR_24;
extern const RawMode * const IMAGING_RAWMODE_BGR_32; extern const RawMode * const IMAGING_RAWMODE_BGR_32;
// I;16 modes. // I;* modes.
extern const RawMode * const IMAGING_RAWMODE_I_16; extern const RawMode * const IMAGING_RAWMODE_I_16;
extern const RawMode * const IMAGING_RAWMODE_I_16L; extern const RawMode * const IMAGING_RAWMODE_I_16L;
extern const RawMode * const IMAGING_RAWMODE_I_16B; extern const RawMode * const IMAGING_RAWMODE_I_16B;
extern const RawMode * const IMAGING_RAWMODE_I_16N; extern const RawMode * const IMAGING_RAWMODE_I_16N;
extern const RawMode * const IMAGING_RAWMODE_I_32L;
extern const RawMode * const IMAGING_RAWMODE_I_32B;
// Rawmodes // Rawmodes
extern const RawMode * const IMAGING_RAWMODE_1_R; extern const RawMode * const IMAGING_RAWMODE_1_R;