Skip to content
Snippets Groups Projects
Commit 9f663ca0 authored by theazgra's avatar theazgra
Browse files

Start of CziInspector in C++.

parent df6701f9
No related branches found
No related tags found
No related merge requests found
......@@ -17,3 +17,5 @@ czi_example_data/
*.fdb_latexmk
.vs/
[Oo]bj
build/
.vscode/
cmake_minimum_required(VERSION 3.0.0)
project(czi-parser VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(czi-parser main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
/**
* This implementation is inspired by BitConverter from .NET
* https://referencesource.microsoft.com/#mscorlib/system/bitconverter.cs
*/
#include <assert.h>
#include <vector>
#include <endian.h>
#include <string>
#include "custom_types.h"
short bytes_to_short(const std::vector<byte> &bytes, const uint fromIndex = 0)
{
assert(bytes.size() >= 2);
assert(fromIndex <= bytes.size() - 2);
// Little endian
auto bit = &bytes[fromIndex];
if (fromIndex % 2 == 0)
{
return (*((short *)bit));
}
else
{
return (short)((*bit) | (*(bit + 1) << 8));
}
}
int bytes_to_int(const std::vector<byte> &bytes, const uint fromIndex = 0)
{
assert(bytes.size() >= 4);
assert(fromIndex <= bytes.size() - 4);
// Little endian
auto bit = &bytes[fromIndex];
if (fromIndex % 4 == 0)
{
return (*((int *)bit));
}
else
{
return (*bit | (*(bit + 1) << 8) | (*(bit + 2) << 16) | (*(bit + 3) << 24));
}
}
int bytes_to_long(const std::vector<byte> &bytes, const uint fromIndex = 0)
{
assert(bytes.size() >= 8);
assert(fromIndex <= bytes.size() - 8);
// Little endian
auto bit = &bytes[fromIndex];
if (fromIndex % 8 == 0)
{
return (*((long *)bit));
}
else
{
int i1 = (*bit) | (*(bit + 1) << 8) | (*(bit + 2) << 16) | (*(bit + 3) << 24);
int i2 = (*(bit + 4)) | (*(bit + 5) << 8) | (*(bit + 6) << 16) | (*(bit + 7) << 24);
return ((uint)i1 | ((long)i2 << 32));
}
}
ushort bytes_to_ushort(const std::vector<byte> &bytes, const uint fromIndex)
{
return ((ushort)bytes_to_short(bytes, fromIndex));
}
uint bytes_to_uint(const std::vector<byte> &bytes, const uint fromIndex)
{
return ((uint)bytes_to_int(bytes, fromIndex));
}
ulong bytes_to_ulong(const std::vector<byte> &bytes, const uint fromIndex)
{
return ((ulong)bytes_to_long(bytes, fromIndex));
}
char get_hex_value(int x)
{
assert(x >= 0 && x < 16);
return (x < 10) ? ((char)(x + '0')) : ((char)((x - 10) + 'A'));
}
std::string bytes_to_string(const std::vector<byte> &bytes, const uint fromIndex, const uint byteCount)
{
assert((fromIndex >= 0) && (fromIndex <= bytes.size() - byteCount));
if (bytes.size() == 0)
return std::string();
int stringLen = byteCount * 3;
std::vector<char> characters;
characters.resize(stringLen);
int index = fromIndex;
for (int i = 0; i < stringLen; i++)
{
byte b = bytes[index++];
characters[i] = get_hex_value(b / 16);
characters[i + 1] = get_hex_value(b % 16);
characters[i + 2] = '-';
}
return std::string(characters.begin(), characters.end() - 1);
}
std::string bytes_to_raw_string(const std::vector<byte> &bytes, const uint fromIndex, const uint byteCount)
{
assert((fromIndex >= 0) && (fromIndex <= bytes.size() - byteCount));
if (bytes.size() == 0)
return std::string();
std::vector<byte> stringBytes(bytes.begin() + fromIndex, bytes.begin() + fromIndex + byteCount);
std::string result(reinterpret_cast<const char *>(stringBytes.data()));
return result;
}
\ No newline at end of file
#pragma once
typedef unsigned char byte;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
\ No newline at end of file
#pragma once
#include <string>
const int SegmentHeaderSize = 32;
struct SegmentHeader
{
std::string sId;
long allocatedSize;
long usedSize;
};
\ No newline at end of file
#include <fstream>
#include <iterator>
#include "bit_converter.cpp"
#include "czi_segments/SegmentHeader.h"
int main(int argc, char **argv)
{
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 = {};
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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment