• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OSGeo / gdal / 13899354245

17 Mar 2025 12:24PM UTC coverage: 70.457% (+0.02%) from 70.436%
13899354245

Pull #11951

github

web-flow
Merge c67e11463 into 05516a020
Pull Request #11951: Doc: Build docs using CMake

554033 of 786337 relevant lines covered (70.46%)

220670.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.89
/frmts/mrf/PNG_band.cpp
1
/*
2
 * Copyright (c) 2002-2012, California Institute of Technology.
3
 * All rights reserved.  Based on Government Sponsored Research under contracts
4
 * NAS7-1407 and/or NAS7-03001.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *   1. Redistributions of source code must retain the above copyright notice,
9
 * this list of conditions and the following disclaimer.
10
 *   2. Redistributions in binary form must reproduce the above copyright
11
 * notice, this list of conditions and the following disclaimer in the
12
 * documentation and/or other materials provided with the distribution.
13
 *   3. Neither the name of the California Institute of Technology (Caltech),
14
 * its operating division the Jet Propulsion Laboratory (JPL), the National
15
 * Aeronautics and Space Administration (NASA), nor the names of its
16
 * contributors may be used to endorse or promote products derived from this
17
 * software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE CALIFORNIA INSTITUTE OF TECHNOLOGY BE
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 *
31
 * Copyright (c) 2014-2021 Esri
32
 *
33
 * Licensed under the Apache License, Version 2.0 (the "License");
34
 * you may not use this file except in compliance with the License.
35
 * You may obtain a copy of the License at
36
 *
37
 * http://www.apache.org/licenses/LICENSE-2.0
38
 *
39
 * Unless required by applicable law or agreed to in writing, software
40
 * distributed under the License is distributed on an "AS IS" BASIS,
41
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42
 * See the License for the specific language governing permissions and
43
 * limitations under the License.
44
 *
45
 * Author: Lucian Plesea
46
 *
47
 */
48

49
/*
50
 * PNG band
51
 * PNG page compression and decompression functions
52
 * These functions are not methods, they reside in the global space
53
 *
54
 */
55

56
#include "marfa.h"
57
#include <cassert>
58
#include <algorithm>
59

60
CPL_C_START
61
#include <png.h>
62
CPL_C_END
63

64
NAMESPACE_MRF_START
65

66
// Do Nothing
67
static void flush_png(png_structp)
×
68
{
69
}
×
70

71
// Warning Emit
72
static void pngWH(png_struct * /*png*/, png_const_charp message)
×
73
{
74
    CPLError(CE_Warning, CPLE_AppDefined, "MRF: PNG warning %s", message);
×
75
}
×
76

77
// Fatal Warning
78
static void pngEH(png_struct *png, png_const_charp message)
×
79
{
80
    CPLError(CE_Failure, CPLE_AppDefined, "MRF: PNG Failure %s", message);
×
81
    longjmp(png_jmpbuf(png), 1);
×
82
}
83

84
// Read memory handlers for PNG
85
// No check for attempting to read past the end of the buffer
86

87
static void read_png(png_structp pngp, png_bytep data, png_size_t length)
321✔
88
{
89
    buf_mgr *pmgr = (buf_mgr *)png_get_io_ptr(pngp);
321✔
90
    if (pmgr->size < length)
321✔
91
    {
92
        CPLError(CE_Failure, CPLE_AppDefined,
×
93
                 "MRF: PNG Failure: Not enough bytes in buffer");
94
        longjmp(png_jmpbuf(pngp), 1);
×
95
    }
96
    memcpy(data, pmgr->buffer, length);
321✔
97
    pmgr->buffer += length;
321✔
98
    pmgr->size -= length;
321✔
99
}
321✔
100

