]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/PBMOutputDev.cc
Fixup pdfto* programs,
[evince.git] / pdf / xpdf / PBMOutputDev.cc
1 //========================================================================
2 //
3 // PBMOutputDev.cc
4 //
5 // Copyright 1998 Derek B. Noonburg
6 //
7 //========================================================================
8
9 #ifdef __GNUC__
10 #pragma implementation
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include "gmem.h"
18 #include "GString.h"
19 #include "Object.h"
20 #include "Stream.h"
21 #include "GfxState.h"
22 #include "GfxFont.h"
23 #include "Error.h"
24 #include "Params.h"
25 #include "PBMOutputDev.h"
26
27 //------------------------------------------------------------------------
28
29 PBMOutputDev *PBMOutputDev::makePBMOutputDev(char *displayName,
30                                              char *fileRoot1) {
31   Display *display;
32   Pixmap pixmap;
33   Window dummyWin;
34   int screen;
35   int invert;
36   unsigned long black, white;
37
38   if (!(display = XOpenDisplay(displayName))) {
39     fprintf(stderr, "Couldn't open display '%s'\n", displayName);
40     exit(1);
41   }
42   screen = DefaultScreen(display);
43
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);
49     return NULL;
50   } 
51   invert = (white & 1) == 1 ? 0xff : 0x00;
52
53   dummyWin = XCreateSimpleWindow(display, RootWindow(display, screen),
54                                  0, 0, 1, 1, 0,
55                                  black, white);
56   pixmap = XCreatePixmap(display, dummyWin, 1, 1, 1);
57   return new PBMOutputDev(display, screen, pixmap, dummyWin,
58                           invert, fileRoot1);
59 }
60
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)))
67 {
68   display = display1;
69   screen = screen1;
70   pixmap = pixmap1;
71   dummyWin = dummyWin1;
72   invert = invert1;
73   fileRoot = fileRoot1;
74   fileName = (char *)gmalloc(strlen(fileRoot) + 20);
75 }
76
77 PBMOutputDev::~PBMOutputDev() {
78   XFreePixmap(display, pixmap);
79   XDestroyWindow(display, dummyWin);
80   XCloseDisplay(display);
81   gfree(fileName);
82 }
83
84 void PBMOutputDev::startPage(int pageNum, GfxState *state) {
85
86   curPage = pageNum;
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);
93 }
94
95 void PBMOutputDev::endPage() {
96   XImage *image;
97   FILE *f;
98   int p;
99   int x, y, i;
100
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,
105                image, 0, 0);
106
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);
110     goto err;
111   }
112   fprintf(f, "P4\n");
113   fprintf(f, "%d %d\n", width, height);
114
115   for (y = 0; y < height; ++y) {
116     for (x = 0; x+8 <= width; x += 8) {
117       p = 0;
118       for (i = 0; i < 8; ++i)
119         p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
120       p ^= invert;
121       fputc((char)p, f);
122     }
123     if (width & 7) {
124       p = 0;
125       for (i = 0; i < (width & 7); ++i)
126         p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
127       p <<= 8 - (width & 7);
128       p ^= invert;
129       fputc((char)p, f);
130     }
131   }
132
133   fclose(f);
134
135  err:
136   gfree(image->data);
137   image->data = NULL;
138   XDestroyImage(image);
139
140   XOutputDev::endPage();
141 }