Back to index

lightning-sunbird  0.9+nobinonly
buffer_diff.c
Go to the documentation of this file.
00001 /* imagediff - Compare two images
00002  *
00003  * Copyright © 2004 Richard D. Worth
00004  *
00005  * Permission to use, copy, modify, distribute, and sell this software
00006  * and its documentation for any purpose is hereby granted without
00007  * fee, provided that the above copyright notice appear in all copies
00008  * and that both that copyright notice and this permission notice
00009  * appear in supporting documentation, and that the name of Richard Worth
00010  * not be used in advertising or publicity pertaining to distribution
00011  * of the software without specific, written prior permission.
00012  * Richard Worth makes no representations about the suitability of this
00013  * software for any purpose.  It is provided "as is" without express
00014  * or implied warranty.
00015  * 
00016  * RICHARD WORTH DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00017  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
00018  * NO EVENT SHALL RICHARD WORTH BE LIABLE FOR ANY SPECIAL, INDIRECT OR
00019  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
00020  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
00021  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
00022  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00023  *
00024  * Author: Richard D. Worth <richard@theworths.org> */
00025 
00026 #include "buffer_diff.h"
00027 
00028 /* Image comparison code courttesy of Richard Worth.
00029  * Returns number of pixels changed.
00030  * Also fills out a "diff" image intended to visually show where the
00031  * images differ.
00032  */
00033 int
00034 buffer_diff (char *buf_a, char *buf_b, char *buf_diff,
00035            int width, int height, int stride)
00036 {
00037     int x, y;
00038     int total_pixels_changed = 0;
00039     unsigned char *row_a, *row_b, *row;
00040 
00041     for (y = 0; y < height; y++)
00042     {
00043        row_a = buf_a + y * stride;
00044        row_b = buf_b + y * stride;
00045        row = buf_diff + y * stride;
00046        for (x = 0; x < width; x++)
00047        {
00048            int channel;
00049            unsigned char value_a, value_b;
00050            int pixel_changed = 0;
00051            for (channel = 0; channel < 4; channel++)
00052            {
00053               double diff;
00054               value_a = row_a[x * 4 + channel];
00055               value_b = row_b[x * 4 + channel];
00056               if (value_a != value_b)
00057                   pixel_changed = 1;
00058               diff = value_a - value_b;
00059               row[x * 4 + channel] = 128 + diff / 3.0;
00060            }
00061            if (pixel_changed) {
00062               total_pixels_changed++;
00063            } else {
00064               row[x*4+0] = 0;
00065               row[x*4+1] = 0;
00066               row[x*4+2] = 0;
00067            }
00068            row[x * 4 + 3] = 0xff; /* Set ALPHA to 100% (opaque) */
00069        }
00070     }
00071 
00072     return total_pixels_changed;
00073 }