Welcome to mirror list, hosted at ThFree Co, Russian Federation.

abc_archive.cc « intern « alembic « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bb527c23b2076c87ffabf242286634668b74677a (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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software  Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2016 Kévin Dietrich.
 * All rights reserved.
 */

/** \file
 * \ingroup balembic
 */

#include "abc_archive.h"
extern "C" {
#include "BKE_blender_version.h"
#include "BKE_main.h"

#include "BLI_path_util.h"
#include "BLI_string.h"
}

#ifdef WIN32
#  include "utfconv.h"
#endif

#include <fstream>

using Alembic::Abc::ErrorHandler;
using Alembic::Abc::Exception;
using Alembic::Abc::IArchive;
using Alembic::Abc::kWrapExisting;
using Alembic::Abc::OArchive;

static IArchive open_archive(const std::string &filename,
                             const std::vector<std::istream *> &input_streams,
                             bool &is_hdf5)
{
  is_hdf5 = false;

  try {
    Alembic::AbcCoreOgawa::ReadArchive archive_reader(input_streams);

    return IArchive(archive_reader(filename), kWrapExisting, ErrorHandler::kThrowPolicy);
  }
  catch (const Exception &e) {
    std::cerr << e.what() << '\n';

#ifdef WITH_ALEMBIC_HDF5
    try {
      is_hdf5 = true;
      Alembic::AbcCoreAbstract::ReadArraySampleCachePtr cache_ptr;

      return IArchive(Alembic::AbcCoreHDF5::ReadArchive(),
                      filename.c_str(),
                      ErrorHandler::kThrowPolicy,
                      cache_ptr);
    }
    catch (const Exception &) {
      std::cerr << e.what() << '\n';
      return IArchive();
    }
#else
    /* Inspect the file to see whether it's really a HDF5 file. */
    char header[4]; /* char(0x89) + "HDF" */
    std::ifstream the_file(filename.c_str(), std::ios::in | std::ios::binary);
    if (!the_file) {
      std::cerr << "Unable to open " << filename << std::endl;
    }
    else if (!the_file.read(header, sizeof(header))) {
      std::cerr << "Unable to read from " << filename << std::endl;
    }
    else if (strncmp(header + 1, "HDF", 3)) {
      std::cerr << filename << " has an unknown file format, unable to read." << std::endl;
    }
    else {
      is_hdf5 = true;
      std::cerr << filename << " is in the obsolete HDF5 format, unable to read." << std::endl;
    }

    if (the_file.is_open()) {
      the_file.close();
    }

    return IArchive();
#endif
  }

  return IArchive();
}

ArchiveReader::ArchiveReader(struct Main *bmain, const char *filename)
{
  char abs_filename[FILE_MAX];
  BLI_strncpy(abs_filename, filename, FILE_MAX);
  BLI_path_abs(abs_filename, BKE_main_blendfile_path(bmain));

#ifdef WIN32
  UTF16_ENCODE(abs_filename);
  std::wstring wstr(abs_filename_16);
  m_infile.open(wstr.c_str(), std::ios::in | std::ios::binary);
  UTF16_UN_ENCODE(abs_filename);
#else
  m_infile.open(abs_filename, std::ios::in | std::ios::binary);
#endif

  m_streams.push_back(&m_infile);

  m_archive = open_archive(abs_filename, m_streams, m_is_hdf5);

  /* We can't open an HDF5 file from a stream, so close it. */
  if (m_is_hdf5) {
    m_infile.close();
    m_streams.clear();
  }
}

bool ArchiveReader::is_hdf5() const
{
  return m_is_hdf5;
}

bool ArchiveReader::valid() const
{
  return m_archive.valid();
}

Alembic::Abc::IObject ArchiveReader::getTop()
{
  return m_archive.getTop();
}

/* ************************************************************************** */

/* This kinda duplicates CreateArchiveWithInfo, but Alembic does not seem to
 * have a version supporting streams. */
static OArchive create_archive(std::ostream *ostream,
                               const std::string &filename,
                               const std::string &scene_name,
                               Alembic::Abc::MetaData &md,
                               bool ogawa)
{
  md.set(Alembic::Abc::kApplicationNameKey, "Blender");
  md.set(Alembic::Abc::kUserDescriptionKey, scene_name);
  md.set("blender_version", versionstr);

  time_t raw_time;
  time(&raw_time);
  char buffer[128];

#if defined _WIN32 || defined _WIN64
  ctime_s(buffer, 128, &raw_time);
#else
  ctime_r(&raw_time, buffer);
#endif

  const std::size_t buffer_len = strlen(buffer);
  if (buffer_len > 0 && buffer[buffer_len - 1] == '\n') {
    buffer[buffer_len - 1] = '\0';
  }

  md.set(Alembic::Abc::kDateWrittenKey, buffer);

  ErrorHandler::Policy policy = ErrorHandler::kThrowPolicy;

#ifdef WITH_ALEMBIC_HDF5
  if (!ogawa) {
    return OArchive(Alembic::AbcCoreHDF5::WriteArchive(), filename, md, policy);
  }
#else
  static_cast<void>(filename);
  static_cast<void>(ogawa);
#endif

  Alembic::AbcCoreOgawa::WriteArchive archive_writer;
  return OArchive(archive_writer(ostream, md), kWrapExisting, policy);
}

ArchiveWriter::ArchiveWriter(const char *filename,
                             const char *scene,
                             bool do_ogawa,
                             Alembic::Abc::MetaData &md)
{
  /* Use stream to support unicode character paths on Windows. */
  if (do_ogawa) {
#ifdef WIN32
    UTF16_ENCODE(filename);
    std::wstring wstr(filename_16);
    m_outfile.open(wstr.c_str(), std::ios::out | std::ios::binary);
    UTF16_UN_ENCODE(filename);
#else
    m_outfile.open(filename, std::ios::out | std::ios::binary);
#endif
  }

  m_archive = create_archive(&m_outfile, filename, scene, md, do_ogawa);
}

OArchive &ArchiveWriter::archive()
{
  return m_archive;
}