Now using the rgbT2rgba conversion for L+transparency to RGBA, dropping the palette hack

This commit is contained in:
wiredfool 2013-11-26 13:04:10 -08:00
parent c546c5a4c0
commit 0356741a29
4 changed files with 28 additions and 23 deletions

View File

@ -721,18 +721,8 @@ class Image:
if dither is None: if dither is None:
dither = FLOYDSTEINBERG dither = FLOYDSTEINBERG
# fake a P-mode image, otherwise the transparency will get lost as there is
# currently no other way to convert transparency into an RGBA image
if self.mode == "L" and mode == "RGBA" and "transparency" in self.info:
from PIL import ImagePalette
self.mode = "P"
bytePalette = bytes(bytearray([i//3 for i in range(768)]))
self.palette = ImagePalette.raw("RGB", bytePalette)
self.palette.dirty = 1
self.load()
# Use transparent conversion to promote from transparent color to an alpha channel. # Use transparent conversion to promote from transparent color to an alpha channel.
if self.mode == "RGB" and mode == "RGBA" and "transparency" in self.info: if self.mode in ("L", "RGB") and mode == "RGBA" and "transparency" in self.info:
return self._new(self.im.convert_transparent(mode, self.info['transparency'])) return self._new(self.im.convert_transparent(mode, self.info['transparency']))
try: try:

View File

@ -182,6 +182,10 @@ def test_save_l_transparency():
file = tempfile("temp.png") file = tempfile("temp.png")
assert_no_exception(lambda: im.save(file)) assert_no_exception(lambda: im.save(file))
# There are 559 transparent pixels.
im = im.convert('RGBA')
assert_equal(im.split()[3].getcolors()[0][0], 559)
def test_save_rgb_single_transparency(): def test_save_rgb_single_transparency():
in_file = "Tests/images/caption_6_33_22.png" in_file = "Tests/images/caption_6_33_22.png"
im = Image.open(in_file) im = Image.open(in_file)

View File

@ -799,12 +799,15 @@ _convert_transparent(ImagingObject* self, PyObject* args)
{ {
char* mode; char* mode;
int r,g,b; int r,g,b;
if (!PyArg_ParseTuple(args, "s(iii)", &mode, &r, &g, &b)) { if (PyArg_ParseTuple(args, "s(iii)", &mode, &r, &g, &b)) {
return NULL;
}
return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, g, b)); return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, g, b));
} }
PyErr_Clear();
if (PyArg_ParseTuple(args, "si", &mode, &r)) {
return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, 0, 0));
}
return NULL;
}
static PyObject* static PyObject*
_copy(ImagingObject* self, PyObject* args) _copy(ImagingObject* self, PyObject* args)

View File

@ -319,7 +319,7 @@ rgba2rgbA(UINT8* out, const UINT8* in, int xsize)
*/ */
static void static void
rgbT2rgba(UINT8* out, const UINT8* in, int xsize, int r, int g, int b) rgbT2rgba(UINT8* out, int xsize, int r, int g, int b)
{ {
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
UINT32 trns = ((r & 0xff)<<24) | ((g & 0xff)<<16) | ((b & 0xff)<<8) | 0xff; UINT32 trns = ((r & 0xff)<<24) | ((g & 0xff)<<16) | ((b & 0xff)<<8) | 0xff;
@ -332,8 +332,6 @@ rgbT2rgba(UINT8* out, const UINT8* in, int xsize, int r, int g, int b)
UINT32* tmp = (UINT32 *)out; UINT32* tmp = (UINT32 *)out;
int i; int i;
rgb2rgba(out, in, xsize);
for (i=0; i < xsize; i++ ,tmp++) { for (i=0; i < xsize; i++ ,tmp++) {
if (tmp[0]==trns) { if (tmp[0]==trns) {
tmp[0]=repl; tmp[0]=repl;
@ -342,7 +340,6 @@ rgbT2rgba(UINT8* out, const UINT8* in, int xsize, int r, int g, int b)
} }
/* ---------------- */ /* ---------------- */
/* CMYK conversions */ /* CMYK conversions */
/* ---------------- */ /* ---------------- */
@ -1197,6 +1194,7 @@ ImagingConvertTransparent(Imaging imIn, const char *mode,
int r, int g, int b) int r, int g, int b)
{ {
ImagingSectionCookie cookie; ImagingSectionCookie cookie;
ImagingShuffler convert;
Imaging imOut = NULL; Imaging imOut = NULL;
int y; int y;
@ -1204,7 +1202,9 @@ ImagingConvertTransparent(Imaging imIn, const char *mode,
return (Imaging) ImagingError_ModeError(); return (Imaging) ImagingError_ModeError();
} }
if (!(strcmp(imIn->mode, "RGB") == 0 && strcmp(mode, "RGBA") == 0)) if (!((strcmp(imIn->mode, "RGB") == 0 ||
strcmp(imIn->mode, "L") == 0)
&& strcmp(mode, "RGBA") == 0))
#ifdef notdef #ifdef notdef
{ {
return (Imaging) ImagingError_ValueError("conversion not supported"); return (Imaging) ImagingError_ValueError("conversion not supported");
@ -1218,6 +1218,12 @@ ImagingConvertTransparent(Imaging imIn, const char *mode,
} }
#endif #endif
if (strcmp(imIn->mode, "RGB") == 0) {
convert = rgb2rgba;
} else {
convert = l2rgb;
g = b = r;
}
imOut = ImagingNew2(mode, imOut, imIn); imOut = ImagingNew2(mode, imOut, imIn);
if (!imOut){ if (!imOut){
@ -1225,9 +1231,11 @@ ImagingConvertTransparent(Imaging imIn, const char *mode,
} }
ImagingSectionEnter(&cookie); ImagingSectionEnter(&cookie);
for (y = 0; y < imIn->ysize; y++) for (y = 0; y < imIn->ysize; y++) {
rgbT2rgba((UINT8*) imOut->image[y], (UINT8*) imIn->image[y], (*convert)((UINT8*) imOut->image[y], (UINT8*) imIn->image[y],
imIn->xsize, r, g, b); imIn->xsize);
rgbT2rgba((UINT8*) imOut->image[y], imIn->xsize, r, g, b);
}
ImagingSectionLeave(&cookie); ImagingSectionLeave(&cookie);
return imOut; return imOut;