device which creates a bitmap and links
[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::preparePage(int pdfpage, int outputpage)
91 {
92     gfxdev->preparePage(pdfpage, outputpage);
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             /* according to endPage()/compositeBackground() in xpdf/SplashOutputDev.cc, we
171                have to premultiply alpha (mix background and pixel according to the alpha channel).
172             */
173             out[x].r = (in[x*3+0]*ain[x])/255;
174             out[x].g = (in[x*3+1]*ain[x])/255;
175             out[x].b = (in[x*3+2]*ain[x])/255;
176             out[x].a = ain[x];
177         }
178     }
179     /* transform bitmap rectangle to "device space" */
180     xmin += movex;
181     ymin += movey;
182     xmax += movex;
183     ymax += movey;
184
185     gfxmatrix_t m;
186     m.tx = xmin;
187     m.ty = ymin;
188     m.m00 = m.m11 = 1;
189     m.m10 = m.m01 = 0;
190
191     gfxline_t* line = gfxline_makerectangle(xmin, ymin, xmax, ymax);
192     dev->fillbitmap(dev, line, img, &m, 0);
193     gfxline_free(line);
194
195     memset(rgbdev->getBitmap()->getAlphaPtr(), 0, rgbdev->getBitmap()->getWidth()*rgbdev->getBitmap()->getHeight());
196     memset(rgbdev->getBitmap()->getDataPtr(), 0, rgbdev->getBitmap()->getRowSize()*rgbdev->getBitmap()->getHeight());
197
198     free(img->data);img->data=0;free(img);img=0;
199 }
200
201 void FullBitmapOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
202 {
203     double x1,y1,x2,y2;
204     state->transform(crop_x1,crop_y1,&x1,&y1);
205     state->transform(crop_x2,crop_y2,&x2,&y2);
206     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
207     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
208     
209     this->movex = -(int)x1 - user_movex;
210     this->movey = -(int)y1 - user_movey;
211     
212     if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
213         x1 = user_clipx1;
214         x2 = user_clipx2;
215         y1 = user_clipy1;
216         y2 = user_clipy2;
217     }
218     this->width = (int)(x2-x1);
219     this->height = (int)(y2-y1);
220
221     msg("<debug> startPage");
222     rgbdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
223     gfxdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
224 }
225
226 void FullBitmapOutputDev::endPage()
227 {
228     msg("<verbose> endPage (FullBitmapOutputDev)");
229     flushBitmap();
230     rgbdev->endPage();
231     gfxdev->endPage();
232 }
233 GBool FullBitmapOutputDev::upsideDown()
234 {
235     return rgbdev->upsideDown();
236 }
237 GBool FullBitmapOutputDev::useDrawChar()
238 {
239     return rgbdev->useDrawChar();
240 }
241 GBool FullBitmapOutputDev::useTilingPatternFill()
242 {
243     return rgbdev->useTilingPatternFill();
244 }
245 GBool FullBitmapOutputDev::useShadedFills()
246 {
247     return rgbdev->useShadedFills();
248 }
249 GBool FullBitmapOutputDev::useDrawForm()
250 {
251     return rgbdev->useDrawForm();
252 }
253 GBool FullBitmapOutputDev::interpretType3Chars()
254 {
255     return rgbdev->interpretType3Chars();
256 }
257 GBool FullBitmapOutputDev::needNonText() 
258 {
259     return rgbdev->needNonText();
260 }
261 void FullBitmapOutputDev::setDefaultCTM(double *ctm) 
262 {
263     rgbdev->setDefaultCTM(ctm);
264     gfxdev->setDefaultCTM(ctm);
265 }
266 void FullBitmapOutputDev::saveState(GfxState *state) 
267 {
268     rgbdev->saveState(state);
269 }
270 void FullBitmapOutputDev::restoreState(GfxState *state) 
271 {
272     rgbdev->restoreState(state);
273 }
274 void FullBitmapOutputDev::updateAll(GfxState *state)
275 {
276     rgbdev->updateAll(state);
277 }
278 void FullBitmapOutputDev::updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32)
279 {
280     rgbdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
281     gfxdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
282 }
283 void FullBitmapOutputDev::updateLineDash(GfxState *state)
284 {
285     rgbdev->updateLineDash(state);
286 }
287 void FullBitmapOutputDev::updateFlatness(GfxState *state)
288 {
289     rgbdev->updateFlatness(state);
290 }
291 void FullBitmapOutputDev::updateLineJoin(GfxState *state)
292 {
293     rgbdev->updateLineJoin(state);
294 }
295 void FullBitmapOutputDev::updateLineCap(GfxState *state)
296 {
297     rgbdev->updateLineCap(state);
298 }
299 void FullBitmapOutputDev::updateMiterLimit(GfxState *state)
300 {
301     rgbdev->updateMiterLimit(state);
302 }
303 void FullBitmapOutputDev::updateLineWidth(GfxState *state)
304 {
305     rgbdev->updateLineWidth(state);
306 }
307 void FullBitmapOutputDev::updateStrokeAdjust(GfxState *state)
308 {
309     rgbdev->updateStrokeAdjust(state);
310 }
311 void FullBitmapOutputDev::updateFillColorSpace(GfxState *state)
312 {
313     rgbdev->updateFillColorSpace(state);
314 }
315 void FullBitmapOutputDev::updateStrokeColorSpace(GfxState *state)
316 {
317     rgbdev->updateStrokeColorSpace(state);
318 }
319 void FullBitmapOutputDev::updateFillColor(GfxState *state)
320 {
321     rgbdev->updateFillColor(state);
322 }
323 void FullBitmapOutputDev::updateStrokeColor(GfxState *state)
324 {
325     rgbdev->updateStrokeColor(state);
326 }
327 void FullBitmapOutputDev::updateBlendMode(GfxState *state)
328 {
329     rgbdev->updateBlendMode(state);
330 }
331 void FullBitmapOutputDev::updateFillOpacity(GfxState *state)
332 {
333     rgbdev->updateFillOpacity(state);
334 }
335 void FullBitmapOutputDev::updateStrokeOpacity(GfxState *state)
336 {
337     rgbdev->updateStrokeOpacity(state);
338 }
339 void FullBitmapOutputDev::updateFillOverprint(GfxState *state)
340 {
341     rgbdev->updateFillOverprint(state);
342 }
343 void FullBitmapOutputDev::updateStrokeOverprint(GfxState *state)
344 {
345     rgbdev->updateStrokeOverprint(state);
346 }
347 void FullBitmapOutputDev::updateTransfer(GfxState *state)
348 {
349     rgbdev->updateTransfer(state);
350 }
351 void FullBitmapOutputDev::updateFont(GfxState *state)
352 {
353     rgbdev->updateFont(state);
354 }
355 void FullBitmapOutputDev::updateTextMat(GfxState *state)
356 {
357     rgbdev->updateTextMat(state);
358 }
359 void FullBitmapOutputDev::updateCharSpace(GfxState *state)
360 {
361     rgbdev->updateCharSpace(state);
362 }
363 void FullBitmapOutputDev::updateRender(GfxState *state)
364 {
365     rgbdev->updateRender(state);
366 }
367 void FullBitmapOutputDev::updateRise(GfxState *state)
368 {
369     rgbdev->updateRise(state);
370 }
371 void FullBitmapOutputDev::updateWordSpace(GfxState *state)
372 {
373     rgbdev->updateWordSpace(state);
374 }
375 void FullBitmapOutputDev::updateHorizScaling(GfxState *state)
376 {
377     rgbdev->updateHorizScaling(state);
378 }
379 void FullBitmapOutputDev::updateTextPos(GfxState *state)
380 {
381     rgbdev->updateTextPos(state);
382 }
383 void FullBitmapOutputDev::updateTextShift(GfxState *state, double shift)
384 {
385     rgbdev->updateTextShift(state, shift);
386 }
387
388 void FullBitmapOutputDev::stroke(GfxState *state)
389 {
390     msg("<debug> stroke");
391     rgbdev->stroke(state);
392 }
393 void FullBitmapOutputDev::fill(GfxState *state)
394 {
395     msg("<debug> fill");
396     rgbdev->fill(state);
397 }
398 void FullBitmapOutputDev::eoFill(GfxState *state)
399 {
400     msg("<debug> eoFill");
401     rgbdev->eoFill(state);
402 }
403 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
404 void FullBitmapOutputDev::tilingPatternFill(GfxState *state, Object *str,
405                                int paintType, Dict *resDict,
406                                double *mat, double *bbox,
407                                int x0, int y0, int x1, int y1,
408                                double xStep, double yStep)
409 {
410     msg("<debug> tilingPatternFill");
411     rgbdev->tilingPatternFill(state, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
412 }
413 #else
414 void FullBitmapOutputDev::tilingPatternFill(GfxState *state, Gfx *gfx, Object *str,
415                                int paintType, Dict *resDict,
416                                double *mat, double *bbox,
417                                int x0, int y0, int x1, int y1,
418                                double xStep, double yStep) 
419 {
420     msg("<debug> tilingPatternFill");
421     rgbdev->tilingPatternFill(state, gfx, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
422 }
423 #endif
424
425 GBool FullBitmapOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading) 
426 {
427     msg("<debug> functionShadedFill");
428     return rgbdev->functionShadedFill(state, shading);
429 }
430 GBool FullBitmapOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading)
431 {
432     msg("<debug> axialShadedFill");
433     return rgbdev->axialShadedFill(state, shading);
434 }
435 GBool FullBitmapOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading)
436 {
437     msg("<debug> radialShadedFill");
438     return rgbdev->radialShadedFill(state, shading);
439 }
440
441 void FullBitmapOutputDev::clip(GfxState *state)
442 {
443     msg("<debug> clip");
444     rgbdev->clip(state);
445 }
446 void FullBitmapOutputDev::eoClip(GfxState *state)
447 {
448     msg("<debug> eoClip");
449     rgbdev->eoClip(state);
450 }
451 void FullBitmapOutputDev::clipToStrokePath(GfxState *state)
452 {
453     msg("<debug> clipToStrokePath");
454     rgbdev->clipToStrokePath(state);
455 }
456
457 void FullBitmapOutputDev::beginStringOp(GfxState *state)
458 {
459     msg("<debug> beginStringOp");
460     rgbdev->beginStringOp(state);
461 }
462 void FullBitmapOutputDev::endStringOp(GfxState *state)
463 {
464     msg("<debug> endStringOp");
465     rgbdev->endStringOp(state);
466 }
467 void FullBitmapOutputDev::beginString(GfxState *state, GString *s)
468 {
469     msg("<debug> beginString");
470     rgbdev->beginString(state, s);
471 }
472 void FullBitmapOutputDev::endString(GfxState *state)
473 {
474     msg("<debug> endString");
475     rgbdev->endString(state);
476 }
477 void FullBitmapOutputDev::drawChar(GfxState *state, double x, double y,
478                       double dx, double dy,
479                       double originX, double originY,
480                       CharCode code, int nBytes, Unicode *u, int uLen)
481 {
482     msg("<debug> drawChar");
483     rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
484 }
485 void FullBitmapOutputDev::drawString(GfxState *state, GString *s)
486 {
487     msg("<error> internal error: drawString not implemented");
488     rgbdev->drawString(state, s);
489 }
490 void FullBitmapOutputDev::endTextObject(GfxState *state)
491 {
492     /* FIXME: the below might render things (stroke outlines etc.) to gfxdev which
493               might end up unflushed- should be handled similarily as
494               drawChar() above
495      */
496     msg("<debug> endTextObject");
497     rgbdev->endTextObject(state);
498 }
499
500 /* TODO: these four operations below *should* do nothing, as type3
501          chars are drawn using operations like fill() */
502 GBool FullBitmapOutputDev::beginType3Char(GfxState *state, double x, double y,
503                              double dx, double dy,
504                              CharCode code, Unicode *u, int uLen)
505 {
506     msg("<debug> beginType3Char");
507     return rgbdev->beginType3Char(state, x, y, dx, dy, code, u, uLen);
508 }
509 void FullBitmapOutputDev::type3D0(GfxState *state, double wx, double wy)
510 {
511     msg("<debug> type3D0");
512     rgbdev->type3D0(state, wx, wy);
513 }
514 void FullBitmapOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
515 {
516     msg("<debug> type3D1");
517     rgbdev->type3D1(state, wx, wy, llx, lly, urx, ury);
518 }
519 void FullBitmapOutputDev::endType3Char(GfxState *state)
520 {
521     msg("<debug> endType3Char");
522     rgbdev->endType3Char(state);
523 }
524 void FullBitmapOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
525                            int width, int height, GBool invert,
526                            GBool inlineImg)
527 {
528     msg("<debug> drawImageMask");
529     rgbdev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
530 }
531 void FullBitmapOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
532                        int width, int height, GfxImageColorMap *colorMap,
533                        int *maskColors, GBool inlineImg)
534 {
535     msg("<debug> drawImage");
536     rgbdev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
537 }
538 void FullBitmapOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
539                              int width, int height,
540                              GfxImageColorMap *colorMap,
541                              Stream *maskStr, int maskWidth, int maskHeight,
542                              GBool maskInvert)
543 {
544     msg("<debug> drawMaskedImage");
545     rgbdev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
546 }
547 void FullBitmapOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
548                                  int width, int height,
549                                  GfxImageColorMap *colorMap,
550                                  Stream *maskStr,
551                                  int maskWidth, int maskHeight,
552                                  GfxImageColorMap *maskColorMap)
553 {
554     msg("<debug> drawSoftMaskedImage");
555     rgbdev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
556 }
557 void FullBitmapOutputDev::drawForm(Ref id)
558 {
559     msg("<debug> drawForm");
560     rgbdev->drawForm(id);
561 }
562
563 void FullBitmapOutputDev::processLink(Link *link, Catalog *catalog)
564 {
565     msg("<debug> processLink");
566     gfxdev->processLink(link, catalog);
567 }
568
569 void FullBitmapOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
570                                     GfxColorSpace *blendingColorSpace,
571                                     GBool isolated, GBool knockout,
572                                     GBool forSoftMask)
573 {
574     msg("<debug> beginTransparencyGroup");
575     rgbdev->beginTransparencyGroup(state, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
576 }
577 void FullBitmapOutputDev::endTransparencyGroup(GfxState *state)
578 {
579     msg("<debug> endTransparencyGroup");
580     rgbdev->endTransparencyGroup(state);
581 }
582 void FullBitmapOutputDev::paintTransparencyGroup(GfxState *state, double *bbox)
583 {
584     msg("<debug> paintTransparencyGroup");
585     rgbdev->paintTransparencyGroup(state,bbox);
586 }
587 void FullBitmapOutputDev::setSoftMask(GfxState *state, double *bbox, GBool alpha, Function *transferFunc, GfxColor *backdropColor)
588 {
589     msg("<debug> setSoftMask");
590     rgbdev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
591 }
592 void FullBitmapOutputDev::clearSoftMask(GfxState *state)
593 {
594     msg("<debug> clearSoftMask");
595     rgbdev->clearSoftMask(state);
596 }