I have written YUYV support for PIL.  YUYV is a format widely used by
crappy webcams.  It's 32 bits for 2 pixels.  The pixels have their own Y
values, but share U and V values.  A patch is attached.

I think it might be possible to add this in pure Python, but I could not
figure out how from the documentation.  Could someone help, so that I
could use PIL before the next release? 

I tested it with my crappy webcam (04f2:b018 Chicony Electronics Co.,
Ltd), and it worked.  
Only in Imaging-yuv/: build
diff -ru Imaging-1.1.6/libImaging/Imaging.h Imaging-yuv/libImaging/Imaging.h
--- Imaging-1.1.6/libImaging/Imaging.h	2006-12-03 06:37:25.000000000 -0500
+++ Imaging-yuv/libImaging/Imaging.h	2009-02-01 19:12:50.000000000 -0500
@@ -459,6 +459,7 @@
 extern void ImagingUnpackBGR(UINT8* out, const UINT8* in, int pixels);
 extern void ImagingUnpackYCC(UINT8* out, const UINT8* in, int pixels);
 extern void ImagingUnpackYCCA(UINT8* out, const UINT8* in, int pixels);
+extern void ImagingUnpackYUYV(UINT8* out, const UINT8* in, int pixels);
 extern void ImagingUnpackYCbCr(UINT8* out, const UINT8* in, int pixels);
 
 extern void ImagingConvertRGB2YCbCr(UINT8* out, const UINT8* in, int pixels);
Only in Imaging-yuv/libImaging: Imaging.h~
diff -ru Imaging-1.1.6/libImaging/Unpack.c Imaging-yuv/libImaging/Unpack.c
--- Imaging-1.1.6/libImaging/Unpack.c	2006-12-03 06:37:25.000000000 -0500
+++ Imaging-yuv/libImaging/Unpack.c	2009-02-01 19:15:58.000000000 -0500
@@ -956,6 +956,7 @@
     {"YCbCr",	"YCbCr;L",	24,	unpackRGBL},
     {"YCbCr",	"YCbCrX",	32,	copy4},
     {"YCbCr",	"YCbCrK",	32,	copy4},
+    {"RGB",	"YUYV", 	16,	ImagingUnpackYUYV},
 
     /* integer variations */
     {"I",	"I",		32,	copy4},
Only in Imaging-yuv/libImaging: Unpack.c~
diff -ru Imaging-1.1.6/libImaging/UnpackYCC.c Imaging-yuv/libImaging/UnpackYCC.c
--- Imaging-1.1.6/libImaging/UnpackYCC.c	2006-12-03 06:37:25.000000000 -0500
+++ Imaging-yuv/libImaging/UnpackYCC.c	2009-02-01 19:34:40.000000000 -0500
@@ -131,6 +131,19 @@
     rgb[2] = (b <= 0) ? 0 : (b >= 255) ? 255 : b;\
 }
 
+#define	YUV2RGB(rgb, y, u, v) {\
+    int r, g, b, C, D, E;\
+    C = y - 16;\
+    D = u - 128;\
+    E = v - 128;\
+    r = ((298 * C + 409 * E + 128) >> 8);		\
+    g = ((298 * C - 100 * D - 208 * E + 128) >> 8);	\
+    b = ((298 * C + 516 * D + 128) >> 8) ;		\
+    rgb[0] = (r <= 0) ? 0 : (r >= 255) ? 255 : r;\
+    rgb[1] = (g <= 0) ? 0 : (g >= 255) ? 255 : g;\
+    rgb[2] = (b <= 0) ? 0 : (b >= 255) ? 255 : b;\
+}
+
 void
 ImagingUnpackYCC(UINT8* out, const UINT8* in, int pixels)
 {
@@ -160,3 +173,18 @@
 	out += 4; in += 4;
     }
 }
+
+void
+ImagingUnpackYUYV(UINT8* out, const UINT8* in, int pixels)
+{
+    int i;
+    /* YUYV */
+    for (i = 0; i < pixels; i+=2) {
+	YUV2RGB(out, in[0], in[1], in[3]);
+	out[A] = 255;
+	out += 4;
+	YUV2RGB(out, in[2], in[1], in[3]);
+	out[A] = 255;
+	out += 4; in += 4;
+    }
+}
Only in Imaging-yuv/libImaging: UnpackYCC.c~
_______________________________________________
Image-SIG maillist  -  Image-SIG@python.org
http://mail.python.org/mailman/listinfo/image-sig

Reply via email to