source: trunk/libdjvu/DjVuInfo.cpp @ 101

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

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

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