001/*
002 * $RCSfile: StdQuantizer.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:20 $
005 * $State: Exp $
006 *
007 * Class:                   StdQuantizer
008 *
009 * Description:             Scalar deadzone quantizer of integer or float
010 *                          data.
011 *
012 *                          Mergerd from StdQuantizerInt and
013 *                          StdQuantizerFloat from Joel Askelof.
014 *
015 *
016 * COPYRIGHT:
017 *
018 * This software module was originally developed by Raphaël Grosbois and
019 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
020 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
021 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
022 * Centre France S.A) in the course of development of the JPEG2000
023 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
024 * software module is an implementation of a part of the JPEG 2000
025 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
026 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
027 * Partners) agree not to assert against ISO/IEC and users of the JPEG
028 * 2000 Standard (Users) any of their rights under the copyright, not
029 * including other intellectual property rights, for this software module
030 * with respect to the usage by ISO/IEC and Users of this software module
031 * or modifications thereof for use in hardware or software products
032 * claiming conformance to the JPEG 2000 Standard. Those intending to use
033 * this software module in hardware or software products are advised that
034 * their use may infringe existing patents. The original developers of
035 * this software module, JJ2000 Partners and ISO/IEC assume no liability
036 * for use of this software module or modifications thereof. No license
037 * or right to this software module is granted for non JPEG 2000 Standard
038 * conforming products. JJ2000 Partners have full right to use this
039 * software module for his/her own purpose, assign or donate this
040 * software module to any third party and to inhibit third parties from
041 * using this software module for non JPEG 2000 Standard conforming
042 * products. This copyright notice must be included in all copies or
043 * derivative works of this software module.
044 *
045 * Copyright (c) 1999/2000 JJ2000 Partners.
046 * */
047package jj2000.j2k.quantization.quantizer;
048import jj2000.j2k.image.DataBlk;
049import jj2000.j2k.quantization.GuardBitsSpec;
050import jj2000.j2k.quantization.QuantStepSizeSpec;
051import jj2000.j2k.quantization.QuantTypeSpec;
052import jj2000.j2k.wavelet.Subband;
053import jj2000.j2k.wavelet.analysis.CBlkWTData;
054import jj2000.j2k.wavelet.analysis.CBlkWTDataFloat;
055import jj2000.j2k.wavelet.analysis.CBlkWTDataInt;
056import jj2000.j2k.wavelet.analysis.CBlkWTDataSrc;
057import jj2000.j2k.wavelet.analysis.SubbandAn;
058
059import com.github.jaiimageio.jpeg2000.impl.J2KImageWriteParamJava;
060
061/**
062 * This class implements scalar quantization of integer or floating-point
063 * valued source data. The source data is the wavelet transformed image data
064 * and the output is the quantized wavelet coefficients represented in
065 * sign-magnitude (see below).
066 *
067 * <P>Sign magnitude representation is used (instead of two's complement) for
068 * the output data. The most significant bit is used for the sign (0 if
069 * positive, 1 if negative). Then the magnitude of the quantized coefficient
070 * is stored in the next M most significat bits. The rest of the bits (least
071 * significant bits) can contain a fractional value of the quantized
072 * coefficient. This fractional value is not to be coded by the entropy
073 * coder. However, it can be used to compute rate-distortion measures with
074 * greater precision.
075 *
076 * <P>The value of M is determined for each subband as the sum of the number
077 * of guard bits G and the nominal range of quantized wavelet coefficients in
078 * the corresponding subband (Rq), minus 1:
079 *
080 * <P>M = G + Rq -1
081 *
082 * <P>The value of G should be the same for all subbands. The value of Rq
083 * depends on the quantization step size, the nominal range of the component
084 * before the wavelet transform and the analysis gain of the subband (see
085 * Subband).
086 *
087 * <P>The blocks of data that are requested should not cross subband
088 * boundaries.
089 *
090 * @see Subband
091 *
092 * @see Quantizer
093 * */
094public class StdQuantizer extends Quantizer {
095
096    /** The number of mantissa bits for the quantization steps */
097    public final static int QSTEP_MANTISSA_BITS = 11;
098
099    /** The number of exponent bits for the quantization steps */
100    // NOTE: formulas in 'convertFromExpMantissa()' and
101    // 'convertToExpMantissa()' methods do not support more than 5 bits.
102    public final static int QSTEP_EXPONENT_BITS = 5;
103
104    /** The maximum value of the mantissa for the quantization steps */
105    public final static int QSTEP_MAX_MANTISSA = (1<<QSTEP_MANTISSA_BITS)-1;
106
107    /** The maximum value of the exponent for the quantization steps */
108    public final static int QSTEP_MAX_EXPONENT = (1<<QSTEP_EXPONENT_BITS)-1;
109
110    /** Natural log of 2, used as a convenience variable */
111    private static double log2 = Math.log(2);
112
113    /** The quantization type specifications */
114    private QuantTypeSpec qts;
115
116    /** The quantization step size specifications */
117    private QuantStepSizeSpec qsss;
118
119    /** The guard bits specifications */
120    private GuardBitsSpec gbs;
121
122    /** The 'CBlkWTDataFloat' object used to request data, used when
123     * quantizing floating-point data. */
124    // This variable makes the class thread unsafe, but it avoids allocating
125    // new objects for code-block that is quantized.
126    private CBlkWTDataFloat infblk;
127
128    /**
129     * Initializes the source of wavelet transform coefficients. The
130     * constructor takes information on whether the quantizer is in
131     * reversible, derived or expounded mode. If the quantizer is reversible
132     * the value of 'derived' is ignored. If the source data is not integer
133     * (int) then the quantizer can not be reversible.
134     *
135     * <P> After initializing member attributes, getAnSubbandTree is called for
136     * all components setting the 'stepWMSE' for all subbands in the current
137     * tile.
138     *
139     * @param src The source of wavelet transform coefficients.
140     *
141     * @param encSpec The encoder specifications
142     * */
143    public StdQuantizer(CBlkWTDataSrc src,J2KImageWriteParamJava wp){
144        super(src);
145        qts  = wp.getQuantizationType();
146        qsss = wp.getQuantizationStep();
147        gbs  = wp.getGuardBits();
148    }
149
150    /**
151     * Returns the quantization type spec object associated to the quantizer.
152     *
153     * @return The quantization type spec
154     * */
155    public QuantTypeSpec getQuantTypeSpec(){
156        return qts;
157    }
158
159    /**
160     * Returns the number of guard bits used by this quantizer in the given
161     * tile-component.
162     *
163     * @param t Tile index
164     *
165     * @param c Component index
166     *
167     * @return The number of guard bits
168     * */
169    public int getNumGuardBits(int t,int c){
170        return ((Integer)gbs.getTileCompVal(t,c)).intValue();
171    }
172
173    /**
174     * Returns true if the quantized data is reversible, for the specified
175     * tile-component. For the quantized data to be reversible it is necessary
176     * and sufficient that the quantization is reversible.
177     *
178     * @param t The tile to test for reversibility
179     *
180     * @param c The component to test for reversibility
181     *
182     * @return True if the quantized data is reversible, false if not.
183     * */
184    public boolean isReversible(int t,int c){
185        return qts.isReversible(t,c);
186    }
187
188    /**
189     * Returns true if given tile-component uses derived quantization step
190     * sizes.
191     *
192     * @param t Tile index
193     *
194     * @param c Component index
195     *
196     * @return True if derived
197     *
198     */
199    public boolean isDerived(int t,int c){
200        return qts.isDerived(t,c);
201    }
202
203    /**
204     * Returns the next code-block in the current tile for the specified
205     * component, as a copy (see below). The order in which code-blocks are
206     * returned is not specified. However each code-block is returned only
207     * once and all code-blocks will be returned if the method is called 'N'
208     * times, where 'N' is the number of code-blocks in the tile. After all
209     * the code-blocks have been returned for the current tile calls to this
210     * method will return 'null'.
211     *
212     * <P>When changing the current tile (through 'setTile()' or 'nextTile()')
213     * this method will always return the first code-block, as if this method
214     * was never called before for the new current tile.
215     *
216     * <P>The data returned by this method is always a copy of the
217     * data. Therfore it can be modified "in place" without any problems after
218     * being returned. The 'offset' of the returned data is 0, and the 'scanw'
219     * is the same as the code-block width. See the 'CBlkWTData' class.
220     *
221     * <P>The 'ulx' and 'uly' members of the returned 'CBlkWTData' object
222     * contain the coordinates of the top-left corner of the block, with
223     * respect to the tile, not the subband.
224     *
225     * @param c The component for which to return the next code-block.
226     *
227     * @param cblk If non-null this object will be used to return the new
228     * code-block. If null a new one will be allocated and returned. If the
229     * "data" array of the object is non-null it will be reused, if possible,
230     * to return the data.
231     *
232     * @return The next code-block in the current tile for component 'n', or
233     * null if all code-blocks for the current tile have been returned.
234     *
235     * @see CBlkWTData
236     * */
237    public CBlkWTData getNextCodeBlock(int c,CBlkWTData cblk) {
238        return getNextInternCodeBlock(c,cblk);
239    }
240
241    /**
242     * Returns the next code-block in the current tile for the specified
243     * component. The order in which code-blocks are returned is not
244     * specified. However each code-block is returned only once and all
245     * code-blocks will be returned if the method is called 'N' times, where
246     * 'N' is the number of code-blocks in the tile. After all the code-blocks
247     * have been returned for the current tile calls to this method will
248     * return 'null'.
249     *
250     * <P>When changing the current tile (through 'setTile()' or 'nextTile()')
251     * this method will always return the first code-block, as if this method
252     * was never called before for the new current tile.
253     *
254     * <P>The data returned by this method can be the data in the internal
255     * buffer of this object, if any, and thus can not be modified by the
256     * caller. The 'offset' and 'scanw' of the returned data can be
257     * arbitrary. See the 'CBlkWTData' class.
258     *
259     * <P>The 'ulx' and 'uly' members of the returned 'CBlkWTData' object
260     * contain the coordinates of the top-left corner of the block, with
261     * respect to the tile, not the subband.
262     *
263     * @param c The component for which to return the next code-block.
264     *
265     * @param cblk If non-null this object will be used to return the new
266     * code-block. If null a new one will be allocated and returned. If the
267     * "data" array of the object is non-null it will be reused, if possible,
268     * to return the data.
269     *
270     * @return The next code-block in the current tile for component 'n', or
271     * null if all code-blocks for the current tile have been returned.
272     *
273     * @see CBlkWTData
274     * */
275    public final CBlkWTData getNextInternCodeBlock(int c, CBlkWTData cblk) {
276        // NOTE: this method is declared final since getNextCodeBlock() relies
277        // on this particular implementation
278        int k,j;
279        int tmp,shiftBits,jmin;
280        int w,h;
281        int outarr[];
282        float infarr[] = null;
283        CBlkWTDataFloat infblk;
284        float invstep;    // The inverse of the quantization step size
285        boolean intq;     // flag for quantizig ints
286        SubbandAn sb;
287        float stepUDR;    // The quantization step size (for a dynamic
288                          // range of 1, or unit)
289        int g = ((Integer)gbs.getTileCompVal(tIdx,c)).intValue();
290
291        // Are we quantizing ints or floats?
292        intq = (src.getDataType(tIdx,c) == DataBlk.TYPE_INT);
293
294        // Check that we have an output object
295        if (cblk == null) {
296            cblk = new CBlkWTDataInt();
297        }
298
299        // Cache input float code-block
300        infblk = this.infblk;
301
302        // Get data to quantize. When quantizing int data 'cblk' is used to
303        // get the data to quantize and to return the quantized data as well,
304        // that's why 'getNextCodeBlock()' is used. This can not be done when
305        // quantizing float data because of the different data types, that's
306        // why 'getNextInternCodeBlock()' is used in that case.
307        if (intq) { // Source data is int
308            cblk = src.getNextCodeBlock(c,cblk);
309            if (cblk == null) {
310                return null; // No more code-blocks in current tile for comp.
311            }
312            // Input and output arrays are the same (for "in place" quant.)
313            outarr = (int[])cblk.getData();
314        }
315        else { // Source data is float
316            // Can not use 'cblk' to get float data, use 'infblk'
317            infblk = (CBlkWTDataFloat) src.getNextInternCodeBlock(c,infblk);
318            if (infblk == null) {
319                // Release buffer from infblk: this enables to garbage collect
320                // the big buffer when we are done with last code-block of
321                // component.
322                this.infblk.setData(null);
323                return null; // No more code-blocks in current tile for comp.
324            }
325            this.infblk = infblk; // Save local cache
326            infarr = (float[])infblk.getData();
327            // Get output data array and check that there is memory to put the
328            // quantized coeffs in
329            outarr = (int[]) cblk.getData();
330            if (outarr == null || outarr.length < infblk.w*infblk.h) {
331                outarr = new int[infblk.w*infblk.h];
332                cblk.setData(outarr);
333            }
334            cblk.m = infblk.m;
335            cblk.n = infblk.n;
336            cblk.sb = infblk.sb;
337            cblk.ulx = infblk.ulx;
338            cblk.uly = infblk.uly;
339            cblk.w = infblk.w;
340            cblk.h = infblk.h;
341            cblk.wmseScaling = infblk.wmseScaling;
342            cblk.offset = 0;
343            cblk.scanw = cblk.w;
344        }
345
346        // Cache width, height and subband of code-block
347        w = cblk.w;
348        h = cblk.h;
349        sb = cblk.sb;
350
351        if(isReversible(tIdx,c)) { // Reversible only for int data
352            cblk.magbits = g-1+src.getNomRangeBits(c)+sb.anGainExp;
353            shiftBits = 31-cblk.magbits;
354
355            // Update the convertFactor field
356            cblk.convertFactor = (1<<shiftBits);
357
358            // Since we used getNextCodeBlock() to get the int data then
359            // 'offset' is 0 and 'scanw' is the width of the code-block The
360            // input and output arrays are the same (i.e. "in place")
361            for(j=w*h-1; j>=0; j--){
362                tmp = (outarr[j]<<shiftBits);
363                outarr[j] = ((tmp < 0) ? (1<<31)|(-tmp) : tmp);
364            }
365        }
366        else{ // Non-reversible, use step size
367            float baseStep =
368                ((Float)qsss.getTileCompVal(tIdx,c)).floatValue();
369
370            // Calculate magnitude bits and quantization step size
371            if(isDerived(tIdx,c)){
372                cblk.magbits = g-1+sb.level-
373                    (int)Math.floor(Math.log(baseStep)/log2);
374                stepUDR = baseStep/(1<<sb.level);
375            }
376            else{
377                cblk.magbits = g-1-(int)Math.floor(Math.log(baseStep/
378                                        (sb.l2Norm*(1<<sb.anGainExp)))/
379                                                   log2);
380                stepUDR = baseStep/(sb.l2Norm*(1<<sb.anGainExp));
381            }
382            shiftBits = 31-cblk.magbits;
383            // Calculate step that decoder will get and use that one.
384            stepUDR =
385                convertFromExpMantissa(convertToExpMantissa(stepUDR));
386            invstep = 1.0f/((1L<<(src.getNomRangeBits(c)+sb.anGainExp))*
387                            stepUDR);
388            // Normalize to magnitude bits (output fractional point)
389            invstep *= (1<<(shiftBits-src.getFixedPoint(c)));
390
391            // Update convertFactor and stepSize fields
392            cblk.convertFactor = invstep;
393            cblk.stepSize = ((1L<<(src.getNomRangeBits(c)+sb.anGainExp))*
394                             stepUDR);
395
396            if(intq){ // Quantizing int data
397                // Since we used getNextCodeBlock() to get the int data then
398                // 'offset' is 0 and 'scanw' is the width of the code-block
399                // The input and output arrays are the same (i.e. "in place")
400                for (j=w*h-1; j>=0; j--) {
401                    tmp = (int)(outarr[j]*invstep);
402                    outarr[j] = ((tmp < 0) ? (1<<31)|(-tmp) : tmp);
403                }
404            }
405            else { // Quantizing float data
406                for (j=w*h-1, k = infblk.offset+(h-1)*infblk.scanw+w-1,
407                         jmin = w*(h-1); j>=0; jmin -= w) {
408                    for (; j>=jmin; k--, j--) {
409                        tmp = (int)(infarr[k]*invstep);
410                        outarr[j] = ((tmp < 0) ? (1<<31)|(-tmp) : tmp);
411                    }
412                    // Jump to beggining of previous line in input
413                    k -= infblk.scanw - w;
414                }
415            }
416        }
417        // Return the quantized code-block
418        return cblk;
419    }
420
421    /**
422     * Calculates the parameters of the SubbandAn objects that depend on the
423     * Quantizer. The 'stepWMSE' field is calculated for each subband which is
424     * a leaf in the tree rooted at 'sb', for the specified component. The
425     * subband tree 'sb' must be the one for the component 'n'.
426     *
427     * @param sb The root of the subband tree.
428     *
429     * @param c The component index
430     *
431     * @see SubbandAn#stepWMSE
432     * */
433    protected void calcSbParams(SubbandAn sb,int c){
434        float baseStep;
435
436        if(sb.stepWMSE>0f) // parameters already calculated
437            return;
438        if(!sb.isNode){
439            if(isReversible(tIdx,c)){
440                sb.stepWMSE = (float) Math.pow(2,-(src.getNomRangeBits(c)<<1))*
441                    sb.l2Norm*sb.l2Norm;
442            }
443            else{
444                baseStep = ((Float)qsss.getTileCompVal(tIdx,c)).floatValue();
445                if(isDerived(tIdx,c)){
446                    sb.stepWMSE = baseStep*baseStep*
447                        (float)Math.pow(2,(sb.anGainExp-sb.level)<<1)*
448                        sb.l2Norm*sb.l2Norm;
449                }
450                else{
451                    sb.stepWMSE = baseStep*baseStep;
452                }
453            }
454        }
455        else{
456            calcSbParams((SubbandAn)sb.getLL(),c);
457            calcSbParams((SubbandAn)sb.getHL(),c);
458            calcSbParams((SubbandAn)sb.getLH(),c);
459            calcSbParams((SubbandAn)sb.getHH(),c);
460            sb.stepWMSE = 1f; // Signal that we already calculated this branch
461        }
462    }
463
464    /**
465     * Converts the floating point value to its exponent-mantissa
466     * representation. The mantissa occupies the 11 least significant bits
467     * (bits 10-0), and the exponent the previous 5 bits (bits 15-11).
468     *
469     * @param step The quantization step, normalized to a dynamic range of 1.
470     *
471     * @return The exponent mantissa representation of the step.
472     * */
473    public static int convertToExpMantissa(float step) {
474        int exp;
475
476        exp = (int)Math.ceil(-Math.log(step)/log2);
477        if (exp>QSTEP_MAX_EXPONENT) {
478            // If step size is too small for exponent representation, use the
479            // minimum, which is exponent QSTEP_MAX_EXPONENT and mantissa 0.
480            return (QSTEP_MAX_EXPONENT<<QSTEP_MANTISSA_BITS);
481        }
482        // NOTE: this formula does not support more than 5 bits for the
483        // exponent, otherwise (-1<<exp) might overflow (the - is used to be
484        // able to represent 2**31)
485        return (exp<<QSTEP_MANTISSA_BITS) |
486            ((int)((-step*(-1<<exp)-1f)*(1<<QSTEP_MANTISSA_BITS)+0.5f));
487    }
488
489    /**
490     * Converts the exponent-mantissa representation to its floating-point
491     * value. The mantissa occupies the 11 least significant bits (bits 10-0),
492     * and the exponent the previous 5 bits (bits 15-11).
493     *
494     * @param ems The exponent-mantissa representation of the step.
495     *
496     * @return The floating point representation of the step, normalized to a
497     * dynamic range of 1.
498     * */
499    private static float convertFromExpMantissa(int ems) {
500        // NOTE: this formula does not support more than 5 bits for the
501        // exponent, otherwise (-1<<exp) might overflow (the - is used to be
502        // able to represent 2**31)
503        return (-1f-((float)(ems&QSTEP_MAX_MANTISSA)) /
504                ((float)(1<<QSTEP_MANTISSA_BITS))) /
505            (float)(-1<<((ems>>QSTEP_MANTISSA_BITS)&QSTEP_MAX_EXPONENT));
506    }
507
508    /**
509     * Returns the maximum number of magnitude bits in any subband of the
510     * current tile.
511     *
512     * @param c the component number
513     *
514     * @return The maximum number of magnitude bits in all subbands of the
515     * current tile.
516     * */
517    public int getMaxMagBits(int c){
518        Subband sb = getAnSubbandTree(tIdx,c);
519        if(isReversible(tIdx,c)){
520            return getMaxMagBitsRev(sb,c);
521        }
522        else{
523            if(isDerived(tIdx,c)){
524                return getMaxMagBitsDerived(sb,tIdx,c);
525            }
526            else {
527                return getMaxMagBitsExpounded(sb,tIdx,c);
528            }
529        }
530    }
531
532
533    /**
534     * Returns the maximum number of magnitude bits in any subband of the
535     * current tile if reversible quantization is used
536     *
537     * @param sb The root of the subband tree of the current tile
538     *
539     * @param c the component number
540     *
541     * @return The highest number of magnitude bit-planes
542     * */
543    private int getMaxMagBitsRev(Subband sb, int c){
544        int tmp,max=0;
545        int g = ((Integer)gbs.getTileCompVal(tIdx,c)).intValue();
546
547        if(!sb.isNode)
548            return g-1+src.getNomRangeBits(c)+sb.anGainExp;
549
550        max=getMaxMagBitsRev(sb.getLL(),c);
551        tmp=getMaxMagBitsRev(sb.getLH(),c);
552        if(tmp>max)
553            max=tmp;
554        tmp=getMaxMagBitsRev(sb.getHL(),c);
555        if(tmp>max)
556            max=tmp;
557        tmp=getMaxMagBitsRev(sb.getHH(),c);
558        if(tmp>max)
559            max=tmp;
560
561        return max;
562    }
563
564    /**
565     * Returns the maximum number of magnitude bits in any subband in the
566     * given tile-component if derived quantization is used
567     *
568     * @param sb The root of the subband tree of the tile-component
569     *
570     * @param t Tile index
571     *
572     * @param c Component index
573     *
574     * @return The highest number of magnitude bit-planes
575     * */
576    private int getMaxMagBitsDerived(Subband sb,int t,int c){
577        int tmp,max=0;
578        int g = ((Integer)gbs.getTileCompVal(t,c)).intValue();
579
580        if(!sb.isNode){
581            float baseStep = ((Float)qsss.getTileCompVal(t,c)).floatValue();
582            return g-1+sb.level-(int)Math.floor(Math.log(baseStep)/log2);
583        }
584
585        max=getMaxMagBitsDerived(sb.getLL(),t,c);
586        tmp=getMaxMagBitsDerived(sb.getLH(),t,c);
587        if(tmp>max)
588            max=tmp;
589        tmp=getMaxMagBitsDerived(sb.getHL(),t,c);
590        if(tmp>max)
591            max=tmp;
592        tmp=getMaxMagBitsDerived(sb.getHH(),t,c);
593        if(tmp>max)
594            max=tmp;
595
596        return max;
597    }
598
599
600    /**
601     * Returns the maximum number of magnitude bits in any subband in the
602     * given tile-component if expounded quantization is used
603     *
604     * @param sb The root of the subband tree of the tile-component
605     *
606     * @param t Tile index
607     *
608     * @param c Component index
609     *
610     * @return The highest number of magnitude bit-planes
611     * */
612    private int getMaxMagBitsExpounded(Subband sb,int t,int c){
613        int tmp,max=0;
614        int g = ((Integer)gbs.getTileCompVal(t,c)).intValue();
615
616        if(!sb.isNode){
617            float baseStep = ((Float)qsss.getTileCompVal(t,c)).floatValue();
618            return g-1-
619                (int)Math.floor(Math.log(baseStep/
620                                (((SubbandAn)sb).l2Norm*(1<<sb.anGainExp)))/
621                                log2);
622        }
623
624        max=getMaxMagBitsExpounded(sb.getLL(),t,c);
625        tmp=getMaxMagBitsExpounded(sb.getLH(),t,c);
626        if(tmp>max)
627            max=tmp;
628        tmp=getMaxMagBitsExpounded(sb.getHL(),t,c);
629        if(tmp>max)
630            max=tmp;
631        tmp=getMaxMagBitsExpounded(sb.getHH(),t,c);
632        if(tmp>max)
633            max=tmp;
634
635        return max;
636    }
637}