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

« back to all changes in this revision

Viewing changes to filter/pdftopdf/P2PGfx.cxx

  • Committer: Package Import Robot
  • Author(s): Till Kamppeter
  • Date: 2012-08-20 14:53:42 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20120820145342-bddzpwqv0klmt84d
Tags: 1.0.22-1
* New upstream release
   - pdftopdf filter replaced by new QPDF-based filter from Tobias
     Hoffmann's Google Summer of Code project. The former Poppler-based
     pdftopdf duplicated a lot of Poppler's code. The old filter is
     still in the package as pdftopdf.old with source code in
     filter/pdftopdf.old. It will be removed in a later release.
   - bannertopdf: Page duplication routine fixed.
   - bannertopdf: Fixed invalid output of a direct stream object.
   - Added most recent contributors to AUTHORS and COPYING files.
* debian/control: Added build dependency on libqpdf-dev.
* debian/copyright: Updated for the addition of the new pdftopdf filter.

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
 
  parser = new Parser(xref, new Lexer(xref, obj), gTrue);
155
 
  go();
156
 
  delete parser;
157
 
  parser = NULL;
158
 
}
159
 
 
160
 
void P2PGfx::go()
161
 
{
162
 
  Object obj;
163
 
  Object args[maxArgs];
164
 
  int numArgs, i;
165
 
 
166
 
  // scan a sequence of objects
167
 
  numArgs = 0;
168
 
  parser->getObj(&obj);
169
 
  while (!obj.isEOF()) {
170
 
 
171
 
    // got a command - execute it
172
 
    if (obj.isCmd()) {
173
 
      // Run the operation
174
 
      execOp(&obj, args, numArgs);
175
 
 
176
 
      obj.free();
177
 
      for (i = 0; i < numArgs; ++i)
178
 
        args[i].free();
179
 
      numArgs = 0;
180
 
    } else if (numArgs < maxArgs) {
181
 
      args[numArgs++] = obj;
182
 
 
183
 
    // too many arguments - something is wrong
184
 
    } else {
185
 
      p2pError(getPos(), const_cast<char *>("Too many args in content stream"));
186
 
      obj.free();
187
 
    }
188
 
 
189
 
    // grab the next object
190
 
    parser->getObj(&obj);
191
 
  }
192
 
  obj.free();
193
 
 
194
 
  // args at end with no command
195
 
  if (numArgs > 0) {
196
 
    p2pError(getPos(), const_cast<char *>("Leftover args in content stream"));
197
 
    for (i = 0; i < numArgs; ++i)
198
 
      args[i].free();
199
 
  }
200
 
}
201
 
 
202
 
void P2PGfx::outputOp(const char *name, Object args[], int numArgs)
203
 
{
204
 
  int i;
205
 
 
206
 
  for (i = 0;i < numArgs;i++) {
207
 
    P2POutput::outputObject(&args[i],str,xref);
208
 
    str->putchar(' ');
209
 
  }
210
 
  str->printf(" %s ",name);
211
 
}
212
 
 
213
 
void P2PGfx::execOp(Object *cmd, Object args[], int numArgs) {
214
 
  P2POperator *op;
215
 
  char *name;
216
 
  Object *argPtr;
217
 
  int i;
218
 
 
219
 
  // find operator
220
 
  name = cmd->getCmd();
221
 
  if (!(op = findOp(name))) {
222
 
    outputOp(name,args,numArgs);
223
 
    return;
224
 
  }
225
 
 
226
 
  // type check args
227
 
  argPtr = args;
228
 
  if (op->numArgs >= 0) {
229
 
    if (numArgs < op->numArgs) {
230
 
      p2pError(getPos(), const_cast<char *>("Too few (%d) args to '%s' operator"),
231
 
         numArgs, name);
232
 
      return;
233
 
    }
234
 
    if (numArgs > op->numArgs) {
235
 
      argPtr += numArgs - op->numArgs;
236
 
      numArgs = op->numArgs;
237
 
    }
238
 
  } else {
239
 
    if (numArgs > -op->numArgs) {
240
 
      p2pError(getPos(), const_cast<char *>("Too many (%d) args to '%s' operator"),
241
 
            numArgs, name);
242
 
      return;
243
 
    }
244
 
  }
245
 
  for (i = 0; i < numArgs; ++i) {
246
 
    if (!checkArg(&argPtr[i], op->tchk[i])) {
247
 
      p2pError(getPos(),
248
 
            const_cast<char *>("Arg #%d to '%s' operator is wrong type (%s)"),
249
 
            i, name, argPtr[i].getTypeName());
250
 
      return;
251
 
    }
252
 
  }
253
 
 
254
 
  // do it
255
 
  (this->*op->func)(argPtr, numArgs);
256
 
}
257
 
 
258
 
