home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsf / gif2rpc / source / 8bpp / c / fsi < prev    next >
Encoding:
Text File  |  1995-10-16  |  4.1 KB  |  132 lines

  1. /* fsi.c
  2.  * AUTHOR:      Cy Booker, cy@cheepnis.demon.co.uk
  3.  * LICENSE:     FreeWare, Copyright (c) 1995 Cy Booker
  4.  *
  5.  * filter:              *  7
  6.  *                   3  5  1            (1/16)
  7.  */
  8.  
  9. #include "internal.h"
  10.  
  11. #include <assert.h>
  12. #include <string.h>             /* memset() */
  13. #include <stdlib.h>             /* calloc() */
  14.  
  15. #include "OS:hourglass.h"
  16. #include "OS:macros.h"
  17.  
  18.  
  19.  
  20. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  21.  */
  22.  
  23. extern bool process_gif_8bpp_floyd_steinberg(
  24.                 const process_gif       *p) {
  25.   byte                  *rove;
  26.   int                   width, height;
  27.   int                   x, y;
  28.   const os_colour       *palette;
  29.   int                   line_length;
  30.   int                   *buffer, *this_row, *next_row;
  31.   int                   buffer_width;
  32.   int                   red, grn, blu;
  33.   os_colour             colour;
  34.   int                   error_red, error_grn, error_blu;
  35.   int                   temp_red, temp_grn, temp_blu;
  36.   rgbtupleout           error;
  37.   rgbtupleout_fn        fn;
  38.  
  39.   assert(p);
  40.   assert(p->pixel_width > 0);
  41.   assert(p->pixel_height > 0);
  42.   assert(p->in_palette.colours);
  43.   assert(p->fn);
  44.  
  45.   /*
  46.    * floyd_steinberg requires storing error info for one pixel to right and one pixel to left
  47.    * so we will just allocate an extra column for each row and not do any edge checks
  48.    * each entry is stored as {r*SCALE, g*SCALE, b*SCALE}
  49.    */
  50.   buffer_width = (p->pixel_width + 2) * 3;
  51.   buffer = calloc(1, sizeof(*buffer) * (buffer_width * 2));
  52.   if (!buffer) {
  53.     /*
  54.      * oh dear
  55.      */
  56.     return TRUE;
  57.   }
  58.  
  59.   error.palette = p->out_palette;               /* structure copy */
  60.   /*
  61.    * not we pre-load values from the process_gif array
  62.    * because it considerably helps the compiler produce better code
  63.    */
  64.   rove = p->buffer;
  65.   width = p->pixel_width;
  66.   height = p->pixel_height;
  67.   palette = p->in_palette.colours;
  68.   line_length = p->line_length;
  69.   fn = p->fn;
  70.   for (y= height; (y > 0); y--) {
  71.     if ((y & 7) == 0) {
  72.       xhourglass_percentage((y * 100) / height);
  73.     }
  74.     this_row = buffer + (buffer_width * ((y + 2) % 2)) + 1*3;   /* skip left hand 'dummy' column */
  75.     next_row = buffer + (buffer_width * ((y + 1) % 2)) + 0*3;   /* point inside left hand dummy column */
  76.     memset(next_row, 0, sizeof(*next_row) * buffer_width);      /* next row has no errors */
  77.     error_red = error_grn = error_blu = 0;                      /* error along this row */
  78.     /*
  79.      * note that just because we are actually scanning/outputting right to left
  80.      * doesn't matter as far as floyd_steinberg is concerned
  81.      * although it might help if we could ``snake''
  82.      */
  83.     for (x= width - 1; (x >= 0); x--) {
  84.       INPUT;
  85.  
  86.       red += *this_row++;                               /* add in errors from other row */
  87.       red += error_red;                                 /* add in errors from this row */
  88.       grn += *this_row++;
  89.       grn += error_grn;
  90.       blu += *this_row++;
  91.       blu += error_blu;
  92.  
  93.       PROCESS;
  94.  
  95.       temp_red = (red * 3) / 16;                        /* diffuse pixel `below left' */
  96.       *next_row += temp_red; next_row++;
  97.       error_red = red - temp_red;
  98.       temp_grn = (grn * 3) / 16;
  99.       *next_row += temp_grn; next_row++;
  100.       error_grn = grn - temp_grn;
  101.       temp_blu = (blu * 3) / 16;
  102.       *next_row += temp_blu; next_row++;
  103.       error_blu = blu- temp_blu;
  104.  
  105.       temp_red = (red * 5) / 16;                        /* diffuse pixel `below' */
  106.       next_row[0] += temp_red;
  107.       error_red -= temp_red;
  108.       temp_grn = (grn * 5) / 16;
  109.       next_row[1] += temp_grn;
  110.       error_grn -= temp_grn;
  111.       temp_blu = (blu* 5) / 16;
  112.       next_row[2] += temp_blu;
  113.       error_blu -= temp_blu;
  114.  
  115.       temp_red = (red * 1) / 16;                        /* diffuse pixel `below right' */
  116.       next_row[3] += temp_red;
  117.       error_red -= temp_red;                            /* diffuse pixel to `right' */
  118.       temp_grn = (grn * 1) / 16;
  119.       next_row[4] += temp_grn;
  120.       error_grn -= temp_grn;
  121.       temp_blu = (blu * 1) / 16;
  122.       next_row[5] += temp_blu;
  123.       error_blu -= temp_blu;
  124.     }
  125.     rove += line_length;
  126.   }
  127.   free(buffer);
  128.   return FALSE;
  129. }
  130.  
  131.  
  132.