101
static void write_png(png_structp pngp, png_bytep data, png_size_t length)
420✔
102
{
103
    buf_mgr *mgr = (buf_mgr *)png_get_io_ptr(pngp);
420✔
104
    // Buffer could be too small, trigger an error on debug mode
105
    assert(length <= mgr->size);
420✔
106
    memcpy(mgr->buffer, data, length);
420✔
107
    mgr->buffer += length;
420✔
108
    mgr->size -= length;
420✔
109
}
420✔
110

111
/**
112
 *\brief In memory decompression of PNG file
113
 */
114

115
CPLErr PNG_Codec::DecompressPNG(buf_mgr &dst, buf_mgr &src)
11✔
116
{
117
    const buf_mgr src_ori = src;
11✔
118
    png_bytep *png_rowp = nullptr;
11✔
119
    volatile png_bytep *p_volatile_png_rowp = (volatile png_bytep *)&png_rowp;
11✔
120

121
    // pngp=png_create_read_struct(PNG_LIBPNG_VER_STRING,0,pngEH,pngWH);
122
    png_structp pngp = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
11✔
123
                                              nullptr, nullptr);
11✔
124
    if (nullptr == pngp)
11✔
125
    {
126
        CPLError(CE_Failure, CPLE_AppDefined,
×
127
                 "MRF: Error creating PNG decompress");
128
        return CE_Failure;
×
129
    }
130

131
    png_infop infop = png_create_info_struct(pngp);
11✔
132
    if (nullptr == infop)
11✔
133
    {
134
        png_destroy_read_struct(&pngp, &infop, nullptr);
×
135
        CPLError(CE_Failure, CPLE_AppDefined, "MRF: Error creating PNG info");
×
136
        return CE_Failure;
×
137
    }
138

139
    if (setjmp(png_jmpbuf(pngp)))
11✔
140
    {
141
        CPLError(CE_Failure, CPLE_AppDefined,
×
142
                 "MRF: Error during PNG decompress");
143
        CPLFree((void *)(*p_volatile_png_rowp));
×
144
        png_destroy_read_struct(&pngp, &infop, nullptr);
×
145
        return CE_Failure;
×
146
    }
147

148
    // The mgr data ptr is already set up
149
    png_set_read_fn(pngp, &src, read_png);
11✔
150
    // Ready to read
151
    png_read_info(pngp, infop);
11✔
152

153
    if (png_get_bit_depth(pngp, infop) == 8)
11✔
154
    {
155
        // Use the PNG driver for decompression of 8-bit images, as it
156
        // has optimizations for whole image decompression.
157
        const CPLString osTmpFilename(VSIMemGenerateHiddenFilename("mrf.png"));
9✔
158
        VSIFCloseL(VSIFileFromMemBuffer(
9✔
159
            osTmpFilename.c_str(), reinterpret_cast<GByte *>(src_ori.buffer),
9✔
160
            src_ori.size, false));
9✔
161
        const char *const apszAllowedDrivers[] = {"PNG", nullptr};
9✔
162
        auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
163
            osTmpFilename.c_str(), GDAL_OF_RASTER, apszAllowedDrivers));
9✔
164
        if (poDS && static_cast<GUIntBig>(poDS->GetRasterXSize()) *
18✔
165
                            poDS->GetRasterYSize() * poDS->GetRasterCount() ==
9✔
166
                        dst.size)
18✔
167
        {
168
            if (poDS->RasterIO(
27✔
169
                    GF_Read, 0, 0, poDS->GetRasterXSize(),
170
                    poDS->GetRasterYSize(), dst.buffer, poDS->GetRasterXSize(),
9✔
171
                    poDS->GetRasterYSize(), GDT_Byte, poDS->GetRasterCount(),
172
                    nullptr, poDS->GetRasterCount(), 0, 1, nullptr) == CE_None)
18✔
173
            {
174
                png_destroy_read_struct(&pngp, &infop, nullptr);
9✔
175
                VSIUnlink(osTmpFilename.c_str());
9✔
176
                return CE_None;
9✔
177
            }
178
        }
