diff --git a/ChangeLog b/ChangeLog index 4a1dccb6..9c595816 100644 --- a/ChangeLog +++ b/ChangeLog @@ -21,6 +21,7 @@ - revise i18n configure, get rid of intltool - command-line IMAGVEC input could segv with non-vips image output - added .tobuffer()/.frombuffer(), .tostring()/.fromstring() to Python binding +- add PIL_mode_from_vips () and vips_from_PIL_mode () utility functions 25/1/08 started 7.14.0 - bump all version numbers for new stable diff --git a/python/vipsCC/VImage.i b/python/vipsCC/VImage.i index aa58e711..69bac098 100644 --- a/python/vipsCC/VImage.i +++ b/python/vipsCC/VImage.i @@ -6,6 +6,8 @@ * 3/8/08 * - add .tobuffer() / .frombuffer (), .tostring (), .fromstring () * methods + * - add PIL_mode_from_vips () and vips_from_PIL_mode () utility + * functions */ %module VImage @@ -142,6 +144,46 @@ public: } } +%pythoncode %{ +# try to guess a PIL mode string from a VIPS image +def PIL_mode_from_vips (vim): + if vim.Bands () == 3 and vim.BandFmt () == VImage.FMTUCHAR: + return 'RGB' + elif vim.Bands () == 4 and vim.BandFmt () == VImage.FMTUCHAR and vim.Type == VImage.VImage.RGB: + return 'RGBA' + elif vim.Bands () == 4 and vim.BandFmt () == VImage.FMTUCHAR and vim.Type == VImage.CMYK: + return 'CMYK' + elif vim.Bands () == 1 and vim.BandFmt () == VImage.FMTUCHAR: + return 'L' + elif vim.Bands () == 1 and vim.BandFmt () == VImage.FMTINT: + return 'I' + elif vim.Bands () == 1 and vim.BandFmt () == VImage.FMTFLOAT: + return 'F' + elif vim.Bands () == 2 and vim.BandFmt () == VImage.FMTUCHAR: + return 'LA' + else: + raise ValueError ('unsupported vips -> pil image') + +# return vips (bands, format, type) for a PIL mode +def vips_from_PIL_mode (mode): + if mode == 'RGB': + return (3, VImage.FMTUCHAR, VImage.RGB) + elif mode == 'RGBA': + return (4, VImage.FMTUCHAR, VImage.RGB) + elif mode == 'CMYK': + return (4, VImage.FMTUCHAR, VImage.CMYK) + elif mode == 'L': + return (1, VImage.FMTUCHAR, VImage.B_W) + elif mode == 'I': + return (1, VImage.FMTINT, VImage.B_W) + elif mode == 'F': + return (1, VImage.FMTFLOAT, VImage.B_W) + elif mode == 'LA': + return (2, VImage.FMTUCHAR, VImage.B_W) + else: + raise ValueError ('unsupported pil -> vips image') +%} + /* Helper code for vips_init(). */ %{ @@ -152,42 +194,42 @@ public: /* Command-line args during parse. */ typedef struct _Args { - /* The n strings we alloc when we get from Python. - */ - int n; - char **str; + /* The n strings we alloc when we get from Python. + */ + int n; + char **str; - /* argc/argv as processed by us. - */ - int argc; - char **argv; + /* argc/argv as processed by us. + */ + int argc; + char **argv; } Args; #ifdef DEBUG static void args_print (Args *args) { - int i; + int i; - printf ("args_print: argc = %d\n", args->argc); - // +1 so we print the trailing NULL too - for (i = 0; i < args->argc + 1; i++) - printf ("\t%2d)\t%s\n", i, args->argv[i]); + printf ("args_print: argc = %d\n", args->argc); + // +1 so we print the trailing NULL too + for (i = 0; i < args->argc + 1; i++) + printf ("\t%2d)\t%s\n", i, args->argv[i]); } #endif /*DEBUG*/ static void args_free (Args *args) { - int i; + int i; - for (i = 0; i < args->n; i++) - IM_FREE (args->str[i]); - args->n = 0; - args->argc = 0; - IM_FREE (args->str); - IM_FREE (args->argv); - IM_FREE (args); + for (i = 0; i < args->n; i++) + IM_FREE (args->str[i]); + args->n = 0; + args->argc = 0; + IM_FREE (args->str); + IM_FREE (args->argv); + IM_FREE (args); } /* Get argv/argc from python. @@ -195,100 +237,99 @@ args_free (Args *args) static Args * args_new (void) { - Args *args; - PyObject *av; - int i; - int n; + Args *args; + PyObject *av; + int i; + int n; - args = g_new (Args, 1); - args->n = 0; - args->str = NULL; - args->argc = 0; - args->argv = NULL; + args = g_new (Args, 1); + args->n = 0; + args->str = NULL; + args->argc = 0; + args->argv = NULL; - if (!(av = PySys_GetObject ((char *) "argv"))) - return (args); - if (!PyList_Check (av)) { - PyErr_Warn (PyExc_Warning, "ignoring sys.argv: " - "it must be a list of strings"); - return (args); - } + if (!(av = PySys_GetObject ((char *) "argv"))) + return (args); + if (!PyList_Check (av)) { + PyErr_Warn (PyExc_Warning, "ignoring sys.argv: " + "it must be a list of strings"); + return args; + } - n = PyList_Size (av); - args->str = g_new (char *, n); - for (i = 0; i < n; i++) - args->str[i] = g_strdup - (PyString_AsString (PyList_GetItem (av, i))); - args->n = n; + n = PyList_Size (av); + args->str = g_new (char *, n); + for (i = 0; i < n; i++) + args->str[i] = g_strdup (PyString_AsString (PyList_GetItem (av, i))); + args->n = n; - /* +1 for NULL termination. - */ - args->argc = n; - args->argv = g_new (char *, n + 1); - for (i = 0; i < n; i++) - args->argv[i] = args->str[i]; - args->argv[i] = NULL; + /* +1 for NULL termination. + */ + args->argc = n; + args->argv = g_new (char *, n + 1); + for (i = 0; i < n; i++) + args->argv[i] = args->str[i]; + args->argv[i] = NULL; - return (args); + return args; } static void vips_fatal (const char *msg) { - char buf[256]; + char buf[256]; - im_snprintf (buf, 256, "%s\n%s", msg, im_error_buffer()); - im_error_clear (); - Py_FatalError (buf); + im_snprintf (buf, 256, "%s\n%s", msg, im_error_buffer()); + im_error_clear (); + Py_FatalError (buf); } %} %init %{ { - Args *args; + Args *args; - args = args_new (); + args = args_new (); #ifdef DEBUG - printf ("on startup:\n"); - args_print (args); + printf ("on startup:\n"); + args_print (args); #endif /*DEBUG*/ - if (im_init_world (args->argv[0])) { - args_free (args); - vips_fatal ("can't initialise module vips"); - } + if (im_init_world (args->argv[0])) { + args_free (args); + vips_fatal ("can't initialise module vips"); + } - /* Now parse any GOptions. - */ - GError *error = NULL; - GOptionContext *context; + /* Now parse any GOptions. + */ + GError *error = NULL; + GOptionContext *context; - context = g_option_context_new ("- vips"); - g_option_context_add_group (context, im_get_option_group()); + context = g_option_context_new ("- vips"); + g_option_context_add_group (context, im_get_option_group()); - g_option_context_set_ignore_unknown_options (context, TRUE); - if( !g_option_context_parse (context, - &args->argc, &args->argv, &error)) { - g_option_context_free (context); - args_free (args); - im_error ("vipsmodule", "%s", error->message); - g_error_free (error); - vips_fatal ("can't initialise module vips"); - } - g_option_context_free (context); + g_option_context_set_ignore_unknown_options (context, TRUE); + if (!g_option_context_parse (context, + &args->argc, &args->argv, &error)) { + g_option_context_free (context); + args_free (args); + im_error ("vipsmodule", "%s", error->message); + g_error_free (error); + vips_fatal ("can't initialise module vips"); + } + g_option_context_free (context); #ifdef DEBUG - printf ("after parse:\n"); - args_print (args); + printf ("after parse:\n"); + args_print (args); #endif /*DEBUG*/ - // Write (possibly) modified argc/argv back again. - if (args->argv) - PySys_SetArgv (args->argc, args->argv); + // Write (possibly) modified argc/argv back again. + if (args->argv) + PySys_SetArgv (args->argc, args->argv); - args_free (args); + args_free (args); } %}