LCOV - code coverage report
Current view: top level - frmts/jpeg - jpgdataset.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 22 22 100.0 %
Date: 2024-05-04 12:52:34 Functions: 12 14 85.7 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  * $Id: jpgdataset.cpp 37340 2017-02-11 18:28:02Z goatbar
       3             :  *
       4             :  * Project:  JPEG JFIF Driver
       5             :  * Purpose:  Implement GDAL JPEG Support based on IJG libjpeg.
       6             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2000, Frank Warmerdam
      10             :  * Copyright (c) 2007-2014, Even Rouault <even dot rouault at spatialys.com>
      11             :  *
      12             :  * Portions Copyright (c) Her majesty the Queen in right of Canada as
      13             :  * represented by the Minister of National Defence, 2006.
      14             :  *
      15             :  * Permission is hereby granted, free of charge, to any person obtaining a
      16             :  * copy of this software and associated documentation files (the "Software"),
      17             :  * to deal in the Software without restriction, including without limitation
      18             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      19             :  * and/or sell copies of the Software, and to permit persons to whom the
      20             :  * Software is furnished to do so, subject to the following conditions:
      21             :  *
      22             :  * The above copyright notice and this permission notice shall be included
      23             :  * in all copies or substantial portions of the Software.
      24             :  *
      25             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      26             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      27             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      28             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      29             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      30             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      31             :  * DEALINGS IN THE SOFTWARE.
      32             :  ****************************************************************************/
      33             : 
      34             : #include "cpl_port.h"
      35             : 
      36             : // TODO(schwehr): Run IWYU.
      37             : #include <cerrno>
      38             : #include <climits>
      39             : #include <cstddef>
      40             : #include <cstdio>
      41             : #include <cstdlib>
      42             : #include <cstring>
      43             : #if HAVE_FCNTL_H
      44             : #include <fcntl.h>
      45             : #endif
      46             : #include <setjmp.h>
      47             : 
      48             : #include <algorithm>
      49             : #include <string>
      50             : 
      51             : #include "cpl_conv.h"
      52             : #include "cpl_error.h"
      53             : #include "cpl_progress.h"
      54             : #include "cpl_string.h"
      55             : #include "cpl_vsi.h"
      56             : #include "gdal.h"
      57             : #include "gdal_frmts.h"
      58             : #include "gdal_pam.h"
      59             : #include "gdal_priv.h"
      60             : 
      61             : CPL_C_START
      62             : 
      63             : // So that D_LOSSLESS_SUPPORTED is visible if defined in jmorecfg of libjpeg-turbo >= 2.2
      64             : #define JPEG_INTERNAL_OPTIONS
      65             : 
      66             : #ifdef LIBJPEG_12_PATH
      67             : #include LIBJPEG_12_PATH
      68             : #else
      69             : #include "jpeglib.h"
      70             : #endif
      71             : CPL_C_END
      72             : #include "memdataset.h"
      73             : #include "vsidataio.h"
      74             : 
      75             : // TIFF header.
      76             : typedef struct
      77             : {
      78             :     GUInt16 tiff_magic;    // Magic number (defines byte order).
      79             :     GUInt16 tiff_version;  // TIFF version number.
      80             :     GUInt32 tiff_diroff;   // byte offset to first directory.
      81             : } TIFFHeader;
      82             : 
      83             : // Ok to use setjmp().
      84             : #ifdef _MSC_VER
      85             : #pragma warning(disable : 4611)
      86             : #endif
      87             : 
      88             : struct JPGDatasetOpenArgs
      89             : {
      90             :     const char *pszFilename = nullptr;
      91             :     VSILFILE *fpLin = nullptr;
      92             :     char **papszSiblingFiles = nullptr;
      93             :     int nScaleFactor = 1;
      94             :     bool bDoPAMInitialize = false;
      95             :     bool bUseInternalOverviews = false;
      96             :     bool bIsLossless = false;
      97             : };
      98             : 
      99             : class JPGDatasetCommon;
     100             : 
     101             : #if defined(JPEG_DUAL_MODE_8_12) && !defined(JPGDataset)
     102             : JPGDatasetCommon *JPEGDataset12Open(JPGDatasetOpenArgs *psArgs);
     103             : GDALDataset *JPEGDataset12CreateCopy(const char *pszFilename,
     104             :                                      GDALDataset *poSrcDS, int bStrict,
     105             :                                      char **papszOptions,
     106             :                                      GDALProgressFunc pfnProgress,
     107             :                                      void *pProgressData);
     108             : #endif
     109             : 
     110             : GDALRasterBand *JPGCreateBand(JPGDatasetCommon *poDS, int nBand);
     111             : 
     112             : typedef void (*my_jpeg_write_m_header)(void *cinfo, int marker,
     113             :                                        unsigned int datalen);
     114             : typedef void (*my_jpeg_write_m_byte)(void *cinfo, int val);
     115             : 
     116             : CPLErr JPGAppendMask(const char *pszJPGFilename, GDALRasterBand *poMask,
     117             :                      GDALProgressFunc pfnProgress, void *pProgressData);
     118             : void JPGAddEXIF(GDALDataType eWorkDT, GDALDataset *poSrcDS, char **papszOptions,
     119             :                 void *cinfo, my_jpeg_write_m_header p_jpeg_write_m_header,
     120             :                 my_jpeg_write_m_byte p_jpeg_write_m_byte,
     121             :                 GDALDataset *(pCreateCopy)(const char *, GDALDataset *, int,
     122             :                                            char **,
     123             :                                            GDALProgressFunc pfnProgress,
     124             :                                            void *pProgressData));
     125             : void JPGAddICCProfile(void *pInfo, const char *pszICCProfile,
     126             :                       my_jpeg_write_m_header p_jpeg_write_m_header,
     127             :                       my_jpeg_write_m_byte p_jpeg_write_m_byte);
     128             : 
     129             : class GDALJPEGUserData
     130             : {
     131             :   public:
     132             :     jmp_buf setjmp_buffer;
     133             :     bool bNonFatalErrorEncountered = false;
     134             :     void (*p_previous_emit_message)(j_common_ptr cinfo,
     135             :                                     int msg_level) = nullptr;
     136             :     int nMaxScans;
     137             : 
     138       10993 :     GDALJPEGUserData()
     139       21986 :         : nMaxScans(atoi(
     140       10993 :               CPLGetConfigOption("GDAL_JPEG_MAX_ALLOWED_SCAN_NUMBER", "100")))
     141             :     {
     142       10993 :         memset(&setjmp_buffer, 0, sizeof(setjmp_buffer));
     143       10993 :     }
     144             : };
     145             : 
     146             : /************************************************************************/
     147             : /* ==================================================================== */
     148             : /*                         JPGDatasetCommon                             */
     149             : /* ==================================================================== */
     150             : /************************************************************************/
     151             : 
     152             : class JPGRasterBand;
     153             : class JPGMaskBand;
     154             : 
     155             : class JPGDatasetCommon CPL_NON_FINAL : public GDALPamDataset
     156             : {
     157             :   protected:
     158             :     friend class JPGDataset;
     159             :     friend class JPGRasterBand;
     160             :     friend class JPGMaskBand;
     161             : 
     162             :     int nScaleFactor;
     163             :     bool bHasInitInternalOverviews;
     164             :     int nInternalOverviewsCurrent;
     165             :     int nInternalOverviewsToFree;
     166             :     GDALDataset **papoInternalOverviews;
     167             :     JPGDatasetCommon *poActiveDS = nullptr; /* only valid in parent DS */
     168             :     JPGDatasetCommon **ppoActiveDS =
     169             :         nullptr; /* &poActiveDS of poActiveDS from parentDS */
     170             :     void InitInternalOverviews();
     171             :     GDALDataset *InitEXIFOverview();
     172             : 
     173             :     OGRSpatialReference m_oSRS{};
     174             :     bool bGeoTransformValid;
     175             :     double adfGeoTransform[6];
     176             :     std::vector<gdal::GCP> m_aoGCPs{};
     177             : 
     178             :     VSILFILE *m_fpImage;
     179             :     GUIntBig nSubfileOffset;
     180             : 
     181             :     int nLoadedScanline;
     182             :     GByte *m_pabyScanline;
     183             : 
     184             :     bool bHasReadEXIFMetadata;
     185             :     bool bHasReadXMPMetadata;
     186             :     bool bHasReadICCMetadata;
     187             :     bool bHasReadFLIRMetadata = false;
     188             :     bool bHasReadImageStructureMetadata = false;
     189             :     char **papszMetadata;
     190             :     int nExifOffset;
     191             :     int nInterOffset;
     192             :     int nGPSOffset;
     193             :     bool bSwabflag;
     194             :     int nTiffDirStart;
     195             :     int nTIFFHEADER;
     196             :     bool bHasDoneJpegCreateDecompress;
     197             :     bool bHasDoneJpegStartDecompress;
     198             : 
     199             :     int m_nSubdatasetCount = 0;
     200             : 
     201             :     // FLIR raw thermal image
     202             :     bool m_bRawThermalLittleEndian = false;
     203             :     int m_nRawThermalImageWidth = 0;
     204             :     int m_nRawThermalImageHeight = 0;
     205             :     std::vector<GByte> m_abyRawThermalImage{};
     206             : 
     207             :     virtual CPLErr LoadScanline(int, GByte *outBuffer = nullptr) = 0;
     208             :     virtual void StopDecompress() = 0;
     209             :     virtual CPLErr Restart() = 0;
     210             : 
     211             :     virtual int GetDataPrecision() = 0;
     212             :     virtual int GetOutColorSpace() = 0;
     213             :     virtual int GetJPEGColorSpace() = 0;
     214             : 
     215             :     bool EXIFInit(VSILFILE *);
     216             :     void ReadICCProfile();
     217             : 
     218             :     void CheckForMask();
     219             :     void DecompressMask();
     220             : 
     221             :     void LoadForMetadataDomain(const char *pszDomain);
     222             : 
     223             :     void ReadImageStructureMetadata();
     224             :     void ReadEXIFMetadata();
     225             :     void ReadXMPMetadata();
     226             :     void ReadFLIRMetadata();
     227             :     GDALDataset *OpenFLIRRawThermalImage();
     228             : 
     229             :     bool bHasCheckedForMask;
     230             :     JPGMaskBand *poMaskBand;
     231             :     GByte *pabyBitMask;
     232             :     bool bMaskLSBOrder;
     233             : 
     234             :     GByte *pabyCMask;
     235             :     int nCMaskSize;
     236             : 
     237             :     // Color space exposed by GDAL.  Not necessarily the in_color_space nor
     238             :     // the out_color_space of JPEG library.
     239             :     /*J_COLOR_SPACE*/ int eGDALColorSpace;
     240             : 
     241             :     bool bIsSubfile;
     242             :     bool bHasTriedLoadWorldFileOrTab;
     243             :     void LoadWorldFileOrTab();
     244             :     CPLString osWldFilename;
     245             : 
     246             :     virtual int CloseDependentDatasets() override;
     247             : 
     248             :     virtual CPLErr IBuildOverviews(const char *, int, const int *, int,
     249             :                                    const int *, GDALProgressFunc, void *,
     250             :                                    CSLConstList papszOptions) override;
     251             : 
     252             :   public:
     253             :     JPGDatasetCommon();
     254             :     virtual ~JPGDatasetCommon();
     255             : 
     256             :     virtual CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
     257             :                              GDALDataType, int, int *, GSpacing nPixelSpace,
     258             :                              GSpacing nLineSpace, GSpacing nBandSpace,
     259             :                              GDALRasterIOExtraArg *psExtraArg) override;
     260             : 
     261             :     virtual CPLErr GetGeoTransform(double *) override;
     262             : 
     263             :     virtual int GetGCPCount() override;
     264             :     const OGRSpatialReference *GetGCPSpatialRef() const override;
     265             :     virtual const GDAL_GCP *GetGCPs() override;
     266             : 
     267             :     virtual char **GetMetadataDomainList() override;
     268             :     virtual char **GetMetadata(const char *pszDomain = "") override;
     269             :     virtual const char *GetMetadataItem(const char *pszName,
     270             :                                         const char *pszDomain = "") override;
     271             : 
     272             :     virtual char **GetFileList(void) override;
     273             : 
     274             :     virtual CPLErr FlushCache(bool bAtClosing) override;
     275             : 
     276             :     CPLStringList GetCompressionFormats(int nXOff, int nYOff, int nXSize,
     277             :                                         int nYSize, int nBandCount,
     278             :                                         const int *panBandList) override;
     279             :     CPLErr ReadCompressedData(const char *pszFormat, int nXOff, int nYOff,
     280             :                               int nXSize, int nYSize, int nBandCount,
     281             :                               const int *panBandList, void **ppBuffer,
     282             :                               size_t *pnBufferSize,
     283             :                               char **ppszDetailedFormat) override;
     284             : 
     285             :     static GDALDataset *Open(GDALOpenInfo *);
     286             : };
     287             : 
     288             : /************************************************************************/
     289             : /* ==================================================================== */
     290             : /*                              JPGDataset                              */
     291             : /* ==================================================================== */
     292             : /************************************************************************/
     293             : 
     294             : class JPGDataset final : public JPGDatasetCommon
     295             : {
     296             :     GDALJPEGUserData sUserData;
     297             : 
     298             :     bool ErrorOutOnNonFatalError();
     299             : 
     300             :     static void EmitMessage(j_common_ptr cinfo, int msg_level);
     301             :     static void ProgressMonitor(j_common_ptr cinfo);
     302             : 
     303             :     struct jpeg_decompress_struct sDInfo;
     304             :     struct jpeg_error_mgr sJErr;
     305             :     struct jpeg_progress_mgr sJProgress;
     306             : 
     307             :     virtual CPLErr LoadScanline(int, GByte *outBuffer) override;
     308             :     CPLErr StartDecompress();
     309             :     virtual void StopDecompress() override;
     310             :     virtual CPLErr Restart() override;
     311             : 
     312       24573 :     virtual int GetDataPrecision() override
     313             :     {
     314       24573 :         return sDInfo.data_precision;
     315             :     }
     316             : 
     317       71362 :     virtual int GetOutColorSpace() override
     318             :     {
     319       71362 :         return sDInfo.out_color_space;
     320             :     }
     321             : 
     322           3 :     virtual int GetJPEGColorSpace() override
     323             :     {
     324           3 :         return sDInfo.jpeg_color_space;
     325             :     }
     326             : 
     327             :     int nQLevel;
     328             : #if !defined(JPGDataset)
     329             :     void LoadDefaultTables(int);
     330             : #endif
     331             :     void SetScaleNumAndDenom();
     332             : 
     333             :     static JPGDatasetCommon *OpenStage2(JPGDatasetOpenArgs *psArgs,
     334             :                                         JPGDataset *&poDS);
     335             : 
     336             :   public:
     337             :     JPGDataset();
     338             :     virtual ~JPGDataset();
     339             : 
     340             :     static JPGDatasetCommon *Open(JPGDatasetOpenArgs *psArgs);
     341             :     static GDALDataset *CreateCopy(const char *pszFilename,
     342             :                                    GDALDataset *poSrcDS, int bStrict,
     343             :                                    char **papszOptions,
     344             :                                    GDALProgressFunc pfnProgress,
     345             :                                    void *pProgressData);
     346             :     static GDALDataset *CreateCopyStage2(
     347             :         const char *pszFilename, GDALDataset *poSrcDS, char **papszOptions,
     348             :         GDALProgressFunc pfnProgress, void *pProgressData, VSILFILE *fpImage,
     349             :         GDALDataType eDT, int nQuality, bool bAppendMask,
     350             :         GDALJPEGUserData &sUserData, struct jpeg_compress_struct &sCInfo,
     351             :         struct jpeg_error_mgr &sJErr, GByte *&pabyScanline);
     352             :     static void ErrorExit(j_common_ptr cinfo);
     353             :     static void OutputMessage(j_common_ptr cinfo);
     354             : };
     355             : 
     356             : /************************************************************************/
     357             : /* ==================================================================== */
     358             : /*                            JPGRasterBand                             */
     359             : /* ==================================================================== */
     360             : /************************************************************************/
     361             : 
     362             : class JPGRasterBand final : public GDALPamRasterBand
     363             : {
     364             :     friend class JPGDatasetCommon;
     365             : 
     366             :     // We have to keep a pointer to the JPGDataset that this JPGRasterBand
     367             :     // belongs to. In some case, we may have this->poGDS != this->poDS
     368             :     // For example for a JPGRasterBand that is set to a NITFDataset.
     369             :     // In other words, this->poDS doesn't necessary point to a JPGDataset
     370             :     // See ticket #1807.
     371             :     JPGDatasetCommon *poGDS;
     372             : 
     373             :   public:
     374             :     JPGRasterBand(JPGDatasetCommon *, int);
     375             : 
     376       48532 :     virtual ~JPGRasterBand()
     377       24266 :     {
     378       48532 :     }
     379             : 
     380             :     virtual CPLErr IReadBlock(int, int, void *) override;
     381             :     virtual GDALColorInterp GetColorInterpretation() override;
     382             : 
     383             :     virtual GDALSuggestedBlockAccessPattern
     384          18 :     GetSuggestedBlockAccessPattern() const override
     385             :     {
     386          18 :         return GSBAP_TOP_TO_BOTTOM;
     387             :     }
     388             : 
     389             :     virtual GDALRasterBand *GetMaskBand() override;
     390             :     virtual int GetMaskFlags() override;
     391             : 
     392             :     virtual GDALRasterBand *GetOverview(int i) override;
     393             :     virtual int GetOverviewCount() override;
     394             : };
     395             : 
     396             : #if !defined(JPGDataset)
     397             : 
     398             : /************************************************************************/
     399             : /* ==================================================================== */
     400             : /*                             JPGMaskBand                              */
     401             : /* ==================================================================== */
     402             : /************************************************************************/
     403             : 
     404             : class JPGMaskBand final : public GDALRasterBand
     405             : {
     406             :   protected:
     407             :     virtual CPLErr IReadBlock(int, int, void *) override;
     408             : 
     409             :   public:
     410             :     explicit JPGMaskBand(JPGDatasetCommon *poDS);
     411             : 
     412          30 :     virtual ~JPGMaskBand()
     413          15 :     {
     414          30 :     }
     415             : };
     416             : 
     417             : /************************************************************************/
     418             : /*                         GDALRegister_JPEG()                          */
     419             : /************************************************************************/
     420             : 
     421             : class GDALJPGDriver final : public GDALDriver
     422             : {
     423             :   public:
     424        1219 :     GDALJPGDriver()
     425        1219 :     {
     426        1219 :     }
     427             : 
     428             :     char **GetMetadata(const char *pszDomain = "") override;
     429             :     const char *GetMetadataItem(const char *pszName,
     430             :                                 const char *pszDomain = "") override;
     431             : };
     432             : 
     433             : #endif  // !defined(JPGDataset)

Generated by: LCOV version 1.14