Skip to content
Snippets Groups Projects
Commit 941fcd45 authored by theazgra's avatar theazgra
Browse files

Mapping of signed types to unsigned.

parent bf8b3c0f
No related branches found
No related tags found
No related merge requests found
Showing
with 146 additions and 6 deletions
...@@ -24,7 +24,7 @@ typedef unsigned long ulong; ...@@ -24,7 +24,7 @@ typedef unsigned long ulong;
constexpr uint UINT_MAX = std::numeric_limits<uint>::max(); constexpr uint UINT_MAX = std::numeric_limits<uint>::max();
constexpr ulong ULONG_MAX = std::numeric_limits<ulong>::max(); constexpr ulong ULONG_MAX = std::numeric_limits<ulong>::max();
constexpr short SHORT_MAX = std::numeric_limits<short>::max(); constexpr short SHORT_MAX = std::numeric_limits<short>::max();
constexpr short USHORT_MAX = std::numeric_limits<ushort>::max(); constexpr ushort USHORT_MAX = std::numeric_limits<ushort>::max();
#define TagType std::string, std::string #define TagType std::string, std::string
typedef std::pair<TagType> Tag; typedef std::pair<TagType> Tag;
......
...@@ -522,9 +522,14 @@ void CziFile::frames_difference() const ...@@ -522,9 +522,14 @@ void CziFile::frames_difference() const
auto framesByChannels = get_subblocks_grouped_by_channels(); auto framesByChannels = get_subblocks_grouped_by_channels();
std::pair<uint, uint> canBeMappedCounts = std::make_pair(0, 0);
for (const std::pair<uint, std::vector<uint>> &channelGroup : framesByChannels) for (const std::pair<uint, std::vector<uint>> &channelGroup : framesByChannels)
{ {
printf("Starting channel %u\n", channelGroup.first); printf("Starting channel %u\n", channelGroup.first);
//#pragma omp parallel for
for (size_t i = 1; i < channelGroup.second.size(); i++) for (size_t i = 1; i < channelGroup.second.size(); i++)
{ {
// This assertion will fail if pixel type isn't Gray16 // This assertion will fail if pixel type isn't Gray16
...@@ -541,17 +546,33 @@ void CziFile::frames_difference() const ...@@ -541,17 +546,33 @@ void CziFile::frames_difference() const
std::vector<int> differenceArray = vecUtil::diff_vectors<ushort, int>(prevFrameValues, currentFrameValues); std::vector<int> differenceArray = vecUtil::diff_vectors<ushort, int>(prevFrameValues, currentFrameValues);
int min = vecUtil::find_min(differenceArray);
int max = vecUtil::find_max(differenceArray);
long maxMappedValue = (min < 0) ? (abs(min) + max) : (max);
bool canBeMappedToUShort = maxMappedValue < USHORT_MAX;
#pragma omp critical
{
if (canBeMappedToUShort)
canBeMappedCounts.first++;
else
canBeMappedCounts.second++;
}
ByteArray intBytes = int_array_to_bytes(differenceArray); ByteArray intBytes = int_array_to_bytes(differenceArray);
auto frameCompResult = test_compression_method(currentFrameData, CompressionMethod_BZIP2, 6); auto frameCompResult = test_compression_method(currentFrameData, CompressionMethod_BZIP2, 6);
auto diffCompResult = test_compression_method(intBytes, CompressionMethod_BZIP2, 6); auto diffCompResult = test_compression_method(intBytes, CompressionMethod_BZIP2, 6);
float diffSizeDif = ((float)diffCompResult.compressedSize / (float)frameCompResult.compressedSize) * 100.0f; float diffSizeDif = ((float)diffCompResult.compressedSize / (float)frameCompResult.compressedSize);
printf("Frame [%u | %u]: Size %7lu Compression ratio: %7f\n", printf("========================\nFrame [%u vs %u]\n\tCR frame: %10.5f\n\tCR diff: %10.5f\n\tSizeDiff: %10.5f\n\tMin: %10i\n\tMax: %10i\n\tMax mapped value: %10li\n\tCan be mapped to ushort: %s\n",
channelGroup.second[i - 1], channelGroup.second[i], frameCompResult.compressedSize, frameCompResult.compressionRatio); channelGroup.second[i - 1], channelGroup.second[i], frameCompResult.compressionRatio, diffCompResult.compressionRatio, diffSizeDif,
min, max, maxMappedValue, canBeMappedToUShort ? "YES" : "NO");
printf(" Diff: Size %7lu Compression ratio: %7f\n Size: %f\n\n", diffCompResult.compressedSize, diffCompResult.compressionRatio, diffSizeDif); //printf("Max value: %7i Min value: %7i #of values: %7li\n", max, min, rangeSize);
} }
} }
printf("%u can be mapped to short difference value.\n%u can not be mapped.\n", canBeMappedCounts.first, canBeMappedCounts.second);
} }
\ No newline at end of file
#include "czi_parser.h" #include "czi_parser.h"
#include "compression/benchmark.h" #include "compression/benchmark.h"
#include "utilities/args.hxx" #include "utilities/args.hxx"
#include "utilities/type_mapper.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
args::ArgumentParser argParser("CZI file tools", "Optional arguments are in `[]` necessary in `<>`."); args::ArgumentParser argParser("CZI file tools", "Optional arguments are in `[]` necessary in `<>`.");
......
#pragma once
#include "../custom_types.h"
#include "vector_utilities.h"
// Provide mapping utilites from signed types to big enaugh unsigned types.
template <typename SourceType, typename TargetType>
class TypeMapper
{
private:
public:
// Map `SourceType` vector to `TargetType` vector. Saving mapping point as first element.
std::vector<TargetType> map(std::vector<SourceType> &data)
{
auto limits = vecUtil::find_min_max(data);
SourceType min = limits.first;
SourceType max = limits.second;
TargetType targetMaxValue = std::numeric_limits<TargetType>::max();
always_assert(max < targetMaxValue);
always_assert((abs(min) + max) < targetMaxValue);
TargetType mappingPoint = max;
std::vector<TargetType> result;
result.resize(data.size() + 1);
// Save mapping point as first value.
result[0] = mappingPoint;
for (size_t i = 0; i < data.size(); i++)
{
if (data[i] >= 0)
{
result[i + 1] = (TargetType)data[i];
}
else
{
result[i + 1] = mappingPoint + abs(data[i]);
}
}
return result;
}
// Map back `TargetType` vector to `SourceType` vector.
std::vector<SourceType> map_back(std::vector<TargetType> &data)
{
std::vector<SourceType> result;
result.resize(data.size() - 1);
// Retrieve mapping point.
TargetType mappingPoint = data[0];
for (size_t i = 1; i < data.size(); i++)
{
if (data[i] <= mappingPoint)
{
// Positive values
result[i - 1] = data[i];
}
else
{
// Negative values
result[i - 1] = -(data[i] - mappingPoint);
}
}
return result;
}
};
\ No newline at end of file
#pragma once #pragma once
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <limits>
namespace vecUtil namespace vecUtil
{ {
...@@ -59,4 +60,53 @@ std::vector<DiffType> diff_vectors(const std::vector<T> &a, const std::vector<T> ...@@ -59,4 +60,53 @@ std::vector<DiffType> diff_vectors(const std::vector<T> &a, const std::vector<T>
return result; return result;
} }
template <typename T>
T find_max(const std::vector<T> &data)
{
T max = std::numeric_limits<T>::min();
for (size_t i = 0; i < data.size(); i++)
{
if (data[i] > max)
{
max = data[i];
}
}
return max;
}
template <typename T>
T find_min(const std::vector<T> &data)
{
T min = std::numeric_limits<T>::max();
for (size_t i = 0; i < data.size(); i++)
{
if (data[i] < min)
{
min = data[i];
}
}
return min;
}
template <typename T>
std::pair<T, T> find_min_max(const std::vector<T> &data)
{
T min = std::numeric_limits<T>::max();
T max = std::numeric_limits<T>::min();
for (size_t i = 0; i < data.size(); i++)
{
if (data[i] < min)
{
min = data[i];
}
if (data[i] > max)
{
max = data[i];
}
}
return std::make_pair(min, max);
}
}; // namespace vecUtil }; // namespace vecUtil
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment