1 /*
   2  * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "splashscreen_impl.h"
  27 
  28 #include <png.h>
  29 
  30 #include <setjmp.h>
  31 
  32 #define SIG_BYTES 8
  33 
  34 void PNGAPI
  35 my_png_read_stream(png_structp png_ptr, png_bytep data, png_size_t length)
  36 {
  37     png_uint_32 check;
  38 
  39     SplashStream * stream = (SplashStream*)png_get_io_ptr(png_ptr);
  40     check = stream->read(stream, data, length);
  41     if (check != length)
  42         png_error(png_ptr, "Read Error");
  43 }
  44 
  45 int
  46 SplashDecodePng(Splash * splash, png_rw_ptr read_func, void *io_ptr)
  47 {
  48     int stride;
  49     ImageFormat srcFormat;
  50     png_uint_32 i, rowbytes;
  51 
  52 #ifdef __GNUC__
  53 #pragma GCC diagnostic push
  54 #pragma GCC diagnostic ignored "-Wclobbered"
  55 #endif
  56     png_bytepp row_pointers = NULL;
  57     png_bytep image_data = NULL;
  58     int success = 0;
  59 #ifdef __GNUC__
  60 #pragma GCC diagnostic pop
  61 #endif
  62 
  63     double gamma;
  64 
  65     png_structp png_ptr = NULL;
  66     png_infop info_ptr = NULL;
  67 
  68     png_uint_32 width, height;
  69     int bit_depth, color_type;
  70 
  71     ImageRect srcRect, dstRect;
  72 
  73     png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  74     if (!png_ptr) {
  75         goto done;
  76     }
  77 
  78     info_ptr = png_create_info_struct(png_ptr);
  79     if (!info_ptr) {
  80         goto done;
  81     }
  82 
  83 #ifdef __APPLE__
  84     /* use setjmp/longjmp versions that do not save/restore the signal mask */
  85     if (_setjmp(png_set_longjmp_fn(png_ptr, _longjmp, sizeof(jmp_buf)))) {
  86 #else
  87     if (setjmp(png_jmpbuf(png_ptr))) {
  88 #endif
  89         goto done;
  90     }
  91 
  92     png_set_read_fn(png_ptr, io_ptr, read_func);
  93 
  94     png_set_sig_bytes(png_ptr, SIG_BYTES);      /* we already read the 8 signature bytes */
  95 
  96     png_read_info(png_ptr, info_ptr);   /* read all PNG info up to image data */
  97 
  98     png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
  99         NULL, NULL, NULL);
 100 
 101     /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
 102      * transparency chunks to full alpha channel; strip 16-bit-per-sample
 103      * images to 8 bits per sample; and convert grayscale to RGB[A]
 104      * this may be sub-optimal but this simplifies implementation */
 105 
 106     png_set_expand(png_ptr);
 107     png_set_tRNS_to_alpha(png_ptr);
 108     png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
 109     png_set_strip_16(png_ptr);
 110     png_set_gray_to_rgb(png_ptr);
 111 
 112     if (png_get_gAMA(png_ptr, info_ptr, &gamma))
 113         png_set_gamma(png_ptr, 2.2, gamma);
 114 
 115     png_read_update_info(png_ptr, info_ptr);
 116 
 117     rowbytes = png_get_rowbytes(png_ptr, info_ptr);
 118 
 119     if (!SAFE_TO_ALLOC(rowbytes, height)) {
 120         goto done;
 121     }
 122 
 123     if ((image_data = (unsigned char *) malloc(rowbytes * height)) == NULL) {
 124         goto done;
 125     }
 126 
 127     if (!SAFE_TO_ALLOC(height, sizeof(png_bytep))) {
 128         goto done;
 129     }
 130     if ((row_pointers = (png_bytepp) malloc(height * sizeof(png_bytep)))
 131             == NULL) {
 132         goto done;
 133     }
 134 
 135     for (i = 0; i < height; ++i)
 136         row_pointers[i] = image_data + i * rowbytes;
 137 
 138     png_read_image(png_ptr, row_pointers);
 139 
 140     SplashCleanup(splash);
 141 
 142     splash->width = width;
 143     splash->height = height;
 144 
 145     if (!SAFE_TO_ALLOC(splash->width, splash->imageFormat.depthBytes)) {
 146         goto done;
 147     }
 148     stride = splash->width * splash->imageFormat.depthBytes;
 149 
 150     if (!SAFE_TO_ALLOC(splash->height, stride)) {
 151         goto done;
 152     }
 153     splash->frameCount = 1;
 154     splash->frames = (SplashImage *)
 155         malloc(sizeof(SplashImage) * splash->frameCount);
 156 
 157     if (splash->frames == NULL) {
 158         goto done;
 159     }
 160 
 161     splash->loopCount = 1;
 162     splash->frames[0].bitmapBits = malloc(stride * splash->height);
 163     if (splash->frames[0].bitmapBits == NULL) {
 164         free(splash->frames);
 165         goto done;
 166     }
 167     splash->frames[0].delay = 0;
 168 
 169     /* FIXME: sort out the real format */
 170     initFormat(&srcFormat, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
 171     srcFormat.byteOrder = BYTE_ORDER_MSBFIRST;
 172 
 173     initRect(&srcRect, 0, 0, width, height, 1, rowbytes,
 174         image_data, &srcFormat);
 175     initRect(&dstRect, 0, 0, width, height, 1, stride,
 176         splash->frames[0].bitmapBits, &splash->imageFormat);
 177     convertRect(&srcRect, &dstRect, CVT_COPY);
 178 
 179     SplashInitFrameShape(splash, 0);
 180 
 181     png_read_end(png_ptr, NULL);
 182     success = 1;
 183 
 184   done:
 185     free(row_pointers);
 186     free(image_data);
 187     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
 188     return success;
 189 }
 190 
 191 int
 192 SplashDecodePngStream(Splash * splash, SplashStream * stream)
 193 {
 194     unsigned char sig[SIG_BYTES];
 195     int success = 0;
 196 
 197     stream->read(stream, sig, SIG_BYTES);
 198     if (png_sig_cmp(sig, 0, SIG_BYTES)) {
 199         goto done;
 200     }
 201     success = SplashDecodePng(splash, my_png_read_stream, stream);
 202 
 203   done:
 204     return success;
 205 }