📄 display.c
字号:
/* display.c, X11 interface *//* Copyright (C) 1994, MPEG Software Simulation Group. All Rights Reserved. *//* * Disclaimer of Warranty * * These software programs are available to the user without any license fee or * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims * any and all warranties, whether express, implied, or statuary, including any * implied warranties or merchantability or of fitness for a particular * purpose. In no event shall the copyright-holder be liable for any * incidental, punitive, or consequential damages of any kind whatsoever * arising from the use of these programs. * * This disclaimer of warranty extends to the user of these programs and user's * customers, employees, agents, transferees, successors, and assigns. * * The MPEG Software Simulation Group does not represent or warrant that the * programs furnished hereunder are free of infringement of any third-party * patents. * * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware, * are subject to royalty fees to patent holders. Many of these patents are * general enough such that they are unavoidable regardless of implementation * design. * */#ifdef DISPLAY /* the Xlib interface is closely modeled after * mpeg_play 2.0 by the Berkeley Plateau Research Group */#include <stdio.h>#include <stdlib.h>#include <X11/Xlib.h>#include <X11/Xutil.h>#include "config.h"#include "global.h"/* private prototypes */static void display_image _ANSI_ARGS_((XImage *ximage, unsigned char *dithered_image));static void ditherframe _ANSI_ARGS_((unsigned char *src[]));static void dithertop _ANSI_ARGS_((unsigned char *src[], unsigned char *dst));static void ditherbot _ANSI_ARGS_((unsigned char *src[], unsigned char *dst));static void dithertop420 _ANSI_ARGS_((unsigned char *src[], unsigned char *dst));static void ditherbot420 _ANSI_ARGS_((unsigned char *src[], unsigned char *dst));/* local data */static unsigned char *dithered_image, *dithered_image2;static unsigned char ytab[256+16];static unsigned char utab[128+16];static unsigned char vtab[128+16];/* X11 related variables */static Display *display;static Window window;static GC gc;static XImage *ximage, *ximage2;static unsigned char pixel[256];#ifdef SH_MEM#include <sys/ipc.h>#include <sys/shm.h>#include <X11/extensions/XShm.h>static int HandleXError _ANSI_ARGS_((Display *dpy, XErrorEvent *event));static void InstallXErrorHandler _ANSI_ARGS_((void));static void DeInstallXErrorHandler _ANSI_ARGS_((void));static int shmem_flag;static XShmSegmentInfo shminfo1, shminfo2;static int gXErrorFlag;static int CompletionType = -1;static int HandleXError(dpy, event)Display *dpy;XErrorEvent *event;{ gXErrorFlag = 1; return 0;}static void InstallXErrorHandler(){ XSetErrorHandler(HandleXError); XFlush(display);}static void DeInstallXErrorHandler(){ XSetErrorHandler(NULL); XFlush(display);}#endif/* connect to server, create and map window, * allocate colors and (shared) memory */void init_display(name)char *name;{ int crv, cbu, cgu, cgv; int y, u, v, r, g, b; int i; char dummy; int screen; Colormap cmap; int private; XColor xcolor; unsigned int fg, bg; char *hello = "MPEG-2 Display"; XSizeHints hint; XVisualInfo vinfo; XEvent xev; unsigned long tmp_pixel; XWindowAttributes xwa; display = XOpenDisplay(name); if (display == NULL) error("Can not open display\n"); screen = DefaultScreen(display); hint.x = 200; hint.y = 200; hint.width = horizontal_size; hint.height = vertical_size; hint.flags = PPosition | PSize; /* Get some colors */ bg = WhitePixel (display, screen); fg = BlackPixel (display, screen); /* Make the window */ if (!XMatchVisualInfo(display, screen, 8, PseudoColor, &vinfo)) { if (!XMatchVisualInfo(display, screen, 8, GrayScale, &vinfo)) error("requires 8 bit display\n"); } window = XCreateSimpleWindow (display, DefaultRootWindow (display), hint.x, hint.y, hint.width, hint.height, 4, fg, bg); XSelectInput(display, window, StructureNotifyMask); /* Tell other applications about this window */ XSetStandardProperties (display, window, hello, hello, None, NULL, 0, &hint); /* Map window. */ XMapWindow(display, window); /* Wait for map. */ do { XNextEvent(display, &xev); } while (xev.type != MapNotify || xev.xmap.event != window); XSelectInput(display, window, NoEventMask); /* matrix coefficients */ crv = convmat[matrix_coefficients][0]; cbu = convmat[matrix_coefficients][1]; cgu = convmat[matrix_coefficients][2]; cgv = convmat[matrix_coefficients][3]; /* allocate colors */ gc = DefaultGC(display, screen); cmap = DefaultColormap(display, screen); private = 0; /* color allocation: * i is the (internal) 8 bit color number, it consists of separate * bit fields for Y, U and V: i = (yyyyuuvv), we don't use yyyy=0000 * and yyyy=1111, this leaves 32 colors for other applications * * the allocated colors correspond to the following Y, U and V values: * Y: 24, 40, 56, 72, 88, 104, 120, 136, 152, 168, 184, 200, 216, 232 * U,V: -48, -16, 16, 48 * * U and V values span only about half the color space; this gives * usually much better quality, although highly saturated colors can * not be displayed properly * * translation to R,G,B is implicitly done by the color look-up table */ for (i=16; i<240; i++) { /* color space conversion */ y = 16*((i>>4)&15) + 8; u = 32*((i>>2)&3) - 48; v = 32*(i&3) - 48; y = 76309 * (y - 16); /* (255/219)*65536 */ r = clp[(y + crv*v + 32768)>>16]; g = clp[(y - cgu*u -cgv*v + 32768)>>16]; b = clp[(y + cbu*u + 32786)>>16]; /* X11 colors are 16 bit */ xcolor.red = r << 8; xcolor.green = g << 8; xcolor.blue = b << 8; if (XAllocColor(display, cmap, &xcolor) != 0) pixel[i] = xcolor.pixel; else { /* allocation failed, have to use a private colormap */ if (private) error("Couldn't allocate private colormap"); private = 1; if (!quiet) fprintf(stderr, "Using private colormap (%d colors were available).\n", i-16); /* Free colors. */ while (--i >= 16) { tmp_pixel = pixel[i]; /* because XFreeColors expects unsigned long */ XFreeColors(display, cmap, &tmp_pixel, 1, 0); } /* i is now 15, this restarts the outer loop */ /* create private colormap */ XGetWindowAttributes(display, window, &xwa); cmap = XCreateColormap(display, window, xwa.visual, AllocNone); XSetWindowColormap(display, window, cmap); } }#ifdef SH_MEM if (XShmQueryExtension(display)) shmem_flag = 1; else { shmem_flag = 0; if (!quiet) fprintf(stderr, "Shared memory not supported\nReverting to normal Xlib\n"); } if (shmem_flag) CompletionType = XShmGetEventBase(display) + ShmCompletion; InstallXErrorHandler(); if (shmem_flag) { ximage = XShmCreateImage(display, None, 8, ZPixmap, NULL, &shminfo1, coded_picture_width, coded_picture_height); if (!prog_seq) ximage2 = XShmCreateImage(display, None, 8, ZPixmap, NULL, &shminfo2, coded_picture_width, coded_picture_height); /* If no go, then revert to normal Xlib calls. */ if (ximage==NULL || (!prog_seq && ximage2==NULL)) { if (ximage!=NULL) XDestroyImage(ximage); if (!prog_seq && ximage2!=NULL) XDestroyImage(ximage2); if (!quiet) fprintf(stderr, "Shared memory error, disabling (Ximage error)\n"); goto shmemerror; } /* Success here, continue. */ shminfo1.shmid = shmget(IPC_PRIVATE, ximage->bytes_per_line * ximage->height, IPC_CREAT | 0777); if (!prog_seq) shminfo2.shmid = shmget(IPC_PRIVATE, ximage2->bytes_per_line * ximage2->height, IPC_CREAT | 0777); if (shminfo1.shmid<0 || (!prog_seq && shminfo2.shmid<0)) { XDestroyImage(ximage); if (!prog_seq) XDestroyImage(ximage2); if (!quiet) fprintf(stderr, "Shared memory error, disabling (seg id error)\n"); goto shmemerror; } shminfo1.shmaddr = (char *) shmat(shminfo1.shmid, 0, 0); shminfo2.shmaddr = (char *) shmat(shminfo2.shmid, 0, 0); if (shminfo1.shmaddr==((char *) -1) || (!prog_seq && shminfo2.shmaddr==((char *) -1))) { XDestroyImage(ximage); if (shminfo1.shmaddr!=((char *) -1)) shmdt(shminfo1.shmaddr); if (!prog_seq) { XDestroyImage(ximage2); if (shminfo2.shmaddr!=((char *) -1)) shmdt(shminfo2.shmaddr); } if (!quiet) { fprintf(stderr, "Shared memory error, disabling (address error)\n"); } goto shmemerror; } ximage->data = shminfo1.shmaddr; dithered_image = (unsigned char *)ximage->data; shminfo1.readOnly = False; XShmAttach(display, &shminfo1); if (!prog_seq) { ximage2->data = shminfo2.shmaddr; dithered_image2 = (unsigned char *)ximage2->data; shminfo2.readOnly = False; XShmAttach(display, &shminfo2); } XSync(display, False); if (gXErrorFlag) { /* Ultimate failure here. */ XDestroyImage(ximage); shmdt(shminfo1.shmaddr); if (!prog_seq) { XDestroyImage(ximage2); shmdt(shminfo2.shmaddr); } if (!quiet) fprintf(stderr, "Shared memory error, disabling.\n"); gXErrorFlag = 0; goto shmemerror; } else { shmctl(shminfo1.shmid, IPC_RMID, 0); if (!prog_seq) shmctl(shminfo2.shmid, IPC_RMID, 0); } if (!quiet) { fprintf(stderr, "Sharing memory.\n"); } } else {shmemerror: shmem_flag = 0;#endif ximage = XCreateImage(display,None,8,ZPixmap,0,&dummy, coded_picture_width,coded_picture_height,8,0); if (!(dithered_image = (unsigned char *)malloc(coded_picture_width* coded_picture_height))) error("malloc failed"); if (!prog_seq) { ximage2 = XCreateImage(display,None,8,ZPixmap,0,&dummy, coded_picture_width,coded_picture_height,8,0); if (!(dithered_image2 = (unsigned char *)malloc(coded_picture_width* coded_picture_height))) error("malloc failed"); }#ifdef SH_MEM } DeInstallXErrorHandler();#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -