From mboxrd@z Thu Jan 1 00:00:00 1970 To: 9fans@cse.psu.edu Message-ID: <20020717094631.A201@next.gli.cas.cz> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="oyUTqETQ0mS9luUI" Content-Disposition: inline User-Agent: Mutt/1.2.5i From: "Peter A. Cejchan" Subject: [9fans] Pls. help 2 catch a bug Date: Wed, 17 Jul 2002 09:46:31 +0200 Topicbox-Message-UUID: cefc519c-eaca-11e9-9e20-41e7f4b1d025 --oyUTqETQ0mS9luUI Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 [http | ftp]://next.gli.cas.cz --oyUTqETQ0mS9luUI Content-Type: multipart/mixed; boundary="upas-hfjwnvtsrgcrjwucpyybnbavfy" Content-Disposition: inline This is a multi-part message in MIME format. --upas-hfjwnvtsrgcrjwucpyybnbavfy Content-Disposition: inline Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit 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" --upas-hfjwnvtsrgcrjwucpyybnbavfy Content-Type: application/octet-stream Content-Disposition: attachment; filename="sharpen.c.suspect" // 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 // License: GPL // Version 1.00 //////////////////////////////////////////////////////////////////////////////////////// // Please, search for "NOT REACHED" to see the probable location of the bug //////////////////////////////////////////////////////////////////////////////////////// #include #include #include #include #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 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; ir); 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; ir)); 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; ir); 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); } --upas-hfjwnvtsrgcrjwucpyybnbavfy-- --oyUTqETQ0mS9luUI--