source: trunk/libdjvu/DjVuDumpHelper.cpp @ 17

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

update makefiles, remove absolute paths, update djvulibre to version 3.5.17

File size: 10.8 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, Version 2. The license should have
9//C- accompanied the software or you may obtain a copy of the license
10//C- from the Free Software Foundation at http://www.fsf.org .
11//C-
12//C- This program is distributed in the hope that it will be useful,
13//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//C- GNU General Public License for more details.
16//C-
17//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
18//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech
19//C- Software authorized us to replace the original DjVu(r) Reference
20//C- Library notice by the following text (see doc/lizard2002.djvu):
21//C-
22//C-  ------------------------------------------------------------------
23//C- | DjVu (r) Reference Library (v. 3.5)
24//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
25//C- | The DjVu Reference Library is protected by U.S. Pat. No.
26//C- | 6,058,214 and patents pending.
27//C- |
28//C- | This software is subject to, and may be distributed under, the
29//C- | GNU General Public License, Version 2. The license should have
30//C- | accompanied the software or you may obtain a copy of the license
31//C- | from the Free Software Foundation at http://www.fsf.org .
32//C- |
33//C- | The computer code originally released by LizardTech under this
34//C- | license and unmodified by other parties is deemed "the LIZARDTECH
35//C- | ORIGINAL CODE."  Subject to any third party intellectual property
36//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
37//C- | non-exclusive license to make, use, sell, or otherwise dispose of
38//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
39//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
40//C- | General Public License.   This grant only confers the right to
41//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
42//C- | the extent such infringement is reasonably necessary to enable
43//C- | recipient to make, have made, practice, sell, or otherwise dispose
44//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
45//C- | any greater extent that may be necessary to utilize further
46//C- | modifications or combinations.
47//C- |
48//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
49//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
50//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
51//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
52//C- +------------------------------------------------------------------
53//
54// $Id: DjVuDumpHelper.cpp,v 1.9 2003/11/07 22:08:20 leonb Exp $
55// $Name:  $
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60#if NEED_GNUG_PRAGMAS
61# pragma implementation
62#endif
63
64#include "DjVuDumpHelper.h"
65#include "DataPool.h"
66#include "DjVmDir.h"
67#include "DjVuInfo.h"
68#include "IFFByteStream.h"
69
70
71#ifdef HAVE_NAMESPACES
72namespace DJVU {
73# ifdef NOT_DEFINED // Just to fool emacs c++ mode
74}
75#endif
76#endif
77
78
79#ifdef putchar
80#undef putchar
81#endif
82
83struct DjVmInfo
84{
85  GP<DjVmDir> dir;
86  GPMap<int,DjVmDir::File> map;
87};
88
89inline static void
90putchar(ByteStream & str, char ch)
91{
92   str.write(&ch, 1);
93}
94
95// ---------- ROUTINES FOR SUMMARIZING CHUNK DATA
96
97static void
98display_djvu_info(ByteStream & out_str, IFFByteStream &iff,
99                  GUTF8String, size_t size, DjVmInfo&, int)
100{
101  GP<DjVuInfo> ginfo=DjVuInfo::create();
102  DjVuInfo &info=*ginfo;
103  info.decode(*iff.get_bytestream());
104  if (size >= 4)
105    out_str.format( "DjVu %dx%d", info.width, info.height);
106  if (size >= 5)
107    out_str.format( ", v%d", info.version);
108  if (size >= 8)
109    out_str.format( ", %d dpi", info.dpi);
110  if (size >= 8)
111    out_str.format( ", gamma=%3.1f", info.gamma);
112}
113
114static void
115display_djbz(ByteStream & out_str, IFFByteStream &iff,
116             GUTF8String, size_t, DjVmInfo&, int)
117{
118  out_str.format( "JB2 shared dictionary");
119}
120
121static void
122display_fgbz(ByteStream & out_str, IFFByteStream &iff,
123             GUTF8String, size_t, DjVmInfo&, int)
124{
125  out_str.format( "JB2 colors data");
126}
127
128static void
129display_sjbz(ByteStream & out_str, IFFByteStream &iff,
130             GUTF8String, size_t, DjVmInfo&, int)
131{
132  out_str.format( "JB2 bilevel data");
133}
134
135static void
136display_smmr(ByteStream & out_str, IFFByteStream &iff,
137             GUTF8String, size_t, DjVmInfo&, int)
138{
139  out_str.format( "G4/MMR stencil data");
140}
141
142static void
143display_iw4(ByteStream & out_str, IFFByteStream &iff,
144            GUTF8String, size_t, DjVmInfo&, int)
145{
146  GP<ByteStream> gbs = iff.get_bytestream();
147  unsigned char serial = gbs->read8();
148  unsigned char slices = gbs->read8();
149  out_str.format( "IW4 data #%d, %d slices", serial+1, slices);
150  if (serial == 0)
151    {
152      unsigned char major = gbs->read8();
153      unsigned char minor = gbs->read8();
154      unsigned char xhi = gbs->read8();
155      unsigned char xlo = gbs->read8();
156      unsigned char yhi = gbs->read8();
157      unsigned char ylo = gbs->read8();
158      out_str.format( ", v%d.%d (%s), %dx%d", major & 0x7f, minor,
159                      (major & 0x80 ? "b&w" : "color"), (xhi<<8)+xlo, (yhi<<8)+ylo );
160    }
161}
162
163static void
164display_djvm_dirm(ByteStream & out_str, IFFByteStream & iff,
165                  GUTF8String head, size_t, DjVmInfo& djvminfo, int)
166{
167  GP<DjVmDir> dir = DjVmDir::create();
168  dir->decode(iff.get_bytestream());
169  GPList<DjVmDir::File> list = dir->get_files_list();
170  if (dir->is_indirect())
171  {
172    out_str.format( "Document directory (indirect, %d files %d pages)", 
173                          dir->get_files_num(), dir->get_pages_num());
174    for (GPosition p=list; p; ++p)
175      out_str.format( "\n%s%s -> %s", (const char*)head, 
176                      (const char*)list[p]->get_load_name(), (const char*)list[p]->get_save_name() );
177  }
178  else
179  {
180    out_str.format( "Document directory (bundled, %d files %d pages)", 
181                          dir->get_files_num(), dir->get_pages_num());
182    djvminfo.dir = dir;
183    djvminfo.map.empty();
184    for (GPosition p=list; p; ++p)
185      djvminfo.map[list[p]->offset] = list[p];
186  }
187}
188
189static void
190display_th44(ByteStream & out_str, IFFByteStream & iff,
191             GUTF8String, size_t, DjVmInfo & djvminfo, int counter)
192{
193   int start_page=-1;
194   if (djvminfo.dir)
195   {
196      GPList<DjVmDir::File> files_list=djvminfo.dir->get_files_list();
197      for(GPosition pos=files_list;pos;++pos)
198      {
199         GP<DjVmDir::File> frec=files_list[pos];
200         if (iff.tell()>=frec->offset &&
201             iff.tell()<frec->offset+frec->size)
202         {
203            while(pos && !files_list[pos]->is_page())
204               ++pos;
205            if (pos)
206               start_page=files_list[pos]->get_page_num();
207            break;
208         }
209      }
210   }
211   if (start_page>=0)
212      out_str.format( "Thumbnail icon for page %d", start_page+counter+1);
213   else
214      out_str.format( "Thumbnail icon");
215}
216
217static void
218display_incl(ByteStream & out_str, IFFByteStream & iff,
219             GUTF8String, size_t, DjVmInfo&, int)
220{
221   GUTF8String name;
222   char ch;
223   while(iff.read(&ch, 1) && ch!='\n')
224     name += ch;
225   out_str.format( "Indirection chunk --> {%s}", (const char *) name);
226}
227
228static void
229display_anno(ByteStream & out_str, IFFByteStream &iff,
230             GUTF8String, size_t, DjVmInfo&, int)
231{
232   out_str.format( "Page annotation");
233   GUTF8String id;
234   iff.short_id(id);
235   out_str.format( " (hyperlinks, etc.)");
236}
237
238static void
239display_text(ByteStream & out_str, IFFByteStream &iff,
240             GUTF8String, size_t, DjVmInfo&, int)
241{
242   out_str.format( "Hidden text");
243   GUTF8String id;
244   iff.short_id(id);
245   out_str.format( " (text, etc.)");
246}
247
248struct displaysubr
249{
250  const char *id;
251  void (*subr)(ByteStream &, IFFByteStream &, GUTF8String,
252               size_t, DjVmInfo&, int counter);
253};
254 
255static displaysubr disproutines[] = 
256{
257  { "DJVU.INFO", display_djvu_info },
258  { "DJVU.Smmr", display_smmr },
259  { "DJVU.Sjbz", display_sjbz },
260  { "DJVU.Djbz", display_djbz },
261  { "DJVU.FG44", display_iw4 },
262  { "DJVU.BG44", display_iw4 },
263  { "DJVU.FGbz", display_fgbz },
264  { "DJVI.Sjbz", display_sjbz },
265  { "DJVI.Djbz", display_djbz },
266  { "DJVI.FGbz", display_fgbz },
267  { "DJVI.FG44", display_iw4 },
268  { "DJVI.BG44", display_iw4 },
269  { "BM44.BM44", display_iw4 },
270  { "PM44.PM44", display_iw4 },
271  { "DJVM.DIRM", display_djvm_dirm },
272  { "THUM.TH44", display_th44 },
273  { "INCL", display_incl },
274  { "ANTa", display_anno },
275  { "ANTz", display_anno },
276  { "TXTa", display_text },
277  { "TXTz", display_text },
278  { 0, 0 },
279};
280
281// ---------- ROUTINES FOR DISPLAYING CHUNK STRUCTURE
282
283static void
284display_chunks(ByteStream & out_str, IFFByteStream &iff,
285               const GUTF8String &head, DjVmInfo djvminfo)
286{
287  size_t size;
288  GUTF8String id, fullid;
289  GUTF8String head2 = head + "  ";
290  GPMap<int,DjVmDir::File> djvmmap;
291  int rawoffset;
292  GMap<GUTF8String, int> counters;
293 
294  while ((size = iff.get_chunk(id, &rawoffset)))
295  {
296    if (!counters.contains(id)) counters[id]=0;
297    else counters[id]++;
298   
299    GUTF8String msg;
300    msg.format("%s%s [%d] ", (const char *)head, (const char *)id, size);
301    out_str.format( "%s", (const char *)msg);
302    // Display DJVM is when adequate
303    if (djvminfo.dir)
304    {
305      GP<DjVmDir::File> rec = djvminfo.map[rawoffset];
306      if (rec)
307        out_str.format( "{%s}", (const char*) rec->get_load_name());
308    }
309    // Test chunk type
310    iff.full_id(fullid);
311    for (int i=0; disproutines[i].id; i++)
312      if (fullid == disproutines[i].id || id == disproutines[i].id)
313      {
314        int n = msg.length();
315        while (n++ < 14+(int) head.length()) putchar(out_str, ' ');
316        if (!iff.composite()) out_str.format( "    ");
317        (*disproutines[i].subr)(out_str, iff, head2,
318                                size, djvminfo, counters[id]);
319        break;
320      }
321      // Default display of composite chunk
322      out_str.format( "\n");
323      if (iff.composite())
324        display_chunks(out_str, iff, head2, djvminfo);
325      // Terminate
326      iff.close_chunk();
327  }
328}
329
330GP<ByteStream>
331DjVuDumpHelper::dump(const GP<DataPool> & pool)
332{
333   return dump(pool->get_stream());
334}
335
336GP<ByteStream>
337DjVuDumpHelper::dump(GP<ByteStream> gstr)
338{
339   GP<ByteStream> out_str=ByteStream::create();
340   GUTF8String head="  ";
341   GP<IFFByteStream> iff=IFFByteStream::create(gstr);
342   DjVmInfo djvminfo;
343   display_chunks(*out_str, *iff, head, djvminfo);
344   return out_str;
345}
346
347
348#ifdef HAVE_NAMESPACES
349}
350# ifndef NOT_USING_DJVU_NAMESPACE
351using namespace DJVU;
352# endif
353#endif
Note: See TracBrowser for help on using the repository browser.