1 /* FullBitmapOutputDev.cc
3 Output Device which creates a bitmap.
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.
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.
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 */
23 #include "FullBitmapOutputDev.h"
24 #include "GFXOutputDev.h"
25 #include "SplashBitmap.h"
26 #include "SplashPattern.h"
30 #include "../devices/record.h"
32 static SplashColor splash_white = {255,255,255};
33 static SplashColor splash_black = {0,0,0};
35 FullBitmapOutputDev::FullBitmapOutputDev(InfoOutputDev*info, PDFDoc*doc)
38 this->xref = doc->getXRef();
40 msg("<verbose> Rendering everything to a bitmap");
42 /* color graphic output device, for creating bitmaps */
43 this->rgbdev = new SplashOutputDev(splashModeRGB8, 1, gFalse, splash_white, gTrue, gTrue);
45 /* device for handling links */
46 this->gfxdev = new GFXOutputDev(info, this->doc);
48 this->rgbdev->startDoc(this->xref);
50 FullBitmapOutputDev::~FullBitmapOutputDev()
53 delete this->rgbdev;this->rgbdev = 0;
56 delete this->gfxdev;this->gfxdev= 0;
60 GBool FullBitmapOutputDev::getVectorAntialias()
62 return this->rgbdev->getVectorAntialias();
64 void FullBitmapOutputDev::setVectorAntialias(GBool vaa)
66 this->rgbdev->setVectorAntialias(vaa);
68 void FullBitmapOutputDev::setDevice(gfxdevice_t*dev)
71 gfxdev->setDevice(dev);
73 void FullBitmapOutputDev::setMove(int x,int y)
79 void FullBitmapOutputDev::setClip(int x1,int y1,int x2,int y2)
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);
87 void FullBitmapOutputDev::setParameter(const char*key, const char*value)
90 void FullBitmapOutputDev::preparePage(int pdfpage, int outputpage)
92 gfxdev->preparePage(pdfpage, outputpage);
95 static void getBitmapBBox(Guchar*alpha, int width, int height, int*xmin, int*ymin, int*xmax, int*ymax)
101 for(y=0;y<height;y++) {
102 Guchar*a = &alpha[y*width];
103 for(x=0;x<width;x++) {
106 int left = x; //first occupied pixel from left
107 int right = x+1; //last non-occupied pixel from right
116 if(left<*xmin) *xmin = left;
117 if(right>*xmax) *xmax = right;
120 if(*xmin>=*xmax || *ymin>=*ymax) {
128 void FullBitmapOutputDev::flushBitmap()
130 int width = rgbdev->getBitmapWidth();
131 int height = rgbdev->getBitmapHeight();
133 SplashColorPtr rgb = rgbdev->getBitmap()->getDataPtr();
134 Guchar*alpha = rgbdev->getBitmap()->getAlphaPtr();
136 int xmin,ymin,xmax,ymax;
137 getBitmapBBox(alpha, width, height, &xmin,&ymin,&xmax,&ymax);
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;
145 msg("<verbose> Flushing bitmap (bbox: %d,%d,%d,%d)", xmin,ymin,xmax,ymax);
147 if((xmax-xmin)<=0 || (ymax-ymin)<=0) // no bitmap, nothing to do
150 if(sizeof(SplashColor)!=3) {
151 msg("<error> sizeof(SplashColor)!=3");
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);
163 img->height = rangey;
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).
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;
179 /* transform bitmap rectangle to "device space" */
191 gfxline_t* line = gfxline_makerectangle(xmin, ymin, xmax, ymax);
192 dev->fillbitmap(dev, line, img, &m, 0);
195 memset(rgbdev->getBitmap()->getAlphaPtr(), 0, rgbdev->getBitmap()->getWidth()*rgbdev->getBitmap()->getHeight());
196 memset(rgbdev->getBitmap()->getDataPtr(), 0, rgbdev->getBitmap()->getRowSize()*rgbdev->getBitmap()->getHeight());
198 free(img->data);img->data=0;free(img);img=0;
201 void FullBitmapOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_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;}
209 this->movex = -(int)x1 - user_movex;
210 this->movey = -(int)y1 - user_movey;
212 if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
218 this->width = (int)(x2-x1);
219 this->height = (int)(y2-y1);
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);
226 void FullBitmapOutputDev::endPage()
228 msg("<verbose> endPage (FullBitmapOutputDev)");
233 GBool FullBitmapOutputDev::upsideDown()
235 return rgbdev->upsideDown();
237 GBool FullBitmapOutputDev::useDrawChar()
239 return rgbdev->useDrawChar();
241 GBool FullBitmapOutputDev::useTilingPatternFill()
243 return rgbdev->useTilingPatternFill();
245 GBool FullBitmapOutputDev::useShadedFills()
247 return rgbdev->useShadedFills();
249 GBool FullBitmapOutputDev::useDrawForm()
251 return rgbdev->useDrawForm();
253 GBool FullBitmapOutputDev::interpretType3Chars()
255 return rgbdev->interpretType3Chars();
257 GBool FullBitmapOutputDev::needNonText()
259 return rgbdev->needNonText();
261 void FullBitmapOutputDev::setDefaultCTM(double *ctm)
263 rgbdev->setDefaultCTM(ctm);
264 gfxdev->setDefaultCTM(ctm);
266 void FullBitmapOutputDev::saveState(GfxState *state)
268 rgbdev->saveState(state);
270 void FullBitmapOutputDev::restoreState(GfxState *state)
272 rgbdev->restoreState(state);
274 void FullBitmapOutputDev::updateAll(GfxState *state)
276 rgbdev->updateAll(state);
278 void FullBitmapOutputDev::updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32)
280 rgbdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
281 gfxdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
283 void FullBitmapOutputDev::updateLineDash(GfxState *state)
285 rgbdev->updateLineDash(state);
287 void FullBitmapOutputDev::updateFlatness(GfxState *state)
289 rgbdev->updateFlatness(state);
291 void FullBitmapOutputDev::updateLineJoin(GfxState *state)
293 rgbdev->updateLineJoin(state);
295 void FullBitmapOutputDev::updateLineCap(GfxState *state)
297 rgbdev->updateLineCap(state);
299 void FullBitmapOutputDev::updateMiterLimit(GfxState *state)
301 rgbdev->updateMiterLimit(state);
303 void FullBitmapOutputDev::updateLineWidth(GfxState *state)
305 rgbdev->updateLineWidth(state);
307 void FullBitmapOutputDev::updateStrokeAdjust(GfxState *state)
309 rgbdev->updateStrokeAdjust(state);
311 void FullBitmapOutputDev::updateFillColorSpace(GfxState *state)
313 rgbdev->updateFillColorSpace(state);
315 void FullBitmapOutputDev::updateStrokeColorSpace(GfxState *state)
317 rgbdev->updateStrokeColorSpace(state);
319 void FullBitmapOutputDev::updateFillColor(GfxState *state)
321 rgbdev->updateFillColor(state);
323 void FullBitmapOutputDev::updateStrokeColor(GfxState *state)
325 rgbdev->updateStrokeColor(state);
327 void FullBitmapOutputDev::updateBlendMode(GfxState *state)
329 rgbdev->updateBlendMode(state);
331 void FullBitmapOutputDev::updateFillOpacity(GfxState *state)
333 rgbdev->updateFillOpacity(state);
335 void FullBitmapOutputDev::updateStrokeOpacity(GfxState *state)
337 rgbdev->updateStrokeOpacity(state);
339 void FullBitmapOutputDev::updateFillOverprint(GfxState *state)
341 rgbdev->updateFillOverprint(state);
343 void FullBitmapOutputDev::updateStrokeOverprint(GfxState *state)
345 rgbdev->updateStrokeOverprint(state);
347 void FullBitmapOutputDev::updateTransfer(GfxState *state)
349 rgbdev->updateTransfer(state);
351 void FullBitmapOutputDev::updateFont(GfxState *state)
353 rgbdev->updateFont(state);
355 void FullBitmapOutputDev::updateTextMat(GfxState *state)
357 rgbdev->updateTextMat(state);
359 void FullBitmapOutputDev::updateCharSpace(GfxState *state)
361 rgbdev->updateCharSpace(state);
363 void FullBitmapOutputDev::updateRender(GfxState *state)
365 rgbdev->updateRender(state);
367 void FullBitmapOutputDev::updateRise(GfxState *state)
369 rgbdev->updateRise(state);
371 void FullBitmapOutputDev::updateWordSpace(GfxState *state)
373 rgbdev->updateWordSpace(state);
375 void FullBitmapOutputDev::updateHorizScaling(GfxState *state)
377 rgbdev->updateHorizScaling(state);
379 void FullBitmapOutputDev::updateTextPos(GfxState *state)
381 rgbdev->updateTextPos(state);
383 void FullBitmapOutputDev::updateTextShift(GfxState *state, double shift)
385 rgbdev->updateTextShift(state, shift);
388 void FullBitmapOutputDev::stroke(GfxState *state)
390 msg("<debug> stroke");
391 rgbdev->stroke(state);
393 void FullBitmapOutputDev::fill(GfxState *state)
398 void FullBitmapOutputDev::eoFill(GfxState *state)
400 msg("<debug> eoFill");
401 rgbdev->eoFill(state);
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)
410 msg("<debug> tilingPatternFill");
411 rgbdev->tilingPatternFill(state, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
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)
420 msg("<debug> tilingPatternFill");
421 rgbdev->tilingPatternFill(state, gfx, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
425 GBool FullBitmapOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading)
427 msg("<debug> functionShadedFill");
428 return rgbdev->functionShadedFill(state, shading);
430 GBool FullBitmapOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading)
432 msg("<debug> axialShadedFill");
433 return rgbdev->axialShadedFill(state, shading);
435 GBool FullBitmapOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading)
437 msg("<debug> radialShadedFill");
438 return rgbdev->radialShadedFill(state, shading);
441 void FullBitmapOutputDev::clip(GfxState *state)
446 void FullBitmapOutputDev::eoClip(GfxState *state)
448 msg("<debug> eoClip");
449 rgbdev->eoClip(state);
451 void FullBitmapOutputDev::clipToStrokePath(GfxState *state)
453 msg("<debug> clipToStrokePath");
454 rgbdev->clipToStrokePath(state);
457 void FullBitmapOutputDev::beginStringOp(GfxState *state)
459 msg("<debug> beginStringOp");
460 rgbdev->beginStringOp(state);
462 void FullBitmapOutputDev::endStringOp(GfxState *state)
464 msg("<debug> endStringOp");
465 rgbdev->endStringOp(state);
467 void FullBitmapOutputDev::beginString(GfxState *state, GString *s)
469 msg("<debug> beginString");
470 rgbdev->beginString(state, s);
472 void FullBitmapOutputDev::endString(GfxState *state)
474 msg("<debug> endString");
475 rgbdev->endString(state);
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)
482 msg("<debug> drawChar");
483 rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
485 void FullBitmapOutputDev::drawString(GfxState *state, GString *s)
487 msg("<error> internal error: drawString not implemented");
488 rgbdev->drawString(state, s);
490 void FullBitmapOutputDev::endTextObject(GfxState *state)
492 /* FIXME: the below might render things (stroke outlines etc.) to gfxdev which
493 might end up unflushed- should be handled similarily as
496 msg("<debug> endTextObject");
497 rgbdev->endTextObject(state);
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)
506 msg("<debug> beginType3Char");
507 return rgbdev->beginType3Char(state, x, y, dx, dy, code, u, uLen);
509 void FullBitmapOutputDev::type3D0(GfxState *state, double wx, double wy)
511 msg("<debug> type3D0");
512 rgbdev->type3D0(state, wx, wy);
514 void FullBitmapOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
516 msg("<debug> type3D1");
517 rgbdev->type3D1(state, wx, wy, llx, lly, urx, ury);
519 void FullBitmapOutputDev::endType3Char(GfxState *state)
521 msg("<debug> endType3Char");
522 rgbdev->endType3Char(state);
524 void FullBitmapOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
525 int width, int height, GBool invert,
528 msg("<debug> drawImageMask");
529 rgbdev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
531 void FullBitmapOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
532 int width, int height, GfxImageColorMap *colorMap,
533 int *maskColors, GBool inlineImg)
535 msg("<debug> drawImage");
536 rgbdev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
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,
544 msg("<debug> drawMaskedImage");
545 rgbdev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
547 void FullBitmapOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
548 int width, int height,
549 GfxImageColorMap *colorMap,
551 int maskWidth, int maskHeight,
552 GfxImageColorMap *maskColorMap)
554 msg("<debug> drawSoftMaskedImage");
555 rgbdev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
557 void FullBitmapOutputDev::drawForm(Ref id)
559 msg("<debug> drawForm");
560 rgbdev->drawForm(id);
563 void FullBitmapOutputDev::processLink(Link *link, Catalog *catalog)
565 msg("<debug> processLink");
566 gfxdev->processLink(link, catalog);
569 void FullBitmapOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
570 GfxColorSpace *blendingColorSpace,
571 GBool isolated, GBool knockout,
574 msg("<debug> beginTransparencyGroup");
575 rgbdev->beginTransparencyGroup(state, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
577 void FullBitmapOutputDev::endTransparencyGroup(GfxState *state)
579 msg("<debug> endTransparencyGroup");
580 rgbdev->endTransparencyGroup(state);
582 void FullBitmapOutputDev::paintTransparencyGroup(GfxState *state, double *bbox)
584 msg("<debug> paintTransparencyGroup");
585 rgbdev->paintTransparencyGroup(state,bbox);
587 void FullBitmapOutputDev::setSoftMask(GfxState *state, double *bbox, GBool alpha, Function *transferFunc, GfxColor *backdropColor)
589 msg("<debug> setSoftMask");
590 rgbdev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
592 void FullBitmapOutputDev::clearSoftMask(GfxState *state)
594 msg("<debug> clearSoftMask");
595 rgbdev->clearSoftMask(state);