source: trunk/poppler/mypoppler/poppler/DCTStream.cc @ 134

Last change on this file since 134 was 134, checked in by Eugene Romanenko, 15 years ago

poppler updated to version 0.5.4

File size: 3.5 KB
Line 
1//========================================================================
2//
3// DCTStream.cc
4//
5// Copyright 1996-2003 Glyph & Cog, LLC
6//
7//========================================================================
8
9#include "DCTStream.h"
10
11static void str_init_source(j_decompress_ptr cinfo)
12{
13}
14
15static boolean str_fill_input_buffer(j_decompress_ptr cinfo)
16{
17  int c;
18  struct str_src_mgr * src = (struct str_src_mgr *)cinfo->src;
19  if (src->index == 0) {
20    c = 0xFF;
21    src->index++;
22  }
23  else if (src->index == 1) {
24    c = 0xD8;
25    src->index++;
26  }
27  else c = src->str->getChar();
28  if (c != EOF)
29  {
30    src->buffer = c;
31    src->pub.next_input_byte = &src->buffer;
32    src->pub.bytes_in_buffer = 1;
33    return TRUE;
34  }
35  else return FALSE;
36}
37
38static void str_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
39{
40  struct str_src_mgr * src = (struct str_src_mgr *)cinfo->src;
41  if (num_bytes > 0) {
42    while (num_bytes > (long) src->pub.bytes_in_buffer) {
43      num_bytes -= (long) src->pub.bytes_in_buffer;
44      str_fill_input_buffer(cinfo);
45    }
46    src->pub.next_input_byte += (size_t) num_bytes;
47    src->pub.bytes_in_buffer -= (size_t) num_bytes;
48  }
49}
50
51static void str_term_source(j_decompress_ptr cinfo)
52{
53}
54
55DCTStream::DCTStream(Stream *strA):
56  FilterStream(strA) {
57  init();
58}
59
60DCTStream::~DCTStream() {
61  jpeg_destroy_decompress(&cinfo);
62  delete str;
63}
64
65void DCTStream::init()
66{
67  jpeg_create_decompress(&cinfo);
68  src.pub.init_source = str_init_source;
69  src.pub.fill_input_buffer = str_fill_input_buffer;
70  src.pub.skip_input_data = str_skip_input_data;
71  src.pub.resync_to_restart = jpeg_resync_to_restart;
72  src.pub.term_source = str_term_source;
73  src.pub.bytes_in_buffer = 0;
74  src.pub.next_input_byte = NULL;
75  src.str = str;
76  src.index = 0;
77  cinfo.src = (jpeg_source_mgr *)&src;
78  cinfo.err = jpeg_std_error(&jerr);
79  x = 0;
80  row_buffer = NULL;
81}
82
83void DCTStream::reset() {
84  int row_stride;
85
86  str->reset();
87
88  if (row_buffer)
89  {
90    jpeg_destroy_decompress(&cinfo);
91    init();
92  }
93
94  // JPEG data has to start with 0xFF 0xD8
95  // but some pdf like the one on
96  // https://bugs.freedesktop.org/show_bug.cgi?id=3299
97  // does have some garbage before that this seeks for
98  // the start marker...
99  bool startFound = false;
100  int c = 0, c2 = 0;
101  while (!startFound)
102  {
103    if (!c)
104    {
105      c = str->getChar();
106      if (c == -1)
107      {
108        error(-1, "Could not find start of jpeg data");
109        exit(1);
110      }
111      if (c != 0xFF) c = 0;
112    }
113    else
114    {
115      c2 = str->getChar();
116      if (c2 != 0xD8)
117      {
118        c = 0;
119        c2 = 0;
120      }
121      else startFound = true;
122    }
123  }
124
125  jpeg_read_header(&cinfo, TRUE);
126  jpeg_start_decompress(&cinfo);
127
128  row_stride = cinfo.output_width * cinfo.output_components;
129  row_buffer = cinfo.mem->alloc_sarray((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
130}
131
132int DCTStream::getChar() {
133  int c;
134
135  if (x == 0) {
136    if (cinfo.output_scanline < cinfo.output_height)
137    {
138      if (!jpeg_read_scanlines(&cinfo, row_buffer, 1)) return EOF;
139    }
140    else return EOF;
141  }
142  c = row_buffer[0][x];
143  x++;
144  if (x == cinfo.output_width * cinfo.output_components)
145    x = 0;
146  return c;
147}
148
149int DCTStream::lookChar() {
150  int c;
151  c = row_buffer[0][x];
152  return c;
153}
154
155GooString *DCTStream::getPSFilter(int psLevel, char *indent) {
156  GooString *s;
157
158  if (psLevel < 2) {
159    return NULL;
160  }
161  if (!(s = str->getPSFilter(psLevel, indent))) {
162    return NULL;
163  }
164  s->append(indent)->append("<< >> /DCTDecode filter\n");
165  return s;
166}
167
168GBool DCTStream::isBinary(GBool last) {
169  return str->isBinary(gTrue);
170}
Note: See TracBrowser for help on using the repository browser.