⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 png_helper.cpp

📁 一个开源的Flash 播放器,可以在Windows/Linux 上运行
💻 CPP
字号:
// png_helper.cpp	-- Thatcher Ulrich <tu@tulrich.com> 2004

// This source code has been donated to the Public Domain.  Do
// whatever you want with it.

// Wrapper for png file operations.  The actual work is done by the
// libpng lib.


#include "base/utility.h"
#include "base/png_helper.h"
#include "base/tu_file.h"
#include <stdio.h>

#if TU_CONFIG_LINK_TO_LIBPNG

#include <png.h>


namespace png_helper
{
	void	write_grayscale(FILE* out, uint8* data, int width, int height)
	// Writes an 8-bit grayscale image in .png format, to the
	// given output stream.
	{
		png_structp	png_ptr;
		png_infop	info_ptr;

		png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
		if (png_ptr == NULL)
		{
			// @@ log error here!
			return;
		}

		info_ptr = png_create_info_struct(png_ptr);
		if (info_ptr == NULL)
		{
			// @@ log error here!
			png_destroy_write_struct(&png_ptr, NULL);
			return;
		}

		if (setjmp(png_jmpbuf(png_ptr)))
		{
			// Error.
			png_destroy_write_struct(&png_ptr, &info_ptr);
			return;
		}

		png_init_io(png_ptr, out);

		png_set_IHDR(
			png_ptr,
			info_ptr,
			width,
			height,
			8,
			PNG_COLOR_TYPE_GRAY,
			PNG_INTERLACE_NONE,
			PNG_COMPRESSION_TYPE_DEFAULT,
			PNG_FILTER_TYPE_DEFAULT);

		png_write_info(png_ptr, info_ptr);
		
		for (int y = 0; y < height; y++)
		{
			png_write_row(png_ptr, data + width * y);
		}

		png_write_end(png_ptr, info_ptr);

		png_destroy_write_struct(&png_ptr, &info_ptr);
	}
}


#else // not TU_CONFIG_LINK_TO_JPEGLIB


namespace jpeg
{
	void	write_grayscale(FILE* out, uint8* data, int width, int height)
	{
		// no-op
	}
}


#endif // not TU_CONFIG_LINK_TO_LIBPNG


// Local Variables:
// mode: C++
// c-basic-offset: 8 
// tab-width: 8
// indent-tabs-mode: t
// End:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -