0N/A/*
3261N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "splashscreen_impl.h"
0N/A
0N/A#include "../libpng/png.h"
0N/A
0N/A#include <setjmp.h>
0N/A
0N/A#define SIG_BYTES 8
0N/A
0N/Avoid PNGAPI
0N/Amy_png_read_stream(png_structp png_ptr, png_bytep data, png_size_t length)
0N/A{
0N/A png_uint_32 check;
0N/A
4418N/A SplashStream * stream = (SplashStream*)png_get_io_ptr(png_ptr);
0N/A check = stream->read(stream, data, length);
0N/A if (check != length)
0N/A png_error(png_ptr, "Read Error");
0N/A}
0N/A
0N/Aint
0N/ASplashDecodePng(Splash * splash, png_rw_ptr read_func, void *io_ptr)
0N/A{
0N/A int stride;
0N/A ImageFormat srcFormat;
0N/A png_uint_32 i, rowbytes;
0N/A png_bytepp row_pointers = NULL;
0N/A png_bytep image_data = NULL;
0N/A int success = 0;
0N/A double gamma;
0N/A
0N/A png_structp png_ptr = NULL;
0N/A png_infop info_ptr = NULL;
0N/A
0N/A png_uint_32 width, height;
0N/A int bit_depth, color_type;
0N/A
0N/A ImageRect srcRect, dstRect;
0N/A
0N/A png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
0N/A if (!png_ptr) {
0N/A goto done;
0N/A }
0N/A
0N/A info_ptr = png_create_info_struct(png_ptr);
0N/A if (!info_ptr) {
0N/A goto done;
0N/A }
0N/A
4418N/A if (setjmp(png_jmpbuf(png_ptr))) {
0N/A goto done;
0N/A }
0N/A
4418N/A png_set_read_fn(png_ptr, io_ptr, read_func);
0N/A
0N/A png_set_sig_bytes(png_ptr, SIG_BYTES); /* we already read the 8 signature bytes */
0N/A
0N/A png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */
0N/A
0N/A png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
0N/A NULL, NULL, NULL);
0N/A
0N/A /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
0N/A * transparency chunks to full alpha channel; strip 16-bit-per-sample
0N/A * images to 8 bits per sample; and convert grayscale to RGB[A]
0N/A * this may be sub-optimal but this simplifies implementation */
0N/A
0N/A png_set_expand(png_ptr);
0N/A png_set_tRNS_to_alpha(png_ptr);
0N/A png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
0N/A png_set_strip_16(png_ptr);
0N/A png_set_gray_to_rgb(png_ptr);
0N/A
0N/A if (png_get_gAMA(png_ptr, info_ptr, &gamma))
0N/A png_set_gamma(png_ptr, 2.2, gamma);
0N/A
0N/A png_read_update_info(png_ptr, info_ptr);
0N/A
0N/A rowbytes = png_get_rowbytes(png_ptr, info_ptr);
0N/A
1123N/A if (!SAFE_TO_ALLOC(rowbytes, height)) {
1123N/A goto done;
1123N/A }
1123N/A
0N/A if ((image_data = (unsigned char *) malloc(rowbytes * height)) == NULL) {
0N/A goto done;
0N/A }
1123N/A
1123N/A if (!SAFE_TO_ALLOC(height, sizeof(png_bytep))) {
1123N/A goto done;
1123N/A }
0N/A if ((row_pointers = (png_bytepp) malloc(height * sizeof(png_bytep)))
0N/A == NULL) {
0N/A goto done;
0N/A }
0N/A
0N/A for (i = 0; i < height; ++i)
0N/A row_pointers[i] = image_data + i * rowbytes;
0N/A
0N/A png_read_image(png_ptr, row_pointers);
0N/A
0N/A SplashCleanup(splash);
0N/A
0N/A splash->width = width;
0N/A splash->height = height;
0N/A
1123N/A if (!SAFE_TO_ALLOC(splash->width, splash->imageFormat.depthBytes)) {
1123N/A goto done;
1123N/A }
0N/A stride = splash->width * splash->imageFormat.depthBytes;
0N/A
1123N/A if (!SAFE_TO_ALLOC(splash->height, stride)) {
1123N/A goto done;
1123N/A }
0N/A splash->frameCount = 1;
0N/A splash->frames = (SplashImage *)
0N/A malloc(sizeof(SplashImage) * splash->frameCount);
1123N/A
1123N/A if (splash->frames == NULL) {
1123N/A goto done;
1123N/A }
1123N/A
0N/A splash->loopCount = 1;
0N/A splash->frames[0].bitmapBits = malloc(stride * splash->height);
1123N/A if (splash->frames[0].bitmapBits == NULL) {
1123N/A free(splash->frames);
1123N/A goto done;
1123N/A }
0N/A splash->frames[0].delay = 0;
0N/A
0N/A /* FIXME: sort out the real format */
0N/A initFormat(&srcFormat, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
0N/A srcFormat.byteOrder = BYTE_ORDER_MSBFIRST;
0N/A
0N/A initRect(&srcRect, 0, 0, width, height, 1, rowbytes,
0N/A image_data, &srcFormat);
0N/A initRect(&dstRect, 0, 0, width, height, 1, stride,
0N/A splash->frames[0].bitmapBits, &splash->imageFormat);
0N/A convertRect(&srcRect, &dstRect, CVT_COPY);
0N/A
0N/A SplashInitFrameShape(splash, 0);
0N/A
0N/A png_read_end(png_ptr, NULL);
0N/A success = 1;
0N/A
0N/A done:
0N/A free(row_pointers);
0N/A free(image_data);
0N/A png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
0N/A return success;
0N/A}
0N/A
0N/Aint
0N/ASplashDecodePngStream(Splash * splash, SplashStream * stream)
0N/A{
0N/A unsigned char sig[SIG_BYTES];
0N/A int success = 0;
0N/A
0N/A stream->read(stream, sig, SIG_BYTES);
2438N/A if (png_sig_cmp(sig, 0, SIG_BYTES)) {
0N/A goto done;
0N/A }
0N/A success = SplashDecodePng(splash, my_png_read_stream, stream);
0N/A
0N/A done:
0N/A return success;
0N/A}