\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#244: PNG Photo Image Support for Tk}
\date{March 22, 2005}
\author{Michael Kirkham}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#244}&\textbf{PNG Photo Image Support for Tk}\\\hline
Author&

Michael Kirkham \tipmail{mikek}{muonics.com}
\\
Created&Tuesday, \(\text{22}^{\text{nd}}\) March 2005\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.7 $\\
Post History&\\
Discussions To&news:comp.lang.tcl\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

Tk's photo image type has had support for alpha channels internally since version 8.3. However, to date there is no photo format in the core that exposes this functionality. This TIP proposes adding support for the PNG (Portable Network Graphics) format.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

There is a current push for ``beautifying'' Tcl/Tk with more modern appearing widgets and default configuration options. One method that can provide significant improvement in appearance is the use of images with alpha channels for antialiasing and smooth blending into the background of the GUI. While the Tk core supports alpha channels, this support is neither exposed through the Tk\_GetColor() format used by \textbf{image put} and similar interfaces, nor by the only photo image format, GIF, which allows only fully-opaque or fully-transparent pixels. Instead, a third party package is currently required to add a format supporting alpha channels, which may not have a suitable license or increase the number of external dependencies unreasonably.

The PNG image format is an open standard supporting a wide range of colors, from 1 bit per pixel black and white to 16 bit per pixel RGBA, with loss-less compression. It's becoming increasingly widely used as the image format of choice in many areas including GUI and web page design for its color range, alpha support and small image size. As such it is the obvious choice for adding alpha support to Tk. An implementation can be light-weight and not add any new dependencies (such as on libpng) to Tk: while the format also depends on the zlib format for compression, \cite{tip234} proposes zlib support for the Tcl core.

\section{Specification}

The implementation shall use the \textit{Tk\_CreatePhotoImageFormat()} mechanisms to supply handlers to Tk for PNG support. The implementation must:
\begin{enumerate}
\item{}
Support the full range of color types and bit depths allowed and required by the PNG standard, including alpha channels and boolean transparency.
\setcounter{enumi}{1}
\item{}
Support reading of interlaced images.
\setcounter{enumi}{2}
\item{}
Support base64 encoded data through \textbf{image create photo -data}.
\setcounter{enumi}{3}
\item{}
Otherwise conform to the requirements of the PNG specification, such as the handling of unsupported ancillary and critical chunks and support for all required filter types.
\end{enumerate}

In addition, the implementation should:
\begin{enumerate}
\setcounter{enumi}{4}
\item{}
Support exporting to the PNG format through \textbf{image data} and \textbf{image write}.
\setcounter{enumi}{5}
\item{}
Allow modification of the overall alpha transparency of imported images through \textbf{-format ``png -alpha value''}.
\setcounter{enumi}{6}
\item{}
Allow application of display gamma (if known) in conjunction with the ``gAMA'' chunk of imported images, if present, through \textbf{-format ``png -gamma value''}.
\end{enumerate}

\section{Reference Implementation}

A reference implementation is available [\url{http://www.muonics.com/FreeStuff/TkPNG/}]. This reference implementation has been done as a loadable extension, but requires only the file \textit{tkImgPNG.c} and the \textit{Tk\_CreatePhototoImageFormat()} invocation from \textit{tkImgPNGInit.c} to be added to \textit{CreateTopLevelWindow()} in \textit{tkWindow.c} to be built into the core.

The only new dependency for this implementation is on zlib. It currently meets requirements 1 --- 6; it does not implement 7.

\subsection{Notes}
\begin{enumerate}
\item{}
It is understood that there will likely be some reformatting required to conform to the Tcl Style Guide if this TIP is accepted.
\setcounter{enumi}{1}
\item{}
Performance is currently comparable to Img for small (i.e., toolbar button) sized images --- a little faster or slower depending on the image. It seems to be somewhat slower for larger images, but there may be room for additional optimizations.
\setcounter{enumi}{2}
\item{}
At this time there is a bug (1155596) in Tk Aqua bevel buttons that will crash Tk if images with partially transparent pixels are used. This is not the fault of the extension, but occurs also with Img. The author is attempting to find a fix, however.
\setcounter{enumi}{3}
\item{}
The implementation has been tested with PngSuite, which is fairly exhaustive, but more tests would probably be useful.
\end{enumerate}

\section{Copyright}

This document has been placed in the public domain.

\section{References}
\begin{itemize}
\item{}
PngSuite [\url{http://www.schaik.com/pngsuite/pngsuite.html}]
\item{}
Portable Network Graphics (PNG) Specification and Extensions [\url{http://www.libpng.org/pub/png/spec/}]
\item{}
zlib Compression Library [\url{http://www.gzip.org/zlib/}]
\item{}
zlib Specifications [\url{http://www.gzip.org/zlib/zlib_docs.html}]
\end{itemize}

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#234]{tip234}
Pascal Scheffers,
\emph{Add Support For Zlib Compression},
on-line at \url{http://purl.org/tcl/tip/234.html}
\end{thebibliography}
\end{document}
