\hypertarget{HDE_8c}{
\section{HDE.c File Reference}
\label{HDE_8c}\index{HDE.c@{HDE.c}}
}
Implementation of the routines to encodes a distribution of symbols using a Huffman encoding technique. Routines are available to encode both the distribution itself and the resulting Huffman table. 

{\tt \#include \char`\"{}LDT/HDE.h\char`\"{}}\par
{\tt \#include \char`\"{}LDT/HUFF.h\char`\"{}}\par
{\tt \#include \char`\"{}LDT/BFP.h\char`\"{}}\par
{\tt \#include \char`\"{}LDT/BA.h\char`\"{}}\par
{\tt \#include \char`\"{}ffs.h\char`\"{}}\par
{\tt \#include \char`\"{}dprintf.h\char`\"{}}\par
{\tt \#include \char`\"{}PBS/TMR.h\char`\"{}}\par
{\tt \#include $<$string.h$>$}\par
{\tt \#include $<$math.h$>$}\par


Include dependency graph for HDE.c:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=227pt]{HDE_8c__incl}
\end{center}
\end{figure}
\subsection*{Data Structures}
\begin{CompactItemize}
\item 
struct \hyperlink{struct__HDE}{\_\-HDE}
\begin{CompactList}\small\item\em The control structure for the HDE facility. \item\end{CompactList}\item 
struct \hyperlink{struct__HistLimits}{\_\-Hist\-Limits}
\begin{CompactList}\small\item\em Min and Max indices of a region of the histogram. \item\end{CompactList}\item 
struct \hyperlink{struct__HistCore}{\_\-Hist\-Core}
\begin{CompactList}\small\item\em Describes the result of the encoding selection process, giving the lower and upper bin limits along with the number of entries outside that region. \item\end{CompactList}\item 
struct \hyperlink{struct__CodesSummary}{\_\-Codes\-Summary}
\begin{CompactList}\small\item\em Lays out the structure of the return summary from codes\_\-condition. \item\end{CompactList}\item 
struct \hyperlink{struct__DeltasSummary}{\_\-Deltas\-Summary}
\begin{CompactList}\small\item\em Summarizes the parameters describing the delta code lengths array. \item\end{CompactList}\end{CompactItemize}
\subsection*{Defines}
\begin{CompactItemize}
\item 
\hypertarget{HDE_8c_a0}{
\#define {\bf BA\_\-32}}
\label{HDE_8c_a0}

\item 
\hypertarget{HDE_8c_a1}{
\#define {\bf NULL}~((void $\ast$)(0))}
\label{HDE_8c_a1}

\item 
\hypertarget{HDE_8c_a2}{
\#define {\bf \_\-monitor\_\-declare}(statements)}
\label{HDE_8c_a2}

\item 
\hypertarget{HDE_8c_a3}{
\#define {\bf \_\-monitor\_\-time}(\_\-mon, \_\-idx)}
\label{HDE_8c_a3}

\item 
\hypertarget{HDE_8c_a4}{
\#define {\bf \_\-monitor}(\_\-var, val)}
\label{HDE_8c_a4}

\item 
\hypertarget{HDE_8c_a5}{
\#define {\bf monitor\_\-report}(\_\-mon)}
\label{HDE_8c_a5}

\item 
\#define {\bf \_\-CODES}(\_\-l0, \_\-c0, \_\-l1, \_\-c1, \_\-l2, \_\-c2)
\end{CompactItemize}
\subsection*{Typedefs}
\begin{CompactItemize}
\item 
\hypertarget{HDE_8c_a7}{
typedef \hyperlink{struct__HDE}{\_\-HDE} {\bf HDE}}
\label{HDE_8c_a7}

\item 
typedef \hyperlink{struct__HistLimits}{\_\-Hist\-Limits} \hyperlink{HDE_8c_a8}{Hist\-Limits}
\begin{CompactList}\small\item\em Typedef for struct \hyperlink{struct__HistLimits}{\_\-Hist\-Limits}. \item\end{CompactList}\item 
typedef \hyperlink{struct__HistCore}{\_\-Hist\-Core} \hyperlink{HDE_8c_a9}{Hist\-Core}
\begin{CompactList}\small\item\em Typedef for struct \hyperlink{struct__HistCore}{\_\-Hist\-Core}. \item\end{CompactList}\item 
\hypertarget{HDE_8c_a10}{
typedef \hyperlink{struct__CodesSummary}{\_\-Codes\-Summary} \hyperlink{HDE_8c_a10}{Codes\-Summary}}
\label{HDE_8c_a10}

\begin{CompactList}\small\item\em Typedef for Codes\-Status. \item\end{CompactList}\item 
\hypertarget{HDE_8c_a11}{
typedef \hyperlink{struct__DeltasSummary}{\_\-Deltas\-Summary} \hyperlink{HDE_8c_a11}{Deltas\-Summary}}
\label{HDE_8c_a11}

\begin{CompactList}\small\item\em Typedef for struct \hyperlink{struct__DeltasSummary}{\_\-Deltas\-Summary}. \item\end{CompactList}\end{CompactItemize}
\subsection*{Functions}
\begin{CompactItemize}
\item 
\hypertarget{HDE_8c_a12}{
static \_\-\_\-inline \hyperlink{struct__HistLimits}{Hist\-Limits} {\bf hist\_\-limits\_\-find} (const unsigned short int $\ast$hist, int cnt)}
\label{HDE_8c_a12}

\item 
\hypertarget{HDE_8c_a13}{
static \_\-\_\-inline \hyperlink{struct__HistCore}{Hist\-Core} {\bf hist\_\-select} (const unsigned short int $\ast$hist, int nentries, \hyperlink{struct__HistLimits}{Hist\-Limits} limits, int nbins)}
\label{HDE_8c_a13}

\item 
static \_\-\_\-inline int \hyperlink{HDE_8c_a14}{hist\_\-copy} (unsigned int $\ast$dst, const unsigned short int $\ast$src, int min, int max)
\begin{CompactList}\small\item\em Copies the core of the histogram (i.e. from the first non-zero element to the last non-zero element. \item\end{CompactList}\item 
static \_\-\_\-inline unsigned int \hyperlink{HDE_8c_a15}{huff\_\-table\_\-encode} (unsigned int $\ast$buf, unsigned int pos, \hyperlink{struct__CodesSummary}{Codes\-Summary} cs, const \hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$codes, const unsigned int $\ast$freq, int min, int max, unsigned int nunderflow, unsigned int noverflow)
\begin{CompactList}\small\item\em Encodes the Huffman encoding table. \item\end{CompactList}\item 
static \_\-\_\-inline unsigned int \hyperlink{HDE_8c_a16}{huff\_\-symbols\_\-encode} (unsigned int $\ast$buf, unsigned int pos, const \hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$codes, int min, int max, unsigned int nunderflow, unsigned int noverflow, int xsymbols, const short int $\ast$symbols, int nsymbols)
\begin{CompactList}\small\item\em Encodes the pedestal subtracted ADC values using the specified Huffman codes. \item\end{CompactList}\item 
static \_\-\_\-inline unsigned int \hyperlink{HDE_8c_a17}{direct\_\-symbols\_\-encode} (\hyperlink{struct__HDE}{HDE} $\ast$hde, unsigned int $\ast$buf, unsigned int pos, int min, int nbits, const short int $\ast$symbols, int nsymbols)
\begin{CompactList}\small\item\em Encodes the specified symbols {\em symbols\/} using the specified histogram of frequencies. Both the symbols and the histogram are presented as 16-bit values. \item\end{CompactList}\item 
static \_\-\_\-inline \hyperlink{struct__CodesSummary}{Codes\-Summary} \hyperlink{HDE_8c_a18}{codes\_\-condition} (\hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$codes, const unsigned int $\ast$freq, int ncodes)
\begin{CompactList}\small\item\em Conditions the code array by setting the code lengths of non-existent codes to the maximum code length + 1. \item\end{CompactList}\item 
static \_\-\_\-inline \hyperlink{struct__DeltasSummary}{Deltas\-Summary} \hyperlink{HDE_8c_a19}{form\_\-deltas} (char $\ast$deltas, const \hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$codes, int ncodes, int nseed, int seed)
\begin{CompactList}\small\item\em Forms the array code lengths deltas. \item\end{CompactList}\item 
static \_\-\_\-inline void \hyperlink{HDE_8c_a20}{hist\_\-deltas} (unsigned int $\ast$hist, int min, int nbins, const char $\ast$deltas, int ndeltas)
\begin{CompactList}\small\item\em Histograms the delta distribution. \item\end{CompactList}\item 
static \_\-\_\-inline unsigned int \hyperlink{HDE_8c_a21}{pack\_\-short} (unsigned int $\ast$buf, unsigned int pos, short int val)
\begin{CompactList}\small\item\em Packs the number of bits needed to contain the short integer and its value. \item\end{CompactList}\item 
static \_\-\_\-inline unsigned int \hyperlink{HDE_8c_a22}{pack\_\-seed\_\-min\_\-max} (unsigned int $\ast$buf, unsigned int pos, int seed, int min, int max)
\item 
static \_\-\_\-inline unsigned int \hyperlink{HDE_8c_a23}{pack\_\-delta1} (unsigned int $\ast$buf, unsigned int pos, const char $\ast$deltas, int ndeltas, int min, int max\_\-means\_\-0)
\begin{CompactList}\small\item\em High efficiency routine to handle the case when all the deltas are 0 or 1. \item\end{CompactList}\item 
static \_\-\_\-inline unsigned int \hyperlink{HDE_8c_a24}{pack\_\-delta2} (unsigned int $\ast$buf, unsigned int pos, const char $\ast$deltas, int ndeltas, int min, int max\_\-means\_\-0)
\begin{CompactList}\small\item\em High efficiency routine to handle the case when all the delta range is only 2 (3 values). \item\end{CompactList}\item 
static \_\-\_\-inline unsigned int \hyperlink{HDE_8c_a25}{pack\_\-delta\_\-huff} (unsigned int $\ast$buf, unsigned int pos, const char $\ast$deltas, int cnt, int min, int max, int max\_\-means\_\-0)
\begin{CompactList}\small\item\em Packs the delta code lengths as a huffman table. \item\end{CompactList}\item 
\hypertarget{HDE_8c_a26}{
{\bf declare} (static double calc\_\-nbits(const int $\ast$f, int cnt, int tot))}
\label{HDE_8c_a26}

\item 
unsigned int \hyperlink{HDE_8c_a27}{HDE\_\-sizeof} (unsigned int nhuff)
\begin{CompactList}\small\item\em Returns the size, in bytes, for a handle to hold a Huffman distribution of {\em huffman\_\-bins\/}. \item\end{CompactList}\item 
void $\ast$ \hyperlink{HDE_8c_a28}{HDE\_\-construct} (\hyperlink{struct__HDE}{HDE} $\ast$hde, unsigned int nhuff)
\begin{CompactList}\small\item\em Constructs (initializes) the specified HDE structure. \item\end{CompactList}\item 
unsigned int \hyperlink{HDE_8c_a29}{HDE\_\-encode\-SS} (\hyperlink{struct__HDE}{HDE} $\ast$hde, unsigned int $\ast$buf, unsigned int pos, unsigned int nhuff, const unsigned short int $\ast$hist, int nbins, const short int $\ast$symbols, int nsymbols)
\begin{CompactList}\small\item\em Encodes the specified symbols {\em symbols\/} using the specified histogram of frequencies. Both the symbols and the histogram are presented as 16-bit values. \item\end{CompactList}\item 
const \hyperlink{struct__HDE__assay}{HDE\_\-assay} $\ast$ \hyperlink{HDE_8c_a30}{HDE\_\-assay\-Locate} (const \hyperlink{struct__HDE}{HDE} $\ast$hde)
\begin{CompactList}\small\item\em Returns a pointer to the publically defined piece of this structure. This can be a one-time call, i.e. for any given HDE structure, this routine always returns the same value. \item\end{CompactList}\item 
void \hyperlink{HDE_8c_a31}{HDE\_\-hist\-Clear\-S} (const \hyperlink{struct__HDE}{HDE} $\ast$hde, unsigned short int $\ast$hist)
\begin{CompactList}\small\item\em Clears the histogram using the active bin range stored in the HDE\_\-assay block of the HDE structure. \item\end{CompactList}\item 
double \hyperlink{HDE_8c_a32}{calc\_\-nbits} (const int $\ast$f, int cnt, int tot)
\begin{CompactList}\small\item\em Calculates the number of bits need to encode a sample with the specified frequency distribution. \item\end{CompactList}\end{CompactItemize}


\subsection{Detailed Description}
Implementation of the routines to encodes a distribution of symbols using a Huffman encoding technique. Routines are available to encode both the distribution itself and the resulting Huffman table. 

\begin{Desc}
\item[Author:]JJRussell - \href{mailto:russell@slac.stanford.edu}{\tt russell@slac.stanford.edu}\end{Desc}


\footnotesize\begin{verbatim}   CVS $Id: HDE.c,v 1.4 2006/11/19 14:34:06 russell Exp $
\end{verbatim}
\normalsize


\subsection{Define Documentation}
\hypertarget{HDE_8c_a6}{
\index{HDE.c@{HDE.c}!_CODES@{\_\-CODES}}
\index{_CODES@{\_\-CODES}!HDE.c@{HDE.c}}
\subsubsection[\_\-CODES]{\setlength{\rightskip}{0pt plus 5cm}\#define \_\-CODES(\_\-l0, \_\-c0, \_\-l1, \_\-c1, \_\-l2, \_\-c2)}}
\label{HDE_8c_a6}


{\bf Value:}

\footnotesize\begin{verbatim}(                                         \
      (((_c2 << 2) | _l2) << 8) |             \
      (((_c1 << 2) | _l1) << 4) |             \
      (((_c0 << 2) | _l0) << 0)               \
    )
\end{verbatim}\normalsize 


\subsection{Typedef Documentation}
\hypertarget{HDE_8c_a9}{
\index{HDE.c@{HDE.c}!HistCore@{HistCore}}
\index{HistCore@{HistCore}!HDE.c@{HDE.c}}
\subsubsection[HistCore]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline \hyperlink{struct__HistCore}{Hist\-Core}}}
\label{HDE_8c_a9}


Typedef for struct \hyperlink{struct__HistCore}{\_\-Hist\-Core}. 

hist\_\-select (const unsigned short int $\ast$hist, int nentries, Hist\-Lmits limits, int nbins)

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hist}]The target histogram \item[{\em nentries}]The total number of entries described by the limits \item[{\em limits}]The lower and upper bins of the histogram. These are the first and last bins with data \item[{\em nbins}]The width to select.\end{description}
\end{Desc}
\hypertarget{HDE_8c_a8}{
\index{HDE.c@{HDE.c}!HistLimits@{HistLimits}}
\index{HistLimits@{HistLimits}!HDE.c@{HDE.c}}
\subsubsection[HistLimits]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline \hyperlink{struct__HistLimits}{Hist\-Limits}}}
\label{HDE_8c_a8}


Typedef for struct \hyperlink{struct__HistLimits}{\_\-Hist\-Limits}. 

hist\_\-limits\_\-find (const unsigned short int $\ast$hist, int cnt)

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hist}]The target histogram \item[{\em cnt}]The total number of entries in the histogram\end{description}
\end{Desc}


\subsection{Function Documentation}
\hypertarget{HDE_8c_a32}{
\index{HDE.c@{HDE.c}!calc_nbits@{calc\_\-nbits}}
\index{calc_nbits@{calc\_\-nbits}!HDE.c@{HDE.c}}
\subsubsection[calc\_\-nbits]{\setlength{\rightskip}{0pt plus 5cm}static double calc\_\-nbits (const int $\ast$ {\em f}, int {\em cnt}, int {\em tot})}}
\label{HDE_8c_a32}


Calculates the number of bits need to encode a sample with the specified frequency distribution. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em f}]The frequency distribution \item[{\em cnt}]The count of entries in the frequency distribution \item[{\em tot}]The total number of entries in the frequency distribution\end{description}
\end{Desc}
This is based on the following formula

nbits = - TOT $\ast$ SUM \mbox{[} f\mbox{[}i\mbox{]} / TOT $\ast$ ln (f\mbox{[}i\mbox{]} / TOT) \mbox{]} = - TOT/TOT $\ast$ SUM \mbox{[} f\mbox{[}i\mbox{]} $\ast$ (ln (f\mbox{[}i\mbox{]}) - ln (TOT)) \mbox{]} = - SUM \mbox{[} f\mbox{[}i\mbox{]} $\ast$ ln (f\mbox{[}i\mbox{]} \mbox{]} + SUM \mbox{[} f\mbox{[}i\mbox{]} $\ast$ ln (TOT) \mbox{]} = - SUM \mbox{[} f\mbox{[}i\mbox{]} $\ast$ ln (f\mbox{[}i\mbox{]} \mbox{]} + TOT $\ast$ ln (TOT) \mbox{]}\hypertarget{HDE_8c_a18}{
\index{HDE.c@{HDE.c}!codes_condition@{codes\_\-condition}}
\index{codes_condition@{codes\_\-condition}!HDE.c@{HDE.c}}
\subsubsection[codes\_\-condition]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline \hyperlink{struct__CodesSummary}{Codes\-Summary} codes\_\-condition (\hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$ {\em codes}, const unsigned int $\ast$ {\em freq}, int {\em ncodes})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a18}


Conditions the code array by setting the code lengths of non-existent codes to the maximum code length + 1. 

\begin{Desc}
\item[Returns:]A packed value with the minimum code length in the low 8-bits\end{Desc}
$\backslash$\hypertarget{HDE_8c_a17}{
\index{HDE.c@{HDE.c}!direct_symbols_encode@{direct\_\-symbols\_\-encode}}
\index{direct_symbols_encode@{direct\_\-symbols\_\-encode}!HDE.c@{HDE.c}}
\subsubsection[direct\_\-symbols\_\-encode]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline unsigned int direct\_\-symbols\_\-encode (\hyperlink{struct__HDE}{HDE} $\ast$ {\em hde}, unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, int {\em min}, int {\em nbits}, const short int $\ast$ {\em symbols}, int {\em nsymbols})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a17}


Encodes the specified symbols {\em symbols\/} using the specified histogram of frequencies. Both the symbols and the histogram are presented as 16-bit values. 

\begin{Desc}
\item[Returns:]The updated bit write position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hde}]The HDE control structure \item[{\em buf}]The output buffer address \item[{\em pos}]The current write bit offset \item[{\em min}]The value of the smallest symbol \item[{\em nbits}]The number of bits needed to encode the widest symbol \item[{\em symbols}]The array of symbols \item[{\em nsymbols}]The number of symbols\end{description}
\end{Desc}
\hypertarget{HDE_8c_a19}{
\index{HDE.c@{HDE.c}!form_deltas@{form\_\-deltas}}
\index{form_deltas@{form\_\-deltas}!HDE.c@{HDE.c}}
\subsubsection[form\_\-deltas]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline \hyperlink{struct__DeltasSummary}{Deltas\-Summary} form\_\-deltas (char $\ast$ {\em deltas}, const \hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$ {\em codes}, int {\em ncodes}, int {\em nseed}, int {\em seed})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a19}


Forms the array code lengths deltas. 

\begin{Desc}
\item[Returns:]A summary description, giving the min, max and seed values\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em deltas}]The array of deltas to be filled out \item[{\em codes}]The HUFF\_\-man code array to be differinated \item[{\em ncodes}]The number of codes in {\em codes\/} \item[{\em nseed}]The seed bin \item[{\em seed}]The seed value\end{description}
\end{Desc}
\hypertarget{HDE_8c_a30}{
\index{HDE.c@{HDE.c}!HDE_assayLocate@{HDE\_\-assayLocate}}
\index{HDE_assayLocate@{HDE\_\-assayLocate}!HDE.c@{HDE.c}}
\subsubsection[HDE\_\-assayLocate]{\setlength{\rightskip}{0pt plus 5cm}const \hyperlink{struct__HDE__assay}{HDE\_\-assay}$\ast$ HDE\_\-assay\-Locate (const \hyperlink{struct__HDE}{HDE} $\ast$ {\em hde})}}
\label{HDE_8c_a30}


Returns a pointer to the publically defined piece of this structure. This can be a one-time call, i.e. for any given HDE structure, this routine always returns the same value. 

\begin{Desc}
\item[Returns:]Pointer to the HDE\_\-assay structure\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hde}]The HDE control structure\end{description}
\end{Desc}
The information returned is mainly of diagnostic use. However, knowing the min and max bin entry allows one to efficiently clear a previously used histogram.\hypertarget{HDE_8c_a28}{
\index{HDE.c@{HDE.c}!HDE_construct@{HDE\_\-construct}}
\index{HDE_construct@{HDE\_\-construct}!HDE.c@{HDE.c}}
\subsubsection[HDE\_\-construct]{\setlength{\rightskip}{0pt plus 5cm}void$\ast$ HDE\_\-construct (\hyperlink{struct__HDE}{HDE} $\ast$ {\em hde}, unsigned int {\em nhuff})}}
\label{HDE_8c_a28}


Constructs (initializes) the specified HDE structure. 

\begin{Desc}
\item[Returns:]The next address after this structure.\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hde}]The HDE control structure to construct \item[{\em nhuff}]The maximum number of Huffman encoding bins that this control structure is meant to support.\end{description}
\end{Desc}
The size of this structure should be acquired by a call to HDE\_\-sizeof. The parameter {\em huffman\_\-nbins\/} must be the same in the call to HDE\_\-sizeof and HDE\_\-construct.\hypertarget{HDE_8c_a29}{
\index{HDE.c@{HDE.c}!HDE_encodeSS@{HDE\_\-encodeSS}}
\index{HDE_encodeSS@{HDE\_\-encodeSS}!HDE.c@{HDE.c}}
\subsubsection[HDE\_\-encodeSS]{\setlength{\rightskip}{0pt plus 5cm}unsigned int HDE\_\-encode\-SS (\hyperlink{struct__HDE}{HDE} $\ast$ {\em hde}, unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, unsigned int {\em nhuff}, const unsigned short int $\ast$ {\em hist}, int {\em nbins}, const short int $\ast$ {\em symbols}, int {\em nsymbols})}}
\label{HDE_8c_a29}


Encodes the specified symbols {\em symbols\/} using the specified histogram of frequencies. Both the symbols and the histogram are presented as 16-bit values. 

\begin{Desc}
\item[Returns:]The updated bit write position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hde}]The HDE control structure \item[{\em buf}]The output buffer address \item[{\em pos}]The current write bit offset \item[{\em nhuff}]Provides a means to limit the size of the huffman encoding region to a region that is smaller than what was specified during the HDE construction (i.e. the {\em nhuff\/} parameter) The construction value is used, if this value is 0 or larger. \item[{\em hist}]The frequency histogram. The histogram is such that it extends -/+ nbins from this pointer. That is, this is the center of the histogram. \item[{\em nbins}]The half width of the histogram \item[{\em symbols}]The array of symbols \item[{\em nsymbols}]The number of symbols\end{description}
\end{Desc}
\hypertarget{HDE_8c_a31}{
\index{HDE.c@{HDE.c}!HDE_histClearS@{HDE\_\-histClearS}}
\index{HDE_histClearS@{HDE\_\-histClearS}!HDE.c@{HDE.c}}
\subsubsection[HDE\_\-histClearS]{\setlength{\rightskip}{0pt plus 5cm}void HDE\_\-hist\-Clear\-S (const \hyperlink{struct__HDE}{HDE} $\ast$ {\em hde}, unsigned short int $\ast$ {\em hist})}}
\label{HDE_8c_a31}


Clears the histogram using the active bin range stored in the HDE\_\-assay block of the HDE structure. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hde}]The HDE control handle \item[{\em hist}]The histogram to clear\end{description}
\end{Desc}
This routine must be called after HDE\_\-encode\-SS has been called, since it is that routine that sets up the HDE\_\-assay block\hypertarget{HDE_8c_a27}{
\index{HDE.c@{HDE.c}!HDE_sizeof@{HDE\_\-sizeof}}
\index{HDE_sizeof@{HDE\_\-sizeof}!HDE.c@{HDE.c}}
\subsubsection[HDE\_\-sizeof]{\setlength{\rightskip}{0pt plus 5cm}unsigned int HDE\_\-sizeof (unsigned int {\em nhuff})}}
\label{HDE_8c_a27}


Returns the size, in bytes, for a handle to hold a Huffman distribution of {\em huffman\_\-bins\/}. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em nhuff}]The maximum size of the Huffman encoding area Suggested values are small powers of 2 like 32, 64, 128. Note also up to 3 bins may be used to hold describe the underflow, overflow and exception areas. Currently these 3 are mapped into 1 bin, but this may change in a future implementation\end{description}
\end{Desc}
For the most part, this only provides a working area. On host-based workstations with expandable stacks, this memory would go on the stack, but, it is too large to do this for embedded systems.\hypertarget{HDE_8c_a14}{
\index{HDE.c@{HDE.c}!hist_copy@{hist\_\-copy}}
\index{hist_copy@{hist\_\-copy}!HDE.c@{HDE.c}}
\subsubsection[hist\_\-copy]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline int hist\_\-copy (unsigned int $\ast$ {\em dst}, const unsigned short int $\ast$ {\em src}, int {\em min}, int {\em max})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a14}


Copies the core of the histogram (i.e. from the first non-zero element to the last non-zero element. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em dst}]The destination histogram \item[{\em src}]The source histogram \item[{\em min}]The first non-zero histogram bin \item[{\em max}]The last non-zero histogram bin\end{description}
\end{Desc}
This routine exists solely because the HUFF build routines only take an integer (not a short integer) frequency distribution.\hypertarget{HDE_8c_a20}{
\index{HDE.c@{HDE.c}!hist_deltas@{hist\_\-deltas}}
\index{hist_deltas@{hist\_\-deltas}!HDE.c@{HDE.c}}
\subsubsection[hist\_\-deltas]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline void hist\_\-deltas (unsigned int $\ast$ {\em hist}, int {\em min}, int {\em nbins}, const char $\ast$ {\em deltas}, int {\em ndeltas})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a20}


Histograms the delta distribution. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hist}]The resulting histogram \item[{\em min}]The minimum bin number \item[{\em nbins}]The number of bins in the histogram \item[{\em deltas}]The deltas to histogram \item[{\em ndeltas}]The number of deltas to histogram\end{description}
\end{Desc}
\hypertarget{HDE_8c_a16}{
\index{HDE.c@{HDE.c}!huff_symbols_encode@{huff\_\-symbols\_\-encode}}
\index{huff_symbols_encode@{huff\_\-symbols\_\-encode}!HDE.c@{HDE.c}}
\subsubsection[huff\_\-symbols\_\-encode]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline unsigned int huff\_\-symbols\_\-encode (unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, const \hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$ {\em codes}, int {\em min}, int {\em max}, unsigned int {\em nunderflow}, unsigned int {\em noverflow}, int {\em xsymbols}, const short int $\ast$ {\em symbols}, int {\em nsymbols})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a16}


Encodes the pedestal subtracted ADC values using the specified Huffman codes. 

\begin{Desc}
\item[Returns:]The updated write bit position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em buf}]The output buffer \item[{\em pos}]The position of the next bit to fill \item[{\em codes}]The Huffman codes \item[{\em min}]The minimum symbol value for Huffman encoding \item[{\em max}]The maximum symbol value for Huffman encoding If 'p \item[{\em nunderflow}]The size of the underflow region \item[{\em noverflow}]The size of the overflow region \item[{\em xsymbols}]The number of symbols in the under/over flow regions \item[{\em symbols}]The symbols to encode \item[{\em nsymbols}]The number of symbols to encode\end{description}
\end{Desc}
\hypertarget{HDE_8c_a15}{
\index{HDE.c@{HDE.c}!huff_table_encode@{huff\_\-table\_\-encode}}
\index{huff_table_encode@{huff\_\-table\_\-encode}!HDE.c@{HDE.c}}
\subsubsection[huff\_\-table\_\-encode]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline unsigned int huff\_\-table\_\-encode (unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, \hyperlink{struct__CodesSummary}{Codes\-Summary} {\em cs}, const \hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$ {\em codes}, const unsigned int $\ast$ {\em freq}, int {\em min}, int {\em max}, unsigned int {\em under}, unsigned int {\em over})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a15}


Encodes the Huffman encoding table. 

\begin{Desc}
\item[Returns:]The updated write bit position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em buf}]The output buffer \item[{\em pos}]The position of the next bit to fill \item[{\em cs}]The code summary status \item[{\em codes}]The Huffman codes \item[{\em freq}]The frequency distribution. This is used to determine valid members. \item[{\em min}]The minimum code index \item[{\em max}]The maximum code index \item[{\em under}]The number of bins in the underflow region \item[{\em over}]The number of bins in the overflow region\end{description}
\end{Desc}
\hypertarget{HDE_8c_a23}{
\index{HDE.c@{HDE.c}!pack_delta1@{pack\_\-delta1}}
\index{pack_delta1@{pack\_\-delta1}!HDE.c@{HDE.c}}
\subsubsection[pack\_\-delta1]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline unsigned int pack\_\-delta1 (unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, const char $\ast$ {\em deltas}, int {\em ndeltas}, int {\em min}, int {\em max\_\-means\_\-0})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a23}


High efficiency routine to handle the case when all the deltas are 0 or 1. 

\begin{Desc}
\item[Returns:]The updated write bit position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em buf}]The output buffer \item[{\em pos}]The position of the next bit to fill \item[{\em deltas}]The deltas, by defintion, these must be 0 or 1 \item[{\em ndeltas}]The number of deltas \item[{\em min}]The minimum value \item[{\em max\_\-means\_\-0}]If true, when the original code lengths restored from the deltas, the maximum code length really means 0\end{description}
\end{Desc}
\hypertarget{HDE_8c_a24}{
\index{HDE.c@{HDE.c}!pack_delta2@{pack\_\-delta2}}
\index{pack_delta2@{pack\_\-delta2}!HDE.c@{HDE.c}}
\subsubsection[pack\_\-delta2]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline unsigned int pack\_\-delta2 (unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, const char $\ast$ {\em deltas}, int {\em ndeltas}, int {\em min}, int {\em max\_\-means\_\-0})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a24}


