📄 xgrabsc.c
字号:
FILE *outfile;{ XImage *ximage = image->ximage; XWDFileHeader header; Visual *visual = DefaultVisual(hDisplay, hScreen); XColor color; dw visMask = (visual->red_mask | visual->green_mask | visual->blue_mask); dw swaptest = 1; int i; if (verbose) fprintf(stderr, "%s: formatting xwd output\n", programName); header.header_size = (CARD32)(sizeof(header)+strlen(imageName)+1); header.file_version = (CARD32) XWD_FILE_VERSION; header.pixmap_format = (CARD32)(ximage->depth>1? ZPixmap : XYPixmap); header.pixmap_depth = (CARD32) ximage->depth; header.pixmap_width = (CARD32) ximage->width; header.pixmap_height = (CARD32) ximage->height; header.xoffset = (CARD32) ximage->xoffset; header.byte_order = (CARD32) ximage->byte_order; header.bitmap_unit = (CARD32) ximage->bitmap_unit; header.bitmap_bit_order = (CARD32) ximage->bitmap_bit_order; header.bitmap_pad = (CARD32) ximage->bitmap_pad; header.bits_per_pixel = (CARD32) ximage->bits_per_pixel; header.bytes_per_line = (CARD32) ximage->bytes_per_line; header.visual_class = (CARD32)visual->class; header.red_mask = (CARD32)visual->red_mask; header.green_mask = (CARD32)visual->green_mask; header.blue_mask = (CARD32)visual->blue_mask; header.bits_per_rgb = (CARD32)visual->bits_per_rgb; header.colormap_entries = (CARD32)visual->map_entries; header.ncolors = image->numcells; header.window_width = (CARD32)ximage->width; header.window_height = (CARD32)ximage->height; header.window_x = 0; header.window_y = 0; header.window_bdrwidth = 0; if (*(char *) &swaptest) swapdws(&header, sizeof(header)); fwrite(&header, sizeof(header), 1, outfile); fwrite(imageName, 1, strlen(imageName)+1, outfile); for (i=0; i<image->numcells; i++) { color.pixel = i; color.red = image->red[i]; color.green = image->green[i]; color.blue = image->blue[i]; color.flags = visMask; color.pad = 0; if (*(char *) &swaptest) { swapdws(&color.pixel, sizeof(color.pixel)); swapwords(&color.red, 3 * sizeof(color.red)); /* assume g and b follow r */ } fwrite(&color, sizeof(XColor), 1, outfile); } fwrite(ximage->data, ximage->height * ximage->bytes_per_line, 1, outfile);}/* * Write a monochrome image out in Bitmap format. XWriteBitmapToFile * requires a Pixmap as input & we'd have to invent one before we could * use it. */writeXYPixmap(image, outfile) imageInfo *image; FILE *outfile;{ XImage *ximage = image->ximage; int w, h; byte b, *line; int lcount; int reverse = BlackPixel(hDisplay, hScreen) == 0; int swap = ximage->bitmap_bit_order != LSBFirst; if (verbose) fprintf(stderr, "%s: formatting Bitmap output\n", programName); if (ximage->depth != 1) { fprintf(stderr, "%s: can't write polychrome images in XY bitmap format\n", programName); return; } fprintf(outfile, "#define %s_width %d\n", imageName, ximage->width); fprintf(outfile, "#define %s_height %d\n", imageName, ximage->height); fprintf(outfile, "#define %s_x_hot 0\n", imageName); fprintf(outfile, "#define %s_y_hot 0\n", imageName); fprintf(outfile, "static char %s_bits[] = {\n", imageName); lcount = 0; fputs(" ", outfile); for (h=0; h<ximage->height; h++) { line = (byte *)(ximage->data + (h * ximage->bytes_per_line)); for (w=0; w<ximage->width; w+=8) { b = line[w/8]; if (reverse) b = ~b; if (swap) b = swapbits(b); fprintf(outfile, " 0x%02x", b); if (h<ximage->height || w+8<ximage->width) fputc(',', outfile); lcount++; if (lcount >= 12) { fputs("\n ", outfile); lcount = 0; } } } fputs(" };\n", outfile);}/* * Write a color image out in Pixmap format, suitable for loading with * "xpd" or "xloadimage". Note that "xpd" usually fails miserably if * the image is wider than 255 characters in the output file. */writeZPixmap(image, outfile) imageInfo *image; FILE *outfile;{ XImage *ximage = image->ximage; int nc, width, height, w, h, cidx, cpp; char mne[MAX_CELLS][3]; if (verbose) fprintf(stderr, "%s: formatting Pixmap output\n", programName); nc = image->numcells; cpp = image->numcells <= 26? 1 : 2; fprintf(outfile, "#define %s_format 1\n", imageName); fprintf(outfile, "#define %s_width %d\n", imageName, ximage->width); fprintf(outfile, "#define %s_height %d\n", imageName, ximage->height); fprintf(outfile, "#define %s_ncolors %d\n", imageName, image->numcells); fprintf(outfile, "#define %s_chars_per_pixel %d\n", imageName, cpp); fprintf(outfile, "static char * %s_colors[] = {\n", imageName); for (cidx=0; cidx<image->numcells; cidx++) { if (cpp > 1) { mne[cidx][0] = (char)(cidx / 10) + 'a'; mne[cidx][1] = (char)(cidx % 10) + '0'; mne[cidx][2] = '\0'; } else { mne[cidx][0] = (char)cidx + (cidx? 'A' : ' '); mne[cidx][1] = '\0'; } fprintf(outfile, "\"%s\", \"#%4.4x%4.4x%4.4x\"\n", mne[cidx], image->red[cidx], image->green[cidx], image->blue[cidx]); } fputs("} ;\n", outfile); fprintf(outfile, "static char * %s_pixels[] = {\n", imageName); for (h=0; h<ximage->height; h++) { fputs("\"", outfile); for (w=0; w<ximage->width; w++) fputs(mne[XGetPixel(ximage, w, h)], outfile); fputs("\",\n", outfile); } fputs("} ;\n", outfile);}main(argc, argv) int argc; char *argv[];{ char *args[100]; extern char *optarg; FILE *outfile; char *outfileName; XRectangle xrect; imageInfo image; int argn; int doAnd; int doOr; int depth; int noBell; int pixmapFormat; int psColor; int psFormat; int puzzleFormat; int simpleFormat; int xwdFormat; int brighten; int forceBitmap; int grabServer; ditherType ditherKind; int halftone; int compress; int sleepSeconds; int andBits; int orBits; int encapsulate; int sourceRoot; int sourceId; int sourceWd; Window sourceWindow; long ignored; char *ptr; char *display; int x, y, width, height, border, i; int brightenFactor; int optchar; int bellOn; bellOn = TRUE; brighten = FALSE; compress = TRUE; display = NULL; ditherKind = NO_DITHER; doAnd = FALSE; doOr = FALSE; encapsulate = FALSE; forceBitmap = FALSE; grabServer = TRUE; halftone = FALSE; outfile = stdout; outfileName = NULL; programName = argv[0]; puzzleFormat = FALSE; psFormat = TRUE; simpleFormat = FALSE; sleepSeconds = 0; sourceId = FALSE; sourceWd = FALSE; sourceRoot = FALSE; verbose = FALSE; xwdFormat = FALSE; /* merge environment options and command line options */ args[0] = programName; args[1] = (char *)getenv("XGRABSC"); if (args[1] != NULL) for (argn=2; argn<100 && (args[argn]=(char *)strchr(args[argn-1], ' ')) != NULL; argn++) { /* remove leading white space */ while (*args[argn] == ' ' || *args[argn] == 9) { *(args[argn]) = '\0'; args[argn]++; } if (*args[argn] == '|' || *args[argn] == '>') /* dbx leaves these in the cmd line */ break; } else argn = 1; for (i=1; i<argc && argn<100; argn++, i++) args[argn] = argv[i]; while ((optchar = getopt(argn, args, "ed:no:s:vg clri:w b:A:BCDFHO: IPWXZ")) != EOF) { switch (optchar) { case 'e': encapsulate = TRUE; goto pslabel; case 'd': display = optarg; break; case 'g': noBell = TRUE; break; case 'n': grabServer = FALSE; break; case 'o': outfileName = optarg; break; case 's': sleepSeconds = atoi(optarg); if (sleepSeconds < 0) sleepSeconds = 0; break; case 'v': verbose = TRUE; break; case 'c': compress = FALSE; goto pslabel; break; case 'r': sourceRoot = TRUE; sourceWd = sourceId = FALSE; break; case 'i': sourceId = TRUE; sourceRoot = sourceWd = FALSE; sourceWindow = 0; if (!sscanf(optarg, "0x%lx", &sourceWindow)) if (!sscanf(optarg, "%ld", &sourceWindow)) { sprintf(stderr, "%s: invalid window id\n", programName); exit(3); } break; case 'w': sourceWd = TRUE; sourceRoot = sourceId = FALSE; break; case 'A': andBits = atoi(optarg); doAnd = TRUE; break; case 'b': brightenFactor = atoi(optarg); if (brightenFactor <= 0) { fprintf(stderr, "%s: brightening factor must be a positive number\n", programName); exit(3); } brighten = TRUE; break; case 'B': halftone = FALSE; forceBitmap = TRUE; break; case 'D': ditherKind = MATRIX_DITHER; halftone = TRUE; forceBitmap = FALSE; break; case 'F': ditherKind = FS_DITHER; halftone = TRUE; forceBitmap = FALSE; break; case 'H': if (forceBitmap) { fprintf(stderr, "%s: both bitmap and halftone conversion requested. Using halftone.\n", programName); forceBitmap = FALSE; } if (halftone) fprintf(stderr, "%s: multiple halftone formats requested. Using Matrix halftoning.\n", programName); ditherKind = MATRIX_HALFTONE; halftone = TRUE; break; case 'O': orBits = atoi(optarg); doOr = TRUE; break; case 'P': psColor = FALSE;pslabel: psFormat = TRUE; simpleFormat = pixmapFormat = xwdFormat = puzzleFormat = FALSE; break; case 'C': psColor = TRUE; goto pslabel; case 'I': simpleFormat = TRUE; puzzleFormat = pixmapFormat = psFormat = xwdFormat = FALSE; break; case 'W': xwdFormat = TRUE; simpleFormat = pixmapFormat = puzzleFormat = psFormat = FALSE; break; case 'X': pixmapFormat = TRUE; simpleFormat = psFormat = puzzleFormat = xwdFormat = FALSE; break; case 'Z': puzzleFormat = TRUE; simpleFormat = pixmapFormat = xwdFormat = psFormat = FALSE; break; } } if (verbose) { fprintf(stderr, "%s: xgrabsc version %s\n", programName, version); fprintf(stderr, "%s: patchlevel %d\n", programName, patchLevel); fprintf(stderr, "%s: %s\n\n", programName, Copyright); } if (!display) display = (char *)getenv("DISPLAY"); hDisplay = XOpenDisplay(display); if (!hDisplay) { fprintf(stderr, "%s: could not open X display\n", programName); exit(3); } hScreen = DefaultScreen(hDisplay); hRoot = DefaultRootWindow(hDisplay); depth = DefaultDepth(hDisplay, hScreen); if (DisplayCells(hDisplay, hScreen) > MAX_CELLS) { fprintf(stderr, "%s: color table is too big for this program\n", programName); XCloseDisplay(hDisplay); exit(3); } /* sleep if asked to do so */ if (sleepSeconds) sleep(sleepSeconds); /* grab the screen if asked to do so */ if (grabServer) XGrabServer(hDisplay); /* get the source rectangle */ if (!sourceId) sourceWindow = hRoot; if (sourceRoot) { xrect.x = xrect.y = 0; xrect.width = DisplayWidth(hDisplay, hScreen); xrect.height = DisplayHeight(hDisplay, hScreen); } else if (sourceId || sourceWd) { if (sourceWd && !(sourceWindow=getWindow())) { fprintf(stderr, "%s: unable to find source window\n", programName); XCloseDisplay(hDisplay); exit(3); } /* get window widthXheight */ if (!XGetGeometry(hDisplay, sourceWindow, &ignored, &ignored, &ignored, &width, &height, &ignored, &border)) { fprintf(stderr, "%s: unable to get window coordinates\n", programName); XCloseDisplay(hDisplay); exit(3); } xrect.width = width; xrect.height = height; xrect.x = 0; xrect.y = 0; } else if (!getRectangle(&xrect)) { XCloseDisplay(hDisplay); exit(3); } /* get the image bounded by the rectangle */ if (!noBell) XBell(hDisplay, 50); if (!getImage(&xrect, &image, sourceWindow)) { XCloseDisplay(hDisplay); exit(3); } if (grabServer) XUngrabServer(hDisplay); if (!noBell) { XBell(hDisplay, 20); XBell(hDisplay, 30); } XFlush(hDisplay); /* do color image processing/conversions */ if (depth >= 2) { if (brighten) brightenColors(&image, brightenFactor); if (doAnd) alterPlanes(&image, TRUE, andBits); if (doOr) alterPlanes(&image, FALSE, orBits); if (forceBitmap) { pixmap2bitmap(&image); depth = 1; } else if (halftone) pixmap2halftone(&image, ditherKind); else compressColormap(&image); } /* open the output stream */ if (outfileName) { outfile = fopen(outfileName, "w"); if (!outfile) { fprintf(stderr, "%s: ", programName); perror(outfileName); exit(3); } ptr = rindex(outfileName, '.'); if (ptr) *ptr = '\0'; imageName = rindex(outfileName, '/'); if (imageName) imageName++; else imageName = outfileName; } else imageName = "unnamed"; /* default for image names */ if (psFormat) { if (psColor) writeColorPS(&image, outfile, compress, encapsulate); else writePostscript(&image, outfile, compress, encapsulate); } else if (xwdFormat) writeXWD(&image, outfile); else if (simpleFormat) writeSimple(&image, outfile); else if (puzzleFormat) writePuzzle(&image, outfile); else if (image.ximage->depth <= 1) writeXYPixmap(&image, outfile); else writeZPixmap(&image, outfile); XDestroyImage(image.ximage); XCloseDisplay(hDisplay); if (outfileName) fclose(outfile); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -