~ubuntu-branches/ubuntu/raring/cups-filters/raring-proposed

« back to all changes in this revision

Viewing changes to pdftopdf/P2PGfx.cxx

  • Committer: Package Import Robot
  • Author(s): Till Kamppeter
  • Date: 2012-07-22 18:57:32 UTC
  • mfrom: (1.1.17)
  • Revision ID: package-import@ubuntu.com-20120722185732-26kkte5p1lth3rt5
Tags: 1.0.20-0bzr1
* New upstream release
   - pdftops: Added another workaround for Kyocera printers: Some
     models get very slow on images which request interpolation,
     so now we remove the image interpolation requests by additional
     PostScript code only inserted for Kyocera printers (LP: #1026974).
   - Made the Poppler-based filters pdftopdf and pdftoopvp build with
     both Poppler 0.18.x and 0.20.x (Upstream bug #1055).
   - Fixes according to Coverity scan results (Upstream bug #1054).
   - Switched build system to autotools. This especially fixes several
     build problems in Gentoo. Also build-tested with CUPS 1.6.0b1.
   - Fixes for compatibility with clang/gcc-4.7.
   - textonly: Filter did not work as a pipe with copies=1 (Upstream bug
     #1032).
   - texttopdf: Avoid trimming the results of FcFontSort(), as this may
     miss some reasonable candidates under certain circumstances. BTW,
     fix passing a non-pointer as a pointer to "result" (Closes: #670055).
   - Corrected documentation. The option for the maximum image rendering
     resolution in pdftops is "pdftops-max-image-resolution", not
     "pdftops-max-image-resolution-default".
* debian/patches/fcfontsort-no-trim.patch: Removed, fixed upstream.
* debian/rules: Updated options for ./configure and make for the new autotools
  build system.
* debian/watch: Switched to bz2 upstream packages.
* debian/rules, debian/copyright, debian/cups-filters.docs: Updated for
  renamed documentation files.
* debian/control, debian/libfontembed1.install,
  debian/libfontembed-dev.install: Added new binary packages for libfontembed.
* debian/copyright: Updated for recent file additions, and rearrangement of
  directories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
  P2PGfx.cc
3
 
  developped by BBR Inc. 2006-2007
4
 
 
5
 
  This file is based on Gfx.cc
6
 
  Gfx.cc copyright notice is follows
7
 
  and is licensed under GPL.
8
 
*/
9
 
//========================================================================
10
 
//
11
 
// Gfx.cc
12
 
//
13
 
// Copyright 1996-2003 Glyph & Cog, LLC
14
 
//
15
 
//========================================================================
16
 
 
17
 
#include <config.h>
18
 
 
19
 
 
20
 
#ifdef USE_GCC_PRAGMAS
21
 
#pragma implementation
22
 
#endif
23
 
 
24
 
#include <stdlib.h>
25
 
#include <stdio.h>
26
 
#include <stddef.h>
27
 
#include <string.h>
28
 
#include <math.h>
29
 
#include "goo/gmem.h"
30
 
#include "goo/GooTimer.h"
31
 
#include "goo/GooHash.h"
32
 
#include "GlobalParams.h"
33
 
#include "CharTypes.h"
34
 
#include "Object.h"
35
 
#include "Array.h"
36
 
#include "Dict.h"
37
 
#include "Stream.h"
38
 
#include "Lexer.h"
39
 
#include "Parser.h"
40
 
#include "GfxFont.h"
41
 
#include "GfxState.h"
42
 
#include "OutputDev.h"
43
 
#include "Page.h"
44
 
#include "P2PError.h"
45
 
#include "Gfx.h"
46
 
#include "ProfileData.h"
47
 
#include "UGooString.h"
48
 
#include "P2PGfx.h"
49
 
#include "P2POutputStream.h"
50
 
#include "P2POutput.h"
51
 
#include "P2PResources.h"
52
 
 
53
 
//------------------------------------------------------------------------
54
 
// Operator table
55
 
//------------------------------------------------------------------------
56
 
 
57
 
#ifdef WIN32 // this works around a bug in the VC7 compiler
58
 
#  pragma optimize("",off)
59
 
#endif
60
 
 
61
 
P2PGfx::P2POperator P2PGfx::opTab[] = {
62
 
  {"\"",  3, {tchkNum,    tchkNum,    tchkString},
63
 
            &P2PGfx::opMoveSetShowText},
64
 
  {"'",   1, {tchkString},
65
 
            &P2PGfx::opMoveShowText},
66
 
  {"BI",  0, {tchkNone},
67
 
          &P2PGfx::opBeginImage},
68
 
  {"CS",  1, {tchkName},
69
 
          &P2PGfx::opSetStrokeColorSpace},
70
 
  {"Do",  1, {tchkName},
71
 
          &P2PGfx::opXObject},
72
 
  {"Q",   0, {tchkNone},
73
 
          &P2PGfx::opRestore},
74
 
  {"SCN", -5, {tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
75
 
               tchkSCN},
76
 
          &P2PGfx::opSetStrokeColorN},
77
 
  {"TJ",  1, {tchkArray},
78
 
            &P2PGfx::opShowSpaceText},
79
 
  {"Tf",  2, {tchkName,   tchkNum},
80
 
          &P2PGfx::opSetFont},
81
 
  {"Tj",  1, {tchkString},
82
 
            &P2PGfx::opShowText},
83
 
  {"cs",  1, {tchkName},
84
 
          &P2PGfx::opSetFillColorSpace},
85
 
  {"gs",  1, {tchkName},
86
 
          &P2PGfx::opSetExtGState},
87
 
  {"q",   0, {tchkNone},
88
 
            &P2PGfx::opSave},
89
 
  {"scn", -5, {tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
90
 
               tchkSCN},
91
 
          &P2PGfx::opSetFillColorN},
92
 
  {"sh",  1, {tchkName},
93
 
          &P2PGfx::opShFill},
94
 
};
95
 
 
96
 
#ifdef WIN32 // this works around a bug in the VC7 compiler
97
 
#  pragma optimize("",on)
98
 
#endif
99
 
 
100
 
#define numOps (sizeof(opTab) / sizeof(P2POperator))
101
 
 
102
 
//------------------------------------------------------------------------
103
 
// P2PGfx
104
 
//------------------------------------------------------------------------
105
 
 
106
 
P2PGfx::P2PGfx(XRef *xrefA, P2POutputStream *strA,
107
 
  P2PFontResource *fontResourceA, P2PResources *resourcesA)
108
 
{
109
 
  xref = xrefA;
110
 
  str = strA;
111
 
  fontResource = fontResourceA;
112
 
  resources = resourcesA;
113
 
 
114
 
  /* make dummy GfxState for initializing color space */
115
 
  PDFRectangle box;
116
 
  GfxState state(72,72,&box,0,gFalse);
117
 
}
118
 
 
119
 
P2PGfx::~P2PGfx() {
120
 
  state.clean();
121
 
}
122
 
 
123
 
void P2PGfx::outputContents(Object *obj, P2PResourceMap *mappingTableA,
124
 
  Dict *orgResourceA, P2PMatrix *matA)
125
 
{
126
 
  Object obj2;
127
 
  int i;
128
 
 
129
 
  mappingTable = mappingTableA;
130
 
  mat = matA;
131
 
  orgCSResource = 0;
132
 
  if (orgResourceA != 0) {
133
 
    orgResourceA->lookup(const_cast<char *>("ColorSpace"),&obj2);
134
 
    if (obj2.isDict()) {
135
 
      orgCSResource = obj2.getDict();
136
 
      orgCSResource->incRef();
137
 
    }
138
 
  }
139
 
  obj2.free();
140
 
  if (obj->isArray()) {
141
 
    for (i = 0; i < obj->arrayGetLength(); ++i) {
142
 
      obj->arrayGet(i, &obj2);
143
 
      if (!obj2.isStream()) {
144
 
        p2pError(-1, const_cast<char *>("Weird page contents"));
145
 
        obj2.free();
146
 
        return;
147
 
      }
148
 
      obj2.free();
149
 
    }
150
 
  } else if (!obj->isStream()) {
151
 
    p2pError(-1, const_cast<char *>("Weird page contents"));
152
 
    return;
153
 
  }
154
 
#ifdef PARSER_HAS_2_ARGS
155
 
  parser = new Parser(xref, new Lexer(xref, obj));
156
 
#else
157
 
  parser = new Parser(xref, new Lexer(xref, obj), gTrue);
158
 
#endif
159
 
  go();
160
 
  delete parser;
161
 
  parser = NULL;
162
 
}
163
 
 
164
 
void P2PGfx::go()
165
 
{
166
 
  Object obj;
167
 
  Object args[maxArgs];
168
 
  int numArgs, i;
169
 
 
170
 
  // scan a sequence of objects
171
 
  numArgs = 0;
172
 
  parser->getObj(&obj);
173
 
  while (!obj.isEOF()) {
174
 
 
175
 
    // got a command - execute it
176
 
    if (obj.isCmd()) {
177
 
      // Run the operation
178
 
      execOp(&obj, args, numArgs);
179
 
 
180
 
      obj.free();
181
 
      for (i = 0; i < numArgs; ++i)
182
 
        args[i].free();
183
 
      numArgs = 0;
184
 
    } else if (numArgs < maxArgs) {
185
 
      args[numArgs++] = obj;
186
 
 
187
 
    // too many arguments - something is wrong
188
 
    } else {
189
 
      p2pError(getPos(), const_cast<char *>("Too many args in content stream"));
190
 
      obj.free();
191
 
    }
192
 
 
193
 
    // grab the next object
194
 
    parser->getObj(&obj);
195
 
  }
196
 
  obj.free();
197
 
 
198
 
  // args at end with no command
199
 
  if (numArgs > 0) {
200
 
    p2pError(getPos(), const_cast<char *>("Leftover args in content stream"));
201
 
    for (i = 0; i < numArgs; ++i)
202
 
      args[i].free();
203
 
  }
204
 
}
205
 
 
206
 
void P2PGfx::outputOp(const char *name, Object args[], int numArgs)
207
 
{
208
 
  int i;
209
 
 
210
 
  for (i = 0;i < numArgs;i++) {
211
 
    P2POutput::outputObject(&args[i],str,xref);
212
 
    str->putchar(' ');
213
 
  }
214
 
  str->printf(" %s ",name);
215
 
}
216
 
 
217
 
void P2PGfx::execOp(Object *cmd, Object args[], int numArgs) {
218
 
  P2POperator *op;
219
 
  char *name;
220
 
  Object *argPtr;
221
 
  int i;
222
 
 
223
 
  // find operator
224
 
  name = cmd->getCmd();
225
 
  if (!(op = findOp(name))) {
226
 
    outputOp(name,args,numArgs);
227
 
    return;
228
 
  }
229
 
 
230
 
  // type check args
231
 
  argPtr = args;
232
 
  if (op->numArgs >= 0) {
233
 
    if (numArgs < op->numArgs) {
234
 
      p2pError(getPos(), const_cast<char *>("Too few (%d) args to '%s' operator"),
235
 
         numArgs, name);
236
 
      return;
237
 
    }
238
 
    if (numArgs > op->numArgs) {
239
 
      argPtr += numArgs - op->numArgs;
240
 
      numArgs = op->numArgs;
241
 
    }
242
 
  } else {
243
 
    if (numArgs > -op->numArgs) {
244
 
      p2pError(getPos(), const_cast<char *>("Too many (%d) args to '%s' operator"),
245
 
            numArgs, name);
246
 
      return;
247
 
    }
248
 
  }
249
 
  for (i = 0; i < numArgs; ++i) {
250
 
    if (!checkArg(&argPtr[i], op->tchk[i])) {
251
 
      p2pError(getPos(),
252
 
            const_cast<char *>("Arg #%d to '%s' operator is wrong type (%s)"),
253
 
            i, name, argPtr[i].getTypeName());
254
 
      return;
255
 
    }
256
 
  }
257
 
 
258
 
  // do it
259
 
  (this->*op->func)(argPtr, numArgs);
260
 
}
261
 
 
262
 
P2PGfx::P2POperator *P2PGfx::findOp(char *name) {
263
 
  int a, b, m, cmp = 1;
264
 
 
265
 
  a = -1;
266
 
  b = numOps;
267
 
  // invariant: opTab[a] < name < opTab[b]
268
 
  while (b - a > 1) {
269
 
    m = (a + b) / 2;
270
 
    cmp = strcmp(opTab[m].name, name);
271
 
    if (cmp < 0)
272
 
      a = m;
273
 
    else if (cmp > 0)
274
 
      b = m;
275
 
    else
276
 
      a = b = m;
277
 
  }
278
 
  if (cmp != 0)
279
 
    return NULL;
280
 
  return &opTab[a];
281
 
}
282
 
 
283
 
GBool P2PGfx::checkArg(Object *arg, TchkType type) {
284
 
  switch (type) {
285
 
  case tchkBool:   return arg->isBool();
286
 
  case tchkInt:    return arg->isInt();
287
 
  case tchkNum:    return arg->isNum();
288
 
  case tchkString: return arg->isString();
289
 
  case tchkName:   return arg->isName();
290
 
  case tchkArray:  return arg->isArray();
291
 
  case tchkProps:  return arg->isDict() || arg->isName();
292
 
  case tchkSCN:    return arg->isNum() || arg->isName();
293
 
  case tchkNone:   return gFalse;
294
 
  }
295
 
  return gFalse;
296
 
}
297
 
 
298
 
int P2PGfx::getPos() {
299
 
  return parser ? parser->getPos() : -1;
300
 
}
301
 
 
302
 
void P2PGfx::opSetStrokeColorSpace(Object args[], int numArgs)
303
 
{
304
 
  if (mappingTable != 0) {
305
 
    P2POutput::outputObject(&args[0],str,xref,
306
 
      mappingTable->tables[P2PResources::ColorSpace]);
307
 
  } else {
308
 
    P2POutput::outputObject(&args[0],str,xref);
309
 
  }
310
 
  str->puts(" CS ");
311
 
}
312
 
 
313
 
void P2PGfx::opXObject(Object args[], int numArgs)
314
 
{
315
 
  if (mappingTable != 0) {
316
 
    P2POutput::outputObject(&args[0],str,xref,
317
 
      mappingTable->tables[P2PResources::XObject]);
318
 
  } else {
319
 
    P2POutput::outputObject(&args[0],str,xref);
320
 
  }
321
 
  str->puts(" Do ");
322
 
}
323
 
 
324
 
void P2PGfx::opSetStrokeColorN(Object args[], int numArgs)
325
 
{
326
 
  int i;
327
 
 
328
 
  for (i = 0;i < numArgs-1;i++) {
329
 
    P2POutput::outputObject(&args[i],str,xref);
330
 
    str->putchar(' ');
331
 
  }
332
 
  if (mappingTable != 0 && args[i].isName()) {
333
 
    P2POutput::outputObject(&args[i],str,xref,
334
 
      mappingTable->tables[P2PResources::Pattern]);
335
 
    str->putchar(' ');
336
 
  } else {
337
 
    P2POutput::outputObject(&args[i],str,xref);
338
 
    str->putchar(' ');
339
 
  }
340
 
  if (args[i].isName() && resources != 0) {
341
 
    /* check pattern */
342
 
    Object obj;
343
 
 
344
 
    char *name = args[i].getName();
345
 
    if (mappingTable != 0
346
 
        && mappingTable->tables[P2PResources::Pattern] != 0) {
347
 
      mappingTable->tables[P2PResources::Pattern]->lookup(name,&obj);
348
 
      name = obj.getName();
349
 
    }
350
 
    resources->refPattern(name,mat);
351
 
    obj.free();
352
 
  }
353
 
  str->puts("SCN ");
354
 
}
355
 
 
356
 
void P2PGfx::opSetFont(Object args[], int numArgs)
357
 
{
358
 
  if (mappingTable != 0) {
359
 
    P2POutput::outputObject(&args[0],str,xref,
360
 
      mappingTable->tables[P2PResources::Font]);
361
 
  } else {
362
 
    P2POutput::outputObject(&args[0],str,xref);
363
 
  }
364
 
  if (fontResource != 0) {
365
 
    if (mappingTable != 0) {
366
 
      Dict *map = mappingTable->tables[P2PResources::Font];
367
 
      if (map != 0) {
368
 
        Object obj;
369
 
#ifdef HAVE_UGOOSTRING_H
370
 
        UGooString nameStr(args[0].getName());
371
 
 
372
 
        map->lookupNF(nameStr,&obj);
373
 
#else
374
 
        map->lookupNF(args[0].getName(),&obj);
375
 
#endif
376
 
        if (obj.isName()) {
377
 
          state.setFont(fontResource->lookup(obj.getName()));
378
 
        }
379
 
        obj.free();
380
 
      }
381
 
    } else {
382
 
      state.setFont(fontResource->lookup(args[0].getName()));
383
 
    }
384
 
  }
385
 
  str->putchar(' ');
386
 
  P2POutput::outputObject(&args[1],str,xref);
387
 
  str->puts(" Tf ");
388
 
}
389
 
 
390
 
void P2PGfx::opSetFillColorSpace(Object args[], int numArgs)
391
 
{
392
 
  if (mappingTable != 0) {
393
 
    P2POutput::outputObject(&args[0],str,xref,
394
 
      mappingTable->tables[P2PResources::ColorSpace]);
395
 
  } else {
396
 
    P2POutput::outputObject(&args[0],str,xref);
397
 
  }
398
 
  str->puts(" cs ");
399
 
}
400
 
 
401
 
void P2PGfx::opSetExtGState(Object args[], int numArgs)
402
 
{
403
 
  if (mappingTable != 0) {
404
 
    P2POutput::outputObject(&args[0],str,xref,
405
 
      mappingTable->tables[P2PResources::ExtGState]);
406
 
    if (fontResource != 0) {
407
 
      Dict *map = mappingTable->tables[P2PResources::ExtGState];
408
 
      if (map != 0) {
409
 
        Object obj;
410
 
#ifdef HAVE_UGOOSTRING_H
411
 
        UGooString nameStr(args[0].getName());
412
 
 
413
 
        map->lookupNF(nameStr,&obj);
414
 
#else
415
 
        map->lookupNF(args[0].getName(),&obj);
416
 
#endif
417
 
        if (obj.isName()) {
418
 
          state.setFont(fontResource->lookupExtGState(obj.getName()));
419
 
        }
420
 
        obj.free();
421
 
      }
422
 
    }
423
 
  } else {
424
 
    P2POutput::outputObject(&args[0],str,xref);
425
 
    if (fontResource != 0) {
426
 
      if (args[0].isName()) {
427
 
        state.setFont(fontResource->lookupExtGState(args[0].getName()));
428
 
      }
429
 
    }
430
 
  }
431
 
  str->puts(" gs ");
432
 
}
433
 
 
434
 
void P2PGfx::opSetFillColorN(Object args[], int numArgs)
435
 
{
436
 
  int i;
437
 
 
438
 
  for (i = 0;i < numArgs-1;i++) {
439
 
    P2POutput::outputObject(&args[i],str,xref);
440
 
    str->putchar(' ');
441
 
  }
442
 
  if (mappingTable != 0 && args[i].isName()) {
443
 
    P2POutput::outputObject(&args[i],str,xref,
444
 
      mappingTable->tables[P2PResources::Pattern]);
445
 
    str->putchar(' ');
446
 
  } else {
447
 
    P2POutput::outputObject(&args[i],str,xref);
448
 
    str->putchar(' ');
449
 
  }
450
 
  if (args[i].isName() && resources != 0) {
451
 
    /* check pattern */
452
 
    Object obj;
453
 
 
454
 
    char *name = args[i].getName();
455
 
    if (mappingTable != 0
456
 
        && mappingTable->tables[P2PResources::Pattern] != 0) {
457
 
      mappingTable->tables[P2PResources::Pattern]->lookup(name,&obj);
458
 
      name = obj.getName();
459
 
    }
460
 
    resources->refPattern(name,mat);
461
 
    obj.free();
462
 
  }
463
 
  str->puts("scn ");
464
 
}
465
 
 
466
 
void P2PGfx::opShFill(Object args[], int numArgs)
467
 
{
468
 
  if (mappingTable != 0) {
469
 
    P2POutput::outputObject(&args[0],str,xref,
470
 
      mappingTable->tables[P2PResources::Shading]);
471
 
  } else {
472
 
    P2POutput::outputObject(&args[0],str,xref);
473
 
  }
474
 
  str->puts(" sh ");
475
 
}
476
 
 
477
 
void P2PGfx::opMoveSetShowText(Object args[], int numArgs)
478
 
{
479
 
  P2PFontDict *f;
480
 
 
481
 
  if ((f = state.getFont()) != 0) f->showText(args[2].getString());
482
 
  outputOp("\"",args,numArgs);
483
 
}
484
 
 
485
 
void P2PGfx::opMoveShowText(Object args[], int numArgs)
486
 
{
487
 
  P2PFontDict *f;
488
 
 
489
 
  if ((f = state.getFont()) != 0) f->showText(args[0].getString());
490
 
  outputOp("'",args,numArgs);
491
 
}
492
 
 
493
 
void P2PGfx::opShowSpaceText(Object args[], int numArgs)
494
 
{
495
 
  P2PFontDict *f;
496
 
 
497
 
  if ((f = state.getFont()) != 0) {
498
 
    Array *a;
499
 
    int i, n;
500
 
 
501
 
    a = args[0].getArray();
502
 
    n = a->getLength();
503
 
    for (i = 0;i < n;i++) {
504
 
      Object obj;
505
 
 
506
 
      a->get(i,&obj);
507
 
      if (obj.isString()) {
508
 
        f->showText(obj.getString());
509
 
      }
510
 
      obj.free();
511
 
    }
512
 
  }
513
 
  outputOp("TJ",args,numArgs);
514
 
}
515
 
 
516
 
void P2PGfx::opShowText(Object args[], int numArgs)
517
 
{
518
 
  P2PFontDict *f;
519
 
 
520
 
  if ((f = state.getFont()) != 0) f->showText(args[0].getString());
521
 
  outputOp("Tj",args,numArgs);
522
 
}
523
 
 
524
 
void P2PGfx::opSave(Object args[], int numArgs)
525
 
{
526
 
  state.save();
527
 
  str->puts(" q ");
528
 
}
529
 
 
530
 
void P2PGfx::opRestore(Object args[], int numArgs)
531
 
{
532
 
  state.restore();
533
 
  str->puts(" Q ");
534
 
}
535
 
 
536
 
void P2PGfx::opBeginImage(Object args[], int numArgs)
537
 
{
538
 
  Object dict;
539
 
  char *key;
540
 
  Stream *imageStr;
541
 
  Object obj;
542
 
 
543
 
  /* output BI */
544
 
  str->puts(" BI");
545
 
  /* handle dictionary */
546
 
  dict.initDict(xref);
547
 
  parser->getObj(&obj);
548
 
  while (!obj.isCmd(const_cast<char *>("ID")) && !obj.isEOF()) {
549
 
    str->putchar(' ');
550
 
    if (!obj.isName()) {
551
 
      p2pError(getPos(), const_cast<char *>("Inline image dictionary key must be a name object"));
552
 
      P2POutput::outputObject(&obj,str,xref);
553
 
      str->putchar(' ');
554
 
      obj.free();
555
 
    } else {
556
 
      key = copyString(obj.getName());
557
 
      if (strcmp("Filter",key) != 0 && strcmp("F",key) != 0
558
 
         && strcmp("DecodeParms",key) != 0 && strcmp("DP",key) != 0) {
559
 
        /* when Filter or DecodeParms, not ouput because image is decoded. */
560
 
        P2POutput::outputObject(&obj,str,xref);
561
 
        str->putchar(' ');
562
 
      }
563
 
      obj.free();
564
 
      parser->getObj(&obj);
565
 
      if (obj.isEOF() || obj.isError()) {
566
 
        gfree(key);
567
 
        break;
568
 
      }
569
 
      if (mappingTable != 0 &&
570
 
          (strcmp(key,"ColorSpace") == 0 || strcmp(key,"CS") == 0)) {
571
 
        /* colorspace */
572
 
        /* resource mapping is needed */
573
 
        P2POutput::outputObject(&obj,str,xref,
574
 
          mappingTable->tables[P2PResources::ColorSpace]);
575
 
      } else if (strcmp("Filter",key) != 0 && strcmp("F",key) != 0
576
 
         && strcmp("DecodeParms",key) != 0 && strcmp("DP",key) != 0) {
577
 
        /* when Filter or DecodeParms, not ouput because image is decoded. */
578
 
        P2POutput::outputObject(&obj,str,xref);
579
 
      }
580
 
      dict.dictAdd(key,&obj);
581
 
    }
582
 
    parser->getObj(&obj);
583
 
  }
584
 
 
585
 
  if (obj.isEOF()) {
586
 
    p2pError(getPos(), const_cast<char *>("End of file in inline image"));
587
 
    obj.free();
588
 
    dict.free();
589
 
  } else if (obj.isError()) {
590
 
    p2pError(getPos(), const_cast<char *>("Error in inline image"));
591
 
    obj.free();
592
 
    dict.free();
593
 
  } else {
594
 
    int c1, c2;
595
 
 
596
 
    /* ouput ID */
597
 
    str->putchar(' ');
598
 
    P2POutput::outputObject(&obj,str,xref);
599
 
    str->putchar('\n');
600
 
    obj.free();
601
 
 
602
 
    /* make image stream */
603
 
    imageStr = new EmbedStream(parser->getStream(), &dict, gFalse,0);
604
 
    imageStr = imageStr->addFilters(&dict);
605
 
    doImage(imageStr);
606
 
    /* handle EI */
607
 
    c1 = imageStr->getBaseStream()->getChar();
608
 
    c2 = imageStr->getBaseStream()->getChar();
609
 
    while (!(c1 == 'E' && c2 == 'I') && c2 != EOF) {
610
 
      c1 = c2;
611
 
      c2 = imageStr->getBaseStream()->getChar();
612
 
    }
613
 
    delete imageStr;
614
 
    str->puts("EI\n");
615
 
  }
616
 
}
617
 
 
618
 
void P2PGfx::doImage(Stream *istr)
619
 
{
620
 
  Dict *dict;
621
 
  int width, height;
622
 
  int bits;
623
 
  GBool mask;
624
 
  Object obj1, obj2;
625
 
  int i, j;
626
 
  GfxColorSpace *colorSpace = NULL;
627
 
  int nComponents;
628
 
  int bytes;
629
 
  Guchar *lineBuf;
630
 
 
631
 
  // get info from the stream
632
 
  bits = 0;
633
 
 
634
 
  // get stream dict
635
 
  dict = istr->getDict();
636
 
 
637
 
  // get size
638
 
  dict->lookup(const_cast<char *>("Width"), &obj1);
639
 
  if (obj1.isNull()) {
640
 
    obj1.free();
641
 
    dict->lookup(const_cast<char *>("W"), &obj1);
642
 
  }
643
 
  if (!obj1.isInt())
644
 
    goto err2;
645
 
  width = obj1.getInt();
646
 
  obj1.free();
647
 
  dict->lookup(const_cast<char *>("Height"), &obj1);
648
 
  if (obj1.isNull()) {
649
 
    obj1.free();
650
 
    dict->lookup(const_cast<char *>("H"), &obj1);
651
 
  }
652
 
  if (!obj1.isInt())
653
 
    goto err2;
654
 
  height = obj1.getInt();
655
 
  obj1.free();
656
 
 
657
 
  // image or mask?
658
 
  dict->lookup(const_cast<char *>("ImageMask"), &obj1);
659
 
  if (obj1.isNull()) {
660
 
    obj1.free();
661
 
    dict->lookup(const_cast<char *>("IM"), &obj1);
662
 
  }
663
 
  mask = gFalse;
664
 
  if (obj1.isBool())
665
 
    mask = obj1.getBool();
666
 
  else if (!obj1.isNull())
667
 
    goto err2;
668
 
  obj1.free();
669
 
 
670
 
  // bit depth
671
 
  if (bits == 0) {
672
 
    dict->lookup(const_cast<char *>("BitsPerComponent"), &obj1);
673
 
    if (obj1.isNull()) {
674
 
      obj1.free();
675
 
      dict->lookup(const_cast<char *>("BPC"), &obj1);
676
 
    }
677
 
    if (obj1.isInt()) {
678
 
      bits = obj1.getInt();
679
 
    } else if (mask) {
680
 
      bits = 1;
681
 
    } else {
682
 
      goto err2;
683
 
    }
684
 
    obj1.free();
685
 
  }
686
 
 
687
 
  // display a mask
688
 
  if (mask) {
689
 
    nComponents = 1;
690
 
  } else {
691
 
    // get color space
692
 
    dict->lookup(const_cast<char *>("ColorSpace"), &obj1);
693
 
    if (obj1.isNull()) {
694
 
      obj1.free();
695
 
      dict->lookup(const_cast<char *>("CS"), &obj1);
696
 
    }
697
 
    if (obj1.isName() && orgCSResource != 0) {
698
 
      orgCSResource->lookup(obj1.getName(), &obj2);
699
 
      if (!obj2.isNull()) {
700
 
        obj1.free();
701
 
        obj1 = obj2;
702
 
      } else {
703
 
        obj2.free();
704
 
      }
705
 
    }
706
 
    if (!obj1.isNull()) {
707
 
#ifdef OLD_CS_PARSE
708
 
      colorSpace = GfxColorSpace::parse(&obj1);
709
 
#else
710
 
      colorSpace = GfxColorSpace::parse(&obj1, NULL);
711
 
#endif
712
 
    }
713
 
    obj1.free();
714
 
    if (!colorSpace) {
715
 
      goto err1;
716
 
    }
717
 
    nComponents = colorSpace->getNComps();
718
 
    delete colorSpace;
719
 
  }
720
 
  istr->reset();
721
 
  /* number of bytes per line */
722
 
  bytes = (bits*nComponents*width+7)/8;
723
 
 
724
 
  /* then, out image body */
725
 
  lineBuf = new Guchar[bytes];
726
 
  for (i = 0;i < height;i++) {
727
 
    for (j = 0;j < bytes;j++) {
728
 
        lineBuf[j] = istr->getChar();
729
 
    }
730
 
    str->write(lineBuf,bytes);
731
 
  }
732
 
  str->putchar('\n');
733
 
  delete[] lineBuf;
734
 
 
735
 
  return;
736
 
 
737
 
 err2:
738
 
  obj1.free();
739
 
 err1:
740
 
  p2pError(getPos(), const_cast<char *>("Bad image parameters"));
741
 
}