mtdiff image differencer (useful for animate gifs etc...)

Paint programs, vector editors, 3d modelers, animation editors, etc.
Post Reply
Message
Author
User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

mtdiff image differencer (useful for animate gifs etc...)

#1 Post by technosaurus »

This is a modification of Mark Tyler's micmp.c example program from the most recent mtcelledit package.

I recalled looking for something like this once and all I found was other people looking, so just in case I need it later, here is mtdiff

Basically all it does is take 2 images of the same size and outputs an image ("diff.png") that shows only the differences (all other pixels get the first color of image 1 which is typically a transparency color) ... nice for reducing the size of animations (this is how webp animations work iirc)

sorry, it only does a static file name, so you'll have to write some kind of script for renaming if you wish to batch process a large number of files.

Code: Select all

/*	micmp.c
	Copyright (C) 2007-2008 Mark Tyler

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program in the file COPYING.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mtpixel.h>

static void compare_images( mtImage *ia, mtImage *ib ){
	mtImage *ic=ia;
	int		i, j, k, rgb[3][3] = { {0,0,0}, {0,0,0}, {0,0,0} };
	unsigned char	*a, *b, *c;
	a = ia->channel[MTPIXEL_CHANNEL_IMAGE];
	b = ib->channel[MTPIXEL_CHANNEL_IMAGE];
	c = ic->channel[MTPIXEL_CHANNEL_IMAGE];

	if ( ia->width == ib->width && ia->height == ib->height && ia->type == ib->type ){
		j = ia->width * ia->height;
		if ( ia->type == MTPIXEL_IMAGE_INDEXED ){
			for ( i=0; i<j; i++ ){
				if ( a[0] != b[0] ) c[0]=b[0];
				else c[0]=0;
				rgb[0][0] = abs( a[0] - b[0] );
				if ( rgb[0][0] > rgb[0][1] ) rgb[0][1] = rgb[0][0];
				rgb[0][2] += rgb[0][0];
				a ++; b ++; c ++;
			}
		}

		if ( ia->type == MTPIXEL_IMAGE_RGB ){
			for ( i=0; i<j; i++ ){
				for ( k=0; k<3; k++ ){
					rgb[k][0] = abs( a[k] - b[k] );
					if ( rgb[k][0] > rgb[k][1] ) rgb[k][1] = rgb[k][0];
					rgb[k][2] += rgb[k][0];
				}
				if ( a[0] == b[0] && a[1] == b[1] && a[2] == b[2] )
					{c[0]=0;c[1]=0;c[2]=0;}
				else
					{c[0]=b[0];c[1]=b[1];c[2]=b[2];}
				a += 3; b += 3; c += 3;
			}
		}
		ic->palette.trans=0;
		mtpixel_image_save( ic, "diff.png", MTPIXEL_FILE_TYPE_PNG, 5 );
	}
}

int main( int argc, char *argv[] ){
	mtImage	*ia, *ib;
	int	res = 0;

mtpixel_init();
if ( argc != 3 ){
	res = 2;
}else{
	ia = mtpixel_image_load( argv[1] );
	if ( !ia ){
		res = 1;
	}else{
		ib = mtpixel_image_load( argv[2] );
		if ( !ib ){
			res = 1;
		}else{
			compare_images( ia, ib );
			mtpixel_image_destroy( ib );
		}
		mtpixel_image_destroy( ia );
	}
}
mtpixel_quit();
return res;
}
Attachments
mtdiff.tar.gz
(11.83 KiB) Downloaded 717 times
Last edited by technosaurus on Tue 09 Oct 2012, 10:27, edited 3 times in total.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#2 Post by technosaurus »

here is an example:
two images that were compared on the left and their diff.png on the right

Edit: if you are 1 of the 2 who already downloaded this, I have transparency working now.
Attachments
mtdiff.tar.gz
saves as diff.gif (the initial post saves as a png)
(11.83 KiB) Downloaded 708 times
example2.png
(4.04 KiB) Downloaded 677 times
example.png
(35.74 KiB) Downloaded 683 times
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply