[gs-commits] mupdf 1.16.1.16 Bug 701397: Allow inverting luminance of

[email protected] (Tor Andersson)
Newsgroups gmane.comp.printing.ghostscript.cvs
Message-ID <[email protected]>
commit 1b3b8a3a7e80a24e8089bff7a410c9d4288dfa83
Author: Tor Andersson <[email protected]>
Date:   Fri Aug 16 17:22:32 2019 +0200

    Bug 701397: Allow inverting luminance of grayscale and BGR images too.

diff --git a/source/fitz/pixmap.c b/source/fitz/pixmap.c
index 56dfb39..a91fdd3 100644
--- a/source/fitz/pixmap.c
+++ b/source/fitz/pixmap.c
@@ -1022,15 +1022,25 @@ fz_tint_pixmap(fz_context *ctx, fz_pixmap *pix, int black, int white)
 	}
 }
 
-/* Invert luminance in RGB pixmap, but keep the colors as is. */
-static void invert_luminance_rgb(unsigned char *rgb)
+/* Invert luminance in RGB/BGR pixmap, but keep the colors as is. */
+static inline void invert_luminance(int type, unsigned char *s)
 {
 	int r, g, b, y, u, v, c, d, e;
 
 	/* Convert to YUV */
-	r = rgb[0];
-	g = rgb[1];
-	b = rgb[2];
+	if (type == FZ_COLORSPACE_RGB)
+	{
+		r = s[0];
+		g = s[1];
+		b = s[2];
+	}
+	else
+	{
+		r = s[2];
+		g = s[1];
+		b = s[0];
+	}
+
 	y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
 	u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
 	v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
@@ -1046,9 +1056,18 @@ static void invert_luminance_rgb(unsigned char *rgb)
 	g = (298 * c - 100 * d - 208 * e + 128) >> 8;
 	b = (298 * c + 516 * d + 128) >> 8;
 
-	rgb[0] = r > 255 ? 255 : r < 0 ? 0 : r;
-	rgb[1] = g > 255 ? 255 : g < 0 ? 0 : g;
-	rgb[2] = b > 255 ? 255 : b < 0 ? 0 : b;
+	if (type == FZ_COLORSPACE_RGB)
+	{
+		s[0] = r > 255 ? 255 : r < 0 ? 0 : r;
+		s[1] = g > 255 ? 255 : g < 0 ? 0 : g;
+		s[2] = b > 255 ? 255 : b < 0 ? 0 : b;
+	}
+	else
+	{
+		s[2] = r > 255 ? 255 : r < 0 ? 0 : r;
+		s[1] = g > 255 ? 255 : g < 0 ? 0 : g;
+		s[0] = b > 255 ? 255 : b < 0 ? 0 : b;
+	}
 }
 
 void
@@ -1056,18 +1075,27 @@ fz_invert_pixmap_luminance(fz_context *ctx, fz_pixmap *pix)
 {
 	unsigned char *s = pix->samples;
 	int x, y, n = pix->n;
+	int type = pix->colorspace ? pix->colorspace->type : FZ_COLORSPACE_NONE;
 
-	if (pix->colorspace->type != FZ_COLORSPACE_RGB)
-		fz_throw(ctx, FZ_ERROR_GENERIC, "can only invert luminance of RGB pixmaps");
-
-	for (y = 0; y < pix->h; y++)
+	if (type == FZ_COLORSPACE_GRAY)
 	{
-		for (x = 0; x < pix->w; x++)
+		fz_invert_pixmap(ctx, pix);
+	}
+	else if (type == FZ_COLORSPACE_RGB || type == FZ_COLORSPACE_BGR)
+	{
+		for (y = 0; y < pix->h; y++)
 		{
-			invert_luminance_rgb(s);
-			s += n;
+			for (x = 0; x < pix->w; x++)
+			{
+				invert_luminance(type, s);
+				s += n;
+			}
+			s += pix->stride - pix->w * n;
 		}
-		s += pix->stride - pix->w * n;
+	}
+	else
+	{
+		fz_throw(ctx, FZ_ERROR_GENERIC, "can only invert luminance of Gray and RGB pixmaps");
 	}
 }
 

http://git.ghostscript.com/?p=mupdf.git;a=commit;h=1b3b8a3a7e80a24e8089bff7a410c9d4288dfa83

--
MuPDF library
Artifex Software, Inc.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.