📄 dither.c
字号:
/* Chokpori Dharamsala Lhasa Laddakh */ ((char *)&ret)[0]=0; ((char *)&ret)[1]=rgb>>16; ((char *)&ret)[2]=rgb>>8; ((char *)&ret)[3]=rgb; return ret;}/* We assume unsgned short holds at least 16 bits. */void pass_0bgr(unsigned short *in, struct bitmap *out){ int skip=out->skip-4*out->x,y,x; unsigned char *outp=out->data; for (y=out->y;y;y--){ for (x=out->x;x;x--){ outp[0]=0; outp[1]=in[2]>>8; outp[2]=in[1]>>8; outp[3]=in[0]>>8; outp+=4; in+=3; } outp+=skip; } }/* We assume long holds at least 32 bits */long color_555be(int rgb){ int r=(rgb>>16)&255; int g=(rgb>>8)&255; int b=(rgb)&255; int i; long ret; r=(r*31+127)/255; g=(g*31+127)/255; b=(b*31+127)/255; i=(r<<10)|(g<<5)|b; ((unsigned char *)&ret)[0]=i>>8; ((unsigned char *)&ret)[1]=i; return ret;}/* We assume long holds at least 32 bits */long color_555(int rgb){ int r=(rgb>>16)&255; int g=(rgb>>8)&255; int b=(rgb)&255; int i; long ret; r=(r*31+127)/255; g=(g*31+127)/255; b=(b*31+127)/255; i=(r<<10)|(g<<5)|b; ((unsigned char *)&ret)[0]=i; ((unsigned char *)&ret)[1]=i>>8; return ret;}long color_565be(int rgb){ int r,g,b; long ret; int i; r=(rgb>>16)&255; g=(rgb>>8)&255; /* Long live the PIN photodiode */ b=rgb&255; r=(r*31+127)/255; g=(g*63+127)/255; b=(b*31+127)/255; i = (r<<11)|(g<<5)|b; ((unsigned char *)&ret)[0]=i>>8; ((unsigned char *)&ret)[1]=i; return ret;}long color_565(int rgb){ int r,g,b; long ret; int i; r=(rgb>>16)&255; g=(rgb>>8)&255; /* Long live the PIN photodiode */ b=rgb&255; r=(r*31+127)/255; g=(g*63+127)/255; b=(b*31+127)/255; i=(r<<11)|(g<<5)|b; ((unsigned char *)&ret)[0]=i; ((unsigned char *)&ret)[1]=i>>8; return ret;}/* rgb = r*65536+g*256+b *//* The selected color_fn returns a long. * When we have for example 2 bytes per pixel, we make them in the memory, * then copy them to the beginning of the memory occupied by the long * variable, and return that long variable. */long (*get_color_fn(int depth))(int rgb){ switch(depth) { case 33: return color_121; break; case 65: return color_332; break; case 122: return color_555; break; case 378: return color_555be; break; case 130: return color_565; break; case 386: return color_565be; break; case 451: return color_pass_rgb; break; case 195: return color_888_bgr; break; case 452: return color_8888_0bgr; break; case 196: return color_8888_bgr0; break; case 708: return color_8888_0rgb; break; default: return NULL; break; }}void make_8_table(int *table, double gamma){ int i,light0; double light; for (i=0;i<256;i++){ light=pow((double)i/255,gamma); /* Long live the Nipkow Disk */ light0=65535*light; if (light0<0) light0=0; if (light0>65535) light0=65535; table[i]=light0; }}/* Gamma says that light=electricity raised to gamma *//* dump_t2c means memory organization defined in comment for * red_table on the top of dither.c *//* dump_t2c is taken into account only if t2c is defined. */void make_16_table(int *table, int bits, int pos,double gamma, int dump_t2c, int bigendian){ int j,light_val,grades=(1<<bits)-1,grade; double voltage; double rev_gamma=1/gamma; const double t=((double)1)/65535; int last_grade, last_content; ttime start_time = get_time(); int sample_state = 0; int x_slow_fpu = slow_fpu; if (gamma_bits != 2) x_slow_fpu = !gamma_bits; repeat_loop: last_grade=-1; last_content=0; for (j=0;j<65536;j++){ if (x_slow_fpu) { if (x_slow_fpu == 1) { if (j & 255) { table[j] = last_content; continue; } } else { if (!(j & (j - 1))) { ttime now = get_time(); if (!sample_state) { if (now != start_time) start_time = now, sample_state = 1; } else { if (now - start_time > SLOW_FPU_DETECT_THRESHOLD && (now - start_time) * 65536 / j > SLOW_FPU_MAX_STARTUP / 3) { x_slow_fpu = 1; goto repeat_loop; } } } } } voltage=pow(j*t,rev_gamma); /* Determine which monitor input voltage is equivalent * to said photon flux level */ grade=voltage*grades+.5; if (grade==last_grade){ table[j]=last_content; continue; } last_grade=grade; voltage=(double)grade/grades; /* Find nearest voltage to this voltage. Finding nearest voltage, not * nearest photon flux ensures the dithered pixels will be perceived to be * near. The voltage input into the monitor was intentionally chosen by * generations of television engineers to roughly comply with eye's * response, thus minimizing and unifying noise impact on transmitted * signal. This is only marginal enhancement however it sounds * kool ;-) (and is kool) */ light_val=pow(voltage,gamma)*65535+0.5; /* Find out what photon flux this index represents */ if (light_val<0) light_val=0; if (light_val>65535) light_val=65535; /* Clip photon flux for safety */#ifdef t2c_xxx/* This branch is broken, but it was never tried */ if (dump_t2c){ t2c sh; int val=grade<<pos; if (bigendian) { ((unsigned char *)&sh)[0]=val; ((unsigned char *)&sh)[1]=val>>8; }else{ ((unsigned char *)&sh)[1]=val; ((unsigned char *)&sh)[0]=val>>8; } last_content=light_val|(sh<<16U); }else{#endif /* #ifdef t2c */ if (bigendian) { int val, val2; val = grade<<pos; val2 = (val>>8) | ((val&0xff)<<8); last_content=light_val|(val2<<16U); }else{ last_content=light_val|(grade<<(pos+16U)); }#ifdef t2c_xxx }#endif /* #ifdef t2c */ table[j]=last_content; /* Save index and photon flux. */ } if (x_slow_fpu == -1) slow_fpu = 0; /* if loop passed once without detecting slow fpu, always assume fast FPU */ if (gamma_bits == 2 && x_slow_fpu == 1) slow_fpu = 1;}void make_red_table(int bits, int pos, int dump_t2c, int be){ make_16_table(red_table,bits,pos,display_red_gamma,dump_t2c, be);}void make_green_table(int bits, int pos, int dump_t2c, int be){ make_16_table(green_table,bits,pos,display_green_gamma,dump_t2c, be);}void make_blue_table(int bits, int pos,int dump_t2c, int be){ make_16_table(blue_table,bits,pos,display_blue_gamma, dump_t2c, be);}void dither(unsigned short *in, struct bitmap *out){ int *dregs; if ((unsigned)out->x > MAXINT / 3 / sizeof(*dregs)) overalloc(); dregs=mem_calloc(out->x*3*sizeof(*dregs)); (*dither_fn_internal)(in, out, dregs); mem_free(dregs);}/* For functions that do dithering. * Returns allocated dregs. */int *dither_start(unsigned short *in, struct bitmap *out){ int *dregs; if ((unsigned)out->x > MAXINT / 3 / sizeof(*dregs)) overalloc(); dregs=mem_calloc(out->x*3*sizeof(*dregs)); (*dither_fn_internal)(in, out, dregs); return dregs;}void dither_restart(unsigned short *in, struct bitmap *out, int *dregs){ (*dither_fn_internal)(in, out, dregs);}void make_round_tables(void){ int a; unsigned short v; for (a=0;a<256;a++){ /* a is sRGB coordinate */ v=apply_gamma_single_8_to_16(a,user_gamma/sRGB_gamma); round_red_table[a]=red_table[v]; round_green_table[a]=green_table[v]; round_blue_table[a]=blue_table[v]; }}/* Also makes up the dithering tables. * You may call it twice - it doesn't leak any memory. */void init_dither(int depth){ switch(depth){ case 33: /* 4bpp, 1Bpp */ make_red_table(1,3,0,0); make_green_table(2,1,0,0); make_blue_table(1,0,0,0); dither_fn_internal=dither_1byte; round_fn=round_1byte; break; case 65: /* 8 bpp, 1 Bpp */ make_red_table(3,5,0,0); make_green_table(3,2,0,0); make_blue_table(2,0,0,0); dither_fn_internal=dither_1byte; round_fn=round_1byte; break; case 122: /* 15bpp, 2Bpp */ make_red_table(5,10,1,0); make_green_table(5,5,1,0); make_blue_table(5,0,1,0); dither_fn_internal=dither_2byte; round_fn=round_2byte; break; case 378: /* 15bpp, 2Bpp, disordered (I have a mental disorder) */ make_red_table(5,10,1,1); make_green_table(5,5,1,1); make_blue_table(5,0,1,1); dither_fn_internal=dither_2byte; round_fn=round_2byte; break; case 130: /* 16bpp, 2Bpp */ make_red_table(5,11,1,0); make_green_table(6,5,1,0); make_blue_table(5,0,1,0); dither_fn_internal=dither_2byte; round_fn=round_2byte; break; case 386: /* 16bpp, 2Bpp, disordered */ make_red_table(5,11,1,1); make_green_table(6,5,1,1); make_blue_table(5,0,1,1); dither_fn_internal=dither_2byte; round_fn=round_2byte; break; case 451: /* 24bpp, 3Bpp, misordered * Even this is dithered! * R G B */ make_red_table(8,0,0,0); make_green_table(8,0,0,0); make_blue_table(8,0,0,0); dither_fn_internal=dither_451; round_fn=round_451; break; case 195: /* 24bpp, 3Bpp * Even this is dithered! * B G R */ make_red_table(8,0,0,0); make_green_table(8,0,0,0); make_blue_table(8,0,0,0); dither_fn_internal=dither_195; round_fn=round_195; break; case 452: /* 24bpp, 4Bpp, misordered * Even this is dithered! * 0 B G R */ make_red_table(8,0,0,0); make_green_table(8,0,0,0); make_blue_table(8,0,0,0); dither_fn_internal=dither_452; round_fn=round_452; break; case 196: /* 24bpp, 4Bpp * Even this is dithered! * B G R 0 */ make_red_table(8,0,0,0); make_green_table(8,0,0,0); make_blue_table(8,0,0,0); dither_fn_internal=dither_196; round_fn=round_196; break; case 708: /* 24bpp, 4Bpp * Even this is dithered! * 0 R G B */ make_red_table(8,0,0,0); make_green_table(8,0,0,0); make_blue_table(8,0,0,0); dither_fn_internal=dither_708; round_fn=round_708; break; default: internal("Graphics driver returned unsupported \pixel memory organisation %d",depth); } make_round_tables();}/* Input is in sRGB space (unrounded, i. e. directly from HTML) * Output is linear 48-bit value (in photons) that has corresponding * voltage nearest to the voltage that would be procduced ideally * by the input value. */void round_color_sRGB_to_48(unsigned short *red, unsigned short *green, unsigned short *blue, int rgb){ *red=round_red_table[(rgb>>16)&255]; *green=round_green_table[(rgb>>8)&255]; *blue=round_blue_table[rgb&255];}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -