remove white matting when loading transparent PSD

This commit is contained in:
Sean Barrett 2016-04-02 03:40:55 -07:00
parent a8876b884d
commit 591c7f8cb3

View File

@ -201,10 +201,11 @@
Janez Zemva John Bartholomew Michal Cichon svdijk@github
Jonathan Blow Ken Hamada Tero Hanninen Baldur Karlsson
Laurent Gomila Cort Stratton Sergio Gonzalez romigrou@github
Aruelien Pocheville Thibault Reuille Cass Everitt
Aruelien Pocheville Thibault Reuille Cass Everitt
Ryamond Barbiero Paul Du Bois Engin Manap
Michaelangel007@github Oriol Ferrer Mesia
Blazej Dariusz Roszkowski
Michaelangel007@github
Oriol Ferrer Mesia
LICENSE
@ -5453,6 +5454,21 @@ static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int
}
}
if (channelCount >= 3) {
for (i=0; i < w*h; ++i) {
unsigned char *pixel = out + 4*i;
if (pixel[3] != 0 && pixel[3] != 255) {
// remove weird white matte from PSD
float a = pixel[3] / 255.0f;
float ra = 1.0f / a;
float inv_a = 255.0f * (1 - ra);
pixel[0] = (unsigned char) (pixel[0]*ra + inv_a);
pixel[1] = (unsigned char) (pixel[1]*ra + inv_a);
pixel[2] = (unsigned char) (pixel[2]*ra + inv_a);
}
}
}
if (req_comp && req_comp != 4) {
out = stbi__convert_format(out, 4, req_comp, w, h);
if (out == NULL) return out; // stbi__convert_format frees input on failure