LCOV - code coverage report
Current view: top level - frmts/pcidsk/sdk/core - libjpeg_io.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 48 60 80.0 %
Date: 2024-05-03 15:49:35 Functions: 4 8 50.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Purpose:  Implementation of the JPEG compression/decompression based
       4             :  *           on libjpeg.  This implements functions suitable for use
       5             :  *           as jpeg interfaces in the PCIDSKInterfaces class.
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2009
       9             :  * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada.
      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 "pcidsk_config.h"
      31             : #include "pcidsk_types.h"
      32             : #include "core/pcidsk_utils.h"
      33             : #include "pcidsk_exception.h"
      34             : #include <cassert>
      35             : #include <cstdio>
      36             : 
      37             : using namespace PCIDSK;
      38             : 
      39             : #if defined(HAVE_LIBJPEG)
      40             : 
      41             : extern "C" {
      42             : #include "jpeglib.h"
      43             : }
      44             : 
      45           2 : static void _DummyMgrMethod( j_compress_ptr /*pUnused*/ ) {}
      46           2 : static void _DummySrcMgrMethod( j_decompress_ptr /*pUnused*/ ) {}
      47           0 : static boolean _DummyFillInputBuffer(j_decompress_ptr) { return 0; }
      48           0 : static void _DummySkipInputData(j_decompress_ptr, long) {}
      49           0 : static boolean _DummyEmptyOutputBuffer(j_compress_ptr) { return 0; }
      50             : 
      51             : /************************************************************************/
      52             : /*                             JpegError()                              */
      53             : /*                                                                      */
      54             : /*      Handle errors generated by the IJG library.  We treat all       */
      55             : /*      errors as fatal at this point.  Future handling may be          */
      56             : /*      improved by overriding other methods.                           */
      57             : /************************************************************************/
      58             : 
      59           0 : static void JpegError(j_common_ptr cinfo)
      60             : {
      61             :     char buf[256];
      62             : 
      63           0 :     cinfo->err->format_message(cinfo, buf);
      64             : 
      65             :     // Make sure we destroy the context before throwing an exception.
      66           0 :     if (cinfo->is_decompressor)
      67           0 :         jpeg_destroy_decompress((j_decompress_ptr) cinfo);
      68             :     else
      69           0 :         jpeg_destroy_compress((j_compress_ptr) cinfo);
      70             : 
      71           0 :     return ThrowPCIDSKException( "%s", buf );
      72             : }
      73             : 
      74             : /************************************************************************/
      75             : /*                      LibJPEG_DecompressBlock()                       */
      76             : /************************************************************************/
      77             : 
      78           1 : void PCIDSK::LibJPEG_DecompressBlock(
      79             :     uint8 *src_data, int src_bytes, uint8 *dst_data, CPL_UNUSED int dst_bytes,
      80             :     int xsize, int ysize, eChanType CPL_UNUSED pixel_type )
      81             : {
      82             :     struct jpeg_decompress_struct sJCompInfo;
      83             :     struct jpeg_source_mgr        sSrcMgr;
      84             :     struct jpeg_error_mgr         sErrMgr;
      85             : 
      86             :     int i;
      87             : 
      88             : /* -------------------------------------------------------------------- */
      89             : /*      Setup the buffer we will compress into.  We make it pretty      */
      90             : /*      big to ensure there is space.  The calling function will        */
      91             : /*      free it as soon as it is done so this should not hurt much.     */
      92             : /* -------------------------------------------------------------------- */
      93           1 :     sSrcMgr.init_source = _DummySrcMgrMethod;
      94           1 :     sSrcMgr.fill_input_buffer = _DummyFillInputBuffer;
      95           1 :     sSrcMgr.skip_input_data = _DummySkipInputData;
      96           1 :     sSrcMgr.resync_to_restart = jpeg_resync_to_restart;
      97           1 :     sSrcMgr.term_source = _DummySrcMgrMethod;
      98             : 
      99           1 :     sSrcMgr.next_input_byte = src_data;
     100           1 :     sSrcMgr.bytes_in_buffer = src_bytes;
     101             : 
     102             : /* -------------------------------------------------------------------- */
     103             : /*      Setup JPEG Decompression                                        */
     104             : /* -------------------------------------------------------------------- */
     105           1 :     jpeg_create_decompress(&sJCompInfo);
     106             : 
     107           1 :     sJCompInfo.src = &sSrcMgr;
     108           1 :     sJCompInfo.err = jpeg_std_error(&sErrMgr);
     109           1 :     sJCompInfo.err->output_message = JpegError;
     110             : 
     111             : /* -------------------------------------------------------------------- */
     112             : /*      Read the header.                                                */
     113             : /* -------------------------------------------------------------------- */
     114           1 :     jpeg_read_header( &sJCompInfo, TRUE );
     115           1 :     if (sJCompInfo.image_width != (unsigned int)xsize ||
     116           1 :         sJCompInfo.image_height != (unsigned int)ysize)
     117             :     {
     118           0 :         jpeg_destroy_decompress( &sJCompInfo );
     119             : 
     120           0 :         return ThrowPCIDSKException("Tile Size wrong in LibJPEG_DecompressTile(), got %dx%d, expected %dx%d.",
     121             :                              sJCompInfo.image_width,
     122             :                              sJCompInfo.image_height,
     123           0 :                              xsize, ysize );
     124             :     }
     125             : 
     126           1 :     sJCompInfo.out_color_space = JCS_GRAYSCALE;
     127           1 :     jpeg_start_decompress(&sJCompInfo);
     128             : 
     129             : /* -------------------------------------------------------------------- */
     130             : /*      Read each of the scanlines.                                     */
     131             : /* -------------------------------------------------------------------- */
     132         257 :     for( i = 0; i < ysize; i++ )
     133             :     {
     134         256 :         uint8   *line_data = dst_data + i*xsize;
     135         256 :         jpeg_read_scanlines( &sJCompInfo, (JSAMPARRAY) &line_data, 1 );
     136             :     }
     137             : 
     138             : /* -------------------------------------------------------------------- */
     139             : /*      Cleanup.                                                        */
     140             : /* -------------------------------------------------------------------- */
     141           1 :     jpeg_finish_decompress( &sJCompInfo );
     142           1 :     jpeg_destroy_decompress( &sJCompInfo );
     143             : }
     144             : 
     145             : /************************************************************************/
     146             : /*                      LibJPEG_CompressBlock()                         */
     147             : /************************************************************************/
     148             : 
     149           1 : void PCIDSK::LibJPEG_CompressBlock(
     150             :     uint8 *src_data, CPL_UNUSED int src_bytes, uint8 *dst_data, int &dst_bytes,
     151             :     int xsize, int ysize, CPL_UNUSED eChanType pixel_type, int quality )
     152             : {
     153             :     struct jpeg_compress_struct sJCompInfo;
     154             :     struct jpeg_destination_mgr sDstMgr;
     155             :     struct jpeg_error_mgr       sErrMgr;
     156             : 
     157             :     int     i;
     158             : 
     159             : /* -------------------------------------------------------------------- */
     160             : /*      Setup the buffer we will compress into.                         */
     161             : /* -------------------------------------------------------------------- */
     162           1 :     sDstMgr.next_output_byte = dst_data;
     163           1 :     sDstMgr.free_in_buffer = dst_bytes;
     164           1 :     sDstMgr.init_destination = _DummyMgrMethod;
     165           1 :     sDstMgr.empty_output_buffer = _DummyEmptyOutputBuffer;
     166           1 :     sDstMgr.term_destination = _DummyMgrMethod;
     167             : 
     168             : /* -------------------------------------------------------------------- */
     169             : /*      Setup JPEG Compression                                          */
     170             : /* -------------------------------------------------------------------- */
     171           1 :     jpeg_create_compress(&sJCompInfo);
     172             : 
     173           1 :     sJCompInfo.dest = &sDstMgr;
     174           1 :     sJCompInfo.err = jpeg_std_error(&sErrMgr);
     175           1 :     sJCompInfo.err->output_message = JpegError;
     176             : 
     177           1 :     sJCompInfo.image_width = xsize;
     178           1 :     sJCompInfo.image_height = ysize;
     179           1 :     sJCompInfo.input_components = 1;
     180           1 :     sJCompInfo.in_color_space = JCS_GRAYSCALE;
     181             : 
     182           1 :     jpeg_set_defaults(&sJCompInfo);
     183           1 :     jpeg_set_quality(&sJCompInfo, quality, TRUE );
     184           1 :     jpeg_start_compress(&sJCompInfo, TRUE );
     185             : 
     186             : /* -------------------------------------------------------------------- */
     187             : /*      Write all the scanlines at once.                                */
     188             : /* -------------------------------------------------------------------- */
     189         257 :     for( i = 0; i < ysize; i++ )
     190             :     {
     191         256 :         uint8   *pabyLine = src_data + i*xsize;
     192             : 
     193         256 :         jpeg_write_scanlines( &sJCompInfo, (JSAMPARRAY)&pabyLine, 1 );
     194             :     }
     195             : 
     196             : /* -------------------------------------------------------------------- */
     197             : /*      Cleanup.                                                        */
     198             : /* -------------------------------------------------------------------- */
     199           1 :     jpeg_finish_compress( &sJCompInfo );
     200             : 
     201           1 :     dst_bytes = static_cast<int>(dst_bytes - sDstMgr.free_in_buffer);
     202             : 
     203           1 :     jpeg_destroy_compress( &sJCompInfo );
     204           1 : }
     205             : 
     206             : #endif /* defined(HAVE_LIBJPEG) */

Generated by: LCOV version 1.14