home *** CD-ROM | disk | FTP | other *** search
- /* fsi.c
- * AUTHOR: Cy Booker, cy@cheepnis.demon.co.uk
- * LICENSE: FreeWare, Copyright (c) 1995 Cy Booker
- *
- * filter: * 7
- * 3 5 1 (1/16)
- */
-
- #include "internal.h"
-
- #include <assert.h>
- #include <string.h> /* memset() */
- #include <stdlib.h> /* calloc() */
-
- #include "OS:hourglass.h"
- #include "OS:macros.h"
-
-
-
- /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- */
-
- extern bool process_gif_8bpp_floyd_steinberg(
- const process_gif *p) {
- byte *rove;
- int width, height;
- int x, y;
- const os_colour *palette;
- int line_length;
- int *buffer, *this_row, *next_row;
- int buffer_width;
- int red, grn, blu;
- os_colour colour;
- int error_red, error_grn, error_blu;
- int temp_red, temp_grn, temp_blu;
- rgbtupleout error;
- rgbtupleout_fn fn;
-
- assert(p);
- assert(p->pixel_width > 0);
- assert(p->pixel_height > 0);
- assert(p->in_palette.colours);
- assert(p->fn);
-
- /*
- * floyd_steinberg requires storing error info for one pixel to right and one pixel to left
- * so we will just allocate an extra column for each row and not do any edge checks
- * each entry is stored as {r*SCALE, g*SCALE, b*SCALE}
- */
- buffer_width = (p->pixel_width + 2) * 3;
- buffer = calloc(1, sizeof(*buffer) * (buffer_width * 2));
- if (!buffer) {
- /*
- * oh dear
- */
- return TRUE;
- }
-
- error.palette = p->out_palette; /* structure copy */
- /*
- * not we pre-load values from the process_gif array
- * because it considerably helps the compiler produce better code
- */
- rove = p->buffer;
- width = p->pixel_width;
- height = p->pixel_height;
- palette = p->in_palette.colours;
- line_length = p->line_length;
- fn = p->fn;
- for (y= height; (y > 0); y--) {
- if ((y & 7) == 0) {
- xhourglass_percentage((y * 100) / height);
- }
- this_row = buffer + (buffer_width * ((y + 2) % 2)) + 1*3; /* skip left hand 'dummy' column */
- next_row = buffer + (buffer_width * ((y + 1) % 2)) + 0*3; /* point inside left hand dummy column */
- memset(next_row, 0, sizeof(*next_row) * buffer_width); /* next row has no errors */
- error_red = error_grn = error_blu = 0; /* error along this row */
- /*
- * note that just because we are actually scanning/outputting right to left
- * doesn't matter as far as floyd_steinberg is concerned
- * although it might help if we could ``snake''
- */
- for (x= width - 1; (x >= 0); x--) {
- INPUT;
-
- red += *this_row++; /* add in errors from other row */
- red += error_red; /* add in errors from this row */
- grn += *this_row++;
- grn += error_grn;
- blu += *this_row++;
- blu += error_blu;
-
- PROCESS;
-
- temp_red = (red * 3) / 16; /* diffuse pixel `below left' */
- *next_row += temp_red; next_row++;
- error_red = red - temp_red;
- temp_grn = (grn * 3) / 16;
- *next_row += temp_grn; next_row++;
- error_grn = grn - temp_grn;
- temp_blu = (blu * 3) / 16;
- *next_row += temp_blu; next_row++;
- error_blu = blu- temp_blu;
-
- temp_red = (red * 5) / 16; /* diffuse pixel `below' */
- next_row[0] += temp_red;
- error_red -= temp_red;
- temp_grn = (grn * 5) / 16;
- next_row[1] += temp_grn;
- error_grn -= temp_grn;
- temp_blu = (blu* 5) / 16;
- next_row[2] += temp_blu;
- error_blu -= temp_blu;
-
- temp_red = (red * 1) / 16; /* diffuse pixel `below right' */
- next_row[3] += temp_red;
- error_red -= temp_red; /* diffuse pixel to `right' */
- temp_grn = (grn * 1) / 16;
- next_row[4] += temp_grn;
- error_grn -= temp_grn;
- temp_blu = (blu * 1) / 16;
- next_row[5] += temp_blu;
- error_blu -= temp_blu;
- }
- rove += line_length;
- }
- free(buffer);
- return FALSE;
- }
-
-
-