Commits

Fredrik Lundh  committed d007573

Added WebP draft mode scaffolding.

  • Participants
  • Parent commits bfa0131

Comments (0)

Files changed (5)

File PIL/WebPImagePlugin.py

         self.tile = [("webp", (0, 0) + self.size, 0, (self.mode,))]
 
     def draft(self, mode, size):
+        """Configure image decoder."""
         if len(self.tile) != 1:
             return
         d, e, o, a = self.tile[0]
-        # FIXME: add support for YCbCr (and RGBA?)
+        if a[0] == "RGB" and mode in ["YCbCr"]:
+            self.mode = mode
+            a = (self.mode,)
         self.tile = [(d, e, o, a)]
         return self
 

File Tests/test_file_webp.py

     assert_equal(im1.mode, im2.mode)
     assert_equal(im1.size, im2.size)
 
+# def test_draft():
+#     # test draft mode
+#     im = Image.open("Images/lena.webp")
+#     px = im.getpixel((0, 0))
+#     im = Image.open("Images/lena.webp")
+#     im.draft("YCbCr", im.size)
+#     assert_equal(im.mode, "YCbCr")
+#     assert_equal(im.size, (128, 128))
+#     im.load()
+#     assert_equal(im.mode, "YCbCr")
+#     assert_equal(im.size, (128, 128))
+#     # make sure the codec doesn't read RGB
+#     assert_false(px == im.getpixel((0, 0)))
+
 def test_quality():
     # test saving with different qualities
     im80 = roundtrip(lena(), quality=80)
 /* WebP                                                                 */
 /* -------------------------------------------------------------------- */
 
+#ifdef HAVE_LIBWEBP
+
 PyObject*
 PyImaging_WebPDecoderNew(PyObject* self, PyObject* args)
 {
     if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode))
 	return NULL;
 
-    decoder = PyImaging_DecoderNew(0);
+    decoder = PyImaging_DecoderNew(sizeof(WEBPCONTEXT));
     if (decoder == NULL)
 	return NULL;
 
     if (get_unpacker(decoder, mode, rawmode) < 0)
 	return NULL;
 
+    strncpy(((WEBPCONTEXT*)decoder->state.context)->rawmode, rawmode, 8);
+
     decoder->decode = ImagingWebPDecode;
 
     return (PyObject*) decoder;
 }
 
+#endif
+
 /* -------------------------------------------------------------------- */
 /* JPEG									*/
 /* -------------------------------------------------------------------- */

File libImaging/WebP.h

 /* WebP.h */
 
 #if PY_VERSION_HEX < 0x02040000
+/* Newer versions of Python.h imports this. */
 #include <stdint.h>
 #endif
 
+/* FIXME: Split CONTEXT into encoder and decoder contexts. */
+
 typedef struct {
 
     /* CONFIGURATION */
 
-    /* Quality (1-100, 0 means default) */
+    /* Converter output mode (input to the shuffler).  If empty,
+       convert conversions are disabled.  Used when decoding. */
+    char rawmode[8+1];
+
+    /* Quality (1-100, 0 means default).  Used when encoding. */
     int quality;
 
     /* INTERNAL */

File libImaging/WebPDecode.c

 int
 ImagingWebPDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
 {
+    WEBPCONTEXT* context = (WEBPCONTEXT*) state->context;
+
     UINT32 image_size;
     UINT8* image_data;
     int y, xsize, ysize;
         return 0;
 
     /* decode and unpack everything in one go */
-
-    image_data = WebPDecodeRGB(buf, bytes, &xsize, &ysize);
+    if (strcmp(context->rawmode, "RGB") == 0)
+        image_data = WebPDecodeRGB(buf, bytes, &xsize, &ysize);
+    else {
+        /* FIXME: add YCbCr support */
+        state->errcode = IMAGING_CODEC_CONFIG;
+	return -1;
+    }
 
     if (!image_data) {
         state->errcode = IMAGING_CODEC_BROKEN;