/* tonyslide.c */

/****************************************************************************
 *
 *  Copyright (C) 2000-2001 Eli-Jean R. Leyssens, aka Pervect of Topix
 *
 *  This file is part of the CodePressor package.
 *
 *  CodePressor is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  CodePressor is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CodePressor; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 *  Eli-Jean R. Leyssens can be reached via email at eli@dnd.utwente.nl
 *  snail: E-J.R. Leyssens, Schivelbeinerstr. 5, 10439 Berlin, Germany
 *
 ***************************************************************************/

#include <oslib/types.h>
#include <stdio.h>

#include "nibslide.h"
#include "tonyslide.h"

void DoTheHainesSlide( byte * pbMemory, int Size, int ExtraSize, int SourceDirection, TNibSlide * ptResult, int ResultDirection, unsigned int usMaxDistance, bool (*CompareFunction)(byte,byte,unsigned short,byte,byte,unsigned short)) {
  // Depack code + Input data at pbMemory
  // if SourceDirection > 0 then
  //    depack code . input data
  // else
  //    input data . depack code
  // endif

    bits * pdwStop;
    bits * pdwHere;
    bits * pdwLookAt;
    bits Here;
    bits LookAt;
    byte bMatch;
    byte bBestMatch;
    byte bMask;
    byte bBestMask;
    unsigned int usDistance;
    unsigned int usBestDistance;
    int Shift;

    if ( SourceDirection > 0) {
      // depack code . input data
      pdwStop = (bits *) ( pbMemory);
      pdwHere = (bits *) ( pbMemory + ExtraSize);
    } else {
      // input data . depack code
      pdwStop = (bits *) ( pbMemory + Size + ExtraSize - 4);
      pdwHere = (bits *) ( pbMemory + Size - 4);
    }

    if ( ResultDirection < 0) {
      ptResult += (Size / 4) - 1;
    }

    while ( Size > 0) {
      Here = *pdwHere;
      pdwLookAt = pdwHere;

      bBestMatch = 0;
      bBestMask = 0xff;
      usBestDistance = 0;

      for ( usDistance = 0 ; ( usDistance < usMaxDistance) && ( pdwLookAt != pdwStop) ; usDistance++) {
        pdwLookAt -= SourceDirection;
        LookAt = Here ^ *pdwLookAt;
        bMask = 0;
        bMatch = 0;
        for ( Shift = 0 ; Shift < 8 ; Shift++) {
          if ( (( LookAt >> ( Shift * 4)) & 0xf) != 0) {
            bMask = bMask | ( 1 << Shift);
          } else {
            bMatch++;
          }
        }
        if ( CompareFunction( bBestMatch, bBestMask, usBestDistance, bMatch, bMask, usDistance)) {
          bBestMatch = bMatch;
          bBestMask = bMask;
          usBestDistance = usDistance;
        }
      }
      ptResult->bMask = bBestMask;
      ptResult->bMatch = bBestMatch;
      ptResult->usDistance = usBestDistance;

      pdwHere += SourceDirection;
      ptResult += ResultDirection;
      Size -= 4;
    }
}
