1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/******************************************************************************
*
* Purpose: Implementation of the JPEG compression/decompression based
* on libjpeg. This implements functions suitable for use
* as jpeg interfaces in the PCIDSKInterfaces class.
*
******************************************************************************
* Copyright (c) 2009
* PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "pcidsk_config.h"
#include "pcidsk_types.h"
#include "core/pcidsk_utils.h"
#include "pcidsk_exception.h"
#include <cassert>
#include <cstdio>
using namespace PCIDSK;
#if defined(HAVE_LIBJPEG)
extern "C" {
#include "jpeglib.h"
}
static void _DummyMgrMethod( j_compress_ptr /*pUnused*/ ) {}
static void _DummySrcMgrMethod( j_decompress_ptr /*pUnused*/ ) {}
/************************************************************************/
/* JpegError() */
/* */
/* Handle errors generated by the IJG library. We treat all */
/* errors as fatal at this point. Future handling may be */
/* improved by overriding other methods. */
/************************************************************************/
static void JpegError(j_common_ptr cinfo)
{
char buf[256];
cinfo->err->format_message(cinfo, buf);
ThrowPCIDSKException( "%s", buf );
}
/************************************************************************/
/* LibJPEG_DecompressBlock() */
/************************************************************************/
void PCIDSK::LibJPEG_DecompressBlock(
uint8 *src_data, int src_bytes, uint8 *dst_data, int dst_bytes,
int xsize, int ysize, eChanType pixel_type )
{
struct jpeg_decompress_struct sJCompInfo;
struct jpeg_source_mgr sSrcMgr;
struct jpeg_error_mgr sErrMgr;
int i;
/* -------------------------------------------------------------------- */
/* Setup the buffer we will compress into. We make it pretty */
/* big to ensure there is space. The calling function will */
/* free it as soon as it is done so this shouldn't hurt much. */
/* -------------------------------------------------------------------- */
sSrcMgr.init_source = _DummySrcMgrMethod;
sSrcMgr.fill_input_buffer = (boolean (*)(j_decompress_ptr))
_DummyMgrMethod;
sSrcMgr.skip_input_data = (void (*)(j_decompress_ptr, long))
_DummyMgrMethod;
sSrcMgr.resync_to_restart = jpeg_resync_to_restart;
sSrcMgr.term_source = _DummySrcMgrMethod;
sSrcMgr.next_input_byte = src_data;
sSrcMgr.bytes_in_buffer = src_bytes;
/* -------------------------------------------------------------------- */
/* Setup JPEG Decompression */
/* -------------------------------------------------------------------- */
jpeg_create_decompress(&sJCompInfo);
sJCompInfo.src = &sSrcMgr;
sJCompInfo.err = jpeg_std_error(&sErrMgr);
sJCompInfo.err->output_message = JpegError;
/* -------------------------------------------------------------------- */
/* Read the header. */
/* -------------------------------------------------------------------- */
jpeg_read_header( &sJCompInfo, TRUE );
if (sJCompInfo.image_width != (unsigned int)xsize ||
sJCompInfo.image_height != (unsigned int)ysize)
{
ThrowPCIDSKException("Tile Size wrong in LibJPEG_DecompressTile(), got %dx%d, expected %dx%d.",
sJCompInfo.image_width,
sJCompInfo.image_height,
xsize, ysize );
}
sJCompInfo.out_color_space = JCS_GRAYSCALE;
jpeg_start_decompress(&sJCompInfo);
/* -------------------------------------------------------------------- */
/* Read each of the scanlines. */
/* -------------------------------------------------------------------- */
for( i = 0; i < ysize; i++ )
{
uint8 *line_data = dst_data + i*xsize;
jpeg_read_scanlines( &sJCompInfo, (JSAMPARRAY) &line_data, 1 );
}
/* -------------------------------------------------------------------- */
/* Cleanup. */
/* -------------------------------------------------------------------- */
jpeg_finish_decompress( &sJCompInfo );
jpeg_destroy_decompress( &sJCompInfo );
}
/************************************************************************/
/* LibJPEG_CompressBlock() */
/************************************************************************/
void PCIDSK::LibJPEG_CompressBlock(
uint8 *src_data, int src_bytes, uint8 *dst_data, int &dst_bytes,
int xsize, int ysize, eChanType pixel_type, int quality )
{
struct jpeg_compress_struct sJCompInfo;
struct jpeg_destination_mgr sDstMgr;
struct jpeg_error_mgr sErrMgr;
int i;
/* -------------------------------------------------------------------- */
/* Setup the buffer we will compress into. */
/* -------------------------------------------------------------------- */
sDstMgr.next_output_byte = dst_data;
sDstMgr.free_in_buffer = dst_bytes;
sDstMgr.init_destination = _DummyMgrMethod;
sDstMgr.empty_output_buffer = (boolean (*)(j_compress_ptr))
_DummyMgrMethod;
sDstMgr.term_destination = _DummyMgrMethod;
/* -------------------------------------------------------------------- */
/* Setup JPEG Compression */
/* -------------------------------------------------------------------- */
jpeg_create_compress(&sJCompInfo);
sJCompInfo.dest = &sDstMgr;
sJCompInfo.err = jpeg_std_error(&sErrMgr);
sJCompInfo.err->output_message = JpegError;
sJCompInfo.image_width = xsize;
sJCompInfo.image_height = ysize;
sJCompInfo.input_components = 1;
sJCompInfo.in_color_space = JCS_GRAYSCALE;
jpeg_set_defaults(&sJCompInfo);
jpeg_set_quality(&sJCompInfo, quality, TRUE );
jpeg_start_compress(&sJCompInfo, TRUE );
/* -------------------------------------------------------------------- */
/* Write all the scanlines at once. */
/* -------------------------------------------------------------------- */
for( i = 0; i < ysize; i++ )
{
uint8 *pabyLine = src_data + i*xsize;
jpeg_write_scanlines( &sJCompInfo, (JSAMPARRAY)&pabyLine, 1 );
}
/* -------------------------------------------------------------------- */
/* Cleanup. */
/* -------------------------------------------------------------------- */
jpeg_finish_compress( &sJCompInfo );
dst_bytes = dst_bytes - sDstMgr.free_in_buffer;
jpeg_destroy_compress( &sJCompInfo );
}
#endif /* defined(HAVE_LIBJPEG) */
|