\documentclass[12pt]{article}

%\usepackage{ngerman,a4}
\usepackage{amstext}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{stmaryrd}
\usepackage{enumerate}
\usepackage{alltt}
\usepackage[latin1]{inputenc}
\usepackage{cancel}
\usepackage{fancyhdr}
\usepackage{pstricks}
\usepackage{pst-plot}

\title{\textsc{Computer Graphics I \\ \large Assignment 6 \\ \small Group III (Yavor Kaloyanov)}}
\author{\shortstack[c]{Jan Hendrik Dithmar\\ \footnotesize 2031259} \and \shortstack[c]{Pascal Gwosdek\\ \footnotesize 2505221}}
\date{}

\pagestyle{fancy}

\lhead{Assignment 6}
\chead{}
\rhead{Dithmar (2031259), Gwosdek (2505221)}
\lfoot{Computer Graphics 1}
\cfoot{Page \thepage}
\rfoot{WS 2006/2007}

\begin{document}

\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\newcommand{\exercise}[1]{\subsection{#1}}
\renewcommand{\theenumi}{\textbf{\alph{enumi}}}

\newcommand{\AND}{\ensuremath{\wedge}}
\newcommand{\OR}{\ensuremath{\vee}}
\newcommand{\SetR}{\ensuremath{\mathbb{R}}}
\newcommand{\SetN}{\ensuremath{\mathbb{N}}}
\newcommand{\SetC}{\ensuremath{\mathbb{C}}}
\newcommand{\SetZ}{\ensuremath{\mathbb{Z}}}
\newcommand{\SetQ}{\ensuremath{\mathbb{Q}}}
\newcommand{\SetB}{\ensuremath{\mathbb{B}}}
\newcommand{\QED}{\ensuremath{\hspace*{\fill}\square}}
\newcommand{\FLASH}{\ensuremath{\hspace*{\fill}\lightning}}
\newcommand{\OnotO}{\ensuremath{\mathcal{O}}}
\newcommand{\bild}[4]{
	\begin{figure}[htpb]
		\centering
			\includegraphics[width=#1cm]{#2}\\[0.2cm]
			\caption{#3}
			\label{#4}
	\end{figure}
	}

\maketitle

\thispagestyle{empty}

\newpage
\setcounter{page}{1}
\setcounter{section}{6}

\exercise{Gamma correction}
We look at the following cases:
\begin{itemize}
	\item $x=0.00000000b$
	\item $x=0.00000001b$
	\item $x=0.11111110b$
	\item $x=0.11111111b$
\end{itemize}
If $\gamma$ equals 1, we have no problem, because the result is exactly the same color value. But -- assuming the worst-case -- in the case that $\gamma$ equal 2.5, 8 bits
actually don't suffice. This is show by the following calculation:
\begin{align*}
	f(0.00000000b) &= 0.0b \\
	f(0.00000001b) &= 0.000000000000000000001111111111b \\
	f(0.11111110b) &= 0.1111101100b \\
	f(0.11111111b) &= 0.1111110110b
\end{align*}
If we count the bits, we see that there are 30 bits needed to calculate the second result which means that we need 30 bits to accurately represent the correct signal after
the gamma correction.

\exercise{Transforms in colorspace}
\begin{enumerate}
\begin{figure}
\centering
\includegraphics[width=0.4\textwidth]{tetrahedron.png} \quad\quad
\includegraphics[width=0.4\textwidth]{shift.png}
\caption{\textbf{Left:} From the color triangle to a tetrahedron - the white point is lifted by 1 along an axis which is parallel to the triangle's normal vector. \textbf{Right:} The shift vector is scaled by the ''height'' of the point being shifted.}
\label{tetrahedron}
\end{figure}

\item To visualize the problem, we imagine the color triangle to be a tetraedron, with the base on the triangle itself (now residing on level 0), but the white point raised to 1 (as depicted by figure \ref{tetrahedron}, left). The points of the colorspace now reside on the outer boundaries of this tetrahedron. It can now easily be seen that a shift of the white point results in a modification of the top corner of the tetrahedron in a plane parallel to its base, while the base points are left where they are (cf. Figure \ref{tetrahedron}, right).

The vector $\underline s$ drawn in the figure represents the difference in each color value, which can be computed using the barycentric coordinates of the color space. By intercept theorems one sees that any point is shifted along this vector, but scaled by the ''height'' of the respective point.

Mathematically, the height can be expressed as
$$h(r,g,b) = \dfrac{r + g + b - 1}2$$
hence the incoming \textsf{RGB} signal $(i_r, i_g, i_b)^T$  must be translated to $(o_r, o_g, o_b)^T$ by
\begin{align*}
\begin{pmatrix}o_r\\ o_g\\ o_b\end{pmatrix} &= h(i_r, i_g, i_b) \ \,\cdot \begin{pmatrix}\underline s_r & 0 & 0\\ 0 & \underline s_g & 0\\ 0 & 0 & \underline s_b\end{pmatrix} \cdot \begin{pmatrix}i_r\\ i_g\\ i_b\end{pmatrix}\\
&= \frac{i_r, i_g, i_b - 1}2 \cdot \begin{pmatrix}\underline s_r & 0 & 0\\ 0 & \underline s_g & 0\\ 0 & 0 & \underline s_b\end{pmatrix} \cdot \begin{pmatrix}i_r\\ i_g\\ i_b\end{pmatrix}
\end{align*}
\item Let $M_1$ be the transformation matrix from \textsf{sRGB} to \textsf{CIE-XYZ} (the computation is depicted in Exercise 6.3) and $M_2$ the transformation matrix for the colorspace with white point $(w_x, w_y) = (0.400, 0.330)$. $M_2$ can be computed as follows:
Compute the $C_X$:
$$\begin{pmatrix}0.6400 & 0.3000 & 0.1500\\ 0.3300 & 0.6000 & 0.0600\\ 0.0300 & 0.1000 & 0.7900\end{pmatrix} \begin{pmatrix}C_r\\C_g\\C_b\end{pmatrix} = \begin{pmatrix}\dfrac{0.4000}{0.3300}\\1\\\dfrac{0.2700}{0.3300}\end{pmatrix}$$
Using Maple, the equation system can be solved with
\begin{align*}
C_r &= 1.277838627\\
C_g &= 0.8762322017\\
C_b &= 0.8762322015
\end{align*}
Hence, $M_2$ can be calculated as:
$$M_2 \approx \begin{pmatrix}0.8178167213 & 0.2628696605 & 0.1314348302\\ 0.4216867469 & 0.5257393210 & 0.0525739321\\ 0.0383351588 & 0.0876232202 & 0.6922234392\end{pmatrix}$$
To transform a value $(a_1, b_1)$ of the first colorspae into $(a_2, b_2)$ in the second one, the following steps must be applied:
\begin{enumerate}
\item Compute $X_1, Y_1, Z_1$ as
$$X_1 = \dfrac{a_1}{b_1},\quad Y_1 = 1,\quad Z_1 = \dfrac{1 - a_1 - b_1}b.$$
\item Transform $X_1, Y_1, Z_1$ into \textsf{RGB} values using $M_1^{-1}$ and retransform these values into $X_2, Y_2, Z_2$ using $M_2$:
$$\begin{pmatrix}X_2\\Y_2\\Z_2\end{pmatrix} = M \cdot \begin{pmatrix}X_1\\Y_1\\Z_1\end{pmatrix},$$
where
\begin{align*}
M :&= M_2 \cdot M_1^{-1}\\
&\approx \begin{pmatrix}2.403046396010846 & -0.790972406319101 & -0.257925781835294\\ 0.860029272312178 & \phantom{-}0.327251898802813 & -0.132841273188141\\ 0.077823293962256 & -0.035755146854610 & \phantom{-}0.716187322029735\end{pmatrix}.
\end{align*}
\item Compute $a_2, b_2$ as
\begin{align*}
a_2 &= \dfrac{X_2}{X_2 + Y_2 + Z_2}\\
b_2 &= \dfrac{Y_2}{X_2 + Y_2 + Z_2}
\end{align*}
\end{enumerate}
\end{enumerate}

\exercise{Color Space}
We have the following:
\begin{align*}
	x = \dfrac{X}{X+Y+Z} & \Leftrightarrow xX + xY + xZ = X \\
	& \Leftrightarrow X \cdot (x-1) = -xY - xZ \\
	& \Leftrightarrow X = \dfrac{xY+xZ}{1-x} \quad (1) \\[0.2cm]
	y = \dfrac{Y}{X+Y+Z} & \Leftrightarrow yX + yY + yZ = Y \\
	& \Leftrightarrow Z = \dfrac{Y-yX-yY}{y} \quad (2)
\end{align*}
Now, we put equation (2) into equation (1):
\begin{align*}
	X &= \dfrac{xY + x \cdot \left(\dfrac{Y-yX-yY}{y}\right)}{1-x} \\
	&= \dfrac{xyY + xY - xyX - xyY}{y-xy} \\
	\Leftrightarrow X + \dfrac{xyX}{y-xy} &= \dfrac{xY}{y-xy} \\
	\Leftrightarrow X \cdot \left(\dfrac{y-xy+xy}{y-xy}\right) &= \dfrac{xY}{y-xy} \\
	\Leftrightarrow X &= \dfrac{xY \cdot (y-xy)}{(y-xy) \cdot y} = \dfrac{xY}{y}
\end{align*}
We put the last equation in equation (2):
$$Z = \dfrac{Y - y \cdot \dfrac{xY}{y} - yY}{y} = \dfrac{Y \cdot (1-x-y)}{y}$$
For $(X_w,Y_w,Z_w)$ we have
\begin{align*}
	Y_w &= 1 \\
	X_w &= \dfrac{x_w}{y_w} \\
	Z_w &= \dfrac{1-x_w-y_w}{y_w}
\end{align*}
Compare the $C_X$:
$$\left(\begin{array}{ccc}x_r & x_g & x_b \\ y_r & y_g & y_b \\ (1-x_r-y_r) & (1-x_g-y_g) & (1-x_b-y_b)\end{array}\right) \left(\begin{array}{c} C_r \\ C_g \\ C_b\end{array}\right) = \left(\begin{array}{c} \dfrac{x_w}{y_w} \\ 1 \\ \dfrac{1-x_w-y_w}{y_w}\end{array}\right)$$
For \textsf{sRGB} we have:
$$\left(\begin{array}{ccc}0.6400 & 0.3000 & 0.1500 \\ 0.3300 & 0.6000 & 0.0600 \\ 0.0300 & 0.1000 & 0.7900\end{array}\right) \left(\begin{array}{c}C_r \\ C_g \\ C_b\end{array}\right) = \left(\begin{array}{c} \dfrac{0.3127}{0.3290} \\ 1 \\ \dfrac{0.3583}{0.3290}\end{array}\right)$$
Using Maple, we can calculate the values for $C_r, C_g, C_b$:
\begin{align*}
	C_r &= 1.191947798 \\
	C_g &= 0.6443606239 \\
	C_b &= 1.203205256
\end{align*}
Now, we get as transformation matrix the following:
\begin{align*}
	M &:= \left(\begin{array}{ccc}x_rC_r & x_gC_g & x_bC_b \\ y_rC_r & y_gC_g & y_bC_b \\ (1-x_ry_r)C_r & (1-x_g-y_g)C_g & (1-x_b-y_b)C_b\end{array}\right) \\
	&\approx \left(\begin{array}{ccc}0.4123907993 & 0.3575843394 & 0.1804807884 \\ 0.2126390059 & 0.7151686788 & 0.07219231536 \\ 0.01933081872 & 0.1191947798 & 0.9505321522\end{array}\right)
\end{align*}
Finally, we transform the \textsf{sRGB} color $(1,1,0)$ in the \textsf{CIE-XYZ} color space:
$$\left(\begin{array}{c}X \\ Y \\ Z\end{array}\right) = M \cdot \left(\begin{array}{c}1 \\ 1 \\ 0\end{array}\right) \approx \left(\begin{array}{c}0.7699751387 \\ 0.9278076847 \\ 0.1385255985\end{array}\right)$$
Computing \textsf{sRGB} color $(1,1,0)$ in the \textsf{CIE-xy} color space:
\begin{align*}
	x &= \dfrac{X}{X+Y+Z} = 0.4193059997 \\
	y &= \dfrac{Y}{X+Y+Z} = 0.5052570002 \\
	z &= 1-x-y = 0.0754370001
\end{align*}

\end{document}
