source: trunk/libdjvu/DjVuInfo.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: 5.9 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: DjVuInfo.cpp,v 1.11 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 "DjVuInfo.h"
67#include "GException.h"
68#include "ByteStream.h"
69#include "GString.h"
70
71
72#ifdef HAVE_NAMESPACES
73namespace DJVU {
74# ifdef NOT_DEFINED // Just to fool emacs c++ mode
75}
76#endif
77#endif
78
79// ----------------------------------------
80// CLASS DJVUINFO
81
82
83#define STRINGIFY(x) STRINGIFY_(x)
84#define STRINGIFY_(x) #x
85
86
87DjVuInfo::DjVuInfo()
88  : width(0), height(0), 
89#ifdef DJVUVERSION_FOR_OUTPUT
90    version(DJVUVERSION_FOR_OUTPUT),
91#else
92    version(DJVUVERSION),
93#endif
94    dpi(300), gamma(2.2), orientation(0)
95{
96}
97
98void 
99DjVuInfo::decode(ByteStream &bs)
100{
101  // Set to default values
102  width = 0;
103  height = 0;
104  version = DJVUVERSION;
105  dpi = 300;
106  gamma = 2.2;
107  orientation=0;
108  // Read data
109  unsigned char buffer[10];
110  int  size = bs.readall((void*)buffer, sizeof(buffer));
111  if (size == 0)
112    G_THROW( ByteStream::EndOfFile );
113  if (size < 5)
114    G_THROW( ERR_MSG("DjVuInfo.corrupt_file") );
115  // Analyze data with backward compatibility in mind!
116  if (size>=2)
117    width = (buffer[0]<<8) + buffer[1];
118  if (size>=4)
119    height = (buffer[2]<<8) + buffer[3];
120  if (size>=5)
121    version = buffer[4];
122  if (size>=6 && buffer[5]!=0xff)
123    version = (buffer[5]<<8) + buffer[4];
124  if (size>=8 && buffer[7]!=0xff)
125    dpi = (buffer[7]<<8) + buffer[6];
126  if (size>=9)
127    gamma = 0.1 * buffer[8];
128  int flags=0;
129  if (size>=10)
130    flags = buffer[9];
131  // Fixup
132  if (gamma<0.3)
133    gamma=0.3;
134  if (gamma>5.0)
135    gamma=5.0;
136  if (dpi < 25 || dpi > 6000)
137    dpi = 300;
138  switch (flags & 0x7)
139    {
140    case 6:  orientation=1; break; 
141    case 2:  orientation=2; break; 
142    case 5:  orientation=3; break;
143    default: orientation=0; break;
144    }
145}
146
147void 
148DjVuInfo::encode(ByteStream &bs)
149{
150  bs.write16(width);
151  bs.write16(height);
152  bs.write8(version & 0xff);
153  bs.write8(version >> 8);
154  bs.write8(dpi & 0xff);
155  bs.write8(dpi >> 8);
156  bs.write8((int)(10.0*gamma+0.5) );
157  unsigned char flags;
158  switch (orientation) 
159    {
160    default: flags=0; break;
161    case 1:  flags=6; break;
162    case 2:  flags=2; break;
163    case 3:  flags=5; break;
164    }
165  bs.write8(flags);
166}
167
168unsigned int 
169DjVuInfo::get_memory_usage() const
170{
171  return sizeof(DjVuInfo);
172}
173
174GUTF8String
175DjVuInfo::get_paramtags(void) const
176{
177  GUTF8String retval;
178  if(orientation)
179    retval+="<PARAM name=\"ROTATE\" value=\""
180      +GUTF8String(((4-orientation)%4)*90)+"\" />\n";
181  if(dpi)
182    retval+="<PARAM name=\"DPI\" value=\""
183      +GUTF8String(dpi)+"\" />\n";
184  if(gamma)
185    retval+="<PARAM name=\"GAMMA\" value=\""
186      +GUTF8String(gamma)+"\" />\n";
187  return retval;
188}
189
190void
191DjVuInfo::writeParam(ByteStream &str_out) const
192{
193  str_out.writestring(get_paramtags());
194}
195
196
197#ifdef HAVE_NAMESPACES
198}
199# ifndef NOT_USING_DJVU_NAMESPACE
200using namespace DJVU;
201# endif
202#endif
Note: See TracBrowser for help on using the repository browser.