| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name png.cpp - The png graphic file loader. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2010 by Lutz Sammer and Jimmy Salmon |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
/*---------------------------------------------------------------------------- |
| 32 |
-- Includes |
| 33 |
----------------------------------------------------------------------------*/ |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <png.h> |
| 39 |
|
| 40 |
#include "stratagus.h" |
| 41 |
#include "video.h" |
| 42 |
#include "iolib.h" |
| 43 |
#include "iocompat.h" |
| 44 |
|
| 45 |
/*---------------------------------------------------------------------------- |
| 46 |
-- Variables |
| 47 |
----------------------------------------------------------------------------*/ |
| 48 |
|
| 49 |
/*---------------------------------------------------------------------------- |
| 50 |
-- Functions |
| 51 |
----------------------------------------------------------------------------*/ |
| 52 |
|
| 53 |
/** |
| 54 |
** png read callback for CL-IO. |
| 55 |
** |
| 56 |
** @param png_ptr png struct pointer. |
| 57 |
** @param data byte address to read to. |
| 58 |
** @param length number of bytes to read. |
| 59 |
*/ |
| 60 |
static void CL_png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
| 61 |
{ |
| 62 |
png_size_t check; |
| 63 |
CFile *f; |
| 64 |
|
| 65 |
f = (CFile *)png_get_io_ptr(png_ptr); |
| 66 |
check = (png_size_t)f->read(data, |
| 67 |
(size_t)length); |
| 68 |
if (check != length) { |
| 69 |
png_error(png_ptr, "Read Error"); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
** Load a png graphic file. |
| 75 |
** Modified function from SDL_Image |
| 76 |
** |
| 77 |
** @param g graphic to load. |
| 78 |
** |
| 79 |
** @param headerOnly If true, load only the image header, not the pixels. |
| 80 |
** Can be called again later to load the pixels too. |
| 81 |
** |
| 82 |
** |
| 83 |
** @return 0 for success, -1 for error. |
| 84 |
*/ |
| 85 |
int LoadGraphicPNG(CGraphic *g, bool headerOnly) |
| 86 |
{ |
| 87 |
CFile fp; |
| 88 |
SDL_Surface *volatile surface; |
| 89 |
png_structp png_ptr; |
| 90 |
png_infop info_ptr; |
| 91 |
png_uint_32 width; |
| 92 |
png_uint_32 height; |
| 93 |
int bit_depth; |
| 94 |
int color_type; |
| 95 |
int interlace_type; |
| 96 |
Uint32 Rmask; |
| 97 |
Uint32 Gmask; |
| 98 |
Uint32 Bmask; |
| 99 |
Uint32 Amask; |
| 100 |
SDL_Palette *palette; |
| 101 |
png_bytep *volatile row_pointers; |
| 102 |
int row; |
| 103 |
volatile int ckey; |
| 104 |
png_color_16 *transv; |
| 105 |
char name[PATH_MAX]; |
| 106 |
int ret; |
| 107 |
int channels; |
| 108 |
|
| 109 |
ckey = -1; |
| 110 |
ret = 0; |
| 111 |
|
| 112 |
if (g->File.empty()) { |
| 113 |
return -1; |
| 114 |
} |
| 115 |
|
| 116 |
name[0] = '\0'; |
| 117 |
LibraryFileName(g->File.c_str(), name, sizeof(name)); |
| 118 |
if (name[0] == '\0') { |
| 119 |
return -1; |
| 120 |
} |
| 121 |
|
| 122 |
if (fp.open(name, CL_OPEN_READ) == -1) { |
| 123 |
perror("Can't open file"); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
/* Initialize the data we will clean up when we're done */ |
| 128 |
png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL; |
| 129 |
|
| 130 |
/* Create the PNG loading context structure */ |
| 131 |
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, |
| 132 |
NULL, NULL, NULL); |
| 133 |
if (png_ptr == NULL) { |
| 134 |
fprintf(stderr, "Couldn't allocate memory for PNG file"); |
| 135 |
ret = -1; |
| 136 |
goto done; |
| 137 |
} |
| 138 |
|
| 139 |
/* Allocate/initialize the memory for image information. REQUIRED. */ |
| 140 |
info_ptr = png_create_info_struct(png_ptr); |
| 141 |
if (info_ptr == NULL) { |
| 142 |
fprintf(stderr, "Couldn't create image information for PNG file"); |
| 143 |
ret = -1; |
| 144 |
goto done; |
| 145 |
} |
| 146 |
|
| 147 |
/* Set error handling if you are using setjmp/longjmp method (this is |
| 148 |
* the normal method of doing things with libpng). REQUIRED unless you |
| 149 |
* set up your own error handlers in png_create_read_struct() earlier. |
| 150 |
*/ |
| 151 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 152 |
fprintf(stderr, "Error reading the PNG file.\n"); |
| 153 |
ret = -1; |
| 154 |
goto done; |
| 155 |
} |
| 156 |
|
| 157 |
/* Set up the input control */ |
| 158 |
png_set_read_fn(png_ptr, &fp, CL_png_read_data); |
| 159 |
|
| 160 |
/* Read PNG header info */ |
| 161 |
png_read_info(png_ptr, info_ptr); |
| 162 |
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, |
| 163 |
&color_type, &interlace_type, NULL, NULL); |
| 164 |
|
| 165 |
if (headerOnly) { |
| 166 |
ret = 0; |
| 167 |
g->GraphicWidth = width; |
| 168 |
g->GraphicHeight = height; |
| 169 |
goto done; |
| 170 |
} |
| 171 |
|
| 172 |
/* tell libpng to strip 16 bit/color files down to 8 bits/color */ |
| 173 |
png_set_strip_16(png_ptr) ; |
| 174 |
|
| 175 |
/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single |
| 176 |
* byte into separate bytes (useful for paletted and grayscale images). |
| 177 |
*/ |
| 178 |
png_set_packing(png_ptr); |
| 179 |
|
| 180 |
/* scale greyscale values to the range 0..255 */ |
| 181 |
if (color_type == PNG_COLOR_TYPE_GRAY) { |
| 182 |
png_set_expand(png_ptr); |
| 183 |
} |
| 184 |
|
| 185 |
/* For images with a single "transparent colour", set colour key; |
| 186 |
if more than one index has transparency, or if partially transparent |
| 187 |
entries exist, use full alpha channel */ |
| 188 |
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 189 |
int num_trans; |
| 190 |
png_bytep trans; |
| 191 |
|
| 192 |
png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, |
| 193 |
&transv); |
| 194 |
if (color_type == PNG_COLOR_TYPE_PALETTE) { |
| 195 |
/* Check if all tRNS entries are opaque except one */ |
| 196 |
int i; |
| 197 |
int t; |
| 198 |
|
| 199 |
t = -1; |
| 200 |
for (i = 0; i < num_trans; ++i) { |
| 201 |
if (trans[i] == 0) { |
| 202 |
if (t >= 0) { |
| 203 |
break; |
| 204 |
} |
| 205 |
t = i; |
| 206 |
} else if (trans[i] != 255) { |
| 207 |
break; |
| 208 |
} |
| 209 |
} |
| 210 |
if (i == num_trans) { |
| 211 |
/* exactly one transparent index */ |
| 212 |
ckey = t; |
| 213 |
} else { |
| 214 |
/* more than one transparent index, or translucency */ |
| 215 |
png_set_expand(png_ptr); |
| 216 |
} |
| 217 |
} else { |
| 218 |
ckey = 0; /* actual value will be set later */ |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { |
| 223 |
png_set_gray_to_rgb(png_ptr); |
| 224 |
} |
| 225 |
|
| 226 |
png_read_update_info(png_ptr, info_ptr); |
| 227 |
|
| 228 |
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, |
| 229 |
&color_type, &interlace_type, NULL, NULL); |
| 230 |
channels = png_get_channels(png_ptr, info_ptr); |
| 231 |
|
| 232 |
/* Allocate the SDL surface to hold the image */ |
| 233 |
Rmask = Gmask = Bmask = Amask = 0 ; |
| 234 |
if (color_type != PNG_COLOR_TYPE_PALETTE) { |
| 235 |
if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { |
| 236 |
Rmask = 0x000000FF; |
| 237 |
Gmask = 0x0000FF00; |
| 238 |
Bmask = 0x00FF0000; |
| 239 |
Amask = (channels == 4) ? 0xFF000000 : 0; |
| 240 |
} else { |
| 241 |
int s; |
| 242 |
|
| 243 |
s = (channels == 4) ? 0 : 8; |
| 244 |
Rmask = 0xFF000000 >> s; |
| 245 |
Gmask = 0x00FF0000 >> s; |
| 246 |
Bmask = 0x0000FF00 >> s; |
| 247 |
Amask = 0x000000FF >> s; |
| 248 |
} |
| 249 |
} |
| 250 |
surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, |
| 251 |
bit_depth * channels, Rmask, Gmask, Bmask, Amask); |
| 252 |
if (surface == NULL) { |
| 253 |
fprintf(stderr, "Out of memory"); |
| 254 |
goto done; |
| 255 |
} |
| 256 |
|
| 257 |
if (ckey != -1) { |
| 258 |
if (color_type != PNG_COLOR_TYPE_PALETTE) { |
| 259 |
/* FIXME: Should these be truncated or shifted down? */ |
| 260 |
ckey = SDL_MapRGB(surface->format, |
| 261 |
(Uint8)transv->red, |
| 262 |
(Uint8)transv->green, |
| 263 |
(Uint8)transv->blue); |
| 264 |
} |
| 265 |
SDL_SetColorKey(surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, ckey); |
| 266 |
} |
| 267 |
|
| 268 |
/* Create the array of pointers to image data */ |
| 269 |
row_pointers = new png_bytep[height]; |
| 270 |
if (row_pointers == NULL) { |
| 271 |
fprintf(stderr, "Out of memory"); |
| 272 |
SDL_FreeSurface(surface); |
| 273 |
surface = NULL; |
| 274 |
goto done; |
| 275 |
} |
| 276 |
for (row = 0; row < (int)height; ++row) { |
| 277 |
row_pointers[row] = (png_bytep) |
| 278 |
(Uint8 *)surface->pixels + row * surface->pitch; |
| 279 |
} |
| 280 |
|
| 281 |
/* Read the entire image in one go */ |
| 282 |
png_read_image(png_ptr, row_pointers); |
| 283 |
|
| 284 |
/* read rest of file, get additional chunks in info_ptr - REQUIRED */ |
| 285 |
png_read_end(png_ptr, info_ptr); |
| 286 |
|
| 287 |
/* Load the palette, if any */ |
| 288 |
palette = surface->format->palette; |
| 289 |
if (palette) { |
| 290 |
int num_palette; |
| 291 |
png_colorp png_palette; |
| 292 |
png_get_PLTE(png_ptr, info_ptr, &png_palette, &num_palette); |
| 293 |
if (color_type == PNG_COLOR_TYPE_GRAY) { |
| 294 |
palette->ncolors = 256; |
| 295 |
for (int i = 0; i < 256; ++i) { |
| 296 |
palette->colors[i].r = i; |
| 297 |
palette->colors[i].g = i; |
| 298 |
palette->colors[i].b = i; |
| 299 |
} |
| 300 |
} else if (num_palette > 0) { |
| 301 |
palette->ncolors = num_palette; |
| 302 |
for (int i = 0; i < num_palette; ++i) { |
| 303 |
palette->colors[i].b = png_palette[i].blue; |
| 304 |
palette->colors[i].g = png_palette[i].green; |
| 305 |
palette->colors[i].r = png_palette[i].red; |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
g->Surface = surface; |
| 311 |
g->GraphicWidth = surface->w; |
| 312 |
g->GraphicHeight = surface->h; |
| 313 |
|
| 314 |
done: /* Clean up and return */ |
| 315 |
png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : (png_infopp)0, |
| 316 |
(png_infopp)0); |
| 317 |
if (row_pointers) { |
| 318 |
delete[] row_pointers; |
| 319 |
} |
| 320 |
fp.close(); |
| 321 |
return ret; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
** Save a screenshot to a PNG file. |
| 326 |
** |
| 327 |
** @param name PNG filename to save. |
| 328 |
*/ |
| 329 |
void SaveScreenshotPNG(const std::string &name) |
| 330 |
{ |
| 331 |
FILE *fp; |
| 332 |
png_structp png_ptr; |
| 333 |
png_infop info_ptr; |
| 334 |
int i; |
| 335 |
int j; |
| 336 |
|
| 337 |
|
| 338 |
fp = fopen(name.c_str(), "wb"); |
| 339 |
if (fp == NULL) { |
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 344 |
if (png_ptr == NULL) { |
| 345 |
fclose(fp); |
| 346 |
return; |
| 347 |
} |
| 348 |
|
| 349 |
info_ptr = png_create_info_struct(png_ptr); |
| 350 |
if (info_ptr == NULL) { |
| 351 |
fclose(fp); |
| 352 |
png_destroy_write_struct(&png_ptr, NULL); |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 357 |
/* If we get here, we had a problem reading the file */ |
| 358 |
fclose(fp); |
| 359 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
/* set up the output control if you are using standard C streams */ |
| 364 |
png_init_io(png_ptr, fp); |
| 365 |
|
| 366 |
png_set_IHDR(png_ptr, info_ptr, Video.Width, Video.Height, 8, |
| 367 |
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, |
| 368 |
PNG_FILTER_TYPE_DEFAULT); |
| 369 |
|
| 370 |
Video.LockScreen(); |
| 371 |
|
| 372 |
png_write_info(png_ptr, info_ptr); |
| 373 |
|
| 374 |
if (UseOpenGL) { |
| 375 |
unsigned char *pixels = new unsigned char[Video.Width * Video.Height * 3]; |
| 376 |
if (!pixels) { |
| 377 |
fprintf(stderr, "Out of memory\n"); |
| 378 |
exit(1); |
| 379 |
} |
| 380 |
glReadBuffer(GL_FRONT); |
| 381 |
glReadPixels(0, 0, Video.Width, Video.Height, GL_RGB, GL_UNSIGNED_BYTE, |
| 382 |
pixels); |
| 383 |
for (i = 0; i < Video.Height; ++i) { |
| 384 |
png_write_row(png_ptr, pixels + (Video.Height - 1 - i) * Video.Width * 3); |
| 385 |
} |
| 386 |
delete[] pixels; |
| 387 |
} else { |
| 388 |
unsigned char *row = new unsigned char[Video.Width * 3]; |
| 389 |
SDL_PixelFormat *fmt = TheScreen->format; |
| 390 |
|
| 391 |
for (i = 0; i < Video.Height; ++i) { |
| 392 |
switch (Video.Depth) { |
| 393 |
case 15: |
| 394 |
case 16: { |
| 395 |
Uint16 c; |
| 396 |
for (j = 0; j < Video.Width; ++j) { |
| 397 |
c = ((Uint16 *)TheScreen->pixels)[j + i * Video.Width]; |
| 398 |
row[j * 3 + 0] = ((c & fmt->Rmask) >> fmt->Rshift) << fmt->Rloss; |
| 399 |
row[j * 3 + 1] = ((c & fmt->Gmask) >> fmt->Gshift) << fmt->Gloss; |
| 400 |
row[j * 3 + 2] = ((c & fmt->Bmask) >> fmt->Bshift) << fmt->Bloss; |
| 401 |
} |
| 402 |
break; |
| 403 |
} |
| 404 |
case 24: { |
| 405 |
char *c; |
| 406 |
c = (char *)TheScreen->pixels + i * Video.Width * 3; |
| 407 |
memcpy(row, c, Video.Width * 3); |
| 408 |
break; |
| 409 |
} |
| 410 |
case 32: { |
| 411 |
Uint32 c; |
| 412 |
for (j = 0; j < Video.Width; ++j) { |
| 413 |
c = ((Uint32 *)TheScreen->pixels)[j + i * Video.Width]; |
| 414 |
row[j * 3 + 0] = ((c & fmt->Rmask) >> fmt->Rshift); |
| 415 |
row[j * 3 + 1] = ((c & fmt->Gmask) >> fmt->Gshift); |
| 416 |
row[j * 3 + 2] = ((c & fmt->Bmask) >> fmt->Bshift); |
| 417 |
} |
| 418 |
break; |
| 419 |
} |
| 420 |
} |
| 421 |
png_write_row(png_ptr, row); |
| 422 |
} |
| 423 |
|
| 424 |
delete[] row; |
| 425 |
} |
| 426 |
|
| 427 |
png_write_end(png_ptr, info_ptr); |
| 428 |
|
| 429 |
Video.UnlockScreen(); |
| 430 |
|
| 431 |
/* clean up after the write, and free any memory allocated */ |
| 432 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
| 433 |
|
| 434 |
fclose(fp); |
| 435 |
} |
| 436 |
|
| 437 |
//@} |