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

📄 gvceps.c

📁 GSview 4.6 PostScript previewer。Ghostscript在MS-Windows, OS/2 and Unix下的图形化接口
💻 C
📖 第 1 页 / 共 5 页
字号:
	        mfh.checksum =  0;
	        pw = (WORD *)&mfh;
		temp = 0;
	        for (i=0; i<10; i++) {
	    	    temp ^= *pw++;
	        }
		mfh.checksum = (WORD)temp;
	        write_dword(mfh.key, outfile);
	        write_word(mfh.hmf, outfile);
	        write_word(mfh.bbox.left,   outfile);
	        write_word(mfh.bbox.top,    outfile);
	        write_word(mfh.bbox.right,  outfile);
	        write_word(mfh.bbox.bottom, outfile);
	        write_word(mfh.inch, outfile);
	        write_dword(mfh.reserved, outfile);
	        write_word(mfh.checksum, outfile);
	    }
	}

        while ( (count = (unsigned int)min(len,COPY_BUF_SIZE)) != 0 ) {
	    count = fread(buffer, 1, count, epsfile);
	    fwrite(buffer, 1, count, outfile);
	    if (count == 0)
		len = 0;
	    else
		len -= count;
	}
	free(buffer);
	fclose(epsfile);
	if (*outname!='\0')
	    fclose(outfile);
}


/* These routines deal with a PBM bitmap under Unix */
/* and a BMP under OS/2 or MS-DOS */

void shift_preview(unsigned char *preview, int bwidth, int offset);
BOOL iswhitespace(char c);
void reverse_triples(unsigned char *line2, int n);


char isblack[256];	/* each byte is non-zero if that colour is black */

BOOL
iswhitespace(char c)
{
	return (c==' ' || c=='\t' || c=='\r' || c=='\n');
}

/* reverse RGB triples to BGR triples, or vice versa */
void
reverse_triples(unsigned char *line2, int n)
{
unsigned char temp;
int i;
    for (i=0; i<n; i++) {
        temp = line2[0];
        line2[0] = line2[2];
	line2[2] = temp;
	line2 += 3;
    }
}

/* this doesn't do a good job of scanning pbm format */
/* instead it makes assumptions about the way Ghostscript writes pbm files */
int
scan_pbmplus(PREBMAP *ppbmap, unsigned char *pbitmap)
{
unsigned char *p;
int i;
    if (pbitmap[0] == 'P' && (pbitmap[1] == '4' || pbitmap[1] == '5' || pbitmap[1] == '6')) {
	/* pbmraw */
	p = pbitmap+3;
	while (*p!='\n')
	    p++;	/* skip comment line */
	p++;
	ppbmap->width = atoi((const char *)p);
	while (isdigit((int)(*p)))
	    p++;
	while (iswhitespace(*p))
	    p++;
	ppbmap->height = atoi((const char *)p);
	while (isdigit((int)(*p)))
	    p++;
	if (pbitmap[1] == '4') {	/* pbmraw */
	    ppbmap->depth = 1;
	    isblack[0] = 0;
	    isblack[1] = 1;
	}
	else if (pbitmap[1] == '5') {	/* pgmraw */
	    ppbmap->depth = 8;
	    while (iswhitespace(*p))
	        p++;
	    /* ignore max value */
	    while (isdigit((int)(*p)))
	        p++;
	    for (i=0; i < 256; i++)
		isblack[i] = 1;
	    isblack[255] = 0;
	}
	else {				/* ppmraw */
	    ppbmap->depth = 24;
	    while (iswhitespace(*p))
	        p++;
	    /* ignore max value */
	    while (isdigit((int)(*p)))
	        p++;
	}
	ppbmap->bits = ((BYTE *)p) +1;
        ppbmap->bytewidth = (( ppbmap->width * ppbmap->depth + 7) & ~7) >> 3;
	ppbmap->topleft = TRUE;
    }
    else {
	gserror(0, "Unknown bitmap format", MB_ICONEXCLAMATION, SOUND_ERROR);
	return 1;
    }
    return 0;
}

void scan_colors(PREBMAP *ppbmap, unsigned char *pbitmap);

int
scan_dib(PREBMAP *ppbmap, unsigned char *pbitmap)
{
    int size =  get_dword(pbitmap);
    if (size == BITMAP1_LENGTH) {
	ppbmap->width = (short)get_word(pbitmap+BITMAP1_WIDTH);
	ppbmap->height = (short)get_dword(pbitmap+BITMAP1_HEIGHT);
	ppbmap->depth = get_word(pbitmap+BITMAP1_BITCOUNT);
	ppbmap->bits =  (((BYTE *)pbitmap) + size)
			+ dib_pal_colors(pbitmap) * RGB3_LENGTH; 
	ppbmap->os2 = TRUE;
    }
    else {
	ppbmap->width = (LONG)get_dword(pbitmap+BITMAP2_WIDTH);
	ppbmap->height = (LONG)get_dword(pbitmap+BITMAP2_HEIGHT);
	ppbmap->depth = get_word(pbitmap+BITMAP2_BITCOUNT);
	ppbmap->bits =  (((BYTE *)pbitmap) + size)
			+ dib_pal_colors(pbitmap) * RGB4_LENGTH; 
	ppbmap->os2 = FALSE;
    }

    ppbmap->bytewidth = (( ppbmap->width * ppbmap->depth + 31) & ~31) >> 3;
    ppbmap->topleft = FALSE;

    scan_colors(ppbmap, pbitmap);
    return 0;
}

/* return number of bytes per line, rounded up to multiple of 4 bytes */
unsigned long
dib_bytewidth(unsigned char *pbitmap)
{
unsigned int width, bitcount;
    if (get_dword(pbitmap) == BITMAP1_LENGTH) {
	width = get_word(pbitmap + BITMAP1_WIDTH);
	bitcount = get_word(pbitmap + BITMAP1_BITCOUNT);
    }
    else {
	width = get_dword(pbitmap + BITMAP2_WIDTH);
	bitcount = get_word(pbitmap + BITMAP2_BITCOUNT);
    }
    return (((width * bitcount + 31) & ~31) >> 3);
}

/* return number of colors in color table */
unsigned int
dib_pal_colors(unsigned char *pbitmap)
{
unsigned int bitcount, planes, clrused;
    if (get_dword(pbitmap) == BITMAP1_LENGTH) {
	planes = get_word(pbitmap + BITMAP1_PLANES);
	bitcount = get_word(pbitmap + BITMAP1_BITCOUNT);
	if (bitcount != 24)
	    return 1<<(bitcount * planes);
    }
    else {
	planes = get_word(pbitmap + BITMAP2_PLANES);
	bitcount = get_word(pbitmap + BITMAP2_BITCOUNT);
	clrused = get_word(pbitmap + BITMAP2_CLRUSED);
	if (bitcount != 24)
	    return clrused ? clrused : 1<<(bitcount * planes);
    }
    if (bitcount != 24)
	    return 1<<(bitcount * planes);
    return 0;
}


#ifdef __BORLANDC__
#pragma argsused
#endif
void
scan_colors(PREBMAP *ppbmap, unsigned char *pbitmap)
{
	unsigned char *prgb;
	unsigned char rr;
	unsigned char gg;
	unsigned char bb;
	int clrtablesize;
	int i;
	unsigned int size = get_dword(pbitmap);

	prgb   = pbitmap + size;
	/* read in the color table */
	clrtablesize = dib_pal_colors(pbitmap);
	for (i = 0; i < clrtablesize; i++) {
		if (ppbmap->os2) {
		    bb = prgb[i*RGB3_LENGTH+RGB3_BLUE];
		    gg = prgb[i*RGB3_LENGTH+RGB3_GREEN];
		    rr = prgb[i*RGB3_LENGTH+RGB3_RED];
		}
		else {
		    bb = prgb[i*RGB4_LENGTH+RGB4_BLUE];
		    gg = prgb[i*RGB4_LENGTH+RGB4_GREEN];
		    rr = prgb[i*RGB4_LENGTH+RGB4_RED];
		}
		isblack[i] = (unsigned char)((rr < 0xff) || (gg < 0xff) || (bb < 0xff));
	}
}

/* return pointer to bitmap bits */
BYTE *
get_dib_bits(unsigned char *pbitmap)
{
BYTE *lpDibBits;
	unsigned int size = get_dword(pbitmap);
	lpDibBits = (((BYTE *)pbitmap) + size);
	if (size == BITMAP1_LENGTH)
	    lpDibBits += dib_pal_colors(pbitmap) * RGB3_LENGTH; 
	else
	    lpDibBits += dib_pal_colors(pbitmap) * RGB4_LENGTH; 
	return lpDibBits;
}

/* get line from DIB and store as 1 bit/pixel in preview */
/* also works for PBM (pnmraw) bitmap */
/* preview has 0=black, 1=white */
void
get_dib_line(BYTE *line, unsigned char *preview, int width, int bitcount)
{
int bwidth = ((width + 7) & ~7) >> 3; /* byte width with 1 bit/pixel */
unsigned char omask;
int oroll;
unsigned char c = 0;
int j;
	memset(preview,0xff,bwidth);
	omask = 0x80;
	oroll = 7;
	if (bitcount == 1) {
	    if (isblack[0])
		for (j = 0; j < bwidth ; j++)
			preview[j] = line[j];
	    else
		for (j = 0; j < bwidth ; j++)
			preview[j] = (unsigned char)~line[j];
	    preview[bwidth-1] |= (unsigned char)(width & 7 ? (1<<(8-(width&7)))-1 : 0);	/* mask for edge of bitmap */
	}
	else {
	    for (j = 0; j < width; j++) {
		switch (bitcount) {
			case 4:
				c = line[j>>1];
				if (!(j&1))
					c >>= 4;
				c = isblack[ c & 0x0f ];
				break;
			case 8:
				c = isblack[ (int)(line[j]) ];
				break;
			case 24:
				c = (unsigned char)(
				    (line[j*3] < 0xff) || 
				    (line[j*3+1] < 0xff) || 
				    (line[j*3+2] < 0xff) );
				break;
		}
		if (c) 
		    preview[j/8] &= (unsigned char)(~omask);
		else
		    preview[j/8] |= omask;
		oroll--;
		omask >>= 1;
		if (oroll < 0) {
		    omask = 0x80;
		    oroll = 7;
		}
	    }
	}
}


/* make a BMP header and palette from a PBMPLUS or BMP bitmap */
/* WARNING: The newpbm structure might not be packed, so it
 * should only be used by write_bitmap_info(), and not written
 * out directly. newpbm->biSize=40 if packed, larger otherwise. */
