Commits

Jason McKesson committed 067b3d3

Tut15: Started diagrams. Pixel image works.

Comments (0)

Files changed (6)

Documents/Basics/GenPixelImage.lua

 require "Viewport"
 require "SubImage"
 require "GridAxis"
+require "PixelImage"
 require "_utils"
 
-local pixelSize = nil;
+local image = PixelImage.PixelImage("PixelImage.txt")
 
-local hFile = io.open("PixelImage.txt", "r");
-local numColors = nil;
-local colorTable = {};
-local pixelData = {};
-for line in hFile:lines() do
-	if(not numColors) then
-		--Header.
-		local width, height, colorCount = line:match("(%d+) (%d+) (%d+)");
-		assert(width);
-		pixelSize = vmath.vec2(width, height);
-		numColors = colorCount;
-	else
-		if(numColors ~= 0) then
-			--Color
-			local clrChar, clrValue = line:match("(%S) c (%#%x%x%x%x%x%x)");
-			assert(clrChar, line);
-			colorTable[clrChar] = clrValue
-			numColors = numColors - 1;
-		else
-			--Pixel data.
-			if(#line ~= 0) then
-				pixelData[#pixelData + 1] = line;
-			end
-		end
-	end
-end
-hFile:close();
+local styleLib = SvgWriter.StyleLibrary();
+image:Style(styleLib);
 
 local magnification = 16;
 local spacing = 40;
 
-local imageWidth = pixelSize.x + spacing + (pixelSize.x * magnification);
-local imageHeight = pixelSize.y * magnification;
+local imageWidth = image:Size().x + spacing + (image:Size().x * magnification);
+local imageHeight = image:Size().y * magnification;
 
-local styleLib = SvgWriter.StyleLibrary();
-
-local styleMap = {};
-local styleIndex = 0;
-for clrChar, clrValue in pairs(colorTable) do
-	local styleName = "color_" .. styleIndex;
-	styleMap[clrChar] = styleName;
-	styleLib:AddStyle(nil, styleName,
-		SvgWriter.Style():stroke("none"):fill(clrValue));
-	styleIndex = styleIndex + 1;
-end
-
-
-	
 local writer = SvgWriter.SvgWriter(ConstructSVGName(arg[0]), {imageWidth .."px", imageHeight .. "px"});
 	writer:StyleLibrary(styleLib);
 	writer:BeginDefinitions();
 	writer:EndDefinitions();
 	
-	for i, row in ipairs(pixelData) do
-		local pixelX = 0;
-		for clrChar in row:gmatch("%S") do
-			writer:Rect({pixelX, i-1}, {1, 1}, nil, {styleMap[clrChar]});
-			pixelX = pixelX + 1;
-		end
-	end
-
-	local offset = vmath.vec2(pixelSize.x + spacing, 0);
-	for i, row in ipairs(pixelData) do
-		local pixelX = 0;
-		for clrChar in row:gmatch("%S") do
-			assert(styleMap[clrChar]);
-			local location = vmath.vec2(pixelX, i-1);
-			location = location * magnification;
-			location = location + offset;
-			writer:Rect(location, {magnification, magnification},
-				nil, {styleMap[clrChar]});
-			pixelX = pixelX + 1;
-		end
-	end
-
+	image:Draw(writer, vmath.vec2(0, 0), image:Size());
+	image:Draw(writer, vmath.vec2(image:Size().x + spacing, 0),
+		image:Size() * magnification);
+	
 writer:Close();
 
-
-
Add a comment to this file

Documents/Basics/PixelImage.svg

Old
Old image
New
New image
 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
 
 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" height="256px" width="312px" >
-	<style type="text/css" ><![CDATA[.color_0
+	<style type="text/css" ><![CDATA[.color_2
 {
-	fill: #000000;
+	fill: #AFAFAF;
 	stroke: none;
 }
 
 	stroke: none;
 }
 
-.color_2
+.color_0
 {
-	fill: #AFAFAF;
+	fill: #000000;
+	stroke: none;
+}
+
+.color_4
+{
+	fill: #D3D3D3;
 	stroke: none;
 }
 
 {
 	fill: #9a9a9a;
 	stroke: none;
-}
-
-.color_4
-{
-	fill: #D3D3D3;
-	stroke: none;
 }]]></style>
 	<defs />
 	<rect y="0" x="0" height="1" width="1" class="color_4" />

Documents/Texturing/GenNearestSampleDiag.lua

+require "SvgWriter"
+require "vmath"
+require "Viewport"
+require "SubImage"
+require "GridAxis"
+require "PixelImage"
+require "_utils"
+
+-- Sizing
+local imageSize = vmath.vec2(400, 400);
+
+local subImages = SubImage.SubImage(1, 1, imageSize.x, imageSize.y, 0, 50);
+
+local coordSize = 6;
+local coordWidth = coordSize * (imageSize.x / imageSize.y);
+
+--image
+local image = PixelImage.PixelImage("NearestSampleDiag.txt")
+
+local pixelSize = image:Size();
+pixelSize = pixelSize / 2;
+pixelSize.y = pixelSize.y;
+
+local vp = Viewport.Viewport(imageSize, pixelSize, image:Size())
+local trans2 = Viewport.Transform2D()
+vp:SetTransform(trans2);
+
+--styles
+local styleLib = SvgWriter.StyleLibrary();
+image:Style(styleLib);
+styleLib:AddStyle(nil, "grid",
+	SvgWriter.Style():stroke("#888"):stroke_width("1.5px"):fill("none"));
+styleLib:AddStyle(nil, "sample_box",
+	SvgWriter.Style():stroke("red"):stroke_width("7px"):stroke_opacity(0.7)
+	:stroke_linejoin("round"):fill("none"));
+styleLib:AddStyle(nil, "sample_pt",
+	SvgWriter.Style():stroke("none"):fill("green"));
+	
+--Sample point.
+local samplePt = vmath.vec2(1.75, 1.75);
+
+local pts =
+{
+	samplePt - vmath.vec2(0.4, 0.4),
+	samplePt + vmath.vec2(0.4, 0.4),
+}
+pts = vp:Transform(pts);
+
+local pathSampleArea = SvgWriter:Path();
+pathSampleArea
+	:M(pts[1])
+	:L{pts[1].x, pts[2].y}
+	:L(pts[2])
+	:L{pts[2].x, pts[1].y}
+	:Z()
+
+samplePt = vp:Transform(samplePt);
+
+local ptRadius = vp:Length(0.05)
+
+local writer = SvgWriter.SvgWriter(ConstructSVGName(arg[0]), {subImages:Size().x .."px", subImages:Size().y .. "px"});
+	writer:StyleLibrary(styleLib);
+	writer:BeginDefinitions();
+	writer:EndDefinitions();
+	
+	image:Draw(writer, vmath.vec2(0, 0), subImages:Size(), {"grid"});
+	
+	writer:Path(pathSampleArea, {"sample_box"})
+	
+	writer:Circle(samplePt, ptRadius, {"sample_pt"})
+writer:Close();
Add a comment to this file

Documents/Texturing/NearestSampleDiag.svg

Added
New image
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" height="400px" width="400px" >
+	<style type="text/css" ><![CDATA[.color_0
+{
+	fill: #000000;
+	stroke: none;
+}
+
+.color_1
+{
+	fill: #FFFFFF;
+	stroke: none;
+}
+
+.grid
+{
+	stroke: #888;
+	stroke-width: 1.5px;
+	fill: none;
+}
+
+.sample_box
+{
+	stroke-opacity: 0.7;
+	stroke-width: 7px;
+	stroke-linejoin: round;
+	stroke: red;
+	fill: none;
+}
+
+.sample_pt
+{
+	fill: green;
+	stroke: none;
+}]]></style>
+	<defs />
+	<rect y="0" x="0" height="100" width="100" class="color_0" />
+	<rect y="0" x="100" height="100" width="100" class="color_0" />
+	<rect y="0" x="200" height="100" width="100" class="color_1" />
+	<rect y="0" x="300" height="100" width="100" class="color_1" />
+	<rect y="100" x="0" height="100" width="100" class="color_0" />
+	<rect y="100" x="100" height="100" width="100" class="color_0" />
+	<rect y="100" x="200" height="100" width="100" class="color_1" />
+	<rect y="100" x="300" height="100" width="100" class="color_1" />
+	<rect y="200" x="0" height="100" width="100" class="color_1" />
+	<rect y="200" x="100" height="100" width="100" class="color_1" />
+	<rect y="200" x="200" height="100" width="100" class="color_0" />
+	<rect y="200" x="300" height="100" width="100" class="color_0" />
+	<rect y="300" x="0" height="100" width="100" class="color_1" />
+	<rect y="300" x="100" height="100" width="100" class="color_1" />
+	<rect y="300" x="200" height="100" width="100" class="color_0" />
+	<rect y="300" x="300" height="100" width="100" class="color_0" />
+	<path d="M 0 100 h 400 M 0 200 h 400 M 0 300 h 400 M 100 0 v 400 M 200 0 v 400 M 300 0 v 400" class="grid" />
+	<path d="M 135 265 L 135 185 L 215 185 L 215 265 Z" class="sample_box" />
+	<circle r="5" cy="225" cx="175" class="sample_pt" />
+</svg>

Documents/Texturing/NearestSampleDiag.txt

+4 4 2 1
+. c #FFFFFF
+O c #000000
+OO..
+OO..
+..OO
+..OO

Documents/Tools/PixelImage.lua

+require "SvgWriter"
+require "vmath"
+require "Viewport"
+require "SubImage"
+require "_utils"
+
+module(..., package.seeall);
+
+local function AddMembersToClass(theTable, theMembers)
+	for funcName, func in pairs(theMembers) do
+		theTable[funcName] = func;
+	end
+	
+	return theTable;
+end
+
+local ImageMembers = {}
+
+function ImageMembers:Size()
+	return vmath.vec2(self.imageSize);
+end
+
+function ImageMembers:Style(styleLib, prefix)
+	prefix = prefix or "color_"
+	
+	assert(not self.styleMap, "Can only write style once.");
+	local styleMap = {};
+	self.styleMap = styleMap;
+	
+	local styleIndex = 0;
+	for clrChar, clrValue in pairs(self.colorTable) do
+		local styleName = prefix .. styleIndex;
+		styleMap[clrChar] = styleName;
+		styleLib:AddStyle(nil, styleName,
+			SvgWriter.Style():stroke("none"):fill(clrValue));
+		styleIndex = styleIndex + 1;
+	end
+end
+
+function ImageMembers:Draw(writer, topLeft, widthHeight, gridStyles, gridId)
+	assert(self.styleMap, "Style has not been added yet.");
+	
+	local offset = topLeft;
+	local scale = widthHeight / self.imageSize;
+	for i, row in ipairs(self.pixelData) do
+		local pixelX = 0;
+		for clrChar in row:gmatch("%S") do
+			assert(self.styleMap[clrChar]);
+			local location = vmath.vec2(pixelX, i-1);
+			location = location * scale;
+			location = location + offset;
+			writer:Rect(location, scale,
+				nil, {self.styleMap[clrChar]});
+			pixelX = pixelX + 1;
+		end
+	end
+	
+	if(not gridStyles) then
+		return;
+	end
+	
+	local path = SvgWriter.Path()
+	--Draw the grid, using the given style set.
+	for y = 2, self.imageSize[2] do
+		local location = vmath.vec2(0, y-1);
+		location = location * scale;
+		location = location + offset;
+		path:M(location):h(widthHeight[1])
+	end
+	for x = 2, self.imageSize[1] do
+		local location = vmath.vec2(x-1, 0);
+		location = location * scale;
+		location = location + offset;
+		path:M(location):v(widthHeight[2])
+	end
+	
+	writer:Path(path, gridStyles, gridId);
+end
+
+function PixelImage(imageFilename)
+	local image = {};
+	
+	local hFile = io.open(imageFilename, "rt");
+	local numColors = nil;
+	image.pixelData = {}
+	image.colorTable = {};
+	for line in hFile:lines() do
+		if(not numColors) then
+			--Header.
+			local width, height, colorCount = line:match("(%d+) (%d+) (%d+)");
+			assert(width);
+			image.imageSize = vmath.vec2(width, height);
+			numColors = colorCount;
+		else
+			if(numColors ~= 0) then
+				--Color
+				local clrChar, clrValue = line:match("(%S) c (%#%x%x%x%x%x%x)");
+				assert(clrChar, line);
+				image.colorTable[clrChar] = clrValue
+				numColors = numColors - 1;
+			else
+				--Pixel data.
+				if(#line ~= 0) then
+					image.pixelData[#image.pixelData + 1] = line;
+				end
+			end
+		end
+	end
+	hFile:close();
+
+	return 	AddMembersToClass(image, ImageMembers)
+end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.