jdk/src/java.desktop/share/native/libmlib_image/mlib_ImageColorTrue2Index.c
author prr
Thu, 12 May 2016 11:03:07 -0700
changeset 38414 3e22d8fd4912
parent 25859 3317bb8137f4
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 8939
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * FUNCTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *      mlib_ImageColorTrue2Index - convert a true color image to an indexed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *                                  color image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * SYNOPSIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *      mlib_status mlib_ImageColorTrue2Index(mlib_image       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *                                            const mlib_image *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *                                            const void       *colormap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * ARGUMENTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *      colormap  Internal data structure for inverse color mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *      dst       Pointer to destination image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *      src       Pointer to source image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * DESCRIPTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      Convert a true color image to a pseudo color image with the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      of finding the nearest matched lut entry for each pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      The src can be an MLIB_BYTE or MLIB_SHORT image with 3 or 4 channels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      The dst must be a 1-channel MLIB_BYTE or MLIB_SHORT image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      The lut might have either 3 or 4 channels. The type of the lut can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *              MLIB_BYTE in, MLIB_BYTE out (i.e., BYTE-to-BYTE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *              MLIB_BYTE in, MLIB_SHORT out (i.e., BYTE-to-SHORT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *              MLIB_SHORT in, MLIB_SHORT out (i.e., SHORT-to-SHORT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *              MLIB_SHORT in, MLIB_BYTE out (i.e., SHORT-to-BYTE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *      The src image and the lut must have same number of channels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#include "mlib_image.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#include "mlib_ImageColormap.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
#include "mlib_ImageCheck.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
/*#define USE_VIS_CODE*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#ifdef USE_VIS_CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
#include "vis_proto.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
#define VIS_ALIGNADDR(X, Y)  vis_alignaddr((void *)(X), (Y))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#define LUT_BYTE_COLORS_3CHANNELS  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#define LUT_BYTE_COLORS_4CHANNELS  3000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
#define LUT_SHORT_COLORS_3CHANNELS 1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
#define LUT_SHORT_COLORS_4CHANNELS 1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
#define MAIN_COLORTRUE2INDEX_LOOP( FROM_TYPE, TO_TYPE, NCHANNELS )       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  for( y = 0; y < height; y++ )                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
  {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    mlib_ImageColorTrue2IndexLine_##FROM_TYPE##_##TO_TYPE##_##NCHANNELS( \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
      sdata, ddata, width, colormap );                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    sdata += sstride;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    ddata += dstride;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
#define COLOR_CUBE_U8_3_SEARCH( TABLE_POINTER_TYPE, SHIFT, STEP ) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
{                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  const mlib_u8 *c0, *c1, *c2;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
  TABLE_POINTER_TYPE *table = s->table;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  mlib_s32 bits = s->bits;                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
  mlib_s32 nbits = 8 - bits;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
  mlib_s32 mask = ~( ( 1 << nbits ) - 1 );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
  mlib_s32 j;                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  c0 = src + SHIFT;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  c1 = src + 1 + SHIFT;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  c2 = src + 2 + SHIFT;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
  switch( bits )                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
  {                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    case 1:                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    case 2:                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      mlib_s32 bits0 = 8 - bits;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
      mlib_s32 bits1 = bits0 - bits;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
      mlib_s32 bits2 = bits1 - bits;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
      for( j = 0; j < length; j++ )                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        dst[ j ] = table[ ( ( *c0 & mask ) >> bits2 ) |           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
          ( ( *c1 & mask ) >> bits1 ) |                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
          ( ( *c2 & mask ) >> bits0 ) ];                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        c0 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        c1 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        c2 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
      break;                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    case 3:                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
      for( j = 0; j < length; j++ )                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
      {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        dst[ j ] = table[ ( ( *c0 & mask ) << 1 ) |               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
          ( ( *c1 & mask ) >> 2 ) |                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
          ( ( *c2 & mask ) >> 5 ) ];                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        c0 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        c1 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        c2 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
      }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
      break;                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    case 4:                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
      for( j = 0; j < length; j++ )                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
      {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        dst[ j ] = table[ ( ( *c0 & mask ) << 4 ) |               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
          ( *c1 & mask ) |                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
          ( ( *c2 & mask ) >> 4 ) ];                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        c0 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        c1 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        c2 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
      }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
      break;                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    case 5:                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    case 6:                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    case 7:                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
      mlib_s32 bits0 = 8 - bits;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
      mlib_s32 bits1 = bits * 2 - 8;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
      mlib_s32 bits2 = bits1 + bits;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
      for( j = 0; j < length; j++ )                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
      {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        dst[ j ] = table[ ( ( *c0 & mask ) << bits2 ) |           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
          ( ( *c1 & mask ) << bits1 ) |                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
          ( ( *c2 & mask ) >> bits0 ) ];                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        c0 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        c1 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        c2 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
      }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
      break;                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    case 8:                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
      for( j = 0; j < length; j++ )                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
      {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        dst[ j ] = table[ ( ( *c0 & mask ) << 16 ) |              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
          ( ( *c1 & mask ) << 8 ) |                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
          ( *c2 & mask ) ];                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        c0 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        c1 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        c2 += STEP;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
      }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
      break;                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
  }                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
#define COLOR_CUBE_U8_4_SEARCH( TABLE_TYPE )                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
{                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
  const mlib_u8 *c0, *c1, *c2, *c3;                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
  TABLE_TYPE *table = s->table;                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
  mlib_s32 bits = s->bits;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
  mlib_s32 nbits = 8 - bits;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
  mlib_s32 mask = ~( ( 1 << nbits ) - 1 );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
  mlib_s32 j;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
  c0 = src;                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
  c1 = src + 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
  c2 = src + 2;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
  c3 = src + 3;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
  switch( bits )                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
  {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    case 1:                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
      for( j = 0; j < length; j++ )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
      {                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        dst[ j ] = table[ ( ( *c0 & mask ) >> 4 ) |             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
          ( ( *c1 & mask ) >> 5 ) |                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
          ( ( *c2 & mask ) >> 6 ) |                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
          ( ( *c3 & mask ) >> 7 ) ];                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        c0 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        c1 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        c2 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        c3 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
      }                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
      break;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    case 2:                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      for( j = 0; j < length; j++ )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
      {                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        dst[ j ] = table[ ( *c0 & mask ) |                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
          ( ( *c1 & mask ) >> 2 ) |                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
          ( ( *c2 & mask ) >> 4 ) |                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
          ( ( *c3 & mask ) >> 6 ) ];                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        c0 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        c1 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        c2 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        c3 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
          }                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
      break;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    case 3:                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
      for( j = 0; j < length; j++ )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
      {                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        dst[ j ] = table[ ( ( *c0 & mask ) << 4 ) |             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
          ( ( *c1 & mask ) << 1 ) |                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
          ( ( *c2 & mask ) >> 2 ) |                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
          ( ( *c3 & mask ) >> 5 ) ];                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        c0 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        c1 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        c2 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        c3 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
      }                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
      break;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    case 4:                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
      for( j = 0; j < length; j++ )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
      {                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        dst[ j ] = table[ ( ( *c0 & mask ) << 8 ) |             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
          ( ( *c1 & mask ) << 4 ) |                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
          ( *c2 & mask ) |                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
          ( ( *c3 & mask ) >> 4 ) ];                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        c0 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        c1 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        c2 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        c3 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
      }                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
      break;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    case 5:                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    case 6:                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
      mlib_s32 bits3 = bits * 4 - 8;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
      mlib_s32 bits2 = bits3 - bits;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
      mlib_s32 bits1 = bits2 - bits;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
      mlib_s32 bits0 = 8 - bits;                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
      for( j = 0; j < length; j++ )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
      {                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        dst[ j ] = table[ ( ( *c0 & mask ) << bits3 ) |         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
          ( ( *c1 & mask ) << bits2 ) |                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
          ( ( *c2 & mask ) << bits1 ) |                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
          ( ( *c3 & mask ) >> bits0 ) ];                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        c0 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        c1 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        c2 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        c3 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
      }                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
      break;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    case 7:                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
      for( j = 0; j < length; j++ )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
      {                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        dst[ j ] = table[ ( ( *c0 & mask ) << 20 ) |            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
          ( ( *c1 & mask ) << 13 ) |                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
          ( ( *c2 & mask ) << 6 ) |                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
          ( ( *c3 & mask ) >> 1 ) ];                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        c0 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        c1 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        c2 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        c3 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
      }                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
      break;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    case 8: /* will never be called */                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
      for( j = 0; j < length; j++ )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
      {                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        dst[ j ] = table[ ( ( *c0 & mask ) << 24 ) |            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
          ( ( *c1 & mask ) << 16 ) |                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
          ( ( *c2 & mask ) << 8 ) |                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
          ( *c3 & mask ) ];                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        c0 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        c1 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        c2 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        c3 += 4;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
      }                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
      break;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
  }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
#define COLOR_CUBE_S16_3_SEARCH( TABLE_TYPE, SHIFT, STEP )                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
{                                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
  const mlib_s16 *c0, *c1, *c2;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
  mlib_s32 bits = s->bits;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
  mlib_s32 nbits = 16 - bits;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
  mlib_s32 mask = ~( ( 1 << nbits ) - 1 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
  TABLE_TYPE *table = s->table;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
  mlib_s32 j;                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
  c0 = src + SHIFT;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
  c1 = src + 1 + SHIFT;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
  c2 = src + 2 + SHIFT;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
  switch( bits )                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
  {                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    case 1:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    case 2:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    case 3:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    case 4:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    case 5:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
      mlib_s32 bits0 = 16 - bits;                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
      mlib_s32 bits1 = bits0 - bits;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
      mlib_s32 bits2 = bits1 - bits;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
      for( j = 0; j < length; j++ )                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
      {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        dst[ j ] = table[ ( ( ( *c0 - MLIB_S16_MIN ) & mask ) >> bits2 ) | \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
          ( ( ( *c1 - MLIB_S16_MIN ) & mask ) >> bits1 ) |                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
          ( ( ( *c2 - MLIB_S16_MIN ) & mask ) >> bits0 ) ];                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        c0 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        c1 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        c2 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
      }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
      break;                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    case 6:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    case 7:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
      mlib_s32 bits0 = 16 - bits;                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
      mlib_s32 bits1 = bits0 - bits;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
      mlib_s32 bits2 = bits * 3 - 16;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
      for( j = 0; j < length; j++ )                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
      {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        dst[ j ] = table[ ( ( ( *c0 - MLIB_S16_MIN ) & mask ) << bits2 ) | \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
          ( ( ( *c1 - MLIB_S16_MIN ) & mask ) >> bits1 ) |                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
          ( ( ( *c2 - MLIB_S16_MIN ) & mask ) >> bits0 ) ];                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        c0 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        c1 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        c2 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
      }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
      break;                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    case 8:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
      for( j = 0; j < length; j++ )                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
      {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        dst[ j ] = table[ ( ( ( *c0 - MLIB_S16_MIN ) & mask ) << 8 ) |     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
          ( ( *c1 - MLIB_S16_MIN ) & mask ) |                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
          ( ( ( *c2 - MLIB_S16_MIN ) & mask ) >> 8 ) ];                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        c0 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        c1 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        c2 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
      }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
      break;                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    case 9:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    case 10:                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
      mlib_s32 bits0 = 16 - bits;                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
      mlib_s32 bits1 = 2 * bits - 16;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
      mlib_s32 bits2 = bits1 + bits;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
      for( j = 0; j < length; j++ )                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
      {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        dst[ j ] = table[ ( ( ( *c0 - MLIB_S16_MIN ) & mask ) << bits2 ) | \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
          ( ( ( *c1 - MLIB_S16_MIN ) & mask ) << bits1 ) |                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
          ( ( ( *c2 - MLIB_S16_MIN ) & mask ) >> bits0 ) ];                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        c0 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        c1 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        c2 += STEP;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
      }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
      break;                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /* Other cases may not be considered as the table size will be more    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
       than 2^32 */                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
  }                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
#define COLOR_CUBE_S16_4_SEARCH( TABLE_TYPE )                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
{                                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
  const mlib_s16 *c0, *c1, *c2, *c3;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
  TABLE_TYPE *table = s->table;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
  mlib_s32 bits = s->bits;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
  mlib_s32 nbits = 16 - bits;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
  mlib_s32 mask = ~( ( 1 << nbits ) - 1 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
  mlib_s32 j;                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
  c0 = src;                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
  c1 = src + 1;                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
  c2 = src + 2;                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
  c3 = src + 3;                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
  switch( bits )                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
  {                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    case 1:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    case 2:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    case 3:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
      mlib_s32 bits0 = 16 - bits;                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
      mlib_s32 bits1 = bits0 - bits;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
      mlib_s32 bits2 = bits1 - bits;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
      mlib_s32 bits3 = bits2 - bits;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
      for( j = 0; j < length; j++ )                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
      {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        dst[ j ] = table[ ( ( ( *c0 - MLIB_S16_MIN ) & mask ) >> bits3 ) | \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
          ( ( ( *c1 - MLIB_S16_MIN ) & mask ) >> bits2 ) |                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
          ( ( ( *c2 - MLIB_S16_MIN ) & mask ) >> bits1 ) |                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
          ( ( ( *c3 - MLIB_S16_MIN ) & mask ) >> bits0 ) ];                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        c0 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        c1 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        c2 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        c3 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
      }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
      break;                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    case 4:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
      for( j = 0; j < length; j++ )                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
      {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        dst[ j ] = table[ ( ( *c0 - MLIB_S16_MIN ) & mask ) |              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
          ( ( ( *c1 - MLIB_S16_MIN ) & mask ) >> 4 ) |                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
          ( ( ( *c2 - MLIB_S16_MIN ) & mask ) >> 8 ) |                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
          ( ( ( *c3 - MLIB_S16_MIN ) & mask ) >> 12 ) ];                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        c0 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        c1 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        c2 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        c3 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
      }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
      break;                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    case 5:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
      for( j = 0; j < length; j++ )                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
      {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        dst[ j ] = table[ ( ( ( *c0 - MLIB_S16_MIN ) & mask ) << 4 ) |     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
          ( ( ( *c1 - MLIB_S16_MIN ) & mask ) >> 1 ) |                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
          ( ( ( *c2 - MLIB_S16_MIN ) & mask ) >> 6 ) |                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
          ( ( ( *c3 - MLIB_S16_MIN ) & mask ) >> 11 ) ];                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        c0 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        c1 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        c2 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        c3 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
      }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
      break;                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    case 6:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    case 7:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
      mlib_s32 bits0 = 16 - bits;                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
      mlib_s32 bits1 = bits0 - bits;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
      mlib_s32 bits3 = bits * 4 - 16;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
      mlib_s32 bits2 = bits3 - bits;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
      for( j = 0; j < length; j++ )                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
      {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        dst[ j ] = table[ ( ( ( *c0 - MLIB_S16_MIN ) & mask ) << bits3 ) | \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
          ( ( ( *c1 - MLIB_S16_MIN ) & mask ) << bits2 ) |                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
          ( ( ( *c2 - MLIB_S16_MIN ) & mask ) >> bits1 ) |                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
          ( ( ( *c3 - MLIB_S16_MIN ) & mask ) >> bits0 ) ];                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        c0 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        c1 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        c2 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        c3 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
      }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
      break;                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    case 8:                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
      for( j = 0; j < length; j++ )                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
      {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        dst[ j ] = table[ ( ( ( *c0 - MLIB_S16_MIN ) & mask ) << 16 ) |    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
          ( ( ( *c1 - MLIB_S16_MIN ) & mask ) << 8 ) |                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
          ( ( *c2 - MLIB_S16_MIN ) & mask ) |                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
          ( ( ( *c3 - MLIB_S16_MIN ) & mask ) >> 8 ) ];                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        c0 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        c1 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        c2 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        c3 += 4;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
      }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
      break;                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /* Other cases may not be considered as the table size will be more    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
       than 2^32 */                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
  }                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
#define BINARY_TREE_SEARCH_RIGHT( POSITION, COLOR_MAX, SHIFT )  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
{                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
  if( ( distance >= ( ( ( position[ POSITION ] + current_size - \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    c[ POSITION ] ) * ( position[ POSITION ] + current_size -   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    c[ POSITION ] ) ) >> SHIFT ) ) &&                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    ( position[ POSITION ] + current_size != COLOR_MAX ) )      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    continue_up = 1;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
#define BINARY_TREE_EXPLORE_RIGHT_3( POSITION, COLOR_MAX, IMAGE_TYPE,    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
  FIRST_NEIBOUR, SECOND_NEIBOUR, SUBSTRACTION, SHIFT )                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
{                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
  if( distance >= ( ( ( position[ POSITION ] + current_size -            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    c[ POSITION ] ) * ( position[ POSITION ] +                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
      current_size - c[ POSITION ] ) ) >> SHIFT ) )                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
  {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    if( distance < ( ( ( COLOR_MAX - c[ POSITION ] ) *                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
      ( COLOR_MAX - c[ POSITION ] ) ) >> SHIFT ) )                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
      if( distance < ( ( ( position[ POSITION ] +                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        current_size * 2 - c[ POSITION ] ) *                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        ( position[ POSITION ] + current_size * 2 -                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
          c[ POSITION ] ) ) >> SHIFT ) )                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        /* Check only a part of quadrant */                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        check_neibours[ FIRST_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        check_neibours[ SECOND_NEIBOUR ] += 1;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        check_corner += 1;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
          mlib_u32 newdistance = FIND_DISTANCE_3( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            p[ 2 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
          /* Only a part of quadrant needs checking */                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
          distance =                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            mlib_search_quadrant_part_to_left_##IMAGE_TYPE##_3(          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
              node->contents.quadrants[ qq ],                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
              distance, &found_color, c, p,                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
              position[ POSITION ] + current_size, pass - 1, POSITION ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
      else /* Check whole quadrant */                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        check_neibours[ FIRST_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        check_neibours[ SECOND_NEIBOUR ] += 2;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        check_corner += 2;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        continue_up = 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
          mlib_u32 newdistance = FIND_DISTANCE_3( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            p[ 2 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
          /* Here is a full node. Just explore it */                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
          distance = mlib_search_quadrant_##IMAGE_TYPE##_3(              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            node->contents.quadrants[ qq ],                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            distance, &found_color, c[ 0 ], c[ 1 ], c[ 2 ], p );         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    else /* Cell is on the edge of the space */                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
      if( position[ POSITION ] + current_size * 2 ==                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        COLOR_MAX )                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        /* Check only a part of quadrant */                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        check_neibours[ FIRST_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        check_neibours[ SECOND_NEIBOUR ] += 1;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        check_corner += 1;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
          mlib_u32 newdistance = FIND_DISTANCE_3( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            p[ 2 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
          /* Only a part of quadrant needs checking */                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
          distance =                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            mlib_search_quadrant_part_to_left_##IMAGE_TYPE##_3(          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
              node->contents.quadrants[ qq ],                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
              distance, &found_color, c, p,                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
              position[ POSITION ] + current_size,                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
              pass - 1, POSITION );                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
      else /* Check whole quadrant */                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        check_neibours[ FIRST_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        check_neibours[ SECOND_NEIBOUR ] += 2;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        check_corner += 2;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        continue_up = 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
          mlib_u32 newdistance = FIND_DISTANCE_3( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            p[ 2 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
          /* Here is a full node. Just explore it */                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
          distance = mlib_search_quadrant_##IMAGE_TYPE##_3(              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            node->contents.quadrants[ qq ],                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            distance, &found_color, c[ 0 ], c[ 1 ], c[ 2 ], p );         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
  }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
#define BINARY_TREE_EXPLORE_RIGHT_4( POSITION, COLOR_MAX, IMAGE_TYPE,    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
  FIRST_NEIBOUR, SECOND_NEIBOUR, THIRD_NEIBOUR, SUBSTRACTION, SHIFT )    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
{                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
  if( distance >= ( ( ( position[ POSITION ] + current_size -            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    c[ POSITION ] ) * ( position[ POSITION ] +                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
      current_size - c[ POSITION ] ) ) >> SHIFT ) )                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
  {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    if( distance < ( ( ( COLOR_MAX - c[ POSITION ] ) *                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
      ( COLOR_MAX - c[ POSITION ] ) ) >> SHIFT ) )                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
      if( distance < ( ( ( position[ POSITION ] +                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        current_size * 2 - c[ POSITION ] ) *                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        ( position[ POSITION ] + current_size * 2 -                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
          c[ POSITION ] ) ) >> SHIFT ) )                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        /* Check only a part of quadrant */                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        check_neibours[ FIRST_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        check_neibours[ SECOND_NEIBOUR ] += 1;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        check_neibours[ THIRD_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
          mlib_u32 newdistance = FIND_DISTANCE_4( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            p[ 2 ][ new_found_color ] - SUBSTRACTION, c[ 3 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            p[ 3 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
          /* Only a part of quadrant needs checking */                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
          distance =                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            mlib_search_quadrant_part_to_left_##IMAGE_TYPE##_4(          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
              node->contents.quadrants[ qq ],                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
              distance, &found_color, c, p,                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
              position[ POSITION ] + current_size, pass - 1, POSITION ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
      else /* Check whole quadrant */                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        check_neibours[ FIRST_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        check_neibours[ SECOND_NEIBOUR ] += 2;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        check_neibours[ THIRD_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        continue_up = 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
          mlib_u32 newdistance = FIND_DISTANCE_4( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            p[ 2 ][ new_found_color ] - SUBSTRACTION, c[ 3 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            p[ 3 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
          /* Here is a full node. Just explore it */                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
          distance = mlib_search_quadrant_##IMAGE_TYPE##_4(              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            node->contents.quadrants[ qq ],                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            distance, &found_color, c[ 0 ], c[ 1 ], c[ 2 ], c[ 3 ], p ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    else /* Cell is on the edge of the space */                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
      if( position[ POSITION ] + current_size * 2 ==                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        COLOR_MAX )                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        /* Check only a part of quadrant */                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        check_neibours[ FIRST_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        check_neibours[ SECOND_NEIBOUR ] += 1;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        check_neibours[ THIRD_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
          mlib_u32 newdistance = FIND_DISTANCE_4( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            p[ 2 ][ new_found_color ] - SUBSTRACTION, c[ 3 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            p[ 3 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
          /* Only a part of quadrant needs checking */                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
          distance =                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            mlib_search_quadrant_part_to_left_##IMAGE_TYPE##_4(          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
              node->contents.quadrants[ qq ],                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
              distance, &found_color, c, p,                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
              position[ POSITION ] + current_size,                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
              pass - 1, POSITION );                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
      else /* Check whole quadrant */                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        check_neibours[ FIRST_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        check_neibours[ SECOND_NEIBOUR ] += 2;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        check_neibours[ THIRD_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        continue_up = 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
          mlib_u32 newdistance = FIND_DISTANCE_4( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            p[ 2 ][ new_found_color ] - SUBSTRACTION, c[ 3 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            p[ 3 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
          /* Here is a full node. Just explore it */                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
          distance = mlib_search_quadrant_##IMAGE_TYPE##_4(              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            node->contents.quadrants[ qq ],                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            distance, &found_color, c[ 0 ], c[ 1 ], c[ 2 ], c[ 3 ], p ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
  }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
#define BINARY_TREE_SEARCH_LEFT( POSITION, SHIFT )                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
{                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
  if( ( distance > ( ( ( position[ POSITION ] - c[ POSITION ] ) * \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    ( position[ POSITION ] - c[ POSITION ] ) ) >> SHIFT ) )  &&   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    position[ POSITION ] )                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    continue_up = 1;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
#define BINARY_TREE_EXPLORE_LEFT_3( POSITION, IMAGE_TYPE,                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
  FIRST_NEIBOUR, SECOND_NEIBOUR, SUBSTRACTION, SHIFT )                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
{                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
  if( distance >                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    ( ( ( c[ POSITION ] - position[ POSITION ] ) *                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    ( c[ POSITION ] - position[ POSITION ] ) ) >> SHIFT ) )              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
  {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    if( distance <= ( ( c[ POSITION ] * c[ POSITION ] ) >> SHIFT ) )     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
      if( distance <= ( ( ( c[ POSITION ] + current_size -               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        position[ POSITION ] ) *                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        ( c[ POSITION ] + current_size -                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
          position[ POSITION ] ) ) >> SHIFT ) )                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        check_neibours[ FIRST_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        check_neibours[ SECOND_NEIBOUR ] += 1;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        check_corner += 1;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
          mlib_u32 newdistance = FIND_DISTANCE_3( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            p[ 2 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
          /* Only a part of quadrant needs checking */                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
          distance =                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            mlib_search_quadrant_part_to_right_##IMAGE_TYPE##_3(         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
              node->contents.quadrants[ qq ],                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
              distance, &found_color, c, p,                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
              position[ POSITION ] - current_size, pass - 1, POSITION ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
      else /* Check whole quadrant */                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        check_neibours[ FIRST_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        check_neibours[ SECOND_NEIBOUR ] += 2;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        check_corner += 2;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        continue_up = 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
          mlib_u32 newdistance = FIND_DISTANCE_3( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            p[ 2 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
          /* Here is a full node. Just explore it */                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
          distance = mlib_search_quadrant_##IMAGE_TYPE##_3(              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            node->contents.quadrants[ qq ],                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            distance, &found_color, c[ 0 ], c[ 1 ], c[ 2 ], p );         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    else                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
      if( !( position[ POSITION ] - current_size ) )                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        check_neibours[ FIRST_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        check_neibours[ SECOND_NEIBOUR ] += 1;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        check_corner += 1;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
          mlib_u32 newdistance = FIND_DISTANCE_3( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            p[ 2 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
          /* Only a part of quadrant needs checking */                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
          distance =                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            mlib_search_quadrant_part_to_right_##IMAGE_TYPE##_3(         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
              node->contents.quadrants[ qq ],                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
              distance, &found_color, c, p,                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
              position[ POSITION ] - current_size, pass - 1, POSITION ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
      else                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        check_neibours[ FIRST_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        check_neibours[ SECOND_NEIBOUR ] += 2;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        check_corner += 2;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        continue_up = 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
          mlib_u32 newdistance = FIND_DISTANCE_3( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            p[ 2 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
          /* Here is a full node. Just explore it */                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
          distance = mlib_search_quadrant_##IMAGE_TYPE##_3(              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            node->contents.quadrants[ qq ],                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            distance, &found_color, c[ 0 ], c[ 1 ], c[ 2 ], p );         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
  }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
#define BINARY_TREE_EXPLORE_LEFT_4( POSITION, IMAGE_TYPE,                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
  FIRST_NEIBOUR, SECOND_NEIBOUR, THIRD_NEIBOUR, SUBSTRACTION, SHIFT )    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
{                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
  if( distance >                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    ( ( ( c[ POSITION ] - position[ POSITION ] ) *                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    ( c[ POSITION ] - position[ POSITION ] ) ) >> SHIFT ) )              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
  {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    if( distance <= ( ( c[ POSITION ] * c[ POSITION ] ) >> SHIFT ) )     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
      if( distance <= ( ( ( c[ POSITION ] + current_size -               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        position[ POSITION ] ) *                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        ( c[ POSITION ] + current_size -                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
          position[ POSITION ] ) ) >> SHIFT ) )                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        check_neibours[ FIRST_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        check_neibours[ SECOND_NEIBOUR ] += 1;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        check_neibours[ THIRD_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
          mlib_u32 newdistance = FIND_DISTANCE_4( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            p[ 2 ][ new_found_color ] - SUBSTRACTION, c[ 3 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            p[ 3 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
          /* Only a part of quadrant needs checking */                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
          distance =                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            mlib_search_quadrant_part_to_right_##IMAGE_TYPE##_4(         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
              node->contents.quadrants[ qq ],                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
              distance, &found_color, c, p,                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
              position[ POSITION ] - current_size, pass - 1, POSITION ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
      else /* Check whole quadrant */                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        check_neibours[ FIRST_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        check_neibours[ SECOND_NEIBOUR ] += 2;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        check_neibours[ THIRD_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        continue_up = 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
          mlib_u32 newdistance = FIND_DISTANCE_4( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            p[ 2 ][ new_found_color ] - SUBSTRACTION, c[ 3 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            p[ 3 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
          /* Here is a full node. Just explore it */                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
          distance = mlib_search_quadrant_##IMAGE_TYPE##_4(              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            node->contents.quadrants[ qq ],                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            distance, &found_color, c[ 0 ], c[ 1 ], c[ 2 ], c[ 3 ], p ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    else                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
      if( !( position[ POSITION ] - current_size ) )                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        check_neibours[ FIRST_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        check_neibours[ SECOND_NEIBOUR ] += 1;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        check_neibours[ THIRD_NEIBOUR ] += 1;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
          mlib_u32 newdistance = FIND_DISTANCE_4( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            p[ 2 ][ new_found_color ] - SUBSTRACTION, c[ 3 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            p[ 3 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
          /* Only a part of quadrant needs checking */                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
          distance =                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            mlib_search_quadrant_part_to_right_##IMAGE_TYPE##_4(         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
              node->contents.quadrants[ qq ],                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
              distance, &found_color, c, p,                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
              position[ POSITION ] - current_size, pass - 1, POSITION ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
      else                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
      {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        mlib_s32 qq = q ^ ( 1 << POSITION );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        check_neibours[ FIRST_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        check_neibours[ SECOND_NEIBOUR ] += 2;                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        check_neibours[ THIRD_NEIBOUR ] += 2;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        continue_up = 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        if( node->tag & ( 1 << qq ) )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
          /* Here is another color cell.                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
             Check the distance */                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
          mlib_s32 new_found_color =                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            node->contents.index[ qq ];                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
          mlib_u32 newdistance = FIND_DISTANCE_4( c[ 0 ],                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            p[ 0 ][ new_found_color ] - SUBSTRACTION, c[ 1 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            p[ 1 ][ new_found_color ] - SUBSTRACTION, c[ 2 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            p[ 2 ][ new_found_color ] - SUBSTRACTION, c[ 3 ],            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            p[ 3 ][ new_found_color ] - SUBSTRACTION, SHIFT );           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
          if( newdistance < distance )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
          {                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            found_color = new_found_color;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            distance = newdistance;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
          }                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        else if( node->contents.quadrants[ qq ] )                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
          /* Here is a full node. Just explore it */                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
          distance = mlib_search_quadrant_##IMAGE_TYPE##_4(              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            node->contents.quadrants[ qq ],                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            distance, &found_color, c[ 0 ], c[ 1 ], c[ 2 ], c[ 3 ], p ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
      }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
  }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
#define CHECK_QUADRANT_U8_3( qq )                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
{                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
  if( node->tag & ( 1 << qq ) )                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
  {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /* Here is another color cell. Check the distance */        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    mlib_s32 new_found_color = node->contents.index[ qq ];      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    mlib_u32 newdistance = FIND_DISTANCE_3( c[ 0 ],             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
      p[ 0 ][ new_found_color ], c[ 1 ],                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
      p[ 1 ][ new_found_color ], c[ 2 ],                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
      p[ 2 ][ new_found_color ], 0 );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    if( newdistance < distance )                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
      found_color = new_found_color;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
      distance = newdistance;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
  }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
  else if( node->contents.quadrants[ qq ] )                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    /* Here is a full node. Just explore it all */              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    distance = mlib_search_quadrant_U8_3(                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
      node->contents.quadrants[ qq ], distance, &found_color,   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
      c[ 0 ], c[ 1 ], c[ 2 ], p );                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
/* Else there is just an empty cell */                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
#define CHECK_QUADRANT_S16_3( qq )                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
{                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
  if( node->tag & ( 1 << qq ) )                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
  {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    /* Here is another color cell. Check the distance */        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    mlib_s32 new_found_color = node->contents.index[ qq ];      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    mlib_u32 palc0, palc1, palc2, newdistance;                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    palc0 = p[ 0 ][ new_found_color ] - MLIB_S16_MIN;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    palc1 = p[ 1 ][ new_found_color ] - MLIB_S16_MIN;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    palc2 = p[ 2 ][ new_found_color ] - MLIB_S16_MIN;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    newdistance = FIND_DISTANCE_3( c[ 0 ], palc0,               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
      c[ 1 ], palc1,                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
      c[ 2 ], palc2, 2 );                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    if( newdistance < distance )                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
      found_color = new_found_color;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
      distance = newdistance;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
  }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
  else if( node->contents.quadrants[ qq ] )                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    /* Here is a full node. Just explore it all */              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    distance = mlib_search_quadrant_S16_3(                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
      node->contents.quadrants[ qq ], distance, &found_color,   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
      c[ 0 ], c[ 1 ], c[ 2 ], p );                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
/* Else there is just an empty cell */                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
#define BINARY_TREE_SEARCH_3( SOURCE_IMAGE, POINTER_TYPE, BITS,              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
  COLOR_MAX, SUBTRACTION, POINTER_SHIFT, STEP, SHIFT )                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
{                                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
  const POINTER_TYPE *channels[ 3 ], *p[ 3 ];                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
  mlib_u32 c[ 3 ];                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
  mlib_s32 j;                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
  p[ 0 ] = s->lut[ 0 ];                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
  p[ 1 ] = s->lut[ 1 ];                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
  p[ 2 ] = s->lut[ 2 ];                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
  channels[ 0 ] = src + POINTER_SHIFT;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
  channels[ 1 ] = src + 1 + POINTER_SHIFT;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
  channels[ 2 ] = src + 2 + POINTER_SHIFT;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
  for( j = 0; j < length; j++ )                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
  {                                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    mlib_s32 pass = BITS - 1;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    mlib_u32 position[ 3 ] = { 0, 0, 0 };                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    mlib_s32 we_found_it = 0;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    struct lut_node_3 *node = s->table;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    /* Stack pointer pointers to the first free element of stack. */         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    /* The node we are in is in the `node' */                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    struct                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    {                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
      struct lut_node_3 *node;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
      mlib_s32 q;                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    } stack[ BITS ];                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    mlib_s32 stack_pointer = 0;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    c[ 0 ] = *channels[ 0 ] - SUBTRACTION;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    c[ 1 ] = *channels[ 1 ] - SUBTRACTION;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    c[ 2 ] = *channels[ 2 ] - SUBTRACTION;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    do                                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    {                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
      mlib_s32 q;                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
      mlib_u32 current_size = 1 << pass;                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
      q = ( ( c[ 0 ] >> pass ) & 1 ) |                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        ( ( ( c[ 1 ] << 1 ) >> pass ) & 2 ) |                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        ( ( ( c[ 2 ] << 2 ) >> pass ) & 4 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
      position[ 0 ] |= c[ 0 ] & current_size;                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
      position[ 1 ] |= c[ 1 ] & current_size;                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
      position[ 2 ] |= c[ 2 ] & current_size;                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
      if( node->tag & ( 1 << q ) )                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
      {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        /*                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
          Here is a cell with one color. We need to be sure it's             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
          the one that is the closest to our color                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        */                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        mlib_s32 palindex = node->contents.index[ q ];                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        mlib_u32 palc[ 3 ];                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        mlib_s32 identical;                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        palc[ 0 ] = p[ 0 ][ palindex ] - SUBTRACTION;                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        palc[ 1 ] = p[ 1 ][ palindex ] - SUBTRACTION;                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        palc[ 2 ] = p[ 2 ][ palindex ] - SUBTRACTION;                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        identical = ( palc[ 0 ] - c[ 0 ] ) | ( palc[ 1 ] - c[ 1 ] ) |        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
          ( palc[ 2 ] - c[ 2 ] );                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        if( !identical || BITS - pass == bits )                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
          /* Oh, here it is :) */                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
          dst[ j ] = palindex + s->offset;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
          we_found_it = 1;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        else                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
          mlib_u32 distance;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
          /* First index is the channel, second is the number of the         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
             side */                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
          mlib_s32 found_color;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
          mlib_s32 continue_up;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
          distance = FIND_DISTANCE_3( c[ 0 ], palc[ 0 ],                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            c[ 1 ], palc[ 1 ], c[ 2 ], palc[ 2 ], SHIFT );                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
          found_color = palindex;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
          do                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            mlib_s32 check_corner;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            /*                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
              Neibours are enumerated in a cicle:                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
              0 - between quadrants 0 and 1,                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
              1 - between quadrants 1 and 2 and                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
              2 - between quadrants 2 and 0                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            */                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            mlib_s32 check_neibours[ 3 ];                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            /*                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
              Others are three two neibour quadrants                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
              Side number is [ <number of the coordinate >][ <the bit        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
              in the quadrant number of the corner, corresponding to         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
              this coordinate> ], e.g. 2 is 0..010b, so the sides it has     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
              near are:                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
              [ 0 (coordinate number) ][ 0 (bit 0 in the number) ]           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
              [ 1 (coordinate number) ][ 1 (bit 1 in the number) ]           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
              Now we can look in the three nearest quadrants. Do             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
              we really need it ? Check it.                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            */                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            check_corner = check_neibours[ 0 ] = check_neibours[ 1 ] =       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
              check_neibours[ 2 ] = 0;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            continue_up = 0;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            if( q & 1 )                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
              BINARY_TREE_EXPLORE_LEFT_3( 0, SOURCE_IMAGE, 2, 0,             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                SUBTRACTION, SHIFT );                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            else                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
              BINARY_TREE_EXPLORE_RIGHT_3( 0, COLOR_MAX, SOURCE_IMAGE, 2, 0, \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                SUBTRACTION, SHIFT );                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            if( q & 2 )                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
              BINARY_TREE_EXPLORE_LEFT_3( 1, SOURCE_IMAGE, 0, 1,             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                SUBTRACTION, SHIFT );                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            else                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
              BINARY_TREE_EXPLORE_RIGHT_3( 1, COLOR_MAX, SOURCE_IMAGE, 0, 1, \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                SUBTRACTION, SHIFT );                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            if( q & 4 )                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
              BINARY_TREE_EXPLORE_LEFT_3( 2, SOURCE_IMAGE, 1, 2,             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                SUBTRACTION, SHIFT );                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            else                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
              BINARY_TREE_EXPLORE_RIGHT_3( 2, COLOR_MAX, SOURCE_IMAGE, 1, 2, \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                SUBTRACTION, SHIFT );                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            if( check_neibours[ 0 ] >= 2 )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
              mlib_s32 qq = q ^ 3;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
              CHECK_QUADRANT_##SOURCE_IMAGE##_3( qq );                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            if( check_neibours[ 1 ] >= 2 )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
              mlib_s32 qq = q ^ 6;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
              CHECK_QUADRANT_##SOURCE_IMAGE##_3( qq );                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            if( check_neibours[ 2 ] >= 2 )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
              mlib_s32 qq = q ^ 5;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
              CHECK_QUADRANT_##SOURCE_IMAGE##_3( qq );                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            if( check_corner >= 3 )                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
              mlib_s32 qq = q ^ 7;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
              CHECK_QUADRANT_##SOURCE_IMAGE##_3( qq );                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            if( q & 1 )                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
              BINARY_TREE_SEARCH_RIGHT( 0, COLOR_MAX, SHIFT );               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            else                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
              BINARY_TREE_SEARCH_LEFT( 0, SHIFT );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            if( q & 2 )                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
              BINARY_TREE_SEARCH_RIGHT( 1, COLOR_MAX, SHIFT );               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            else                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
              BINARY_TREE_SEARCH_LEFT( 1, SHIFT );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            if( q & 4 )                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
              BINARY_TREE_SEARCH_RIGHT( 2, COLOR_MAX, SHIFT );               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            else                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            {                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
              BINARY_TREE_SEARCH_LEFT( 2, SHIFT );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            }                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            position[ 0 ] &= ~( c[ 0 ] & current_size );                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            position[ 1 ] &= ~( c[ 1 ] & current_size );                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            position[ 2 ] &= ~( c[ 2 ] & current_size );                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            current_size <<= 1;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            pass++;                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            stack_pointer--;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            q = stack[ stack_pointer ].q;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            node = stack[ stack_pointer ].node;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
          } while( continue_up );                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
          dst[ j ] = found_color + s->offset;                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
          we_found_it = 1;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        }                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
      }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
      else if( node->contents.quadrants[ q ] )                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
      {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        /* Descend one level */                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        stack[ stack_pointer ].node = node;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        stack[ stack_pointer++ ].q = q;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        node = node->contents.quadrants[ q ];                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
      }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
      else                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
      {                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        /* Found the empty quadrant. Look around */                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        mlib_u32 distance = MLIB_U32_MAX;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        mlib_s32 found_color;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        mlib_s32 continue_up;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        /*                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
          As we had come to this level, it is warranted that there           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
          are other points on this level near the empty quadrant             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        */                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        do                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        {                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
          mlib_s32 check_corner;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
          mlib_s32 check_neibours[ 3 ];                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
          check_corner = check_neibours[ 0 ] = check_neibours[ 1 ] =         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            check_neibours[ 2 ] = 0;                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
          continue_up = 0;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
          if( q & 1 )                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            BINARY_TREE_EXPLORE_LEFT_3( 0, SOURCE_IMAGE, 2, 0,               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
              SUBTRACTION, SHIFT );                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
          else                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            BINARY_TREE_EXPLORE_RIGHT_3( 0, COLOR_MAX, SOURCE_IMAGE, 2, 0,   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
              SUBTRACTION, SHIFT );                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
          if( q & 2 )                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            BINARY_TREE_EXPLORE_LEFT_3( 1, SOURCE_IMAGE, 0, 1,               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
              SUBTRACTION, SHIFT );                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
          else                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            BINARY_TREE_EXPLORE_RIGHT_3( 1, COLOR_MAX, SOURCE_IMAGE, 0, 1,   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
              SUBTRACTION, SHIFT );                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
          if( q & 4 )                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            BINARY_TREE_EXPLORE_LEFT_3( 2, SOURCE_IMAGE, 1, 2,               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
              SUBTRACTION, SHIFT );                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
          else                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            BINARY_TREE_EXPLORE_RIGHT_3( 2, COLOR_MAX, SOURCE_IMAGE, 1, 2,   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
              SUBTRACTION, SHIFT );                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
          if( check_neibours[ 0 ] >= 2 )                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            mlib_s32 qq = q ^ 3;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            CHECK_QUADRANT_##SOURCE_IMAGE##_3( qq );                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
          if( check_neibours[ 1 ] >= 2 )                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            mlib_s32 qq = q ^ 6;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            CHECK_QUADRANT_##SOURCE_IMAGE##_3( qq );                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
          if( check_neibours[ 2 ] >= 2 )                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            mlib_s32 qq = q ^ 5;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            CHECK_QUADRANT_##SOURCE_IMAGE##_3( qq );                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
          if( check_corner >= 3 )                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            mlib_s32 qq = q ^ 7;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            CHECK_QUADRANT_##SOURCE_IMAGE##_3( qq );                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
          if( q & 1 )                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            BINARY_TREE_SEARCH_RIGHT( 0, COLOR_MAX, SHIFT );                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
          else                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            BINARY_TREE_SEARCH_LEFT( 0, SHIFT );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
          if( q & 2 )                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            BINARY_TREE_SEARCH_RIGHT( 1, COLOR_MAX, SHIFT );                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
          else                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            BINARY_TREE_SEARCH_LEFT( 1, SHIFT );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
          if( q & 4 )                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            BINARY_TREE_SEARCH_RIGHT( 2, COLOR_MAX, SHIFT );                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
          else                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
          {                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            BINARY_TREE_SEARCH_LEFT( 2, SHIFT );                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
          }                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
          position[ 0 ] &= ~( c[ 0 ] & current_size );                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
          position[ 1 ] &= ~( c[ 1 ] & current_size );                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
          position[ 2 ] &= ~( c[ 2 ] & current_size );                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
          current_size <<= 1;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
          pass++;                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
          stack_pointer--;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
          q = stack[ stack_pointer ].q;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
          node = stack[ stack_pointer ].node;                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        } while( continue_up );                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        dst[ j ] = found_color + s->offset;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        we_found_it = 1;                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
      }                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
      pass--;                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    } while( !we_found_it );                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    channels[ 0 ] += STEP;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    channels[ 1 ] += STEP;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    channels[ 2 ] += STEP;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
  }                                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
#define CHECK_QUADRANT_U8_4( qq )                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
{                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
  if( node->tag & ( 1 << qq ) )                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
  {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    /* Here is another color cell. Check the distance */        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    mlib_s32 new_found_color = node->contents.index[ qq ];      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    mlib_u32 newdistance = FIND_DISTANCE_4( c[ 0 ],             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
      p[ 0 ][ new_found_color ], c[ 1 ],                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
      p[ 1 ][ new_found_color ], c[ 2 ],                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
      p[ 2 ][ new_found_color ], c[ 3 ],                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
      p[ 3 ][ new_found_color ], 0 );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    if( newdistance < distance )                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
      found_color = new_found_color;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
      distance = newdistance;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
  }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
  else if( node->contents.quadrants[ qq ] )                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    /* Here is a full node. Just explore it all */              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    distance = mlib_search_quadrant_U8_4(                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
      node->contents.quadrants[ qq ], distance, &found_color,   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
      c[ 0 ], c[ 1 ], c[ 2 ], c[ 3 ], p );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
/* Else there is just an empty cell */                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
#define CHECK_QUADRANT_S16_4( qq )                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
{                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
  if( node->tag & ( 1 << qq ) )                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
  {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    /* Here is another color cell. Check the distance */        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    mlib_s32 new_found_color = node->contents.index[ qq ];      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
    mlib_u32 palc0, palc1, palc2, palc3, newdistance;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    palc0 = p[ 0 ][ new_found_color ] - MLIB_S16_MIN;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    palc1 = p[ 1 ][ new_found_color ] - MLIB_S16_MIN;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    palc2 = p[ 2 ][ new_found_color ] - MLIB_S16_MIN;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    palc3 = p[ 3 ][ new_found_color ] - MLIB_S16_MIN;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    newdistance = FIND_DISTANCE_4( c[ 0 ], palc0,               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
      c[ 1 ], palc1,                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
      c[ 2 ], palc2,                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
      c[ 3 ], palc3, 2 );                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    if( newdistance < distance )                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
      found_color = new_found_color;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
      distance = newdistance;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
  }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
  else if( node->contents.quadrants[ qq ] )                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    /* Here is a full node. Just explore it all */              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    distance = mlib_search_quadrant_S16_4(                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
      node->contents.quadrants[ qq ], distance, &found_color,   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
      c[ 0 ], c[ 1 ], c[ 2 ], c[ 3 ], p );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
/* Else there is just an empty cell */                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
#define BINARY_TREE_SEARCH_4( SOURCE_IMAGE, POINTER_TYPE, BITS,               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
  COLOR_MAX, SUBTRACTION, SHIFT )                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
{                                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
  const POINTER_TYPE *channels[ 4 ], *p[ 4 ];                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
  mlib_u32 c[ 4 ];                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
  mlib_s32 j;                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
  p[ 0 ] = s->lut[ 0 ];                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
  p[ 1 ] = s->lut[ 1 ];                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
  p[ 2 ] = s->lut[ 2 ];                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
  p[ 3 ] = s->lut[ 3 ];                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
  channels[ 0 ] = src;                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
  channels[ 1 ] = src + 1;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
  channels[ 2 ] = src + 2;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
  channels[ 3 ] = src + 3;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
  for( j = 0; j < length; j++ )                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
  {                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    mlib_s32 pass = BITS - 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    mlib_u32 position[ 4 ] = { 0, 0, 0, 0 };                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    mlib_s32 we_found_it = 0;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    struct lut_node_4 *node = s->table;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
    /* Stack pointer pointers to the first free element of stack. */          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    /* The node we are in is in the `node' */                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    struct                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    {                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
      struct lut_node_4 *node;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
      mlib_s32 q;                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    } stack[ BITS ];                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    mlib_s32 stack_pointer = 0;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    c[ 0 ] = *channels[ 0 ] - SUBTRACTION;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    c[ 1 ] = *channels[ 1 ] - SUBTRACTION;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    c[ 2 ] = *channels[ 2 ] - SUBTRACTION;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    c[ 3 ] = *channels[ 3 ] - SUBTRACTION;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    do                                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    {                                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
      mlib_s32 q;                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
      mlib_u32 current_size = 1 << pass;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
      q = ( ( c[ 0 ] >> pass ) & 1 ) |                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        ( ( ( c[ 1 ] << 1 ) >> pass ) & 2 ) |                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        ( ( ( c[ 2 ] << 2 ) >> pass ) & 4 ) |                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        ( ( ( c[ 3 ] << 3 ) >> pass ) & 8 );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
      position[ 0 ] |= c[ 0 ] & current_size;                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
      position[ 1 ] |= c[ 1 ] & current_size;                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
      position[ 2 ] |= c[ 2 ] & current_size;                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
      position[ 3 ] |= c[ 3 ] & current_size;                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
      if( node->tag & ( 1 << q ) )                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
      {                                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        /*                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
          Here is a cell with one color. We need to be sure it's              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
          the one that is the closest to our color                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        */                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        mlib_s32 palindex = node->contents.index[ q ];                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        mlib_u32 palc[ 4 ];                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        mlib_s32 identical;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        palc[ 0 ] = p[ 0 ][ palindex ] - SUBTRACTION;                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        palc[ 1 ] = p[ 1 ][ palindex ] - SUBTRACTION;                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        palc[ 2 ] = p[ 2 ][ palindex ] - SUBTRACTION;                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        palc[ 3 ] = p[ 3 ][ palindex ] - SUBTRACTION;                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        identical = ( palc[ 0 ] - c[ 0 ] ) | ( palc[ 1 ] - c[ 1 ] ) |         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
          ( palc[ 2 ] - c[ 2 ] ) | ( palc[ 3 ] - c[ 3 ] );                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        if( !identical || BITS - pass == bits )                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        {                                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
          /* Oh, here it is :) */                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
          dst[ j ] = palindex + s->offset;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
          we_found_it = 1;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        }                                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        else                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        {                                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
          mlib_u32 distance;                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
          /* First index is the channel, second is the number of the          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
             side */                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
          mlib_s32 found_color;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
          mlib_s32 continue_up;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
          distance = FIND_DISTANCE_4( c[ 0 ], palc[ 0 ],                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            c[ 1 ], palc[ 1 ], c[ 2 ], palc[ 2 ], c[ 3 ], palc[ 3 ], SHIFT ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
          found_color = palindex;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
          do                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            mlib_s32 check_corner;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            mlib_s32 check_neibours[ 6 ];                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            mlib_s32 check_far_neibours[ 4 ];                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            /*                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
              Check neibours: quadrants that are different by 2 bits          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
              from the quadrant, that we are in:                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
              3 -  0                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
              5 -  1                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
              6 -  2                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
              9 -  3                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
              10 - 4                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
              12 - 5                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
              Far quadrants: different by 3 bits:                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
              7  - 0                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
              11 - 1                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
              13 - 2                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
              14 - 3                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            */                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            check_neibours[ 0 ] = check_neibours[ 1 ] =                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
              check_neibours[ 2 ] = check_neibours[ 3 ] =                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
              check_neibours[ 4 ] = check_neibours[ 5 ] = 0;                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            continue_up = 0;                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            if( q & 1 )                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
              BINARY_TREE_EXPLORE_LEFT_4( 0, SOURCE_IMAGE, 0, 1, 3,           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                SUBTRACTION, SHIFT );                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            else                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
              BINARY_TREE_EXPLORE_RIGHT_4( 0, COLOR_MAX, SOURCE_IMAGE,        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                0, 1, 3, SUBTRACTION, SHIFT );                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            if( q & 2 )                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
              BINARY_TREE_EXPLORE_LEFT_4( 1, SOURCE_IMAGE, 0, 2, 4,           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                SUBTRACTION, SHIFT );                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            else                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
              BINARY_TREE_EXPLORE_RIGHT_4( 1, COLOR_MAX, SOURCE_IMAGE,        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                0, 2, 4, SUBTRACTION, SHIFT );                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            if( q & 4 )                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
              BINARY_TREE_EXPLORE_LEFT_4( 2, SOURCE_IMAGE, 1, 2, 5,           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                SUBTRACTION, SHIFT );                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            else                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
              BINARY_TREE_EXPLORE_RIGHT_4( 2, COLOR_MAX, SOURCE_IMAGE,        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                1, 2, 5, SUBTRACTION, SHIFT );                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            if( q & 8 )                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
              BINARY_TREE_EXPLORE_LEFT_4( 3, SOURCE_IMAGE, 3, 4, 5,           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                SUBTRACTION, SHIFT );                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            else                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
              BINARY_TREE_EXPLORE_RIGHT_4( 3, COLOR_MAX, SOURCE_IMAGE,        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                3, 4, 5, SUBTRACTION, SHIFT );                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            check_far_neibours[ 0 ] = check_neibours[ 0 ] +                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
              check_neibours[ 1 ] + check_neibours[ 2 ];                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            check_far_neibours[ 1 ] = check_neibours[ 0 ] +                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
              check_neibours[ 3 ] + check_neibours[ 4 ];                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            check_far_neibours[ 2 ] = check_neibours[ 1 ] +                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
              check_neibours[ 3 ] + check_neibours[ 5 ];                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            check_far_neibours[ 3 ] = check_neibours[ 2 ] +                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
              check_neibours[ 4 ] + check_neibours[ 5 ];                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
            check_corner = check_far_neibours[ 0 ] +                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
              check_far_neibours[ 1 ] +                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
              check_far_neibours[ 2 ] +                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
              check_far_neibours[ 3 ];                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            if( check_neibours[ 0 ] >= 2 )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
              mlib_s32 qq = q ^ 3;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            if( check_neibours[ 1 ] >= 2 )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
              mlib_s32 qq = q ^ 5;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            if( check_neibours[ 2 ] >= 2 )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
              mlib_s32 qq = q ^ 6;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            if( check_neibours[ 3 ] >= 2 )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
              mlib_s32 qq = q ^ 9;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            if( check_neibours[ 4 ] >= 2 )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
              mlib_s32 qq = q ^ 10;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            if( check_neibours[ 5 ] >= 2 )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
              mlib_s32 qq = q ^ 12;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            if( check_far_neibours[ 0 ] >= 3 )                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
              mlib_s32 qq = q ^ 7;                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            if( check_far_neibours[ 1 ] >= 3 )                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
              mlib_s32 qq = q ^ 11;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            if( check_far_neibours[ 2 ] >= 3 )                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
              mlib_s32 qq = q ^ 13;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            if( check_far_neibours[ 3 ] >= 3 )                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
              mlib_s32 qq = q ^ 14;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            if( check_corner >= 4 )                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
              mlib_s32 qq = q ^ 15;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
              CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            if( q & 1 )                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
              BINARY_TREE_SEARCH_RIGHT( 0, COLOR_MAX, SHIFT );                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
            else                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
              BINARY_TREE_SEARCH_LEFT( 0, SHIFT );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            if( q & 2 )                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
              BINARY_TREE_SEARCH_RIGHT( 1, COLOR_MAX, SHIFT );                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            else                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
              BINARY_TREE_SEARCH_LEFT( 1, SHIFT );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            if( q & 4 )                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
              BINARY_TREE_SEARCH_RIGHT( 2, COLOR_MAX, SHIFT );                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            else                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
              BINARY_TREE_SEARCH_LEFT( 2, SHIFT );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            if( q & 8 )                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
              BINARY_TREE_SEARCH_RIGHT( 3, COLOR_MAX, SHIFT );                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            else                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
              BINARY_TREE_SEARCH_LEFT( 3, SHIFT );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            position[ 0 ] &= ~( c[ 0 ] & current_size );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            position[ 1 ] &= ~( c[ 1 ] & current_size );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            position[ 2 ] &= ~( c[ 2 ] & current_size );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            position[ 3 ] &= ~( c[ 3 ] & current_size );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            current_size <<= 1;                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            pass++;                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            stack_pointer--;                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            q = stack[ stack_pointer ].q;                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            node = stack[ stack_pointer ].node;                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
          } while( continue_up );                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
          dst[ j ] = found_color + s->offset;                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
          we_found_it = 1;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        }                                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
      }                                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
      else if( node->contents.quadrants[ q ] )                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
      {                                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        /* Descend one level */                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        stack[ stack_pointer ].node = node;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        stack[ stack_pointer++ ].q = q;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        node = node->contents.quadrants[ q ];                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
      }                                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
      else                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
      {                                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        /* Found the empty quadrant. Look around */                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        mlib_u32 distance = MLIB_U32_MAX;                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        mlib_s32 found_color;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        mlib_s32 continue_up;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        /*                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
          As we had come to this level, it is warranted that there            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
          are other points on this level near the empty quadrant              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        */                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        do                                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        {                                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
          mlib_s32 check_corner;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
          mlib_s32 check_neibours[ 6 ];                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
          mlib_s32 check_far_neibours[ 4 ];                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
          /*                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            Check neibours: quadrants that are different by 2 bits            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            from the quadrant, that we are in:                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            3 -  0                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            5 -  1                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            6 -  2                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            9 -  3                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
            10 - 4                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            12 - 5                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            Far quadrants: different by 3 bits:                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            7  - 0                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            11 - 1                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            13 - 2                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            14 - 3                                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
          */                                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
          check_neibours[ 0 ] = check_neibours[ 1 ] =                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            check_neibours[ 2 ] = check_neibours[ 3 ] =                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            check_neibours[ 4 ] = check_neibours[ 5 ] = 0;                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
          continue_up = 0;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
          if( q & 1 )                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            BINARY_TREE_EXPLORE_LEFT_4( 0, SOURCE_IMAGE, 0, 1, 3,             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
              SUBTRACTION, SHIFT );                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
          else                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            BINARY_TREE_EXPLORE_RIGHT_4( 0, COLOR_MAX, SOURCE_IMAGE,          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
              0, 1, 3, SUBTRACTION, SHIFT );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
          if( q & 2 )                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            BINARY_TREE_EXPLORE_LEFT_4( 1, SOURCE_IMAGE, 0, 2, 4,             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
              SUBTRACTION, SHIFT );                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
          else                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
            BINARY_TREE_EXPLORE_RIGHT_4( 1, COLOR_MAX, SOURCE_IMAGE,          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
              0, 2, 4, SUBTRACTION, SHIFT );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
          if( q & 4 )                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            BINARY_TREE_EXPLORE_LEFT_4( 2, SOURCE_IMAGE, 1, 2, 5,             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
              SUBTRACTION, SHIFT );                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
          else                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            BINARY_TREE_EXPLORE_RIGHT_4( 2, COLOR_MAX, SOURCE_IMAGE,          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
              1, 2, 5, SUBTRACTION, SHIFT );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
          if( q & 8 )                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            BINARY_TREE_EXPLORE_LEFT_4( 3, SOURCE_IMAGE, 3, 4, 5,             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
              SUBTRACTION, SHIFT );                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
          else                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
            BINARY_TREE_EXPLORE_RIGHT_4( 3, COLOR_MAX, SOURCE_IMAGE,          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
              3, 4, 5, SUBTRACTION, SHIFT );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
          check_far_neibours[ 0 ] = check_neibours[ 0 ] +                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
            check_neibours[ 1 ] + check_neibours[ 2 ];                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
          check_far_neibours[ 1 ] = check_neibours[ 0 ] +                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            check_neibours[ 3 ] + check_neibours[ 4 ];                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
          check_far_neibours[ 2 ] = check_neibours[ 1 ] +                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            check_neibours[ 3 ] + check_neibours[ 5 ];                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
          check_far_neibours[ 3 ] = check_neibours[ 2 ] +                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            check_neibours[ 4 ] + check_neibours[ 5 ];                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
          check_corner = check_far_neibours[ 0 ] +                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
            check_far_neibours[ 1 ] +                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            check_far_neibours[ 2 ] +                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            check_far_neibours[ 3 ];                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
          if( check_neibours[ 0 ] >= 2 )                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            mlib_s32 qq = q ^ 3;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
          if( check_neibours[ 1 ] >= 2 )                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            mlib_s32 qq = q ^ 5;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
          if( check_neibours[ 2 ] >= 2 )                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            mlib_s32 qq = q ^ 6;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
          if( check_neibours[ 3 ] >= 2 )                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            mlib_s32 qq = q ^ 9;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
          if( check_neibours[ 4 ] >= 2 )                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
            mlib_s32 qq = q ^ 10;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
          if( check_neibours[ 5 ] >= 2 )                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            mlib_s32 qq = q ^ 12;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
          if( check_far_neibours[ 0 ] >= 3 )                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
            mlib_s32 qq = q ^ 7;                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
          if( check_far_neibours[ 1 ] >= 3 )                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
            mlib_s32 qq = q ^ 11;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
          if( check_far_neibours[ 2 ] >= 3 )                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            mlib_s32 qq = q ^ 13;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
          if( check_far_neibours[ 3 ] >= 3 )                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            mlib_s32 qq = q ^ 14;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
          if( check_corner >= 4 )                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            mlib_s32 qq = q ^ 15;                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            CHECK_QUADRANT_##SOURCE_IMAGE##_4( qq );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
          if( q & 1 )                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            BINARY_TREE_SEARCH_RIGHT( 0, COLOR_MAX, SHIFT );                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
          else                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
            BINARY_TREE_SEARCH_LEFT( 0, SHIFT );                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
          if( q & 2 )                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
            BINARY_TREE_SEARCH_RIGHT( 1, COLOR_MAX, SHIFT );                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
          else                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            BINARY_TREE_SEARCH_LEFT( 1, SHIFT );                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
          if( q & 4 )                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            BINARY_TREE_SEARCH_RIGHT( 2, COLOR_MAX, SHIFT );                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
          else                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            BINARY_TREE_SEARCH_LEFT( 2, SHIFT );                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
          if( q & 8 )                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            BINARY_TREE_SEARCH_RIGHT( 3, COLOR_MAX, SHIFT );                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
          else                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
          {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            BINARY_TREE_SEARCH_LEFT( 3, SHIFT );                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
          }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
          position[ 0 ] &= ~( c[ 0 ] & current_size );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
          position[ 1 ] &= ~( c[ 1 ] & current_size );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
          position[ 2 ] &= ~( c[ 2 ] & current_size );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
          position[ 3 ] &= ~( c[ 3 ] & current_size );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
          current_size <<= 1;                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
          pass++;                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
          stack_pointer--;                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
          q = stack[ stack_pointer ].q;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
          node = stack[ stack_pointer ].node;                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        } while( continue_up );                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        dst[ j ] = found_color + s->offset;                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        we_found_it = 1;                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
      }                                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
      pass--;                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
    } while( !we_found_it );                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                                                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
    channels[ 0 ] += 4;                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    channels[ 1 ] += 4;                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
    channels[ 2 ] += 4;                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
    channels[ 3 ] += 4;                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
  }                                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
#define FIND_NEAREST_U8_3_C( SHIFT, STEP )                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
  mlib_s32 i, k, k_min, min_dist, diff, mask;                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
  mlib_s32 offset = mlib_ImageGetLutOffset( s ) - 1;            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
  mlib_s32 entries = s -> lutlength;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
  mlib_d64 *double_lut = mlib_ImageGetLutDoubleData( s );       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
  mlib_d64 col0, col1, col2;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
  mlib_d64 dist, len0, len1, len2;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
  for ( i = 0; i < length; i++ ) {                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    col0 = src[ STEP * i + SHIFT ];                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    col1 = src[ STEP * i + 1 + SHIFT ];                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
    col2 = src[ STEP * i + 2 + SHIFT ];                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
    min_dist = MLIB_S32_MAX;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
    k_min = 1;                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    len0 = double_lut[ 0 ] - col0;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
    len1 = double_lut[ 1 ] - col1;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
    len2 = double_lut[ 2 ] - col2;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
    for ( k = 1; k <= entries; k++ ) {                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
      dist = len0 * len0;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
      len0 = double_lut[ 3 * k ] - col0;                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
      dist += len1 * len1;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
      len1 = double_lut[ 3 * k + 1 ] - col1;                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
      dist += len2 * len2;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
      len2 = double_lut[ 3 * k + 2 ] - col2;                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
      diff = ( mlib_s32 )dist - min_dist;                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
      mask = diff >> 31;                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
      min_dist += diff & mask;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
      k_min += ( k - k_min ) & mask;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
    dst[ i ] = k_min + offset;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
#define FIND_NEAREST_U8_4_C                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
  mlib_s32 i, k, k_min, min_dist, diff, mask;                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
  mlib_s32 offset = mlib_ImageGetLutOffset( s ) - 1;            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
  mlib_s32 entries = s -> lutlength;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
  mlib_d64 *double_lut = mlib_ImageGetLutDoubleData( s );       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
  mlib_d64 col0, col1, col2, col3;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
  mlib_d64 dist, len0, len1, len2, len3;                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
  for ( i = 0; i < length; i++ ) {                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
    col0 = src[ 4 * i ];                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
    col1 = src[ 4 * i + 1 ];                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
    col2 = src[ 4 * i + 2 ];                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
    col3 = src[ 4 * i + 3 ];                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
    min_dist = MLIB_S32_MAX;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
    k_min = 1;                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
    len0 = double_lut[ 0 ] - col0;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
    len1 = double_lut[ 1 ] - col1;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
    len2 = double_lut[ 2 ] - col2;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
    len3 = double_lut[ 3 ] - col3;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
    for ( k = 1; k <= entries; k++ ) {                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
      dist = len0 * len0;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
      len0 =  double_lut[ 4 * k ] - col0;                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
      dist += len1 * len1;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
      len1 = double_lut[ 4 * k + 1 ] - col1;                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
      dist += len2 * len2;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
      len2 =  double_lut[ 4 * k + 2 ] - col2;                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
      dist += len3 * len3;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
      len3 =  double_lut[ 4 * k + 3 ] - col3;                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
      diff = ( mlib_s32 )dist - min_dist;                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
      mask = diff >> 31;                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
      min_dist += diff & mask;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
      k_min += ( k - k_min ) & mask;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    dst[ i ] = k_min + offset;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
#define FSQR_S16_HI(dsrc)                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
  vis_fpadd32( vis_fmuld8ulx16( vis_read_hi( dsrc ), vis_read_hi( dsrc ) ), \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    vis_fmuld8sux16( vis_read_hi( dsrc ), vis_read_hi( dsrc ) ) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
#define FSQR_S16_LO(dsrc)                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
  vis_fpadd32( vis_fmuld8ulx16( vis_read_lo( dsrc ), vis_read_lo( dsrc) ), \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
    vis_fmuld8sux16( vis_read_lo( dsrc ), vis_read_lo( dsrc ) ) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
#define FIND_NEAREST_U8_3                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
{                                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
  mlib_d64 *dpsrc, dsrc, dsrc1, ddist, ddist1, ddist2, ddist3;        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
  mlib_d64 dcolor, dind, dres, dres1, dpind[1], dpmin[1];             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
  mlib_d64 done = vis_to_double_dup( 1 ),                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
           dmax = vis_to_double_dup( MLIB_S32_MAX );                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
  mlib_f32 *lut = ( mlib_f32 * )mlib_ImageGetLutNormalTable( s );     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
  mlib_f32 fone = vis_to_float( 0x100 );                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
  mlib_s32 i, k, mask;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
  mlib_s32 gsr[1];                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
  mlib_s32 offset = mlib_ImageGetLutOffset( s ) - 1;                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
  mlib_s32 entries = s->lutlength;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
  gsr[0] = vis_read_gsr();                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
  for( i = 0; i <= ( length-2 ); i += 2 )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
  {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
    dpsrc = VIS_ALIGNADDR( src, -1 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
    src += 6;                                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
    dsrc = dpsrc[ 0 ];                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
    dsrc1 = dpsrc[ 1 ];                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
    dsrc1 = vis_faligndata( dsrc, dsrc1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
    dsrc = vis_fmul8x16al( vis_read_hi( dsrc1 ), fone );              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
    VIS_ALIGNADDR( dpsrc, 3 );                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
    dsrc1 = vis_faligndata( dsrc1, dsrc1 );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
    dsrc1 = vis_fmul8x16al( vis_read_hi( dsrc1 ), fone );             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
    dpind[ 0 ] = dind = done;                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
    dpmin[ 0 ] = dmax;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
    dcolor = vis_fmul8x16al( lut[ 0 ], fone );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
    for( k = 1; k <= entries; k++ )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
    {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
      ddist1 = vis_fpsub16( dcolor, dsrc );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
      ddist = FSQR_S16_HI( ddist1 );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
      ddist1 = FSQR_S16_LO( ddist1 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
      dres = vis_fpadd32( ddist, ddist1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
      ddist3 = vis_fpsub16( dcolor, dsrc1 );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
      ddist2 = FSQR_S16_HI( ddist3 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
      ddist3 = FSQR_S16_LO( ddist3 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
      dres1 = vis_fpadd32( ddist2, ddist3 );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
      dcolor = vis_fmul8x16al( lut[ k ], fone );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
      dres = vis_freg_pair(                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
        vis_fpadd32s( vis_read_hi( dres ), vis_read_lo( dres ) ),     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        vis_fpadd32s( vis_read_hi( dres1 ), vis_read_lo( dres1 ) ) ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
      mask = vis_fcmplt32( dres, dpmin[ 0 ] );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
      vis_pst_32( dind, ( void * )dpind, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
      dind = vis_fpadd32( dind, done );                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
      vis_pst_32( dres, ( void * )dpmin, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
    }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
    dst[ i ] = ( ( mlib_s32 * )dpind )[ 0 ] + offset;                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
    dst[ i + 1 ] = ( ( mlib_s32 * )dpind)[ 1 ] + offset;              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
  }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
  if( i < length )                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
  {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
    dpsrc = VIS_ALIGNADDR( src, -1 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
    dsrc = dpsrc[ 0 ];                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
    dsrc1 = dpsrc[ 1 ];                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
    dsrc1 = vis_faligndata( dsrc, dsrc1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
    dsrc = vis_fmul8x16al( vis_read_hi( dsrc1 ), fone );              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
    dpind[ 0 ] = dind = done;                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
    dpmin[ 0 ] = dmax;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
    for( k = 0; k < entries; k++ )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
    {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
      dcolor = vis_fmul8x16al( lut[ k ], fone );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
      ddist1 = vis_fpsub16( dcolor, dsrc );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
      ddist = FSQR_S16_HI( ddist1 );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
      ddist1 = FSQR_S16_LO( ddist1 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
      dres = vis_fpadd32( ddist, ddist1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
      dres = vis_write_lo( dres,                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        vis_fpadd32s( vis_read_hi( dres ), vis_read_lo( dres ) ) );   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
      mask = vis_fcmplt32( dres, dpmin[ 0 ] );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
      vis_pst_32( dind, ( void * )dpind, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
      dind = vis_fpadd32( dind, done );                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
      vis_pst_32( dres, ( void * )dpmin, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
    }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
    dst[ i ] = ( ( mlib_s32 * )dpind)[ 1 ] + offset;                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
  }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
  vis_write_gsr(gsr[0]);                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
#define FIND_NEAREST_U8_3_IN4                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
{                                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
  mlib_d64 *dpsrc, dsrc, dsrc1, ddist, ddist1, ddist2, ddist3;        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
  mlib_d64 dcolor, dind, dres, dres1, dpind[1], dpmin[1];             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
  mlib_d64 done = vis_to_double_dup( 1 ),                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
           dmax = vis_to_double_dup( MLIB_S32_MAX );                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
  mlib_f32 *lut = ( mlib_f32 * )mlib_ImageGetLutNormalTable( s );     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
  mlib_f32 fone = vis_to_float( 0x100 );                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
  mlib_s32 i, k, mask, gsr[1];                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
  mlib_s32 offset = mlib_ImageGetLutOffset( s ) - 1;                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
  mlib_s32 entries = s->lutlength;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
  gsr[0] = vis_read_gsr();                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
  dpsrc = VIS_ALIGNADDR( src, 0 );                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
  for( i = 0; i <= ( length-2 ); i += 2 )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
  {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
    dsrc = dpsrc[ 0 ];                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
    dsrc1 = dpsrc[ 1 ];                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
    dsrc1 = vis_faligndata( dsrc, dsrc1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
    dpsrc++;                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
    dsrc = vis_fmul8x16al( vis_read_hi( dsrc1 ), fone );              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
    dsrc1 = vis_fmul8x16al( vis_read_lo( dsrc1 ), fone );             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
    dpind[ 0 ] = dind = done;                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
    dpmin[ 0 ] = dmax;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
    dcolor = vis_fmul8x16al( lut[ 0 ], fone );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
    for( k = 1; k <= entries; k++ )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
    {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
      ddist1 = vis_fpsub16( dcolor, dsrc );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
      ddist = FSQR_S16_HI( ddist1 );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
      ddist1 = FSQR_S16_LO( ddist1 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
      dres = vis_fpadd32( ddist, ddist1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
      ddist3 = vis_fpsub16( dcolor, dsrc1 );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
      ddist2 = FSQR_S16_HI( ddist3 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
      ddist3 = FSQR_S16_LO( ddist3 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
      dres1 = vis_fpadd32( ddist2, ddist3 );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
      dcolor = vis_fmul8x16al( lut[ k ], fone );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
      dres = vis_freg_pair(                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        vis_fpadd32s( vis_read_hi( dres ), vis_read_lo( dres ) ),     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        vis_fpadd32s( vis_read_hi( dres1 ), vis_read_lo( dres1 ) ) ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
      mask = vis_fcmplt32( dres, dpmin[ 0 ] );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
      vis_pst_32( dind, ( void * )dpind, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
      dind = vis_fpadd32( dind, done );                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
      vis_pst_32( dres, ( void * )dpmin, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
    }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
    dst[ i ] = ( ( mlib_s32 * )dpind )[ 0 ] + offset;                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
    dst[ i + 1 ] = ( ( mlib_s32 * )dpind)[ 1 ] + offset;              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
  }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
  if( i < length )                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
  {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
    dsrc = dpsrc[ 0 ];                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
    dsrc1 = dpsrc[ 1 ];                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    dsrc1 = vis_faligndata( dsrc, dsrc1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
    dsrc = vis_fmul8x16al( vis_read_hi( dsrc1 ), fone );              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    dpind[ 0 ] = dind = done;                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
    dpmin[ 0 ] = dmax;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
    for( k = 0; k < entries; k++ )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
    {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
      dcolor = vis_fmul8x16al( lut[ k ], fone );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
      ddist1 = vis_fpsub16( dcolor, dsrc );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
      ddist = FSQR_S16_HI( ddist1 );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
      ddist1 = FSQR_S16_LO( ddist1 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
      dres = vis_fpadd32( ddist, ddist1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
      dres = vis_write_lo( dres,                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        vis_fpadd32s( vis_read_hi( dres ), vis_read_lo( dres ) ) );   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
      mask = vis_fcmplt32( dres, dpmin[ 0 ] );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
      vis_pst_32( dind, ( void * )dpind, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
      dind = vis_fpadd32( dind, done );                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
      vis_pst_32( dres, ( void * )dpmin, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
    }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
    dst[ i ] = ( ( mlib_s32 * )dpind)[ 1 ] + offset;                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
  }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
  vis_write_gsr(gsr[0]);                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
#define FIND_NEAREST_U8_4                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
{                                                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
  mlib_d64 *dpsrc, dsrc, dsrc1, ddist, ddist1, ddist2, ddist3;        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
  mlib_d64 dcolor, dind, dres, dres1, dpind[ 1 ], dpmin[ 1 ];         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
  mlib_d64 done = vis_to_double_dup( 1 ),                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
           dmax = vis_to_double_dup( MLIB_S32_MAX );                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
  mlib_f32 *lut = ( mlib_f32 * )mlib_ImageGetLutNormalTable( s );     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
  mlib_f32 fone = vis_to_float( 0x100 );                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
  mlib_s32 i, k, mask, gsr[1];                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
  mlib_s32 offset = mlib_ImageGetLutOffset( s ) - 1;                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
  mlib_s32 entries = s->lutlength;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                                                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
  gsr[0] = vis_read_gsr();                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
  dpsrc = VIS_ALIGNADDR( src, 0 );                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
  for( i = 0; i <= ( length-2 ); i += 2 )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
  {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
    dsrc = dpsrc[ 0 ];                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
    dsrc1 = dpsrc[ 1 ];                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
    dsrc1 = vis_faligndata( dsrc, dsrc1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
    dpsrc++;                                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
    dsrc = vis_fmul8x16al( vis_read_hi( dsrc1 ), fone );              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
    dsrc1 = vis_fmul8x16al( vis_read_lo( dsrc1 ), fone );             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
    dpind[ 0 ] = dind = done;                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
    dpmin[ 0 ] = dmax;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
    dcolor = vis_fmul8x16al(lut[0], fone);                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
    for( k = 1; k <= entries; k++ )                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
    {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
      ddist1 = vis_fpsub16( dcolor, dsrc );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
      ddist = FSQR_S16_HI( ddist1 );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
      ddist1 = FSQR_S16_LO( ddist1 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
      dres = vis_fpadd32( ddist, ddist1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
      ddist3 = vis_fpsub16( dcolor, dsrc1 );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
      ddist2 = FSQR_S16_HI( ddist3 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
      ddist3 = FSQR_S16_LO( ddist3 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
      dres1 = vis_fpadd32( ddist2, ddist3 );                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
      dcolor = vis_fmul8x16al( lut[ k ], fone );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
      dres = vis_freg_pair(                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
        vis_fpadd32s( vis_read_hi( dres ), vis_read_lo( dres ) ),     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        vis_fpadd32s( vis_read_hi( dres1 ), vis_read_lo( dres1 ) ) ); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
      mask = vis_fcmplt32( dres, dpmin[ 0 ] );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
      vis_pst_32( dind, ( void * )dpind, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
      dind = vis_fpadd32( dind, done );                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
      vis_pst_32( dres, ( void * )dpmin, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
    }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
    dst[ i ] = ( ( mlib_s32 * )dpind )[ 0 ] + offset;                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
    dst[ i + 1 ] = ( ( mlib_s32 * )dpind )[ 1 ] + offset;             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
  }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
  if( i < length )                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
  {                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
    dsrc = dpsrc[ 0 ];                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    dsrc1 = dpsrc[ 1 ];                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    dsrc1 = vis_faligndata( dsrc, dsrc1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
    dsrc = vis_fmul8x16al( vis_read_hi( dsrc1 ), fone );              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    dpind[ 0 ] = dind = done;                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
    dpmin[ 0 ] = dmax;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
    for( k = 0; k < entries; k++ )                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
    {                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
      dcolor = vis_fmul8x16al( lut[ k ], fone );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
      ddist1 = vis_fpsub16( dcolor, dsrc );                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
      ddist = FSQR_S16_HI( ddist1 );                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
      ddist1 = FSQR_S16_LO( ddist1 );                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
      dres = vis_fpadd32( ddist, ddist1 );                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
      dres = vis_write_lo( dres,                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        vis_fpadd32s( vis_read_hi( dres ), vis_read_lo( dres ) ) );   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
      mask = vis_fcmplt32( dres, dpmin[ 0 ] );                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
      vis_pst_32( dind, ( void * )dpind, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
      dind = vis_fpadd32( dind, done );                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
      vis_pst_32( dres, ( void * )dpmin, mask );                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
    }                                                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
    dst[ i ] = ( ( mlib_s32 * )dpind )[ 1 ] + offset;                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
  }                                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
  vis_write_gsr(gsr[0]);                                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
#define FIND_NEAREST_S16_3( SHIFT, STEP )                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
  mlib_s32 i, k, k_min, min_dist, diff, mask;                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
  mlib_s32 offset = mlib_ImageGetLutOffset( s ) - 1;            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
  mlib_s32 entries = s->lutlength;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
  mlib_d64 *double_lut = mlib_ImageGetLutDoubleData( s );       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
  mlib_d64 col0, col1, col2;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
  mlib_d64 dist, len0, len1, len2;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
  for( i = 0; i < length; i++ )                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
  {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
    col0 = src[ STEP * i + SHIFT ];                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
    col1 = src[ STEP * i + 1 + SHIFT ];                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
    col2 = src[ STEP * i + 2 + SHIFT ];                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
    min_dist = MLIB_S32_MAX;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
    k_min = 1;                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
    len0 = double_lut[ 0 ] - col0;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
    len1 = double_lut[ 1 ] - col1;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
    len2 = double_lut[ 2 ] - col2;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
    for( k = 1; k <= entries; k++ )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
      dist = len0 * len0;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
      len0 = double_lut[ 3 * k ] - col0;                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
      dist += len1 * len1;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
      len1 = double_lut[ 3 * k + 1 ] - col1;                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
      dist += len2 * len2;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
      len2 = double_lut[ 3 * k + 2 ] - col2;                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
      diff = ( mlib_s32 )( dist * 0.125 ) - min_dist;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
      mask = diff >> 31;                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
      min_dist += diff & mask;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
      k_min += ( k - k_min ) & mask;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
    dst[ i ] = k_min + offset;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
#define FIND_NEAREST_S16_4                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
  mlib_s32 i, k, k_min, min_dist, diff, mask;                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
  mlib_s32 offset = mlib_ImageGetLutOffset( s ) - 1;            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
  mlib_s32 entries = s->lutlength;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
  mlib_d64 *double_lut = mlib_ImageGetLutDoubleData( s );       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
  mlib_d64 col0, col1, col2, col3;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
  mlib_d64 dist, len0, len1, len2, len3;                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
  for( i = 0; i < length; i++ )                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
  {                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
    col0 = src[ 4 * i ];                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
    col1 = src[ 4 * i + 1 ];                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
    col2 = src[ 4 * i + 2 ];                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
    col3 = src[ 4 * i + 3 ];                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
    min_dist = MLIB_S32_MAX;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
    k_min = 1;                                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
    len0 = double_lut[ 0 ] - col0;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
    len1 = double_lut[ 1 ] - col1;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
    len2 = double_lut[ 2 ] - col2;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
    len3 = double_lut[ 3 ] - col3;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
    for( k = 1; k <= entries; k++ )                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
    {                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
      dist = len0 * len0;                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
      len0 =  double_lut[ 4 * k ] - col0;                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
      dist += len1 * len1;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
      len1 = double_lut[ 4 * k + 1 ] - col1;                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
      dist += len2 * len2;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
      len2 =  double_lut[ 4 * k + 2 ] - col2;                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
      dist += len3 * len3;                                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
      len3 =  double_lut[ 4 * k + 3 ] - col3;                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
      diff = ( mlib_s32 )( dist * 0.125 ) - min_dist;           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
      mask = diff >> 31;                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
      min_dist += diff & mask;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
      k_min += ( k - k_min ) & mask;                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
    dst[ i ] = k_min + offset;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
mlib_status mlib_ImageColorTrue2Index(mlib_image       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                                      const mlib_image *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                                      const void       *colormap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
  mlib_s32 y, width, height, sstride, dstride, schann;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
  mlib_colormap *s = (mlib_colormap *)colormap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
  mlib_s32 channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
  mlib_type stype, dtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
  MLIB_IMAGE_CHECK(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
  MLIB_IMAGE_CHECK(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
  MLIB_IMAGE_SIZE_EQUAL(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
  MLIB_IMAGE_HAVE_CHAN(dst, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
  if (!colormap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
    return MLIB_NULLPOINTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
  channels = s->channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
  stype = mlib_ImageGetType(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
  dtype = mlib_ImageGetType(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
  width = mlib_ImageGetWidth(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
  height = mlib_ImageGetHeight(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
  sstride = mlib_ImageGetStride(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
  dstride = mlib_ImageGetStride(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
  schann = mlib_ImageGetChannels(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
  if (stype != s->intype || dtype != s->outtype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
    return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
  if (channels != schann)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
    return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
  switch (stype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
    case MLIB_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        mlib_u8 *sdata = mlib_ImageGetData(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        switch (dtype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
          case MLIB_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
              mlib_u8 *ddata = mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
              switch (channels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                    MAIN_COLORTRUE2INDEX_LOOP(U8, U8, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                    return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                    MAIN_COLORTRUE2INDEX_LOOP(U8, U8, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                    return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                  return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
          case MLIB_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
              mlib_s16 *ddata = mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
              dstride /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
              switch (channels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                    MAIN_COLORTRUE2INDEX_LOOP(U8, S16, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                    return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                    MAIN_COLORTRUE2INDEX_LOOP(U8, S16, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
                    return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
                  return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
            }
8939
04615dca2a76 6989717: media native code compiler warnings
bae
parents: 5506
diff changeset
  2626
        default:
04615dca2a76 6989717: media native code compiler warnings
bae
parents: 5506
diff changeset
  2627
          /* Unsupported type of destination image */
04615dca2a76 6989717: media native code compiler warnings
bae
parents: 5506
diff changeset
  2628
          return MLIB_FAILURE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
    case MLIB_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
        mlib_s16 *sdata = mlib_ImageGetData(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
        sstride /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
        switch (dtype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
          case MLIB_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
              mlib_u8 *ddata = mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
              switch (channels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                    MAIN_COLORTRUE2INDEX_LOOP(S16, U8, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                    return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                    MAIN_COLORTRUE2INDEX_LOOP(S16, U8, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
                    return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                  return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
          case MLIB_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
              mlib_s16 *ddata = mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
              dstride /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
              switch (channels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                    MAIN_COLORTRUE2INDEX_LOOP(S16, S16, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                    return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                    MAIN_COLORTRUE2INDEX_LOOP(S16, S16, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                    return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
                  return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
            }
8939
04615dca2a76 6989717: media native code compiler warnings
bae
parents: 5506
diff changeset
  2682
        default:
04615dca2a76 6989717: media native code compiler warnings
bae
parents: 5506
diff changeset
  2683
          /* Unsupported type of destination image */
04615dca2a76 6989717: media native code compiler warnings
bae
parents: 5506
diff changeset
  2684
          return MLIB_FAILURE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
      return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
mlib_u32 mlib_search_quadrant_U8_3(struct lut_node_3 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
                                   mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                                    mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                                   mlib_u32          c0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                                   mlib_u32          c1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
                                   mlib_u32          c2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                                   const mlib_u8     **base)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
  for (i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
    if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
      /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
      mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
      mlib_u32 newpalc0, newpalc1, newpalc2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
      mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
      newpalc0 = base[0][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
      newpalc1 = base[1][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
      newpalc2 = base[2][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
      newdistance = FIND_DISTANCE_3(c0, newpalc0, c1, newpalc1, c2, newpalc2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
      if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
        *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
        distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
    else if (node->contents.quadrants[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
      distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
        mlib_search_quadrant_U8_3(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                                  found_color, c0, c1, c2, base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
mlib_u32 mlib_search_quadrant_part_to_left_U8_3(struct lut_node_3 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                                                mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
                                                 mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
                                                const mlib_u32    *c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                                                const mlib_u8     **base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
                                                mlib_u32          position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                                                mlib_s32          pass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                                                mlib_s32          dir_bit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
  mlib_u32 current_size = 1 << pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
  static mlib_s32 opposite_quadrants[3][4] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
    {0, 2, 4, 6},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
    {0, 1, 4, 5},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
    {0, 1, 2, 3}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
/* Search only quadrant's half untill it is necessary to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
  whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
  if (distance < (position + current_size - c[dir_bit]) * (position + current_size - c[dir_bit])) { /* Search half of quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
    for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
      mlib_s32 qq = opposite_quadrants[dir_bit][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
      if (node->tag & (1 << qq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
        mlib_s32 newindex = node->contents.index[qq];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
        mlib_u32 newpalc0, newpalc1, newpalc2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
        newpalc0 = base[0][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
        newpalc1 = base[1][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
        newpalc2 = base[2][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
        newdistance = FIND_DISTANCE_3(c[0], newpalc0, c[1], newpalc1, c[2], newpalc2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
      else if (node->contents.quadrants[qq])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
        distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
          mlib_search_quadrant_part_to_left_U8_3(node->contents.quadrants[qq],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                                                 distance, found_color, c, base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                                                 position, pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
  else {                                    /* Search whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
    mlib_s32 mask = 1 << dir_bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
    for (i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
      if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
        mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
        mlib_u32 newpalc0, newpalc1, newpalc2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
        newpalc0 = base[0][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
        newpalc1 = base[1][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        newpalc2 = base[2][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        newdistance = FIND_DISTANCE_3(c[0], newpalc0, c[1], newpalc1, c[2], newpalc2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
      else if (node->contents.quadrants[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
        if (i & mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
          /* This quadrant may require partial checking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
            mlib_search_quadrant_part_to_left_U8_3(node->contents.quadrants[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                                                   distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
                                                   base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                                                   position + current_size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                                                   pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
          /* Here we should check all */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
            mlib_search_quadrant_U8_3(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                                      found_color, c[0], c[1], c[2], base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
mlib_u32 mlib_search_quadrant_part_to_right_U8_3(struct lut_node_3 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                                                 mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                                                  mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                                                 const mlib_u32    *c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
                                                 const mlib_u8     **base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                                                 mlib_u32          position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
                                                 mlib_s32          pass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
                                                 mlib_s32          dir_bit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
  mlib_u32 current_size = 1 << pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
  static mlib_s32 opposite_quadrants[3][4] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
    {1, 3, 5, 7},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
    {2, 3, 6, 7},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
    {4, 5, 6, 7}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
/* Search only quadrant's half untill it is necessary to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
  whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
  if (distance <= (c[dir_bit] - position - current_size) * (c[dir_bit] - position - current_size)) { /* Search half of quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
    for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
      mlib_s32 qq = opposite_quadrants[dir_bit][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
      if (node->tag & (1 << qq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
        mlib_s32 newindex = node->contents.index[qq];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
        mlib_u32 newpalc0, newpalc1, newpalc2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        newpalc0 = base[0][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        newpalc1 = base[1][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
        newpalc2 = base[2][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
        newdistance = FIND_DISTANCE_3(c[0], newpalc0, c[1], newpalc1, c[2], newpalc2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
      else if (node->contents.quadrants[qq])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
        distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
          mlib_search_quadrant_part_to_right_U8_3(node->contents.quadrants[qq],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                                                  distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                                                  base, position + current_size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                                                  pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
  else {                                    /* Search whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
    mlib_s32 mask = 1 << dir_bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
    for (i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
      if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
        mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
        mlib_u32 newpalc0, newpalc1, newpalc2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
        newpalc0 = base[0][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
        newpalc1 = base[1][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
        newpalc2 = base[2][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
        newdistance = FIND_DISTANCE_3(c[0], newpalc0, c[1], newpalc1, c[2], newpalc2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
      else if (node->contents.quadrants[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
        if (i & mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
          /* Here we should check all */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
            mlib_search_quadrant_U8_3(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                                      found_color, c[0], c[1], c[2], base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
          /* This quadrant may require partial checking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
            mlib_search_quadrant_part_to_right_U8_3(node->contents.quadrants[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                                                    distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                                                    base, position, pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
mlib_u32 mlib_search_quadrant_S16_3(struct lut_node_3 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                                    mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                                     mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                                    mlib_u32          c0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
                                    mlib_u32          c1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                                    mlib_u32          c2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
                                    const mlib_s16    **base)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
  for (i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
    if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
      /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
      mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
      mlib_u32 newpalc0, newpalc1, newpalc2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
      mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
      newpalc0 = base[0][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
      newpalc1 = base[1][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
      newpalc2 = base[2][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
      newdistance = FIND_DISTANCE_3(c0, newpalc0, c1, newpalc1, c2, newpalc2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
      if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
        *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
        distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
    else if (node->contents.quadrants[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
      distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
        mlib_search_quadrant_S16_3(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                                   found_color, c0, c1, c2, base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
mlib_u32 mlib_search_quadrant_part_to_left_S16_3(struct lut_node_3 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                                                 mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                                                  mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
                                                 const mlib_u32    *c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
                                                 const mlib_s16    **base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
                                                 mlib_u32          position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                                                 mlib_s32          pass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
                                                 mlib_s32          dir_bit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
  mlib_u32 current_size = 1 << pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
  static mlib_s32 opposite_quadrants[3][4] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
    {0, 2, 4, 6},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
    {0, 1, 4, 5},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
    {0, 1, 2, 3}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
/* Search only quadrant's half untill it is necessary to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
  whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
  if (distance < (((position + current_size - c[dir_bit]) * (position + current_size - c[dir_bit])) >> 2)) { /* Search half of quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
    for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
      mlib_s32 qq = opposite_quadrants[dir_bit][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
      if (node->tag & (1 << qq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
        mlib_s32 newindex = node->contents.index[qq];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
        mlib_u32 newpalc0, newpalc1, newpalc2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
        newpalc0 = base[0][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        newpalc1 = base[1][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
        newpalc2 = base[2][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
        newdistance = FIND_DISTANCE_3(c[0], newpalc0, c[1], newpalc1, c[2], newpalc2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
      else if (node->contents.quadrants[qq])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
        distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
          mlib_search_quadrant_part_to_left_S16_3(node->contents.quadrants[qq],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
                                                  distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                                                  base, position, pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
  else {                                    /* Search whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
    mlib_s32 mask = 1 << dir_bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
    for (i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
      if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
        mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
        mlib_u32 newpalc0, newpalc1, newpalc2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
        newpalc0 = base[0][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
        newpalc1 = base[1][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
        newpalc2 = base[2][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
        newdistance = FIND_DISTANCE_3(c[0], newpalc0, c[1], newpalc1, c[2], newpalc2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
      else if (node->contents.quadrants[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
        if (i & mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
          /* This quadrant may require partial checking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
            mlib_search_quadrant_part_to_left_S16_3(node->contents.quadrants[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
                                                    distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
                                                    base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
                                                    position + current_size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
                                                    pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
          /* Here we should check all */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
            mlib_search_quadrant_S16_3(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
                                       found_color, c[0], c[1], c[2], base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
mlib_u32 mlib_search_quadrant_part_to_right_S16_3(struct lut_node_3 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
                                                  mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
                                                   mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
                                                  const mlib_u32    *c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
                                                  const mlib_s16    **base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
                                                  mlib_u32          position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
                                                  mlib_s32          pass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
                                                  mlib_s32          dir_bit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
  mlib_u32 current_size = 1 << pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
  static mlib_s32 opposite_quadrants[3][4] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
    {1, 3, 5, 7},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
    {2, 3, 6, 7},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
    {4, 5, 6, 7}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
/* Search only quadrant's half untill it is necessary to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
  whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
  if (distance <= (((c[dir_bit] - position - current_size) * (c[dir_bit] - position - current_size)) >> 2)) { /* Search half of quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
    for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
      mlib_s32 qq = opposite_quadrants[dir_bit][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
      if (node->tag & (1 << qq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
        mlib_s32 newindex = node->contents.index[qq];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
        mlib_u32 newpalc0, newpalc1, newpalc2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
        newpalc0 = base[0][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
        newpalc1 = base[1][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
        newpalc2 = base[2][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
        newdistance = FIND_DISTANCE_3(c[0], newpalc0, c[1], newpalc1, c[2], newpalc2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
      else if (node->contents.quadrants[qq])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
          mlib_search_quadrant_part_to_right_S16_3(node->contents.quadrants[qq],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
                                                   distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
                                                   base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
                                                   position + current_size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
                                                   pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
  else {                                    /* Search whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
    mlib_s32 mask = 1 << dir_bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
    for (i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
      if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
        mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
        mlib_u32 newpalc0, newpalc1, newpalc2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
        newpalc0 = base[0][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        newpalc1 = base[1][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
        newpalc2 = base[2][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
        newdistance = FIND_DISTANCE_3(c[0], newpalc0, c[1], newpalc1, c[2], newpalc2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
      else if (node->contents.quadrants[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
        if (i & mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
          /* Here we should check all */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
            mlib_search_quadrant_S16_3(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
                                       found_color, c[0], c[1], c[2], base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
          /* This quadrant may require partial checking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
            mlib_search_quadrant_part_to_right_S16_3(node->contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
                                                     quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
                                                     found_color, c, base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
                                                     position, pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
mlib_u32 mlib_search_quadrant_U8_4(struct lut_node_4 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
                                   mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
                                    mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
                                   mlib_u32          c0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
                                   mlib_u32          c1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
                                   mlib_u32          c2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
                                   mlib_u32          c3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                                   const mlib_u8     **base)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
  for (i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
    if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
      /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
      mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
      mlib_u32 newpalc0, newpalc1, newpalc2, newpalc3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
      mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
      newpalc0 = base[0][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
      newpalc1 = base[1][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
      newpalc2 = base[2][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
      newpalc3 = base[3][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
      newdistance = FIND_DISTANCE_4(c0, newpalc0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
                                    c1, newpalc1, c2, newpalc2, c3, newpalc3, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
      if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
        *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
        distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
    else if (node->contents.quadrants[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
      distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
        mlib_search_quadrant_U8_4(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
                                  found_color, c0, c1, c2, c3, base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
mlib_u32 mlib_search_quadrant_part_to_left_U8_4(struct lut_node_4 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
                                                mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
                                                 mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
                                                const mlib_u32    *c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
                                                const mlib_u8     **base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
                                                mlib_u32          position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
                                                mlib_s32          pass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
                                                mlib_s32          dir_bit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
  mlib_u32 current_size = 1 << pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
  static mlib_s32 opposite_quadrants[4][8] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
    {0, 2, 4, 6, 8, 10, 12, 14},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
    {0, 1, 4, 5, 8, 9, 12, 13},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
    {0, 1, 2, 3, 8, 9, 10, 11},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
    {0, 1, 2, 3, 4, 5, 6, 7}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
/* Search only quadrant's half untill it is necessary to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
  whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
  if (distance < (position + current_size - c[dir_bit]) * (position + current_size - c[dir_bit])) { /* Search half of quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
    for (i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
      mlib_s32 qq = opposite_quadrants[dir_bit][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
      if (node->tag & (1 << qq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
        mlib_s32 newindex = node->contents.index[qq];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
        mlib_u32 newpalc0, newpalc1, newpalc2, newpalc3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
        newpalc0 = base[0][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
        newpalc1 = base[1][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
        newpalc2 = base[2][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
        newpalc3 = base[3][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
        newdistance = FIND_DISTANCE_4(c[0], newpalc0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
                                      c[1], newpalc1, c[2], newpalc2, c[3], newpalc3, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
      else if (node->contents.quadrants[qq])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
        distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
          mlib_search_quadrant_part_to_left_U8_4(node->contents.quadrants[qq],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
                                                 distance, found_color, c, base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
                                                 position, pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
  else {                                    /* Search whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
    mlib_s32 mask = 1 << dir_bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
    for (i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
      if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
        mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
        mlib_u32 newpalc0, newpalc1, newpalc2, newpalc3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
        newpalc0 = base[0][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
        newpalc1 = base[1][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
        newpalc2 = base[2][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
        newpalc3 = base[3][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
        newdistance = FIND_DISTANCE_4(c[0], newpalc0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
                                      c[1], newpalc1, c[2], newpalc2, c[3], newpalc3, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
      else if (node->contents.quadrants[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
        if (i & mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
          /* This quadrant may require partial checking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
            mlib_search_quadrant_part_to_left_U8_4(node->contents.quadrants[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
                                                   distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
                                                   base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
                                                   position + current_size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
                                                   pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
          /* Here we should check all */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
            mlib_search_quadrant_U8_4(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
                                      found_color, c[0], c[1], c[2], c[3], base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
mlib_u32 mlib_search_quadrant_part_to_right_U8_4(struct lut_node_4 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
                                                 mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
                                                  mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
                                                 const mlib_u32    *c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
                                                 const mlib_u8     **base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
                                                 mlib_u32          position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
                                                 mlib_s32          pass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
                                                 mlib_s32          dir_bit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
  mlib_u32 current_size = 1 << pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
  static mlib_s32 opposite_quadrants[4][8] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
    {1, 3, 5, 7, 9, 11, 13, 15},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
    {2, 3, 6, 7, 10, 11, 14, 15},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
    {4, 5, 6, 7, 12, 13, 14, 15},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
    {8, 9, 10, 11, 12, 13, 14, 15}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
/* Search only quadrant's half untill it is necessary to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
  whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
  if (distance <= (c[dir_bit] - position - current_size) * (c[dir_bit] - position - current_size)) { /* Search half of quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
    for (i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
      mlib_s32 qq = opposite_quadrants[dir_bit][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
      if (node->tag & (1 << qq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
        mlib_s32 newindex = node->contents.index[qq];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
        mlib_u32 newpalc0, newpalc1, newpalc2, newpalc3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
        newpalc0 = base[0][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
        newpalc1 = base[1][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
        newpalc2 = base[2][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
        newpalc3 = base[3][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
        newdistance = FIND_DISTANCE_4(c[0], newpalc0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
                                      c[1], newpalc1, c[2], newpalc2, c[3], newpalc3, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
      else if (node->contents.quadrants[qq])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
        distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
          mlib_search_quadrant_part_to_right_U8_4(node->contents.quadrants[qq],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
                                                  distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
                                                  base, position + current_size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
                                                  pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
  else {                                    /* Search whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
    mlib_s32 mask = 1 << dir_bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
    for (i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
      if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
        mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
        mlib_u32 newpalc0, newpalc1, newpalc2, newpalc3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
        newpalc0 = base[0][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
        newpalc1 = base[1][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
        newpalc2 = base[2][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
        newpalc3 = base[3][newindex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
        newdistance = FIND_DISTANCE_4(c[0], newpalc0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                                      c[1], newpalc1, c[2], newpalc2, c[3], newpalc3, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
      else if (node->contents.quadrants[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
        if (i & mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
          /* Here we should check all */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
            mlib_search_quadrant_U8_4(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
                                      found_color, c[0], c[1], c[2], c[3], base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
          /* This quadrant may require partial checking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
            mlib_search_quadrant_part_to_right_U8_4(node->contents.quadrants[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
                                                    distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
                                                    base, position, pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
mlib_u32 mlib_search_quadrant_S16_4(struct lut_node_4 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
                                    mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
                                     mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
                                    mlib_u32          c0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
                                    mlib_u32          c1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
                                    mlib_u32          c2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
                                    mlib_u32          c3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
                                    const mlib_s16    **base)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
  for (i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
    if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
      /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
      mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
      mlib_u32 newpalc0, newpalc1, newpalc2, newpalc3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
      mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
      newpalc0 = base[0][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
      newpalc1 = base[1][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
      newpalc2 = base[2][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
      newpalc3 = base[3][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
      newdistance = FIND_DISTANCE_4(c0, newpalc0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
                                    c1, newpalc1, c2, newpalc2, c3, newpalc3, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
      if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
        *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
        distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
    else if (node->contents.quadrants[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
      distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
        mlib_search_quadrant_S16_4(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
                                   found_color, c0, c1, c2, c3, base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
mlib_u32 mlib_search_quadrant_part_to_left_S16_4(struct lut_node_4 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
                                                 mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
                                                  mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
                                                 const mlib_u32    *c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
                                                 const mlib_s16    **base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
                                                 mlib_u32          position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
                                                 mlib_s32          pass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
                                                 mlib_s32          dir_bit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
  mlib_u32 current_size = 1 << pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
  static mlib_s32 opposite_quadrants[4][8] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
    {0, 2, 4, 6, 8, 10, 12, 14},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
    {0, 1, 4, 5, 8, 9, 12, 13},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
    {0, 1, 2, 3, 8, 9, 10, 11},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
    {0, 1, 2, 3, 4, 5, 6, 7}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
/* Search only quadrant's half untill it is necessary to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
  whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
  if (distance < (((position + current_size - c[dir_bit]) * (position + current_size - c[dir_bit])) >> 2)) { /* Search half of quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
    for (i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
      mlib_s32 qq = opposite_quadrants[dir_bit][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
      if (node->tag & (1 << qq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
        mlib_s32 newindex = node->contents.index[qq];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
        mlib_u32 newpalc0, newpalc1, newpalc2, newpalc3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
        newpalc0 = base[0][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
        newpalc1 = base[1][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
        newpalc2 = base[2][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
        newpalc3 = base[3][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
        newdistance = FIND_DISTANCE_4(c[0], newpalc0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
                                      c[1], newpalc1, c[2], newpalc2, c[3], newpalc3, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
      else if (node->contents.quadrants[qq])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
        distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
          mlib_search_quadrant_part_to_left_S16_4(node->contents.quadrants[qq],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
                                                  distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                                                  base, position, pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
  else {                                    /* Search whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
    mlib_s32 mask = 1 << dir_bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
    for (i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
      if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
        mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
        mlib_u32 newpalc0, newpalc1, newpalc2, newpalc3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
        newpalc0 = base[0][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
        newpalc1 = base[1][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
        newpalc2 = base[2][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
        newpalc3 = base[3][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
        newdistance = FIND_DISTANCE_4(c[0], newpalc0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
                                      c[1], newpalc1, c[2], newpalc2, c[3], newpalc3, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
      else if (node->contents.quadrants[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
        if (i & mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
          /* This quadrant may require partial checking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
            mlib_search_quadrant_part_to_left_S16_4(node->contents.quadrants[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
                                                    distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
                                                    base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
                                                    position + current_size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
                                                    pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
          /* Here we should check all */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
            mlib_search_quadrant_S16_4(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
                                       found_color, c[0], c[1], c[2], c[3], base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
mlib_u32 mlib_search_quadrant_part_to_right_S16_4(struct lut_node_4 *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
                                                  mlib_u32          distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
                                                   mlib_s32    *found_color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
                                                  const mlib_u32    *c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
                                                  const mlib_s16    **base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
                                                  mlib_u32          position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
                                                  mlib_s32          pass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
                                                  mlib_s32          dir_bit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
  mlib_u32 current_size = 1 << pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
  static mlib_s32 opposite_quadrants[4][8] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
    {1, 3, 5, 7, 9, 11, 13, 15},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
    {2, 3, 6, 7, 10, 11, 14, 15},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
    {4, 5, 6, 7, 12, 13, 14, 15},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
    {8, 9, 10, 11, 12, 13, 14, 15}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
/* Search only quadrant's half untill it is necessary to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
  whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
  if (distance <= (((c[dir_bit] - position - current_size) * (c[dir_bit] - position - current_size)) >> 2)) { /* Search half of quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
    for (i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
      mlib_s32 qq = opposite_quadrants[dir_bit][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
      if (node->tag & (1 << qq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
        mlib_s32 newindex = node->contents.index[qq];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
        mlib_u32 newpalc0, newpalc1, newpalc2, newpalc3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
        newpalc0 = base[0][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
        newpalc1 = base[1][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
        newpalc2 = base[2][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
        newpalc3 = base[3][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
        newdistance = FIND_DISTANCE_4(c[0], newpalc0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
                                      c[1], newpalc1, c[2], newpalc2, c[3], newpalc3, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
      else if (node->contents.quadrants[qq])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
        distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
          mlib_search_quadrant_part_to_right_S16_4(node->contents.quadrants[qq],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
                                                   distance, found_color, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
                                                   base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
                                                   position + current_size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
                                                   pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
  else {                                    /* Search whole quadrant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
    mlib_s32 mask = 1 << dir_bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
    for (i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
      if (node->tag & (1 << i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
        /* Here is alone color cell. Check the distance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
        mlib_s32 newindex = node->contents.index[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
        mlib_u32 newpalc0, newpalc1, newpalc2, newpalc3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
        mlib_u32 newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
        newpalc0 = base[0][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
        newpalc1 = base[1][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
        newpalc2 = base[2][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
        newpalc3 = base[3][newindex] - MLIB_S16_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
        newdistance = FIND_DISTANCE_4(c[0], newpalc0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
                                      c[1], newpalc1, c[2], newpalc2, c[3], newpalc3, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
        if (distance > newdistance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
          *found_color = newindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
          distance = newdistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
      else if (node->contents.quadrants[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
        if (i & mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
          /* Here we should check all */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
            mlib_search_quadrant_S16_4(node->contents.quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
                                       found_color, c[0], c[1], c[2], c[3], base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
          /* This quadrant may require partial checking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
          distance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
            mlib_search_quadrant_part_to_right_S16_4(node->contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
                                                     quadrants[i], distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
                                                     found_color, c, base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
                                                     position, pass - 1, dir_bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
  return distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
#define TAB_SIZE_mlib_u8   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
#define TAB_SIZE_mlib_s16 1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
#define SRC_mlib_u8(i)    src[i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
#define SRC_mlib_s16(i)   (((mlib_u16*)src)[i] >> 6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
#define DIMENSIONS_SEARCH_3(STYPE, DTYPE, STEP)                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
{                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
  DTYPE  *tab0 = ((mlib_colormap *)state)->table;               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
  DTYPE  *tab1 = tab0 + TAB_SIZE_##STYPE;                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
  DTYPE  *tab2 = tab1 + TAB_SIZE_##STYPE;                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
  mlib_s32 i;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
  for (i = 0; i < length; i++) {                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
    dst[i] = tab0[SRC_##STYPE(0)] + tab1[SRC_##STYPE(1)] +      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
             tab2[SRC_##STYPE(2)];                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
    src += STEP;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
  }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
#define DIMENSIONS_SEARCH_4(STYPE, DTYPE)                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
{                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
  DTYPE  *tab0 = ((mlib_colormap *)state)->table;               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
  DTYPE  *tab1 = tab0 + TAB_SIZE_##STYPE;                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
  DTYPE  *tab2 = tab1 + TAB_SIZE_##STYPE;                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
  DTYPE  *tab3 = tab2 + TAB_SIZE_##STYPE;                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
  mlib_s32 i;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
                                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
  for (i = 0; i < length; i++) {                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
    dst[i] = tab0[SRC_##STYPE(0)] + tab1[SRC_##STYPE(1)] +      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
             tab2[SRC_##STYPE(2)] + tab3[SRC_##STYPE(3)];       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
    src += 4;                                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
  }                                                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
void mlib_ImageColorTrue2IndexLine_U8_U8_3(const mlib_u8 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
                                           mlib_u8       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
                                           mlib_s32      length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
                                           const void    *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
#if LUT_BYTE_COLORS_3CHANNELS <= 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
        mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
        BINARY_TREE_SEARCH_3(U8, mlib_u8, 8, (MLIB_U8_MAX + 1), 0, 0, 3, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
#endif /* LUT_BYTE_COLORS_3CHANNELS <= 256 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
        COLOR_CUBE_U8_3_SEARCH(mlib_u8, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
#ifdef USE_VIS_CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
        FIND_NEAREST_U8_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
        FIND_NEAREST_U8_3_C(0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
      DIMENSIONS_SEARCH_3(mlib_u8, mlib_u8, 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
void mlib_ImageColorTrue2IndexLine_U8_U8_3_in_4(const mlib_u8 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
                                                mlib_u8       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
                                                mlib_s32      length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
                                                const void    *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
#if LUT_BYTE_COLORS_3CHANNELS <= 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
        mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
        BINARY_TREE_SEARCH_3(U8, mlib_u8, 8, (MLIB_U8_MAX + 1), 0, 1, 4, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
#endif /* LUT_BYTE_COLORS_3CHANNELS <= 256 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
        COLOR_CUBE_U8_3_SEARCH(mlib_u8, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
#ifdef USE_VIS_CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
        FIND_NEAREST_U8_3_IN4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
        FIND_NEAREST_U8_3_C(1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
      src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
      DIMENSIONS_SEARCH_3(mlib_u8, mlib_u8, 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
void mlib_ImageColorTrue2IndexLine_U8_U8_4(const mlib_u8 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
                                           mlib_u8       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
                                           mlib_s32      length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
                                           const void    *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
#if LUT_BYTE_COLORS_4CHANNELS <= 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
        mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
        BINARY_TREE_SEARCH_4(U8, mlib_u8, 8, (MLIB_U8_MAX + 1), 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
#endif /* LUT_BYTE_COLORS_4CHANNELS <= 256 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
        COLOR_CUBE_U8_4_SEARCH(mlib_u8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
#ifdef USE_VIS_CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
        FIND_NEAREST_U8_4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
        FIND_NEAREST_U8_4_C;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
      DIMENSIONS_SEARCH_4(mlib_u8, mlib_u8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
void mlib_ImageColorTrue2IndexLine_U8_S16_3(const mlib_u8 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
                                            mlib_s16      *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
                                            mlib_s32      length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
                                            const void    *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
  mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
        BINARY_TREE_SEARCH_3(U8, mlib_u8, 8, (MLIB_U8_MAX + 1), 0, 0, 3, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
        switch (s->indexsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
          case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
              COLOR_CUBE_U8_3_SEARCH(mlib_u8, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
          case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
              COLOR_CUBE_U8_3_SEARCH(mlib_s16, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
#ifdef USE_VIS_CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
        FIND_NEAREST_U8_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
        FIND_NEAREST_U8_3_C(0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
      DIMENSIONS_SEARCH_3(mlib_u8, mlib_s16, 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
void mlib_ImageColorTrue2IndexLine_U8_S16_3_in_4(const mlib_u8 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
                                                 mlib_s16      *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
                                                 mlib_s32      length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
                                                 const void    *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
  mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
        BINARY_TREE_SEARCH_3(U8, mlib_u8, 8, (MLIB_U8_MAX + 1), 0, 1, 4, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
        switch (s->indexsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
          case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
              COLOR_CUBE_U8_3_SEARCH(mlib_u8, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
          case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
              COLOR_CUBE_U8_3_SEARCH(mlib_s16, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
#ifdef USE_VIS_CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
        FIND_NEAREST_U8_3_IN4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
        FIND_NEAREST_U8_3_C(1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
      src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
      DIMENSIONS_SEARCH_3(mlib_u8, mlib_s16, 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
void mlib_ImageColorTrue2IndexLine_U8_S16_4(const mlib_u8 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
                                            mlib_s16      *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
                                            mlib_s32      length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
                                            const void    *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
  mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
        BINARY_TREE_SEARCH_4(U8, mlib_u8, 8, (MLIB_U8_MAX + 1), 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
        switch (s->indexsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
          case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
              COLOR_CUBE_U8_4_SEARCH(mlib_u8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
          case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
              COLOR_CUBE_U8_4_SEARCH(mlib_s16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
#ifdef USE_VIS_CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
        FIND_NEAREST_U8_4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
        FIND_NEAREST_U8_4_C;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
      DIMENSIONS_SEARCH_4(mlib_u8, mlib_s16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
void mlib_ImageColorTrue2IndexLine_S16_S16_3(const mlib_s16 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
                                             mlib_s16       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
                                             mlib_s32       length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
                                             const void     *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
  mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
        BINARY_TREE_SEARCH_3(S16, mlib_s16, 16, ((MLIB_S16_MAX + 1) * 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
                             MLIB_S16_MIN, 0, 3, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
        switch (s->indexsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
          case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
              COLOR_CUBE_S16_3_SEARCH(mlib_u8, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
          case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
              COLOR_CUBE_S16_3_SEARCH(mlib_s16, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
        FIND_NEAREST_S16_3(0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
      DIMENSIONS_SEARCH_3(mlib_s16, mlib_s16, 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
void mlib_ImageColorTrue2IndexLine_S16_S16_3_in_4(const mlib_s16 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
                                                  mlib_s16       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
                                                  mlib_s32       length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
                                                  const void     *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
  mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
        BINARY_TREE_SEARCH_3(S16, mlib_s16, 16, ((MLIB_S16_MAX + 1) * 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
                             MLIB_S16_MIN, 1, 4, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
        switch (s->indexsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
          case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
              COLOR_CUBE_S16_3_SEARCH(mlib_u8, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
          case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
              COLOR_CUBE_S16_3_SEARCH(mlib_s16, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4009
        FIND_NEAREST_S16_3(1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4010
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4011
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
      src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
      DIMENSIONS_SEARCH_3(mlib_s16, mlib_s16, 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
void mlib_ImageColorTrue2IndexLine_S16_S16_4(const mlib_s16 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
                                             mlib_s16       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
                                             mlib_s32       length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
                                             const void     *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
  mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
        BINARY_TREE_SEARCH_4(S16, mlib_s16, 16, ((MLIB_S16_MAX + 1) * 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
                             MLIB_S16_MIN, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
        switch (s->indexsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
          case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
              COLOR_CUBE_S16_4_SEARCH(mlib_u8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4046
          case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
              COLOR_CUBE_S16_4_SEARCH(mlib_s16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
        FIND_NEAREST_S16_4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
      DIMENSIONS_SEARCH_4(mlib_s16, mlib_s16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
void mlib_ImageColorTrue2IndexLine_S16_U8_3(const mlib_s16 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
                                            mlib_u8        *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
                                            mlib_s32       length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
                                            const void     *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
#if LUT_SHORT_COLORS_3CHANNELS <= 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
        mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
        BINARY_TREE_SEARCH_3(S16, mlib_s16, 16, ((MLIB_S16_MAX + 1) * 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
                             MLIB_S16_MIN, 0, 3, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
#endif /* LUT_SHORT_COLORS_3CHANNELS <= 256 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
        COLOR_CUBE_S16_3_SEARCH(mlib_u8, 0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
        FIND_NEAREST_S16_3(0, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
      DIMENSIONS_SEARCH_3(mlib_s16, mlib_u8, 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
void mlib_ImageColorTrue2IndexLine_S16_U8_3_in_4(const mlib_s16 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
                                                 mlib_u8        *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
                                                 mlib_s32       length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
                                                 const void     *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
#if LUT_SHORT_COLORS_3CHANNELS <= 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
        mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
        BINARY_TREE_SEARCH_3(S16, mlib_s16, 16, ((MLIB_S16_MAX + 1) * 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
                             MLIB_S16_MIN, 1, 4, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
#endif /* LUT_SHORT_COLORS_3CHANNELS <= 256 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
        COLOR_CUBE_S16_3_SEARCH(mlib_u8, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
        FIND_NEAREST_S16_3(1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
      src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
      DIMENSIONS_SEARCH_3(mlib_s16, mlib_u8, 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
void mlib_ImageColorTrue2IndexLine_S16_U8_4(const mlib_s16 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
                                            mlib_u8        *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
                                            mlib_s32       length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
                                            const void     *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
  mlib_colormap *s = (mlib_colormap *)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
  switch (s->method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
#if LUT_SHORT_COLORS_4CHANNELS <= 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
    case LUT_BINARY_TREE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
        mlib_s32 bits = s->bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
        BINARY_TREE_SEARCH_4(S16, mlib_s16, 16, ((MLIB_S16_MAX + 1) * 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
                             MLIB_S16_MIN, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
#endif /* LUT_SHORT_COLORS_4CHANNELS <= 256 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
    case LUT_COLOR_CUBE_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
        COLOR_CUBE_S16_4_SEARCH(mlib_u8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
    case LUT_STUPID_SEARCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
        FIND_NEAREST_S16_4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
    case LUT_COLOR_DIMENSIONS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
      DIMENSIONS_SEARCH_4(mlib_s16, mlib_u8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
#ifndef VIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
void mlib_c_ImageThresh1_U81_1B(void     *psrc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
                                void     *pdst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
                                mlib_s32 src_stride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
                                mlib_s32 dst_stride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
                                mlib_s32 width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4189
                                mlib_s32 height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4190
                                void     *thresh,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4191
                                void     *ghigh,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
                                void     *glow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
                                mlib_s32 dbit_off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
void mlib_ImageColorTrue2IndexLine_U8_BIT_1(const mlib_u8 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
                                            mlib_u8       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
                                            mlib_s32      bit_offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
                                            mlib_s32      length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4201
                                            const void    *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4202
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4203
  mlib_u8  *lut = ((mlib_colormap *)state)->table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
  mlib_s32 thresh[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
  mlib_s32 ghigh[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
  mlib_s32 glow[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
  thresh[0] = lut[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
  glow[0]  = lut[0] - lut[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
  ghigh[0] = lut[1] - lut[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
  mlib_c_ImageThresh1_U81_1B((void*)src, dst, 0, 0, length, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
                             thresh, ghigh, glow, bit_offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4217
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4219
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4221
void mlib_v_ImageThresh1B_U8_1(const mlib_u8  *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4222
                               mlib_s32       slb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
                               mlib_u8        *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4224
                               mlib_s32       dlb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4225
                               mlib_s32       xsize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4226
                               mlib_s32       ysize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4227
                               mlib_s32       dbit_off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4228
                               const mlib_s32 *th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
                               mlib_s32       hc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
                               mlib_s32       lc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
void mlib_ImageColorTrue2IndexLine_U8_BIT_1(const mlib_u8 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
                                            mlib_u8       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4236
                                            mlib_s32      bit_offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4237
                                            mlib_s32      length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4238
                                            const void    *state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4239
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4240
  mlib_u8  *lut = ((mlib_colormap *)state)->table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4241
  mlib_s32 thresh[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4242
  mlib_s32 ghigh[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
  mlib_s32 glow[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
  thresh[0] = thresh[1] = thresh[2] = thresh[3] = lut[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
  glow[0]  = (lut[1] < lut[0]) ? 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
  ghigh[0] = (lut[1] < lut[0]) ? 0 : 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
  mlib_v_ImageThresh1B_U8_1((void*)src, 0, dst, 0, length, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
                            bit_offset, thresh, ghigh[0], glow[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4252
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4254
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4256
#endif