head	1.7;
access
	monica
	thoth;
symbols
	Eglin_alpha_1:1.1;
locks
	monica:1.7; strict;
comment	@// @;


1.7
date	94.12.10.20.32.04;	author monica;	state Exp;
branches;
next	1.6;

1.6
date	94.12.08.22.30.29;	author monica;	state Exp;
branches;
next	1.5;

1.5
date	94.12.08.04.54.36;	author monica;	state Exp;
branches;
next	1.4;

1.4
date	94.11.09.20.25.21;	author monica;	state Exp;
branches;
next	1.3;

1.3
date	94.11.02.19.44.48;	author monica;	state Exp;
branches;
next	1.2;

1.2
date	94.08.17.04.12.11;	author monica;	state Exp;
branches;
next	1.1;

1.1
date	93.01.12.14.47.05;	author thoth;	state Exp;
branches;
next	;


desc
@primitive fast-convolution test.
@


1.7
log
@reincluded some .h files
@
text
@// Emacs: -*- C++ -*-
static char rcsid[] = "$Id: test014.c,v 1.6 1994/12/08 22:30:29 monica Exp monica $";

//
// $Log: test014.c,v $
// Revision 1.6  1994/12/08  22:30:29  monica
// removed refs to ImageDisplay
//
// Revision 1.5  1994/12/08  04:54:36  monica
// commented out all references to system files and timing
//
// Revision 1.4  1994/11/09  20:25:21  monica
// removed include devfrags & any debugging calls to malloc
//
// Revision 1.3  1994/11/02  19:44:48  monica
// removed display commands
//
// Revision 1.2  1994/08/17  04:12:11  monica
// added int main & return 0
//
// Revision 1.1  1993/01/12  14:47:05  thoth
// Initial revision
//
// Revision 1.1  92/10/07  22:46:07  thoth
// Initial revision
// 
//

 #include	<iostream.h>
 #include	<stdio.h>
 #include	<stdlib.h>

// #include <sys/time.h>
// #include <sys/resource.h>

#include	"IntPS.h"
#include	"PSIter.h"
// #include "ImageDisplayProtocol.h"
// #include "tutil.h"

IA_Set<IA_Point<int> >
slurp_pgm_file(const char *fname, unsigned char **data, unsigned *maxval)
{
    FILE	*fp = fopen(fname, "r");
    if (!fp) {
	cerr << "error opening PGM file " << fname << "\n";
	return IA_Set<IA_Point<int> >();
    }

    unsigned width, height;
    if (3!=fscanf(fp, "P5 %u %u %u", &width, &height, maxval)) {
        cerr << "Error: was expecting raw PGM (P5) image in file "
             << fname << "\n";
	fclose(fp);
        return IA_Set<IA_Point<int> >();
    }

    int ch;
    if ((ch=getc(fp))!='\n')
        ungetc(ch, fp);

    const unsigned size = width*height;

    *data = new unsigned char [size];

    if (!fread(*data, size, 1, fp)) {
	cerr << "short data read on PGM file " << fname << "\n";
	fclose(fp);
        exit(1);
    }

    fclose(fp);

    return IA_Set<IA_Point<int> >(IA_Point<int>(0,0), IA_Point<int>(height-1, width-1));
}

IA_Set<IA_Point<int> >
fast_boxy_conv(IA_Set<IA_Point<int> > src_ps, const unsigned char *src_data,
	       IA_Set<IA_Point<int> > templ_ps, const double *templ_data,
	       unsigned char **dest_data)
{
    IA_Set<IA_Point<int> >	rval;
    if (!src_ps.boxy()) {
	cerr << "source pointset not boxy\n";
	return rval;
    }

    IA_Point<int>	inf_(src_ps.inf()-templ_ps.inf());
    {
	IA_Point<int>	sup_(src_ps.sup()-templ_ps.sup());
	if (sup_<inf_) {
	    cerr << "image too small for template\n";
	    return rval;
	}
	
	rval = IA_Set<IA_Point<int> >(inf_,sup_);
    }

    *dest_data = new unsigned char[rval.card()];

    const int n=templ_ps.card();
    int *offsets = new int [n];
    {
	unsigned	base = src_ps.index(inf_);

	IA_PSIter<IA_Point<int> >	iter(templ_ps);
	IA_Point<int>	ip;

	unsigned i=0;
	while (iter(ip)) {
	    offsets[i++] = (int)src_ps.index(inf_+ip) - (int)base;
	}
    }

    IA_PSIter<IA_Point<int> > iter(rval);
    IA_Point<int>	ip;
    unsigned char	*valp=*dest_data;
    while (iter(ip)) {
	const unsigned char	*base = src_data + src_ps.index(ip);
	double	a=0;
	for (unsigned i=0; i<n; i++) {
	    a += templ_data[i]*base[offsets[i]];
	}
	*(valp++) = (unsigned char)a;
    }

    delete[] offsets;
    return rval;
}

int main(int argc, char**argv)
{
//    char	*disp=getenv("DISPLAY");
//    if (disp==0) {
//	cerr << "No DISPLAY environment variable, hasta.\n";
//	exit(1);
//    }

//    if (argc!=2) {
//	cerr << "Usage: "<<argv[0]<<" pgm-file\n";
//	exit(1);
//    }

//    const char	*fname=argv[1];
    const char	*fname="sr71_noisy.pgm";

    unsigned char	*data;
    unsigned	maxval;
    IA_Set<IA_Point<int> >	domain;
    domain = slurp_pgm_file(fname, &data, &maxval);

    const IA_Point<int>	gauss1[] = {
	IA_Point<int>(-3,0),
	    IA_Point<int>(-2,0),
	    IA_Point<int>(-1,0),
	    IA_Point<int>(0, 0),
	    IA_Point<int>( 1,0),
	    IA_Point<int>( 2,0),
	    IA_Point<int>( 3,0),
	};
    const IA_Point<int>	gauss2[] = {
	IA_Point<int>(0,-3),
	    IA_Point<int>(0,-2),
	    IA_Point<int>(0,-1),
	    IA_Point<int>(0, 0),
	    IA_Point<int>(0, 1),
	    IA_Point<int>(0, 2),
	    IA_Point<int>(0, 3),
	};
    static const double	gvalues[] = {
	0.00443,
	    0.05399,
	    0.24197,
	    0.39894,
	    0.24197,
	    0.05399,
	    0.00443,
	};

    IA_Set<IA_Point<int> >	templ(2,gauss1,7);

//    IDP::ClientID	cid;
//    cid = IDP::NewClient("iac++ test013");
//    IDP::LinkID		lid;
    // lid = IDP::CreateDisplay(cid, disp, IDP::READ_WRITE);

//    struct rusage	now, later;

//    getrusage(RUSAGE_SELF ,&now);

    unsigned char	*data1;
    IA_Set<IA_Point<int> >	ps1 = fast_boxy_conv(domain, data, templ, gvalues, &data1);

    templ = IA_Set<IA_Point<int> >(2,gauss2,7);
    unsigned char	*data2;
    IA_Set<IA_Point<int> >	ps2 = fast_boxy_conv(ps1, data1, templ, gvalues, &data2);
//    getrusage(RUSAGE_SELF ,&later);

//    double dt = (Ttof(later.ru_utime)- Ttof(now.ru_utime));
//    cout << "both convolutions took a total of " << dt << " user seconds\n";

    IA_Array<unsigned>	dims(2);
    {
	IA_Point<int>	width = ps2.sup() - ps2.inf();
	dims[0] = width[0]+1;
	dims[1] = width[1]+1;
    }
    // IDP::WriteImage(lid, "gaussed twice", IDP::U_BYTE, 1, dims, data2);
    delete[] data1;
    delete[] data2;

    return 0;

}
@


1.6
log
@removed refs to ImageDisplay
@
text
@d2 1
a2 1
static char rcsid[] = "$Id: test014.c,v 1.5 1994/12/08 04:54:36 monica Exp monica $";
d6 3
d29 3
a31 3
// #include	<iostream.h>
// #include	<stdio.h>
// #include	<stdlib.h>
@


1.5
log
@commented out all references to system files and timing
@
text
@d2 1
a2 1
static char rcsid[] = "$Id: test014.c,v 1.4 1994/11/09 20:25:21 monica Exp monica $";
d6 3
d35 1
a35 1
#include "ImageDisplayProtocol.h"
d130 5
a134 5
    char	*disp=getenv("DISPLAY");
    if (disp==0) {
	cerr << "No DISPLAY environment variable, hasta.\n";
	exit(1);
    }
@


