summaryrefslogtreecommitdiff
path: root/vcl/source/filter/png/PngImageReader.cxx
blob: 829f3dd45bcaf5a65e62b9ee50c7cbdb38a0b36e (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#include <vcl/filter/PngImageReader.hxx>
#include <png.h>
#include <rtl/crc.h>
#include <tools/stream.hxx>
#include <vcl/bitmap.hxx>
#include <vcl/alpha.hxx>
#include <vcl/BitmapTools.hxx>

#include <bitmap/BitmapWriteAccess.hxx>
#include <svdata.hxx>
#include <salinst.hxx>

namespace
{
void lclReadStream(png_structp pPng, png_bytep pOutBytes, png_size_t nBytesToRead)
{
    png_voidp pIO = png_get_io_ptr(pPng);

    if (pIO == nullptr)
        return;

    SvStream* pStream = static_cast<SvStream*>(pIO);

    sal_Size nBytesRead = pStream->ReadBytes(pOutBytes, nBytesToRead);

    if (nBytesRead != nBytesToRead)
    {
        // Make sure to not reuse old data (could cause infinite loop).
        memset(pOutBytes + nBytesRead, 0, nBytesToRead - nBytesRead);
        png_warning(pPng, "Error reading");
    }
}

constexpr int PNG_SIGNATURE_SIZE = 8;

bool isPng(SvStream& rStream)
{
    // Check signature bytes
    sal_uInt8 aHeader[PNG_SIGNATURE_SIZE];
    rStream.ReadBytes(aHeader, PNG_SIGNATURE_SIZE);

    return png_sig_cmp(aHeader, 0, PNG_SIGNATURE_SIZE) == 0;
}

bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool bUseBitmap32)
{
    if (!isPng(rStream))
        return false;

    png_structp pPng = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
    if (!pPng)
        return false;

    png_infop pInfo = png_create_info_struct(pPng);
    if (!pInfo)
    {
        png_destroy_read_struct(&pPng, nullptr, nullptr);
        return false;
    }

    // All variables holding resources need to be declared here in order to be
    // properly cleaned up in case of an error, otherwise libpng's longjmp()
    // jumps over the destructor calls.
    Bitmap aBitmap;
    AlphaMask aBitmapAlpha;
    Size prefSize;
    BitmapScopedWriteAccess pWriteAccess;
    AlphaScopedWriteAccess pWriteAccessAlpha;
    std::vector<std::vector<png_byte>> aRows;

    if (setjmp(png_jmpbuf(pPng)))
    {
        png_destroy_read_struct(&pPng, &pInfo, nullptr);
        // Set the bitmap if it contains something, even on failure. This allows
        // reading images that are only partially broken.
        pWriteAccess.reset();
        pWriteAccessAlpha.reset();
        if (!aBitmap.IsEmpty() && !aBitmapAlpha.IsEmpty())
            rBitmapEx = BitmapEx(aBitmap, aBitmapAlpha);
        else if (!aBitmap.IsEmpty())
            rBitmapEx = BitmapEx(aBitmap);
        if (!rBitmapEx.IsEmpty() && !prefSize.IsEmpty())
        {
            rBitmapEx.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
            rBitmapEx.SetPrefSize(prefSize);
        }
        return false;
    }

    png_set_option(pPng, PNG_MAXIMUM_INFLATE_WINDOW, PNG_OPTION_ON);

    png_set_read_fn(pPng, &rStream, lclReadStream);

    png_set_crc_action(pPng, PNG_CRC_ERROR_QUIT, PNG_CRC_WARN_DISCARD);

    png_set_sig_bytes(pPng, PNG_SIGNATURE_SIZE);

    png_read_info(pPng, pInfo);

    png_uint_32 width = 0;
    png_uint_32 height = 0;
    int bitDepth = 0;
    int colorType = -1;
    int interlace = -1;

    png_uint_32 returnValue = png_get_IHDR(pPng, pInfo, &width, &height, &bitDepth, &colorType,
                                           &interlace, nullptr, nullptr);

    if (returnValue != 1)
    {
        png_destroy_read_struct(&pPng, &pInfo, nullptr);
        return false;
    }

    if (colorType == PNG_COLOR_TYPE_PALETTE)
        png_set_palette_to_rgb(pPng);

    if (colorType == PNG_COLOR_TYPE_GRAY)
        png_set_expand_gray_1_2_4_to_8(pPng);

    if (png_get_valid(pPng, pInfo, PNG_INFO_tRNS))
        png_set_tRNS_to_alpha(pPng);

    if (bitDepth == 16)
        png_set_scale_16(pPng);

    if (bitDepth < 8)
        png_set_packing(pPng);

    // Convert gray+alpha to RGBA, keep gray as gray.
    if (colorType == PNG_COLOR_TYPE_GRAY_ALPHA
        || (colorType == PNG_COLOR_TYPE_GRAY && png_get_valid(pPng, pInfo, PNG_INFO_tRNS)))
    {
        png_set_gray_to_rgb(pPng);
    }

    // Sets the filler byte - if RGB it converts to RGBA
    // png_set_filler(pPng, 0xFF, PNG_FILLER_AFTER);

    int nNumberOfPasses = png_set_interlace_handling(pPng);

    png_read_update_info(pPng, pInfo);
    returnValue = png_get_IHDR(pPng, pInfo, &width, &height, &bitDepth, &colorType, nullptr,
                               nullptr, nullptr);

    if (returnValue != 1)
    {
        png_destroy_read_struct(&pPng, &pInfo, nullptr);
        return false;
    }

    if (bitDepth != 8
        || (colorType != PNG_COLOR_TYPE_RGB && colorType != PNG_COLOR_TYPE_RGB_ALPHA
            && colorType != PNG_COLOR_TYPE_GRAY))
    {
        png_destroy_read_struct(&pPng, &pInfo, nullptr);
        return false;
    }

    png_uint_32 res_x = 0;
    png_uint_32 res_y = 0;
    int unit_type = 0;
    if (png_get_pHYs(pPng, pInfo, &res_x, &res_y, &unit_type) != 0
        && unit_type == PNG_RESOLUTION_METER && res_x && res_y)
    {
        // convert into MapUnit::Map100thMM
        prefSize = Size(static_cast<sal_Int32>((100000.0 * width) / res_x),
                        static_cast<sal_Int32>((100000.0 * height) / res_y));
    }

    {
        if (colorType == PNG_COLOR_TYPE_RGB)
        {
            size_t aRowSizeBytes = png_get_rowbytes(pPng, pInfo);

            aBitmap = Bitmap(Size(width, height), 24);
            {
                pWriteAccess = BitmapScopedWriteAccess(aBitmap);
                ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
                if (eFormat == ScanlineFormat::N24BitTcBgr)
                    png_set_bgr(pPng);

                aRows = std::vector<std::vector<png_byte>>(height);
                for (auto& rRow : aRows)
                    rRow.resize(aRowSizeBytes, 0);

                for (int pass = 0; pass < nNumberOfPasses; pass++)
                {
                    for (png_uint_32 y = 0; y < height; y++)
                    {
                        Scanline pScanline = pWriteAccess->GetScanline(y);
                        png_bytep pRow = aRows[y].data();
                        png_read_row(pPng, pRow, nullptr);
                        size_t iColor = 0;
                        for (size_t i = 0; i < aRowSizeBytes; i += 3)
                        {
                            pScanline[iColor++] = pRow[i + 0];
                            pScanline[iColor++] = pRow[i + 1];
                            pScanline[iColor++] = pRow[i + 2];
                        }
                    }
                }
                pWriteAccess.reset();
            }
            rBitmapEx = BitmapEx(aBitmap);
        }
        else if (colorType == PNG_COLOR_TYPE_RGB_ALPHA)
        {
            size_t aRowSizeBytes = png_get_rowbytes(pPng, pInfo);

            if (bUseBitmap32)
            {
                aBitmap = Bitmap(Size(width, height), 32);
                {
                    pWriteAccess = BitmapScopedWriteAccess(aBitmap);
                    ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
                    if (eFormat == ScanlineFormat::N32BitTcAbgr
                        || eFormat == ScanlineFormat::N32BitTcBgra)
                    {
                        png_set_bgr(pPng);
                    }

                    aRows = std::vector<std::vector<png_byte>>(height);
                    for (auto& rRow : aRows)
                        rRow.resize(aRowSizeBytes, 0);

                    auto const alphaFirst = (eFormat == ScanlineFormat::N32BitTcAbgr
                                             || eFormat == ScanlineFormat::N32BitTcArgb);
                    for (int pass = 0; pass < nNumberOfPasses; pass++)
                    {
                        for (png_uint_32 y = 0; y < height; y++)
                        {
                            Scanline pScanline = pWriteAccess->GetScanline(y);
                            png_bytep pRow = aRows[y].data();
                            png_read_row(pPng, pRow, nullptr);
                            size_t iColor = 0;
                            for (size_t i = 0; i < aRowSizeBytes; i += 4)
                            {
                                sal_Int8 alpha = pRow[i + 3];
                                if (alphaFirst)
                                {
                                    pScanline[iColor++] = alpha;
                                }
                                pScanline[iColor++] = vcl::bitmap::premultiply(pRow[i + 0], alpha);
                                pScanline[iColor++] = vcl::bitmap::premultiply(pRow[i + 1], alpha);
                                pScanline[iColor++] = vcl::bitmap::premultiply(pRow[i + 2], alpha);
                                if (!alphaFirst)
                                {
                                    pScanline[iColor++] = alpha;
                                }
                            }
                        }
                    }
                    pWriteAccess.reset();
                }
                rBitmapEx = BitmapEx(aBitmap);
            }
            else
            {
                aBitmap = Bitmap(Size(width, height), 24);
                aBitmapAlpha = AlphaMask(Size(width, height), nullptr);
                {
                    pWriteAccess = BitmapScopedWriteAccess(aBitmap);
                    ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
                    if (eFormat == ScanlineFormat::N24BitTcBgr)
                        png_set_bgr(pPng);

                    pWriteAccessAlpha = AlphaScopedWriteAccess(aBitmapAlpha);

                    aRows = std::vector<std::vector<png_byte>>(height);
                    for (auto& rRow : aRows)
                        rRow.resize(aRowSizeBytes, 0);

                    for (int pass = 0; pass < nNumberOfPasses; pass++)
                    {
                        for (png_uint_32 y = 0; y < height; y++)
                        {
                            Scanline pScanline = pWriteAccess->GetScanline(y);
                            Scanline pScanAlpha = pWriteAccessAlpha->GetScanline(y);
                            png_bytep pRow = aRows[y].data();
                            png_read_row(pPng, pRow, nullptr);
                            size_t iAlpha = 0;
                            size_t iColor = 0;
                            for (size_t i = 0; i < aRowSizeBytes; i += 4)
                            {
                                pScanline[iColor++] = pRow[i + 0];
                                pScanline[iColor++] = pRow[i + 1];
                                pScanline[iColor++] = pRow[i + 2];
                                pScanAlpha[iAlpha++] = 0xFF - pRow[i + 3];
                            }
                        }
                    }
                    pWriteAccess.reset();
                    pWriteAccessAlpha.reset();
                }
                rBitmapEx = BitmapEx(aBitmap, aBitmapAlpha);
            }
        }
        else if (colorType == PNG_COLOR_TYPE_GRAY)
        {
            size_t aRowSizeBytes = png_get_rowbytes(pPng, pInfo);

            aBitmap = Bitmap(Size(width, height), 8, &Bitmap::GetGreyPalette(256));
            aBitmap.Erase(COL_WHITE);
            {
                pWriteAccess = BitmapScopedWriteAccess(aBitmap);

                aRows = std::vector<std::vector<png_byte>>(height);
                for (auto& rRow : aRows)
                    rRow.resize(aRowSizeBytes, 0);

                for (int pass = 0; pass < nNumberOfPasses; pass++)
                {
                    for (png_uint_32 y = 0; y < height; y++)
                    {
                        Scanline pScanline = pWriteAccess->GetScanline(y);
                        png_bytep pRow = aRows[y].data();
                        png_read_row(pPng, pRow, nullptr);
                        size_t iColor = 0;
                        for (size_t i = 0; i < aRowSizeBytes; ++i)
                            pScanline[iColor++] = pRow[i];
                    }
                }
                pWriteAccess.reset();
            }
            rBitmapEx = BitmapEx(aBitmap);
        }
    }

    png_read_end(pPng, pInfo);

    png_destroy_read_struct(&pPng, &pInfo, nullptr);

    if (!prefSize.IsEmpty())
    {
        rBitmapEx.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
        rBitmapEx.SetPrefSize(prefSize);
    }

    return true;
}

std::unique_ptr<sal_uInt8[]> getMsGifChunk(SvStream& rStream, sal_Int32* chunkSize)
{
    if (chunkSize)
        *chunkSize = 0;
    if (!isPng(rStream))
        return nullptr;
    // It's easier to read manually the contents and find the chunk than
    // try to get it using libpng.
    // https://en.wikipedia.org/wiki/Portable_Network_Graphics#File_format
    // Each chunk is: 4 bytes length, 4 bytes type, <length> bytes, 4 bytes crc
    for (;;)
    {
        sal_uInt32 length, type, crc;
        rStream.ReadUInt32(length);
        rStream.ReadUInt32(type);
        if (!rStream.good())
            return nullptr;
        constexpr sal_uInt32 PNGCHUNK_msOG = 0x6d734f47; // Microsoft Office Animated GIF
        constexpr sal_uInt64 MSGifHeaderSize = 11; // "MSOFFICE9.0"
        if (type == PNGCHUNK_msOG && length > MSGifHeaderSize)
        {
            // calculate chunktype CRC (swap it back to original byte order)
            sal_uInt32 typeForCrc = type;
#if defined(__LITTLEENDIAN) || defined(OSL_LITENDIAN)
            typeForCrc = OSL_SWAPDWORD(typeForCrc);
#endif
            sal_uInt32 computedCrc = rtl_crc32(0, &typeForCrc, 4);
            const sal_uInt64 pos = rStream.Tell();
            if (pos + length >= rStream.TellEnd())
                return nullptr; // broken PNG

            char msHeader[MSGifHeaderSize];
            if (rStream.ReadBytes(msHeader, MSGifHeaderSize) != MSGifHeaderSize)
                return nullptr;
            computedCrc = rtl_crc32(computedCrc, msHeader, MSGifHeaderSize);
            length -= MSGifHeaderSize;

            std::unique_ptr<sal_uInt8[]> chunk(new sal_uInt8[length]);
            if (rStream.ReadBytes(chunk.get(), length) != length)
                return nullptr;
            computedCrc = rtl_crc32(computedCrc, chunk.get(), length);
            rStream.ReadUInt32(crc);
            if (crc != computedCrc)
                continue; // invalid chunk, ignore
            if (chunkSize)
                *chunkSize = length;
            return chunk;
        }
        if (rStream.remainingSize() < length)
            return nullptr;
        rStream.SeekRel(length);
        rStream.ReadUInt32(crc);
        constexpr sal_uInt32 PNGCHUNK_IEND = 0x49454e44;
        if (type == PNGCHUNK_IEND)
            return nullptr;
    }
}

} // anonymous namespace

namespace vcl
{
PngImageReader::PngImageReader(SvStream& rStream)
    : mrStream(rStream)
{
}

bool PngImageReader::read(BitmapEx& rBitmapEx)
{
    auto pBackendCapabilities = ImplGetSVData()->mpDefInst->GetBackendCapabilities();
    bool bSupportsBitmap32 = pBackendCapabilities->mbSupportsBitmap32;

    return reader(mrStream, rBitmapEx, bSupportsBitmap32);
}

std::unique_ptr<sal_uInt8[]> PngImageReader::getMicrosoftGifChunk(SvStream& rStream,
                                                                  sal_Int32* chunkSize)
{
    sal_uInt64 originalPosition = rStream.Tell();
    SvStreamEndian originalEndian = rStream.GetEndian();
    rStream.SetEndian(SvStreamEndian::BIG);
    std::unique_ptr<sal_uInt8[]> chunk = getMsGifChunk(rStream, chunkSize);
    rStream.SetEndian(originalEndian);
    rStream.Seek(originalPosition);
    return chunk;
}

} // namespace vcl

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */