Commits

Fredrik Lundh committed 3477629

Added WebP read support (simple non-incremental implementation).

  • Participants
  • Parent commits 8918581

Comments (0)

Files changed (2)

File PIL/WebPImagePlugin.py

             raise SyntaxError("not a WebP file")
 
         frame_header = self.fp.read(10)
-        if frame_header[3:6] != "\x9d\x01\x2a":
+        if frame_header[3:6] != "\x9d\x01\x2a" or len(frame_header) != 10:
             raise SyntaxError("unsupported WebP frame signature")
 
         data = map(ord, frame_header)
         self.size = xsize, ysize
         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
+        # FIXME: add support for YCbCr (and RGBA?)
         self.tile = [(d, e, o, a)]
         return self
 

File libImaging/WebPDecode.c

 int
 ImagingWebPDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
 {
-    WEBPSTATE* webpstate = state->context;
+    /* WEBPSTATE* webpstate = state->context; */
+    UINT32 image_size;
+    UINT8* image_data;
+    int y, xsize, ysize;
+    UINT8* p;
 
-    return 0;
+    if (bytes < 20)
+        return 0;
+
+    image_size = (buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24)) + 8;
+
+    if (bytes < image_size)
+        return 0;
+
+    image_data = WebPDecodeRGB(buf, bytes, &xsize, &ysize);
+
+    if (!image_data) {
+        state->errcode = IMAGING_CODEC_BROKEN;
+	return -1;
+    }
+
+    for (y = 0, p = image_data; y < state->ysize; y++, p += state->xsize*3) {
+        state->shuffle((UINT8*) im->image[y + state->yoff] +
+		       state->xoff * im->pixelsize, p,
+		       state->xsize);
+    }
+
+    free(image_data);
+
+    return -1; /* done */
 }
 
 #endif