1.4
log
@removed include devfrags & any debugging calls to malloc
@
text
@d2 1
a2 1
static char rcsid[] = "$Id: test014.c,v 1.3 1994/11/02 19:44:48 monica Exp monica $";
d6 3
d23 3
a25 3
#include	<iostream.h>
#include	<stdio.h>
#include	<stdlib.h>
d27 2
a28 2
#include <sys/time.h>
#include <sys/resource.h>
d33 1
a33 1
#include "tutil.h"
d181 1
a181 1
    struct rusage	now, later;
d183 1
a183 1
    getrusage(RUSAGE_SELF ,&now);
d191 1
a191 1
    getrusage(RUSAGE_SELF ,&later);
d193 2
a194 2
    double dt = (Ttof(later.ru_utime)- Ttof(now.ru_utime));
    cout << "both convolutions took a total of " << dt << " user seconds\n";
@


1.3
log
@removed display commands
@
text
@d2 1
a2 1
static char rcsid[] = "$Id: test014.c,v 1.2 1994/08/17 04:12:11 monica Exp monica $";
d6 3
a123 2
    // malloc_debug(2);

@


1.2
log
@added int main & return 0
@
text
@d2 1
a2 1
static char rcsid[] = "$Id: test014.c,v 1.1 1993/01/12 14:47:05 thoth Exp monica $";
d6 3
d172 3
a174 3
    IDP::ClientID	cid;
    cid = IDP::NewClient("iac++ test013");
    IDP::LinkID		lid;
@


1.1
log
@Initial revision
@
text
@d2 1
a2 1
static char rcsid[] = "$Id: test009.c,v 1.1 92/10/07 22:46:07 thoth Exp Locker: thoth $";
d5 4
a8 1
// $Log:	test009.c,v $
d21 2
a22 2
#include	"IntPointSet.h"
#include	"IPSIter.h"
d26 1
a26 3
#include "with-ia-pointset.h"

IntPointSet
d32 1
a32 1
	return IntPointSet();
d40 1
a40 1
        return IntPointSet();
d59 1
a59 1
    return IntPointSet(IntPoint(0,0), IntPoint(height-1, width-1));
d62 3
a64 3
IntPointSet
fast_boxy_conv(IntPointSet src_ps, const unsigned char *src_data,
	       IntPointSet templ_ps, const double *templ_data,
d67 1
a67 1
    IntPointSet	rval;
d73 1
a73 1
    IntPoint	inf_(src_ps.inf()-templ_ps.inf());
d75 1
a75 1
	IntPoint	sup_(src_ps.sup()-templ_ps.sup());
d81 1
a81 1
	rval = IntPointSet(inf_,sup_);
d91 2
a92 2
	IPSIter	iter(templ_ps);
	IntPoint	ip;
d100 2
a101 2
    IPSIter iter(rval);
    IntPoint	ip;
d126 4
a129 4
    if (argc!=2) {
	cerr << "Usage: "<<argv[0]<<" pgm-file\n";
	exit(1);
    }
d131 2
a132 1
    const char	*fname=argv[1];
d136 1
a136 1
    IntPointSet	domain;
d139 8
a146 8
    const IntPoint	gauss1[] = {
	IntPoint(-3,0),
	    IntPoint(-2,0),
	    IntPoint(-1,0),
	    IntPoint(0, 0),
	    IntPoint( 1,0),
	    IntPoint( 2,0),
	    IntPoint( 3,0),
d148 8
a155 8
    const IntPoint	gauss2[] = {
	IntPoint(0,-3),
	    IntPoint(0,-2),
	    IntPoint(0,-1),
	    IntPoint(0, 0),
	    IntPoint(0, 1),
	    IntPoint(0, 2),
	    IntPoint(0, 3),
d167 1
a167 1
    IntPointSet	templ(2,gauss1,7);
d179 1
a179 1
    IntPointSet	ps1 = fast_boxy_conv(domain, data, templ, gvalues, &data1);
d181 1
a181 1
    templ = IntPointSet(2,gauss2,7);
d183 1
a183 1
    IntPointSet	ps2 = fast_boxy_conv(ps1, data1, templ, gvalues, &data2);
d189 1
a189 1
    Array<unsigned>	dims(2);
d191 1
a191 1
	IntPoint	width = ps2.sup() - ps2.inf();
d198 3
@
