Exemple #1
0
static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
                                       SingleChannelElement *sce,
                                       const float lambda)
{
    int start = 0, i, w, w2, g;
    float uplim[128], maxq[128];
    int minq, maxsf;
    float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
    int last = 0, lastband = 0, curband = 0;
    float avg_energy = 0.0;
    if (sce->ics.num_windows == 1) {
        start = 0;
        for (i = 0; i < 1024; i++) {
            if (i - start >= sce->ics.swb_sizes[curband]) {
                start += sce->ics.swb_sizes[curband];
                curband++;
            }
            if (sce->coeffs[i]) {
                avg_energy += sce->coeffs[i] * sce->coeffs[i];
                last = i;
                lastband = curband;
            }
        }
    } else {
        for (w = 0; w < 8; w++) {
            const float *coeffs = sce->coeffs + w*128;
            curband = start = 0;
            for (i = 0; i < 128; i++) {
                if (i - start >= sce->ics.swb_sizes[curband]) {
                    start += sce->ics.swb_sizes[curband];
                    curband++;
                }
                if (coeffs[i]) {
                    avg_energy += coeffs[i] * coeffs[i];
                    last = FFMAX(last, i);
                    lastband = FFMAX(lastband, curband);
                }
            }
        }
    }
    last++;
    avg_energy /= last;
    if (avg_energy == 0.0f) {
        for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
            sce->sf_idx[i] = SCALE_ONE_POS;
        return;
    }
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
        start = w*128;
        for (g = 0; g < sce->ics.num_swb; g++) {
            float *coefs   = sce->coeffs + start;
            const int size = sce->ics.swb_sizes[g];
            int start2 = start, end2 = start + size, peakpos = start;
            float maxval = -1, thr = 0.0f, t;
            maxq[w*16+g] = 0.0f;
            if (g > lastband) {
                maxq[w*16+g] = 0.0f;
                start += size;
                for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
                    memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
                continue;
            }
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
                for (i = 0; i < size; i++) {
                    float t = coefs[w2*128+i]*coefs[w2*128+i];
                    maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
                    thr += t;
                    if (sce->ics.num_windows == 1 && maxval < t) {
                        maxval  = t;
                        peakpos = start+i;
                    }
                }
            }
            if (sce->ics.num_windows == 1) {
                start2 = FFMAX(peakpos - 2, start2);
                end2   = FFMIN(peakpos + 3, end2);
            } else {
                start2 -= start;
                end2   -= start;
            }
            start += size;
            thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
            t   = 1.0 - (1.0 * start2 / last);
            uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
        }
    }
    memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
        start = w*128;
        for (g = 0;  g < sce->ics.num_swb; g++) {
            const float *coefs  = sce->coeffs + start;
            const float *scaled = s->scoefs   + start;
            const int size      = sce->ics.swb_sizes[g];
            int scf, prev_scf, step;
            int min_scf = -1, max_scf = 256;
            float curdiff;
            if (maxq[w*16+g] < 21.544) {
                sce->zeroes[w*16+g] = 1;
                start += size;
                continue;
            }
            sce->zeroes[w*16+g] = 0;
            scf  = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
            step = 16;
            for (;;) {
                float dist = 0.0f;
                int quant_max;

                for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
                    int b;
                    dist += quantize_band_cost(s, coefs + w2*128,
                                               scaled + w2*128,
                                               sce->ics.swb_sizes[g],
                                               scf,
                                               ESC_BT,
                                               lambda,
                                               INFINITY,
                                               &b);
                    dist -= b;
                }
                dist *= 1.0f / 512.0f / lambda;
                quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512]);
                if (quant_max >= 8191) { // too much, return to the previous quantizer
                    sce->sf_idx[w*16+g] = prev_scf;
                    break;
                }
                prev_scf = scf;
                curdiff = fabsf(dist - uplim[w*16+g]);
                if (curdiff <= 1.0f)
                    step = 0;
                else
                    step = log2f(curdiff);
                if (dist > uplim[w*16+g])
                    step = -step;
                scf += step;
                scf = av_clip_uint8(scf);
                step = scf - prev_scf;
                if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
                    sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
                    break;
                }
                if (step > 0)
                    min_scf = prev_scf;
                else
                    max_scf = prev_scf;
            }
            start += size;
        }
    }
    minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
    for (i = 1; i < 128; i++) {
        if (!sce->sf_idx[i])
            sce->sf_idx[i] = sce->sf_idx[i-1];
        else
            minq = FFMIN(minq, sce->sf_idx[i]);
    }
    if (minq == INT_MAX)
        minq = 0;
    minq = FFMIN(minq, SCALE_MAX_POS);
    maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
    for (i = 126; i >= 0; i--) {
        if (!sce->sf_idx[i])
            sce->sf_idx[i] = sce->sf_idx[i+1];
        sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
    }
}
Exemple #2
0
void tbreak(void)
{
	int pad, k;
	Tchar *i, j;
	int resol;
	int un0 = un;

	trap = 0;
	if (nb)
		return;
	if (dip == d && numtabp[NL].val == -1) {
		newline(1);
		return;
	}
	if (!nc) {
		setnel();
		if (!wch)
			return;
		if (pendw)
			getword(1);
		movword();
	} else if (pendw && !brflg) {
		getword(1);
		movword();
	}
	*linep = dip->nls = 0;
	if (NROFF && dip == d)
		horiz(po);
	if (lnmod)
		donum();
	lastl = ne;
	if (brflg != 1) {
		totout = 0;
	} else if (ad) {
		if ((lastl = ll - un) < ne)
			lastl = ne;
	}
	if (admod && ad && (brflg != 2)) {
		lastl = ne;
		adsp = adrem = 0;
		if (admod == 1)
			un +=  quant(nel / 2, HOR);
		else if (admod == 2)
			un += nel;
	}
	totout++;
	brflg = 0;
	if (lastl + un > dip->maxl)
		dip->maxl = lastl + un;
	horiz(un);
	if (NROFF) {
		if (adrem % t.Adj)
			resol = t.Hor; 
		else 
			resol = t.Adj;
	} else
		resol = HOR;

	lastl = ne + (nwd-1) * adsp + adrem;
	for (i = line; nc > 0; ) {
		if ((cbits(j = *i++)) == ' ') {
			pad = 0;
			do {
				pad += width(j);
				nc--;
			} while ((cbits(j = *i++)) == ' ');
			i--;
			pad += adsp;
			--nwd;
			if (adrem) {
				if (adrem < 0) {
					pad -= resol;
					adrem += resol;
				} else if ((totout & 01) || adrem / resol >= nwd) {
					pad += resol;
					adrem -= resol;
				}
			}
			pchar((Tchar) WORDSP);
			horiz(pad);
		} else {
			pchar(j);
			nc--;
		}
	}
	if (ic) {
		if ((k = ll - un0 - lastl + ics) > 0)
			horiz(k);
		pchar(ic);
	}
	if (icf)
		icf++;
	else 
		ic = 0;
	ne = nwd = 0;
	un = in;
	setnel();
	newline(0);
	if (dip != d) {
		if (dip->dnl > dip->hnl)
			dip->hnl = dip->dnl;
	} else {
		if (numtabp[NL].val > dip->hnl)
			dip->hnl = numtabp[NL].val;
	}
	for (k = ls - 1; k > 0 && !trap; k--)
		newline(0);
	spread = 0;
}
Exemple #3
0
int main (int argc,char **argv){	
	FILE *fp = fopen(argv[1], "rb");
	FILE *fp2 = fopen("lower.tga", "wb");
	FILE *fp3 = fopen("upper.tga", "wb");
	int w, h, mask_r,mask_g,mask_b;
	long long r=0,g=0,b=0;
	//int r_bit=argv[3][0]-48,g_bit=argv[4][0]-48,b_bit=argv[5][0]-48;
	//printf("%d %d %d",r_bit,g_bit,b_bit);
	int r_bit=3,g_bit=3,b_bit=2;
	r_bit=b_bit=g_bit=(argv[2][0]-48);
	float mse,mse_r,mse_g,mse_b,snr,snr_r,snr_g,snr_b;
	mse_r=mse_g=mse_b=mse=snr=snr_r=snr_g=snr_b=0;
	mask_r=get_mask(r_bit);
	mask_g=get_mask(g_bit);
	mask_b=get_mask(b_bit);
	int header[18];	
	int header2[18];
	for (int i =0; i<18 ; i++){
 		header[i]=getc(fp);
 		header2[i]=header[i];
 	}
 	//pobieranie nagłówka
 	w = (header[13]<<8)+header[12];
	h = (header[15]<<8)+header[14];
	for (int i =0; i<18 ; i++){
                putc(header2[i],fp2);
                putc(header2[i],fp3);
    }
	int i=0;
	int j=0;
	int rgb[3];
	int rgb_l[3];
	int rgb_u[3],rgb_ur[3];
	int nrgb[3];
	int prgb[3]={0},prgb_u[3]={0};
	for (i=0;i<h;i++){
		for( j=0;j < w;j++){
			if((j%2)==0){
				for(int k=0;k<3;k++){
					rgb[k]=getc(fp);
					int tmp2=rgb[k];
					rgb_l[k]=(tmp2+prgb[k])/2;
					int tmp=rgb[k];		
					rgb_u[k]=((unsigned)(tmp-prgb[k])/2);
					rgb_ur[k]=rgb_u[k]-prgb_u[k];				
				}
				for(int l=0;l<3;l++) prgb[l]=rgb[l];
				for(int l=0;l<3;l++) prgb_u[l]=rgb_u[l];
				nrgb[0]=quant(rgb_l[0],mask_r,r_bit);
				nrgb[1]=quant(rgb_l[1],mask_g,g_bit);
				nrgb[2]=quant(rgb_l[2],mask_b,b_bit);
				put3c(nrgb[0],nrgb[1],nrgb[2],fp2);
				put3c(rgb_ur[0],rgb_ur[1],rgb_ur[2],fp2);
			}else{
				for(int i=0;i<3;i++){
					getc(fp);
				}
			}
		}
	}
	int all=w*h;
	return 0;
}
Exemple #4
0
/**
 * Calculate rate distortion cost for quantizing with given codebook
 *
 * @return quantization distortion
 */
static av_always_inline float quantize_and_encode_band_cost_template(
                                struct AACEncContext *s,
                                PutBitContext *pb, const float *in,
                                const float *scaled, int size, int scale_idx,
                                int cb, const float lambda, const float uplim,
                                int *bits, int BT_ZERO, int BT_UNSIGNED,
                                int BT_PAIR, int BT_ESC)
{
    const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
    const float Q   = ff_aac_pow2sf_tab [q_idx];
    const float Q34 = ff_aac_pow34sf_tab[q_idx];
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    const float CLIPPED_ESCAPE = 165140.0f*IQ;
    int i, j;
    float cost = 0;
    const int dim = BT_PAIR ? 2 : 4;
    int resbits = 0;
    const int range  = aac_cb_range[cb];
    const int maxval = aac_cb_maxval[cb];
    int off;

    if (BT_ZERO) {
        for (i = 0; i < size; i++)
            cost += in[i]*in[i];
        if (bits)
            *bits = 0;
        return cost * lambda;
    }
    if (!scaled) {
        abs_pow34_v(s->scoefs, in, size);
        scaled = s->scoefs;
    }
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval);
    if (BT_UNSIGNED) {
        off = 0;
    } else {
        off = maxval;
    }
    for (i = 0; i < size; i += dim) {
        const float *vec;
        int *quants = s->qcoefs + i;
        int curidx = 0;
        int curbits;
        float rd = 0.0f;
        for (j = 0; j < dim; j++) {
            curidx *= range;
            curidx += quants[j] + off;
        }
        curbits =  ff_aac_spectral_bits[cb-1][curidx];
        vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
        if (BT_UNSIGNED) {
            for (j = 0; j < dim; j++) {
                float t = fabsf(in[i+j]);
                float di;
                if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
                    if (t >= CLIPPED_ESCAPE) {
                        di = t - CLIPPED_ESCAPE;
                        curbits += 21;
                    } else {
                        int c = av_clip(quant(t, Q), 0, 8191);
                        di = t - c*cbrtf(c)*IQ;
                        curbits += av_log2(c)*2 - 4 + 1;
                    }
                } else {
                    di = t - vec[j]*IQ;
                }
                if (vec[j] != 0.0f)
                    curbits++;
                rd += di*di;
            }
        } else {
            for (j = 0; j < dim; j++) {
                float di = in[i+j] - vec[j]*IQ;
                rd += di*di;
            }
        }
        cost    += rd * lambda + curbits;
        resbits += curbits;
        if (cost >= uplim)
            return uplim;
        if (pb) {
            put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
            if (BT_UNSIGNED)
                for (j = 0; j < dim; j++)
                    if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
                        put_bits(pb, 1, in[i+j] < 0.0f);
            if (BT_ESC) {
                for (j = 0; j < 2; j++) {
                    if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
                        int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
                        int len = av_log2(coef);

                        put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
                        put_bits(pb, len, coef & ((1 << len) - 1));
                    }
                }
            }
        }
    }

    if (bits)
        *bits = resbits;
    return cost;
}
Exemple #5
0
tbreak(){
	register *i, j, pad;
	int res;

	trap = 0;
	if(nb)return;
	if((dip == d) && (v.nl == -1)){
		newline(1);
		return;
	}
	if(!nc){
		setnel();
		if(!wch)return;
		if(pendw)getword(1);
		movword();
	}else if(pendw && !brflg){
		getword(1);
		movword();
	}
	*linep = dip->nls = 0;
#ifdef NROFF
	if(dip == d)horiz(po);
#endif
	if(lnmod)donum();
	lastl = ne;
	if(brflg != 1){
		totout = 0;
	}else if(ad){
		if((lastl = (ll - un)) < ne)lastl = ne;
	}
	if(admod && ad && (brflg != 2)){
		lastl = ne;
		adsp = adrem = 0;
#ifdef NROFF
		if(admod == 1)un +=  quant(nel/2,t.Adj);
#endif
#ifndef NROFF
		if(admod == 1)un += nel/2;
#endif
		else if(admod ==2)un += nel;
	}
	totout++;
	brflg = 0;
	if((lastl+un) > dip->maxl)dip->maxl = (lastl+un);
	horiz(un);
#ifdef NROFF
	if(adrem%t.Adj)res = t.Hor; else res = t.Adj;
#endif
	for(i = line;nc > 0;){
		if(((j = *i++) & CMASK) == ' '){
			pad = 0;
			do{
				pad += width(j);
				nc--;
			  }while(((j = *i++) & CMASK) == ' ');
			i--;
			pad += adsp;
			--nwd;
			if(adrem){
				if(adrem < 0){
#ifdef NROFF
					pad -= res;
					adrem += res;
				}else if((totout&01) ||
					((adrem/res)>=(nwd))){
					pad += res;
					adrem -= res;
#endif
#ifndef NROFF
					pad--;
					adrem++;
				}else{
					pad++;
					adrem--;
#endif
				}
			}
			horiz(pad);
		}else{
			pchar(j);
			nc--;
		}
	}
	if(ic){
		if((j = ll - un - lastl + ics) > 0)horiz(j);
		pchar(ic);
	}
	if(icf)icf++;
		else ic = 0;
	ne = nwd = 0;
	un = in;
	setnel();
	newline(0);
	if(dip != d){if(dip->dnl > dip->hnl)dip->hnl = dip->dnl;}
	else{if(v.nl > dip->hnl)dip->hnl = v.nl;}
	for(j=ls-1; (j >0) && !trap; j--)newline(0);
	spread = 0;
}
Exemple #6
0
/*
 * Generate internal cookies for a drawing function.
 */
int
setdraw(void)
{
	int i, j, k, dx[NPAIR], dy[NPAIR], delim, type __unused;
	tchar c, drawbuf[NC];

	/* input is \D'f dx dy dx dy ... c' (or at least it had better be) */
	/* this does drawing function f with character c and the */
	/* specified dx,dy pairs interpreted as appropriate */
	/* pairs are deltas from last point, except for radii */

	/* l dx dy:	line from here by dx,dy */
	/* c x:		circle of diameter x, left side here */
	/* e x y:	ellipse of diameters x,y, left side here */
	/*
	 * a dx1 dy1 dx2 dy2:	ccw arc: ctr at dx1,dy1,
	 * then end at dx2,dy2 from there
	 */
	/*
	 * ~ dx1 dy1 dx2 dy2...:
	 * spline to dx1,dy1 to dx2,dy2 ...
	 */
	/* f dx dy ...:	f is any other char:  like spline */

	if (ismot(c = getch()))
		return (0);
	delim = cbits(c);
	type = cbits(getch());
	for (i = 0; i < NPAIR; i++) {
		c = getch();
		if (cbits(c) == delim)
			break;
	/* ought to pick up optional drawing character */
		if (cbits(c) != ' ')
			ch = c;
		vflag = 0;
		dfact = EM;
		dx[i] = quant(atoi(), HOR);
		if (dx[i] > MAXMOT)
			dx[i] = MAXMOT;
		else if (dx[i] < -MAXMOT)
			dx[i] = -MAXMOT;
		if (cbits((c = getch())) == delim) {	/* spacer */
			dy[i++] = 0;
			break;
		}
		vflag = 1;
		dfact = lss;
		dy[i] = quant(atoi(), VERT);
		if (dy[i] > MAXMOT)
			dy[i] = MAXMOT;
		else if (dy[i] < -MAXMOT)
			dy[i] = -MAXMOT;
	}
	dfact = 1;
	vflag = 0;
#ifndef NROFF
	drawbuf[0] = DRAWFCN | chbits | ZBIT;
	drawbuf[1] = type | chbits | ZBIT;
	drawbuf[2] = '.' | chbits | ZBIT; /* use default drawing character */
	for (k = 0, j = 3; k < i; k++) {
		drawbuf[j++] = MOT | ((dx[k] >= 0) ? dx[k] : (NMOT | -dx[k]));
		drawbuf[j++] = MOT | VMOT | ((dy[k] >= 0) ?
		    dy[k] : (NMOT | -dy[k]));
	}
	if (type == DRAWELLIPSE) {
		/* so the net vertical is zero */
		drawbuf[5] = drawbuf[4] | NMOT;
		j = 6;
	}
	drawbuf[j++] = DRAWFCN | chbits | ZBIT;	/* marks end for ptout */
	drawbuf[j] = 0;
	pushback(drawbuf);
#endif
	return (0);
}
Exemple #7
0
int
tbreak()
{
	int	pad, k;
	tchar	*i, j;
	int resol = 0;
#ifdef EUC
#ifdef NROFF
	tchar	l;
#endif /* NROFF */
#endif /* EUC */

	trap = 0;
	if (nb)
		return (0);
	if (dip == d && numtab[NL].val == -1) {
		newline(1);
		return (0);
	}
	if (!nc) {
		setnel();
		if (!wch)
			return (0);
		if (pendw)
			getword(1);
		movword();
	} else if (pendw && !brflg) {
		getword(1);
		movword();
	}
	*linep = dip->nls = 0;
#ifdef NROFF
	if (dip == d)
		horiz(po);
#endif
	if (lnmod)
		donum();
	lastl = ne;
	if (brflg != 1) {
		totout = 0;
	} else if (ad) {
		if ((lastl = ll - un) < ne)
			lastl = ne;
	}
	if (admod && ad && (brflg != 2)) {
		lastl = ne;
		adsp = adrem = 0;
		if (admod == 1)
			un +=  quant(nel / 2, HOR);
		else if (admod == 2)
			un += nel;
	}
	totout++;
	brflg = 0;
	if (lastl + un > dip->maxl)
		dip->maxl = lastl + un;
	horiz(un);
#ifdef NROFF
	if (adrem % t.Adj)
		resol = t.Hor; 
	else 
		resol = t.Adj;
#else
	resol = HOR;
#endif
	adrem = (adrem / resol) * resol;
	for (i = line; nc > 0; ) {
#ifndef EUC
		if ((cbits(j = *i++)) == ' ') {
#else
#ifndef NROFF
		if ((cbits(j = *i++)) == ' ') {
#else
		if ((cbits(j = *i++) & ~MBMASK) == ' ') {
#endif /* NROFF */
#endif /* EUC */
			pad = 0;
			do {
				pad += width(j);
				nc--;
#ifndef EUC
			} while ((cbits(j = *i++)) == ' ');
#else
#ifndef NROFF
			} while ((cbits(j = *i++)) == ' ');
#else
			} while ((cbits(j = *i++) & ~MBMASK) == ' ');
#endif /* NROFF */
#endif /* EUC */
			i--;
			pad += adsp;
			--nwd;
			if (adrem) {
				if (adrem < 0) {
					pad -= resol;
					adrem += resol;
				} else if ((totout & 01) || adrem / resol >= nwd) {
					pad += resol;
					adrem -= resol;
				}
			}
			pchar((tchar) WORDSP);
			horiz(pad);
		} else {
			pchar(j);
			nc--;
		}
	}
	if (ic) {
		if ((k = ll - un - lastl + ics) > 0)
			horiz(k);
		pchar(ic);
	}
	if (icf)
		icf++;
	else 
		ic = 0;
	ne = nwd = 0;
	un = in;
	setnel();
	newline(0);
	if (dip != d) {
		if (dip->dnl > dip->hnl)
			dip->hnl = dip->dnl;
	} else {
		if (numtab[NL].val > dip->hnl)
			dip->hnl = numtab[NL].val;
	}
	for (k = ls - 1; k > 0 && !trap; k--)
		newline(0);
	spread = 0;

	return (0);
}
Exemple #8
0
void casetl(void)
{
	int j;
	int w[3];
	Tchar buf[LNSIZE];
	Tchar *tp;
	Tchar i, delim;

 	/*
 	 * bug fix
 	 *
 	 * if .tl is the first thing in the file, the p1
 	 * doesn't come out, also the pagenumber will be 0
 	 *
 	 * tends too confuse the device filter (and the user as well)
 	 */
 	if (dip == d && numtabp[NL].val == -1)
 		newline(1);
	dip->nls = 0;
	skip();
	if (ismot(delim = getch())) {
		ch = delim;
		delim = '\'';
	} else 
		delim = cbits(delim);
	tp = buf;
	numtabp[HP].val = 0;
	w[0] = w[1] = w[2] = 0;
	j = 0;
	while (cbits(i = getch()) != '\n') {
		if (cbits(i) == cbits(delim)) {
			if (j < 3)
				w[j] = numtabp[HP].val;
			numtabp[HP].val = 0;
			if (w[j] != 0)
				*tp++ = WORDSP;
			j++;
			*tp++ = 0;
		} else {
			if (cbits(i) == pagech) {
				setn1(numtabp[PN].val, numtabp[findr('%')].fmt,
				      i&SFMASK);
				continue;
			}
			numtabp[HP].val += width(i);
			if (tp < &buf[LNSIZE-10]) {
				if (cbits(i) == ' ' && *tp != WORDSP)
					*tp++ = WORDSP;
				*tp++ = i;
			} else {
				ERROR "Overflow in casetl" WARN;
			}
		}
	}
	if (j<3)
		w[j] = numtabp[HP].val;
	*tp++ = 0;
	*tp++ = 0;
	*tp = 0;
	tp = buf;
	if (NROFF)
		horiz(po);
	while (i = *tp++)
		pchar(i);
	if (w[1] || w[2])
		horiz(j = quant((lt - w[1]) / 2 - w[0], HOR));
	while (i = *tp++)
		pchar(i);
	if (w[2]) {
		horiz(lt - w[0] - w[1] - w[2] - j);
		while (i = *tp++)
			pchar(i);
	}
	newline(0);
	if (dip != d) {
		if (dip->dnl > dip->hnl)
			dip->hnl = dip->dnl;
	} else {
		if (numtabp[NL].val > dip->hnl)
			dip->hnl = numtabp[NL].val;
	}
}
Exemple #9
0
void setdraw(void)	/* generate internal cookies for a drawing function */
{
	int i, j, k, dx[NPAIR], dy[NPAIR], delim, type;
	Tchar c, drawbuf[NC];
	int drawch = '.';	/* character to draw with */

	/* input is \D'f dx dy dx dy ... c' (or at least it had better be) */
	/* this does drawing function f with character c and the */
	/* specified dx,dy pairs interpreted as appropriate */
	/* pairs are deltas from last point, except for radii */

	/* l dx dy:	line from here by dx,dy */
	/* c x:		circle of diameter x, left side here */
	/* e x y:	ellipse of diameters x,y, left side here */
	/* a dx1 dy1 dx2 dy2:
			ccw arc: ctr at dx1,dy1, then end at dx2,dy2 from there */
	/* ~ dx1 dy1 dx2 dy2...:
			spline to dx1,dy1 to dx2,dy2 ... */
	/* b x c:
			built-up character of type c, ht x */
	/* f dx dy ...:	f is any other char:  like spline */

	if (ismot(c = getch()))
		return;
	delim = cbits(c);
	numerr.escarg = type = cbits(getch());
	if (type == '~')	/* head off the .tr ~ problem */
		type = 's';
	for (i = 0; i < NPAIR ; i++) {
		skip();
		vflag = 0;
		dfact = EM;
		dx[i] = quant(atoi0(), HOR);
		if (dx[i] > MAXMOT)
			dx[i] = MAXMOT;
		else if (dx[i] < -MAXMOT)
			dx[i] = -MAXMOT;
		skip();
		if (type == 'c') {
			dy[i] = 0;
			goto eat;
		}
		vflag = 1;
		dfact = lss;
		dy[i] = quant(atoi0(), VERT);
		if (dy[i] > MAXMOT)
			dy[i] = MAXMOT;
		else if (dy[i] < -MAXMOT)
			dy[i] = -MAXMOT;
eat:
		if (cbits(c = getch()) != ' ') {	/* must be the end */
			if (cbits(c) != delim) {
				drawch = cbits(c);
				getch();
			}
			i++;
			break;
		}
	}
	dfact = 1;
	vflag = 0;
	if (TROFF) {
		drawbuf[0] = DRAWFCN | chbits | ZBIT;
		drawbuf[1] = type | chbits | ZBIT;
		drawbuf[2] = drawch | chbits | ZBIT;
		for (k = 0, j = 3; k < i; k++) {
			drawbuf[j++] = MOT | ((dx[k] >= 0) ? dx[k] : (NMOT | -dx[k]));
			drawbuf[j++] = MOT | VMOT | ((dy[k] >= 0) ? dy[k] : (NMOT | -dy[k]));
		}
		if (type == DRAWELLIPSE) {
			drawbuf[5] = drawbuf[4] | NMOT;	/* so the net vertical is zero */
			j = 6;
		} else if (type == DRAWBUILD) {
			drawbuf[4] = drawbuf[3] | NMOT;	/* net horizontal motion is zero */
			drawbuf[2] &= ~ZBIT;		/* width taken from drawing char */
			j = 5;
		}
		drawbuf[j++] = DRAWFCN | chbits | ZBIT;	/* marks end for ptout */
		drawbuf[j] = 0;
		pushback(drawbuf);
	}
}
Exemple #10
0
void
setdraw (void)	/* generate internal cookies for a drawing function */
{
	int i, dx[NPAIR], dy[NPAIR], type;
	tchar c, delim;
#ifndef	NROFF
	int	hpos, vpos;
	int j, k;
	tchar drawbuf[NC];
#else
	extern int tlp, utf8;
	char drawbuf[NC];
#endif	/* NROFF */

	/* input is \D'f dx dy dx dy ... c' (or at least it had better be) */
	/* this does drawing function f with character c and the */
	/* specified dx,dy pairs interpreted as appropriate */
	/* pairs are deltas from last point, except for radii */

	/* l dx dy:	line from here by dx,dy */
	/* c x:		circle of diameter x, left side here */
	/* e x y:	ellipse of diameters x,y, left side here */
	/* a dx1 dy1 dx2 dy2:
			ccw arc: ctr at dx1,dy1, then end at dx2,dy2 from there */
	/* ~ dx1 dy1 dx2 dy2...:
			spline to dx1,dy1 to dx2,dy2 ... */
	/* f dx dy ...:	f is any other char:  like spline */

	if (ismot(c = getch()))
		return;
	delim = c;
	type = cbits(getch());
	for (i = 0; i < NPAIR ; i++) {
		do {
			c = getch();
			if (issame(c, delim)) goto argend;
		} while (cbits(c) == ' ');
	/* ought to pick up optional drawing character */
		if (cbits(c) != ' ')
			ch = c;
		vflag = 0;
		dfact = type == DRAWTHICKNESS ? 1 : EM;
		dx[i] = quant(hatoi(), HOR);
		if (dx[i] > MAXMOT)
			dx[i] = MAXMOT;
		else if (dx[i] < -MAXMOT)
			dx[i] = -MAXMOT;
		do {
			c = getch();
			if (issame(c, delim)) {
				dy[i++] = 0;
				goto argend;
			}
		} while (cbits(c) == ' ');
		if (cbits(c) != ' ')
			ch = c;
		vflag = 1;
		dfact = lss;
		dy[i] = quant(hatoi(), VERT);
		if (type == DRAWTHICKNESS)
			dy[i] = 0;
		else if (dy[i] > MAXMOT)
			dy[i] = MAXMOT;
		else if (dy[i] < -MAXMOT)
			dy[i] = -MAXMOT;
	}
argend:
	dfact = 1;
	vflag = 0;
#ifndef NROFF
	drawbuf[0] = DRAWFCN | chbits | ZBIT;
	drawbuf[1] = type | chbits | ZBIT;
	drawbuf[2] = '.' | chbits | ZBIT;	/* use default drawing character */
	hpos = vpos = 0;
	for (k = 0, j = 3; k < i; k++) {
		drawbuf[j++] = MOT | ((dx[k] >= 0) ?
				sabsmot(dx[k]) : (NMOT | sabsmot(-dx[k])));
		drawbuf[j++] = MOT | VMOT | ((dy[k] >= 0) ?
				sabsmot(dy[k]) : (NMOT | sabsmot(-dy[k])));
		hpos += dx[k];
		vpos += dy[k];
	}
	if (type == DRAWELLIPSE || type == DRAWELLIPSEFI) {
		drawbuf[5] = drawbuf[4] | NMOT;	/* so the net vertical is zero */
		j = 6;
	}
	if (gflag && (type == DRAWPOLYGON || type == DRAWPOLYGONFI) &&
			(hpos || vpos)) {
		drawbuf[j++] = MOT | ((hpos < 0) ?
				sabsmot(-hpos) : (NMOT | sabsmot(hpos)));
		drawbuf[j++] = MOT | VMOT | ((vpos < 0) ?
				sabsmot(-vpos) : (NMOT | sabsmot(vpos)));
	}
	drawbuf[j++] = DRAWFCN | chbits | ZBIT;	/* marks end for ptout */
	drawbuf[j] = 0;
	pushback(drawbuf);
#else
	switch (type) {
	case 'l':
		if (dx[0] && !dy[0]) {
			if (dx[0] < 0) {
				snprintf(drawbuf, sizeof(drawbuf), "\\h'%du'",
				    dx[0]);
				cpushback(drawbuf);
			}
			snprintf(drawbuf, sizeof(drawbuf), "\\l'%du%s'",
			    dx[0], tlp ? "\\&-" : utf8 ? "\\U'2500'" : "");
			cpushback(drawbuf);
		} else if (dy[0] && !dx[0]) {
			snprintf(drawbuf, sizeof(drawbuf), "\\L'%du%s'",
			    dy[0], tlp ? "|" : utf8 ? "\\U'2502'" : "");
			cpushback(drawbuf);
		}
	}
#endif
}
Exemple #11
0
void
setvline(void)
{
	register int i;
	tchar c, _d, delim, rem, ver, neg;
	int	cnt, v;
	tchar vlbuf[NC];
	register tchar *vlp;

	if (ismot(c = getch()))
		return;
	delim = c;
	dfact = lss;
	vflag++;
	i = quant(hatoi(), VERT);
	dfact = 1;
	if (!i) {
		eat(delim);
		vflag = 0;
		return;
	}
	if (c = getch(), issame(c, delim)) {
		c = BOXRULE | chbits;	/*default box rule*/
	} else {
		_d = getch();
		if (!issame(_d, delim))
			nodelim(delim);
	}
	c |= ZBIT;
	neg = 0;
	if (i < 0) {
		i = -i;
		neg = NMOT;
	}
#ifdef NROFF
	v = 2 * t.Halfline;
#endif
#ifndef NROFF
	v = EM;
#endif
	cnt = i / v;
	rem = makem(i % v) | neg;
	ver = makem(v) | neg;
	vlp = vlbuf;
	if (!neg)
		*vlp++ = ver;
	if (absmot(rem) != 0) {
		*vlp++ = c;
		*vlp++ = rem;
	}
	while ((vlp < (vlbuf + NC - 3)) && cnt--) {
		*vlp++ = c;
		*vlp++ = ver;
	}
	*(vlp - 2) &= ~ZBIT;
	if (!neg)
		vlp--;
	*vlp++ = 0;
	pushback(vlbuf);
	vflag = 0;
}