179
        VSIUnlink(osTmpFilename.c_str());
×
180
    }
181

182
    GInt32 height = static_cast<GInt32>(png_get_image_height(pngp, infop));
2✔
183
    // Check the size
184
    if (dst.size < (png_get_rowbytes(pngp, infop) * height))
2✔
185
    {
186
        CPLError(CE_Failure, CPLE_AppDefined,
×
187
                 "MRF: PNG Page data bigger than the buffer provided");
188
        png_destroy_read_struct(&pngp, &infop, nullptr);
×
189
        return CE_Failure;
×
190
    }
191

192
    png_rowp = (png_bytep *)CPLMalloc(sizeof(png_bytep) * height);
2✔
193

194
    int rowbytes = static_cast<int>(png_get_rowbytes(pngp, infop));
2✔
195
    for (int i = 0; i < height; i++)
1,026✔
196
        png_rowp[i] = (png_bytep)dst.buffer + i * rowbytes;
1,024✔
197

198
#if defined(CPL_LSB)
199
    if (png_get_bit_depth(pngp, infop) > 8)
2✔
200
    {
201
        png_set_swap(pngp);
2✔
202
        // Call update info if any png_set is used
203
        png_read_update_info(pngp, infop);
2✔
204
    }
205
#endif
206

207
    // Finally, the read
208
    // This is the lower level, the png_read_end allows some transforms
209
    // Like palette to RGBA
210
    png_read_image(pngp, png_rowp);
2✔
211

212
    //    ppmWrite("Test.ppm",(char *)data,ILSize(512,512,1,4,0));
213
    // Required
214
    png_read_end(pngp, infop);
2✔
215

216
    // png_set_rows(pngp,infop,png_rowp);
217
    // png_read_png(pngp,infop,PNG_TRANSFORM_IDENTITY,0);
218

219
    CPLFree(png_rowp);
2✔
220
    png_destroy_read_struct(&pngp, &infop, nullptr);
2✔
221
    return CE_None;
2✔
222
}
223

224
/**
225
 *\brief Compress a page in PNG format
226
 * Returns the compressed size in dst.size
227
 *
228
 */
229

230
CPLErr PNG_Codec::CompressPNG(buf_mgr &dst, const buf_mgr &src)
16✔
231

232
{
233
    png_structp pngp;
234
    png_infop infop;
235
    buf_mgr mgr = dst;
16✔
236

237
    pngp =
16✔
238
        png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, pngEH, pngWH);
16✔
239
    if (!pngp)
16✔
240
    {
241
        CPLError(CE_Failure, CPLE_AppDefined,
×
242
                 "MRF: Error creating png structure");
243
        return CE_Failure;
×
244
    }
245
    infop = png_create_info_struct(pngp);
16✔
246
    if (!infop)
16✔
247
    {
248
        png_destroy_write_struct(&pngp, nullptr);
×
249
        CPLError(CE_Failure, CPLE_AppDefined,
×
250
                 "MRF: Error creating png info structure");
251
        return CE_Failure;
×
252
    }
253

254
    if (setjmp(png_jmpbuf(pngp)))
16✔
255
    {
256
        png_destroy_write_struct(&pngp, &infop);
×
257
        CPLError(CE_Failure, CPLE_AppDefined, "MRF: Error during png init");
×
258
        return CE_Failure;
×
259
    }
260

261
    png_set_write_fn(pngp, &mgr, write_png, flush_png);
16✔
262

263
    int png_ctype;
264

265
    switch (img.pagesize.c)
16✔
266
    {
267
        case 1:
16✔
268
            if (PNGColors != nullptr)
16✔
269
                png_ctype = PNG_COLOR_TYPE_PALETTE;
1✔
270
            else
271
                png_ctype = PNG_COLOR_TYPE_GRAY;
15✔
272
            break;
16✔
273
        case 2:
×
274
            png_ctype = PNG_COLOR_TYPE_GRAY_ALPHA;
×
275
            break;
×
276
        case 3:
×
277
            png_ctype = PNG_COLOR_TYPE_RGB;
×
278
            break;
×
279
        case 4:
×
280
            png_ctype = PNG_COLOR_TYPE_RGB_ALPHA;
×
281
            break;
×
282
        default:
×
283
        {  // This never happens if we check at the open
284
            CPLError(CE_Failure, CPLE_AppDefined,
×
285
                     "MRF:PNG Write with %d colors called", img.pagesize.c);
×
286
            return CE_Failure;
×
287
        }
288
    }
289

290
    png_set_IHDR(pngp, infop, img.pagesize.x, img.pagesize.y,
16✔
291
                 GDALGetDataTypeSize(img.dt), png_ctype, PNG_INTERLACE_NONE,
16✔
292
                 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
293

294
    // Optional, force certain filters only.  Makes it somewhat faster but worse
295
    // compression png_set_filter(pngp, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
296

297
#if defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER > 10200) &&                     \
298
    defined(PNG_SELECT_READ)
299
    png_uint_32 mask, flags;
300

301
    flags = png_get_asm_flags(pngp);
302
    mask = png_get_asm_flagmask(PNG_SELECT_READ | PNG_SELECT_WRITE);
303
    png_set_asm_flags(pngp, flags | mask);  // use flags &~mask to disable all
304
#endif
305

306
    // Let the quality control the compression level
307
    png_set_compression_level(pngp, std::clamp(img.quality / 10, 1, 9));
16✔
308

309
    // Custom strategy for zlib, set using the band option Z_STRATEGY
310
    if (deflate_flags & ZFLAG_SMASK)
16✔
311
        png_set_compression_strategy(pngp, (deflate_flags & ZFLAG_SMASK) >> 6);
×
312

313
    // Write the palette and the transparencies if they exist
314
    if (PNGColors != nullptr)
16✔
315
    {
316
        png_set_PLTE(pngp, infop, (png_colorp)PNGColors, PalSize);
1✔
317
        if (TransSize != 0)
1✔
318
            png_set_tRNS(pngp, infop, (unsigned char *)PNGAlpha, TransSize,
×
319
                         nullptr);
320
    }
321

322
    png_write_info(pngp, infop);
16✔
323

324
#if defined(CPL_LSB)
325
    if (img.dt != GDT_Byte)
16✔
326
        png_set_swap(pngp);
4✔
327
#endif
328

329
    png_bytep *png_rowp =
330
        (png_bytep *)CPLMalloc(sizeof(png_bytep) * img.pagesize.y);
16✔
331

332
    if (setjmp(png_jmpbuf(pngp)))
16✔
333
    {
334
        CPLFree(png_rowp);
×
335
        png_destroy_write_struct(&pngp, &infop);
×
336
        CPLError(CE_Failure, CPLE_AppDefined,
×
337
                 "MRF: Error during png compression");
338
        return CE_Failure;
×
339
    }
340

341
    int rowbytes = static_cast<int>(png_get_rowbytes(pngp, infop));
16✔
342
    for (int i = 0; i < img.pagesize.y; i++)
8,208✔
343
        png_rowp[i] = (png_bytep)(src.buffer + i * rowbytes);
8,192✔
344

345
    png_write_image(pngp, png_rowp);
16✔
346
    png_write_end(pngp, infop);
16✔
347

348
    // Done
349
    CPLFree(png_rowp);
16✔
350
    png_destroy_write_struct(&pngp, &infop);
16✔
351

352
    // Done
353
    // mgr.size holds the available bytes, so the size of the compressed png
354
    // is the original destination size minus the still available bytes
355
    dst.size -= mgr.size;
16✔
356

357
    return CE_None;
16✔
358
}
359

360
// Builds a PNG palette from a GDAL color table
361
static void ResetPalette(GDALColorTable *poCT, PNG_Codec &codec)
1✔
362
{  // Convert the GDAL LUT to PNG style
363
    codec.TransSize = codec.PalSize = poCT->GetColorEntryCount();
1✔
364

365
    png_color *pasPNGColors =
366
        (png_color *)CPLMalloc(sizeof(png_color) * codec.PalSize);
1✔
367
    unsigned char *pabyAlpha = (unsigned char *)CPLMalloc(codec.TransSize);
1✔
368
    codec.PNGColors = (void *)pasPNGColors;
1✔
369
    codec.PNGAlpha = (void *)pabyAlpha;
1✔
370
    bool NoTranspYet = true;
1✔
371

372
    // Set the palette from the end to reduce the size of the opacity mask
373
    for (int iColor = codec.PalSize - 1; iColor >= 0; iColor--)
257✔
374
    {
375
        GDALColorEntry sEntry;
376
        poCT->GetColorEntryAsRGB(iColor, &sEntry);
256✔
377

378
        pasPNGColors[iColor].red = (png_byte)sEntry.c1;
256✔
379
        pasPNGColors[iColor].green = (png_byte)sEntry.c2;
256✔
380
        pasPNGColors[iColor].blue = (png_byte)sEntry.c3;
256✔
381
        if (NoTranspYet && sEntry.c4 == 255)
256✔
382
            codec.TransSize--;
256✔
383
        else
384
        {
385
            NoTranspYet = false;
×
386
            pabyAlpha[iColor] = (unsigned char)sEntry.c4;
×
387
        }
388
    }
389
}
1✔
390

391
CPLErr PNG_Band::Decompress(buf_mgr &dst, buf_mgr &src)
11✔
392
{
393
    return codec.DecompressPNG(dst, src);
11✔
394
}
395

396
CPLErr PNG_Band::Compress(buf_mgr &dst, buf_mgr &src)
16✔
397
{
398
    if (!codec.PNGColors && img.comp == IL_PPNG)
16✔
399
    {  // Late set PNG palette to conserve memory
400
        GDALColorTable *poCT = GetColorTable();
1✔
401
        if (!poCT)
1✔
402
        {
403
            CPLError(CE_Failure, CPLE_NotSupported,
×
404
                     "MRF PPNG needs a color table");
405
            return CE_Failure;
×
406
        }
407
        ResetPalette(poCT, codec);
1✔
408
    }
409

410
    codec.deflate_flags = deflate_flags;
16✔
411
    return codec.CompressPNG(dst, src);
16✔
412
}
413

414
/**
415
 * \brief For PPNG, builds the data structures needed to write the palette
416
 * The presence of the PNGColors and PNGAlpha is used as a flag for PPNG only
417
 */
418

419
PNG_Band::PNG_Band(MRFDataset *pDS, const ILImage &image, int b, int level)
159✔
420
    : MRFRasterBand(pDS, image, b, level), codec(image)
159✔
421
{  // Check error conditions
422
    if (image.dt != GDT_Byte && image.dt != GDT_Int16 && image.dt != GDT_UInt16)
159✔
423
    {
424
        CPLError(CE_Failure, CPLE_NotSupported,
52✔
425
                 "Data type not supported by MRF PNG");
426
        return;
52✔
427
    }
428
    if (image.pagesize.c > 4)
107✔
429
    {
430
        CPLError(CE_Failure, CPLE_NotSupported,
×
431
                 "MRF PNG can only handle up to 4 bands per page");
432
        return;
×
433
    }
434
    // PNGs can be larger than the source, especially for small page size
435
    // If PPNG is used, the palette can take up to 2100 bytes
436
    poMRFDS->SetPBufferSize(
107✔
437
        static_cast<unsigned int>(1.1 * image.pageSizeBytes + 4000));
107✔
438
}
439

440
NAMESPACE_MRF_END
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc