Changeset - r14195:ea01033bb77f
[Not reviewed]
master
0 3 0
glx - 15 years ago 2010-01-08 03:17:12
glx@openttd.org
(svn r18756) -Codechange: direct accesses to png_*_struct members are deprecated
3 files changed with 19 insertions and 19 deletions:
0 comments (0 inline, 0 general)
src/heightmap.cpp
Show inline comments
 
@@ -40,25 +40,25 @@ static inline byte RGBToGrayscale(byte r
 
#include <png.h>
 

	
 
/**
 
 * The PNG Heightmap loader.
 
 */
 
static void ReadHeightmapPNGImageData(byte *map, png_structp png_ptr, png_infop info_ptr)
 
{
 
	uint x, y;
 
	byte gray_palette[256];
 
	png_bytep *row_pointers = NULL;
 

	
 
	/* Get palette and convert it to grayscale */
 
	if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
 
	if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE) {
 
		int i;
 
		int palette_size;
 
		png_color *palette;
 
		bool all_gray = true;
 

	
 
		png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size);
 
		for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) {
 
			all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue;
 
			gray_palette[i] = RGBToGrayscale(palette[i].red, palette[i].green, palette[i].blue);
 
		}
 

	
 
		/**
 
@@ -68,32 +68,32 @@ static void ReadHeightmapPNGImageData(by
 
		 * level 1, etc.
 
		 */
 
		if (palette_size == 16 && !all_gray) {
 
			for (i = 0; i < palette_size; i++) {
 
				gray_palette[i] = 256 * i / palette_size;
 
			}
 
		}
 
	}
 

	
 
	row_pointers = png_get_rows(png_ptr, info_ptr);
 

	
 
	/* Read the raw image data and convert in 8-bit grayscale */
 
	for (x = 0; x < info_ptr->width; x++) {
 
		for (y = 0; y < info_ptr->height; y++) {
 
			byte *pixel = &map[y * info_ptr->width + x];
 
			uint x_offset = x * info_ptr->channels;
 
	for (x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
 
		for (y = 0; y < png_get_image_height(png_ptr, info_ptr); y++) {
 
			byte *pixel = &map[y * png_get_image_width(png_ptr, info_ptr) + x];
 
			uint x_offset = x * png_get_channels(png_ptr, info_ptr);
 

	
 
			if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
 
			if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE) {
 
				*pixel = gray_palette[row_pointers[y][x_offset]];
 
			} else if (info_ptr->channels == 3) {
 
			} else if (png_get_channels(png_ptr, info_ptr) == 3) {
 
				*pixel = RGBToGrayscale(row_pointers[y][x_offset + 0],
 
						row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]);
 
			} else {
 
				*pixel = row_pointers[y][x_offset];
 
			}
 
		}
 
	}
 
}
 

	
 
/**
 
 * Reads the heightmap and/or size of the heightmap from a PNG file.
 
 * If map == NULL only the size of the PNG is read, otherwise a map
 
@@ -126,38 +126,38 @@ static bool ReadHeightmapPNG(char *filen
 
		return false;
 
	}
 

	
 
	png_init_io(png_ptr, fp);
 

	
 
	/* Allocate memory and read image, without alpha or 16-bit samples
 
	 * (result is either 8-bit indexed/grayscale or 24-bit RGB) */
 
	png_set_packing(png_ptr);
 
	png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_STRIP_16, NULL);
 

	
 
	/* Maps of wrong colour-depth are not used.
 
	 * (this should have been taken care of by stripping alpha and 16-bit samples on load) */
 
	if ((info_ptr->channels != 1) && (info_ptr->channels != 3) && (info_ptr->bit_depth != 8)) {
 
	if ((png_get_channels(png_ptr, info_ptr) != 1) && (png_get_channels(png_ptr, info_ptr) != 3) && (png_get_bit_depth(png_ptr, info_ptr) != 8)) {
 
		ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_IMAGE_TYPE, 0, 0);
 
		fclose(fp);
 
		png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
 
		return false;
 
	}
 

	
 
	if (map != NULL) {
 
		*map = MallocT<byte>(info_ptr->width * info_ptr->height);
 
		*map = MallocT<byte>(png_get_image_width(png_ptr, info_ptr) * png_get_image_height(png_ptr, info_ptr));
 
		ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
 
	}
 

	
 
	*x = info_ptr->width;
 
	*y = info_ptr->height;
 
	*x = png_get_image_width(png_ptr, info_ptr);
 
	*y = png_get_image_height(png_ptr, info_ptr);
 

	
 
	fclose(fp);
 
	png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
 
	return true;
 
}
 

	
 
#endif /* WITH_PNG */
 

	
 

	
 
/**
 
 * The BMP Heightmap loader.
 
 */
src/screenshot.cpp
Show inline comments
 
@@ -198,25 +198,25 @@ static bool MakeBMPImage(const char *nam
 
	return true;
 
}
 

	
 
/*********************************************************
 
 **** SCREENSHOT CODE FOR PORTABLE NETWORK GRAPHICS (.PNG)
 
 *********************************************************/
 
#if defined(WITH_PNG)
 
#include <png.h>
 

	
 
static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
 
{
 
	DEBUG(misc, 0, "[libpng] error: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
 
	longjmp(png_ptr->jmpbuf, 1);
 
	longjmp(png_jmpbuf(png_ptr), 1);
 
}
 

	
 
static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
 
{
 
	DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
 
}
 

	
 
static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
 
{
 
	png_color rq[256];
 
	FILE *f;
 
	uint i, y, n;
src/spriteloader/png.cpp
Show inline comments
 
@@ -19,25 +19,25 @@
 
#include <png.h>
 

	
 
#define PNG_SLOT 62
 

	
 
static void PNGAPI png_my_read(png_structp png_ptr, png_bytep data, png_size_t length)
 
{
 
	FioReadBlock(data, length);
 
}
 

	
 
static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
 
{
 
	DEBUG(sprite, 0, "ERROR (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
 
	longjmp(png_ptr->jmpbuf, 1);
 
	longjmp(png_jmpbuf(png_ptr), 1);
 
}
 

	
 
static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
 
{
 
	DEBUG(sprite, 0, "WARNING (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
 
}
 

	
 
static bool OpenPNGFile(const char *filename, uint32 id, bool mask)
 
{
 
	char png_file[MAX_PATH];
 

	
 
	snprintf(png_file, sizeof(png_file), "sprites" PATHSEP "%s" PATHSEP "%d%s.png", filename, id, mask ? "m" : "");
 
@@ -96,26 +96,26 @@ static bool LoadPNG(SpriteLoader::Sprite
 
	if (!mask) {
 
		/* Read the text chunks */
 
		png_textp text_ptr;
 
		int num_text = 0;
 
		png_get_text(png_ptr, info_ptr, &text_ptr, &num_text);
 
		if (num_text == 0) DEBUG(misc, 0, "Warning: PNG Sprite '%s/%d.png' doesn't have x_offs and y_offs; expect graphical problems", filename, id);
 
		for (int i = 0; i < num_text; i++) {
 
			/* x_offs and y_offs are in the text-chunk of PNG */
 
			if (strcmp("x_offs", text_ptr[i].key) == 0) sprite->x_offs = strtol(text_ptr[i].text, NULL, 0);
 
			if (strcmp("y_offs", text_ptr[i].key) == 0) sprite->y_offs = strtol(text_ptr[i].text, NULL, 0);
 
		}
 

	
 
		sprite->height = info_ptr->height;
 
		sprite->width  = info_ptr->width;
 
		sprite->height = png_get_image_height(png_ptr, info_ptr);
 
		sprite->width  = png_get_image_width(png_ptr, info_ptr);
 
		sprite->AllocateData(sprite->width * sprite->height);
 
	}
 

	
 
	bit_depth  = png_get_bit_depth(png_ptr, info_ptr);
 
	colour_type = png_get_color_type(png_ptr, info_ptr);
 

	
 
	if (mask && (bit_depth != 8 || colour_type != PNG_COLOR_TYPE_PALETTE)) {
 
		DEBUG(misc, 0, "Ignoring mask for SpriteID %d as it isn't a 8 bit palette image", id);
 
		return true;
 
	}
 

	
 
	if (!mask) {
 
@@ -130,32 +130,32 @@ static bool LoadPNG(SpriteLoader::Sprite
 
			colour_type = PNG_COLOR_TYPE_RGB;
 
		}
 

	
 
		if (colour_type == PNG_COLOR_TYPE_RGB) {
 
			png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
 
		}
 

	
 
		pixelsize = sizeof(uint32);
 
	} else {
 
		pixelsize = sizeof(uint8);
 
	}
 

	
 
	png_bytep row_pointer = AllocaM(png_byte, info_ptr->width * pixelsize);
 
	png_bytep row_pointer = AllocaM(png_byte, png_get_image_width(png_ptr, info_ptr) * pixelsize);
 

	
 
	for (i = 0; i < info_ptr->height; i++) {
 
	for (i = 0; i < png_get_image_height(png_ptr, info_ptr); i++) {
 
		png_read_row(png_ptr, row_pointer, NULL);
 

	
 
		dst = sprite->data + i * info_ptr->width;
 
		dst = sprite->data + i * png_get_image_width(png_ptr, info_ptr);
 

	
 
		for (uint x = 0; x < info_ptr->width; x++) {
 
		for (uint x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
 
			if (mask) {
 
				if (row_pointer[x * sizeof(uint8)] != 0) {
 
					dst[x].r = 0;
 
					dst[x].g = 0;
 
					dst[x].b = 0;
 
					/* Alpha channel is used from the original image (to allow transparency in remap colours) */
 
					dst[x].m = row_pointer[x * sizeof(uint8)];
 
				}
 
			} else {
 
				dst[x].r = row_pointer[x * sizeof(uint32) + 0];
 
				dst[x].g = row_pointer[x * sizeof(uint32) + 1];
 
				dst[x].b = row_pointer[x * sizeof(uint32) + 2];
0 comments (0 inline, 0 general)