Skip to content

foreign: replace coding table with flag enum #4522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions libvips/foreign/dzsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -2047,17 +2047,12 @@ vips_foreign_save_dz_build(VipsObject *object)
*/
if (dz->direct) {
VipsImage *z;
gboolean coding[VIPS_CODING_LAST];

for (int i = 0; i < VIPS_CODING_LAST; i++)
coding[i] = FALSE;
coding[VIPS_CODING_NONE] = TRUE;

if (vips__foreign_convert_saveable(save->ready, &z,
VIPS_FOREIGN_SAVEABLE_MONO |
VIPS_FOREIGN_SAVEABLE_RGB |
VIPS_FOREIGN_SAVEABLE_CMYK,
bandfmt_dzsave, coding, save->background))
bandfmt_dzsave, VIPS_FOREIGN_CODING_NONE, save->background))
return -1;

VIPS_UNREF(save->ready);
Expand Down Expand Up @@ -2325,7 +2320,7 @@ vips_foreign_save_dz_class_init(VipsForeignSaveDzClass *class)

save_class->saveable = VIPS_FOREIGN_SAVEABLE_ANY;
save_class->format_table = bandfmt_dz;
save_class->coding[VIPS_CODING_LABQ] = TRUE;
save_class->coding |= VIPS_FOREIGN_CODING_LABQ;

VIPS_ARG_STRING(class, "imagename", 2,
_("Image name"),
Expand Down
31 changes: 16 additions & 15 deletions libvips/foreign/foreign.c
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ vips_foreign_apply_saveable(VipsImage *in, VipsImage **ready,
*/
int
vips__foreign_convert_saveable(VipsImage *in, VipsImage **ready,
VipsForeignSaveable saveable, VipsBandFormat *format, VipsCoding *coding,
VipsForeignSaveable saveable, VipsBandFormat *format, VipsForeignCoding coding,
VipsArrayDouble *background)
{
VipsImage *out;
Expand All @@ -1536,13 +1536,14 @@ vips__foreign_convert_saveable(VipsImage *in, VipsImage **ready,
g_object_ref(in);

g_assert(format);
g_assert(coding);

/* For coded images, can this class save the coding we are in now?
* Nothing to do.
*/
if (in->Coding != VIPS_CODING_NONE &&
coding[in->Coding]) {
if ((in->Coding == VIPS_CODING_LABQ &&
(coding & VIPS_FOREIGN_CODING_LABQ)) ||
(in->Coding == VIPS_CODING_RAD &&
(coding & VIPS_FOREIGN_CODING_RAD))) {
*ready = in;
return 0;
}
Expand Down Expand Up @@ -1650,30 +1651,34 @@ vips__foreign_convert_saveable(VipsImage *in, VipsImage **ready,
in = out;
}

/* Does this class want a coded image? Search the coding table for the
* first one.
/* Does this class want a coded image?
*/
if (coding[in->Coding]) {
if ((in->Coding == VIPS_CODING_NONE &&
(coding & VIPS_FOREIGN_CODING_NONE)) ||
(in->Coding == VIPS_CODING_LABQ &&
(coding & VIPS_FOREIGN_CODING_LABQ)) ||
(in->Coding == VIPS_CODING_RAD &&
(coding & VIPS_FOREIGN_CODING_RAD))) {
/* Already there, nothing to do.
*/
}
else if (coding[VIPS_CODING_LABQ]) {
else if (coding & VIPS_FOREIGN_CODING_LABQ) {
if (vips_Lab2LabQ(in, &out, NULL)) {
g_object_unref(in);
return -1;
}
g_object_unref(in);
in = out;
}
else if (coding[VIPS_CODING_RAD]) {
else if (coding & VIPS_FOREIGN_CODING_RAD) {
if (vips_float2rad(in, &out, NULL)) {
g_object_unref(in);
return -1;
}
g_object_unref(in);
in = out;
}
else if (coding[VIPS_CODING_NONE]) {
else if (coding & VIPS_FOREIGN_CODING_NONE) {
if (vips_image_decode(in, &out)) {
g_object_unref(in);
return -1;
Expand Down Expand Up @@ -1832,8 +1837,6 @@ vips_foreign_save_class_init(VipsForeignSaveClass *class)
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsOperationClass *operation_class = (VipsOperationClass *) class;

int i;

gobject_class->dispose = vips_foreign_save_dispose;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
Expand All @@ -1856,9 +1859,7 @@ vips_foreign_save_class_init(VipsForeignSaveClass *class)

/* Default to no coding allowed.
*/
for (i = 0; i < VIPS_CODING_LAST; i++)
class->coding[i] = FALSE;
class->coding[VIPS_CODING_NONE] = TRUE;
class->coding = VIPS_FOREIGN_CODING_NONE;

/* Default to no cast on save.
*/
Expand Down
3 changes: 1 addition & 2 deletions libvips/foreign/radsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ vips_foreign_save_rad_class_init(VipsForeignSaveRadClass *class)
save_class->saveable =
VIPS_FOREIGN_SAVEABLE_MONO | VIPS_FOREIGN_SAVEABLE_RGB;
save_class->format_table = vips_foreign_save_rad_format_table;
save_class->coding[VIPS_CODING_NONE] = FALSE;
save_class->coding[VIPS_CODING_RAD] = TRUE;
save_class->coding = VIPS_FOREIGN_CODING_RAD;
}

static void
Expand Down
2 changes: 1 addition & 1 deletion libvips/foreign/tiffsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ vips_foreign_save_tiff_class_init(VipsForeignSaveTiffClass *class)
foreign_class->suffs = vips__foreign_tiff_suffs;

save_class->saveable = VIPS_FOREIGN_SAVEABLE_ANY;
save_class->coding[VIPS_CODING_LABQ] = TRUE;
save_class->coding |= VIPS_FOREIGN_CODING_LABQ;

VIPS_ARG_ENUM(class, "compression", 6,
_("Compression"),
Expand Down
5 changes: 1 addition & 4 deletions libvips/foreign/vipssave.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ extern const char *vips__suffs[];
static void
vips_foreign_save_vips_class_init(VipsForeignSaveVipsClass *class)
{
int i;

GObjectClass *gobject_class = G_OBJECT_CLASS(class);
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
Expand All @@ -144,8 +142,7 @@ vips_foreign_save_vips_class_init(VipsForeignSaveVipsClass *class)
foreign_class->suffs = vips__suffs;

save_class->saveable = VIPS_FOREIGN_SAVEABLE_ANY;
for (i = 0; i < VIPS_CODING_LAST; i++)
save_class->coding[i] = TRUE;
save_class->coding = VIPS_FOREIGN_CODING_ALL;
}

static void
Expand Down
35 changes: 29 additions & 6 deletions libvips/include/vips/foreign.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,28 @@ typedef enum /*< flags >*/ {
VIPS_FOREIGN_SAVEABLE_ALPHA)
} VipsForeignSaveable;

/**
* VipsForeignCoding:
* @VIPS_FOREIGN_CODING_NONE: saver supports [enum@Vips.Coding.NONE]
* @VIPS_FOREIGN_CODING_LABQ: saver supports [enum@Vips.Coding.LABQ]
* @VIPS_FOREIGN_CODING_RAD: saver supports [enum@Vips.Coding.RAD]
* @VIPS_FOREIGN_CODING_ALL: saver supports all coding types
*
* The set of coding types supported by a saver.
*
* ::: seealso
* [enum@Coding].
*/
typedef enum /*< flags >*/ {
VIPS_FOREIGN_CODING_NONE = 1 << 0,
VIPS_FOREIGN_CODING_LABQ = 1 << 1,
VIPS_FOREIGN_CODING_RAD = 1 << 2,

VIPS_FOREIGN_CODING_ALL = (VIPS_FOREIGN_CODING_NONE |
VIPS_FOREIGN_CODING_LABQ |
VIPS_FOREIGN_CODING_RAD)
} VipsForeignCoding;

/**
* VipsForeignKeep:
* @VIPS_FOREIGN_KEEP_NONE: don't attach metadata
Expand All @@ -357,7 +379,7 @@ typedef enum /*< flags >*/ {
VIPS_FOREIGN_KEEP_XMP |
VIPS_FOREIGN_KEEP_IPTC |
VIPS_FOREIGN_KEEP_ICC |
VIPS_FOREIGN_KEEP_OTHER),
VIPS_FOREIGN_KEEP_OTHER)
} VipsForeignKeep;

typedef struct _VipsForeignSave {
Expand Down Expand Up @@ -408,7 +430,7 @@ typedef struct _VipsForeignSaveClass {
*
* @saveable describes the image types that your saver can handle. For
* example, PPM images can have 1 or 3 bands (mono or RGB), so it
* uses VIPS_SAVEABLE_FLAGS_MONO | VIPS_SAVEABLE_FLAGS_RGB.
* uses [flags@Vips.ForeignSaveable.MONO] | [flags@Vips.ForeignSaveable.RGB].
*/
VipsForeignSaveable saveable;

Expand All @@ -420,12 +442,13 @@ typedef struct _VipsForeignSaveClass {
*/
VipsBandFormat *format_table;

/* The set of coding types this format can save. For example, jpeg can
* only save NONE, so has NONE TRUE and RAD and LABQ FALSE.
/* The set of coding types this format can save. For example,
* [method@Image.vipssave] can save all coding types, so it
* uses [flags@Vips.ForeignCoding.ALL]
*
* Default NONE TRUE, RAD and LABQ FALSE.
* Default to [flags@Vips.ForeignCoding.NONE].
*/
gboolean coding[VIPS_CODING_LAST];
VipsForeignCoding coding;
} VipsForeignSaveClass;

VIPS_API
Expand Down
2 changes: 1 addition & 1 deletion libvips/include/vips/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ typedef struct _VipsImagePixels {
} VipsImagePixels;

int vips__foreign_convert_saveable(VipsImage *in, VipsImage **ready,
VipsForeignSaveable saveable, VipsBandFormat *format, VipsCoding *coding,
VipsForeignSaveable saveable, VipsBandFormat *format, VipsForeignCoding coding,
VipsArrayDouble *background);

int vips_foreign_load(const char *filename, VipsImage **out, ...)
Expand Down
Loading