High efficiency routine to handle the case when all the delta range is only 2 (3 values). 

\begin{Desc}
\item[Returns:]The updated write bit position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em buf}]The output buffer \item[{\em pos}]The position of the next bit to fill \item[{\em deltas}]The deltas, must have a range of only 2 \item[{\em ndeltas}]The number of deltas \item[{\em min}]The minimum value \item[{\em max\_\-means\_\-0}]If true, when the original code lengths restored from the deltas, the maximum code length really means 0\end{description}
\end{Desc}
Need just 1 or 2 bits of information to define this packing scheme\begin{enumerate}
\item 1 bit indicating that 0 is not the maximum occuring bin, and if so, another bits indicating which of the other two is the maximum occurring bin.\end{enumerate}


In practice here are the assignments

If max is Bin0 Bin1 Bin2 Len:Code Len:Code Lne:Code Min == -1 2,3 1,0 2,2 Min != -1 1:0 2,2 2,3\hypertarget{HDE_8c_a25}{
\index{HDE.c@{HDE.c}!pack_delta_huff@{pack\_\-delta\_\-huff}}
\index{pack_delta_huff@{pack\_\-delta\_\-huff}!HDE.c@{HDE.c}}
\subsubsection[pack\_\-delta\_\-huff]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline unsigned int pack\_\-delta\_\-huff (unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, const char $\ast$ {\em deltas}, int {\em ndeltas}, int {\em min}, int {\em max}, int {\em max\_\-means\_\-0})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a25}


