Blame libdjvu/DjVuInfo.cpp

Packit df99a1
//C-  -*- C++ -*-
Packit df99a1
//C- -------------------------------------------------------------------
Packit df99a1
//C- DjVuLibre-3.5
Packit df99a1
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
Packit df99a1
//C- Copyright (c) 2001  AT&T
Packit df99a1
//C-
Packit df99a1
//C- This software is subject to, and may be distributed under, the
Packit df99a1
//C- GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- or (at your option) any later version. The license should have
Packit df99a1
//C- accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C-
Packit df99a1
//C- This program is distributed in the hope that it will be useful,
Packit df99a1
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit df99a1
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit df99a1
//C- GNU General Public License for more details.
Packit df99a1
//C- 
Packit df99a1
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
Packit df99a1
//C- Lizardtech Software.  Lizardtech Software has authorized us to
Packit df99a1
//C- replace the original DjVu(r) Reference Library notice by the following
Packit df99a1
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
Packit df99a1
//C-
Packit df99a1
//C-  ------------------------------------------------------------------
Packit df99a1
//C- | DjVu (r) Reference Library (v. 3.5)
Packit df99a1
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
Packit df99a1
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
Packit df99a1
//C- | 6,058,214 and patents pending.
Packit df99a1
//C- |
Packit df99a1
//C- | This software is subject to, and may be distributed under, the
Packit df99a1
//C- | GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- | or (at your option) any later version. The license should have
Packit df99a1
//C- | accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- | from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C- |
Packit df99a1
//C- | The computer code originally released by LizardTech under this
Packit df99a1
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
Packit df99a1
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
Packit df99a1
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
Packit df99a1
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
Packit df99a1
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
Packit df99a1
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
Packit df99a1
//C- | General Public License.   This grant only confers the right to 
Packit df99a1
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
Packit df99a1
//C- | the extent such infringement is reasonably necessary to enable 
Packit df99a1
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
Packit df99a1
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
Packit df99a1
//C- | any greater extent that may be necessary to utilize further 
Packit df99a1
//C- | modifications or combinations.
Packit df99a1
//C- |
Packit df99a1
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
Packit df99a1
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
Packit df99a1
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
Packit df99a1
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit df99a1
//C- +------------------------------------------------------------------
Packit df99a1
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
# include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma implementation
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#include "DjVuInfo.h"
Packit df99a1
#include "GException.h"
Packit df99a1
#include "ByteStream.h"
Packit df99a1
#include "GString.h"
Packit df99a1
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
namespace DJVU {
Packit df99a1
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
Packit df99a1
}
Packit df99a1
#endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
// ----------------------------------------
Packit df99a1
// CLASS DJVUINFO
Packit df99a1
Packit df99a1
Packit df99a1
#define STRINGIFY(x) STRINGIFY_(x)
Packit df99a1
#define STRINGIFY_(x) #x
Packit df99a1
Packit df99a1
Packit df99a1
DjVuInfo::DjVuInfo()
Packit df99a1
  : width(0), height(0), 
Packit df99a1
#ifdef DJVUVERSION_FOR_OUTPUT
Packit df99a1
    version(DJVUVERSION_FOR_OUTPUT),
Packit df99a1
#else
Packit df99a1
    version(DJVUVERSION),
Packit df99a1
#endif
Packit df99a1
    dpi(300), gamma(2.2), orientation(0)
