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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/******************************************************************************
*
* Project: NITF Read/Write Translator
* Purpose: GDALDataset/GDALRasterBand implementation on top of "nitflib".
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2002, Frank Warmerdam
* Copyright (c) 2009-2010, Even Rouault <even dot rouault at spatialys.com>
*
* Portions Copyright (c) Her majesty the Queen in right of Canada as
* represented by the Minister of National Defence, 2006.
*
* 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.
****************************************************************************/
#ifdef JPEG_SUPPORTED
#include "cpl_port.h"
#include "gdal_pam.h"
CPL_C_START
#ifdef LIBJPEG_12_PATH
#include LIBJPEG_12_PATH
#else
#include "jpeglib.h"
#endif
CPL_C_END
#if defined(EXPECTED_JPEG_LIB_VERSION) && !defined(LIBJPEG_12_PATH)
#if EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION
#error EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION
#endif
#endif
/*
* Do we want to do special processing suitable for when JSAMPLE is a
* 16bit value?
*/
#if defined(JPEG_LIB_MK1)
#define JPEG_LIB_MK1_OR_12BIT 1
#elif BITS_IN_JSAMPLE == 12
#define JPEG_LIB_MK1_OR_12BIT 1
#endif
#if defined(JPEG_DUAL_MODE_8_12) && !defined(NITFWriteJPEGBlock)
int NITFWriteJPEGBlock_12(GDALDataset *poSrcDS, VSILFILE *fp, int nBlockXOff,
int nBlockYOff, int nBlockXSize, int nBlockYSize,
int bProgressive, int nQuality, const GByte *pabyAPP6,
int nRestartInterval, GDALProgressFunc pfnProgress,
void *pProgressData);
#endif
int NITFWriteJPEGBlock(GDALDataset *poSrcDS, VSILFILE *fp, int nBlockXOff,
int nBlockYOff, int nBlockXSize, int nBlockYSize,
int bProgressive, int nQuality, const GByte *pabyAPP6,
int nRestartInterval, GDALProgressFunc pfnProgress,
void *pProgressData);
void jpeg_vsiio_dest(j_compress_ptr cinfo, VSILFILE *outfile);
/************************************************************************/
/* NITFWriteJPEGBlock() */
/************************************************************************/
int NITFWriteJPEGBlock(GDALDataset *poSrcDS, VSILFILE *fp, int nBlockXOff,
int nBlockYOff, int nBlockXSize, int nBlockYSize,
int bProgressive, int nQuality, const GByte *pabyAPP6,
int nRestartInterval, GDALProgressFunc pfnProgress,
void *pProgressData)
{
GDALDataType eDT = poSrcDS->GetRasterBand(1)->GetRasterDataType();
#if defined(JPEG_DUAL_MODE_8_12) && !defined(NITFWriteJPEGBlock)
if (eDT == GDT_UInt16)
{
return NITFWriteJPEGBlock_12(poSrcDS, fp, nBlockXOff, nBlockYOff,
nBlockXSize, nBlockYSize, bProgressive,
nQuality, pabyAPP6, nRestartInterval,
pfnProgress, pProgressData);
}
#endif
int anBandList[3] = {1, 2, 3};
/* -------------------------------------------------------------------- */
/* Initialize JPG access to the file. */
/* -------------------------------------------------------------------- */
struct jpeg_compress_struct sCInfo;
struct jpeg_error_mgr sJErr;
memset(&sCInfo, 0, sizeof(sCInfo));
sCInfo.err = jpeg_std_error(&sJErr);
jpeg_create_compress(&sCInfo);
jpeg_vsiio_dest(&sCInfo, fp);
sCInfo.image_width = nBlockXSize;
sCInfo.image_height = nBlockYSize;
const int nBands = poSrcDS->GetRasterCount();
sCInfo.input_components = nBands;
if (nBands == 1)
{
sCInfo.in_color_space = JCS_GRAYSCALE;
}
else
{
sCInfo.in_color_space = JCS_RGB;
}
jpeg_set_defaults(&sCInfo);
#if defined(JPEG_LIB_MK1_OR_12BIT)
if (eDT == GDT_UInt16)
{
sCInfo.data_precision = 12;
}
else
{
sCInfo.data_precision = 8;
}
#endif
GDALDataType eWorkDT;
#ifdef JPEG_LIB_MK1
sCInfo.bits_in_jsample = sCInfo.data_precision;
eWorkDT = GDT_UInt16; /* Always force to 16 bit for JPEG_LIB_MK1 */
#else
eWorkDT = eDT;
#endif
sCInfo.write_JFIF_header = FALSE;
/* Set the restart interval */
if (nRestartInterval < 0)
{
/* nRestartInterval < 0 means that we will guess the value */
/* so we set it at the maximum allowed by MIL-STD-188-198 */
/* that is to say the number of MCU per row-block */
nRestartInterval = nBlockXSize / 8;
}
if (nRestartInterval > 0)
sCInfo.restart_interval = nRestartInterval;
jpeg_set_quality(&sCInfo, nQuality, TRUE);
if (bProgressive)
jpeg_simple_progression(&sCInfo);
jpeg_start_compress(&sCInfo, TRUE);
/* -------------------------------------------------------------------- */
/* Emits APP6 NITF application segment (required by MIL-STD-188-198) */
/* -------------------------------------------------------------------- */
if (pabyAPP6)
{
/* 0xe6 = APP6 marker */
jpeg_write_marker(&sCInfo, 0xe6, (const JOCTET *)pabyAPP6, 23);
}
/* -------------------------------------------------------------------- */
/* Loop over image, copying image data. */
/* -------------------------------------------------------------------- */
const int nWorkDTSize = GDALGetDataTypeSizeBytes(eWorkDT);
GByte *pabyScanline = reinterpret_cast<GByte *>(
CPLMalloc(nBands * nBlockXSize * nWorkDTSize));
const int nXSize = poSrcDS->GetRasterXSize();
const int nYSize = poSrcDS->GetRasterYSize();
const double nTotalPixels = static_cast<double>(nXSize * nYSize);
int nBlockXSizeToRead = nBlockXSize;
if (nBlockXSize * nBlockXOff + nBlockXSize > nXSize)
{
nBlockXSizeToRead = nXSize - nBlockXSize * nBlockXOff;
}
int nBlockYSizeToRead = nBlockYSize;
if (nBlockYSize * nBlockYOff + nBlockYSize > nYSize)
{
nBlockYSizeToRead = nYSize - nBlockYSize * nBlockYOff;
}
#if defined(JPEG_LIB_MK1_OR_12BIT)
bool bClipWarn = false;
#endif
CPLErr eErr = CE_None;
for (int iLine = 0; iLine < nBlockYSize && eErr == CE_None; iLine++)
{
if (iLine < nBlockYSizeToRead)
{
eErr = poSrcDS->RasterIO(
GF_Read, nBlockXSize * nBlockXOff,
iLine + nBlockYSize * nBlockYOff, nBlockXSizeToRead, 1,
pabyScanline, nBlockXSizeToRead, 1, eWorkDT, nBands, anBandList,
nBands * nWorkDTSize, nBands * nBlockXSize * nWorkDTSize,
nWorkDTSize, nullptr);
#if !defined(JPEG_LIB_MK1_OR_12BIT)
/* Repeat the last pixel till the end of the line */
/* to minimize discontinuity */
if (nBlockXSizeToRead < nBlockXSize)
{
for (int iBand = 0; iBand < nBands; iBand++)
{
GByte bVal =
pabyScanline[nBands * (nBlockXSizeToRead - 1) + iBand];
for (int iX = nBlockXSizeToRead; iX < nBlockXSize; iX++)
{
pabyScanline[nBands * iX + iBand] = bVal;
}
}
}
#endif
}
#if defined(JPEG_LIB_MK1_OR_12BIT)
// clamp 16bit values to 12bit.
if (eDT == GDT_UInt16)
{
GUInt16 *panScanline = reinterpret_cast<GUInt16 *>(pabyScanline);
for (int iPixel = 0; iPixel < nXSize * nBands; iPixel++)
{
if (panScanline[iPixel] > 4095)
{
panScanline[iPixel] = 4095;
if (!bClipWarn)
{
bClipWarn = true;
CPLError(CE_Warning, CPLE_AppDefined,
"One or more pixels clipped to fit 12bit "
"domain for jpeg output.");
}
}
}
}
#endif
JSAMPLE *ppSamples = reinterpret_cast<JSAMPLE *>(pabyScanline);
if (eErr == CE_None)
jpeg_write_scanlines(&sCInfo, &ppSamples, 1);
double nCurPixels =
static_cast<double>(nBlockYOff) * nBlockYSize * nXSize +
static_cast<double>(nBlockXOff) * nBlockYSize * nBlockXSize +
(iLine + 1) * nBlockXSizeToRead;
if (eErr == CE_None &&
!pfnProgress(nCurPixels / nTotalPixels, nullptr, pProgressData))
{
eErr = CE_Failure;
CPLError(CE_Failure, CPLE_UserInterrupt,
"User terminated CreateCopy()");
}
}
/* -------------------------------------------------------------------- */
/* Cleanup and close. */
/* -------------------------------------------------------------------- */
CPLFree(pabyScanline);
if (eErr == CE_None)
jpeg_finish_compress(&sCInfo);
jpeg_destroy_compress(&sCInfo);
return eErr == CE_None;
}
#endif /* def JPEG_SUPPORTED */
|