gif2epsn.c

来自「在EM85XX」· C语言 代码 · 共 594 行 · 第 1/2 页

C
594
字号
    };    static char Dither3[3][3] = {	{ 7, 9, 5 },	{ 2, 1, 4 },	{ 6, 3, 8 }    };    static char Dither4[4][4] = {	{ 1,  9,  3,  11 },	{ 13, 5,  15, 7 },	{ 4,  12, 2,  10 },	{ 16, 8,  14, 6 }    };    int i, j, k, Level;    long Intensity;    GifColorType *ColorMapEntry;    /* Scan the Rows (Size rows) evaluate intensity every Size pixel and use */    /* the dither matrix to set the dithered result;			     */    for (i = 0; i <= RowSize - DitherSize; i += DitherSize) {	Intensity = 0;	for (j = Row; j < Row + DitherSize; j++)	    for (k = 0; k < DitherSize; k++) {		ColorMapEntry = &ColorMap[ScreenBuffer[j][i+k]];		Intensity += 30 * ((int) ColorMapEntry->Red) +			     59 * ((int) ColorMapEntry->Green) +			     11 * ((int) ColorMapEntry->Blue);	    }	/* Find the intensity level (between 0 and Size^2) of our matrix:    */	/* Expression is "Intensity * BWThreshold / (25500 * DefThresh)"     */	/* but to prevent from overflow in the long evaluation we do this:   */	Level = ((Intensity / 2550) * ((long) DEFAULT_THRESHOLD) /						(((long) BWThreshold) * 10));	switch (DitherSize) {	    case 2:     		for (j = 0; j < DitherSize; j++)		    for (k = 0; k < DitherSize; k++)			DitherBuffer[j][i+k] = Dither2[j][k] <= Level;		break;	    case 3:		for (j = 0; j < DitherSize; j++)		    for (k = 0; k < DitherSize; k++)			DitherBuffer[j][i+k] = Dither3[j][k] <= Level;		break;	    case 4:		for (j = 0; j < DitherSize; j++)		    for (k = 0; k < DitherSize; k++)			DitherBuffer[j][i+k] = Dither4[j][k] <= Level;		break;	}    }}/******************************************************************************* The real dumping routine. Few things are taken into account:		      ** 1. The Nice flag. If TRUE each pixel is printed twice in double density.    ** 2. The Invert flag. If TRUE each pixel before drawn is inverted.	      ** 3. The rendering mode and dither matrix flag if dithering is selected.      **   The image is drawn from ScreenBuffer ScreenTop/Left in the bottom/right   ** directions.							     	      **   Unfortunatelly, there is a BUG in DOS that does not handle ctrl-Z	      ** correctly if we open lptx: device in binary mode (should treat it as any    ** other char). Therefore I had to write to it directly using biosprint. I     ** dont like it either, and if you have better way to do it, let me know.      *******************************************************************************/static void DumpScreen2Epsn(GifRowType *ScreenBuffer,					int ScreenWidth, int ScreenHeight){    int i, j, p, Size, LeftCWidth, Len, DirectPrint = 0,	DitheredLinesLeft = 0, DitheredLinesCount = 0, MapInvert[2];    char LinePrefixLen[2];			     /* Length of scan line. */    GifByteType *EpsonBuffer;    GifPixelType *Line;    GifRowType *DitherBuffer;    GifColorType *ColorMapEntry;    FILE *Prt = NULL;#ifdef __MSDOS__    for (i = 0;	 i < strlen(PrinterName); i++)	if (islower(PrinterName[i]))	    PrinterName[i] = toupper(PrinterName[i]);    if (strcmp(PrinterName, "LPT1") == 0 ||	strcmp(PrinterName, "PRN") == 0)	DirectPrint = 1;    else if (strcmp(PrinterName, "LPT2") == 0)        DirectPrint = 2;    else if (strcmp(PrinterName, "LPT3") == 0)        DirectPrint = 3;#endif /* __MSDOS__ */    if (!DirectPrint) {#ifdef __MSDOS__	if (strlen(PrinterName) == 0) {	    setmode(1, O_BINARY);	  /* Make sure it is in binary mode. */	    Prt = stdout;	}	else if ((Prt = fopen(PrinterName, "wb")) == NULL ||		 setvbuf(Prt, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE))#else	if (strlen(PrinterName) == 0)	    Prt = stdout;	else if ((Prt = fopen(PrinterName, "w")) == NULL)#endif /* __MSDOS__ */	    GIF_EXIT("Failed to open output (printer) file.");    }    if ((EpsonBuffer = (GifByteType *) malloc(ScreenWidth)) == NULL)	GIF_EXIT("Failed to allocate memory required, aborted.");    /* Allocate the buffer to save the dithered information. */    if (ColorToBWMapping == C2BW_DITHER) {	if ((DitherBuffer = (GifRowType *)	    malloc(DITHER_MAX_MATRIX * sizeof(GifRowType *))) != NULL) {	    Size = ScreenWidth * sizeof(GifPixelType);	 /* Size of one row. */	    for (i = 0; i < DITHER_MAX_MATRIX; i++) {		if ((DitherBuffer[i] = (GifRowType) malloc(Size)) == NULL) {		    DitherBuffer = NULL;		    break;		}	    }	}	if (DitherBuffer == NULL)	    GIF_EXIT("Failed to allocate memory required, aborted.");    }    else	DitherBuffer = NULL;    /* Reset the printer, and make sure no space between adjacent lines: */    PutString(Prt, DirectPrint, EPSON_RESET, 2);    PutString(Prt, DirectPrint, EPSON_VERTICAL_SPACE, 3);    /* Prepar left spacing to begin with, so image will be in the middle.    */    LeftCWidth = (EPSON_WIDTH - (ScreenWidth / EPSON_PIXEL_2_CHAR)) / 2;    if (InvertFlag) {		   /* Make the inversion as fast a possible. */	MapInvert[0] = 1;	MapInvert[1] = 0;    }    else {	MapInvert[0] = 0;	MapInvert[1] = 1;    }    for (i = 0, p = 0; i < ScreenHeight; i++, p++) {	GifQprintf("\b\b\b\b%-4d", ScreenHeight - i);	Line = ScreenBuffer[i];	/* If 8 lines were accumulated in printer buffer - dump them out.    */	if (p == 8) {	    for (Len = ScreenWidth-1; Len >= 0; Len--)	        if (EpsonBuffer[Len]) break;	    /* Only in case this line is not empty: */	    if (Len++ >= 0) {		/* Make the left space, so image will be centered: */		for (j = 0; j < LeftCWidth; j++)		    PutString(Prt, DirectPrint,  " ", 1);		/* Full printer line is ready to be dumped - send it out: */		if (NiceFlag) {		    PutString(Prt, DirectPrint, EPSON_DUAL_DENSITY, 2);		    LinePrefixLen[0] = (Len * 2) % 256;		    LinePrefixLen[1] = (Len * 2) / 256;		    PutString(Prt, DirectPrint, LinePrefixLen, 2);		    PutString2(Prt, DirectPrint, (char *) EpsonBuffer, Len);		}		else {		    PutString(Prt, DirectPrint, EPSON_REG_DENSITY, 2);		    LinePrefixLen[0] = Len % 256;		    LinePrefixLen[1] = Len / 256;		    PutString(Prt, DirectPrint, LinePrefixLen, 2);		    PutString(Prt, DirectPrint, (char *) EpsonBuffer, Len);		}	    }	    PutString(Prt, DirectPrint, "\015\012", 2);	    p = 0;	}	/* We decide right here what method to map Colors to BW so the inner */	/* loop will be independent of it (and therefore faster):	     */	switch(ColorToBWMapping) {	    case C2BW_BACK_GROUND:		for (j = 0; j < ScreenWidth; j++)		    EpsonBuffer[j] = (EpsonBuffer[j] << 1) +					MapInvert[Line[j] != BackGround];		break;	    case C2BW_GREY_LEVELS:		for (j = 0; j < ScreenWidth; j++) {		    ColorMapEntry = &ColorMap[Line[j]];		    /* For the transformation from RGB to BW, see Folley &   */		    /* Van Dam pp 613: The Y channel is the BW we need:	     */		    /* As colors are 255 maximum, the result can be up to    */		    /* 25500 which is still in range of our 16 bits integers */		    EpsonBuffer[j] = (EpsonBuffer[j] << 1) +			MapInvert[(30 * (int) ColorMapEntry->Red) +				   59 * ((int) ColorMapEntry->Green) +				   11 * ((int) ColorMapEntry->Blue) >					BWThreshold];		}		break;	    case C2BW_DITHER:		if (DitheredLinesLeft-- == 0) {		    EvalDitheredScanline(ScreenBuffer,			(i < ScreenHeight - DitherSize ? i :						 ScreenHeight - DitherSize),			ScreenWidth, DitherBuffer);		    DitheredLinesLeft = DitherSize - 1;		    DitheredLinesCount = 0;		}		Line = DitherBuffer[DitheredLinesCount++];		for (j = 0; j < ScreenWidth; j++)		    EpsonBuffer[j] = (EpsonBuffer[j] << 1) +							MapInvert[Line[j]];		break;	}    }    /* If buffer in incomplete - complete it and dump it out: */    if (p != 0) {	for (Len = ScreenWidth - 1; Len >= 0; Len--)	    if (EpsonBuffer[Len]) break;	if (Len++ >= 0) {	    i = 8 - p;					 /* Amount to shift. */	    for (j = 0; j < ScreenWidth; j++) EpsonBuffer[j] <<= i;	    /* Make the left space, so image will be centered: */	    for (j = 0; j < LeftCWidth; j++)		PutString(Prt, DirectPrint, " ", 1);	    if (NiceFlag) {		PutString(Prt, DirectPrint, EPSON_DUAL_DENSITY, 2);		LinePrefixLen[0] = (Len * 2) % 256;		LinePrefixLen[1] = (Len * 2) / 256;		PutString(Prt, DirectPrint, LinePrefixLen, 2);		PutString2(Prt, DirectPrint, (char *) EpsonBuffer, Len);	    }	    else {		PutString(Prt, DirectPrint, EPSON_REG_DENSITY, 2);		LinePrefixLen[0] = Len % 256;		LinePrefixLen[1] = Len / 256;		PutString(Prt, DirectPrint, LinePrefixLen, 2);		PutString(Prt, DirectPrint, (char *) EpsonBuffer, Len);	    }	}	PutString(Prt, DirectPrint, "\015\012", 2);    }    fclose(Prt);}/******************************************************************************* Dumps the string of given length, to Prt. No char in Str has special	      ** meaning, and even zero (NULL) chars are dumped.			      ** If however DirectPrint is non zero, string is dumped to specifed lpt port.  *******************************************************************************/static void PutString(FILE *Prt, int DirectPrint, char *Str, int Len){    int i;    if (DirectPrint) {#ifdef __MSDOS__	for (i = 0; i < Len; i++) biosprint(0, Str[i], DirectPrint - 1);#else	GIF_EXIT("Can not print directly to a printer if not MSDOS.");#endif /* __MSDOS__ */    }    else	for (i = 0; i < Len; i++) fputc(Str[i], Prt);}/******************************************************************************* Dumps the string of given length, to Prt. No char in Str has special	      ** meaning, and even zero (NULL) chars are dumped. Every char is dumped twice. ** If however DirectPrint is non zero, string is dumped to specifed lpt port.  *******************************************************************************/static void PutString2(FILE *Prt, int DirectPrint, char *Str, int Len){    int i;    if (DirectPrint) {#ifdef __MSDOS__	for (i = 0; i < Len; i++) {	    biosprint(0, Str[i], DirectPrint - 1);	    biosprint(0, Str[i], DirectPrint - 1);	}#else	GIF_EXIT("Can not print directly to a printer if not MSDOS.");#endif /* __MSDOS__ */    }    else	for (i = 0; i < Len; i++) {	    fputc(Str[i], Prt);	    fputc(Str[i], Prt);	}}

⌨️ 快捷键说明

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