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

📄 libfbx-pixel.c

📁 libfxb是linux下只写操作framebuffer的一个轻量级的库。
💻 C
字号:
/* *  libfbx-pixel.c -- Pixel Drawing and Reading Functions *  (C)opyright 2000-2001 U4X Labs * *  Written by: Mike Bourgeous <nitrogen@u4x.org> *              Sat Sep 2 17:40:09 EDT 2000 * *  Updated by: Paul Mundt     <lethal@u4x.org> *              Sun Dec 10 20:52:31 EST 2000 * *              Kenneth Mills  <ken@u4x.org> *              Sun Dec 10 20:53:00 EST 2000 * *  $Id: libfbx-pixel.c,v 1.12 2001/01/01 08:46:17 lethal Exp $ * *       Originally part of fbgraph.c, now separate.  Responsible  *  for drawing pixels onto surfaces and retreiving pixel values *  from surfaces.  fb_putpixel_3d() is in libfbx-3d.c. * *  See ChangeLog for modifications, CREDITS for credits. *  			 *  All source herein is copyright U4X Labs and its original author.  *  Any code modifications or additions are (C)opyright the original  *  author and U4X Labs respectively. * *  libfbx is free software; you can redistribute it and/or modify it  *  under the terms of the GNU Lesser General Public License as  *  published by the Free Software Foundation; either version 2.1 of  *  the License, or (at your option) any later version. * *  libfbx is distributed in the hope that it will be useful, but  *  WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  *  GNU Lesser General Public License for more details. * *  You should have received a copy of the GNU Lesser General Public *  License along with libfbx; if not, write to the Free Software  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *  USA */#include <stdio.h>#include <errno.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <signal.h>#include <time.h>#include <math.h>#include <linux/fb.h>#include <linux/kd.h>#include <sys/mman.h>#include <sys/ioctl.h>#include <sys/user.h>#include <termios.h>#include <ctype.h>#include <libfbx/libfbx.h>/* * Function:	fb_putpixel() * Arguments:	x and y positioners, rgb values, and pointer to surface * Returns:	None * Description: Draws a pixel to the surface at x, y position with rgb * 		value. */void fb_putpixel(int x, int y, int r, int g, int b, fb_surface *surface){	register unsigned char *ptr_char;	register unsigned short *ptr_short;	register unsigned int *ptr_int;	unsigned int c = 0;	ptr_char = 		(unsigned char *)ptr_short = 		(unsigned short *)ptr_int = 		(unsigned int *)(surface->mem_pointer + 				 surface->mem_offset + 				 y * surface->line_size + 				 x * surface->pixel_size);	if (x >= surface->width || x < 0 ||             y >= surface->height || y < 0)		return;	switch(surface->bpp) {		case 8:			c = surface->make_color(r, g, b);			*ptr_char = c & 0xFF;		case 15:		case 16:			c = surface->make_color(r, g, b);			*ptr_short = c & 0xFFFF;			return;		case 24:			ptr_int = (unsigned int *)(ptr_char - 1);						r >>= 8 - surface->red_size;			g >>= 8 - surface->green_size;			b >>= 8 - surface->blue_size;			c = fb_screen->make_color(r, g, b);						*(ptr_char + 2) = (c >> 16) & 0xFF;			*(ptr_char + 1) = (c >> 8) & 0xFF;			*(ptr_char + 0) = c & 0xFF;						return;		case 32:			c = surface->make_color(r, g, b);			*(ptr_int) = c;			return;		default:			r >>= 8 - surface->red_size;			g >>= 8 - surface->green_size;			b >>= 8 - surface->blue_size;			c = r << surface->red_offset | 			    g << surface->green_offset | 			    b << surface->blue_offset;	}}/* * Function:	fb_getpixel() * Arguments:	x, y positioners, rgb values, pointer to surface * Returns:	None * Description:	Gets a pixel on surface at specified x, y position */inline void fb_getpixel(int x, int y, 			       int *r, int *g, int *b, 			       fb_surface *surface){	register unsigned int c, *ptr;	ptr = (unsigned int *)(surface->mem_pointer +                                surface->mem_offset +                                y * surface->line_size +                                x * surface->pixel_size);	if (x >= surface->width || x < 0 ||             y >= surface->height || y < 0) {		*r = *g = *b = 0;		return;	}	c = *ptr;	switch(surface->bpp) {		case 16:			*r = (c & (31 << 11)) >> 8;			*g = (c & (63 << 5)) >> 3;			*b = (c & 31) << 3;			break;		case 24:		case 32:			*r = (c & (0xFF << fb_screen->red_offset))                                         >> fb_screen->red_offset;			*g = (c & (0xFF << fb_screen->green_offset))                                         >> fb_screen->green_offset;			*b = (c & (0xFF << fb_screen->blue_offset))                                         >> fb_screen->blue_offset;			break;		default:			fprintf(stderr, "Color depth %i is not yet"					"supported in fb_getpixel\n", 					surface->bpp);			break;	}}/* * Function:    fb_blend() * Arguments:	alpha, r1, g1, b1, r2, g2, b2, *              *r, *g, *b (gets blended value) * Returns:     None. * Description: blends first rgb values with second rgb values *              according to the value of alpha *               *              with alpha going from 0-255, 0 starts out as *              the first rgb values. 255 ends with the second *              rgb values. */void fb_blend(int alpha, int r1, int g1, int b1,              int r2, int g2, int b2,              int *r, int *g, int *b){	int alpha2;	if (alpha == 127) {		*r = (r1 + r2) >> 1;		*g = (g1 + g2) >> 1;		*b = (b1 + b2) >> 1;	} else if (alpha == 255) {		*r = r2;		*g = g2;		*b = b2;	} else if (!alpha) {		*r = r1;		*g = g1;		*b = b1;	} else {		alpha2 = 255 - alpha;		*r = floor((((r1 * alpha2) + (r2 * alpha)) >> 8));		*g = floor((((g1 * alpha2) + (g2 * alpha)) >> 8));		*b = floor((((b1 * alpha2) + (b2 * alpha)) >> 8));	}	      }/* * Function:    fb_putpixel_alpha() * Arguments:	alpha, x, y, rgb values, and surface * Returns:     None. * Description: draws a pixel onto a surface at x, y position *              with rgb and alpha values */void fb_putpixel_alpha(int alpha, int x, int y,                       int r1, int g1, int b1,                       fb_surface *surface){	int r2, g2, b2, r, g, b;	fb_getpixel(x, y, &r2, &g2, &b2, surface);	fb_blend(alpha, r1, g1, b1, r2, g2, b2, &r, &g, &b);	fb_putpixel(x, y, r, g, b, surface);}/* * Function:    fb_putpixel_aa() * Arguments:   alpha, x, y coords (float values), rgb values, surface. * Returns:     None. * Description: Draws an anti-aliased pixel to a surface at *              the specified x, y position, rgb, and alpha values. */void fb_putpixel_aa(int alpha, float x, float y, int r, int g, int b, fb_surface *surface){	float fx, fy, inv_fx, inv_fy;	int x2, y2;	x2 = floor(x);	y2 = floor(y);	// get fraction parts	fx = x - x2;	fy = y - y2;	// invert fraction parts	inv_fx = 1 - fx;	inv_fy = 1 - fy;	fb_putpixel_alpha(floor(((inv_fx * inv_fy) * alpha)),  x2,     y2,    r, g, b, surface); // top left	fb_putpixel_alpha(floor(((fx     * inv_fy) * alpha)), (x2++),  y2,    r, g, b, surface); // top right	fb_putpixel_alpha(floor(((inv_fx * fy)     * alpha)),  x2,    (y2++), r, g, b, surface); // bottom left	fb_putpixel_alpha(floor(((fx     * fy)     * alpha)), (x2++), (y2++), r, g, b, surface); // bottom right}

⌨️ 快捷键说明

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