added bounding box stubs
[swftools.git] / lib / pdf / BitmapOutputDev.cc
1 /* InfoOutputDev.h
2
3    Output Device which creates a bitmap.
4
5    Swftools is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    Swftools is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with swftools; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <memory.h>
22 #include <assert.h>
23 #include "config.h"
24 #include "BitmapOutputDev.h"
25 #include "GFXOutputDev.h"
26 #include "SplashBitmap.h"
27 #include "SplashPattern.h"
28 #include "Splash.h"
29 #include "../log.h"
30 #include "../png.h"
31 #include "../devices/record.h"
32
33 static SplashColor splash_white = {255,255,255};
34 static SplashColor splash_black = {0,0,0};
35     
36 ClipState::ClipState()
37 {
38     this->next = 0;
39     this->clipbitmap = 0;
40     this->written = 0;
41 }
42
43 BitmapOutputDev::BitmapOutputDev(InfoOutputDev*info, PDFDoc*doc)
44 {
45     this->info = info;
46     this->doc = doc;
47     this->xref = doc->getXRef();
48     
49     /* color graphic output device, for creating bitmaps */
50     this->rgbdev = new SplashOutputDev(splashModeRGB8, 1, gFalse, splash_white, gTrue, gTrue);
51   
52     /* color mode for binary bitmaps */
53     SplashColorMode colorMode = splashModeMono8;
54
55     /* two devices for testing things against clipping: one clips, the other doesn't */
56     this->clip0dev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
57     this->clip1dev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
58     
59     /* device indicating where polygonal pixels were drawn */
60     this->boolpolydev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
61     /* device indicating where text pixels were drawn */
62     this->booltextdev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
63
64     /* device for handling texts and links */
65     this->gfxdev = new GFXOutputDev(info, this->doc);
66
67     this->rgbdev->startDoc(this->xref);
68     this->boolpolydev->startDoc(this->xref);
69     this->booltextdev->startDoc(this->xref);
70     this->clip0dev->startDoc(this->xref);
71     this->clip1dev->startDoc(this->xref);
72
73     this->gfxoutput = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
74     gfxdevice_record_init(this->gfxoutput);
75
76     this->gfxdev->setDevice(this->gfxoutput);
77     
78     this->config_extrafontdata = 0;
79     this->bboxpath = 0;
80     //this->clipdev = 0;
81     //this->clipstates = 0;
82 }
83 BitmapOutputDev::~BitmapOutputDev()
84 {
85     if(this->gfxoutput) {
86         gfxresult_t*r = this->gfxoutput->finish(this->gfxoutput);
87         r->destroy(r);
88         free(this->gfxoutput);this->gfxoutput = 0;
89     }
90     if(this->bboxpath) {
91         delete this->bboxpath;this->bboxpath = 0;
92     }
93     if(this->rgbdev) {
94         delete this->rgbdev;this->rgbdev = 0;
95     }
96     if(this->gfxdev) {
97         delete this->gfxdev;this->gfxdev= 0;
98     }
99     if(this->boolpolydev) {
100         delete this->boolpolydev;this->boolpolydev = 0;
101     }
102     if(this->booltextdev) {
103         delete this->booltextdev;this->booltextdev = 0;
104     }
105     if(this->clip0dev) {
106         delete this->clip0dev;this->clip0dev = 0;
107     }
108     if(this->clip1dev) {
109         delete this->clip1dev;this->clip1dev = 0;
110     }
111     //if(this->clipbitmap) {
112     //    delete this->clipbitmap;this->clipbitmap = 0;
113     //}
114     //if(this->clipdev) {
115     //    delete this->clipdev;this->clipdev = 0;
116     //}
117
118 }
119
120 GBool BitmapOutputDev::getVectorAntialias()
121 {
122     return this->rgbdev->getVectorAntialias();
123 }
124 void BitmapOutputDev::setVectorAntialias(GBool vaa)
125 {
126     this->rgbdev->setVectorAntialias(vaa);
127 }
128 void BitmapOutputDev::setDevice(gfxdevice_t*dev)
129 {
130     this->dev = dev;
131 }
132 void BitmapOutputDev::setMove(int x,int y)
133 {
134     this->gfxdev->setMove(x,y);
135     this->user_movex = x;
136     this->user_movey = y;
137 }
138 void BitmapOutputDev::setClip(int x1,int y1,int x2,int y2)
139 {
140     this->gfxdev->setClip(x1,y1,x2,y2);
141     this->user_clipx1 = x1;
142     this->user_clipy1 = y1;
143     this->user_clipx2 = x2;
144     this->user_clipy2 = y2;
145 }
146 void BitmapOutputDev::setParameter(const char*key, const char*value)
147 {
148     if(!strcmp(key, "extrafontdata")) {
149         this->config_extrafontdata = atoi(value);
150     }
151     this->gfxdev->setParameter(key, value);
152 }
153 void BitmapOutputDev::preparePage(int pdfpage, int outputpage)
154 {
155 }
156
157 static void getBitmapBBox(Guchar*alpha, int width, int height, int*xmin, int*ymin, int*xmax, int*ymax)
158 {
159     *ymin = -1;
160     *xmin = width;
161     *xmax = 0;
162     int x,y;
163     for(y=0;y<height;y++) {
164         Guchar*a = &alpha[y*width];
165         for(x=0;x<width;x++) {
166             if(a[x]) break;
167         }
168         int left = x; //first occupied pixel from left
169         int right = x+1; //last non-occupied pixel from right
170         for(;x<width;x++) {
171             if(a[x]) right=x+1;
172         }
173
174         if(left!=width) {
175             if(*ymin<0) 
176                 *ymin=y;
177             *ymax=y+1;
178             if(left<*xmin) *xmin = left;
179             if(right>*xmax) *xmax = right;
180         }
181     }
182     if(*xmin>=*xmax || *ymin>=*ymax) {
183         *xmin = 0;
184         *ymin = 0;
185         *xmax = 0;
186         *ymax = 0;
187     }
188 }
189
190 void BitmapOutputDev::flushBitmap()
191 {
192     int width = rgbdev->getBitmapWidth();
193     int height = rgbdev->getBitmapHeight();
194     
195     SplashColorPtr rgb = rgbbitmap->getDataPtr();
196     Guchar*alpha = rgbbitmap->getAlphaPtr();
197
198     int xmin,ymin,xmax,ymax;
199     getBitmapBBox(alpha, width, height, &xmin,&ymin,&xmax,&ymax);
200
201     /* clip against (-movex, -movey, -movex+width, -movey+height) */
202     if(xmin < -this->movex) xmin = -this->movex;
203     if(ymin < -this->movey) ymin = -this->movey;
204     if(xmax > -this->movex + width) xmax = -this->movex+this->width;
205     if(ymax > -this->movey + height) ymax = -this->movey+this->height;
206
207     msg("<verbose> Flushing bitmap (bbox: %d,%d,%d,%d)", xmin,ymin,xmax,ymax);
208     
209     if((xmax-xmin)<=0 || (ymax-ymin)<=0) // no bitmap, nothing to do
210         return;
211
212     if(sizeof(SplashColor)!=3) {
213         msg("<error> sizeof(SplashColor)!=3");
214         return;
215     }
216     //xmin = ymin = 0;
217     //xmax = width;
218     //ymax = height;
219
220     int rangex = xmax-xmin;
221     int rangey = ymax-ymin;
222     gfximage_t*img = (gfximage_t*)malloc(sizeof(gfximage_t)); 
223     img->data = (gfxcolor_t*)malloc(rangex * rangey * 4);
224     img->width = rangex;
225     img->height = rangey;
226     int x,y;
227     for(y=0;y<rangey;y++) {
228         SplashColorPtr in=&rgb[((y+ymin)*width+xmin)*sizeof(SplashColor)];
229         gfxcolor_t*out = &img->data[y*rangex];
230         Guchar*ain = &alpha[(y+ymin)*width+xmin];
231         if(this->emptypage) {
232             for(x=0;x<rangex;x++) {
233                 /* the first bitmap on the page doesn't need to have an alpha channel-
234                    blend against a white background*/
235                 out[x].r = (in[x*3+0]*ain[x])/255 + 255-ain[x];
236                 out[x].g = (in[x*3+1]*ain[x])/255 + 255-ain[x];
237                 out[x].b = (in[x*3+2]*ain[x])/255 + 255-ain[x];
238                 out[x].a = 255;
239             }
240         } else {
241             for(x=0;x<rangex;x++) {
242                 /* according to endPage()/compositeBackground() in xpdf/SplashOutputDev.cc, we
243                    have to premultiply alpha (mix background and pixel according to the alpha channel).
244                 */
245                 out[x].r = (in[x*3+0]*ain[x])/255;
246                 out[x].g = (in[x*3+1]*ain[x])/255;
247                 out[x].b = (in[x*3+2]*ain[x])/255;
248                 out[x].a = ain[x];
249             }
250         }
251     }
252     /* transform bitmap rectangle to "device space" */
253     xmin += movex;
254     ymin += movey;
255     xmax += movex;
256     ymax += movey;
257
258     gfxmatrix_t m;
259     m.tx = xmin;
260     m.ty = ymin;
261     m.m00 = m.m11 = 1;
262     m.m10 = m.m01 = 0;
263
264     gfxline_t* line = gfxline_makerectangle(xmin, ymin, xmax, ymax);
265     dev->fillbitmap(dev, line, img, &m, 0);
266     gfxline_free(line);
267
268     memset(rgbbitmap->getAlphaPtr(), 0, rgbbitmap->getWidth()*rgbbitmap->getHeight());
269     memset(rgbbitmap->getDataPtr(), 0, rgbbitmap->getRowSize()*rgbbitmap->getHeight());
270
271     free(img->data);img->data=0;free(img);img=0;
272
273     this->emptypage = 0;
274 }
275
276 void BitmapOutputDev::flushText()
277 {
278     msg("<verbose> Flushing text/polygons");
279     gfxdevice_record_flush(this->gfxoutput, this->dev);
280     
281     this->emptypage = 0;
282 }
283
284 void writeAlpha(SplashBitmap*bitmap, char*filename)
285 {
286     int y,x;
287     
288     int width = bitmap->getWidth();
289     int height = bitmap->getHeight();
290
291     gfxcolor_t*data = (gfxcolor_t*)malloc(sizeof(gfxcolor_t)*width*height);
292
293     for(y=0;y<height;y++) {
294         gfxcolor_t*line = &data[y*width];
295         for(x=0;x<width;x++) {
296             int a = bitmap->getAlpha(x,y);
297             line[x].r = a;
298             line[x].g = a;
299             line[x].b = a;
300             line[x].a = 255;
301         }
302     }
303     writePNG(filename, (unsigned char*)data, width, height);
304     free(data);
305 }
306 static int dbg_btm_counter=1;
307
308 static const char*STATE_NAME[] = {"parallel", "textabovebitmap", "bitmapabovetext"};
309
310 void BitmapOutputDev::checkNewText(int x1, int y1, int x2, int y2)
311 {
312     /* called once some new text was drawn on booltextdev, and
313        before the same thing is drawn on gfxdev */
314    
315     msg("<trace> Testing new text data against current bitmap data, state=%s, counter=%d\n", STATE_NAME[layerstate], dbg_btm_counter);
316     
317     char filename1[80];
318     char filename2[80];
319     sprintf(filename1, "state%dbitmap_afternewtext.png", dbg_btm_counter);
320     sprintf(filename2, "state%dtext_afternewtext.png", dbg_btm_counter);
321     if(0) {
322         writeAlpha(boolpolybitmap, filename1);
323         writeAlpha(booltextbitmap, filename2);
324     }
325     dbg_btm_counter++;
326
327     if(intersection(x1,y1,x2,y2)) {
328         if(layerstate==STATE_PARALLEL) {
329             /* the new text is above the bitmap. So record that fact,
330                and also clear the bitmap buffer, so we can check for
331                new intersections */
332             msg("<verbose> Text is above current bitmap/polygon data");
333             layerstate=STATE_TEXT_IS_ABOVE;
334             clearBoolPolyDev();
335         } else if(layerstate==STATE_BITMAP_IS_ABOVE) {
336             /* there's a bitmap above the (old) text. So we need
337                to flush out that text, and record that the *new*
338                text is now *above* the bitmap
339              */
340             msg("<verbose> Text is above current bitmap/polygon data (which is above some other text)");
341             flushText();
342             layerstate=STATE_TEXT_IS_ABOVE;
343             /* clear both bool devices- the text device because
344                we just dumped out all the (old) text, and the
345                poly dev so we can check for new intersections */
346             clearBoolPolyDev();
347             clearBoolTextDev();
348         } else {
349             /* we already know that the current text section is
350                above the current bitmap section- now just new
351                bitmap data *and* new text data was drawn, and
352                *again* it's above the current bitmap- so clear
353                the polygon bitmap again, so we can check for
354                new intersections */
355             msg("<verbose> Text is still above current bitmap/polygon data");
356             clearBoolPolyDev();
357         }
358     } 
359 }
360
361 void BitmapOutputDev::checkNewBitmap()
362 {
363     /* similar to checkNewText() above, only in reverse */
364     msg("<trace> Testing new graphics data against current text data, state=%s, counter=%d\n", STATE_NAME[layerstate], dbg_btm_counter);
365
366     char filename1[80];
367     char filename2[80];
368     sprintf(filename1, "state%dbitmap_afternewgfx.png", dbg_btm_counter);
369     sprintf(filename2, "state%dtext_afternewgfx.png", dbg_btm_counter);
370     if(0) {
371         writeAlpha(boolpolybitmap, filename1);
372         writeAlpha(booltextbitmap, filename2);
373     }
374     dbg_btm_counter++;
375
376     if(intersection(0,0,0,0)) {
377         if(layerstate==STATE_PARALLEL) {
378             msg("<verbose> Bitmap is above current text data");
379             layerstate=STATE_BITMAP_IS_ABOVE;
380             clearBoolTextDev();
381         } else if(layerstate==STATE_TEXT_IS_ABOVE) {
382             msg("<verbose> Bitmap is above current text data (which is above some bitmap)");
383             flushBitmap();
384             layerstate=STATE_BITMAP_IS_ABOVE;
385             clearBoolTextDev();
386             clearBoolPolyDev();
387         } else {
388             msg("<verbose> Bitmap is still above current text data");
389             clearBoolTextDev();
390         }
391     } 
392 }
393
394 //void checkNewText() {
395 //    Guchar*alpha = rgbbitmap->getAlphaPtr();
396 //    Guchar*charpixels = clip1bitmap->getDataPtr();
397 //    int xx,yy;
398 //    for(yy=0;yy<height;yy++) {
399 //        Guchar*aline = &alpha[yy*width];
400 //        Guchar*cline = &charpixels[yy*width8];
401 //        for(xx=0;xx<width;xx++) {
402 //            int bit = xx&7;
403 //            int bytepos = xx>>3;
404 //            /* TODO: is the bit order correct? */
405 //            if(aline[xx] && (cline[bytepos]&(1<<bit))) 
406 //              break;
407 //        }
408 //        if(xx!=width)
409 //            break;
410 //}
411
412 GBool BitmapOutputDev::clip0and1differ(int x1,int y1,int x2,int y2)
413 {
414     if(clip0bitmap->getMode()==splashModeMono1) {
415         if(x2<=x1)
416             return gFalse;
417         if(x2<0)
418             return gFalse;
419         if(x1<0)
420             x1 = 0;
421         if(x1>=clip0bitmap->getWidth())
422             return gFalse;
423         if(x2>clip0bitmap->getWidth())
424             x2=clip0bitmap->getWidth();
425
426         if(y2<=y1)
427             return gFalse;
428         if(y2<0)
429             return gFalse;
430         if(y1<0)
431             y1 = 0;
432         if(y1>=clip0bitmap->getHeight())
433             return gFalse;
434         if(y2>clip0bitmap->getHeight())
435             y2=clip0bitmap->getHeight();
436         
437         SplashBitmap*clip0 = clip0bitmap;
438         SplashBitmap*clip1 = clip1bitmap;
439         int width8 = (clip0bitmap->getWidth()+7)/8;
440         int height = clip0bitmap->getHeight();
441         int x18 = x1/8;
442         int x28 = (x2+7)/8;
443         int y;
444
445         for(y=y1;y<y2;y++) {
446             unsigned char*row1 = &clip0bitmap->getDataPtr()[width8*y+x18];
447             unsigned char*row2 = &clip1bitmap->getDataPtr()[width8*y+x28];
448             if(memcmp(row1, row2, x28-x18))
449                 return gTrue;
450         }
451         return gFalse;
452     } else {
453         SplashBitmap*clip0 = clip0bitmap;
454         SplashBitmap*clip1 = clip1bitmap;
455         int width = clip0->getAlphaRowSize();
456         int height = clip0->getHeight();
457         return memcmp(clip0->getAlphaPtr(), clip1->getAlphaPtr(), width*height);
458     }
459 }
460
461 static void clearBooleanBitmap(SplashBitmap*btm)
462 {
463     if(btm->getMode()==splashModeMono1) {
464         int width8 = (btm->getWidth()+7)/8;
465         int width = btm->getWidth();
466         int height = btm->getHeight();
467         memset(btm->getDataPtr(), 0, width8*height);
468     } else {
469         int width = btm->getAlphaRowSize();
470         int height = btm->getHeight();
471         memset(btm->getAlphaPtr(), 0, width*height);
472     }
473 }
474
475 long long unsigned int compare64(long long unsigned int*data1, long long unsigned int*data2, int len)
476 {
477     long long unsigned int c;
478     int t;
479     for(t=0;t<len;t++) {
480         c |= data1[t]&data2[t];
481     }
482     return c;
483 }
484
485 GBool BitmapOutputDev::intersection(int x1, int y1, int x2, int y2)
486 {
487     SplashBitmap*boolpoly = boolpolybitmap;
488     SplashBitmap*booltext = booltextbitmap;
489         
490     if(boolpoly->getMode()==splashModeMono1) {
491         /* alternative implementation, using one bit per pixel-
492            would work if xpdf wouldn't try to dither everything */
493
494         Guchar*polypixels = boolpoly->getDataPtr();
495         Guchar*textpixels = booltext->getDataPtr();
496
497         int width8 = (width+7)/8;
498         int height = boolpoly->getHeight();
499         
500         if(x1|y1|x2|y2) {
501             if(y1>=0 && y1<=y2 && y2<=height) {
502                 polypixels+=y1*width8;
503                 textpixels+=y1*width8;
504                 height=y2-y1;
505             }
506         }
507         
508         int t;
509         int len = height*width8;
510         unsigned long long int c=0;
511         assert(sizeof(unsigned long long int)==8);
512         {
513             if(((int)polypixels&7) || ((int)textpixels&7)) {
514                 //msg("<warning> Non-optimal alignment");
515             }
516             int l2 = len;
517             len /= sizeof(unsigned long long int);
518             c = compare64((unsigned long long int*)polypixels, (unsigned long long int*)textpixels, len);
519             int l1 = len*sizeof(unsigned long long int);
520             for(t=l1;t<l2;t++) {
521                 c |= (unsigned long long int)(polypixels[t]&textpixels[t]);
522             }
523         }
524         if(c)
525             /* if graphic data and the characters overlap, they have common bits */
526             return gTrue;
527         else
528             return gFalse;
529     } else {
530         Guchar*polypixels = boolpoly->getAlphaPtr();
531         Guchar*textpixels = booltext->getAlphaPtr();
532         
533         int width = boolpoly->getAlphaRowSize();
534         int height = boolpoly->getHeight();
535         
536         int t;
537         int len = height*width;
538         unsigned int c=0;
539         if(len & (sizeof(unsigned int)-1)) {
540             Guchar c2=0;
541             for(t=0;t<len;t++) {
542                 if(polypixels[t]&&textpixels[t])
543                     return gTrue;
544             }
545         } else {
546             len /= sizeof(unsigned int);
547             for(t=0;t<len;t++) {
548                 if((((unsigned int*)polypixels)[t]) & (((unsigned int*)textpixels)[t]))
549                     return gTrue;
550             }
551         }
552         return gFalse;
553     }
554 }
555
556
557 void BitmapOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
558 {
559     double x1,y1,x2,y2;
560     state->transform(crop_x1,crop_y1,&x1,&y1);
561     state->transform(crop_x2,crop_y2,&x2,&y2);
562     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
563     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
564     
565     this->movex = -(int)x1 - user_movex;
566     this->movey = -(int)y1 - user_movey;
567     
568     if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
569         x1 = user_clipx1;
570         x2 = user_clipx2;
571         y1 = user_clipy1;
572         y2 = user_clipy2;
573     }
574     this->width = (int)(x2-x1);
575     this->height = (int)(y2-y1);
576
577     msg("<debug> startPage");
578     rgbdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
579     boolpolydev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
580     booltextdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
581     clip0dev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
582     clip1dev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
583     gfxdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
584
585     boolpolybitmap = boolpolydev->getBitmap();
586     booltextbitmap = booltextdev->getBitmap();
587     clip0bitmap = clip0dev->getBitmap();
588     clip1bitmap = clip1dev->getBitmap();
589     rgbbitmap = rgbdev->getBitmap();
590     
591     flushText(); // write out the initial clipping rectangle
592
593     /* just in case any device did draw a white background rectangle 
594        into the device */
595     clearBoolTextDev();
596     clearBoolPolyDev();
597
598     this->layerstate = STATE_PARALLEL;
599     this->emptypage = 1;
600     msg("<debug> startPage done");
601 }
602
603 void BitmapOutputDev::endPage()
604 {
605     msg("<verbose> endPage (BitmapOutputDev)");
606
607     /* notice: we're not fully done yet with this page- there might still be 
608        a few calls to drawLink() yet to come */
609 }
610 void BitmapOutputDev::finishPage()
611 {
612     msg("<verbose> finishPage (BitmapOutputDev)");
613     gfxdev->endPage();
614    
615     if(layerstate == STATE_BITMAP_IS_ABOVE) {
616         this->flushText();
617         this->flushBitmap();
618     } else {
619         this->flushBitmap();
620         this->flushText();
621     }
622
623     /* splash will now destroy alpha, and paint the 
624        background color into the "holes" in the bitmap */
625     boolpolydev->endPage();
626     booltextdev->endPage();
627     rgbdev->endPage();
628     clip0dev->endPage();
629     clip1dev->endPage();
630 }
631
632 GBool BitmapOutputDev::upsideDown()
633 {
634     boolpolydev->upsideDown();
635     booltextdev->upsideDown();
636     clip0dev->upsideDown();
637     clip1dev->upsideDown();
638     return rgbdev->upsideDown();
639 }
640
641 GBool BitmapOutputDev::useDrawChar()
642 {
643     boolpolydev->useDrawChar();
644     booltextdev->useDrawChar();
645     clip0dev->useDrawChar();
646     clip1dev->useDrawChar();
647     return rgbdev->useDrawChar();
648 }
649
650 GBool BitmapOutputDev::useTilingPatternFill()
651 {
652     boolpolydev->useTilingPatternFill();
653     booltextdev->useTilingPatternFill();
654     clip0dev->useTilingPatternFill();
655     clip1dev->useTilingPatternFill();
656     return rgbdev->useTilingPatternFill();
657 }
658
659 GBool BitmapOutputDev::useShadedFills()
660 {
661     boolpolydev->useShadedFills();
662     booltextdev->useShadedFills();
663     clip0dev->useShadedFills();
664     clip1dev->useShadedFills();
665     return rgbdev->useShadedFills();
666 }
667
668 GBool BitmapOutputDev::useDrawForm()
669 {
670     boolpolydev->useDrawForm();
671     booltextdev->useDrawForm();
672     clip0dev->useDrawForm();
673     clip1dev->useDrawForm();
674     return rgbdev->useDrawForm();
675 }
676
677 GBool BitmapOutputDev::interpretType3Chars()
678 {
679     boolpolydev->interpretType3Chars();
680     booltextdev->interpretType3Chars();
681     clip0dev->interpretType3Chars();
682     clip1dev->interpretType3Chars();
683     return rgbdev->interpretType3Chars();
684 }
685
686 GBool BitmapOutputDev::needNonText() 
687 {
688     boolpolydev->needNonText();
689     booltextdev->needNonText();
690     clip0dev->needNonText();
691     clip1dev->needNonText();
692     return rgbdev->needNonText();
693 }
694 /*GBool BitmapOutputDev::checkPageSlice(Page *page, double hDPI, double vDPI,
695                            int rotate, GBool useMediaBox, GBool crop,
696                            int sliceX, int sliceY, int sliceW, int sliceH,
697                            GBool printing, Catalog *catalog,
698                            GBool (*abortCheckCbk)(void *data),
699                            void *abortCheckCbkData)
700 {
701     return gTrue;
702 }*/
703 void BitmapOutputDev::setDefaultCTM(double *ctm) 
704 {
705     boolpolydev->setDefaultCTM(ctm);
706     booltextdev->setDefaultCTM(ctm);
707     rgbdev->setDefaultCTM(ctm);
708     clip0dev->setDefaultCTM(ctm);
709     clip1dev->setDefaultCTM(ctm);
710     gfxdev->setDefaultCTM(ctm);
711 }
712 void BitmapOutputDev::saveState(GfxState *state) 
713 {
714     boolpolydev->saveState(state);
715     booltextdev->saveState(state);
716     rgbdev->saveState(state);
717     clip0dev->saveState(state);
718     clip1dev->saveState(state);
719
720     /*ClipState*cstate = new ClipState();
721     cstate->next = this->clipstates;
722     this->clipstates = cstate;*/
723 }
724 void BitmapOutputDev::restoreState(GfxState *state) 
725 {
726     boolpolydev->restoreState(state);
727     booltextdev->restoreState(state);
728     rgbdev->restoreState(state);
729     clip0dev->restoreState(state);
730     clip1dev->restoreState(state);
731
732     /*if(this->clipstates) {
733         ClipState*old = this->clipstates;
734         if(old->written) {
735             gfxdev->restoreState(state);
736         }
737         this->clipstates = this->clipstates->next;
738         delete(old);
739     } else {
740         msg("<error> invalid restoreState()");
741     }*/
742 }
743 void BitmapOutputDev::updateAll(GfxState *state)
744 {
745     boolpolydev->updateAll(state);
746     booltextdev->updateAll(state);
747     rgbdev->updateAll(state);
748     clip0dev->updateAll(state);
749     clip1dev->updateAll(state);
750     gfxdev->updateAll(state);
751 }
752 void BitmapOutputDev::updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32)
753 {
754     boolpolydev->updateCTM(state,m11,m12,m21,m22,m31,m32);
755     booltextdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
756     rgbdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
757     clip0dev->updateCTM(state,m11,m12,m21,m22,m31,m32);
758     clip1dev->updateCTM(state,m11,m12,m21,m22,m31,m32);
759     gfxdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
760 }
761 void BitmapOutputDev::updateLineDash(GfxState *state)
762 {
763     boolpolydev->updateLineDash(state);
764     booltextdev->updateLineDash(state);
765     rgbdev->updateLineDash(state);
766     clip0dev->updateLineDash(state);
767     clip1dev->updateLineDash(state);
768     gfxdev->updateLineDash(state);
769 }
770 void BitmapOutputDev::updateFlatness(GfxState *state)
771 {
772     boolpolydev->updateFlatness(state);
773     booltextdev->updateFlatness(state);
774     rgbdev->updateFlatness(state);
775     clip0dev->updateFlatness(state);
776     clip1dev->updateFlatness(state);
777     gfxdev->updateFlatness(state);
778 }
779 void BitmapOutputDev::updateLineJoin(GfxState *state)
780 {
781     boolpolydev->updateLineJoin(state);
782     booltextdev->updateLineJoin(state);
783     rgbdev->updateLineJoin(state);
784     clip0dev->updateLineJoin(state);
785     clip1dev->updateLineJoin(state);
786     gfxdev->updateLineJoin(state);
787 }
788 void BitmapOutputDev::updateLineCap(GfxState *state)
789 {
790     boolpolydev->updateLineCap(state);
791     booltextdev->updateLineCap(state);
792     rgbdev->updateLineCap(state);
793     clip0dev->updateLineCap(state);
794     clip1dev->updateLineCap(state);
795     gfxdev->updateLineCap(state);
796 }
797 void BitmapOutputDev::updateMiterLimit(GfxState *state)
798 {
799     boolpolydev->updateMiterLimit(state);
800     booltextdev->updateMiterLimit(state);
801     rgbdev->updateMiterLimit(state);
802     clip0dev->updateMiterLimit(state);
803     clip1dev->updateMiterLimit(state);
804     gfxdev->updateMiterLimit(state);
805 }
806 void BitmapOutputDev::updateLineWidth(GfxState *state)
807 {
808     boolpolydev->updateLineWidth(state);
809     booltextdev->updateLineWidth(state);
810     rgbdev->updateLineWidth(state);
811     clip0dev->updateLineWidth(state);
812     clip1dev->updateLineWidth(state);
813     gfxdev->updateLineWidth(state);
814 }
815 void BitmapOutputDev::updateStrokeAdjust(GfxState *state)
816 {
817     boolpolydev->updateStrokeAdjust(state);
818     booltextdev->updateStrokeAdjust(state);
819     rgbdev->updateStrokeAdjust(state);
820     clip0dev->updateStrokeAdjust(state);
821     clip1dev->updateStrokeAdjust(state);
822     gfxdev->updateStrokeAdjust(state);
823 }
824 void BitmapOutputDev::updateFillColorSpace(GfxState *state)
825 {
826     boolpolydev->updateFillColorSpace(state);
827     booltextdev->updateFillColorSpace(state);
828     rgbdev->updateFillColorSpace(state);
829     clip0dev->updateFillColorSpace(state);
830     clip1dev->updateFillColorSpace(state);
831     gfxdev->updateFillColorSpace(state);
832 }
833 void BitmapOutputDev::updateStrokeColorSpace(GfxState *state)
834 {
835     boolpolydev->updateStrokeColorSpace(state);
836     booltextdev->updateStrokeColorSpace(state);
837     rgbdev->updateStrokeColorSpace(state);
838     clip0dev->updateStrokeColorSpace(state);
839     clip1dev->updateStrokeColorSpace(state);
840     gfxdev->updateStrokeColorSpace(state);
841 }
842 void BitmapOutputDev::updateFillColor(GfxState *state)
843 {
844     boolpolydev->updateFillColor(state);
845     booltextdev->updateFillColor(state);
846     rgbdev->updateFillColor(state);
847     clip0dev->updateFillColor(state);
848     clip1dev->updateFillColor(state);
849     gfxdev->updateFillColor(state);
850 }
851 void BitmapOutputDev::updateStrokeColor(GfxState *state)
852 {
853     boolpolydev->updateStrokeColor(state);
854     booltextdev->updateStrokeColor(state);
855     rgbdev->updateStrokeColor(state);
856     clip0dev->updateStrokeColor(state);
857     clip1dev->updateStrokeColor(state);
858     gfxdev->updateStrokeColor(state);
859 }
860 void BitmapOutputDev::updateBlendMode(GfxState *state)
861 {
862     boolpolydev->updateBlendMode(state);
863     booltextdev->updateBlendMode(state);
864     rgbdev->updateBlendMode(state);
865     clip0dev->updateBlendMode(state);
866     clip1dev->updateBlendMode(state);
867     gfxdev->updateBlendMode(state);
868 }
869 void BitmapOutputDev::updateFillOpacity(GfxState *state)
870 {
871     boolpolydev->updateFillOpacity(state);
872     booltextdev->updateFillOpacity(state);
873     rgbdev->updateFillOpacity(state);
874     clip0dev->updateFillOpacity(state);
875     clip1dev->updateFillOpacity(state);
876     gfxdev->updateFillOpacity(state);
877 }
878 void BitmapOutputDev::updateStrokeOpacity(GfxState *state)
879 {
880     boolpolydev->updateStrokeOpacity(state);
881     booltextdev->updateStrokeOpacity(state);
882     rgbdev->updateStrokeOpacity(state);
883     clip0dev->updateStrokeOpacity(state);
884     clip1dev->updateStrokeOpacity(state);
885     gfxdev->updateStrokeOpacity(state);
886 }
887 void BitmapOutputDev::updateFillOverprint(GfxState *state)
888 {
889     boolpolydev->updateFillOverprint(state);
890     booltextdev->updateFillOverprint(state);
891     rgbdev->updateFillOverprint(state);
892     clip0dev->updateFillOverprint(state);
893     clip1dev->updateFillOverprint(state);
894     gfxdev->updateFillOverprint(state);
895 }
896 void BitmapOutputDev::updateStrokeOverprint(GfxState *state)
897 {
898     boolpolydev->updateStrokeOverprint(state);
899     booltextdev->updateStrokeOverprint(state);
900     rgbdev->updateStrokeOverprint(state);
901     clip0dev->updateStrokeOverprint(state);
902     clip1dev->updateStrokeOverprint(state);
903     gfxdev->updateStrokeOverprint(state);
904 }
905 void BitmapOutputDev::updateTransfer(GfxState *state)
906 {
907     boolpolydev->updateTransfer(state);
908     booltextdev->updateTransfer(state);
909     rgbdev->updateTransfer(state);
910     clip0dev->updateTransfer(state);
911     clip1dev->updateTransfer(state);
912     gfxdev->updateTransfer(state);
913 }
914 void BitmapOutputDev::updateFont(GfxState *state)
915 {
916     boolpolydev->updateFont(state);
917     booltextdev->updateFont(state);
918     rgbdev->updateFont(state);
919     clip0dev->updateFont(state);
920     clip1dev->updateFont(state);
921     gfxdev->updateFont(state);
922 }
923 void BitmapOutputDev::updateTextMat(GfxState *state)
924 {
925     boolpolydev->updateTextMat(state);
926     booltextdev->updateTextMat(state);
927     rgbdev->updateTextMat(state);
928     clip0dev->updateTextMat(state);
929     clip1dev->updateTextMat(state);
930     gfxdev->updateTextMat(state);
931 }
932 void BitmapOutputDev::updateCharSpace(GfxState *state)
933 {
934     boolpolydev->updateCharSpace(state);
935     booltextdev->updateCharSpace(state);
936     rgbdev->updateCharSpace(state);
937     clip0dev->updateCharSpace(state);
938     clip1dev->updateCharSpace(state);
939     gfxdev->updateCharSpace(state);
940 }
941 void BitmapOutputDev::updateRender(GfxState *state)
942 {
943     boolpolydev->updateRender(state);
944     booltextdev->updateRender(state);
945     rgbdev->updateRender(state);
946     clip0dev->updateRender(state);
947     clip1dev->updateRender(state);
948     gfxdev->updateRender(state);
949 }
950 void BitmapOutputDev::updateRise(GfxState *state)
951 {
952     boolpolydev->updateRise(state);
953     booltextdev->updateRise(state);
954     rgbdev->updateRise(state);
955     clip0dev->updateRise(state);
956     clip1dev->updateRise(state);
957     gfxdev->updateRise(state);
958 }
959 void BitmapOutputDev::updateWordSpace(GfxState *state)
960 {
961     boolpolydev->updateWordSpace(state);
962     booltextdev->updateWordSpace(state);
963     rgbdev->updateWordSpace(state);
964     clip0dev->updateWordSpace(state);
965     clip1dev->updateWordSpace(state);
966     gfxdev->updateWordSpace(state);
967 }
968 void BitmapOutputDev::updateHorizScaling(GfxState *state)
969 {
970     boolpolydev->updateHorizScaling(state);
971     booltextdev->updateHorizScaling(state);
972     rgbdev->updateHorizScaling(state);
973     clip0dev->updateHorizScaling(state);
974     clip1dev->updateHorizScaling(state);
975     gfxdev->updateHorizScaling(state);
976 }
977 void BitmapOutputDev::updateTextPos(GfxState *state)
978 {
979     boolpolydev->updateTextPos(state);
980     booltextdev->updateTextPos(state);
981     rgbdev->updateTextPos(state);
982     clip0dev->updateTextPos(state);
983     clip1dev->updateTextPos(state);
984     gfxdev->updateTextPos(state);
985 }
986 void BitmapOutputDev::updateTextShift(GfxState *state, double shift)
987 {
988     boolpolydev->updateTextShift(state, shift);
989     booltextdev->updateTextShift(state, shift);
990     rgbdev->updateTextShift(state, shift);
991     clip0dev->updateTextShift(state, shift);
992     clip1dev->updateTextShift(state, shift);
993     gfxdev->updateTextShift(state, shift);
994 }
995
996 void BitmapOutputDev::stroke(GfxState *state)
997 {
998     msg("<debug> stroke");
999     boolpolydev->stroke(state);
1000     checkNewBitmap();
1001     rgbdev->stroke(state);
1002 }
1003 void BitmapOutputDev::fill(GfxState *state)
1004 {
1005     msg("<debug> fill");
1006     boolpolydev->fill(state);
1007     checkNewBitmap();
1008     rgbdev->fill(state);
1009 }
1010 void BitmapOutputDev::eoFill(GfxState *state)
1011 {
1012     msg("<debug> eoFill");
1013     boolpolydev->eoFill(state);
1014     checkNewBitmap();
1015     rgbdev->eoFill(state);
1016 }
1017 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
1018 void BitmapOutputDev::tilingPatternFill(GfxState *state, Object *str,
1019                                int paintType, Dict *resDict,
1020                                double *mat, double *bbox,
1021                                int x0, int y0, int x1, int y1,
1022                                double xStep, double yStep)
1023 {
1024     msg("<debug> tilingPatternFill");
1025     boolpolydev->tilingPatternFill(state, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
1026     checkNewBitmap();
1027     rgbdev->tilingPatternFill(state, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
1028 }
1029 #else
1030 void BitmapOutputDev::tilingPatternFill(GfxState *state, Gfx *gfx, Object *str,
1031                                int paintType, Dict *resDict,
1032                                double *mat, double *bbox,
1033                                int x0, int y0, int x1, int y1,
1034                                double xStep, double yStep) 
1035 {
1036     msg("<debug> tilingPatternFill");
1037     boolpolydev->tilingPatternFill(state, gfx, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
1038     checkNewBitmap();
1039     rgbdev->tilingPatternFill(state, gfx, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
1040 }
1041 #endif
1042
1043 GBool BitmapOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading) 
1044 {
1045     msg("<debug> functionShadedFill");
1046     boolpolydev->functionShadedFill(state, shading);
1047     checkNewBitmap();
1048     return rgbdev->functionShadedFill(state, shading);
1049 }
1050 GBool BitmapOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading)
1051 {
1052     msg("<debug> axialShadedFill");
1053     boolpolydev->axialShadedFill(state, shading);
1054     checkNewBitmap();
1055     return rgbdev->axialShadedFill(state, shading);
1056 }
1057 GBool BitmapOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading)
1058 {
1059     msg("<debug> radialShadedFill");
1060     boolpolydev->radialShadedFill(state, shading);
1061     checkNewBitmap();
1062     return rgbdev->radialShadedFill(state, shading);
1063 }
1064
1065 SplashColor black = {0,0,0};
1066 SplashColor white = {255,255,255};
1067
1068 void BitmapOutputDev::clip(GfxState *state)
1069 {
1070     msg("<debug> clip");
1071     boolpolydev->clip(state);
1072     booltextdev->clip(state);
1073     rgbdev->clip(state);
1074     clip1dev->clip(state);
1075 }
1076 void BitmapOutputDev::eoClip(GfxState *state)
1077 {
1078     msg("<debug> eoClip");
1079     boolpolydev->eoClip(state);
1080     booltextdev->eoClip(state);
1081     rgbdev->eoClip(state);
1082     clip1dev->eoClip(state);
1083 }
1084 void BitmapOutputDev::clipToStrokePath(GfxState *state)
1085 {
1086     msg("<debug> clipToStrokePath");
1087     boolpolydev->clipToStrokePath(state);
1088     booltextdev->clipToStrokePath(state);
1089     rgbdev->clipToStrokePath(state);
1090     clip1dev->clipToStrokePath(state);
1091 }
1092
1093 void BitmapOutputDev::beginStringOp(GfxState *state)
1094 {
1095     msg("<debug> beginStringOp");
1096     clip0dev->beginStringOp(state);
1097     clip1dev->beginStringOp(state);
1098     booltextdev->beginStringOp(state);
1099     gfxdev->beginStringOp(state);
1100 }
1101 void BitmapOutputDev::endStringOp(GfxState *state)
1102 {
1103     msg("<debug> endStringOp");
1104     clip0dev->endStringOp(state);
1105     clip1dev->endStringOp(state);
1106     booltextdev->endStringOp(state);
1107     checkNewText(0,0,0,0);
1108     gfxdev->endStringOp(state);
1109 }
1110 void BitmapOutputDev::beginString(GfxState *state, GString *s)
1111 {
1112     msg("<debug> beginString");
1113     clip0dev->beginString(state, s);
1114     clip1dev->beginString(state, s);
1115     booltextdev->beginString(state, s);
1116     gfxdev->beginString(state, s);
1117 }
1118 void BitmapOutputDev::endString(GfxState *state)
1119 {
1120     msg("<debug> endString");
1121     clip0dev->endString(state);
1122     clip1dev->endString(state);
1123     booltextdev->endString(state);
1124     checkNewText(0,0,0,0);
1125     gfxdev->endString(state);
1126 }
1127
1128 void BitmapOutputDev::clearClips()
1129 {
1130     clearBooleanBitmap(clip0bitmap);
1131     clearBooleanBitmap(clip1bitmap);
1132 }
1133 void BitmapOutputDev::clearBoolPolyDev()
1134 {
1135     clearBooleanBitmap(boolpolybitmap);
1136 }
1137 void BitmapOutputDev::clearBoolTextDev()
1138 {
1139     clearBooleanBitmap(booltextbitmap);
1140 }
1141 void BitmapOutputDev::drawChar(GfxState *state, double x, double y,
1142                       double dx, double dy,
1143                       double originX, double originY,
1144                       CharCode code, int nBytes, Unicode *u, int uLen)
1145 {
1146     msg("<debug> drawChar");
1147     if(state->getRender()&RENDER_CLIP) {
1148         rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1149     } else {
1150         clearClips();
1151         clip0dev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1152         clip1dev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1153    
1154         /* calculate the bbox of this character */
1155         int x1 = (int)x, x2 = (int)x+1, y1 = (int)y, y2 = (int)y+1;
1156         SplashPath*path = clip0dev->getCurrentFont()->getGlyphPath(code);
1157         int t;
1158         for(t=0;t<path->getLength();t++) {
1159             double xx,yy;
1160             Guchar f;
1161             path->getPoint(t,&xx,&yy,&f);
1162             xx+=x;
1163             yy+=y;
1164             if(xx<x1) x1=(int)xx;
1165             if(yy<y1) y1=(int)yy;
1166             if(xx>x2) x2=(int)xx+1;
1167             if(yy>y2) y2=(int)yy+1;
1168         }
1169
1170         /* if this character is affected somehow by the various clippings (i.e., it looks
1171            different on a device without clipping), then draw it on the bitmap, not as
1172            text */
1173         if(clip0and1differ(x1,y1,x2,y2)) {
1174             msg("<verbose> Char %d is affected by clipping", code);
1175             boolpolydev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1176             checkNewBitmap();
1177             rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1178             if(config_extrafontdata) {
1179                 int oldrender = state->getRender();
1180                 state->setRender(3); //invisible
1181                 gfxdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1182                 state->setRender(oldrender);
1183             }
1184         } else {
1185             /* this char is not at all affected by clipping. 
1186                Now just dump out the bitmap we're currently working on, if necessary. */
1187             booltextdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1188             checkNewText(x1,y1,x2,y2);
1189             /* use polygonal output device to do the actual text handling */
1190             gfxdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1191         }
1192     }
1193 }
1194 void BitmapOutputDev::drawString(GfxState *state, GString *s)
1195 {
1196     msg("<error> internal error: drawString not implemented");
1197     return;
1198     clip0dev->drawString(state, s);
1199     clip1dev->drawString(state, s);
1200     booltextdev->drawString(state, s);
1201     gfxdev->drawString(state, s);
1202 }
1203 void BitmapOutputDev::endTextObject(GfxState *state)
1204 {
1205     msg("<debug> endTextObject");
1206     rgbdev->endTextObject(state);
1207     clip0dev->endTextObject(state);
1208     clip1dev->endTextObject(state);
1209     booltextdev->endTextObject(state);
1210     /* TODO: do this only if rendermode!=0 */
1211     checkNewText(0,0,0,0);
1212     gfxdev->endTextObject(state);
1213 }
1214
1215 /* TODO: these four operations below *should* do nothing, as type3
1216          chars are drawn using operations like fill() */
1217 GBool BitmapOutputDev::beginType3Char(GfxState *state, double x, double y,
1218                              double dx, double dy,
1219                              CharCode code, Unicode *u, int uLen)
1220 {
1221     msg("<debug> beginType3Char");
1222     /* call gfxdev so that it can generate "invisible" characters
1223        on top of the actual graphic content, for text extraction */
1224     return gfxdev->beginType3Char(state, x, y, dx, dy, code, u, uLen);
1225 }
1226 void BitmapOutputDev::type3D0(GfxState *state, double wx, double wy)
1227 {
1228     msg("<debug> type3D0");
1229     return gfxdev->type3D0(state, wx, wy);
1230 }
1231 void BitmapOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
1232 {
1233     msg("<debug> type3D1");
1234     return gfxdev->type3D1(state, wx, wy, llx, lly, urx, ury);
1235 }
1236 void BitmapOutputDev::endType3Char(GfxState *state)
1237 {
1238     msg("<debug> endType3Char");
1239     gfxdev->endType3Char(state);
1240 }
1241
1242 class CopyStream: public Object
1243 {
1244     Dict*dict;
1245     char*buf;
1246     MemStream*memstream;
1247     public:
1248     CopyStream(Stream*str, int len)
1249     {
1250         buf = 0;
1251         str->reset();
1252         if(len) {
1253             buf = (char*)malloc(len);
1254             int t;
1255             for (t=0; t<len; t++)
1256               buf[t] = str->getChar();
1257         }
1258         str->close();
1259         this->dict = str->getDict();
1260         this->memstream = new MemStream(buf, 0, len, this);
1261     }
1262     ~CopyStream() 
1263     {
1264         ::free(this->buf);this->buf = 0;
1265         delete this->memstream;
1266     }
1267     Dict* getDict() {return dict;}
1268     Stream* getStream() {return this->memstream;};
1269 };
1270
1271 void BitmapOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1272                            int width, int height, GBool invert,
1273                            GBool inlineImg)
1274 {
1275     msg("<debug> drawImageMask streamkind=%d", str->getKind());
1276     CopyStream*cpystr = 0;
1277     if(inlineImg) {
1278         cpystr = new CopyStream(str, height * ((width + 7) / 8));
1279         str = cpystr->getStream();
1280     }
1281     boolpolydev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
1282     checkNewBitmap();
1283     rgbdev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
1284     if(cpystr)
1285         delete cpystr;
1286 }
1287 void BitmapOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1288                        int width, int height, GfxImageColorMap *colorMap,
1289                        int *maskColors, GBool inlineImg)
1290 {
1291     msg("<debug> drawImage streamkind=%d", str->getKind());
1292     CopyStream*cpystr = 0;
1293     if(inlineImg) {
1294         cpystr = new CopyStream(str, height * ((width * colorMap->getNumPixelComps() * colorMap->getBits() + 7) / 8));
1295         str = cpystr->getStream();
1296     }
1297     boolpolydev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
1298     checkNewBitmap();
1299     rgbdev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
1300     if(cpystr)
1301         delete cpystr;
1302 }
1303 void BitmapOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
1304                              int width, int height,
1305                              GfxImageColorMap *colorMap,
1306                              Stream *maskStr, int maskWidth, int maskHeight,
1307                              GBool maskInvert)
1308 {
1309     msg("<debug> drawMaskedImage streamkind=%d", str->getKind());
1310     boolpolydev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
1311     checkNewBitmap();
1312     rgbdev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
1313 }
1314 void BitmapOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
1315                                  int width, int height,
1316                                  GfxImageColorMap *colorMap,
1317                                  Stream *maskStr,
1318                                  int maskWidth, int maskHeight,
1319                                  GfxImageColorMap *maskColorMap)
1320 {
1321     msg("<debug> drawSoftMaskedImage %dx%d (%dx%d) streamkind=%d", width, height, maskWidth, maskHeight, str->getKind());
1322     boolpolydev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
1323     checkNewBitmap();
1324     rgbdev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
1325 }
1326 void BitmapOutputDev::drawForm(Ref id)
1327 {
1328     msg("<debug> drawForm");
1329     boolpolydev->drawForm(id);
1330     checkNewBitmap();
1331     rgbdev->drawForm(id);
1332 }
1333
1334 void BitmapOutputDev::processLink(Link *link, Catalog *catalog)
1335 {
1336     msg("<debug> processLink");
1337     gfxdev->processLink(link, catalog);
1338 }
1339
1340 void BitmapOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
1341                                     GfxColorSpace *blendingColorSpace,
1342                                     GBool isolated, GBool knockout,
1343                                     GBool forSoftMask)
1344 {
1345     msg("<debug> beginTransparencyGroup");
1346 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
1347     GfxState*state1 = state->copy();
1348     GfxState*state2 = state->copy();
1349     state1->setPath(0);
1350     state1->setPath(state->getPath()->copy());
1351     state2->setPath(0);
1352     state2->setPath(state->getPath()->copy());
1353 #else
1354     GfxState*state1 = state->copy(gTrue);
1355     GfxState*state2 = state->copy(gTrue);
1356 #endif
1357     boolpolydev->beginTransparencyGroup(state1, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1358     rgbdev->beginTransparencyGroup(state2, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1359     clip1dev->beginTransparencyGroup(state, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1360     delete state1;
1361     delete state2;
1362 }
1363 void BitmapOutputDev::endTransparencyGroup(GfxState *state)
1364 {
1365     msg("<debug> endTransparencyGroup");
1366 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
1367     GfxState*state1 = state->copy();
1368     GfxState*state2 = state->copy();
1369     state1->setPath(0);
1370     state1->setPath(state->getPath()->copy());
1371     state2->setPath(0);
1372     state2->setPath(state->getPath()->copy());
1373 #else
1374     GfxState*state1 = state->copy(gTrue);
1375     GfxState*state2 = state->copy(gTrue);
1376 #endif
1377     boolpolydev->endTransparencyGroup(state1);
1378     checkNewBitmap();
1379     rgbdev->endTransparencyGroup(state2);
1380     delete state1;
1381     delete state2;
1382     clip1dev->endTransparencyGroup(state);
1383 }
1384 void BitmapOutputDev::paintTransparencyGroup(GfxState *state, double *bbox)
1385 {
1386     msg("<debug> paintTransparencyGroup");
1387     boolpolydev->paintTransparencyGroup(state,bbox);
1388     checkNewBitmap();
1389     rgbdev->paintTransparencyGroup(state,bbox);
1390     clip1dev->paintTransparencyGroup(state,bbox);
1391 }
1392 void BitmapOutputDev::setSoftMask(GfxState *state, double *bbox, GBool alpha, Function *transferFunc, GfxColor *backdropColor)
1393 {
1394     msg("<debug> setSoftMask");
1395     boolpolydev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1396     checkNewBitmap();
1397     rgbdev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1398     clip1dev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1399 }
1400 void BitmapOutputDev::clearSoftMask(GfxState *state)
1401 {
1402     msg("<debug> clearSoftMask");
1403     boolpolydev->clearSoftMask(state);
1404     checkNewBitmap();
1405     rgbdev->clearSoftMask(state);
1406     clip1dev->clearSoftMask(state);
1407 }