LCOV - code coverage report
Current view: top level - autotest/cpp - test_gdal_gtiff.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 107 108 99.1 %
Date: 2024-05-14 13:00:50 Functions: 27 27 100.0 %

          Line data    Source code
       1             : ///////////////////////////////////////////////////////////////////////////////
       2             : //
       3             : // Project:  C++ Test Suite for GDAL/OGR
       4             : // Purpose:  Test read/write functionality for GeoTIFF format.
       5             : //           Ported from gcore/tiff_read.py, gcore/tiff_write.py.
       6             : // Author:   Mateusz Loskot <mateusz@loskot.net>
       7             : //
       8             : ///////////////////////////////////////////////////////////////////////////////
       9             : // Copyright (c) 2006, Mateusz Loskot <mateusz@loskot.net>
      10             : /*
      11             :  * Permission is hereby granted, free of charge, to any person obtaining a
      12             :  * copy of this software and associated documentation files (the "Software"),
      13             :  * to deal in the Software without restriction, including without limitation
      14             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15             :  * and/or sell copies of the Software, and to permit persons to whom the
      16             :  * Software is furnished to do so, subject to the following conditions:
      17             :  *
      18             :  * The above copyright notice and this permission notice shall be included
      19             :  * in all copies or substantial portions of the Software.
      20             :  *
      21             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      22             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      24             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      27             :  * DEALINGS IN THE SOFTWARE.
      28             :  ****************************************************************************/
      29             : 
      30             : #include "gdal_unit_test.h"
      31             : 
      32             : #include "cpl_string.h"
      33             : #include "gdal_alg.h"
      34             : #include "gdal_priv.h"
      35             : #include "gdal.h"
      36             : 
      37             : #include <vector>
      38             : 
      39             : #include "gtest_include.h"
      40             : 
      41             : namespace
      42             : {
      43             : 
      44             : // Common fixture with test data
      45             : struct test_gdal_gtiff : public ::testing::Test
      46             : {
      47             :     struct raster_t
      48             :     {
      49             :         std::string file_;
      50             :         int band_;
      51             :         int checksum_;
      52             : 
      53          72 :         raster_t(std::string const &f, int b, int c)
      54          72 :             : file_(f), band_(b), checksum_(c)
      55             :         {
      56          72 :         }
      57             :     };
      58             : 
      59             :     typedef std::vector<raster_t> rasters_t;
      60             : 
      61             :     GDALDriverH drv_;
      62             :     std::string drv_name_;
      63             :     std::string data_;
      64             :     std::string data_tmp_;
      65             :     rasters_t rasters_;
      66             : 
      67           6 :     test_gdal_gtiff() : drv_(nullptr), drv_name_("GTiff")
      68             :     {
      69           6 :         drv_ = GDALGetDriverByName(drv_name_.c_str());
      70             : 
      71             :         // Compose data path for test group
      72           6 :         data_ = tut::common::data_basedir;
      73           6 :         data_tmp_ = tut::common::tmp_basedir;
      74             : 
      75             :         // Collection of test GeoTIFF rasters
      76           6 :         rasters_.push_back(raster_t("byte.tif", 1, 4672));
      77           6 :         rasters_.push_back(raster_t("int16.tif", 1, 4672));
      78           6 :         rasters_.push_back(raster_t("uint16.tif", 1, 4672));
      79           6 :         rasters_.push_back(raster_t("int32.tif", 1, 4672));
      80           6 :         rasters_.push_back(raster_t("uint32.tif", 1, 4672));
      81           6 :         rasters_.push_back(raster_t("float32.tif", 1, 4672));
      82           6 :         rasters_.push_back(raster_t("float64.tif", 1, 4672));
      83           6 :         rasters_.push_back(raster_t("cint16.tif", 1, 5028));
      84           6 :         rasters_.push_back(raster_t("cint32.tif", 1, 5028));
      85           6 :         rasters_.push_back(raster_t("cfloat32.tif", 1, 5028));
      86           6 :         rasters_.push_back(raster_t("cfloat64.tif", 1, 5028));
      87           6 :         rasters_.push_back(raster_t("utmsmall.tif", 1, 50054));
      88           6 :     }
      89             : 
      90           6 :     void SetUp() override
      91             :     {
      92           6 :         if (drv_ == nullptr)
      93           0 :             GTEST_SKIP() << "GTiff driver missing";
      94             :     }
      95             : };
      96             : 
      97             : // Test open dataset
      98           4 : TEST_F(test_gdal_gtiff, open)
      99             : {
     100          13 :     for (const auto &raster : rasters_)
     101             :     {
     102          12 :         std::string file(data_ + SEP);
     103          12 :         file += raster.file_;
     104          12 :         GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
     105          12 :         ASSERT_TRUE(nullptr != ds);
     106          12 :         GDALClose(ds);
     107             :     }
     108             : }
     109             : 
     110             : // Test dataset checksums
     111           4 : TEST_F(test_gdal_gtiff, checksum)
     112             : {
     113          13 :     for (const auto &raster : rasters_)
     114             :     {
     115          12 :         std::string file(data_ + SEP);
     116          12 :         file += raster.file_;
     117             : 
     118          12 :         GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
     119          12 :         ASSERT_TRUE(nullptr != ds);
     120             : 
     121          12 :         GDALRasterBandH band = GDALGetRasterBand(ds, raster.band_);
     122          12 :         ASSERT_TRUE(nullptr != band);
     123             : 
     124          12 :         const int xsize = GDALGetRasterXSize(ds);
     125          12 :         const int ysize = GDALGetRasterYSize(ds);
     126          12 :         const int checksum = GDALChecksumImage(band, 0, 0, xsize, ysize);
     127             : 
     128          12 :         EXPECT_EQ(raster.checksum_, checksum);
     129             : 
     130          12 :         GDALClose(ds);
     131             :     }
     132             : }
     133             : 
     134             : // Test GeoTIFF driver metadata
     135           4 : TEST_F(test_gdal_gtiff, driver_metadata)
     136             : {
     137           1 :     const char *mdItem = GDALGetMetadataItem(drv_, "DMD_MIMETYPE", nullptr);
     138           1 :     ASSERT_TRUE(nullptr != mdItem);
     139             : 
     140           1 :     EXPECT_STREQ(mdItem, "image/tiff");
     141             : }
     142             : 
     143             : // Create a simple file by copying from an existing one
     144           4 : TEST_F(test_gdal_gtiff, copy)
     145             : {
     146             :     // Index of test file being copied
     147           1 :     const std::size_t fileIdx = 10;
     148             : 
     149           1 :     std::string src(data_ + SEP);
     150           1 :     src += rasters_.at(fileIdx).file_;
     151           1 :     GDALDatasetH dsSrc = GDALOpen(src.c_str(), GA_ReadOnly);
     152           1 :     ASSERT_TRUE(nullptr != dsSrc);
     153             : 
     154           1 :     std::string dst(data_tmp_ + "\\test_2.tif");
     155           1 :     GDALDatasetH dsDst = nullptr;
     156           1 :     dsDst = GDALCreateCopy(drv_, dst.c_str(), dsSrc, FALSE, nullptr, nullptr,
     157             :                            nullptr);
     158           1 :     ASSERT_TRUE(nullptr != dsDst);
     159             : 
     160           1 :     GDALClose(dsDst);
     161           1 :     GDALClose(dsSrc);
     162             : 
     163             :     // Re-open copied dataset and test it
     164           1 :     dsDst = GDALOpen(dst.c_str(), GA_ReadOnly);
     165           1 :     GDALRasterBandH band = GDALGetRasterBand(dsDst, rasters_.at(fileIdx).band_);
     166           1 :     ASSERT_TRUE(nullptr != band);
     167             : 
     168           1 :     const int xsize = GDALGetRasterXSize(dsDst);
     169           1 :     const int ysize = GDALGetRasterYSize(dsDst);
     170           1 :     const int checksum = GDALChecksumImage(band, 0, 0, xsize, ysize);
     171             : 
     172           1 :     EXPECT_EQ(rasters_.at(fileIdx).checksum_, checksum);
     173             : 
     174           1 :     GDALClose(dsDst);
     175           1 :     GDALDeleteDataset(drv_, dst.c_str());
     176             : }
     177             : 
     178             : // Create a simple file by copying from an existing one using creation options
     179           4 : TEST_F(test_gdal_gtiff, copy_creation_options)
     180             : {
     181             :     // Index of test file being copied
     182           1 :     const std::size_t fileIdx = 11;
     183           1 :     std::string src(data_ + SEP);
     184           1 :     src += rasters_.at(fileIdx).file_;
     185           1 :     GDALDatasetH dsSrc = GDALOpen(src.c_str(), GA_ReadOnly);
     186           1 :     ASSERT_TRUE(nullptr != dsSrc);
     187             : 
     188           1 :     std::string dst(data_tmp_ + "\\test_3.tif");
     189             : 
     190           1 :     char **options = nullptr;
     191           1 :     options = CSLSetNameValue(options, "TILED", "YES");
     192           1 :     options = CSLSetNameValue(options, "BLOCKXSIZE", "32");
     193           1 :     options = CSLSetNameValue(options, "BLOCKYSIZE", "32");
     194             : 
     195           1 :     GDALDatasetH dsDst = nullptr;
     196           1 :     dsDst = GDALCreateCopy(drv_, dst.c_str(), dsSrc, FALSE, options, nullptr,
     197             :                            nullptr);
     198           1 :     ASSERT_TRUE(nullptr != dsDst);
     199             : 
     200           1 :     GDALClose(dsDst);
     201           1 :     CSLDestroy(options);
     202           1 :     GDALClose(dsSrc);
     203             : 
     204             :     // Re-open copied dataset and test it
     205           1 :     dsDst = GDALOpen(dst.c_str(), GA_ReadOnly);
     206           1 :     GDALRasterBandH band = GDALGetRasterBand(dsDst, rasters_.at(fileIdx).band_);
     207           1 :     ASSERT_TRUE(nullptr != band);
     208             : 
     209           1 :     const int xsize = GDALGetRasterXSize(dsDst);
     210           1 :     const int ysize = GDALGetRasterYSize(dsDst);
     211           1 :     const int checksum = GDALChecksumImage(band, 0, 0, xsize, ysize);
     212             : 
     213           1 :     EXPECT_EQ(rasters_.at(fileIdx).checksum_, checksum);
     214             : 
     215           1 :     GDALClose(dsDst);
     216           1 :     GDALDeleteDataset(drv_, dst.c_str());
     217             : }
     218             : 
     219             : // Test raster min/max calculation
     220           4 : TEST_F(test_gdal_gtiff, raster_min_max)
     221             : {
     222             :     // Index of test file being copied
     223           1 :     const std::size_t fileIdx = 10;
     224             : 
     225           1 :     std::string src(data_ + SEP);
     226           1 :     src += rasters_.at(fileIdx).file_;
     227           1 :     GDALDatasetH ds = GDALOpen(src.c_str(), GA_ReadOnly);
     228           1 :     ASSERT_TRUE(nullptr != ds);
     229             : 
     230           1 :     GDALRasterBandH band = GDALGetRasterBand(ds, rasters_.at(fileIdx).band_);
     231           1 :     ASSERT_TRUE(nullptr != band);
     232             : 
     233           1 :     double expect[2] = {74.0, 255.0};
     234           1 :     double minmax[2] = {0};
     235           1 :     GDALComputeRasterMinMax(band, TRUE, minmax);
     236             : 
     237           1 :     EXPECT_EQ(expect[0], minmax[0]);
     238           1 :     EXPECT_EQ(expect[1], minmax[1]);
     239             : 
     240           1 :     GDALClose(ds);
     241             : }
     242             : 
     243             : }  // namespace

Generated by: LCOV version 1.14