home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / contrib / seejpeg / seejpeg-.4 / seejpeg- / seejpeg-1.4.3 / cmap.c < prev    next >
C/C++ Source or Header  |  1994-04-05  |  2KB  |  115 lines

  1. /*
  2.  * cmap.c
  3.  *
  4.  * Copyright (C) 1993, 1994 Evan Harris
  5.  *
  6.  * Permission is granted to freely redistribute and modify this code,
  7.  * providing the author(s) get credit for having written it.
  8.  */
  9.  
  10. #include "seejpeg.h"
  11. #include <vga.h>
  12.  
  13.  
  14. #define HASHTABLESIZE 1024
  15. #define CMAPSIZE 256
  16.  
  17. /*
  18.  * We probably need a better hash function, but this is better than
  19.  * nothing
  20.  */
  21. #define HASH(r, g, b) (((r) << 4) ^ ((g) << 2) ^ (b))
  22.  
  23. #define NOENTRY 0x80000000
  24.  
  25. static unsigned long hashtable[HASHTABLESIZE];
  26.  
  27. static unsigned long last_colour;
  28. static int next_cmap_entry, black_seen;
  29.  
  30. void
  31. translate_init()
  32. {
  33.     int i;
  34.  
  35.     last_colour = 0;
  36.     next_cmap_entry = 1;
  37.     black_seen = 0;
  38.  
  39.     for (i = 0; i < HASHTABLESIZE; i++) {
  40.     hashtable[i] = NOENTRY;
  41.     }
  42. }
  43.  
  44.  
  45. static JSAMPLE
  46. lookup_colour(JSAMPLE r, JSAMPLE g, JSAMPLE b)
  47. {
  48.     unsigned long colour;
  49.     int key;
  50.  
  51.     /*
  52.      * VGA colours are 18 bit.
  53.      */
  54.     r >>= 2;
  55.     g >>= 2;
  56.     b >>= 2;
  57.  
  58.     colour = ((r << 20) | (g << 14) | (b << 8));
  59.     if (colour == (last_colour & 0xffffff00)) {
  60.     return (last_colour & 0xff);
  61.     }
  62.  
  63.     key = HASH(r, g, b);
  64.   
  65.     while (hashtable[key] != NOENTRY
  66.        && colour != (hashtable[key] & 0xffffff00)) {
  67.     if (++key == HASHTABLESIZE) {
  68.         key = 0;
  69.     }
  70.     }
  71.     if (hashtable[key] == NOENTRY) {
  72.     if (colour == 0) {
  73.         hashtable[key] = (colour | 0);
  74.         vga_setpalette(0, r, g, b);
  75.         black_seen = 1;
  76.     } else {
  77.         if (next_cmap_entry == CMAPSIZE && !black_seen) {
  78.         next_cmap_entry = 0;
  79.         } else if (next_cmap_entry == CMAPSIZE || next_cmap_entry == 0) {
  80.         error_exit("Colour map full");
  81.         }
  82.  
  83.         hashtable[key] = (colour | next_cmap_entry);
  84.         vga_setpalette(next_cmap_entry, r, g, b);
  85.  
  86.         if (next_cmap_entry > 0) {
  87.         next_cmap_entry++;
  88.         }
  89.     }
  90.     }
  91.     last_colour = hashtable[key];
  92.  
  93.     return (last_colour & 0xff);
  94. }
  95.  
  96.  
  97. void
  98. translate_row(int width, JSAMPARRAY rgb_row, JSAMPARRAY cmap_row)
  99. {
  100.     JSAMPROW inr, ing, inb, out;
  101.     int i;
  102.  
  103.     /*
  104.      * Assume there are three components in the input and one in the output
  105.      */
  106.     inr = rgb_row[0];
  107.     ing = rgb_row[1];
  108.     inb = rgb_row[2];
  109.     out = cmap_row[0];
  110.   
  111.     for (i = 0; i < width; i++) {
  112.     *out++ = lookup_colour(*inr++, *ing++, *inb++);
  113.     }
  114. }
  115.