void
make_bmp_info(LPBITMAP2 newpbm, PREBMAP *ppbmap, unsigned char *pbitmap)
{
unsigned char *prgb;
int clrtablesize;
int i;
	if (*pbitmap == 'P') {
	    newpbm->biSize = sizeof(BITMAP2); /* WARNING - MAY NOT BE PACKED */
	    newpbm->biWidth = ppbmap->width;
	    newpbm->biHeight = ppbmap->height;
	    newpbm->biPlanes = 1;
	    newpbm->biBitCount = (WORD)ppbmap->depth;
	    newpbm->biCompression = 0;
	    newpbm->biSizeImage = 0;
	    newpbm->biXPelsPerMeter = (long)(1000 * option.xdpi / 25.4);
	    newpbm->biYPelsPerMeter = (long)(1000 * option.ydpi / 25.4);
	    newpbm->biClrUsed = 0;
	    newpbm->biClrImportant = 0;
	    /* write palette */
	    prgb = ((BYTE *)newpbm) + sizeof(BITMAP2);
	    switch (ppbmap->depth) {
		case 24:
		    break;	/* no palette */
		case 8:
	    	    for (i = 0; i < 256; i++) {
			*prgb++ = (unsigned char)i;	/* blue */
			*prgb++ = (unsigned char)i;	/* green */
			*prgb++ = (unsigned char)i;	/* red */
			*prgb++ = 0;
		    }
		    break;
		default:
		    *prgb++ = 255;	/* blue */
		    *prgb++ = 255;	/* green */
		    *prgb++ = 255;	/* red */
		    *prgb++ = 0;
		    *prgb++ = 0;	/* blue */
		    *prgb++ = 0;	/* green */
		    *prgb++ = 0;	/* red */
		    *prgb   = 0;
	    }
	}
	else {
	    /* already a BMP, so easy */
	    unsigned int size = get_dword(pbitmap);
	    unsigned char *poldrgb = pbitmap + size;

	    if (size == BITMAP1_LENGTH) {
		newpbm->biSize = sizeof(BITMAP2);
		newpbm->biWidth = ppbmap->width;
		newpbm->biHeight = ppbmap->height;
		newpbm->biPlanes = 1;
		newpbm->biBitCount = (WORD)ppbmap->depth;
		newpbm->biCompression = 0;
		newpbm->biSizeImage = 0;
		newpbm->biXPelsPerMeter = (long)(1000 * option.xdpi / 25.4);
		newpbm->biYPelsPerMeter = (long)(1000 * option.ydpi / 25.4);
		newpbm->biClrUsed = 0;
		newpbm->biClrImportant = 0;
	    }
	    else {
		/* BITMAP2 - just copy it */
	        memmove((char *)newpbm, pbitmap, size);
	    }

	    /* copy palette */
	    prgb = ((unsigned char *)newpbm) + newpbm->biSize;
	    /* read in the color table */
	    if (ppbmap->depth == 24)
		clrtablesize = 0;
	    else
		clrtablesize = 1 << ppbmap->depth;
	    for (i = 0; i < clrtablesize; i++) {
		if (ppbmap->os2) {
		    *prgb++ = poldrgb[i*RGB3_LENGTH + RGB3_BLUE];
		    *prgb++ = poldrgb[i*RGB3_LENGTH + RGB3_GREEN];
		    *prgb++ = poldrgb[i*RGB3_LENGTH + RGB3_RED];
		    *prgb++ = 0;
		}
		else {
		    *prgb++ = poldrgb[i*RGB4_LENGTH + RGB4_BLUE];
		    *prgb++ = poldrgb[i*RGB4_LENGTH + RGB4_GREEN];
		    *prgb++ = poldrgb[i*RGB4_LENGTH + RGB4_RED];
		    *prgb++ = 0;
		}
	    }
	}
}



#define TIFF_BYTE 1
#define TIFF_ASCII 2
#define TIFF_SHORT 3
#define TIFF_LONG 4
#define TIFF_RATIONAL 5

struct rational_s {
	DWORD numerator;
	DWORD denominator;
};
#define TIFF_RATIONAL_SIZE 8

struct ifd_entry_s {
	WORD tag;
	WORD type;
	DWORD length;
	DWORD value;
};
#define TIFF_IFD_SIZE 12

struct tiff_head_s {
	WORD order;
	WORD version;
	DWORD ifd_offset;
};
#define TIFF_HEAD_SIZE 8

DWORD
get_dword(unsigned char *buf)
{
	DWORD dw;
    dw = (DWORD)buf[0];
    dw += ((DWORD)buf[1])<<8;
    dw += ((DWORD)buf[2])<<16;
    dw += ((DWORD)buf[3])<<24;
    return dw;
}

WORD
get_word(unsigned char *buf)
{
    WORD w;
    w = (WORD)buf[0];
    w |= (WORD)(buf[1]<<8);
    return w;
}


/* write DWORD as DWORD */
void
write_dword(DWORD val, FILE *f)
{
    fputc((unsigned char)( val      & 0xff), f);
    fputc((unsigned char)((val>>8)  & 0xff), f);
    fputc((unsigned char)((val>>16) & 0xff), f);
    fputc((unsigned char)((val>>24) & 0xff), f);
}

/* write WORD as DWORD */
void
write_word_as_dword(WORD val, FILE *f)
{
    fputc((unsigned char)( val      & 0xff), f);
    fputc((unsigned char)((val>>8)  & 0xff), f);
    fputc('\0', f);
    fputc('\0', f);
}

/* write WORD as WORD */
void
write_word(WORD val, FILE *f)
{
    fputc((unsigned char)( val      & 0xff), f);
    fputc((unsigned char)((val>>8)  & 0xff), f);
}

int
packbits(BYTE *comp, BYTE *raw, int length)
{
BYTE *cp;
int literal = 0;	/* number of literal bytes */
int prevlit = 0;	/* length of previous literal */
int repeat = 0;		/* number of repeat bytes - 1*/

⌨️ 快捷键说明

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