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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/* -*- tab-width:4; c-basic-offset:4 indent-tabs-mode:t -*- */
/*
* libopenraw - rafcontainer.cpp
*
* Copyright (C) 2011-2016 Hubert Figuiere
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <string.h>
#include <memory>
#include "rafcontainer.hpp"
#include "raffile.hpp"
#include "jfifcontainer.hpp"
#include "ifdfilecontainer.hpp"
#include "rafmetacontainer.hpp"
#include "io/stream.hpp"
#include "io/streamclone.hpp"
namespace OpenRaw {
namespace Internals {
RafContainer::RafContainer(const IO::Stream::Ptr &_file)
: RawContainer(_file, 0)
, m_read(false)
, m_version(0)
, m_jpegPreview(nullptr)
, m_cfaContainer(nullptr)
, m_metaContainer(nullptr)
{
memset((void*)&m_offsetDirectory, 0, sizeof(m_offsetDirectory));
}
RafContainer::~RafContainer()
{
delete m_jpegPreview;
delete m_cfaContainer;
delete m_metaContainer;
}
const std::string & RafContainer::getModel()
{
if(!m_read) {
_readHeader();
}
return m_model;
}
IfdFileContainer * RafContainer::getCfaContainer()
{
if(!m_cfaContainer) {
if(!m_read) {
_readHeader();
}
if(m_offsetDirectory.cfaOffset && m_offsetDirectory.cfaLength) {
m_cfaContainer = new IfdFileContainer(
std::make_shared<IO::StreamClone>(
m_file, m_offsetDirectory.cfaOffset), 0);
}
}
return m_cfaContainer;
}
JfifContainer * RafContainer::getJpegPreview()
{
if(!m_jpegPreview) {
if(!m_read) {
_readHeader();
}
if(m_offsetDirectory.jpegOffset && m_offsetDirectory.jpegLength) {
m_jpegPreview = new JfifContainer(
std::make_shared<IO::StreamClone>(
m_file, m_offsetDirectory.jpegOffset), 0);
}
}
return m_jpegPreview;
}
RafMetaContainer * RafContainer::getMetaContainer()
{
if(!m_metaContainer) {
if(!m_read) {
_readHeader();
}
if(m_offsetDirectory.metaOffset && m_offsetDirectory.metaLength) {
m_metaContainer = new RafMetaContainer(
std::make_shared<IO::StreamClone>(
m_file, m_offsetDirectory.metaOffset));
}
}
return m_metaContainer;
}
bool RafContainer::_readHeader()
{
char magic[29];
char model[33];
magic[28] = 0;
model[32] = 0;
m_read = true;
m_file->read(magic, 28);
if(strncmp(magic, RAF_MAGIC, RAF_MAGIC_LEN) != 0) {
// not a RAF file
return false;
}
setEndian(ENDIAN_BIG);
m_file->read(model, 32);
m_model = model;
auto result = readUInt32(m_file);
if (result.empty()) {
return false;
}
m_version = result.unwrap();
m_file->seek(20, SEEK_CUR);
result = readUInt32(m_file);
if (result.empty()) {
return false;
}
m_offsetDirectory.jpegOffset = result.unwrap();
result = readUInt32(m_file);
if (result.empty()) {
return false;
}
m_offsetDirectory.jpegLength = result.unwrap();
result = readUInt32(m_file);
if (result.empty()) {
return false;
}
m_offsetDirectory.metaOffset = result.unwrap();
result = readUInt32(m_file);
if (result.empty()) {
return false;
}
m_offsetDirectory.metaLength = result.unwrap();
result = readUInt32(m_file);
if (result.empty()) {
return false;
}
m_offsetDirectory.cfaOffset = result.unwrap();
result = readUInt32(m_file);
if (result.empty()) {
return false;
}
m_offsetDirectory.cfaLength = result.unwrap();
return true;
}
}
}
|