Path: blob/master/src/java.desktop/share/native/libsplashscreen/splashscreen_png.c
41149 views
/*1* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "splashscreen_impl.h"2627#include <png.h>2829#include <setjmp.h>3031#define SIG_BYTES 83233void PNGAPI34my_png_read_stream(png_structp png_ptr, png_bytep data, png_size_t length)35{36png_uint_32 check;3738SplashStream * stream = (SplashStream*)png_get_io_ptr(png_ptr);39check = stream->read(stream, data, length);40if (check != length)41png_error(png_ptr, "Read Error");42}4344int45SplashDecodePng(Splash * splash, png_rw_ptr read_func, void *io_ptr)46{47int stride;48ImageFormat srcFormat;49png_uint_32 i, rowbytes;50volatile png_bytepp row_pointers = NULL;51volatile png_bytep image_data = NULL;52int success = 0;53double gamma;5455png_structp png_ptr = NULL;56png_infop info_ptr = NULL;5758png_uint_32 width, height;59int bit_depth, color_type;6061ImageRect srcRect, dstRect;6263png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);64if (!png_ptr) {65goto done;66}6768info_ptr = png_create_info_struct(png_ptr);69if (!info_ptr) {70goto done;71}7273#ifdef __APPLE__74/* use setjmp/longjmp versions that do not save/restore the signal mask */75if (_setjmp(png_set_longjmp_fn(png_ptr, _longjmp, sizeof(jmp_buf)))) {76#else77if (setjmp(png_jmpbuf(png_ptr))) {78#endif79goto done;80}8182png_set_read_fn(png_ptr, io_ptr, read_func);8384png_set_sig_bytes(png_ptr, SIG_BYTES); /* we already read the 8 signature bytes */8586png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */8788png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,89NULL, NULL, NULL);9091/* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,92* transparency chunks to full alpha channel; strip 16-bit-per-sample93* images to 8 bits per sample; and convert grayscale to RGB[A]94* this may be sub-optimal but this simplifies implementation */9596png_set_expand(png_ptr);97png_set_tRNS_to_alpha(png_ptr);98png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);99png_set_strip_16(png_ptr);100png_set_gray_to_rgb(png_ptr);101102if (png_get_gAMA(png_ptr, info_ptr, &gamma))103png_set_gamma(png_ptr, 2.2, gamma);104105png_set_interlace_handling(png_ptr);106png_read_update_info(png_ptr, info_ptr);107108rowbytes = png_get_rowbytes(png_ptr, info_ptr);109110if (!SAFE_TO_ALLOC(rowbytes, height)) {111goto done;112}113114if ((image_data = (unsigned char *) malloc(rowbytes * height)) == NULL) {115goto done;116}117118if (!SAFE_TO_ALLOC(height, sizeof(png_bytep))) {119goto done;120}121if ((row_pointers = (png_bytepp) malloc(height * sizeof(png_bytep)))122== NULL) {123goto done;124}125126for (i = 0; i < height; ++i)127row_pointers[i] = image_data + i * rowbytes;128129png_read_image(png_ptr, row_pointers);130131SplashCleanup(splash);132133splash->width = width;134splash->height = height;135136if (!SAFE_TO_ALLOC(splash->width, splash->imageFormat.depthBytes)) {137goto done;138}139stride = splash->width * splash->imageFormat.depthBytes;140141if (!SAFE_TO_ALLOC(splash->height, stride)) {142goto done;143}144splash->frameCount = 1;145splash->frames = (SplashImage *)146malloc(sizeof(SplashImage) * splash->frameCount);147148if (splash->frames == NULL) {149goto done;150}151152splash->loopCount = 1;153splash->frames[0].bitmapBits = malloc(stride * splash->height);154if (splash->frames[0].bitmapBits == NULL) {155free(splash->frames);156goto done;157}158splash->frames[0].delay = 0;159160/* FIXME: sort out the real format */161initFormat(&srcFormat, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);162srcFormat.byteOrder = BYTE_ORDER_MSBFIRST;163164initRect(&srcRect, 0, 0, width, height, 1, rowbytes,165image_data, &srcFormat);166initRect(&dstRect, 0, 0, width, height, 1, stride,167splash->frames[0].bitmapBits, &splash->imageFormat);168convertRect(&srcRect, &dstRect, CVT_COPY);169170SplashInitFrameShape(splash, 0);171172png_read_end(png_ptr, NULL);173success = 1;174175done:176free(row_pointers);177free(image_data);178png_destroy_read_struct(&png_ptr, &info_ptr, NULL);179return success;180}181182int183SplashDecodePngStream(Splash * splash, SplashStream * stream)184{185unsigned char sig[SIG_BYTES];186int success = 0;187188stream->read(stream, sig, SIG_BYTES);189if (png_sig_cmp(sig, 0, SIG_BYTES)) {190goto done;191}192success = SplashDecodePng(splash, my_png_read_stream, stream);193194done:195return success;196}197198199