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.05.05.25;	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.22.51.27;	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.50.06;	author thoth;	state Exp;
branches;
next	;


desc
@a faster complete convolution method that works for non-boxys
@


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

//
// $Log: test016.c,v $
// Revision 1.6  1994/12/08  22:30:29  monica
// removed refs to ImageDisplay
//
// Revision 1.5  1994/12/08  05:05:25  monica
// removed timing references
//
// Revision 1.4  1994/11/09  20:25:21  monica
// removed include devfrags & any debugging calls to malloc
//
// Revision 1.3  1994/11/02  22:51:27  monica
// removed display refs
//
// Revision 1.2  1994/08/17  04:12:11  monica
// added int main & return 0
//
// Revision 1.1  1993/01/12  14:50:06  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));
}

void zero_extend_scan(IA_SetStructure ss, const unsigned char **src,
		      unsigned char **dest)
{
    for (unsigned i=0; i<ss.nintervals(); i++) {
	IA_ss_interval	temp(ss.retrieve_interval(i));
	if (temp.substructure == IA_SetStructure::FIRST_ONLY) {
	    *src += temp.count;
	} else if (temp.substructure == IA_SetStructure::BOTH) {
	    for (unsigned j=0; j<temp.count; j++)
		*((*dest)++) = *((*src)++);
	} else if (temp.substructure == IA_SetStructure::SECOND_ONLY) {
	    for (unsigned j=0; j<temp.count; j++)
		*((*dest)++) = 0;
	} else {
	    for (unsigned j=0; j<temp.count; j++)
		zero_extend_scan(temp.substructure, src, dest);
	}
    }
}

void zero_extend(IA_Set<IA_Point<int> > src_ps, const unsigned char *src_data,
		 IA_Set<IA_Point<int> > dest_ps, unsigned char *dest_data)
{
    IA_SetStructure	ss;
    intersect_with_dualstruct(src_ps, dest_ps, &ss);
    const unsigned char	*s=src_data;
    unsigned char	*p=dest_data;
    zero_extend_scan(ss, &s, &p);
}

void 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,
		    IA_Set<IA_Point<int> > dest_ps, unsigned char **dest_data)
{
    *dest_data=0;
    IA_Set<IA_Point<int> > intermed_ps;
    if (!src_ps.boxy()) {
	cerr << "source pointset not boxy\n";
	return;
    }

    {
	IA_Point<int>	inf_(src_ps.inf()+templ_ps.inf());
	IA_Point<int>	sup_(src_ps.sup()+templ_ps.sup());
	
	intermed_ps = IA_Set<IA_Point<int> >(inf_,sup_);
    }

    unsigned char	*intermed_data;
    intermed_data = new unsigned char[intermed_ps.card()];
    zero_extend(src_ps, src_data, intermed_ps, intermed_data);

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

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

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

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

    IA_PSIter<IA_Point<int> > iter(dest_ps);
    IA_Point<int>	ip;
    unsigned char	*valp=*dest_data;
    while (iter(ip)) {
	const unsigned char	*base = intermed_data + intermed_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;
}

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;
    fast_boxy_conv(domain, data, templ, gvalues, domain, &data1);

    templ = IA_Set<IA_Point<int> >(2,gauss2,7);
    unsigned char	*data2;
    fast_boxy_conv(domain, data1, templ, gvalues, domain, &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 = domain.sup() - domain.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: test016.c,v 1.5 1994/12/08 05:05:25 monica Exp monica $";
d6 3
d29 3
a31 3
// #include	<iostream.h>
// #include	<stdio.h>
// #include	<stdlib.h>
@


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


1.4
log
@removed include devfrags & any debugging calls to malloc
@
text
@d2 1
a2 1
static char rcsid[] = "$Id: test016.c,v 1.3 1994/11/02 22:51:27 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 3
#include "tutil.h"

/* #include "with-ia-pointset.h" */
d211 1
a211 1
    struct rusage	now, later;
d213 1
a213 1
    getrusage(RUSAGE_SELF ,&now);
d221 1
a221 1
    getrusage(RUSAGE_SELF ,&later);
d223 2
a224 2
    double dt = (Ttof(later.ru_utime)- Ttof(now.ru_utime));
    cout << "both convolutions took a total of " << dt << " user seconds\n";
d239 1
@


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

@


1.2
log
@added int main & return 0
@
text
@d2 1
a2 1
static char rcsid[] = "$Id: test016.c,v 1.1 1993/01/12 14:50:06 thoth Exp monica $";
d6 3
d204 4
a207 4
    IDP::ClientID	cid;
    cid = IDP::NewClient("iac++ test013");
    IDP::LinkID		lid;
    lid = IDP::CreateDisplay(cid, disp, IDP::READ_WRITE);
d230 1
a230 1
    IDP::WriteImage(lid, "gaussed twice", IDP::U_BYTE, 1, dims, data2);
@


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 1
#include "with-ia-pointset.h"
d28 1
a28 1
IntPointSet
d34 1
a34 1
	return IntPointSet();
d42 1
a42 1
        return IntPointSet();
d61 1
a61 1
    return IntPointSet(IntPoint(0,0), IntPoint(height-1, width-1));
d64 1
a64 1
void zero_extend_scan(SetStructure ss, const unsigned char **src,
d68 2
a69 2
	ss_interval	temp(ss.retrieve_interval(i));
	if (temp.substructure == SetStructure::FIRST_ONLY) {
d71 1
a71 1
	} else if (temp.substructure == SetStructure::BOTH) {
d74 1
a74 1
	} else if (temp.substructure == SetStructure::SECOND_ONLY) {
d84 2
a85 2
void zero_extend(IntPointSet src_ps, const unsigned char *src_data,
		 IntPointSet dest_ps, unsigned char *dest_data)
d87 1
a87 1
    SetStructure	ss;
d94 3
a96 3
void fast_boxy_conv(IntPointSet src_ps, const unsigned char *src_data,
		    IntPointSet templ_ps, const double *templ_data,
		    IntPointSet dest_ps, unsigned char **dest_data)
d99 1
a99 1
    IntPointSet intermed_ps;
d106 2
a107 2
	IntPoint	inf_(src_ps.inf()+templ_ps.inf());
	IntPoint	sup_(src_ps.sup()+templ_ps.sup());
d109 1
a109 1
	intermed_ps = IntPointSet(inf_,sup_);
d121 1
a121 1
	IntPoint	inf_(src_ps.inf());
d124 2
a125 2
	IPSIter	iter(templ_ps);
	IntPoint	ip;
d133 2
a134 2
    IPSIter iter(dest_ps);
    IntPoint	ip;
d158 4
a161 4
    if (argc!=2) {
	cerr << "Usage: "<<argv[0]<<" pgm-file\n";
	exit(1);
    }
d163 2
a164 1
    const char	*fname=argv[1];
d168 1
a168 1
    IntPointSet	domain;
d171 8
a178 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),
d180 8
a187 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),
d199 1
a199 1
    IntPointSet	templ(2,gauss1,7);
d213 1
a213 1
    templ = IntPointSet(2,gauss2,7);
d221 1
a221 1
    Array<unsigned>	dims(2);
d223 1
a223 1
	IntPoint	width = domain.sup() - domain.inf();
d230 3
@
