LCOV - code coverage report
Current view: directory - frmts/pcidsk/sdk/core - libjpeg_io.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 53 48 90.6 %
Date: 2012-12-26 Functions: 5 4 80.0 %

       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, 50 West Wilmot Street, Richmond Hill, Ont, 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                 : 
      48                 : /************************************************************************/
      49                 : /*                             JpegError()                              */
      50                 : /*                                                                      */
      51                 : /*      Handle errors generated by the IJG library.  We treat all       */
      52                 : /*      errors as fatal at this point.  Future handling may be          */
      53                 : /*      improved by overriding other methods.                           */
      54                 : /************************************************************************/
      55                 : 
      56               0 : static void JpegError(j_common_ptr cinfo)
      57                 : {
      58                 :     char buf[256];
      59                 : 
      60               0 :     cinfo->err->format_message(cinfo, buf);
      61               0 :     ThrowPCIDSKException( "%s", buf );
      62               0 : }
      63                 : 
      64                 : /************************************************************************/
      65                 : /*                      LibJPEG_DecompressBlock()                       */
      66                 : /************************************************************************/
      67                 : 
      68               1 : void PCIDSK::LibJPEG_DecompressBlock(
      69                 :     uint8 *src_data, int src_bytes, uint8 *dst_data, int dst_bytes,
      70                 :     int xsize, int ysize, eChanType pixel_type )
      71                 : 
      72                 : {
      73                 :     struct jpeg_decompress_struct sJCompInfo;
      74                 :     struct jpeg_source_mgr  sSrcMgr;
      75                 :     struct jpeg_error_mgr sErrMgr;
      76                 : 
      77                 :     int       i;
      78                 : 
      79                 : /* -------------------------------------------------------------------- */
      80                 : /*      Setup the buffer we will compress into.  We make it pretty      */
      81                 : /*      big to ensure there is space.  The calling function will        */
      82                 : /*      free it as soon as it is done so this shouldn't hurt much.      */
      83                 : /* -------------------------------------------------------------------- */
      84               1 :     sSrcMgr.init_source = _DummySrcMgrMethod;
      85                 :     sSrcMgr.fill_input_buffer = (boolean (*)(j_decompress_ptr))
      86               1 :                     _DummyMgrMethod;
      87                 :     sSrcMgr.skip_input_data = (void (*)(j_decompress_ptr, long))
      88               1 :                     _DummyMgrMethod;
      89               1 :     sSrcMgr.resync_to_restart = jpeg_resync_to_restart;
      90               1 :     sSrcMgr.term_source = _DummySrcMgrMethod;
      91                 : 
      92               1 :     sSrcMgr.next_input_byte = src_data;
      93               1 :     sSrcMgr.bytes_in_buffer = src_bytes;
      94                 :     
      95                 : /* -------------------------------------------------------------------- */
      96                 : /*      Setup JPEG Decompression                                        */
      97                 : /* -------------------------------------------------------------------- */
      98               1 :     jpeg_create_decompress(&sJCompInfo);
      99                 : 
     100               1 :     sJCompInfo.src = &sSrcMgr;
     101               1 :     sJCompInfo.err = jpeg_std_error(&sErrMgr);
     102               1 :     sJCompInfo.err->output_message = JpegError;
     103                 : 
     104                 : /* -------------------------------------------------------------------- */
     105                 : /*      Read the header.                                                */
     106                 : /* -------------------------------------------------------------------- */
     107               1 :     jpeg_read_header( &sJCompInfo, TRUE );
     108               1 :     if (sJCompInfo.image_width != (unsigned int)xsize ||
     109                 :         sJCompInfo.image_height != (unsigned int)ysize)
     110                 :     {
     111                 :         ThrowPCIDSKException("Tile Size wrong in LibJPEG_DecompressTile(), got %dx%d, expected %dx%d.",
     112                 :                              sJCompInfo.image_width,
     113                 :                              sJCompInfo.image_height,
     114               0 :                              xsize, ysize );
     115                 :     }
     116                 : 
     117               1 :     sJCompInfo.out_color_space = JCS_GRAYSCALE;
     118                 : 
     119               1 :     jpeg_start_decompress(&sJCompInfo);
     120                 :     
     121                 : /* -------------------------------------------------------------------- */
     122                 : /*  Read each of the scanlines.         */
     123                 : /* -------------------------------------------------------------------- */
     124             128 :     for( i = 0; i < ysize; i++ )
     125                 :     {
     126             127 :         uint8 *line_data = dst_data + i*xsize;
     127                 :         
     128             127 :         jpeg_read_scanlines( &sJCompInfo, (JSAMPARRAY) &line_data, 1 );
     129                 :     }
     130                 : 
     131                 : /* -------------------------------------------------------------------- */
     132                 : /*      Cleanup.                                                        */
     133                 : /* -------------------------------------------------------------------- */
     134               1 :     jpeg_finish_decompress( &sJCompInfo );
     135               1 :     jpeg_destroy_decompress( &sJCompInfo );
     136               1 : }
     137                 : 
     138                 : /************************************************************************/
     139                 : /*                      LibJPEG_CompressBlock()                         */
     140                 : /************************************************************************/
     141                 : 
     142               1 : void PCIDSK::LibJPEG_CompressBlock(
     143                 :     uint8 *src_data, int src_bytes, uint8 *dst_data, int &dst_bytes,
     144                 :     int xsize, int ysize, eChanType pixel_type, int quality )
     145                 : 
     146                 : {
     147                 :     struct jpeg_compress_struct sJCompInfo;
     148                 :     struct jpeg_destination_mgr sDstMgr;
     149                 :     struct jpeg_error_mgr sErrMgr;
     150                 : 
     151                 :     int   i;
     152                 : 
     153                 : /* -------------------------------------------------------------------- */
     154                 : /*      Setup the buffer we will compress into.                         */
     155                 : /* -------------------------------------------------------------------- */
     156               1 :     sDstMgr.next_output_byte = dst_data;
     157               1 :     sDstMgr.free_in_buffer = dst_bytes;
     158               1 :     sDstMgr.init_destination = _DummyMgrMethod;
     159                 :     sDstMgr.empty_output_buffer = (boolean (*)(j_compress_ptr))
     160               1 :                     _DummyMgrMethod;
     161               1 :     sDstMgr.term_destination = _DummyMgrMethod;
     162                 :     
     163                 : /* -------------------------------------------------------------------- */
     164                 : /*      Setup JPEG Compression                                          */
     165                 : /* -------------------------------------------------------------------- */
     166               1 :     jpeg_create_compress(&sJCompInfo);
     167                 : 
     168               1 :     sJCompInfo.dest = &sDstMgr;
     169               1 :     sJCompInfo.err = jpeg_std_error(&sErrMgr);
     170               1 :     sJCompInfo.err->output_message = JpegError;
     171                 :     
     172               1 :     sJCompInfo.image_width = xsize;
     173               1 :     sJCompInfo.image_height = ysize;
     174               1 :     sJCompInfo.input_components = 1;
     175               1 :     sJCompInfo.in_color_space = JCS_GRAYSCALE;
     176                 : 
     177               1 :     jpeg_set_defaults(&sJCompInfo);
     178               1 :     jpeg_set_quality(&sJCompInfo, quality, TRUE );
     179               1 :     jpeg_start_compress(&sJCompInfo, TRUE );
     180                 :     
     181                 : /* -------------------------------------------------------------------- */
     182                 : /*  Write all the scanlines at once.        */
     183                 : /* -------------------------------------------------------------------- */
     184             128 :     for( i = 0; i < ysize; i++ )
     185                 :     {
     186             127 :         uint8 *pabyLine = src_data + i*xsize;
     187                 :         
     188             127 :         jpeg_write_scanlines( &sJCompInfo, (JSAMPARRAY)&pabyLine, 1 );
     189                 :     }
     190                 :     
     191                 : /* -------------------------------------------------------------------- */
     192                 : /*  Cleanup.              */
     193                 : /* -------------------------------------------------------------------- */
     194               1 :     jpeg_finish_compress( &sJCompInfo );
     195                 : 
     196               1 :     dst_bytes = dst_bytes - sDstMgr.free_in_buffer;
     197                 :     
     198               1 :     jpeg_destroy_compress( &sJCompInfo );
     199               1 : }
     200                 : 
     201                 : #endif /* defined(HAVE_LIBJPEG) */

Generated by: LCOV version 1.7