29cf789a0cdcb8419213e4473fcd41ce6d65b252
[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 #include "../gfxtools.h"
33 #include "../types.h"
34 #include "bbox.h"
35
36 #define UNKNOWN_BOUNDING_BOX 0,0,0,0
37
38 static SplashColor splash_white = {255,255,255};
39 static SplashColor splash_black = {0,0,0};
40     
41 ClipState::ClipState()
42 {
43     this->next = 0;
44     this->clipbitmap = 0;
45     this->written = 0;
46 }
47
48 BitmapOutputDev::BitmapOutputDev(InfoOutputDev*info, PDFDoc*doc)
49 {
50     this->info = info;
51     this->doc = doc;
52     this->xref = doc->getXRef();
53     
54     /* color graphic output device, for creating bitmaps */
55     this->rgbdev = new SplashOutputDev(splashModeRGB8, 1, gFalse, splash_white, gTrue, gTrue);
56   
57     /* color mode for binary bitmaps */
58     SplashColorMode colorMode = splashModeMono1;
59
60     /* two devices for testing things against clipping: one clips, the other doesn't */
61     this->clip0dev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
62     this->clip1dev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
63     
64     /* device indicating where polygonal pixels were drawn */
65     this->boolpolydev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
66     /* device indicating where text pixels were drawn */
67     this->booltextdev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
68
69     /* device for handling texts and links */
70     this->gfxdev = new GFXOutputDev(info, this->doc);
71
72     this->rgbdev->startDoc(this->xref);
73     this->boolpolydev->startDoc(this->xref);
74     this->booltextdev->startDoc(this->xref);
75     this->clip0dev->startDoc(this->xref);
76     this->clip1dev->startDoc(this->xref);
77
78     this->gfxoutput = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
79     gfxdevice_record_init(this->gfxoutput, 0);
80
81     this->gfxdev->setDevice(this->gfxoutput);
82     
83     this->config_extrafontdata = 0;
84     this->config_skewedtobitmap = 0;
85     this->config_optimizeplaincolorfills = 0;
86     this->bboxpath = 0;
87     //this->clipdev = 0;
88     //this->clipstates = 0;
89 }
90 BitmapOutputDev::~BitmapOutputDev()
91 {
92     if(this->gfxoutput) {
93         gfxresult_t*r = this->gfxoutput->finish(this->gfxoutput);
94         r->destroy(r);
95         free(this->gfxoutput);this->gfxoutput = 0;
96     }
97     if(this->bboxpath) {
98         delete this->bboxpath;this->bboxpath = 0;
99     }
100     if(this->rgbdev) {
101         delete this->rgbdev;this->rgbdev = 0;
102     }
103     if(this->gfxdev) {
104         delete this->gfxdev;this->gfxdev= 0;
105     }
106     if(this->boolpolydev) {
107         delete this->boolpolydev;this->boolpolydev = 0;
108     }
109     if(this->stalepolybitmap) {
110         delete this->stalepolybitmap;this->stalepolybitmap = 0;
111     }
112     if(this->staletextbitmap) {
113         delete this->staletextbitmap;this->staletextbitmap = 0;
114     }
115     if(this->booltextdev) {
116         delete this->booltextdev;this->booltextdev = 0;
117     }
118     if(this->clip0dev) {
119         delete this->clip0dev;this->clip0dev = 0;
120     }
121     if(this->clip1dev) {
122         delete this->clip1dev;this->clip1dev = 0;
123     }
124     //if(this->clipbitmap) {
125     //    delete this->clipbitmap;this->clipbitmap = 0;
126     //}
127     //if(this->clipdev) {
128     //    delete this->clipdev;this->clipdev = 0;
129     //}
130
131 }
132
133 GBool BitmapOutputDev::getVectorAntialias()
134 {
135     return this->rgbdev->getVectorAntialias();
136 }
137 void BitmapOutputDev::setVectorAntialias(GBool vaa)
138 {
139     this->rgbdev->setVectorAntialias(vaa);
140 }
141 void BitmapOutputDev::setDevice(gfxdevice_t*dev)
142 {
143     this->dev = dev;
144 }
145 void BitmapOutputDev::setMove(int x,int y)
146 {
147     this->gfxdev->setMove(x,y);
148     this->user_movex = x;
149     this->user_movey = y;
150 }
151 void BitmapOutputDev::setClip(int x1,int y1,int x2,int y2)
152 {
153     this->gfxdev->setClip(x1,y1,x2,y2);
154     this->user_clipx1 = x1;
155     this->user_clipy1 = y1;
156     this->user_clipx2 = x2;
157     this->user_clipy2 = y2;
158 }
159 void BitmapOutputDev::setParameter(const char*key, const char*value)
160 {
161     if(!strcmp(key, "extrafontdata")) {
162         this->config_extrafontdata = atoi(value);
163     }
164     this->gfxdev->setParameter(key, value);
165 }
166 void BitmapOutputDev::setPageMap(int*page2page, int num_pages)
167 {
168     this->gfxdev->setPageMap(page2page, num_pages);
169 }
170
171 void writeBitmap(SplashBitmap*bitmap, char*filename);
172 void writeAlpha(SplashBitmap*bitmap, char*filename);
173
174 static int dbg_btm_counter=1;
175
176 void BitmapOutputDev::flushBitmap()
177 {
178     int width = rgbdev->getBitmapWidth();
179     int height = rgbdev->getBitmapHeight();
180
181     if(sizeof(SplashColor)!=3) {
182         msg("<error> sizeof(SplashColor)!=3");
183         return;
184     }
185
186     /*static int counter=0;
187     if(!counter) {
188         writeBitmap(rgbdev->getBitmap(), "test.png");
189     } counter++;*/
190     
191     /*static int counter=0;
192     char filename[160];
193     sprintf(filename, "test%d.png", counter++);
194     writeBitmap(rgbbitmap, filename);*/
195     
196     SplashColorPtr rgb = rgbbitmap->getDataPtr();
197     Guchar*alpha = rgbbitmap->getAlphaPtr();
198     
199     Guchar*alpha2 = stalepolybitmap->getDataPtr();
200     int width8 = (stalepolybitmap->getWidth()+7)/8;
201
202     /*char filename[80];
203     sprintf(filename, "flush%d_mask.png", dbg_btm_counter);
204     writeAlpha(stalepolybitmap, filename);
205     sprintf(filename, "flush%d_alpha.png", dbg_btm_counter);
206     writeAlpha(rgbbitmap, filename);
207     sprintf(filename, "flush%d_bitmap.png", dbg_btm_counter);
208     writeBitmap(rgbbitmap, filename);*/
209
210     ibbox_t* boxes = get_bitmap_bboxes((unsigned char*)alpha, width, height);
211     ibbox_t*b;
212
213     for(b=boxes;b;b=b->next) {
214         int xmin = b->xmin;
215         int ymin = b->ymin;
216         int xmax = b->xmax;
217         int ymax = b->ymax;
218
219         /* clip against (-movex, -movey, -movex+width, -movey+height) */
220
221         msg("<verbose> Flushing bitmap (bbox: %d,%d,%d,%d %dx%d) (clipped against %d,%d,%d,%d)", xmin,ymin,xmax,ymax, xmax-xmin, ymax-ymin,
222                 -this->movex, -this->movey, -this->movex+this->width, -this->movey+this->height);
223
224         if(xmin < -this->movex) {
225             xmin = -this->movex;
226             if(xmax < -this->movex) continue;
227         }
228         if(ymin < -this->movey) {
229             ymin = -this->movey;
230             if(ymax < -this->movey) continue;
231         }
232         if(xmax >= -this->movex + this->width) {
233             xmax = -this->movex+this->width;
234             if(xmin >= -this->movex + this->width) continue;
235         }
236         if(ymax >= -this->movey + this->height) {
237             ymax = -this->movey+this->height;
238             if(ymin >= -this->movey + this->height) continue;
239         }
240         
241         if((xmax-xmin)<=0 || (ymax-ymin)<=0) // no bitmap, nothing to do
242             continue;
243
244         int rangex = xmax-xmin;
245         int rangey = ymax-ymin;
246         gfximage_t*img = (gfximage_t*)malloc(sizeof(gfximage_t)); 
247         img->data = (gfxcolor_t*)malloc(rangex * rangey * 4);
248         img->width = rangex;
249         img->height = rangey;
250         int x,y;
251         for(y=0;y<rangey;y++) {
252             SplashColorPtr in=&rgb[((y+ymin)*width+xmin)*sizeof(SplashColor)];
253             gfxcolor_t*out = &img->data[y*rangex];
254             Guchar*ain = &alpha[(y+ymin)*width+xmin];
255             Guchar*ain2 = &alpha2[(y+ymin)*width8];
256             if(this->emptypage) {
257                 for(x=0;x<rangex;x++) {
258                     /* the first bitmap on the page doesn't need to have an alpha channel-
259                        blend against a white background*/
260                     out[x].r = (in[x*3+0]*ain[x])/255 + 255-ain[x];
261                     out[x].g = (in[x*3+1]*ain[x])/255 + 255-ain[x];
262                     out[x].b = (in[x*3+2]*ain[x])/255 + 255-ain[x];
263                     out[x].a = 255;
264                 }
265             } else {
266                 for(x=0;x<rangex;x++) {
267                     if(!(ain2[(x+xmin)/8]&(0x80>>((x+xmin)&7)))) {
268                         /* cut away pixels that we don't remember drawing (i.e., that are
269                            not in the monochrome bitmap). Prevents some "hairlines" showing
270                            up to the left and right of bitmaps. */
271                         out[x].r = 0;out[x].g = 0;out[x].b = 0;out[x].a = 0;
272                     } else {
273                         /* according to endPage()/compositeBackground() in xpdf/SplashOutputDev.cc, this
274                            data has non-premultiplied alpha, which is exactly what the output device 
275                            expects, so don't premultiply it here, either.
276                         */
277                         out[x].r = in[x*3+0];
278                         out[x].g = in[x*3+1];
279                         out[x].b = in[x*3+2];
280                         out[x].a = ain[x];
281                     }
282                 }
283             }
284         }
285
286         /* transform bitmap rectangle to "device space" */
287         xmin += movex;
288         ymin += movey;
289         xmax += movex;
290         ymax += movey;
291
292         gfxmatrix_t m;
293         m.tx = xmin;
294         m.ty = ymin;
295         m.m00 = m.m11 = 1;
296         m.m10 = m.m01 = 0;
297         m.tx -= 0.5;
298         m.ty -= 0.5;
299
300         gfxline_t* line = gfxline_makerectangle(xmin, ymin, xmax, ymax);
301         dev->fillbitmap(dev, line, img, &m, 0);
302         gfxline_free(line);
303     
304         free(img->data);img->data=0;free(img);img=0;
305     }
306     ibbox_destroy(boxes);
307
308     memset(rgbbitmap->getAlphaPtr(), 0, rgbbitmap->getWidth()*rgbbitmap->getHeight());
309     memset(rgbbitmap->getDataPtr(), 0, rgbbitmap->getRowSize()*rgbbitmap->getHeight());
310
311     this->emptypage = 0;
312 }
313
314 void BitmapOutputDev::flushText()
315 {
316     msg("<verbose> Flushing text");
317
318     static gfxfontlist_t*output_font_list = 0;
319     static gfxdevice_t*last = 0;
320     if(last != this->dev) {
321         if(output_font_list)
322             gfxfontlist_free(output_font_list, 0);
323         output_font_list = gfxfontlist_create();
324     }
325     gfxdevice_record_flush(this->gfxoutput, this->dev, &output_font_list);
326     last = this->dev;
327     
328     this->emptypage = 0;
329 }
330
331 void writeMonoBitmap(SplashBitmap*btm, char*filename)
332 {
333     int width8 = (btm->getWidth()+7)/8;
334     int width = btm->getWidth();
335     int height = btm->getHeight();
336     gfxcolor_t*b = (gfxcolor_t*)malloc(sizeof(gfxcolor_t)*width*height);
337     unsigned char*data = btm->getDataPtr();
338     int x,y;
339     for(y=0;y<height;y++) {
340         unsigned char*l = &data[width8*y];
341         gfxcolor_t*d = &b[width*y];
342         for(x=0;x<width;x++) {
343             if(l[x>>3]&(128>>(x&7))) {
344                 d[x].r = d[x].b = d[x].a = 255;
345                 d[x].g = 0;
346             } else {
347                 d[x].r = d[x].g = d[x].b = d[x].a = 0;
348             }
349         }
350     }
351     writePNG(filename, (unsigned char*)b, width, height);
352     free(b);
353 }
354
355 void writeBitmap(SplashBitmap*bitmap, char*filename)
356 {
357     int y,x;
358     
359     int width = bitmap->getWidth();
360     int height = bitmap->getHeight();
361
362     gfxcolor_t*data = (gfxcolor_t*)malloc(sizeof(gfxcolor_t)*width*height);
363
364     if(bitmap->getMode()==splashModeMono1) {
365         writeMonoBitmap(bitmap, filename);
366         return;
367     }
368
369     for(y=0;y<height;y++) {
370         gfxcolor_t*line = &data[y*width];
371         for(x=0;x<width;x++) {
372             Guchar c[4] = {0,0,0,0};
373             bitmap->getPixel(x,y,c);
374             line[x].r = c[0];
375             line[x].g = c[1];
376             line[x].b = c[2];
377             line[x].a =  bitmap->getAlpha(x,y);
378         }
379     }
380     writePNG(filename, (unsigned char*)data, width, height);
381     free(data);
382 }
383
384 void writeAlpha(SplashBitmap*bitmap, char*filename)
385 {
386     int y,x;
387     
388     int width = bitmap->getWidth();
389     int height = bitmap->getHeight();
390     
391     if(bitmap->getMode()==splashModeMono1) {
392         writeMonoBitmap(bitmap, filename);
393         return;
394     }
395
396     gfxcolor_t*data = (gfxcolor_t*)malloc(sizeof(gfxcolor_t)*width*height);
397
398     for(y=0;y<height;y++) {
399         gfxcolor_t*line = &data[y*width];
400         for(x=0;x<width;x++) {
401             int a = bitmap->getAlpha(x,y);
402             line[x].r = a;
403             line[x].g = 0;
404             line[x].b = a;
405             line[x].a = a;
406         }
407     }
408     writePNG(filename, (unsigned char*)data, width, height);
409     free(data);
410 }
411
412 static const char*STATE_NAME[] = {"parallel", "textabovebitmap", "bitmapabovetext"};
413
414 int checkAlphaSanity(SplashBitmap*boolbtm, SplashBitmap*alphabtm)
415 {
416     assert(boolbtm->getWidth() == alphabtm->getWidth());
417     assert(boolbtm->getHeight() == alphabtm->getHeight());
418     if(boolbtm->getMode()==splashModeMono1) {
419         return 1;
420     }
421
422     int width = boolbtm->getWidth();
423     int height = boolbtm->getHeight();
424
425     int bad=0;
426     int x,y;
427     for(y=0;y<height;y++) {
428         for(x=0;x<width;x++) {
429             int a1 = alphabtm->getAlpha(x,y);
430             int a2 = boolbtm->getAlpha(x,y);
431             if(a1!=a2) {
432                 bad++;
433             }
434         }
435     }
436     double badness = bad/(double)(width*height);
437     if(badness>0.2) {
438         msg("<error> Bitmaps don't correspond: %d out of %d pixels wrong (%.2f%%)", bad, width*height, 
439                 badness*100.0);
440         return 0;
441     }
442     msg("<notice> %f", badness);
443     return 1;
444 }
445
446 static inline GBool fixBBox(int*x1, int*y1, int*x2, int*y2, int width, int height)
447 {
448     if(!(*x1|*y1|*x2|*y2)) {
449         // undefined bbox
450         *x1 = *y1 = 0;
451         *x2 = width;
452         *y2 = height;
453         return gTrue;
454     }
455     if(*x2<=*x1) return gFalse;
456     if(*x2<0) return gFalse;
457     if(*x1<0) *x1 = 0;
458     if(*x1>=width) return gFalse;
459     if(*x2>width) *x2=width;
460
461     if(*y2<=*y1) return gFalse;
462     if(*y2<0) return gFalse;
463     if(*y1<0) *y1 = 0;
464     if(*y1>=height) return gFalse;
465     if(*y2>height) *y2=height;
466     return gTrue;
467 }
468
469 static void update_bitmap(SplashBitmap*bitmap, SplashBitmap*update, int x1, int y1, int x2, int y2, char overwrite)
470 {
471     assert(bitmap->getMode()==splashModeMono1);
472     assert(update->getMode()==splashModeMono1);
473
474     int width8 = (bitmap->getWidth()+7)/8;
475     assert(width8 == bitmap->getRowSize());
476     assert(width8 == update->getRowSize());
477     int height = bitmap->getHeight();
478     assert(height == update->getHeight());
479
480     if(!fixBBox(&x1, &y1, &x2, &y2, bitmap->getWidth(), bitmap->getHeight()))
481         return;
482     
483     Guchar*b = bitmap->getDataPtr() + y1*width8 + x1/8;
484     Guchar*u = update->getDataPtr() + y1*width8 + x1/8;
485     int yspan = y2-y1;
486     int xspan = (x2+7)/8 - x1/8;
487     int size = (y2-y1)*width8;
488
489     if(overwrite) {
490         int y;
491         for(y=0;y<yspan;y++) {
492             memcpy(b, u, xspan);
493             b += width8;
494             u += width8;
495         }
496     } else {
497         if(((ptroff_t)b&7)==((ptroff_t)u&7)) {
498             int x,y;
499             for(y=0;y<yspan;y++) {
500                 Guchar*e1 = b+xspan-8;
501                 Guchar*e2 = b+xspan;
502                 while(((ptroff_t)b&7) && b<e1) {
503                     *b |= *u;
504                     b++;u++;
505                 }
506                 while(b<e1) {
507                     *(long long*)b |= *(long long*)u;
508                     b+=8;u+=8;
509                 }
510                 while(b<e2) {
511                     *b |= *u;
512                     b++;u++;
513                 }
514                 b += width8-xspan;
515                 u += width8-xspan;
516             }
517         } else {
518             int x,y;
519             for(y=0;y<yspan;y++) {
520                 for(x=0;x<xspan;x++) {
521                     b[x] |= u[x];
522                 }
523                 b += width8;
524                 u += width8;
525             }
526         }
527     }
528 }
529
530 static void clearBooleanBitmap(SplashBitmap*btm, int x1, int y1, int x2, int y2)
531 {
532     if(!fixBBox(&x1, &y1, &x2, &y2, btm->getWidth(), btm->getHeight()))
533         return;
534     
535     if(btm->getMode()==splashModeMono1) {
536         int width8 = (btm->getWidth()+7)/8;
537         assert(width8 == btm->getRowSize());
538         int width = btm->getWidth();
539         int height = btm->getHeight();
540         Guchar*data = btm->getDataPtr();
541         memset(data+y1*width8, 0, width8*(y2-y1));
542     } else {
543         int width = btm->getAlphaRowSize();
544         int height = btm->getHeight();
545         memset(btm->getAlphaPtr(), 0, width*height);
546     }
547 }
548
549 void BitmapOutputDev::dbg_newdata(char*newdata)
550 {
551     if(0) {
552         char filename1[80];
553         char filename2[80];
554         char filename3[80];
555         sprintf(filename1, "state%03dboolbitmap_after%s.png", dbg_btm_counter, newdata);
556         sprintf(filename2, "state%03dbooltext_after%s.png", dbg_btm_counter, newdata);
557         sprintf(filename3, "state%03dbitmap_after%s.png", dbg_btm_counter, newdata);
558         msg("<verbose> %s %s %s", filename1, filename2, filename3);
559         writeAlpha(stalepolybitmap, filename1);
560         writeAlpha(booltextbitmap, filename2);
561         writeBitmap(rgbdev->getBitmap(), filename3);
562     }
563     dbg_btm_counter++;
564 }
565
566 GBool BitmapOutputDev::checkNewText(int x1, int y1, int x2, int y2)
567 {
568     /* called once some new text was drawn on booltextdev, and
569        before the same thing is drawn on gfxdev */
570    
571     msg("<trace> Testing new text data against current bitmap data, state=%s, counter=%d\n", STATE_NAME[layerstate], dbg_btm_counter);
572     
573     GBool ret = false;
574     if(intersection(booltextbitmap, stalepolybitmap, x1,y1,x2,y2)) {
575         if(layerstate==STATE_PARALLEL) {
576             /* the new text is above the bitmap. So record that fact. */
577             msg("<verbose> Text is above current bitmap/polygon data");
578             layerstate=STATE_TEXT_IS_ABOVE;
579             update_bitmap(staletextbitmap, booltextbitmap, x1, y1, x2, y2, 0);
580         } else if(layerstate==STATE_BITMAP_IS_ABOVE) {
581             /* there's a bitmap above the (old) text. So we need
582                to flush out that text, and record that the *new*
583                text is now *above* the bitmap
584              */
585             msg("<verbose> Text is above current bitmap/polygon data (which is above some other text)");
586             flushText();
587             layerstate=STATE_TEXT_IS_ABOVE;
588            
589             clearBoolTextDev();
590             /* re-apply the update (which we would otherwise lose) */
591             update_bitmap(staletextbitmap, booltextbitmap, x1, y1, x2, y2, 1);
592             ret = true;
593         } else {
594             /* we already know that the current text section is
595                above the current bitmap section- now just new
596                bitmap data *and* new text data was drawn, and
597                *again* it's above the current bitmap. */
598             msg("<verbose> Text is still above current bitmap/polygon data");
599             update_bitmap(staletextbitmap, booltextbitmap, x1, y1, x2, y2, 0);
600         }
601     }  else {
602         update_bitmap(staletextbitmap, booltextbitmap, x1, y1, x2, y2, 0);
603     }
604     
605     /* clear the thing we just drew from our temporary drawing bitmap */
606     clearBooleanBitmap(booltextbitmap, x1, y1, x2, y2);
607
608     return ret;
609 }
610 GBool BitmapOutputDev::checkNewBitmap(int x1, int y1, int x2, int y2)
611 {
612     /* similar to checkNewText() above, only in reverse */
613     msg("<trace> Testing new graphics data against current text data, state=%s, counter=%d\n", STATE_NAME[layerstate], dbg_btm_counter);
614
615     GBool ret = false;
616     if(intersection(boolpolybitmap, staletextbitmap, x1,y1,x2,y2)) {
617         if(layerstate==STATE_PARALLEL) {
618             msg("<verbose> Bitmap is above current text data");
619             layerstate=STATE_BITMAP_IS_ABOVE;
620             update_bitmap(stalepolybitmap, boolpolybitmap, x1, y1, x2, y2, 0);
621         } else if(layerstate==STATE_TEXT_IS_ABOVE) {
622             msg("<verbose> Bitmap is above current text data (which is above some bitmap)");
623             flushBitmap();
624             layerstate=STATE_BITMAP_IS_ABOVE;
625             clearBoolPolyDev();
626             update_bitmap(stalepolybitmap, boolpolybitmap, x1, y1, x2, y2, 1);
627             ret = true;
628         } else {
629             msg("<verbose> Bitmap is still above current text data");
630             update_bitmap(stalepolybitmap, boolpolybitmap, x1, y1, x2, y2, 0);
631         }
632     }  else {
633         update_bitmap(stalepolybitmap, boolpolybitmap, x1, y1, x2, y2, 0);
634     }
635     
636     /* clear the thing we just drew from our temporary drawing bitmap */
637     clearBooleanBitmap(boolpolybitmap, x1, y1, x2, y2);
638
639     return ret;
640 }
641
642 //void checkNewText() {
643 //    Guchar*alpha = rgbbitmap->getAlphaPtr();
644 //    Guchar*charpixels = clip1bitmap->getDataPtr();
645 //    int xx,yy;
646 //    for(yy=0;yy<height;yy++) {
647 //        Guchar*aline = &alpha[yy*width];
648 //        Guchar*cline = &charpixels[yy*width8];
649 //        for(xx=0;xx<width;xx++) {
650 //            int bit = xx&7;
651 //            int bytepos = xx>>3;
652 //            /* TODO: is the bit order correct? */
653 //            if(aline[xx] && (cline[bytepos]&(1<<bit))) 
654 //              break;
655 //        }
656 //        if(xx!=width)
657 //            break;
658 //}
659
660 GBool BitmapOutputDev::clip0and1differ(int x1,int y1,int x2,int y2)
661 {
662     if(clip0bitmap->getMode()==splashModeMono1) {
663         int width = clip0bitmap->getWidth();
664         int width8 = (width+7)/8;
665         int height = clip0bitmap->getHeight();
666
667         if(!fixBBox(&x1,&y1,&x2,&y2,width,height)) {
668             /* area is outside or null */
669             return gFalse;
670         }
671         
672         SplashBitmap*clip0 = clip0bitmap;
673         SplashBitmap*clip1 = clip1bitmap;
674         int x18 = x1/8;
675         int x28 = (x2+7)/8;
676         int y;
677
678         for(y=y1;y<y2;y++) {
679             unsigned char*row1 = &clip0bitmap->getDataPtr()[width8*y+x18];
680             unsigned char*row2 = &clip1bitmap->getDataPtr()[width8*y+x18];
681             if(memcmp(row1, row2, x28-x18)) {
682                 return gTrue;
683             }
684         }
685         return gFalse;
686     } else {
687         SplashBitmap*clip0 = clip0bitmap;
688         SplashBitmap*clip1 = clip1bitmap;
689         int width = clip0->getAlphaRowSize();
690         int height = clip0->getHeight();
691
692         if(!fixBBox(&x1, &y1, &x2, &y2, width, height)) {
693             x1=y1=0;x2=y2=1;
694         }
695
696         Guchar*a0 = clip0->getAlphaPtr();
697         Guchar*a1 = clip1->getAlphaPtr();
698         int x,y;
699         char differs=0;
700         for(y=y1;y<y2;y++) {
701             for(x=x1;x<x2;x++) {
702                 if(a0[y*width+x]!=a1[y*width+x]) {
703                     differs=1;
704                     break;
705                 }
706             }
707             if(differs)
708                 break;
709         }
710         char differs2 = memcmp(a0, a1, width*height);
711         if(differs && !differs2) 
712             msg("<warning> Strange internal error (2)");
713         else if(!differs && differs2) {
714             msg("<warning> Bad Bounding Box: Difference in clip0 and clip1 outside bbox");
715             msg("<warning> %d %d %d %d", x1, y1, x2, y2);
716         }
717         return differs2;
718     }
719 }
720
721 GBool compare8(unsigned char*data1, unsigned char*data2, int len)
722 {
723     if(!len)
724         return 0;
725     if(((ptroff_t)data1&7)==((ptroff_t)data2&7)) {
726         // oh good, we can align both to 8 byte
727         while((ptroff_t)data1&7) {
728             if(*data1&*data2)
729                 return 1;
730             data1++;
731             data2++;
732             if(!--len)
733                 return 0;
734         }
735     }
736     /* use 64 bit for the (hopefully aligned) middle section */
737     int l8 = len/8;
738     long long unsigned int*d1 = (long long unsigned int*)data1;
739     long long unsigned int*d2 = (long long unsigned int*)data2;
740     long long unsigned int x = 0;
741     int t;
742     for(t=0;t<l8;t++) {
743         x |= d1[t]&d2[t];
744     }
745     if(x)
746         return 1;
747
748     data1+=l8*8;
749     data2+=l8*8;
750     len -= l8*8;
751     for(t=0;t<len;t++) {
752         if(data1[t]&data2[t]) {
753             return 1;
754         }
755     }
756     return 0;
757 }
758
759 GBool BitmapOutputDev::intersection(SplashBitmap*boolpoly, SplashBitmap*booltext, int x1, int y1, int x2, int y2)
760 {
761     if(boolpoly->getMode()==splashModeMono1) {
762         /* alternative implementation, using one bit per pixel-
763            needs the no-dither patch in xpdf */
764         
765         int width = boolpoly->getWidth();
766         int height = boolpoly->getHeight();
767
768         if(!fixBBox(&x1,&y1,&x2,&y2, width, height)) {
769             return gFalse;
770         }
771
772         Guchar*polypixels = boolpoly->getDataPtr();
773         Guchar*textpixels = booltext->getDataPtr();
774
775         int width8 = (width+7)/8;
776         int runx = width8;
777         int runy = height;
778         
779         if(x1|y1|x2|y2) {
780             polypixels+=y1*width8+x1/8;
781             textpixels+=y1*width8+x1/8;
782             runx=(x2+7)/8 - x1/8;
783             runy=y2-y1;
784         }
785         
786         int t;
787         unsigned char c=0;
788         
789         /*assert(sizeof(unsigned long long int)==8);
790         if(((ptroff_t)polypixels&7) || ((ptroff_t)textpixels&7)) {
791             //msg("<warning> Non-optimal alignment");
792         }*/
793
794         int x,y;
795         unsigned char*data1 = (unsigned char*)polypixels;
796         unsigned char*data2 = (unsigned char*)textpixels;
797         msg("<verbose> Testing area (%d,%d,%d,%d), runx=%d,runy=%d", x1,y1,x2,y2, runx, runy);
798         for(y=0;y<runy;y++) {
799             if(compare8(data1,data2,runx))
800                 return gTrue;
801             data1+=width8;
802             data2+=width8;
803         }
804         return gFalse;
805     } else {
806         int width = boolpoly->getAlphaRowSize();
807         int height = boolpoly->getHeight();
808
809         if(!fixBBox(&x1, &y1, &x2, &y2, width, height)) {
810             x1=y1=0;x2=y2=1;
811         }
812         Guchar*polypixels = boolpoly->getAlphaPtr();
813         Guchar*textpixels = booltext->getAlphaPtr();
814
815         int x,y;
816         char overlap1 = 0;
817         char overlap2 = 0;
818         for(x=x1;x<x2;x++) {
819             for(y=y1;y<y2;y++) {
820                 if(polypixels[width*y+x]&&textpixels[width*y+x])
821                     overlap1 = 1;
822             }
823         }
824         int ax1=0,ay1=0,ax2=0,ay2=0;
825         for(y=0;y<height;y++) {
826             for(x=0;x<width;x++) {
827                 if(polypixels[width*y+x]&&textpixels[width*y+x]) {
828                     overlap2 = 1;
829                     if(!(ax1|ay1|ax2|ay2)) {
830                         ax1 = ax2 = x;
831                         ay1 = ay2 = y;
832                     } else {
833                         ax1 = ax1<x?ax1:x;
834                         ay1 = ay1<y?ay1:y;
835                         ax2 = ax2>x?ax2:x;
836                         ay2 = ay2>y?ay2:y;
837                     }
838                 }
839             }
840         }
841         if(overlap1 && !overlap2)
842             msg("<warning> strange internal error");
843         if(!overlap1 && overlap2) {
844             msg("<warning> Bad bounding box: intersection outside bbox");
845             msg("<warning> given bbox: %d %d %d %d", x1, y1, x2, y2);
846             msg("<warning> changed area: %d %d %d %d", ax1, ay1, ax2, ay2);
847         }
848         return overlap2;
849     }
850 }
851
852
853 void BitmapOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
854 {
855     double x1,y1,x2,y2;
856     state->transform(crop_x1,crop_y1,&x1,&y1);
857     state->transform(crop_x2,crop_y2,&x2,&y2);
858     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
859     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
860     
861     this->movex = -(int)x1 - user_movex;
862     this->movey = -(int)y1 - user_movey;
863     
864     if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
865         x1 = user_clipx1;
866         x2 = user_clipx2;
867         y1 = user_clipy1;
868         y2 = user_clipy2;
869     }
870     this->width = (int)(x2-x1);
871     this->height = (int)(y2-y1);
872
873     rgbdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
874     boolpolydev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
875     booltextdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
876     clip0dev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
877     clip1dev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
878     gfxdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
879
880     boolpolybitmap = boolpolydev->getBitmap();
881     stalepolybitmap = new SplashBitmap(boolpolybitmap->getWidth(), boolpolybitmap->getHeight(), 1, boolpolybitmap->getMode(), 0);
882     assert(stalepolybitmap->getRowSize() == boolpolybitmap->getRowSize());
883
884     booltextbitmap = booltextdev->getBitmap();
885     staletextbitmap = new SplashBitmap(booltextbitmap->getWidth(), booltextbitmap->getHeight(), 1, booltextbitmap->getMode(), 0);
886     assert(staletextbitmap->getRowSize() == booltextbitmap->getRowSize());
887     
888     msg("<debug> startPage %dx%d (%dx%d)", this->width, this->height, booltextbitmap->getWidth(), booltextbitmap->getHeight());
889
890     clip0bitmap = clip0dev->getBitmap();
891     clip1bitmap = clip1dev->getBitmap();
892     rgbbitmap = rgbdev->getBitmap();
893     
894     flushText(); // write out the initial clipping rectangle
895
896     /* just in case any device did draw a white background rectangle 
897        into the device */
898     clearBoolTextDev();
899     clearBoolPolyDev();
900
901     this->layerstate = STATE_PARALLEL;
902     this->emptypage = 1;
903     msg("<debug> startPage done");
904 }
905
906 void BitmapOutputDev::endPage()
907 {
908     msg("<verbose> endPage (BitmapOutputDev)");
909
910     /* notice: we're not fully done yet with this page- there might still be 
911        a few calls to drawLink() yet to come */
912 }
913 void BitmapOutputDev::finishPage()
914 {
915     msg("<verbose> finishPage (BitmapOutputDev)");
916     gfxdev->endPage();
917    
918     if(layerstate == STATE_BITMAP_IS_ABOVE) {
919         this->flushText();
920         this->flushBitmap();
921     } else {
922         this->flushBitmap();
923         this->flushText();
924     }
925
926     /* splash will now destroy alpha, and paint the 
927        background color into the "holes" in the bitmap */
928     boolpolydev->endPage();
929     booltextdev->endPage();
930     rgbdev->endPage();
931     clip0dev->endPage();
932     clip1dev->endPage();
933 }
934
935 GBool BitmapOutputDev::upsideDown()
936 {
937     boolpolydev->upsideDown();
938     booltextdev->upsideDown();
939     clip0dev->upsideDown();
940     clip1dev->upsideDown();
941     return rgbdev->upsideDown();
942 }
943
944 GBool BitmapOutputDev::useDrawChar()
945 {
946     boolpolydev->useDrawChar();
947     booltextdev->useDrawChar();
948     clip0dev->useDrawChar();
949     clip1dev->useDrawChar();
950     return rgbdev->useDrawChar();
951 }
952
953 GBool BitmapOutputDev::useTilingPatternFill()
954 {
955     boolpolydev->useTilingPatternFill();
956     booltextdev->useTilingPatternFill();
957     clip0dev->useTilingPatternFill();
958     clip1dev->useTilingPatternFill();
959     return rgbdev->useTilingPatternFill();
960 }
961
962 GBool BitmapOutputDev::useShadedFills()
963 {
964     boolpolydev->useShadedFills();
965     booltextdev->useShadedFills();
966     clip0dev->useShadedFills();
967     clip1dev->useShadedFills();
968     return rgbdev->useShadedFills();
969 }
970
971 GBool BitmapOutputDev::useDrawForm()
972 {
973     boolpolydev->useDrawForm();
974     booltextdev->useDrawForm();
975     clip0dev->useDrawForm();
976     clip1dev->useDrawForm();
977     return rgbdev->useDrawForm();
978 }
979
980 GBool BitmapOutputDev::interpretType3Chars()
981 {
982     boolpolydev->interpretType3Chars();
983     booltextdev->interpretType3Chars();
984     clip0dev->interpretType3Chars();
985     clip1dev->interpretType3Chars();
986     return rgbdev->interpretType3Chars();
987 }
988
989 GBool BitmapOutputDev::needNonText() 
990 {
991     boolpolydev->needNonText();
992     booltextdev->needNonText();
993     clip0dev->needNonText();
994     clip1dev->needNonText();
995     return rgbdev->needNonText();
996 }
997 /*GBool BitmapOutputDev::checkPageSlice(Page *page, double hDPI, double vDPI,
998                            int rotate, GBool useMediaBox, GBool crop,
999                            int sliceX, int sliceY, int sliceW, int sliceH,
1000                            GBool printing, Catalog *catalog,
1001                            GBool (*abortCheckCbk)(void *data),
1002                            void *abortCheckCbkData)
1003 {
1004     return gTrue;
1005 }*/
1006 void BitmapOutputDev::setDefaultCTM(double *ctm) 
1007 {
1008     boolpolydev->setDefaultCTM(ctm);
1009     booltextdev->setDefaultCTM(ctm);
1010     rgbdev->setDefaultCTM(ctm);
1011     clip0dev->setDefaultCTM(ctm);
1012     clip1dev->setDefaultCTM(ctm);
1013     gfxdev->setDefaultCTM(ctm);
1014 }
1015 void BitmapOutputDev::saveState(GfxState *state) 
1016 {
1017     boolpolydev->saveState(state);
1018     booltextdev->saveState(state);
1019     rgbdev->saveState(state);
1020     clip0dev->saveState(state);
1021     clip1dev->saveState(state);
1022
1023     /*ClipState*cstate = new ClipState();
1024     cstate->next = this->clipstates;
1025     this->clipstates = cstate;*/
1026 }
1027 void BitmapOutputDev::restoreState(GfxState *state) 
1028 {
1029     boolpolydev->restoreState(state);
1030     booltextdev->restoreState(state);
1031     rgbdev->restoreState(state);
1032     clip0dev->restoreState(state);
1033     clip1dev->restoreState(state);
1034
1035     /*if(this->clipstates) {
1036         ClipState*old = this->clipstates;
1037         if(old->written) {
1038             gfxdev->restoreState(state);
1039         }
1040         this->clipstates = this->clipstates->next;
1041         delete(old);
1042     } else {
1043         msg("<error> invalid restoreState()");
1044     }*/
1045 }
1046 void BitmapOutputDev::updateAll(GfxState *state)
1047 {
1048     boolpolydev->updateAll(state);
1049     booltextdev->updateAll(state);
1050     rgbdev->updateAll(state);
1051     clip0dev->updateAll(state);
1052     clip1dev->updateAll(state);
1053     gfxdev->updateAll(state);
1054 }
1055 void BitmapOutputDev::updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32)
1056 {
1057     boolpolydev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1058     booltextdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1059     rgbdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1060     clip0dev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1061     clip1dev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1062     gfxdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1063 }
1064 void BitmapOutputDev::updateLineDash(GfxState *state)
1065 {
1066     boolpolydev->updateLineDash(state);
1067     booltextdev->updateLineDash(state);
1068     rgbdev->updateLineDash(state);
1069     clip0dev->updateLineDash(state);
1070     clip1dev->updateLineDash(state);
1071     gfxdev->updateLineDash(state);
1072 }
1073 void BitmapOutputDev::updateFlatness(GfxState *state)
1074 {
1075     boolpolydev->updateFlatness(state);
1076     booltextdev->updateFlatness(state);
1077     rgbdev->updateFlatness(state);
1078     clip0dev->updateFlatness(state);
1079     clip1dev->updateFlatness(state);
1080     gfxdev->updateFlatness(state);
1081 }
1082 void BitmapOutputDev::updateLineJoin(GfxState *state)
1083 {
1084     boolpolydev->updateLineJoin(state);
1085     booltextdev->updateLineJoin(state);
1086     rgbdev->updateLineJoin(state);
1087     clip0dev->updateLineJoin(state);
1088     clip1dev->updateLineJoin(state);
1089     gfxdev->updateLineJoin(state);
1090 }
1091 void BitmapOutputDev::updateLineCap(GfxState *state)
1092 {
1093     boolpolydev->updateLineCap(state);
1094     booltextdev->updateLineCap(state);
1095     rgbdev->updateLineCap(state);
1096     clip0dev->updateLineCap(state);
1097     clip1dev->updateLineCap(state);
1098     gfxdev->updateLineCap(state);
1099 }
1100 void BitmapOutputDev::updateMiterLimit(GfxState *state)
1101 {
1102     boolpolydev->updateMiterLimit(state);
1103     booltextdev->updateMiterLimit(state);
1104     rgbdev->updateMiterLimit(state);
1105     clip0dev->updateMiterLimit(state);
1106     clip1dev->updateMiterLimit(state);
1107     gfxdev->updateMiterLimit(state);
1108 }
1109 void BitmapOutputDev::updateLineWidth(GfxState *state)
1110 {
1111     boolpolydev->updateLineWidth(state);
1112     booltextdev->updateLineWidth(state);
1113     rgbdev->updateLineWidth(state);
1114     clip0dev->updateLineWidth(state);
1115     clip1dev->updateLineWidth(state);
1116     gfxdev->updateLineWidth(state);
1117 }
1118 void BitmapOutputDev::updateStrokeAdjust(GfxState *state)
1119 {
1120     boolpolydev->updateStrokeAdjust(state);
1121     booltextdev->updateStrokeAdjust(state);
1122     rgbdev->updateStrokeAdjust(state);
1123     clip0dev->updateStrokeAdjust(state);
1124     clip1dev->updateStrokeAdjust(state);
1125     gfxdev->updateStrokeAdjust(state);
1126 }
1127 void BitmapOutputDev::updateFillColorSpace(GfxState *state)
1128 {
1129     boolpolydev->updateFillColorSpace(state);
1130     booltextdev->updateFillColorSpace(state);
1131     rgbdev->updateFillColorSpace(state);
1132     clip0dev->updateFillColorSpace(state);
1133     clip1dev->updateFillColorSpace(state);
1134     gfxdev->updateFillColorSpace(state);
1135 }
1136 void BitmapOutputDev::updateStrokeColorSpace(GfxState *state)
1137 {
1138     boolpolydev->updateStrokeColorSpace(state);
1139     booltextdev->updateStrokeColorSpace(state);
1140     rgbdev->updateStrokeColorSpace(state);
1141     clip0dev->updateStrokeColorSpace(state);
1142     clip1dev->updateStrokeColorSpace(state);
1143     gfxdev->updateStrokeColorSpace(state);
1144 }
1145 void BitmapOutputDev::updateFillColor(GfxState *state)
1146 {
1147     boolpolydev->updateFillColor(state);
1148     booltextdev->updateFillColor(state);
1149     rgbdev->updateFillColor(state);
1150     clip0dev->updateFillColor(state);
1151     clip1dev->updateFillColor(state);
1152     gfxdev->updateFillColor(state);
1153 }
1154 void BitmapOutputDev::updateStrokeColor(GfxState *state)
1155 {
1156     boolpolydev->updateStrokeColor(state);
1157     booltextdev->updateStrokeColor(state);
1158     rgbdev->updateStrokeColor(state);
1159     clip0dev->updateStrokeColor(state);
1160     clip1dev->updateStrokeColor(state);
1161     gfxdev->updateStrokeColor(state);
1162 }
1163 void BitmapOutputDev::updateBlendMode(GfxState *state)
1164 {
1165     boolpolydev->updateBlendMode(state);
1166     booltextdev->updateBlendMode(state);
1167     rgbdev->updateBlendMode(state);
1168     clip0dev->updateBlendMode(state);
1169     clip1dev->updateBlendMode(state);
1170     gfxdev->updateBlendMode(state);
1171 }
1172 void BitmapOutputDev::updateFillOpacity(GfxState *state)
1173 {
1174     boolpolydev->updateFillOpacity(state);
1175     booltextdev->updateFillOpacity(state);
1176     rgbdev->updateFillOpacity(state);
1177     clip0dev->updateFillOpacity(state);
1178     clip1dev->updateFillOpacity(state);
1179     gfxdev->updateFillOpacity(state);
1180 }
1181 void BitmapOutputDev::updateStrokeOpacity(GfxState *state)
1182 {
1183     boolpolydev->updateStrokeOpacity(state);
1184     booltextdev->updateStrokeOpacity(state);
1185     rgbdev->updateStrokeOpacity(state);
1186     clip0dev->updateStrokeOpacity(state);
1187     clip1dev->updateStrokeOpacity(state);
1188     gfxdev->updateStrokeOpacity(state);
1189 }
1190 void BitmapOutputDev::updateFillOverprint(GfxState *state)
1191 {
1192     boolpolydev->updateFillOverprint(state);
1193     booltextdev->updateFillOverprint(state);
1194     rgbdev->updateFillOverprint(state);
1195     clip0dev->updateFillOverprint(state);
1196     clip1dev->updateFillOverprint(state);
1197     gfxdev->updateFillOverprint(state);
1198 }
1199 void BitmapOutputDev::updateStrokeOverprint(GfxState *state)
1200 {
1201     boolpolydev->updateStrokeOverprint(state);
1202     booltextdev->updateStrokeOverprint(state);
1203     rgbdev->updateStrokeOverprint(state);
1204     clip0dev->updateStrokeOverprint(state);
1205     clip1dev->updateStrokeOverprint(state);
1206     gfxdev->updateStrokeOverprint(state);
1207 }
1208 void BitmapOutputDev::updateTransfer(GfxState *state)
1209 {
1210     boolpolydev->updateTransfer(state);
1211     booltextdev->updateTransfer(state);
1212     rgbdev->updateTransfer(state);
1213     clip0dev->updateTransfer(state);
1214     clip1dev->updateTransfer(state);
1215     gfxdev->updateTransfer(state);
1216 }
1217
1218 void BitmapOutputDev::updateFont(GfxState *state)
1219 {
1220     boolpolydev->updateFont(state);
1221     booltextdev->updateFont(state);
1222     rgbdev->updateFont(state);
1223     clip0dev->updateFont(state);
1224     clip1dev->updateFont(state);
1225     gfxdev->updateFont(state);
1226 }
1227 void BitmapOutputDev::updateTextMat(GfxState *state)
1228 {
1229     boolpolydev->updateTextMat(state);
1230     booltextdev->updateTextMat(state);
1231     rgbdev->updateTextMat(state);
1232     clip0dev->updateTextMat(state);
1233     clip1dev->updateTextMat(state);
1234     gfxdev->updateTextMat(state);
1235 }
1236 void BitmapOutputDev::updateCharSpace(GfxState *state)
1237 {
1238     boolpolydev->updateCharSpace(state);
1239     booltextdev->updateCharSpace(state);
1240     rgbdev->updateCharSpace(state);
1241     clip0dev->updateCharSpace(state);
1242     clip1dev->updateCharSpace(state);
1243     gfxdev->updateCharSpace(state);
1244 }
1245 void BitmapOutputDev::updateRender(GfxState *state)
1246 {
1247     boolpolydev->updateRender(state);
1248     booltextdev->updateRender(state);
1249     rgbdev->updateRender(state);
1250     clip0dev->updateRender(state);
1251     clip1dev->updateRender(state);
1252     gfxdev->updateRender(state);
1253 }
1254 void BitmapOutputDev::updateRise(GfxState *state)
1255 {
1256     boolpolydev->updateRise(state);
1257     booltextdev->updateRise(state);
1258     rgbdev->updateRise(state);
1259     clip0dev->updateRise(state);
1260     clip1dev->updateRise(state);
1261     gfxdev->updateRise(state);
1262 }
1263 void BitmapOutputDev::updateWordSpace(GfxState *state)
1264 {
1265     boolpolydev->updateWordSpace(state);
1266     booltextdev->updateWordSpace(state);
1267     rgbdev->updateWordSpace(state);
1268     clip0dev->updateWordSpace(state);
1269     clip1dev->updateWordSpace(state);
1270     gfxdev->updateWordSpace(state);
1271 }
1272 void BitmapOutputDev::updateHorizScaling(GfxState *state)
1273 {
1274     boolpolydev->updateHorizScaling(state);
1275     booltextdev->updateHorizScaling(state);
1276     rgbdev->updateHorizScaling(state);
1277     clip0dev->updateHorizScaling(state);
1278     clip1dev->updateHorizScaling(state);
1279     gfxdev->updateHorizScaling(state);
1280 }
1281 void BitmapOutputDev::updateTextPos(GfxState *state)
1282 {
1283     boolpolydev->updateTextPos(state);
1284     booltextdev->updateTextPos(state);
1285     rgbdev->updateTextPos(state);
1286     clip0dev->updateTextPos(state);
1287     clip1dev->updateTextPos(state);
1288     gfxdev->updateTextPos(state);
1289 }
1290 void BitmapOutputDev::updateTextShift(GfxState *state, double shift)
1291 {
1292     boolpolydev->updateTextShift(state, shift);
1293     booltextdev->updateTextShift(state, shift);
1294     rgbdev->updateTextShift(state, shift);
1295     clip0dev->updateTextShift(state, shift);
1296     clip1dev->updateTextShift(state, shift);
1297     gfxdev->updateTextShift(state, shift);
1298 }
1299
1300 double max(double x, double y) 
1301 {
1302     return x>y?x:y;
1303 }
1304 double min(double x, double y) 
1305 {
1306     return x<y?x:y;
1307 }
1308
1309 gfxbbox_t BitmapOutputDev::getBBox(GfxState*state)
1310 {
1311     GfxPath * path = state->getPath();
1312     int num = path->getNumSubpaths();
1313     gfxbbox_t bbox = {0,0,1,1};
1314     char valid=0;
1315     int t;
1316     for(t = 0; t < num; t++) {
1317         GfxSubpath *subpath = path->getSubpath(t);
1318         int subnum = subpath->getNumPoints();
1319         int s;
1320         for(s=0;s<subnum;s++) {
1321            double x,y;
1322            state->transform(subpath->getX(s),subpath->getY(s),&x,&y);
1323            if(!valid) {
1324                bbox.xmin = x; bbox.ymin = y;
1325                bbox.xmax = x; bbox.ymax = y;
1326                valid = 1;
1327            } else {
1328                bbox.xmin = min(bbox.xmin, x);
1329                bbox.ymin = min(bbox.ymin, y);
1330                bbox.xmax = max(bbox.xmax, x);
1331                bbox.ymax = max(bbox.ymax, y);
1332            }
1333         }
1334     }
1335     return bbox;
1336 }
1337
1338 void BitmapOutputDev::stroke(GfxState *state)
1339 {
1340     msg("<debug> stroke");
1341     boolpolydev->stroke(state);
1342     gfxbbox_t bbox = getBBox(state);
1343     double width = state->getTransformedLineWidth();
1344     bbox.xmin -= width; bbox.ymin -= width;
1345     bbox.xmax += width; bbox.ymax += width;
1346     checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1347     rgbdev->stroke(state);
1348     dbg_newdata("stroke");
1349 }
1350
1351 extern gfxcolor_t getFillColor(GfxState * state);
1352
1353 char area_is_plain_colored(GfxState*state, SplashBitmap*boolpoly, SplashBitmap*rgbbitmap, int x1, int y1, int x2, int y2)
1354 {
1355     int width = boolpoly->getWidth();
1356     int height = boolpoly->getHeight();
1357     if(!fixBBox(&x1, &y1, &x2, &y2, width, height)) {
1358         return 0;
1359     }
1360     gfxcolor_t color = getFillColor(state);
1361     SplashColorPtr rgb = rgbbitmap->getDataPtr() 
1362                        + (y1*width+x1)*sizeof(SplashColor);
1363     int width8 = (width+7)/8;
1364     unsigned char*bits = (unsigned char*)boolpoly->getDataPtr() 
1365                          + (y1*width8+x1);
1366     int x,y;
1367     int w = x2-x1;
1368     int h = y2-y1;
1369     for(y=0;y<h;y++) {
1370         for(x=0;x<w;x++) {
1371             if(rgb[x*3+0] != color.r ||
1372                rgb[x*3+1] != color.g ||
1373                rgb[x*3+2] != color.b)
1374                 return 0;
1375         }
1376         rgb += width*sizeof(SplashColor);
1377     }
1378     return 1;
1379 }
1380
1381 void BitmapOutputDev::fill(GfxState *state)
1382 {
1383     msg("<debug> fill");
1384     boolpolydev->fill(state);
1385     gfxbbox_t bbox = getBBox(state);
1386     if(config_optimizeplaincolorfills) {
1387         if(area_is_plain_colored(state, boolpolybitmap, rgbbitmap, bbox.xmin, bbox.ymin, bbox.xmax, bbox.ymax)) {
1388             return;
1389         }
1390     }
1391     checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1392     rgbdev->fill(state);
1393     dbg_newdata("fill");
1394 }
1395 void BitmapOutputDev::eoFill(GfxState *state)
1396 {
1397     msg("<debug> eoFill");
1398     boolpolydev->eoFill(state);
1399     gfxbbox_t bbox = getBBox(state);
1400     checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1401     rgbdev->eoFill(state);
1402     dbg_newdata("eofill");
1403 }
1404 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
1405 void BitmapOutputDev::tilingPatternFill(GfxState *state, Object *str,
1406                                int paintType, Dict *resDict,
1407                                double *mat, double *bbox,
1408                                int x0, int y0, int x1, int y1,
1409                                double xStep, double yStep)
1410 {
1411     msg("<debug> tilingPatternFill");
1412     boolpolydev->tilingPatternFill(state, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
1413     checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1414     rgbdev->tilingPatternFill(state, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
1415     dbg_newdata("tilingpatternfill");
1416 }
1417 #else
1418 void BitmapOutputDev::tilingPatternFill(GfxState *state, Gfx *gfx, Object *str,
1419                                int paintType, Dict *resDict,
1420                                double *mat, double *bbox,
1421                                int x0, int y0, int x1, int y1,
1422                                double xStep, double yStep) 
1423 {
1424     msg("<debug> tilingPatternFill");
1425     boolpolydev->tilingPatternFill(state, gfx, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
1426     checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1427     rgbdev->tilingPatternFill(state, gfx, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
1428     dbg_newdata("tilingpatternfill");
1429 }
1430 #endif
1431
1432 GBool BitmapOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading) 
1433 {
1434     msg("<debug> functionShadedFill");
1435     boolpolydev->functionShadedFill(state, shading);
1436     checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1437     return rgbdev->functionShadedFill(state, shading);
1438 }
1439 GBool BitmapOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading)
1440 {
1441     msg("<debug> axialShadedFill");
1442     boolpolydev->axialShadedFill(state, shading);
1443     checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1444     return rgbdev->axialShadedFill(state, shading);
1445 }
1446 GBool BitmapOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading)
1447 {
1448     msg("<debug> radialShadedFill");
1449     boolpolydev->radialShadedFill(state, shading);
1450     checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1451     return rgbdev->radialShadedFill(state, shading);
1452 }
1453
1454 SplashColor black = {0,0,0};
1455 SplashColor white = {255,255,255};
1456
1457 void BitmapOutputDev::clip(GfxState *state)
1458 {
1459     msg("<debug> clip");
1460     boolpolydev->clip(state);
1461     booltextdev->clip(state);
1462     rgbdev->clip(state);
1463     clip1dev->clip(state);
1464 }
1465 void BitmapOutputDev::eoClip(GfxState *state)
1466 {
1467     msg("<debug> eoClip");
1468     boolpolydev->eoClip(state);
1469     booltextdev->eoClip(state);
1470     rgbdev->eoClip(state);
1471     clip1dev->eoClip(state);
1472 }
1473 void BitmapOutputDev::clipToStrokePath(GfxState *state)
1474 {
1475     msg("<debug> clipToStrokePath");
1476     boolpolydev->clipToStrokePath(state);
1477     booltextdev->clipToStrokePath(state);
1478     rgbdev->clipToStrokePath(state);
1479     clip1dev->clipToStrokePath(state);
1480 }
1481
1482 void BitmapOutputDev::beginStringOp(GfxState *state)
1483 {
1484     msg("<debug> beginStringOp");
1485     clip0dev->beginStringOp(state);
1486     clip1dev->beginStringOp(state);
1487     booltextdev->beginStringOp(state);
1488     gfxdev->beginStringOp(state);
1489 }
1490 void BitmapOutputDev::beginString(GfxState *state, GString *s)
1491 {
1492     msg("<debug> beginString");
1493     clip0dev->beginString(state, s);
1494     clip1dev->beginString(state, s);
1495     booltextdev->beginString(state, s);
1496     gfxdev->beginString(state, s);
1497 }
1498
1499 void BitmapOutputDev::clearClips()
1500 {
1501     clearBooleanBitmap(clip0bitmap, 0, 0, 0, 0);
1502     clearBooleanBitmap(clip1bitmap, 0, 0, 0, 0);
1503 }
1504 void BitmapOutputDev::clearBoolPolyDev()
1505 {
1506     clearBooleanBitmap(stalepolybitmap, 0, 0, stalepolybitmap->getWidth(), stalepolybitmap->getHeight());
1507 }
1508 void BitmapOutputDev::clearBoolTextDev()
1509 {
1510     clearBooleanBitmap(staletextbitmap, 0, 0, staletextbitmap->getWidth(), staletextbitmap->getHeight());
1511 }
1512 void BitmapOutputDev::drawChar(GfxState *state, double x, double y,
1513                       double dx, double dy,
1514                       double originX, double originY,
1515                       CharCode code, int nBytes, Unicode *u, int uLen)
1516 {
1517     msg("<debug> drawChar render=%d", state->getRender());
1518
1519     if(state->getRender()&RENDER_CLIP) {
1520         //char is just a clipping boundary
1521         rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1522         boolpolydev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1523         booltextdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1524         clip1dev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1525     } else if(state->getRender()&RENDER_STROKE) {
1526         // we're drawing as stroke
1527         boolpolydev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1528         rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1529     } else if(rgbbitmap != rgbdev->getBitmap()) {
1530         // we're doing softmasking or transparency grouping
1531         boolpolydev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1532         rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1533     } else {
1534         // we're drawing a regular char
1535         clearClips();
1536         clip0dev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1537         clip1dev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1538    
1539         /* calculate the bbox of this character */
1540         int x1 = (int)x, x2 = (int)x+1, y1 = (int)y, y2 = (int)y+1;
1541         SplashFont*font = clip0dev->getCurrentFont();
1542         SplashPath*path = font?font->getGlyphPath(code):NULL;
1543         x-=originX;
1544         y-=originY;
1545         if(path) {
1546             path->offset((SplashCoord)x, (SplashCoord)y);
1547             int t;
1548             for(t=0;t<path->getLength();t++) {
1549                 double xx,yy;
1550                 Guchar f;
1551                 path->getPoint(t,&xx,&yy,&f);
1552                 state->transform(xx,yy,&xx,&yy);
1553                 if(xx<x1) x1=(int)xx;
1554                 if(yy<y1) y1=(int)yy;
1555                 if(xx>=x2) x2=(int)xx+1;
1556                 if(yy>=y2) y2=(int)yy+1;
1557             }
1558             delete(path);path=0;
1559         }
1560
1561         /* if this character is affected somehow by the various clippings (i.e., it looks
1562            different on a device without clipping), then draw it on the bitmap, not as
1563            text */
1564         if(clip0and1differ(x1,y1,x2,y2)) {
1565             msg("<verbose> Char %d is affected by clipping", code);
1566             boolpolydev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1567             checkNewBitmap(x1,y1,x2,y2);
1568             rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1569             if(config_extrafontdata) {
1570                 int oldrender = state->getRender();
1571                 state->setRender(3); //invisible
1572                 gfxdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1573                 state->setRender(oldrender);
1574             }
1575         } else {
1576
1577             /* this char is not at all affected by clipping. 
1578                Now just dump out the bitmap we're currently working on, if necessary. */
1579             booltextdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1580             checkNewText(x1,y1,x2,y2);
1581             /* use polygonal output device to do the actual text handling */
1582             gfxdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1583         }
1584     }
1585     dbg_newdata("text");
1586 }
1587 void BitmapOutputDev::drawString(GfxState *state, GString *s)
1588 {
1589     msg("<error> internal error: drawString not implemented");
1590     return;
1591 }
1592 void BitmapOutputDev::endTextObject(GfxState *state)
1593 {
1594     msg("<debug> endTextObject");
1595     rgbdev->endTextObject(state);
1596     clip0dev->endTextObject(state);
1597     clip1dev->endTextObject(state);
1598     booltextdev->endTextObject(state);
1599     /* the only thing "drawn" here is clipping */
1600     //checkNewText(UNKNOWN_BOUNDING_BOX);
1601     gfxdev->endTextObject(state);
1602     dbg_newdata("endtextobject");
1603 }
1604 void BitmapOutputDev::endString(GfxState *state)
1605 {
1606     msg("<debug> endString");
1607     clip0dev->endString(state);
1608     clip1dev->endString(state);
1609     booltextdev->endString(state);
1610     int render = state->getRender();
1611     if(render != RENDER_INVISIBLE && render != RENDER_FILL) {
1612         /* xpdf draws things like stroke text or fill+stroke text in the
1613            endString() method */
1614         checkNewText(UNKNOWN_BOUNDING_BOX);
1615     }
1616     gfxdev->endString(state);
1617     dbg_newdata("endstring");
1618 }
1619 void BitmapOutputDev::endStringOp(GfxState *state)
1620 {
1621     msg("<debug> endStringOp");
1622     clip0dev->endStringOp(state);
1623     clip1dev->endStringOp(state);
1624     booltextdev->endStringOp(state);
1625     gfxdev->endStringOp(state);
1626     dbg_newdata("endstringop");
1627 }
1628
1629 /* TODO: these four operations below *should* do nothing, as type3
1630          chars are drawn using operations like fill() */
1631 GBool BitmapOutputDev::beginType3Char(GfxState *state, double x, double y,
1632                              double dx, double dy,
1633                              CharCode code, Unicode *u, int uLen)
1634 {
1635     msg("<debug> beginType3Char");
1636     /* call gfxdev so that it can generate "invisible" characters
1637        on top of the actual graphic content, for text extraction */
1638     return gfxdev->beginType3Char(state, x, y, dx, dy, code, u, uLen);
1639 }
1640 void BitmapOutputDev::type3D0(GfxState *state, double wx, double wy)
1641 {
1642     msg("<debug> type3D0");
1643     return gfxdev->type3D0(state, wx, wy);
1644 }
1645 void BitmapOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
1646 {
1647     msg("<debug> type3D1");
1648     return gfxdev->type3D1(state, wx, wy, llx, lly, urx, ury);
1649 }
1650 void BitmapOutputDev::endType3Char(GfxState *state)
1651 {
1652     msg("<debug> endType3Char");
1653     gfxdev->endType3Char(state);
1654 }
1655
1656 class CopyStream: public Object
1657 {
1658     Dict*dict;
1659     char*buf;
1660     MemStream*memstream;
1661     public:
1662     CopyStream(Stream*str, int len)
1663     {
1664         buf = 0;
1665         str->reset();
1666         if(len) {
1667             buf = (char*)malloc(len);
1668             int t;
1669             for (t=0; t<len; t++)
1670               buf[t] = str->getChar();
1671         }
1672         str->close();
1673         this->dict = str->getDict();
1674         this->memstream = new MemStream(buf, 0, len, this);
1675     }
1676     ~CopyStream() 
1677     {
1678         ::free(this->buf);this->buf = 0;
1679         delete this->memstream;
1680     }
1681     Dict* getDict() {return dict;}
1682     Stream* getStream() {return this->memstream;};
1683 };
1684
1685 gfxbbox_t BitmapOutputDev::getImageBBox(GfxState*state)
1686 {
1687     gfxbbox_t bbox;
1688     double x,y;
1689     state->transform(0, 1, &x, &y);
1690     bbox.xmin=bbox.xmax = x;
1691     bbox.ymin=bbox.ymax = y;
1692     state->transform(0, 0, &x, &y);
1693     bbox.xmin=min(bbox.xmin,x);
1694     bbox.ymin=min(bbox.ymin,y);
1695     bbox.xmax=max(bbox.xmax,x);
1696     bbox.ymax=max(bbox.ymax,y);
1697     state->transform(1, 0, &x, &y);
1698     bbox.xmin=min(bbox.xmin,x);
1699     bbox.ymin=min(bbox.ymin,y);
1700     bbox.xmax=max(bbox.xmax,x);
1701     bbox.ymax=max(bbox.ymax,y);
1702     state->transform(1, 1, &x, &y);
1703     bbox.xmin=min(bbox.xmin,x);
1704     bbox.ymin=min(bbox.ymin,y);
1705     bbox.xmax=max(bbox.xmax,x);
1706     bbox.ymax=max(bbox.ymax,y);
1707     return bbox;
1708 }
1709 void BitmapOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1710                            int width, int height, GBool invert,
1711                            GBool inlineImg)
1712 {
1713     msg("<debug> drawImageMask streamkind=%d", str->getKind());
1714
1715     CopyStream*cpystr = new CopyStream(str, height * ((width + 7) / 8));
1716     str = cpystr->getStream();
1717     
1718     boolpolydev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
1719     gfxbbox_t bbox = getImageBBox(state);
1720     checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1721     rgbdev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
1722     delete cpystr;
1723     dbg_newdata("imagemask");
1724 }
1725 void BitmapOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1726                        int width, int height, GfxImageColorMap *colorMap,
1727                        int *maskColors, GBool inlineImg)
1728 {
1729     msg("<debug> drawImage streamkind=%d", str->getKind());
1730         
1731     CopyStream*cpystr = new CopyStream(str, height * ((width * colorMap->getNumPixelComps() * colorMap->getBits() + 7) / 8));
1732     str = cpystr->getStream();
1733
1734     boolpolydev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
1735     gfxbbox_t bbox=getImageBBox(state);
1736     checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1737     rgbdev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
1738     delete cpystr;
1739     dbg_newdata("image");
1740 }
1741 void BitmapOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
1742                              int width, int height,
1743                              GfxImageColorMap *colorMap,
1744                              Stream *maskStr, int maskWidth, int maskHeight,
1745                              GBool maskInvert)
1746 {
1747     msg("<debug> drawMaskedImage streamkind=%d", str->getKind());
1748     
1749     CopyStream*cpystr = new CopyStream(str, height * ((width * colorMap->getNumPixelComps() * colorMap->getBits() + 7) / 8));
1750     str = cpystr->getStream();
1751
1752     boolpolydev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
1753     gfxbbox_t bbox=getImageBBox(state);
1754     checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1755     rgbdev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
1756     delete cpystr;
1757     dbg_newdata("maskedimage");
1758 }
1759 void BitmapOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
1760                                  int width, int height,
1761                                  GfxImageColorMap *colorMap,
1762                                  Stream *maskStr,
1763                                  int maskWidth, int maskHeight,
1764                                  GfxImageColorMap *maskColorMap)
1765 {
1766     msg("<debug> drawSoftMaskedImage %dx%d (%dx%d) streamkind=%d", width, height, maskWidth, maskHeight, str->getKind());
1767
1768     CopyStream*cpystr = new CopyStream(str, height * ((width * colorMap->getNumPixelComps() * colorMap->getBits() + 7) / 8));
1769     str = cpystr->getStream();
1770
1771     boolpolydev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
1772     gfxbbox_t bbox=getImageBBox(state);
1773     checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1774     rgbdev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
1775     delete cpystr;
1776     dbg_newdata("softmaskimage");
1777 }
1778 void BitmapOutputDev::drawForm(Ref id)
1779 {
1780     msg("<debug> drawForm");
1781     boolpolydev->drawForm(id);
1782     checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1783     rgbdev->drawForm(id);
1784 }
1785
1786 void BitmapOutputDev::processLink(Link *link, Catalog *catalog)
1787 {
1788     msg("<debug> processLink");
1789     gfxdev->processLink(link, catalog);
1790 }
1791
1792 void BitmapOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
1793                                     GfxColorSpace *blendingColorSpace,
1794                                     GBool isolated, GBool knockout,
1795                                     GBool forSoftMask)
1796 {
1797     msg("<debug> beginTransparencyGroup");
1798 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
1799     GfxState*state1 = state->copy();
1800     GfxState*state2 = state->copy();
1801     state1->setPath(0);
1802     state1->setPath(state->getPath()->copy());
1803     state2->setPath(0);
1804     state2->setPath(state->getPath()->copy());
1805 #else
1806     GfxState*state1 = state->copy(gTrue);
1807     GfxState*state2 = state->copy(gTrue);
1808 #endif
1809     boolpolydev->beginTransparencyGroup(state1, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1810     rgbdev->beginTransparencyGroup(state2, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1811     clip1dev->beginTransparencyGroup(state, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1812     delete state1;
1813     delete state2;
1814     dbg_newdata("endtransparencygroup");
1815 }
1816 void BitmapOutputDev::endTransparencyGroup(GfxState *state)
1817 {
1818     msg("<debug> endTransparencyGroup");
1819 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
1820     GfxState*state1 = state->copy();
1821     GfxState*state2 = state->copy();
1822     state1->setPath(0);
1823     state1->setPath(state->getPath()->copy());
1824     state2->setPath(0);
1825     state2->setPath(state->getPath()->copy());
1826 #else
1827     GfxState*state1 = state->copy(gTrue);
1828     GfxState*state2 = state->copy(gTrue);
1829 #endif
1830     boolpolydev->endTransparencyGroup(state1);
1831     checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1832     rgbdev->endTransparencyGroup(state2);
1833     delete state1;
1834     delete state2;
1835     clip1dev->endTransparencyGroup(state);
1836     dbg_newdata("endtransparencygroup");
1837 }
1838 void BitmapOutputDev::paintTransparencyGroup(GfxState *state, double *bbox)
1839 {
1840     msg("<debug> paintTransparencyGroup");
1841     boolpolydev->paintTransparencyGroup(state,bbox);
1842     checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1843     rgbdev->paintTransparencyGroup(state,bbox);
1844     clip1dev->paintTransparencyGroup(state,bbox);
1845     dbg_newdata("painttransparencygroup");
1846 }
1847 void BitmapOutputDev::setSoftMask(GfxState *state, double *bbox, GBool alpha, Function *transferFunc, GfxColor *backdropColor)
1848 {
1849     msg("<debug> setSoftMask");
1850     boolpolydev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1851     checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1852     rgbdev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1853     clip1dev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1854     dbg_newdata("setsoftmask");
1855 }
1856 void BitmapOutputDev::clearSoftMask(GfxState *state)
1857 {
1858     msg("<debug> clearSoftMask");
1859     boolpolydev->clearSoftMask(state);
1860     checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1861     rgbdev->clearSoftMask(state);
1862     clip1dev->clearSoftMask(state);
1863     dbg_newdata("clearsoftmask");
1864 }