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;
38 if (!(display = XOpenDisplay(displayName))) {
39 fprintf(stderr, "Couldn't open display '%s'\n", displayName);
42 screen = DefaultScreen(display);
44 black = BlackPixel(display, screen);
45 white = WhitePixel(display, screen);
46 if ((black & 1) == (white & 1)) {
47 fprintf(stderr, "Weird black/white pixel colors\n");
48 XCloseDisplay(display);
51 invert = (white & 1) == 1 ? 0xff : 0x00;
53 dummyWin = XCreateSimpleWindow(display, RootWindow(display, screen),
56 pixmap = XCreatePixmap(display, dummyWin, 1, 1, 1);
57 return new PBMOutputDev(display, screen, pixmap, dummyWin,
61 PBMOutputDev::PBMOutputDev(Display *display1, int screen1,
62 Pixmap pixmap1, Window dummyWin1,
63 int invert1, char *fileRoot1):
64 XOutputDev(display1, pixmap1, 1,
65 DefaultColormap(display1, screen1),
66 WhitePixel(display1, DefaultScreen(display1)))
74 fileName = (char *)gmalloc(strlen(fileRoot) + 20);
77 PBMOutputDev::~PBMOutputDev() {
78 XFreePixmap(display, pixmap);
79 XDestroyWindow(display, dummyWin);
80 XCloseDisplay(display);
84 void PBMOutputDev::startPage(int pageNum, GfxState *state) {
87 width = (int)(state->getPageWidth() + 0.5);
88 height = (int)(state->getPageHeight() + 0.5);
89 XFreePixmap(display, pixmap);
90 pixmap = XCreatePixmap(display, dummyWin, width, height, 1);
91 setPixmap(pixmap, width, height);
92 XOutputDev::startPage(pageNum, state);
95 void PBMOutputDev::endPage() {
101 image = XCreateImage(display, DefaultVisual(display, screen),
102 1, ZPixmap, 0, NULL, width, height, 8, 0);
103 image->data = (char *)gmalloc(height * image->bytes_per_line);
104 XGetSubImage(display, pixmap, 0, 0, width, height, 1, ZPixmap,
107 sprintf(fileName, "%s-%06d.pbm", fileRoot, curPage);
108 if (!(f = fopen(fileName, "wb"))) {
109 fprintf(stderr, "Couldn't open output file '%s'\n", fileName);
113 fprintf(f, "%d %d\n", width, height);
115 for (y = 0; y < height; ++y) {
116 for (x = 0; x+8 <= width; x += 8) {
118 for (i = 0; i < 8; ++i)
119 p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
125 for (i = 0; i < (width & 7); ++i)
126 p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
127 p <<= 8 - (width & 7);
138 XDestroyImage(image);
140 XOutputDev::endPage();