aboutsummaryrefslogtreecommitdiff
path: root/lib/pdfstream.cc
blob: 4875de560c3278bc9190586b6163977e17eb10b9 (plain)
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
//  pdfstream.cc -- image streams producing PDF files
//  Copyright (C) 2008  SEIKO EPSON CORPORATION
//
//  This file is part of the 'iscan' program.
//
//  The 'iscan' program is free-ish 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.
//
//  This program is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY;  without even the implied warranty of FITNESS
//  FOR A PARTICULAR PURPOSE or MERCHANTABILITY.
//  See the GNU General Public License for more details.
//
//  You should have received a verbatim copy of the GNU General Public
//  License along with this program; if not, write to:
//
//      Free Software Foundation, Inc.
//      59 Temple Place, Suite 330
//      Boston, MA  02111-1307  USA
//
//  As a special exception, the copyright holders give permission
//  to link the code of this program with the esmod library and
//  distribute linked combinations including the two.  You must obey
//  the GNU General Public License in all respects for all of the
//  code used other than esmod.


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "pdfstream.hh"
#include "jpegstream.hh"

#include <sstream>

namespace iscan
{

pdfstream::pdfstream (FILE *file, bool match_direction)
  : imgstream (),               // avoid recursion
    _file (file), _g3 (NULL), _rotate_180 (false)
{
  _match_direction = match_direction;
  init ();
}

void
pdfstream::init ()
{
  if (!is_usable ())
    {
      throw std::runtime_error ("pdf not usable");
    }

  _page = 0;
  _row = 0;
  _need_page_trailer = false;
  _data_size = 0;
  _pdf_h_sz = 0;
  _pdf_v_sz = 0;
  _do_jpeg = false;

  _doc = NULL;
  _pages = NULL;
  _page_list = NULL;
  _trailer = NULL;
  _img_height_obj = NULL;
  _stream = NULL;

  _doc = new pdf::writer (_file);

  pdf::object::reset_object_numbers ();
}

pdfstream::~pdfstream ()
{
  if (_need_page_trailer)
    {
      write_page_trailer ();
    }

  fflush (_file);

  delete _doc;
  delete _pages;
  delete _page_list;
  delete _trailer;
  delete _img_height_obj;

  delete _stream;

  delete _g3;
}

bool
pdfstream::is_usable ()
{
  return true;
}

void
pdfstream::rotate_180 (bool yes)
{
  _rotate_180 = yes;
}

void
pdfstream::next ()
{
  if (0 == _row)
    {
      return;
    }

  write_page_trailer ();
  _row = 0;
  _rotate_180 = _match_direction && is_back (_page);
}

imgstream&
pdfstream::write (const byte_type *line, size_type n)
{
  if (!line || 0 == n) return *this;
  if (0 == _page)
    {
      write_header ();
    }
  if (0 == _row)
    {
      // adjust to PDF coordinate system (is this correct?)
      _pdf_h_sz = (72 * _h_sz) / _hres;
      _pdf_v_sz = (72 * _v_sz) / _vres;

      write_page_header ();
      ++_page;
    }

  if (_stream)
    {
      _stream->write (line, n);
    }
  else if (_g3)
    {
      string buf = (*_g3) (line, n);
      _doc->write (buf.data (), buf.size ());
    }
  else
    {
      _doc->write (line, n);
    }
  ++_row;

  return *this;
}

void
pdfstream::write_header ()
{
  _doc->header ();

  delete _pages;
  _pages = new pdf::dictionary ();

  pdf::dictionary info;
  info.insert ("Producer", pdf::primitive ("(iscan)"));
  info.insert ("Creator", pdf::primitive ("(iscan)"));
  _doc->write (info);

  pdf::dictionary catalog;
  catalog.insert ("Type", pdf::primitive ("/Catalog"));
  catalog.insert ("Pages", pdf::object (_pages->obj_num ()));
  _doc->write (catalog);

  delete _trailer;
  _trailer = new pdf::dictionary ();
  _trailer->insert ("Info", pdf::object (info.obj_num ()));
  _trailer->insert ("Root", pdf::object (catalog.obj_num ()));

  delete _page_list;
  _page_list = new pdf::array ();
}

void
pdfstream::write_page_header ()
{
  pdf::dictionary page;

  _page_list->insert (pdf::object (page.obj_num ()));

  _pages->insert ("Type", pdf::primitive ("/Pages"));
  _pages->insert ("Kids", _page_list);
  _pages->insert ("Count", pdf::primitive (_page_list->size ()));

  _doc->write (*_pages);

  pdf::dictionary image;
  pdf::dictionary contents;

  pdf::array mbox;
  mbox.insert (pdf::primitive (0));
  mbox.insert (pdf::primitive (0));
  mbox.insert (pdf::primitive (_pdf_h_sz));
  mbox.insert (pdf::primitive (_pdf_v_sz));

  std::stringstream ss2;
  std::string img_name;

  ss2 << "iscanImage" << _page;
  img_name = ss2.str ();

  pdf::array procset;
  std::string cproc = "/ImageB";
  if (RGB == _cspc)
    {
      cproc = "/ImageC";
    }
  pdf::dictionary tmp;
  tmp.insert (img_name.c_str (), pdf::object (image.obj_num ()));

  procset.insert (pdf::primitive ("/PDF"));
  procset.insert (pdf::primitive (cproc));

  pdf::dictionary rsrc;
  rsrc.insert ("XObject", &tmp);
  rsrc.insert ("ProcSet", &procset);

  page.insert ("Type", pdf::primitive ("/Page"));
  page.insert ("Parent", pdf::object (_pages->obj_num ()));
  page.insert ("Resources", &rsrc);
  page.insert ("MediaBox", &mbox);
  page.insert ("Contents", pdf::object (contents.obj_num ()));

  _doc->write (page);

  _doc->begin_stream (contents);

  // transformation matrices must be specified in reverse order
  std::stringstream ss;
  ss << "q" << std::endl;
  ss << _pdf_h_sz << " 0 0 " << _pdf_v_sz << " 0 0 cm" << std::endl;
  if (_rotate_180)
    {
      // undo the translation below
      ss << "1 0 0 1 0.5 0.5 cm" << std::endl;

      // reflect along x and y axis
      ss << "-1 0 0 -1 0 0 cm" << std::endl;

      // translate so the image midpoint lies on the origin
      ss << "1 0 0 1 -0.5 -0.5 cm" << std::endl;
    }
  ss << "/" << img_name << " Do" << std::endl;
  ss << "Q";

  _doc->write (ss.str ());
  _doc->end_stream ();

  write_image_object (image, img_name);

  _need_page_trailer = true;
}

void
pdfstream::write_image_object (pdf::dictionary& image, std::string name)
{
  delete _img_height_obj;
  _img_height_obj = new pdf::primitive ();

  image.insert ("Type", pdf::primitive ("/XObject"));
  image.insert ("Subtype", pdf::primitive ("/Image"));
  image.insert ("Width", pdf::primitive (_h_sz));
  image.insert ("Height", pdf::object (_img_height_obj->obj_num ()));

  pdf::array decode;
  std::string dev = "/DeviceGray";
  if (RGB == _cspc) dev = "/DeviceRGB";
  
  if (monochrome == _cspc)
    {
      if (!_g3) _g3 = new fax_encoder;
    }
  else
    {
      _do_jpeg = iscan::jpegstream::is_usable ();
    }
  image.insert ("ColorSpace", pdf::primitive (dev));
  image.insert ("BitsPerComponent", pdf::primitive (_bits));
  image.insert ("Interpolate", pdf::primitive ("true"));

  pdf::dictionary parms;
  if (_do_jpeg)
    {
      image.insert ("Filter", pdf::primitive ("/DCTDecode"));
    }
  else if (_g3)
    {
      image.insert ("Filter", pdf::primitive ("/CCITTFaxDecode"));

      parms.insert ("Columns", pdf::primitive (_h_sz));
      parms.insert ("Rows", pdf::primitive (_v_sz));
      parms.insert ("EndOfBlock", pdf::primitive ("false"));
      parms.insert ("EndOfLine", pdf::primitive ("true"));
      parms.insert ("EncodedByteAlign", pdf::primitive ("true"));
      parms.insert ("K", pdf::primitive (0));   // CCITT3 1-D encoding
      image.insert ("DecodeParms", &parms);
    }

  // see PDF reference 1.7 p. 342 and p. 1107 # 53
  image.insert ("Name", pdf::primitive ("/" + name));

  _doc->begin_stream (image);

  if (_do_jpeg)
    {
      _stream = new jpegstream (_file);
    }

  if (_stream)
    {
      _stream->size (_h_sz, _v_sz);
      _stream->depth (_bits);
      _stream->colour (_cspc);
      _stream->resolution (_hres, _vres);
    }
}

void
pdfstream::write_page_trailer ()
{
  delete _stream;
  _stream = NULL;

  _doc->end_stream ();

  *_img_height_obj = pdf::primitive (_row);
  _doc->write (*_img_height_obj);

  _doc->trailer (*_trailer);

  _need_page_trailer = false;

  _pdf_h_sz = 0;
  _pdf_v_sz = 0;

  _do_jpeg = false;
}

}       // namespace iscan