LCOV - code coverage report
Current view: top level - frmts/mrf - PNG_band.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 137 183 74.9 %
Date: 2024-05-04 12:52:34 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             : 
      59             : CPL_C_START
      60             : #include <png.h>
      61             : CPL_C_END
      62             : 
      63             : NAMESPACE_MRF_START
      64             : 
      65             : // Do Nothing
      66           0 : static void flush_png(png_structp)
      67             : {
      68           0 : }
      69             : 
      70             : // Warning Emit
      71           0 : static void pngWH(png_struct * /*png*/, png_const_charp message)
      72             : {
      73           0 :     CPLError(CE_Warning, CPLE_AppDefined, "MRF: PNG warning %s", message);
      74           0 : }
      75             : 
      76             : // Fatal Warning
      77           0 : static void pngEH(png_struct *png, png_const_charp message)
      78             : {
      79           0 :     CPLError(CE_Failure, CPLE_AppDefined, "MRF: PNG Failure %s", message);
      80           0 :     longjmp(png_jmpbuf(png), 1);
      81             : }
      82             : 
      83             : // Read memory handlers for PNG
      84             : // No check for attempting to read past the end of the buffer
      85             : 
      86         321 : static void read_png(png_structp pngp, png_bytep data, png_size_t length)
      87             : {
      88         321 :     buf_mgr *pmgr = (buf_mgr *)png_get_io_ptr(pngp);
      89         321 :     if (pmgr->size < length)
      90             :     {
      91           0 :         CPLError(CE_Failure, CPLE_AppDefined,
      92             :                  "MRF: PNG Failure: Not enough bytes in buffer");
      93           0 :         longjmp(png_jmpbuf(pngp), 1);
      94             :     }
      95         321 :     memcpy(data, pmgr->buffer, length);
      96         321 :     pmgr->buffer += length;
      97         321 :     pmgr->size -= length;
      98         321 : }
      99             : 
     100         411 : static void write_png(png_structp pngp, png_bytep data, png_size_t length)
     101             : {
     102         411 :     buf_mgr *mgr = (buf_mgr *)png_get_io_ptr(pngp);
     103             :     // Buffer could be too small, trigger an error on debug mode
     104         411 :     assert(length <= mgr->size);
     105         411 :     memcpy(mgr->buffer, data, length);
     106         411 :     mgr->buffer += length;
     107         411 :     mgr->size -= length;
     108         411 : }
     109             : 
     110             : /**
     111             :  *\brief In memory decompression of PNG file
     112             :  */
     113             : 
     114          11 : CPLErr PNG_Codec::DecompressPNG(buf_mgr &dst, buf_mgr &src)
     115             : {
     116          11 :     const buf_mgr src_ori = src;
     117          11 :     png_bytep *png_rowp = nullptr;
     118          11 :     volatile png_bytep *p_volatile_png_rowp = (volatile png_bytep *)&png_rowp;
     119             : 
     120             :     // pngp=png_create_read_struct(PNG_LIBPNG_VER_STRING,0,pngEH,pngWH);
     121          11 :     png_structp pngp = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
     122          11 :                                               nullptr, nullptr);
     123          11 :     if (nullptr == pngp)
     124             :     {
     125           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     126             :                  "MRF: Error creating PNG decompress");
     127           0 :         return CE_Failure;
     128             :     }
     129             : 
     130          11 :     png_infop infop = png_create_info_struct(pngp);
     131          11 :     if (nullptr == infop)
     132             :     {
     133           0 :         png_destroy_read_struct(&pngp, &infop, nullptr);
     134           0 :         CPLError(CE_Failure, CPLE_AppDefined, "MRF: Error creating PNG info");
     135           0 :         return CE_Failure;
     136             :     }
     137             : 
     138          11 :     if (setjmp(png_jmpbuf(pngp)))
     139             :     {
     140           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     141             :                  "MRF: Error during PNG decompress");
     142           0 :         CPLFree((void *)(*p_volatile_png_rowp));
     143           0 :         png_destroy_read_struct(&pngp, &infop, nullptr);
     144           0 :         return CE_Failure;
     145             :     }
     146             : 
     147             :     // The mgr data ptr is already set up
     148          11 :     png_set_read_fn(pngp, &src, read_png);
     149             :     // Ready to read
     150          11 :     png_read_info(pngp, infop);
     151             : 
     152          11 :     if (png_get_bit_depth(pngp, infop) == 8)
     153             :     {
     154             :         // Use the PNG driver for decompression of 8-bit images, as it
     155             :         // has optimizations for whole image decompression.
     156           9 :         CPLString osTmpFilename(CPLSPrintf("/vsimem/mrf/%p.png", &dst));
     157           9 :         VSIFCloseL(VSIFileFromMemBuffer(
     158           9 :             osTmpFilename.c_str(), reinterpret_cast<GByte *>(src_ori.buffer),
     159           9 :             src_ori.size, false));
     160           9 :         const char *const apszAllowedDrivers[] = {"PNG", nullptr};
     161             :         auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
     162           9 :             osTmpFilename.c_str(), GDAL_OF_RASTER, apszAllowedDrivers));
     163          18 :         if (poDS && static_cast<GUIntBig>(poDS->GetRasterXSize()) *
     164           9 :                             poDS->GetRasterYSize() * poDS->GetRasterCount() ==
     165          18 :                         dst.size)
     166             :         {
     167          27 :             if (poDS->RasterIO(
     168             :                     GF_Read, 0, 0, poDS->GetRasterXSize(),
     169           9 :                     poDS->GetRasterYSize(), dst.buffer, poDS->GetRasterXSize(),
     170             :                     poDS->GetRasterYSize(), GDT_Byte, poDS->GetRasterCount(),
     171          18 :                     nullptr, poDS->GetRasterCount(), 0, 1, nullptr) == CE_None)
     172             :             {
     173           9 :                 png_destroy_read_struct(&pngp, &infop, nullptr);
     174           9 :                 VSIUnlink(osTmpFilename.c_str());
     175           9 :                 return CE_None;
     176             :             }
     177             :         }
     178           0 :         VSIUnlink(osTmpFilename.c_str());
     179             :     }
     180             : 
     181           2 :     GInt32 height = static_cast<GInt32>(png_get_image_height(pngp, infop));
     182             :     // Check the size
     183           2 :     if (dst.size < (png_get_rowbytes(pngp, infop) * height))
     184             :     {
     185           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     186             :                  "MRF: PNG Page data bigger than the buffer provided");
     187           0 :         png_destroy_read_struct(&pngp, &infop, nullptr);
     188           0 :         return CE_Failure;
     189             :     }
     190             : 
     191           2 :     png_rowp = (png_bytep *)CPLMalloc(sizeof(png_bytep) * height);
     192             : 
     193           2 :     int rowbytes = static_cast<int>(png_get_rowbytes(pngp, infop));
     194        1026 :     for (int i = 0; i < height; i++)
     195        1024 :         png_rowp[i] = (png_bytep)dst.buffer + i * rowbytes;
     196             : 
     197             : #if defined(CPL_LSB)
     198           2 :     if (png_get_bit_depth(pngp, infop) > 8)
     199             :     {
     200           2 :         png_set_swap(pngp);
     201             :         // Call update info if any png_set is used
     202           2 :         png_read_update_info(pngp, infop);
     203             :     }
     204             : #endif
     205             : 
     206             :     // Finally, the read
     207             :     // This is the lower level, the png_read_end allows some transforms
     208             :     // Like palette to RGBA
     209           2 :     png_read_image(pngp, png_rowp);
     210             : 
     211             :     //    ppmWrite("Test.ppm",(char *)data,ILSize(512,512,1,4,0));
     212             :     // Required
     213           2 :     png_read_end(pngp, infop);
     214             : 
     215             :     // png_set_rows(pngp,infop,png_rowp);
     216             :     // png_read_png(pngp,infop,PNG_TRANSFORM_IDENTITY,0);
     217             : 
     218           2 :     CPLFree(png_rowp);
     219           2 :     png_destroy_read_struct(&pngp, &infop, nullptr);
     220           2 :     return CE_None;
     221             : }
     222             : 
     223             : /**
     224             :  *\brief Compress a page in PNG format
     225             :  * Returns the compressed size in dst.size
     226             :  *
     227             :  */
     228             : 
     229          15 : CPLErr PNG_Codec::CompressPNG(buf_mgr &dst, const buf_mgr &src)
     230             : 
     231             : {
     232             :     png_structp pngp;
     233             :     png_infop infop;
     234          15 :     buf_mgr mgr = dst;
     235             : 
     236          15 :     pngp =
     237          15 :         png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, pngEH, pngWH);
     238          15 :     if (!pngp)
     239             :     {
     240           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     241             :                  "MRF: Error creating png structure");
     242           0 :         return CE_Failure;
     243             :     }
     244          15 :     infop = png_create_info_struct(pngp);
     245          15 :     if (!infop)
     246             :     {
     247           0 :         png_destroy_write_struct(&pngp, nullptr);
     248           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     249             :                  "MRF: Error creating png info structure");
     250           0 :         return CE_Failure;
     251             :     }
     252             : 
     253          15 :     if (setjmp(png_jmpbuf(pngp)))
     254             :     {
     255           0 :         png_destroy_write_struct(&pngp, &infop);
     256           0 :         CPLError(CE_Failure, CPLE_AppDefined, "MRF: Error during png init");
     257           0 :         return CE_Failure;
     258             :     }
     259             : 
     260          15 :     png_set_write_fn(pngp, &mgr, write_png, flush_png);
     261             : 
     262             :     int png_ctype;
     263             : 
     264          15 :     switch (img.pagesize.c)
     265             :     {
     266          12 :         case 1:
     267          12 :             if (PNGColors != nullptr)
     268           1 :                 png_ctype = PNG_COLOR_TYPE_PALETTE;
     269             :             else
     270          11 :                 png_ctype = PNG_COLOR_TYPE_GRAY;
     271          12 :             break;
     272           1 :         case 2:
     273           1 :             png_ctype = PNG_COLOR_TYPE_GRAY_ALPHA;
     274           1 :             break;
     275           1 :         case 3:
     276           1 :             png_ctype = PNG_COLOR_TYPE_RGB;
     277           1 :             break;
     278           1 :         case 4:
     279           1 :             png_ctype = PNG_COLOR_TYPE_RGB_ALPHA;
     280           1 :             break;
     281           0 :         default:
     282             :         {  // This never happens if we check at the open
     283           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     284           0 :                      "MRF:PNG Write with %d colors called", img.pagesize.c);
     285           0 :             return CE_Failure;
     286             :         }
     287             :     }
     288             : 
     289          15 :     png_set_IHDR(pngp, infop, img.pagesize.x, img.pagesize.y,
     290          15 :                  GDALGetDataTypeSize(img.dt), png_ctype, PNG_INTERLACE_NONE,
     291             :                  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
     292             : 
     293             :     // Optional, force certain filters only.  Makes it somewhat faster but worse
     294             :     // compression png_set_filter(pngp, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
     295             : 
     296             : #if defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER > 10200) &&                     \
     297             :     defined(PNG_SELECT_READ)
     298             :     png_uint_32 mask, flags;
     299             : 
     300             :     flags = png_get_asm_flags(pngp);
     301             :     mask = png_get_asm_flagmask(PNG_SELECT_READ | PNG_SELECT_WRITE);
     302             :     png_set_asm_flags(pngp, flags | mask);  // use flags &~mask to disable all
     303             : 
     304             :     // Test that the MMX is compiled into PNG
     305             :     // fprintf(stderr,"MMX support is %d\n", png_mmx_support());
     306             : 
     307             : #endif
     308             : 
     309             :     // Let the quality control the compression level
     310             :     // Start at level 1, level 0 means uncompressed
     311          15 :     int zlvl = img.quality / 10;
     312          15 :     if (0 == zlvl)
     313           0 :         zlvl = 1;
     314          15 :     png_set_compression_level(pngp, zlvl);
     315             : 
     316             :     // Custom strategy for zlib, set using the band option Z_STRATEGY
     317          15 :     if (deflate_flags & ZFLAG_SMASK)
     318           0 :         png_set_compression_strategy(pngp, (deflate_flags & ZFLAG_SMASK) >> 6);
     319             : 
     320             :     // Write the palette and the transparencies if they exist
     321          15 :     if (PNGColors != nullptr)
     322             :     {
     323           1 :         png_set_PLTE(pngp, infop, (png_colorp)PNGColors, PalSize);
     324           1 :         if (TransSize != 0)
     325           0 :             png_set_tRNS(pngp, infop, (unsigned char *)PNGAlpha, TransSize,
     326             :                          nullptr);
     327             :     }
     328             : 
     329          15 :     png_write_info(pngp, infop);
     330             : 
     331             : #if defined(CPL_LSB)
     332          15 :     if (img.dt != GDT_Byte)
     333           4 :         png_set_swap(pngp);
     334             : #endif
     335             : 
     336             :     png_bytep *png_rowp =
     337          15 :         (png_bytep *)CPLMalloc(sizeof(png_bytep) * img.pagesize.y);
     338             : 
     339          15 :     if (setjmp(png_jmpbuf(pngp)))
     340             :     {
     341           0 :         CPLFree(png_rowp);
     342           0 :         png_destroy_write_struct(&pngp, &infop);
     343           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     344             :                  "MRF: Error during png compression");
     345           0 :         return CE_Failure;
     346             :     }
     347             : 
     348          15 :     int rowbytes = static_cast<int>(png_get_rowbytes(pngp, infop));
     349        7695 :     for (int i = 0; i < img.pagesize.y; i++)
     350        7680 :         png_rowp[i] = (png_bytep)(src.buffer + i * rowbytes);
     351             : 
     352          15 :     png_write_image(pngp, png_rowp);
     353          15 :     png_write_end(pngp, infop);
     354             : 
     355             :     // Done
     356          15 :     CPLFree(png_rowp);
     357          15 :     png_destroy_write_struct(&pngp, &infop);
     358             : 
     359             :     // Done
     360             :     // mgr.size holds the available bytes, so the size of the compressed png
     361             :     // is the original destination size minus the still available bytes
     362          15 :     dst.size -= mgr.size;
     363             : 
     364          15 :     return CE_None;
     365             : }
     366             : 
     367             : // Builds a PNG palette from a GDAL color table
     368           1 : static void ResetPalette(GDALColorTable *poCT, PNG_Codec &codec)
     369             : {  // Convert the GDAL LUT to PNG style
     370           1 :     codec.TransSize = codec.PalSize = poCT->GetColorEntryCount();
     371             : 
     372             :     png_color *pasPNGColors =
     373           1 :         (png_color *)CPLMalloc(sizeof(png_color) * codec.PalSize);
     374           1 :     unsigned char *pabyAlpha = (unsigned char *)CPLMalloc(codec.TransSize);
     375           1 :     codec.PNGColors = (void *)pasPNGColors;
     376           1 :     codec.PNGAlpha = (void *)pabyAlpha;
     377           1 :     bool NoTranspYet = true;
     378             : 
     379             :     // Set the palette from the end to reduce the size of the opacity mask
     380         257 :     for (int iColor = codec.PalSize - 1; iColor >= 0; iColor--)
     381             :     {
     382             :         GDALColorEntry sEntry;
     383         256 :         poCT->GetColorEntryAsRGB(iColor, &sEntry);
     384             : 
     385         256 :         pasPNGColors[iColor].red = (png_byte)sEntry.c1;
     386         256 :         pasPNGColors[iColor].green = (png_byte)sEntry.c2;
     387         256 :         pasPNGColors[iColor].blue = (png_byte)sEntry.c3;
     388         256 :         if (NoTranspYet && sEntry.c4 == 255)
     389         256 :             codec.TransSize--;
     390             :         else
     391             :         {
     392           0 :             NoTranspYet = false;
     393           0 :             pabyAlpha[iColor] = (unsigned char)sEntry.c4;
     394             :         }
     395             :     }
     396           1 : }
     397             : 
     398          11 : CPLErr PNG_Band::Decompress(buf_mgr &dst, buf_mgr &src)
     399             : {
     400          11 :     return codec.DecompressPNG(dst, src);
     401             : }
     402             : 
     403          15 : CPLErr PNG_Band::Compress(buf_mgr &dst, buf_mgr &src)
     404             : {
     405          15 :     if (!codec.PNGColors && img.comp == IL_PPNG)
     406             :     {  // Late set PNG palette to conserve memory
     407           1 :         GDALColorTable *poCT = GetColorTable();
     408           1 :         if (!poCT)
     409             :         {
     410           0 :             CPLError(CE_Failure, CPLE_NotSupported,
     411             :                      "MRF PPNG needs a color table");
     412           0 :             return CE_Failure;
     413             :         }
     414           1 :         ResetPalette(poCT, codec);
     415             :     }
     416             : 
     417          15 :     codec.deflate_flags = deflate_flags;
     418          15 :     return codec.CompressPNG(dst, src);
     419             : }
     420             : 
     421             : /**
     422             :  * \brief For PPNG, builds the data structures needed to write the palette
     423             :  * The presence of the PNGColors and PNGAlpha is used as a flag for PPNG only
     424             :  */
     425             : 
     426         155 : PNG_Band::PNG_Band(MRFDataset *pDS, const ILImage &image, int b, int level)
     427         155 :     : MRFRasterBand(pDS, image, b, level), codec(image)
     428             : {  // Check error conditions
     429         155 :     if (image.dt != GDT_Byte && image.dt != GDT_Int16 && image.dt != GDT_UInt16)
     430             :     {
     431          52 :         CPLError(CE_Failure, CPLE_NotSupported,
     432             :                  "Data type not supported by MRF PNG");
     433          52 :         return;
     434             :     }
     435         103 :     if (image.pagesize.c > 4)
     436             :     {
     437           1 :         CPLError(CE_Failure, CPLE_NotSupported,
     438             :                  "MRF PNG can only handle up to 4 bands per page");
     439           1 :         return;
     440             :     }
     441             :     // PNGs can be larger than the source, especially for small page size
     442             :     // If PPNG is used, the palette can take up to 2100 bytes
     443         102 :     poMRFDS->SetPBufferSize(
     444         102 :         static_cast<unsigned int>(1.1 * image.pageSizeBytes + 4000));
     445             : }
     446             : 
     447             : NAMESPACE_MRF_END

Generated by: LCOV version 1.14