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

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

First import

File size: 3.4 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 
58  jpeg_create_decompress(&cinfo);
59  src.pub.init_source = str_init_source;
60  src.pub.fill_input_buffer = str_fill_input_buffer;
61  src.pub.skip_input_data = str_skip_input_data;
62  src.pub.resync_to_restart = jpeg_resync_to_restart;
63  src.pub.term_source = str_term_source;
64  src.pub.bytes_in_buffer = 0;
65  src.pub.next_input_byte = NULL;
66  src.str = str;
67  src.index = 0;
68  cinfo.src = (jpeg_source_mgr *)&src;
69  cinfo.err = jpeg_std_error(&jerr);
70  x = 0;
71}
72
73DCTStream::~DCTStream() {
74  jpeg_destroy_decompress(&cinfo);
75  delete str;
76}
77
78void DCTStream::reset() {
79  int row_stride;
80
81  str->reset();
82 
83  // JPEG data has to start with 0xFF 0xD8
84  // but some pdf like the one on
85  // https://bugs.freedesktop.org/show_bug.cgi?id=3299
86  // does have some garbage before that this seeks for
87  // the start marker...
88  bool startFound = false;
89  int c = 0, c2 = 0;
90  while (!startFound)
91  {
92    if (!c)
93    {
94      c = str->getChar();
95      if (c == -1)
96      {
97        error(-1, "Could not find start of jpeg data");
98        exit(1);
99      }
100      if (c != 0xFF) c = 0;
101    }
102    else
103    {
104      c2 = str->getChar();
105      if (c2 != 0xD8)
106      {
107        c = 0;
108        c2 = 0;
109      }
110      else startFound = true;
111    }
112  }
113
114  jpeg_read_header(&cinfo, TRUE);
115  jpeg_start_decompress(&cinfo);
116
117  row_stride = cinfo.output_width * cinfo.output_components;
118  row_buffer = cinfo.mem->alloc_sarray((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
119}
120
121int DCTStream::getChar() {
122  int c;
123
124  if (x == 0) {
125    if (cinfo.output_scanline < cinfo.output_height)
126    {
127      if (!jpeg_read_scanlines(&cinfo, row_buffer, 1)) return EOF;
128    }
129    else return EOF;
130  }
131  c = row_buffer[0][x];
132  x++;
133  if (x == cinfo.output_width * cinfo.output_components)
134    x = 0;
135  return c;
136}
137
138int DCTStream::lookChar() {
139  int c;
140  c = row_buffer[0][x];
141  return c;
142}
143
144GooString *DCTStream::getPSFilter(int psLevel, char *indent) {
145  GooString *s;
146
147  if (psLevel < 2) {
148    return NULL;
149  }
150  if (!(s = str->getPSFilter(psLevel, indent))) {
151    return NULL;
152  }
153  s->append(indent)->append("<< >> /DCTDecode filter\n");
154  return s;
155}
156
157GBool DCTStream::isBinary(GBool last) {
158  return str->isBinary(gTrue);
159}
Note: See TracBrowser for help on using the repository browser.