📄 xgrabsc.c
字号:
/* use depth as the number of bits in output samples */ depth = ximage->depth; /* postscript only supports 1, 2, 4, or 8 */ if (depth > 8) depth = 8; /* max postscript bits/sample */ if (depth < 8 && depth > 4) depth = 8; if (depth == 3) depth = 4; bpl = ((ximage->width * depth) + 7) / 8; if (depth == 1) psimage = ximage; else { /* colors have to be changed to luminescence */ ptr = (byte *)malloc(ximage->height * bpl); psimage = XCreateImage(hDisplay, DefaultVisual(hDisplay, hScreen), depth, ZPixmap, 0, ptr, ximage->width, ximage->height, 0, bpl); if (!psimage) { fprintf(stderr, "%s: could not create image for Postscript conversion\n", programName); exit(3); } /* force the bits_per_pixel to be what is needed */ psimage->bits_per_pixel = depth; } spb = 8 / psimage->bits_per_pixel; /* samples per byte */ if (depth > 1) { /* translate colors into grays */ lshift = 16 - psimage->bits_per_pixel; lmask = (1 << psimage->bits_per_pixel) - 1; for (y = 0; y < ximage->height; y++) { for (x = 0; x < ximage->width; x++) { p = XGetPixel(ximage, x, y); i = (0.30*(double)image->red[p]) + (0.59*(double)image->green[p])+ (0.11*(double)image->blue[p]); i = (i >> lshift) & lmask; XPutPixel(psimage, x, y, i); } } }#ifndef NO_RLE_CHECKS if (encode) { rletotal = 0; rlecount = 0; firstSample = TRUE; for (y=0; y<psimage->height; y++) for (x=0, ptr=(byte *)(psimage->data + (y * psimage->bytes_per_line)); x<psimage->width; x+=spb, ptr++) { b = *ptr; if (firstSample || b != rlesample || rlecount==254) { if (!firstSample) rletotal += 2; else firstSample = FALSE; rlecount = 0; rlesample = b; } else rlecount++; } if (!firstSample) rletotal += 2; f = (float)(rletotal) / (float)(psimage->height*bpl); if (verbose) fprintf(stderr, "%s: encoding would change to %5.1f%% of orig size\n", programName, f * 100.0); encode = f <= 0.95; }#endif if (verbose) fprintf(stderr, "%s: image will %sbe encoded\n", programName, encode? "" : "not "); if (encapsulate) { fprintf(outfile, "%%!PS-Adobe-2.0 EPSF-1.2\n"); fprintf(outfile, "%%%%BoundingBox: %d %d %d %d\n", 0, 0, psimage->width, psimage->height); } else fprintf(outfile, "%%!\n"); fprintf(outfile, "%%%%Creator: xgrabsc\n"); fprintf(outfile, "%%%%Title: %s\n", imageName); time(&p); fprintf(outfile, "%%%%CreationDate: %s", ctime(&p)); fprintf(outfile, "%%%%EndComments\n"); fprintf(outfile, "%%\n"); fprintf(outfile, "%%\n"); /* standard inch procedure */ fputs("/inch {72 mul} def\n", outfile); /* define a string to hold image bytes */ if (encode) fputs("/rlebuffer 2 string def\n", outfile); else fprintf(outfile, "/picstr %d string def\n", bpl); /* define the image plotting procedure */ fputs("/plotimage\n", outfile); /* parameters for the standard image procedure */ fprintf(outfile, " {%d %d %d [%d 0 0 -%d 0 %d]\n", psimage->width, psimage->height, psimage->bits_per_pixel, psimage->width, psimage->height, psimage->height); /* line reading function */ if (encode) { fputs("% run-length decoding block\n", outfile); fputs(" { currentfile rlebuffer readhexstring pop pop\n", outfile); fputs(" rlebuffer 0 get 1 add %% number of copies of the sample\n", outfile); fputs(" /nsamples exch store %% save it away\n", outfile); fputs(" /lum rlebuffer 1 get store %% the sample itself\n", outfile); fputs(" /samples nsamples string store\n", outfile); fputs(" 0 1 nsamples -1 add { samples exch lum put } for\n", outfile); fputs(" samples %% leave the \n", outfile); fputs(" }\n", outfile); } else fputs(" {currentfile picstr readhexstring pop}\n", outfile); fputs(" image\n} def\n", outfile); /* save context and move to a nice origin */ fputs("gsave\n", outfile); if (encapsulate) { /* for encapsulated postscript, we need a scale factor that is equal * to the image width/height in samples */ fprintf(outfile, "%d %d scale\n", psimage->width, psimage->height); } else { /* For physical output we need a scale factor that will create * the same size image, and we need to center it on the page. * -Figure out the physical dimensions on the screen * and make it come out the same on the printer. * -Use inch units and assume the paper is 8.5x11.0. * (future change: allow selection of paper size and margins) */ xdpi = (((double)DisplayWidth(hDisplay,hScreen)) * 25.4) / ((double)DisplayWidthMM(hDisplay,hScreen)); ydpi = (((double)DisplayHeight(hDisplay,hScreen)) * 25.4) / ((double)DisplayHeightMM(hDisplay,hScreen)); xscale = ((double)psimage->width) / xdpi; yscale = ((double)psimage->height) / ydpi; if (xscale > 7.5) { yscale *= 7.5 / xscale; xscale = 7.5; } else if (yscale > 10.0) { xscale *= 10.0 / yscale; yscale = 10.0; } fprintf(outfile, "%1.2g inch %1.2g inch translate\n", (8.5 - xscale) / 2.0, (11.0 - yscale) / 2.0); fprintf(outfile, "%1.2g inch %1.2g inch scale\n", xscale, yscale); } fputs("plotimage\n", outfile); reverse = depth == 1? BlackPixel(hDisplay,hScreen)==1 : FALSE; if (encode) { rletotal = 0; rlecount = 0; firstSample = TRUE; } widthcount = 0; for (y=0; y<psimage->height; y++) { for (x=0, ptr=(byte *)(psimage->data+(y * psimage->bytes_per_line)); x<psimage->width; x+=spb, ptr++) { b = *ptr; if (reverse) b = ~b; if (depth == 1 && psimage->bitmap_bit_order == LSBFirst) b = swapbits(b); if (encode) { if (firstSample || b != rlesample || rlecount==254) { if (!firstSample) { fprintf(outfile, "%02.2x%02.2x", rlecount, rlesample); rletotal += 2; widthcount += 4; if (widthcount >= 60) { fputc('\n', outfile); widthcount = 0; } } else firstSample = FALSE; rlecount = 0; rlesample = b; } else rlecount++; } else { fprintf(outfile, "%02.2x", b); widthcount += 2; if (widthcount >= 60) { fputc('\n', outfile); widthcount = 0; } } } } if (encode) { if (!firstSample) { fprintf(outfile, "%02.2x%02.2x\n", rlecount, rlesample); rletotal += 2; } fputs("%\n", outfile); fprintf(outfile, "%% Run-length encoding savings = %5.1f%%\n", 100.0 - ((float)(rletotal) * 100.0 / (float)(psimage->height * bpl))); fputs("%\n", outfile); } fputs("\n\n\ngrestore\nshowpage\n", outfile); if (psimage != ximage) { free(psimage->data); free(psimage); }}/* * Write an image in Color Postscript format * Run-length encoding is not yet implemented but the request is accepted. */writeColorPS(image, outfile, encode, encapsulate) imageInfo *image; FILE *outfile; int encode; /* TRUE if we're to encode the Postscript output */ int encapsulate; /* TRUE if encapsulated Postscript output is wanted */{ register byte *ptr, b; register int x, y; XImage *ximage = image->ximage; double xdpi, ydpi, xscale, yscale, f; double left, right, top, bottom; int depth, bpl, spb; long p; /* rle variables */ int rlecount; dw rletotal; byte rlesample; int firstSample; int widthcount; if (verbose) fprintf(stderr, "%s: formatting Color Postscript output\n", programName); depth = 8; /* bits per sample */ spb = 1; /* samples per byte */ bpl = ((ximage->width * depth) + 7) / 8; /* bytes per line */#ifndef NO_RLE_CHECKS if (encode) { rletotal = 0; rlecount = 0; firstSample = TRUE; for (y=0; y<ximage->height; y++) for (x=0, ptr=(byte *)(ximage->data + (y * ximage->bytes_per_line)); x<ximage->width; x+=spb, ptr++) { b = *ptr; if (firstSample || b != rlesample || rlecount==254) { if (!firstSample) rletotal += 2; else firstSample = FALSE; rlecount = 0; rlesample = b; } else rlecount++; } rletotal += 2; f = (float)(rletotal) / (float)(ximage->height*bpl); if (verbose) fprintf(stderr, "%s: encoding would change to %5.1f%% of orig size\n", programName, f * 100.0); encode = f <= 0.95; }#endif if (encapsulate) { fprintf(outfile, "%%!PS-Adobe-2.0 EPSF-2.0\n"); } else fprintf(outfile, "%%!\n"); fprintf(outfile, "%%%%Creator: xgrabsc\n"); fprintf(outfile, "%%%%Title: %s\n", imageName); if (encapsulate) { fprintf(outfile, "%%%%Pages: 1\n"); fprintf(outfile, "%%%%BoundingBox: %d %d %d %d\n", 0, 0, ximage->width, ximage->height); } time(&p); fprintf(outfile, "%%%%CreationDate: %s", ctime(&p)); fprintf(outfile, "%%%%EndComments\n"); if (encapsulate) { fprintf(outfile, "%%%%EndProlog\n"); fprintf(outfile, "%%%%Page: 1 1\n"); } fprintf(outfile, "\n\ngsave\n\n");#ifndef NO_COLORIMAGE_PROC if (!encapsulate) { /* put a colorImage procedure into the output file so a monochrome * printer can handle the file as well */ for (x=0; ColorImage[x] != NULL; x++) { fputs(ColorImage[x], outfile); fputc('\n', outfile); } fprintf(outfile, "\n\n\n"); }#endif fputs("/inch {72 mul} def\n", outfile); fprintf(outfile, "/rgbstr %d string def\n", (int)ximage->width * 3); fputs("/buffer 2 string def\n", outfile); fputs("/rgb (000) def\n", outfile); fprintf(outfile, "/rgbmap %d string def\n", image->numcells * 3); fputs("\n\n", outfile); if (encapsulate) { /* don't translate the image for encapsulated postscript. The * scale should match the data dimensions of the image in samples. */ fprintf(outfile, "%d %d scale\n", ximage->width, ximage->height); } else { /* For physical output we need a scale factor that will create * the same size image, and we need to center it on the page. * -Figure out the physical dimensions on the screen * and make it come out the same on the printer. * -Use inch units and assume the paper is 8.5x11.0. * (future change: allow selection of paper size and margins) */ xdpi = (((double)DisplayWidth(hDisplay,hScreen)) * 25.4) / ((double)DisplayWidthMM(hDisplay,hScreen)); ydpi = (((double)DisplayHeight(hDisplay,hScreen)) * 25.4) / ((double)DisplayHeightMM(hDisplay,hScreen)); xscale = ((double)ximage->width) / xdpi; yscale = ((double)ximage->height) / ydpi; if (xscale > 7.5) { yscale *= 7.5 / xscale; xscale = 7.5; } else if (yscale > 10.0) { xscale *= 10.0 / yscale; yscale = 10.0; } left = ((8.5 - xscale) / 2.0); top = ((11.0 - yscale) / 2.0); right = (left + xscale); bottom = (top + yscale); fprintf(outfile, "%1.2g inch %1.2g inch translate\n", left, top); fprintf(outfile, "%1.2g inch %1.2g inch scale\n", xscale, yscale); fprintf(outfile, "\n\n\n"); } if (encode) { /* define a drawcolorimage procedure geared to this image */ fputs("/drawcolorimage {\n", outfile); fprintf(outfile, " %d %d %d\n", ximage->width, ximage->height,depth); fprintf(outfile, " [%d 0 0 -%d 0 %d]\n", ximage->width, ximage->height, ximage->height); fputs(" %% define a block of code to read and decode the rle input stream\n", outfile); fputs(" { currentfile buffer readhexstring pop pop %% run length and index\n", outfile); fputs(" /npixels buffer 0 get 1 add 3 mul store %% number of bytes\n", outfile); fputs(" /color buffer 1 get 3 mul store %% fix index into rgb map\n", outfile); fputs(" /rgb rgbmap color 3 getinterval store %% and get the colors\n", outfile); fputs(" /pixels npixels string store %% make a string to hold the rgb\n", outfile); fputs(" 0 3 npixels -1 add { %% loop to store the rgb bytes\n", outfile); fputs(" pixels exch rgb putinterval\n", outfile); fputs(" } for\n", outfile); fputs(" pixels\n", outfile); fputs(" }\n", outfile); fputs(" false 3 colorimage\n", outfile); fputs("} bind def\n", outfile); fprintf(outfile, "\n\n\n"); fputs("%% get the rgb map\n", outfile); fputs("currentfile rgbmap readhexstring pop pop\n", outfile); for (x=0; x<image->numcells; x++) fprintf(outfile, "%02.2x%02.2x%02.2x\n", (byte)((image->red[x] >> 8) & 0xff), (byte)((image->green[x] >> 8) & 0xff), (byte)((image->blue[x] >> 8) & 0xff) ); fputs("\n\n", outfile); fputs("drawcolorimage\n", outfile); rletotal = 0; rlecount = 0; firstSample = TRUE; widthcount = 0; for (y=0; y<ximage->height; y++) { for (x=0, ptr=(byte *)(ximage->data+(y * ximage->bytes_per_line)); x<ximage->width; x+=spb, ptr++) { b = *ptr; if (depth == 1 && ximage->bitmap_bit_order == LSBFirst) b = swapbits(b); if (firstSample || b != rlesample || rlecount==254) { if (!firstSample) { fprintf(outfile, "%02.2x%02.2x", rlecount, rlesample); rletotal += 2; widthcount += 4; if (widthcount >= 60) { fputc('\n', outfile); widthcount = 0; } } else firstSample = FALSE; rlecount = 0; rlesample = b; } else rlecount++; } } if (!firstSample) { fprintf(outfile, "%02.2x%02.2x\n", rlecount, rlesample); rletotal += 2; } fputs("%\n", outfile); fprintf(outfile, "%% Run-length encoding savings = %5.1f%%\n", 100.0 - ((float)(rletotal) * 100.0 / (float)(ximage->height * bpl))); fputs("%\n", outfile); } else { /* parameters for the standard image procedure */ fprintf(outfile, "%d %d %d [%d 0 0 -%d 0 %d]\n", ximage->width, ximage->height, depth, ximage->width, ximage->height, ximage->height); fputs("{ currentfile rgbstr readhexstring pop }\n", outfile); fputs("false 3\n", outfile); fputs("colorimage\n", outfile); for (y = 0, widthcount = 0; y < ximage->height; y++) { for (x = 0; x < ximage->width; x++) { p = XGetPixel(ximage, x, y); fprintf(outfile, "%02.2x%02.2x%02.2x", (byte)((image->red[p] >> 8) & 0xFF), (byte)((image->green[p] >> 8) & 0xFF), (byte)((image->blue[p] >> 8) & 0xFF) ); widthcount += 6; if (widthcount >= 60) { fputs("\n", outfile); widthcount = 0; } } } } fputs("\ngrestore\nshowpage\n%%Trailer\n", outfile);}/* * Write an image in 'puzzle' format, suitable for loading with * "puzzle -picture". */writePuzzle(image, outfile) imageInfo *image; FILE *outfile;{ XImage *ximage = image->ximage; int nc, width, height, w, h, cidx; dw swaptest = 1; if (verbose) fprintf(stderr, "%s: formatting Puzzle output\n", programName); if (ximage->depth > 8) { fprintf(stderr, "%s: Puzzle converter can't handle depth > 8 yet\n", programName); return; } nc = image->numcells; width = ximage->width; height = ximage->height; if (*(char *)&swaptest) { swapbytes(&width); swapbytes(&height); } fwrite(&width, 4, 1, outfile); fwrite(&height, 4, 1, outfile); fputc(nc, outfile); for (cidx=0; cidx<nc; cidx++) { fputc(image->red[cidx]>>8, outfile); fputc(image->green[cidx]>>8, outfile); fputc(image->blue[cidx]>>8, outfile); } for (h=0; h<ximage->height; h++) if (ximage->bits_per_pixel == 8) fwrite(ximage->data+(h*ximage->bytes_per_line),ximage->width,1,outfile); else /* this won't work if depth > 8 */ for (w=0; w<ximage->width; w++) fputc(XGetPixel(ximage, w, h), outfile);}writeXWD(image, outfile) imageInfo *image;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -