?? gdith.c
字號:
/* * gdith.c -- * * Procedures dealing with grey-scale and mono dithering, * as well as X Windows set up procedures. * *//* * Copyright (c) 1995 The Regents of the University of California. * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice and the following * two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. *//* * Portions of this software Copyright (c) 1995 Brown University. * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement * is hereby granted, provided that the above copyright notice and the * following two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */#include <math.h>#include "video.h"#include "proto.h"#include "dither.h"#ifndef NOCONTROLS#include "ctrlbar.h"#endif#include <sys/time.h>#ifdef __STDC__#include <stdlib.h>#include <string.h>#endif/* Changes to make the code reentrant: X variables now passed in xinfo: display, ximage,cmap,window, gc, etc De-globalized: ditherType, matched_depth, totNumFrames vid_stream->film_has_ended instead of FilmState Additional changes: Now can name and position each movie window individually DISABLE_DITHER cpp define - do not include dither code if defined NOFRAMECOUNT cpp define - do not count frames when running without controls Short circuit InitColorDisplay if not displaying anything ExistingWindow default now 0 -lsh@cs.brown.edu (Loring Holden)*//* Range values for lum, cr, cb. */int LUM_RANGE;int CR_RANGE;int CB_RANGE;/* Array that remaps color numbers to actual pixel values used by X server. */unsigned char pixel[256];unsigned long wpixel[256];/* Arrays holding quantized value ranged for lum, cr, and cb. */int *lum_values;int *cr_values;int *cb_values;/* Declaration of global variable containing dither type. *//* Frame Rate Info */extern int framerate;/* Video rates table *//* Cheat on Vid rates, round to 30, and use 30 if illegal value Except for 9, where Xing means 15, and given their popularity, we'll be nice and do it */static int VidRateNum[16]={30, 24, 24, 25, 30, 30, 50, 60, 60, 15, 30, 30, 30, 30, 30, 30};/* Luminance and chrominance lookup tables */static double *L_tab, *Cr_r_tab, *Cr_g_tab, *Cb_g_tab, *Cb_b_tab;/* *-------------------------------------------------------------- * * InitColor -- * * Initialize lum, cr, and cb quantized range value arrays. * Also initializes the lookup tables for the possible * values of lum, cr, and cb. * Color values from ITU-R BT.470-2 System B, G and SMPTE 170M * see InitColorDither in 16bits.c for more * * Results: * None. * * Side effects: * None. * *-------------------------------------------------------------- */voidInitColor(){ int i, CR, CB; L_tab = (double *)malloc(LUM_RANGE*sizeof(double)); Cr_r_tab = (double *)malloc(CR_RANGE*sizeof(double)); Cr_g_tab = (double *)malloc(CR_RANGE*sizeof(double)); Cb_g_tab = (double *)malloc(CB_RANGE*sizeof(double)); Cb_b_tab = (double *)malloc(CB_RANGE*sizeof(double)); if (L_tab == NULL || Cr_r_tab == NULL || Cr_g_tab == NULL || Cb_g_tab == NULL || Cb_b_tab == NULL) { fprintf(stderr, "Could not alloc memory in InitColor\n"); exit(1); } for (i=0; i<LUM_RANGE; i++) { lum_values[i] = ((i * 256) / (LUM_RANGE)) + (256/(LUM_RANGE*2)); L_tab[i] = lum_values[i]; if (gammaCorrectFlag) { L_tab[i] = GAMMA_CORRECTION(L_tab[i]); } } for (i=0; i<CR_RANGE; i++) { register double tmp; if (chromaCorrectFlag) { tmp = ((i * 256) / (CR_RANGE)) + (256/(CR_RANGE*2)); Cr_r_tab[i] = (int) (0.419/0.299) * CHROMA_CORRECTION128D(tmp - 128.0); Cr_g_tab[i] = (int) -(0.299/0.419) * CHROMA_CORRECTION128D(tmp - 128.0); cr_values[i] = CHROMA_CORRECTION256(tmp); } else { tmp = ((i * 256) / (CR_RANGE)) + (256/(CR_RANGE*2)); Cr_r_tab[i] = (int) (0.419/0.299) * (tmp - 128.0); Cr_g_tab[i] = (int) -(0.299/0.419) * (tmp - 128.0); cr_values[i] = (int) tmp; } } for (i=0; i<CB_RANGE; i++) { register double tmp; if (chromaCorrectFlag) { tmp = ((i * 256) / (CB_RANGE)) + (256/(CB_RANGE*2)); Cb_g_tab[i] = (int) -(0.114/0.331) * CHROMA_CORRECTION128D(tmp - 128.0); Cb_b_tab[i] = (int) (0.587/0.331) * CHROMA_CORRECTION128D(tmp - 128.0); cb_values[i] = CHROMA_CORRECTION256(tmp); } else { tmp = ((i * 256) / (CB_RANGE)) + (256/(CB_RANGE*2)); Cb_g_tab[i] = (int) -(0.114/0.331) * (tmp - 128.0); Cb_b_tab[i] = (int) (0.587/0.331) * (tmp - 128.0); cb_values[i] = (int) tmp; } }}/* *-------------------------------------------------------------- * * ConvertColor -- * * Given a l, cr, cb tuple, converts it to r,g,b. * * Results: * r,g,b values returned in pointers passed as parameters. * * Side effects: * None. * *-------------------------------------------------------------- */static voidConvertColor(l, cr, cb, r, g, b) unsigned int l, cr, cb; unsigned char *r, *g, *b;{ double fl, fcr, fcb, fr, fg, fb;/* * Old method w/o lookup table * * fl = 1.164*(((double) l)-16.0); * fcr = ((double) cr) - 128.0; * fcb = ((double) cb) - 128.0; * * fr = fl + (1.366 * fcr); * fg = fl - (0.700 * fcr) - (0.334 * fcb); * fb = fl + (1.732 * fcb); */ fl = L_tab[l]; fr = fl + Cr_r_tab[cr]; fg = fl + Cr_g_tab[cr] + Cb_g_tab[cb]; fb = fl + Cb_b_tab[cb]; if (fr < 0.0) fr = 0.0; else if (fr > 255.0) fr = 255.0; if (fg < 0.0) fg = 0.0; else if (fg > 255.0) fg = 255.0; if (fb < 0.0) fb = 0.0; else if (fb > 255.0) fb = 255.0; *r = (unsigned char) fr; *g = (unsigned char) fg; *b = (unsigned char) fb;}#ifdef SH_MEMint gXErrorFlag = 0;int HandleXError(dpy, event) Display *dpy; XErrorEvent *event;{ gXErrorFlag = 1; return 0;}int HandleXError();void InstallXErrorHandler(display) Display *display;{ XSetErrorHandler(HandleXError); XFlush(display);}void DeInstallXErrorHandler(display) Display *display;{ XSetErrorHandler(NULL); XFlush(display);}#endif/* *-------------------------------------------------------------- * * ResizeDisplay -- * * Resizes display window. * * Results: * None. * * Side effects: * None. * *-------------------------------------------------------------- */void ResizeDisplay(w, h, xinfo) unsigned int w, h; XInfo *xinfo;{#ifndef DISABLE_DITHER if (xinfo->ditherType == NO_DITHER || xinfo->ditherType == PPM_DITHER) return;#endif XResizeWindow(xinfo->display, xinfo->window, w, h); XFlush(xinfo->display);}/* *-------------------------------------------------------------- * * MakeWindow -- * * Create X Window * * Results: * Read the code. * * Side effects: * None. * *-------------------------------------------------------------- */#ifdef SH_MEMint CompletionType = -1;#endifstatic intMakeWindow(name, xinfo) char *name;XInfo *xinfo;{ unsigned int fg, bg; char *hello = (xinfo->name == NULL) ? "MPEG Play" : xinfo->name; int screen;/* void CreateFullColorWindow();*/ XVisualInfo vinfo; Display *display; if (xinfo == NULL) return 0;#ifndef DISABLE_DITHER if ((xinfo->ditherType == NO_DITHER) || (xinfo->ditherType == PPM_DITHER)) return 0;#endif if (xinfo->display == NULL) { display = xinfo->display = XOpenDisplay(name); if (xinfo->display == NULL) { fprintf(stderr, "Cannot open display\n"); exit(-2); } } display = xinfo->display;#ifdef SH_MEM if(shmemFlag) CompletionType = XShmGetEventBase(display) + ShmCompletion;#endif screen = DefaultScreen (display); /* Fill in hint structure */ if (xinfo->hints.width == 0) { xinfo->hints.width = 150; xinfo->hints.height = 150; } xinfo->hints.x = xinfo->hints.x; xinfo->hints.y = xinfo->hints.y; xinfo->hints.flags = PPosition | PSize; /* Get some colors */ bg = WhitePixel(display, screen); fg = BlackPixel(display, screen); if (xinfo->ExistingWindow) { /* This dowsnt work. it used to. why not? */ xinfo->window = xinfo->ExistingWindow; if (!quietFlag) printf("Display is 0x%X, window is 0x%X\n", display, xinfo->window); return TRUE; } /* Make the window */ #ifndef DISABLE_DITHER if (xinfo->ditherType == FULL_COLOR_DITHER || xinfo->ditherType == FULL_COLOR2_DITHER) {#endif CreateFullColorWindow (xinfo); if (xinfo->window == 0) { fprintf (stderr, "-color option only valid on full color display\n"); exit(-1); }#ifndef DISABLE_DITHER } else { if (((XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo) != 0) || (XMatchVisualInfo (display, screen, 24, DirectColor, &vinfo) != 0)) && (!quietFlag)) { printf("\nOn 24 bit displays: use -dither color to get full color\n\t\tordered dither is the default.\n"); } if (xinfo->ditherType == MONO_DITHER || xinfo->ditherType == MONO_THRESHOLD) { xinfo->window = XCreateSimpleWindow (display, DefaultRootWindow(display), xinfo->hints.x, xinfo-> hints.y, xinfo->hints.width, xinfo->hints.height, 4, fg, bg); xinfo->depth = 1; } else { Visual *vis; XSetWindowAttributes attrib; unsigned long attrib_flags=0; if (!XMatchVisualInfo (display, screen, xinfo->depth = 8, PseudoColor, &vinfo)) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -