当前位置: 首页>>代码示例>>C++>>正文


C++ png_set_swap函数代码示例

本文整理汇总了C++中png_set_swap函数的典型用法代码示例。如果您正苦于以下问题:C++ png_set_swap函数的具体用法?C++ png_set_swap怎么用?C++ png_set_swap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了png_set_swap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: init

	void init()
	{
		char buf[PNG_BYTES_TO_CHECK];

		// read in some of the signature bytes
		io_error_if( fread( buf, 1, PNG_BYTES_TO_CHECK, get() ) != detail::PNG_BYTES_TO_CHECK,
			     "png_check_validity: fail to read file" );
		// compare the first PNG_BYTES_TO_CHECK bytes of the signature.
		io_error_if( png_sig_cmp( (png_bytep)buf, (png_size_t)0, detail::PNG_BYTES_TO_CHECK ) != 0,
			     "png_check_validity: invalid png file" );

		_png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
		io_error_if( _png_ptr == NULL, "png_get_file_size: fail to call png_create_write_struct()" );
		// allocate/initialize the image information data
		_info_ptr = png_create_info_struct( _png_ptr );
		if( _info_ptr == NULL )
		{
			png_destroy_read_struct( &_png_ptr, png_infopp_NULL, png_infopp_NULL );
			io_error( "png_get_file_size: fail to call png_create_info_struct()" );
		}
		if( setjmp( png_jmpbuf( _png_ptr ) ) )
		{
			//free all of the memory associated with the png_ptr and info_ptr
			png_destroy_read_struct( &_png_ptr, &_info_ptr, png_infopp_NULL );
			io_error( "png_get_file_size: fail to call setjmp()" );
		}
		png_init_io( _png_ptr, get() );
		png_set_sig_bytes( _png_ptr, PNG_BYTES_TO_CHECK );
		png_read_info( _png_ptr, _info_ptr );
		if( little_endian() && png_get_bit_depth( _png_ptr, _info_ptr ) > 8 )
			png_set_swap( _png_ptr );
	}
开发者ID:Achammem,项目名称:TuttleOFX,代码行数:32,代码来源:png_adds.hpp

示例2: WritePNG

errort	WritePNG( FILE * fp , unsigned char * data, unsigned int sizeX, unsigned int sizeY, int img_depth, int img_alpha)
{
  png_structp png_ptr = png_create_write_struct
    (PNG_LIBPNG_VER_STRING, (png_voidp)NULL,NULL,NULL);
  if (!png_ptr)
    return BadFormat;
  
  png_infop info_ptr = png_create_info_struct(png_ptr);
  if (!info_ptr) {
    png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
    return BadFormat;
  }
  if (setjmp(png_ptr->jmpbuf)) {
    png_destroy_write_struct(&png_ptr, &info_ptr);
    return BadFormat;
  }

  png_init_io(png_ptr, fp);

  png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
  png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);

  /* set other zlib parameters */
  png_set_compression_mem_level(png_ptr, 8);
  png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
  png_set_compression_window_bits(png_ptr, 15);
  png_set_compression_method(png_ptr, 8);
  
  png_set_IHDR(png_ptr, 
	       info_ptr, 
	       sizeX,
	       sizeY,
	       img_depth, 
	       img_alpha?PNG_COLOR_TYPE_RGB_ALPHA:PNG_COLOR_TYPE_RGB, 
	       PNG_INTERLACE_NONE,
	       PNG_COMPRESSION_TYPE_DEFAULT, 
	       PNG_FILTER_TYPE_DEFAULT);
  
  png_write_info(png_ptr, info_ptr);
# if __BYTE_ORDER != __BIG_ENDIAN  
  if (img_depth==16) {
    png_set_swap(png_ptr);
  }
#endif
  int stride = (img_depth/8)*(img_alpha?4:3);
  png_byte **row_pointers = new png_byte*[sizeY];
  for (unsigned int i=0;i<sizeY;i++) {
    row_pointers[i]= (png_byte *)&data[stride*i*sizeX];
  }
  png_write_image (png_ptr,row_pointers);
  png_write_end(png_ptr, info_ptr);
  png_write_flush(png_ptr);
  png_destroy_write_struct(&png_ptr, &info_ptr);

  //free (data);
  delete [] row_pointers;
  return Ok;
}
开发者ID:bsmr-games,项目名称:Privateer-Gemini-Gold,代码行数:58,代码来源:shrinker.cpp

示例3: png_read_info

