i420_rgb.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:20k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * i420_rgb.c : YUV to bitmap RGB conversion module for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2000, 2001, 2004, 2008 the VideoLAN team
  5.  * $Id: 1d56f9d5f1ce8e44ec03bd7dfbe6a8a7014b5e72 $
  6.  *
  7.  * Authors: Sam Hocevar <sam@zoy.org>
  8.  *          Damien Fouilleul <damienf@videolan.org>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <math.h>                                            /* exp(), pow() */
  31. #include <vlc_common.h>
  32. #include <vlc_plugin.h>
  33. #include <vlc_filter.h>
  34. #include <vlc_vout.h>
  35. #include "i420_rgb.h"
  36. #if defined (MODULE_NAME_IS_i420_rgb)
  37. #   include "i420_rgb_c.h"
  38. #endif
  39. /*****************************************************************************
  40.  * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
  41.  *****************************************************************************/
  42. #define RGB2PIXEL( p_filter, i_r, i_g, i_b )                 
  43.     (((((uint32_t)i_r) >> p_filter->fmt_out.video.i_rrshift) 
  44.                        << p_filter->fmt_out.video.i_lrshift) 
  45.    | ((((uint32_t)i_g) >> p_filter->fmt_out.video.i_rgshift) 
  46.                        << p_filter->fmt_out.video.i_lgshift) 
  47.    | ((((uint32_t)i_b) >> p_filter->fmt_out.video.i_rbshift) 
  48.                        << p_filter->fmt_out.video.i_lbshift))
  49. /*****************************************************************************
  50.  * Local and extern prototypes.
  51.  *****************************************************************************/
  52. static int  Activate   ( vlc_object_t * );
  53. static void Deactivate ( vlc_object_t * );
  54. #if defined (MODULE_NAME_IS_i420_rgb)
  55. static void SetGammaTable       ( int *pi_table, double f_gamma );
  56. static void SetYUV              ( filter_t * );
  57. static void Set8bppPalette      ( filter_t *, uint8_t * );
  58. #endif
  59. /*****************************************************************************
  60.  * Module descriptor.
  61.  *****************************************************************************/
  62. vlc_module_begin ()
  63. #if defined (MODULE_NAME_IS_i420_rgb)
  64.     set_description( N_("I420,IYUV,YV12 to "
  65.                        "RGB2,RV15,RV16,RV24,RV32 conversions") )
  66.     set_capability( "video filter2", 80 )
  67. #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
  68.     set_description( N_( "MMX I420,IYUV,YV12 to "
  69.                         "RV15,RV16,RV24,RV32 conversions") )
  70.     set_capability( "video filter2", 100 )
  71.     add_requirement( MMX )
  72. #elif defined (MODULE_NAME_IS_i420_rgb_sse2)
  73.     set_description( N_( "SSE2 I420,IYUV,YV12 to "
  74.                         "RV15,RV16,RV24,RV32 conversions") )
  75.     set_capability( "video filter2", 120 )
  76.     add_requirement( SSE2 )
  77. #endif
  78.     set_callbacks( Activate, Deactivate )
  79. vlc_module_end ()
  80. /*****************************************************************************
  81.  * Activate: allocate a chroma function
  82.  *****************************************************************************
  83.  * This function allocates and initializes a chroma function
  84.  *****************************************************************************/
  85. static int Activate( vlc_object_t *p_this )
  86. {
  87.     filter_t *p_filter = (filter_t *)p_this;
  88. #if defined (MODULE_NAME_IS_i420_rgb)
  89.     size_t i_tables_size;
  90. #endif
  91.     if( p_filter->fmt_out.video.i_width & 1
  92.      || p_filter->fmt_out.video.i_height & 1 )
  93.     {
  94.         return VLC_EGENERIC;
  95.     }
  96.     switch( p_filter->fmt_in.video.i_chroma )
  97.     {
  98.         case VLC_FOURCC('Y','V','1','2'):
  99.         case VLC_FOURCC('I','4','2','0'):
  100.         case VLC_FOURCC('I','Y','U','V'):
  101.             switch( p_filter->fmt_out.video.i_chroma )
  102.             {
  103. #if defined (MODULE_NAME_IS_i420_rgb)
  104.                 case VLC_FOURCC('R','G','B','2'):
  105.                     p_filter->pf_video_filter = I420_RGB8_Filter;
  106.                     break;
  107. #endif
  108.                 case VLC_FOURCC('R','V','1','5'):
  109.                 case VLC_FOURCC('R','V','1','6'):
  110. #if ! defined (MODULE_NAME_IS_i420_rgb)
  111.                     /* If we don't have support for the bitmasks, bail out */
  112.                     if( ( p_filter->fmt_out.video.i_rmask == 0x7c00
  113.                        && p_filter->fmt_out.video.i_gmask == 0x03e0
  114.                        && p_filter->fmt_out.video.i_bmask == 0x001f ) )
  115.                     {
  116.                         /* R5G5B6 pixel format */
  117.                         msg_Dbg(p_this, "RGB pixel format is R5G5B5");
  118.                         p_filter->pf_video_filter = I420_R5G5B5_Filter;
  119.                     }
  120.                     else if( ( p_filter->fmt_out.video.i_rmask == 0xf800
  121.                             && p_filter->fmt_out.video.i_gmask == 0x07e0
  122.                             && p_filter->fmt_out.video.i_bmask == 0x001f ) )
  123.                     {
  124.                         /* R5G6B5 pixel format */
  125.                         msg_Dbg(p_this, "RGB pixel format is R5G6B5");
  126.                         p_filter->pf_video_filter = I420_R5G6B5_Filter;
  127.                     }
  128.                     else
  129.                         return VLC_EGENERIC;
  130. #else
  131.                     // generic C chroma converter */
  132.                     p_filter->pf_video_filter = I420_RGB16_Filter;
  133. #endif
  134.                     break;
  135. #if 0
  136.                 /* Hmmm, is there only X11 using 32bits per pixel for RV24 ? */
  137.                 case VLC_FOURCC('R','V','2','4'):
  138. #endif
  139.                 case VLC_FOURCC('R','V','3','2'):
  140. #if ! defined (MODULE_NAME_IS_i420_rgb)
  141.                     /* If we don't have support for the bitmasks, bail out */
  142.                     if( p_filter->fmt_out.video.i_rmask == 0x00ff0000
  143.                      && p_filter->fmt_out.video.i_gmask == 0x0000ff00
  144.                      && p_filter->fmt_out.video.i_bmask == 0x000000ff )
  145.                     {
  146.                         /* A8R8G8B8 pixel format */
  147.                         msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
  148.                         p_filter->pf_video_filter = I420_A8R8G8B8_Filter;
  149.                     }
  150.                     else if( p_filter->fmt_out.video.i_rmask == 0xff000000
  151.                           && p_filter->fmt_out.video.i_gmask == 0x00ff0000
  152.                           && p_filter->fmt_out.video.i_bmask == 0x0000ff00 )
  153.                     {
  154.                         /* R8G8B8A8 pixel format */
  155.                         msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
  156.                         p_filter->pf_video_filter = I420_R8G8B8A8_Filter;
  157.                     }
  158.                     else if( p_filter->fmt_out.video.i_rmask == 0x0000ff00
  159.                           && p_filter->fmt_out.video.i_gmask == 0x00ff0000
  160.                           && p_filter->fmt_out.video.i_bmask == 0xff000000 )
  161.                     {
  162.                         /* B8G8R8A8 pixel format */
  163.                         msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
  164.                         p_filter->pf_video_filter = I420_B8G8R8A8_Filter;
  165.                     }
  166.                     else if( p_filter->fmt_out.video.i_rmask == 0x000000ff
  167.                           && p_filter->fmt_out.video.i_gmask == 0x0000ff00
  168.                           && p_filter->fmt_out.video.i_bmask == 0x00ff0000 )
  169.                     {
  170.                         /* A8B8G8R8 pixel format */
  171.                         msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
  172.                         p_filter->pf_video_filter = I420_A8B8G8R8_Filter;
  173.                     }
  174.                     else
  175.                         return VLC_EGENERIC;
  176. #else
  177.                     /* generic C chroma converter */
  178.                     p_filter->pf_video_filter = I420_RGB32_Filter;
  179. #endif
  180.                     break;
  181.                 default:
  182.                     return VLC_EGENERIC;
  183.             }
  184.             break;
  185.         default:
  186.             return VLC_EGENERIC;
  187.     }
  188.     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
  189.     if( p_filter->p_sys == NULL )
  190.     {
  191.         return VLC_EGENERIC;
  192.     }
  193.     switch( p_filter->fmt_out.video.i_chroma )
  194.     {
  195. #if defined (MODULE_NAME_IS_i420_rgb)
  196.         case VLC_FOURCC('R','G','B','2'):
  197.             p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
  198.             break;
  199. #endif
  200.         case VLC_FOURCC('R','V','1','5'):
  201.         case VLC_FOURCC('R','V','1','6'):
  202.             p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
  203.             break;
  204.         case VLC_FOURCC('R','V','2','4'):
  205.         case VLC_FOURCC('R','V','3','2'):
  206.             p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
  207.             break;
  208.         default:
  209.             p_filter->p_sys->p_buffer = NULL;
  210.             break;
  211.     }
  212.     if( p_filter->p_sys->p_buffer == NULL )
  213.     {
  214.         free( p_filter->p_sys );
  215.         return VLC_EGENERIC;
  216.     }
  217.     p_filter->p_sys->p_offset = malloc( p_filter->fmt_out.video.i_width
  218.                     * ( ( p_filter->fmt_out.video.i_chroma
  219.                            == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
  220.                     * sizeof( int ) );
  221.     if( p_filter->p_sys->p_offset == NULL )
  222.     {
  223.         free( p_filter->p_sys->p_buffer );
  224.         free( p_filter->p_sys );
  225.         return VLC_EGENERIC;
  226.     }
  227. #if defined (MODULE_NAME_IS_i420_rgb)
  228.     switch( p_filter->fmt_out.video.i_chroma )
  229.     {
  230.     case VLC_FOURCC('R','G','B','2'):
  231.         i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
  232.         break;
  233.     case VLC_FOURCC('R','V','1','5'):
  234.     case VLC_FOURCC('R','V','1','6'):
  235.         i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
  236.         break;
  237.     default: /* RV24, RV32 */
  238.         i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
  239.         break;
  240.     }
  241.     p_filter->p_sys->p_base = malloc( i_tables_size );
  242.     if( p_filter->p_sys->p_base == NULL )
  243.     {
  244.         free( p_filter->p_sys->p_offset );
  245.         free( p_filter->p_sys->p_buffer );
  246.         free( p_filter->p_sys );
  247.         return -1;
  248.     }
  249.     SetYUV( p_filter );
  250. #endif
  251.     return 0;
  252. }
  253. /*****************************************************************************
  254.  * Deactivate: free the chroma function
  255.  *****************************************************************************
  256.  * This function frees the previously allocated chroma function
  257.  *****************************************************************************/
  258. static void Deactivate( vlc_object_t *p_this )
  259. {
  260.     filter_t *p_filter = (filter_t *)p_this;
  261. #if defined (MODULE_NAME_IS_i420_rgb)
  262.     free( p_filter->p_sys->p_base );
  263. #endif
  264.     free( p_filter->p_sys->p_offset );
  265.     free( p_filter->p_sys->p_buffer );
  266.     free( p_filter->p_sys );
  267. }
  268. #if defined (MODULE_NAME_IS_i420_rgb)
  269. VIDEO_FILTER_WRAPPER( I420_RGB8 )
  270. VIDEO_FILTER_WRAPPER( I420_RGB16 )
  271. VIDEO_FILTER_WRAPPER( I420_RGB32 )
  272. #else
  273. VIDEO_FILTER_WRAPPER( I420_R5G5B5 )
  274. VIDEO_FILTER_WRAPPER( I420_R5G6B5 )
  275. VIDEO_FILTER_WRAPPER( I420_A8R8G8B8 )
  276. VIDEO_FILTER_WRAPPER( I420_R8G8B8A8 )
  277. VIDEO_FILTER_WRAPPER( I420_B8G8R8A8 )
  278. VIDEO_FILTER_WRAPPER( I420_A8B8G8R8 )
  279. #endif
  280. #if defined (MODULE_NAME_IS_i420_rgb)
  281. /*****************************************************************************
  282.  * SetGammaTable: return intensity table transformed by gamma curve.
  283.  *****************************************************************************
  284.  * pi_table is a table of 256 entries from 0 to 255.
  285.  *****************************************************************************/
  286. static void SetGammaTable( int *pi_table, double f_gamma )
  287. {
  288.     int i_y;                                               /* base intensity */
  289.     /* Use exp(gamma) instead of gamma */
  290.     f_gamma = exp( f_gamma );
  291.     /* Build gamma table */
  292.     for( i_y = 0; i_y < 256; i_y++ )
  293.     {
  294.         pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
  295.     }
  296. }
  297. /*****************************************************************************
  298.  * SetYUV: compute tables and set function pointers
  299.  *****************************************************************************/
  300. static void SetYUV( filter_t *p_filter )
  301. {
  302.     int          pi_gamma[256];                               /* gamma table */
  303.     volatile int i_index;                                 /* index in tables */
  304.                    /* We use volatile here to work around a strange gcc-3.3.4
  305.                     * optimization bug */
  306.     /* Build gamma table */
  307.     SetGammaTable( pi_gamma, 0 ); //p_filter/*FIXME wasn't used anywhere anyway*/->f_gamma );
  308.     /*
  309.      * Set pointers and build YUV tables
  310.      */
  311.     /* Color: build red, green and blue tables */
  312.     switch( p_filter->fmt_out.video.i_chroma )
  313.     {
  314.     case VLC_FOURCC('R','G','B','2'):
  315.         p_filter->p_sys->p_rgb8 = (uint8_t *)p_filter->p_sys->p_base;
  316.         Set8bppPalette( p_filter, p_filter->p_sys->p_rgb8 );
  317.         break;
  318.     case VLC_FOURCC('R','V','1','5'):
  319.     case VLC_FOURCC('R','V','1','6'):
  320.         p_filter->p_sys->p_rgb16 = (uint16_t *)p_filter->p_sys->p_base;
  321.         for( i_index = 0; i_index < RED_MARGIN; i_index++ )
  322.         {
  323.             p_filter->p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
  324.             p_filter->p_sys->p_rgb16[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
  325.         }
  326.         for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
  327.         {
  328.             p_filter->p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
  329.             p_filter->p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
  330.         }
  331.         for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
  332.         {
  333.             p_filter->p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
  334.             p_filter->p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
  335.         }
  336.         for( i_index = 0; i_index < 256; i_index++ )
  337.         {
  338.             p_filter->p_sys->p_rgb16[RED_OFFSET + i_index] =   RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
  339.             p_filter->p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
  340.             p_filter->p_sys->p_rgb16[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
  341.         }
  342.         break;
  343.     case VLC_FOURCC('R','V','2','4'):
  344.     case VLC_FOURCC('R','V','3','2'):
  345.         p_filter->p_sys->p_rgb32 = (uint32_t *)p_filter->p_sys->p_base;
  346.         for( i_index = 0; i_index < RED_MARGIN; i_index++ )
  347.         {
  348.             p_filter->p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
  349.             p_filter->p_sys->p_rgb32[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
  350.         }
  351.         for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
  352.         {
  353.             p_filter->p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
  354.             p_filter->p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
  355.         }
  356.         for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
  357.         {
  358.             p_filter->p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
  359.             p_filter->p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
  360.         }
  361.         for( i_index = 0; i_index < 256; i_index++ )
  362.         {
  363.             p_filter->p_sys->p_rgb32[RED_OFFSET + i_index] =   RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
  364.             p_filter->p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
  365.             p_filter->p_sys->p_rgb32[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
  366.         }
  367.         break;
  368.     }
  369. }
  370. static void Set8bppPalette( filter_t *p_filter, uint8_t *p_rgb8 )
  371. {
  372.     #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
  373.     int y,u,v;
  374.     int r,g,b;
  375.     int i = 0, j = 0;
  376.     uint16_t *p_cmap_r = p_filter->p_sys->p_rgb_r;
  377.     uint16_t *p_cmap_g = p_filter->p_sys->p_rgb_g;
  378.     uint16_t *p_cmap_b = p_filter->p_sys->p_rgb_b;
  379.     unsigned char p_lookup[PALETTE_TABLE_SIZE];
  380.     /* This loop calculates the intersection of an YUV box and the RGB cube. */
  381.     for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
  382.     {
  383.         for ( u = 0; u <= 256; u += 32 )
  384.         {
  385.             for ( v = 0; v <= 256; v += 32 )
  386.             {
  387.                 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
  388.                 g = y + ( (U_GREEN_COEF*(u-128)
  389.                          + V_GREEN_COEF*(v-128)) >> SHIFT );
  390.                 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
  391.                 if( r >= 0x00 && g >= 0x00 && b >= 0x00
  392.                         && r <= 0xff && g <= 0xff && b <= 0xff )
  393.                 {
  394.                     /* This one should never happen unless someone
  395.                      * fscked up my code */
  396.                     if( j == 256 )
  397.                     {
  398.                         msg_Err( p_filter, "no colors left in palette" );
  399.                         break;
  400.                     }
  401.                     /* Clip the colors */
  402.                     p_cmap_r[ j ] = CLIP( r );
  403.                     p_cmap_g[ j ] = CLIP( g );
  404.                     p_cmap_b[ j ] = CLIP( b );
  405. #if 0
  406.             printf("+++Alloc RGB cmap %d (%d, %d, %d)n", j,
  407.                p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
  408.                p_cmap_b[ j ] >>8);
  409. #endif
  410.                     /* Allocate color */
  411.                     p_lookup[ i ] = 1;
  412.                     p_rgb8[ i++ ] = j;
  413.                     j++;
  414.                 }
  415.                 else
  416.                 {
  417.                     p_lookup[ i ] = 0;
  418.                     p_rgb8[ i++ ] = 0;
  419.                 }
  420.             }
  421.         }
  422.     }
  423.     /* The colors have been allocated, we can set the palette */
  424.     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
  425.     p_filter->fmt_out.video.pf_setpalette( p_filter, p_cmap_r, p_cmap_g, p_cmap_b );*/
  426. #if 0
  427.     /* There will eventually be a way to know which colors
  428.      * couldn't be allocated and try to find a replacement */
  429.     p_vout->i_white_pixel = 0xff;
  430.     p_vout->i_black_pixel = 0x00;
  431.     p_vout->i_gray_pixel = 0x44;
  432.     p_vout->i_blue_pixel = 0x3b;
  433. #endif
  434.     /* This loop allocates colors that got outside the RGB cube */
  435.     for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
  436.     {
  437.         for ( u = 0; u <= 256; u += 32 )
  438.         {
  439.             for ( v = 0; v <= 256; v += 32, i++ )
  440.             {
  441.                 int u2, v2, dist, mindist = 100000000;
  442.                 if( p_lookup[ i ] || y == 0 )
  443.                 {
  444.                     continue;
  445.                 }
  446.                 /* Heavy. yeah. */
  447.                 for( u2 = 0; u2 <= 256; u2 += 32 )
  448.                 {
  449.                     for( v2 = 0; v2 <= 256; v2 += 32 )
  450.                     {
  451.                         j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
  452.                         dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
  453.                         /* Find the nearest color */
  454.                         if( p_lookup[ j ] && dist < mindist )
  455.                         {
  456.                             p_rgb8[ i ] = p_rgb8[ j ];
  457.                             mindist = dist;
  458.                         }
  459.                         j -= 128;
  460.                         /* Find the nearest color */
  461.                         if( p_lookup[ j ] && dist + 128 < mindist )
  462.                         {
  463.                             p_rgb8[ i ] = p_rgb8[ j ];
  464.                             mindist = dist + 128;
  465.                         }
  466.                     }
  467.                 }
  468.             }
  469.         }
  470.     }
  471. }
  472. #endif