1 //========================================================================
5 // Copyright 1998-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
26 #include "PBMOutputDev.h"
28 //------------------------------------------------------------------------
30 PBMOutputDev *PBMOutputDev::makePBMOutputDev(char *displayName,
37 unsigned long black, white;
40 if (!(displayA = XOpenDisplay(displayName))) {
41 fprintf(stderr, "Couldn't open display '%s'\n", displayName);
44 screenA = DefaultScreen(displayA);
46 black = BlackPixel(displayA, screenA);
47 white = WhitePixel(displayA, screenA);
48 if ((black & 1) == (white & 1)) {
49 fprintf(stderr, "Weird black/white pixel colors\n");
50 XCloseDisplay(displayA);
53 invertA = (white & 1) == 1 ? 0xff : 0x00;
55 dummyWinA = XCreateSimpleWindow(displayA, RootWindow(displayA, screenA),
58 pixmapA = XCreatePixmap(displayA, dummyWinA, 1, 1, 1);
59 out = new PBMOutputDev(displayA, screenA, pixmapA, dummyWinA,
64 void PBMOutputDev::killPBMOutputDev(PBMOutputDev *out) {
69 displayA = out->display;
70 pixmapA = out->pixmap;
71 dummyWinA = out->dummyWin;
75 // these have to be done *after* the XOutputDev (parent of the
76 // PBMOutputDev) is deleted, since XOutputDev::~XOutputDev() needs
78 XFreePixmap(displayA, pixmapA);
79 XDestroyWindow(displayA, dummyWinA);
80 XCloseDisplay(displayA);
83 PBMOutputDev::PBMOutputDev(Display *displayA, int screenA,
84 Pixmap pixmapA, Window dummyWinA,
85 int invertA, char *fileRootA):
86 XOutputDev(displayA, screenA,
87 DefaultVisual(displayA, screenA),
88 DefaultColormap(displayA, screenA),
90 WhitePixel(displayA, DefaultScreen(displayA)),
99 fileName = (char *)gmalloc(strlen(fileRoot) + 20);
102 PBMOutputDev::~PBMOutputDev() {
106 void PBMOutputDev::startPage(int pageNum, GfxState *state) {
108 width = (int)(state->getPageWidth() + 0.5);
109 height = (int)(state->getPageHeight() + 0.5);
110 XFreePixmap(display, pixmap);
111 pixmap = XCreatePixmap(display, dummyWin, width, height, 1);
112 setPixmap(pixmap, width, height);
113 XOutputDev::startPage(pageNum, state);
116 void PBMOutputDev::endPage() {
122 image = XCreateImage(display, DefaultVisual(display, screen),
123 1, ZPixmap, 0, NULL, width, height, 8, 0);
124 image->data = (char *)gmalloc(height * image->bytes_per_line);
125 XGetSubImage(display, pixmap, 0, 0, width, height, 1, ZPixmap,
128 sprintf(fileName, "%s-%06d.pbm", fileRoot, curPage);
129 if (!(f = fopen(fileName, "wb"))) {
130 fprintf(stderr, "Couldn't open output file '%s'\n", fileName);
134 fprintf(f, "%d %d\n", width, height);
136 for (y = 0; y < height; ++y) {
137 for (x = 0; x+8 <= width; x += 8) {
139 for (i = 0; i < 8; ++i)
140 p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
146 for (i = 0; i < (width & 7); ++i)
147 p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
148 p <<= 8 - (width & 7);
159 XDestroyImage(image);
161 XOutputDev::endPage();