Six Degrees of Document Format
[ipdf/sam.git] / chapters / Background.tex
1 \chapter{Literature Review}\label{Background}
2
3 The first half of this chapter will be devoted to documents themselves, including: the representation and displaying of graphics primitives\cite{computergraphics2}, and how collections of these primitives are represented in document formats, focusing on widely used standards\cite{plrm, pdfref17, svg2011-1.1}.
4
5 We will find that although there has been a great deal of research into the rendering, storing, editing, manipulation, and extension of document formats, modern standards are content to specify at best single precision IEEE-754 floating point arithmetic.
6
7 The research on arbitrary precision arithmetic applied to documents is very sparse; however arbitrary precision arithmetic itself is a very active field of research. Therefore, the second half of this chapter will be devoted to considering fixed precision floating point numbers as specified by the IEEE-754 standard, possible limitations in precision, and alternative number representations for increased or arbitrary precision arithmetic.
8
9 In Chapter \ref{Progress}, we will discuss our findings so far with regards to arbitrary precision arithmetic applied to document formats, and expand upon the goals outlined in Chapture \ref{Proposal}.
10
11 \section{Raster and Vector Images}\label{Raster and Vector Images}
12 \input{chapters/Background_Raster-vs-Vector}
13
14 \section{Rasterising Vector Images}\label{Rasterising Vector Images}
15
16 Throughout Section \ref{vector-vs-raster-graphics} we were careful to refer to ``modern'' display devices, which are raster based. It is of some historical significance that vector display devices were popular during the 70s and 80s, and papers oriented towards drawing on these devices can be found\cite{brassel1979analgorithm}. Whilst curves can be drawn at high resolution on vector displays, a major disadvantage was shading; by the early 90s the vast majority of computer displays were raster based\cite{computergraphics2}.
17
18 Hearn and Baker's textbook ``Computer Graphics''\cite{computergraphics2} gives a comprehensive overview of graphics from physical display technologies through fundamental drawing algorithms to popular graphics APIs. This section will examine algorithms for drawing two dimensional geometric primitives on raster displays as discussed in ``Computer Graphics'' and the relevant literature. Informal tutorials are abundant on the internet\cite{elias2000graphics}. This section is by no means a comprehensive survey of the literature but intends to provide some idea of the computations which are required to render a document.
19
20 \subsection{Straight Lines}\label{Straight Lines}
21 \input{chapters/Background_Lines}
22
23 \subsection{Spline Curves}\label{Spline Curves}
24
25 Splines are continuous curves formed from piecewise polynomial segments. A polynomial of $n$th degree is defined by $n$ constants $\{a_0, a_1, ... a_n\}$ and:
26 \begin{align}
27         y(x) &= \displaystyle\sum_{k=0}^n a_k x^k
28 \end{align}
29
30
31 A straight line is simply a polynomial of $0$th degree. Splines may be rasterised by sampling of $y(x)$ at a number of points $x_i$ and rendering straight lines between  $(x_i, y_i)$ and $(x_{i+1}, y_{i+1})$ as discussed in Section \ref{Straight Lines}. More direct algorithms for drawing splines based upon Brasenham and Wu's algorithms also exist\cite{citationneeded}.
32
33 There are many different ways to define a spline. One approach is to specify ``knots'' on the spline and solve for the cooefficients to generate a cubic spline ($n = 3$) passing through the points. Alternatively, special polynomials may be defined using ``control'' points which themselves are not part of the curve; these are convenient for graphical based editors. Bezier splines are the most straight forward way to define a curve in the standards considered in Section \ref{Document Representations}
34 \subsubsection{Bezier Curves}
35 \input{chapters/Background_Bezier}
36
37 \subsection{Font Rendering}
38
39 Donald Knuth's 1986 textbook ``Metafont'' blargh
40
41
42
43 \subsection{Shading}
44
45 Algorithms for shading on vector displays involved drawing equally spaced lines in the region with endpoints defined by the boundaries of the region\cite{brassel1979analgorithm}. Apart from being unrealistic, these techniques required a computationally expensive sorting of vertices\cite{lane1983analgorithm}.
46
47 On raster displays, shading is typically based upon Lane's algorithm of 1983\cite{lane1983analgorithm}. Lane's algorithm relies on the ability to ``subtract'' fill from a region. This algorithm is now implemented in the GPU \rephrase{stencil buffer-y and... stuff} \cite{kilgard2012gpu}
48
49 \subsection{Compositing and the Painter's Model}\label{Compositing and the Painter's Model}
50
51 So far we have discussed techniques for rendering vector graphics primitives in isolation, with no regard to the overall structure of a document which may contain many thousands of primitives. A straight forward approach would be to render all elements sequentially to the display, with the most recently drawn pixels overwriting lower elements. Such an approach is particularly inconvenient for anti-aliased images where colours must appear to smoothly blur between the edge of a primitive and any drawn underneath it.
52
53 Colour raster displays are based on an additive red-green-blue $(r,g,b)$ colour representation which matches the human eye's response to light\cite{computergraphics2}. In 1984, Porter and Duff introduced a fourth colour channel for rasterised images called the ``alpha'' channel, analogous to the transparency of a pixel\cite{porter1984compositing}. In compositing models, elements can be rendered seperately, with the four colour channels of successively drawn elements being combined according to one of several possible operations.
54
55 In the ``painter's model'' as described by the SVG standard, Porter and Duff's ``over'' operation is used when rendering one primitive over another\cite{svg2011-1.1}.
56 Given an existing pixel $P_1$ with colour values $(r_1, g_1, b_1, a_1)$ and a pixel $P_2$ with colours $(r_2, g_2, b_2, a_2)$ to be painted over $P_1$, the resultant pixel $P_T$ has colours given by:
57 \begin{align}
58         a_T &= 1 - (1-a_1)(1-a_2) \\
59         r_T &= (1 - a_2)r_1 + r_2 \quad \text{(similar for $g_T$ and $b_T$)}
60 \end{align}
61 It should be apparent that alpha values of $1$ correspond to an opaque pixel; that is, when $a_2 = 1$ the resultant pixel $P_T$ is the same as $P_2$.
62 When the final pixel is actually drawn on an rgb display, the $(r, g, b)$ components are $(r_T/a_T, g_T/a_T, b_T/a_T)$.
63
64 The PostScript and PDF standards, as well as the OpenGL API also use a painter's model for compositing. However, PostScript does not include an alpha channel, so $P_T = P_2$ always\cite{plrm}. Figure \ref{SVG} illustrates the painter's model for partially transparent shapes as they would appear in both the SVG and PDF models.
65
66 \subsection{Rasterisation on the CPU and GPU}
67
68 Traditionally, vector graphics have been rasterized by the CPU before being sent to the GPU for drawing\cite{kilgard2012gpu}. Lots of people would like to change this \cite{worth2003xr, loop2007rendering, rice2008openvg, kilgard2012gpu, green2007improved}.
69
70 \rephrase{2. Here are the ways documents are structured ... we got here eventually}
71
72 \section{Document Representations}\label{Document Representations}
73
74 The representation of information, particularly for scientific purposes, has changed dramatically over the last few decades. For example, Brassel's 1979 paper referenced earlier has been produced on a mechanical type writer. Although the paper discusses an algorithm for shading on computer displays, the figures illustrating this algorithm have not been generated by a computer, but drawn by Brassel's assistant\cite{brassel1979analgorithm}. In contrast, modern papers such as Barnes et. al's recent paper on embedding 3d images in PDF documents\cite{barnes2013embeddding} can themselves be an interactive proof of concept.
75
76 In this section we will consider various approaches and motivations to specifying the structure and appearance of a document, including: early interpreted formats (PostScript, \TeX, DVI), the Document Object Model popular in standards for web based documents (HTML, SVG), and Adobe's ubiquitous Portable Document Format (PDF). Some of these formats were discussed in a recent paper ``Pixels Or Perish'' by Hayes\cite{hayes2012pixelsor} who argues for greater interactivity in the PDF standard.
77
78 \subsection{Interpreted Document Formats}
79 \input{chapters/Background_Interpreted}
80
81
82 \begin{itemize}
83         \item This model treats a document as the source code program which produces graphics
84         \item Arose from the desire to produce printed documents using computers (which were still limited to text only displays).
85         \item Typed by hand or (later) generated by a GUI program
86         \item PostScript --- largely supersceded by PDF on the desktop but still used by printers\footnote{Desktop pdf viewers can still cope with PS, but I wonder if a smartphone pdf viewer would implement it?}
87         \item \TeX --- Predates PostScript, similar idea
88         \begin{itemize}
89                 \item Maybe if \LaTeX were more popular there would be desktop viewers that converted \LaTeX directly into graphics
90         \end{itemize}
91         \item Potential for dynamic content, interactivity; dynamic PostScript, enhanced Postscript
92
93         \item Problems with security --- Turing complete, can be exploited easily
94 \end{itemize}
95
96 \pagebreak
97 \subsection{Document Object Model}\label{Document Object Model}
98 \input{chapters/Background_DOM}
99
100 \subsection{The Portable Document Format}
101
102
103 \subsection{Scientific Computation Packages}
104
105 The document and the code that produces it are one and the same.
106
107 \begin{itemize}
108         \item Numerical computation packages such as Mathematica and Maple use arbitrary precision floats
109         \begin{itemize}
110                 \item Mathematica is not open source which is an issue when publishing scientific research (because people who do not fork out money for Mathematica cannot verify results)
111                 \item What about Maple? \cite{HFP} and \cite{fousse2007mpfr} both mention it being buggy. 
112                 \item Octave and Matlab use fixed precision doubles
113         \end{itemize}
114         \item IPython is pretty cool guys
115 \end{itemize}
116
117 \section{Precision in Modern Document Formats}
118
119 We briefly summarise the requirements of the standards discussed so far in regards to the precision of mathematical operations:
120 \begin{itemize}
121         \item {\bf PostScript} predates the IEEE-754 standard and originally specified a floating point representation with ? bits of exponent and ? bits of mantissa. Version ? of the PostScript standard changed to specify IEEE-754 binary32 ``single precision'' floats.
122         \item {\bf PDF} has also specified IEEE-754 binary32 since version ?. Importantly, the standard states that this is a \emph{maximum} precision; documents created with higher precision would not be viewable in Adobe Reader.
123         \item {\bf SVG} specifies a minimum of IEEE-754 binary32 but recommends more bits be used internally
124         \item {\bf Javascript} uses binary32 floats for all operations, and does not distinguish between integers and floats.   
125         \item {\bf Python} uses binary64 floats
126         \item {\bf Matlab} uses binary64 floats
127         \item {\bf Mathematica} uses some kind of terrifying symbolic / arbitrary float combination
128         \item {\bf Maple} is similar but by many accounts horribly broken
129         
130 \end{itemize}
131
132
133 \rephrase{4. Here is IEEE-754 which is what these standards use}
134
135 \section{Real Number Representations}
136
137 We have found that PostScript, PDF, and SVG document standards all restrict themselves to IEEE floating point number representations of coordinates. This is unsurprising as the IEEE standard has been successfully adopted almost universally by hardware manufactures and programming language standards since the early 1990s. In the traditional view of a document as a static, finite sheet of paper, there is little motivation for enhanced precision.
138
139 In this section we will begin by investigating floating point numbers as defined in the IEEE standard and their limitations. We will then consider alternative number representations including fixed point numbers, arbitrary precision floats, rational numbers, p-adic numbers and symbolic representations. \rephrase{Oh god I am still writing about IEEE floats let alone all those other things}
140
141 \rephrase{Reorder to start with Integers, General Floats, then go to IEEE, then other things}
142
143 \subsection{IEEE Floating Points}
144
145 Although the concept of a floating point representation has been attributed to various early computer scientists including Charles Babbage\cite{citationneeded}, it is widely accepted that William Kahan and his colleagues working on the IEEE-754 standard in the 1980s are the ``fathers of modern floating point computation''\cite{citationneeded}. The original IEEE-754 standard specified the encoding, number of bits, rounding methods, and maximum acceptable errors for the basic floating point operations for base $B = 2$ floats. It also specifies ``exceptions'' --- mechanisms by which a program can detect an error such as division by zero\footnote{Kahan has argued that exceptions in IEEE-754 are conceptually different to Exceptions as defined in several programming languages including C++ and Java. An IEEE exception is intended to prevent an error by its detection, whilst an exception in those languages is used to indicate an error has already occurred\cite{}}. We will restrict ourselves to considering $B = 2$, since it was found that this base in general gives the smallest rounding errors\cite{HFP}, although it is worth noting that different choices of base had been used historically\cite{goldman1991whatevery}, and the IEEE-854 and later the revised IEEE-754 standard specify a decimal representation $B = 10$ intended for use in financial applications.
146
147 \subsection{Floating Point Definition}
148
149 A floating point number $x$ is commonly represented by a tuple of integers $(s, e, m)$ in base $B$ as\cite{HFP, ieee2008-754}:
150
151 \begin{align*}
152         x &= (-1)^{s} \times m \times B^{e}
153 \end{align*}
154
155 Where $s$ is the sign and may be zero or one, $m$ is commonly called the ``mantissa'' and $e$ is the exponent.
156 The name ``floating point'' refers to the equivelance of the $\times B^e$ operation to a shifting of a decimal point along the mantissa. This contrasts with a ``fixed point'' representation where $x$ is the sum of two fixed size numbers representing the integer and fractional part.
157
158 In the IEEE-754 standard, for a base of $B = 2$, numbers are encoded in continuous memory by a fixed number of bits, with $s$ occupying 1 bit, followed by $e$ and $m$ occupying a number of bits specified by the precision; 5 and 10 for a binary16 or ``half precision'' float, 8 and 23 for a binary32 or ``single precision'' and 15 and 52 for a binary64 or ``double precision'' float\cite{HFP, ieee2008-754}.
159
160
161 \subsection{Precision and Rounding}
162
163 Real values which cannot be represented exactly in a floating point representation must be rounded. The results of a floating point operation will in general be such values and thus there is a rounding error possible in any floating point operation. Goldberg's assertively titled 1991 paper ``What Every Computer Scientist Needs to Know about Floating Point Arithmetic'' provides a comprehensive overview of issues in floating point arithmetic and relates these to the 1984 version of the IEEE-754 standard\cite{goldberg1991whatevery}. More recently, after the release of the revised IEEE-754 standard in 2008, a textbook ``Handbook Of Floating Point Arithmetic'' has been published which provides a thourough review of literature relating to floating point arithmetic in both software and hardware\cite{HFP}.
164
165
166 Figure \ref{minifloat.pdf} shows the positive real numbers which can be represented exactly by an 8 bit base $B = 2$ floating point number; and illustrates that a set of fixed precision floating point numbers forms a discrete approximation of the reals. There are only $2^7 = 256$ numbers in this set, which means it is easier to see some of the properties of floats that would be unclear using one of the IEEE-754 encodings. The first set of points corresponds to using 2 and 5 bits to encode $e$ and $m$ whilst the second set of points corresponds to a 3 and 4 bit encoding. This allows us to see the trade off between the precision and range of real values represented. 
167
168 \begin{figure}[H]
169         \centering
170         \includegraphics[width=0.8\textwidth]{figures/minifloat.pdf} \\
171         \includegraphics[width=0.8\textwidth]{figures/minifloat_diff.pdf}
172         \caption{The mapping of 8 bit floats to reals}
173 \end{figure}
174
175 \subsection{Floating Point Operations}
176
177 Floating point operations can in principle be performed using integer operations, but specialised Floating Point Units (FPUs) are an almost universal component of modern processors\cite{citationneeded}. The improvement of FPUs remains highly active in several areas including: efficiency\cite{seidel2001onthe}; accuracy of operations\cite{dieter2007lowcost}; and even the adaptation of algorithms originally used in software for reducing the overal error of a sequence of operations\cite{kadric2013accurate}. In this section we will consider the algorithms for floating point operations without focusing on the hardware implementation of these algorithms.
178
179
180 \subsection{Some sort of Example(s) or Floating Point Mayhem}
181
182 \rephrase{Eg: $f(x) = |x|$ calculated from sqrt and squaring}
183
184 \rephrase{Eg: Massive rounding errors from calculatepi}
185
186 \rephrase{Eg: Actual graphics things :S}
187
188
189 \subsection{Limitations Imposed By Graphics APIs and/or GPUs}
190
191 Traditionally algorithms for drawing vector graphics are performed on the CPU; the image is rasterised and then sent to the GPU for rendering\cite{}. Recently there has been a great deal of literature relating to implementation of algorithms such as bezier curve rendering\cite{} or shading\cite{} on the GPU. As it seems the trend is to move towards GPU 
192
193 \rephrase{6. Here are ways GPU might not be IEEE-754 --- This goes *somewhere* in here but not sure yet}
194
195 \begin{itemize}
196         \item Internal representations are GPU dependent and may not match IEEE\cite{hillesland2004paranoia}
197         \item OpenGL standards specify: binary16, binary32, binary64
198         \item OpenVG aims to become a standard API for SVG viewers but the API only uses binary32 and hardware implementations may use less than this internally\cite{rice2008openvg}
199         \item It seems that IEEE has not been entirely successful; although all modern CPUs and GPUs are able to read and write IEEE floating point types, many do not conform to the IEEE standard in how they represent floating point numbers internally. 
200         \item \rephrase{Blog post alert} \url{https://dolphin-emu.org/blog/2014/03/15/pixel-processing-problems/}
201 \end{itemize}
202
203
204
205 \rephrase{7. Sod all that, let's just use an arbitrary precision library (AND THUS WE FINALLY GET TO THE POINT)}
206
207 \subsection{Arbitrary Precision Floating Point Numbers}
208
209 An arbitrary precision floating point number simply uses extra bits to store extra precision. Do it all using MFPR\cite{fousse2007mpfr}, she'll be right.
210
211 \rephrase{8. Here is a brilliant summary of sections 7- above}
212
213 Dear reader, thankyou for your persistance in reading this mangled excuse for a Literature Review.
214 Hopefully we have brought together the radically different areas of interest together in some sort of coherant fashion.
215 In the next chapter we will talk about how we have succeeded in rendering a rectangle. It will be fun. I am looking forward to it.
216
217 \rephrase{Oh dear this is not going well}
218

UCC git Repository :: git.ucc.asn.au