LCDP0.c
上传用户:zbk8730
上传日期:2017-08-10
资源大小:12168k
文件大小:4k
源码类别:

uCOS

开发平台:

C/C++

  1. /*
  2. *********************************************************************************************************
  3. *                                                uC/GUI
  4. *                        Universal graphic software for embedded applications
  5. *
  6. *                       (c) Copyright 2002, Micrium Inc., Weston, FL
  7. *                       (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
  8. *
  9. *              礐/GUI is protected by international copyright laws. Knowledge of the
  10. *              source code may not be used to write a similar product. This file may
  11. *              only be used in accordance with a license and should not be redistributed
  12. *              in any way. We appreciate your understanding and fairness.
  13. *
  14. ----------------------------------------------------------------------
  15. File        : LCDP0.C
  16. Purpose     : Color conversion routines for LCD-drivers
  17. ---------------------------END-OF-HEADER------------------------------
  18. */
  19. #include <stdlib.H>
  20. #include "LCD_Protected.h"    /* inter modul definitions */
  21. /*
  22.         *********************************************************
  23.         *                                                       *
  24.         *       calculation macros and support                  *
  25.         *                                                       *
  26.         *********************************************************
  27. */
  28. #if 1  /* Normaly calculate square values */
  29.   #define  SQUARE(Dist) ((U16)Dist) * ((U16)Dist)
  30. #else
  31. #define S(x) ((x)*(x))
  32. #define SQUARES(Base)  S(Base+0),  S(Base+1),  S(Base+2),  S(Base+3), S(Base+4),  S(Base+5),   
  33.                        S(Base+6),  S(Base+7),  S(Base+8),  S(Base+9), S(Base+10), S(Base+11), 
  34.                        S(Base+12), S(Base+13), S(Base+14), S(Base+15)
  35. static const U16 aSquare[] = {
  36.   SQUARES(0*16)
  37.   ,SQUARES(1*16), SQUARES(2*16), SQUARES(3*16)
  38.   ,SQUARES(4*16) ,SQUARES(5*16), SQUARES(6*16), SQUARES(7*16)
  39.   ,SQUARES(8*16) ,SQUARES(9*16), SQUARES(10*16),SQUARES(11*16)
  40.   ,SQUARES(12*16),SQUARES(13*16),SQUARES(14*16),SQUARES(15*16)
  41. };
  42.   #define  SQUARE(Dist) aSquare[Dist]
  43. #endif
  44. /*
  45.         *********************************************************
  46.         *                                                       *
  47.         *       Color conversions for palette based displays    *
  48.         *                                                       *
  49.         *********************************************************
  50. */
  51. static U32 CalcColorDist (LCD_COLOR PalColor, LCD_COLOR  Color) {
  52. /* This routine does not use abs() because we are optimizing for speed ! */
  53.   I16 Dist;
  54.   U32 Sum;
  55.   Dist  = (PalColor&0xff) - (Color&0xff);
  56.   if (Dist < 0)
  57.   Dist = -Dist;
  58.   Sum = SQUARE(Dist);
  59.   Dist  = ((PalColor>>8)&0xff) -  ((Color>>8)&0xff);
  60.   if (Dist < 0)
  61.   Dist = -Dist;
  62.   Sum += SQUARE(Dist);
  63.   Dist  = (PalColor>>16) - (Color>>16);
  64.   if (Dist < 0)
  65.   Dist = -Dist;
  66.   return Sum + SQUARE(Dist);
  67. }
  68.   /*
  69.           *********************************************************
  70.           *                                                       *
  71.           *   Color conversions for 1/2/4/8 bpp color displays    *
  72.           *                                                       *
  73.           *              with table based palette                 *
  74.           *                                                       *
  75.           *********************************************************
  76.   */
  77.   int LCD_Color2Index_0(LCD_COLOR Color, const LCD_PHYSPALETTE* pPhysPal) {
  78.     int i;
  79.     int NumEntries = pPhysPal->NumEntries;
  80.     int BestIndex;
  81.     U32 BestDiff = 0xffffff; /* Initialize to worst match */
  82.     const LCD_COLOR* pPalEntry;
  83. /* Try to find perfect match */
  84.     i=0; pPalEntry = &pPhysPal->pPalEntries[0];
  85.     do {
  86.     if (Color==*(pPalEntry+i))
  87.       return i;
  88.     } while (++i<NumEntries);
  89. /* Find best match */
  90.     i=0; pPalEntry = &pPhysPal->pPalEntries[0];
  91.     do {
  92.       U32 Diff = CalcColorDist (Color, *(pPalEntry+i));
  93.       if (Diff < BestDiff) {
  94.         BestDiff  = Diff;
  95.         BestIndex = i;
  96.       }
  97.     } while (++i<NumEntries);
  98.     return BestIndex;
  99.   }
  100.   LCD_COLOR LCD_Index2Color_0(int Index, const LCD_PHYSPALETTE* pPhysPal) {
  101.     if ((unsigned)Index >= (unsigned) pPhysPal->NumEntries) {
  102.       return 0;     /* Illegal index */
  103.     }
  104.     return *(pPhysPal->pPalEntries+Index);
  105.   }