📄 libfbx-ppm.c
字号:
/* * libfbx-ppm.c -- PPM Image Functions * (C)opyright 2000-2001 U4X Labs * * Written by: Paul Mundt <lethal@stampede.org> * Mike Bourgeous <nitrogen@u4x.org> * Sat Sep 2 01:45:15 EDT 2000 * * $Id: libfbx-ppm.c,v 1.12 2001/01/01 08:46:17 lethal Exp $ * * Originally part of fbgraph.c, now separate. Handles the * loading and saving of PPM format image files. * * See ChangeLog for modifcations, 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_load_ppm() * Arguments: Filename of ppm image to load * Returns: Image on an fb surface * Description: Attempts to load a ppm format image and * place it in an fb_surface memory bitmap. */fb_surface *fb_load_ppm(char *filename){ fb_surface *input_ppm; FILE *ppm_file; char buf[1024]; char tmpc1, tmpc2; int num = 0, gonum = 0, i, w = 0, h = 0, m = 0, x, y, r, g, b;/* * Reduces duplicate code. Trust me. */#define tmpc1_if() \{ \ if (tmpc1 == '#') { \ fgets(buf, 1024, ppm_file); \ tmpc1 = fgetc(ppm_file); \ } \} ppm_file = fopen(filename, "rb"); if (ppm_file == NULL) { snprintf(buf, 1024, "Unable to open %s", filename); perror(buf); return NULL; } tmpc1 = fgetc(ppm_file); tmpc2 = fgetc(ppm_file); if (tmpc1 != 'P' || tmpc2 != '6') { fprintf(stderr, "This file isn't a proper format ppm. " "Can only handle RAWBITS format.\n"); return NULL; } /* reduces duplicate code */ goto first; /* HACK HACK HACK HACK!!!!!!! */ for (num = 0; num < 3; num++) { second: gonum++; while (isspace(tmpc1)) { first: tmpc1 = fgetc(ppm_file); tmpc1_if(); if (gonum == 0) goto second; } buf[0] = tmpc1; buf[1] = '\0'; for (i = 1; !isspace(tmpc1); i++) { tmpc1 = fgetc(ppm_file); tmpc1_if(); buf[i] = tmpc1; buf[i + 1] = '\0'; } if (num == 0) w = atoi(buf); else if (num == 1) h = atoi(buf); else m = atoi(buf); } input_ppm = fb_create_surface(w, h); for (y = 0; y < h && !feof(ppm_file); y++) for(x = 0; x < w && !feof(ppm_file); x++) { r = (unsigned char)fgetc(ppm_file); g = (unsigned char)fgetc(ppm_file); b = (unsigned char)fgetc(ppm_file); fb_putpixel(x, y, r, g, b, input_ppm); } fclose(ppm_file); return input_ppm;}/* * Function: fb_save_ppm() * Arguments: Name of file to save to, surface to save * Returns: Zero on success, otherwise non zero * Description: Saves the contents of the surface indicated to a ppm * RAWBITS format image file. */int fb_save_ppm(char *filename, fb_surface *surface, int grayscale){ int x, y; int r, g, b; FILE *output_image; output_image = fopen(filename, "wb"); if (surface == NULL) return -1; if (output_image == NULL) return -1; fprintf(output_image, "P%d\n", grayscale ? 5 : 6); fprintf(output_image, "%i %i\n", surface->width, surface->height); fprintf(output_image, "255\n"); for (y = 0; y < surface->width; y++) for (x = 0; x < surface->width; x++) { fb_getpixel(x, y, &r, &g, &b, surface); fputc(r & 0xFF, output_image); fputc(g & 0xFF, output_image); fputc(b & 0xFF, output_image); } if (fclose(output_image)) return errno; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -