diff --git a/.gitignore b/.gitignore index f1351cf6f447c913fea77ca644b4d88e77812db9..88ffee06a5dcbca9237e5f97148d14f1d1756524 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ czi_example_data/ *.fdb_latexmk .vs/ [Oo]bj +build/ +.vscode/ diff --git a/czi-format/inspector/czi-parser/CMakeLists.txt b/czi-format/inspector/czi-parser/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..2fd7033dd4c92f66c4e5c680bf0e5962798d7c85 --- /dev/null +++ b/czi-format/inspector/czi-parser/CMakeLists.txt @@ -0,0 +1,11 @@ +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) diff --git a/czi-format/inspector/czi-parser/bit_converter.cpp b/czi-format/inspector/czi-parser/bit_converter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90ce7770b940a1dc93d28fff49c186da43ae0218 --- /dev/null +++ b/czi-format/inspector/czi-parser/bit_converter.cpp @@ -0,0 +1,119 @@ +/** + * 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 diff --git a/czi-format/inspector/czi-parser/custom_types.h b/czi-format/inspector/czi-parser/custom_types.h new file mode 100644 index 0000000000000000000000000000000000000000..f96a3fcc3ccf996366c33006c9d6a30c57ef7056 --- /dev/null +++ b/czi-format/inspector/czi-parser/custom_types.h @@ -0,0 +1,6 @@ +#pragma once + +typedef unsigned char byte; +typedef unsigned short ushort; +typedef unsigned int uint; +typedef unsigned long ulong; \ No newline at end of file diff --git a/czi-format/inspector/czi-parser/czi_segments/SegmentHeader.h b/czi-format/inspector/czi-parser/czi_segments/SegmentHeader.h new file mode 100644 index 0000000000000000000000000000000000000000..4e17d54a03166755271d82109a375b3677d01a83 --- /dev/null +++ b/czi-format/inspector/czi-parser/czi_segments/SegmentHeader.h @@ -0,0 +1,11 @@ +#pragma once +#include <string> + +const int SegmentHeaderSize = 32; + +struct SegmentHeader +{ + std::string sId; + long allocatedSize; + long usedSize; +}; \ No newline at end of file diff --git a/czi-format/inspector/czi-parser/main.cpp b/czi-format/inspector/czi-parser/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10d88146a270981ed7fdaa69be5fe70132ecd1e3 --- /dev/null +++ b/czi-format/inspector/czi-parser/main.cpp @@ -0,0 +1,35 @@ +#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; +}