⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dither.c

📁 ELinks is an advanced and well-established feature-rich text mode web (HTTP/FTP/..) browser. ELinks
💻 C
📖 第 1 页 / 共 2 页
字号:
/* dither.c * Dithering * (c) 2000-2002 Karel 'Clock' Kulhavy * This file is a part of the Links program, released under GPL. */#include "cfg.h"#ifdef G#include "links.h"#include "bits.h"#ifdef HAVE_ENDIAN_H#include <endian.h>#endif#ifdef HAVE_MATH_H#include <math.h>#endif/* The input of dithering function is 3 times 16-bit value. The value is * proportional to light that will go out of the monitor. Only in this space it * is possible to dither accurately because distributing the error means maintaining * the photon count (blurring caused by human eye from big distance preservers photon * count, just spreads the photons a little around) * The 8-bit dithering functions are to be used only for dithering text. *//* This source does dithering and rounding of images (in photon space) into * struct bitmap. It also computes colors given r,g,b. */ /* No dither function destroys the passed bitmap *//* All dither functions take format in booklike order without inter-line gaps. * red, green, blue order. Input bytes=3*y*x. Takes x and y from bitmap. *//* The input of dithering function is 3 times 8-bit value. The value is * proportional to desired input into graphics driver (which is in fact * proportional to monitor's input voltage for graphic drivers that do not * pollute the picture with gamma correction) *//* Dithering algorithm: Floyd-Steinberg error distribution. The used * coefficients are depicted in the following table. The empty box denotes the * originator pixel that generated the error. * *                    +----+----+ *                    |    |7/16| *               +----+----+----+ *               |3/16|5/16|1/16| *               +----+----+----+ *//* We assume here int holds at least 32 bits */static int red_table[65536],green_table[65536],blue_table[65536];/* If we want to represent some 16-bit from-screen-light, it would require certain display input * value (0-255 red, 0-255 green, 0-255 blue), possibly not a whole number. [red|green|blue]_table * translares 16-bit light to the nearest index (that should be fed into the * display). Nearest is meant in realm of numbers that are proportional to * display input. The table also says what will be the real value this rounded * display input yields. index is in * bits 16-31, real light value is in bits 0-15. real light value is 0 (no * photons) to 65535 (maximum photon flux). This is subtracted from wanted * value and error remains which is the distributed into some neighboring * pixels.   * * Index memory organization  * -------------------------  * 	1 byte per pixel: obvious. The output byte is OR of all three LSB's from red_table, * 		green_table, blue_table  * 	2 bytes per pixel: cast all three values to unsigned  short, OR them together  * 		and dump the short into the memory  * 	3 and 4 bytes per pixel: LSB's contain the red, green, and blue bytes. *//* These tables allow the most precise dithering possible: * a) Rouding is performed always to perceptually nearest value, not to *    nearest light flux * b) error addition is performed in photon space to maintain fiedlity * c) photon space addition from b) is performed with 16 bits thus not *    degrading 24-bit images *//* We assume here unsigned short holds at least 16 bits */static unsigned short round_red_table[256];static unsigned short round_green_table[256];static unsigned short round_blue_table[256];/* Transforms sRGB red, green, blue (0-255) to light of nearest voltage to * voltage appropriate to given sRGB coordinate. */void (*round_fn)(unsigned short *in, struct bitmap *out);/* When you finish the stuff with dither_start, dither_restart, just do "if (dregs) mem_free(dregs);" */void (*dither_fn_internal)(unsigned short *in, struct bitmap *out, int * dregs);     /* prototypes */void dither_1byte(unsigned short *, struct bitmap *, int *);   /* DITHER_TEMPLATE */void round_1byte(unsigned short *, struct bitmap *);           /* ROUND_TEMPLATE */void dither_2byte(unsigned short *, struct bitmap *, int *);   /* DITHER_TEMPLATE */void round_2byte(unsigned short *, struct bitmap *);           /* ROUND_TEMPLATE */void dither_195(unsigned short *, struct bitmap *, int *);   /* DITHER_TEMPLATE */void round_195(unsigned short *, struct bitmap *);           /* ROUND_TEMPLATE */void dither_451(unsigned short *, struct bitmap *, int *);   /* DITHER_TEMPLATE */void round_451(unsigned short *, struct bitmap *);           /* ROUND_TEMPLATE */void dither_196(unsigned short *, struct bitmap *, int *);   /* DITHER_TEMPLATE */void round_196(unsigned short *, struct bitmap *);           /* ROUND_TEMPLATE */void dither_452(unsigned short *, struct bitmap *, int *);   /* DITHER_TEMPLATE */void round_452(unsigned short *, struct bitmap *);           /* ROUND_TEMPLATE */void dither_708(unsigned short *, struct bitmap *, int *);   /* DITHER_TEMPLATE */void round_708(unsigned short *, struct bitmap *);           /* ROUND_TEMPLATE */long color_332(int);long color_121(int);long color_pass_rgb(int);long color_888_bgr(int);void pass_bgr(unsigned short *, struct bitmap *);long color_8888_bgr0(int);long color_8888_0bgr(int);long color_8888_0rgb(int);void pass_0bgr(unsigned short *, struct bitmap *);long color_555be(int);long color_555(int);long color_565be(int);long color_565(int);void make_8_table(int *, double);void make_16_table(int *, int, int, double , int, int);void make_red_table(int, int, int, int);void make_green_table(int, int, int, int);void make_blue_table(int, int, int, int);void make_round_tables(void);int slow_fpu = -1;#define LIN \	r+=(int)(in[0]);\	g+=(int)(in[1]);\	b+=(int)(in[2]);\	in+=3;/* EMPIRE IMAGINE FEAR */#define LTABLES \	{\		int rc=r,gc=g,bc=b;\		if ((unsigned)rc>65535) rc=rc<0?0:65535;\		if ((unsigned)gc>65535) gc=gc<0?0:65535;\		if ((unsigned)bc>65535) bc=bc<0?0:65535;\		rt=red_table[rc];\		gt=green_table[gc];\		bt=blue_table[bc];\	}\	SAVE_CODE\	rt=r-(rt&65535);\	gt=g-(gt&65535);\	bt=b-(bt&65535);\	#define BODY \	LIN\	LTABLES\	r=bptr[3];\	g=bptr[4];\	b=bptr[5];\	r+=rt;\	g+=gt;\	b+=bt;\	rt+=8;\	gt+=8;\	bt+=8;\	rt>>=4;\	gt>>=4;\	bt>>=4;\	r-=9*rt;\	g-=9*gt;\	b-=9*bt;\	bptr[3]=rt;\	bptr[4]=gt;\	bptr[5]=bt;#define BODYR \	LIN\	LTABLES\	rt+=8;\	gt+=8;\	bt+=8;\	rt>>=4;\	gt>>=4;\	bt>>=4;\	bptr[-3]+=3*rt;\	bptr[-2]+=3*gt;\	bptr[-1]+=3*bt;\	*bptr+=5*rt;\	bptr[1]+=5*gt;\	bptr[2]+=5*bt;#define BODYC \	LIN\	LTABLES\	r=rt;\	g=gt;\	b=bt;#define BODYL \	bptr=dregs;\	r=bptr[0];\	g=bptr[1];\	b=bptr[2];\	BODY\	bptr[0]=5*rt;\	bptr[1]=5*gt;\	bptr[2]=5*bt;\	bptr+=3;#define BODYI \	BODY\	bptr[0]+=5*rt;\	bptr[1]+=5*gt;\	bptr[2]+=5*bt;\	bptr[-3]+=3*rt;\	bptr[-2]+=3*gt;\	bptr[-1]+=3*bt;\	bptr+=3;	#define DITHER_TEMPLATE(template_name) \	void template_name(unsigned short *in, struct bitmap *out, int *dregs)\		{\		int r,g,b,o,rt,gt,bt,y,x;\		unsigned char *outp=out->data;\		int *bptr;\		int skip=out->skip-SKIP_CODE;\\		o=0; /*warning go away */\		switch(out->x){\\			case 0:\			return;\\			case 1:\			r=g=b=0;\			for (y=out->y;y;y--){\				BODYC\				outp+=skip;\			}\			break;\\			default:\			for (y=out->y;y;y--){\				BODYL\				for (x=out->x-2;x;x--){\					BODYI\				}\				BODYR\				outp+=skip;\			}\			break;\		}\	}#define ROUND_TEMPLATE(template_name)\	void template_name(unsigned short *in, struct bitmap *out)\	{\		int rt,gt,bt,o,x,y;\		unsigned char *outp=out->data;\		int skip=out->skip-SKIP_CODE;\	\		o=0; /*warning go away */\		for (y=out->y;y;y--){\			for (x=out->x;x;x--){\				rt=red_table[in[0]];\				gt=green_table[in[1]];\				bt=blue_table[in[2]];\				in+=3;\				SAVE_CODE\			}\			outp+=skip;\		}\	}/* Expression determining line length in bytes */#define SKIP_CODE out->x/* Code with input in rt, gt, bt (values from red_table, green_table, blue_table) * that saves appropriate code on *outp (unsigned char *outp). We can use int o; * as a scratchpad. */#define SAVE_CODE \	o=rt|gt|bt;\	*outp++=(o>>16);		DITHER_TEMPLATE(dither_1byte)ROUND_TEMPLATE(round_1byte);#undef SKIP_CODE#undef SAVE_CODE#define SKIP_CODE out->x*2#if defined(t2c) && defined(C_LITTLE_ENDIAN)#define SAVE_CODE \	o=rt|gt|bt;\	*(t2c *)outp=(o>>16);\	outp+=2;#else#define SAVE_CODE \	o=rt|gt|bt;\	o>>=16;\	*(unsigned char *)outp=o;\	((unsigned char *)outp)[1]=o>>8;\	outp+=2;#endif /* #ifdef t2c */		DITHER_TEMPLATE(dither_2byte)ROUND_TEMPLATE(round_2byte)#undef SAVE_CODE#undef SKIP_CODE/* B G R */#define SKIP_CODE out->x*3;#define SAVE_CODE outp[0]=bt>>16;\	outp[1]=gt>>16;\	outp[2]=rt>>16;\	outp+=3;DITHER_TEMPLATE(dither_195)ROUND_TEMPLATE(round_195)#undef SAVE_CODE#undef SKIP_CODE/* R G B */#define SKIP_CODE out->x*3;#define SAVE_CODE *outp=rt>>16;\	outp[1]=gt>>16;\	outp[2]=bt>>16;\	outp+=3;DITHER_TEMPLATE(dither_451)ROUND_TEMPLATE(round_451)#undef SAVE_CODE#undef SKIP_CODE/* B G R 0 */#define SKIP_CODE out->x*4;#define SAVE_CODE *outp=bt>>16;\	outp[1]=gt>>16;\	outp[2]=rt>>16;\	outp[3]=0;\	outp+=4;DITHER_TEMPLATE(dither_196)ROUND_TEMPLATE(round_196)#undef SAVE_CODE#undef SKIP_CODE /* 0 B G R */#define SKIP_CODE out->x*4;#define SAVE_CODE *outp=0;\	outp[1]=bt>>16;\	outp[2]=gt>>16;\	outp[3]=rt>>16;\	outp+=4;DITHER_TEMPLATE(dither_452)ROUND_TEMPLATE(round_452)#undef SAVE_CODE#undef SKIP_CODE /* 0 R G B */#define SKIP_CODE out->x*4;#define SAVE_CODE *outp=0;\	outp[1]=rt>>16;\	outp[2]=gt>>16;\	outp[3]=bt>>16;\	outp+=4;DITHER_TEMPLATE(dither_708)ROUND_TEMPLATE(round_708)#undef SAVE_CODE#undef SKIP_CODE /* For 256-color cube */long color_332(int rgb){	int r,g,b;	long ret;	r=(rgb>>16)&255;	g=(rgb>>8)&255;	b=rgb&255;	r=(r*7+127)/255;	g=(g*7+127)/255;	b=(b*3+127)/255;	*(char *)&ret=(r<<5)|(g<<2)|b;	return ret;}long color_121(int rgb){	int r,g,b;	long ret;	r=(rgb>>16)&255;	g=(rgb>>8)&255;	b=rgb&255;	r=(r+127)/255;	g=(3*g+127)/255;	b=(b+127)/255;	*(char *)&ret=(r<<3)|(g<<1)|b;	return ret;}long color_pass_rgb(int rgb){	long ret;	*(char *)&ret=rgb>>16;	((char *)&ret)[1]=rgb>>8;	((char *)&ret)[2]=rgb;	return ret;}long color_888_bgr(int rgb){	long ret;	((char *)&ret)[0]=rgb;	((char *)&ret)[1]=rgb>>8;	((char *)&ret)[2]=rgb>>16;	return ret;}/* Long live the Manchester Modulation! */void pass_bgr(unsigned short *in, struct bitmap *out){	int skip=out->skip-3*out->x,y,x;	unsigned char *outp=out->data;		for (y=out->y;y;y--){		for (x=out->x;x;x--){			outp[0]=in[2];			outp[1]=in[1];			outp[2]=in[0];			outp+=3;			in+=3;		}		outp+=skip;	}		}long color_8888_bgr0(int rgb){	long ret;	((char *)&ret)[0]=rgb;	((char *)&ret)[1]=rgb>>8;	((char *)&ret)[2]=rgb>>16;	((char *)&ret)[3]=0;	return ret;}/* Long live the sigma-delta modulator! */long color_8888_0bgr(int rgb){	long ret;	/* Atmospheric lightwave communication rulez */	((char *)&ret)[0]=0;	((char *)&ret)[1]=rgb;	((char *)&ret)[2]=rgb>>8;	((char *)&ret)[3]=rgb>>16;	return ret;}/* Long live His Holiness The 14. Dalai Lama Taendzin Gjamccho! *//* The above line will probably cause a ban of this browser in China under * the capital punishment ;-) */long color_8888_0rgb(int rgb){	long ret;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -