File: FullBitmapOutputDev.cc

package info (click to toggle)
swftools 0.9.2%2Bds1-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,488 kB
  • sloc: ansic: 122,576; sh: 8,494; cpp: 8,020; yacc: 2,260; lisp: 904; makefile: 581; python: 304
file content (575 lines) | stat: -rw-r--r-- 16,832 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* FullBitmapOutputDev.cc

   Output Device which creates a bitmap.

   Swftools is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   Swftools is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with swftools; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include "FullBitmapOutputDev.h"
#include "CharOutputDev.h"

#ifdef HAVE_POPPLER
  #include "splash/SplashBitmap.h"
  #include "splash/SplashPattern.h"
  #include "splash/Splash.h"
#else
  #include "xpdf/config.h"
  #include "SplashBitmap.h"
  #include "SplashPattern.h"
  #include "Splash.h"
#endif

#include "../log.h"
#include "../png.h"
#include "../devices/record.h"

static SplashColor splash_white = {255,255,255};
static SplashColor splash_black = {0,0,0};
    
FullBitmapOutputDev::FullBitmapOutputDev(InfoOutputDev*info, PDFDoc*doc, int*page2page, int num_pages, int x, int y, int x1, int y1, int x2, int y2)
:CommonOutputDev(info, doc, page2page, num_pages, x, y, x1, y1, x2, y2)
{
    this->doc = doc;
    this->xref = doc->getXRef();
    
    msg("<verbose> Rendering everything to a bitmap");
    
    /* color graphic output device, for creating bitmaps */
    this->rgbdev = new SplashOutputDev(splashModeRGB8, 1, gFalse, splash_white, gTrue, gTrue);
  
    /* device for handling links */
    this->gfxdev = new CharOutputDev(info, this->doc, page2page, num_pages, x, y, x1, y1, x2, y2);

    this->rgbdev->startDoc(this->xref);
}
FullBitmapOutputDev::~FullBitmapOutputDev()
{
    if(this->rgbdev) {
	delete this->rgbdev;this->rgbdev = 0;
    }
    if(this->gfxdev) {
	delete this->gfxdev;this->gfxdev= 0;
    }
}

GBool FullBitmapOutputDev::getVectorAntialias()
{
    return this->rgbdev->getVectorAntialias();
}
void FullBitmapOutputDev::setVectorAntialias(GBool vaa)
{
    this->rgbdev->setVectorAntialias(vaa);
}
void FullBitmapOutputDev::setDevice(gfxdevice_t*dev)
{
    this->dev = dev;
    gfxdev->setDevice(dev);
}

void FullBitmapOutputDev::setParameter(const char*key, const char*value)
{
}
static void getBitmapBBox(Guchar*alpha, int width, int height, int*xmin, int*ymin, int*xmax, int*ymax)
{
    *ymin = -1;
    *xmin = width;
    *xmax = 0;
    int x,y;
    for(y=0;y<height;y++) {
	Guchar*a = &alpha[y*width];
	for(x=0;x<width;x++) {
	    if(a[x]) break;
	}
	int left = x; //first occupied pixel from left
	int right = x+1; //last non-occupied pixel from right
	for(;x<width;x++) {
	    if(a[x]) right=x+1;
	}

	if(left!=width) {
	    if(*ymin<0) 
		*ymin=y;
	    *ymax=y+1;
	    if(left<*xmin) *xmin = left;
	    if(right>*xmax) *xmax = right;
	}
    }
    if(*xmin>=*xmax || *ymin>=*ymax) {
	*xmin = 0;
	*ymin = 0;
	*xmax = 0;
	*ymax = 0;
    }
}

void FullBitmapOutputDev::flushBitmap()
{
    int width = rgbdev->getBitmapWidth();
    int height = rgbdev->getBitmapHeight();
    
    SplashColorPtr rgb = rgbdev->getBitmap()->getDataPtr();
    Guchar*alpha = rgbdev->getBitmap()->getAlphaPtr();

    int xmin,ymin,xmax,ymax;
    getBitmapBBox(alpha, width, height, &xmin,&ymin,&xmax,&ymax);

    /* clip against (-movex, -movey, -movex+width, -movey+height) */
    if(xmin < -this->movex) xmin = -this->movex;
    if(ymin < -this->movey) ymin = -this->movey;
    if(xmax > -this->movex + width) xmax = -this->movex+this->width;
    if(ymax > -this->movey + height) ymax = -this->movey+this->height;

    msg("<verbose> Flushing bitmap (bbox: %d,%d,%d,%d)", xmin,ymin,xmax,ymax);
    
    if((xmax-xmin)<=0 || (ymax-ymin)<=0) // no bitmap, nothing to do
	return;

    if(sizeof(SplashColor)!=3) {
	msg("<error> sizeof(SplashColor)!=3");
	return;
    }
    //xmin = ymin = 0;
    //xmax = width;
    //ymax = height;

    int rangex = xmax-xmin;
    int rangey = ymax-ymin;
    gfximage_t*img = (gfximage_t*)malloc(sizeof(gfximage_t)); 
    img->data = (gfxcolor_t*)malloc(rangex * rangey * 4);
    img->width = rangex;
    img->height = rangey;
    int x,y;
    for(y=0;y<rangey;y++) {
	SplashColorPtr in=&rgb[((y+ymin)*width+xmin)*sizeof(SplashColor)];
	gfxcolor_t*out = &img->data[y*rangex];
	Guchar*ain = &alpha[(y+ymin)*width+xmin];
	for(x=0;x<rangex;x++) {
	    // blend against a white background
	    out[x].r = (in[x*3+0]*ain[x])/255 + 255-ain[x];
	    out[x].g = (in[x*3+1]*ain[x])/255 + 255-ain[x];
	    out[x].b = (in[x*3+2]*ain[x])/255 + 255-ain[x];
	    out[x].a = 255;//ain[x];
	}
    }
    /* transform bitmap rectangle to "device space" */
    xmin += movex;
    ymin += movey;
    xmax += movex;
    ymax += movey;

    gfxmatrix_t m;
    m.tx = xmin;
    m.ty = ymin;
    m.m00 = m.m11 = 1;
    m.m10 = m.m01 = 0;

    gfxline_t* line = gfxline_makerectangle(xmin, ymin, xmax, ymax);
    dev->fillbitmap(dev, line, img, &m, 0);
    gfxline_free(line);

    free(img->data);img->data=0;free(img);img=0;
}

GBool FullBitmapOutputDev::checkPageSlice(Page *page, double hDPI, double vDPI,
             int rotate, GBool useMediaBox, GBool crop,
             int sliceX, int sliceY, int sliceW, int sliceH,
             GBool printing, Catalog *catalog,
             GBool (*abortCheckCbk)(void *data),
             void *abortCheckCbkData)
{
    this->setPage(page);
    gfxdev->setPage(page);
    return gTrue;
}

void FullBitmapOutputDev::beginPage(GfxState *state, int pageNum)
{
    msg("<debug> startPage");
    rgbdev->startPage(pageNum, state);
    gfxdev->startPage(pageNum, state);
}

void FullBitmapOutputDev::endPage()
{
    msg("<verbose> endPage (FullBitmapOutputDev)");
    flushBitmap();
    rgbdev->endPage();
    gfxdev->endPage();
}
GBool FullBitmapOutputDev::upsideDown()
{
    return rgbdev->upsideDown();
}
GBool FullBitmapOutputDev::useDrawChar()
{
    return rgbdev->useDrawChar();
}
GBool FullBitmapOutputDev::useTilingPatternFill()
{
    return rgbdev->useTilingPatternFill();
}
GBool FullBitmapOutputDev::useShadedFills()
{
    return rgbdev->useShadedFills();
}
GBool FullBitmapOutputDev::useDrawForm()
{
    return rgbdev->useDrawForm();
}
GBool FullBitmapOutputDev::interpretType3Chars()
{
    return rgbdev->interpretType3Chars();
}
GBool FullBitmapOutputDev::needNonText() 
{
    return rgbdev->needNonText();
}
void FullBitmapOutputDev::setDefaultCTM(double *ctm) 
{
    rgbdev->setDefaultCTM(ctm);
    gfxdev->setDefaultCTM(ctm);
}
void FullBitmapOutputDev::saveState(GfxState *state) 
{
    rgbdev->saveState(state);
}
void FullBitmapOutputDev::restoreState(GfxState *state) 
{
    rgbdev->restoreState(state);
}
void FullBitmapOutputDev::updateAll(GfxState *state)
{
    rgbdev->updateAll(state);
}
void FullBitmapOutputDev::updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32)
{
    rgbdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
    gfxdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
}
void FullBitmapOutputDev::updateLineDash(GfxState *state)
{
    rgbdev->updateLineDash(state);
}
void FullBitmapOutputDev::updateFlatness(GfxState *state)
{
    rgbdev->updateFlatness(state);
}
void FullBitmapOutputDev::updateLineJoin(GfxState *state)
{
    rgbdev->updateLineJoin(state);
}
void FullBitmapOutputDev::updateLineCap(GfxState *state)
{
    rgbdev->updateLineCap(state);
}
void FullBitmapOutputDev::updateMiterLimit(GfxState *state)
{
    rgbdev->updateMiterLimit(state);
}
void FullBitmapOutputDev::updateLineWidth(GfxState *state)
{
    rgbdev->updateLineWidth(state);
}
void FullBitmapOutputDev::updateStrokeAdjust(GfxState *state)
{
    rgbdev->updateStrokeAdjust(state);
}
void FullBitmapOutputDev::updateFillColorSpace(GfxState *state)
{
    rgbdev->updateFillColorSpace(state);
}
void FullBitmapOutputDev::updateStrokeColorSpace(GfxState *state)
{
    rgbdev->updateStrokeColorSpace(state);
}
void FullBitmapOutputDev::updateFillColor(GfxState *state)
{
    rgbdev->updateFillColor(state);
}
void FullBitmapOutputDev::updateStrokeColor(GfxState *state)
{
    rgbdev->updateStrokeColor(state);
}
void FullBitmapOutputDev::updateBlendMode(GfxState *state)
{
    rgbdev->updateBlendMode(state);
}
void FullBitmapOutputDev::updateFillOpacity(GfxState *state)
{
    rgbdev->updateFillOpacity(state);
}
void FullBitmapOutputDev::updateStrokeOpacity(GfxState *state)
{
    rgbdev->updateStrokeOpacity(state);
}
void FullBitmapOutputDev::updateFillOverprint(GfxState *state)
{
    rgbdev->updateFillOverprint(state);
}
void FullBitmapOutputDev::updateStrokeOverprint(GfxState *state)
{
    rgbdev->updateStrokeOverprint(state);
}
void FullBitmapOutputDev::updateTransfer(GfxState *state)
{
    rgbdev->updateTransfer(state);
}
void FullBitmapOutputDev::updateFont(GfxState *state)
{
    rgbdev->updateFont(state);
}
void FullBitmapOutputDev::updateTextMat(GfxState *state)
{
    rgbdev->updateTextMat(state);
}
void FullBitmapOutputDev::updateCharSpace(GfxState *state)
{
    rgbdev->updateCharSpace(state);
}
void FullBitmapOutputDev::updateRender(GfxState *state)
{
    rgbdev->updateRender(state);
}
void FullBitmapOutputDev::updateRise(GfxState *state)
{
    rgbdev->updateRise(state);
}
void FullBitmapOutputDev::updateWordSpace(GfxState *state)
{
    rgbdev->updateWordSpace(state);
}
void FullBitmapOutputDev::updateHorizScaling(GfxState *state)
{
    rgbdev->updateHorizScaling(state);
}
void FullBitmapOutputDev::updateTextPos(GfxState *state)
{
    rgbdev->updateTextPos(state);
}
void FullBitmapOutputDev::updateTextShift(GfxState *state, double shift)
{
    rgbdev->updateTextShift(state, shift);
}

void FullBitmapOutputDev::stroke(GfxState *state)
{
    msg("<debug> stroke");
    rgbdev->stroke(state);
}
void FullBitmapOutputDev::fill(GfxState *state)
{
    msg("<debug> fill");
    rgbdev->fill(state);
}
void FullBitmapOutputDev::eoFill(GfxState *state)
{
    msg("<debug> eoFill");
    rgbdev->eoFill(state);
}
POPPLER_TILING_PATERN_RETURN FullBitmapOutputDev::tilingPatternFill(GfxState *state, POPPLER_TILING_PATERN_GFX
             Object *str,
			       int paintType, Dict *resDict,
			       double *mat, double *bbox,
			       int x0, int y0, int x1, int y1,
			       double xStep, double yStep)
{
    msg("<debug> tilingPatternFill");
#ifdef HAVE_POPPLER
    return
#endif
    rgbdev->tilingPatternFill(state, POPPLER_TILING_PATERN_GFX_ARG str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
}

GBool FullBitmapOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading) 
{
    msg("<debug> functionShadedFill");
    return rgbdev->functionShadedFill(state, shading);
}
GBool FullBitmapOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading POPPLER_RAXIAL_MIN_MAX)
{
    msg("<debug> axialShadedFill");
    return rgbdev->axialShadedFill(state, shading POPPLER_RAXIAL_MIN_MAX_ARG);
}
GBool FullBitmapOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading POPPLER_RAXIAL_MIN_MAX)
{
    msg("<debug> radialShadedFill");
    return rgbdev->radialShadedFill(state, shading POPPLER_RAXIAL_MIN_MAX_ARG);
}

void FullBitmapOutputDev::clip(GfxState *state)
{
    msg("<debug> clip");
    rgbdev->clip(state);
}
void FullBitmapOutputDev::eoClip(GfxState *state)
{
    msg("<debug> eoClip");
    rgbdev->eoClip(state);
}
void FullBitmapOutputDev::clipToStrokePath(GfxState *state)
{
    msg("<debug> clipToStrokePath");
    rgbdev->clipToStrokePath(state);
}

void FullBitmapOutputDev::beginStringOp(GfxState *state)
{
    msg("<debug> beginStringOp");
    rgbdev->beginStringOp(state);
}
void FullBitmapOutputDev::endStringOp(GfxState *state)
{
    msg("<debug> endStringOp");
    rgbdev->endStringOp(state);
}
void FullBitmapOutputDev::beginString(GfxState *state, GString *s)
{
    msg("<debug> beginString");
    rgbdev->beginString(state, s);
}
void FullBitmapOutputDev::endString(GfxState *state)
{
    msg("<debug> endString");
    rgbdev->endString(state);
}
void FullBitmapOutputDev::drawChar(GfxState *state, double x, double y,
		      double dx, double dy,
		      double originX, double originY,
		      CharCode code, int nBytes, Unicode *u, int uLen)
{
    msg("<debug> drawChar");
    rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
}
void FullBitmapOutputDev::drawString(GfxState *state, GString *s)
{
    msg("<error> internal error: drawString not implemented");
    rgbdev->drawString(state, s);
}
void FullBitmapOutputDev::endTextObject(GfxState *state)
{
    /* FIXME: the below might render things (stroke outlines etc.) to gfxdev which
              might end up unflushed- should be handled similarily as
	      drawChar() above
     */
    msg("<debug> endTextObject");
    rgbdev->endTextObject(state);
}

/* TODO: these four operations below *should* do nothing, as type3
         chars are drawn using operations like fill() */
GBool FullBitmapOutputDev::beginType3Char(GfxState *state, double x, double y,
			     double dx, double dy,
			     CharCode code, Unicode *u, int uLen)
{
    msg("<debug> beginType3Char");
    return rgbdev->beginType3Char(state, x, y, dx, dy, code, u, uLen);
}
void FullBitmapOutputDev::type3D0(GfxState *state, double wx, double wy)
{
    msg("<debug> type3D0");
    rgbdev->type3D0(state, wx, wy);
}
void FullBitmapOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
{
    msg("<debug> type3D1");
    rgbdev->type3D1(state, wx, wy, llx, lly, urx, ury);
}
void FullBitmapOutputDev::endType3Char(GfxState *state)
{
    msg("<debug> endType3Char");
    rgbdev->endType3Char(state);
}
void FullBitmapOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
			   int width, int height, GBool invert, POPPLER_INTERPOLATE
			   GBool inlineImg)
{
    msg("<debug> drawImageMask");
    rgbdev->drawImageMask(state, ref, str, width, height, invert, POPPLER_INTERPOLATE_ARG inlineImg);
}
void FullBitmapOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
		       int width, int height, GfxImageColorMap *colorMap, POPPLER_INTERPOLATE
		       int *maskColors, GBool inlineImg)
{
    msg("<debug> drawImage");
    rgbdev->drawImage(state, ref, str, width, height, colorMap,
      POPPLER_INTERPOLATE_ARG maskColors, inlineImg);
}
void FullBitmapOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
			     int width, int height,
			     GfxImageColorMap *colorMap, POPPLER_INTERPOLATE
			     Stream *maskStr, int maskWidth, int maskHeight,
			     GBool maskInvert POPPLER_MASK_INTERPOLATE)
{
    msg("<debug> drawMaskedImage");
    rgbdev->drawMaskedImage(state, ref, str, width, height, colorMap,
      POPPLER_INTERPOLATE_ARG maskStr, maskWidth, maskHeight,
      maskInvert POPPLER_MASK_INTERPOLATE_ARG);
}
void FullBitmapOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
				 int width, int height,
				 GfxImageColorMap *colorMap, POPPLER_INTERPOLATE
				 Stream *maskStr, int maskWidth, int maskHeight,
				 GfxImageColorMap *maskColorMap POPPLER_MASK_INTERPOLATE)
{
    msg("<debug> drawSoftMaskedImage");
    rgbdev->drawSoftMaskedImage(state, ref, str, width, height, colorMap,
      POPPLER_INTERPOLATE_ARG maskStr, maskWidth, maskHeight,
      maskColorMap POPPLER_MASK_INTERPOLATE_ARG);
}
void FullBitmapOutputDev::drawForm(Ref id)
{
    msg("<debug> drawForm");
    rgbdev->drawForm(id);
}

void FullBitmapOutputDev::processLink(Link *link, Catalog *catalog)
{
    msg("<debug> processLink");
    gfxdev->processLink(link, catalog);
}

void FullBitmapOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
				    GfxColorSpace *blendingColorSpace,
				    GBool isolated, GBool knockout,
				    GBool forSoftMask)
{
    msg("<debug> beginTransparencyGroup");
    rgbdev->beginTransparencyGroup(state, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
}
void FullBitmapOutputDev::endTransparencyGroup(GfxState *state)
{
    msg("<debug> endTransparencyGroup");
    rgbdev->endTransparencyGroup(state);
}
void FullBitmapOutputDev::paintTransparencyGroup(GfxState *state, double *bbox)
{
    msg("<debug> paintTransparencyGroup");
    rgbdev->paintTransparencyGroup(state,bbox);
}
void FullBitmapOutputDev::setSoftMask(GfxState *state, double *bbox, GBool alpha, Function *transferFunc, GfxColor *backdropColor)
{
    msg("<debug> setSoftMask");
    rgbdev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
}
void FullBitmapOutputDev::clearSoftMask(GfxState *state)
{
    msg("<debug> clearSoftMask");
    rgbdev->clearSoftMask(state);
}