9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Pls. help 2 catch a bug
@ 2002-07-17  7:46 Peter A. Cejchan
  0 siblings, 0 replies; only message in thread
From: Peter A. Cejchan @ 2002-07-17  7:46 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 308 bytes --]

Hi,

the attached code compiles fine, but commits suicide when run :-(
(the failure point is indicated)
I would be most happy if someone could put his/her hands on it...

TIA,
-- 
++pac.

Peter A. Cejchan
Paleobiology Lab, GLU Acad. Sci. CZ
<pac@next.gli.cas.cz>
[http | ftp]://next.gli.cas.cz

[-- Attachment #2.1: Type: text/plain, Size: 276 bytes --]

The following attachment had content that we can't
prove to be harmless.  To avoid possible automatic
execution, we changed the content headers.
The original header was:

	Content-Type: text/x-csrc; charset=us-ascii
	Content-Disposition: attachment; filename="sharpen.c"

[-- Attachment #2.2: sharpen.c.suspect --]
[-- Type: application/octet-stream, Size: 13234 bytes --]

// equalize.c
// makes a new RGB image by  sharpening the old one
// heavily, and in a dirty(?) way, reuses code from resample.c 
// and from  sharpen.c,v 1.32 2002/05/25 18:10:04       image filter plug-in for The Gimp image
// manipulation program,  by Michael Sweet (mike@easysw.com)
// sorry, and thanks!
// Wed Jul 17 09:23:27 EDT 2002  <pac@next.gli.cas.cz>
// License: GPL
// Version 1.00



////////////////////////////////////////////////////////////////////////////////////////
//	Please, search for "NOT REACHED" to see the probable location of the bug
////////////////////////////////////////////////////////////////////////////////////////

#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>

#define ALPHA 3

// dirty defines: quick hack
#define guchar uchar
#define intneg int
#define intpos int
#define gint int
#define dst nscan
#define sel_width xsize
#define img_bpp nchan
#define sel_y1 0
#define sel_y2 ysize
#define sel_height ysize

// Globals...

static int     pos_lut[256];		/* Positive coefficient LUT */
static int     neg_lut[256];		/* Negative coefficient LUT */
static int  sharpen_percent=85;
static int  nchan;
static int  img_bpp=3;
static int  xsize, ysize;
static uchar **oscan, **nscan;


static void	gray_filter  (int width, guchar *src, guchar *dst, intneg *neg0,
			      intneg *neg1, intneg *neg2);
static void	graya_filter (int width, guchar *src, guchar *dst, intneg *neg0,
			      intneg *neg1, intneg *neg2);
static void	 rgb_filter (int width, guchar *src, guchar *dst, intneg *neg0,
			      intneg *neg1, intneg *neg2);
static void	rgba_filter  (int width, guchar *src, guchar *dst, intneg *neg0,
			      intneg *neg1, intneg *neg2);

// Funcs...

void
usage(void)
{
	fprint(2, "usage:\n\sharpen  [imagefile]\n");
	exits("usage");
}

static void
compute_luts (void)
{
  gint i;	/* Looping var */
  gint fact;	/* 1 - sharpness */

  fact = 100 - sharpen_percent;
  if (fact < 1)
    fact = 1;

  for (i = 0; i < 256; i ++)
    {
      pos_lut[i] = 800 * i / fact;
      neg_lut[i] = (4 + pos_lut[i] - (i << 3)) >> 3;
    };
}



/*
 * 'sharpen()' - Sharpen an image using a convolution filter.
 */

static void
sharpen (int xsize, int ysize)
{
  guchar	*src_rows[4],	/* Source pixel rows */
		*src_ptr,	/* Current source pixel */
		*dst_row;	/* Destination pixel row */
  intneg	*neg_rows[4],	/* Negative coefficient rows */
		*neg_ptr;	/* Current negative coefficient */
  gint		i,		/* Looping vars */
		y,		/* Current location in image */
		row,		/* Current row in src_rows */
		count,		/* Current number of filled src_rows */
		width;		/* Byte width of the image */
  void		(*filter)(int, guchar *, guchar *, intneg *, intneg *, intneg *);

  filter = nil;

  /*
   * Setup for filter...
   */

  compute_luts ();

  width = sel_width * img_bpp;

  for (row = 0; row < 4; row ++)
    {
     neg_rows[row] = malloc(xsize*sizeof(int*));
    };



  /*
   * Pre-load the first row for the filter...
   */



  src_rows[0]=oscan[0];
  dst_row = nscan[0];

  for (i = width, src_ptr = src_rows[0], neg_ptr = neg_rows[0];
       i > 0;
       i --, src_ptr ++, neg_ptr ++)
    *neg_ptr = neg_lut[*src_ptr];

  row   = 1;
  count = 1;

  /*
   * Select the filter...
   */

  switch (img_bpp)
    {
    case 1 :
      filter = gray_filter;
      break;
    case 2 :
      filter = graya_filter;
      break;
    case 3 :
      filter = rgb_filter;
      break;
    case 4 :
      filter = rgba_filter;
      break;
    };

  /*
   * Sharpen...
   */
  for (y = sel_y1; y < sel_y2; y ++)
    {
      /*
       * Load the next pixel row...
       */

      if ((y + 1) < sel_y2)
	{
	  /*
	   * Check to see if our src_rows[] array is overflowing yet...
	   */

	  if (count >= 3)
	    count --;

	  /*
	   * Grab the next row...
	   */

  src_rows[row]=oscan[row];

	  for (i = width, src_ptr = src_rows[row], neg_ptr = neg_rows[row];
	       i > 0;
	       i --, src_ptr ++, neg_ptr ++)
	    *neg_ptr = neg_lut[*src_ptr];

	  count ++;
	  row = (row + 1) & 3;
	}
      else
	{
	  /*
	   * No more pixels at the bottom...  Drop the oldest samples...
	   */

	  count --;
	};

      /*
       * Now sharpen pixels and save the results...
       */

dst_row=nscan[y];
      if (count == 3)
	{


	  (* filter) (sel_width, src_rows[(row + 2) & 3], dst_row,
		      neg_rows[(row + 1) & 3] + img_bpp,
		      neg_rows[(row + 2) & 3] + img_bpp,
		      neg_rows[(row + 3) & 3] + img_bpp);

	  /*
	   * Set the row...
	   */

	}
      else if (count == 2)
	{
	  if (y == sel_y1)	/* first row */


for(i=0; i<xsize;i++) dst_row[i]=src_rows[0][i];


	  else                  /* last row  */

for(i=0; i<xsize;i++) dst_row[i]=src_rows[(sel_height - 1) & 3][i];

	};

    };

  /*
   * OK, we're done.  Free all memory used...
   */

  for (row = 0; row < 4; row ++)
    {
//	free (neg_rows[row]);    <-- FREE THIS, LATER
    };
}



/*
 * 'gray_filter()' - Sharpen grayscale pixels.
 */

static void
gray_filter (gint    width,	/* I - Width of line in pixels */
	     guchar *src,	/* I - Source line */
	     guchar *dst,	/* O - Destination line */
	     intneg *neg0,	/* I - Top negative coefficient line */
	     intneg *neg1,	/* I - Middle negative coefficient line */
	     intneg *neg2)	/* I - Bottom negative coefficient line */
{
  intpos pixel;		/* New pixel value */

  *dst++ = *src++;
  width -= 2;

  while (width > 0)
    {
      pixel = (pos_lut[*src++] - neg0[-1] - neg0[0] - neg0[1] -
	       neg1[-1] - neg1[1] -
	       neg2[-1] - neg2[0] - neg2[1]);
      pixel = (pixel + 4) >> 3;
      if (pixel < 0)
	*dst++ = 0;
      else if (pixel < 255)
	*dst++ = pixel;
      else
	*dst++ = 255;

      neg0 ++;
      neg1 ++;
      neg2 ++;
      width --;
    };

  *dst++ = *src++;
}

/*
 * 'graya_filter()' - Sharpen grayscale+alpha pixels.
 */

static void
graya_filter (gint   width,	/* I - Width of line in pixels */
	      guchar *src,	/* I - Source line */
	      guchar *dst,	/* O - Destination line */
	      intneg *neg0,	/* I - Top negative coefficient line */
	      intneg *neg1,	/* I - Middle negative coefficient line */
	      intneg *neg2)	/* I - Bottom negative coefficient line */
{
  intpos pixel;		/* New pixel value */

  *dst++ = *src++;
  *dst++ = *src++;
  width -= 2;

  while (width > 0)
    {
      pixel = (pos_lut[*src++] - neg0[-2] - neg0[0] - neg0[2] -
	       neg1[-2] - neg1[2] -
	       neg2[-2] - neg2[0] - neg2[2]);
      pixel = (pixel + 4) >> 3;
      if (pixel < 0)
	*dst++ = 0;
      else if (pixel < 255)
	*dst++ = pixel;
      else
	*dst++ = 255;

      *dst++ = *src++;
      neg0 += 2;
      neg1 += 2;
      neg2 += 2;
      width --;
    };

  *dst++ = *src++;
  *dst++ = *src++;
}

/*
 * 'rgb_filter()' - Sharpen RGB pixels.
 */

static void
rgb_filter (gint    width,	/* I - Width of line in pixels */
	    guchar *src,	/* I - Source line */
	    guchar *dst,	/* O - Destination line */
	    intneg *neg0,	/* I - Top negative coefficient line */
	    intneg *neg1,	/* I - Middle negative coefficient line */
	    intneg *neg2)	/* I - Bottom negative coefficient line */
{
  intpos pixel;		/* New pixel value */

  *dst++ = *src++;
  *dst++ = *src++;
  *dst++ = *src++;
  width -= 2;

  while (width > 0)
    {
      pixel = (pos_lut[*src++] - neg0[-3] - neg0[0] - neg0[3] -
	       neg1[-3] - neg1[3] -
	       neg2[-3] - neg2[0] - neg2[3]);
      pixel = (pixel + 4) >> 3;
      if (pixel < 0)
	*dst++ = 0;
      else if (pixel < 255)
	*dst++ = pixel;
      else
	*dst++ = 255;

      pixel = (pos_lut[*src++] - neg0[-2] - neg0[1] - neg0[4] -
	       neg1[-2] - neg1[4] -
	       neg2[-2] - neg2[1] - neg2[4]);
      pixel = (pixel + 4) >> 3;
      if (pixel < 0)
	*dst++ = 0;
      else if (pixel < 255)
	*dst++ = pixel;
      else
	*dst++ = 255;

      pixel = (pos_lut[*src++] - neg0[-1] - neg0[2] - neg0[5] -
	       neg1[-1] - neg1[5] -
	       neg2[-1] - neg2[2] - neg2[5]);
      pixel = (pixel + 4) >> 3;
      if (pixel < 0)
	*dst++ = 0;
      else if (pixel < 255)
	*dst++ = pixel;
      else
	*dst++ = 255;

      neg0 += 3;
      neg1 += 3;
      neg2 += 3;
      width --;
    };

  *dst++ = *src++;
  *dst++ = *src++;
  *dst++ = *src++;
}


/*
 * 'rgba_filter()' - Sharpen RGBA pixels.
 */

static void
rgba_filter (gint   width,	/* I - Width of line in pixels */
	     guchar *src,	/* I - Source line */
	     guchar *dst,	/* O - Destination line */
	     intneg *neg0,	/* I - Top negative coefficient line */
	     intneg *neg1,	/* I - Middle negative coefficient line */
	     intneg *neg2)	/* I - Bottom negative coefficient line */
{
  intpos pixel;		/* New pixel value */

  *dst++ = *src++;
  *dst++ = *src++;
  *dst++ = *src++;
  *dst++ = *src++;
  width -= 2;

  while (width > 0)
    {
      pixel = (pos_lut[*src++] - neg0[-4] - neg0[0] - neg0[4] -
	       neg1[-4] - neg1[4] -
	       neg2[-4] - neg2[0] - neg2[4]);
      pixel = (pixel + 4) >> 3;
      if (pixel < 0)
	*dst++ = 0;
      else if (pixel < 255)
	*dst++ = pixel;
      else
	*dst++ = 255;

      pixel = (pos_lut[*src++] - neg0[-3] - neg0[1] - neg0[5] -
	       neg1[-3] - neg1[5] -
	       neg2[-3] - neg2[1] - neg2[5]);
      pixel = (pixel + 4) >> 3;
      if (pixel < 0)
	*dst++ = 0;
      else if (pixel < 255)
	*dst++ = pixel;
      else
	*dst++ = 255;

      pixel = (pos_lut[*src++] - neg0[-2] - neg0[2] - neg0[6] -
	       neg1[-2] - neg1[6] -
	       neg2[-2] - neg2[2] - neg2[6]);
      pixel = (pixel + 4) >> 3;
      if (pixel < 0)
	*dst++ = 0;
      else if (pixel < 255)
	*dst++ = pixel;
      else
	*dst++ = 255;

      *dst++ = *src++;

      neg0 += 4;
      neg1 += 4;
      neg2 += 4;
      width --;
    };

  *dst++ = *src++;
  *dst++ = *src++;
  *dst++ = *src++;
  *dst++ = *src++;
}



int
max(int a, int b)
{
	if(a > b)
		return a;
	return b;
}


Memimage*
mknewimg(int xsize, int ysize, Memimage *m)
{
	int i, j, bpl, nchan;
	Memimage *new;

	new = allocmemimage(Rect(0, 0, xsize, ysize), m->chan);
	if(new == nil)
		sysfatal("can't allocate new image: %r");

	oscan = malloc(Dy(m->r)*sizeof(uchar*));
	nscan = malloc(max(ysize, Dy(m->r))*sizeof(uchar*));
	if(oscan == nil || nscan == nil)
		sysfatal("can't allocate: %r");

	/* unload original image into scan lines */
	bpl = bytesperline(m->r, m->depth);
	for(i=0; i<Dy(m->r); i++){
		oscan[i] = malloc(bpl);
		if(oscan[i] == nil)
			sysfatal("can't allocate: %r");
		j = unloadmemimage(m, Rect(m->r.min.x, m->r.min.y+i, m->r.max.x, m->r.min.y+i+1), oscan[i], bpl);
		if(j != bpl)
			sysfatal("unloadmemimage");
	}

	/* allocate scan lines for destination. we do y first, so need at least Dy(m->r) lines */
	bpl = bytesperline(Rect(0, 0, xsize, Dy(m->r)), m->depth);
	for(i=0; i<max(ysize, Dy(m->r)); i++){
		nscan[i] = malloc(bpl);
		if(nscan[i] == nil)
			sysfatal("can't allocate: %r");
	}

	nchan = m->depth/8;

	sharpen(xsize, ysize);



	/* pack data into destination */
	bpl = bytesperline(new->r, m->depth);
	for(i=0; i<ysize; i++){
		j = loadmemimage(new, Rect(0, i, xsize, i+1), nscan[i], bpl);
		if(j != bpl)
			sysfatal("loadmemimage: %r");
	}
	return new;
}



void
main(int argc, char *argv[])
{
	int i, fd,  dummy;
	Rectangle rparam;
	Memimage *m, *new, *t1, *t2;
	char *file;
	ulong tchan;
	char tmp[100];
	double v;

	memimageinit();
	memset(&rparam, 0, sizeof rparam);
	xsize = ysize = 0;


	file = "<stdin>";
	fd = 0;

	switch (argc) {
	case 2:
		file = argv[1];
		fd = open(file, OREAD);
		if(fd < 0)
			sysfatal("can't open %s: %r", file);
	case 1:
		break;
	default:
		usage();
	}

	m = readmemimage(fd);
	if(m == nil)
		sysfatal("can't read %s: %r", file);

	xsize = Dx(m->r);
	ysize = Dy(m->r);

	new = nil;
	switch(m->chan){

	case RGB24:
	case RGBA32:
	case ARGB32:
	case XRGB32:
		new = mknewimg(xsize, ysize, m);

		break;

	case GREY1:
	case GREY2:
	case GREY4:
	case GREY8:
	case CMAP8:
	case RGB15:
	case RGB16:
		tchan = RGB24;
		goto Convert;

	Convert:
		/* use library to convert to byte-per-chan form, then convert back */
		t1 = allocmemimage(m->r, tchan);
		if(t1 == nil)
			sysfatal("can't allocate temporary image: %r");
		memimagedraw(t1, t1->r, m, m->r.min, nil, ZP);
		t2 = mknewimg(xsize, ysize, t1);
		freememimage(t1);
		new = allocmemimage(Rect(0, 0, xsize, ysize), m->chan);
		if(new == nil)
			sysfatal("can't allocate new image: %r");
		/* should do error diffusion here */
		memimagedraw(new, new->r, t2, t2->r.min, nil, ZP);
		freememimage(t2);
		break;

	default:
		sysfatal("can't handle channel type %s", chantostr(tmp, m->chan));
	}
	assert(new);
// print("LAST POINT");
	if(writememimage(1, new) < 0)
		sysfatal("write error on output: %r");
// print("NOT REACHED");

	exits(nil);
}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-07-17  7:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-17  7:46 [9fans] Pls. help 2 catch a bug Peter A. Cejchan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).