home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Jeux / demos / crystalPPC.lha / system.cpp < prev    next >
C/C++ Source or Header  |  1998-02-05  |  3KB  |  151 lines

  1. #ifndef DEF_H
  2. #include "def.h"
  3. #endif
  4.  
  5. #ifndef SYSTEM_H
  6. #include "system.h"
  7. #endif
  8.  
  9. #ifndef CONFIG_H
  10. #include "config.h"
  11. #endif
  12.  
  13. char *graphicsData;
  14. colorRGB graphicsPalette[255];
  15. int graphicsPalette_alloc[256];
  16.  
  17. System::System(int argc, char *argv[])
  18. {
  19.   Graph = new Graphics(argc, argv);
  20.   Key = new Keyboard(argc, argv);
  21. }
  22.  
  23. System::~System(void)
  24. {
  25.   Close();
  26. }
  27.  
  28. int System::Open(void)
  29. {
  30.   if(Graph->Open() && Key->Open()) return (1);
  31.   return (0);
  32. }
  33.  
  34. void System::Close(void)
  35. {
  36.   Key->Close();
  37.   Graph->Close();
  38. }
  39.  
  40. // void System::Loop() redefined
  41. /*
  42.  * Get a rgb value.
  43.  */
  44. void GetRGB (int i, int* r, int* g, int* b)
  45. {
  46.   *r = graphicsPalette[i].red ;
  47.   *g = graphicsPalette[i].green;
  48.   *b = graphicsPalette[i].blue;
  49. }
  50.  
  51. /*
  52.  * Find the color best matching the given red/green/blue values.
  53.  */
  54. int worst_r, worst_g, worst_b;
  55. int worst_distance = -1;
  56. int worst2_r, worst2_g, worst2_b;
  57. int worst2_distance = -1;
  58. int worst3_r, worst3_g, worst3_b;
  59. int worst3_distance = -1;
  60. int worst4_r, worst4_g, worst4_b;
  61. int worst4_distance = -1;
  62. int worst5_r, worst5_g, worst5_b;
  63. int worst5_distance = -1;
  64.  
  65. int rgb_stats[256];
  66.  
  67. void dump_rgb_usage_stats ()
  68. {
  69.   int i;
  70.  
  71.   printf ("Print color statistics:\n");
  72.   for (i = 0 ; i < 256 ; i++)
  73.     printf ("Color %d with rgb %d,%d,%d is used %d times.\n", i,
  74.         graphicsPalette[i].red, graphicsPalette[i].green, graphicsPalette[i].blue, rgb_stats[i]);
  75.   printf ("Worst        r,g,b value: %d,%d,%d (distance %d)\n",
  76.       worst_r, worst_g, worst_b, worst_distance);
  77.   printf ("Second Worst r,g,b value: %d,%d,%d (distance %d)\n",
  78.       worst2_r, worst2_g, worst2_b, worst2_distance);
  79.   printf ("Third Worst  r,g,b value: %d,%d,%d (distance %d)\n",
  80.       worst3_r, worst3_g, worst3_b, worst3_distance);
  81.   printf ("Fourth Worst r,g,b value: %d,%d,%d (distance %d)\n",
  82.       worst4_r, worst4_g, worst4_b, worst4_distance);
  83.   printf ("Fifth Worst  r,g,b value: %d,%d,%d (distance %d)\n",
  84.       worst5_r, worst5_g, worst5_b, worst5_distance);
  85.   printf ("Done!\n");
  86. }
  87.  
  88. int find_rgb (int r, int g, int b)
  89. {
  90.   static int first_rgb = TRUE;
  91.   int i;
  92.   int pr, pg, pb;
  93.   int max, best, min_best;
  94.   int dr, dg, db;
  95.  
  96.   if (first_rgb)
  97.   {
  98.     for (i = 0 ; i < 256 ; i++)
  99.       rgb_stats[i] = 0;
  100.     first_rgb = FALSE;
  101.   }
  102.  
  103.   best = 0;
  104.   min_best = 1000;
  105.   for (i = 0 ; i < 256 ; i++)
  106.   {
  107.     if (graphicsPalette_alloc[i])
  108.     {
  109.       pr = graphicsPalette[i].red;
  110.       pg = graphicsPalette[i].green;
  111.       pb = graphicsPalette[i].blue;
  112.       dr = ABS (pr-r);
  113.       dg = ABS (pg-g);
  114.       db = ABS (pb-b);
  115.       max = MAX (dr, dg);
  116.       max = MAX (db, max);
  117.       if (max < min_best)
  118.       {
  119.     best = i;
  120.     min_best = max;
  121.       }
  122.     }
  123.   }
  124.  
  125.   if (min_best > worst_distance)
  126.   {
  127.     worst5_r = worst4_r;
  128.     worst5_g = worst4_g;
  129.     worst5_b = worst4_b;
  130.     worst5_distance = worst4_distance;
  131.     worst4_r = worst3_r;
  132.     worst4_g = worst3_g;
  133.     worst4_b = worst3_b;
  134.     worst4_distance = worst3_distance;
  135.     worst3_r = worst2_r;
  136.     worst3_g = worst2_g;
  137.     worst3_b = worst2_b;
  138.     worst3_distance = worst2_distance;
  139.     worst2_r = worst_r;
  140.     worst2_g = worst_g;
  141.     worst2_b = worst_b;
  142.     worst2_distance = worst_distance;
  143.     worst_r = r;
  144.     worst_g = g;
  145.     worst_b = b;
  146.     worst_distance = min_best;
  147.   }
  148.   rgb_stats[best]++;
  149.   return best;
  150. }
  151.