Packs the delta code lengths as a huffman table. 

\begin{Desc}
\item[Returns:]The updated write bit position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em buf}]The output buffer \item[{\em pos}]The position of the next bit to fill \item[{\em deltas}]The deltas to pack \item[{\em ndeltas}]The number of deltas to pack \item[{\em min}]The minimum delta value \item[{\em max}]The maximum delta value \item[{\em max\_\-means\_\-0}]If true, when the original code lengths restored from the deltas, the maximum code length really means 0\end{description}
\end{Desc}
\hypertarget{HDE_8c_a22}{
\index{HDE.c@{HDE.c}!pack_seed_min_max@{pack\_\-seed\_\-min\_\-max}}
\index{pack_seed_min_max@{pack\_\-seed\_\-min\_\-max}!HDE.c@{HDE.c}}
\subsubsection[pack\_\-seed\_\-min\_\-max]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline unsigned int pack\_\-seed\_\-min\_\-max (unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, int {\em seed}, int {\em min}, int {\em max})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a22}


\begin{Desc}
\item[Returns:]The updated write bit position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em buf}]The output buffer \item[{\em pos}]The position of the next bit to fill \item[{\em seed}]The seed value \item[{\em min}]The minimum delta value \item[{\em max}]The maximum delta value\end{description}
\end{Desc}
Wish to make an optimization when all three values are small, as they are likely to be (usually 3 bits or less). A code giving the number of bits each is pack in is output. This code is\begin{itemize}
\item 0 for 2 bits\item 1 for 3 bits\item 2 for 4 bits\item 3 for 6 bits\end{itemize}
\hypertarget{HDE_8c_a21}{
\index{HDE.c@{HDE.c}!pack_short@{pack\_\-short}}
\index{pack_short@{pack\_\-short}!HDE.c@{HDE.c}}
\subsubsection[pack\_\-short]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline unsigned int pack\_\-short (unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, short int {\em val})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a21}


Packs the number of bits needed to contain the short integer and its value. 

\begin{Desc}
\item[Returns:]The update bit write position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em buf}]The output buffer \item[{\em pos}]The position of the next bit to fill \item[{\em val}]The value to pack\end{description}
\end{Desc}
