Skip to content
Snippets Groups Projects
Commit 0a96edb3 authored by theazgra's avatar theazgra
Browse files

Basics in CZI parser.

parent 9f663ca0
Branches
No related tags found
No related merge requests found
Showing
with 258 additions and 32 deletions
LICENSE 0 → 100644
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
cmake_minimum_required(VERSION 3.0.0) cmake_minimum_required(VERSION 3.8.0)
project(czi-parser VERSION 0.1.0) project(czi-parser VERSION 0.1.0)
include(CTest) include(CTest)
...@@ -6,6 +6,15 @@ enable_testing() ...@@ -6,6 +6,15 @@ enable_testing()
add_executable(czi-parser main.cpp) add_executable(czi-parser main.cpp)
# set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS locale)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(czi-parser ${Boost_LIBRARIES})
endif()
set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack) include(CPack)
#include "binary_stream.h"
#include <iterator>
BinaryStream::BinaryStream(const std::string &file)
{
this->fileStream = std::ifstream(file, std::ios::binary);
this->fileStream.unsetf(std::ios::skipws);
assert(this->fileStream.is_open());
this->currentPosition = 0;
this->fileStream.seekg(std::ios::end);
this->fileSize = fileStream.tellg();
this->fileStream.seekg(std::ios::beg);
}
BinaryStream::~BinaryStream()
{
this->fileStream.close();
}
size_t BinaryStream::get_size() const
{
return this->fileSize;
}
void BinaryStream::move_to(const long position)
{
this->fileStream.seekg(position);
this->currentPosition = position;
}
void BinaryStream::move_to_beginning()
{
this->fileStream.seekg(std::ios::beg);
this->currentPosition = 0;
}
void BinaryStream::move_to_end()
{
this->fileStream.seekg(std::ios::end);
this->currentPosition = this->fileSize;
}
bool BinaryStream::can_read()
{
return (this->currentPosition < this->fileSize);
}
std::vector<byte> BinaryStream::consume_bytes(const long byteCount)
{
//TODO: Maybe this should be replace with faster reading.
// Check if iterator is set at current stream position, or
// we have to move iterator manually to match this->currentPosition
auto readIterator = std::istream_iterator<byte>(fileStream);
std::vector<byte> result;
result.resize(byteCount);
for (size_t i = 0; i < byteCount; i++)
{
result[i] = *readIterator++;
}
this->currentPosition += byteCount;
return result;
}
std::vector<byte> BinaryStream::consume_bytes_at(const long position, const long byteCount)
{
}
\ No newline at end of file
#pragma once
#include <fstream>
#include "custom_types.h"
class BinaryStream
{
private:
std::ifstream fileStream;
size_t fileSize;
long currentPosition;
public:
BinaryStream(const std::string &file);
~BinaryStream();
size_t get_size() const;
void move_to(const long position);
void move_to_beginning();
void move_to_end();
bool can_read();
std::vector<byte> consume_bytes(const long byteCount);
std::vector<byte> consume_bytes_at(const long position, const long byteCount);
};
#include "binary_stream.cpp"
\ No newline at end of file
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include <string> #include <string>
#include "custom_types.h" #include "custom_types.h"
#include <boost/locale.hpp>
short bytes_to_short(const std::vector<byte> &bytes, const uint fromIndex = 0) short bytes_to_short(const std::vector<byte> &bytes, const uint fromIndex = 0)
{ {
assert(bytes.size() >= 2); assert(bytes.size() >= 2);
...@@ -116,4 +118,12 @@ std::string bytes_to_raw_string(const std::vector<byte> &bytes, const uint fromI ...@@ -116,4 +118,12 @@ std::string bytes_to_raw_string(const std::vector<byte> &bytes, const uint fromI
std::vector<byte> stringBytes(bytes.begin() + fromIndex, bytes.begin() + fromIndex + byteCount); std::vector<byte> stringBytes(bytes.begin() + fromIndex, bytes.begin() + fromIndex + byteCount);
std::string result(reinterpret_cast<const char *>(stringBytes.data())); std::string result(reinterpret_cast<const char *>(stringBytes.data()));
return result; return result;
}
std::string utf8bytes_to_string(const std::vector<byte> &bytes, const uint fromIndex, const uint byteCount)
{
auto fromIt = bytes.begin() + fromIndex;
std::vector<byte> stringBytes(fromIt, fromIt + byteCount);
std::string result = boost::locale::conv::from_utf<char>((char *)stringBytes.data(), "UTF-8");
return result;
} }
\ No newline at end of file
#pragma once #pragma once
#include <vector>
#include <assert.h>
typedef unsigned char byte; typedef unsigned char byte;
typedef unsigned short ushort; typedef unsigned short ushort;
......
#include "czi_file.h"
CziFile::CziFile()
{
}
CziFile::~CziFile()
{
}
#pragma once
#include "czi_segments/FileHeader.h"
class CziFile
{
private:
FileHeader _fileHeader;
public:
CziFile();
~CziFile();
};
#include "czi_file.cpp"
\ No newline at end of file
#include "czi_parser.h"
#include <fstream>
#include "bit_converter.cpp"
CziFile parse_czi_file(std::string file)
{
/*
std::ifstream fileStream = std::ifstream("data/CZT-Stack-Anno.czi", std::ios::binary);
assert(fileStream.is_open());
// Don't read newlines in binary mode??
fileStream.unsetf(std::ios::skipws);
const int readCount = 100;
std::vector<byte> fileHeaderBytes;
fileHeaderBytes.resize(readCount);
auto fileIterator = std::istream_iterator<byte>(fileStream);
for (size_t i = 0; i < readCount; i++)
{
fileHeaderBytes[i] = *fileIterator++;
}
//fileStream.read(reinterpret_cast<char *>(&fileHeaderBytes), 32);
printf("Read %i bytes.\n", ((int)fileHeaderBytes.size()));
SegmentHeader fileSegmentHeader = {};
std::vector<byte> sidBytes(fileHeaderBytes.begin(), fileHeaderBytes.begin() + 16);
fileSegmentHeader.sId = utf8bytes_to_string(fileHeaderBytes, 0, 16); // bytes_to_raw_string(fileHeaderBytes, 0, 16);
fileSegmentHeader.allocatedSize = bytes_to_long(fileHeaderBytes, 16);
fileSegmentHeader.usedSize = bytes_to_long(fileHeaderBytes, 16 + 8);
//std::vector<byte> x_guid(fileHeaderBytes.begin() + SegmentHeaderSize + 16, fileHeaderBytes.begin() + SegmentHeaderSize + 16 + 16);
//std::string utf8text = "\x48\x65\x6C\x6C\x6F\x20\x57\x6F\x72\x6C\x64";
//std::string decoded = boost::locale::conv::from_utf<char>((char *)x_guid.data(), "utf-8");
std::string guid = utf8bytes_to_string(fileHeaderBytes, SegmentHeaderSize + 16, 16);
std::string guid2 = utf8bytes_to_string(fileHeaderBytes, SegmentHeaderSize + 32, 16);
assert(guid == guid2);
*/
return CziFile();
}
\ No newline at end of file
#pragma once
#include "czi_file.h"
CziFile parse_czi_file(std::string file);
#include "czi_parser.cpp"
\ No newline at end of file
#pragma once
#include "Version.h"
#include "SegmentHeader.h"
#include <vector>
struct FileHeader
{
// Standart segment header.
SegmentHeader header;
// Version of the file.
Version fileVersion;
// GUID of the master file.
std::vector<byte> masterFileGuid;
// GUID of this file.
std::vector<byte> fileGuid;
// Part number in multi-file scenario. 0 if single file.
int filePart;
// File position of the SubBlockDirectory segment
long subBlockDirectoryPosition;
// File position of the Metadata segment.
long metadataPosition;
// This flag indicates a currently inconsistent situation.
long attachmentDirectoryPosition;
// File position of the AttachmentDirectory segment.
bool updatePending;
};
\ No newline at end of file
#pragma once #pragma once
#include <string> #include <string>
#include "../custom_types.h"
const int SegmentHeaderSize = 32; const int SegmentHeaderSize = 32;
......
struct Version
{
int major;
int minor;
};
\ No newline at end of file
#include <fstream> #include "czi_parser.h"
#include <iterator> #include "binary_stream.h"
#include "bit_converter.cpp"
#include "czi_segments/SegmentHeader.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
std::ifstream fileStream = std::ifstream("data/CZT-Stack-Anno.czi", std::ios::binary); CziFile result = parse_czi_file("dede");
assert(fileStream.is_open());
// Don't read newlines in binary mode??
fileStream.unsetf(std::ios::skipws);
const int readCount = 100;
std::vector<byte> fileHeaderBytes;
fileHeaderBytes.resize(readCount);
auto fileIterator = std::istream_iterator<byte>(fileStream);
for (size_t i = 0; i < readCount; i++)
{
fileHeaderBytes[i] = *fileIterator++;
}
//fileStream.read(reinterpret_cast<char *>(&fileHeaderBytes), 32);
printf("Read %i bytes.\n", ((int)fileHeaderBytes.size()));
SegmentHeader fileSegmentHeader = {};
fileSegmentHeader.sId = bytes_to_raw_string(fileHeaderBytes, 0, 16);
fileSegmentHeader.allocatedSize = bytes_to_long(fileHeaderBytes, 16);
fileSegmentHeader.usedSize = bytes_to_long(fileHeaderBytes, 16 + 8);
std::string guid = bytes_to_raw_string(fileHeaderBytes, SegmentHeaderSize + 16, 16);
std::string guid2 = bytes_to_raw_string(fileHeaderBytes, SegmentHeaderSize + 32, 16);
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment