diff --git a/czi-format/czi-parser/czi_file.cpp b/czi-format/czi-parser/czi_file.cpp index ad0f0633938f85b9966a27c55fe81cccb32f7f2b..44cf40b94b4e9816255d95b12e30d13fb2f001f4 100644 --- a/czi-format/czi-parser/czi_file.cpp +++ b/czi-format/czi-parser/czi_file.cpp @@ -322,6 +322,30 @@ ImageMatrix CziFile::get_image(const uint subblockId) return image; } +void CziFile::differences_between_next(const std::string baseName) +{ + if (subBlockDirectory.entries.size() <= 1) + { + printf("There aren't 2 different images in this CZI file.\n"); + return; + } + + std::string fName; + for (size_t i = 1; i < subBlockDirectory.entries.size(); i++) + { + fName = baseName + "diff_" + std::to_string(i - 1) + "_" + std::to_string(i) + ".ppm"; + + ImageMatrix ref = get_image(i - 1); + ImageMatrix next = get_image(i); + + ImageMatrix diff_ref_next = ref.create_difference_matrix(next); + + diff_ref_next.save_as_ppm(fName.c_str()); + + printf("Saved %s\n", fName.c_str()); + } +} + // void CziFile::extract_images(const std::string &baseName) const // { // BinaryFileStream cziStream(fileName); diff --git a/czi-format/czi-parser/czi_file.h b/czi-format/czi-parser/czi_file.h index 09518312be92194eeb6bd42559cc28985ed58849..81537e4b906e007d9faaaba14f532e238aedae33 100644 --- a/czi-format/czi-parser/czi_file.h +++ b/czi-format/czi-parser/czi_file.h @@ -31,6 +31,8 @@ public: ImageMatrix get_image(const uint subblockId); void dump_image_data(const std::string &baseName) const; //void extract_images(const std::string &baseName) const; + // Save differences between next images, so 0x1;1x2;... + void differences_between_next(const std::string baseName); }; #include "czi_file.cpp" \ No newline at end of file diff --git a/czi-format/czi-parser/image/image_matrix.cpp b/czi-format/czi-parser/image/image_matrix.cpp index db630c4ac6d94f6fe8cf7aa66d5e434ef78118c5..992fd732cdc4bbca2e93329679a30abf757d577f 100644 --- a/czi-format/czi-parser/image/image_matrix.cpp +++ b/czi-format/czi-parser/image/image_matrix.cpp @@ -114,8 +114,8 @@ ImageMatrix::ImageMatrix(const uint width, const uint height, const PixelType pi { always_assert(width > 0 && height > 0 && pixelType != PixelType_None); - this->rowCount = rowCount; - this->colCount = colCount; + this->rowCount = height; + this->colCount = width; this->pixelType = pixelType; // Allocate memory. @@ -142,10 +142,13 @@ ImageMatrix::ImageMatrix(const uint width, const uint height, const PixelType pi ImageMatrix::~ImageMatrix() { + //TODO: This is bonkers bad. Let's use either std::shared_ptr or std::unique_ptr... printf("ImageMatrix destructor...\n"); for (size_t i = 0; i < data.size(); i++) { - if (data[i]) + printf("Trying to delete data[%lu]\n", i); + + if (data[i] && !data[i]->isEmpty) { delete data[i]; } @@ -190,11 +193,10 @@ bool ImageMatrix::operator!=(const ImageMatrix &other) const return (!equals(other)); } -void ImageMatrix::save_as_ppm(std::string baseFileName) const +void ImageMatrix::save_as_ppm(const char *fName) const { always_assert((this->pixelType == PixelType_Gray8 || this->pixelType == PixelType_Bgr24) && "Currently only those pixel types are supported."); - std::string fName = baseFileName + ".ppm"; if (this->pixelType == PixelType_Gray8) { auto bwImage = cimg_wrapper::create_grayscale_image(colCount, rowCount); @@ -206,7 +208,7 @@ void ImageMatrix::save_as_ppm(std::string baseFileName) const cimg_wrapper::set_grayscale_pixel(bwImage, row, col, ((Gray8Pixel *)data[index++])->value); } } - bwImage.save_pnm(fName.c_str()); + bwImage.save_pnm(fName); } else if (this->pixelType == PixelType_Bgr24) { @@ -221,7 +223,7 @@ void ImageMatrix::save_as_ppm(std::string baseFileName) const cimg_wrapper::set_color_pixel(colorImage, row, col, *px); } } - colorImage.save_pnm(fName.c_str()); + colorImage.save_pnm(fName); } } @@ -244,7 +246,7 @@ ImageMatrix ImageMatrix::create_difference_matrix(const ImageMatrix &other) ulong diffCounter = 0; for (size_t i = 0; i < size; i++) { - if (!(this->data[i]->equals(other.get(size)))) + if (!(this->data[i]->equals(other.get(i)))) { diffCounter++; diff.set(i, whitePixel); diff --git a/czi-format/czi-parser/image/image_matrix.h b/czi-format/czi-parser/image/image_matrix.h index 865fec78cc57ab1b2821f20fe514bd9bc588c058..4b300e7cd4e186fa17d69057376ddeb860702b9b 100644 --- a/czi-format/czi-parser/image/image_matrix.h +++ b/czi-format/czi-parser/image/image_matrix.h @@ -6,56 +6,57 @@ class ImageMatrix { - private: - // Number of rows, height of an image. - uint rowCount; - // Number of columns, width of an image. - uint colCount; - // Image data. - std::vector<BasePixel *> data; - // Type of an image pixel. - PixelType pixelType = PixelType_None; +private: + // Number of rows, height of an image. + uint rowCount; + // Number of columns, width of an image. + uint colCount; + //TODO: Bad.Bad.Bad. Replace with smart pointer. + // Image data. + std::vector<BasePixel *> data; + // Type of an image pixel. + PixelType pixelType = PixelType_None; - // Check equality of two ImageMatrices. - bool equals(const ImageMatrix &other) const; - // Returns function to parse bytes into corresponding Pixel structure based on `PixelType` - std::function<BasePixel *(const std::vector<byte> &, ulong &)> get_pixel_parse_function(const PixelType pixelType); - // Get number of bytes needed to encode specific `PixelType` - int get_bytes_per_pixel_type(const PixelType &pt) const; - // Parse bytes to this matrix. - void parse_bytes_to_image(const std::vector<byte> &bytes, const PixelType &pt); + // Check equality of two ImageMatrices. + bool equals(const ImageMatrix &other) const; + // Returns function to parse bytes into corresponding Pixel structure based on `PixelType` + std::function<BasePixel *(const std::vector<byte> &, ulong &)> get_pixel_parse_function(const PixelType pixelType); + // Get number of bytes needed to encode specific `PixelType` + int get_bytes_per_pixel_type(const PixelType &pt) const; + // Parse bytes to this matrix. + void parse_bytes_to_image(const std::vector<byte> &bytes, const PixelType &pt); - public: - // Default constructor for empty image. - ImageMatrix(); - // Constuctor creating an empty image with allocated space. - ImageMatrix(const uint width, const uint height, const PixelType pixelType); - // Constuctor creating image matrix from image data. - ImageMatrix(const uint width, const uint height, const PixelType pixelType, const std::vector<byte> &imageBytes); - ~ImageMatrix(); - // Get number of rows. - uint rows() const; - // Get number of cols. - uint cols() const; - // Get pixel type of an image. - PixelType pixel_type() const; - // Set pixel at row and col. - inline void set(const uint &row, const uint &col, BasePixel *px); - // Set pixel at raw index. - inline void set(const ulong index, BasePixel *px); - // Get pixel at row and col. - inline const BasePixel *get(const uint &row, const uint &col) const; - // Get pixel at raw index. - inline const BasePixel *get(const ulong index) const; - // Equal operator. - bool operator==(const ImageMatrix &other) const; - // Not equal operator. - bool operator!=(const ImageMatrix &other) const; - // Save Matrix as image, supports pixels: `Bgr24`, `Gray8` - void save_as_ppm(std::string baseFileName) const; - // Set all pixels to one value of `px`. - void fill(BasePixel *px); - ImageMatrix create_difference_matrix(const ImageMatrix &other); +public: + // Default constructor for empty image. + ImageMatrix(); + // Constuctor creating an empty image with allocated space. + ImageMatrix(const uint width, const uint height, const PixelType pixelType); + // Constuctor creating image matrix from image data. + ImageMatrix(const uint width, const uint height, const PixelType pixelType, const std::vector<byte> &imageBytes); + ~ImageMatrix(); + // Get number of rows. + uint rows() const; + // Get number of cols. + uint cols() const; + // Get pixel type of an image. + PixelType pixel_type() const; + // Set pixel at row and col. + inline void set(const uint &row, const uint &col, BasePixel *px); + // Set pixel at raw index. + inline void set(const ulong index, BasePixel *px); + // Get pixel at row and col. + inline const BasePixel *get(const uint &row, const uint &col) const; + // Get pixel at raw index. + inline const BasePixel *get(const ulong index) const; + // Equal operator. + bool operator==(const ImageMatrix &other) const; + // Not equal operator. + bool operator!=(const ImageMatrix &other) const; + // Save Matrix as image, supports pixels: `Bgr24`, `Gray8` + void save_as_ppm(const char *fName) const; + // Set all pixels to one value of `px`. + void fill(BasePixel *px); + ImageMatrix create_difference_matrix(const ImageMatrix &other); }; #include "image_matrix.cpp" \ No newline at end of file diff --git a/czi-format/czi-parser/main.cpp b/czi-format/czi-parser/main.cpp index 616eda6170586bd64f1b4a408e691a86a42ac85b..77e7eb7e8690926145ac780aa978bd2ae9b396b5 100644 --- a/czi-format/czi-parser/main.cpp +++ b/czi-format/czi-parser/main.cpp @@ -3,21 +3,17 @@ int main(int argc, char **argv) { - std::string cziFile; - // #if DEBUG - // printf("***DEBUG MODE***\n"); - // if (argc == 1) - // { - // argv[1] = (char *)"/home/mor0146/gitlab/data_project/czi-format/data/MultiResolution-Mosaic.czi"; - // argv[2] = (char *)"--dump-image-data"; - // argv[3] = (char *)"imgdump/"; - // } - // argc = 4; - // cziFile = argv[1]; - // #else - - // #endif - cziFile = (argc > 1) ? argv[1] : "/home/mor0146/gitlab/data_project/czi-format/data/CZT-Stack-Anno.czi"; //"/home/mor0146/gitlab/data_project/czi-format/data/m2/exampleSingleChannel.czi"; + + std::string cziFile = (argc > 1) ? argv[1] : "/home/mor0146/gitlab/data_project/czi-format/data/CZT-Stack-Anno.czi"; //"/home/mor0146/gitlab/data_project/czi-format/data/m2/exampleSingleChannel.czi"; + if (cziFile == "-v" || cziFile == "--version") + { +#if DEBUG + printf("Running `Debug` version\n."); +#else + printf("Running `Release` version\n."); +#endif + return 0; + } always_assert(is_file(cziFile)); @@ -28,6 +24,7 @@ int main(int argc, char **argv) bool reportAll = method == "--report-verbose"; bool dumpRawImageData = method == "--dump-raw-image-data"; bool dumpImageData = method == "--dump-image-data"; + bool nextImageDiff = method == "--diff-next"; auto name = get_filename_without_extension(cziFile); auto x = get_files_in_parent_directory(cziFile, true); @@ -45,10 +42,15 @@ int main(int argc, char **argv) else if (dumpImageData) { auto imgMat = parseResult.get_image(0); - imgMat.save_as_ppm("matrix"); + imgMat.save_as_ppm("matrix.ppm"); //TODO: Re-Enable it. //parseResult.extract_images(dumpName); } + else if (nextImageDiff) + { + always_assert(dumpName != ""); + parseResult.differences_between_next(dumpName); + } printf("Finished.\n");