P2PGfx::P2POperator *P2PGfx::findOp(char *name) {
259
 
  int a, b, m, cmp = 1;
260
 
 
261
 
  a = -1;
262
 
  b = numOps;
263
 
  // invariant: opTab[a] < name < opTab[b]
264
 
  while (b - a > 1) {
265
 
    m = (a + b) / 2;
266
 
    cmp = strcmp(opTab[m].name, name);
267
 
    if (cmp < 0)
268
 
      a = m;
269
 
    else if (cmp > 0)
270
 
      b = m;
271
 
    else
272
 
      a = b = m;
273
 
  }
274
 
  if (cmp != 0)
275
 
    return NULL;
276
 
  return &opTab[a];
277
 
}
278
 
 
279
 
GBool P2PGfx::checkArg(Object *arg, TchkType type) {
280
 
  switch (type) {
281
 
  case tchkBool:   return arg->isBool();
282
 
  case tchkInt:    return arg->isInt();
283
 
  case tchkNum:    return arg->isNum();
284
 
  case tchkString: return arg->isString();
285
 
  case tchkName:   return arg->isName();
286
 
  case tchkArray:  return arg->isArray();
287
 
  case tchkProps:  return arg->isDict() || arg->isName();
288
 
  case tchkSCN:    return arg->isNum() || arg->isName();
289
 
  case tchkNone:   return gFalse;
290
 
  }
291
 
  return gFalse;
292
 
}
293
 
 
294
 
int P2PGfx::getPos() {
295
 
  return parser ? parser->getPos() : -1;
296
 
}
297
 
 
298
 
void P2PGfx::opSetStrokeColorSpace(Object args[], int numArgs)
299
 
{
300
 
  if (mappingTable != 0) {
301
 
    P2POutput::outputObject(&args[0],str,xref,
302
 
      mappingTable->tables[P2PResources::ColorSpace]);
303
 
  } else {
304
 
    P2POutput::outputObject(&args[0],str,xref);
305
 
  }
306
 
  str->puts(" CS ");
307
 
}
308
 
 
309
 
void P2PGfx::opXObject(Object args[], int numArgs)
310
 
{
311
 
  if (mappingTable != 0) {
312
 
    P2POutput::outputObject(&args[0],str,xref,
313
 
      mappingTable->tables[P2PResources::XObject]);
314
 
  } else {
315
 
    P2POutput::outputObject(&args[0],str,xref);
316
 
  }
317
 
  str->puts(" Do ");
318
 
}
319
 
 
320
 
void P2PGfx::opSetStrokeColorN(Object args[], int numArgs)
321
 
{
322
 
  int i;
323
 
 
324
 
  for (i = 0;i < numArgs-1;i++) {
325
 
    P2POutput::outputObject(&args[i],str,xref);
326
 
    str->putchar(' ');
327
 
  }
328
 
  if (mappingTable != 0 && args[i].isName()) {
329
 
    P2POutput::outputObject(&args[i],str,xref,
330
 
      mappingTable->tables[P2PResources::Pattern]);
331
 
    str->putchar(' ');
332
 
  } else {
333
 
    P2POutput::outputObject(&args[i],str,xref);
334
 
    str->putchar(' ');
335
 
  }
336
 
  if (args[i].isName() && resources != 0) {
337
 
    /* check pattern */
338
 
    Object obj;
339
 
 
340
 
    char *name = args[i].getName();
341
 
    if (mappingTable != 0
342
 
        && mappingTable->tables[P2PResources::Pattern] != 0) {
343
 
      mappingTable->tables[P2PResources::Pattern]->lookup(name,&obj);
344
 
      name = obj.getName();
345
 
    }
346
 
    resources->refPattern(name,mat);
347
 
    obj.free();
348
 
  }
349
 
  str->puts("SCN ");
350
 
}
351
 
 
352
 
void P2PGfx::opSetFont(Object args[], int numArgs)
353
 
{
354
 
  if (mappingTable != 0) {
355
 
    P2POutput::outputObject(&args[0],str,xref,
356
 
      mappingTable->tables[P2PResources::Font]);
357
 
  } else {
358
 
    P2POutput::outputObject(&args[0],str,xref);
359
 
  }
360
 
  if (fontResource != 0) {
361
 
    if (mappingTable != 0) {
362
 
      Dict *map = mappingTable->tables[P2PResources::Font];
363
 
      if (map != 0) {
364
 
        Object obj;
365
 
#ifdef HAVE_UGOOSTRING_H
366
 
        UGooString nameStr(args[0].getName());
367
 
 
368
 
        map->lookupNF(nameStr,&obj);
369
 
#else
370
 
        map->lookupNF(args[0].getName(),&obj);
371
 
#endif
372
 
        if (obj.isName()) {
373
 
          state.setFont(fontResource->lookup(obj.getName()));
374
 
        }
375
 
        obj.free();
376
 
      }
377
 
    } else {
378
 
      state.setFont(fontResource->lookup(args[0].getName()));
379
 
    }
380
 
  }
381
 
  str->putchar(' ');
382
 
  P2POutput::outputObject(&args[1],str,xref);
383
 
  str->puts(" Tf ");
384
 
}
385
 
 
386
 
void P2PGfx::opSetFillColorSpace(Object args[], int numArgs)
387
 
{
388
 
  if (mappingTable != 0) {
389
 
    P2POutput::outputObject(&args[0],str,xref,
390
 
      mappingTable->tables[P2PResources::ColorSpace]);
391
 
  } else {
392
 
    P2POutput::outputObject(&args[0],str,xref);
393
 
  }
394
 
  str->puts(" cs ");
395
 
}
396
 
 
397
 
void P2PGfx::opSetExtGState(Object args[], int numArgs)
398
 
{
399
 
  if (mappingTable != 0) {
400
 
    P2POutput::outputObject(&args[0],str,xref,
401
 
      mappingTable->tables[P2PResources::ExtGState]);
402
 
    if (fontResource != 0) {
403
 
      Dict *map = mappingTable->tables[P2PResources::ExtGState];
404
 
      if (map != 0) {
405
 
        Object obj;
406
 
#ifdef HAVE_UGOOSTRING_H
407
 
        UGooString nameStr(args[0].getName());
408
 
 
409
 
        map->lookupNF(nameStr,&obj);
410
 
#else
411
 
        map->lookupNF(args[0].getName(),&obj);
412
 
#endif
413
 
        if (obj.isName()) {
414
 
          state.setFont(fontResource->lookupExtGState(obj.getName()));
415
 
        }
416
 
        obj.free();
417
 
      }
418
 
    }
419
 
  } else {
420
 
    P2POutput::outputObject(&args[0],str,xref);
421
 
    if (fontResource != 0) {
422
 
      if (args[0].isName()) {
423
 
        state.setFont(fontResource->lookupExtGState(args[0].getName()));
424
 
      }
425
 
    }
426
 
  }
427
 
  str->puts(" gs ");
428
 
}
429
 
 
430
 
void P2PGfx::opSetFillColorN(Object args[], int numArgs)
431
 
{
432
 
  int i;
433
 
 
434
 
  for (i = 0;i < numArgs-1;i++) {
435
 
    P2POutput::outputObject(&args[i],str,xref);
436
 
    str->putchar(' ');
437
 
  }
438
 
  if (mappingTable != 0 && args[i].isName()) {
439
 
    P2POutput::outputObject(&args[i],str,xref,
440
 
      mappingTable->tables[P2PResources::Pattern]);
441
 
    str->putchar(' ');
442
 
  } else {
443
 
    P2POutput::outputObject(&args[i],str,xref);
444
 
    str->putchar(' ');
445
 
  }
446
 
  if (args[i].isName() && resources != 0) {
447
 
    /* check pattern */
448
 
    Object obj;
449
 
 
450
 
    char *name = args[i].getName();
451
 
    if (mappingTable != 0
452
 
        && mappingTable->tables[P2PResources::Pattern] != 0) {
453
 
      mappingTable->tables[P2PResources::Pattern]->lookup(name,&obj);
454
 
      name = obj.getName();
455
 
    }
456
 
    resources->refPattern(name,mat);
457
 
    obj.free();
458
 
  }
459
 
  str->puts("scn ");
460
 
}
461
 
 
462
 
void P2PGfx::opShFill(Object args[], int numArgs)
463
 
{
464
 
  if (mappingTable != 0) {
465
 
    P2POutput::outputObject(&args[0],str,xref,
466
 
      mappingTable->tables[P2PResources::Shading]);
467
 
  } else {
468
 
    P2POutput::outputObject(&args[0],str,xref);
469
 
  }
470
 
  str->puts(" sh ");
471
 
}
472
 
 
473
 
void P2PGfx::opMoveSetShowText(Object args[], int numArgs)
474
 
{
475
 
  P2PFontDict *f;
476
 
 
477
 
  if ((f = state.getFont()) != 0) f->showText(args[2].getString());
478
 
  outputOp("\"",args,numArgs);
479
 
}
480
 
 
481
 
void P2PGfx::opMoveShowText(Object args[], int numArgs)
482
 
{
483
 
  P2PFontDict *f;
484
 
 
485
 
  if ((f = state.getFont()) != 0) f->showText(args[0].getString());
486
 
  outputOp("'",args,numArgs);
487
 
}
488
 
 
489
 
void P2PGfx::opShowSpaceText(Object args[], int numArgs)
490
 
{
491
 
  P2PFontDict *f;
492
 
 
493
 
  if ((f = state.getFont()) != 0) {
494
 
    Array *a;
495
 
    int i, n;
496
 
 
497
 
    a = args[0].getArray();
498
 
    n = a->getLength();
499
 
    for (i = 0;i < n;i++) {
500
 
      Object obj;
501
 
 
502
 
      a->get(i,&obj);
503
 
      if (obj.isString()) {
504
 
        f->showText(obj.getString());
505
 
      }
506
 
      obj.free();
507
 
    }
508
 
  }
509
 
  outputOp("TJ",args,numArgs);
510
 
}
511
 
 
512
 
void P2PGfx::opShowText(Object args[], int numArgs)
513
 
{
514
 
  P2PFontDict *f;
515
 
 
516
 
  if ((f = state.getFont()) != 0) f->showText(args[0].getString());
517
 
  outputOp("Tj",args,numArgs);
518
 
}
519
 
 
520
 
void P2PGfx::opSave(Object args[], int numArgs)
521
 
{
522
 
  state.save();
523
 
  str->puts(" q ");
524
 
}
525
 
 
526
 
void P2PGfx::opRestore(Object args[], int numArgs)
527
 
{
528
 
  state.restore();
529
 
  str->puts(" Q ");
530
 
}
531
 
 
532
 
void P2PGfx::opBeginImage(Object args[], int numArgs)
533
 
{
534
 
  Object dict;
535
 
  char *key;
536
 
  Stream *imageStr;
537
 
  Object obj;
538
 
 
539
 
  /* output BI */
540
 
  str->puts(" BI");
541
 
  /* handle dictionary */
542
 
  dict.initDict(xref);
543
 
  parser->getObj(&obj);
544
 
  while (!obj.isCmd(const_cast<char *>("ID")) && !obj.isEOF()) {
545
 
    str->putchar(' ');
546
 
    if (!obj.isName()) {
547
 
      p2pError(getPos(), const_cast<char *>("Inline image dictionary key must be a name object"));
548
 
      P2POutput::outputObject(&obj,str,xref);
549
 
      str->putchar(' ');
550
 
      obj.free();
551
 
    } else {
552
 
      key = copyString(obj.getName());
553
 
      if (strcmp("Filter",key) != 0 && strcmp("F",key) != 0
554
 
         && strcmp("DecodeParms",key) != 0 && strcmp("DP",key) != 0) {
555
 
        /* when Filter or DecodeParms, not ouput because image is decoded. */
556
 
        P2POutput::outputObject(&obj,str,xref);
557
 
        str->putchar(' ');
558
 
      }
559
 
      obj.free();
560
 
      parser->getObj(&obj);
561
 
      if (obj.isEOF() || obj.isError()) {
562
 
        gfree(key);
563
 
        break;
564
 
      }
565
 
      if (mappingTable != 0 &&
566
 
          (strcmp(key,"ColorSpace") == 0 || strcmp(key,"CS") == 0)) {
567
 
        /* colorspace */
568
 
        /* resource mapping is needed */
569
 
        P2POutput::outputObject(&obj,str,xref,
570
 
          mappingTable->tables[P2PResources::ColorSpace]);
571
 
      } else if (strcmp("Filter",key) != 0 && strcmp("F",key) != 0
572
 
         && strcmp("DecodeParms",key) != 0 && strcmp("DP",key) != 0) {
573
 
        /* when Filter or DecodeParms, not ouput because image is decoded. */
574
 
        P2POutput::outputObject(&obj,str,xref);
575
 
      }
576
 
      dict.dictAdd(key,&obj);
577
 
    }
578
 
    parser->getObj(&obj);
579
 
  }
580
 
 
581
 
  if (obj.isEOF()) {
582
 
    p2pError(getPos(), const_cast<char *>("End of file in inline image"));
583
 
    obj.free();
584
 
    dict.free();
585
 
  } else if (obj.isError()) {
586
 
    p2pError(getPos(), const_cast<char *>("Error in inline image"));
587
 
    obj.free();
588
 
    dict.free();
589
 
  } else {
590
 
    int c1, c2;
591
 
 
592
 
    /* ouput ID */
593
 
    str->putchar(' ');
594
 
    P2POutput::outputObject(&obj,str,xref);
595
 
    str->putchar('\n');
596
 
    obj.free();
597
 
 
598
 
    /* make image stream */
599
 
    imageStr = new EmbedStream(parser->getStream(), &dict, gFalse,0);
600
 
    imageStr = imageStr->addFilters(&dict);
601
 
    doImage(imageStr);
602
 
    /* handle EI */
603
 
    c1 = imageStr->getBaseStream()->getChar();
604
 
    c2 = imageStr->getBaseStream()->getChar();
605
 
    while (!(c1 == 'E' && c2 == 'I') && c2 != EOF) {
606
 
      c1 = c2;
607
 
      c2 = imageStr->getBaseStream()->getChar();
608
 
    }
609
 
    delete imageStr;
610
 
    str->puts("EI\n");
611
 
  }
612
 
}
613
 
 
614
 
void P2PGfx::doImage(Stream *istr)
615
 
