os/macosx/splash.c
changeset 2736 1ea068235989
child 2847 5c667829bc67
equal deleted inserted replaced
2735:6f1b148a676b 2736:1ea068235989
       
     1 
       
     2 #include "../../stdafx.h"
       
     3 #include "../../openttd.h"
       
     4 #include "../../variables.h"
       
     5 #include "../../macros.h"
       
     6 #include "../../debug.h"
       
     7 #include "../../functions.h"
       
     8 #include "../../gfx.h"
       
     9 #include "../../fileio.h"
       
    10 
       
    11 #include "splash.h"
       
    12 
       
    13 #ifdef WITH_PNG
       
    14 
       
    15 #include <png.h>
       
    16 
       
    17 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
       
    18 {
       
    19 	DEBUG(misc, 0) ("ERROR(libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
       
    20 	longjmp(png_ptr->jmpbuf, 1);
       
    21 }
       
    22 
       
    23 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
       
    24 {
       
    25 	DEBUG(misc, 0) ("WARNING(libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
       
    26 }
       
    27 
       
    28 void DisplaySplashImage(void)
       
    29 {
       
    30 	png_byte header[8];
       
    31 	FILE *f;
       
    32 	png_structp png_ptr;
       
    33 	png_infop info_ptr, end_info;
       
    34 	uint width, height, bit_depth, color_type;
       
    35 	png_colorp palette;
       
    36 	int num_palette;
       
    37 	png_bytep *row_pointers;
       
    38 	uint8 *src, *dst;
       
    39 	uint y;
       
    40 	uint xoff, yoff;
       
    41 	int i;
       
    42 
       
    43 	f = FioFOpenFile(SPLASH_IMAGE_FILE);
       
    44 	if (f == NULL) return;
       
    45 
       
    46 	fread(header, 1, 8, f);
       
    47 	if (png_sig_cmp(header, 0, 8) != 0) {
       
    48 		fclose(f);
       
    49 		return;
       
    50 	}
       
    51 
       
    52 	png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, (png_voidp) NULL, png_my_error, png_my_warning);
       
    53 
       
    54 	if (png_ptr == NULL) {
       
    55 		fclose(f);
       
    56 		return;
       
    57 	}
       
    58 
       
    59 	info_ptr = png_create_info_struct(png_ptr);
       
    60 	if (info_ptr == NULL) {
       
    61 		png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
       
    62 		fclose(f);
       
    63 		return;
       
    64 	}
       
    65 
       
    66 	end_info = png_create_info_struct(png_ptr);
       
    67 	if (end_info == NULL) {
       
    68 		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
       
    69 		fclose(f);
       
    70 		return;
       
    71 	}
       
    72 
       
    73 	if (setjmp(png_jmpbuf(png_ptr))) {
       
    74 		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
       
    75 		fclose(f);
       
    76 		return;
       
    77 	}
       
    78 
       
    79 	png_init_io(png_ptr, f);
       
    80 	png_set_sig_bytes(png_ptr, 8);
       
    81 
       
    82 	png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
       
    83 
       
    84 	width            = png_get_image_width(png_ptr, info_ptr);
       
    85 	height           = png_get_image_height(png_ptr, info_ptr);
       
    86 	bit_depth        = png_get_bit_depth(png_ptr, info_ptr);
       
    87 	color_type       = png_get_color_type(png_ptr, info_ptr);
       
    88 
       
    89 	if(color_type != PNG_COLOR_TYPE_PALETTE || bit_depth != 8) {
       
    90 		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
       
    91 		fclose(f);
       
    92 		return;
       
    93 	}
       
    94 
       
    95 	if(!png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)) {
       
    96 		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
       
    97 		fclose(f);
       
    98 		return;
       
    99 	}
       
   100 
       
   101 	png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
       
   102 
       
   103 	row_pointers = png_get_rows(png_ptr, info_ptr);
       
   104 
       
   105 	memset(_screen.dst_ptr, 0xff, _screen.pitch * _screen.height);
       
   106 
       
   107 	if(width > (uint) _screen.width)
       
   108 		width = _screen.width;
       
   109 	if(height > (uint) _screen.height)
       
   110 		height = _screen.height;
       
   111 
       
   112 	xoff = (_screen.width - width) / 2;
       
   113 	yoff = (_screen.height - height) / 2;
       
   114 	for(y = 0; y < height; y++) {
       
   115 		src = row_pointers[y];
       
   116 		dst = ((uint8 *) _screen.dst_ptr) + (yoff + y) * _screen.pitch + xoff;
       
   117 
       
   118 		memcpy(dst, src, width);
       
   119 	}
       
   120 
       
   121 	for (i = 0; i < num_palette; i++) {
       
   122 		_cur_palette[i].r = palette[i].red;
       
   123 		_cur_palette[i].g = palette[i].green;
       
   124 		_cur_palette[i].b = palette[i].blue;
       
   125 	}
       
   126 
       
   127 	_cur_palette[0xff].r = 0;
       
   128 	_cur_palette[0xff].g = 0;
       
   129 	_cur_palette[0xff].b = 0;
       
   130 
       
   131 	_pal_first_dirty = 0;
       
   132 	_pal_last_dirty = 0xff;
       
   133 
       
   134 	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
       
   135 	fclose(f);
       
   136 	return;
       
   137 }
       
   138 
       
   139 
       
   140 
       
   141 #else // WITH_PNG
       
   142 
       
   143 void DisplaySplashImage(void) {}
       
   144 
       
   145 #endif // WITH_PNG