Fredrik Lundh avatar Fredrik Lundh committed 874f700 Merge

Merge with CMS branch.

Comments (0)

Files changed (5)

 import Image
 import _imagingcms
 
-cmscore = _imagingcms
+core = _imagingcms
 
 #
 # intent/direction values
         # accepts a string (filename), a file-like object, or a low-level
         # profile object
         if Image.isStringType(profile):
-            self._set(cmscore.profile_open(profile))
+            self._set(core.profile_open(profile), profile)
         elif hasattr(profile, "read"):
-            self._set(cmscore.profile_fromstring(profile.read()))
+            self._set(core.profile_fromstring(profile.read()))
         else:
             self._set(profile) # assume it's already a profile
 
-    def _set(self, profile):
+    def _set(self, profile, filename=None):
         self.profile = profile
+        self.filename = filename
         if profile:
             self.product_name = profile.product_name
             self.product_info = profile.product_info
                  intent=INTENT_PERCEPTUAL,
                  proof=None, proof_intent=INTENT_ABSOLUTE_COLORIMETRIC, flags=0):
         if proof is None:
-            self.transform = cmscore.buildTransform(
+            self.transform = core.buildTransform(
                 input.profile, output.profile,
                 input_mode, output_mode,
                 intent,
                 flags
                 )
         else:
-            self.transform = cmscore.buildProofTransform(
+            self.transform = core.buildProofTransform(
                 input.profile, output.profile, proof.profile,
                 input_mode, output_mode,
                 intent, proof_intent,
         result = self.transform.apply(im.im.id, im.im.id)
         return im
 
+##
+# (experimental) Fetches the profile for the current display device.
+# Returns None if the profile is not known.
+
+def get_display_profile(handle=None):
+    import sys
+    if sys.platform == "win32":
+        import ImageWin
+        if isinstance(handle, ImageWin.HDC):
+            profile = core.get_display_profile_win32(handle, 1)
+        else:
+            profile = core.get_display_profile_win32(handle or 0)
+    else:
+        try:
+            get = _imagingcms.get_display_profile
+        except AttributeError:
+            return None
+        else:
+            profile = get()
+    return ImageCmsProfile(profile)
+
 # --------------------------------------------------------------------.
 # pyCMS compatible layer
 # --------------------------------------------------------------------.
             raise PyCMSError("Color temperature must be a positive integer, \"%s\" not valid" % colorTemp)
 
     try:
-        return cmscore.createProfile(colorSpace, colorTemp)
+        return core.createProfile(colorSpace, colorTemp)
     except (TypeError, ValueError), v:
         raise PyCMSError(v)
 
 
 def versions():
     import sys
-    pycms, lcms = cmscore.versions()
-    return (pycms, "%d.%d" % divmod(lcms, 100),
-            sys.version.split()[0], Image.VERSION)
+    return (VERSION, core.getversion(), sys.version.split()[0], Image.VERSION)
 
 # --------------------------------------------------------------------
 

Tests/test_imagecms.py

     assert_equal(v[0], '0.1.0 pil')
     assert_equal(map(type, v), [str, str, str, str])
 
+    v = ImageCms.core.getversion() # should return a "major.minor" string
+    assert_match("\d+\.\d+$", v)
+
     i = ImageCms.profileToProfile(lena(), SRGB, SRGB)
     assert_image(i, "RGB", (128, 128))
 
 
     # test PointTransform convenience API
     im = lena().point(t)
+
+    # try fetching the profile for the current display device
+    assert_no_exception(lambda: ImageCms.get_display_profile())
     else:
         failure(msg or "got %r, expected %r" % (a, b))
 
+def assert_match(p, s, msg=None):
+    import re
+    if re.match(p, s):
+        success()
+    else:
+        failure(msg or "got %r, doesn't match pattern %r" % (s, p))
+
 def assert_exception(exc_class, func):
     import sys, traceback
     try:
 #include "lcms.h"
 #include "Imaging.h"
 
+#ifdef WIN32
+#include <wingdi.h>
+#endif
+
 #define PYCMSVERSION "0.1.0 pil"
 
 /* version history */
 /* Python callable functions */
 
 static PyObject *
-versions (PyObject *self, PyObject *args)
+getversion(PyObject *self, PyObject *args)
 {
-  return Py_BuildValue("si", PYCMSVERSION, LCMS_VERSION);
+  return PyString_FromFormat("%d.%d", LCMS_VERSION / 100, LCMS_VERSION % 100);
 }
 
 static PyObject *
   return PyInt_FromLong(result != 0);
 }
 
+#ifdef WIN32
+static PyObject *
+cms_get_display_profile_win32(PyObject* self, PyObject* args)
+{
+  char filename[MAX_PATH];
+  DWORD filename_size;
+  BOOL ok;
+
+  int handle = 0;
+  int is_dc = 0;
+  if (!PyArg_ParseTuple(args, "|ii:get_display_profile", &handle, &is_dc))
+    return NULL;
+
+  filename_size = sizeof(filename);
+
+  if (is_dc) {
+    ok = GetICMProfile((HDC) handle, &filename_size, filename);
+  } else {
+    HDC dc = GetDC((HWND) handle);
+    ok = GetICMProfile(dc, &filename_size, filename);
+    ReleaseDC((HWND) handle, dc);
+  }
+
+  if (ok)
+    return PyString_FromStringAndSize(filename, filename_size-1);
+
+  Py_INCREF(Py_None);
+  return Py_None;
+}
+#endif
+
 /* -------------------------------------------------------------------- */
 /* Python interface setup */
 
   {"profile_open", cms_profile_open, 1},
   {"profile_fromstring", cms_profile_fromstring, 1},
 
-  /* pyCMS info */
-  {"versions", versions, 1},
-
   /* profile and transform functions */
   {"buildTransform", buildTransform, 1},
   {"buildProofTransform", buildProofTransform, 1},
   {"createProfile", createProfile, 1},
 
+  /* platform specific tools */
+#ifdef WIN32
+  {"get_display_profile_win32", cms_get_display_profile_win32, 1},
+#endif
+
+  {"getversion", getversion, 1},
+
   {NULL, NULL}
 };
 
         if os.path.isfile("_imagingcms.c") and feature.lcms:
             extra = []
             if sys.platform == "win32":
-                extra.append("user32") # FIXME: littlecms build error?
+                extra.extend(["user32", "gdi32"])
             exts.append(Extension(
                 "_imagingcms", ["_imagingcms.c"], libraries=["lcms"] + extra
                 ))
             ],
         cmdclass = {"build_ext": pil_build_ext},
         description=DESCRIPTION,
-        download_url="http://effbot.org/zone/pil-changes-116.htm",
+        download_url="http://effbot.org/downloads",
         ext_modules = [Extension("_imaging", ["_imaging.c"])], # dummy
         extra_path = "PIL",
         license="Python (MIT style)",
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.