// part of this being separated allows for us to play nicely with the setjmp of libpng
bool ImageSourcePng::loadHeader()
{
	bool success = true;

	if( setjmp( png_jmpbuf(mPngPtr) ) ) {
		success = false;
	}
	else {
		png_read_info( mPngPtr, mInfoPtr );

		png_uint_32 width, height;
		int bitDepth, colorType, interlaceType, compressionType, filterMethod;

		if( ! png_get_IHDR( mPngPtr, mInfoPtr, &width, &height, &bitDepth, &colorType, &interlaceType, &compressionType, &filterMethod ) ) {
			png_destroy_read_struct( &mPngPtr, &mInfoPtr, (png_infopp)NULL );
			mPngPtr = 0;
			return false;
		}

		setSize( width, height );
		setDataType( ( bitDepth == 16 ) ? ImageIo::UINT16 : ImageIo::UINT8 );
		
	#ifdef CINDER_LITTLE_ENDIAN
		png_set_swap( mPngPtr );
	#endif

		switch( colorType ) {
			case PNG_COLOR_TYPE_GRAY:
				setColorModel( ImageIo::CM_GRAY );
				setChannelOrder( ImageIo::Y );			
			break;
			case PNG_COLOR_TYPE_GRAY_ALPHA:
				setColorModel( ImageIo::CM_GRAY );
				setChannelOrder( ImageIo::YA );
			break;
			case PNG_COLOR_TYPE_RGB:
			case PNG_COLOR_TYPE_PALETTE:
				setColorModel( ImageIo::CM_RGB );
				setChannelOrder( ImageIo::RGB );
			break;
			case PNG_COLOR_TYPE_RGB_ALPHA:
				setColorModel( ImageIo::CM_RGB );
				setChannelOrder( ImageIo::RGBA );
			break;
			default:
				throw ImageSourcePngException( "Unexpected png color type." );
		}	

		png_set_expand_gray_1_2_4_to_8( mPngPtr );
		png_set_palette_to_rgb( mPngPtr );
		png_set_tRNS_to_alpha( mPngPtr );
		
		png_read_update_info( mPngPtr, mInfoPtr );
	}
	
	return success;
}
开发者ID:CinimodStudio,项目名称:Cinder,代码行数:58,代码来源:ImageSourcePng.cpp

示例4: png_write

void png_write( const char *myfile, unsigned char *data, unsigned int width, unsigned int height, bool alpha, char bpp )
{
    FILE *fp = VSFileSystem::vs_open( myfile, "wb" );
    png_structp png_ptr = png_create_write_struct
                              ( PNG_LIBPNG_VER_STRING, (png_voidp) NULL, NULL, NULL );
    if (!png_ptr)
        return;
    png_infop info_ptr = png_create_info_struct( png_ptr );
    if (!info_ptr) {
        png_destroy_write_struct( &png_ptr, (png_infopp) NULL );
        return;
    }
    if ( setjmp( png_ptr->jmpbuf ) ) {
        png_destroy_write_struct( &png_ptr, &info_ptr );
        VSFileSystem::vs_close( fp );
        return;
    }
    png_init_io( png_ptr, fp );
    png_set_filter( png_ptr, 0, PNG_FILTER_NONE );
    png_set_compression_level( png_ptr, Z_BEST_COMPRESSION );

    /* set other zlib parameters */
    png_set_compression_mem_level( png_ptr, 8 );
    png_set_compression_strategy( png_ptr, Z_DEFAULT_STRATEGY );
    png_set_compression_window_bits( png_ptr, 15 );
    png_set_compression_method( png_ptr, 8 );

    png_set_IHDR( png_ptr,
                  info_ptr,
                  width,
                  height,
                  bpp,
                  alpha ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB,
                  PNG_INTERLACE_NONE,
                  PNG_COMPRESSION_TYPE_DEFAULT,
                  PNG_FILTER_TYPE_DEFAULT );

    png_write_info( png_ptr, info_ptr );
# if __BYTE_ORDER != __BIG_ENDIAN
    if (bpp == 16)
        png_set_swap( png_ptr );
#endif
    int stride = (bpp/8)*(alpha ? 4 : 3);
    png_byte **row_pointers = new png_byte*[height];
    for (unsigned int i = 0; i < height; i++)
        row_pointers[i] = (png_byte*) &data[stride*i*width];
    png_write_image( png_ptr, row_pointers );
    png_write_end( png_ptr, info_ptr );
    png_write_flush( png_ptr );
    png_destroy_write_struct( &png_ptr, &info_ptr );

    VSFileSystem::vs_close( fp );
    free( data );
    delete[] row_pointers;
}
开发者ID:vegastrike,项目名称:Vega-Strike-Engine-Source,代码行数:55,代码来源:bmp_to_png.cpp

示例5: SavePng

			///
			/// LDR I/O, only available to LDR images
			void SavePng(const std::string& file_name)
			{
				BOOST_STATIC_ASSERT( (boost::is_same<T, UCHAR>::value) );
				BOOST_STATIC_ASSERT( channel == 3 );

				size_t pos = file_name.find_last_of(".");
				std::string file_type = file_name.substr(++pos, std::string::npos);
				boost::to_lower(file_type);
				assert(file_type == "png");

				FILE* fp;
				if((fp = fopen(file_name.c_str(), "wb")) == NULL)
					assert(0);

				png_structp _png_ptr;
				png_infop _info_ptr;
			    
				_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
				if(!_png_ptr)
					assert(0);

				_info_ptr = png_create_info_struct(_png_ptr);
				if (!_info_ptr) 
				{
					png_destroy_write_struct(&_png_ptr, png_infopp_NULL);
					assert(0);
				}

				if (setjmp(png_jmpbuf(_png_ptr))) 
				{
					png_destroy_write_struct(&_png_ptr, &_info_ptr);
					assert(0);
				}
			    
				png_init_io(_png_ptr, fp);
					
				png_set_IHDR(_png_ptr, _info_ptr, GetWidth(), GetHeight(), 8,
								 PNG_COLOR_TYPE_RGB,
								 PNG_INTERLACE_NONE,
								 PNG_COMPRESSION_TYPE_DEFAULT,
								 PNG_FILTER_TYPE_DEFAULT);
			    
				png_write_info(_png_ptr, _info_ptr);

				if (little_endian() && false /* RGB depth always = 8 */)
					png_set_swap(_png_ptr);
			    
				for(UINT y = 0; y < GetHeight(); ++y) 
					png_write_row(_png_ptr, (png_bytep)GetPixelPtr(0, y));

				png_write_end(_png_ptr,_info_ptr);
				fclose(fp);
			}
开发者ID:qiangd6,项目名称:QTK,代码行数:55,代码来源:qImage.hpp

示例6: Raster16ToPng16

void Raster16ToPng16(
	const char*		name,
	const uint16*	raster,
	int				w,
	int				h,
	FILE*			flog )
{
	FILE*			f = FileOpenOrDie( name, "w", flog );
	png_structp		png_ptr;
	png_infop		info_ptr;
	png_byte**		prow;

// init I/O
	png_ptr		= png_create_write_struct(
					PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );

	info_ptr	= png_create_info_struct( png_ptr );

	png_init_io( png_ptr, f );

// header
	png_set_IHDR( png_ptr, info_ptr,
		w, h, 16, PNG_COLOR_TYPE_GRAY,
		PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
		PNG_FILTER_TYPE_BASE );

	png_write_info( png_ptr, info_ptr );

// data
	prow = (png_byte**)malloc( h * sizeof(png_byte*) );

	for( int i = 0; i < h; ++i )
		prow[i] = (png_byte*)(raster + w * i);

	png_set_swap( png_ptr );
	png_write_image( png_ptr, prow );
	free( prow );

// cleanup
	png_write_end( png_ptr, NULL );
	png_destroy_write_struct( &png_ptr, &info_ptr );
	fclose( f );
}
开发者ID:coocoky,项目名称:Alignment_Projects,代码行数:43,代码来源:ImageIO.cpp

示例7: PyErr_Clear


//.........这里部分代码省略.........
    }

    if (fp)
    {
        png_init_io(png_ptr, fp);
    }
    else
    {
        png_set_read_fn(png_ptr, (void*)py_file, &read_png_data);
    }
    png_set_sig_bytes(png_ptr, 8);
    png_read_info(png_ptr, info_ptr);

    png_uint_32 width = png_get_image_width(png_ptr, info_ptr);
    png_uint_32 height = png_get_image_height(png_ptr, info_ptr);

    int bit_depth = png_get_bit_depth(png_ptr, info_ptr);

    // Unpack 1, 2, and 4-bit images
    if (bit_depth < 8)
        png_set_packing(png_ptr);

    // If sig bits are set, shift data
    png_color_8p sig_bit;
    if ((png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_PALETTE) &&
        png_get_sBIT(png_ptr, info_ptr, &sig_bit))
    {
        png_set_shift(png_ptr, sig_bit);
    }

    // Convert big endian to little
    if (bit_depth == 16)
    {
        png_set_swap(png_ptr);
    }

    // Convert palletes to full RGB
    if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE)
    {
        png_set_palette_to_rgb(png_ptr);
        bit_depth = 8;
    }

    // If there's an alpha channel convert gray to RGB
    if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY_ALPHA)
    {
        png_set_gray_to_rgb(png_ptr);
    }

    png_set_interlace_handling(png_ptr);
    png_read_update_info(png_ptr, info_ptr);

    /* read file */
    if (setjmp(png_jmpbuf(png_ptr)))
    {
        throw Py::RuntimeError(
            "_image_module::readpng: error during read_image");
    }

    png_bytep *row_pointers = new png_bytep[height];
    png_uint_32 row;

    for (row = 0; row < height; row++)
    {
        row_pointers[row] = new png_byte[png_get_rowbytes(png_ptr,info_ptr)];
    }
开发者ID:keflavich,项目名称:matplotlib,代码行数:67,代码来源:_png.cpp

示例8: ReadPNG

unsigned char *	ReadPNG(FILE * fp, unsigned int & sizeX, unsigned int &sizeY, int &img_depth, int &img_color_type, unsigned char *** row_pointer_ptr)
{
	png_structp png_ptr;
	png_bytepp row_pointers;
	png_infop info_ptr;
	int  interlace_type;

	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
	if (png_ptr == NULL)
	{
		exit(1);
		return NULL;
	}
	info_ptr = png_create_info_struct(png_ptr);
	if (info_ptr == NULL)
	{
		png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
		fprintf(stderr,"VSImage ERROR : PNG info_ptr == NULL !!!\n");
		exit(1);
		return NULL;
	}
	if (setjmp(png_jmpbuf(png_ptr)))
	{
		/* Free all of the memory associated with the png_ptr and info_ptr */
		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
		/* If we get here, we had a problem reading the file */
		exit(1);
		return NULL;
	}

        png_init_io(png_ptr, fp);

	//png_set_sig_bytes(png_ptr, 8);
	png_read_info(png_ptr, info_ptr);  /* read all PNG info up to image data */
	png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *)&sizeX, (png_uint_32 *)&sizeY, &img_depth, &img_color_type, &interlace_type, NULL, NULL);

# if __BYTE_ORDER != __BIG_ENDIAN
	if (img_depth==16)
		png_set_swap (png_ptr);
#endif

	if (img_depth==16)//for now
		png_set_strip_16(png_ptr);
	if (img_color_type == PNG_COLOR_TYPE_PALETTE)
		png_set_palette_to_rgb(png_ptr);
		   
	if (img_color_type == PNG_COLOR_TYPE_GRAY && img_depth < 8)
		png_set_gray_1_2_4_to_8(png_ptr);

	png_set_expand (png_ptr);
	png_read_update_info (png_ptr,info_ptr);
	png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *)&sizeX, (png_uint_32 *)&sizeY, &img_depth, &img_color_type, &interlace_type, NULL, NULL);
	row_pointers = (unsigned char **)malloc (sizeof (unsigned char *) *sizeY);
	int numchan=1;
	if (img_color_type&PNG_COLOR_MASK_COLOR)
		numchan =3;
	if (img_color_type &PNG_COLOR_MASK_PALETTE)
		numchan =1;
	if (img_color_type&PNG_COLOR_MASK_ALPHA)
		numchan++;
	unsigned long stride = numchan*sizeof (unsigned char)*img_depth/8;
	unsigned char * image = (unsigned char *) malloc (stride*sizeX*sizeY);
	for (unsigned int i=0;i<sizeY;i++)
	{
		row_pointers[i] = &image[i*stride*sizeX];
	}
	png_read_image (png_ptr,row_pointers);
	unsigned char * result;
        result = image;
	//free (row_pointers);
        *row_pointer_ptr=row_pointers;
	png_read_end(png_ptr, info_ptr);
	png_destroy_read_struct(&png_ptr, &info_ptr, NULL);

	return result;
}
开发者ID:bsmr-games,项目名称:Privateer-Gemini-Gold,代码行数:76,代码来源:shrinker.cpp

示例9: LoadPng

			///
			/// LDR I/O, only available to LDR images
			void LoadPng(const std::string& file_name)
			{
				BOOST_STATIC_ASSERT( (boost::is_same<T, UCHAR>::value) );
				BOOST_STATIC_ASSERT( channel == 3 );

				size_t pos = file_name.find_last_of(".");
				std::string file_type = file_name.substr(++pos, std::string::npos);
				boost::to_lower(file_type);
				assert(file_type == "png");

				FILE* fp = 0;
				fp = fopen(file_name.c_str(), "rb");
				if(fp == 0) 
				{
					assert(0);
					return;
				}
						
				char buf[PNG_BYTES_TO_CHECK];
				png_structp _png_ptr;
				png_infop _info_ptr;

				// read in some of the signature bytes
				if(fread(buf, 1, PNG_BYTES_TO_CHECK, fp) != PNG_BYTES_TO_CHECK)
					assert(0);

				// compare the first PNG_BYTES_TO_CHECK bytes of the signature.
				if(png_sig_cmp((png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK) != 0)
					assert(0);

				_png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
				if(_png_ptr == NULL)
					assert(0);

				// allocate/initialize the image information data
				_info_ptr = png_create_info_struct(_png_ptr);
				if (_info_ptr == NULL) 
				{
					png_destroy_read_struct(&_png_ptr,png_infopp_NULL,png_infopp_NULL);
					assert(0);
				}

				if (setjmp(png_jmpbuf(_png_ptr))) 
				{
					//free all of the memory associated with the png_ptr and info_ptr
					png_destroy_read_struct(&_png_ptr, &_info_ptr, png_infopp_NULL);
					assert(0);
				}

				png_init_io(_png_ptr, fp);
				png_set_sig_bytes(_png_ptr, PNG_BYTES_TO_CHECK);
				png_read_info(_png_ptr, _info_ptr);

				if (little_endian() && png_get_bit_depth(_png_ptr,_info_ptr)>8)
					png_set_swap(_png_ptr);
						
				SetSize((png_get_image_width(_png_ptr,_info_ptr)), (png_get_image_height(_png_ptr,_info_ptr)));
												  
				png_uint_32 width, height;
				int bit_depth, color_type, interlace_type;

				png_get_IHDR(_png_ptr, _info_ptr, &width, &height,&bit_depth,&color_type,&interlace_type, int_p_NULL, int_p_NULL);
			    
				for(png_uint_32 y = 0; y < height; ++y) 
					png_read_row(_png_ptr,(png_bytep)GetPixelPtr(0, y), NULL);

				png_read_end(_png_ptr,NULL);
				fclose(fp);
			}
开发者ID:qiangd6,项目名称:QTK,代码行数:71,代码来源:qImage.hpp

示例10: png_info_callback

void png_info_callback(png_structp png_ptr, png_infop info_ptr)
{
	int bit_depth, color_type, intent;
	double gamma;
	int bytes_per_pixel=3;
	struct cached_image *cimg;

	cimg=global_cimg;

	bit_depth=png_get_bit_depth(png_ptr, info_ptr);
	color_type=png_get_color_type(png_ptr, info_ptr);
	if (color_type == PNG_COLOR_TYPE_PALETTE)
        	png_set_expand(png_ptr);
    	if (color_type == PNG_COLOR_TYPE_GRAY &&
        	bit_depth < 8) png_set_expand(png_ptr);
	if (png_get_valid(png_ptr, info_ptr,
        	PNG_INFO_tRNS)){
		png_set_expand(png_ptr); /* Legacy version of
		png_set_tRNS_to_alpha(png_ptr); */
		bytes_per_pixel++;
	}
	if (color_type == PNG_COLOR_TYPE_GRAY ||
		color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
		png_set_gray_to_rgb(png_ptr);
	if (bit_depth==16){
#ifndef REPACK_16
#ifdef C_LITTLE_ENDIAN
		/* We use native endianity only if unsigned short is 2-byte
		 * because otherwise we have to reassemble the buffer so we
		 * will leave in the libpng-native big endian.
		 */
		png_set_swap(png_ptr);
#endif /* #ifdef C_LITTLE_ENDIAN */
#endif /* #ifndef REPACK_16 */
		bytes_per_pixel*=sizeof(unsigned short);
	}
	png_set_interlace_handling(png_ptr);
	if (color_type==PNG_COLOR_TYPE_RGB_ALPHA
		||color_type==PNG_COLOR_TYPE_GRAY_ALPHA){
		if (bytes_per_pixel==3
			||bytes_per_pixel==3*sizeof(unsigned short))
			bytes_per_pixel=4*bytes_per_pixel/3;
	}
	cimg->width=png_get_image_width(png_ptr,info_ptr);
	cimg->height=png_get_image_height(png_ptr,info_ptr);
	cimg->buffer_bytes_per_pixel=bytes_per_pixel;
	if (png_get_sRGB(png_ptr, info_ptr, &intent)){
		gamma=sRGB_gamma;
	}
	else
 	{              
  		if (!png_get_gAMA(png_ptr, info_ptr, &gamma)){
			gamma=sRGB_gamma;
		}
	}
	cimg->red_gamma=gamma;
	cimg->green_gamma=gamma;
	cimg->blue_gamma=gamma;
	png_read_update_info(png_ptr,info_ptr);                 
	cimg->strip_optimized=0;
	header_dimensions_known(cimg);
}
开发者ID:Gingar,项目名称:port,代码行数:62,代码来源:png.c

示例11: OSG_CHECK_ARG


//.........这里部分代码省略.........
            
#if defined(GL_BGRA) || defined(GL_BGRA_EXT)
        case Image::OSG_BGRA_PF:
#endif
        case Image::OSG_RGBA_PF:
            ctype = PNG_COLOR_TYPE_RGB_ALPHA;           
            break;
            
        default:
            FWARNING(("PNGImageFileType::write: unknown pixel format %d!\n",
                      pImage->getPixelFormat()));
            png_destroy_write_struct(&png_ptr,  NULL);

            return 0;
    }
    
    Int32 bit_depth;

    switch (pImage->getDataType()) 
    {
        case Image::OSG_UINT8_IMAGEDATA:
            bit_depth = 8;
            break;
        case Image::OSG_UINT16_IMAGEDATA:
            bit_depth = 16;
            break;
        default:
            FWARNING (("Invalid pixeldepth, cannot store data\n"));
            return 0;
    };

    png_set_IHDR(png_ptr, 
                 info_ptr, 
                 pImage->getWidth(), 
                 pImage->getHeight(),
                 bit_depth,
                 ctype,      
                 PNG_INTERLACE_NONE, 
                 PNG_COMPRESSION_TYPE_BASE, 
                 PNG_FILTER_TYPE_BASE);
    
    /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */
    /* note that if sRGB is present the gAMA and cHRM chunks must be ignored
    * on read and must be written in accordance with the sRGB profile */

    /* Write the file header information.  REQUIRED */

    png_write_info(png_ptr, info_ptr);

#if BYTE_ORDER == LITTLE_ENDIAN
    if (bit_depth == 16)
        png_set_swap(png_ptr);
#endif
    
    if(pImage->getPixelFormat() == Image::OSG_BGR_PF ||
       pImage->getPixelFormat() == Image::OSG_BGRA_PF)
    {
        /* flip BGR pixels to RGB */
        png_set_bgr(png_ptr);
        
        /* swap location of alpha bytes from ARGB to RGBA */
        png_set_swap_alpha(png_ptr);
    }
    
    /* The easiest way to write the image (you may have a different memory
     * layout, however, so choose what fits your needs best).  You need to
     * use the first method if you aren't handling interlacing yourself.
     */

    png_bytep *row_pointers = new png_bytep [pImage->getHeight()];
    
    for(Int32 k = 0; k < pImage->getHeight(); k++)
    {
        row_pointers[k] = 
            (const_cast<UInt8 *>(pImage->getData())) + 
            (pImage->getHeight() - 1 - k) * 
            pImage->getWidth() * pImage->getBpp();
    }
    
    /* write out the entire image data in one call */
    png_write_image(png_ptr, row_pointers);

    /* It is REQUIRED to call this to finish writing the rest of the file */
    png_write_end(png_ptr, info_ptr);
    
    /* clean up after the write, and free any memory allocated */
    png_destroy_write_struct(&png_ptr, &info_ptr);

    delete [] row_pointers;

    /* that's it */
    return bufferInfo.length;
#else
    SWARNING << getMimeType() 
             << " storeData is not compiled into the current binary " 
             << std::endl;

    return 0;
#endif
}
开发者ID:Langkamp,项目名称:OpenSGDevMaster_Toolbox,代码行数:101,代码来源:OSGPNGImageFileType.cpp

示例12: colorspace_set_default_role


//.........这里部分代码省略.........
				bit_depth = 8;
			}
			break;
		default:
			printf("PNG format not supported\n");
			longjmp(png_jmpbuf(png_ptr), 1);
	}
	
	ibuf = IMB_allocImBuf(width, height, 8 * bytesperpixel, 0);

	if (ibuf) {
		ibuf->ftype = PNG;
		if (bit_depth == 16)
			ibuf->ftype |= PNG_16BIT;

		if (png_get_valid(png_ptr, info_ptr, PNG_INFO_pHYs)) {
			int unit_type;
			png_uint_32 xres, yres;

			if (png_get_pHYs(png_ptr, info_ptr, &xres, &yres, &unit_type))
				if (unit_type == PNG_RESOLUTION_METER) {
					ibuf->ppm[0] = xres;
					ibuf->ppm[1] = yres;
				}
		}
	}
	else {
		printf("Couldn't allocate memory for PNG image\n");
	}

	if (ibuf && ((flags & IB_test) == 0)) {
		if (bit_depth == 16) {
			imb_addrectfloatImBuf(ibuf);
			png_set_swap(png_ptr);

			pixels16 = MEM_mallocN(ibuf->x * ibuf->y * bytesperpixel * sizeof(png_uint_16), "pixels");
			if (pixels16 == NULL) {
				printf("Cannot allocate pixels array\n");
				longjmp(png_jmpbuf(png_ptr), 1);
			}

			/* allocate memory for an array of row-pointers */
			row_pointers = (png_bytepp) MEM_mallocN(ibuf->y * sizeof(png_uint_16p), "row_pointers");
			if (row_pointers == NULL) {
				printf("Cannot allocate row-pointers array\n");
				longjmp(png_jmpbuf(png_ptr), 1);
			}

			/* set the individual row-pointers to point at the correct offsets */
			for (i = 0; i < ibuf->y; i++) {
				row_pointers[ibuf->y - 1 - i] = (png_bytep)
				                                ((png_uint_16 *)pixels16 + (i * ibuf->x) * bytesperpixel);
			}

			png_read_image(png_ptr, row_pointers);

			/* copy image data */

			to_float = ibuf->rect_float;
			from16 = pixels16;

			switch (bytesperpixel) {
				case 4:
					for (i = ibuf->x * ibuf->y; i > 0; i--) {
						to_float[0] = from16[0] / 65535.0;
						to_float[1] = from16[1] / 65535.0;
开发者ID:scorpion81,项目名称:blender-voro,代码行数:67,代码来源:png.c

示例13: fclose

/* Read an PNG file, returning the storage where it's located */
BitmapStorage *
BitmapIO_PNG::ReadPNGFile(BitmapInfo *fbi, BitmapManager *manager) 
{
    BitmapStorage *storage = NULL;
	unsigned char magic_numbers[8];
    
    if((istream=_tfopen(fbi->Name(), _T("rb")))==NULL)
		return NULL;

	// grab the first 8 bytes for testing
	if (fread(magic_numbers, 1, 8, istream) != 8) {
		fclose(istream);
		return NULL;
	} else 
		rewind(istream);

	// Make sure we're a png file
	if (!png_check_sig(magic_numbers, 8)) {
		fclose(istream);
		return NULL;
	}

    png = png_create_read_struct (PNG_VERSION, (void *) this, error_func, warning_func);
    if (setjmp(png->jmpbuf)) {
        if (info)
		    for (png_uint_32 i = 0; i < info->height; i++)
    			if (row_pointers[i]) free(row_pointers[i]);
		if (row_pointers) {
			free(row_pointers);
			row_pointers = NULL;
		}

		if (storage) {
			delete storage;
			storage = NULL;
		}

        fclose(istream);
        png_destroy_read_struct (&png, &info, NULL);
        return NULL;
    }
    info = png_create_info_struct(png);

    png_init_io(png, istream);
	png_read_info(png, info);

    fbi->SetWidth((WORD)info->width);
    fbi->SetHeight((WORD)info->height);
	if (info->valid & PNG_INFO_gAMA)
		fbi->SetGamma(info->gamma);
//	else
//		fbi->SetGamma (1.0f);
	if (info->valid & PNG_INFO_pHYs)
		fbi->SetAspect((float)info->x_pixels_per_unit / (float)info->y_pixels_per_unit);
	else
		fbi->SetAspect(1.0f);
    fbi->SetFlags(0);

	/* expand grayscale images to the full 8 bits */
	/* expand images with transparency to full alpha channels */
	/* I'm going to ignore lineart and just expand it to 8 bits */
	if ((info->color_type == PNG_COLOR_TYPE_PALETTE && info->bit_depth < 8) ||
		(info->color_type == PNG_COLOR_TYPE_GRAY && info->bit_depth < 8) ||
		(info->valid & PNG_INFO_tRNS))
		png_set_expand(png);

	int number_passes = 1;

	if (info->interlace_type)
		number_passes = png_set_interlace_handling(png);

	if (info->bit_depth == 16)
		png_set_swap(png);

	png_read_update_info(png, info);
   
	int bmtype = BMM_NO_TYPE;

	if (info->bit_depth == 1) {
			bmtype = BMM_LINE_ART;
	} else {
		switch(info->color_type) {
		case PNG_COLOR_TYPE_PALETTE:
			bmtype = BMM_PALETTED;
			break;
		case PNG_COLOR_TYPE_RGB:
		case PNG_COLOR_TYPE_RGB_ALPHA:
			switch(info->bit_depth) {
			case 2:
			case 4:
				// Not allowed
				break;
			case 8:
				bmtype = BMM_TRUE_32;  // zero alpha for those that don't have it
				break;
			case 16:
				bmtype = BMM_TRUE_64;
				break;
			}
//.........这里部分代码省略.........
开发者ID:2asoft,项目名称:xray,代码行数:101,代码来源:png.cpp

示例14: Load

static FIBITMAP * DLL_CALLCONV
Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
	png_structp png_ptr = NULL;
	png_infop info_ptr;
	png_uint_32 width, height;
	png_colorp png_palette = NULL;
	int color_type, palette_entries = 0;
	int bit_depth, pixel_depth;		// pixel_depth = bit_depth * channels

	FIBITMAP *dib = NULL;
	RGBQUAD *palette = NULL;		// pointer to dib palette
	png_bytepp  row_pointers = NULL;
	int i;

    fi_ioStructure fio;
    fio.s_handle = handle;
	fio.s_io = io;
    
	if (handle) {
		BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;

		try {		
			// check to see if the file is in fact a PNG file

			BYTE png_check[PNG_BYTES_TO_CHECK];

			io->read_proc(png_check, PNG_BYTES_TO_CHECK, 1, handle);

			if (png_sig_cmp(png_check, (png_size_t)0, PNG_BYTES_TO_CHECK) != 0) {
				return NULL;	// Bad signature
			}
			
			// create the chunk manage structure

			png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, error_handler, warning_handler);

			if (!png_ptr) {
				return NULL;			
			}

			// create the info structure

		    info_ptr = png_create_info_struct(png_ptr);

			if (!info_ptr) {
				png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
				return NULL;
			}

			// init the IO

			png_set_read_fn(png_ptr, &fio, _ReadProc);

            if (setjmp(png_jmpbuf(png_ptr))) {
				png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
				return NULL;
			}

			// because we have already read the signature...

			png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK);

			// read the IHDR chunk

			png_read_info(png_ptr, info_ptr);
			png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);

			pixel_depth = png_get_bit_depth(png_ptr, info_ptr) * png_get_channels(png_ptr, info_ptr);

			// get image data type (assume standard image type)

			FREE_IMAGE_TYPE image_type = FIT_BITMAP;
			if (bit_depth == 16) {
				if ((pixel_depth == 16) && (color_type == PNG_COLOR_TYPE_GRAY)) {
					image_type = FIT_UINT16;
				} 
				else if ((pixel_depth == 48) && (color_type == PNG_COLOR_TYPE_RGB)) {
					image_type = FIT_RGB16;
				} 
				else if ((pixel_depth == 64) && (color_type == PNG_COLOR_TYPE_RGB_ALPHA)) {
					image_type = FIT_RGBA16;
				} else {
					// tell libpng to strip 16 bit/color files down to 8 bits/color
					png_set_strip_16(png_ptr);
					bit_depth = 8;
				}
			}

#ifndef FREEIMAGE_BIGENDIAN
			if((image_type == FIT_UINT16) || (image_type == FIT_RGB16) || (image_type == FIT_RGBA16)) {
				// turn on 16 bit byte swapping
				png_set_swap(png_ptr);
			}
#endif						

			// set some additional flags

			switch(color_type) {
				case PNG_COLOR_TYPE_RGB:
				case PNG_COLOR_TYPE_RGB_ALPHA:
//.........这里部分代码省略.........
开发者ID:elfprince13,项目名称:G3D10,代码行数:101,代码来源:PluginPNG.cpp

示例15: _buffer

bool  PngDecoder::readData( Mat& img )
{
    bool result = false;
    AutoBuffer<uchar*> _buffer(m_height);
    uchar** buffer = _buffer;
    int color = img.channels() > 1;
    uchar* data = img.data;
    int step = (int)img.step;

    if( m_png_ptr && m_info_ptr && m_end_info && m_width && m_height )
    {
        png_structp png_ptr = (png_structp)m_png_ptr;
        png_infop info_ptr = (png_infop)m_info_ptr;
        png_infop end_info = (png_infop)m_end_info;

        if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 )
        {
            int y;

            if( img.depth() == CV_8U && m_bit_depth == 16 )
                png_set_strip_16( png_ptr );
            else if( !isBigEndian() )
                png_set_swap( png_ptr );

            if(img.channels() < 4)
            {
                /* observation: png_read_image() writes 400 bytes beyond
                 * end of data when reading a 400x118 color png
                 * "mpplus_sand.png".  OpenCV crashes even with demo
                 * programs.  Looking at the loaded image I'd say we get 4
                 * bytes per pixel instead of 3 bytes per pixel.  Test
                 * indicate that it is a good idea to always ask for
                 * stripping alpha..  18.11.2004 Axel Walthelm
                 */
                 png_set_strip_alpha( png_ptr );
            }

            if( m_color_type == PNG_COLOR_TYPE_PALETTE )
                png_set_palette_to_rgb( png_ptr );

            if( m_color_type == PNG_COLOR_TYPE_GRAY && m_bit_depth < 8 )
#if (PNG_LIBPNG_VER_MAJOR*10000 + PNG_LIBPNG_VER_MINOR*100 + PNG_LIBPNG_VER_RELEASE >= 10209) || \
    (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR == 0 && PNG_LIBPNG_VER_RELEASE >= 18)
                png_set_expand_gray_1_2_4_to_8( png_ptr );
#else
                png_set_gray_1_2_4_to_8( png_ptr );
#endif

            if( CV_MAT_CN(m_type) > 1 && color )
                png_set_bgr( png_ptr ); // convert RGB to BGR
            else if( color )
                png_set_gray_to_rgb( png_ptr ); // Gray->RGB
            else
                png_set_rgb_to_gray( png_ptr, 1, 0.299, 0.587 ); // RGB->Gray

            png_read_update_info( png_ptr, info_ptr );

            for( y = 0; y < m_height; y++ )
                buffer[y] = data + y*step;

            png_read_image( png_ptr, buffer );
            png_read_end( png_ptr, end_info );

            result = true;
        }
    }

    close();
    return result;
}
开发者ID:2693,项目名称:opencv,代码行数:70,代码来源:grfmt_png.cpp


注:本文中的png_set_swap函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。