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

📄 edit.c

📁 LINUX lilo-22.7 源代码。
💻 C
📖 第 1 页 / 共 2 页
字号:
/* edit.c -- bitmap file manipulation and editing *//*Copyright 2002-2004 John Coffman.All rights reserved.Licensed under the terms contained in the file 'COPYING' in the source directory.*/#define _GNU_SOURCE#include <unistd.h>#include <sys/types.h>#include <sys/statfs.h>#include <sys/stat.h>#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <errno.h>#include <string.h>#include <ctype.h>#include <math.h>#ifdef	_SYS_STATFS_H#define	_I386_STATFS_H	/* two versions of statfs is not good ... */#endif#include "config.h"#include "lilo.h"#include "common.h"#include "cfg.h"#include "temp.h"#include "bsect.h"#include "bitmap.h"#include "edit.h"#define USE_BSECT_PW_INPUT 0		/* use another's input routine */#define BMP_CONF ".dat"#define BMP_BMP  ".bmp"#define NPALETTE 256/* Luminance of a color 0..255 -- YIQ color model */#define Y(c) (((c).red*0.30+(c).green*0.59+(c).blue*0.11)/255)#define NORM(x) ((float)(x)/255.0)static BITMAPFILEHEADER fh;static BITMAPHEADER bmh;static RGB palette[NPALETTE];static int filepos, npal;static BITMAPLILOHEADER lh0 = {	{sizeof(BITMAPLILOHEADER), 0}, "LILO",	5*16, 12*8, 1,			/* row, col, ncol */	25, (MAX_IMAGE_NAME+1)*8,	/* maxcol, xpitch */	7, 7, 7,			/* normal text fg, bg, shadow */	15, 15, 15,			/* highlight  fg, bg, shadow  */	7, 0, 7,			/* timer text  fg, bg, shadow */	2*16, 64*8,			/* timer row, col */	4, {0, 0, 0}			/* mincol, reserved[3] */		};#if USE_BSECT_PW_INPUT#define getLine pw_input#elsestatic char *getLine(void){    char *pass;    char buf[MAX_TOKEN+1];    int i, ch;        i = 0;    fflush(stdout);    while((ch=getchar())!='\n') if (i<MAX_TOKEN) buf[i++]=ch;    buf[i]=0;    pass = stralloc(buf);/*    while (i) buf[--i]=0;	*/    return pass;}#endifint get_std_headers(int fd,	BITMAPFILEHEADER *fh,	BITMAPHEADER *bmh,	BITMAPLILOHEADER *lh){    short size;    BITMAPHEADER2 bmh2;    int n, i;        lseek(fd, 0, SEEK_SET);    if (read(fd, (void*)fh, sizeof(BITMAPFILEHEADER)) !=			sizeof(BITMAPFILEHEADER) )  return -1;    if (fh->magic != 0x4D42 /* "BM" */)  return 1;    if (read(fd, &size, sizeof(size)) != sizeof(size))   return -1;    if (size==sizeof(BITMAPHEADER2)) { /* an OS/2 bitmap */	if (read(fd, (void*)&bmh2+sizeof(size), sizeof(BITMAPHEADER2)-sizeof(size))		!= sizeof(BITMAPHEADER2)-sizeof(size) )   return -1;	memset(bmh, 0, sizeof(BITMAPHEADER));	bmh->width = bmh2.width;	bmh->height = bmh2.height;	n = bmh->numBitPlanes = bmh2.numBitPlanes;	n *= bmh->numBitsPerPlane = bmh2.numBitsPerPlane;	bmh->numColorsUsed = bmh->numImportantColors = 1 << n;	bmh->sizeImageData = *(int*)(fh->size) - *(int*)(fh->offsetToBits);	bmh->size = sizeof(*bmh);    /* new size!! */	n = sizeof(RGB2);    }    else if (size==sizeof(BITMAPHEADER)) {	if (read(fd, (void*)bmh+sizeof(size), sizeof(BITMAPHEADER)-sizeof(size))		!= sizeof(BITMAPHEADER)-sizeof(size) )   return -1;	bmh->size = size;	n = sizeof(RGB);    }    else  return 2;    *lh = lh0;    npal = 1 << (bmh->numBitPlanes * bmh->numBitsPerPlane);    colormax = npal - 1;    if (npal > nelem(palette) )   return 3;    for (i=0; i<npal; i++) {	if (read(fd, &palette[i], n) != n)  return -1;	if (n==sizeof(RGB2)) palette[i].null = 0;    }    if (*(int*)(fh->offsetToBits) == sizeof(BITMAPFILEHEADER) +    		sizeof(BITMAPHEADER) + sizeof(BITMAPLILOHEADER) +    		npal*sizeof(RGB) )  /* test will fail for OS/2 bitmaps */ {      /* get probable BITMAPLILOHEADER */	if (read(fd, &size, sizeof(size)) != sizeof(size))  return -1;	if (size != sizeof(BITMAPLILOHEADER))   return 4;	if (read(fd, (void*)lh+sizeof(size), sizeof(*lh)-sizeof(size)) !=		sizeof(*lh)-sizeof(size))  return -1;	*(int*)(lh->size) = size;	if (strncmp(lh->magic, "LILO", 4) != 0)   return 5;    } else { /* there is no BITMAPLILOHEADER present */#ifdef STANDALONE        printf("No BITMAPLILOHEADER\n");#endif    }/* file is left positioned at the start of the bitmap data */    filepos = lseek(fd, 0, SEEK_CUR);    return 0;}int put_std_bmpfile(int fd, int ifd,	BITMAPFILEHEADER *fh,	BITMAPHEADER *bmh,	BITMAPLILOHEADER *lh){    int n, total, npalette;    char buf[1024];        npalette = 1 << (bmh->numBitPlanes * bmh->numBitsPerPlane);    write(fd, fh, sizeof(*fh));    write(fd, bmh, sizeof(*bmh));    write(fd, palette, npalette*sizeof(palette[0]));    write(fd, lh, sizeof(*lh));    total=0;    lseek(ifd, filepos, SEEK_SET);    do {	n = read(ifd, buf, sizeof(buf));	if (n>0) {	    if (write(fd, buf, n) != n)  return -1;	    total += n;	}	else if (n<0)  printf("Error reading input\n");    } while (n>0);    bmh->sizeImageData = total;    *(int*)(fh->offsetToBits) = n = sizeof(BITMAPFILEHEADER) +    		sizeof(BITMAPHEADER) + sizeof(BITMAPLILOHEADER) +    		npalette*sizeof(RGB);    *(int*)(fh->size) = total + n;    lseek(fd, 0, SEEK_SET);    write(fd, fh, sizeof(*fh));    write(fd, bmh, sizeof(*bmh));    return 0;}#ifndef STANDALONEstatic	char *temp_file, *bitmap_file;static	int ifd, ofd;static	union {	   unsigned char buffer[256];	   MENUTABLE mt;	   BITMAPLILOHEADER bmlh;	} tm;static	MENUTABLE *menu = &tm.mt;static	BITMAPLILOHEADER *lh = (void*)tm.buffer +     			((long)&tm.mt.row - (long)&tm.bmlh.row);/* a convenience definition */#define mn tm.mt/* timer = 1 if timer is enabled, 0 if timer is disabled */#define timer (mn.t_row>=0)static int yesno(char *query, int def){    char *yn;    int ans = 2;    while (ans>1) {    	printf("%s (yes or no) [%c]:  ", query, def?'Y':'N');	yn = getLine();	if (!*yn) ans = def;	else if (toupper(*yn) == 'Y') ans = 1;	else if (toupper(*yn) == 'N') ans = 0;	free(yn);    }    return ans;}static void dat_file_creat(char *bmp){    char *datfile;    FILE *fdat;        datfile = stralloc(bmp);    *strrchr(datfile,*(BMP_BMP)) = 0;    strcat(datfile, BMP_CONF);    if (!(fdat = fopen(datfile, "w"))) pdie("Open .dat file");    fprintf(fdat,"#\n# generated companion file to:\n#\n");    fprintf(fdat,"bitmap = %s\n", bmp);        fprintf(fdat,"bmp-table = %d%s,%d%s;%d,%d,%d%s,%d\n",    	mn.col%8 ? mn.col : mn.col/8+1,    	mn.col%8 ? "p" : "",    	mn.row%16 ? mn.row : mn.row/16+1,    	mn.row%16 ? "p" : "",    	mn.ncol,    	mn.maxcol,    	mn.xpitch%8 ? mn.xpitch : mn.xpitch/8,    	mn.xpitch%8 ? "p" : "",    	mn.mincol );        fprintf(fdat,"bmp-colors = %d,", mn.fg);    if (mn.bg != mn.fg) fprintf(fdat,"%d",mn.bg);    putc(',',fdat);    if (mn.sh != mn.fg) fprintf(fdat,"%d",mn.sh);    putc(';',fdat);    fprintf(fdat,"%d,", mn.h_fg);    if (mn.h_bg != mn.h_fg) fprintf(fdat,"%d",mn.h_bg);    putc(',',fdat);    if (mn.h_sh != mn.h_fg) fprintf(fdat,"%d",mn.h_sh);    putc('\n',fdat);    fprintf(fdat,"bmp-timer = ");    if (mn.t_row < 0) fprintf(fdat,"none\n");    else {	fprintf(fdat,"%d%s,%d%s;%d,",	    mn.t_col%8 ? mn.t_col : mn.t_col/8+1,	    mn.t_col%8 ? "p" : "",	    mn.t_row%16 ? mn.t_row : mn.t_row/16+1,	    mn.t_row%16 ? "p" : "",	    mn.t_fg );	if (mn.t_bg != mn.t_fg) fprintf(fdat,"%d", mn.t_bg);	putc(',',fdat);	if (mn.t_sh != mn.t_fg) fprintf(fdat,"%d", mn.t_sh);	putc('\n',fdat);    }    fclose(fdat);}static void bmp_file_open(char *bmp){    int n;    bitmap_file = bmp;    temp_file = strcat(strcpy(alloc(strlen(bitmap_file)+strlen(MAP_TMP_APP)+1),    			      bitmap_file),    		       MAP_TMP_APP);    ifd = open(bitmap_file, O_RDONLY);    if (ifd<0) pdie("Cannot open bitmap file");    ofd = open(temp_file, O_CREAT|O_WRONLY, 0644);    if (ofd<0) pdie("Cannot open temporary file");    temp_register(temp_file);        n = get_std_headers(ifd, &fh, &bmh, lh);    if (verbose >= 3) printf("get_std_headers:  returns %d\n", n);        if (n<0) die("read file '%s': %s", bitmap_file, strerror(errno));    switch (n) {    	case 1:    	case 2:    	    die("Not a bitmap file '%s'", bitmap_file);    	case 3:    	    die("Unsupported bitmap file '%s' (%d bit color)", bitmap_file,    	    	bmh.numBitPlanes*bmh.numBitsPerPlane);    	case 4:    	case 5:    	    die("Unrecognized auxiliary header in file '%s'", bitmap_file);    	default:    	    ;    }}static void bmp_file_close(int update){    int n;        if (update) n = put_std_bmpfile(ofd, ifd, &fh, &bmh, lh);        close(ifd);    close(ofd);    temp_unregister(temp_file);    if (!update || test) {	if (verbose < 9) remove(temp_file);    } else {	n = rename(temp_file, bitmap_file);    }}static void location(char *what, short x, short y){    printf("%sColumn(X): %d%s (chars) or %hdp (pixels)", what, x/8+1, x%8?"+":"", x);    printf("   Row(Y): %d%s (chars) or %hdp (pixels)\n", y/16+1, y%16?"+":"", y);}static void color(char *what, short fg, short bg, short sh){static char sp[] = "   ";    printf("%sForeground: %hd%sBackground: ", what, fg, sp);    if (bg==fg) printf("transparent%s",sp);    else printf("%hd%s", bg, sp);    printf("Shadow: ");    if (sh==fg) printf("none\n");    else printf("%hd\n", sh);}static void get3colors(char *what, short *color){static char *co[] = { "fg", "bg", "sh" };static char *op[] = { "", ",transparent", ",none" };    int i;    int tr, no;    unsigned int c;    char n[4], *line, *end;    int dcol[3];    for (i=0; i<3; i++) dcol[i] = color[i];	/* save inputs */    tr = (dcol[0] == dcol[1]);    no = (dcol[0] == dcol[2]);        printf("\n");    for (i=0; i<3; i++) {	sprintf(n, "%hd", dcol[i]);	printf("%s text %s color (0..%d%s) [%s]: ", what, co[i], npal-1, op[i],		i==1 && tr ? "transparent" :		i==2 && no ? "none" : n);	line = getLine();	if (!*line) c = dcol[i];	else if (toupper(*line)=='T' && i==1) c = color[0];	else if (toupper(*line)=='N' && i==2) c = color[0];	else {	    c = strtol(line, &end, 0);	    if (line==end || c>=npal || *end) {		c = dcol[i];		printf("???\n");	    }	}	color[i] = c;	free(line);	if (i==0) {	    if (tr) dcol[1]=c;	    if (no) dcol[2]=c;	}    }}static void number(char *what, short *num, int min, int max){    char *line, *end;    int val;        printf("%s (%d..%d) [%hd]:  ", what, min, max, *num);    line = getLine();    if (!*line) val = *num;    else {	val = strtol(line, &end, 0);	if (val < min || val > max || *end) {	    printf("???");	    val = *num;	}    }    free(line);    *num = val;}static void getXY(char *what, short *locp, int scale, int abs){    char *line, *end;    int val;    int min = abs ? 1 : MAX_IMAGE_NAME;    int minp = min*scale;    int max = scale==8 ? 80 : 30;    int maxp = (max-abs)*scale;    int loc = *locp/scale + abs;    char *plus = *locp%scale ? "+" : "";        printf("%s (%d..%d) or (%dp..%dp) [%d%s or %dp]: ", what,    			min, max, minp, maxp, loc, plus, (int)*locp);        line = getLine();    if (!*line) val = *locp;    else {	val = strtol(line, &end, 0);	if (line==end || (*end && toupper(*end)!='P')) {	    val = *locp;	    printf("???1\n");	}	if (toupper(*end)!='P') val = (val-1)*scale;	if (val > maxp) {	    val = *locp;	    printf("???2\n");	}    }    *locp = val;    free(line);}static void show_timer(void){    if (timer) {	color("    Timer:  ", mn.t_fg, mn.t_bg, mn.t_sh);	location("Timer position:\n  ", mn.t_col, mn.t_row);    }    else    {	printf("\n\tThe timer is DISABLED.\n");    }}static void show_colors(int timopt){

⌨️ 快捷键说明

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