source: trunk/libdjvu/DjVuDumpHelper.cpp @ 206

Last change on this file since 206 was 206, checked in by Eugene Romanenko, 14 years ago

DJVU plugin: djvulibre updated to version 3.5.19

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