From 4008b4d986b700830fad5086df3acfc1028a9e3b Mon Sep 17 00:00:00 2001
From: Tor Andersson <tor@ghostscript.com>
Date: Mon, 29 Nov 2004 17:33:59 +0100
Subject: turn 1x1 images into rectangles

---
 tree/optimize.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)

(limited to 'tree/optimize.c')

diff --git a/tree/optimize.c b/tree/optimize.c
index cbb9ad31..9f85cdd7 100644
--- a/tree/optimize.c
+++ b/tree/optimize.c
@@ -214,6 +214,84 @@ retry:
 		cleanmasks(current);
 }
 
+/*
+ * Turn 1x1 images into rectangle fills
+ */
+
+static fz_error *clean1x1(fz_node *node)
+{
+	fz_error *error;
+	fz_node *current;
+	fz_node *color;
+	fz_pathnode *rect;
+	fz_node *mask;
+	fz_image *image;
+	fz_pixmap *pix;
+	float v[FZ_MAXCOLORS];
+	int i;
+
+	for (current = node->first; current; current = current->next)
+	{
+		if (fz_isimagenode(current))
+		{
+			image = ((fz_imagenode*)current)->image;
+			if (image->w == 1 && image->h == 1)
+			{
+				error = fz_newpathnode(&rect);
+				fz_moveto(rect, 0, 0);
+				fz_lineto(rect, 1, 0);
+				fz_lineto(rect, 1, 1);
+				fz_lineto(rect, 0, 1);
+				fz_closepath(rect);
+				fz_endpath(rect, FZ_FILL, nil, nil);
+
+				if (image->cs)
+				{
+					error = fz_newpixmap(&pix, 0, 0, 1, 1, image->n + 1);
+					if (error)
+						return error;
+
+					error = image->loadtile(image, pix);
+					if (error)
+						return error;
+
+					for (i = 0; i < image->n; i++)
+						v[i] = pix->samples[i + 1] / 255.0;
+
+					fz_droppixmap(pix);
+
+					error = fz_newcolornode(&color, image->cs, image->n, v);
+					if (error)
+						return error;
+					error = fz_newmasknode(&mask);
+					if (error)
+						return error;
+
+					fz_insertnodeafter(mask, (fz_node*)rect);
+					fz_insertnodeafter(mask, color);
+					fz_insertnodeafter(current, mask);
+					fz_removenode(current);
+					current = color;
+				}
+
+				else
+				{
+					/* pray that the 1x1 image mask is all opaque */
+					fz_insertnodeafter(current, (fz_node*)rect);
+					fz_removenode(current);
+					current = (fz_node*)rect;
+				}
+			}
+		}
+
+		error = clean1x1(current);
+		if (error)
+			return error;
+	}
+
+	return nil;
+}
+
 /*
  *
  */
@@ -226,6 +304,7 @@ fz_optimizetree(fz_tree *tree)
 	cleanwhite(tree->root);
 	cleanovers(tree->root);
 	cleanmasks(tree->root);
+	clean1x1(tree->root);
 	return nil;
 }
 
-- 
cgit v1.2.3