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:
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.
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']))
try:

View File

@ -182,6 +182,10 @@ def test_save_l_transparency():
file = tempfile("temp.png")
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():
in_file = "Tests/images/caption_6_33_22.png"
im = Image.open(in_file)

View File

@ -799,11 +799,14 @@ _convert_transparent(ImagingObject* self, PyObject* args)
{
char* mode;
int r,g,b;
if (!PyArg_ParseTuple(args, "s(iii)", &mode, &r, &g, &b)) {
return NULL;
if (PyArg_ParseTuple(args, "s(iii)", &mode, &r, &g, &b)) {
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*

View File

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