summaryrefslogtreecommitdiff
path: root/src/lib/PictXParser.cpp
blob: 120af59b498411519219ece3085c16e411d10e99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * This file is part of the libpict project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "PictXParser.h"
#include "libpict_utils.h"

PictXParser::PictXParser(librevenge::RVNGInputStream *input, librevenge::RVNGDrawingInterface *painter):
  m_input(input), m_painter(painter), m_colorPalette(std::map<int,libpict::PictColor>())
{
}

PictXParser::PictXParser(const PictXParser &parser):
  m_input(parser.m_input), m_painter(parser.m_painter),
  m_colorPalette(parser.m_colorPalette)
{
}

unsigned char PictXParser::readU8()
{
  if (!m_input || m_input->isEnd())
    return (unsigned char)0;
  unsigned long numBytesRead;
  unsigned char const *p = m_input->read(sizeof(unsigned char), numBytesRead);

  if (p && numBytesRead == 1)
    return *(unsigned char const *)(p);
  return (unsigned char)0;
}

unsigned short PictXParser::readU16()
{
  unsigned short p0 = (unsigned short)readU8();
  unsigned short p1 = (unsigned short)readU8();
  return (unsigned short)(p1|(p0<<8));
}

unsigned PictXParser::readU32()
{
  unsigned p0 = (unsigned)readU8();
  unsigned p1 = (unsigned)readU8();
  unsigned p2 = (unsigned)readU8();
  unsigned p3 = (unsigned)readU8();
  return (unsigned long)(p3|(p2<<8)|(p1<<16)|(p0<<24));
}

short PictXParser::readS16()
{
  return (short)readU16();
}

int PictXParser::readS32()
{
  return (int)readU32();
}

unsigned PictXParser::readVariableLengthInteger()
{
  // read a byte
  unsigned char value8 = readU8();
  // if it's in the range 0-0xFE, then we have a 8-bit value
  if (value8<=0xFE)
  {
    return (unsigned)value8;
  }
  else
  {
    // now read a 16 bit value
    unsigned short value16 = readU16();
    // if the MSB is 1, we have a 32 bit value
    if (value16>>15)
    {
      // read the next 16 bit value (LSB part, in value16 resides the MSB part)
      unsigned long lvalue16 = readU16();
      unsigned long value32 = value16 & 0x7fff;  // mask out the MSB
      return (value32<<16)+lvalue16;
    }
    else
    {
      // we have a 16 bit value, return it
      return (unsigned)value16;
    }
  }
}

PictXParser &PictXParser::operator=(const PictXParser &parser)
{
  m_input = parser.m_input;
  m_painter = parser.m_painter;
  m_colorPalette = parser.m_colorPalette;
  return *this;
}
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */