From a371b204f57e49d3799630b7f2074eb99eedb5ef Mon Sep 17 00:00:00 2001 From: rmitton Date: Tue, 7 Jul 2015 15:15:38 -0700 Subject: [PATCH 1/3] Added support for 16-bit PSD loading. This extends the current PSD loader to add support for 16-bit images, by quantizing them down to 8-bit upon load. --- stb_image.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/stb_image.h b/stb_image.h index d0fa9c2..2981df3 100644 --- a/stb_image.h +++ b/stb_image.h @@ -5054,6 +5054,7 @@ static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int int pixelCount; int channelCount, compression; int channel, i, count, len; + int bitdepth; int w,h; stbi_uc *out; @@ -5078,8 +5079,9 @@ static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int w = stbi__get32be(s); // Make sure the depth is 8 bits. - if (stbi__get16be(s) != 8) - return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 bit"); + bitdepth = stbi__get16be(s); + if (bitdepth != 8 && bitdepth != 16) + return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 or 16 bit"); // Make sure the color mode is RGB. // Valid options are: @@ -5191,8 +5193,13 @@ static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int *p = channel == 3 ? 255 : 0; } else { // Read the data. - for (i = 0; i < pixelCount; i++, p += 4) - *p = stbi__get8(s); + if (bitdepth == 16) { + for (i = 0; i < pixelCount; i++, p += 4) + *p = (stbi__get16le(s) + 127) >> 8; + } else { + for (i = 0; i < pixelCount; i++, p += 4) + *p = stbi__get8(s); + } } } } From 608cbec1f59a72fcd2b6862a98daaabe9c001f28 Mon Sep 17 00:00:00 2001 From: rmitton Date: Tue, 7 Jul 2015 15:47:37 -0700 Subject: [PATCH 2/3] Fixed overflow for high values. 0xffff would accidentally round to 0x10000. --- stb_image.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb_image.h b/stb_image.h index 2981df3..e1c29b8 100644 --- a/stb_image.h +++ b/stb_image.h @@ -5195,7 +5195,7 @@ static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int // Read the data. if (bitdepth == 16) { for (i = 0; i < pixelCount; i++, p += 4) - *p = (stbi__get16le(s) + 127) >> 8; + *p = stbi__get16le(s) * 255 / 65535; } else { for (i = 0; i < pixelCount; i++, p += 4) *p = stbi__get8(s); From 6645ea5915833f6a89f51c3d95fcb8f50948c7fa Mon Sep 17 00:00:00 2001 From: rmitton Date: Fri, 24 Jul 2015 12:00:09 -0700 Subject: [PATCH 3/3] Fixed stupid endianness bug. Incorrect endianness hilariously doesn't manifest _if_ the original image was upconverted from 8-bit to 16-bit. --- stb_image.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb_image.h b/stb_image.h index e1c29b8..5d54bd1 100644 --- a/stb_image.h +++ b/stb_image.h @@ -5195,7 +5195,7 @@ static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int // Read the data. if (bitdepth == 16) { for (i = 0; i < pixelCount; i++, p += 4) - *p = stbi__get16le(s) * 255 / 65535; + *p = stbi__get16be(s) * 255 / 65535; } else { for (i = 0; i < pixelCount; i++, p += 4) *p = stbi__get8(s);