Add new function to output grayscale PNG images. Needed for ticket:652 net map/savegame format.

git-svn-id: https://warzone2100.svn.sourceforge.net/svnroot/warzone2100/trunk@7817 4a71c877-e1ca-e34f-864e-861f7616d084
master
Per Inge Mathisen 2009-06-25 18:51:46 +00:00 committed by Git SVN Gateway
parent d58af93eb5
commit 4d33f6a51d
3 changed files with 32 additions and 7 deletions

View File

@ -25,8 +25,6 @@
#include <physfs.h>
#define PNG_BYTES_TO_CHECK 8
static const unsigned int channelBitdepth = 8;
static const unsigned int channelsPerPixel = 3;
// PNG callbacks
static void wzpng_read_data(png_structp ctx, png_bytep area, png_size_t size)
@ -172,11 +170,19 @@ BOOL iV_loadImage_PNG(const char *fileName, iV_Image *image)
return true;
}
void iV_saveImage_PNG(const char *fileName, const iV_Image *image)
static void internal_saveImage_PNG(const char *fileName, const iV_Image *image, int color_type)
{
unsigned char** scanlines = NULL;
png_infop info_ptr = NULL;
png_structp png_ptr = NULL;
unsigned int channelsPerPixel = 3;
if (color_type == PNG_COLOR_TYPE_GRAY)
{
channelsPerPixel = 1;
}
ASSERT(image->depth != 0, "Bad depth");
PHYSFS_file* fileHandle = PHYSFS_openWrite(fileName);
if (fileHandle == NULL)
@ -207,7 +213,7 @@ void iV_saveImage_PNG(const char *fileName, const iV_Image *image)
else
{
unsigned int currentRow;
unsigned int row_stride = image->width * channelsPerPixel;
unsigned int row_stride = image->width * channelsPerPixel * image->depth / 8;
scanlines = malloc(sizeof(const char*) * image->height);
if (scanlines == NULL)
@ -244,8 +250,8 @@ void iV_saveImage_PNG(const char *fileName, const iV_Image *image)
// so to spare some CPU cycles we comment this out.
// png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
png_set_IHDR(png_ptr, info_ptr, image->width, image->height, channelBitdepth,
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_set_IHDR(png_ptr, info_ptr, image->width, image->height, image->depth,
color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
// Create an array of scanlines
for (currentRow = 0; currentRow < image->height; ++currentRow)
@ -263,3 +269,13 @@ void iV_saveImage_PNG(const char *fileName, const iV_Image *image)
free(scanlines);
return PNGWriteCleanup(&info_ptr, &png_ptr, fileHandle);
}
void iV_saveImage_PNG(const char *fileName, const iV_Image *image)
{
internal_saveImage_PNG(fileName, image, PNG_COLOR_TYPE_RGB);
}
void iV_saveImage_PNG_Gray(const char *fileName, const iV_Image *image)
{
internal_saveImage_PNG(fileName, image, PNG_COLOR_TYPE_GRAY);
}

View File

@ -41,4 +41,13 @@ BOOL iV_loadImage_PNG(const char *fileName, iV_Image *image);
*/
void iV_saveImage_PNG(const char *fileName, const iV_Image *image);
/*!
* Save a PNG from image into file using grayscale colour model.
*
* \param fileName output file to save to
* \param image Texture to read from
* \return true on success, false otherwise
*/
void iV_saveImage_PNG_Gray(const char *fileName, const iV_Image *image);
#endif // _LIBIVIS_COMMON_PNG_H_

View File

@ -389,7 +389,7 @@ static const unsigned int channelsPerPixel = 3;
void screenDoDumpToDiskIfRequired(void)
{
const char* fileName = screendump_filename;
static iV_Image image = { 0, 0, 0, NULL };
static iV_Image image = { 0, 0, 8, NULL };
if (!screendump_required) return;
debug( LOG_3D, "Saving screenshot %s\n", fileName );