📄 display.c
字号:
* not be displayed properly
*
* translation to R,G,B is implicitly done by the color look-up table */
for (i = 32; 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)
perror ("Couldn't allocate private colormap");
private = 1;
if (!quiet)
{
char tmp[256];
sprintf(tmp,"\nerror: Using private colormap (%d colors were "
"available).\n", i - 32);
perror(tmp);
}
/* Free colors. */
while (--i >= 32)
{
tmp_pixel = pixel[i]; /* because XFreeColors expects unsigned
* long */
XFreeColors (display, cmap, &tmp_pixel, 1, 0);
}
/* i is now 31, 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)
perror("\nerror: Shared memory not supported\nReverting to normal "
"Xlib\n");
}
if (shmem_flag)
CompletionType = XShmGetEventBase (display) + ShmCompletion;
InstallXErrorHandler ();
if (shmem_flag)
{
if (expand)
ximage = XShmCreateImage (display, visual, dpy_depth, ZPixmap, NULL,
&shminfo1,
2 * coded_picture_width, 2 * coded_picture_height);
else
ximage = XShmCreateImage (display, visual, dpy_depth, ZPixmap, NULL,
&shminfo1,
coded_picture_width, coded_picture_height);
/* If no go, then revert to normal Xlib calls. */
if (ximage == NULL)
{
if (ximage != NULL)
XDestroyImage (ximage);
if (!quiet)
perror("\nerror: 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 (shminfo1.shmid < 0)
{
XDestroyImage (ximage);
if (!quiet)
perror("\nerror: 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))
{
XDestroyImage (ximage);
if (shminfo1.shmaddr != ((char *) -1))
shmdt (shminfo1.shmaddr);
if (!quiet)
{
perror("\nerror: 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);
XSync (display, False);
if (gXErrorFlag)
{
/* Ultimate failure here. */
XDestroyImage (ximage);
shmdt (shminfo1.shmaddr);
if (!quiet)
perror("\nerror: Shared memory error, disabling.\n");
gXErrorFlag = 0;
goto shmemerror;
} else
{
shmctl (shminfo1.shmid, IPC_RMID, 0);
}
if (!quiet)
{
perror("\nerror: Sharing memory.\n");
}
} else
{
shmemerror:
shmem_flag = 0;
#endif
if (expand)
{
ximage = XCreateImage (display, visual, dpy_depth, ZPixmap, 0, &dummy,
2 * coded_picture_width, 2 * coded_picture_height, 8, 0);
if (!(dithered_image =
(unsigned char *) malloc (coded_picture_width * coded_picture_height *
(dpy_depth > 8 ? sizeof (int) * 4 :
sizeof (unsigned char)) * 4)))
perror ("malloc failed");
} else
{
ximage = XCreateImage (display, visual, dpy_depth, ZPixmap, 0, &dummy,
coded_picture_width, coded_picture_height, 8, 0);
if (!(dithered_image =
(unsigned char *) malloc (coded_picture_width * coded_picture_height *
(dpy_depth > 8 ? sizeof (int) :
sizeof (unsigned char)))))
perror ("malloc failed");
}
#ifdef SH_MEM
}
DeInstallXErrorHandler ();
#endif
if (dpy_depth == 32 || dpy_depth == 24 || dpy_depth == 16)
{
XWindowAttributes xwa;
XGetWindowAttributes (display, window, &xwa);
wpixel[0] = xwa.visual->red_mask;
wpixel[1] = xwa.visual->green_mask;
wpixel[2] = xwa.visual->blue_mask;
/* If the colors in 16/24/32-bit mode are wrong, try this instead of
* the above three lines */
/* wpixel[2] = xwa.visual->red_mask; wpixel[1] =
* xwa.visual->green_mask; wpixel[0] = xwa.visual->blue_mask; */
InitColorDither (dpy_depth == 24 || dpy_depth == 32);
} else
{
ord4x4_dither_init ();
}
}
void exit_display ()
{
#ifdef SH_MEM
if (shmem_flag)
{
XShmDetach (display, &shminfo1);
XDestroyImage (ximage);
shmdt (shminfo1.shmaddr);
}
#endif
if(clp1 != NULL)
{
free(clp1);
clp1 = NULL;
}
}
static void display_image (ximage, dithered_image)
XImage *ximage;
unsigned char *dithered_image;
{
int t = 1;
/* Always work in native bit and byte order. This tells Xlib to reverse
* bit and byte order if necessary when crossing a network. Frankly,
* this part of XImages is somewhat underdocumented, so this may not be
* exactly correct. */
if (*(char *) &t == 1)
{
ximage->byte_order = LSBFirst;
ximage->bitmap_bit_order = LSBFirst;
} else
{
ximage->byte_order = MSBFirst;
ximage->bitmap_bit_order = MSBFirst;
}
/* display dithered image */
#ifdef SH_MEM
if (shmem_flag)
{
XShmPutImage (display, window, gc, ximage,
0, 0, 0, 0, ximage->width, ximage->height, True);
XFlush (display);
while (1)
{
XEvent xev;
XNextEvent (display, &xev);
if (xev.type == CompletionType)
break;
}
} else
#endif
{
ximage->data = (char *) dithered_image;
XPutImage (display, window, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
}
}
void dither (src)
unsigned char *src[];
{
if (dpy_depth == 24 || dpy_depth == 32)
{
if (ximage->bits_per_pixel == 24)
ConvertYUVtoRGB (src[0], src[1], src[2], dithered_image,
coded_picture_width,
coded_picture_height);
else
Color32DitherImage (src, dithered_image);
} else if (dpy_depth == 16)
{
Color16DitherImage (src, dithered_image);
} else
{
ord4x4_dither_frame (src, dithered_image);
}
display_image (ximage, dithered_image);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -