source: trunk/libdjvu/DjVuInfo.cpp @ 15

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

needed libs update

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.9 2003/11/07 22:08:21 leonb Exp $
55// $Name: release_3_5_16 $
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), compressable(false), orientation(GRect::BULRNR)
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  compressable=false;
106  orientation=GRect::BULRNR;
107  // Read data
108  unsigned char buffer[10];
109  int  size = bs.readall((void*)buffer, sizeof(buffer));
110  if (size == 0)
111    G_THROW( ByteStream::EndOfFile );
112  if (size < 5)
113    G_THROW( ERR_MSG("DjVuInfo.corrupt_file") );
114  // Analyze data with backward compatibility in mind!
115  if (size>=2)
116    width = (buffer[0]<<8) + buffer[1];
117  if (size>=4)
118    height = (buffer[2]<<8) + buffer[3];
119  if (size>=5)
120    version = buffer[4];
121  if (size>=6 && buffer[5]!=0xff)
122    version = (buffer[5]<<8) + buffer[4];
123  if (size>=8 && buffer[7]!=0xff)
124    dpi = (buffer[7]<<8) + buffer[6];
125  if (size>=9)
126    gamma = 0.1 * buffer[8];
127  int flags=0;
128  if (size>=10)
129    flags = buffer[9];
130  // Fixup
131  if (gamma<0.3)
132     gamma=0.3;
133  if (gamma>5.0)
134     gamma=5.0;
135  if (dpi < 25 || dpi > 6000)
136    dpi = 300;
137  if(flags&COMPRESSABLE_FLAG)
138    compressable=true;
139  if(version>=DJVUVERSION_ORIENTATION)
140  {
141    orientation=(GRect::Orientations)(flags&((int)GRect::BOTTOM_UP|(int)GRect::MIRROR|(int)GRect::ROTATE90_CW));
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=orientation;
156  if(compressable) 
157  {
158    flags|=COMPRESSABLE_FLAG;
159  }
160  bs.write8(flags);
161}
162
163unsigned int 
164DjVuInfo::get_memory_usage() const
165{
166  return sizeof(DjVuInfo);
167}
168
169GUTF8String
170DjVuInfo::get_paramtags(void) const
171{
172  const int angle=GRect::findangle(orientation);
173  GUTF8String retval;
174  if(angle)
175  {
176    retval+="<PARAM name=\"ROTATE\" value=\""+GUTF8String(angle)+"\" />\n";
177  }
178  if(orientation == GRect::rotate(angle,GRect::TDLRNR))
179  {
180    retval+="<PARAM name=\"VFLIP\" value=\"true\" />\n";
181  }
182  if(dpi)
183  {
184    retval+="<PARAM name=\"DPI\" value=\""+GUTF8String(dpi)+"\" />\n";
185  }
186  if(gamma)
187  {
188    retval+="<PARAM name=\"GAMMA\" value=\""+GUTF8String(gamma)+"\" />\n";
189  }
190  return retval;
191}
192
193void
194DjVuInfo::writeParam(ByteStream &str_out) const
195{
196  str_out.writestring(get_paramtags());
197}
198
199
200#ifdef HAVE_NAMESPACES
201}
202# ifndef NOT_USING_DJVU_NAMESPACE
203using namespace DJVU;
204# endif
205#endif
Note: See TracBrowser for help on using the repository browser.