src/java.desktop/share/native/libsplashscreen/splashscreen_png.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 35296 jdk/src/java.desktop/share/native/libsplashscreen/splashscreen_png.c@659e92aaf7f4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 10576
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2608
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2608
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2608
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2608
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2608
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "splashscreen_impl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
23305
e7d0093bfc6c 8035341: Allow using a system installed libpng
omajid
parents: 10576
diff changeset
    28
#include <png.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <setjmp.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#define SIG_BYTES 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
void PNGAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
my_png_read_stream(png_structp png_ptr, png_bytep data, png_size_t length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    png_uint_32 check;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 7668
diff changeset
    39
    SplashStream * stream = (SplashStream*)png_get_io_ptr(png_ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    check = stream->read(stream, data, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    if (check != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        png_error(png_ptr, "Read Error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
SplashDecodePng(Splash * splash, png_rw_ptr read_func, void *io_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    int stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    ImageFormat srcFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    png_uint_32 i, rowbytes;
31659
dd5357cd927d 8080695: splashscreen_png.c compile error with gcc 4.9.2
plevart
parents: 25859
diff changeset
    51
    volatile png_bytepp row_pointers = NULL;
dd5357cd927d 8080695: splashscreen_png.c compile error with gcc 4.9.2
plevart
parents: 25859
diff changeset
    52
    volatile png_bytep image_data = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    int success = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    double gamma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    png_structp png_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    png_infop info_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    png_uint_32 width, height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int bit_depth, color_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    ImageRect srcRect, dstRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    if (!png_ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    info_ptr = png_create_info_struct(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    if (!info_ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
25092
b35eb67cf1e3 8026385: [macosx] (awt) setjmp/longjmp changes the process signal mask on OS X
ddehaven
parents: 23328
diff changeset
    74
#ifdef __APPLE__
b35eb67cf1e3 8026385: [macosx] (awt) setjmp/longjmp changes the process signal mask on OS X
ddehaven
parents: 23328
diff changeset
    75
    /* use setjmp/longjmp versions that do not save/restore the signal mask */
b35eb67cf1e3 8026385: [macosx] (awt) setjmp/longjmp changes the process signal mask on OS X
ddehaven
parents: 23328
diff changeset
    76
    if (_setjmp(png_set_longjmp_fn(png_ptr, _longjmp, sizeof(jmp_buf)))) {
b35eb67cf1e3 8026385: [macosx] (awt) setjmp/longjmp changes the process signal mask on OS X
ddehaven
parents: 23328
diff changeset
    77
#else
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 7668
diff changeset
    78
    if (setjmp(png_jmpbuf(png_ptr))) {
25092
b35eb67cf1e3 8026385: [macosx] (awt) setjmp/longjmp changes the process signal mask on OS X
ddehaven
parents: 23328
diff changeset
    79
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 7668
diff changeset
    83
    png_set_read_fn(png_ptr, io_ptr, read_func);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    png_set_sig_bytes(png_ptr, SIG_BYTES);      /* we already read the 8 signature bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    png_read_info(png_ptr, info_ptr);   /* read all PNG info up to image data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * transparency chunks to full alpha channel; strip 16-bit-per-sample
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * images to 8 bits per sample; and convert grayscale to RGB[A]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * this may be sub-optimal but this simplifies implementation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    png_set_expand(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    png_set_tRNS_to_alpha(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    png_set_strip_16(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    png_set_gray_to_rgb(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    if (png_get_gAMA(png_ptr, info_ptr, &gamma))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        png_set_gamma(png_ptr, 2.2, gamma);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
35296
659e92aaf7f4 8143941: Update splashscreen displays
azvegint
parents: 31659
diff changeset
   106
    png_set_interlace_handling(png_ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    png_read_update_info(png_ptr, info_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    rowbytes = png_get_rowbytes(png_ptr, info_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
2608
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   111
    if (!SAFE_TO_ALLOC(rowbytes, height)) {
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   112
        goto done;
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   113
    }
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   114
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    if ((image_data = (unsigned char *) malloc(rowbytes * height)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
2608
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   118
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   119
    if (!SAFE_TO_ALLOC(height, sizeof(png_bytep))) {
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   120
        goto done;
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   121
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    if ((row_pointers = (png_bytepp) malloc(height * sizeof(png_bytep)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    for (i = 0; i < height; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        row_pointers[i] = image_data + i * rowbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    png_read_image(png_ptr, row_pointers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    SplashCleanup(splash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    splash->width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    splash->height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
2608
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   137
    if (!SAFE_TO_ALLOC(splash->width, splash->imageFormat.depthBytes)) {
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   138
        goto done;
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   139
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    stride = splash->width * splash->imageFormat.depthBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
2608
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   142
    if (!SAFE_TO_ALLOC(splash->height, stride)) {
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   143
        goto done;
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   144
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    splash->frameCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    splash->frames = (SplashImage *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        malloc(sizeof(SplashImage) * splash->frameCount);
2608
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   148
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   149
    if (splash->frames == NULL) {
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   150
        goto done;
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   151
    }
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   152
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    splash->loopCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    splash->frames[0].bitmapBits = malloc(stride * splash->height);
2608
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   155
    if (splash->frames[0].bitmapBits == NULL) {
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   156
        free(splash->frames);
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   157
        goto done;
fe2af56a83d3 6804996: JWS PNG Decoding Integer Overflow [V-flrhat2ln8]
bae
parents: 2
diff changeset
   158
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    splash->frames[0].delay = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /* FIXME: sort out the real format */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    initFormat(&srcFormat, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    srcFormat.byteOrder = BYTE_ORDER_MSBFIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    initRect(&srcRect, 0, 0, width, height, 1, rowbytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        image_data, &srcFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    initRect(&dstRect, 0, 0, width, height, 1, stride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        splash->frames[0].bitmapBits, &splash->imageFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    convertRect(&srcRect, &dstRect, CVT_COPY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    SplashInitFrameShape(splash, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    png_read_end(png_ptr, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    success = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
  done:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    free(row_pointers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    free(image_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    return success;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
SplashDecodePngStream(Splash * splash, SplashStream * stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    unsigned char sig[SIG_BYTES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    int success = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    stream->read(stream, sig, SIG_BYTES);
5751
5f6d488b1972 6959123: Remove use of obsolete png_check_sig function in splashscreen_png.c
andrew
parents: 5506
diff changeset
   190
    if (png_sig_cmp(sig, 0, SIG_BYTES)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    success = SplashDecodePng(splash, my_png_read_stream, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
  done:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    return success;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
}