1 //========================================================================
5 // Copyright 1998 Derek B. Noonburg
7 //========================================================================
10 #pragma implementation
25 #include "PBMOutputDev.h"
27 //------------------------------------------------------------------------
29 PBMOutputDev *PBMOutputDev::makePBMOutputDev(char *displayName,
36 unsigned long black, white;
39 if (!(display = XOpenDisplay(displayName))) {
40 fprintf(stderr, "Couldn't open display '%s'\n", displayName);
43 screen = DefaultScreen(display);
45 black = BlackPixel(display, screen);
46 white = WhitePixel(display, screen);
47 if ((black & 1) == (white & 1)) {
48 fprintf(stderr, "Weird black/white pixel colors\n");
49 XCloseDisplay(display);
52 invert = (white & 1) == 1 ? 0xff : 0x00;
54 dummyWin = XCreateSimpleWindow(display, RootWindow(display, screen),
57 pixmap = XCreatePixmap(display, dummyWin, 1, 1, 1);
58 out = new PBMOutputDev(display, screen, pixmap, dummyWin,
64 void PBMOutputDev::killPBMOutputDev(PBMOutputDev *out) {
69 display = out->display;
71 dummyWin = out->dummyWin;
75 // these have to be done *after* the XOutputDev (parent of the
76 // PBMOutputDev) is deleted, since XOutputDev::~XOutputDev() needs
78 XFreePixmap(display, pixmap);
79 XDestroyWindow(display, dummyWin);
80 XCloseDisplay(display);
83 PBMOutputDev::PBMOutputDev(Display *display1, int screen1,
84 Pixmap pixmap1, Window dummyWin1,
85 int invert1, char *fileRoot1):
86 XOutputDev(display1, pixmap1, 1,
87 DefaultColormap(display1, screen1),
88 WhitePixel(display1, DefaultScreen(display1)))
96 fileName = (char *)gmalloc(strlen(fileRoot) + 20);
99 PBMOutputDev::~PBMOutputDev() {
103 void PBMOutputDev::startPage(int pageNum, GfxState *state) {
106 width = (int)(state->getPageWidth() + 0.5);
107 height = (int)(state->getPageHeight() + 0.5);
108 XFreePixmap(display, pixmap);
109 pixmap = XCreatePixmap(display, dummyWin, width, height, 1);
110 setPixmap(pixmap, width, height);
111 XOutputDev::startPage(pageNum, state);
114 void PBMOutputDev::endPage() {
120 image = XCreateImage(display, DefaultVisual(display, screen),
121 1, ZPixmap, 0, NULL, width, height, 8, 0);
122 image->data = (char *)gmalloc(height * image->bytes_per_line);
123 XGetSubImage(display, pixmap, 0, 0, width, height, 1, ZPixmap,
126 sprintf(fileName, "%s-%06d.pbm", fileRoot, curPage);
127 if (!(f = fopen(fileName, "wb"))) {
128 fprintf(stderr, "Couldn't open output file '%s'\n", fileName);
132 fprintf(f, "%d %d\n", width, height);
134 for (y = 0; y < height; ++y) {
135 for (x = 0; x+8 <= width; x += 8) {
137 for (i = 0; i < 8; ++i)
138 p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
144 for (i = 0; i < (width & 7); ++i)
145 p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
146 p <<= 8 - (width & 7);
157 XDestroyImage(image);
159 XOutputDev::endPage();