kviewshell
DjVuInfo.cpp
Go to the documentation of this file.00001 //C- -*- C++ -*- 00002 //C- ------------------------------------------------------------------- 00003 //C- DjVuLibre-3.5 00004 //C- Copyright (c) 2002 Leon Bottou and Yann Le Cun. 00005 //C- Copyright (c) 2001 AT&T 00006 //C- 00007 //C- This software is subject to, and may be distributed under, the 00008 //C- GNU General Public License, Version 2. The license should have 00009 //C- accompanied the software or you may obtain a copy of the license 00010 //C- from the Free Software Foundation at http://www.fsf.org . 00011 //C- 00012 //C- This program is distributed in the hope that it will be useful, 00013 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 //C- GNU General Public License for more details. 00016 //C- 00017 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library 00018 //C- distributed by Lizardtech Software. On July 19th 2002, Lizardtech 00019 //C- Software authorized us to replace the original DjVu(r) Reference 00020 //C- Library notice by the following text (see doc/lizard2002.djvu): 00021 //C- 00022 //C- ------------------------------------------------------------------ 00023 //C- | DjVu (r) Reference Library (v. 3.5) 00024 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved. 00025 //C- | The DjVu Reference Library is protected by U.S. Pat. No. 00026 //C- | 6,058,214 and patents pending. 00027 //C- | 00028 //C- | This software is subject to, and may be distributed under, the 00029 //C- | GNU General Public License, Version 2. The license should have 00030 //C- | accompanied the software or you may obtain a copy of the license 00031 //C- | from the Free Software Foundation at http://www.fsf.org . 00032 //C- | 00033 //C- | The computer code originally released by LizardTech under this 00034 //C- | license and unmodified by other parties is deemed "the LIZARDTECH 00035 //C- | ORIGINAL CODE." Subject to any third party intellectual property 00036 //C- | claims, LizardTech grants recipient a worldwide, royalty-free, 00037 //C- | non-exclusive license to make, use, sell, or otherwise dispose of 00038 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 00039 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 00040 //C- | General Public License. This grant only confers the right to 00041 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 00042 //C- | the extent such infringement is reasonably necessary to enable 00043 //C- | recipient to make, have made, practice, sell, or otherwise dispose 00044 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 00045 //C- | any greater extent that may be necessary to utilize further 00046 //C- | modifications or combinations. 00047 //C- | 00048 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY 00049 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 00050 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF 00051 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 00052 //C- +------------------------------------------------------------------ 00053 // 00054 // $Id: DjVuInfo.cpp,v 1.9 2003/11/07 22:08:21 leonb Exp $ 00055 // $Name: release_3_5_15 $ 00056 00057 #ifdef HAVE_CONFIG_H 00058 # include "config.h" 00059 #endif 00060 #if NEED_GNUG_PRAGMAS 00061 # pragma implementation 00062 #endif 00063 00064 #include "DjVuInfo.h" 00065 #include "GException.h" 00066 #include "ByteStream.h" 00067 #include "GString.h" 00068 00069 00070 #ifdef HAVE_NAMESPACES 00071 namespace DJVU { 00072 # ifdef NOT_DEFINED // Just to fool emacs c++ mode 00073 } 00074 #endif 00075 #endif 00076 00077 // ---------------------------------------- 00078 // CLASS DJVUINFO 00079 00080 00081 #define STRINGIFY(x) STRINGIFY_(x) 00082 #define STRINGIFY_(x) #x 00083 00084 00085 DjVuInfo::DjVuInfo() 00086 : width(0), height(0), 00087 #ifdef DJVUVERSION_FOR_OUTPUT 00088 version(DJVUVERSION_FOR_OUTPUT), 00089 #else 00090 version(DJVUVERSION), 00091 #endif 00092 dpi(300), gamma(2.2), compressable(false), orientation(GRect::BULRNR) 00093 { 00094 } 00095 00096 void 00097 DjVuInfo::decode(ByteStream &bs) 00098 { 00099 // Set to default values 00100 width = 0; 00101 height = 0; 00102 version = DJVUVERSION; 00103 dpi = 300; 00104 gamma = 2.2; 00105 compressable=false; 00106 orientation=GRect::BULRNR; 00107 // Read data 00108 unsigned char buffer[10]; 00109 int size = bs.readall((void*)buffer, sizeof(buffer)); 00110 if (size == 0) 00111 G_THROW( ByteStream::EndOfFile ); 00112 if (size < 5) 00113 G_THROW( ERR_MSG("DjVuInfo.corrupt_file") ); 00114 // Analyze data with backward compatibility in mind! 00115 if (size>=2) 00116 width = (buffer[0]<<8) + buffer[1]; 00117 if (size>=4) 00118 height = (buffer[2]<<8) + buffer[3]; 00119 if (size>=5) 00120 version = buffer[4]; 00121 if (size>=6 && buffer[5]!=0xff) 00122 version = (buffer[5]<<8) + buffer[4]; 00123 if (size>=8 && buffer[7]!=0xff) 00124 dpi = (buffer[7]<<8) + buffer[6]; 00125 if (size>=9) 00126 gamma = 0.1 * buffer[8]; 00127 int flags=0; 00128 if (size>=10) 00129 flags = buffer[9]; 00130 // Fixup 00131 if (gamma<0.3) 00132 gamma=0.3; 00133 if (gamma>5.0) 00134 gamma=5.0; 00135 if (dpi < 25 || dpi > 6000) 00136 dpi = 300; 00137 if(flags&COMPRESSABLE_FLAG) 00138 compressable=true; 00139 if(version>=DJVUVERSION_ORIENTATION) 00140 { 00141 orientation=(GRect::Orientations)(flags&((int)GRect::BOTTOM_UP|(int)GRect::MIRROR|(int)GRect::ROTATE90_CW)); 00142 } 00143 } 00144 00145 void 00146 DjVuInfo::encode(ByteStream &bs) 00147 { 00148 bs.write16(width); 00149 bs.write16(height); 00150 bs.write8(version & 0xff); 00151 bs.write8(version >> 8); 00152 bs.write8(dpi & 0xff); 00153 bs.write8(dpi >> 8); 00154 bs.write8((int)(10.0*gamma+0.5) ); 00155 unsigned char flags=orientation; 00156 if(compressable) 00157 { 00158 flags|=COMPRESSABLE_FLAG; 00159 } 00160 bs.write8(flags); 00161 } 00162 00163 unsigned int 00164 DjVuInfo::get_memory_usage() const 00165 { 00166 return sizeof(DjVuInfo); 00167 } 00168 00169 GUTF8String 00170 DjVuInfo::get_paramtags(void) const 00171 { 00172 const int angle=GRect::findangle(orientation); 00173 GUTF8String retval; 00174 if(angle) 00175 { 00176 retval+="<PARAM name=\"ROTATE\" value=\""+GUTF8String(angle)+"\" />\n"; 00177 } 00178 if(orientation == GRect::rotate(angle,GRect::TDLRNR)) 00179 { 00180 retval+="<PARAM name=\"VFLIP\" value=\"true\" />\n"; 00181 } 00182 if(dpi) 00183 { 00184 retval+="<PARAM name=\"DPI\" value=\""+GUTF8String(dpi)+"\" />\n"; 00185 } 00186 if(gamma) 00187 { 00188 retval+="<PARAM name=\"GAMMA\" value=\""+GUTF8String(gamma)+"\" />\n"; 00189 } 00190 return retval; 00191 } 00192 00193 void 00194 DjVuInfo::writeParam(ByteStream &str_out) const 00195 { 00196 str_out.writestring(get_paramtags()); 00197 } 00198 00199 00200 #ifdef HAVE_NAMESPACES 00201 } 00202 # ifndef NOT_USING_DJVU_NAMESPACE 00203 using namespace DJVU; 00204 # endif 00205 #endif