polygon intersector: added horizontal line reconstruction
[swftools.git] / lib / pdf / FullBitmapOutputDev.cc
1 /* FullBitmapOutputDev.cc
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 "config.h"
23 #include "FullBitmapOutputDev.h"
24 #include "GFXOutputDev.h"
25 #include "SplashBitmap.h"
26 #include "SplashPattern.h"
27 #include "Splash.h"
28 #include "../log.h"
29 #include "../png.h"
30 #include "../devices/record.h"
31
32 static SplashColor splash_white = {255,255,255};
33 static SplashColor splash_black = {0,0,0};
34     
35 FullBitmapOutputDev::FullBitmapOutputDev(InfoOutputDev*info, PDFDoc*doc)
36 {
37     this->doc = doc;
38     this->xref = doc->getXRef();
39     
40     msg("<verbose> Rendering everything to a bitmap");
41     
42     /* color graphic output device, for creating bitmaps */
43     this->rgbdev = new SplashOutputDev(splashModeRGB8, 1, gFalse, splash_white, gTrue, gTrue);
44   
45     /* device for handling links */
46     this->gfxdev = new GFXOutputDev(info, this->doc);
47
48     this->rgbdev->startDoc(this->xref);
49 }
50 FullBitmapOutputDev::~FullBitmapOutputDev()
51 {
52     if(this->rgbdev) {
53         delete this->rgbdev;this->rgbdev = 0;
54     }
55     if(this->gfxdev) {
56         delete this->gfxdev;this->gfxdev= 0;
57     }
58 }
59
60 GBool FullBitmapOutputDev::getVectorAntialias()
61 {
62     return this->rgbdev->getVectorAntialias();
63 }
64 void FullBitmapOutputDev::setVectorAntialias(GBool vaa)
65 {
66     this->rgbdev->setVectorAntialias(vaa);
67 }
68 void FullBitmapOutputDev::setDevice(gfxdevice_t*dev)
69 {
70     this->dev = dev;
71     gfxdev->setDevice(dev);
72 }
73 void FullBitmapOutputDev::setMove(int x,int y)
74 {
75     this->user_movex = x;
76     this->user_movey = y;
77     gfxdev->setMove(x,y);
78 }
79 void FullBitmapOutputDev::setClip(int x1,int y1,int x2,int y2)
80 {
81     this->user_clipx1 = x1;
82     this->user_clipy1 = y1;
83     this->user_clipx2 = x2;
84     this->user_clipy2 = y2;
85     gfxdev->setClip(x1,y1,x2,y2);
86 }
87 void FullBitmapOutputDev::setParameter(const char*key, const char*value)
88 {
89 }
90 void FullBitmapOutputDev::setPageMap(int*pagemap, int pagemap_len)
91 {
92     gfxdev->setPageMap(pagemap, pagemap_len);
93 }
94
95 static void getBitmapBBox(Guchar*alpha, int width, int height, int*xmin, int*ymin, int*xmax, int*ymax)
96 {
97     *ymin = -1;
98     *xmin = width;
99     *xmax = 0;
100     int x,y;
101     for(y=0;y<height;y++) {
102         Guchar*a = &alpha[y*width];
103         for(x=0;x<width;x++) {
104             if(a[x]) break;
105         }
106         int left = x; //first occupied pixel from left
107         int right = x+1; //last non-occupied pixel from right
108         for(;x<width;x++) {
109             if(a[x]) right=x+1;
110         }
111
112         if(left!=width) {
113             if(*ymin<0) 
114                 *ymin=y;
115             *ymax=y+1;
116             if(left<*xmin) *xmin = left;
117             if(right>*xmax) *xmax = right;
118         }
119     }
120     if(*xmin>=*xmax || *ymin>=*ymax) {
121         *xmin = 0;
122         *ymin = 0;
123         *xmax = 0;
124         *ymax = 0;
125     }
126 }
127
128 void FullBitmapOutputDev::flushBitmap()
129 {
130     int width = rgbdev->getBitmapWidth();
131     int height = rgbdev->getBitmapHeight();
132     
133     SplashColorPtr rgb = rgbdev->getBitmap()->getDataPtr();
134     Guchar*alpha = rgbdev->getBitmap()->getAlphaPtr();
135
136     int xmin,ymin,xmax,ymax;
137     getBitmapBBox(alpha, width, height, &xmin,&ymin,&xmax,&ymax);
138
139     /* clip against (-movex, -movey, -movex+width, -movey+height) */
140     if(xmin < -this->movex) xmin = -this->movex;
141     if(ymin < -this->movey) ymin = -this->movey;
142     if(xmax > -this->movex + width) xmax = -this->movex+this->width;
143     if(ymax > -this->movey + height) ymax = -this->movey+this->height;
144
145     msg("<verbose> Flushing bitmap (bbox: %d,%d,%d,%d)", xmin,ymin,xmax,ymax);
146     
147     if((xmax-xmin)<=0 || (ymax-ymin)<=0) // no bitmap, nothing to do
148         return;
149
150     if(sizeof(SplashColor)!=3) {
151         msg("<error> sizeof(SplashColor)!=3");
152         return;
153     }
154     //xmin = ymin = 0;
155     //xmax = width;
156     //ymax = height;
157
158     int rangex = xmax-xmin;
159     int rangey = ymax-ymin;
160     gfximage_t*img = (gfximage_t*)malloc(sizeof(gfximage_t)); 
161     img->data = (gfxcolor_t*)malloc(rangex * rangey * 4);
162     img->width = rangex;
163     img->height = rangey;
164     int x,y;
165     for(y=0;y<rangey;y++) {
166         SplashColorPtr in=&rgb[((y+ymin)*width+xmin)*sizeof(SplashColor)];
167         gfxcolor_t*out = &img->data[y*rangex];
168         Guchar*ain = &alpha[(y+ymin)*width+xmin];
169         for(x=0;x<rangex;x++) {
170             // blend against a white background
171             out[x].r = (in[x*3+0]*ain[x])/255 + 255-ain[x];
172             out[x].g = (in[x*3+1]*ain[x])/255 + 255-ain[x];
173             out[x].b = (in[x*3+2]*ain[x])/255 + 255-ain[x];
174             out[x].a = 255;//ain[x];
175         }
176     }
177     /* transform bitmap rectangle to "device space" */
178     xmin += movex;
179     ymin += movey;
180     xmax += movex;
181     ymax += movey;
182
183     gfxmatrix_t m;
184     m.tx = xmin;
185     m.ty = ymin;
186     m.m00 = m.m11 = 1;
187     m.m10 = m.m01 = 0;
188
189     gfxline_t* line = gfxline_makerectangle(xmin, ymin, xmax, ymax);
190     dev->fillbitmap(dev, line, img, &m, 0);
191     gfxline_free(line);
192
193     free(img->data);img->data=0;free(img);img=0;
194 }
195
196 void FullBitmapOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
197 {
198     double x1,y1,x2,y2;
199     state->transform(crop_x1,crop_y1,&x1,&y1);
200     state->transform(crop_x2,crop_y2,&x2,&y2);
201     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
202     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
203     
204     this->movex = -(int)x1 - user_movex;
205     this->movey = -(int)y1 - user_movey;
206     
207     if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
208         x1 = user_clipx1;
209         x2 = user_clipx2;
210         y1 = user_clipy1;
211         y2 = user_clipy2;
212     }
213     this->width = (int)(x2-x1);
214     this->height = (int)(y2-y1);
215
216     msg("<debug> startPage");
217     rgbdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
218     gfxdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
219 }
220
221 void FullBitmapOutputDev::endPage()
222 {
223     msg("<verbose> endPage (FullBitmapOutputDev)");
224     flushBitmap();
225     rgbdev->endPage();
226     gfxdev->endPage();
227 }
228 GBool FullBitmapOutputDev::upsideDown()
229 {
230     return rgbdev->upsideDown();
231 }
232 GBool FullBitmapOutputDev::useDrawChar()
233 {
234     return rgbdev->useDrawChar();
235 }
236 GBool FullBitmapOutputDev::useTilingPatternFill()
237 {
238     return rgbdev->useTilingPatternFill();
239 }
240 GBool FullBitmapOutputDev::useShadedFills()
241 {
242     return rgbdev->useShadedFills();
243 }
244 GBool FullBitmapOutputDev::useDrawForm()
245 {
246     return rgbdev->useDrawForm();
247 }
248 GBool FullBitmapOutputDev::interpretType3Chars()
249 {
250     return rgbdev->interpretType3Chars();
251 }
252 GBool FullBitmapOutputDev::needNonText() 
253 {
254     return rgbdev->needNonText();
255 }
256 void FullBitmapOutputDev::setDefaultCTM(double *ctm) 
257 {
258     rgbdev->setDefaultCTM(ctm);
259     gfxdev->setDefaultCTM(ctm);
260 }
261 void FullBitmapOutputDev::saveState(GfxState *state) 
262 {
263     rgbdev->saveState(state);
264 }
265 void FullBitmapOutputDev::restoreState(GfxState *state) 
266 {
267     rgbdev->restoreState(state);
268 }
269 void FullBitmapOutputDev::updateAll(GfxState *state)
270 {
271     rgbdev->updateAll(state);
272 }
273 void FullBitmapOutputDev::updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32)
274 {
275     rgbdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
276     gfxdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
277 }
278 void FullBitmapOutputDev::updateLineDash(GfxState *state)
279 {
280     rgbdev->updateLineDash(state);
281 }
282 void FullBitmapOutputDev::updateFlatness(GfxState *state)
283 {
284     rgbdev->updateFlatness(state);
285 }
286 void FullBitmapOutputDev::updateLineJoin(GfxState *state)
287 {
288     rgbdev->updateLineJoin(state);
289 }
290 void FullBitmapOutputDev::updateLineCap(GfxState *state)
291 {
292     rgbdev->updateLineCap(state);
293 }
294 void FullBitmapOutputDev::updateMiterLimit(GfxState *state)
295 {
296     rgbdev->updateMiterLimit(state);
297 }
298 void FullBitmapOutputDev::updateLineWidth(GfxState *state)
299 {
300     rgbdev->updateLineWidth(state);
301 }
302 void FullBitmapOutputDev::updateStrokeAdjust(GfxState *state)
303 {
304     rgbdev->updateStrokeAdjust(state);
305 }
306 void FullBitmapOutputDev::updateFillColorSpace(GfxState *state)
307 {
308     rgbdev->updateFillColorSpace(state);
309 }
310 void FullBitmapOutputDev::updateStrokeColorSpace(GfxState *state)
311 {
312     rgbdev->updateStrokeColorSpace(state);
313 }
314 void FullBitmapOutputDev::updateFillColor(GfxState *state)
315 {
316     rgbdev->updateFillColor(state);
317 }
318 void FullBitmapOutputDev::updateStrokeColor(GfxState *state)
319 {
320     rgbdev->updateStrokeColor(state);
321 }
322 void FullBitmapOutputDev::updateBlendMode(GfxState *state)
323 {
324     rgbdev->updateBlendMode(state);
325 }
326 void FullBitmapOutputDev::updateFillOpacity(GfxState *state)
327 {
328     rgbdev->updateFillOpacity(state);
329 }
330 void FullBitmapOutputDev::updateStrokeOpacity(GfxState *state)
331 {
332     rgbdev->updateStrokeOpacity(state);
333 }
334 void FullBitmapOutputDev::updateFillOverprint(GfxState *state)
335 {
336     rgbdev->updateFillOverprint(state);
337 }
338 void FullBitmapOutputDev::updateStrokeOverprint(GfxState *state)
339 {
340     rgbdev->updateStrokeOverprint(state);
341 }
342 void FullBitmapOutputDev::updateTransfer(GfxState *state)
343 {
344     rgbdev->updateTransfer(state);
345 }
346 void FullBitmapOutputDev::updateFont(GfxState *state)
347 {
348     rgbdev->updateFont(state);
349 }
350 void FullBitmapOutputDev::updateTextMat(GfxState *state)
351 {
352     rgbdev->updateTextMat(state);
353 }
354 void FullBitmapOutputDev::updateCharSpace(GfxState *state)
355 {
356     rgbdev->updateCharSpace(state);
357 }
358 void FullBitmapOutputDev::updateRender(GfxState *state)
359 {
360     rgbdev->updateRender(state);
361 }
362 void FullBitmapOutputDev::updateRise(GfxState *state)
363 {
364     rgbdev->updateRise(state);
365 }
366 void FullBitmapOutputDev::updateWordSpace(GfxState *state)
367 {
368     rgbdev->updateWordSpace(state);
369 }
370 void FullBitmapOutputDev::updateHorizScaling(GfxState *state)
371 {
372     rgbdev->updateHorizScaling(state);
373 }
374 void FullBitmapOutputDev::updateTextPos(GfxState *state)
375 {
376     rgbdev->updateTextPos(state);
377 }
378 void FullBitmapOutputDev::updateTextShift(GfxState *state, double shift)
379 {
380     rgbdev->updateTextShift(state, shift);
381 }
382
383 void FullBitmapOutputDev::stroke(GfxState *state)
384 {
385     msg("<debug> stroke");
386     rgbdev->stroke(state);
387 }
388 void FullBitmapOutputDev::fill(GfxState *state)
389 {
390     msg("<debug> fill");
391     rgbdev->fill(state);
392 }
393 void FullBitmapOutputDev::eoFill(GfxState *state)
394 {
395     msg("<debug> eoFill");
396     rgbdev->eoFill(state);
397 }
398 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
399 void FullBitmapOutputDev::tilingPatternFill(GfxState *state, Object *str,
400                                int paintType, Dict *resDict,
401                                double *mat, double *bbox,
402                                int x0, int y0, int x1, int y1,
403                                double xStep, double yStep)
404 {
405     msg("<debug> tilingPatternFill");
406     rgbdev->tilingPatternFill(state, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
407 }
408 #else
409 void FullBitmapOutputDev::tilingPatternFill(GfxState *state, Gfx *gfx, Object *str,
410                                int paintType, Dict *resDict,
411                                double *mat, double *bbox,
412                                int x0, int y0, int x1, int y1,
413                                double xStep, double yStep) 
414 {
415     msg("<debug> tilingPatternFill");
416     rgbdev->tilingPatternFill(state, gfx, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
417 }
418 #endif
419
420 GBool FullBitmapOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading) 
421 {
422     msg("<debug> functionShadedFill");
423     return rgbdev->functionShadedFill(state, shading);
424 }
425 GBool FullBitmapOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading)
426 {
427     msg("<debug> axialShadedFill");
428     return rgbdev->axialShadedFill(state, shading);
429 }
430 GBool FullBitmapOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading)
431 {
432     msg("<debug> radialShadedFill");
433     return rgbdev->radialShadedFill(state, shading);
434 }
435
436 void FullBitmapOutputDev::clip(GfxState *state)
437 {
438     msg("<debug> clip");
439     rgbdev->clip(state);
440 }
441 void FullBitmapOutputDev::eoClip(GfxState *state)
442 {
443     msg("<debug> eoClip");
444     rgbdev->eoClip(state);
445 }
446 void FullBitmapOutputDev::clipToStrokePath(GfxState *state)
447 {
448     msg("<debug> clipToStrokePath");
449     rgbdev->clipToStrokePath(state);
450 }
451
452 void FullBitmapOutputDev::beginStringOp(GfxState *state)
453 {
454     msg("<debug> beginStringOp");
455     rgbdev->beginStringOp(state);
456 }
457 void FullBitmapOutputDev::endStringOp(GfxState *state)
458 {
459     msg("<debug> endStringOp");
460     rgbdev->endStringOp(state);
461 }
462 void FullBitmapOutputDev::beginString(GfxState *state, GString *s)
463 {
464     msg("<debug> beginString");
465     rgbdev->beginString(state, s);
466 }
467 void FullBitmapOutputDev::endString(GfxState *state)
468 {
469     msg("<debug> endString");
470     rgbdev->endString(state);
471 }
472 void FullBitmapOutputDev::drawChar(GfxState *state, double x, double y,
473                       double dx, double dy,
474                       double originX, double originY,
475                       CharCode code, int nBytes, Unicode *u, int uLen)
476 {
477     msg("<debug> drawChar");
478     rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
479 }
480 void FullBitmapOutputDev::drawString(GfxState *state, GString *s)
481 {
482     msg("<error> internal error: drawString not implemented");
483     rgbdev->drawString(state, s);
484 }
485 void FullBitmapOutputDev::endTextObject(GfxState *state)
486 {
487     /* FIXME: the below might render things (stroke outlines etc.) to gfxdev which
488               might end up unflushed- should be handled similarily as
489               drawChar() above
490      */
491     msg("<debug> endTextObject");
492     rgbdev->endTextObject(state);
493 }
494
495 /* TODO: these four operations below *should* do nothing, as type3
496          chars are drawn using operations like fill() */
497 GBool FullBitmapOutputDev::beginType3Char(GfxState *state, double x, double y,
498                              double dx, double dy,
499                              CharCode code, Unicode *u, int uLen)
500 {
501     msg("<debug> beginType3Char");
502     return rgbdev->beginType3Char(state, x, y, dx, dy, code, u, uLen);
503 }
504 void FullBitmapOutputDev::type3D0(GfxState *state, double wx, double wy)
505 {
506     msg("<debug> type3D0");
507     rgbdev->type3D0(state, wx, wy);
508 }
509 void FullBitmapOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
510 {
511     msg("<debug> type3D1");
512     rgbdev->type3D1(state, wx, wy, llx, lly, urx, ury);
513 }
514 void FullBitmapOutputDev::endType3Char(GfxState *state)
515 {
516     msg("<debug> endType3Char");
517     rgbdev->endType3Char(state);
518 }
519 void FullBitmapOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
520                            int width, int height, GBool invert,
521                            GBool inlineImg)
522 {
523     msg("<debug> drawImageMask");
524     rgbdev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
525 }
526 void FullBitmapOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
527                        int width, int height, GfxImageColorMap *colorMap,
528                        int *maskColors, GBool inlineImg)
529 {
530     msg("<debug> drawImage");
531     rgbdev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
532 }
533 void FullBitmapOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
534                              int width, int height,
535                              GfxImageColorMap *colorMap,
536                              Stream *maskStr, int maskWidth, int maskHeight,
537                              GBool maskInvert)
538 {
539     msg("<debug> drawMaskedImage");
540     rgbdev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
541 }
542 void FullBitmapOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
543                                  int width, int height,
544                                  GfxImageColorMap *colorMap,
545                                  Stream *maskStr,
546                                  int maskWidth, int maskHeight,
547                                  GfxImageColorMap *maskColorMap)
548 {
549     msg("<debug> drawSoftMaskedImage");
550     rgbdev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
551 }
552 void FullBitmapOutputDev::drawForm(Ref id)
553 {
554     msg("<debug> drawForm");
555     rgbdev->drawForm(id);
556 }
557
558 void FullBitmapOutputDev::processLink(Link *link, Catalog *catalog)
559 {
560     msg("<debug> processLink");
561     gfxdev->processLink(link, catalog);
562 }
563
564 void FullBitmapOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
565                                     GfxColorSpace *blendingColorSpace,
566                                     GBool isolated, GBool knockout,
567                                     GBool forSoftMask)
568 {
569     msg("<debug> beginTransparencyGroup");
570     rgbdev->beginTransparencyGroup(state, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
571 }
572 void FullBitmapOutputDev::endTransparencyGroup(GfxState *state)
573 {
574     msg("<debug> endTransparencyGroup");
575     rgbdev->endTransparencyGroup(state);
576 }
577 void FullBitmapOutputDev::paintTransparencyGroup(GfxState *state, double *bbox)
578 {
579     msg("<debug> paintTransparencyGroup");
580     rgbdev->paintTransparencyGroup(state,bbox);
581 }
582 void FullBitmapOutputDev::setSoftMask(GfxState *state, double *bbox, GBool alpha, Function *transferFunc, GfxColor *backdropColor)
583 {
584     msg("<debug> setSoftMask");
585     rgbdev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
586 }
587 void FullBitmapOutputDev::clearSoftMask(GfxState *state)
588 {
589     msg("<debug> clearSoftMask");
590     rgbdev->clearSoftMask(state);
591 }
592
593
594