| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | * This file is a part of the libzmf project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | */ |
| 9 | |
| 10 | #include "BMIHeader.h" |
| 11 | #include <algorithm> |
| 12 | |
| 13 | namespace libzmf |
| 14 | { |
| 15 | |
| 16 | BMIHeader::BMIHeader() |
| 17 | : m_signature() |
| 18 | , m_size(0) |
| 19 | , m_startOffset(0) |
| 20 | , m_width(0) |
| 21 | , m_height(0) |
| 22 | , m_isPaletteMode(false) |
| 23 | , m_colorDepth(0) |
| 24 | , m_offsets() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | bool BMIHeader::load(const RVNGInputStreamPtr &input) |
| 29 | { |
| 30 | // don't allow multiple calls |
| 31 | if (m_signature.length() > 0) |
| 32 | throw GenericException(); |
| 33 | |
| 34 | m_startOffset = input->tell(); |
| 35 | |
| 36 | unsigned sigLen = 9; |
| 37 | m_signature.assign(reinterpret_cast<const char *>(readNBytes(input, sigLen)), sigLen); |
| 38 | |
| 39 | m_width = readU16(input); |
| 40 | m_height = readU16(input); |
| 41 | |
| 42 | m_isPaletteMode = static_cast<bool>(readU16(input)); |
| 43 | |
| 44 | m_colorDepth = readU16(input); |
| 45 | if (!(m_colorDepth == 1 || m_colorDepth == 4 || m_colorDepth == 8 || m_colorDepth == 24)) |
| 46 | { |
| 47 | ZMF_DEBUG_MSG(("Invalid color depth %d\n", m_colorDepth)); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | skip(input, 2); |
| 52 | |
| 53 | uint16_t offsetCount = readU16(input); |
| 54 | if (offsetCount == 0 || offsetCount > 6) |
| 55 | return false; |
| 56 | |
| 57 | if (m_isPaletteMode) |
| 58 | { |
| 59 | skip(input, 4 * paletteColorCount()); |
| 60 | } |
| 61 | |
| 62 | readOffsets(input, offsetCount); |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | bool BMIHeader::isSupported() const |
| 68 | { |
| 69 | return m_signature == "ZonerBMIa"; |
| 70 | } |
| 71 | unsigned BMIHeader::size() const |
| 72 | { |
| 73 | return m_size; |
| 74 | } |
| 75 | |
| 76 | unsigned BMIHeader::startOffset() const |
| 77 | { |
| 78 | return m_startOffset; |
| 79 | } |
| 80 | |
| 81 | unsigned BMIHeader::width() const |
| 82 | { |
| 83 | return m_width; |
| 84 | } |
| 85 | |
| 86 | unsigned BMIHeader::height() const |
| 87 | { |
| 88 | return m_height; |
| 89 | } |
| 90 | |
| 91 | bool BMIHeader::isPaletteMode() const |
| 92 | { |
| 93 | return m_isPaletteMode; |
| 94 | } |
| 95 | |
| 96 | unsigned BMIHeader::colorDepth() const |
| 97 | { |
| 98 | return m_colorDepth; |
| 99 | } |
| 100 | |
| 101 | unsigned BMIHeader::paletteColorCount() const |
| 102 | { |
| 103 | return isPaletteMode() ? (1 << colorDepth()) : 0; |
| 104 | } |
| 105 | |
| 106 | const std::vector<BMIOffset> &BMIHeader::offsets() const |
| 107 | { |
| 108 | return m_offsets; |
| 109 | } |
| 110 | |
| 111 | void BMIHeader::readOffsets(const RVNGInputStreamPtr &input, uint16_t offsetCount) |
| 112 | { |
| 113 | for (unsigned i = 0; i < offsetCount; i++) |
| 114 | { |
| 115 | BMIOffset off; |
| 116 | |
| 117 | auto type = readU16(input); |
| 118 | off.start = readU32(input); |
| 119 | |
| 120 | switch (type) |
| 121 | { |
| 122 | case 0x1: |
| 123 | off.type = BMIStreamType::BITMAP; |
| 124 | break; |
| 125 | case 0xff: |
| 126 | off.type = BMIStreamType::END_OF_FILE; |
| 127 | m_size = off.start; |
| 128 | break; |
| 129 | default: |
| 130 | off.type = BMIStreamType::UNKNOWN; |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | m_offsets.push_back(off); |
| 135 | } |
| 136 | |
| 137 | std::sort(m_offsets.begin(), m_offsets.end(), [](const BMIOffset &a, const BMIOffset &b) |
| 138 | { |
| 139 | return (a.start < b.start); |
| 140 | }); |
| 141 | |
| 142 | m_offsets.erase(std::unique(m_offsets.begin(), m_offsets.end()), m_offsets.end()); |
| 143 | |
| 144 | for (unsigned i = 0; i < m_offsets.size() - 1; i++) |
| 145 | { |
| 146 | m_offsets[i].end = m_offsets[i + 1].start; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | namespace |
| 151 | { |
| 152 | |
| 153 | bool reconcileValue(unsigned &v1, unsigned &v2, unsigned &v3) |
| 154 | { |
| 155 | if (v1 == v2) |
| 156 | { |
| 157 | if (v2 != v3) |
| 158 | v3 = v1; |
| 159 | } |
| 160 | else if (v1 == v3) |
| 161 | { |
| 162 | v2 = v1; |
| 163 | } |
| 164 | else if (v2 == v3) |
| 165 | { |
| 166 | v1 = v2; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | return false; |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | bool BMIHeader::reconcileWidth(unsigned &colorWidth, unsigned &transparencyWidth) |
| 178 | { |
| 179 | return reconcileValue(m_width, colorWidth, transparencyWidth); |
| 180 | } |
| 181 | |
| 182 | bool BMIHeader::reconcileHeight(unsigned &colorHeight, unsigned &transparencyHeight) |
| 183 | { |
| 184 | return reconcileValue(m_height, colorHeight, transparencyHeight); |
| 185 | } |
| 186 | |
| 187 | } |
| 188 | |
| 189 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |