コード例 #1
0
ファイル: main.cpp プロジェクト: Stymphalian/SettlersOfCatan
void png_test(){
	png_image image;

	memset(&image, 0, (sizeof image));
	image.version = PNG_IMAGE_VERSION;

	
	if(png_image_begin_read_from_file(&image, "data/hex.png")){
		png_bytep buffer;
		image.format = PNG_FORMAT_RGBA;
		buffer = (png_bytep)malloc(PNG_IMAGE_SIZE(image));

	
		if(buffer != NULL &&  png_image_finish_read(&image, NULL, buffer, 0, NULL)){
			if(png_image_write_to_file(&image, "data/hex.jordan.png",0, buffer, 0, NULL)){
				printf("Write was successful. F**k freeing memory\n");
				char c;
				int rs = scanf("%c", &c);
				exit(0);
			}
		} else{
			if(buffer == NULL){
				png_image_free(&image);
			} else{
				free(buffer);
			}

		}
	}	
	
	printf("You've f****d up\n");
	char c;
	int rs = scanf("%c", &c);
}
コード例 #2
0
ファイル: gfwx_image.c プロジェクト: awakecoding/gfwx
int gfwx_PngWriteFile(const char* filename, uint8_t* data, uint32_t width, uint32_t height, uint32_t bytesPerPixel)
{
	int status = -1;
	int png_status;
	int row_stride;
	png_image image;

	memset(&image, 0, sizeof(png_image));
	image.version = PNG_IMAGE_VERSION;
	image.format = (bytesPerPixel == 4) ? PNG_FORMAT_BGRA : PNG_FORMAT_BGR;
	image.width = width;
	image.height = height;

	row_stride = PNG_IMAGE_ROW_STRIDE(image);

	png_status = png_image_write_to_file(&image, filename, 0,
		(const void*) data, row_stride, NULL);

	if (!png_status)
		goto exit;

	status = 1;

exit:
	png_image_free(&image);
	return status;
}
コード例 #3
0
ファイル: fmt.c プロジェクト: HaxeFoundation/hl
HL_PRIM bool HL_NAME(png_decode)( vbyte *data, int dataLen, vbyte *out, int width, int height, int stride, int format, int flags ) {
#	ifdef PNG_IMAGE_VERSION
	png_image img;
	memset(&img, 0, sizeof(img));
	img.version = PNG_IMAGE_VERSION;
	if( png_image_begin_read_from_memory(&img,data,dataLen) == 0 ) {
		png_image_free(&img);
		return false;
	}
	switch( format ) {
	case 0:
		img.format = PNG_FORMAT_RGB;
		break;
	case 1:
		img.format = PNG_FORMAT_BGR;
		break;
	case 7:
		img.format = PNG_FORMAT_RGBA;
		break;
	case 8:
		img.format = PNG_FORMAT_BGRA;
		break;
	case 9:
		img.format = PNG_FORMAT_ABGR;
		break;
	case 10:
		img.format = PNG_FORMAT_ARGB;
		break;
	default:
		png_image_free(&img);
		hl_error("Unsupported format");
		break;
	}
	if( img.width != width || img.height != height ) {
		png_image_free(&img);
		return false;
	}
	if( png_image_finish_read(&img,NULL,out,stride * (flags & 1 ? -1 : 1),NULL) == 0 ) {
		png_image_free(&img);
		return false;
	}
	png_image_free(&img);
#	else
	hl_error("PNG support is missing for this libPNG version");
#	endif
	return true;
}
コード例 #4
0
ファイル: LoadBitmap.c プロジェクト: rlugojr/Tilengine
/* Loads PNG using libpng */
static TLN_Bitmap LoadPNG (char *filename)
{
    png_image image;
    TLN_Bitmap bitmap = NULL;

    /* Only the image structure version number needs to be set. */
    memset (&image, 0, sizeof image);
    image.version = PNG_IMAGE_VERSION;

    if (png_image_begin_read_from_file (&image, filename))
    {
        int src_line_size = image.width * PNG_IMAGE_PIXEL_SIZE(image.format);
        int lut_size = PNG_IMAGE_COLORMAP_SIZE(image);
        BYTE* data = malloc (src_line_size * image.height);
        BYTE* lut = malloc (lut_size);

        bitmap = TLN_CreateBitmap (image.width, image.height, PNG_IMAGE_PIXEL_SIZE(image.format)<<3);

        /* Change this to try different formats!  If you set a colormap format
        * then you must also supply a colormap below.
        */
        image.format = PNG_FORMAT_RGB_COLORMAP;
        if (png_image_finish_read (&image, NULL, data, 0, lut))
        {
            BYTE *src, *dst;
            unsigned int c;

            png_image_free (&image);

            /* copy scanlines */
            src = data;
            for (c=0; c<image.height; c++)
            {
                dst = TLN_GetBitmapPtr (bitmap, 0, c);
                memcpy (dst, src, src_line_size);
                src += src_line_size;
            }

            /* get palette */
            {
                BYTE *src = lut;
                png_uint_32 c;
                TLN_Palette palette;
                palette = TLN_CreatePalette (image.colormap_entries);
                for (c=0; c<image.colormap_entries; c++)
                {
                    TLN_SetPaletteColor (palette, c, src[0], src[1], src[2]);
                    src += 3;
                }
                TLN_SetBitmapPalette (bitmap, palette);
            }
        }
        free (lut);
        free (data);
    }
    return bitmap;
}
コード例 #5
0
ファイル: QtHost.cpp プロジェクト: lavo27/ppsspp
void TakeScreenshot() {
	g_TakeScreenshot = false;
	mkDir(g_Config.memCardDirectory + "/PSP/SCREENSHOT");

	// First, find a free filename.
	int i = 0;

	char temp[256];
	while (i < 10000){
		if(g_Config.bScreenshotsAsPNG)
			sprintf(temp, "%s/PSP/SCREENSHOT/screen%05d.png", g_Config.memCardDirectory.c_str(), i);
		else
			sprintf(temp, "%s/PSP/SCREENSHOT/screen%05d.jpg", g_Config.memCardDirectory.c_str(), i);
		FileInfo info;
		if (!getFileInfo(temp, &info))
			break;
		i++;
	}

	// Okay, allocate a buffer.
	u8 *buffer = new u8[3 * pixel_xres * pixel_yres];
	// Silly openGL reads upside down, we flip to another buffer for simplicity.
	u8 *flipbuffer = new u8[3 * pixel_xres * pixel_yres];

	glReadPixels(0, 0, pixel_xres, pixel_yres, GL_RGB, GL_UNSIGNED_BYTE, buffer);

	for (int y = 0; y < pixel_yres; y++) {
		memcpy(flipbuffer + y * pixel_xres * 3, buffer + (pixel_yres - y - 1) * pixel_xres * 3, pixel_xres * 3);
	}

	if (g_Config.bScreenshotsAsPNG) {
		png_image png;
		memset(&png, 0, sizeof(png));
		png.version = PNG_IMAGE_VERSION;
		png.format = PNG_FORMAT_RGB;
		png.width = pixel_xres;
		png.height = pixel_yres;
		png_image_write_to_file(&png, temp, 0, flipbuffer, pixel_xres * 3, NULL);
		png_image_free(&png);
	} else {
		jpge::params params;
		params.m_quality = 90;
		compress_image_to_jpeg_file(temp, pixel_xres, pixel_yres, 3, flipbuffer, params);
	}

	delete [] buffer;
	delete [] flipbuffer;

	osm.Show(temp);
}
コード例 #6
0
ファイル: TextureReplacer.cpp プロジェクト: kg/ppsspp
void ReplacedTexture::Load(int level, void *out, int rowPitch) {
	_assert_msg_(G3D, (size_t)level < levels_.size(), "Invalid miplevel");
	_assert_msg_(G3D, out != nullptr && rowPitch > 0, "Invalid out/pitch");

	const ReplacedTextureLevel &info = levels_[level];

#ifdef USING_QT_UI
	ERROR_LOG(G3D, "Replacement texture loading not implemented for Qt");
#else
	png_image png = {};
	png.version = PNG_IMAGE_VERSION;

	FILE *fp = File::OpenCFile(info.file, "rb");
	if (!png_image_begin_read_from_stdio(&png, fp)) {
		ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", info.file.c_str(), png.message);
		return;
	}

	bool checkedAlpha = false;
	if ((png.format & PNG_FORMAT_FLAG_ALPHA) == 0) {
		// Well, we know for sure it doesn't have alpha.
		if (level == 0) {
			alphaStatus_ = ReplacedTextureAlpha::FULL;
		}
		checkedAlpha = true;
	}
	png.format = PNG_FORMAT_RGBA;

	if (!png_image_finish_read(&png, nullptr, out, rowPitch, nullptr)) {
		ERROR_LOG(G3D, "Could not load texture replacement: %s - %s", info.file.c_str(), png.message);
		return;
	}

	if (!checkedAlpha) {
		// This will only check the hashed bits.
		CheckAlphaResult res = CheckAlphaRGBA8888Basic((u32 *)out, rowPitch / sizeof(u32), png.width, png.height);
		if (res == CHECKALPHA_ANY || level == 0) {
			alphaStatus_ = ReplacedTextureAlpha(res);
		} else if (res == CHECKALPHA_ZERO && alphaStatus_ == ReplacedTextureAlpha::FULL) {
			alphaStatus_ = ReplacedTextureAlpha(res);
		}
	}

	fclose(fp);
	png_image_free(&png);
#endif
}
コード例 #7
0
ファイル: gfwx_image.c プロジェクト: awakecoding/gfwx
int gfwx_PngReadFile(const char* filename, uint8_t** pData, uint32_t* pWidth, uint32_t* pHeight, uint32_t bytesPerPixel)
{
	int status = -1;
	int png_status;
	png_image image;
	int row_stride;
	uint8_t* buffer = NULL;

	memset(&image, 0, sizeof(png_image));
	image.version = PNG_IMAGE_VERSION;

	png_status = png_image_begin_read_from_file(&image, filename);

	if (!png_status)
		goto exit;

	image.format = (bytesPerPixel == 4) ? PNG_FORMAT_BGRA : PNG_FORMAT_BGR;
	row_stride = PNG_IMAGE_ROW_STRIDE(image);

	buffer = malloc(PNG_IMAGE_SIZE(image));

	if (!buffer)
		goto exit;

	png_status = png_image_finish_read(&image, NULL, buffer, row_stride, NULL);

	if (!png_status)
		goto exit;

	*pWidth = image.width;
	*pHeight = image.height;
	*pData = buffer;

	status = 1;

exit:
	if (status < 1)
		free(buffer);

	png_image_free(&image);
	return status;
}
コード例 #8
0
ファイル: pngtocss.c プロジェクト: bluesmoon/pngtocss
static int read_png(const char *fname, image *image, gradient *g)
{
	int result = 0;
	memset(&image->image, 0, sizeof image->image);
	image->image.version = PNG_IMAGE_VERSION;

	if (png_image_begin_read_from_file(&image->image, image->file_name))
	{
		image->image.format = PNG_FORMAT_RGBA;
		image->stride = PNG_IMAGE_ROW_STRIDE(image->image);
		image->buffer = malloc(PNG_IMAGE_SIZE(image->image));
		image->pixel_bytes = PNG_IMAGE_PIXEL_SIZE(image->image.format);

		if (image->buffer != NULL) {
			if(png_image_finish_read(&image->image, NULL /*background*/,
									 image->buffer, (png_int_32)image->stride,
									 image->colormap)) {

				if(calculate_gradient(image, g))
					result = 1;
				else
					printf("pngtocss: Gradient type not supported\n");
			}
			else {
				fprintf(stderr, "pngtocss: read %s: %s\n", fname,
						image->image.message);

				png_image_free(&image->image);
			}
		}
		else
			fprintf(stderr, "pngtocss: out of memory: %lu bytes\n",
					(unsigned long)PNG_IMAGE_SIZE(image->image));
	}
	else
		/* Failed to read the argument: */
		fprintf(stderr, "pngtocss: %s: %s\n", fname, image->image.message);

	return result;
}
コード例 #9
0
ファイル: TextureReplacer.cpp プロジェクト: kg/ppsspp
void TextureReplacer::PopulateReplacement(ReplacedTexture *result, u64 cachekey, u32 hash, int w, int h) {
	int newW = w;
	int newH = h;
	LookupHashRange(cachekey >> 32, newW, newH);

	for (int i = 0; i < MAX_MIP_LEVELS; ++i) {
		const std::string hashfile = LookupHashFile(cachekey, hash, i);
		const std::string filename = basePath_ + hashfile;
		if (hashfile.empty() || !File::Exists(filename)) {
			// Out of valid mip levels.  Bail out.
			break;
		}

		ReplacedTextureLevel level;
		level.fmt = ReplacedTextureFormat::F_8888;
		level.file = filename;

#ifdef USING_QT_UI
		ERROR_LOG(G3D, "Replacement texture loading not implemented for Qt");
#else
		png_image png = {};
		png.version = PNG_IMAGE_VERSION;
		FILE *fp = File::OpenCFile(filename, "rb");
		if (png_image_begin_read_from_stdio(&png, fp)) {
			// We pad files that have been hashrange'd so they are the same texture size.
			level.w = (png.width * w) / newW;
			level.h = (png.height * h) / newH;

			result->levels_.push_back(level);
		} else {
			ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", filename.c_str(), png.message);
		}
		fclose(fp);

		png_image_free(&png);
#endif
	}

	result->alphaStatus_ = ReplacedTextureAlpha::UNKNOWN;
}
コード例 #10
0
ファイル: TextureReplacer.cpp プロジェクト: kg/ppsspp
void TextureReplacer::NotifyTextureDecoded(const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int pitch, int level, int w, int h) {
	_assert_msg_(G3D, enabled_, "Replacement not enabled");
	if (!g_Config.bSaveNewTextures) {
		// Ignore.
		return;
	}
	if (replacedInfo.addr > 0x05000000 && replacedInfo.addr < 0x08800000) {
		// Don't save the PPGe texture.
		return;
	}
	if (replacedInfo.isVideo && !allowVideo_) {
		return;
	}

	std::string hashfile = LookupHashFile(replacedInfo.cachekey, replacedInfo.hash, level);
	const std::string filename = basePath_ + hashfile;
	const std::string saveFilename = basePath_ + NEW_TEXTURE_DIR + hashfile;

	// If it's empty, it's an ignored hash, we intentionally don't save.
	if (hashfile.empty() || File::Exists(filename)) {
		// If it exists, must've been decoded and saved as a new texture already.
		return;
	}

	ReplacementCacheKey replacementKey(replacedInfo.cachekey, replacedInfo.hash);
	auto it = savedCache_.find(replacementKey);
	if (it != savedCache_.end() && File::Exists(saveFilename)) {
		// We've already saved this texture.  Let's only save if it's bigger (e.g. scaled now.)
		if (it->second.w >= w && it->second.h >= h) {
			return;
		}
	}

#ifdef _WIN32
	size_t slash = hashfile.find_last_of("/\\");
#else
	size_t slash = hashfile.find_last_of("/");
#endif
	if (slash != hashfile.npos) {
		// Create any directory structure as needed.
		const std::string saveDirectory = basePath_ + NEW_TEXTURE_DIR + hashfile.substr(0, slash);
		if (!File::Exists(saveDirectory)) {
			File::CreateFullPath(saveDirectory);
		}
	}

	// Only save the hashed portion of the PNG.
	int lookupW = w / replacedInfo.scaleFactor;
	int lookupH = h / replacedInfo.scaleFactor;
	if (LookupHashRange(replacedInfo.addr, lookupW, lookupH)) {
		w = lookupW * replacedInfo.scaleFactor;
		h = lookupH * replacedInfo.scaleFactor;
	}

#ifdef USING_QT_UI
	ERROR_LOG(G3D, "Replacement texture saving not implemented for Qt");
#else
	if (replacedInfo.fmt != ReplacedTextureFormat::F_8888) {
		saveBuf.resize((pitch * h) / sizeof(u16));
		switch (replacedInfo.fmt) {
		case ReplacedTextureFormat::F_5650:
			ConvertRGBA565ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
			break;
		case ReplacedTextureFormat::F_5551:
			ConvertRGBA5551ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
			break;
		case ReplacedTextureFormat::F_4444:
			ConvertRGBA4444ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
			break;
		case ReplacedTextureFormat::F_0565_ABGR:
			ConvertABGR565ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
			break;
		case ReplacedTextureFormat::F_1555_ABGR:
			ConvertABGR1555ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
			break;
		case ReplacedTextureFormat::F_4444_ABGR:
			ConvertABGR4444ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
			break;
		case ReplacedTextureFormat::F_8888_BGRA:
			ConvertBGRA8888ToRGBA8888(saveBuf.data(), (const u32 *)data, (pitch * h) / sizeof(u32));
			break;
		case ReplacedTextureFormat::F_8888:
			// Impossible.  Just so we can get warnings on other missed formats.
			break;
		}

		data = saveBuf.data();
		if (replacedInfo.fmt != ReplacedTextureFormat::F_8888_BGRA) {
			// We doubled our pitch.
			pitch *= 2;
		}
	}

	png_image png;
	memset(&png, 0, sizeof(png));
	png.version = PNG_IMAGE_VERSION;
	png.format = PNG_FORMAT_RGBA;
	png.width = w;
	png.height = h;
	bool success = WriteTextureToPNG(&png, saveFilename, 0, data, pitch, nullptr);
	png_image_free(&png);

	if (png.warning_or_error >= 2) {
		ERROR_LOG(COMMON, "Saving screenshot to PNG produced errors.");
	} else if (success) {
		NOTICE_LOG(G3D, "Saving texture for replacement: %08x / %dx%d", replacedInfo.hash, w, h);
	}
#endif

	// Remember that we've saved this for next time.
	ReplacedTextureLevel saved;
	saved.fmt = ReplacedTextureFormat::F_8888;
	saved.file = filename;
	saved.w = w;
	saved.h = h;
	savedCache_[replacementKey] = saved;
}
コード例 #11
0
ファイル: png2c.c プロジェクト: 01org/kernelflinger
int main(int argc, char **argv)
{
	png_image image;
	png_bytep buffer;
	unsigned int size;
	bool format_initialized = false;
	png_uint_32 format = 0;
	const char *ipath = NULL;
	const char *opath = NULL;
	const char *prefix = NULL;
	char c;

	program_name = argv[0];

	while ((c = getopt_long(argc, argv, "i:o:f:p:h", long_options, NULL)) != -1) {
		switch (c) {
		case 'i':
			ipath = optarg;
			break;
		case 'o':
			opath = optarg;
			break;
		case 'p':
			prefix = optarg;
			break;
		case 'f':
			format = get_format_from_string(optarg);
			format_initialized = true;
			break;
		case 'h':
			usage(EXIT_SUCCESS);
			break;
		default:
			usage(EXIT_FAILURE);
			break;
		}
	}

	if (!format_initialized || !opath || !ipath || !prefix)
		usage(EXIT_FAILURE);

	memset(&image, 0, sizeof(image));
	image.version = PNG_IMAGE_VERSION;

	if (!png_image_begin_read_from_file(&image, ipath))
		error("Failed to open PNG file.");

	image.format = format;
	size = PNG_IMAGE_SIZE(image);

	buffer = malloc(size);
	if (!buffer)
		error("Failed to allocate buffer.");

	if (!png_image_finish_read(&image, NULL, buffer, 0, NULL))
		error("Failed to read  PNG file.");

	write_to_c_source(prefix, buffer, size, opath);

	png_image_free(&image);
	free(buffer);

	return EXIT_SUCCESS;
}
コード例 #12
0
ファイル: pngio.cpp プロジェクト: pbhogan/pngio
png_image::~png_image( void )
{
	png_image_free( this );
}
コード例 #13
0
ファイル: pngio.cpp プロジェクト: pbhogan/pngio
static uint8_t png_read( png_structp readPtr, png_image * image, uint32_t flags )
{
//	png_set_error_fn( readPtr, NULL, png_user_error, NULL );

	png_infop infoPtr = png_create_info_struct( readPtr );
	if (!infoPtr) 
	{
		pngio_error( "Couldn't initialize PNG info struct." );
		png_destroy_read_struct( & readPtr, NULL, NULL );
		return 0;
	}
	
	if (setjmp( png_jmpbuf( readPtr ) ))
	{
		pngio_error( "An error occured while reading the PNG file." );
		png_destroy_read_struct( & readPtr, & infoPtr, NULL );
		png_image_free( image );
		return 0;
	}

	png_set_sig_bytes( readPtr, 8 );
	
	#ifdef PNG_APPLE_MODE_SUPPORTED 
	if (png_get_apple_mode())
	{
		png_set_keep_unknown_chunks( readPtr, PNG_HANDLE_CHUNK_ALWAYS, NULL, 0 );
		png_set_read_user_chunk_fn( readPtr, NULL, png_read_user_chunk );
	}
	#endif
	
	png_read_info( readPtr, infoPtr );
	
	png_uint_32 w = png_get_image_width( readPtr, infoPtr );
	png_uint_32 h = png_get_image_height( readPtr, infoPtr );
	png_uint_32 bitDepth = png_get_bit_depth( readPtr, infoPtr );
	png_uint_32 channels = png_get_channels( readPtr, infoPtr );
	png_uint_32 interlaceType = png_get_interlace_type( readPtr, infoPtr );
	png_uint_32 colorType = png_get_color_type( readPtr, infoPtr );
	
	switch (colorType) 
	{
		case PNG_COLOR_TYPE_PALETTE:
			png_set_palette_to_rgb( readPtr );
			channels = 3;           
			break;
		case PNG_COLOR_TYPE_GRAY:
			if (bitDepth < 8)
			{
				png_set_expand_gray_1_2_4_to_8( readPtr );
				bitDepth = 8;
			}
			png_set_gray_to_rgb( readPtr );
			break;
	}
	
	if (png_get_valid( readPtr, infoPtr, PNG_INFO_tRNS )) 
	{
		png_set_tRNS_to_alpha( readPtr );
		channels += 1;
	}
	else if (!(colorType & PNG_COLOR_MASK_ALPHA)) 
	{
		png_set_add_alpha( readPtr, 0xff, PNG_FILLER_AFTER );
	}
	
	if (bitDepth == 16)
	{
		png_set_strip_16( readPtr );
	}

	#ifdef PNG_APPLE_MODE_SUPPORTED
	if (png_get_apple_mode())
	{
		if (flags & PNG_IMAGE_PREMULTIPLY_ALPHA)
		{
			png_set_read_user_transform_fn( readPtr, png_read_swap_transform );
		}
		else
		{
			png_set_read_user_transform_fn( readPtr, png_read_swap_and_unpremultiply_transform );
		}
		png_set_user_transform_info( readPtr, NULL, bitDepth, channels );
		png_read_update_info( readPtr, infoPtr );
	}
	else
	#endif
	{
		if (flags & PNG_IMAGE_PREMULTIPLY_ALPHA)
		{
			png_set_read_user_transform_fn( readPtr, png_read_premultiply_transform );
		}
	}

	png_image_alloc( image, w, h );
	png_bytep p = image->data;
	
	const size_t passCount = interlaceType == PNG_INTERLACE_NONE ? 1 : png_set_interlace_handling( readPtr );
	const size_t bytesPerRow = w * 4;
	if (flags & PNG_IMAGE_FLIP_VERTICAL)
	{
		for (size_t pass = 0; pass < passCount; pass++)
		{
			for (size_t i = 0; i < h; i++) 
			{
				png_read_row( readPtr, p + (bytesPerRow * (h - i - 1)), NULL );
			}
		}
	}
	else
	{
//		png_bytep rp[h];
//		for (size_t i = 0; i < h; i++) 
//		{
//			rp[i] = p + (bytesPerRow * i);
//		}
//		png_read_image( readPtr, rp );
		for (size_t pass = 0; pass < passCount; pass++)
		{
			for (size_t i = 0; i < h; i++) 
			{
				png_read_row( readPtr, p + (bytesPerRow * i), NULL );
			}
		}
	}
	
	png_destroy_read_struct( & readPtr, & infoPtr, NULL );
	
	return 1;
}