fixed double free in path handling
[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()
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()) {
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()) {
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()
413 {
414     if(clip0bitmap->getMode()==splashModeMono1) {
415         SplashBitmap*clip0 = clip0bitmap;
416         SplashBitmap*clip1 = clip1bitmap;
417         int width8 = (clip0->getWidth()+7)/8;
418         int height = clip0->getHeight();
419         return memcmp(clip0->getDataPtr(), clip1->getDataPtr(), width8*height);
420     } else {
421         SplashBitmap*clip0 = clip0bitmap;
422         SplashBitmap*clip1 = clip1bitmap;
423         int width = clip0->getAlphaRowSize();
424         int height = clip0->getHeight();
425         return memcmp(clip0->getAlphaPtr(), clip1->getAlphaPtr(), width*height);
426     }
427 }
428
429 static void clearBooleanBitmap(SplashBitmap*btm)
430 {
431     if(btm->getMode()==splashModeMono1) {
432         int width8 = (btm->getWidth()+7)/8;
433         int width = btm->getWidth();
434         int height = btm->getHeight();
435         memset(btm->getDataPtr(), 0, width8*height);
436     } else {
437         int width = btm->getAlphaRowSize();
438         int height = btm->getHeight();
439         memset(btm->getAlphaPtr(), 0, width*height);
440     }
441 }
442
443 GBool BitmapOutputDev::intersection()
444 {
445     SplashBitmap*boolpoly = boolpolybitmap;
446     SplashBitmap*booltext = booltextbitmap;
447         
448     if(boolpoly->getMode()==splashModeMono1) {
449         /* alternative implementation, using one bit per pixel-
450            would work if xpdf wouldn't try to dither everything */
451
452         Guchar*polypixels = boolpoly->getDataPtr();
453         Guchar*textpixels = booltext->getDataPtr();
454     
455         int width8 = (width+7)/8;
456         int height = boolpoly->getHeight();
457         
458         int t;
459         int len = height*width8;
460         unsigned int c=0;
461         if(len & (sizeof(unsigned int)-1)) {
462             Guchar c2=0;
463             for(t=0;t<len;t++) {
464                 c2 |= polypixels[t]&textpixels[t];
465             }
466             c = c2;
467         } else {
468             len /= sizeof(unsigned int);
469             for(t=0;t<len;t++) {
470                 c |= (((unsigned int*)polypixels)[t]) & (((unsigned int*)textpixels)[t]);
471             }
472         }
473         if(c)
474             /* if graphic data and the characters overlap, they have common bits */
475             return gTrue;
476         else
477             return gFalse;
478     } else {
479         Guchar*polypixels = boolpoly->getAlphaPtr();
480         Guchar*textpixels = booltext->getAlphaPtr();
481         
482         int width = boolpoly->getAlphaRowSize();
483         int height = boolpoly->getHeight();
484         
485         int t;
486         int len = height*width;
487         unsigned int c=0;
488         if(len & (sizeof(unsigned int)-1)) {
489             Guchar c2=0;
490             for(t=0;t<len;t++) {
491                 if(polypixels[t]&&textpixels[t])
492                     return gTrue;
493             }
494         } else {
495             len /= sizeof(unsigned int);
496             for(t=0;t<len;t++) {
497                 if((((unsigned int*)polypixels)[t]) & (((unsigned int*)textpixels)[t]))
498                     return gTrue;
499             }
500         }
501         return gFalse;
502     }
503 }
504
505
506 void BitmapOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
507 {
508     double x1,y1,x2,y2;
509     state->transform(crop_x1,crop_y1,&x1,&y1);
510     state->transform(crop_x2,crop_y2,&x2,&y2);
511     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
512     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
513     
514     this->movex = -(int)x1 - user_movex;
515     this->movey = -(int)y1 - user_movey;
516     
517     if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
518         x1 = user_clipx1;
519         x2 = user_clipx2;
520         y1 = user_clipy1;
521         y2 = user_clipy2;
522     }
523     this->width = (int)(x2-x1);
524     this->height = (int)(y2-y1);
525
526     msg("<debug> startPage");
527     rgbdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
528     boolpolydev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
529     booltextdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
530     clip0dev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
531     clip1dev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
532     gfxdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
533
534     boolpolybitmap = boolpolydev->getBitmap();
535     booltextbitmap = booltextdev->getBitmap();
536     clip0bitmap = clip0dev->getBitmap();
537     clip1bitmap = clip1dev->getBitmap();
538     rgbbitmap = rgbdev->getBitmap();
539     
540     flushText(); // write out the initial clipping rectangle
541
542     /* just in case any device did draw a white background rectangle 
543        into the device */
544     clearBoolTextDev();
545     clearBoolPolyDev();
546
547     this->layerstate = STATE_PARALLEL;
548     this->emptypage = 1;
549     msg("<debug> startPage done");
550 }
551
552 void BitmapOutputDev::endPage()
553 {
554     msg("<verbose> endPage (BitmapOutputDev)");
555     gfxdev->endPage();
556    
557     if(layerstate == STATE_BITMAP_IS_ABOVE) {
558         this->flushText();
559         this->flushBitmap();
560     } else {
561         this->flushBitmap();
562         this->flushText();
563     }
564
565     /* splash will now destroy alpha, and paint the 
566        background color into the "holes" in the bitmap */
567     boolpolydev->endPage();
568     booltextdev->endPage();
569     rgbdev->endPage();
570     clip0dev->endPage();
571     clip1dev->endPage();
572
573     /* notice: we're not fully done yet with this page- there might still be 
574        a few calls to drawLink() yet to come */
575 }
576
577 GBool BitmapOutputDev::upsideDown()
578 {
579     boolpolydev->upsideDown();
580     booltextdev->upsideDown();
581     clip0dev->upsideDown();
582     clip1dev->upsideDown();
583     return rgbdev->upsideDown();
584 }
585
586 GBool BitmapOutputDev::useDrawChar()
587 {
588     boolpolydev->useDrawChar();
589     booltextdev->useDrawChar();
590     clip0dev->useDrawChar();
591     clip1dev->useDrawChar();
592     return rgbdev->useDrawChar();
593 }
594
595 GBool BitmapOutputDev::useTilingPatternFill()
596 {
597     boolpolydev->useTilingPatternFill();
598     booltextdev->useTilingPatternFill();
599     clip0dev->useTilingPatternFill();
600     clip1dev->useTilingPatternFill();
601     return rgbdev->useTilingPatternFill();
602 }
603
604 GBool BitmapOutputDev::useShadedFills()
605 {
606     boolpolydev->useShadedFills();
607     booltextdev->useShadedFills();
608     clip0dev->useShadedFills();
609     clip1dev->useShadedFills();
610     return rgbdev->useShadedFills();
611 }
612
613 GBool BitmapOutputDev::useDrawForm()
614 {
615     boolpolydev->useDrawForm();
616     booltextdev->useDrawForm();
617     clip0dev->useDrawForm();
618     clip1dev->useDrawForm();
619     return rgbdev->useDrawForm();
620 }
621
622 GBool BitmapOutputDev::interpretType3Chars()
623 {
624     boolpolydev->interpretType3Chars();
625     booltextdev->interpretType3Chars();
626     clip0dev->interpretType3Chars();
627     clip1dev->interpretType3Chars();
628     return rgbdev->interpretType3Chars();
629 }
630
631 GBool BitmapOutputDev::needNonText() 
632 {
633     boolpolydev->needNonText();
634     booltextdev->needNonText();
635     clip0dev->needNonText();
636     clip1dev->needNonText();
637     return rgbdev->needNonText();
638 }
639 /*GBool BitmapOutputDev::checkPageSlice(Page *page, double hDPI, double vDPI,
640                            int rotate, GBool useMediaBox, GBool crop,
641                            int sliceX, int sliceY, int sliceW, int sliceH,
642                            GBool printing, Catalog *catalog,
643                            GBool (*abortCheckCbk)(void *data),
644                            void *abortCheckCbkData)
645 {
646     return gTrue;
647 }*/
648 void BitmapOutputDev::setDefaultCTM(double *ctm) 
649 {
650     boolpolydev->setDefaultCTM(ctm);
651     booltextdev->setDefaultCTM(ctm);
652     rgbdev->setDefaultCTM(ctm);
653     clip0dev->setDefaultCTM(ctm);
654     clip1dev->setDefaultCTM(ctm);
655     gfxdev->setDefaultCTM(ctm);
656 }
657 void BitmapOutputDev::saveState(GfxState *state) 
658 {
659     boolpolydev->saveState(state);
660     booltextdev->saveState(state);
661     rgbdev->saveState(state);
662     clip0dev->saveState(state);
663     clip1dev->saveState(state);
664
665     /*ClipState*cstate = new ClipState();
666     cstate->next = this->clipstates;
667     this->clipstates = cstate;*/
668 }
669 void BitmapOutputDev::restoreState(GfxState *state) 
670 {
671     boolpolydev->restoreState(state);
672     booltextdev->restoreState(state);
673     rgbdev->restoreState(state);
674     clip0dev->restoreState(state);
675     clip1dev->restoreState(state);
676
677     /*if(this->clipstates) {
678         ClipState*old = this->clipstates;
679         if(old->written) {
680             gfxdev->restoreState(state);
681         }
682         this->clipstates = this->clipstates->next;
683         delete(old);
684     } else {
685         msg("<error> invalid restoreState()");
686     }*/
687 }
688 void BitmapOutputDev::updateAll(GfxState *state)
689 {
690     boolpolydev->updateAll(state);
691     booltextdev->updateAll(state);
692     rgbdev->updateAll(state);
693     clip0dev->updateAll(state);
694     clip1dev->updateAll(state);
695     gfxdev->updateAll(state);
696 }
697 void BitmapOutputDev::updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32)
698 {
699     boolpolydev->updateCTM(state,m11,m12,m21,m22,m31,m32);
700     booltextdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
701     rgbdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
702     clip0dev->updateCTM(state,m11,m12,m21,m22,m31,m32);
703     clip1dev->updateCTM(state,m11,m12,m21,m22,m31,m32);
704     gfxdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
705 }
706 void BitmapOutputDev::updateLineDash(GfxState *state)
707 {
708     boolpolydev->updateLineDash(state);
709     booltextdev->updateLineDash(state);
710     rgbdev->updateLineDash(state);
711     clip0dev->updateLineDash(state);
712     clip1dev->updateLineDash(state);
713     gfxdev->updateLineDash(state);
714 }
715 void BitmapOutputDev::updateFlatness(GfxState *state)
716 {
717     boolpolydev->updateFlatness(state);
718     booltextdev->updateFlatness(state);
719     rgbdev->updateFlatness(state);
720     clip0dev->updateFlatness(state);
721     clip1dev->updateFlatness(state);
722     gfxdev->updateFlatness(state);
723 }
724 void BitmapOutputDev::updateLineJoin(GfxState *state)
725 {
726     boolpolydev->updateLineJoin(state);
727     booltextdev->updateLineJoin(state);
728     rgbdev->updateLineJoin(state);
729     clip0dev->updateLineJoin(state);
730     clip1dev->updateLineJoin(state);
731     gfxdev->updateLineJoin(state);
732 }
733 void BitmapOutputDev::updateLineCap(GfxState *state)
734 {
735     boolpolydev->updateLineCap(state);
736     booltextdev->updateLineCap(state);
737     rgbdev->updateLineCap(state);
738     clip0dev->updateLineCap(state);
739     clip1dev->updateLineCap(state);
740     gfxdev->updateLineCap(state);
741 }
742 void BitmapOutputDev::updateMiterLimit(GfxState *state)
743 {
744     boolpolydev->updateMiterLimit(state);
745     booltextdev->updateMiterLimit(state);
746     rgbdev->updateMiterLimit(state);
747     clip0dev->updateMiterLimit(state);
748     clip1dev->updateMiterLimit(state);
749     gfxdev->updateMiterLimit(state);
750 }
751 void BitmapOutputDev::updateLineWidth(GfxState *state)
752 {
753     boolpolydev->updateLineWidth(state);
754     booltextdev->updateLineWidth(state);
755     rgbdev->updateLineWidth(state);
756     clip0dev->updateLineWidth(state);
757     clip1dev->updateLineWidth(state);
758     gfxdev->updateLineWidth(state);
759 }
760 void BitmapOutputDev::updateStrokeAdjust(GfxState *state)
761 {
762     boolpolydev->updateStrokeAdjust(state);
763     booltextdev->updateStrokeAdjust(state);
764     rgbdev->updateStrokeAdjust(state);
765     clip0dev->updateStrokeAdjust(state);
766     clip1dev->updateStrokeAdjust(state);
767     gfxdev->updateStrokeAdjust(state);
768 }
769 void BitmapOutputDev::updateFillColorSpace(GfxState *state)
770 {
771     boolpolydev->updateFillColorSpace(state);
772     booltextdev->updateFillColorSpace(state);
773     rgbdev->updateFillColorSpace(state);
774     clip0dev->updateFillColorSpace(state);
775     clip1dev->updateFillColorSpace(state);
776     gfxdev->updateFillColorSpace(state);
777 }
778 void BitmapOutputDev::updateStrokeColorSpace(GfxState *state)
779 {
780     boolpolydev->updateStrokeColorSpace(state);
781     booltextdev->updateStrokeColorSpace(state);
782     rgbdev->updateStrokeColorSpace(state);
783     clip0dev->updateStrokeColorSpace(state);
784     clip1dev->updateStrokeColorSpace(state);
785     gfxdev->updateStrokeColorSpace(state);
786 }
787 void BitmapOutputDev::updateFillColor(GfxState *state)
788 {
789     boolpolydev->updateFillColor(state);
790     booltextdev->updateFillColor(state);
791     rgbdev->updateFillColor(state);
792     clip0dev->updateFillColor(state);
793     clip1dev->updateFillColor(state);
794     gfxdev->updateFillColor(state);
795 }
796 void BitmapOutputDev::updateStrokeColor(GfxState *state)
797 {
798     boolpolydev->updateStrokeColor(state);
799     booltextdev->updateStrokeColor(state);
800     rgbdev->updateStrokeColor(state);
801     clip0dev->updateStrokeColor(state);
802     clip1dev->updateStrokeColor(state);
803     gfxdev->updateStrokeColor(state);
804 }
805 void BitmapOutputDev::updateBlendMode(GfxState *state)
806 {
807     boolpolydev->updateBlendMode(state);
808     booltextdev->updateBlendMode(state);
809     rgbdev->updateBlendMode(state);
810     clip0dev->updateBlendMode(state);
811     clip1dev->updateBlendMode(state);
812     gfxdev->updateBlendMode(state);
813 }
814 void BitmapOutputDev::updateFillOpacity(GfxState *state)
815 {
816     boolpolydev->updateFillOpacity(state);
817     booltextdev->updateFillOpacity(state);
818     rgbdev->updateFillOpacity(state);
819     clip0dev->updateFillOpacity(state);
820     clip1dev->updateFillOpacity(state);
821     gfxdev->updateFillOpacity(state);
822 }
823 void BitmapOutputDev::updateStrokeOpacity(GfxState *state)
824 {
825     boolpolydev->updateStrokeOpacity(state);
826     booltextdev->updateStrokeOpacity(state);
827     rgbdev->updateStrokeOpacity(state);
828     clip0dev->updateStrokeOpacity(state);
829     clip1dev->updateStrokeOpacity(state);
830     gfxdev->updateStrokeOpacity(state);
831 }
832 void BitmapOutputDev::updateFillOverprint(GfxState *state)
833 {
834     boolpolydev->updateFillOverprint(state);
835     booltextdev->updateFillOverprint(state);
836     rgbdev->updateFillOverprint(state);
837     clip0dev->updateFillOverprint(state);
838     clip1dev->updateFillOverprint(state);
839     gfxdev->updateFillOverprint(state);
840 }
841 void BitmapOutputDev::updateStrokeOverprint(GfxState *state)
842 {
843     boolpolydev->updateStrokeOverprint(state);
844     booltextdev->updateStrokeOverprint(state);
845     rgbdev->updateStrokeOverprint(state);
846     clip0dev->updateStrokeOverprint(state);
847     clip1dev->updateStrokeOverprint(state);
848     gfxdev->updateStrokeOverprint(state);
849 }
850 void BitmapOutputDev::updateTransfer(GfxState *state)
851 {
852     boolpolydev->updateTransfer(state);
853     booltextdev->updateTransfer(state);
854     rgbdev->updateTransfer(state);
855     clip0dev->updateTransfer(state);
856     clip1dev->updateTransfer(state);
857     gfxdev->updateTransfer(state);
858 }
859 void BitmapOutputDev::updateFont(GfxState *state)
860 {
861     boolpolydev->updateFont(state);
862     booltextdev->updateFont(state);
863     rgbdev->updateFont(state);
864     clip0dev->updateFont(state);
865     clip1dev->updateFont(state);
866     gfxdev->updateFont(state);
867 }
868 void BitmapOutputDev::updateTextMat(GfxState *state)
869 {
870     boolpolydev->updateTextMat(state);
871     booltextdev->updateTextMat(state);
872     rgbdev->updateTextMat(state);
873     clip0dev->updateTextMat(state);
874     clip1dev->updateTextMat(state);
875     gfxdev->updateTextMat(state);
876 }
877 void BitmapOutputDev::updateCharSpace(GfxState *state)
878 {
879     boolpolydev->updateCharSpace(state);
880     booltextdev->updateCharSpace(state);
881     rgbdev->updateCharSpace(state);
882     clip0dev->updateCharSpace(state);
883     clip1dev->updateCharSpace(state);
884     gfxdev->updateCharSpace(state);
885 }
886 void BitmapOutputDev::updateRender(GfxState *state)
887 {
888     boolpolydev->updateRender(state);
889     booltextdev->updateRender(state);
890     rgbdev->updateRender(state);
891     clip0dev->updateRender(state);
892     clip1dev->updateRender(state);
893     gfxdev->updateRender(state);
894 }
895 void BitmapOutputDev::updateRise(GfxState *state)
896 {
897     boolpolydev->updateRise(state);
898     booltextdev->updateRise(state);
899     rgbdev->updateRise(state);
900     clip0dev->updateRise(state);
901     clip1dev->updateRise(state);
902     gfxdev->updateRise(state);
903 }
904 void BitmapOutputDev::updateWordSpace(GfxState *state)
905 {
906     boolpolydev->updateWordSpace(state);
907     booltextdev->updateWordSpace(state);
908     rgbdev->updateWordSpace(state);
909     clip0dev->updateWordSpace(state);
910     clip1dev->updateWordSpace(state);
911     gfxdev->updateWordSpace(state);
912 }
913 void BitmapOutputDev::updateHorizScaling(GfxState *state)
914 {
915     boolpolydev->updateHorizScaling(state);
916     booltextdev->updateHorizScaling(state);
917     rgbdev->updateHorizScaling(state);
918     clip0dev->updateHorizScaling(state);
919     clip1dev->updateHorizScaling(state);
920     gfxdev->updateHorizScaling(state);
921 }
922 void BitmapOutputDev::updateTextPos(GfxState *state)
923 {
924     boolpolydev->updateTextPos(state);
925     booltextdev->updateTextPos(state);
926     rgbdev->updateTextPos(state);
927     clip0dev->updateTextPos(state);
928     clip1dev->updateTextPos(state);
929     gfxdev->updateTextPos(state);
930 }
931 void BitmapOutputDev::updateTextShift(GfxState *state, double shift)
932 {
933     boolpolydev->updateTextShift(state, shift);
934     booltextdev->updateTextShift(state, shift);
935     rgbdev->updateTextShift(state, shift);
936     clip0dev->updateTextShift(state, shift);
937     clip1dev->updateTextShift(state, shift);
938     gfxdev->updateTextShift(state, shift);
939 }
940
941 void BitmapOutputDev::stroke(GfxState *state)
942 {
943     msg("<debug> stroke");
944     boolpolydev->stroke(state);
945     checkNewBitmap();
946     rgbdev->stroke(state);
947 }
948 void BitmapOutputDev::fill(GfxState *state)
949 {
950     msg("<debug> fill");
951     boolpolydev->fill(state);
952     checkNewBitmap();
953     rgbdev->fill(state);
954 }
955 void BitmapOutputDev::eoFill(GfxState *state)
956 {
957     msg("<debug> eoFill");
958     boolpolydev->eoFill(state);
959     checkNewBitmap();
960     rgbdev->eoFill(state);
961 }
962 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
963 void BitmapOutputDev::tilingPatternFill(GfxState *state, Object *str,
964                                int paintType, Dict *resDict,
965                                double *mat, double *bbox,
966                                int x0, int y0, int x1, int y1,
967                                double xStep, double yStep)
968 {
969     msg("<debug> tilingPatternFill");
970     boolpolydev->tilingPatternFill(state, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
971     checkNewBitmap();
972     rgbdev->tilingPatternFill(state, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
973 }
974 #else
975 void BitmapOutputDev::tilingPatternFill(GfxState *state, Gfx *gfx, Object *str,
976                                int paintType, Dict *resDict,
977                                double *mat, double *bbox,
978                                int x0, int y0, int x1, int y1,
979                                double xStep, double yStep) 
980 {
981     msg("<debug> tilingPatternFill");
982     boolpolydev->tilingPatternFill(state, gfx, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
983     checkNewBitmap();
984     rgbdev->tilingPatternFill(state, gfx, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
985 }
986 #endif
987
988 GBool BitmapOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading) 
989 {
990     msg("<debug> functionShadedFill");
991     boolpolydev->functionShadedFill(state, shading);
992     checkNewBitmap();
993     return rgbdev->functionShadedFill(state, shading);
994 }
995 GBool BitmapOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading)
996 {
997     msg("<debug> axialShadedFill");
998     boolpolydev->axialShadedFill(state, shading);
999     checkNewBitmap();
1000     return rgbdev->axialShadedFill(state, shading);
1001 }
1002 GBool BitmapOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading)
1003 {
1004     msg("<debug> radialShadedFill");
1005     boolpolydev->radialShadedFill(state, shading);
1006     checkNewBitmap();
1007     return rgbdev->radialShadedFill(state, shading);
1008 }
1009
1010 SplashColor black = {0,0,0};
1011 SplashColor white = {255,255,255};
1012
1013 void BitmapOutputDev::clip(GfxState *state)
1014 {
1015     msg("<debug> clip");
1016     boolpolydev->clip(state);
1017     booltextdev->clip(state);
1018     rgbdev->clip(state);
1019     clip1dev->clip(state);
1020 }
1021 void BitmapOutputDev::eoClip(GfxState *state)
1022 {
1023     msg("<debug> eoClip");
1024     boolpolydev->eoClip(state);
1025     booltextdev->eoClip(state);
1026     rgbdev->eoClip(state);
1027     clip1dev->eoClip(state);
1028 }
1029 void BitmapOutputDev::clipToStrokePath(GfxState *state)
1030 {
1031     msg("<debug> clipToStrokePath");
1032     boolpolydev->clipToStrokePath(state);
1033     booltextdev->clipToStrokePath(state);
1034     rgbdev->clipToStrokePath(state);
1035     clip1dev->clipToStrokePath(state);
1036 }
1037
1038 void BitmapOutputDev::beginStringOp(GfxState *state)
1039 {
1040     msg("<debug> beginStringOp");
1041     clip0dev->beginStringOp(state);
1042     clip1dev->beginStringOp(state);
1043     booltextdev->beginStringOp(state);
1044     gfxdev->beginStringOp(state);
1045 }
1046 void BitmapOutputDev::endStringOp(GfxState *state)
1047 {
1048     msg("<debug> endStringOp");
1049     clip0dev->endStringOp(state);
1050     clip1dev->endStringOp(state);
1051     booltextdev->endStringOp(state);
1052     checkNewText();
1053     gfxdev->endStringOp(state);
1054 }
1055 void BitmapOutputDev::beginString(GfxState *state, GString *s)
1056 {
1057     msg("<debug> beginString");
1058     clip0dev->beginString(state, s);
1059     clip1dev->beginString(state, s);
1060     booltextdev->beginString(state, s);
1061     gfxdev->beginString(state, s);
1062 }
1063 void BitmapOutputDev::endString(GfxState *state)
1064 {
1065     msg("<debug> endString");
1066     clip0dev->endString(state);
1067     clip1dev->endString(state);
1068     booltextdev->endString(state);
1069     checkNewText();
1070     gfxdev->endString(state);
1071 }
1072
1073 void BitmapOutputDev::clearClips()
1074 {
1075     clearBooleanBitmap(clip0bitmap);
1076     clearBooleanBitmap(clip1bitmap);
1077 }
1078 void BitmapOutputDev::clearBoolPolyDev()
1079 {
1080     clearBooleanBitmap(boolpolybitmap);
1081 }
1082 void BitmapOutputDev::clearBoolTextDev()
1083 {
1084     clearBooleanBitmap(booltextbitmap);
1085 }
1086 void BitmapOutputDev::drawChar(GfxState *state, double x, double y,
1087                       double dx, double dy,
1088                       double originX, double originY,
1089                       CharCode code, int nBytes, Unicode *u, int uLen)
1090 {
1091     msg("<debug> drawChar");
1092     if(state->getRender()&RENDER_CLIP) {
1093         rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1094     } else {
1095         clearClips();
1096         clip0dev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1097         clip1dev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1098
1099         /* if this character is affected somehow by the various clippings (i.e., it looks
1100            different on a device without clipping), then draw it on the bitmap, not as
1101            text */
1102         if(clip0and1differ()) {
1103             msg("<verbose> Char %d is affected by clipping", code);
1104             boolpolydev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1105             checkNewBitmap();
1106             rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1107             if(config_extrafontdata) {
1108                 int oldrender = state->getRender();
1109                 state->setRender(3); //invisible
1110                 gfxdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1111                 state->setRender(oldrender);
1112             }
1113         } else {
1114             /* this char is not at all affected by clipping. 
1115                Now just dump out the bitmap we're currently working on, if necessary. */
1116             booltextdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1117             checkNewText();
1118             /* use polygonal output device to do the actual text handling */
1119             gfxdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1120         }
1121     }
1122 }
1123 void BitmapOutputDev::drawString(GfxState *state, GString *s)
1124 {
1125     msg("<error> internal error: drawString not implemented");
1126     return;
1127     clip0dev->drawString(state, s);
1128     clip1dev->drawString(state, s);
1129     booltextdev->drawString(state, s);
1130     gfxdev->drawString(state, s);
1131 }
1132 void BitmapOutputDev::endTextObject(GfxState *state)
1133 {
1134     msg("<debug> endTextObject");
1135     rgbdev->endTextObject(state);
1136     clip0dev->endTextObject(state);
1137     clip1dev->endTextObject(state);
1138     booltextdev->endTextObject(state);
1139     checkNewText();
1140     gfxdev->endTextObject(state);
1141 }
1142
1143 /* TODO: these four operations below *should* do nothing, as type3
1144          chars are drawn using operations like fill() */
1145 GBool BitmapOutputDev::beginType3Char(GfxState *state, double x, double y,
1146                              double dx, double dy,
1147                              CharCode code, Unicode *u, int uLen)
1148 {
1149     msg("<debug> beginType3Char");
1150     /* call gfxdev so that it can generate "invisible" characters
1151        on top of the actual graphic content, for text extraction */
1152     return gfxdev->beginType3Char(state, x, y, dx, dy, code, u, uLen);
1153 }
1154 void BitmapOutputDev::type3D0(GfxState *state, double wx, double wy)
1155 {
1156     msg("<debug> type3D0");
1157     return gfxdev->type3D0(state, wx, wy);
1158 }
1159 void BitmapOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
1160 {
1161     msg("<debug> type3D1");
1162     return gfxdev->type3D1(state, wx, wy, llx, lly, urx, ury);
1163 }
1164 void BitmapOutputDev::endType3Char(GfxState *state)
1165 {
1166     msg("<debug> endType3Char");
1167     gfxdev->endType3Char(state);
1168 }
1169
1170 class CopyStream: public Object
1171 {
1172     Dict*dict;
1173     char*buf;
1174     MemStream*memstream;
1175     public:
1176     CopyStream(Stream*str, int len)
1177     {
1178         buf = 0;
1179         str->reset();
1180         if(len) {
1181             buf = (char*)malloc(len);
1182             int t;
1183             for (t=0; t<len; t++)
1184               buf[t] = str->getChar();
1185         }
1186         str->close();
1187         this->dict = str->getDict();
1188         this->memstream = new MemStream(buf, 0, len, this);
1189     }
1190     ~CopyStream() 
1191     {
1192         ::free(this->buf);this->buf = 0;
1193         delete this->memstream;
1194     }
1195     Dict* getDict() {return dict;}
1196     Stream* getStream() {return this->memstream;};
1197 };
1198
1199 void BitmapOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1200                            int width, int height, GBool invert,
1201                            GBool inlineImg)
1202 {
1203     msg("<debug> drawImageMask streamkind=%d", str->getKind());
1204     CopyStream*cpystr = 0;
1205     if(inlineImg) {
1206         cpystr = new CopyStream(str, height * ((width + 7) / 8));
1207         str = cpystr->getStream();
1208     }
1209     boolpolydev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
1210     checkNewBitmap();
1211     rgbdev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
1212     if(cpystr)
1213         delete cpystr;
1214 }
1215 void BitmapOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1216                        int width, int height, GfxImageColorMap *colorMap,
1217                        int *maskColors, GBool inlineImg)
1218 {
1219     msg("<debug> drawImage streamkind=%d", str->getKind());
1220     CopyStream*cpystr = 0;
1221     if(inlineImg) {
1222         cpystr = new CopyStream(str, height * ((width * colorMap->getNumPixelComps() * colorMap->getBits() + 7) / 8));
1223         str = cpystr->getStream();
1224     }
1225     boolpolydev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
1226     checkNewBitmap();
1227     rgbdev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
1228     if(cpystr)
1229         delete cpystr;
1230 }
1231 void BitmapOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
1232                              int width, int height,
1233                              GfxImageColorMap *colorMap,
1234                              Stream *maskStr, int maskWidth, int maskHeight,
1235                              GBool maskInvert)
1236 {
1237     msg("<debug> drawMaskedImage streamkind=%d", str->getKind());
1238     boolpolydev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
1239     checkNewBitmap();
1240     rgbdev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
1241 }
1242 void BitmapOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
1243                                  int width, int height,
1244                                  GfxImageColorMap *colorMap,
1245                                  Stream *maskStr,
1246                                  int maskWidth, int maskHeight,
1247                                  GfxImageColorMap *maskColorMap)
1248 {
1249     msg("<debug> drawSoftMaskedImage %dx%d (%dx%d) streamkind=%d", width, height, maskWidth, maskHeight, str->getKind());
1250     boolpolydev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
1251     checkNewBitmap();
1252     rgbdev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
1253 }
1254 void BitmapOutputDev::drawForm(Ref id)
1255 {
1256     msg("<debug> drawForm");
1257     boolpolydev->drawForm(id);
1258     checkNewBitmap();
1259     rgbdev->drawForm(id);
1260 }
1261
1262 void BitmapOutputDev::processLink(Link *link, Catalog *catalog)
1263 {
1264     msg("<debug> processLink");
1265     gfxdev->processLink(link, catalog);
1266 }
1267
1268 void BitmapOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
1269                                     GfxColorSpace *blendingColorSpace,
1270                                     GBool isolated, GBool knockout,
1271                                     GBool forSoftMask)
1272 {
1273     msg("<debug> beginTransparencyGroup");
1274 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
1275     GfxState*state1 = state->copy();
1276     GfxState*state2 = state->copy();
1277     state1->setPath(0);
1278     state1->setPath(state->getPath()->copy());
1279     state2->setPath(0);
1280     state2->setPath(state->getPath()->copy());
1281 #else
1282     GfxState*state1 = state->copy(gTrue);
1283     GfxState*state2 = state->copy(gTrue);
1284 #endif
1285     boolpolydev->beginTransparencyGroup(state1, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1286     rgbdev->beginTransparencyGroup(state2, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1287     clip1dev->beginTransparencyGroup(state, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1288     delete state1;
1289     delete state2;
1290 }
1291 void BitmapOutputDev::endTransparencyGroup(GfxState *state)
1292 {
1293     msg("<debug> endTransparencyGroup");
1294 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
1295     GfxState*state1 = state->copy();
1296     GfxState*state2 = state->copy();
1297     state1->setPath(0);
1298     state1->setPath(state->getPath()->copy());
1299     state2->setPath(0);
1300     state2->setPath(state->getPath()->copy());
1301 #else
1302     GfxState*state1 = state->copy(gTrue);
1303     GfxState*state2 = state->copy(gTrue);
1304 #endif
1305     boolpolydev->endTransparencyGroup(state1);
1306     checkNewBitmap();
1307     rgbdev->endTransparencyGroup(state2);
1308     delete state1;
1309     delete state2;
1310     clip1dev->endTransparencyGroup(state);
1311 }
1312 void BitmapOutputDev::paintTransparencyGroup(GfxState *state, double *bbox)
1313 {
1314     msg("<debug> paintTransparencyGroup");
1315     boolpolydev->paintTransparencyGroup(state,bbox);
1316     checkNewBitmap();
1317     rgbdev->paintTransparencyGroup(state,bbox);
1318     clip1dev->paintTransparencyGroup(state,bbox);
1319 }
1320 void BitmapOutputDev::setSoftMask(GfxState *state, double *bbox, GBool alpha, Function *transferFunc, GfxColor *backdropColor)
1321 {
1322     msg("<debug> setSoftMask");
1323     boolpolydev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1324     checkNewBitmap();
1325     rgbdev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1326     clip1dev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1327 }
1328 void BitmapOutputDev::clearSoftMask(GfxState *state)
1329 {
1330     msg("<debug> clearSoftMask");
1331     boolpolydev->clearSoftMask(state);
1332     checkNewBitmap();
1333     rgbdev->clearSoftMask(state);
1334     clip1dev->clearSoftMask(state);
1335 }