source: trunk/libdjvu/DjVuDumpHelper.cpp @ 280

Last change on this file since 280 was 280, checked in by rbri, 11 years ago

DJVU plugin: djvulibre updated to version 3.5.22

File size: 10.7 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.11 2008/02/26 05:07:40 leonb Exp $
57// $Name: release_3_5_22 $
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  GP<ByteStream> gbs = iff.get_bytestream();
128  int version = gbs->read8();
129  int size = gbs->read16();
130  out_str.format( "JB2 colors data, v%d, %d colors", 
131                  version & 0x7f, size);
132}
133
134static void
135display_sjbz(ByteStream & out_str, IFFByteStream &iff,
136             GUTF8String, size_t, DjVmInfo&, int)
137{
138  out_str.format( "JB2 bilevel data");
139}
140
141static void
142display_smmr(ByteStream & out_str, IFFByteStream &iff,
143             GUTF8String, size_t, DjVmInfo&, int)
144{
145  out_str.format( "G4/MMR stencil data");
146}
147
148static void
149display_iw4(ByteStream & out_str, IFFByteStream &iff,
150            GUTF8String, size_t, DjVmInfo&, int)
151{
152  GP<ByteStream> gbs = iff.get_bytestream();
153  unsigned char serial = gbs->read8();
154  unsigned char slices = gbs->read8();
155  out_str.format( "IW4 data #%d, %d slices", serial+1, slices);
156  if (serial == 0)
157    {
158      unsigned char major = gbs->read8();
159      unsigned char minor = gbs->read8();
160      unsigned char xhi = gbs->read8();
161      unsigned char xlo = gbs->read8();
162      unsigned char yhi = gbs->read8();
163      unsigned char ylo = gbs->read8();
164      out_str.format( ", v%d.%d (%s), %dx%d", major & 0x7f, minor,
165                      (major & 0x80 ? "b&w" : "color"), 
166                      (xhi<<8)+xlo, (yhi<<8)+ylo );
167    }
168}
169
170static void
171display_djvm_dirm(ByteStream & out_str, IFFByteStream & iff,
172                  GUTF8String head, size_t, DjVmInfo& djvminfo, int)
173{
174  GP<DjVmDir> dir = DjVmDir::create();
175  dir->decode(iff.get_bytestream());
176  GPList<DjVmDir::File> list = dir->get_files_list();
177  if (dir->is_indirect())
178  {
179    out_str.format( "Document directory (indirect, %d files %d pages)", 
180                          dir->get_files_num(), dir->get_pages_num());
181    for (GPosition p=list; p; ++p)
182      out_str.format( "\n%s%s -> %s", (const char*)head, 
183                      (const char*)list[p]->get_load_name(), (const char*)list[p]->get_save_name() );
184  }
185  else
186  {
187    out_str.format( "Document directory (bundled, %d files %d pages)", 
188                          dir->get_files_num(), dir->get_pages_num());
189    djvminfo.dir = dir;
190    djvminfo.map.empty();
191    for (GPosition p=list; p; ++p)
192      djvminfo.map[list[p]->offset] = list[p];
193  }
194}
195
196static void
197display_th44(ByteStream & out_str, IFFByteStream & iff,
198             GUTF8String, size_t, DjVmInfo & djvminfo, int counter)
199{
200   int start_page=-1;
201   if (djvminfo.dir)
202   {
203      GPList<DjVmDir::File> files_list=djvminfo.dir->get_files_list();
204      for(GPosition pos=files_list;pos;++pos)
205      {
206         GP<DjVmDir::File> frec=files_list[pos];
207         if (iff.tell()>=frec->offset &&
208             iff.tell()<frec->offset+frec->size)
209         {
210            while(pos && !files_list[pos]->is_page())
211               ++pos;
212            if (pos)
213               start_page=files_list[pos]->get_page_num();
214            break;
215         }
216      }
217   }
218   if (start_page>=0)
219      out_str.format( "Thumbnail icon for page %d", start_page+counter+1);
220   else
221      out_str.format( "Thumbnail icon");
222}
223
224static void
225display_incl(ByteStream & out_str, IFFByteStream & iff,
226             GUTF8String, size_t, DjVmInfo&, int)
227{
228   GUTF8String name;
229   char ch;
230   while(iff.read(&ch, 1) && ch!='\n')
231     name += ch;
232   out_str.format( "Indirection chunk --> {%s}", (const char *) name);
233}
234
235static void
236display_anno(ByteStream & out_str, IFFByteStream &iff,
237             GUTF8String, size_t, DjVmInfo&, int)
238{
239   out_str.format( "Page annotation");
240   GUTF8String id;
241   iff.short_id(id);
242   out_str.format( " (hyperlinks, etc.)");
243}
244
245static void
246display_text(ByteStream & out_str, IFFByteStream &iff,
247             GUTF8String, size_t, DjVmInfo&, int)
248{
249   out_str.format( "Hidden text");
250   GUTF8String id;
251   iff.short_id(id);
252   out_str.format( " (text, etc.)");
253}
254
255struct displaysubr
256{
257  const char *id;
258  void (*subr)(ByteStream &, IFFByteStream &, GUTF8String,
259               size_t, DjVmInfo&, int counter);
260};
261 
262static displaysubr disproutines[] = 
263{
264  { "DJVU.INFO", display_djvu_info },
265  { "DJVU.Smmr", display_smmr },
266  { "DJVU.Sjbz", display_sjbz },
267  { "DJVU.Djbz", display_djbz },
268  { "DJVU.FG44", display_iw4 },
269  { "DJVU.BG44", display_iw4 },
270  { "DJVU.FGbz", display_fgbz },
271  { "DJVI.Sjbz", display_sjbz },
272  { "DJVI.Djbz", display_djbz },
273  { "DJVI.FGbz", display_fgbz },
274  { "DJVI.FG44", display_iw4 },
275  { "DJVI.BG44", display_iw4 },
276  { "BM44.BM44", display_iw4 },
277  { "PM44.PM44", display_iw4 },
278  { "DJVM.DIRM", display_djvm_dirm },
279  { "THUM.TH44", display_th44 },
280  { "INCL", display_incl },
281  { "ANTa", display_anno },
282  { "ANTz", display_anno },
283  { "TXTa", display_text },
284  { "TXTz", display_text },
285  { 0, 0 },
286};
287
288// ---------- ROUTINES FOR DISPLAYING CHUNK STRUCTURE
289
290static void
291display_chunks(ByteStream & out_str, IFFByteStream &iff,
292               const GUTF8String &head, DjVmInfo djvminfo)
293{
294  size_t size;
295  GUTF8String id, fullid;
296  GUTF8String head2 = head + "  ";
297  GPMap<int,DjVmDir::File> djvmmap;
298  int rawoffset;
299  GMap<GUTF8String, int> counters;
300 
301  while ((size = iff.get_chunk(id, &rawoffset)))
302  {
303    if (!counters.contains(id)) counters[id]=0;
304    else counters[id]++;
305   
306    GUTF8String msg;
307    msg.format("%s%s [%d] ", (const char *)head, (const char *)id, size);
308    out_str.format( "%s", (const char *)msg);
309    // Display DJVM is when adequate
310    if (djvminfo.dir)
311    {
312      GP<DjVmDir::File> rec = djvminfo.map[rawoffset];
313      if (rec)
314        out_str.format( "{%s}", (const char*) rec->get_load_name());
315    }
316    // Test chunk type
317    iff.full_id(fullid);
318    for (int i=0; disproutines[i].id; i++)
319      if (fullid == disproutines[i].id || id == disproutines[i].id)
320      {
321        int n = msg.length();
322        while (n++ < 14+(int) head.length()) putchar(out_str, ' ');
323        if (!iff.composite()) out_str.format( "    ");
324        (*disproutines[i].subr)(out_str, iff, head2,
325                                size, djvminfo, counters[id]);
326        break;
327      }
328      // Default display of composite chunk
329      out_str.format( "\n");
330      if (iff.composite())
331        display_chunks(out_str, iff, head2, djvminfo);
332      // Terminate
333      iff.close_chunk();
334  }
335}
336
337GP<ByteStream>
338DjVuDumpHelper::dump(const GP<DataPool> & pool)
339{
340   return dump(pool->get_stream());
341}
342
343GP<ByteStream>
344DjVuDumpHelper::dump(GP<ByteStream> gstr)
345{
346   GP<ByteStream> out_str=ByteStream::create();
347   GUTF8String head="  ";
348   GP<IFFByteStream> iff=IFFByteStream::create(gstr);
349   DjVmInfo djvminfo;
350   display_chunks(*out_str, *iff, head, djvminfo);
351   return out_str;
352}
353
354
355#ifdef HAVE_NAMESPACES
356}
357# ifndef NOT_USING_DJVU_NAMESPACE
358using namespace DJVU;
359# endif
360#endif
Note: See TracBrowser for help on using the repository browser.