LCOV - code coverage report
Current view: top level - frmts/mrf - PNG_band.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 124 180 68.9 %
Date: 2025-05-31 00:00:17 Functions: 8 11 72.7 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2002-2012, California Institute of Technology.
       3             :  * All rights reserved.  Based on Government Sponsored Research under contracts
       4             :  * NAS7-1407 and/or NAS7-03001.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are met:
       8             :  *   1. Redistributions of source code must retain the above copyright notice,
       9             :  * this list of conditions and the following disclaimer.
      10             :  *   2. Redistributions in binary form must reproduce the above copyright
      11             :  * notice, this list of conditions and the following disclaimer in the
      12             :  * documentation and/or other materials provided with the distribution.
      13             :  *   3. Neither the name of the California Institute of Technology (Caltech),
      14             :  * its operating division the Jet Propulsion Laboratory (JPL), the National
      15             :  * Aeronautics and Space Administration (NASA), nor the names of its
      16             :  * contributors may be used to endorse or promote products derived from this
      17             :  * software without specific prior written permission.
      18             :  *
      19             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      20             :  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      21             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      22             :  * ARE DISCLAIMED. IN NO EVENT SHALL THE CALIFORNIA INSTITUTE OF TECHNOLOGY BE
      23             :  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      24             :  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      25             :  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
      26             :  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
      27             :  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      28             :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      29             :  * POSSIBILITY OF SUCH DAMAGE.
      30             :  *
      31             :  * Copyright (c) 2014-2021 Esri
      32             :  *
      33             :  * Licensed under the Apache License, Version 2.0 (the "License");
      34             :  * you may not use this file except in compliance with the License.
      35             :  * You may obtain a copy of the License at
      36             :  *
      37             :  * http://www.apache.org/licenses/LICENSE-2.0
      38             :  *
      39             :  * Unless required by applicable law or agreed to in writing, software
      40             :  * distributed under the License is distributed on an "AS IS" BASIS,
      41             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      42             :  * See the License for the specific language governing permissions and
      43             :  * limitations under the License.
      44             :  *
      45             :  * Author: Lucian Plesea
      46             :  *
      47             :  */
      48             : 
      49             : /*
      50             :  * PNG band
      51             :  * PNG page compression and decompression functions
      52             :  * These functions are not methods, they reside in the global space
      53             :  *
      54             :  */
      55             : 
      56             : #include "marfa.h"
      57             : #include <cassert>
      58             : #include <algorithm>
      59             : 
      60             : CPL_C_START
      61             : #include <png.h>
      62             : CPL_C_END
      63             : 
      64             : NAMESPACE_MRF_START
      65             : 
      66             : // Do Nothing
      67           0 : static void flush_png(png_structp)
      68             : {
      69           0 : }
      70             : 
      71             : // Warning Emit
      72           0 : static void pngWH(png_struct * /*png*/, png_const_charp message)
      73             : {
      74           0 :     CPLError(CE_Warning, CPLE_AppDefined, "MRF: PNG warning %s", message);
      75           0 : }
      76             : 
      77             : // Fatal Warning
      78           0 : static void pngEH(png_struct *png, png_const_charp message)
      79             : {
      80           0 :     CPLError(CE_Failure, CPLE_AppDefined, "MRF: PNG Failure %s", message);
      81           0 :     longjmp(png_jmpbuf(png), 1);
      82             : }
      83             : 
      84             : // Read memory handlers for PNG
      85             : // No check for attempting to read past the end of the buffer
      86             : 
      87         321 : static void read_png(png_structp pngp, png_bytep data, png_size_t length)
      88             : {
      89         321 :     buf_mgr *pmgr = (buf_mgr *)png_get_io_ptr(pngp);
      90         321 :     if (pmgr->size < length)
      91             :     {
      92           0 :         CPLError(CE_Failure, CPLE_AppDefined,
      93             :                  "MRF: PNG Failure: Not enough bytes in buffer");
      94           0 :         longjmp(png_jmpbuf(pngp), 1);
      95             :     }
      96         321 :     memcpy(data, pmgr->buffer, length);
      97         321 :     pmgr->buffer += length;
      98         321 :     pmgr->size -= length;
      99         321 : }
     100             : 
     101         420 : static void write_png(png_structp pngp, png_bytep data, png_size_t length)
     102             : {
     103         420 :     buf_mgr *mgr = (buf_mgr *)png_get_io_ptr(pngp);
     104             :     // Buffer could be too small, trigger an error on debug mode
     105         420 :     assert(length <= mgr->size);
     106         420 :     memcpy(mgr->buffer, data, length);
     107         420 :     mgr->buffer += length;
     108         420 :     mgr->size -= length;
     109         420 : }
     110             : 
     111             : /**
     112             :  *\brief In memory decompression of PNG file
     113             :  */
     114             : 
     115          11 : CPLErr PNG_Codec::DecompressPNG(buf_mgr &dst, buf_mgr &src)
     116             : {
     117          11 :     const buf_mgr src_ori = src;
     118          11 :     png_bytep *png_rowp = nullptr;
     119          11 :     volatile png_bytep *p_volatile_png_rowp =
     120             :         reinterpret_cast<volatile png_bytep *>(&png_rowp);
     121             : 
     122             :     // pngp=png_create_read_struct(PNG_LIBPNG_VER_STRING,0,pngEH,pngWH);
     123          11 :     png_structp pngp = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
     124          11 :                                               nullptr, nullptr);
     125          11 :     if (nullptr == pngp)
     126             :     {
     127           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     128             :                  "MRF: Error creating PNG decompress");
     129           0 :         return CE_Failure;
     130             :     }
     131             : 
     132          11 :     png_infop infop = png_create_info_struct(pngp);
     133          11 :     if (nullptr == infop)
     134             :     {
     135           0 :         png_destroy_read_struct(&pngp, &infop, nullptr);
     136           0 :         CPLError(CE_Failure, CPLE_AppDefined, "MRF: Error creating PNG info");
     137           0 :         return CE_Failure;
     138             :     }
     139             : 
     140          11 :     if (setjmp(png_jmpbuf(pngp)))
     141             :     {
     142           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     143             :                  "MRF: Error during PNG decompress");
     144           0 :         CPLFree((void *)(*p_volatile_png_rowp));
     145           0 :         png_destroy_read_struct(&pngp, &infop, nullptr);
     146           0 :         return CE_Failure;
     147             :     }
     148             : 
     149             :     // The mgr data ptr is already set up
     150          11 :     png_set_read_fn(pngp, &src, read_png);
     151             :     // Ready to read
     152          11 :     png_read_info(pngp, infop);
     153             : 
     154          11 :     if (png_get_bit_depth(pngp, infop) == 8)
     155             :     {
     156             :         // Use the PNG driver for decompression of 8-bit images, as it
     157             :         // has optimizations for whole image decompression.
     158           9 :         const CPLString osTmpFilename(VSIMemGenerateHiddenFilename("mrf.png"));
     159           9 :         VSIFCloseL(VSIFileFromMemBuffer(
     160           9 :             osTmpFilename.c_str(), reinterpret_cast<GByte *>(src_ori.buffer),
     161           9 :             src_ori.size, false));
     162           9 :         const char *const apszAllowedDrivers[] = {"PNG", nullptr};
     163             :         auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
     164           9 :             osTmpFilename.c_str(), GDAL_OF_RASTER, apszAllowedDrivers));
     165          18 :         if (poDS && static_cast<GUIntBig>(poDS->GetRasterXSize()) *
     166           9 :                             poDS->GetRasterYSize() * poDS->GetRasterCount() ==
     167          18 :                         dst.size)
     168             :         {
     169          27 :             if (poDS->RasterIO(
     170             :                     GF_Read, 0, 0, poDS->GetRasterXSize(),
     171           9 :                     poDS->GetRasterYSize(), dst.buffer, poDS->GetRasterXSize(),
     172             :                     poDS->GetRasterYSize(), GDT_Byte, poDS->GetRasterCount(),
     173          18 :                     nullptr, poDS->GetRasterCount(), 0, 1, nullptr) == CE_None)
     174             :             {
     175           9 :                 png_destroy_read_struct(&pngp, &infop, nullptr);
     176           9 :                 VSIUnlink(osTmpFilename.c_str());
     177           9 :                 return CE_None;
     178             :             }
     179             :         }
     180           0 :         VSIUnlink(osTmpFilename.c_str());
     181             :     }
     182             : 
     183           2 :     GInt32 height = static_cast<GInt32>(png_get_image_height(pngp, infop));
     184             :     // Check the size
     185           2 :     if (dst.size < (png_get_rowbytes(pngp, infop) * height))
     186             :     {
     187           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     188             :                  "MRF: PNG Page data bigger than the buffer provided");
     189           0 :         png_destroy_read_struct(&pngp, &infop, nullptr);
     190           0 :         return CE_Failure;
     191             :     }
     192             : 
     193           2 :     png_rowp = (png_bytep *)CPLMalloc(sizeof(png_bytep) * height);
     194             : 
     195           2 :     int rowbytes = static_cast<int>(png_get_rowbytes(pngp, infop));
     196        1026 :     for (int i = 0; i < height; i++)
     197        1024 :         png_rowp[i] = (png_bytep)dst.buffer + i * rowbytes;
     198             : 
     199             : #if defined(CPL_LSB)
     200           2 :     if (png_get_bit_depth(pngp, infop) > 8)
     201             :     {
     202           2 :         png_set_swap(pngp);
     203             :         // Call update info if any png_set is used
     204           2 :         png_read_update_info(pngp, infop);
     205             :     }
     206             : #endif
     207             : 
     208             :     // Finally, the read
     209             :     // This is the lower level, the png_read_end allows some transforms
     210             :     // Like palette to RGBA
     211           2 :     png_read_image(pngp, png_rowp);
     212             : 
     213             :     //    ppmWrite("Test.ppm",(char *)data,ILSize(512,512,1,4,0));
     214             :     // Required
     215           2 :     png_read_end(pngp, infop);
     216             : 
     217             :     // png_set_rows(pngp,infop,png_rowp);
     218             :     // png_read_png(pngp,infop,PNG_TRANSFORM_IDENTITY,0);
     219             : 
     220           2 :     CPLFree(png_rowp);
     221           2 :     png_destroy_read_struct(&pngp, &infop, nullptr);
     222           2 :     return CE_None;
     223             : }
     224             : 
     225             : /**
     226             :  *\brief Compress a page in PNG format
     227             :  * Returns the compressed size in dst.size
     228             :  *
     229             :  */
     230             : 
     231          16 : CPLErr PNG_Codec::CompressPNG(buf_mgr &dst, const buf_mgr &src)
     232             : 
     233             : {
     234             :     png_structp pngp;
     235             :     png_infop infop;
     236          16 :     buf_mgr mgr = dst;
     237             : 
     238          16 :     pngp =
     239          16 :         png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, pngEH, pngWH);
     240          16 :     if (!pngp)
     241             :     {
     242           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     243             :                  "MRF: Error creating png structure");
     244           0 :         return CE_Failure;
     245             :     }
     246          16 :     infop = png_create_info_struct(pngp);
     247          16 :     if (!infop)
     248             :     {
     249           0 :         png_destroy_write_struct(&pngp, nullptr);
     250           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     251             :                  "MRF: Error creating png info structure");
     252           0 :         return CE_Failure;
     253             :     }
     254             : 
     255          16 :     if (setjmp(png_jmpbuf(pngp)))
     256             :     {
     257           0 :         png_destroy_write_struct(&pngp, &infop);
     258           0 :         CPLError(CE_Failure, CPLE_AppDefined, "MRF: Error during png init");
     259           0 :         return CE_Failure;
     260             :     }
     261             : 
     262          16 :     png_set_write_fn(pngp, &mgr, write_png, flush_png);
     263             : 
     264             :     int png_ctype;
     265             : 
     266          16 :     switch (img.pagesize.c)
     267             :     {
     268          16 :         case 1:
     269          16 :             if (PNGColors != nullptr)
     270           1 :                 png_ctype = PNG_COLOR_TYPE_PALETTE;
     271             :             else
     272          15 :                 png_ctype = PNG_COLOR_TYPE_GRAY;
     273          16 :             break;
     274           0 :         case 2:
     275           0 :             png_ctype = PNG_COLOR_TYPE_GRAY_ALPHA;
     276           0 :             break;
     277           0 :         case 3:
     278           0 :             png_ctype = PNG_COLOR_TYPE_RGB;
     279           0 :             break;
     280           0 :         case 4:
     281           0 :             png_ctype = PNG_COLOR_TYPE_RGB_ALPHA;
     282           0 :             break;
     283           0 :         default:
     284             :         {  // This never happens if we check at the open
     285           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     286           0 :                      "MRF:PNG Write with %d colors called", img.pagesize.c);
     287           0 :             return CE_Failure;
     288             :         }
     289             :     }
     290             : 
     291          16 :     png_set_IHDR(pngp, infop, img.pagesize.x, img.pagesize.y,
     292          16 :                  GDALGetDataTypeSize(img.dt), png_ctype, PNG_INTERLACE_NONE,
     293             :                  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
     294             : 
     295             :     // Optional, force certain filters only.  Makes it somewhat faster but worse
     296             :     // compression png_set_filter(pngp, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
     297             : 
     298             : #if defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER > 10200) &&                     \
     299             :     defined(PNG_SELECT_READ)
     300             :     png_uint_32 mask, flags;
     301             : 
     302             :     flags = png_get_asm_flags(pngp);
     303             :     mask = png_get_asm_flagmask(PNG_SELECT_READ | PNG_SELECT_WRITE);
     304             :     png_set_asm_flags(pngp, flags | mask);  // use flags &~mask to disable all
     305             : #endif
     306             : 
     307             :     // Let the quality control the compression level
     308          16 :     png_set_compression_level(pngp, std::clamp(img.quality / 10, 1, 9));
     309             : 
     310             :     // Custom strategy for zlib, set using the band option Z_STRATEGY
     311          16 :     if (deflate_flags & ZFLAG_SMASK)
     312           0 :         png_set_compression_strategy(pngp, (deflate_flags & ZFLAG_SMASK) >> 6);
     313             : 
     314             :     // Write the palette and the transparencies if they exist
     315          16 :     if (PNGColors != nullptr)
     316             :     {
     317           1 :         png_set_PLTE(pngp, infop, (png_colorp)PNGColors, PalSize);
     318           1 :         if (TransSize != 0)
     319           0 :             png_set_tRNS(pngp, infop, (unsigned char *)PNGAlpha, TransSize,
     320             :                          nullptr);
     321             :     }
     322             : 
     323          16 :     png_write_info(pngp, infop);
     324             : 
     325             : #if defined(CPL_LSB)
     326          16 :     if (img.dt != GDT_Byte)
     327           4 :         png_set_swap(pngp);
     328             : #endif
     329             : 
     330             :     png_bytep *png_rowp =
     331          16 :         (png_bytep *)CPLMalloc(sizeof(png_bytep) * img.pagesize.y);
     332             : 
     333          16 :     if (setjmp(png_jmpbuf(pngp)))
     334             :     {
     335           0 :         CPLFree(png_rowp);
     336           0 :         png_destroy_write_struct(&pngp, &infop);
     337           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     338             :                  "MRF: Error during png compression");
     339           0 :         return CE_Failure;
     340             :     }
     341             : 
     342          16 :     int rowbytes = static_cast<int>(png_get_rowbytes(pngp, infop));
     343        8208 :     for (int i = 0; i < img.pagesize.y; i++)
     344        8192 :         png_rowp[i] = (png_bytep)(src.buffer + i * rowbytes);
     345             : 
     346          16 :     png_write_image(pngp, png_rowp);
     347          16 :     png_write_end(pngp, infop);
     348             : 
     349             :     // Done
     350          16 :     CPLFree(png_rowp);
     351          16 :     png_destroy_write_struct(&pngp, &infop);
     352             : 
     353             :     // Done
     354             :     // mgr.size holds the available bytes, so the size of the compressed png
     355             :     // is the original destination size minus the still available bytes
     356          16 :     dst.size -= mgr.size;
     357             : 
     358          16 :     return CE_None;
     359             : }
     360             : 
     361             : // Builds a PNG palette from a GDAL color table
     362           1 : static void ResetPalette(GDALColorTable *poCT, PNG_Codec &codec)
     363             : {  // Convert the GDAL LUT to PNG style
     364           1 :     codec.TransSize = codec.PalSize = poCT->GetColorEntryCount();
     365             : 
     366             :     png_color *pasPNGColors =
     367           1 :         (png_color *)CPLMalloc(sizeof(png_color) * codec.PalSize);
     368           1 :     unsigned char *pabyAlpha = (unsigned char *)CPLMalloc(codec.TransSize);
     369           1 :     codec.PNGColors = (void *)pasPNGColors;
     370           1 :     codec.PNGAlpha = (void *)pabyAlpha;
     371           1 :     bool NoTranspYet = true;
     372             : 
     373             :     // Set the palette from the end to reduce the size of the opacity mask
     374         257 :     for (int iColor = codec.PalSize - 1; iColor >= 0; iColor--)
     375             :     {
     376             :         GDALColorEntry sEntry;
     377         256 :         poCT->GetColorEntryAsRGB(iColor, &sEntry);
     378             : 
     379         256 :         pasPNGColors[iColor].red = (png_byte)sEntry.c1;
     380         256 :         pasPNGColors[iColor].green = (png_byte)sEntry.c2;
     381         256 :         pasPNGColors[iColor].blue = (png_byte)sEntry.c3;
     382         256 :         if (NoTranspYet && sEntry.c4 == 255)
     383         256 :             codec.TransSize--;
     384             :         else
     385             :         {
     386           0 :             NoTranspYet = false;
     387           0 :             pabyAlpha[iColor] = (unsigned char)sEntry.c4;
     388             :         }
     389             :     }
     390           1 : }
     391             : 
     392          11 : CPLErr PNG_Band::Decompress(buf_mgr &dst, buf_mgr &src)
     393             : {
     394          11 :     return codec.DecompressPNG(dst, src);
     395             : }
     396             : 
     397          16 : CPLErr PNG_Band::Compress(buf_mgr &dst, buf_mgr &src)
     398             : {
     399          16 :     if (!codec.PNGColors && img.comp == IL_PPNG)
     400             :     {  // Late set PNG palette to conserve memory
     401           1 :         GDALColorTable *poCT = GetColorTable();
     402           1 :         if (!poCT)
     403             :         {
     404           0 :             CPLError(CE_Failure, CPLE_NotSupported,
     405             :                      "MRF PPNG needs a color table");
     406           0 :             return CE_Failure;
     407             :         }
     408           1 :         ResetPalette(poCT, codec);
     409             :     }
     410             : 
     411          16 :     codec.deflate_flags = deflate_flags;
     412          16 :     return codec.CompressPNG(dst, src);
     413             : }
     414             : 
     415             : /**
     416             :  * \brief For PPNG, builds the data structures needed to write the palette
     417             :  * The presence of the PNGColors and PNGAlpha is used as a flag for PPNG only
     418             :  */
     419             : 
     420         159 : PNG_Band::PNG_Band(MRFDataset *pDS, const ILImage &image, int b, int level)
     421         159 :     : MRFRasterBand(pDS, image, b, level), codec(image)
     422             : {  // Check error conditions
     423         159 :     if (image.dt != GDT_Byte && image.dt != GDT_Int16 && image.dt != GDT_UInt16)
     424             :     {
     425          52 :         CPLError(CE_Failure, CPLE_NotSupported,
     426             :                  "Data type not supported by MRF PNG");
     427          52 :         return;
     428             :     }
     429         107 :     if (image.pagesize.c > 4)
     430             :     {
     431           0 :         CPLError(CE_Failure, CPLE_NotSupported,
     432             :                  "MRF PNG can only handle up to 4 bands per page");
     433           0 :         return;
     434             :     }
     435             :     // PNGs can be larger than the source, especially for small page size
     436             :     // If PPNG is used, the palette can take up to 2100 bytes
     437         107 :     poMRFDS->SetPBufferSize(
     438         107 :         static_cast<unsigned int>(1.1 * image.pageSizeBytes + 4000));
     439             : }
     440             : 
     441             : NAMESPACE_MRF_END

Generated by: LCOV version 1.14