Fredrik Lundh avatar Fredrik Lundh committed 8918581

Added libwebp binding boilerplate.

Comments (0)

Files changed (12)

 Imaging/libImaging/Jpeg.h
 Imaging/libImaging/Lzw.h
 Imaging/libImaging/Raw.h
+Imaging/libImaging/WebP.h
 Imaging/libImaging/Zip.h
 Imaging/libImaging/BitDecode.c
 Imaging/libImaging/EpsEncode.c
 Imaging/libImaging/RawEncode.c
 Imaging/libImaging/SunRleDecode.c
 Imaging/libImaging/TgaRleDecode.c
+Imaging/libImaging/WebPDecode.c
+Imaging/libImaging/WebPEncode.c
 Imaging/libImaging/XbmDecode.c
 Imaging/libImaging/XbmEncode.c
 Imaging/libImaging/ZipDecode.c

PIL/WebPImagePlugin.py

         tag = data[0] | (data[1] << 8) | (data[2] << 16)
 
         frame_type = (tag & 1) # 0=key frame, 1=interframe
-        version = (((tag >> 1) & 7) > 3)
+        version = (((tag >> 1) & 7) > 3) # scaling filter
         show_frame = ((tag >> 4) & 1)
         partition_size = (tag >> 5)
 
 
         self.mode = "RGB" # file is always YCbCr (w. chroma subsampling)
         self.size = xsize, ysize
-        self.tile = [("webp", (0, 0) + self.size, 0, (self.mode, 0, 1))]
+        self.tile = [("webp", (0, 0) + self.size, 0, (self.mode,))]
+
+        # make sure the decoder sees the data we've already parsed
+        self.tile_prefix = container_header + frame_header
+
+    def draft(self, mode, size):
+        if len(self.tile) != 1:
+            return
+        d, e, o, a = self.tile[0]
+        # FIXME: add support for RGBA and YCbCr
+        self.tile = [(d, e, o, a)]
+        return self
 
 #
 # --------------------------------------------------------------------

Tests/test_file_webp.py

 
 codecs = dir(Image.core)
 
-# if "webp_decoder" not in codecs:
-#     skip("webp support not available")
+if "webp_decoder" not in codecs or "webp_encoder" not in codecs:
+    skip("webp support not available")
 
 def test_sanity():
 
     # assert_match(Image.core.webp_version, "\d+\.\d+\.\d+(\.\d+)?$")
 
     im = Image.open("Images/lena.webp")
-    # im.load()
+    im.load()
     assert_equal(im.mode, "RGB")
     assert_equal(im.size, (128, 128))
     assert_equal(im.format, "WEBP")
 extern PyObject* PyImaging_RawDecoderNew(PyObject* self, PyObject* args);
 extern PyObject* PyImaging_SunRleDecoderNew(PyObject* self, PyObject* args);
 extern PyObject* PyImaging_TgaRleDecoderNew(PyObject* self, PyObject* args);
+extern PyObject* PyImaging_WebPDecoderNew(PyObject* self, PyObject* args);
 extern PyObject* PyImaging_XbmDecoderNew(PyObject* self, PyObject* args);
 extern PyObject* PyImaging_ZipDecoderNew(PyObject* self, PyObject* args);
 
 extern PyObject* PyImaging_JpegEncoderNew(PyObject* self, PyObject* args);
 extern PyObject* PyImaging_PcxEncoderNew(PyObject* self, PyObject* args);
 extern PyObject* PyImaging_RawEncoderNew(PyObject* self, PyObject* args);
+extern PyObject* PyImaging_WebPEncoderNew(PyObject* self, PyObject* args);
 extern PyObject* PyImaging_XbmEncoderNew(PyObject* self, PyObject* args);
 extern PyObject* PyImaging_ZipEncoderNew(PyObject* self, PyObject* args);
 
     {"raw_encoder", (PyCFunction)PyImaging_RawEncoderNew, METH_VARARGS},
     {"sun_rle_decoder", (PyCFunction)PyImaging_SunRleDecoderNew, METH_VARARGS},
     {"tga_rle_decoder", (PyCFunction)PyImaging_TgaRleDecoderNew, METH_VARARGS},
+#ifdef HAVE_LIBWEBP
+    {"webp_decoder", (PyCFunction)PyImaging_WebPDecoderNew, METH_VARARGS},
+    {"webp_encoder", (PyCFunction)PyImaging_WebPEncoderNew, METH_VARARGS},
+#endif
     {"xbm_decoder", (PyCFunction)PyImaging_XbmDecoderNew, METH_VARARGS},
     {"xbm_encoder", (PyCFunction)PyImaging_XbmEncoderNew, METH_VARARGS},
 #ifdef HAVE_LIBZ
   }
 #endif
 
+#ifdef HAVE_WEBP
+  /* FIXME */
+#endif
+
 #ifdef HAVE_LIBZ
 #include "zlib.h"
   /* zip encoding strategies */
 #include "Lzw.h"
 #include "Raw.h"
 #include "Bit.h"
+#include "WebP.h"
 
 
 /* -------------------------------------------------------------------- */
 
 
 /* -------------------------------------------------------------------- */
+/* WebP                                                                 */
+/* -------------------------------------------------------------------- */
+
+PyObject*
+PyImaging_WebPDecoderNew(PyObject* self, PyObject* args)
+{
+    ImagingDecoderObject* decoder;
+
+    char* mode;
+    char* rawmode;
+    if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode))
+	return NULL;
+
+    decoder = PyImaging_DecoderNew(0);
+    if (decoder == NULL)
+	return NULL;
+
+    if (get_unpacker(decoder, mode, rawmode) < 0)
+	return NULL;
+
+    decoder->decode = ImagingWebPDecode;
+
+    return (PyObject*) decoder;
+}
+
+/* -------------------------------------------------------------------- */
 /* JPEG									*/
 /* -------------------------------------------------------------------- */
 
 
 
 /* -------------------------------------------------------------------- */
+/* WEBP									*/
+/* -------------------------------------------------------------------- */
+
+#ifdef HAVE_LIBWEBP
+
+#include "WebP.h"
+
+PyObject*
+PyImaging_WebPEncoderNew(PyObject* self, PyObject* args)
+{
+    ImagingEncoderObject* encoder;
+
+    char* mode;
+    char* rawmode;
+    if (!PyArg_ParseTuple(args, ARG("ss", "ss"), &mode, &rawmode))
+	return NULL;
+
+    encoder = PyImaging_EncoderNew(sizeof(WEBPSTATE));
+    if (encoder == NULL)
+	return NULL;
+
+    if (get_packer(encoder, mode, rawmode) < 0)
+	return NULL;
+
+    encoder->encode = ImagingWebPEncode;
+
+    return (PyObject*) encoder;
+}
+#endif
+
+
+/* -------------------------------------------------------------------- */
 /* ZIP									*/
 /* -------------------------------------------------------------------- */
 

libImaging/Imaging.h

 			       UINT8* buffer, int bytes);
 extern int ImagingTgaRleDecode(Imaging im, ImagingCodecState state,
 			       UINT8* buffer, int bytes);
+#ifdef	HAVE_LIBWEBP
+extern int ImagingWebPDecode(Imaging im, ImagingCodecState state,
+			     UINT8* buffer, int bytes);
+extern int ImagingWebPEncode(Imaging im, ImagingCodecState state,
+			     UINT8* buffer, int bytes);
+#endif
 extern int ImagingXbmDecode(Imaging im, ImagingCodecState state,
 			    UINT8* buffer, int bytes);
 extern int ImagingXbmEncode(Imaging im, ImagingCodecState state,

libImaging/WebP.h

+/* WebP.h */
+
+typedef struct {
+
+    /* CONFIGURATION */
+
+    /* INTERNAL */
+
+} WEBPSTATE;

libImaging/WebPDecode.c

+/*
+ * The Python Imaging Library.
+ * $Id$
+ *
+ * WebP decoder
+ *
+ * history:
+ * 2011-06-26 fl  created
+ *
+ * Copyright (c) Fredrik Lundh 2011.
+ *
+ * See the README file for information on usage and redistribution.
+ */
+
+#include "Imaging.h"
+
+#ifdef HAVE_LIBWEBP
+
+#include "WebP.h"
+#include "webp/decode.h"
+
+int
+ImagingWebPDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
+{
+    WEBPSTATE* webpstate = state->context;
+
+    return 0;
+}
+
+#endif

libImaging/WebPEncode.c

+/*
+ * The Python Imaging Library.
+ * $Id$
+ *
+ * encoder for WebP data
+ *
+ * history:
+ * 2011-06-26 fl  created
+ *
+ * Copyright (c) Fredrik Lundh 2011.
+ *
+ * See the README file for information on usage and redistribution.
+ */
+
+#include "Imaging.h"
+
+#ifdef HAVE_LIBWEBP
+
+#include "WebP.h"
+#include "webp/encode.h"
+
+int
+ImagingWebPEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
+{
+  return 0;
+}
+
+#endif
     check_module("PIL CORE", "PIL._imaging")
     check_module("TKINTER", "PIL._imagingtk")
     check_codec("JPEG", "jpeg", "jpeglib_version")
+    check_codec("WEBP", "webp", "webplib_version")
     check_codec("ZLIB (PNG/ZIP)", "zip", "zlib_version")
     check_module("FREETYPE2", "PIL._imagingft", "freetype2_version")
     check_module("LITTLECMS", "PIL._imagingcms", "littlecms_version")
 
 TCL_ROOT = None
 JPEG_ROOT = None
+TIFF_ROOT = None
+WEBP_ROOT = None
 ZLIB_ROOT = None
-TIFF_ROOT = None
 FREETYPE_ROOT = None
 LCMS_ROOT = None
 
     "PackDecode", "Palette", "Paste", "Quant", "QuantOctree", "QuantHash",
     "QuantHeap", "PcdDecode", "PcxDecode", "PcxEncode", "Point",
     "RankFilter", "RawDecode", "RawEncode", "Storage", "SunRleDecode",
-    "TgaRleDecode", "Unpack", "UnpackYCC", "UnsharpMask", "XbmDecode",
-    "XbmEncode", "ZipDecode", "ZipEncode"
+    "TgaRleDecode", "Unpack", "UnpackYCC", "UnsharpMask", "WebPDecode",
+    "WebPEncode", "XbmDecode", "XbmEncode", "ZipDecode", "ZipEncode"
     ]
 
 # --------------------------------------------------------------------
         # look for available libraries
 
         class feature:
-            zlib = jpeg = tiff = freetype = tcl = tk = lcms = None
+            jpeg = tiff = webp = zlib = freetype = tcl = tk = lcms = None
         feature = feature()
 
-        if find_include_file(self, "zlib.h"):
-            if find_library_file(self, "z"):
-                feature.zlib = "z"
-            elif sys.platform == "win32" and find_library_file(self, "zlib"):
-                feature.zlib = "zlib" # alternative name
-
         if find_include_file(self, "jpeglib.h"):
             if find_library_file(self, "jpeg"):
                 feature.jpeg = "jpeg"
         if find_library_file(self, "tiff"):
             feature.tiff = "tiff"
 
+        if find_include_file(self, "webp/decode.h") and find_include_file(self, "webp/encode.h"):
+            if find_library_file(self, "webp"):
+                feature.webp = "webp"
+
+        if find_include_file(self, "zlib.h"):
+            if find_library_file(self, "z"):
+                feature.zlib = "z"
+            elif sys.platform == "win32" and find_library_file(self, "zlib"):
+                feature.zlib = "zlib" # alternative name
+
         if find_library_file(self, "freetype"):
             # look for freetype2 include files
             freetype_version = 0
         if feature.jpeg:
             libs.append(feature.jpeg)
             defs.append(("HAVE_LIBJPEG", None))
+        if feature.webp:
+            libs.append(feature.webp)
+            defs.append(("HAVE_LIBWEBP", None))
         if feature.zlib:
             libs.append(feature.zlib)
             defs.append(("HAVE_LIBZ", None))
         options = [
             (feature.tcl and feature.tk, "TKINTER"),
             (feature.jpeg, "JPEG"),
+            (feature.webp, "WEBP"),
             (feature.zlib, "ZLIB (PNG/ZIP)"),
             # (feature.tiff, "experimental TIFF G3/G4 read"),
             (feature.freetype, "FREETYPE2"),
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.