{
616
 
  Dict *dict;
617
 
  int width, height;
618
 
  int bits;
619
 
  GBool mask;
620
 
  Object obj1, obj2;
621
 
  int i, j;
622
 
  GfxColorSpace *colorSpace = NULL;
623
 
  int nComponents;
624
 
  int bytes;
625
 
  Guchar *lineBuf;
626
 
 
627
 
  // get info from the stream
628
 
  bits = 0;
629
 
 
630
 
  // get stream dict
631
 
  dict = istr->getDict();
632
 
 
633
 
  // get size
634
 
  dict->lookup(const_cast<char *>("Width"), &obj1);
635
 
  if (obj1.isNull()) {
636
 
    obj1.free();
637
 
    dict->lookup(const_cast<char *>("W"), &obj1);
638
 
  }
639
 
  if (!obj1.isInt())
640
 
    goto err2;
641
 
  width = obj1.getInt();
642
 
  obj1.free();
643
 
  dict->lookup(const_cast<char *>("Height"), &obj1);
644
 
  if (obj1.isNull()) {
645
 
    obj1.free();
646
 
    dict->lookup(const_cast<char *>("H"), &obj1);
647
 
  }
648
 
  if (!obj1.isInt())
649
 
    goto err2;
650
 
  height = obj1.getInt();
651
 
  obj1.free();
652
 
 
653
 
  // image or mask?
654
 
  dict->lookup(const_cast<char *>("ImageMask"), &obj1);
655
 
  if (obj1.isNull()) {
656
 
    obj1.free();
657
 
    dict->lookup(const_cast<char *>("IM"), &obj1);
658
 
  }
659
 
  mask = gFalse;
660
 
  if (obj1.isBool())
661
 
    mask = obj1.getBool();
662
 
  else if (!obj1.isNull())
663
 
    goto err2;
664
 
  obj1.free();
665
 
 
666
 
  // bit depth
667
 
  if (bits == 0) {
668
 
    dict->lookup(const_cast<char *>("BitsPerComponent"), &obj1);
669
 
    if (obj1.isNull()) {
670
 
      obj1.free();
671
 
      dict->lookup(const_cast<char *>("BPC"), &obj1);
672
 
    }
673
 
    if (obj1.isInt()) {
674
 
      bits = obj1.getInt();
675
 
    } else if (mask) {
676
 
      bits = 1;
677
 
    } else {
678
 
      goto err2;
679
 
    }
680
 
    obj1.free();
681
 
  }
682
 
 
683
 
  // display a mask
684
 
  if (mask) {
685
 
    nComponents = 1;
686
 
  } else {
687
 
    // get color space
688
 
    dict->lookup(const_cast<char *>("ColorSpace"), &obj1);
689
 
    if (obj1.isNull()) {
690
 
      obj1.free();
691
 
      dict->lookup(const_cast<char *>("CS"), &obj1);
692
 
    }
693
 
    if (obj1.isName() && orgCSResource != 0) {
694
 
      orgCSResource->lookup(obj1.getName(), &obj2);
695
 
      if (!obj2.isNull()) {
696
 
        obj1.free();
697
 
        obj1 = obj2;
698
 
      } else {
699
 
        obj2.free();
700
 
      }
701
 
    }
702
 
    if (!obj1.isNull()) {
703
 
#ifdef OLD_CS_PARSE
704
 
      colorSpace = GfxColorSpace::parse(&obj1);
705
 
#else
706
 
      colorSpace = GfxColorSpace::parse(&obj1, NULL);
707
 
#endif
708
 
    }
709
 
    obj1.free();
710
 
    if (!colorSpace) {
711
 
      goto err1;
712
 
    }
713
 
    nComponents = colorSpace->getNComps();
714
 
    delete colorSpace;
715
 
  }
716
 
  istr->reset();
717
 
  /* number of bytes per line */
718
 
  bytes = (bits*nComponents*width+7)/8;
719
 
 
720
 
  /* then, out image body */
721
 
  lineBuf = new Guchar[bytes];
722
 
  for (i = 0;i < height;i++) {
723
 
    for (j = 0;j < bytes;j++) {
724
 
        lineBuf[j] = istr->getChar();
725
 
    }
726
 
    str->write(lineBuf,bytes);
727
 
  }
728
 
  str->putchar('\n');
729
 
  delete[] lineBuf;
730
 
 
731
 
  return;
732
 
 
733
 
 err2:
734
 
  obj1.free();
735
 
 err1:
736
 
  p2pError(getPos(), const_cast<char *>("Bad image parameters"));
737
 
}