Packit df99a1
{
Packit df99a1
}
Packit df99a1
Packit df99a1
void 
Packit df99a1
DjVuInfo::decode(ByteStream &bs)
Packit df99a1
{
Packit df99a1
  // Set to default values
Packit df99a1
  width = 0;
Packit df99a1
  height = 0;
Packit df99a1
  version = DJVUVERSION;
Packit df99a1
  dpi = 300;
Packit df99a1
  gamma = 2.2;
Packit df99a1
  orientation=0;
Packit df99a1
  // Read data
Packit df99a1
  unsigned char buffer[10];
Packit df99a1
  int  size = bs.readall((void*)buffer, sizeof(buffer));
Packit df99a1
  if (size == 0)
Packit df99a1
    G_THROW( ByteStream::EndOfFile );
Packit df99a1
  if (size < 5)
Packit df99a1
    G_THROW( ERR_MSG("DjVuInfo.corrupt_file") );
Packit df99a1
  // Analyze data with backward compatibility in mind!
Packit df99a1
  if (size>=2)
Packit df99a1
    width = (buffer[0]<<8) + buffer[1];
Packit df99a1
  if (size>=4)
Packit df99a1
    height = (buffer[2]<<8) + buffer[3];
Packit df99a1
  if (size>=5)
Packit df99a1
    version = buffer[4];
Packit df99a1
  if (size>=6 && buffer[5]!=0xff)
Packit df99a1
    version = (buffer[5]<<8) + buffer[4];
Packit df99a1
  if (size>=8 && buffer[7]!=0xff)
Packit df99a1
    dpi = (buffer[7]<<8) + buffer[6];
Packit df99a1
  if (size>=9)
Packit df99a1
    gamma = 0.1 * buffer[8];
Packit df99a1
  int flags=0;
Packit df99a1
  if (size>=10)
Packit df99a1
    flags = buffer[9];
Packit df99a1
  // Fixup
Packit df99a1
  if (gamma<0.3)
Packit df99a1
    gamma=0.3;
Packit df99a1
  if (gamma>5.0)
Packit df99a1
    gamma=5.0;
Packit df99a1
  if (dpi < 25 || dpi > 6000)
Packit df99a1
    dpi = 300;
Packit df99a1
  switch (flags & 0x7)
Packit df99a1
    {
Packit df99a1
    case 6:  orientation=1; break; 
Packit df99a1
    case 2:  orientation=2; break; 
Packit df99a1
    case 5:  orientation=3; break;
Packit df99a1
    default: orientation=0; break;
Packit df99a1
    }
Packit df99a1
}
Packit df99a1
Packit df99a1
void 
Packit df99a1
DjVuInfo::encode(ByteStream &bs)
Packit df99a1
{
Packit df99a1
  bs.write16(width);
Packit df99a1
  bs.write16(height);
Packit df99a1
  bs.write8(version & 0xff);
Packit df99a1
  bs.write8(version >> 8);
Packit df99a1
  bs.write8(dpi & 0xff);
Packit df99a1
  bs.write8(dpi >> 8);
Packit df99a1
  bs.write8((int)(10.0*gamma+0.5) );
Packit df99a1
  unsigned char flags;
Packit df99a1
  switch (orientation) 
Packit df99a1
    {
Packit df99a1
    default: flags=1; break;
Packit df99a1
    case 1:  flags=6; break;
Packit df99a1
    case 2:  flags=2; break;
Packit df99a1
    case 3:  flags=5; break;
Packit df99a1
    }
Packit df99a1
  bs.write8(flags);
Packit df99a1
}
Packit df99a1
Packit df99a1
unsigned int 
Packit df99a1
DjVuInfo::get_memory_usage() const
Packit df99a1
{
Packit df99a1
  return sizeof(DjVuInfo);
Packit df99a1
}
Packit df99a1
Packit df99a1
GUTF8String
Packit df99a1
DjVuInfo::get_paramtags(void) const
Packit df99a1
{
Packit df99a1
  GUTF8String retval;
Packit df99a1
  if(orientation)
Packit df99a1
    retval+="
Packit df99a1
      +GUTF8String(((4-orientation)%4)*90)+"\" />\n";
Packit df99a1
  if(dpi)
Packit df99a1
    retval+="
Packit df99a1
      +GUTF8String(dpi)+"\" />\n";
Packit df99a1
  if(gamma)
Packit df99a1
    retval+="
Packit df99a1
      +GUTF8String(gamma)+"\" />\n";
Packit df99a1
  return retval;
Packit df99a1
}
Packit df99a1
Packit df99a1
void
Packit df99a1
DjVuInfo::writeParam(ByteStream &str_out) const
Packit df99a1
{
Packit df99a1
  str_out.writestring(get_paramtags());
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
}
Packit df99a1
# ifndef NOT_USING_DJVU_NAMESPACE
Packit df99a1
using namespace DJVU;
Packit df99a1
# endif
Packit df99a1
#endif