[q-lang-cvs] q-audio/src draw_line.c,NONE,1.1 ggilib.c,NONE,1.1 ggilib.h,NONE,1.1 Makefile.am,1.2,1.
Brought to you by:
agraef
From: <ag...@us...> - 2003-12-23 01:20:52
|
Update of /cvsroot/q-lang/q-audio/src In directory sc8-pr-cvs1:/tmp/cvs-serv25197/src Modified Files: Makefile.am Makefile.msc audio_player.q draw_wave.c Added Files: draw_line.c ggilib.c ggilib.h Log Message: update for latest Q-GGI --- NEW FILE: draw_line.c --- /* line clipping algorithm, pilfered from GGI sources ************************/ /* $Id: draw_line.c,v 1.1 2003/12/23 01:20:48 agraef Exp $ ****************************************************************************** Graphics library for GGI. Copyright (C) 1998 Alexander Larsson [al...@ly...] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************** */ #include <ggi/internal/ggi-dl.h> /* This is a line-clipper using the algorithm by cohen-sutherland. It is modified to do pixel-perfect clipping. This means that it will generate the same endpoints that would be drawn if an ordinary bresenham line-drawer where used and only visible pixels drawn. It can be used with a bresenham-like linedrawer if it is modified to start with a correct error-term. */ #define OC_LEFT 1 #define OC_RIGHT 2 #define OC_TOP 4 #define OC_BOTTOM 8 /* Outcodes: +-> x | | | V 0101 | 0100 | 0110 y --------------------- 0001 | 0000 | 0010 --------------------- 1001 | 1000 | 1010 | | */ #define outcode(code, xx, yy) \ {\ code = 0;\ if ((xx)<(LIBGGI_GC(vis)->cliptl.x))\ code |= OC_LEFT;\ else if ((xx)>=(LIBGGI_GC(vis)->clipbr.x))\ code |= OC_RIGHT;\ if ((yy)<(LIBGGI_GC(vis)->cliptl.y))\ code |= OC_TOP;\ else if ((yy)>=(LIBGGI_GC(vis)->clipbr.y))\ code |= OC_BOTTOM;\ } /* Calculates |_ a/b _| with mathematically correct floor */ static int FloorDiv(int a, int b) { int _floor; if (b>0) { if (a>0) { return a /b; } else { _floor = -((-a)/b); if ((-a)%b != 0) _floor--; } return _floor; } else { if (a>0) { _floor = -(a/(-b)); if (a%(-b) != 0) _floor--; return _floor; } else { return (-a)/(-b); } } } /* Calculates |^ a/b ^| with mathamatically correct floor */ static int CeilDiv(int a,int b) { if (b>0) return FloorDiv(a-1,b)+1; else return FloorDiv(-a-1,-b)+1; } static int _ggi_clip2d(ggi_visual *vis,int *_x0, int *_y0, int *_x1, int *_y1, int *clip_first, int *clip_last) { int first,last, code; int x0,y0,x1,y1; int x,y; int dx,dy; int xmajor; int slope; *clip_first = first = 0; *clip_last = last = 0; outcode(first,*_x0,*_y0); outcode(last,*_x1,*_y1); if ((first | last) == 0) { return 1; /* Trivially accepted! */ } if ((first & last) != 0) { return 0; /* Trivially rejected! */ } x0=*_x0; y0=*_y0; x1=*_x1; y1=*_y1; dx = x1 - x0; dy = y1 - y0; xmajor = (abs(dx) > abs(dy)); slope = ((dx>=0) && (dy>=0)) || ((dx<0) && (dy<0)); for (;;) { code = first; if (first==0) code = last; if (code&OC_LEFT) { x = LIBGGI_GC(vis)->cliptl.x; if (xmajor) { y = *_y0 + FloorDiv(dy*(x - *_x0)*2 + dx, 2*dx); } else { if (slope) { y = *_y0 + CeilDiv(dy*((x - *_x0)*2 - 1), 2*dx); } else { y = *_y0 + FloorDiv(dy*((x - *_x0)*2 - 1), 2*dx); } } } else if (code&OC_RIGHT) { x = LIBGGI_GC(vis)->clipbr.x - 1; if (xmajor) { y = *_y0 + FloorDiv(dy*(x - *_x0)*2 + dx, 2*dx); } else { if (slope) { y = *_y0 + CeilDiv(dy*((x - *_x0)*2 + 1), 2*dx)-1; } else { y = *_y0 + FloorDiv(dy*((x - *_x0)*2 + 1), 2*dx)+1; } } } else if (code&OC_TOP) { y = LIBGGI_GC(vis)->cliptl.y; if (xmajor) { if (slope) { x = *_x0 + CeilDiv(dx*((y - *_y0)*2 - 1), 2*dy); } else { x = *_x0 + FloorDiv(dx*((y - *_y0)*2 - 1), 2*dy); } } else { x = *_x0 + FloorDiv( dx*(y - *_y0)*2 + dy, 2*dy); } } else { /* OC_BOTTOM */ y = LIBGGI_GC(vis)->clipbr.y - 1; if (xmajor) { if (slope) { x = *_x0 + CeilDiv(dx*((y - *_y0)*2 + 1), 2*dy)-1; } else { x = *_x0 + FloorDiv(dx*((y - *_y0)*2 + 1), 2*dy)+1; } } else { x = *_x0 + FloorDiv(dx*(y - *_y0)*2 + dy, 2*dy); } } if (first!=0) { x0 = x; y0 = y; outcode(first,x0,y0); *clip_first = 1; } else { x1 = x; y1 = y; last = code; outcode(last,x1,y1); *clip_last = 1; } if ((first & last) != 0) { return 0; /* Trivially rejected! */ } if ((first | last) == 0) { *_x0=x0; *_y0=y0; *_x1=x1; *_y1=y1; return 1; /* Trivially accepted! */ } } } /* line drawing algorithm from GGI sources, slightly modified for Q-GGI ******/ /* $Id: draw_line.c,v 1.1 2003/12/23 01:20:48 agraef Exp $ ****************************************************************************** Graphics library for GGI. Copyright (C) 1995 Andreas Beck [be...@gg...] Copyright (C) 1997 Jason McMullan [jm...@gg...] Copyright (C) 1998 Alexander Larsson [al...@ly...] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************** */ typedef struct { ggi_visual_t vis; } visual_t; extern int draw_box(visual_t *v, int x, int y, int w, int h); static inline int DrawHLine(visual_t *v, int x, int y, int w) { return (draw_box(v,x,y,w,1) >= 0)?0:-1; } static inline int DrawVLine(visual_t *v, int x, int y, int h) { return (draw_box(v,x,y,1,h) >= 0)?0:-1; } static inline int DrawPixel(visual_t *v, int x, int y) { return (draw_box(v,x,y,1,1) >= 0)?0:-1; } int draw_line(visual_t *v, int orig_x1, int orig_y1, int orig_x2, int orig_y2) { ggi_visual *vis = v->vis; int orig_dx, orig_dy, sx, sy; int dx,dy; int i; int x1, y1, x2, y2; int clip_first, clip_last; x1 = orig_x1; y1 = orig_y1; x2 = orig_x2; y2 = orig_y2; /* clip x1,y1 and x2,y2. Set clip_first and clip_last if clipped */ if (!_ggi_clip2d(vis, &x1,&y1,&x2,&y2,&clip_first,&clip_last)) { return 0; /* Clipped */ } dy = y2 - y1; orig_dy = orig_y2 - orig_y1; sy = 1; if (orig_dy < 0) { orig_dy = -orig_dy; dy = -dy; sy = -1; } dx = x2-x1; orig_dx = orig_x2 - orig_x1; sx = 1; if (orig_dx < 0) { sx = -1; orig_dx = -orig_dx; dx = -dx; } if (dx == 0) { return (sy>0) ? DrawVLine(v,x1,y1,dy+1) : DrawVLine(v,x2,y2,dy+1); } if (dy == 0) { return (sx>0) ? DrawHLine(v,x1,y1,dx+1) : DrawHLine(v,x2,y2,dx+1); } if (orig_dx == orig_dy) { for (i=dx; i >= 0; i--) { DrawPixel(v, x1, y1); x1 += sx; y1 += sy; } return 0; } if (orig_dx >= orig_dy) { /* x major */ int runlen,adjup,adjdown,e,len; int firstlen,lastlen; runlen = orig_dx/orig_dy; adjup = orig_dx%orig_dy; lastlen = firstlen = (runlen>>1) + 1; if (clip_first) { /* clipped, Adjust firstlen */ int clip_dx = abs(x1 - orig_x1); int clip_dy = abs(y1 - orig_y1); int d = (2*clip_dy+1)*orig_dx; firstlen = d/(2*orig_dy) - clip_dx + 1; e = d%(2*orig_dy); if ((e==0) && (sy>0)) { /* Special case, arbitrary choise. Select lower pixel.(?) */ firstlen--; e += 2*orig_dy; } e -= (orig_dy*2); } else { /* Not clipped, calculate start error term */ e = adjup - (orig_dy<<1); /* initial errorterm == half a step */ if ((runlen&1) != 0) { e += orig_dy; } } if (clip_last) { /* Last endpoint clipped */ int clip_dx = abs(x2 - orig_x2); int clip_dy = abs(y2 - orig_y2); int d = (1+2*clip_dy)*orig_dx; lastlen = d/(2*orig_dy) - clip_dx + 1; if ((sy<0) && ((d%(2*orig_dy))==0)) { /* special arbitrary case */ lastlen--; } } adjup <<= 1; adjdown = orig_dy<<1; if (sy>0) { /* line goes down */ if ((adjup==0) && ((runlen&1)==0) && (!clip_first)) { firstlen--; } if (sx>0) { /* line goes right */ DrawHLine(v,x1,y1,firstlen); x1 += firstlen; y1 ++; for (i=dy-1; i>0; i--) { len = runlen; e += adjup; if (e>0) { len++; e -= adjdown; } DrawHLine(v,x1,y1,len); x1 += len; y1++; } DrawHLine(v,x1,y1,lastlen); return 0; } else { /* line goes left */ x1++; /* because ggiDrawHLine draws right */ x1 -= firstlen; DrawHLine(v,x1,y1,firstlen); y1++; for (i=dy-1; i>0; i--) { len = runlen; e += adjup; if (e>0) { len++; e -= adjdown; } x1 -= len; DrawHLine(v,x1,y1,len); y1 ++; } x1 -= lastlen; DrawHLine(v,x1,y1,lastlen); return 0; } } else { /* line goes up */ if ((adjup==0) && ((runlen&1)==0) && (!clip_last)) { lastlen--; } if (sx>0) { /* line goes right */ DrawHLine(v,x1,y1,firstlen); x1 += firstlen; y1--; for (i=dy-1; i>0; i--) { len = runlen; e += adjup; if (e>=0) { len++; e -= adjdown; } DrawHLine(v,x1,y1,len); x1 += len; y1--; } DrawHLine(v,x1,y1,lastlen); return 0; } else { /* line goes left */ x1++; /* because ggiDrawHLine draws right */ x1 -= firstlen; DrawHLine(v,x1,y1,firstlen); y1--; for (i=dy-1; i>0; i--) { len = runlen; e += adjup; if (e>=0) { len++; e -= adjdown; } x1 -= len; DrawHLine(v,x1,y1,len); y1--; } x1 -= lastlen; DrawHLine(v,x1,y1,lastlen); return 0; } } } else { /* y major */ int runlen,adjup,adjdown,e,len; int firstlen,lastlen; runlen = orig_dy/orig_dx; adjup = orig_dy%orig_dx; lastlen = firstlen = (runlen>>1) + 1; if (clip_first) { /* clipped, Adjust firstlen */ int clip_dx = abs(x1 - orig_x1); int clip_dy = abs(y1 - orig_y1); int d = (2*clip_dx+1)*orig_dy; firstlen = d/(2*orig_dx) - clip_dy + 1; e = d%(2*orig_dx); if ((e==0) && (sx>0)) { /* Special case, arbitrary choise. Select lower pixel.(?) */ firstlen--; e += 2*orig_dx; } e -= (orig_dx*2); } else { /* Not clipped, calculate start error term */ e = adjup - (orig_dx<<1); /* initial errorterm == half a step */ if ((runlen&1) != 0) { e += orig_dx; } } if (clip_last) { /* Last endpoint clipped */ int clip_dx = abs(x2 - orig_x2); int clip_dy = abs(y2 - orig_y2); int d = (1+2*clip_dx)*orig_dy; lastlen = d/(2*orig_dx) - clip_dy + 1; if ((sx<0) && ((d%(2*orig_dx))==0)) { /* special arbitrary case */ lastlen--; } } adjup <<= 1; adjdown = orig_dx<<1; if (sy>0) { /* Line goes DOWN */ if (sx>0) { /* line goes RIGHT */ if ((adjup==0) && ((runlen&1)==0) && (!clip_first)) { firstlen--; } DrawVLine(v,x1,y1,firstlen); y1 += firstlen; x1++; for (i=dx-1; i>0; i--) { len = runlen; e += adjup; if (e>0) { len++; e -= adjdown; } DrawVLine(v,x1,y1,len); y1 += len; x1++; } DrawVLine(v,x1,y1,lastlen); return 0; } else { /* line goes LEFT */ if ((adjup==0) && ((runlen&1)==0) && (!clip_last)) { lastlen--; } DrawVLine(v,x1,y1,firstlen); y1 += firstlen; x1--; for (i=dx-1; i>0; i--) { len = runlen; e += adjup; if (e>=0) { len++; e -= adjdown; } DrawVLine(v,x1,y1,len); y1 += len; x1--; } DrawVLine(v,x1,y1,lastlen); return 0; } } else { /* Line goes UP */ y1++; if (sx>0) { /* line goes RIGHT */ if ((adjup==0) && ((runlen&1)==0) && (!clip_first)) { firstlen--; } y1 -= firstlen; DrawVLine(v,x1,y1,firstlen); x1++; for (i=dx-1; i>0; i--) { len = runlen; e += adjup; if (e>0) { len++; e -= adjdown; } y1 -= len; DrawVLine(v,x1,y1,len); x1++; } y1 -= lastlen; DrawVLine(v,x1,y1,lastlen); return 0; } else { /* line goes LEFT */ if ((adjup==0) && ((runlen&1)==0) && (!clip_last)) { lastlen--; } y1 -= firstlen; DrawVLine(v,x1,y1,firstlen); x1--; for (i=dx-1; i>0; i--) { len = runlen; e += adjup; if (e>=0) { len++; e -= adjdown; } y1 -= len; DrawVLine(v,x1,y1,len); x1--; } y1 -= lastlen; DrawVLine(v,x1,y1,lastlen); return 0; } } } } /*****************************************************************************/ --- NEW FILE: ggilib.c --- /* This file is part of the Q programming system. The Q programming system 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 2, or (at your option) any later version. The Q programming system 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; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <limits.h> #include "ggilib.h" typedef struct { ggi_visual_t vis; void *abuf; unsigned char asz; unsigned short afg, abg; int stride, lines; } visual_t; /* calculate the size of a pixel pack */ static inline int pack_size(ggi_visual_t vis, int n) { int bpp, r; ggi_mode m; if (ggiGetMode(vis, &m)) return -1; bpp = GT_SIZE(m.graphtype); if (n > INT_MAX / bpp) return -1; r = ((bpp*n)%8>0)?1:0; return (bpp*n)/8+r; } /* clear clip area of alpha buffer */ int clear_alpha_buffer(void *_v) { visual_t *v = (visual_t*)_v; int x1, y1, x2, y2; if (ggiGetGCClipping(v->vis, &x1, &y1, &x2, &y2)) return -1; if (v->asz == 1) { unsigned char *abuf = (unsigned char*)v->abuf; int i, y; for (i = y1*v->stride+x1, y = y1; y < y2; y++, i += v->stride) memset(abuf+i, v->abg, x2-x1); } else { unsigned short *abuf = (unsigned short*)v->abuf; int i, j, x, y; for (i = y1*v->stride+x1, y = y1; y < y2; y++, i += v->stride) for (j = i, x = x1; x < x2; x++, j++) abuf[j] = v->abg; } return 0; } /* set alpha values, with clipping */ int set_alpha_box(void *_v, int x, int y, int w, int h, ggi_color *c) { visual_t *v = (visual_t*)_v; int x1, y1, x2, y2; int i, j, k, xx, yy; if (ggiGetGCClipping(v->vis, &x1, &y1, &x2, &y2)) return -1; if (v->asz == 1) { unsigned char *abuf = (unsigned char*)v->abuf; /* FIXME: optimize me! */ for (k = 0, i = y*v->stride+x, yy = y; yy < y+h; yy++, i += v->stride) for (j = i, xx = x; xx < x+w; xx++, j++, k++) if (xx >= x1 && xx < x2 && yy >= y1 && yy < y2) abuf[j] = c[k].a/0x101; } else { unsigned short *abuf = (unsigned short*)v->abuf; /* FIXME: optimize me! */ for (k = 0, i = y*v->stride+x, yy = y; yy < y+h; yy++, i += v->stride) for (j = i, xx = x; xx < x+w; xx++, j++, k++) if (xx >= x1 && xx < x2 && yy >= y1 && yy < y2) abuf[j] = c[k].a; } return 0; } /* same as above, but use current foreground color */ int draw_alpha_box(void *_v, int x, int y, int w, int h) { visual_t *v = (visual_t*)_v; int x1, y1, x2, y2; int i, j, xx, yy; if (ggiGetGCClipping(v->vis, &x1, &y1, &x2, &y2)) return -1; if (v->asz == 1) { unsigned char *abuf = (unsigned char*)v->abuf; /* FIXME: optimize me! */ for (i = y*v->stride+x, yy = y; yy < y+h; yy++, i += v->stride) for (j = i, xx = x; xx < x+w; xx++, j++) if (xx >= x1 && xx < x2 && yy >= y1 && yy < y2) abuf[j] = v->afg; } else { unsigned short *abuf = (unsigned short*)v->abuf; /* FIXME: optimize me! */ for (i = y*v->stride+x, yy = y; yy < y+h; yy++, i += v->stride) for (j = i, xx = x; xx < x+w; xx++, j++) if (xx >= x1 && xx < x2 && yy >= y1 && yy < y2) abuf[j] = v->afg; } return 0; } /* retrieve a rectangle of pixels from a visual, including alpha values */ int get_box(void *_v, int x, int y, int w, int h, ggi_color **c) { visual_t *v = (visual_t*)_v; int n = w*h, bpp, stride, ret; int xc = x, yc = y, wc = w, hc = h; int i, j, k, xx, yy; unsigned char *buf; ggi_mode m; *c = NULL; /* FIXME: we don't support packed non-byte-aligned pixels for now */ if (ggiGetMode(v->vis, &m) || (bpp = GT_SIZE(m.graphtype))%8 != 0) return 0; if (x >= v->stride || y >= v->lines || h <= 0 || w <= 0) return n; if (w > INT_MAX/h) return -1; if (!(buf = malloc(pack_size(v->vis, n)))) return -1; if (n > INT_MAX/sizeof(ggi_color) || !(*c = malloc(n*sizeof(ggi_color)))) { free(buf); return -1; } /* we do our own clipping here, since GGI doesn't handle it :( */ memset(buf, 0, n); memset(*c, 0, n*sizeof(ggi_color)); if (xc < 0) { wc += xc; xc = 0; } if (xc+wc > v->stride) wc = v->stride - xc; if (yc < 0) { hc += yc; yc = 0; } if (yc+hc > v->lines) hc = v->lines - yc; bpp /= 8; stride = w*bpp; if (xc == x && wc == w) { ret = ggiGetBox(v->vis, xc, yc, wc, hc, buf+(yc-y)*stride); } else { /* horizontal clipping, read the pixels line by line */ unsigned char *bufp; int off = (xc-x)*bpp; for (bufp = buf+(yc-y)*stride, yy = yc; yy < yc+hc; yy++, bufp += stride) if ((ret = ggiGetHLine(v->vis, xc, yy, wc, bufp+off))) break; } if (ret) { free(buf); free(*c); return 0; } if (ggiUnpackPixels(v->vis, buf, *c, n)) { free(buf); free(*c); return 0; } free(buf); /* get the alpha values from the alpha buffer if any */ if (v->abuf) { if (v->asz == 1) { unsigned char *abuf = (unsigned char*)v->abuf; /* FIXME: optimize me! */ for (i = y*v->stride+x, k = 0, yy = y; yy < y+h; yy++, i += v->stride) for (j = i, xx = x; xx < x+w; xx++, j++) if (xx >= 0 && xx < v->stride && yy >= 0 && yy < v->lines) (*c)[k++].a = abuf[j]*0x101; else (*c)[k++].a = 0xffff; } else { unsigned short *abuf = (unsigned short*)v->abuf; /* FIXME: optimize me! */ for (i = y*v->stride+x, k = 0, yy = y; yy < y+h; yy++, i += v->stride) for (j = i, xx = x; xx < x+w; xx++, j++) if (xx >= 0 && xx < v->stride && yy >= 0 && yy < v->lines) (*c)[k++].a = abuf[j]; else (*c)[k++].a = 0xffff; } } else { /* default alpha values */ int i; for (i = 0; i < n; i++) (*c)[i].a = 0xffff; } return n; } /* perform alpha blending */ static int blend_box(visual_t *v, int x, int y, int w, int h, ggi_color *c, ggi_color **d) { int i, n = w*h; *d = c; if (!v->abuf) return n; if ((n = get_box(v, x, y, w, h, d)) <= 0 || !(*d)) return n; for (i = 0; i < n; i++) { (*d)[i].r = (((unsigned long)c[i].a)*((unsigned long)c[i].r) + ((unsigned long)0xffff-c[i].a)*((unsigned long)(*d)[i].r))/0xffff; (*d)[i].g = (((unsigned long)c[i].a)*((unsigned long)c[i].g) + ((unsigned long)0xffff-c[i].a)*((unsigned long)(*d)[i].g))/0xffff; (*d)[i].b = (((unsigned long)c[i].a)*((unsigned long)c[i].b) + ((unsigned long)0xffff-c[i].a)*((unsigned long)(*d)[i].b))/0xffff; (*d)[i].a = (0xffffL*((unsigned long)c[i].a) + ((unsigned long)0xffff-c[i].a)*((unsigned long)(*d)[i].a))/0xffff; } return n; } /* same, for single source pixel */ static int blend_box1(visual_t *v, int x, int y, int w, int h, ggi_color *c, ggi_color **d) { int i, n = w*h; *d = NULL; if (!v->abuf || c->a == 0xffff) return n; if ((n = get_box(v, x, y, w, h, d)) <= 0 || !(*d)) return n; for (i = 0; i < n; i++) { (*d)[i].r = (((unsigned long)c->a)*((unsigned long)c->r) + ((unsigned long)0xffff-c->a)*((unsigned long)(*d)[i].r))/0xffff; (*d)[i].g = (((unsigned long)c->a)*((unsigned long)c->g) + ((unsigned long)0xffff-c->a)*((unsigned long)(*d)[i].g))/0xffff; (*d)[i].b = (((unsigned long)c->a)*((unsigned long)c->b) + ((unsigned long)0xffff-c->a)*((unsigned long)(*d)[i].b))/0xffff; (*d)[i].a = (0xffffL*((unsigned long)c->a) + ((unsigned long)0xffff-c->a)*((unsigned long)(*d)[i].a))/0xffff; } return n; } /* store a rectangle of pixels in a visual, with alpha blending */ int put_box(void *_v, int x, int y, int w, int h, ggi_color *c) { visual_t *v = (visual_t*)_v; int n = w*h, ret; unsigned char *buf; ggi_color *d = c; if (h <= 0 || w <= 0) return 0; if (w > INT_MAX/h) return -1; if ((ret = blend_box(v, x, y, w, h, c, &d)) <= 0 || !d) return ret; if (!(buf = malloc(pack_size(v->vis, n)))) return -1; ret = ggiPackColors(v->vis, buf, d, n) || ggiPutBox(v->vis, x, y, w, h, buf); free(buf); if (!ret && v->abuf) ret = set_alpha_box(v, x, y, w, h, d); if (d != c) free(d); if (ret) return 0; else return n; } /* same, with current foreground pixel */ int draw_box(void *_v, int x, int y, int w, int h) { visual_t *v = (visual_t*)_v; int n = w*h, ret; unsigned char *buf; ggi_pixel pix; ggi_color c, *d = NULL; if (h <= 0 || w <= 0) return 0; if (ggiGetGCForeground(v->vis, &pix) || ggiUnmapPixel(v->vis, pix, &c)) return 0; if (v->abuf) c.a = (v->asz==1)?(v->afg*0x101):v->afg; else c.a = 0xffff; if (w > INT_MAX/h) return -1; if ((ret = blend_box1(v, x, y, w, h, &c, &d)) <= 0) return ret; if (d) { if (!(buf = malloc(pack_size(v->vis, n)))) return -1; ret = ggiPackColors(v->vis, buf, d, n) || ggiPutBox(v->vis, x, y, w, h, buf); free(buf); if (!ret && v->abuf) ret = set_alpha_box(v, x, y, w, h, d); free(d); } else { ret = ggiDrawBox(v->vis, x, y, w, h); if (!ret && v->abuf) ret = draw_alpha_box(v, x, y, w, h); } if (ret) return 0; else return n; } --- NEW FILE: ggilib.h --- /* C interface to some of the internals of the Q-GGI module, to facilitate access for "third-party" extensions like the draw_wave module. 2003-12-23 AG */ /* This file is part of the Q programming system. The Q programming system 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 2, or (at your option) any later version. The Q programming system 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; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef __GGILIB_H__ #define __GGILIB_H__ 1 #include <ggi/ggi.h> /* NOTE: The v argument of the following operations must point to a valid GGIVisual object, as obtained from the object pointer of a GGIVisual expression. */ /* the following operations return a negative value on error, zero otherwise */ /* clear clip area of alpha buffer */ extern int clear_alpha_buffer(void *v); /* set alpha values, with clipping */ extern int set_alpha_box(void *v, int x, int y, int w, int h, ggi_color *c); /* same as above, but use current foreground color */ extern int draw_alpha_box(void *v, int x, int y, int w, int h); /* the following operations return the number of processed pixels if successful, negative otherwise */ /* retrieve a rectangle of pixels from a visual, including alpha values (storage pointed to by c is malloc'ed, user must free it) */ extern int get_box(void *v, int x, int y, int w, int h, ggi_color **c); /* store a rectangle of pixels in a visual, with alpha blending */ extern int put_box(void *v, int x, int y, int w, int h, ggi_color *c); /* same, with current foreground pixel */ extern int draw_box(void *v, int x, int y, int w, int h); /* for this one, you need to link in draw_line.c; returns nonzero iff error */ extern int draw_line(void *v, int x1, int y1, int x2, int y2); /* convenience inline functions, to process horizontal and vertical lines and single pixels */ static inline int set_alpha_hline(void *v, int x, int y, int w, ggi_color *c) { return set_alpha_box(v, x, y, w, 1, c); } static inline int set_alpha_vline(void *v, int x, int y, int h, ggi_color *c) { return set_alpha_box(v, x, y, 1, h, c); } static inline int set_alpha_pixel(void *v, int x, int y, ggi_color *c) { return set_alpha_box(v, x, y, 1, 1, c); } static inline int draw_alpha_hline(void *v, int x, int y, int w) { return draw_alpha_box(v, x, y, w, 1); } static inline int draw_alpha_vline(void *v, int x, int y, int h) { return draw_alpha_box(v, x, y, 1, h); } static inline int draw_alpha_pixel(void *v, int x, int y) { return draw_alpha_box(v, x, y, 1, 1); } static inline int get_hline(void *v, int x, int y, int w, ggi_color **c) { return get_box(v, x, y, w, 1, c); } static inline int get_vline(void *v, int x, int y, int h, ggi_color **c) { return get_box(v, x, y, 1, h, c); } static inline int get_pixel(void *v, int x, int y, ggi_color **c) { return get_box(v, x, y, 1, 1, c); } static inline int put_hline(void *v, int x, int y, int w, ggi_color *c) { return put_box(v, x, y, w, 1, c); } static inline int put_vline(void *v, int x, int y, int h, ggi_color *c) { return put_box(v, x, y, 1, h, c); } static inline int put_pixel(void *v, int x, int y, ggi_color *c) { return put_box(v, x, y, 1, 1, c); } static inline int draw_hline(void *v, int x, int y, int w) { return draw_box(v, x, y, w, 1); } static inline int draw_vline(void *v, int x, int y, int h) { return draw_box(v, x, y, 1, h); } static inline int draw_pixel(void *v, int x, int y) { return draw_box(v, x, y, 1, 1); } #endif Index: Makefile.am =================================================================== RCS file: /cvsroot/q-lang/q-audio/src/Makefile.am,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Makefile.am 15 Dec 2003 12:25:16 -0000 1.2 --- Makefile.am 23 Dec 2003 01:20:48 -0000 1.3 *************** *** 40,44 **** sndfile_la_LIBADD = -lsndfile @LIBDMALLOCTH@ ! draw_wave_la_SOURCES = draw_wave.c draw_wave_la_LDFLAGS = -no-undefined -module -avoid-version draw_wave_la_LIBADD = -lggi @LIBDMALLOCTH@ --- 40,44 ---- sndfile_la_LIBADD = -lsndfile @LIBDMALLOCTH@ ! draw_wave_la_SOURCES = draw_wave.c ggilib.h ggilib.c draw_line.c draw_wave_la_LDFLAGS = -no-undefined -module -avoid-version draw_wave_la_LIBADD = -lggi @LIBDMALLOCTH@ Index: Makefile.msc =================================================================== RCS file: /cvsroot/q-lang/q-audio/src/Makefile.msc,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Makefile.msc 15 Dec 2003 12:25:16 -0000 1.2 --- Makefile.msc 23 Dec 2003 01:20:48 -0000 1.3 *************** *** 44,49 **** # NOTE: this one is compiled using mingw ! draw_wave.dll: draw_wave.c ! $(QCC) $(QCCFLAGS) -o draw_wave.dll draw_wave.c -- $(CFLAGS) $(DEFS) -I. -I../portaudio/pa_common -I../../ggi/include --link -L../../ggi/lib -lggi -L../../q-src/libq clean mostlyclean distclean maintainer-clean:: --- 44,49 ---- # NOTE: this one is compiled using mingw ! draw_wave.dll: draw_wave.c ggilib.h ggilib.c draw_line.c ! $(QCC) $(QCCFLAGS) -o draw_wave.dll draw_wave.c ggilib.c draw_line.c -- $(CFLAGS) $(DEFS) -I. -I../portaudio/pa_common -I../../ggi/include --link -L../../ggi/lib -lggi -L../../q-src/libq clean mostlyclean distclean maintainer-clean:: Index: audio_player.q =================================================================== RCS file: /cvsroot/q-lang/q-audio/src/audio_player.q,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** audio_player.q 15 Dec 2003 12:33:33 -0000 1.1 --- audio_player.q 23 Dec 2003 01:20:48 -0000 1.2 *************** *** 207,213 **** /* Initialize the display. */ ! init_display VIS = ggi_set_foreground VIS BACK || ! ggi_fill_screen VIS || ! ggi_flush VIS; /* Update the display. */ --- 207,211 ---- /* Initialize the display. */ ! init_display VIS = ggi_clear VIS || ggi_flush VIS; /* Update the display. */ Index: draw_wave.c =================================================================== RCS file: /cvsroot/q-lang/q-audio/src/draw_wave.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** draw_wave.c 15 Dec 2003 12:33:33 -0000 1.1 --- draw_wave.c 23 Dec 2003 01:20:48 -0000 1.2 *************** *** 65,69 **** #include <libq.h> #include <portaudio.h> ! #include <ggi/ggi.h> MODULE(draw_wave) --- 65,69 ---- #include <libq.h> #include <portaudio.h> ! #include "ggilib.h" MODULE(draw_wave) *************** *** 74,77 **** --- 74,81 ---- } bstr_t; + typedef struct { + ggi_visual_t vis; + } visual_t; + static void shade(ggi_color *c) { *************** *** 87,97 **** FUNCTION(draw_wave,draw_wave,argc,argv) { ! ggi_visual_t *vis; expr *xv; int n; long x, y, w, h, channels, format, chan; bstr_t *m; ! if (argc == 7 && isobj(argv[0], type(GGIVisual), (void**)&vis) && ! *vis && istuple(argv[1], &n, &xv) && n == 2 && isint(xv[0], &x) && isint(xv[1], &y) && istuple(argv[2], &n, &xv) && n == 2 && --- 91,102 ---- FUNCTION(draw_wave,draw_wave,argc,argv) { ! visual_t *v; ! ggi_visual_t vis; expr *xv; int n; long x, y, w, h, channels, format, chan; bstr_t *m; ! if (argc == 7 && isobj(argv[0], type(GGIVisual), (void**)&v) && ! (vis = v->vis) && istuple(argv[1], &n, &xv) && n == 2 && isint(xv[0], &x) && isint(xv[1], &y) && istuple(argv[2], &n, &xv) && n == 2 && *************** *** 125,131 **** int val = (int)((data[i+chan]+1.0f)/2.0f*(h-1)); int xe = (int)x+k*dx, ye = y+h-1-val; ! if (k > 0 && ggiDrawLine(*vis, xa, ya, xe, ye)) return __FAIL; ! if (show_dots && ggiDrawBox(*vis, xe-1, ye-1, 3, 3)) return __FAIL; xa = xe; ya = ye; --- 130,136 ---- int val = (int)((data[i+chan]+1.0f)/2.0f*(h-1)); int xe = (int)x+k*dx, ye = y+h-1-val; ! if (k > 0 && draw_line(v, xa, ya, xe, ye)) return __FAIL; ! if (show_dots && draw_box(v, xe-1, ye-1, 3, 3) < 0) return __FAIL; xa = xe; ya = ye; *************** *** 141,147 **** int val = (int)((samp+1.0)/2.0*(h-1)); int xe = (int)x+k*dx, ye = y+h-1-val; ! if (k > 0 && ggiDrawLine(*vis, xa, ya, xe, ye)) return __FAIL; ! if (show_dots && ggiDrawBox(*vis, xe-1, ye-1, 3, 3)) return __FAIL; xa = xe; ya = ye; --- 146,152 ---- int val = (int)((samp+1.0)/2.0*(h-1)); int xe = (int)x+k*dx, ye = y+h-1-val; ! if (k > 0 && draw_line(v, xa, ya, xe, ye)) return __FAIL; ! if (show_dots && draw_box(v, xe-1, ye-1, 3, 3) < 0) return __FAIL; xa = xe; ya = ye; *************** *** 157,163 **** int val = (int)((samp+1.0)/2.0*(h-1)); int xe = (int)x+k*dx, ye = y+h-1-val; ! if (k > 0 && ggiDrawLine(*vis, xa, ya, xe, ye)) return __FAIL; ! if (show_dots && ggiDrawBox(*vis, xe-1, ye-1, 3, 3)) return __FAIL; xa = xe; ya = ye; --- 162,168 ---- int val = (int)((samp+1.0)/2.0*(h-1)); int xe = (int)x+k*dx, ye = y+h-1-val; ! if (k > 0 && draw_line(v, xa, ya, xe, ye)) return __FAIL; ! if (show_dots && draw_box(v, xe-1, ye-1, 3, 3) < 0) return __FAIL; xa = xe; ya = ye; *************** *** 277,299 **** } } ! ggiGetGCForeground(*vis, &pix); ! if (ggiUnmapPixel(*vis, pix, &col)) return __FAIL; shade(&col); ! pix2 = ggiMapColor(*vis, &col); for (k = 0; k < vals; k++) { int xa, xe = (int)x+(k+1)*dx; for (xa = (int)x+k*dx; xa < xe; xa++) ! if (ggiDrawVLine(*vis, xa, y+h-1-max[k], max[k]-min[k]+1)) return __FAIL; } ! ggiSetGCForeground(*vis, pix2); for (k = 0; k < vals; k++) { int xa, xe = (int)x+(k+1)*dx; if (rmsl[k] <= rmsh[k]) for (xa = (int)x+k*dx; xa < xe; xa++) ! if (ggiDrawVLine(*vis, xa, y+h-1-rmsh[k], rmsh[k]-rmsl[k]+1)) return __FAIL; } ! ggiSetGCForeground(*vis, pix); free(min); free(max); free(rmsh); free(rmsl); #ifdef DEBUG --- 282,304 ---- } } ! ggiGetGCForeground(vis, &pix); ! if (ggiUnmapPixel(vis, pix, &col)) return __FAIL; shade(&col); ! pix2 = ggiMapColor(vis, &col); for (k = 0; k < vals; k++) { int xa, xe = (int)x+(k+1)*dx; for (xa = (int)x+k*dx; xa < xe; xa++) ! if (draw_vline(v, xa, y+h-1-max[k], max[k]-min[k]+1) < 0) return __FAIL; } ! ggiSetGCForeground(vis, pix2); for (k = 0; k < vals; k++) { int xa, xe = (int)x+(k+1)*dx; if (rmsl[k] <= rmsh[k]) for (xa = (int)x+k*dx; xa < xe; xa++) ! if (draw_vline(v, xa, y+h-1-rmsh[k], rmsh[k]-rmsl[k]+1) < 0) return __FAIL; } ! ggiSetGCForeground(vis, pix); free(min); free(max); free(rmsh); free(rmsl); #ifdef DEBUG |