% -*- mode: latex; -*- mustache tags:  
\ifx\wholebook\relax\else

	\documentclass[10pt,twoside,english]{root/support/latex/sbabook/sbabook}
  \usepackage{import}
  \subimport{root/support/latex/}{common.tex}

  \hypersetup{pdfinfo = {
    Title = {{Pharo Mooc}},
    Author = {},
    Keywords = {pharo, smalltalk}}}
  \begin{document}
\fi

\chapter{Challenge 2 - Solution}
Extract sprites from a larger png file such as the one we can find at \url{http://gaurav.munjal.us/Universal-LPC-Spritesheet-Character-Generator/}
The picture below is showing one of the sprite collection. 
\subsection{Hints:}
\begin{itemize}
\item You can read and convert a PNG into a form (a graphics pharo basic element) using \textcode{PNGReadWriter}.
\item Use \textcode{asFileReference} or \textcode{FileSystem workingDirectory} if the file is located close to the pharo.image file to get to a file.
\item You can use the message \textcode{/} to specify a file name in the path. 
\item Pay attention the stream should be binary.
\item You can access the size of a form using message \textcode{width} and \textcode{height}.
\item You can copy a part of a form using the message \textcode{form copy: aRectangle}.
\item Looking at the results of expressions with the inspector is a great idea.
\end{itemize}
\subsection{Solution }
\begin{displaycode}{smalltalk}
	| form sprites |
	form := 'images/Sprites.png' asFileReference 
		binaryReadStreamDo: [ :stream | PNGReadWriter formFromStream: stream ].
	sprites := OrderedCollection new.
	0 to: (form width - 64) by: 64 do: [ :x |
	   0 to: (form height - 64) by: 64 do: [ :y |
	      sprites add: (form copy: (x@y corner: (x+64)@(y+64)))]].
	sprites
\end{displaycode}


\begin{figure}

\begin{center}
\includegraphics[width=0.8\textwidth]{figures/sprites.png}\caption{\label{figures/sprites.png}}\end{center}
\end{figure}



\ifx\wholebook\relax\else
\end{document}
\fi