home *** CD-ROM | disk | FTP | other *** search
/ APDL Eductation Resources / APDL Eductation Resources.iso / programs / graphics / gif2rpc / source / 16bpp_66bi / c / nearest < prev    next >
Encoding:
Text File  |  1995-10-16  |  2.6 KB  |  102 lines

  1. /* nearest.c
  2.  * AUTHOR:      Cy Booker, cy@cheepnis.demon.co.uk
  3.  * LICENSE:     FreeWare, Copyright (c) 1995 Cy Booker
  4.  */
  5.  
  6. #include "internal.h"
  7.  
  8. #include <assert.h>
  9.  
  10. #include "OS:macros.h"
  11.  
  12.  
  13.  
  14. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  15.  */
  16.  
  17. static void calc_pixtrans(
  18.                 const process_gif       *p,
  19.                 bits                    *pixtrans);
  20.  
  21.  
  22.  
  23. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  24.  */
  25.  
  26. extern bool process_gif_16bpp_nearest_66bit(
  27.                 const process_gif       *p) {
  28.   byte                  *rove;
  29.   bits                  dest;
  30.   int                   width;
  31.   int                   x, y;
  32.   const os_colour       *palette;
  33.   int                   line_length;
  34.   bits                  pixtrans[256];
  35.  
  36.   assert(p);
  37.   assert(p->pixel_width > 0);
  38.   assert(p->pixel_height > 0);
  39.   assert(p->in_palette.colours);
  40.  
  41.   calc_pixtrans(p, pixtrans);
  42.  
  43.   /*
  44.    * not we pre-load values from the process_gif array
  45.    * because it considerably helps the compiler produce better code
  46.    */
  47.   rove = p->buffer;
  48.   width = (p->pixel_width + 1) & ~1;
  49.   palette = p->in_palette.colours;
  50.   line_length = p->line_length;
  51.  
  52.   for (y= p->pixel_height - 1; (y >= 0); y--) {
  53.     for (x= width - 1; (x >= 0); x--) {
  54.       /*
  55.        * writing one 32-bit word is much faster than two 16-bit shorts
  56.        */
  57.       dest = pixtrans[rove[x]];
  58.       x--;
  59.       dest <<= 16;
  60.       dest |= pixtrans[rove[x]];
  61.       *(((int *)rove) + (((unsigned int)x) >> 1)) = dest;
  62.     }
  63.     rove += line_length;
  64.   }
  65.   return FALSE;
  66. }
  67.  
  68.  
  69.  
  70. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  71.  */
  72.  
  73. static void calc_pixtrans(
  74.                 const process_gif       *p,
  75.                 bits                    *pixtrans) {
  76.   int                   red, grn, blu;
  77.   os_colour             colour;
  78.   int                   i;
  79.   const os_colour       *palette;
  80.  
  81.   assert(p);
  82.   assert(p->in_palette.colours);
  83.   assert(p->in_palette.ncolours > 0);
  84.   assert(p->in_palette.ncolours <= 256);
  85.   assert(pixtrans);
  86.  
  87.   palette = p->in_palette.colours;
  88.   for (i= p->in_palette.ncolours - 1; (i >= 0); i--) {
  89.     colour = palette[i];
  90.     red = Gscale_8bit_to_22bit[(colour >> 8) & 0xff];
  91.     grn = Gscale_8bit_to_22bit[(colour >> 16) & 0xff];
  92.     blu = Gscale_8bit_to_22bit[(colour >> 24) & 0xff];
  93.     red = ((bits)(red * 0x1f) + (SCALE / 2)) / (bits)SCALE;     /* scale to output */
  94.     grn = ((bits)(grn * 0x1f) + (SCALE / 2)) / (bits)SCALE;
  95.     blu = ((bits)(blu * 0x1f) + (SCALE / 2)) / (bits)SCALE;
  96.     pixtrans[i] = red | (grn << 5) | (blu << 10);
  97.   }
  98. }
  99.  
  100.  
  101.  
  102.