1 //========================================================================
5 // Copyright 1998-2002 Glyph & Cog, LLC
7 //========================================================================
10 #pragma implementation
25 #include "PBMOutputDev.h"
27 //------------------------------------------------------------------------
29 PBMOutputDev *PBMOutputDev::makePBMOutputDev(char *displayName,
36 unsigned long black, white;
39 if (!(displayA = XOpenDisplay(displayName))) {
40 fprintf(stderr, "Couldn't open display '%s'\n", displayName);
43 screenA = DefaultScreen(displayA);
45 black = BlackPixel(displayA, screenA);
46 white = WhitePixel(displayA, screenA);
47 if ((black & 1) == (white & 1)) {
48 fprintf(stderr, "Weird black/white pixel colors\n");
49 XCloseDisplay(displayA);
52 invertA = (white & 1) == 1 ? 0xff : 0x00;
54 dummyWinA = XCreateSimpleWindow(displayA, RootWindow(displayA, screenA),
57 pixmapA = XCreatePixmap(displayA, dummyWinA, 1, 1, 1);
58 out = new PBMOutputDev(displayA, screenA, pixmapA, dummyWinA,
63 void PBMOutputDev::killPBMOutputDev(PBMOutputDev *out) {
68 displayA = out->display;
69 pixmapA = out->pixmap;
70 dummyWinA = out->dummyWin;
74 // these have to be done *after* the XOutputDev (parent of the
75 // PBMOutputDev) is deleted, since XOutputDev::~XOutputDev() needs
77 XFreePixmap(displayA, pixmapA);
78 XDestroyWindow(displayA, dummyWinA);
79 XCloseDisplay(displayA);
82 PBMOutputDev::PBMOutputDev(Display *displayA, int screenA,
83 Pixmap pixmapA, Window dummyWinA,
84 int invertA, char *fileRootA):
85 XOutputDev(displayA, pixmapA, 1,
86 DefaultColormap(displayA, screenA), gFalse,
87 WhitePixel(displayA, DefaultScreen(displayA)),
96 fileName = (char *)gmalloc(strlen(fileRoot) + 20);
99 PBMOutputDev::~PBMOutputDev() {
103 void PBMOutputDev::startPage(int pageNum, GfxState *state) {
105 width = (int)(state->getPageWidth() + 0.5);
106 height = (int)(state->getPageHeight() + 0.5);
107 XFreePixmap(display, pixmap);
108 pixmap = XCreatePixmap(display, dummyWin, width, height, 1);
109 setPixmap(pixmap, width, height);
110 XOutputDev::startPage(pageNum, state);
113 void PBMOutputDev::endPage() {
119 image = XCreateImage(display, DefaultVisual(display, screen),
120 1, ZPixmap, 0, NULL, width, height, 8, 0);
121 image->data = (char *)gmalloc(height * image->bytes_per_line);
122 XGetSubImage(display, pixmap, 0, 0, width, height, 1, ZPixmap,
125 sprintf(fileName, "%s-%06d.pbm", fileRoot, curPage);
126 if (!(f = fopen(fileName, "wb"))) {
127 fprintf(stderr, "Couldn't open output file '%s'\n", fileName);
131 fprintf(f, "%d %d\n", width, height);
133 for (y = 0; y < height; ++y) {
134 for (x = 0; x+8 <= width; x += 8) {
136 for (i = 0; i < 8; ++i)
137 p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
143 for (i = 0; i < (width & 7); ++i)
144 p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
145 p <<= 8 - (width & 7);
156 XDestroyImage(image);
158 XOutputDev::endPage();